Skip to content

Commit 086bb89

Browse files
authored
Merge pull request #2282 from bluca/event_test
Problems: test_monitor failing, DRAFT events leaking when draft APIs are disabled
2 parents 0ebed80 + 3f3601e commit 086bb89

5 files changed

Lines changed: 123 additions & 1 deletion

File tree

NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
0MQ version 4.2.1 stable, released on 201x/xx/xx
22
=============================================
33

4+
* New DRAFT (see NEWS for 4.2.0) Socket Monitor events:
5+
- ZMQ_EVENT_HANDSHAKE_SUCCEED
6+
- ZMQ_EVENT_HANDSHAKE_FAILED
7+
These events trigger when the ZMTP security mechanism handshake is
8+
completed. See doc/zmq_socket_monitor.txt for more information.
9+
410
* New DRAFT (see NEWS for 4.2.0) Context options:
511
- ZMQ_MSG_T_SIZE
12+
See doc/zmq_ctx_get.txt for more information.
613

714
* Fixed #2268 - improved compatibility with mingw32
815

doc/zmq_socket_monitor.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ your own 'ZMQ_PAIR' socket, and connect that to the endpoint.
2323

2424
The 'events' argument is a bitmask of the socket events you wish to
2525
monitor, see 'Supported events' below. To monitor all events, use the
26-
event value ZMQ_EVENT_ALL.
26+
event value ZMQ_EVENT_ALL. NOTE: as new events are added, the catch-all
27+
value will start returning them. An application that relies on a strict
28+
and fixed sequence of events must not use ZMQ_EVENT_ALL in order to
29+
guarantee compatibility with future versions.
2730

2831
Each event is sent as two frames. The first frame contains an event
2932
number (16 bits), and an event value (32 bits) that provides additional
@@ -96,6 +99,18 @@ ZMQ_EVENT_MONITOR_STOPPED
9699
~~~~~~~~~~~~~~~~~~~~~~~~~
97100
Monitoring on this socket ended.
98101

102+
ZMQ_EVENT_HANDSHAKE_FAILED
103+
~~~~~~~~~~~~~~~~~~~~~~~~~~
104+
The ZMTP security mechanism handshake failed.
105+
The event value is unspecified.
106+
NOTE: in DRAFT state, not yet available in stable releases.
107+
108+
ZMQ_EVENT_HANDSHAKE_SUCCEED
109+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
The ZMTP security mechanism handshake succeeded.
111+
The event value is unspecified.
112+
NOTE: in DRAFT state, not yet available in stable releases.
113+
99114

100115
RETURN VALUE
101116
------------

src/stream_engine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,10 @@ int zmq::stream_engine_t::next_handshake_command (msg_t *msg_)
790790

791791
if (rc == 0)
792792
msg_->set_flags (msg_t::command);
793+
#ifdef ZMQ_BUILD_DRAFT_API
793794
if(mechanism->status() == mechanism_t::error)
794795
socket->event_handshake_failed(endpoint, 0);
796+
#endif
795797

796798
return rc;
797799
}
@@ -871,7 +873,9 @@ void zmq::stream_engine_t::mechanism_ready ()
871873
if (!properties.empty ())
872874
metadata = new (std::nothrow) metadata_t (properties);
873875

876+
#ifdef ZMQ_BUILD_DRAFT_API
874877
socket->event_handshake_succeed(endpoint, 0);
878+
#endif
875879
}
876880

877881
int zmq::stream_engine_t::pull_msg_from_session (msg_t *msg_)
@@ -976,8 +980,10 @@ void zmq::stream_engine_t::error (error_reason_t reason)
976980
terminator.close();
977981
}
978982
zmq_assert (session);
983+
#ifdef ZMQ_BUILD_DRAFT_API
979984
if(reason == encryption_error)
980985
socket->event_handshake_failed(endpoint, (int) s);
986+
#endif
981987
socket->event_disconnected (endpoint, (int) s);
982988
session->flush ();
983989
session->engine_error (reason);

tests/test_monitor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ int main (void)
116116
if (event == ZMQ_EVENT_CONNECT_DELAYED)
117117
event = get_monitor_event (client_mon, NULL, NULL);
118118
assert (event == ZMQ_EVENT_CONNECTED);
119+
#ifdef ZMQ_BUILD_DRAFT_API
120+
event = get_monitor_event (client_mon, NULL, NULL);
121+
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEED);
122+
#endif
119123
event = get_monitor_event (client_mon, NULL, NULL);
120124
assert (event == ZMQ_EVENT_MONITOR_STOPPED);
121125

@@ -124,6 +128,10 @@ int main (void)
124128
assert (event == ZMQ_EVENT_LISTENING);
125129
event = get_monitor_event (server_mon, NULL, NULL);
126130
assert (event == ZMQ_EVENT_ACCEPTED);
131+
#ifdef ZMQ_BUILD_DRAFT_API
132+
event = get_monitor_event (server_mon, NULL, NULL);
133+
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEED);
134+
#endif
127135
event = get_monitor_event (server_mon, NULL, NULL);
128136
// Sometimes the server sees the client closing before it gets closed.
129137
if (event != ZMQ_EVENT_DISCONNECTED) {

tests/test_security_curve.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ static char client_secret [41];
4646
static char server_public [41];
4747
static char server_secret [41];
4848

49+
#ifdef ZMQ_BUILD_DRAFT_API
50+
// Read one event off the monitor socket; return value and address
51+
// by reference, if not null, and event number by value. Returns -1
52+
// in case of error.
53+
54+
static int
55+
get_monitor_event (void *monitor, int *value, char **address)
56+
{
57+
// First frame in message contains event number and value
58+
zmq_msg_t msg;
59+
zmq_msg_init (&msg);
60+
if (zmq_msg_recv (&msg, monitor, 0) == -1)
61+
return -1; // Interruped, presumably
62+
assert (zmq_msg_more (&msg));
63+
64+
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
65+
uint16_t event = *(uint16_t *) (data);
66+
if (value)
67+
*value = *(uint32_t *) (data + 2);
68+
69+
// Second frame in message contains event address
70+
zmq_msg_init (&msg);
71+
if (zmq_msg_recv (&msg, monitor, 0) == -1)
72+
return -1; // Interruped, presumably
73+
assert (!zmq_msg_more (&msg));
74+
75+
if (address) {
76+
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
77+
size_t size = zmq_msg_size (&msg);
78+
*address = (char *) malloc (size + 1);
79+
memcpy (*address, data, size);
80+
*address [size] = 0;
81+
}
82+
return event;
83+
}
84+
#endif
85+
86+
4987
// --------------------------------------------------------------------------
5088
// This methods receives and validates ZAP requestes (allowing or denying
5189
// each client connection).
@@ -138,6 +176,21 @@ int main (void)
138176
rc = zmq_bind (server, "tcp://127.0.0.1:9998");
139177
assert (rc == 0);
140178

179+
#ifdef ZMQ_BUILD_DRAFT_API
180+
// Monitor handshake events on the server
181+
rc = zmq_socket_monitor (server, "inproc://monitor-server",
182+
ZMQ_EVENT_HANDSHAKE_SUCCEED | ZMQ_EVENT_HANDSHAKE_FAILED);
183+
assert (rc == 0);
184+
185+
// Create socket for collecting monitor events
186+
void *server_mon = zmq_socket (ctx, ZMQ_PAIR);
187+
assert (server_mon);
188+
189+
// Connect it to the inproc endpoints so they'll get events
190+
rc = zmq_connect (server_mon, "inproc://monitor-server");
191+
assert (rc == 0);
192+
#endif
193+
141194
// Check CURVE security with valid credentials
142195
void *client = zmq_socket (ctx, ZMQ_DEALER);
143196
assert (client);
@@ -153,6 +206,11 @@ int main (void)
153206
rc = zmq_close (client);
154207
assert (rc == 0);
155208

209+
#ifdef ZMQ_BUILD_DRAFT_API
210+
int event = get_monitor_event (server_mon, NULL, NULL);
211+
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEED);
212+
#endif
213+
156214
// Check CURVE security with a garbage server key
157215
// This will be caught by the curve_server class, not passed to ZAP
158216
char garbage_key [] = "0000000000000000000000000000000000000000";
@@ -169,6 +227,11 @@ int main (void)
169227
expect_bounce_fail (server, client);
170228
close_zero_linger (client);
171229

230+
#ifdef ZMQ_BUILD_DRAFT_API
231+
event = get_monitor_event (server_mon, NULL, NULL);
232+
assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
233+
#endif
234+
172235
// Check CURVE security with a garbage client public key
173236
// This will be caught by the curve_server class, not passed to ZAP
174237
client = zmq_socket (ctx, ZMQ_DEALER);
@@ -184,6 +247,11 @@ int main (void)
184247
expect_bounce_fail (server, client);
185248
close_zero_linger (client);
186249

250+
#ifdef ZMQ_BUILD_DRAFT_API
251+
event = get_monitor_event (server_mon, NULL, NULL);
252+
assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
253+
#endif
254+
187255
// Check CURVE security with a garbage client secret key
188256
// This will be caught by the curve_server class, not passed to ZAP
189257
client = zmq_socket (ctx, ZMQ_DEALER);
@@ -199,6 +267,11 @@ int main (void)
199267
expect_bounce_fail (server, client);
200268
close_zero_linger (client);
201269

270+
#ifdef ZMQ_BUILD_DRAFT_API
271+
event = get_monitor_event (server_mon, NULL, NULL);
272+
assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
273+
#endif
274+
202275
// Check CURVE security with bogus client credentials
203276
// This must be caught by the ZAP handler
204277
char bogus_public [41];
@@ -218,6 +291,11 @@ int main (void)
218291
expect_bounce_fail (server, client);
219292
close_zero_linger (client);
220293

294+
#ifdef ZMQ_BUILD_DRAFT_API
295+
event = get_monitor_event (server_mon, NULL, NULL);
296+
assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
297+
#endif
298+
221299
// Check CURVE security with NULL client credentials
222300
// This must be caught by the curve_server class, not passed to ZAP
223301
client = zmq_socket (ctx, ZMQ_DEALER);
@@ -227,6 +305,11 @@ int main (void)
227305
expect_bounce_fail (server, client);
228306
close_zero_linger (client);
229307

308+
#ifdef ZMQ_BUILD_DRAFT_API
309+
event = get_monitor_event (server_mon, NULL, NULL);
310+
assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
311+
#endif
312+
230313
// Check CURVE security with PLAIN client credentials
231314
// This must be caught by the curve_server class, not passed to ZAP
232315
client = zmq_socket (ctx, ZMQ_DEALER);
@@ -282,6 +365,9 @@ int main (void)
282365
assert (rc == 0);
283366

284367
// Shutdown
368+
#ifdef ZMQ_BUILD_DRAFT_API
369+
close_zero_linger (server_mon);
370+
#endif
285371
rc = zmq_close (server);
286372
assert (rc == 0);
287373
rc = zmq_ctx_term (ctx);

0 commit comments

Comments
 (0)