Commit 8372797c authored by Simon Giesecke's avatar Simon Giesecke

Problem: code duplication in test_reqrep_tcp

Solution: extracted bind_loopback
parent c84c3a52
......@@ -40,31 +40,13 @@ void tearDown ()
teardown_test_context ();
}
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)
{
if (ipv6 && !is_ipv6_available ()) {
TEST_IGNORE_MESSAGE ("ipv6 is not available");
}
return ipv6 ? bind_address_loopback_ipv6 : bind_address_loopback_ipv4;
}
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));
bind_loopback (sb, ipv6, my_endpoint, len);
void *sc = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (
......@@ -73,6 +55,9 @@ void test_single_connect (int ipv6)
bounce (sb, sc);
// TODO is explicit disconnect/unbind essential for the test? if not,
// these calls should probably be left out, for clarity/readability
TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, my_endpoint));
......@@ -89,30 +74,14 @@ void test_multi_connect (int ipv6)
char my_endpoint_2[MAX_SOCKET_STRING];
char my_endpoint_3[MAX_SOCKET_STRING * 2];
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));
bind_loopback (sb0, ipv6, my_endpoint_0, len);
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));
len = MAX_SOCKET_STRING;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb1, ZMQ_LAST_ENDPOINT, my_endpoint_1, &len));
bind_loopback (sb1, ipv6, 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));
len = MAX_SOCKET_STRING;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb2, ZMQ_LAST_ENDPOINT, my_endpoint_2, &len));
bind_loopback (sb2, ipv6, my_endpoint_2, len);
void *sc = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (
......@@ -159,22 +128,11 @@ void test_multi_connect_same_port (int ipv6)
char my_endpoint_4[MAX_SOCKET_STRING * 2];
char my_endpoint_5[MAX_SOCKET_STRING * 2];
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));
bind_loopback (sb0, ipv6, my_endpoint_0, len);
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));
len = MAX_SOCKET_STRING;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sb1, ZMQ_LAST_ENDPOINT, my_endpoint_1, &len));
bind_loopback (sb1, ipv6, my_endpoint_1, len);
void *sc0 = test_context_socket (ZMQ_REQ);
TEST_ASSERT_SUCCESS_ERRNO (
......
......@@ -192,9 +192,21 @@ void *test_context_socket_close (void *socket)
return socket;
}
void bind_loopback_ipv4 (void *socket, char *my_endpoint, size_t len)
void bind_loopback (void *socket, int ipv6, char *my_endpoint, size_t len)
{
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (socket, "tcp://127.0.0.1:*"));
if (ipv6 && !is_ipv6_available ()) {
TEST_IGNORE_MESSAGE ("ipv6 is not available");
}
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (socket, ZMQ_IPV6, &ipv6, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_bind (socket, ipv6 ? "tcp://[::1]:*" : "tcp://127.0.0.1:*"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (socket, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
}
void bind_loopback_ipv4 (void *socket, char *my_endpoint, size_t len)
{
bind_loopback (socket, false, my_endpoint, len);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment