test_invalid_rep.cpp 2.61 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This file is part of 0MQ.

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

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

20
#include "testutil.hpp"
21

22
int main (void)
23
{
24
    setup_test_environment();
25
    //  Create REQ/ROUTER wiring.
26
    void *ctx = zmq_ctx_new ();
27
    assert (ctx);
28
    
29 30
    void *router_socket = zmq_socket (ctx, ZMQ_ROUTER);
    assert (router_socket);
31
    
32 33
    void *req_socket = zmq_socket (ctx, ZMQ_REQ);
    assert (req_socket);
34
    
35
    int linger = 0;
36
    int rc = zmq_setsockopt (router_socket, ZMQ_LINGER, &linger, sizeof (int));
37 38 39
    assert (rc == 0);
    rc = zmq_setsockopt (req_socket, ZMQ_LINGER, &linger, sizeof (int));
    assert (rc == 0);
40
    rc = zmq_bind (router_socket, "inproc://hi");
41 42 43 44 45 46 47 48 49
    assert (rc == 0);
    rc = zmq_connect (req_socket, "inproc://hi");
    assert (rc == 0);

    //  Initial request.
    rc = zmq_send (req_socket, "r", 1, 0);
    assert (rc == 1);

    //  Receive the request.
50 51
    char addr [32];
    int addr_size;
52
    char bottom [1];
53
    char body [1];
54
    addr_size = zmq_recv (router_socket, addr, sizeof (addr), 0);
55
    assert (addr_size >= 0);
56
    rc = zmq_recv (router_socket, bottom, sizeof (bottom), 0);
57
    assert (rc == 0);
58
    rc = zmq_recv (router_socket, body, sizeof (body), 0);
59 60 61
    assert (rc == 1);

    //  Send invalid reply.
62
    rc = zmq_send (router_socket, addr, addr_size, 0);
63
    assert (rc == addr_size);
64 65

    //  Send valid reply.
66
    rc = zmq_send (router_socket, addr, addr_size, ZMQ_SNDMORE);
67
    assert (rc == addr_size);
68
    rc = zmq_send (router_socket, bottom, 0, ZMQ_SNDMORE);
69
    assert (rc == 0);
70
    rc = zmq_send (router_socket, "b", 1, 0);
71 72 73 74 75
    assert (rc == 1);

    //  Check whether we've got the valid reply.
    rc = zmq_recv (req_socket, body, sizeof (body), 0);
    assert (rc == 1);
76
    assert (body [0] == 'b');
77 78

    //  Tear down the wiring.
79
    rc = zmq_close (router_socket);
80 81 82
    assert (rc == 0);
    rc = zmq_close (req_socket);
    assert (rc == 0);
83
    rc = zmq_ctx_term (ctx);
84 85 86 87 88
    assert (rc == 0);

    return 0;
}