test_reqrep_tcp.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. void test_single_connect (int ipv6_)
  29. {
  30. size_t len = MAX_SOCKET_STRING;
  31. char my_endpoint[MAX_SOCKET_STRING];
  32. void *sb = test_context_socket (ZMQ_REP);
  33. bind_loopback (sb, ipv6_, my_endpoint, len);
  34. void *sc = test_context_socket (ZMQ_REQ);
  35. TEST_ASSERT_SUCCESS_ERRNO (
  36. zmq_setsockopt (sc, ZMQ_IPV6, &ipv6_, sizeof (int)));
  37. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
  38. bounce (sb, sc);
  39. // the sockets are disconnected and unbound explicitly in this test case
  40. // to check that this can be done successfully with the expected
  41. // endpoints/addresses
  42. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint));
  43. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
  44. test_context_socket_close (sc);
  45. test_context_socket_close (sb);
  46. }
  47. void make_connect_address (char *connect_address_,
  48. const int ipv6_,
  49. const int port_,
  50. const char *bind_address_)
  51. {
  52. sprintf (connect_address_, "tcp://%s:%i;%s", ipv6_ ? "[::1]" : "127.0.0.1",
  53. port_, strrchr (bind_address_, '/') + 1);
  54. }
  55. void test_multi_connect (int ipv6_)
  56. {
  57. size_t len = MAX_SOCKET_STRING;
  58. char my_endpoint_0[MAX_SOCKET_STRING];
  59. char my_endpoint_1[MAX_SOCKET_STRING];
  60. char my_endpoint_2[MAX_SOCKET_STRING];
  61. char my_endpoint_3[MAX_SOCKET_STRING * 2];
  62. void *sb0 = test_context_socket (ZMQ_REP);
  63. bind_loopback (sb0, ipv6_, my_endpoint_0, len);
  64. void *sb1 = test_context_socket (ZMQ_REP);
  65. bind_loopback (sb1, ipv6_, my_endpoint_1, len);
  66. void *sb2 = test_context_socket (ZMQ_REP);
  67. bind_loopback (sb2, ipv6_, my_endpoint_2, len);
  68. void *sc = test_context_socket (ZMQ_REQ);
  69. TEST_ASSERT_SUCCESS_ERRNO (
  70. zmq_setsockopt (sc, ZMQ_IPV6, &ipv6_, sizeof (int)));
  71. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_0));
  72. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_1));
  73. make_connect_address (my_endpoint_3, ipv6_, 5564, my_endpoint_2);
  74. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
  75. bounce (sb0, sc);
  76. bounce (sb1, sc);
  77. bounce (sb2, sc);
  78. bounce (sb0, sc);
  79. bounce (sb1, sc);
  80. bounce (sb2, sc);
  81. bounce (sb0, sc);
  82. /// see comment on zmq_disconnect/zmq_unbind in test_single_connect
  83. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_0));
  84. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_3));
  85. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_1));
  86. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
  87. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
  88. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb2, my_endpoint_2));
  89. test_context_socket_close (sc);
  90. test_context_socket_close (sb0);
  91. test_context_socket_close (sb1);
  92. test_context_socket_close (sb2);
  93. }
  94. void test_multi_connect_same_port (int ipv6_)
  95. {
  96. size_t len = MAX_SOCKET_STRING;
  97. char my_endpoint_0[MAX_SOCKET_STRING];
  98. char my_endpoint_1[MAX_SOCKET_STRING];
  99. char my_endpoint_2[MAX_SOCKET_STRING * 2];
  100. char my_endpoint_3[MAX_SOCKET_STRING * 2];
  101. char my_endpoint_4[MAX_SOCKET_STRING * 2];
  102. char my_endpoint_5[MAX_SOCKET_STRING * 2];
  103. void *sb0 = test_context_socket (ZMQ_REP);
  104. bind_loopback (sb0, ipv6_, my_endpoint_0, len);
  105. void *sb1 = test_context_socket (ZMQ_REP);
  106. bind_loopback (sb1, ipv6_, my_endpoint_1, len);
  107. void *sc0 = test_context_socket (ZMQ_REQ);
  108. TEST_ASSERT_SUCCESS_ERRNO (
  109. zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6_, sizeof (int)));
  110. make_connect_address (my_endpoint_2, ipv6_, 5564, my_endpoint_0);
  111. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
  112. make_connect_address (my_endpoint_3, ipv6_, 5565, my_endpoint_1);
  113. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
  114. void *sc1 = test_context_socket (ZMQ_REQ);
  115. TEST_ASSERT_SUCCESS_ERRNO (
  116. zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6_, sizeof (int)));
  117. make_connect_address (my_endpoint_4, ipv6_, 5565, my_endpoint_0);
  118. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
  119. make_connect_address (my_endpoint_5, ipv6_, 5564, my_endpoint_1);
  120. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
  121. bounce (sb0, sc0);
  122. bounce (sb1, sc0);
  123. bounce (sb0, sc1);
  124. bounce (sb1, sc1);
  125. bounce (sb0, sc0);
  126. bounce (sb1, sc0);
  127. /// see comment on zmq_disconnect/zmq_unbind in test_single_connect
  128. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc1, my_endpoint_4));
  129. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc1, my_endpoint_5));
  130. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc0, my_endpoint_2));
  131. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc0, my_endpoint_3));
  132. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
  133. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
  134. test_context_socket_close (sc0);
  135. test_context_socket_close (sc1);
  136. test_context_socket_close (sb0);
  137. test_context_socket_close (sb1);
  138. }
  139. void test_single_connect_ipv4 ()
  140. {
  141. test_single_connect (false);
  142. }
  143. void test_multi_connect_ipv4 ()
  144. {
  145. test_multi_connect (false);
  146. }
  147. void test_multi_connect_same_port_ipv4 ()
  148. {
  149. test_multi_connect_same_port (false);
  150. }
  151. void test_single_connect_ipv6 ()
  152. {
  153. test_single_connect (true);
  154. }
  155. void test_multi_connect_ipv6 ()
  156. {
  157. test_multi_connect (true);
  158. }
  159. void test_multi_connect_same_port_ipv6 ()
  160. {
  161. test_multi_connect_same_port (true);
  162. }
  163. int main (void)
  164. {
  165. setup_test_environment ();
  166. UNITY_BEGIN ();
  167. RUN_TEST (test_single_connect_ipv4);
  168. RUN_TEST (test_multi_connect_ipv4);
  169. RUN_TEST (test_multi_connect_same_port_ipv4);
  170. RUN_TEST (test_single_connect_ipv6);
  171. RUN_TEST (test_multi_connect_ipv6);
  172. RUN_TEST (test_multi_connect_same_port_ipv6);
  173. return UNITY_END ();
  174. }