test_reqrep_tcp.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 #include <string.h>
7 
9 
10 void test_single_connect (int ipv6_)
11 {
12  size_t len = MAX_SOCKET_STRING;
14 
15  void *sb = test_context_socket (ZMQ_REP);
16  bind_loopback (sb, ipv6_, my_endpoint, len);
17 
18  void *sc = test_context_socket (ZMQ_REQ);
20  zmq_setsockopt (sc, ZMQ_IPV6, &ipv6_, sizeof (int)));
22 
23  bounce (sb, sc);
24 
25  // the sockets are disconnected and unbound explicitly in this test case
26  // to check that this can be done successfully with the expected
27  // endpoints/addresses
28 
30 
32 
35 }
36 
37 void make_connect_address (char *connect_address_,
38  const int ipv6_,
39  const int port_,
40  const char *bind_address_)
41 {
42  if (ipv6_)
43  snprintf (connect_address_, 30 * sizeof (char), "tcp://[::1]:%i;%s",
44  port_, strrchr (bind_address_, '/') + 1);
45  else
46  snprintf (connect_address_, 38 * sizeof (char), "tcp://127.0.0.1:%i;%s",
47  port_, strrchr (bind_address_, '/') + 1);
48 }
49 
50 void test_multi_connect (int ipv6_)
51 {
52  size_t len = MAX_SOCKET_STRING;
53  char my_endpoint_0[MAX_SOCKET_STRING];
54  char my_endpoint_1[MAX_SOCKET_STRING];
55  char my_endpoint_2[MAX_SOCKET_STRING];
56  char my_endpoint_3[MAX_SOCKET_STRING * 2];
57 
58  void *sb0 = test_context_socket (ZMQ_REP);
59  bind_loopback (sb0, ipv6_, my_endpoint_0, len);
60 
61  void *sb1 = test_context_socket (ZMQ_REP);
62  bind_loopback (sb1, ipv6_, my_endpoint_1, len);
63 
64  void *sb2 = test_context_socket (ZMQ_REP);
65  bind_loopback (sb2, ipv6_, my_endpoint_2, len);
66 
67  void *sc = test_context_socket (ZMQ_REQ);
69  zmq_setsockopt (sc, ZMQ_IPV6, &ipv6_, sizeof (int)));
70  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_0));
71  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_1));
72  make_connect_address (my_endpoint_3, ipv6_, 5564, my_endpoint_2);
73  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
74 
75  bounce (sb0, sc);
76  bounce (sb1, sc);
77  bounce (sb2, sc);
78  bounce (sb0, sc);
79  bounce (sb1, sc);
80  bounce (sb2, sc);
81  bounce (sb0, sc);
82 
84 
85  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_0));
86  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_3));
87  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, my_endpoint_1));
88 
89  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
90  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
91  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb2, my_endpoint_2));
92 
97 }
98 
100 {
101  size_t len = MAX_SOCKET_STRING;
102  char my_endpoint_0[MAX_SOCKET_STRING];
103  char my_endpoint_1[MAX_SOCKET_STRING];
104  char my_endpoint_2[MAX_SOCKET_STRING * 2];
105  char my_endpoint_3[MAX_SOCKET_STRING * 2];
106  char my_endpoint_4[MAX_SOCKET_STRING * 2];
107  char my_endpoint_5[MAX_SOCKET_STRING * 2];
108 
109  void *sb0 = test_context_socket (ZMQ_REP);
110  bind_loopback (sb0, ipv6_, my_endpoint_0, len);
111 
112  void *sb1 = test_context_socket (ZMQ_REP);
113  bind_loopback (sb1, ipv6_, my_endpoint_1, len);
114 
115  void *sc0 = test_context_socket (ZMQ_REQ);
117  zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6_, sizeof (int)));
118  make_connect_address (my_endpoint_2, ipv6_, 5564, my_endpoint_0);
119  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
120  make_connect_address (my_endpoint_3, ipv6_, 5565, my_endpoint_1);
121  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
122 
123  void *sc1 = test_context_socket (ZMQ_REQ);
125  zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6_, sizeof (int)));
126  make_connect_address (my_endpoint_4, ipv6_, 5565, my_endpoint_0);
127  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
128  make_connect_address (my_endpoint_5, ipv6_, 5564, my_endpoint_1);
129  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
130 
131  bounce (sb0, sc0);
132  bounce (sb1, sc0);
133  bounce (sb0, sc1);
134  bounce (sb1, sc1);
135  bounce (sb0, sc0);
136  bounce (sb1, sc0);
137 
139 
140  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc1, my_endpoint_4));
141  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc1, my_endpoint_5));
142  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc0, my_endpoint_2));
143  TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc0, my_endpoint_3));
144 
145  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb0, my_endpoint_0));
146  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb1, my_endpoint_1));
147 
152 }
153 
155 {
156  test_single_connect (false);
157 }
158 
160 {
161  test_multi_connect (false);
162 }
163 
165 {
167 }
168 
170 {
171  test_single_connect (true);
172 }
173 
175 {
176  test_multi_connect (true);
177 }
178 
180 {
182 }
183 
184 int main (void)
185 {
187 
188  UNITY_BEGIN ();
195 
196  return UNITY_END ();
197 }
bounce
static void bounce(void *socket_)
Definition: test_req_relaxed.cpp:50
test_multi_connect
void test_multi_connect(int ipv6_)
Definition: test_reqrep_tcp.cpp:50
test_multi_connect_ipv4
void test_multi_connect_ipv4()
Definition: test_reqrep_tcp.cpp:159
UNITY_END
return UNITY_END()
test_multi_connect_same_port
void test_multi_connect_same_port(int ipv6_)
Definition: test_reqrep_tcp.cpp:99
test_single_connect_ipv6
void test_single_connect_ipv6()
Definition: test_reqrep_tcp.cpp:169
zmq_unbind
ZMQ_EXPORT int zmq_unbind(void *s_, const char *addr_)
Definition: zmq.cpp:337
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
ZMQ_REQ
#define ZMQ_REQ
Definition: zmq.h:261
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
testutil_unity.hpp
snprintf
int snprintf(char *str, size_t size, const char *format,...)
Definition: port.cc:64
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
zmq_disconnect
ZMQ_EXPORT int zmq_disconnect(void *s_, const char *addr_)
Definition: zmq.cpp:345
test_multi_connect_same_port_ipv6
void test_multi_connect_same_port_ipv6()
Definition: test_reqrep_tcp.cpp:179
sb
void * sb
Definition: test_channel.cpp:8
testutil.hpp
ZMQ_REP
#define ZMQ_REP
Definition: zmq.h:262
test_multi_connect_ipv6
void test_multi_connect_ipv6()
Definition: test_reqrep_tcp.cpp:174
main
int main(void)
Definition: test_reqrep_tcp.cpp:184
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_curve.cpp:31
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
bind_loopback
void bind_loopback(void *socket_, int ipv6_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:233
sc
void * sc
Definition: test_channel.cpp:9
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
make_connect_address
void make_connect_address(char *connect_address_, const int ipv6_, const int port_, const char *bind_address_)
Definition: test_reqrep_tcp.cpp:37
len
int len
Definition: php/ext/google/protobuf/map.c:206
ZMQ_IPV6
#define ZMQ_IPV6
Definition: zmq.h:307
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
test_multi_connect_same_port_ipv4
void test_multi_connect_same_port_ipv4()
Definition: test_reqrep_tcp.cpp:164
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
test_single_connect_ipv4
void test_single_connect_ipv4()
Definition: test_reqrep_tcp.cpp:154
test_single_connect
SETUP_TEARDOWN_TESTCONTEXT void test_single_connect(int ipv6_)
Definition: test_reqrep_tcp.cpp:10


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59