test_mock_pub_sub.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright (c) 2018 Contributors as noted in the AUTHORS file
  3. This file is part of 0MQ.
  4. 0MQ is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. 0MQ is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "testutil.hpp"
  16. #include "testutil_unity.hpp"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. SETUP_TEARDOWN_TESTCONTEXT
  20. // Read one event off the monitor socket; return value and address
  21. // by reference, if not null, and event number by value. Returns -1
  22. // in case of error.
  23. static int get_monitor_event (void *monitor_)
  24. {
  25. for (int i = 0; i < 2; i++) {
  26. // First frame in message contains event number and value
  27. zmq_msg_t msg;
  28. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  29. if (zmq_msg_recv (&msg, monitor_, ZMQ_DONTWAIT) == -1) {
  30. msleep (SETTLE_TIME);
  31. continue; // Interrupted, presumably
  32. }
  33. TEST_ASSERT_TRUE (zmq_msg_more (&msg));
  34. uint8_t *data = static_cast<uint8_t *> (zmq_msg_data (&msg));
  35. uint16_t event = *reinterpret_cast<uint16_t *> (data);
  36. // Second frame in message contains event address
  37. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  38. if (zmq_msg_recv (&msg, monitor_, 0) == -1) {
  39. return -1; // Interrupted, presumably
  40. }
  41. TEST_ASSERT_FALSE (zmq_msg_more (&msg));
  42. return event;
  43. }
  44. return -1;
  45. }
  46. static void recv_with_retry (fd_t fd_, char *buffer_, int bytes_)
  47. {
  48. int received = 0;
  49. while (true) {
  50. int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
  51. recv (fd_, buffer_ + received, bytes_ - received, 0));
  52. TEST_ASSERT_GREATER_THAN_INT (0, rc);
  53. received += rc;
  54. TEST_ASSERT_LESS_OR_EQUAL_INT (bytes_, received);
  55. if (received == bytes_)
  56. break;
  57. }
  58. }
  59. static void mock_handshake (fd_t fd_, bool sub_command, bool mock_pub)
  60. {
  61. char buffer[128];
  62. memset (buffer, 0, sizeof (buffer));
  63. memcpy (buffer, zmtp_greeting_null, sizeof (zmtp_greeting_null));
  64. // Mock ZMTP 3.1 which uses commands
  65. if (sub_command) {
  66. buffer[11] = 1;
  67. }
  68. int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 64, 0));
  69. TEST_ASSERT_EQUAL_INT (64, rc);
  70. recv_with_retry (fd_, buffer, 64);
  71. if (!mock_pub) {
  72. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (
  73. fd_, (const char *) zmtp_ready_sub, sizeof (zmtp_ready_sub), 0));
  74. TEST_ASSERT_EQUAL_INT (sizeof (zmtp_ready_sub), rc);
  75. } else {
  76. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (
  77. fd_, (const char *) zmtp_ready_xpub, sizeof (zmtp_ready_xpub), 0));
  78. TEST_ASSERT_EQUAL_INT (sizeof (zmtp_ready_xpub), rc);
  79. }
  80. // greeting - XPUB has one extra byte
  81. memset (buffer, 0, sizeof (buffer));
  82. recv_with_retry (fd_, buffer,
  83. mock_pub ? sizeof (zmtp_ready_sub)
  84. : sizeof (zmtp_ready_xpub));
  85. }
  86. static void prep_server_socket (void **server_out_,
  87. void **mon_out_,
  88. char *endpoint_,
  89. size_t ep_length_,
  90. int socket_type)
  91. {
  92. // We'll be using this socket in raw mode
  93. void *server = test_context_socket (socket_type);
  94. int value = 0;
  95. TEST_ASSERT_SUCCESS_ERRNO (
  96. zmq_setsockopt (server, ZMQ_LINGER, &value, sizeof (value)));
  97. bind_loopback_ipv4 (server, endpoint_, ep_length_);
  98. // Create and connect a socket for collecting monitor events on xpub
  99. void *server_mon = test_context_socket (ZMQ_PAIR);
  100. TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
  101. server, "inproc://monitor-dealer",
  102. ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
  103. // Connect to the inproc endpoint so we'll get events
  104. TEST_ASSERT_SUCCESS_ERRNO (
  105. zmq_connect (server_mon, "inproc://monitor-dealer"));
  106. *server_out_ = server;
  107. *mon_out_ = server_mon;
  108. }
  109. static void test_mock_pub_sub (bool sub_command_, bool mock_pub_)
  110. {
  111. int rc;
  112. char my_endpoint[MAX_SOCKET_STRING];
  113. void *server, *server_mon;
  114. prep_server_socket (&server, &server_mon, my_endpoint, MAX_SOCKET_STRING,
  115. mock_pub_ ? ZMQ_SUB : ZMQ_XPUB);
  116. fd_t s = connect_socket (my_endpoint);
  117. // Mock a ZMTP 3 client so we can forcibly try sub commands
  118. mock_handshake (s, sub_command_, mock_pub_);
  119. // By now everything should report as connected
  120. rc = get_monitor_event (server_mon);
  121. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
  122. char buffer[32];
  123. memset (buffer, 0, sizeof (buffer));
  124. if (mock_pub_) {
  125. rc = zmq_setsockopt (server, ZMQ_SUBSCRIBE, "A", 1);
  126. TEST_ASSERT_EQUAL_INT (0, rc);
  127. // SUB binds, let its state machine run
  128. // Because zeromq attach the pipe after the handshake, we need more time here before we can run the state-machine
  129. msleep (1);
  130. zmq_recv (server, buffer, 16, ZMQ_DONTWAIT);
  131. if (sub_command_) {
  132. recv_with_retry (s, buffer, 13);
  133. TEST_ASSERT_EQUAL_INT (0,
  134. memcmp (buffer, "\4\xb\x9SUBSCRIBEA", 13));
  135. } else {
  136. recv_with_retry (s, buffer, 4);
  137. TEST_ASSERT_EQUAL_INT (0, memcmp (buffer, "\0\2\1A", 4));
  138. }
  139. memcpy (buffer, "\0\4ALOL", 6);
  140. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (s, buffer, 6, 0));
  141. TEST_ASSERT_EQUAL_INT (6, rc);
  142. memset (buffer, 0, sizeof (buffer));
  143. rc = zmq_recv (server, buffer, 4, 0);
  144. TEST_ASSERT_EQUAL_INT (4, rc);
  145. TEST_ASSERT_EQUAL_INT (0, memcmp (buffer, "ALOL", 4));
  146. } else {
  147. if (sub_command_) {
  148. const uint8_t sub[13] = {4, 11, 9, 'S', 'U', 'B', 'S',
  149. 'C', 'R', 'I', 'B', 'E', 'A'};
  150. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
  151. send (s, (const char *) sub, 13, 0));
  152. TEST_ASSERT_EQUAL_INT (13, rc);
  153. } else {
  154. const uint8_t sub[4] = {0, 2, 1, 'A'};
  155. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
  156. send (s, (const char *) sub, 4, 0));
  157. TEST_ASSERT_EQUAL_INT (4, rc);
  158. }
  159. rc = zmq_recv (server, buffer, 2, 0);
  160. TEST_ASSERT_EQUAL_INT (2, rc);
  161. TEST_ASSERT_EQUAL_INT (0, memcmp (buffer, "\1A", 2));
  162. rc = zmq_send (server, "ALOL", 4, 0);
  163. TEST_ASSERT_EQUAL_INT (4, rc);
  164. memset (buffer, 0, sizeof (buffer));
  165. recv_with_retry (s, buffer, 6);
  166. TEST_ASSERT_EQUAL_INT (0, memcmp (buffer, "\0\4ALOL", 6));
  167. }
  168. close (s);
  169. test_context_socket_close (server);
  170. test_context_socket_close (server_mon);
  171. }
  172. void test_mock_sub_command ()
  173. {
  174. test_mock_pub_sub (true, false);
  175. }
  176. void test_mock_sub_legacy ()
  177. {
  178. test_mock_pub_sub (false, false);
  179. }
  180. void test_mock_pub_command ()
  181. {
  182. test_mock_pub_sub (true, true);
  183. }
  184. void test_mock_pub_legacy ()
  185. {
  186. test_mock_pub_sub (false, true);
  187. }
  188. int main (void)
  189. {
  190. setup_test_environment ();
  191. UNITY_BEGIN ();
  192. RUN_TEST (test_mock_sub_command);
  193. RUN_TEST (test_mock_sub_legacy);
  194. RUN_TEST (test_mock_pub_command);
  195. RUN_TEST (test_mock_pub_legacy);
  196. return UNITY_END ();
  197. }