test_router_notify.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 
11 {
12  void *router = test_context_socket (ZMQ_ROUTER);
13  int opt_notify;
14 
15  int opt_notify_read;
16  size_t opt_notify_read_size = sizeof (opt_notify_read);
17 
18 
19  // default value is off when socket is constructed
21  router, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
22 
23  TEST_ASSERT_EQUAL (0, opt_notify_read);
24 
25 
26  // valid value - Connect
27  opt_notify = ZMQ_NOTIFY_CONNECT;
29  router, ZMQ_ROUTER_NOTIFY, &opt_notify, sizeof (opt_notify)));
30 
32  router, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
33 
34  TEST_ASSERT_EQUAL (opt_notify, opt_notify_read);
35 
36 
37  // valid value - Disconnect
38  opt_notify = ZMQ_NOTIFY_DISCONNECT;
40  router, ZMQ_ROUTER_NOTIFY, &opt_notify, sizeof (opt_notify)));
41 
43  router, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
44 
45  TEST_ASSERT_EQUAL (opt_notify, opt_notify_read);
46 
47 
48  // valid value - Off
49  opt_notify = 0;
51  router, ZMQ_ROUTER_NOTIFY, &opt_notify, sizeof (opt_notify)));
52 
54  router, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
55 
56  TEST_ASSERT_EQUAL (opt_notify, opt_notify_read);
57 
58 
59  // valid value - Both
62  router, ZMQ_ROUTER_NOTIFY, &opt_notify, sizeof (opt_notify)));
63 
65  router, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
66 
67  TEST_ASSERT_EQUAL (opt_notify, opt_notify_read);
68 
69 
70  // value boundary
71  opt_notify = -1;
73  EINVAL, zmq_setsockopt (router, ZMQ_ROUTER_NOTIFY, &opt_notify,
74  sizeof (opt_notify)));
75 
76  opt_notify = (ZMQ_NOTIFY_CONNECT | ZMQ_NOTIFY_DISCONNECT) + 1;
78  EINVAL, zmq_setsockopt (router, ZMQ_ROUTER_NOTIFY, &opt_notify,
79  sizeof (opt_notify)));
80 
81  // failures don't update the value
83  router, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
84 
86  opt_notify_read);
87 
88 
90 
91 
92  // check a non-router socket type
93  void *dealer = test_context_socket (ZMQ_DEALER);
94 
95  // setsockopt fails for non-router sockets
96  opt_notify = ZMQ_NOTIFY_CONNECT;
98  EINVAL, zmq_setsockopt (dealer, ZMQ_ROUTER_NOTIFY, &opt_notify,
99  sizeof (opt_notify)));
100 
101  // getsockopts returns off for any non-router socket
103  dealer, ZMQ_ROUTER_NOTIFY, &opt_notify_read, &opt_notify_read_size));
104 
105  TEST_ASSERT_EQUAL (0, opt_notify_read);
106 
107 
108  test_context_socket_close (dealer);
109 }
110 
111 
112 void test_router_notify_helper (int opt_notify_)
113 {
114  void *router = test_context_socket (ZMQ_ROUTER);
115  int opt_more;
116  size_t opt_more_length = sizeof (opt_more);
117  int opt_events;
118  size_t opt_events_length = sizeof (opt_events);
120 
121 
122  // valid values
124  router, ZMQ_ROUTER_NOTIFY, &opt_notify_, sizeof (opt_notify_)));
125 
127 
128  void *dealer = test_context_socket (ZMQ_DEALER);
129  const char *dealer_routing_id = "X";
130 
132  zmq_setsockopt (dealer, ZMQ_ROUTING_ID, dealer_routing_id, 1));
133 
134  // dealer connects
136 
137  // connection notification msg
138  if (opt_notify_ & ZMQ_NOTIFY_CONNECT) {
139  // routing-id only message of the connect
140  recv_string_expect_success (router, dealer_routing_id,
141  0); // 1st part: routing-id
142  recv_string_expect_success (router, "", 0); // 2nd part: empty
144  zmq_getsockopt (router, ZMQ_RCVMORE, &opt_more, &opt_more_length));
145  TEST_ASSERT_EQUAL (0, opt_more);
146  }
147 
148  // test message from the dealer
149  send_string_expect_success (dealer, "Hello", 0);
150  recv_string_expect_success (router, dealer_routing_id, 0);
151  recv_string_expect_success (router, "Hello", 0);
153  zmq_getsockopt (router, ZMQ_RCVMORE, &opt_more, &opt_more_length));
154  TEST_ASSERT_EQUAL (0, opt_more);
155 
156  // dealer disconnects
158 
159  // need one more process_commands() (???)
161  zmq_getsockopt (dealer, ZMQ_EVENTS, &opt_events, &opt_events_length);
162 
163  // connection notification msg
164  if (opt_notify_ & ZMQ_NOTIFY_DISCONNECT) {
165  // routing-id only message of the connect
166  recv_string_expect_success (router, dealer_routing_id,
167  0); // 1st part: routing-id
168  recv_string_expect_success (router, "", 0); // 2nd part: empty
170  zmq_getsockopt (router, ZMQ_RCVMORE, &opt_more, &opt_more_length));
171  TEST_ASSERT_EQUAL (0, opt_more);
172  }
173 
174  test_context_socket_close (dealer);
175  test_context_socket_close (router);
176 }
177 
178 
180 {
182 }
183 
184 
186 {
188 }
189 
190 
192 {
194 }
195 
196 
198 {
199  // setup router socket
200  void *router = test_context_socket (ZMQ_ROUTER);
201  int opt_timeout = 200;
202  int opt_notify = ZMQ_NOTIFY_CONNECT | ZMQ_NOTIFY_DISCONNECT;
204 
205  // valid values
207  router, ZMQ_ROUTER_NOTIFY, &opt_notify, sizeof (opt_notify)));
208 
210  router, ZMQ_RCVTIMEO, &opt_timeout, sizeof (opt_timeout)));
211 
213 
214  // send something on raw tcp
217 
218  send_string_expect_success (stream, "not-a-handshake", 0);
219 
222 
223  // no notification delivered
224  char buffer[255];
226  zmq_recv (router, buffer, sizeof (buffer), 0));
227 
228  test_context_socket_close (router);
229 }
230 
231 
233 {
234  /*
235  * If the disconnect occurs in the middle of the multipart
236  * message, the socket should not add the notification at the
237  * end of the incomplete message. It must discard the incomplete
238  * message, and delivert the notification as a new message.
239  */
240 
242  char long_str[128] = {0};
243  memset (long_str, '*', sizeof (long_str) - 1);
244 
245  // setup router
246  void *router = test_context_socket (ZMQ_ROUTER);
247 
248  int opt_notify = ZMQ_NOTIFY_DISCONNECT;
250  router, ZMQ_ROUTER_NOTIFY, &opt_notify, sizeof (opt_notify)));
251 
252  int64_t opt_maxmsgsize = 64; // the handshake fails if this is too small
254  router, ZMQ_MAXMSGSIZE, &opt_maxmsgsize, sizeof (opt_maxmsgsize)));
255 
257 
258  // setup dealer
259  void *dealer = test_context_socket (ZMQ_DEALER);
260  const char *dealer_routing_id = "X";
261 
263  zmq_setsockopt (dealer, ZMQ_ROUTING_ID, dealer_routing_id, 1));
264 
266 
267 
268  // send multipart message, the 2nd part causes a disconnect.
269  send_string_expect_success (dealer, "Hello2", ZMQ_SNDMORE);
270  send_string_expect_success (dealer, long_str, 0);
271 
272  // disconnect notification
273  recv_string_expect_success (router, dealer_routing_id, 0);
274  recv_string_expect_success (router, "", 0);
275 
276 
277  test_context_socket_close (dealer);
278  test_context_socket_close (router);
279 }
280 
281 
282 int main (void)
283 {
285 
286  UNITY_BEGIN ();
293 
294  return UNITY_END ();
295 }
stream
GLuint GLuint stream
Definition: glcorearb.h:3946
UNITY_END
return UNITY_END()
ZMQ_EVENTS
#define ZMQ_EVENTS
Definition: zmq.h:286
ZMQ_STREAM
#define ZMQ_STREAM
Definition: zmq.h:269
EINVAL
#define EINVAL
Definition: errno.hpp:25
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
test_sockopt_router_notify
SETUP_TEARDOWN_TESTCONTEXT void test_sockopt_router_notify()
Definition: test_router_notify.cpp:10
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
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
test_router_notify_both
void test_router_notify_both()
Definition: test_router_notify.cpp:191
testutil_unity.hpp
ZMQ_DEALER
#define ZMQ_DEALER
Definition: zmq.h:263
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
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
test_router_notify_disconnect
void test_router_notify_disconnect()
Definition: test_router_notify.cpp:185
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
buffer
Definition: buffer_processor.h:43
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
TEST_ASSERT_EQUAL
#define TEST_ASSERT_EQUAL(expected, actual)
Definition: unity.h:133
ZMQ_NOTIFY_DISCONNECT
#define ZMQ_NOTIFY_DISCONNECT
Definition: zmq_draft.h:102
test_router_notify_helper
void test_router_notify_helper(int opt_notify_)
Definition: test_router_notify.cpp:112
ZMQ_NOTIFY_CONNECT
#define ZMQ_NOTIFY_CONNECT
Definition: zmq_draft.h:101
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
ZMQ_RCVTIMEO
#define ZMQ_RCVTIMEO
Definition: zmq.h:296
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
test_error_during_multipart
void test_error_during_multipart()
Definition: test_router_notify.cpp:232
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
UNITY_BEGIN
UNITY_BEGIN()
connect_address
SETUP_TEARDOWN_TESTCONTEXT char connect_address[MAX_SOCKET_STRING]
Definition: tests/test_fork.cpp:14
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
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
ZMQ_ROUTER_NOTIFY
#define ZMQ_ROUTER_NOTIFY
Definition: zmq_draft.h:29
main
int main(void)
Definition: test_router_notify.cpp:282
ZMQ_MAXMSGSIZE
#define ZMQ_MAXMSGSIZE
Definition: zmq.h:292
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
ZMQ_RCVMORE
#define ZMQ_RCVMORE
Definition: zmq.h:284
test_router_notify_connect
void test_router_notify_connect()
Definition: test_router_notify.cpp:179
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261
test_handshake_fail
void test_handshake_fail()
Definition: test_router_notify.cpp:197


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