1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/* SPDX-License-Identifier: MPL-2.0 */
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <string.h>
SETUP_TEARDOWN_TESTCONTEXT
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);
bind_loopback (sb, ipv6_, my_endpoint, len);
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));
bounce (sb, sc);
// the sockets are disconnected and unbound explicitly in this test case
// to check that this can be done successfully with the expected
// endpoints/addresses
TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
test_context_socket_close (sc);
test_context_socket_close (sb);
}
void make_connect_address (char *connect_address_,
const int ipv6_,
const int port_,
const char *bind_address_)
{
if (ipv6_)
snprintf (connect_address_, 30 * sizeof (char), "tcp://[::1]:%i;%s",
port_, strrchr (bind_address_, '/') + 1);
else
snprintf (connect_address_, 38 * sizeof (char), "tcp://127.0.0.1:%i;%s",
port_, strrchr (bind_address_, '/') + 1);
}
void test_multi_connect (int ipv6_)
{
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];
void *sb0 = test_context_socket (ZMQ_REP);
bind_loopback (sb0, ipv6_, my_endpoint_0, len);
void *sb1 = test_context_socket (ZMQ_REP);
bind_loopback (sb1, ipv6_, my_endpoint_1, len);
void *sb2 = test_context_socket (ZMQ_REP);
bind_loopback (sb2, ipv6_, my_endpoint_2, len);
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));
make_connect_address (my_endpoint_3, ipv6_, 5564, my_endpoint_2);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
bounce (sb0, sc);
bounce (sb1, sc);
bounce (sb2, sc);
bounce (sb0, sc);
bounce (sb1, sc);
bounce (sb2, sc);
bounce (sb0, sc);
/// see comment on zmq_disconnect/zmq_unbind in test_single_connect
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));
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));
test_context_socket_close (sc);
test_context_socket_close (sb0);
test_context_socket_close (sb1);
test_context_socket_close (sb2);
}
void test_multi_connect_same_port (int ipv6_)
{
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];
void *sb0 = test_context_socket (ZMQ_REP);
bind_loopback (sb0, ipv6_, my_endpoint_0, len);
void *sb1 = test_context_socket (ZMQ_REP);
bind_loopback (sb1, ipv6_, my_endpoint_1, len);
void *sc0 = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6_, sizeof (int)));
make_connect_address (my_endpoint_2, ipv6_, 5564, my_endpoint_0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
make_connect_address (my_endpoint_3, ipv6_, 5565, my_endpoint_1);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
void *sc1 = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6_, sizeof (int)));
make_connect_address (my_endpoint_4, ipv6_, 5565, my_endpoint_0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
make_connect_address (my_endpoint_5, ipv6_, 5564, my_endpoint_1);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
bounce (sb0, sc0);
bounce (sb1, sc0);
bounce (sb0, sc1);
bounce (sb1, sc1);
bounce (sb0, sc0);
bounce (sb1, sc0);
/// see comment on zmq_disconnect/zmq_unbind in test_single_connect
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));
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
test_context_socket_close (sc0);
test_context_socket_close (sc1);
test_context_socket_close (sb0);
test_context_socket_close (sb1);
}
void test_single_connect_ipv4 ()
{
test_single_connect (false);
}
void test_multi_connect_ipv4 ()
{
test_multi_connect (false);
}
void test_multi_connect_same_port_ipv4 ()
{
test_multi_connect_same_port (false);
}
void test_single_connect_ipv6 ()
{
test_single_connect (true);
}
void test_multi_connect_ipv6 ()
{
test_multi_connect (true);
}
void test_multi_connect_same_port_ipv6 ()
{
test_multi_connect_same_port (true);
}
int main (void)
{
setup_test_environment ();
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);
return UNITY_END ();
}
|