test_radio_dish.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. Copyright (c) 2007-2016 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. #ifndef _WIN32
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <unistd.h>
  32. #endif
  33. // Helper macro to define the v4/v6 function pairs
  34. #define MAKE_TEST_V4V6(_test) \
  35. static void _test##_ipv4 () { _test (false); } \
  36. \
  37. static void _test##_ipv6 () \
  38. { \
  39. if (!is_ipv6_available ()) { \
  40. TEST_IGNORE_MESSAGE ("ipv6 is not available"); \
  41. } \
  42. _test (true); \
  43. }
  44. SETUP_TEARDOWN_TESTCONTEXT
  45. void msg_send_expect_success (void *s_, const char *group_, const char *body_)
  46. {
  47. zmq_msg_t msg;
  48. const size_t len = strlen (body_);
  49. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, len));
  50. memcpy (zmq_msg_data (&msg), body_, len);
  51. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_set_group (&msg, group_));
  52. int rc = zmq_msg_send (&msg, s_, 0);
  53. TEST_ASSERT_EQUAL_INT ((int) len, rc);
  54. // TODO isn't the msg closed by zmq_msg_send?
  55. zmq_msg_close (&msg);
  56. }
  57. void msg_recv_cmp (void *s_, const char *group_, const char *body_)
  58. {
  59. zmq_msg_t msg;
  60. const size_t len = strlen (body_);
  61. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  62. int recv_rc = TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, s_, 0));
  63. TEST_ASSERT_EQUAL_INT (len, recv_rc);
  64. TEST_ASSERT_EQUAL_STRING (group_, zmq_msg_group (&msg));
  65. TEST_ASSERT_EQUAL_STRING_LEN (body_, zmq_msg_data (&msg), len);
  66. zmq_msg_close (&msg);
  67. }
  68. void test_leave_unjoined_fails ()
  69. {
  70. void *dish = test_context_socket (ZMQ_DISH);
  71. // Leaving a group which we didn't join
  72. TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_leave (dish, "Movies"));
  73. test_context_socket_close (dish);
  74. }
  75. void test_long_group ()
  76. {
  77. size_t len = MAX_SOCKET_STRING;
  78. char my_endpoint[MAX_SOCKET_STRING];
  79. void *radio = test_context_socket (ZMQ_RADIO);
  80. bind_loopback (radio, false, my_endpoint, len);
  81. void *dish = test_context_socket (ZMQ_DISH);
  82. // Joining to a long group, over 14 chars
  83. char group[19] = "0123456789ABCDEFGH";
  84. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, group));
  85. // Connecting
  86. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dish, my_endpoint));
  87. msleep (SETTLE_TIME);
  88. // This is going to be sent to the dish
  89. msg_send_expect_success (radio, group, "HELLO");
  90. // Check the correct message arrived
  91. msg_recv_cmp (dish, group, "HELLO");
  92. test_context_socket_close (dish);
  93. test_context_socket_close (radio);
  94. }
  95. void test_join_too_long_fails ()
  96. {
  97. void *dish = test_context_socket (ZMQ_DISH);
  98. // Joining too long group
  99. char too_long_group[ZMQ_GROUP_MAX_LENGTH + 2];
  100. for (int index = 0; index < ZMQ_GROUP_MAX_LENGTH + 2; index++)
  101. too_long_group[index] = 'A';
  102. too_long_group[ZMQ_GROUP_MAX_LENGTH + 1] = '\0';
  103. TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_join (dish, too_long_group));
  104. test_context_socket_close (dish);
  105. }
  106. void test_join_twice_fails ()
  107. {
  108. void *dish = test_context_socket (ZMQ_DISH);
  109. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, "Movies"));
  110. // Duplicate Joining
  111. TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_join (dish, "Movies"));
  112. test_context_socket_close (dish);
  113. }
  114. void test_radio_dish_tcp_poll (int ipv6_)
  115. {
  116. size_t len = MAX_SOCKET_STRING;
  117. char my_endpoint[MAX_SOCKET_STRING];
  118. void *radio = test_context_socket (ZMQ_RADIO);
  119. bind_loopback (radio, ipv6_, my_endpoint, len);
  120. void *dish = test_context_socket (ZMQ_DISH);
  121. TEST_ASSERT_SUCCESS_ERRNO (
  122. zmq_setsockopt (dish, ZMQ_IPV6, &ipv6_, sizeof (int)));
  123. // Joining
  124. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, "Movies"));
  125. // Connecting
  126. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dish, my_endpoint));
  127. msleep (SETTLE_TIME);
  128. // This is not going to be sent as dish only subscribe to "Movies"
  129. msg_send_expect_success (radio, "TV", "Friends");
  130. // This is going to be sent to the dish
  131. msg_send_expect_success (radio, "Movies", "Godfather");
  132. // Check the correct message arrived
  133. msg_recv_cmp (dish, "Movies", "Godfather");
  134. // Join group during connection optvallen
  135. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, "TV"));
  136. zmq_sleep (1);
  137. // This should arrive now as we joined the group
  138. msg_send_expect_success (radio, "TV", "Friends");
  139. // Check the correct message arrived
  140. msg_recv_cmp (dish, "TV", "Friends");
  141. // Leaving group
  142. TEST_ASSERT_SUCCESS_ERRNO (zmq_leave (dish, "TV"));
  143. zmq_sleep (1);
  144. // This is not going to be sent as dish only subscribe to "Movies"
  145. msg_send_expect_success (radio, "TV", "Friends");
  146. // This is going to be sent to the dish
  147. msg_send_expect_success (radio, "Movies", "Godfather");
  148. // test zmq_poll with dish
  149. zmq_pollitem_t items[] = {
  150. {radio, 0, ZMQ_POLLIN, 0}, // read publications
  151. {dish, 0, ZMQ_POLLIN, 0}, // read subscriptions
  152. };
  153. int rc = zmq_poll (items, 2, 2000);
  154. TEST_ASSERT_EQUAL_INT (1, rc);
  155. TEST_ASSERT_EQUAL_INT (ZMQ_POLLIN, items[1].revents);
  156. // Check the correct message arrived
  157. msg_recv_cmp (dish, "Movies", "Godfather");
  158. test_context_socket_close (dish);
  159. test_context_socket_close (radio);
  160. }
  161. MAKE_TEST_V4V6 (test_radio_dish_tcp_poll)
  162. void test_dish_connect_fails (int ipv6_)
  163. {
  164. void *dish = test_context_socket (ZMQ_DISH);
  165. TEST_ASSERT_SUCCESS_ERRNO (
  166. zmq_setsockopt (dish, ZMQ_IPV6, &ipv6_, sizeof (int)));
  167. const char *url = ipv6_ ? "udp://[::1]:5556" : "udp://127.0.0.1:5556";
  168. // Connecting dish should fail
  169. TEST_ASSERT_FAILURE_ERRNO (ENOCOMPATPROTO, zmq_connect (dish, url));
  170. test_context_socket_close (dish);
  171. }
  172. MAKE_TEST_V4V6 (test_dish_connect_fails)
  173. void test_radio_bind_fails (int ipv6_)
  174. {
  175. void *radio = test_context_socket (ZMQ_RADIO);
  176. TEST_ASSERT_SUCCESS_ERRNO (
  177. zmq_setsockopt (radio, ZMQ_IPV6, &ipv6_, sizeof (int)));
  178. // Connecting dish should fail
  179. // Bind radio should fail
  180. TEST_ASSERT_FAILURE_ERRNO (ENOCOMPATPROTO,
  181. zmq_bind (radio, "udp://*:5556"));
  182. test_context_socket_close (radio);
  183. }
  184. MAKE_TEST_V4V6 (test_radio_bind_fails)
  185. void test_radio_dish_udp (int ipv6_)
  186. {
  187. void *radio = test_context_socket (ZMQ_RADIO);
  188. void *dish = test_context_socket (ZMQ_DISH);
  189. TEST_ASSERT_SUCCESS_ERRNO (
  190. zmq_setsockopt (radio, ZMQ_IPV6, &ipv6_, sizeof (int)));
  191. TEST_ASSERT_SUCCESS_ERRNO (
  192. zmq_setsockopt (dish, ZMQ_IPV6, &ipv6_, sizeof (int)));
  193. const char *radio_url = ipv6_ ? "udp://[::1]:5556" : "udp://127.0.0.1:5556";
  194. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (dish, "udp://*:5556"));
  195. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (radio, radio_url));
  196. msleep (SETTLE_TIME);
  197. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, "TV"));
  198. msg_send_expect_success (radio, "TV", "Friends");
  199. msg_recv_cmp (dish, "TV", "Friends");
  200. test_context_socket_close (dish);
  201. test_context_socket_close (radio);
  202. }
  203. MAKE_TEST_V4V6 (test_radio_dish_udp)
  204. #define MCAST_IPV4 "226.8.5.5"
  205. #define MCAST_IPV6 "ff02::7a65:726f:6df1:0a01"
  206. static const char *mcast_url (int ipv6_)
  207. {
  208. if (ipv6_) {
  209. return "udp://[" MCAST_IPV6 "]:5555";
  210. }
  211. return "udp://" MCAST_IPV4 ":5555";
  212. }
  213. // OSX uses a different name for this socket option
  214. #ifndef IPV6_ADD_MEMBERSHIP
  215. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  216. #endif
  217. union sa_u
  218. {
  219. struct sockaddr generic;
  220. struct sockaddr_in ipv4;
  221. struct sockaddr_in6 ipv6;
  222. };
  223. // Test if multicast is available on this machine by attempting to
  224. // send a receive a multicast datagram
  225. static bool is_multicast_available (int ipv6_)
  226. {
  227. int family = ipv6_ ? AF_INET6 : AF_INET;
  228. fd_t bind_sock = retired_fd;
  229. fd_t send_sock = retired_fd;
  230. int port = 5555;
  231. bool success = false;
  232. const char *msg = "it works";
  233. char buf[32];
  234. union sa_u any;
  235. union sa_u mcast;
  236. socklen_t sl;
  237. int rc;
  238. if (ipv6_) {
  239. struct sockaddr_in6 *any_ipv6 = &any.ipv6;
  240. struct sockaddr_in6 *mcast_ipv6 = &mcast.ipv6;
  241. any_ipv6->sin6_family = AF_INET6;
  242. any_ipv6->sin6_port = htons (port);
  243. any_ipv6->sin6_flowinfo = 0;
  244. any_ipv6->sin6_scope_id = 0;
  245. rc = test_inet_pton (AF_INET6, "::", &any_ipv6->sin6_addr);
  246. if (rc == 0) {
  247. goto out;
  248. }
  249. *mcast_ipv6 = *any_ipv6;
  250. rc = test_inet_pton (AF_INET6, MCAST_IPV6, &mcast_ipv6->sin6_addr);
  251. if (rc == 0) {
  252. goto out;
  253. }
  254. sl = sizeof (*any_ipv6);
  255. } else {
  256. struct sockaddr_in *any_ipv4 = &any.ipv4;
  257. struct sockaddr_in *mcast_ipv4 = &mcast.ipv4;
  258. any_ipv4->sin_family = AF_INET;
  259. any_ipv4->sin_port = htons (5555);
  260. rc = test_inet_pton (AF_INET, "0.0.0.0", &any_ipv4->sin_addr);
  261. if (rc == 0) {
  262. goto out;
  263. }
  264. *mcast_ipv4 = *any_ipv4;
  265. rc = test_inet_pton (AF_INET, MCAST_IPV4, &mcast_ipv4->sin_addr);
  266. if (rc == 0) {
  267. goto out;
  268. }
  269. sl = sizeof (*any_ipv4);
  270. }
  271. bind_sock = socket (family, SOCK_DGRAM, IPPROTO_UDP);
  272. if (bind_sock < 0) {
  273. goto out;
  274. }
  275. send_sock = socket (family, SOCK_DGRAM, IPPROTO_UDP);
  276. if (bind_sock < 0) {
  277. goto out;
  278. }
  279. rc = bind (bind_sock, &any.generic, sl);
  280. if (rc < 0) {
  281. goto out;
  282. }
  283. if (ipv6_) {
  284. struct ipv6_mreq mreq;
  285. const sockaddr_in6 *const mcast_ipv6 = &mcast.ipv6;
  286. mreq.ipv6mr_multiaddr = mcast_ipv6->sin6_addr;
  287. mreq.ipv6mr_interface = 0;
  288. rc = setsockopt (bind_sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
  289. as_setsockopt_opt_t (&mreq), sizeof (mreq));
  290. if (rc < 0) {
  291. goto out;
  292. }
  293. int loop = 1;
  294. rc = setsockopt (send_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  295. as_setsockopt_opt_t (&loop), sizeof (loop));
  296. if (rc < 0) {
  297. goto out;
  298. }
  299. } else {
  300. struct ip_mreq mreq;
  301. const sockaddr_in *const mcast_ipv4 = &mcast.ipv4;
  302. mreq.imr_multiaddr = mcast_ipv4->sin_addr;
  303. mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  304. rc = setsockopt (bind_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  305. as_setsockopt_opt_t (&mreq), sizeof (mreq));
  306. if (rc < 0) {
  307. goto out;
  308. }
  309. int loop = 1;
  310. rc = setsockopt (send_sock, IPPROTO_IP, IP_MULTICAST_LOOP,
  311. as_setsockopt_opt_t (&loop), sizeof (loop));
  312. if (rc < 0) {
  313. goto out;
  314. }
  315. }
  316. msleep (SETTLE_TIME);
  317. rc = sendto (send_sock, msg, static_cast<socklen_t> (strlen (msg)), 0,
  318. &mcast.generic, sl);
  319. if (rc < 0) {
  320. goto out;
  321. }
  322. msleep (SETTLE_TIME);
  323. rc = recvfrom (bind_sock, buf, sizeof (buf) - 1, 0, NULL, 0);
  324. if (rc < 0) {
  325. goto out;
  326. }
  327. buf[rc] = '\0';
  328. success = (strcmp (msg, buf) == 0);
  329. out:
  330. if (bind_sock >= 0) {
  331. close (bind_sock);
  332. }
  333. if (send_sock >= 0) {
  334. close (send_sock);
  335. }
  336. return success;
  337. }
  338. static void ignore_if_unavailable (int ipv6_)
  339. {
  340. if (ipv6_ && !is_ipv6_available ())
  341. TEST_IGNORE_MESSAGE ("No IPV6 available");
  342. if (!is_multicast_available (ipv6_))
  343. TEST_IGNORE_MESSAGE ("No multicast available");
  344. }
  345. static void test_radio_dish_mcast (int ipv6_)
  346. {
  347. ignore_if_unavailable (ipv6_);
  348. void *radio = test_context_socket (ZMQ_RADIO);
  349. void *dish = test_context_socket (ZMQ_DISH);
  350. TEST_ASSERT_SUCCESS_ERRNO (
  351. zmq_setsockopt (radio, ZMQ_IPV6, &ipv6_, sizeof (int)));
  352. TEST_ASSERT_SUCCESS_ERRNO (
  353. zmq_setsockopt (dish, ZMQ_IPV6, &ipv6_, sizeof (int)));
  354. const char *url = mcast_url (ipv6_);
  355. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (dish, url));
  356. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (radio, url));
  357. msleep (SETTLE_TIME);
  358. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, "TV"));
  359. msg_send_expect_success (radio, "TV", "Friends");
  360. msg_recv_cmp (dish, "TV", "Friends");
  361. test_context_socket_close (dish);
  362. test_context_socket_close (radio);
  363. }
  364. MAKE_TEST_V4V6 (test_radio_dish_mcast)
  365. static void test_radio_dish_no_loop (int ipv6_)
  366. {
  367. #ifdef _WIN32
  368. TEST_IGNORE_MESSAGE (
  369. "ZMQ_MULTICAST_LOOP=false does not appear to work on Windows (TODO)");
  370. #endif
  371. ignore_if_unavailable (ipv6_);
  372. void *radio = test_context_socket (ZMQ_RADIO);
  373. void *dish = test_context_socket (ZMQ_DISH);
  374. TEST_ASSERT_SUCCESS_ERRNO (
  375. zmq_setsockopt (radio, ZMQ_IPV6, &ipv6_, sizeof (int)));
  376. TEST_ASSERT_SUCCESS_ERRNO (
  377. zmq_setsockopt (dish, ZMQ_IPV6, &ipv6_, sizeof (int)));
  378. // Disable multicast loop for radio
  379. int loop = 0;
  380. TEST_ASSERT_SUCCESS_ERRNO (
  381. zmq_setsockopt (radio, ZMQ_MULTICAST_LOOP, &loop, sizeof (int)));
  382. const char *url = mcast_url (ipv6_);
  383. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (dish, url));
  384. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (radio, url));
  385. msleep (SETTLE_TIME);
  386. TEST_ASSERT_SUCCESS_ERRNO (zmq_join (dish, "TV"));
  387. msg_send_expect_success (radio, "TV", "Friends");
  388. // Looping is disabled, we shouldn't receive anything
  389. msleep (SETTLE_TIME);
  390. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (dish, NULL, 0, ZMQ_DONTWAIT));
  391. test_context_socket_close (dish);
  392. test_context_socket_close (radio);
  393. }
  394. MAKE_TEST_V4V6 (test_radio_dish_no_loop)
  395. int main (void)
  396. {
  397. setup_test_environment ();
  398. UNITY_BEGIN ();
  399. RUN_TEST (test_leave_unjoined_fails);
  400. RUN_TEST (test_join_too_long_fails);
  401. RUN_TEST (test_long_group);
  402. RUN_TEST (test_join_twice_fails);
  403. RUN_TEST (test_radio_bind_fails_ipv4);
  404. RUN_TEST (test_radio_bind_fails_ipv6);
  405. RUN_TEST (test_dish_connect_fails_ipv4);
  406. RUN_TEST (test_dish_connect_fails_ipv6);
  407. RUN_TEST (test_radio_dish_tcp_poll_ipv4);
  408. RUN_TEST (test_radio_dish_tcp_poll_ipv6);
  409. RUN_TEST (test_radio_dish_udp_ipv4);
  410. RUN_TEST (test_radio_dish_udp_ipv6);
  411. RUN_TEST (test_radio_dish_mcast_ipv4);
  412. RUN_TEST (test_radio_dish_no_loop_ipv4);
  413. RUN_TEST (test_radio_dish_mcast_ipv6);
  414. RUN_TEST (test_radio_dish_no_loop_ipv6);
  415. return UNITY_END ();
  416. }