test_use_fd.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. Copyright (c) 2016-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 <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. SETUP_TEARDOWN_TESTCONTEXT
  30. #if !defined(ZMQ_HAVE_WINDOWS)
  31. void pre_allocate_sock_tcp (void *socket_, char *my_endpoint_)
  32. {
  33. fd_t s = bind_socket_resolve_port ("127.0.0.1", "0", my_endpoint_);
  34. TEST_ASSERT_SUCCESS_ERRNO (
  35. zmq_setsockopt (socket_, ZMQ_USE_FD, &s, sizeof (s)));
  36. }
  37. typedef void (*pre_allocate_sock_fun_t) (void *, char *);
  38. void setup_socket_pair (pre_allocate_sock_fun_t pre_allocate_sock_fun_,
  39. int bind_socket_type_,
  40. int connect_socket_type_,
  41. void **out_sb_,
  42. void **out_sc_)
  43. {
  44. *out_sb_ = test_context_socket (bind_socket_type_);
  45. char my_endpoint[MAX_SOCKET_STRING];
  46. pre_allocate_sock_fun_ (*out_sb_, my_endpoint);
  47. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (*out_sb_, my_endpoint));
  48. *out_sc_ = test_context_socket (connect_socket_type_);
  49. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (*out_sc_, my_endpoint));
  50. }
  51. void test_socket_pair (pre_allocate_sock_fun_t pre_allocate_sock_fun_,
  52. int bind_socket_type_,
  53. int connect_socket_type_)
  54. {
  55. void *sb, *sc;
  56. setup_socket_pair (pre_allocate_sock_fun_, bind_socket_type_,
  57. connect_socket_type_, &sb, &sc);
  58. bounce (sb, sc);
  59. test_context_socket_close (sc);
  60. test_context_socket_close (sb);
  61. }
  62. void test_req_rep (pre_allocate_sock_fun_t pre_allocate_sock_fun_)
  63. {
  64. test_socket_pair (pre_allocate_sock_fun_, ZMQ_REP, ZMQ_REQ);
  65. }
  66. void test_pair (pre_allocate_sock_fun_t pre_allocate_sock_fun_)
  67. {
  68. test_socket_pair (pre_allocate_sock_fun_, ZMQ_PAIR, ZMQ_PAIR);
  69. }
  70. void test_client_server (pre_allocate_sock_fun_t pre_allocate_sock_fun_)
  71. {
  72. #if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
  73. void *sb, *sc;
  74. setup_socket_pair (pre_allocate_sock_fun_, ZMQ_SERVER, ZMQ_CLIENT, &sb,
  75. &sc);
  76. zmq_msg_t msg;
  77. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, 1));
  78. char *data = static_cast<char *> (zmq_msg_data (&msg));
  79. data[0] = 1;
  80. int rc = zmq_msg_send (&msg, sc, ZMQ_SNDMORE);
  81. // TODO which error code is expected?
  82. TEST_ASSERT_EQUAL_INT (-1, rc);
  83. rc = zmq_msg_send (&msg, sc, 0);
  84. TEST_ASSERT_EQUAL_INT (1, rc);
  85. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  86. rc = zmq_msg_recv (&msg, sb, 0);
  87. TEST_ASSERT_EQUAL_INT (1, rc);
  88. uint32_t routing_id = zmq_msg_routing_id (&msg);
  89. TEST_ASSERT_NOT_EQUAL (0, routing_id);
  90. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  91. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, 1));
  92. data = static_cast<char *> (zmq_msg_data (&msg));
  93. data[0] = 2;
  94. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_set_routing_id (&msg, routing_id));
  95. rc = zmq_msg_send (&msg, sb, ZMQ_SNDMORE);
  96. // TODO which error code is expected?
  97. TEST_ASSERT_EQUAL_INT (-1, rc);
  98. rc = zmq_msg_send (&msg, sb, 0);
  99. TEST_ASSERT_EQUAL_INT (1, rc);
  100. rc = zmq_msg_recv (&msg, sc, 0);
  101. TEST_ASSERT_EQUAL_INT (1, rc);
  102. routing_id = zmq_msg_routing_id (&msg);
  103. TEST_ASSERT_EQUAL_INT (0, routing_id);
  104. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  105. test_context_socket_close (sc);
  106. test_context_socket_close (sb);
  107. #endif
  108. }
  109. void test_req_rep_tcp ()
  110. {
  111. test_req_rep (pre_allocate_sock_tcp);
  112. }
  113. void test_pair_tcp ()
  114. {
  115. test_pair (pre_allocate_sock_tcp);
  116. }
  117. void test_client_server_tcp ()
  118. {
  119. #if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
  120. test_client_server (pre_allocate_sock_tcp);
  121. #endif
  122. }
  123. char ipc_endpoint[MAX_SOCKET_STRING] = "";
  124. void pre_allocate_sock_ipc (void *sb_, char *my_endpoint_)
  125. {
  126. fd_t s = bind_socket_resolve_port ("", "", my_endpoint_, AF_UNIX, 0);
  127. TEST_ASSERT_SUCCESS_ERRNO (
  128. zmq_setsockopt (sb_, ZMQ_USE_FD, &s, sizeof (s)));
  129. strcpy (ipc_endpoint, strchr (my_endpoint_, '/') + 2);
  130. }
  131. void test_req_rep_ipc ()
  132. {
  133. test_req_rep (pre_allocate_sock_ipc);
  134. TEST_ASSERT_SUCCESS_ERRNO (unlink (ipc_endpoint));
  135. }
  136. void test_pair_ipc ()
  137. {
  138. test_pair (pre_allocate_sock_ipc);
  139. TEST_ASSERT_SUCCESS_ERRNO (unlink (ipc_endpoint));
  140. }
  141. void test_client_server_ipc ()
  142. {
  143. #if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
  144. test_client_server (pre_allocate_sock_ipc);
  145. TEST_ASSERT_SUCCESS_ERRNO (unlink (ipc_endpoint));
  146. #endif
  147. }
  148. int main ()
  149. {
  150. setup_test_environment ();
  151. UNITY_BEGIN ();
  152. RUN_TEST (test_req_rep_tcp);
  153. RUN_TEST (test_pair_tcp);
  154. RUN_TEST (test_client_server_tcp);
  155. RUN_TEST (test_req_rep_ipc);
  156. RUN_TEST (test_pair_ipc);
  157. RUN_TEST (test_client_server_ipc);
  158. return UNITY_END ();
  159. }
  160. #else
  161. int main ()
  162. {
  163. return 0;
  164. }
  165. #endif