2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2014-05-02 22:19:30 +02:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2018-12-12 05:27:50 -05:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <stdlib.h>
|
2018-12-12 05:27:50 -05:00
|
|
|
#include <unity.h>
|
|
|
|
|
|
|
|
void setUp ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown ()
|
|
|
|
{
|
|
|
|
}
|
2014-05-02 22:19:30 +02:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
static void zap_handler (void *handler_)
|
2014-05-02 22:19:30 +02:00
|
|
|
{
|
|
|
|
uint8_t metadata[] = {5, 'H', 'e', 'l', 'l', 'o', 0, 0,
|
|
|
|
0, 5, 'W', 'o', 'r', 'l', 'd'};
|
|
|
|
|
|
|
|
// Process ZAP requests forever
|
|
|
|
while (true) {
|
2018-05-27 07:01:36 -04:00
|
|
|
char *version = s_recv (handler_);
|
2014-05-02 22:19:30 +02:00
|
|
|
if (!version)
|
|
|
|
break; // Terminating
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
char *sequence = s_recv (handler_);
|
|
|
|
char *domain = s_recv (handler_);
|
|
|
|
char *address = s_recv (handler_);
|
|
|
|
char *routing_id = s_recv (handler_);
|
|
|
|
char *mechanism = s_recv (handler_);
|
2014-05-02 22:19:30 +02:00
|
|
|
|
2019-03-23 07:53:38 -04:00
|
|
|
TEST_ASSERT_EQUAL_STRING ("1.0", version);
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
|
2015-01-16 14:52:16 -06:00
|
|
|
|
2019-03-24 13:02:50 -04:00
|
|
|
send_string_expect_success (handler_, version, ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (handler_, sequence, ZMQ_SNDMORE);
|
2014-05-02 22:19:30 +02:00
|
|
|
if (streq (domain, "DOMAIN")) {
|
2019-03-24 13:02:50 -04:00
|
|
|
send_string_expect_success (handler_, "200", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (handler_, "OK", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (handler_, "anonymous", ZMQ_SNDMORE);
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_send (handler_, metadata, sizeof (metadata), 0);
|
2014-05-02 22:19:30 +02:00
|
|
|
} else {
|
2019-03-24 13:02:50 -04:00
|
|
|
send_string_expect_success (handler_, "400", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (handler_, "BAD DOMAIN", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (handler_, "", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (handler_, "", 0);
|
2014-05-02 22:19:30 +02:00
|
|
|
}
|
|
|
|
free (version);
|
|
|
|
free (sequence);
|
|
|
|
free (domain);
|
|
|
|
free (address);
|
2017-09-07 11:21:13 +02:00
|
|
|
free (routing_id);
|
2014-05-02 22:19:30 +02:00
|
|
|
free (mechanism);
|
|
|
|
}
|
2018-05-27 07:01:36 -04:00
|
|
|
close_zero_linger (handler_);
|
2014-05-02 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
void test_metadata ()
|
2014-05-02 22:19:30 +02:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2018-12-12 05:27:50 -05:00
|
|
|
setup_test_context ();
|
2014-05-02 22:19:30 +02:00
|
|
|
|
|
|
|
// Spawn ZAP handler
|
|
|
|
// We create and bind ZAP socket in main thread to avoid case
|
|
|
|
// where child thread does not start up fast enough.
|
2018-12-12 05:27:50 -05:00
|
|
|
void *handler = zmq_socket (get_test_context (), ZMQ_REP);
|
|
|
|
TEST_ASSERT_NOT_NULL (handler);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
|
2014-05-02 22:19:30 +02:00
|
|
|
void *zap_thread = zmq_threadstart (&zap_handler, handler);
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
void *server = test_context_socket (ZMQ_DEALER);
|
|
|
|
void *client = test_context_socket (ZMQ_DEALER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "DOMAIN", 6));
|
|
|
|
bind_loopback_ipv4 (server, my_endpoint, sizeof (my_endpoint));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
|
2014-05-02 22:19:30 +02:00
|
|
|
|
2019-03-24 13:02:50 -04:00
|
|
|
send_string_expect_success (client, "This is a message", 0);
|
2014-05-02 22:19:30 +02:00
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
2018-12-12 05:27:50 -05:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, server, 0));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("World", zmq_msg_gets (&msg, "Hello"));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("DEALER", zmq_msg_gets (&msg, "Socket-Type"));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("anonymous", zmq_msg_gets (&msg, "User-Id"));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("127.0.0.1", zmq_msg_gets (&msg, "Peer-Address"));
|
|
|
|
|
|
|
|
TEST_ASSERT_NULL (zmq_msg_gets (&msg, "No Such"));
|
|
|
|
TEST_ASSERT_EQUAL_INT (EINVAL, zmq_errno ());
|
2014-05-02 22:19:30 +02:00
|
|
|
zmq_msg_close (&msg);
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
test_context_socket_close_zero_linger (client);
|
|
|
|
test_context_socket_close_zero_linger (server);
|
2014-05-02 22:19:30 +02:00
|
|
|
|
|
|
|
// Shutdown
|
2018-12-12 05:27:50 -05:00
|
|
|
teardown_test_context ();
|
2014-05-02 22:19:30 +02:00
|
|
|
|
|
|
|
// Wait until ZAP handler terminates
|
|
|
|
zmq_threadclose (zap_thread);
|
2018-12-12 05:27:50 -05:00
|
|
|
}
|
2014-05-02 22:19:30 +02:00
|
|
|
|
2021-12-16 10:15:55 +02:00
|
|
|
void test_router_prefetch_metadata ()
|
|
|
|
{
|
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
|
|
|
setup_test_context ();
|
|
|
|
|
|
|
|
// Spawn ZAP handler
|
|
|
|
// We create and bind ZAP socket in main thread to avoid case
|
|
|
|
// where child thread does not start up fast enough.
|
|
|
|
void *handler = zmq_socket (get_test_context (), ZMQ_REP);
|
|
|
|
TEST_ASSERT_NOT_NULL (handler);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
|
|
|
|
void *zap_thread = zmq_threadstart (&zap_handler, handler);
|
|
|
|
|
|
|
|
void *server = test_context_socket (ZMQ_ROUTER);
|
|
|
|
void *client = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "DOMAIN", 6));
|
|
|
|
bind_loopback_ipv4 (server, my_endpoint, sizeof (my_endpoint));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
|
|
|
|
|
|
|
|
send_string_expect_success (client, "This is a message", 0);
|
|
|
|
|
|
|
|
msleep (SETTLE_TIME);
|
|
|
|
|
|
|
|
// Check for messages in the ROUTER socket which will trigger a prefetch
|
|
|
|
unsigned long int dummy;
|
|
|
|
size_t dummy_size = sizeof (dummy);
|
|
|
|
zmq_getsockopt (server, ZMQ_EVENTS, &dummy, &dummy_size);
|
|
|
|
|
|
|
|
zmq_msg_t msg;
|
|
|
|
|
|
|
|
// Ensure all frames in the message contain metadata
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, server, 0));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("World", zmq_msg_gets (&msg, "Hello"));
|
|
|
|
zmq_msg_close (&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_context_socket_close_zero_linger (client);
|
|
|
|
test_context_socket_close_zero_linger (server);
|
|
|
|
|
|
|
|
// Shutdown
|
|
|
|
teardown_test_context ();
|
|
|
|
|
|
|
|
// Wait until ZAP handler terminates
|
|
|
|
zmq_threadclose (zap_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-12 05:27:50 -05:00
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_metadata);
|
2021-12-16 10:15:55 +02:00
|
|
|
RUN_TEST (test_router_prefetch_metadata);
|
2018-12-12 05:27:50 -05:00
|
|
|
return UNITY_END ();
|
2014-05-02 22:19:30 +02:00
|
|
|
}
|