test_heartbeats.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. Copyright (c) 2007-2017 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. #if defined(ZMQ_HAVE_WINDOWS)
  17. #include <winsock2.h>
  18. #include <ws2tcpip.h>
  19. #include <stdexcept>
  20. #define close closesocket
  21. typedef SOCKET raw_socket;
  22. #else
  23. #include <arpa/inet.h>
  24. #include <unistd.h>
  25. typedef int raw_socket;
  26. #endif
  27. #include <limits.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. // TODO remove this here, either ensure that UINT16_MAX is always properly
  31. // defined or handle this at a more central location
  32. #ifndef UINT16_MAX
  33. #define UINT16_MAX 65535
  34. #endif
  35. #include "testutil_unity.hpp"
  36. SETUP_TEARDOWN_TESTCONTEXT
  37. // Read one event off the monitor socket; return value and address
  38. // by reference, if not null, and event number by value. Returns -1
  39. // in case of error.
  40. static int get_monitor_event (void *monitor_)
  41. {
  42. for (int i = 0; i < 10; i++) {
  43. // First frame in message contains event number and value
  44. zmq_msg_t msg;
  45. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  46. if (zmq_msg_recv (&msg, monitor_, ZMQ_DONTWAIT) == -1) {
  47. msleep (SETTLE_TIME);
  48. continue; // Interrupted, presumably
  49. }
  50. TEST_ASSERT_TRUE (zmq_msg_more (&msg));
  51. uint8_t *data = static_cast<uint8_t *> (zmq_msg_data (&msg));
  52. uint16_t event = *reinterpret_cast<uint16_t *> (data);
  53. // Second frame in message contains event address
  54. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  55. if (zmq_msg_recv (&msg, monitor_, 0) == -1) {
  56. return -1; // Interrupted, presumably
  57. }
  58. TEST_ASSERT_FALSE (zmq_msg_more (&msg));
  59. return event;
  60. }
  61. return -1;
  62. }
  63. static void recv_with_retry (raw_socket fd_, char *buffer_, int bytes_)
  64. {
  65. int received = 0;
  66. while (true) {
  67. int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
  68. recv (fd_, buffer_ + received, bytes_ - received, 0));
  69. TEST_ASSERT_GREATER_THAN_INT (0, rc);
  70. received += rc;
  71. TEST_ASSERT_LESS_OR_EQUAL_INT (bytes_, received);
  72. if (received == bytes_)
  73. break;
  74. }
  75. }
  76. static void mock_handshake (raw_socket fd_, int mock_ping_)
  77. {
  78. char buffer[128];
  79. memset (buffer, 0, sizeof (buffer));
  80. memcpy (buffer, zmtp_greeting_null, sizeof (zmtp_greeting_null));
  81. int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
  82. send (fd_, buffer, sizeof (zmtp_greeting_null), 0));
  83. TEST_ASSERT_EQUAL_INT (sizeof (zmtp_greeting_null), rc);
  84. recv_with_retry (fd_, buffer, sizeof (zmtp_greeting_null));
  85. memset (buffer, 0, sizeof (buffer));
  86. memcpy (buffer, zmtp_ready_dealer, sizeof (zmtp_ready_dealer));
  87. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
  88. send (fd_, buffer, sizeof (zmtp_ready_dealer), 0));
  89. TEST_ASSERT_EQUAL_INT (sizeof (zmtp_ready_dealer), rc);
  90. // greeting
  91. recv_with_retry (fd_, buffer, sizeof (zmtp_ready_dealer));
  92. if (mock_ping_) {
  93. // test PING context - should be replicated in the PONG
  94. // to avoid timeouts, do a bulk send
  95. const uint8_t zmtp_ping[12] = {4, 10, 4, 'P', 'I', 'N',
  96. 'G', 0, 0, 'L', 'O', 'L'};
  97. uint8_t zmtp_pong[10] = {4, 8, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'};
  98. memset (buffer, 0, sizeof (buffer));
  99. memcpy (buffer, zmtp_ping, 12);
  100. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 12, 0));
  101. TEST_ASSERT_EQUAL_INT (12, rc);
  102. // test a larger body that won't fit in a small message and should get
  103. // truncated
  104. memset (buffer, 'z', sizeof (buffer));
  105. memcpy (buffer, zmtp_ping, 12);
  106. buffer[1] = 65;
  107. rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd_, buffer, 67, 0));
  108. TEST_ASSERT_EQUAL_INT (67, rc);
  109. // small pong
  110. recv_with_retry (fd_, buffer, 10);
  111. TEST_ASSERT_EQUAL_INT (0, memcmp (zmtp_pong, buffer, 10));
  112. // large pong
  113. recv_with_retry (fd_, buffer, 23);
  114. uint8_t zmtp_pooong[65] = {4, 21, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'};
  115. memset (zmtp_pooong + 10, 'z', 55);
  116. TEST_ASSERT_EQUAL_INT (0, memcmp (zmtp_pooong, buffer, 23));
  117. }
  118. }
  119. static void setup_curve (void *socket_, int is_server_)
  120. {
  121. const char *secret_key;
  122. const char *public_key;
  123. const char *server_key;
  124. if (is_server_) {
  125. secret_key = "JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6";
  126. public_key = "rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7";
  127. server_key = NULL;
  128. } else {
  129. secret_key = "D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs";
  130. public_key = "Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID";
  131. server_key = "rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7";
  132. }
  133. zmq_setsockopt (socket_, ZMQ_CURVE_SECRETKEY, secret_key,
  134. strlen (secret_key));
  135. zmq_setsockopt (socket_, ZMQ_CURVE_PUBLICKEY, public_key,
  136. strlen (public_key));
  137. if (is_server_)
  138. zmq_setsockopt (socket_, ZMQ_CURVE_SERVER, &is_server_,
  139. sizeof (is_server_));
  140. else
  141. zmq_setsockopt (socket_, ZMQ_CURVE_SERVERKEY, server_key,
  142. strlen (server_key));
  143. }
  144. static void prep_server_socket (int set_heartbeats_,
  145. int is_curve_,
  146. void **server_out_,
  147. void **mon_out_,
  148. char *endpoint_,
  149. size_t ep_length_,
  150. int socket_type_)
  151. {
  152. // We'll be using this socket in raw mode
  153. void *server = test_context_socket (socket_type_);
  154. int value = 0;
  155. TEST_ASSERT_SUCCESS_ERRNO (
  156. zmq_setsockopt (server, ZMQ_LINGER, &value, sizeof (value)));
  157. if (set_heartbeats_) {
  158. value = 50;
  159. TEST_ASSERT_SUCCESS_ERRNO (
  160. zmq_setsockopt (server, ZMQ_HEARTBEAT_IVL, &value, sizeof (value)));
  161. }
  162. if (is_curve_)
  163. setup_curve (server, 1);
  164. bind_loopback_ipv4 (server, endpoint_, ep_length_);
  165. // Create and connect a socket for collecting monitor events on dealer
  166. void *server_mon = test_context_socket (ZMQ_PAIR);
  167. TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
  168. server, "inproc://monitor-dealer",
  169. ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
  170. // Connect to the inproc endpoint so we'll get events
  171. TEST_ASSERT_SUCCESS_ERRNO (
  172. zmq_connect (server_mon, "inproc://monitor-dealer"));
  173. *server_out_ = server;
  174. *mon_out_ = server_mon;
  175. }
  176. // This checks for a broken TCP connection (or, in this case a stuck one
  177. // where the peer never responds to PINGS). There should be an accepted event
  178. // then a disconnect event.
  179. static void test_heartbeat_timeout (int server_type_, int mock_ping_)
  180. {
  181. int rc;
  182. char my_endpoint[MAX_SOCKET_STRING];
  183. void *server, *server_mon;
  184. prep_server_socket (!mock_ping_, 0, &server, &server_mon, my_endpoint,
  185. MAX_SOCKET_STRING, server_type_);
  186. fd_t s = connect_socket (my_endpoint);
  187. // Mock a ZMTP 3 client so we can forcibly time out a connection
  188. mock_handshake (s, mock_ping_);
  189. // By now everything should report as connected
  190. rc = get_monitor_event (server_mon);
  191. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
  192. if (!mock_ping_) {
  193. // We should have been disconnected
  194. rc = get_monitor_event (server_mon);
  195. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, rc);
  196. }
  197. close (s);
  198. test_context_socket_close (server);
  199. test_context_socket_close (server_mon);
  200. }
  201. // This checks that peers respect the TTL value in ping messages
  202. // We set up a mock ZMTP 3 client and send a ping message with a TLL
  203. // to a server that is not doing any heartbeating. Then we sleep,
  204. // if the server disconnects the client, then we know the TTL did
  205. // its thing correctly.
  206. static void test_heartbeat_ttl (int client_type_, int server_type_)
  207. {
  208. int rc, value;
  209. char my_endpoint[MAX_SOCKET_STRING];
  210. void *server, *server_mon, *client;
  211. prep_server_socket (0, 0, &server, &server_mon, my_endpoint,
  212. MAX_SOCKET_STRING, server_type_);
  213. client = test_context_socket (client_type_);
  214. // Set the heartbeat TTL to 0.1 seconds
  215. value = 100;
  216. TEST_ASSERT_SUCCESS_ERRNO (
  217. zmq_setsockopt (client, ZMQ_HEARTBEAT_TTL, &value, sizeof (value)));
  218. // Set the heartbeat interval to much longer than the TTL so that
  219. // the socket times out oon the remote side.
  220. value = 250;
  221. TEST_ASSERT_SUCCESS_ERRNO (
  222. zmq_setsockopt (client, ZMQ_HEARTBEAT_IVL, &value, sizeof (value)));
  223. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  224. // By now everything should report as connected
  225. rc = get_monitor_event (server_mon);
  226. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
  227. msleep (SETTLE_TIME);
  228. // We should have been disconnected
  229. rc = get_monitor_event (server_mon);
  230. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, rc);
  231. test_context_socket_close (server);
  232. test_context_socket_close (server_mon);
  233. test_context_socket_close (client);
  234. }
  235. // This checks for normal operation - that is pings and pongs being
  236. // exchanged normally. There should be an accepted event on the server,
  237. // and then no event afterwards.
  238. static void
  239. test_heartbeat_notimeout (int is_curve_, int client_type_, int server_type_)
  240. {
  241. int rc;
  242. char my_endpoint[MAX_SOCKET_STRING];
  243. void *server, *server_mon;
  244. prep_server_socket (1, is_curve_, &server, &server_mon, my_endpoint,
  245. MAX_SOCKET_STRING, server_type_);
  246. void *client = test_context_socket (client_type_);
  247. if (is_curve_)
  248. setup_curve (client, 0);
  249. rc = zmq_connect (client, my_endpoint);
  250. // Give it a sec to connect and handshake
  251. msleep (SETTLE_TIME);
  252. // By now everything should report as connected
  253. rc = get_monitor_event (server_mon);
  254. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
  255. // We should still be connected because pings and pongs are happenin'
  256. TEST_ASSERT_EQUAL_INT (-1, get_monitor_event (server_mon));
  257. test_context_socket_close (client);
  258. test_context_socket_close (server);
  259. test_context_socket_close (server_mon);
  260. }
  261. void test_heartbeat_timeout_router ()
  262. {
  263. test_heartbeat_timeout (ZMQ_ROUTER, 0);
  264. }
  265. void test_heartbeat_timeout_router_mock_ping ()
  266. {
  267. test_heartbeat_timeout (ZMQ_ROUTER, 1);
  268. }
  269. #define DEFINE_TESTS(first, second, first_define, second_define) \
  270. void test_heartbeat_ttl_##first##_##second () \
  271. { \
  272. test_heartbeat_ttl (first_define, second_define); \
  273. } \
  274. void test_heartbeat_notimeout_##first##_##second () \
  275. { \
  276. test_heartbeat_notimeout (0, first_define, second_define); \
  277. } \
  278. void test_heartbeat_notimeout_##first##_##second##_with_curve () \
  279. { \
  280. test_heartbeat_notimeout (1, first_define, second_define); \
  281. }
  282. DEFINE_TESTS (dealer, router, ZMQ_DEALER, ZMQ_ROUTER)
  283. DEFINE_TESTS (req, rep, ZMQ_REQ, ZMQ_REP)
  284. DEFINE_TESTS (pull, push, ZMQ_PULL, ZMQ_PUSH)
  285. DEFINE_TESTS (sub, pub, ZMQ_SUB, ZMQ_PUB)
  286. DEFINE_TESTS (pair, pair, ZMQ_PAIR, ZMQ_PAIR)
  287. #ifdef ZMQ_BUILD_DRAFT_API
  288. DEFINE_TESTS (gather, scatter, ZMQ_GATHER, ZMQ_SCATTER)
  289. DEFINE_TESTS (client, server, ZMQ_CLIENT, ZMQ_SERVER)
  290. #endif
  291. const int deciseconds_per_millisecond = 100;
  292. const int heartbeat_ttl_max =
  293. (UINT16_MAX + 1) * deciseconds_per_millisecond - 1;
  294. void test_setsockopt_heartbeat_success (const int value_)
  295. {
  296. void *const socket = test_context_socket (ZMQ_PAIR);
  297. TEST_ASSERT_SUCCESS_ERRNO (
  298. zmq_setsockopt (socket, ZMQ_HEARTBEAT_TTL, &value_, sizeof (value_)));
  299. int value_read;
  300. size_t value_read_size = sizeof (value_read);
  301. TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (socket, ZMQ_HEARTBEAT_TTL,
  302. &value_read, &value_read_size));
  303. TEST_ASSERT_EQUAL_INT (value_ - value_ % deciseconds_per_millisecond,
  304. value_read);
  305. test_context_socket_close (socket);
  306. }
  307. void test_setsockopt_heartbeat_ttl_max ()
  308. {
  309. test_setsockopt_heartbeat_success (heartbeat_ttl_max);
  310. }
  311. void test_setsockopt_heartbeat_ttl_more_than_max_fails ()
  312. {
  313. void *const socket = test_context_socket (ZMQ_PAIR);
  314. const int value = heartbeat_ttl_max + 1;
  315. TEST_ASSERT_FAILURE_ERRNO (
  316. EINVAL,
  317. zmq_setsockopt (socket, ZMQ_HEARTBEAT_TTL, &value, sizeof (value)));
  318. test_context_socket_close (socket);
  319. }
  320. void test_setsockopt_heartbeat_ttl_near_zero ()
  321. {
  322. test_setsockopt_heartbeat_success (deciseconds_per_millisecond - 1);
  323. }
  324. int main (void)
  325. {
  326. // The test cases are very long-running. The default timeout of 60 seconds
  327. // is not always enough.
  328. setup_test_environment (90);
  329. UNITY_BEGIN ();
  330. RUN_TEST (test_heartbeat_timeout_router);
  331. RUN_TEST (test_heartbeat_timeout_router_mock_ping);
  332. RUN_TEST (test_heartbeat_ttl_dealer_router);
  333. RUN_TEST (test_heartbeat_ttl_req_rep);
  334. RUN_TEST (test_heartbeat_ttl_pull_push);
  335. RUN_TEST (test_heartbeat_ttl_sub_pub);
  336. RUN_TEST (test_heartbeat_ttl_pair_pair);
  337. RUN_TEST (test_setsockopt_heartbeat_ttl_max);
  338. RUN_TEST (test_setsockopt_heartbeat_ttl_more_than_max_fails);
  339. RUN_TEST (test_setsockopt_heartbeat_ttl_near_zero);
  340. RUN_TEST (test_heartbeat_notimeout_dealer_router);
  341. RUN_TEST (test_heartbeat_notimeout_req_rep);
  342. RUN_TEST (test_heartbeat_notimeout_pull_push);
  343. RUN_TEST (test_heartbeat_notimeout_sub_pub);
  344. RUN_TEST (test_heartbeat_notimeout_pair_pair);
  345. RUN_TEST (test_heartbeat_notimeout_dealer_router_with_curve);
  346. RUN_TEST (test_heartbeat_notimeout_req_rep_with_curve);
  347. RUN_TEST (test_heartbeat_notimeout_pull_push_with_curve);
  348. RUN_TEST (test_heartbeat_notimeout_sub_pub_with_curve);
  349. RUN_TEST (test_heartbeat_notimeout_pair_pair_with_curve);
  350. #ifdef ZMQ_BUILD_DRAFT_API
  351. RUN_TEST (test_heartbeat_ttl_client_server);
  352. RUN_TEST (test_heartbeat_ttl_gather_scatter);
  353. RUN_TEST (test_heartbeat_notimeout_client_server);
  354. RUN_TEST (test_heartbeat_notimeout_gather_scatter);
  355. RUN_TEST (test_heartbeat_notimeout_client_server_with_curve);
  356. RUN_TEST (test_heartbeat_notimeout_gather_scatter_with_curve);
  357. #endif
  358. return UNITY_END ();
  359. }