test_connect_rid.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. const char *rconn1routing_id = "conn1";
  29. const char *x_routing_id = "X";
  30. const char *y_routing_id = "Y";
  31. const char *z_routing_id = "Z";
  32. void test_stream_2_stream ()
  33. {
  34. char buff[256];
  35. const char msg[] = "hi 1";
  36. const int disabled = 0;
  37. const int zero = 0;
  38. char my_endpoint[MAX_SOCKET_STRING];
  39. // Set up listener STREAM.
  40. void *rbind = test_context_socket (ZMQ_STREAM);
  41. TEST_ASSERT_SUCCESS_ERRNO (
  42. zmq_setsockopt (rbind, ZMQ_STREAM_NOTIFY, &disabled, sizeof (disabled)));
  43. TEST_ASSERT_SUCCESS_ERRNO (
  44. zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof zero));
  45. bind_loopback_ipv4 (rbind, my_endpoint, sizeof my_endpoint);
  46. // Set up connection stream.
  47. void *rconn1 = test_context_socket (ZMQ_STREAM);
  48. TEST_ASSERT_SUCCESS_ERRNO (
  49. zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof zero));
  50. // Do the connection.
  51. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID,
  52. rconn1routing_id,
  53. strlen (rconn1routing_id)));
  54. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, my_endpoint));
  55. /* Uncomment to test assert on duplicate routing id.
  56. // Test duplicate connect attempt.
  57. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, rconn1routing_id, strlen(rconn1routing_id)));
  58. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, bindip));
  59. */
  60. // Send data to the bound stream.
  61. send_string_expect_success (rconn1, rconn1routing_id, ZMQ_SNDMORE);
  62. send_string_expect_success (rconn1, msg, 0);
  63. // Accept data on the bound stream.
  64. TEST_ASSERT_GREATER_THAN (
  65. 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (rbind, buff, 256, 0)));
  66. TEST_ASSERT_EQUAL (0, buff[0]); // an auto-generated routing id
  67. recv_string_expect_success (rbind, msg, 0);
  68. // Handle close of the socket.
  69. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (rbind, my_endpoint));
  70. test_context_socket_close (rbind);
  71. test_context_socket_close (rconn1);
  72. }
  73. void test_router_2_router (bool named_)
  74. {
  75. char buff[256];
  76. const char msg[] = "hi 1";
  77. const int zero = 0;
  78. char my_endpoint[MAX_SOCKET_STRING];
  79. // Create bind socket.
  80. void *rbind = test_context_socket (ZMQ_ROUTER);
  81. TEST_ASSERT_SUCCESS_ERRNO (
  82. zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero)));
  83. bind_loopback_ipv4 (rbind, my_endpoint, sizeof my_endpoint);
  84. // Create connection socket.
  85. void *rconn1 = test_context_socket (ZMQ_ROUTER);
  86. TEST_ASSERT_SUCCESS_ERRNO (
  87. zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero)));
  88. // If we're in named mode, set some identities.
  89. if (named_) {
  90. TEST_ASSERT_SUCCESS_ERRNO (
  91. zmq_setsockopt (rbind, ZMQ_ROUTING_ID, x_routing_id, 1));
  92. TEST_ASSERT_SUCCESS_ERRNO (
  93. zmq_setsockopt (rconn1, ZMQ_ROUTING_ID, y_routing_id, 1));
  94. }
  95. // Make call to connect using a connect_routing_id.
  96. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID,
  97. rconn1routing_id,
  98. strlen (rconn1routing_id)));
  99. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, my_endpoint));
  100. /* Uncomment to test assert on duplicate routing id
  101. // Test duplicate connect attempt.
  102. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, rconn1routing_id, strlen (rconn1routing_id)));
  103. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, bindip));
  104. */
  105. // Send some data.
  106. send_string_expect_success (rconn1, rconn1routing_id, ZMQ_SNDMORE);
  107. send_string_expect_success (rconn1, msg, 0);
  108. // Receive the name.
  109. const int routing_id_len = zmq_recv (rbind, buff, 256, 0);
  110. if (named_) {
  111. TEST_ASSERT_EQUAL_INT (strlen (y_routing_id), routing_id_len);
  112. TEST_ASSERT_EQUAL_STRING_LEN (y_routing_id, buff, routing_id_len);
  113. } else {
  114. TEST_ASSERT_TRUE (routing_id_len && 0 == buff[0]);
  115. }
  116. // Receive the data.
  117. recv_string_expect_success (rbind, msg, 0);
  118. // Send some data back.
  119. const int ret = zmq_send (rbind, buff, routing_id_len, ZMQ_SNDMORE);
  120. TEST_ASSERT_EQUAL_INT (routing_id_len, ret);
  121. send_string_expect_success (rbind, "ok", 0);
  122. // If bound socket identity naming a problem, we'll likely see something funky here.
  123. recv_string_expect_success (rconn1, rconn1routing_id, 0);
  124. recv_string_expect_success (rconn1, "ok", 0);
  125. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (rbind, my_endpoint));
  126. test_context_socket_close (rbind);
  127. test_context_socket_close (rconn1);
  128. }
  129. void test_router_2_router_while_receiving ()
  130. {
  131. char buff[256];
  132. const char msg[] = "hi 1";
  133. const int zero = 0;
  134. char x_endpoint[MAX_SOCKET_STRING];
  135. char z_endpoint[MAX_SOCKET_STRING];
  136. // Create xbind socket.
  137. void *xbind = test_context_socket (ZMQ_ROUTER);
  138. TEST_ASSERT_SUCCESS_ERRNO (
  139. zmq_setsockopt (xbind, ZMQ_LINGER, &zero, sizeof (zero)));
  140. bind_loopback_ipv4 (xbind, x_endpoint, sizeof x_endpoint);
  141. // Create zbind socket.
  142. void *zbind = test_context_socket (ZMQ_ROUTER);
  143. TEST_ASSERT_SUCCESS_ERRNO (
  144. zmq_setsockopt (zbind, ZMQ_LINGER, &zero, sizeof (zero)));
  145. bind_loopback_ipv4 (zbind, z_endpoint, sizeof z_endpoint);
  146. // Create connection socket.
  147. void *yconn = test_context_socket (ZMQ_ROUTER);
  148. TEST_ASSERT_SUCCESS_ERRNO (
  149. zmq_setsockopt (yconn, ZMQ_LINGER, &zero, sizeof (zero)));
  150. // set identities for each socket
  151. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  152. xbind, ZMQ_ROUTING_ID, x_routing_id, strlen (x_routing_id)));
  153. TEST_ASSERT_SUCCESS_ERRNO (
  154. zmq_setsockopt (yconn, ZMQ_ROUTING_ID, y_routing_id, 2));
  155. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  156. zbind, ZMQ_ROUTING_ID, z_routing_id, strlen (z_routing_id)));
  157. // Connect Y to X using a routing id
  158. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  159. yconn, ZMQ_CONNECT_ROUTING_ID, x_routing_id, strlen (x_routing_id)));
  160. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (yconn, x_endpoint));
  161. // Send some data from Y to X.
  162. send_string_expect_success (yconn, x_routing_id, ZMQ_SNDMORE);
  163. send_string_expect_success (yconn, msg, 0);
  164. // wait for the Y->X message to be received
  165. msleep (SETTLE_TIME);
  166. // Now X tries to connect to Z and send a message
  167. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  168. xbind, ZMQ_CONNECT_ROUTING_ID, z_routing_id, strlen (z_routing_id)));
  169. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (xbind, z_endpoint));
  170. // Try to send some data from X to Z.
  171. send_string_expect_success (xbind, z_routing_id, ZMQ_SNDMORE);
  172. send_string_expect_success (xbind, msg, 0);
  173. // wait for the X->Z message to be received (so that our non-blocking check will actually
  174. // fail if the message is routed to Y)
  175. msleep (SETTLE_TIME);
  176. // nothing should have been received on the Y socket
  177. TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
  178. zmq_recv (yconn, buff, 256, ZMQ_DONTWAIT));
  179. // the message should have been received on the Z socket
  180. recv_string_expect_success (zbind, x_routing_id, 0);
  181. recv_string_expect_success (zbind, msg, 0);
  182. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (xbind, x_endpoint));
  183. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (zbind, z_endpoint));
  184. test_context_socket_close (yconn);
  185. test_context_socket_close (xbind);
  186. test_context_socket_close (zbind);
  187. }
  188. void test_router_2_router_unnamed ()
  189. {
  190. test_router_2_router (false);
  191. }
  192. void test_router_2_router_named ()
  193. {
  194. test_router_2_router (true);
  195. }
  196. int main ()
  197. {
  198. setup_test_environment ();
  199. UNITY_BEGIN ();
  200. RUN_TEST (test_stream_2_stream);
  201. RUN_TEST (test_router_2_router_unnamed);
  202. RUN_TEST (test_router_2_router_named);
  203. RUN_TEST (test_router_2_router_while_receiving);
  204. return UNITY_END ();
  205. }