test_security_plain.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include <stdlib.h>
  27. #include <string.h>
  28. static void zap_handler (void *zap_)
  29. {
  30. // Process ZAP requests forever
  31. while (true) {
  32. char *version = s_recv (zap_);
  33. if (!version)
  34. break; // Terminating
  35. char *sequence = s_recv (zap_);
  36. char *domain = s_recv (zap_);
  37. char *address = s_recv (zap_);
  38. char *routing_id = s_recv (zap_);
  39. char *mechanism = s_recv (zap_);
  40. char *username = s_recv (zap_);
  41. char *password = s_recv (zap_);
  42. TEST_ASSERT_EQUAL_STRING ("1.0", version);
  43. TEST_ASSERT_EQUAL_STRING ("PLAIN", mechanism);
  44. TEST_ASSERT_EQUAL_STRING ("IDENT", routing_id);
  45. send_string_expect_success (zap_, version, ZMQ_SNDMORE);
  46. send_string_expect_success (zap_, sequence, ZMQ_SNDMORE);
  47. if (streq (username, "admin") && streq (password, "password")) {
  48. send_string_expect_success (zap_, "200", ZMQ_SNDMORE);
  49. send_string_expect_success (zap_, "OK", ZMQ_SNDMORE);
  50. send_string_expect_success (zap_, "anonymous", ZMQ_SNDMORE);
  51. send_string_expect_success (zap_, "", 0);
  52. } else {
  53. send_string_expect_success (zap_, "400", ZMQ_SNDMORE);
  54. send_string_expect_success (zap_, "Invalid username or password",
  55. ZMQ_SNDMORE);
  56. send_string_expect_success (zap_, "", ZMQ_SNDMORE);
  57. send_string_expect_success (zap_, "", 0);
  58. }
  59. free (version);
  60. free (sequence);
  61. free (domain);
  62. free (address);
  63. free (routing_id);
  64. free (mechanism);
  65. free (username);
  66. free (password);
  67. }
  68. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (zap_));
  69. }
  70. void *zap_thread;
  71. char my_endpoint[MAX_SOCKET_STRING];
  72. static void setup_zap_handler ()
  73. {
  74. // Spawn ZAP handler
  75. // We create and bind ZAP socket in main thread to avoid case
  76. // where child thread does not start up fast enough.
  77. void *handler = zmq_socket (get_test_context (), ZMQ_REP);
  78. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
  79. zap_thread = zmq_threadstart (&zap_handler, handler);
  80. }
  81. static void teardown_zap_handler ()
  82. {
  83. // Wait until ZAP handler terminates
  84. zmq_threadclose (zap_thread);
  85. }
  86. const char domain[] = "test";
  87. void *server;
  88. static void setup_server ()
  89. {
  90. // Server socket will accept connections
  91. server = test_context_socket (ZMQ_DEALER);
  92. TEST_ASSERT_SUCCESS_ERRNO (
  93. zmq_setsockopt (server, ZMQ_ROUTING_ID, "IDENT", 6));
  94. TEST_ASSERT_SUCCESS_ERRNO (
  95. zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, domain, strlen (domain)));
  96. const int as_server = 1;
  97. TEST_ASSERT_SUCCESS_ERRNO (
  98. zmq_setsockopt (server, ZMQ_PLAIN_SERVER, &as_server, sizeof (int)));
  99. bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
  100. }
  101. static void teardown_server ()
  102. {
  103. test_context_socket_close (server);
  104. }
  105. void setUp ()
  106. {
  107. setup_test_context ();
  108. setup_zap_handler ();
  109. setup_server ();
  110. }
  111. void tearDown ()
  112. {
  113. teardown_server ();
  114. teardown_test_context ();
  115. teardown_zap_handler ();
  116. }
  117. void test_plain_success ()
  118. {
  119. // Check PLAIN security with correct username/password
  120. void *client = test_context_socket (ZMQ_DEALER);
  121. const char username[] = "admin";
  122. TEST_ASSERT_SUCCESS_ERRNO (
  123. zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, username, strlen (username)));
  124. const char password[] = "password";
  125. TEST_ASSERT_SUCCESS_ERRNO (
  126. zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, password, strlen (password)));
  127. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  128. bounce (server, client);
  129. test_context_socket_close (client);
  130. }
  131. void test_plain_client_as_server_fails ()
  132. {
  133. // Check PLAIN security with badly configured client (as_server)
  134. // This will be caught by the plain_server class, not passed to ZAP
  135. void *client = test_context_socket (ZMQ_DEALER);
  136. TEST_ASSERT_SUCCESS_ERRNO (
  137. zmq_setsockopt (client, ZMQ_ZAP_DOMAIN, domain, strlen (domain)));
  138. const int as_server = 1;
  139. TEST_ASSERT_SUCCESS_ERRNO (
  140. zmq_setsockopt (client, ZMQ_PLAIN_SERVER, &as_server, sizeof (int)));
  141. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  142. expect_bounce_fail (server, client);
  143. test_context_socket_close_zero_linger (client);
  144. }
  145. void test_plain_wrong_credentials_fails ()
  146. {
  147. // Check PLAIN security -- failed authentication
  148. void *client = test_context_socket (ZMQ_DEALER);
  149. const char username[] = "wronguser";
  150. const char password[] = "wrongpass";
  151. TEST_ASSERT_SUCCESS_ERRNO (
  152. zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, username, strlen (username)));
  153. TEST_ASSERT_SUCCESS_ERRNO (
  154. zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, password, strlen (password)));
  155. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  156. expect_bounce_fail (server, client);
  157. test_context_socket_close_zero_linger (client);
  158. }
  159. void test_plain_vanilla_socket ()
  160. {
  161. // Unauthenticated messages from a vanilla socket shouldn't be received
  162. fd_t s = connect_socket (my_endpoint);
  163. // send anonymous ZMTP/1.0 greeting
  164. send (s, "\x01\x00", 2, 0);
  165. // send sneaky message that shouldn't be received
  166. send (s, "\x08\x00sneaky\0", 9, 0);
  167. int timeout = 250;
  168. zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
  169. char *buf = s_recv (server);
  170. if (buf != NULL) {
  171. printf ("Received unauthenticated message: %s\n", buf);
  172. TEST_ASSERT_NULL (buf);
  173. }
  174. close (s);
  175. }
  176. int main (void)
  177. {
  178. setup_test_environment ();
  179. UNITY_BEGIN ();
  180. RUN_TEST (test_plain_success);
  181. RUN_TEST (test_plain_client_as_server_fails);
  182. RUN_TEST (test_plain_wrong_credentials_fails);
  183. RUN_TEST (test_plain_vanilla_socket);
  184. return UNITY_END ();
  185. }