test_stream.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #include <string.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. // ZMTP protocol greeting structure
  29. typedef uint8_t byte;
  30. typedef struct
  31. {
  32. byte signature[10]; // 0xFF 8*0x00 0x7F
  33. byte version[2]; // 0x03 0x01 for ZMTP/3.1
  34. byte mechanism[20]; // "NULL"
  35. byte as_server;
  36. byte filler[31];
  37. } zmtp_greeting_t;
  38. #define ZMTP_DEALER 5 // Socket type constants
  39. // This is a greeting matching what 0MQ will send us; note the
  40. // 8-byte size is set to 1 for backwards compatibility
  41. static zmtp_greeting_t greeting = {
  42. {0xFF, 0, 0, 0, 0, 0, 0, 0, 1, 0x7F}, {3, 1}, {'N', 'U', 'L', 'L'}, 0, {0}};
  43. static void test_stream_to_dealer ()
  44. {
  45. int rc;
  46. char my_endpoint[MAX_SOCKET_STRING];
  47. // We'll be using this socket in raw mode
  48. void *stream = test_context_socket (ZMQ_STREAM);
  49. int zero = 0;
  50. TEST_ASSERT_SUCCESS_ERRNO (
  51. zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
  52. int enabled = 1;
  53. TEST_ASSERT_SUCCESS_ERRNO (
  54. zmq_setsockopt (stream, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
  55. bind_loopback_ipv4 (stream, my_endpoint, sizeof my_endpoint);
  56. // We'll be using this socket as the other peer
  57. void *dealer = test_context_socket (ZMQ_DEALER);
  58. TEST_ASSERT_SUCCESS_ERRNO (
  59. zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
  60. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
  61. // Send a message on the dealer socket
  62. send_string_expect_success (dealer, "Hello", 0);
  63. // Connecting sends a zero message
  64. // First frame is routing id
  65. zmq_msg_t routing_id;
  66. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&routing_id));
  67. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0));
  68. TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
  69. // Verify the existence of Peer-Address metadata
  70. char const *peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
  71. TEST_ASSERT_NOT_NULL (peer_address);
  72. TEST_ASSERT_EQUAL_STRING ("127.0.0.1", peer_address);
  73. // Second frame is zero
  74. byte buffer[255];
  75. TEST_ASSERT_EQUAL_INT (
  76. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (stream, buffer, 255, 0)));
  77. // Verify the existence of Peer-Address metadata
  78. peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
  79. TEST_ASSERT_NOT_NULL (peer_address);
  80. TEST_ASSERT_EQUAL_STRING ("127.0.0.1", peer_address);
  81. // Real data follows
  82. // First frame is routing id
  83. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0));
  84. TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
  85. // Verify the existence of Peer-Address metadata
  86. peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
  87. TEST_ASSERT_NOT_NULL (peer_address);
  88. TEST_ASSERT_EQUAL_STRING ("127.0.0.1", peer_address);
  89. // Second frame is greeting signature
  90. recv_array_expect_success (stream, greeting.signature, 0);
  91. // Send our own protocol greeting
  92. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&routing_id, stream, ZMQ_SNDMORE));
  93. TEST_ASSERT_EQUAL_INT (
  94. sizeof (greeting), TEST_ASSERT_SUCCESS_ERRNO (
  95. zmq_send (stream, &greeting, sizeof (greeting), 0)));
  96. // Now we expect the data from the DEALER socket
  97. // We want the rest of greeting along with the Ready command
  98. int bytes_read = 0;
  99. while (bytes_read < 97) {
  100. // First frame is the routing id of the connection (each time)
  101. TEST_ASSERT_GREATER_THAN_INT (
  102. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0)));
  103. TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
  104. // Second frame contains the next chunk of data
  105. TEST_ASSERT_SUCCESS_ERRNO (
  106. rc = zmq_recv (stream, buffer + bytes_read, 255 - bytes_read, 0));
  107. bytes_read += rc;
  108. }
  109. // First two bytes are major and minor version numbers.
  110. TEST_ASSERT_EQUAL_INT (3, buffer[0]); // ZMTP/3.1
  111. TEST_ASSERT_EQUAL_INT (1, buffer[1]);
  112. // Mechanism is "NULL"
  113. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 2,
  114. "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20);
  115. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 54, "\4\51\5READY", 8);
  116. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 62, "\13Socket-Type\0\0\0\6DEALER",
  117. 22);
  118. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 84, "\10Identity\0\0\0\0", 13);
  119. // Announce we are ready
  120. memcpy (buffer, "\4\51\5READY", 8);
  121. memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22);
  122. memcpy (buffer + 30, "\10Identity\0\0\0\0", 13);
  123. // Send Ready command
  124. TEST_ASSERT_GREATER_THAN_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (
  125. &routing_id, stream, ZMQ_SNDMORE)));
  126. TEST_ASSERT_EQUAL_INT (
  127. 43, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (stream, buffer, 43, 0)));
  128. // Now we expect the data from the DEALER socket
  129. // First frame is, again, the routing id of the connection
  130. TEST_ASSERT_GREATER_THAN_INT (
  131. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&routing_id, stream, 0)));
  132. TEST_ASSERT_TRUE (zmq_msg_more (&routing_id));
  133. // Third frame contains Hello message from DEALER
  134. TEST_ASSERT_EQUAL_INT (7, TEST_ASSERT_SUCCESS_ERRNO (
  135. zmq_recv (stream, buffer, sizeof buffer, 0)));
  136. // Then we have a 5-byte message "Hello"
  137. TEST_ASSERT_EQUAL_INT (0, buffer[0]); // Flags = 0
  138. TEST_ASSERT_EQUAL_INT (5, buffer[1]); // Size = 5
  139. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer + 2, "Hello", 5);
  140. // Send "World" back to DEALER
  141. TEST_ASSERT_GREATER_THAN_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (
  142. &routing_id, stream, ZMQ_SNDMORE)));
  143. byte world[] = {0, 5, 'W', 'o', 'r', 'l', 'd'};
  144. TEST_ASSERT_EQUAL_INT (
  145. sizeof (world),
  146. TEST_ASSERT_SUCCESS_ERRNO (zmq_send (stream, world, sizeof (world), 0)));
  147. // Expect response on DEALER socket
  148. recv_string_expect_success (dealer, "World", 0);
  149. // Test large messages over STREAM socket
  150. #define size 64000
  151. uint8_t msgout[size];
  152. memset (msgout, 0xAB, size);
  153. zmq_send (dealer, msgout, size, 0);
  154. uint8_t msgin[9 + size];
  155. memset (msgin, 0, 9 + size);
  156. bytes_read = 0;
  157. while (bytes_read < 9 + size) {
  158. // Get routing id frame
  159. TEST_ASSERT_GREATER_THAN_INT (
  160. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (stream, buffer, 256, 0)));
  161. // Get next chunk
  162. TEST_ASSERT_GREATER_THAN_INT (
  163. 0,
  164. TEST_ASSERT_SUCCESS_ERRNO (rc = zmq_recv (stream, msgin + bytes_read,
  165. 9 + size - bytes_read, 0)));
  166. bytes_read += rc;
  167. }
  168. for (int byte_nbr = 0; byte_nbr < size; byte_nbr++) {
  169. TEST_ASSERT_EQUAL_UINT8 (0xAB, msgin[9 + byte_nbr]);
  170. }
  171. test_context_socket_close (dealer);
  172. test_context_socket_close (stream);
  173. }
  174. static void test_stream_to_stream ()
  175. {
  176. char my_endpoint[MAX_SOCKET_STRING];
  177. // Set-up our context and sockets
  178. void *server = test_context_socket (ZMQ_STREAM);
  179. int enabled = 1;
  180. TEST_ASSERT_SUCCESS_ERRNO (
  181. zmq_setsockopt (server, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
  182. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  183. void *client = test_context_socket (ZMQ_STREAM);
  184. TEST_ASSERT_SUCCESS_ERRNO (
  185. zmq_setsockopt (client, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
  186. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  187. uint8_t id[256];
  188. uint8_t buffer[256];
  189. // Connecting sends a zero message
  190. // Server: First frame is routing id, second frame is zero
  191. TEST_ASSERT_GREATER_THAN_INT (
  192. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, id, 256, 0)));
  193. TEST_ASSERT_EQUAL_INT (
  194. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, buffer, 256, 0)));
  195. // Client: First frame is routing id, second frame is zero
  196. TEST_ASSERT_GREATER_THAN_INT (
  197. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, id, 256, 0)));
  198. TEST_ASSERT_EQUAL_INT (
  199. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, buffer, 256, 0)));
  200. // Sent HTTP request on client socket
  201. // Get server routing id
  202. size_t id_size = sizeof id;
  203. TEST_ASSERT_SUCCESS_ERRNO (
  204. zmq_getsockopt (client, ZMQ_ROUTING_ID, id, &id_size));
  205. // First frame is server routing id
  206. TEST_ASSERT_EQUAL_INT ((int) id_size, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (
  207. client, id, id_size, ZMQ_SNDMORE)));
  208. // Second frame is HTTP GET request
  209. TEST_ASSERT_EQUAL_INT (
  210. 7, TEST_ASSERT_SUCCESS_ERRNO (zmq_send (client, "GET /\n\n", 7, 0)));
  211. // Get HTTP request; ID frame and then request
  212. TEST_ASSERT_GREATER_THAN_INT (
  213. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, id, 256, 0)));
  214. TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, buffer, 256, 0));
  215. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer, "GET /\n\n", 7);
  216. // Send reply back to client
  217. char http_response[] = "HTTP/1.0 200 OK\r\n"
  218. "Content-Type: text/plain\r\n"
  219. "\r\n"
  220. "Hello, World!";
  221. TEST_ASSERT_SUCCESS_ERRNO (zmq_send (server, id, id_size, ZMQ_SNDMORE));
  222. TEST_ASSERT_SUCCESS_ERRNO (
  223. zmq_send (server, http_response, sizeof (http_response), ZMQ_SNDMORE));
  224. // Send zero to close connection to client
  225. TEST_ASSERT_SUCCESS_ERRNO (zmq_send (server, id, id_size, ZMQ_SNDMORE));
  226. TEST_ASSERT_SUCCESS_ERRNO (zmq_send (server, NULL, 0, ZMQ_SNDMORE));
  227. // Get reply at client and check that it's complete
  228. TEST_ASSERT_GREATER_THAN_INT (
  229. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, id, 256, 0)));
  230. TEST_ASSERT_EQUAL_INT (
  231. sizeof http_response,
  232. TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, buffer, 256, 0)));
  233. TEST_ASSERT_EQUAL_INT8_ARRAY (buffer, http_response,
  234. sizeof (http_response));
  235. // // Get disconnection notification
  236. // FIXME: why does this block? Bug in STREAM disconnect notification?
  237. // id_size = zmq_recv (client, id, 256, 0);
  238. // assert (id_size > 0);
  239. // rc = zmq_recv (client, buffer, 256, 0);
  240. // assert (rc == 0);
  241. test_context_socket_close (server);
  242. test_context_socket_close (client);
  243. }
  244. int main ()
  245. {
  246. setup_test_environment ();
  247. UNITY_BEGIN ();
  248. RUN_TEST (test_stream_to_dealer);
  249. RUN_TEST (test_stream_to_stream);
  250. return UNITY_END ();
  251. }