2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2018-03-12 01:41:33 +01:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2018-11-09 16:46:48 +00:00
|
|
|
#include "testutil_unity.hpp"
|
2018-03-12 01:41:33 +01:00
|
|
|
#include <unity.h>
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <string.h>
|
2018-03-12 01:41:33 +01:00
|
|
|
|
|
|
|
void setUp ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void tearDown ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_app_meta_reqrep ()
|
|
|
|
{
|
|
|
|
void *ctx;
|
|
|
|
zmq_msg_t msg;
|
|
|
|
void *rep_sock, *req_sock;
|
2018-11-09 16:46:48 +00:00
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
2018-03-12 01:41:33 +01:00
|
|
|
const char *req_hello = "X-hello:hello";
|
|
|
|
const char *req_connection = "X-connection:primary";
|
|
|
|
const char *req_z85 = "X-bin:009c6";
|
|
|
|
const char *rep_hello = "X-hello:world";
|
|
|
|
const char *rep_connection = "X-connection:backup";
|
|
|
|
const char *bad_strings[] = {
|
|
|
|
":",
|
|
|
|
"key:",
|
|
|
|
":value",
|
|
|
|
"keyvalue",
|
|
|
|
"",
|
|
|
|
"X-"
|
|
|
|
"KeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKe"
|
|
|
|
"yTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyT"
|
|
|
|
"ooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyToo"
|
|
|
|
"LongKeyTooLongKeyTooLongKeyTooLongKeyTooLong:value"};
|
|
|
|
|
|
|
|
ctx = zmq_ctx_new ();
|
|
|
|
rep_sock = zmq_socket (ctx, ZMQ_REP);
|
|
|
|
TEST_ASSERT_NOT_NULL (rep_sock);
|
|
|
|
req_sock = zmq_socket (ctx, ZMQ_REQ);
|
|
|
|
TEST_ASSERT_NOT_NULL (req_sock);
|
|
|
|
|
|
|
|
int rc =
|
|
|
|
zmq_setsockopt (rep_sock, ZMQ_METADATA, rep_hello, strlen (rep_hello));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
int l = 0;
|
|
|
|
rc = zmq_setsockopt (rep_sock, ZMQ_LINGER, &l, sizeof (l));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_setsockopt (rep_sock, ZMQ_METADATA, rep_connection,
|
|
|
|
strlen (rep_connection));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
rc = zmq_setsockopt (rep_sock, ZMQ_METADATA, bad_strings[i],
|
|
|
|
strlen (bad_strings[i]));
|
|
|
|
TEST_ASSERT_EQUAL_INT (-1, rc);
|
|
|
|
}
|
|
|
|
|
2019-03-24 12:35:19 -04:00
|
|
|
bind_loopback_ipv4 (rep_sock, connect_address, sizeof connect_address);
|
2018-03-12 01:41:33 +01:00
|
|
|
|
|
|
|
l = 0;
|
|
|
|
rc = zmq_setsockopt (req_sock, ZMQ_LINGER, &l, sizeof (l));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_setsockopt (req_sock, ZMQ_METADATA, req_hello, strlen (req_hello));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_setsockopt (req_sock, ZMQ_METADATA, req_connection,
|
|
|
|
strlen (req_connection));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_setsockopt (req_sock, ZMQ_METADATA, req_z85, strlen (req_z85));
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
2018-11-09 16:46:48 +00:00
|
|
|
rc = zmq_connect (req_sock, connect_address);
|
2018-03-12 01:41:33 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_msg_init_size (&msg, 1);
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
2019-12-08 14:26:57 +01:00
|
|
|
char *data = static_cast<char *> (zmq_msg_data (&msg));
|
2018-03-12 01:41:33 +01:00
|
|
|
data[0] = 1;
|
|
|
|
|
|
|
|
rc = zmq_msg_send (&msg, req_sock, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (1, rc);
|
|
|
|
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_msg_recv (&msg, rep_sock, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (1, rc);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("hello", zmq_msg_gets (&msg, "X-hello"));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("primary", zmq_msg_gets (&msg, "X-connection"));
|
2020-02-04 12:37:31 +01:00
|
|
|
const char *const bindata = zmq_msg_gets (&msg, "X-bin");
|
2018-03-12 01:41:33 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (bindata);
|
|
|
|
uint8_t rawdata[4];
|
2020-02-04 12:37:31 +01:00
|
|
|
const uint8_t *const ret = zmq_z85_decode (rawdata, bindata);
|
2018-03-12 01:41:33 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (ret);
|
|
|
|
TEST_ASSERT_EQUAL_UINT8 (0, rawdata[0]);
|
|
|
|
TEST_ASSERT_EQUAL_UINT8 (1, rawdata[1]);
|
|
|
|
TEST_ASSERT_EQUAL_UINT8 (2, rawdata[2]);
|
|
|
|
TEST_ASSERT_EQUAL_UINT8 (3, rawdata[3]);
|
|
|
|
|
|
|
|
TEST_ASSERT_NULL (zmq_msg_gets (&msg, "X-foobar"));
|
|
|
|
TEST_ASSERT_NULL (zmq_msg_gets (&msg, "foobar"));
|
|
|
|
|
|
|
|
rc = zmq_msg_send (&msg, rep_sock, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (1, rc);
|
|
|
|
|
|
|
|
rc = zmq_msg_recv (&msg, req_sock, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (1, rc);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("world", zmq_msg_gets (&msg, "X-hello"));
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("backup", zmq_msg_gets (&msg, "X-connection"));
|
|
|
|
|
|
|
|
rc = zmq_msg_close (&msg);
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_close (req_sock);
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
rc = zmq_close (rep_sock);
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
|
|
|
|
zmq_ctx_term (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_app_meta_reqrep);
|
|
|
|
|
|
|
|
return UNITY_END ();
|
|
|
|
}
|