test_security_zap.cpp 15.5 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/*
    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/>.
*/

#include "testutil_security.hpp"

static void zap_handler_wrong_version (void *ctx)
{
    zap_handler_generic (ctx, zap_wrong_version);
}

static void zap_handler_wrong_request_id (void *ctx)
{
    zap_handler_generic (ctx, zap_wrong_request_id);
}

static void zap_handler_wrong_status_invalid (void *ctx)
{
    zap_handler_generic (ctx, zap_status_invalid);
}

static void zap_handler_wrong_status_temporary_failure (void *ctx)
{
    zap_handler_generic (ctx, zap_status_temporary_failure);
}

static void zap_handler_wrong_status_internal_error (void *ctx)
{
    zap_handler_generic (ctx, zap_status_internal_error);
}

static void zap_handler_too_many_parts (void *ctx)
{
    zap_handler_generic (ctx, zap_too_many_parts);
}

62 63 64 65 66
static void zap_handler_disconnect (void *ctx)
{
    zap_handler_generic (ctx, zap_disconnect);
}

67 68 69 70 71 72 73 74 75 76
static void zap_handler_do_not_recv (void *ctx)
{
    zap_handler_generic (ctx, zap_do_not_recv);
}

static void zap_handler_do_not_send (void *ctx)
{
    zap_handler_generic (ctx, zap_do_not_send);
}

77 78 79 80 81 82 83 84
int expect_new_client_bounce_fail_and_count_monitor_events (
  void *ctx,
  char *my_endpoint,
  void *server,
  socket_config_fn socket_config_,
  void *socket_config_data_,
  void **client_mon,
  void *server_mon,
85 86 87 88
  int expected_server_event,
  int expected_server_value,
  int expected_client_event = 0,
  int expected_client_value = 0)
89
{
90 91 92
    expect_new_client_bounce_fail (
      ctx, my_endpoint, server, socket_config_, socket_config_data_, client_mon,
      expected_client_event, expected_client_value);
93 94 95

    int events_received = 0;
#ifdef ZMQ_BUILD_DRAFT_API
96 97
    events_received = expect_monitor_event_multiple (
      server_mon, expected_server_event, expected_server_value);
98 99 100 101 102
#endif

    return events_received;
}

103 104 105 106
void test_zap_unsuccessful (void *ctx,
                            char *my_endpoint,
                            void *server,
                            void *server_mon,
107 108
                            int expected_server_event,
                            int expected_server_value,
109
                            socket_config_fn socket_config_,
110
                            void *socket_config_data_,
111 112 113
                            void **client_mon = NULL,
                            int expected_client_event = 0,
                            int expected_client_value = 0)
114
{
115
    int server_events_received =
116 117
      expect_new_client_bounce_fail_and_count_monitor_events (
        ctx, my_endpoint, server, socket_config_, socket_config_data_,
118 119
        client_mon, server_mon, expected_server_event, expected_server_value,
        expected_client_event, expected_client_value);
120

121
    //  there may be more than one ZAP request due to repeated attempts by the
122
    //  client (actually only in case if ZAP status code 300)
123
    assert (server_events_received == 0
124 125 126
            || 1 <= zmq_atomic_counter_value (zap_requests_handled));
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void test_zap_unsuccessful_no_handler (void *ctx,
                                       char *my_endpoint,
                                       void *server,
                                       void *server_mon,
                                       int expected_event,
                                       int expected_err,
                                       socket_config_fn socket_config_,
                                       void *socket_config_data_,
                                       void **client_mon = NULL)
{
    int events_received =
      expect_new_client_bounce_fail_and_count_monitor_events (
        ctx, my_endpoint, server, socket_config_, socket_config_data_,
        client_mon, server_mon, expected_event, expected_err);

#ifdef ZMQ_BUILD_DRAFT_API
    //  there may be more than one ZAP request due to repeated attempts by the
    //  client
    assert (events_received > 0);
#else
    LIBZMQ_UNUSED (events_received);
#endif
}

151 152 153 154 155
void test_zap_protocol_error (void *ctx,
                              char *my_endpoint,
                              void *server,
                              void *server_mon,
                              socket_config_fn socket_config_,
156 157
                              void *socket_config_data_,
                              int expected_error)
158 159 160
{
    test_zap_unsuccessful (ctx, my_endpoint, server, server_mon,
#ifdef ZMQ_BUILD_DRAFT_API
161
                           ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, expected_error,
162 163 164 165 166 167
#else
                           0, 0,
#endif
                           socket_config_, socket_config_data_);
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void test_zap_unsuccessful_status_300 (void *ctx,
                                       char *my_endpoint,
                                       void *server,
                                       void *server_mon,
                                       socket_config_fn client_socket_config_,
                                       void *client_socket_config_data_)
{
    void *client_mon;
    test_zap_unsuccessful (ctx, my_endpoint, server, server_mon,
#ifdef ZMQ_BUILD_DRAFT_API
                           ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 300,
#else
                           0, 0,
#endif
                           client_socket_config_, client_socket_config_data_,
                           &client_mon);

#ifdef ZMQ_BUILD_DRAFT_API
186 187
    // we can use a 0 timeout here, since the client socket is already closed
    assert_no_more_monitor_events_with_timeout (client_mon, 0);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    int rc = zmq_close (client_mon);
    assert (rc == 0);
#endif
}

void test_zap_unsuccessful_status_500 (void *ctx,
                                       char *my_endpoint,
                                       void *server,
                                       void *server_mon,
                                       socket_config_fn client_socket_config_,
                                       void *client_socket_config_data_)
{
    test_zap_unsuccessful (ctx, my_endpoint, server, server_mon,
#ifdef ZMQ_BUILD_DRAFT_API
                           ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500,
#else
                           0, 0,
#endif
                           client_socket_config_, client_socket_config_data_,
208
                           NULL,
209
#ifdef ZMQ_BUILD_DRAFT_API
210 211 212
                           ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500
#else
                           0, 0
213
#endif
214
    );
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
void test_zap_errors (socket_config_fn server_socket_config_,
                      void *server_socket_config_data_,
                      socket_config_fn client_socket_config_,
                      void *client_socket_config_data_)
{
    void *ctx;
    void *handler;
    void *zap_thread;
    void *server;
    void *server_mon;
    char my_endpoint[MAX_SOCKET_STRING];

    //  Invalid ZAP protocol tests

    //  wrong version
    fprintf (stderr, "test_zap_protocol_error wrong_version\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_wrong_version, server_socket_config_,
      server_socket_config_data_);
    test_zap_protocol_error (ctx, my_endpoint, server, server_mon,
238 239 240 241 242 243 244
                             client_socket_config_, client_socket_config_data_,
#ifdef ZMQ_BUILD_DRAFT_API
                             ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION
#else
                             0
#endif
    );
245 246 247 248 249 250 251 252 253 254
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);

    //  wrong request id
    fprintf (stderr, "test_zap_protocol_error wrong_request_id\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_wrong_request_id, server_socket_config_,
      server_socket_config_data_);
    test_zap_protocol_error (ctx, my_endpoint, server, server_mon,
255 256 257 258 259 260 261
                             client_socket_config_, client_socket_config_data_,
#ifdef ZMQ_BUILD_DRAFT_API
                             ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID
#else
                             0
#endif
    );
262 263 264 265 266 267 268 269 270 271
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);

    //  status invalid (not a 3-digit number)
    fprintf (stderr, "test_zap_protocol_error wrong_status_invalid\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_wrong_status_invalid, server_socket_config_,
      server_socket_config_data_);
    test_zap_protocol_error (ctx, my_endpoint, server, server_mon,
272 273 274 275 276 277 278
                             client_socket_config_, client_socket_config_data_,
#ifdef ZMQ_BUILD_DRAFT_API
                             ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE
#else
                             0
#endif
    );
279 280 281 282 283 284 285 286 287 288
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);

    //  too many parts
    fprintf (stderr, "test_zap_protocol_error too_many_parts\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_too_many_parts, server_socket_config_,
      server_socket_config_data_);
    test_zap_protocol_error (ctx, my_endpoint, server, server_mon,
289 290 291 292 293 294 295
                             client_socket_config_, client_socket_config_data_,
#ifdef ZMQ_BUILD_DRAFT_API
                             ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY
#else
                             0
#endif
    );
296 297 298 299 300 301 302 303 304 305 306 307 308 309
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);

    //  ZAP non-standard cases

    //  TODO make these observable on the client side as well (they are
    //  transmitted as an ERROR message)

    //  status 300 temporary failure
    fprintf (stderr, "test_zap_unsuccessful status 300\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_wrong_status_temporary_failure, server_socket_config_,
      server_socket_config_data_);
310 311 312
    test_zap_unsuccessful_status_300 (ctx, my_endpoint, server, server_mon,
                                      client_socket_config_,
                                      client_socket_config_data_);
313 314 315 316 317 318 319 320
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);

    //  status 500 internal error
    fprintf (stderr, "test_zap_unsuccessful status 500\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_wrong_status_internal_error, server_socket_config_);
321 322 323
    test_zap_unsuccessful_status_500 (ctx, my_endpoint, server, server_mon,
                                      client_socket_config_,
                                      client_socket_config_data_);
324 325
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);
326

327
#ifdef ZMQ_ZAP_ENFORCE_DOMAIN
328
    //  no ZAP handler
329
    int enforce = 1;
330
    fprintf (stderr, "test_zap_unsuccessful no ZAP handler started\n");
331 332 333 334
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint, NULL,
      server_socket_config_,
      server_socket_config_data_ ? server_socket_config_data_ : &enforce);
335 336
    test_zap_unsuccessful_no_handler (
      ctx, my_endpoint, server, server_mon,
337
#ifdef ZMQ_BUILD_DRAFT_API
338
      ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, EFAULT,
339
#else
340
      0, 0,
341
#endif
342
      client_socket_config_, client_socket_config_data_);
343 344
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);
345
#endif
346 347

    //  ZAP handler disconnecting on first message
348 349 350 351
    fprintf (stderr, "test_zap_unsuccessful ZAP handler disconnects\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_disconnect, server_socket_config_);
352 353 354 355 356 357 358 359
    test_zap_unsuccessful_no_handler (
      ctx, my_endpoint, server, server_mon,
#ifdef ZMQ_BUILD_DRAFT_API
      ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, EPIPE,
#else
      0, 0,
#endif
      client_socket_config_, client_socket_config_data_);
360 361 362 363 364 365
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler, true);

    //  ZAP handler does not read request
    fprintf (stderr,
             "test_zap_unsuccessful ZAP handler does not read request\n");
366 367 368
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_do_not_recv, server_socket_config_);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    test_zap_unsuccessful_no_handler (
      ctx, my_endpoint, server, server_mon,
#ifdef ZMQ_BUILD_DRAFT_API
      ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, EPIPE,
#else
      0, 0,
#endif
      client_socket_config_, client_socket_config_data_);
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);

    //  ZAP handler does not send reply
    fprintf (stderr,
             "test_zap_unsuccessful ZAP handler does not write reply\n");
    setup_context_and_server_side (
      &ctx, &handler, &zap_thread, &server, &server_mon, my_endpoint,
      &zap_handler_do_not_send, server_socket_config_);
    test_zap_unsuccessful_no_handler (
      ctx, my_endpoint, server, server_mon,
#ifdef ZMQ_BUILD_DRAFT_API
      ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL, EPIPE,
#else
      0, 0,
#endif
      client_socket_config_, client_socket_config_data_);
    shutdown_context_and_server_side (ctx, zap_thread, server, server_mon,
                                      handler);
396 397 398 399 400 401
}

int main (void)
{
    setup_test_environment ();

402 403 404 405
    fprintf (stderr, "NULL mechanism\n");
    test_zap_errors (&socket_config_null_server, NULL,
                     &socket_config_null_client, NULL);

406 407 408 409
    fprintf (stderr, "PLAIN mechanism\n");
    test_zap_errors (&socket_config_plain_server, NULL,
                     &socket_config_plain_client, NULL);

410 411 412 413 414 415 416 417 418
    if (zmq_has ("curve")) {
        fprintf (stderr, "CURVE mechanism\n");
        setup_testutil_security_curve ();

        curve_client_data_t curve_client_data = {
          valid_server_public, valid_client_public, valid_client_secret};
        test_zap_errors (&socket_config_curve_server, valid_server_secret,
                         &socket_config_curve_client, &curve_client_data);
    }
419
}