test_security_zap.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_security.hpp"
  25. #include "testutil_unity.hpp"
  26. SETUP_TEARDOWN_TESTCONTEXT
  27. static void zap_handler_wrong_version (void * /*unused_*/)
  28. {
  29. zap_handler_generic (zap_wrong_version);
  30. }
  31. static void zap_handler_wrong_request_id (void * /*unused_*/)
  32. {
  33. zap_handler_generic (zap_wrong_request_id);
  34. }
  35. static void zap_handler_wrong_status_invalid (void * /*unused_*/)
  36. {
  37. zap_handler_generic (zap_status_invalid);
  38. }
  39. static void zap_handler_wrong_status_temporary_failure (void * /*unused_*/)
  40. {
  41. zap_handler_generic (zap_status_temporary_failure);
  42. }
  43. static void zap_handler_wrong_status_internal_error (void * /*unused_*/)
  44. {
  45. zap_handler_generic (zap_status_internal_error);
  46. }
  47. static void zap_handler_too_many_parts (void * /*unused_*/)
  48. {
  49. zap_handler_generic (zap_too_many_parts);
  50. }
  51. static void zap_handler_disconnect (void * /*unused_*/)
  52. {
  53. zap_handler_generic (zap_disconnect);
  54. }
  55. static void zap_handler_do_not_recv (void * /*unused_*/)
  56. {
  57. zap_handler_generic (zap_do_not_recv);
  58. }
  59. static void zap_handler_do_not_send (void * /*unused_*/)
  60. {
  61. zap_handler_generic (zap_do_not_send);
  62. }
  63. int expect_new_client_bounce_fail_and_count_monitor_events (
  64. char *my_endpoint_,
  65. void *server_,
  66. socket_config_fn socket_config_,
  67. void *socket_config_data_,
  68. void **client_mon_,
  69. void *server_mon_,
  70. int expected_server_event_,
  71. int expected_server_value_,
  72. int expected_client_event_ = 0,
  73. int expected_client_value_ = 0)
  74. {
  75. expect_new_client_bounce_fail (
  76. my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
  77. expected_client_event_, expected_client_value_);
  78. int events_received = 0;
  79. events_received = expect_monitor_event_multiple (
  80. server_mon_, expected_server_event_, expected_server_value_);
  81. return events_received;
  82. }
  83. void test_zap_unsuccessful (char *my_endpoint_,
  84. void *server_,
  85. void *server_mon_,
  86. int expected_server_event_,
  87. int expected_server_value_,
  88. socket_config_fn socket_config_,
  89. void *socket_config_data_,
  90. void **client_mon_ = NULL,
  91. int expected_client_event_ = 0,
  92. int expected_client_value_ = 0)
  93. {
  94. int server_events_received =
  95. expect_new_client_bounce_fail_and_count_monitor_events (
  96. my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
  97. server_mon_, expected_server_event_, expected_server_value_,
  98. expected_client_event_, expected_client_value_);
  99. // there may be more than one ZAP request due to repeated attempts by the
  100. // client (actually only in case if ZAP status code 300)
  101. TEST_ASSERT_TRUE (server_events_received == 0
  102. || 1 <= zmq_atomic_counter_value (zap_requests_handled));
  103. }
  104. void test_zap_unsuccessful_no_handler (char *my_endpoint_,
  105. void *server_,
  106. void *server_mon_,
  107. int expected_event_,
  108. int expected_err_,
  109. socket_config_fn socket_config_,
  110. void *socket_config_data_,
  111. void **client_mon_ = NULL)
  112. {
  113. const int events_received =
  114. expect_new_client_bounce_fail_and_count_monitor_events (
  115. my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
  116. server_mon_, expected_event_, expected_err_);
  117. // there may be more than one ZAP request due to repeated attempts by the
  118. // client
  119. TEST_ASSERT_GREATER_THAN_INT (0, events_received);
  120. }
  121. void test_zap_protocol_error (char *my_endpoint_,
  122. void *server_,
  123. void *server_mon_,
  124. socket_config_fn socket_config_,
  125. void *socket_config_data_,
  126. int expected_error_)
  127. {
  128. test_zap_unsuccessful (my_endpoint_, server_, server_mon_,
  129. ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, expected_error_,
  130. socket_config_, socket_config_data_);
  131. }
  132. void test_zap_unsuccessful_status_300 (char *my_endpoint_,
  133. void *server_,
  134. void *server_mon_,
  135. socket_config_fn client_socket_config_,
  136. void *client_socket_config_data_)
  137. {
  138. void *client_mon;
  139. test_zap_unsuccessful (
  140. my_endpoint_, server_, server_mon_, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 300,
  141. client_socket_config_, client_socket_config_data_, &client_mon);
  142. // we can use a 0 timeout here, since the client socket is already closed
  143. assert_no_more_monitor_events_with_timeout (client_mon, 0);
  144. test_context_socket_close (client_mon);
  145. }
  146. void test_zap_unsuccessful_status_500 (char *my_endpoint_,
  147. void *server_,
  148. void *server_mon_,
  149. socket_config_fn client_socket_config_,
  150. void *client_socket_config_data_)
  151. {
  152. test_zap_unsuccessful (my_endpoint_, server_, server_mon_,
  153. ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500,
  154. client_socket_config_, client_socket_config_data_,
  155. NULL, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500);
  156. }
  157. static void
  158. test_zap_protocol_error_closure (socket_config_fn server_socket_config_,
  159. socket_config_fn client_socket_config_,
  160. void *client_socket_config_data_,
  161. void *server_socket_config_data_,
  162. zmq_thread_fn zap_handler_,
  163. int expected_failure_)
  164. {
  165. void *handler, *zap_thread, *server, *server_mon;
  166. char my_endpoint[MAX_SOCKET_STRING];
  167. setup_context_and_server_side (
  168. &handler, &zap_thread, &server, &server_mon, my_endpoint, zap_handler_,
  169. server_socket_config_, server_socket_config_data_);
  170. test_zap_protocol_error (my_endpoint, server, server_mon,
  171. client_socket_config_, client_socket_config_data_,
  172. expected_failure_);
  173. shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
  174. }
  175. static void
  176. test_zap_protocol_error_wrong_version (socket_config_fn server_socket_config_,
  177. socket_config_fn client_socket_config_,
  178. void *client_socket_config_data_,
  179. void *server_socket_config_data_)
  180. {
  181. test_zap_protocol_error_closure (
  182. server_socket_config_, client_socket_config_, client_socket_config_data_,
  183. server_socket_config_data_, &zap_handler_wrong_version,
  184. ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION);
  185. }
  186. static void test_zap_protocol_error_wrong_request_id (
  187. socket_config_fn server_socket_config_,
  188. socket_config_fn client_socket_config_,
  189. void *client_socket_config_data_,
  190. void *server_socket_config_data_)
  191. {
  192. test_zap_protocol_error_closure (
  193. server_socket_config_, client_socket_config_, client_socket_config_data_,
  194. server_socket_config_data_, &zap_handler_wrong_request_id,
  195. ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID);
  196. }
  197. static void test_zap_protocol_error_wrong_status_invalid (
  198. socket_config_fn server_socket_config_,
  199. socket_config_fn client_socket_config_,
  200. void *client_socket_config_data_,
  201. void *server_socket_config_data_)
  202. {
  203. test_zap_protocol_error_closure (
  204. server_socket_config_, client_socket_config_, client_socket_config_data_,
  205. server_socket_config_data_, &zap_handler_wrong_status_invalid,
  206. ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE);
  207. }
  208. static void
  209. test_zap_protocol_error_too_many_parts (socket_config_fn server_socket_config_,
  210. socket_config_fn client_socket_config_,
  211. void *client_socket_config_data_,
  212. void *server_socket_config_data_)
  213. {
  214. test_zap_protocol_error_closure (
  215. server_socket_config_, client_socket_config_, client_socket_config_data_,
  216. server_socket_config_data_, &zap_handler_too_many_parts,
  217. ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY);
  218. }
  219. // TODO the failed status (300/500) should be observable as monitoring events on the client side as well (they are
  220. // already transmitted as an ERROR message)
  221. static void
  222. test_zap_wrong_status_temporary_failure (socket_config_fn server_socket_config_,
  223. socket_config_fn client_socket_config_,
  224. void *client_socket_config_data_,
  225. void *server_socket_config_data_)
  226. {
  227. void *handler, *zap_thread, *server, *server_mon;
  228. char my_endpoint[MAX_SOCKET_STRING];
  229. setup_context_and_server_side (
  230. &handler, &zap_thread, &server, &server_mon, my_endpoint,
  231. &zap_handler_wrong_status_temporary_failure, server_socket_config_,
  232. server_socket_config_data_);
  233. test_zap_unsuccessful_status_300 (my_endpoint, server, server_mon,
  234. client_socket_config_,
  235. client_socket_config_data_);
  236. shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
  237. }
  238. static void
  239. test_zap_wrong_status_internal_error (socket_config_fn server_socket_config_,
  240. socket_config_fn client_socket_config_,
  241. void *client_socket_config_data_)
  242. {
  243. void *handler, *zap_thread, *server, *server_mon;
  244. char my_endpoint[MAX_SOCKET_STRING];
  245. setup_context_and_server_side (
  246. &handler, &zap_thread, &server, &server_mon, my_endpoint,
  247. &zap_handler_wrong_status_internal_error, server_socket_config_);
  248. test_zap_unsuccessful_status_500 (my_endpoint, server, server_mon,
  249. client_socket_config_,
  250. client_socket_config_data_);
  251. shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
  252. }
  253. static void
  254. test_zap_unsuccesful_no_handler_started (socket_config_fn server_socket_config_,
  255. socket_config_fn client_socket_config_,
  256. void *client_socket_config_data_,
  257. void *server_socket_config_data_)
  258. {
  259. #ifdef ZMQ_ZAP_ENFORCE_DOMAIN
  260. void *handler, *zap_thread, *server, *server_mon;
  261. char my_endpoint[MAX_SOCKET_STRING];
  262. // TODO this looks wrong, where will the enforce value be used?
  263. // no ZAP handler
  264. int enforce = 1;
  265. setup_context_and_server_side (
  266. &handler, &zap_thread, &server, &server_mon, my_endpoint, NULL,
  267. server_socket_config_,
  268. server_socket_config_data_ ? server_socket_config_data_ : &enforce);
  269. test_zap_unsuccessful_no_handler (
  270. my_endpoint, server, server_mon, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
  271. EFAULT, client_socket_config_, client_socket_config_data_);
  272. shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
  273. #endif
  274. }
  275. static void
  276. test_zap_unsuccesful_no_handler_closure (socket_config_fn server_socket_config_,
  277. socket_config_fn client_socket_config_,
  278. void *client_socket_config_data_,
  279. zmq_thread_fn zap_handler_func_,
  280. bool zap_handler_disconnected_ = false)
  281. {
  282. void *handler, *zap_thread, *server, *server_mon;
  283. char my_endpoint[MAX_SOCKET_STRING];
  284. setup_context_and_server_side (&handler, &zap_thread, &server, &server_mon,
  285. my_endpoint, zap_handler_func_,
  286. server_socket_config_);
  287. test_zap_unsuccessful_no_handler (
  288. my_endpoint, server, server_mon, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
  289. EPIPE, client_socket_config_, client_socket_config_data_);
  290. shutdown_context_and_server_side (zap_thread, server, server_mon, handler,
  291. zap_handler_disconnected_);
  292. }
  293. static void
  294. test_zap_unsuccesful_disconnect (socket_config_fn server_socket_config_,
  295. socket_config_fn client_socket_config_,
  296. void *client_socket_config_data_)
  297. {
  298. test_zap_unsuccesful_no_handler_closure (
  299. server_socket_config_, client_socket_config_, client_socket_config_data_,
  300. &zap_handler_disconnect, true);
  301. }
  302. static void
  303. test_zap_unsuccesful_do_not_recv (socket_config_fn server_socket_config_,
  304. socket_config_fn client_socket_config_,
  305. void *client_socket_config_data_)
  306. {
  307. test_zap_unsuccesful_no_handler_closure (
  308. server_socket_config_, client_socket_config_, client_socket_config_data_,
  309. &zap_handler_do_not_recv);
  310. }
  311. static void
  312. test_zap_unsuccesful_do_not_send (socket_config_fn server_socket_config_,
  313. socket_config_fn client_socket_config_,
  314. void *client_socket_config_data_)
  315. {
  316. test_zap_unsuccesful_no_handler_closure (
  317. server_socket_config_, client_socket_config_, client_socket_config_data_,
  318. &zap_handler_do_not_send);
  319. }
  320. #define DEFINE_ZAP_ERROR_TESTS( \
  321. name_, server_socket_config_, server_socket_config_data_, \
  322. client_socket_config_, client_socket_config_data_) \
  323. void test_zap_protocol_error_wrong_version_##name_ () \
  324. { \
  325. test_zap_protocol_error_wrong_version ( \
  326. server_socket_config_, client_socket_config_, \
  327. client_socket_config_data_, server_socket_config_data_); \
  328. } \
  329. void test_zap_protocol_error_wrong_request_id_##name_ () \
  330. { \
  331. test_zap_protocol_error_wrong_request_id ( \
  332. server_socket_config_, client_socket_config_, \
  333. client_socket_config_data_, server_socket_config_data_); \
  334. } \
  335. void test_zap_protocol_error_wrong_status_invalid_##name_ () \
  336. { \
  337. test_zap_protocol_error_wrong_status_invalid ( \
  338. server_socket_config_, client_socket_config_, \
  339. client_socket_config_data_, server_socket_config_data_); \
  340. } \
  341. void test_zap_protocol_error_too_many_parts_##name_ () \
  342. { \
  343. test_zap_protocol_error_too_many_parts ( \
  344. server_socket_config_, client_socket_config_, \
  345. client_socket_config_data_, server_socket_config_data_); \
  346. } \
  347. void test_zap_wrong_status_temporary_failure_##name_ () \
  348. { \
  349. test_zap_wrong_status_temporary_failure ( \
  350. server_socket_config_, client_socket_config_, \
  351. client_socket_config_data_, server_socket_config_data_); \
  352. } \
  353. void test_zap_wrong_status_internal_error_##name_ () \
  354. { \
  355. test_zap_wrong_status_internal_error (server_socket_config_, \
  356. client_socket_config_, \
  357. client_socket_config_data_); \
  358. } \
  359. void test_zap_unsuccessful_no_handler_started_##name_ () \
  360. { \
  361. test_zap_unsuccesful_no_handler_started ( \
  362. server_socket_config_, client_socket_config_, \
  363. client_socket_config_data_, server_socket_config_data_); \
  364. } \
  365. void test_zap_unsuccessful_disconnect_##name_ () \
  366. { \
  367. test_zap_unsuccesful_disconnect (server_socket_config_, \
  368. client_socket_config_, \
  369. client_socket_config_data_); \
  370. } \
  371. void test_zap_unsuccessful_do_not_recv_##name_ () \
  372. { \
  373. test_zap_unsuccesful_do_not_recv (server_socket_config_, \
  374. client_socket_config_, \
  375. client_socket_config_data_); \
  376. } \
  377. void test_zap_unsuccessful_do_not_send_##name_ () \
  378. { \
  379. test_zap_unsuccesful_do_not_send (server_socket_config_, \
  380. client_socket_config_, \
  381. client_socket_config_data_); \
  382. }
  383. DEFINE_ZAP_ERROR_TESTS (
  384. null, &socket_config_null_server, NULL, &socket_config_null_client, NULL)
  385. DEFINE_ZAP_ERROR_TESTS (
  386. plain, &socket_config_plain_server, NULL, &socket_config_plain_client, NULL)
  387. static curve_client_data_t curve_client_data = {
  388. valid_server_public, valid_client_public, valid_client_secret};
  389. DEFINE_ZAP_ERROR_TESTS (curve,
  390. &socket_config_curve_server,
  391. valid_server_secret,
  392. &socket_config_curve_client,
  393. &curve_client_data)
  394. #define RUN_ZAP_ERROR_TESTS(name_) \
  395. { \
  396. RUN_TEST (test_zap_protocol_error_wrong_version_##name_); \
  397. RUN_TEST (test_zap_protocol_error_wrong_request_id_##name_); \
  398. RUN_TEST (test_zap_protocol_error_wrong_status_invalid_##name_); \
  399. RUN_TEST (test_zap_protocol_error_too_many_parts_##name_); \
  400. RUN_TEST (test_zap_wrong_status_temporary_failure_##name_); \
  401. RUN_TEST (test_zap_wrong_status_internal_error_##name_); \
  402. RUN_TEST (test_zap_unsuccessful_no_handler_started_##name_); \
  403. RUN_TEST (test_zap_unsuccessful_disconnect_##name_); \
  404. RUN_TEST (test_zap_unsuccessful_do_not_recv_##name_); \
  405. RUN_TEST (test_zap_unsuccessful_do_not_send_##name_); \
  406. }
  407. int main ()
  408. {
  409. setup_test_environment ();
  410. if (zmq_has ("curve")) {
  411. setup_testutil_security_curve ();
  412. }
  413. UNITY_BEGIN ();
  414. RUN_ZAP_ERROR_TESTS (null);
  415. RUN_ZAP_ERROR_TESTS (plain);
  416. if (zmq_has ("curve")) {
  417. RUN_ZAP_ERROR_TESTS (curve);
  418. }
  419. return UNITY_END ();
  420. }