test_reqrep_tcp.cpp 7.23 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
    You should have received a copy of the GNU Lesser General Public License
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "testutil.hpp"
31
#include "testutil_unity.hpp"
32

33
void setUp ()
34
{
35 36 37 38 39 40 41
    setup_test_context ();
}

void tearDown ()
{
    teardown_test_context ();
}
42

43 44 45 46 47 48
void test_single_connect (int ipv6)
{
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];

    void *sb = test_context_socket (ZMQ_REP);
49
    bind_loopback (sb, ipv6, my_endpoint, len);
50

51 52 53 54
    void *sc = test_context_socket (ZMQ_REQ);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
55

56
    bounce (sb, sc);
57

58 59 60
    // the sockets are disconnected and unbound explicitly in this test case
    // to check that this can be done successfully with the expected
    // endpoints/addresses
61

62
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint));
63

64
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
65

66 67
    test_context_socket_close (sc);
    test_context_socket_close (sb);
68 69
}

70 71 72 73 74 75 76 77 78
void make_connect_address (char *connect_address,
                           const int ipv6,
                           const int port,
                           const char *bind_address)
{
    sprintf (connect_address, "tcp://%s:%i;%s", ipv6 ? "[::1]" : "127.0.0.1",
             port, strrchr (bind_address, '/') + 1);
}

79
void test_multi_connect (int ipv6)
80
{
81 82 83 84 85 86
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint_0[MAX_SOCKET_STRING];
    char my_endpoint_1[MAX_SOCKET_STRING];
    char my_endpoint_2[MAX_SOCKET_STRING];
    char my_endpoint_3[MAX_SOCKET_STRING * 2];

87
    void *sb0 = test_context_socket (ZMQ_REP);
88
    bind_loopback (sb0, ipv6, my_endpoint_0, len);
89

90
    void *sb1 = test_context_socket (ZMQ_REP);
91
    bind_loopback (sb1, ipv6, my_endpoint_1, len);
92 93

    void *sb2 = test_context_socket (ZMQ_REP);
94
    bind_loopback (sb2, ipv6, my_endpoint_2, len);
95 96 97 98 99 100

    void *sc = test_context_socket (ZMQ_REQ);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_0));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_1));
101
    make_connect_address (my_endpoint_3, ipv6, 5564, my_endpoint_2);
102
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
103 104 105 106 107 108 109 110 111

    bounce (sb0, sc);
    bounce (sb1, sc);
    bounce (sb2, sc);
    bounce (sb0, sc);
    bounce (sb1, sc);
    bounce (sb2, sc);
    bounce (sb0, sc);

112 113
    /// see comment on zmq_disconnect/zmq_unbind in test_single_connect

114 115 116
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_0));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_3));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_1));
117

118 119 120
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb2, my_endpoint_2));
121

122 123 124 125
    test_context_socket_close (sc);
    test_context_socket_close (sb0);
    test_context_socket_close (sb1);
    test_context_socket_close (sb2);
126 127
}

128
void test_multi_connect_same_port (int ipv6)
129
{
130 131 132 133 134 135 136 137
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint_0[MAX_SOCKET_STRING];
    char my_endpoint_1[MAX_SOCKET_STRING];
    char my_endpoint_2[MAX_SOCKET_STRING * 2];
    char my_endpoint_3[MAX_SOCKET_STRING * 2];
    char my_endpoint_4[MAX_SOCKET_STRING * 2];
    char my_endpoint_5[MAX_SOCKET_STRING * 2];

138
    void *sb0 = test_context_socket (ZMQ_REP);
139
    bind_loopback (sb0, ipv6, my_endpoint_0, len);
140

141
    void *sb1 = test_context_socket (ZMQ_REP);
142
    bind_loopback (sb1, ipv6, my_endpoint_1, len);
143

144 145 146
    void *sc0 = test_context_socket (ZMQ_REQ);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6, sizeof (int)));
147
    make_connect_address (my_endpoint_2, ipv6, 5564, my_endpoint_0);
148
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
149
    make_connect_address (my_endpoint_3, ipv6, 5565, my_endpoint_1);
150
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
151

152 153 154
    void *sc1 = test_context_socket (ZMQ_REQ);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6, sizeof (int)));
155
    make_connect_address (my_endpoint_4, ipv6, 5565, my_endpoint_0);
156
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
157
    make_connect_address (my_endpoint_5, ipv6, 5564, my_endpoint_1);
158
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
159 160 161 162 163 164 165 166

    bounce (sb0, sc0);
    bounce (sb1, sc0);
    bounce (sb0, sc1);
    bounce (sb1, sc1);
    bounce (sb0, sc0);
    bounce (sb1, sc0);

167 168
    /// see comment on zmq_disconnect/zmq_unbind in test_single_connect

169 170 171 172
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc1, my_endpoint_4));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc1, my_endpoint_5));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc0, my_endpoint_2));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc0, my_endpoint_3));
173

174 175
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
176

177 178 179 180 181
    test_context_socket_close (sc0);
    test_context_socket_close (sc1);
    test_context_socket_close (sb0);
    test_context_socket_close (sb1);
}
182

183 184 185 186
void test_single_connect_ipv4 ()
{
    test_single_connect (false);
}
187

188 189 190 191
void test_multi_connect_ipv4 ()
{
    test_multi_connect (false);
}
192

193 194 195 196 197 198 199 200 201
void test_multi_connect_same_port_ipv4 ()
{
    test_multi_connect_same_port (false);
}

void test_single_connect_ipv6 ()
{
    test_single_connect (true);
}
202

203 204 205 206
void test_multi_connect_ipv6 ()
{
    test_multi_connect (true);
}
207

208 209 210
void test_multi_connect_same_port_ipv6 ()
{
    test_multi_connect_same_port (true);
211 212
}

213 214 215 216
int main (void)
{
    setup_test_environment ();

217 218 219 220 221 222 223
    UNITY_BEGIN ();
    RUN_TEST (test_single_connect_ipv4);
    RUN_TEST (test_multi_connect_ipv4);
    RUN_TEST (test_multi_connect_same_port_ipv4);
    RUN_TEST (test_single_connect_ipv6);
    RUN_TEST (test_multi_connect_ipv6);
    RUN_TEST (test_multi_connect_same_port_ipv6);
224

225
    return UNITY_END ();
226
}