test_stream.cpp 11.1 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

    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/>.
*/

30
#include "testutil.hpp"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

//  ZMTP protocol greeting structure

typedef unsigned char byte;
typedef struct {
    byte signature [10];    //  0xFF 8*0x00 0x7F
    byte version [2];       //  0x03 0x00 for ZMTP/3.0
    byte mechanism [20];    //  "NULL"
    byte as_server;
    byte filler [31];
} zmtp_greeting_t;

#define ZMTP_DEALER  5      //  Socket type constants

//  This is a greeting matching what 0MQ will send us; note the
//  8-byte size is set to 1 for backwards compatibility

48 49 50 51 52 53 54
static zmtp_greeting_t
    greeting = { { 0xFF, 0, 0, 0, 0, 0, 0, 0, 1, 0x7F },
                 { 3, 0 },
                 { 'N', 'U', 'L', 'L'},
                 0,
                 { 0 }
    };
55 56

static void
57
test_stream_to_dealer (void)
58 59
{
    int rc;
60 61
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
62 63 64 65 66 67 68 69 70 71 72 73

    //  Set up our context and sockets
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    //  We'll be using this socket in raw mode
    void *stream = zmq_socket (ctx, ZMQ_STREAM);
    assert (stream);

    int zero = 0;
    rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero));
    assert (rc == 0);
74 75 76
    int enabled = 1;
    rc = zmq_setsockopt (stream, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled));
    assert (rc == 0);
77
    rc = zmq_bind (stream, "tcp://127.0.0.1:*");
78
    assert (rc == 0);
79 80 81
    rc = zmq_getsockopt (stream, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
    assert (rc == 0);

82 83 84 85 86 87

    //  We'll be using this socket as the other peer
    void *dealer = zmq_socket (ctx, ZMQ_DEALER);
    assert (dealer);
    rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
    assert (rc == 0);
88
    rc = zmq_connect (dealer, my_endpoint);
89 90 91 92 93

    //  Send a message on the dealer socket
    rc = zmq_send (dealer, "Hello", 5, 0);
    assert (rc == 5);

94
    //  Connecting sends a zero message
95 96 97
    //  First frame is routing id
    zmq_msg_t routing_id;
    rc = zmq_msg_init (&routing_id);
98
    assert (rc == 0);
99
    rc = zmq_msg_recv (&routing_id, stream, 0);
100
    assert (rc > 0);
101
    assert (zmq_msg_more (&routing_id));
102

103
    //  Verify the existence of Peer-Address metadata
104
    char const *peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
105 106
    assert (peer_address != 0);
    assert (streq (peer_address, "127.0.0.1"));
107

108
    //  Second frame is zero
109 110
    byte buffer [255];
    rc = zmq_recv (stream, buffer, 255, 0);
111
    assert (rc == 0);
112

113
    //  Verify the existence of Peer-Address metadata
114
    peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
115 116
    assert (peer_address != 0);
    assert (streq (peer_address, "127.0.0.1"));
117

118
    //  Real data follows
119 120
    //  First frame is routing id
    rc = zmq_msg_recv (&routing_id, stream, 0);
121
    assert (rc > 0);
122
    assert (zmq_msg_more (&routing_id));
123

124
    //  Verify the existence of Peer-Address metadata
125
    peer_address = zmq_msg_gets (&routing_id, "Peer-Address");
126 127
    assert (peer_address != 0);
    assert (streq (peer_address, "127.0.0.1"));
128

129
    //  Second frame is greeting signature
130
    rc = zmq_recv (stream, buffer, 255, 0);
131 132 133 134
    assert (rc == 10);
    assert (memcmp (buffer, greeting.signature, 10) == 0);

    //  Send our own protocol greeting
135
    rc = zmq_msg_send (&routing_id, stream, ZMQ_SNDMORE);
136 137 138 139 140
    assert (rc > 0);
    rc = zmq_send (stream, &greeting, sizeof (greeting), 0);
    assert (rc == sizeof (greeting));

    //  Now we expect the data from the DEALER socket
141
    //  We want the rest of greeting along with the Ready command
Pieter Hintjens's avatar
Pieter Hintjens committed
142 143
    int bytes_read = 0;
    while (bytes_read < 97) {
144 145
        //  First frame is the routing id of the connection (each time)
        rc = zmq_msg_recv (&routing_id, stream, 0);
146
        assert (rc > 0);
147
        assert (zmq_msg_more (&routing_id));
148
        //  Second frame contains the next chunk of data
Pieter Hintjens's avatar
Pieter Hintjens committed
149 150 151 152
        rc = zmq_recv (stream, buffer + bytes_read, 255 - bytes_read, 0);
        assert (rc >= 0);
        bytes_read += rc;
    }
153 154 155 156 157 158

    //  First two bytes are major and minor version numbers.
    assert (buffer [0] == 3);       //  ZMTP/3.0
    assert (buffer [1] == 0);

    //  Mechanism is "NULL"
159 160
    assert (memcmp (buffer + 2, "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0);
    assert (memcmp (buffer + 54, "\4\51\5READY", 8) == 0);
161 162
    assert (memcmp (buffer + 62, "\13Socket-Type\0\0\0\6DEALER", 22) == 0);
    assert (memcmp (buffer + 84, "\10Identity\0\0\0\0", 13) == 0);
163 164

    //  Announce we are ready
165
    memcpy (buffer, "\4\51\5READY", 8);
166
    memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22);
167
    memcpy (buffer + 30, "\10Identity\0\0\0\0", 13);
168 169

    //  Send Ready command
170
    rc = zmq_msg_send (&routing_id, stream, ZMQ_SNDMORE);
171
    assert (rc > 0);
172 173
    rc = zmq_send (stream, buffer, 43, 0);
    assert (rc == 43);
174 175

    //  Now we expect the data from the DEALER socket
176 177
    //  First frame is, again, the routing id of the connection
    rc = zmq_msg_recv (&routing_id, stream, 0);
178
    assert (rc > 0);
179
    assert (zmq_msg_more (&routing_id));
180 181 182 183 184 185 186 187 188 189 190

    //  Third frame contains Hello message from DEALER
    rc = zmq_recv (stream, buffer, sizeof buffer, 0);
    assert (rc == 7);

    //  Then we have a 5-byte message "Hello"
    assert (buffer [0] == 0);       //  Flags = 0
    assert (buffer [1] == 5);       //  Size = 5
    assert (memcmp (buffer + 2, "Hello", 5) == 0);

    //  Send "World" back to DEALER
191
    rc = zmq_msg_send (&routing_id, stream, ZMQ_SNDMORE);
192 193 194 195 196 197 198 199 200 201
    assert (rc > 0);
    byte world [] = { 0, 5, 'W', 'o', 'r', 'l', 'd' };
    rc = zmq_send (stream, world, sizeof (world), 0);
    assert (rc == sizeof (world));

    //  Expect response on DEALER socket
    rc = zmq_recv (dealer, buffer, 255, 0);
    assert (rc == 5);
    assert (memcmp (buffer, "World", 5) == 0);

202
    //  Test large messages over STREAM socket
203
#   define size  64000
204 205 206 207 208 209 210 211
    uint8_t msgout [size];
    memset (msgout, 0xAB, size);
    zmq_send (dealer, msgout, size, 0);

    uint8_t msgin [9 + size];
    memset (msgin, 0, 9 + size);
    bytes_read = 0;
    while (bytes_read < 9 + size) {
212
        //  Get routing id frame
213 214 215 216 217 218 219 220 221 222 223 224
        rc = zmq_recv (stream, buffer, 256, 0);
        assert (rc > 0);
        //  Get next chunk
        rc = zmq_recv (stream, msgin + bytes_read, 9 + size - bytes_read, 0);
        assert (rc > 0);
        bytes_read += rc;
    }
    int byte_nbr;
    for (byte_nbr = 0; byte_nbr < size; byte_nbr++) {
        if (msgin [9 + byte_nbr] != 0xAB)
            assert (false);
    }
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    rc = zmq_close (dealer);
    assert (rc == 0);

    rc = zmq_close (stream);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}


static void
test_stream_to_stream (void)
{
    int rc;
240 241
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
242 243 244
    //  Set-up our context and sockets
    void *ctx = zmq_ctx_new ();
    assert (ctx);
245

246 247
    void *server = zmq_socket (ctx, ZMQ_STREAM);
    assert (server);
248 249 250
    int enabled = 1;
    rc = zmq_setsockopt (server, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled));
    assert (rc == 0);
251 252 253
    rc = zmq_bind (server, "tcp://127.0.0.1:*");
    assert (rc == 0);
    rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
254 255 256 257
    assert (rc == 0);

    void *client = zmq_socket (ctx, ZMQ_STREAM);
    assert (client);
258 259
    rc = zmq_setsockopt (client, ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled));
    assert (rc == 0);
260
    rc = zmq_connect (client, my_endpoint);
261 262 263
    assert (rc == 0);
    uint8_t id [256];
    size_t id_size = 256;
264
    uint8_t buffer [256];
265

266
    //  Connecting sends a zero message
267
    //  Server: First frame is routing id, second frame is zero
268 269 270 271
    id_size = zmq_recv (server, id, 256, 0);
    assert (id_size > 0);
    rc = zmq_recv (server, buffer, 256, 0);
    assert (rc == 0);
272
    //  Client: First frame is routing id, second frame is zero
273 274 275 276 277
    id_size = zmq_recv (client, id, 256, 0);
    assert (id_size > 0);
    rc = zmq_recv (client, buffer, 256, 0);
    assert (rc == 0);

278
    //  Sent HTTP request on client socket
279
    //  Get server routing id
280
    rc = zmq_getsockopt (client, ZMQ_ROUTING_ID, id, &id_size);
281
    assert (rc == 0);
282
    //  First frame is server routing id
283 284 285 286 287
    rc = zmq_send (client, id, id_size, ZMQ_SNDMORE);
    assert (rc == (int) id_size);
    //  Second frame is HTTP GET request
    rc = zmq_send (client, "GET /\n\n", 7, 0);
    assert (rc == 7);
288

289 290 291 292
    //  Get HTTP request; ID frame and then request
    id_size = zmq_recv (server, id, 256, 0);
    assert (id_size > 0);
    rc = zmq_recv (server, buffer, 256, 0);
293
    assert (rc != -1);
294
    assert (memcmp (buffer, "GET /\n\n", 7) == 0);
295

296 297
    //  Send reply back to client
    char http_response [] =
298 299 300
        "HTTP/1.0 200 OK\r\n"
        "Content-Type: text/plain\r\n"
        "\r\n"
301
        "Hello, World!";
302 303 304 305 306 307 308 309 310 311
    rc = zmq_send (server, id, id_size, ZMQ_SNDMORE);
    assert (rc != -1);
    rc = zmq_send (server, http_response, sizeof (http_response), ZMQ_SNDMORE);
    assert (rc != -1);

    //  Send zero to close connection to client
    rc = zmq_send (server, id, id_size, ZMQ_SNDMORE);
    assert (rc != -1);
    rc = zmq_send (server, NULL, 0, ZMQ_SNDMORE);
    assert (rc != -1);
312 313 314 315 316 317 318

    //  Get reply at client and check that it's complete
    id_size = zmq_recv (client, id, 256, 0);
    assert (id_size > 0);
    rc = zmq_recv (client, buffer, 256, 0);
    assert (rc == sizeof (http_response));
    assert (memcmp (buffer, http_response, sizeof (http_response)) == 0);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

    // //  Get disconnection notification
    // FIXME: why does this block? Bug in STREAM disconnect notification?
    // id_size = zmq_recv (client, id, 256, 0);
    // assert (id_size > 0);
    // rc = zmq_recv (client, buffer, 256, 0);
    // assert (rc == 0);

    rc = zmq_close (server);
    assert (rc == 0);

    rc = zmq_close (client);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
335 336 337 338
}

int main (void)
{
339
    setup_test_environment();
340 341
    test_stream_to_dealer ();
    test_stream_to_stream ();
342
}