2010-08-27 18:35:59 +02:00
|
|
|
/*
|
2017-05-01 13:11:11 +02:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2010-08-27 18:35:59 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2010-08-27 18:35:59 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2010-08-27 18:35:59 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2010-08-27 18:35:59 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-08-27 18:35:59 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void setUp ()
|
2010-08-27 18:35:59 +02:00
|
|
|
{
|
2018-03-21 13:54:23 +01:00
|
|
|
setup_test_context ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown ()
|
|
|
|
{
|
|
|
|
teardown_test_context ();
|
|
|
|
}
|
2016-09-17 19:55:25 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
const char *bind_address_loopback_ipv4 = "tcp://127.0.0.1:*";
|
|
|
|
const char *bind_address_loopback_ipv6 = "tcp://[::1]:*";
|
|
|
|
|
|
|
|
const char *get_loopback_bin_address (bool ipv6)
|
|
|
|
{
|
2017-05-01 13:11:11 +02:00
|
|
|
if (ipv6 && !is_ipv6_available ()) {
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_IGNORE_MESSAGE ("ipv6 is not available");
|
2016-09-17 20:08:53 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
return ipv6 ? bind_address_loopback_ipv6 : bind_address_loopback_ipv4;
|
|
|
|
}
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_single_connect (int ipv6)
|
|
|
|
{
|
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
|
|
|
|
|
|
|
const char *address = get_loopback_bin_address (ipv6);
|
|
|
|
|
|
|
|
void *sb = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, address));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
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));
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
bounce (sb, sc);
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint));
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
test_context_socket_close (sc);
|
|
|
|
test_context_socket_close (sb);
|
2016-09-17 20:08:53 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect (int ipv6)
|
2016-09-17 20:08:53 +02:00
|
|
|
{
|
2017-05-01 13:11:11 +02: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
|
|
|
const char *address = get_loopback_bin_address (ipv6);
|
|
|
|
|
|
|
|
void *sb0 = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sb0, ZMQ_IPV6, &ipv6, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb0, address));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sb0, ZMQ_LAST_ENDPOINT, my_endpoint_0, &len));
|
2016-09-17 20:08:53 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sb1 = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sb1, ZMQ_IPV6, &ipv6, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb1, address));
|
2017-05-01 13:11:11 +02:00
|
|
|
len = MAX_SOCKET_STRING;
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sb1, ZMQ_LAST_ENDPOINT, my_endpoint_1, &len));
|
|
|
|
|
|
|
|
void *sb2 = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sb2, ZMQ_IPV6, &ipv6, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb2, address));
|
2017-05-01 13:11:11 +02:00
|
|
|
len = MAX_SOCKET_STRING;
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sb2, ZMQ_LAST_ENDPOINT, 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));
|
2017-05-01 13:11:11 +02:00
|
|
|
if (!ipv6)
|
|
|
|
sprintf (my_endpoint_3, "tcp://127.0.0.1:5564;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_2, '/') + 1);
|
2017-05-01 13:11:11 +02:00
|
|
|
else
|
|
|
|
sprintf (my_endpoint_3, "tcp://[::1]:5564;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_2, '/') + 1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
|
2016-09-17 20:08:53 +02: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 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 20:08:53 +02: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 20:08:53 +02: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 20:08:53 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_same_port (int ipv6)
|
2016-10-23 22:30:51 +02:00
|
|
|
{
|
2017-05-01 13:11:11 +02: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
|
|
|
const char *address = get_loopback_bin_address (ipv6);
|
|
|
|
|
|
|
|
void *sb0 = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sb0, ZMQ_IPV6, &ipv6, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb0, address));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sb0, ZMQ_LAST_ENDPOINT, my_endpoint_0, &len));
|
2016-10-23 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sb1 = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sb1, ZMQ_IPV6, &ipv6, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb1, address));
|
2017-05-01 13:11:11 +02:00
|
|
|
len = MAX_SOCKET_STRING;
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sb1, ZMQ_LAST_ENDPOINT, my_endpoint_1, &len));
|
2016-10-23 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sc0 = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6, sizeof (int)));
|
2017-05-01 13:11:11 +02:00
|
|
|
if (!ipv6)
|
|
|
|
sprintf (my_endpoint_2, "tcp://127.0.0.1:5564;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_0, '/') + 1);
|
2017-05-01 13:11:11 +02:00
|
|
|
else
|
|
|
|
sprintf (my_endpoint_2, "tcp://[::1]:5564;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_0, '/') + 1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
|
2017-05-01 13:11:11 +02:00
|
|
|
if (!ipv6)
|
|
|
|
sprintf (my_endpoint_3, "tcp://127.0.0.1:5565;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_1, '/') + 1);
|
2017-05-01 13:11:11 +02:00
|
|
|
else
|
|
|
|
sprintf (my_endpoint_3, "tcp://[::1]:5565;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_1, '/') + 1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
|
2016-10-23 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void *sc1 = test_context_socket (ZMQ_REQ);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6, sizeof (int)));
|
2017-05-01 13:11:11 +02:00
|
|
|
if (!ipv6)
|
|
|
|
sprintf (my_endpoint_4, "tcp://127.0.0.1:5565;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_0, '/') + 1);
|
2017-05-01 13:11:11 +02:00
|
|
|
else
|
|
|
|
sprintf (my_endpoint_4, "tcp://[::1]:5565;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_0, '/') + 1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
|
2017-05-01 13:11:11 +02:00
|
|
|
if (!ipv6)
|
|
|
|
sprintf (my_endpoint_5, "tcp://127.0.0.1:5564;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_1, '/') + 1);
|
2017-05-01 13:11:11 +02:00
|
|
|
else
|
|
|
|
sprintf (my_endpoint_5, "tcp://[::1]:5564;%s",
|
2018-02-01 11:46:09 +01:00
|
|
|
strrchr (my_endpoint_1, '/') + 1);
|
2018-03-21 13:54:23 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
|
2016-10-23 22:30:51 +02:00
|
|
|
|
|
|
|
bounce (sb0, sc0);
|
|
|
|
bounce (sb1, sc0);
|
|
|
|
bounce (sb0, sc1);
|
|
|
|
bounce (sb1, sc1);
|
|
|
|
bounce (sb0, sc0);
|
|
|
|
bounce (sb1, sc0);
|
|
|
|
|
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 22:30:51 +02: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 22:30:51 +02: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 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_single_connect_ipv4 ()
|
|
|
|
{
|
|
|
|
test_single_connect (false);
|
|
|
|
}
|
2016-10-23 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_ipv4 ()
|
|
|
|
{
|
|
|
|
test_multi_connect (false);
|
|
|
|
}
|
2016-10-23 22:30:51 +02: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 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
void test_multi_connect_ipv6 ()
|
|
|
|
{
|
|
|
|
test_multi_connect (true);
|
|
|
|
}
|
2016-10-23 22:30:51 +02: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 22:30:51 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 19:46:21 +02: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 22:30:51 +02:00
|
|
|
|
2018-03-21 13:54:23 +01:00
|
|
|
return UNITY_END ();
|
2010-08-27 18:35:59 +02:00
|
|
|
}
|