testutil_security.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef __TESTUTIL_SECURITY_HPP_INCLUDED__
  25. #define __TESTUTIL_SECURITY_HPP_INCLUDED__
  26. #include "testutil_unity.hpp"
  27. #include "testutil_monitoring.hpp"
  28. // security test utils
  29. typedef void(socket_config_fn) (void *, void *);
  30. // NULL specific functions
  31. void socket_config_null_client (void *server_, void *server_secret_);
  32. void socket_config_null_server (void *server_, void *server_secret_);
  33. // PLAIN specific functions
  34. void socket_config_plain_client (void *server_, void *server_secret_);
  35. void socket_config_plain_server (void *server_, void *server_secret_);
  36. // CURVE specific functions
  37. // We'll generate random test keys at startup
  38. extern char valid_client_public[41];
  39. extern char valid_client_secret[41];
  40. extern char valid_server_public[41];
  41. extern char valid_server_secret[41];
  42. void setup_testutil_security_curve ();
  43. void socket_config_curve_server (void *server_, void *server_secret_);
  44. struct curve_client_data_t
  45. {
  46. const char *server_public;
  47. const char *client_public;
  48. const char *client_secret;
  49. };
  50. void socket_config_curve_client (void *client_, void *data_);
  51. // --------------------------------------------------------------------------
  52. // This methods receives and validates ZAP requests (allowing or denying
  53. // each client connection).
  54. enum zap_protocol_t
  55. {
  56. zap_ok,
  57. // ZAP-compliant non-standard cases
  58. zap_status_temporary_failure,
  59. zap_status_internal_error,
  60. // ZAP protocol errors
  61. zap_wrong_version,
  62. zap_wrong_request_id,
  63. zap_status_invalid,
  64. zap_too_many_parts,
  65. zap_disconnect,
  66. zap_do_not_recv,
  67. zap_do_not_send
  68. };
  69. extern void *zap_requests_handled;
  70. void zap_handler_generic (zap_protocol_t zap_protocol_,
  71. const char *expected_routing_id_ = "IDENT");
  72. void zap_handler (void * /*unused_*/);
  73. // Security-specific monitor event utilities
  74. // assert_* are macros rather than functions, to allow assertion failures be
  75. // attributed to the causing source code line
  76. #define assert_no_more_monitor_events_with_timeout(monitor, timeout) \
  77. { \
  78. int event_count = 0; \
  79. int event, err; \
  80. while ((event = get_monitor_event_with_timeout ((monitor), &err, NULL, \
  81. (timeout))) \
  82. != -1) { \
  83. if (event == ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL \
  84. && (err == EPIPE || err == ECONNRESET \
  85. || err == ECONNABORTED)) { \
  86. fprintf (stderr, \
  87. "Ignored event (skipping any further events): %x " \
  88. "(err = %i == %s)\n", \
  89. event, err, zmq_strerror (err)); \
  90. continue; \
  91. } \
  92. ++event_count; \
  93. /* TODO write this into a buffer and attach to the assertion msg below */ \
  94. print_unexpected_event_stderr (event, err, 0, 0); \
  95. } \
  96. TEST_ASSERT_EQUAL_INT (0, event_count); \
  97. }
  98. void setup_context_and_server_side (
  99. void **zap_control_,
  100. void **zap_thread_,
  101. void **server_,
  102. void **server_mon_,
  103. char *my_endpoint_,
  104. zmq_thread_fn zap_handler_ = &zap_handler,
  105. socket_config_fn socket_config_ = &socket_config_curve_server,
  106. void *socket_config_data_ = valid_server_secret,
  107. const char *routing_id_ = "IDENT");
  108. void shutdown_context_and_server_side (void *zap_thread_,
  109. void *server_,
  110. void *server_mon_,
  111. void *zap_control_,
  112. bool zap_handler_stopped_ = false);
  113. void *create_and_connect_client (char *my_endpoint_,
  114. socket_config_fn socket_config_,
  115. void *socket_config_data_,
  116. void **client_mon_ = NULL);
  117. void expect_new_client_bounce_fail (char *my_endpoint_,
  118. void *server_,
  119. socket_config_fn socket_config_,
  120. void *socket_config_data_,
  121. void **client_mon_ = NULL,
  122. int expected_client_event_ = 0,
  123. int expected_client_value_ = 0);
  124. #endif