| 1 | /* |
| 2 | Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file |
| 3 | |
| 4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
| 5 | |
| 6 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #ifndef __TESTUTIL_SECURITY_HPP_INCLUDED__ |
| 31 | #define __TESTUTIL_SECURITY_HPP_INCLUDED__ |
| 32 | |
| 33 | #include "testutil_unity.hpp" |
| 34 | #include "testutil_monitoring.hpp" |
| 35 | |
| 36 | // security test utils |
| 37 | |
| 38 | typedef void(socket_config_fn) (void *, void *); |
| 39 | |
| 40 | // NULL specific functions |
| 41 | void socket_config_null_client (void *server_, void *server_secret_); |
| 42 | |
| 43 | void socket_config_null_server (void *server_, void *server_secret_); |
| 44 | |
| 45 | // PLAIN specific functions |
| 46 | void socket_config_plain_client (void *server_, void *server_secret_); |
| 47 | |
| 48 | void socket_config_plain_server (void *server_, void *server_secret_); |
| 49 | |
| 50 | // CURVE specific functions |
| 51 | |
| 52 | // We'll generate random test keys at startup |
| 53 | extern char valid_client_public[41]; |
| 54 | extern char valid_client_secret[41]; |
| 55 | extern char valid_server_public[41]; |
| 56 | extern char valid_server_secret[41]; |
| 57 | |
| 58 | void setup_testutil_security_curve (); |
| 59 | |
| 60 | void socket_config_curve_server (void *server_, void *server_secret_); |
| 61 | |
| 62 | struct curve_client_data_t |
| 63 | { |
| 64 | const char *server_public; |
| 65 | const char *client_public; |
| 66 | const char *client_secret; |
| 67 | }; |
| 68 | |
| 69 | void socket_config_curve_client (void *client_, void *data_); |
| 70 | |
| 71 | // -------------------------------------------------------------------------- |
| 72 | // This methods receives and validates ZAP requests (allowing or denying |
| 73 | // each client connection). |
| 74 | |
| 75 | enum zap_protocol_t |
| 76 | { |
| 77 | zap_ok, |
| 78 | // ZAP-compliant non-standard cases |
| 79 | zap_status_temporary_failure, |
| 80 | zap_status_internal_error, |
| 81 | // ZAP protocol errors |
| 82 | zap_wrong_version, |
| 83 | zap_wrong_request_id, |
| 84 | zap_status_invalid, |
| 85 | zap_too_many_parts, |
| 86 | zap_disconnect, |
| 87 | zap_do_not_recv, |
| 88 | zap_do_not_send |
| 89 | }; |
| 90 | |
| 91 | extern void *zap_requests_handled; |
| 92 | |
| 93 | void zap_handler_generic (zap_protocol_t zap_protocol_, |
| 94 | const char *expected_routing_id_ = "IDENT" ); |
| 95 | |
| 96 | void zap_handler (void * /*unused_*/); |
| 97 | |
| 98 | // Security-specific monitor event utilities |
| 99 | |
| 100 | // assert_* are macros rather than functions, to allow assertion failures be |
| 101 | // attributed to the causing source code line |
| 102 | #define assert_no_more_monitor_events_with_timeout(monitor, timeout) \ |
| 103 | { \ |
| 104 | int event_count = 0; \ |
| 105 | int event, err; \ |
| 106 | while ((event = get_monitor_event_with_timeout ((monitor), &err, NULL, \ |
| 107 | (timeout))) \ |
| 108 | != -1) { \ |
| 109 | if (event == ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL \ |
| 110 | && (err == EPIPE || err == ECONNRESET \ |
| 111 | || err == ECONNABORTED)) { \ |
| 112 | fprintf (stderr, \ |
| 113 | "Ignored event (skipping any further events): %x " \ |
| 114 | "(err = %i == %s)\n", \ |
| 115 | event, err, zmq_strerror (err)); \ |
| 116 | continue; \ |
| 117 | } \ |
| 118 | ++event_count; \ |
| 119 | /* TODO write this into a buffer and attach to the assertion msg below */ \ |
| 120 | print_unexpected_event_stderr (event, err, 0, 0); \ |
| 121 | } \ |
| 122 | TEST_ASSERT_EQUAL_INT (0, event_count); \ |
| 123 | } |
| 124 | |
| 125 | void setup_context_and_server_side ( |
| 126 | void **zap_control_, |
| 127 | void **zap_thread_, |
| 128 | void **server_, |
| 129 | void **server_mon_, |
| 130 | char *my_endpoint_, |
| 131 | zmq_thread_fn zap_handler_ = &zap_handler, |
| 132 | socket_config_fn socket_config_ = &socket_config_curve_server, |
| 133 | void *socket_config_data_ = valid_server_secret, |
| 134 | const char *routing_id_ = "IDENT" ); |
| 135 | |
| 136 | void shutdown_context_and_server_side (void *zap_thread_, |
| 137 | void *server_, |
| 138 | void *server_mon_, |
| 139 | void *zap_control_, |
| 140 | bool zap_handler_stopped_ = false); |
| 141 | |
| 142 | void *create_and_connect_client (char *my_endpoint_, |
| 143 | socket_config_fn socket_config_, |
| 144 | void *socket_config_data_, |
| 145 | void **client_mon_ = NULL); |
| 146 | |
| 147 | void expect_new_client_bounce_fail (char *my_endpoint_, |
| 148 | void *server_, |
| 149 | socket_config_fn socket_config_, |
| 150 | void *socket_config_data_, |
| 151 | void **client_mon_ = NULL, |
| 152 | int expected_client_event_ = 0, |
| 153 | int expected_client_value_ = 0); |
| 154 | |
| 155 | #endif |
| 156 | |