testutil_security.hpp 6.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file

    This file is part of libzmq, the ZeroMQ core engine in C++.

    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __TESTUTIL_SECURITY_HPP_INCLUDED__
#define __TESTUTIL_SECURITY_HPP_INCLUDED__

33
#include "testutil_unity.hpp"
34
#include "testutil_monitoring.hpp"
35 36 37 38 39

//  security test utils

typedef void(socket_config_fn) (void *, void *);

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 51 52
//  CURVE specific functions

//  We'll generate random test keys at startup
53 54 55 56
extern char valid_client_public[41];
extern char valid_client_secret[41];
extern char valid_server_public[41];
extern char valid_server_secret[41];
57

58 59 60
void setup_testutil_security_curve ();

void socket_config_curve_server (void *server_, void *server_secret_);
61 62 63 64 65 66 67 68

struct curve_client_data_t
{
    const char *server_public;
    const char *client_public;
    const char *client_secret;
};

69
void socket_config_curve_client (void *client_, void *data_);
70 71 72 73 74 75 76

//  --------------------------------------------------------------------------
//  This methods receives and validates ZAP requests (allowing or denying
//  each client connection).

enum zap_protocol_t
{
77 78 79 80 81 82 83 84
    zap_ok,
    // ZAP-compliant non-standard cases
    zap_status_temporary_failure,
    zap_status_internal_error,
    // ZAP protocol errors
    zap_wrong_version,
    zap_wrong_request_id,
    zap_status_invalid,
85
    zap_too_many_parts,
86 87 88
    zap_disconnect,
    zap_do_not_recv,
    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 101

// assert_* are macros rather than functions, to allow assertion failures be
// attributed to the causing source code line
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#define assert_no_more_monitor_events_with_timeout(monitor, timeout)                  \
    {                                                                                 \
        int event_count = 0;                                                          \
        int event, err;                                                               \
        while ((event = get_monitor_event_with_timeout ((monitor), &err, NULL,        \
                                                        (timeout)))                   \
               != -1) {                                                               \
            if (event == ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL                         \
                && (err == EPIPE || err == ECONNRESET                                 \
                    || err == ECONNABORTED)) {                                        \
                fprintf (stderr,                                                      \
                         "Ignored event (skipping any further events): %x "           \
                         "(err = %i == %s)\n",                                        \
                         event, err, zmq_strerror (err));                             \
                continue;                                                             \
            }                                                                         \
            ++event_count;                                                            \
            /* TODO write this into a buffer and attach to the assertion msg below */ \
            print_unexpected_event_stderr (event, err, 0, 0);                         \
        }                                                                             \
        TEST_ASSERT_EQUAL_INT (0, event_count);                                       \
123 124
    }

125
void setup_context_and_server_side (
126 127 128 129 130
  void **zap_control_,
  void **zap_thread_,
  void **server_,
  void **server_mon_,
  char *my_endpoint_,
131 132 133
  zmq_thread_fn zap_handler_ = &zap_handler,
  socket_config_fn socket_config_ = &socket_config_curve_server,
  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 138 139
                                       void *server_,
                                       void *server_mon_,
                                       void *zap_control_,
140
                                       bool zap_handler_stopped_ = false);
141

142
void *create_and_connect_client (char *my_endpoint_,
143 144
                                 socket_config_fn socket_config_,
                                 void *socket_config_data_,
145
                                 void **client_mon_ = NULL);
146

147
void expect_new_client_bounce_fail (char *my_endpoint_,
148
                                    void *server_,
149 150
                                    socket_config_fn socket_config_,
                                    void *socket_config_data_,
151 152
                                    void **client_mon_ = NULL,
                                    int expected_client_event_ = 0,
153
                                    int expected_client_value_ = 0);
154

155
#endif