test_connect_rid.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 const char *rconn1routing_id = "conn1";
11 const char *x_routing_id = "X";
12 const char *y_routing_id = "Y";
13 const char *z_routing_id = "Z";
14 
16 {
17  char buff[256];
18  const char msg[] = "hi 1";
19  const int disabled = 0;
20  const int zero = 0;
22 
23  // Set up listener STREAM.
24  void *rbind = test_context_socket (ZMQ_STREAM);
26  zmq_setsockopt (rbind, ZMQ_STREAM_NOTIFY, &disabled, sizeof (disabled)));
27 
29  zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof zero));
31 
32  // Set up connection stream.
33  void *rconn1 = test_context_socket (ZMQ_STREAM);
35  zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof zero));
36 
37  // Do the connection.
40  strlen (rconn1routing_id)));
42 
43  /* Uncomment to test assert on duplicate routing id.
44  // Test duplicate connect attempt.
45  TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, rconn1routing_id, strlen(rconn1routing_id)));
46  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, bindip));
47 */
48  // Send data to the bound stream.
50  send_string_expect_success (rconn1, msg, 0);
51 
52  // Accept data on the bound stream.
54  0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (rbind, buff, 256, 0)));
55  TEST_ASSERT_EQUAL (0, buff[0]); // an auto-generated routing id
56  recv_string_expect_success (rbind, msg, 0);
57 
58  // Handle close of the socket.
62 }
63 
64 void test_router_2_router (bool named_)
65 {
66  char buff[256];
67  const char msg[] = "hi 1";
68  const int zero = 0;
70 
71  // Create bind socket.
72  void *rbind = test_context_socket (ZMQ_ROUTER);
74  zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero)));
76 
77  // Create connection socket.
78  void *rconn1 = test_context_socket (ZMQ_ROUTER);
80  zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero)));
81 
82  // If we're in named mode, set some identities.
83  if (named_) {
88  }
89 
90  // Make call to connect using a connect_routing_id.
93  strlen (rconn1routing_id)));
95  /* Uncomment to test assert on duplicate routing id
96  // Test duplicate connect attempt.
97  TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, rconn1routing_id, strlen (rconn1routing_id)));
98  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rconn1, bindip));
99 */
100  // Send some data.
101 
103  send_string_expect_success (rconn1, msg, 0);
104 
105  // Receive the name.
106  const int routing_id_len = zmq_recv (rbind, buff, 256, 0);
107  if (named_) {
108  TEST_ASSERT_EQUAL_INT (strlen (y_routing_id), routing_id_len);
109  TEST_ASSERT_EQUAL_STRING_LEN (y_routing_id, buff, routing_id_len);
110  } else {
111  TEST_ASSERT_TRUE (routing_id_len && 0 == buff[0]);
112  }
113 
114  // Receive the data.
115  recv_string_expect_success (rbind, msg, 0);
116 
117  // Send some data back.
118  const int ret = zmq_send (rbind, buff, routing_id_len, ZMQ_SNDMORE);
119  TEST_ASSERT_EQUAL_INT (routing_id_len, ret);
120  send_string_expect_success (rbind, "ok", 0);
121 
122  // If bound socket identity naming a problem, we'll likely see something funky here.
124  recv_string_expect_success (rconn1, "ok", 0);
125 
128  test_context_socket_close (rconn1);
129 }
130 
132 {
133  char buff[256];
134  const char msg[] = "hi 1";
135  const int zero = 0;
136  char x_endpoint[MAX_SOCKET_STRING];
137  char z_endpoint[MAX_SOCKET_STRING];
138 
139  // Create xbind socket.
140  void *xbind = test_context_socket (ZMQ_ROUTER);
142  zmq_setsockopt (xbind, ZMQ_LINGER, &zero, sizeof (zero)));
143  bind_loopback_ipv4 (xbind, x_endpoint, sizeof x_endpoint);
144 
145  // Create zbind socket.
146  void *zbind = test_context_socket (ZMQ_ROUTER);
148  zmq_setsockopt (zbind, ZMQ_LINGER, &zero, sizeof (zero)));
149  bind_loopback_ipv4 (zbind, z_endpoint, sizeof z_endpoint);
150 
151  // Create connection socket.
152  void *yconn = test_context_socket (ZMQ_ROUTER);
154  zmq_setsockopt (yconn, ZMQ_LINGER, &zero, sizeof (zero)));
155 
156  // set identities for each socket
158  xbind, ZMQ_ROUTING_ID, x_routing_id, strlen (x_routing_id)));
162  zbind, ZMQ_ROUTING_ID, z_routing_id, strlen (z_routing_id)));
163 
164  // Connect Y to X using a routing id
167  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (yconn, x_endpoint));
168 
169  // Send some data from Y to X.
171  send_string_expect_success (yconn, msg, 0);
172 
173  // wait for the Y->X message to be received
175 
176  // Now X tries to connect to Z and send a message
179  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (xbind, z_endpoint));
180 
181  // Try to send some data from X to Z.
183  send_string_expect_success (xbind, msg, 0);
184 
185  // wait for the X->Z message to be received (so that our non-blocking check will actually
186  // fail if the message is routed to Y)
188 
189  // nothing should have been received on the Y socket
191  zmq_recv (yconn, buff, 256, ZMQ_DONTWAIT));
192 
193  // the message should have been received on the Z socket
195  recv_string_expect_success (zbind, msg, 0);
196 
197  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (xbind, x_endpoint));
198  TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (zbind, z_endpoint));
199 
203 }
204 
206 {
207  test_router_2_router (false);
208 }
209 
211 {
212  test_router_2_router (true);
213 }
214 
215 int main ()
216 {
218 
219  UNITY_BEGIN ();
224  return UNITY_END ();
225 }
TEST_ASSERT_EQUAL_STRING_LEN
#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len)
Definition: unity.h:236
UNITY_END
return UNITY_END()
ZMQ_STREAM
#define ZMQ_STREAM
Definition: zmq.h:269
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
zmq_unbind
ZMQ_EXPORT int zmq_unbind(void *s_, const char *addr_)
Definition: zmq.cpp:337
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
TEST_ASSERT_GREATER_THAN
#define TEST_ASSERT_GREATER_THAN(threshold, actual)
Definition: unity.h:152
z_routing_id
const char * z_routing_id
Definition: test_connect_rid.cpp:13
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
ZMQ_STREAM_NOTIFY
#define ZMQ_STREAM_NOTIFY
Definition: zmq.h:334
test_router_2_router_while_receiving
void test_router_2_router_while_receiving()
Definition: test_connect_rid.cpp:131
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
ZMQ_CONNECT_ROUTING_ID
#define ZMQ_CONNECT_ROUTING_ID
Definition: zmq.h:323
testutil_unity.hpp
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
y_routing_id
const char * y_routing_id
Definition: test_connect_rid.cpp:12
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
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
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
TEST_ASSERT_EQUAL
#define TEST_ASSERT_EQUAL(expected, actual)
Definition: unity.h:133
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
x_routing_id
const char * x_routing_id
Definition: test_connect_rid.cpp:11
ZMQ_LINGER
#define ZMQ_LINGER
Definition: zmq.h:288
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
test_stream_2_stream
void test_stream_2_stream()
Definition: test_connect_rid.cpp:15
recv_string_expect_success
void recv_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:101
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
rconn1routing_id
const SETUP_TEARDOWN_TESTCONTEXT char * rconn1routing_id
Definition: test_connect_rid.cpp:10
UNITY_BEGIN
UNITY_BEGIN()
ZMQ_SNDMORE
#define ZMQ_SNDMORE
Definition: zmq.h:359
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
main
int main()
Definition: test_connect_rid.cpp:215
test_router_2_router
void test_router_2_router(bool named_)
Definition: test_connect_rid.cpp:64
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
test_router_2_router_unnamed
void test_router_2_router_unnamed()
Definition: test_connect_rid.cpp:205
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
test_router_2_router_named
void test_router_2_router_named()
Definition: test_connect_rid.cpp:210


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