2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2010-08-27 18:35:59 +02:00
|
|
|
|
2010-08-28 13:58:23 +02:00
|
|
|
#include "testutil.hpp"
|
2018-03-21 13:54:23 +01:00
|
|
|
#include "testutil_unity.hpp"
|
2010-08-28 13:58:23 +02:00
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2016-09-17 18:55:25 +01:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void test_single_connect (int ipv6_)
|
2018-03-21 13:54:23 +01:00
|
|
|
{
|
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
|
|
|
|
|
|
|
void *sb = test_context_socket (ZMQ_REP);
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (sb, ipv6_, my_endpoint, len);
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sc = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (sc, ZMQ_IPV6, &ipv6_, sizeof (int)));
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
bounce (sb, sc);
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 16:28:10 +01:00
|
|
|
// the sockets are disconnected and unbound explicitly in this test case
|
|
|
|
// to check that this can be done successfully with the expected
|
|
|
|
// endpoints/addresses
|
2018-03-21 15:01:25 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint));
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
test_context_socket_close (sc);
|
|
|
|
test_context_socket_close (sb);
|
2016-09-17 19:08:53 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void make_connect_address (char *connect_address_,
|
|
|
|
const int ipv6_,
|
|
|
|
const int port_,
|
|
|
|
const char *bind_address_)
|
2018-03-21 15:05:33 +01:00
|
|
|
{
|
2023-01-24 12:54:48 +01:00
|
|
|
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);
|
2018-03-21 15:05:33 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void test_multi_connect (int ipv6_)
|
2016-09-17 19:08:53 +01:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
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];
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sb0 = test_context_socket (ZMQ_REP);
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (sb0, ipv6_, my_endpoint_0, len);
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sb1 = test_context_socket (ZMQ_REP);
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (sb1, ipv6_, my_endpoint_1, len);
|
2018-03-21 13:54:23 +01:00
|
|
|
|
|
|
|
void *sb2 = test_context_socket (ZMQ_REP);
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (sb2, ipv6_, my_endpoint_2, len);
|
2018-03-21 13:54:23 +01:00
|
|
|
|
|
|
|
void *sc = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (sc, ZMQ_IPV6, &ipv6_, sizeof (int)));
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_0));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_1));
|
2018-05-27 07:01:36 -04:00
|
|
|
make_connect_address (my_endpoint_3, ipv6_, 5564, my_endpoint_2);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
|
2016-09-17 19:08:53 +01:00
|
|
|
|
|
|
|
bounce (sb0, sc);
|
|
|
|
bounce (sb1, sc);
|
|
|
|
bounce (sb2, sc);
|
|
|
|
bounce (sb0, sc);
|
|
|
|
bounce (sb1, sc);
|
|
|
|
bounce (sb2, sc);
|
|
|
|
bounce (sb0, sc);
|
|
|
|
|
2018-03-21 16:28:10 +01:00
|
|
|
/// see comment on zmq_disconnect/zmq_unbind in test_single_connect
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
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));
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
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));
|
2016-09-17 19:08:53 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
test_context_socket_close (sc);
|
|
|
|
test_context_socket_close (sb0);
|
|
|
|
test_context_socket_close (sb1);
|
|
|
|
test_context_socket_close (sb2);
|
2016-09-17 19:08:53 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void test_multi_connect_same_port (int ipv6_)
|
2016-10-23 21:30:51 +01:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
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];
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sb0 = test_context_socket (ZMQ_REP);
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (sb0, ipv6_, my_endpoint_0, len);
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sb1 = test_context_socket (ZMQ_REP);
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_loopback (sb1, ipv6_, my_endpoint_1, len);
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sc0 = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6_, sizeof (int)));
|
|
|
|
make_connect_address (my_endpoint_2, ipv6_, 5564, my_endpoint_0);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
|
2018-05-27 07:01:36 -04:00
|
|
|
make_connect_address (my_endpoint_3, ipv6_, 5565, my_endpoint_1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sc1 = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6_, sizeof (int)));
|
|
|
|
make_connect_address (my_endpoint_4, ipv6_, 5565, my_endpoint_0);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
|
2018-05-27 07:01:36 -04:00
|
|
|
make_connect_address (my_endpoint_5, ipv6_, 5564, my_endpoint_1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
|
2016-10-23 21:30:51 +01:00
|
|
|
|
|
|
|
bounce (sb0, sc0);
|
|
|
|
bounce (sb1, sc0);
|
|
|
|
bounce (sb0, sc1);
|
|
|
|
bounce (sb1, sc1);
|
|
|
|
bounce (sb0, sc0);
|
|
|
|
bounce (sb1, sc0);
|
|
|
|
|
2018-03-21 16:28:10 +01:00
|
|
|
/// see comment on zmq_disconnect/zmq_unbind in test_single_connect
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
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));
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
test_context_socket_close (sc0);
|
|
|
|
test_context_socket_close (sc1);
|
|
|
|
test_context_socket_close (sb0);
|
|
|
|
test_context_socket_close (sb1);
|
|
|
|
}
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_single_connect_ipv4 ()
|
|
|
|
{
|
|
|
|
test_single_connect (false);
|
|
|
|
}
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_ipv4 ()
|
|
|
|
{
|
|
|
|
test_multi_connect (false);
|
|
|
|
}
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_same_port_ipv4 ()
|
|
|
|
{
|
|
|
|
test_multi_connect_same_port (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_single_connect_ipv6 ()
|
|
|
|
{
|
|
|
|
test_single_connect (true);
|
|
|
|
}
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_ipv6 ()
|
|
|
|
{
|
|
|
|
test_multi_connect (true);
|
|
|
|
}
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_same_port_ipv6 ()
|
|
|
|
{
|
|
|
|
test_multi_connect_same_port (true);
|
2016-10-23 21:30:51 +01:00
|
|
|
}
|
|
|
|
|
2016-09-17 18:46:21 +01:00
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
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);
|
2016-10-23 21:30:51 +01:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
return UNITY_END ();
|
2010-08-27 18:35:59 +02:00
|
|
|
}
|