test_security_null.cpp 7.22 KB
Newer Older
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 7 8
    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
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
25 26 27 28 29 30

    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.hpp"
31 32
#include "testutil_unity.hpp"

33 34 35 36 37
#if defined(ZMQ_HAVE_WINDOWS)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdexcept>
#define close closesocket
38
#else
39 40 41 42
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
43
#endif
44

45 46
#include <stdlib.h>

47
static void zap_handler (void *handler_)
48 49 50
{
    //  Process ZAP requests forever
    while (true) {
51
        char *version = s_recv (handler_);
52
        if (!version)
53
            break; //  Terminating
54

55 56 57 58 59
        char *sequence = s_recv (handler_);
        char *domain = s_recv (handler_);
        char *address = s_recv (handler_);
        char *routing_id = s_recv (handler_);
        char *mechanism = s_recv (handler_);
60

61 62
        TEST_ASSERT_EQUAL_STRING ("1.0", version);
        TEST_ASSERT_EQUAL_STRING ("NULL", mechanism);
63

64 65
        send_string_expect_success (handler_, version, ZMQ_SNDMORE);
        send_string_expect_success (handler_, sequence, ZMQ_SNDMORE);
66
        if (streq (domain, "TEST")) {
67 68 69 70
            send_string_expect_success (handler_, "200", ZMQ_SNDMORE);
            send_string_expect_success (handler_, "OK", ZMQ_SNDMORE);
            send_string_expect_success (handler_, "anonymous", ZMQ_SNDMORE);
            send_string_expect_success (handler_, "", 0);
71
        } else {
72 73 74 75
            send_string_expect_success (handler_, "400", ZMQ_SNDMORE);
            send_string_expect_success (handler_, "BAD DOMAIN", ZMQ_SNDMORE);
            send_string_expect_success (handler_, "", ZMQ_SNDMORE);
            send_string_expect_success (handler_, "", 0);
76
        }
77 78 79 80
        free (version);
        free (sequence);
        free (domain);
        free (address);
81
        free (routing_id);
82 83
        free (mechanism);
    }
84
    close_zero_linger (handler_);
85 86
}

87
void *zap_thread;
88

89 90
static void setup_zap_handler ()
{
91
    //  Spawn ZAP handler
92 93
    //  We create and bind ZAP socket in main thread to avoid case
    //  where child thread does not start up fast enough.
94 95 96 97
    void *handler = zmq_socket (get_test_context (), ZMQ_REP);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (handler, "inproc://zeromq.zap.01"));
    zap_thread = zmq_threadstart (&zap_handler, handler);
}
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static void teardown_zap_handler ()
{
    //  Wait until ZAP handler terminates
    zmq_threadclose (zap_thread);
}

void setUp ()
{
    setup_test_context ();
    setup_zap_handler ();
}

void tearDown ()
{
    teardown_test_context ();
    teardown_zap_handler ();
}
116

117 118
void test_no_domain ()
{
Pieter Hintjens's avatar
Pieter Hintjens committed
119 120
    //  We first test client/server with no ZAP domain
    //  Libzmq does not call our ZAP handler, the connect must succeed
121 122 123 124 125
    void *server = test_context_socket (ZMQ_DEALER);
    void *client = test_context_socket (ZMQ_DEALER);
    char my_endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
Pieter Hintjens's avatar
Pieter Hintjens committed
126
    bounce (server, client);
127 128 129
    test_context_socket_close_zero_linger (client);
    test_context_socket_close_zero_linger (server);
}
130

131 132
void test_wrong_domain_fails ()
{
133
    //  Now define a ZAP domain for the server; this enables
134 135
    //  authentication. We're using the wrong domain so this test
    //  must fail.
136 137 138 139 140 141 142
    void *server = test_context_socket (ZMQ_DEALER);
    void *client = test_context_socket (ZMQ_DEALER);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5));
    char my_endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
143
    expect_bounce_fail (server, client);
144 145 146
    test_context_socket_close_zero_linger (client);
    test_context_socket_close_zero_linger (server);
}
147

148 149
void test_success ()
{
Pieter Hintjens's avatar
Pieter Hintjens committed
150
    //  Now use the right domain, the test must pass
151 152 153 154 155 156 157
    void *server = test_context_socket (ZMQ_DEALER);
    void *client = test_context_socket (ZMQ_DEALER);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "TEST", 4));
    char my_endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
Pieter Hintjens's avatar
Pieter Hintjens committed
158
    bounce (server, client);
159 160 161
    test_context_socket_close_zero_linger (client);
    test_context_socket_close_zero_linger (server);
}
162

163 164
void test_vanilla_socket ()
{
165
    // Unauthenticated messages from a vanilla socket shouldn't be received
166 167 168 169 170
    void *server = test_context_socket (ZMQ_DEALER);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (server, ZMQ_ZAP_DOMAIN, "WRONG", 5));
    char my_endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
171 172

    struct sockaddr_in ip4addr;
173
    fd_t s;
174

175
    unsigned short int port;
176 177
    int rc = sscanf (my_endpoint, "tcp://127.0.0.1:%hu", &port);
    TEST_ASSERT_EQUAL_INT (1, rc);
178

179
    ip4addr.sin_family = AF_INET;
180 181
    ip4addr.sin_port = htons (port);
#if defined(ZMQ_HAVE_WINDOWS) && (_WIN32_WINNT < 0x0600)
182 183
    ip4addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
#else
184
    inet_pton (AF_INET, "127.0.0.1", &ip4addr.sin_addr);
185
#endif
186 187

    s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
188
    rc = connect (s, (struct sockaddr *) &ip4addr, sizeof ip4addr);
189
    TEST_ASSERT_GREATER_THAN_INT (-1, rc);
190 191 192 193
    // send anonymous ZMTP/1.0 greeting
    send (s, "\x01\x00", 2, 0);
    // send sneaky message that shouldn't be received
    send (s, "\x08\x00sneaky\0", 9, 0);
194
    int timeout = 250;
195
    zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
196
    char *buf = s_recv (server);
197 198
    if (buf != NULL) {
        printf ("Received unauthenticated message: %s\n", buf);
199
        TEST_ASSERT_NULL (buf);
200
    }
201
    close (s);
202 203
    test_context_socket_close_zero_linger (server);
}
204

205 206 207
int main ()
{
    setup_test_environment ();
208

209 210 211 212 213 214
    UNITY_BEGIN ();
    RUN_TEST (test_no_domain);
    RUN_TEST (test_wrong_domain_fails);
    RUN_TEST (test_success);
    RUN_TEST (test_vanilla_socket);
    return UNITY_END ();
215
}