test_client_server.cpp 4.06 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 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
#include "testutil_unity.hpp"
32

33 34 35
#include <unity.h>

void setUp ()
36
{
37 38
    setup_test_context ();
}
39

40 41 42 43
void tearDown ()
{
    teardown_test_context ();
}
44

45
void create_inproc_client_server_pair (void **server, void **client)
46
{
47 48
    *server = test_context_socket (ZMQ_SERVER);
    *client = test_context_socket (ZMQ_CLIENT);
49

50
    TEST_ASSERT_SUCCESS_ERRNO (
51
      zmq_bind (*server, "inproc://test-client-server"));
52
    TEST_ASSERT_SUCCESS_ERRNO (
53 54
      zmq_connect (*client, "inproc://test-client-server"));
}
55

56 57 58 59 60 61
void send_sndmore_expect_failure (void *socket)
{
    int rc = zmq_send (socket, "X", 1, ZMQ_SNDMORE);
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (EINVAL, errno);
}
62

63 64 65 66
void test_client_sndmore_fails ()
{
    void *server, *client;
    create_inproc_client_server_pair (&server, &client);
67

68
    send_sndmore_expect_failure (client);
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    test_context_socket_close (server);
    test_context_socket_close (client);
}

void test_server_sndmore_fails ()
{
    void *server, *client;
    create_inproc_client_server_pair (&server, &client);

    send_sndmore_expect_failure (server);

    test_context_socket_close (server);
    test_context_socket_close (client);
}

void test_routing_id ()
{
    void *server, *client;
    create_inproc_client_server_pair (&server, &client);

    send_string_expect_success (client, "X", 0);
91

92 93 94 95
    uint32_t routing_id;
    {
        zmq_msg_t msg;
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
96

97 98
        int rc = TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, server, 0));
        TEST_ASSERT_EQUAL_INT (1, rc);
99

100 101
        routing_id = zmq_msg_routing_id (&msg);
        TEST_ASSERT_NOT_EQUAL (0, routing_id);
102

103 104
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
    }
105

106 107 108
    {
        zmq_msg_t msg;
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, 1));
109

110 111
        char *data = (char *) zmq_msg_data (&msg);
        data[0] = 2;
112

113
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_set_routing_id (&msg, routing_id));
114

115
        int rc = zmq_msg_send (&msg, server, 0);
116 117
        TEST_ASSERT_EQUAL_INT (1, rc);
    }
118

119 120 121
    {
        zmq_msg_t msg;
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
122

123 124
        int rc = zmq_msg_recv (&msg, client, 0);
        TEST_ASSERT_EQUAL_INT (1, rc);
125

126 127
        routing_id = zmq_msg_routing_id (&msg);
        TEST_ASSERT_EQUAL_UINT32 (0, routing_id);
128

129 130
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
    }
131

132 133 134
    test_context_socket_close (server);
    test_context_socket_close (client);
}
135

136 137 138
int main (void)
{
    setup_test_environment ();
139

140
    UNITY_BEGIN ();
141 142 143
    RUN_TEST (test_client_sndmore_fails);
    RUN_TEST (test_server_sndmore_fails);
    RUN_TEST (test_routing_id);
144
    return UNITY_END ();
145
}