2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-06-27 20:47:34 +02:00
|
|
|
|
2013-08-17 14:43:45 +02:00
|
|
|
#include "testutil.hpp"
|
2018-08-20 17:44:16 +02:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 13:04:57 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// ZMTP protocol greeting structure
|
|
|
|
|
2018-08-20 17:44:16 +02:00
|
|
|
typedef uint8_t byte;
|
2013-06-27 20:47:34 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
byte signature[10]; // 0xFF 8*0x00 0x7F
|
2018-05-31 00:53:29 +02:00
|
|
|
byte version[2]; // 0x03 0x01 for ZMTP/3.1
|
2013-06-27 20:47:34 +02:00
|
|
|
byte mechanism[20]; // "NULL"
|
|
|
|
byte as_server;
|
|
|
|
byte filler[31];
|
|
|
|
} zmtp_greeting_t;
|
|
|
|
|
|
|
|
#define ZMTP_DEALER 5 // Socket type constants
|
|
|
|
|
|
|
|
// This is a greeting matching what 0MQ will send us; note the
|
|
|
|
// 8-byte size is set to 1 for backwards compatibility
|
|
|
|
|
2015-07-19 16:11:22 +02:00
|
|
|
static zmtp_greeting_t greeting = {
|
2018-05-31 00:53:29 +02:00
|
|
|
{0xFF, 0, 0, 0, 0, 0, 0, 0, 1, 0x7F}, {3, 1}, {'N', 'U', 'L', 'L'}, 0, {0}};
|
2013-06-27 20:47:34 +02:00
|
|
|
|
2018-08-20 17:44:16 +02:00
|
|
|
static void test_stream_to_dealer ()
|
2013-06-27 20:47:34 +02:00
|
|
|
{
|
|
|
|
int rc;
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// We'll be using this socket in raw mode
|
2018-08-20 17:44:16 +02:00
|
|
|
void *stream = test_context_socket (ZMQ_STREAM);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
int zero = 0;
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
|
2015-01-23 15:25:40 +01:00
|
|
|
int enabled = 1;
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (stream, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
|
|
|
|
bind_loopback_ipv4 (stream, my_endpoint, sizeof my_endpoint);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// We'll be using this socket as the other peer
|
2018-08-20 17:44:16 +02:00
|
|
|
void *dealer = test_context_socket (ZMQ_DEALER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Send a message on the dealer socket
|
2018-08-20 17:44:16 +02:00
|
|
|
send_string_expect_success (dealer, "Hello", 0);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
2014-01-17 23:21:42 +01:00
|
|
|
// Connecting sends a zero message
|
2017-09-07 11:21:13 +02:00
|
|
|
// First frame is routing id
|
|
|
|
zmq_msg_t routing_id;
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&routing_id));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0));
|
|
|
|
TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
2015-07-19 16:11:22 +02:00
|
|
|
// Verify the existence of Peer-Address metadata
|
2017-09-07 11:21:13 +02:00
|
|
|
char const *peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_NOT_NULL (peer_address);
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("127.0.0.1", peer_address);
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2014-01-17 23:21:42 +01:00
|
|
|
// Second frame is zero
|
2013-06-27 20:47:34 +02:00
|
|
|
byte buffer[255];
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (stream, buffer, 255, 0)));
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2015-07-19 16:11:22 +02:00
|
|
|
// Verify the existence of Peer-Address metadata
|
2017-09-07 11:21:13 +02:00
|
|
|
peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_NOT_NULL (peer_address);
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("127.0.0.1", peer_address);
|
2015-02-19 20:22:32 +01:00
|
|
|
|
2014-01-17 23:21:42 +01:00
|
|
|
// Real data follows
|
2017-09-07 11:21:13 +02:00
|
|
|
// First frame is routing id
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0));
|
|
|
|
TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
2015-07-19 16:11:22 +02:00
|
|
|
// Verify the existence of Peer-Address metadata
|
2017-09-07 11:21:13 +02:00
|
|
|
peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_NOT_NULL (peer_address);
|
|
|
|
TEST_ASSERT_EQUAL_STRING ("127.0.0.1", peer_address);
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2015-02-18 20:05:05 +01:00
|
|
|
// Second frame is greeting signature
|
2018-08-20 17:44:16 +02:00
|
|
|
recv_array_expect_success (stream, greeting.signature, 0);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Send our own protocol greeting
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&routing_id, stream, ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
sizeof (greeting), TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_send (stream, &greeting, sizeof (greeting), 0)));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Now we expect the data from the DEALER socket
|
2013-10-23 23:58:39 +02:00
|
|
|
// We want the rest of greeting along with the Ready command
|
2013-09-06 09:22:09 +02:00
|
|
|
int bytes_read = 0;
|
|
|
|
while (bytes_read < 97) {
|
2017-09-07 11:21:13 +02:00
|
|
|
// First frame is the routing id of the connection (each time)
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0)));
|
|
|
|
TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
|
2013-10-23 23:58:39 +02:00
|
|
|
// Second frame contains the next chunk of data
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
rc = zmq_recv (stream, buffer + bytes_read, 255 - bytes_read, 0));
|
2013-09-06 09:22:09 +02:00
|
|
|
bytes_read += rc;
|
|
|
|
}
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// First two bytes are major and minor version numbers.
|
2018-05-31 00:53:29 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (3, buffer[0]); // ZMTP/3.1
|
|
|
|
TEST_ASSERT_EQUAL_INT (1, buffer[1]);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Mechanism is "NULL"
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 2,
|
|
|
|
"NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20);
|
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 54, "\4\51\5READY", 8);
|
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 62, "\13Socket-Type\0\0\0\6DEALER",
|
|
|
|
22);
|
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 84, "\10Identity\0\0\0\0", 13);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Announce we are ready
|
2013-09-04 17:59:45 +02:00
|
|
|
memcpy (buffer, "\4\51\5READY", 8);
|
2013-07-01 10:04:54 +02:00
|
|
|
memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22);
|
2013-06-28 11:42:54 +02:00
|
|
|
memcpy (buffer + 30, "\10Identity\0\0\0\0", 13);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Send Ready command
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (
|
|
|
|
&routing_id, stream, ZMQ_SNDMORE)));
|
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
43, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (stream, buffer, 43, 0)));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Now we expect the data from the DEALER socket
|
2017-09-07 11:21:13 +02:00
|
|
|
// First frame is, again, the routing id of the connection
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0)));
|
|
|
|
TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Third frame contains Hello message from DEALER
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (7, TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_recv (stream, buffer, sizeof buffer, 0)));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Then we have a 5-byte message "Hello"
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, buffer[0]); // Flags = 0
|
|
|
|
TEST_ASSERT_EQUAL_INT (5, buffer[1]); // Size = 5
|
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 2, "Hello", 5);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Send "World" back to DEALER
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (
|
|
|
|
&routing_id, stream, ZMQ_SNDMORE)));
|
2013-06-27 20:47:34 +02:00
|
|
|
byte world[] = {0, 5, 'W', 'o', 'r', 'l', 'd'};
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
sizeof (world),
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_send (stream, world, sizeof (world), 0)));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Expect response on DEALER socket
|
2018-08-20 17:44:16 +02:00
|
|
|
recv_string_expect_success (dealer, "World", 0);
|
2013-06-27 20:47:34 +02:00
|
|
|
|
2015-07-19 16:11:22 +02:00
|
|
|
// Test large messages over STREAM socket
|
2015-07-20 09:04:40 +02:00
|
|
|
#define size 64000
|
2015-07-19 16:11:22 +02:00
|
|
|
uint8_t msgout[size];
|
|
|
|
memset (msgout, 0xAB, size);
|
|
|
|
zmq_send (dealer, msgout, size, 0);
|
|
|
|
|
|
|
|
uint8_t msgin[9 + size];
|
|
|
|
memset (msgin, 0, 9 + size);
|
|
|
|
bytes_read = 0;
|
|
|
|
while (bytes_read < 9 + size) {
|
2017-09-07 11:21:13 +02:00
|
|
|
// Get routing id frame
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (stream, buffer, 256, 0)));
|
2015-07-19 16:11:22 +02:00
|
|
|
// Get next chunk
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0,
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (rc = zmq_recv (stream, msgin + bytes_read,
|
|
|
|
9 + size - bytes_read, 0)));
|
2015-07-19 16:11:22 +02:00
|
|
|
bytes_read += rc;
|
|
|
|
}
|
2018-08-20 17:44:16 +02:00
|
|
|
for (int byte_nbr = 0; byte_nbr < size; byte_nbr++) {
|
|
|
|
TEST_ASSERT_EQUAL_UINT8 (0xAB, msgin[9 + byte_nbr]);
|
2015-07-19 16:11:22 +02:00
|
|
|
}
|
2018-08-20 17:44:16 +02:00
|
|
|
test_context_socket_close (dealer);
|
|
|
|
test_context_socket_close (stream);
|
2013-06-27 20:47:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-20 17:44:16 +02:00
|
|
|
static void test_stream_to_stream ()
|
2013-06-27 20:47:34 +02:00
|
|
|
{
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2013-06-27 20:47:34 +02:00
|
|
|
// Set-up our context and sockets
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2018-08-20 17:44:16 +02:00
|
|
|
void *server = test_context_socket (ZMQ_STREAM);
|
2015-01-23 15:25:40 +01:00
|
|
|
int enabled = 1;
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (server, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
|
|
|
|
bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
|
|
|
|
|
|
|
|
void *client = test_context_socket (ZMQ_STREAM);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (client, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
|
2013-06-27 20:47:34 +02:00
|
|
|
uint8_t id[256];
|
2014-01-17 23:21:42 +01:00
|
|
|
uint8_t buffer[256];
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2014-01-17 23:21:42 +01:00
|
|
|
// Connecting sends a zero message
|
2017-09-07 11:21:13 +02:00
|
|
|
// Server: First frame is routing id, second frame is zero
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, id, 256, 0)));
|
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, buffer, 256, 0)));
|
2017-09-07 11:21:13 +02:00
|
|
|
// Client: First frame is routing id, second frame is zero
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, id, 256, 0)));
|
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, buffer, 256, 0)));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
2013-06-27 20:47:34 +02:00
|
|
|
// Sent HTTP request on client socket
|
2017-09-07 11:21:13 +02:00
|
|
|
// Get server routing id
|
2018-08-20 17:44:16 +02:00
|
|
|
size_t id_size = sizeof id;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (client, ZMQ_ROUTING_ID, id, &id_size));
|
2017-09-07 11:21:13 +02:00
|
|
|
// First frame is server routing id
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT ((int) id_size, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (
|
|
|
|
client, id, id_size, ZMQ_SNDMORE)));
|
2013-06-27 20:47:34 +02:00
|
|
|
// Second frame is HTTP GET request
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
7, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (client, "GET /\n\n", 7, 0)));
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2013-06-27 20:47:34 +02:00
|
|
|
// Get HTTP request; ID frame and then request
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, id, 256, 0)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, buffer, 256, 0));
|
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer, "GET /\n\n", 7);
|
2015-02-18 19:28:58 +01:00
|
|
|
|
2013-06-27 20:47:34 +02:00
|
|
|
// Send reply back to client
|
|
|
|
char http_response[] = "HTTP/1.0 200 OK\r\n"
|
2015-02-18 19:28:58 +01:00
|
|
|
"Content-Type: text/plain\r\n"
|
|
|
|
"\r\n"
|
2013-06-27 20:47:34 +02:00
|
|
|
"Hello, World!";
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_send (server, id, id_size, ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_send (server, http_response, sizeof (http_response), ZMQ_SNDMORE));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
|
|
|
// Send zero to close connection to client
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_send (server, id, id_size, ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_send (server, NULL, 0, ZMQ_SNDMORE));
|
2013-06-27 20:47:34 +02:00
|
|
|
|
|
|
|
// Get reply at client and check that it's complete
|
2018-08-20 17:44:16 +02:00
|
|
|
TEST_ASSERT_GREATER_THAN_INT (
|
|
|
|
0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, id, 256, 0)));
|
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
sizeof http_response,
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, buffer, 256, 0)));
|
|
|
|
TEST_ASSERT_EQUAL_INT8_ARRAY (buffer, http_response,
|
|
|
|
sizeof (http_response));
|
2014-01-17 23:21:42 +01:00
|
|
|
|
|
|
|
// // Get disconnection notification
|
|
|
|
// FIXME: why does this block? Bug in STREAM disconnect notification?
|
|
|
|
// id_size = zmq_recv (client, id, 256, 0);
|
|
|
|
// assert (id_size > 0);
|
|
|
|
// rc = zmq_recv (client, buffer, 256, 0);
|
|
|
|
// assert (rc == 0);
|
|
|
|
|
2018-08-20 17:44:16 +02:00
|
|
|
test_context_socket_close (server);
|
|
|
|
test_context_socket_close (client);
|
2013-06-27 20:47:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 17:44:16 +02:00
|
|
|
int main ()
|
2013-06-27 20:47:34 +02:00
|
|
|
{
|
2013-08-17 14:43:45 +02:00
|
|
|
setup_test_environment ();
|
2018-08-20 17:44:16 +02:00
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_stream_to_dealer);
|
|
|
|
RUN_TEST (test_stream_to_stream);
|
|
|
|
return UNITY_END ();
|
2013-07-01 10:04:54 +02:00
|
|
|
}
|