test_security_null.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #if defined(ZMQ_HAVE_WINDOWS)
  27. #include <winsock2.h>
  28. #include <ws2tcpip.h>
  29. #include <stdexcept>
  30. #define close closesocket
  31. #else
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #include <unistd.h>
  36. #endif
  37. #include <stdlib.h>
  38. static void zap_handler (void *handler_)
  39. {
  40. // Process ZAP requests forever
  41. while (true) {
  42. char *version = s_recv (handler_);
  43. if (!version)
  44. break; // Terminating
  45. char *sequence = s_recv (handler_);
  46. char *domain = s_recv (handler_);
  47. char *address = s_recv (handler_);
  48. char *routing_id = s_recv (handler_);
  49. char *mechanism = s_recv (handler_);
  50. TEST_ASSERT_EQUAL_STRING ("1.0", version);
  51. TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
  52. send_string_expect_success (handler_, version, ZMQ_SNDMORE);
  53. send_string_expect_success (handler_, sequence, ZMQ_SNDMORE);
  54. if (streq (domain, "TEST")) {
  55. send_string_expect_success (handler_, "200", ZMQ_SNDMORE);
  56. send_string_expect_success (handler_, "OK", ZMQ_SNDMORE);
  57. send_string_expect_success (handler_, "anonymous", ZMQ_SNDMORE);
  58. send_string_expect_success (handler_, "", 0);
  59. } else {
  60. send_string_expect_success (handler_, "400", ZMQ_SNDMORE);
  61. send_string_expect_success (handler_, "BAD DOMAIN", ZMQ_SNDMORE);
  62. send_string_expect_success (handler_, "", ZMQ_SNDMORE);
  63. send_string_expect_success (handler_, "", 0);
  64. }
  65. free (version);
  66. free (sequence);
  67. free (domain);
  68. free (address);
  69. free (routing_id);
  70. free (mechanism);
  71. }
  72. close_zero_linger (handler_);
  73. }
  74. void *zap_thread;
  75. static void setup_zap_handler ()
  76. {
  77. // Spawn ZAP handler
  78. // We create and bind ZAP socket in main thread to avoid case
  79. // where child thread does not start up fast enough.
  80. void *handler = zmq_socket (get_test_context (), ZMQ_REP);
  81. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
  82. zap_thread = zmq_threadstart (&zap_handler, handler);
  83. }
  84. static void teardown_zap_handler ()
  85. {
  86. // Wait until ZAP handler terminates
  87. zmq_threadclose (zap_thread);
  88. }
  89. void setUp ()
  90. {
  91. setup_test_context ();
  92. setup_zap_handler ();
  93. }
  94. void tearDown ()
  95. {
  96. teardown_test_context ();
  97. teardown_zap_handler ();
  98. }
  99. void test_no_domain ()
  100. {
  101. // We first test client/server with no ZAP domain
  102. // Libzmq does not call our ZAP handler, the connect must succeed
  103. void *server = test_context_socket (ZMQ_DEALER);
  104. void *client = test_context_socket (ZMQ_DEALER);
  105. char my_endpoint[MAX_SOCKET_STRING];
  106. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  107. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  108. bounce (server, client);
  109. test_context_socket_close_zero_linger (client);
  110. test_context_socket_close_zero_linger (server);
  111. }
  112. void test_wrong_domain_fails ()
  113. {
  114. // Now define a ZAP domain for the server; this enables
  115. // authentication. We're using the wrong domain so this test
  116. // must fail.
  117. void *server = test_context_socket (ZMQ_DEALER);
  118. void *client = test_context_socket (ZMQ_DEALER);
  119. TEST_ASSERT_SUCCESS_ERRNO (
  120. zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5));
  121. char my_endpoint[MAX_SOCKET_STRING];
  122. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  123. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  124. expect_bounce_fail (server, client);
  125. test_context_socket_close_zero_linger (client);
  126. test_context_socket_close_zero_linger (server);
  127. }
  128. void test_success ()
  129. {
  130. // Now use the right domain, the test must pass
  131. void *server = test_context_socket (ZMQ_DEALER);
  132. void *client = test_context_socket (ZMQ_DEALER);
  133. TEST_ASSERT_SUCCESS_ERRNO (
  134. zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "TEST", 4));
  135. char my_endpoint[MAX_SOCKET_STRING];
  136. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  137. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  138. bounce (server, client);
  139. test_context_socket_close_zero_linger (client);
  140. test_context_socket_close_zero_linger (server);
  141. }
  142. void test_vanilla_socket ()
  143. {
  144. // Unauthenticated messages from a vanilla socket shouldn't be received
  145. void *server = test_context_socket (ZMQ_DEALER);
  146. TEST_ASSERT_SUCCESS_ERRNO (
  147. zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5));
  148. char my_endpoint[MAX_SOCKET_STRING];
  149. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  150. fd_t s = connect_socket (my_endpoint);
  151. // send anonymous ZMTP/1.0 greeting
  152. send (s, "\x01\x00", 2, 0);
  153. // send sneaky message that shouldn't be received
  154. send (s, "\x08\x00sneaky\0", 9, 0);
  155. int timeout = 250;
  156. zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
  157. char *buf = s_recv (server);
  158. if (buf != NULL) {
  159. printf ("Received unauthenticated message: %s\n", buf);
  160. TEST_ASSERT_NULL (buf);
  161. }
  162. close (s);
  163. test_context_socket_close_zero_linger (server);
  164. }
  165. int main ()
  166. {
  167. setup_test_environment ();
  168. UNITY_BEGIN ();
  169. RUN_TEST (test_no_domain);
  170. RUN_TEST (test_wrong_domain_fails);
  171. RUN_TEST (test_success);
  172. RUN_TEST (test_vanilla_socket);
  173. return UNITY_END ();
  174. }