test_security_zap.cpp 21.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
/*
    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"
31 32
#include "testutil_unity.hpp"

33
SETUP_TEARDOWN_TESTCONTEXT
34

35
static void zap_handler_wrong_version (void * /*unused_*/)
36
{
37
    zap_handler_generic (zap_wrong_version);
38 39
}

40
static void zap_handler_wrong_request_id (void * /*unused_*/)
41
{
42
    zap_handler_generic (zap_wrong_request_id);
43 44
}

45
static void zap_handler_wrong_status_invalid (void * /*unused_*/)
46
{
47
    zap_handler_generic (zap_status_invalid);
48 49
}

50
static void zap_handler_wrong_status_temporary_failure (void * /*unused_*/)
51
{
52
    zap_handler_generic (zap_status_temporary_failure);
53 54
}

55
static void zap_handler_wrong_status_internal_error (void * /*unused_*/)
56
{
57
    zap_handler_generic (zap_status_internal_error);
58 59
}

60
static void zap_handler_too_many_parts (void * /*unused_*/)
61
{
62
    zap_handler_generic (zap_too_many_parts);
63 64
}

65
static void zap_handler_disconnect (void * /*unused_*/)
66
{
67
    zap_handler_generic (zap_disconnect);
68 69
}

70
static void zap_handler_do_not_recv (void * /*unused_*/)
71
{
72
    zap_handler_generic (zap_do_not_recv);
73 74
}

75
static void zap_handler_do_not_send (void * /*unused_*/)
76
{
77
    zap_handler_generic (zap_do_not_send);
78 79
}

80
int expect_new_client_bounce_fail_and_count_monitor_events (
81 82
  char *my_endpoint_,
  void *server_,
83 84
  socket_config_fn socket_config_,
  void *socket_config_data_,
85 86 87 88 89 90
  void **client_mon_,
  void *server_mon_,
  int expected_server_event_,
  int expected_server_value_,
  int expected_client_event_ = 0,
  int expected_client_value_ = 0)
91
{
92
    expect_new_client_bounce_fail (
93 94
      my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
      expected_client_event_, expected_client_value_);
95 96

    int events_received = 0;
97
    events_received = expect_monitor_event_multiple (
98
      server_mon_, expected_server_event_, expected_server_value_);
99 100 101 102

    return events_received;
}

103
void test_zap_unsuccessful (char *my_endpoint_,
104 105 106 107
                            void *server_,
                            void *server_mon_,
                            int expected_server_event_,
                            int expected_server_value_,
108
                            socket_config_fn socket_config_,
109
                            void *socket_config_data_,
110 111 112
                            void **client_mon_ = NULL,
                            int expected_client_event_ = 0,
                            int expected_client_value_ = 0)
113
{
114
    int server_events_received =
115
      expect_new_client_bounce_fail_and_count_monitor_events (
116 117 118
        my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
        server_mon_, expected_server_event_, expected_server_value_,
        expected_client_event_, expected_client_value_);
119

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

126
void test_zap_unsuccessful_no_handler (char *my_endpoint_,
127 128 129 130
                                       void *server_,
                                       void *server_mon_,
                                       int expected_event_,
                                       int expected_err_,
131 132
                                       socket_config_fn socket_config_,
                                       void *socket_config_data_,
133
                                       void **client_mon_ = NULL)
134
{
135
    const int events_received =
136
      expect_new_client_bounce_fail_and_count_monitor_events (
137 138
        my_endpoint_, server_, socket_config_, socket_config_data_, client_mon_,
        server_mon_, expected_event_, expected_err_);
139 140 141

    //  there may be more than one ZAP request due to repeated attempts by the
    //  client
142
    TEST_ASSERT_GREATER_THAN_INT (0, events_received);
143 144
}

145
void test_zap_protocol_error (char *my_endpoint_,
146 147
                              void *server_,
                              void *server_mon_,
148
                              socket_config_fn socket_config_,
149
                              void *socket_config_data_,
150
                              int expected_error_)
151
{
152
    test_zap_unsuccessful (my_endpoint_, server_, server_mon_,
153
                           ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL, expected_error_,
154 155 156
                           socket_config_, socket_config_data_);
}

157
void test_zap_unsuccessful_status_300 (char *my_endpoint_,
158 159
                                       void *server_,
                                       void *server_mon_,
160 161 162 163
                                       socket_config_fn client_socket_config_,
                                       void *client_socket_config_data_)
{
    void *client_mon;
164
    test_zap_unsuccessful (
165 166
      my_endpoint_, server_, server_mon_, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 300,
      client_socket_config_, client_socket_config_data_, &client_mon);
167

168 169
    // we can use a 0 timeout here, since the client socket is already closed
    assert_no_more_monitor_events_with_timeout (client_mon, 0);
170

171
    test_context_socket_close (client_mon);
172 173
}

174
void test_zap_unsuccessful_status_500 (char *my_endpoint_,
175 176
                                       void *server_,
                                       void *server_mon_,
177 178 179
                                       socket_config_fn client_socket_config_,
                                       void *client_socket_config_data_)
{
180
    test_zap_unsuccessful (my_endpoint_, server_, server_mon_,
181 182
                           ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500,
                           client_socket_config_, client_socket_config_data_,
183
                           NULL, ZMQ_EVENT_HANDSHAKE_FAILED_AUTH, 500);
184 185
}

186 187 188 189 190 191 192
static void
test_zap_protocol_error_closure (socket_config_fn server_socket_config_,
                                 socket_config_fn client_socket_config_,
                                 void *client_socket_config_data_,
                                 void *server_socket_config_data_,
                                 zmq_thread_fn zap_handler_,
                                 int expected_failure_)
193
{
194
    void *handler, *zap_thread, *server, *server_mon;
195 196 197
    char my_endpoint[MAX_SOCKET_STRING];

    setup_context_and_server_side (
198 199 200
      &handler, &zap_thread, &server, &server_mon, my_endpoint, zap_handler_,
      server_socket_config_, server_socket_config_data_);
    test_zap_protocol_error (my_endpoint, server, server_mon,
201
                             client_socket_config_, client_socket_config_data_,
202
                             expected_failure_);
203
    shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
204
}
205

206 207 208 209 210 211 212 213 214 215 216
static void
test_zap_protocol_error_wrong_version (socket_config_fn server_socket_config_,
                                       socket_config_fn client_socket_config_,
                                       void *client_socket_config_data_,
                                       void *server_socket_config_data_)
{
    test_zap_protocol_error_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      server_socket_config_data_, &zap_handler_wrong_version,
      ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION);
}
217

218 219 220 221 222 223 224 225 226 227 228
static void test_zap_protocol_error_wrong_request_id (
  socket_config_fn server_socket_config_,
  socket_config_fn client_socket_config_,
  void *client_socket_config_data_,
  void *server_socket_config_data_)
{
    test_zap_protocol_error_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      server_socket_config_data_, &zap_handler_wrong_request_id,
      ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID);
}
229

230 231 232 233 234 235 236 237 238 239 240
static void test_zap_protocol_error_wrong_status_invalid (
  socket_config_fn server_socket_config_,
  socket_config_fn client_socket_config_,
  void *client_socket_config_data_,
  void *server_socket_config_data_)
{
    test_zap_protocol_error_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      server_socket_config_data_, &zap_handler_wrong_status_invalid,
      ZMQ_PROTOCOL_ERROR_ZAP_INVALID_STATUS_CODE);
}
241

242 243 244 245 246 247 248 249 250 251 252
static void
test_zap_protocol_error_too_many_parts (socket_config_fn server_socket_config_,
                                        socket_config_fn client_socket_config_,
                                        void *client_socket_config_data_,
                                        void *server_socket_config_data_)
{
    test_zap_protocol_error_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      server_socket_config_data_, &zap_handler_too_many_parts,
      ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY);
}
253

254 255
//  TODO the failed status (300/500) should be observable as monitoring events on the client side as well (they are
//  already transmitted as an ERROR message)
256

257 258 259 260 261 262
static void
test_zap_wrong_status_temporary_failure (socket_config_fn server_socket_config_,
                                         socket_config_fn client_socket_config_,
                                         void *client_socket_config_data_,
                                         void *server_socket_config_data_)
{
263
    void *handler, *zap_thread, *server, *server_mon;
264
    char my_endpoint[MAX_SOCKET_STRING];
265
    setup_context_and_server_side (
266
      &handler, &zap_thread, &server, &server_mon, my_endpoint,
267 268
      &zap_handler_wrong_status_temporary_failure, server_socket_config_,
      server_socket_config_data_);
269
    test_zap_unsuccessful_status_300 (my_endpoint, server, server_mon,
270 271
                                      client_socket_config_,
                                      client_socket_config_data_);
272
    shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
273
}
274

275 276 277 278 279
static void
test_zap_wrong_status_internal_error (socket_config_fn server_socket_config_,
                                      socket_config_fn client_socket_config_,
                                      void *client_socket_config_data_)
{
280
    void *handler, *zap_thread, *server, *server_mon;
281
    char my_endpoint[MAX_SOCKET_STRING];
282
    setup_context_and_server_side (
283
      &handler, &zap_thread, &server, &server_mon, my_endpoint,
284
      &zap_handler_wrong_status_internal_error, server_socket_config_);
285
    test_zap_unsuccessful_status_500 (my_endpoint, server, server_mon,
286 287
                                      client_socket_config_,
                                      client_socket_config_data_);
288
    shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
289
}
290

291 292 293 294 295 296
static void
test_zap_unsuccesful_no_handler_started (socket_config_fn server_socket_config_,
                                         socket_config_fn client_socket_config_,
                                         void *client_socket_config_data_,
                                         void *server_socket_config_data_)
{
297
#ifdef ZMQ_ZAP_ENFORCE_DOMAIN
298
    void *handler, *zap_thread, *server, *server_mon;
299 300 301
    char my_endpoint[MAX_SOCKET_STRING];
    // TODO this looks wrong, where will the enforce value be used?

302
    //  no ZAP handler
303 304
    int enforce = 1;
    setup_context_and_server_side (
305
      &handler, &zap_thread, &server, &server_mon, my_endpoint, NULL,
306 307
      server_socket_config_,
      server_socket_config_data_ ? server_socket_config_data_ : &enforce);
308 309 310 311
    test_zap_unsuccessful_no_handler (
      my_endpoint, server, server_mon, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
      EFAULT, client_socket_config_, client_socket_config_data_);
    shutdown_context_and_server_side (zap_thread, server, server_mon, handler);
312
#endif
313
}
314

315 316 317 318 319 320 321
static void
test_zap_unsuccesful_no_handler_closure (socket_config_fn server_socket_config_,
                                         socket_config_fn client_socket_config_,
                                         void *client_socket_config_data_,
                                         zmq_thread_fn zap_handler_func_,
                                         bool zap_handler_disconnected_ = false)
{
322
    void *handler, *zap_thread, *server, *server_mon;
323
    char my_endpoint[MAX_SOCKET_STRING];
324 325
    setup_context_and_server_side (&handler, &zap_thread, &server, &server_mon,
                                   my_endpoint, zap_handler_func_,
326
                                   server_socket_config_);
327 328 329 330 331
    test_zap_unsuccessful_no_handler (
      my_endpoint, server, server_mon, ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL,
      EPIPE, client_socket_config_, client_socket_config_data_);
    shutdown_context_and_server_side (zap_thread, server, server_mon, handler,
                                      zap_handler_disconnected_);
332
}
333

334 335 336 337 338 339 340 341 342
static void
test_zap_unsuccesful_disconnect (socket_config_fn server_socket_config_,
                                 socket_config_fn client_socket_config_,
                                 void *client_socket_config_data_)
{
    test_zap_unsuccesful_no_handler_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      &zap_handler_disconnect, true);
}
343

344 345 346 347 348 349 350 351
static void
test_zap_unsuccesful_do_not_recv (socket_config_fn server_socket_config_,
                                  socket_config_fn client_socket_config_,
                                  void *client_socket_config_data_)
{
    test_zap_unsuccesful_no_handler_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      &zap_handler_do_not_recv);
352 353
}

354 355 356 357
static void
test_zap_unsuccesful_do_not_send (socket_config_fn server_socket_config_,
                                  socket_config_fn client_socket_config_,
                                  void *client_socket_config_data_)
358
{
359 360 361 362 363 364 365 366 367 368 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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    test_zap_unsuccesful_no_handler_closure (
      server_socket_config_, client_socket_config_, client_socket_config_data_,
      &zap_handler_do_not_send);
}

#define DEFINE_ZAP_ERROR_TESTS(                                                \
  name_, server_socket_config_, server_socket_config_data_,                    \
  client_socket_config_, client_socket_config_data_)                           \
    void test_zap_protocol_error_wrong_version_##name_ ()                      \
    {                                                                          \
        test_zap_protocol_error_wrong_version (                                \
          server_socket_config_, client_socket_config_,                        \
          client_socket_config_data_, server_socket_config_data_);             \
    }                                                                          \
    void test_zap_protocol_error_wrong_request_id_##name_ ()                   \
    {                                                                          \
        test_zap_protocol_error_wrong_request_id (                             \
          server_socket_config_, client_socket_config_,                        \
          client_socket_config_data_, server_socket_config_data_);             \
    }                                                                          \
    void test_zap_protocol_error_wrong_status_invalid_##name_ ()               \
    {                                                                          \
        test_zap_protocol_error_wrong_status_invalid (                         \
          server_socket_config_, client_socket_config_,                        \
          client_socket_config_data_, server_socket_config_data_);             \
    }                                                                          \
    void test_zap_protocol_error_too_many_parts_##name_ ()                     \
    {                                                                          \
        test_zap_protocol_error_too_many_parts (                               \
          server_socket_config_, client_socket_config_,                        \
          client_socket_config_data_, server_socket_config_data_);             \
    }                                                                          \
    void test_zap_wrong_status_temporary_failure_##name_ ()                    \
    {                                                                          \
        test_zap_wrong_status_temporary_failure (                              \
          server_socket_config_, client_socket_config_,                        \
          client_socket_config_data_, server_socket_config_data_);             \
    }                                                                          \
    void test_zap_wrong_status_internal_error_##name_ ()                       \
    {                                                                          \
        test_zap_wrong_status_internal_error (server_socket_config_,           \
                                              client_socket_config_,           \
                                              client_socket_config_data_);     \
    }                                                                          \
    void test_zap_unsuccessful_no_handler_started_##name_ ()                   \
    {                                                                          \
        test_zap_unsuccesful_no_handler_started (                              \
          server_socket_config_, client_socket_config_,                        \
          client_socket_config_data_, server_socket_config_data_);             \
    }                                                                          \
    void test_zap_unsuccessful_disconnect_##name_ ()                           \
    {                                                                          \
        test_zap_unsuccesful_disconnect (server_socket_config_,                \
                                         client_socket_config_,                \
                                         client_socket_config_data_);          \
    }                                                                          \
    void test_zap_unsuccessful_do_not_recv_##name_ ()                          \
    {                                                                          \
        test_zap_unsuccesful_do_not_recv (server_socket_config_,               \
                                          client_socket_config_,               \
                                          client_socket_config_data_);         \
    }                                                                          \
    void test_zap_unsuccessful_do_not_send_##name_ ()                          \
    {                                                                          \
        test_zap_unsuccesful_do_not_send (server_socket_config_,               \
                                          client_socket_config_,               \
                                          client_socket_config_data_);         \
    }
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
DEFINE_ZAP_ERROR_TESTS (
  null, &socket_config_null_server, NULL, &socket_config_null_client, NULL)

DEFINE_ZAP_ERROR_TESTS (
  plain, &socket_config_plain_server, NULL, &socket_config_plain_client, NULL)

static curve_client_data_t curve_client_data = {
  valid_server_public, valid_client_public, valid_client_secret};

DEFINE_ZAP_ERROR_TESTS (curve,
                        &socket_config_curve_server,
                        valid_server_secret,
                        &socket_config_curve_client,
                        &curve_client_data)

#define RUN_ZAP_ERROR_TESTS(name_)                                             \
    {                                                                          \
        RUN_TEST (test_zap_protocol_error_wrong_version_##name_);              \
        RUN_TEST (test_zap_protocol_error_wrong_request_id_##name_);           \
        RUN_TEST (test_zap_protocol_error_wrong_status_invalid_##name_);       \
        RUN_TEST (test_zap_protocol_error_too_many_parts_##name_);             \
        RUN_TEST (test_zap_wrong_status_temporary_failure_##name_);            \
        RUN_TEST (test_zap_wrong_status_internal_error_##name_);               \
        RUN_TEST (test_zap_unsuccessful_no_handler_started_##name_);           \
        RUN_TEST (test_zap_unsuccessful_disconnect_##name_);                   \
        RUN_TEST (test_zap_unsuccessful_do_not_recv_##name_);                  \
        RUN_TEST (test_zap_unsuccessful_do_not_send_##name_);                  \
    }
456

457 458 459
int main ()
{
    setup_test_environment ();
460

461 462
    if (zmq_has ("curve")) {
        setup_testutil_security_curve ();
463
    }
464

465 466 467 468 469
    UNITY_BEGIN ();
    RUN_ZAP_ERROR_TESTS (null);
    RUN_ZAP_ERROR_TESTS (plain);
    if (zmq_has ("curve")) {
        RUN_ZAP_ERROR_TESTS (curve);
470
    }
471
    return UNITY_END ();
472
}