Debian Codegraph @

c/tests/proactor_test.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "../src/proactor/proactor-internal.h"
21 #include "./pn_test_proactor.hpp"
22 #include "./test_config.h"
23
24 #include <proton/condition.h>
25 #include <proton/connection.h>
26 #include <proton/delivery.h>
27 #include <proton/event.h>
28 #include <proton/link.h>
29 #include <proton/listener.h>
30 #include <proton/netaddr.h>
31 #include <proton/proactor.h>
32 #include <proton/session.h>
33 #include <proton/ssl.h>
34 #include <proton/transport.h>
35
36 #include <string.h>
37
38 #include <iostream>
39
40 using namespace pn_test;
41 using Catch::Matchers::Contains;
42 using Catch::Matchers::Equals;
43
44 /* Test that interrupt and timeout events cause pn_proactor_wait() to return. */
45 TEST_CASE("proactor_interrupt_timeout") {
46 proactor p;
47
48 CHECK(pn_proactor_get(p) == NULL); /* idle */
49 pn_proactor_interrupt(p);
50 CHECK(PN_PROACTOR_INTERRUPT == p.wait_next());
51 CHECK(pn_proactor_get(p) == NULL); /* idle */
52
53 /* Set an immediate timeout */
54 pn_proactor_set_timeout(p, 0);
55 CHECK(PN_PROACTOR_TIMEOUT == p.wait_next());
56 CHECK(PN_PROACTOR_INACTIVE == p.wait_next());
57
58 /* Set a (very short) timeout */
59 pn_proactor_set_timeout(p, 1);
60 CHECK(PN_PROACTOR_TIMEOUT == p.wait_next());
61 CHECK(PN_PROACTOR_INACTIVE == p.wait_next());
62
63 /* Set and cancel a timeout, make sure we don't get the timeout event */
64 pn_proactor_set_timeout(p, 10000000);
65 pn_proactor_cancel_timeout(p);
66 CHECK(PN_PROACTOR_INACTIVE == p.wait_next());
67 CHECK(pn_proactor_get(p) == NULL); /* idle */
68 }
69
70 namespace {
71
72 class common_handler : public handler {
73 handler *accept_; // Handler for accepted connections
74
75 public:
76 explicit common_handler(handler *accept = 0) : accept_(accept) {}
77
78 bool handle(pn_event_t *e) override {
79 switch (pn_event_type(e)) {
80 /* Always stop on these noteworthy events */
81 case PN_TRANSPORT_ERROR:
82 case PN_LISTENER_OPEN:
83 case PN_LISTENER_CLOSE:
84 case PN_PROACTOR_INACTIVE:
85 return true;
86
87 case PN_LISTENER_ACCEPT:
88 listener = pn_event_listener(e);
89 connection = pn_connection();
90 if (accept_) pn_connection_set_context(connection, accept_);
91 pn_listener_accept2(listener, connection, NULL);
92 return false;
93
94 // Return remote opens
95 case PN_CONNECTION_REMOTE_OPEN:
96 pn_connection_open(pn_event_connection(e));
97 return false;
98 case PN_SESSION_REMOTE_OPEN:
99 pn_session_open(pn_event_session(e));
100 return false;
101 case PN_LINK_REMOTE_OPEN:
102 pn_link_open(pn_event_link(e));
103 return false;
104
105 // Return remote closes
106 case PN_CONNECTION_REMOTE_CLOSE:
107 pn_connection_close(pn_event_connection(e));
108 return false;
109 case PN_SESSION_REMOTE_CLOSE:
110 pn_session_close(pn_event_session(e));
111 return false;
112 case PN_LINK_REMOTE_CLOSE:
113 pn_link_close(pn_event_link(e));
114 return false;
115
116 default:
117 return false;
118 }
119 }
120 };
121
122 /* close a connection when it is remote open */
123 struct close_on_open_handler : public common_handler {
124 bool handle(pn_event_t *e) override {
125 switch (pn_event_type(e)) {
126 case PN_CONNECTION_REMOTE_OPEN:
127 pn_connection_close(pn_event_connection(e));
128 return false;
129 default:
130 return common_handler::handle(e);
131 }
132 }
133 };
134
135 } // namespace
136
137 /* Test simple client/server connection that opens and closes */
138 TEST_CASE("proactor_connect") {
139 close_on_open_handler h;
140 proactor p(&h);
141 /* Connect and wait for close at both ends */
142 pn_listener_t *l = p.listen(":0", &h);
143 REQUIRE_RUN(p, PN_LISTENER_OPEN);
144 p.connect(l);
145 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
146 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
147 }
148
149 namespace {
150 /* Return on connection open, close and return on wake */
151 struct close_on_wake_handler : public common_handler {
152 bool handle(pn_event_t *e) override {
153 switch (pn_event_type(e)) {
154 case PN_CONNECTION_WAKE:
155 pn_connection_close(pn_event_connection(e));
156 return true;
157 default:
158 return common_handler::handle(e);
159 }
160 }
161 };
162 } // namespace
163
164 // Test waking up a connection that is idle
165 TEST_CASE("proactor_connection_wake") {
166 common_handler h;
167 proactor p(&h);
168 close_on_wake_handler wh;
169 pn_listener_t *l = p.listen();
170 REQUIRE_RUN(p, PN_LISTENER_OPEN);
171 pn_connection_t *c = p.connect(l, &wh);
172 pn_incref(c); /* Keep a reference for wake() after free */
173
174 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
175 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
176 while (p.flush().first != 0);
177 pn_connection_wake(c);
178 REQUIRE_RUN(p, PN_CONNECTION_WAKE);
179 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
180 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED); /* Both ends */
181
182 /* Verify we don't get a wake after close even if they happen together */
183 pn_connection_t *c2 = p.connect(l, &wh);
184 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
185 pn_connection_wake(c2);
186 pn_proactor_disconnect(p, NULL);
187 pn_connection_wake(c2);
188
189 for (pn_event_type_t et = p.run(); et != PN_PROACTOR_INACTIVE; et = p.run()) {
190 switch (et) {
191 case PN_TRANSPORT_ERROR:
192 case PN_TRANSPORT_CLOSED:
193 case PN_LISTENER_CLOSE:
194 break; // expected
195 default:
196 FAIL("Unexpected event type: " << et);
197 }
198 }
199 // The pn_connection_t is still valid so wake is legal but a no-op.
200 // Make sure there's no memory error.
201 pn_connection_wake(c);
202 pn_decref(c);
203 }
204
205 namespace {
206 struct abort_handler : public common_handler {
207 bool handle(pn_event_t *e) {
208 switch (pn_event_type(e)) {
209 case PN_CONNECTION_REMOTE_OPEN:
210 /* Close the transport - abruptly closes the socket */
211 pn_transport_close_tail(pn_connection_transport(pn_event_connection(e)));
212 pn_transport_close_head(pn_connection_transport(pn_event_connection(e)));
213 return false;
214
215 default:
216 return common_handler::handle(e);
217 }
218 }
219 };
220 } // namespace
221
222 /* Verify that pn_transport_close_head/tail aborts a connection without an AMQP
223 * protocol close */
224 TEST_CASE("proactor_abort") {
225 abort_handler sh; // Handle listener and server side of connection
226 proactor p(&sh);
227 pn_listener_t *l = p.listen();
228 REQUIRE_RUN(p, PN_LISTENER_OPEN);
229 common_handler ch; // Handle client side of connection
230 pn_connection_t *c = p.connect(l, &ch);
231 REQUIRE(c != nullptr);
232
233 /* server transport closes */
234 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
235 CHECK_THAT(*sh.last_condition,
236 cond_matches("amqp:connection:framing-error", "abort"));
237
238 /* client transport closes */
239 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
240 CHECK_THAT(*ch.last_condition,
241 cond_matches("amqp:connection:framing-error", "abort"));
242 pn_listener_close(l);
243 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
244 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
245
246 /* Verify expected event sequences, no unexpected events */
247 CHECK_THAT(ETYPES(PN_CONNECTION_INIT, PN_CONNECTION_LOCAL_OPEN,
248 PN_CONNECTION_BOUND, PN_TRANSPORT_TAIL_CLOSED,
249 PN_TRANSPORT_ERROR, PN_TRANSPORT_HEAD_CLOSED,
250 PN_TRANSPORT_CLOSED),
251 Equals(ch.log_clear()));
252 CHECK_THAT(ETYPES(PN_LISTENER_OPEN, PN_LISTENER_ACCEPT, PN_CONNECTION_INIT,
253 PN_CONNECTION_BOUND, PN_CONNECTION_REMOTE_OPEN,
254 PN_TRANSPORT_TAIL_CLOSED, PN_TRANSPORT_ERROR,
255 PN_TRANSPORT_HEAD_CLOSED, PN_TRANSPORT_CLOSED,
256 PN_LISTENER_CLOSE, PN_PROACTOR_INACTIVE),
257 Equals(sh.log_clear()));
258 }
259
260 /* Test that INACTIVE event is generated when last connections/listeners closes.
261 */
262 TEST_CASE("proactor_inactive") {
263 close_on_wake_handler h;
264 proactor p(&h);
265
266 /* Listen, connect, disconnect */
267 pn_listener_t *l = p.listen();
268 REQUIRE_RUN(p, PN_LISTENER_OPEN);
269 pn_connection_t *c = p.connect(l, &h);
270 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
271 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
272 pn_connection_wake(c);
273 REQUIRE_RUN(p, PN_CONNECTION_WAKE);
274 /* Expect TRANSPORT_CLOSED both ends */
275 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
276 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
277 pn_listener_close(l);
278 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
279 /* Immediate timer generates INACTIVE (no connections) */
280 pn_proactor_set_timeout(p, 0);
281 REQUIRE_RUN(p, PN_PROACTOR_TIMEOUT);
282 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
283
284 /* Connect, set-timer, disconnect */
285 l = p.listen();
286 REQUIRE_RUN(p, PN_LISTENER_OPEN);
287 c = p.connect(l, &h);
288 pn_proactor_set_timeout(p, 1000000);
289 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
290 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
291 pn_connection_wake(c);
292 REQUIRE_RUN(p, PN_CONNECTION_WAKE);
293 /* Expect TRANSPORT_CLOSED from client and server */
294 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
295 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
296 pn_listener_close(l);
297 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
298 /* No INACTIVE till timer is cancelled */
299 CHECK(!pn_proactor_get(p)); // idle
300 pn_proactor_cancel_timeout(p);
301 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
302 }
303
304 /* Tests for error handling */
305 TEST_CASE("proactor_errors") {
306 close_on_wake_handler h;
307 proactor p(&h);
308 /* Invalid connect/listen service name */
309 p.connect("127.0.0.1:xxx");
310 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
311 CHECK_THAT(*h.last_condition, cond_matches("proton:io", "xxx"));
312 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
313 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
314
315 pn_listener_t *l = pn_listener();
316 pn_proactor_listen(p, l, "127.0.0.1:xxx", 1);
317 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
318 CHECK_THAT(*h.last_condition, cond_matches("proton:io", "xxx"));
319 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
320
321 /* Invalid connect/listen host name */
322 p.connect("nosuch.example.com:");
323 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
324 CHECK_THAT(*h.last_condition, cond_matches("proton:io", "nosuch"));
325 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
326 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
327
328 pn_proactor_listen(p, pn_listener(), "nosuch.example.com:", 1);
329 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
330 CHECK_THAT(*h.last_condition, cond_matches("proton:io", "nosuch"));
331 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
332
333 /* Listen on a port already in use */
334 l = p.listen();
335 REQUIRE_RUN(p, PN_LISTENER_OPEN);
336 std::string laddr = ":" + listening_port(l);
337 p.listen(laddr);
338 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
339 CHECK_THAT(*h.last_condition, cond_matches("proton:io"));
340
341 pn_listener_close(l);
342 REQUIRE_RUN(p, PN_LISTENER_CLOSE);
343 REQUIRE_RUN(p, PN_PROACTOR_INACTIVE);
344
345 /* Connect with no listener */
346 p.connect(laddr);
347 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
348 CHECK_THAT(*h.last_condition, cond_matches("proton:io", "refused"));
349 }
350
351 namespace {
352 /* Closing the connection during PN_TRANSPORT_ERROR should be a no-op
353 * Regression test for: https://issues.apache.org/jira/browse/PROTON-1586
354 */
355 struct transport_close_connection_handler : public common_handler {
356 bool handle(pn_event_t *e) {
357 switch (pn_event_type(e)) {
358 case PN_TRANSPORT_ERROR:
359 pn_connection_close(pn_event_connection(e));
360 break;
361 default:
362 return common_handler::handle(e);
363 }
364 return PN_EVENT_NONE;
365 }
366 };
367 } // namespace
368
369 /* Closing the connection during PN_TRANSPORT_ERROR due to connection failure
370 * should be a no-op. Regression test for:
371 * https://issues.apache.org/jira/browse/PROTON-1586
372 */
373 TEST_CASE("proactor_proton_1586") {
374 transport_close_connection_handler h;
375 proactor p(&h);
376 p.connect(":yyy");
377 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
378 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
379 CHECK_THAT(*h.last_condition, cond_matches("proton:io", ":yyy"));
380
381 // No events expected after PN_TRANSPORT_CLOSED, proactor is inactive.
382 CHECK(PN_PROACTOR_INACTIVE == p.wait_next());
383 }
384
385 /* Test that we can control listen/select on ipv6/v4 and listen on both by
386 * default */
387 TEST_CASE("proactor_ipv4_ipv6") {
388 close_on_open_handler h;
389 proactor p(&h);
390
391 /* Listen on all interfaces for IPv4 only. */
392 pn_listener_t *l4 = p.listen("0.0.0.0:0");
393 REQUIRE_RUN(p, PN_LISTENER_OPEN);
394 /* Empty address listens on both IPv4 and IPv6 on all interfaces */
395 pn_listener_t *l = p.listen();
396 REQUIRE_RUN(p, PN_LISTENER_OPEN);
397
398 #define EXPECT_CONNECT(LISTENER, HOST) \
399 do { \
400 p.connect(std::string(HOST) + ":" + listening_port(LISTENER)); \
401 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED); \
402 CHECK_THAT(*h.last_condition, cond_empty()); \
403 } while (0)
404
405 EXPECT_CONNECT(l4, "127.0.0.1"); /* v4->v4 */
406 EXPECT_CONNECT(l4, ""); /* local->v4*/
407
408 EXPECT_CONNECT(l, "127.0.0.1"); /* v4->all */
409 EXPECT_CONNECT(l, ""); /* local->all */
410
411 /* Listen on ipv6 loopback, if it fails skip ipv6 tests.
412
413 NOTE: Don't use the unspecified address "::" here - ipv6-disabled platforms
414 may allow listening on "::" without complaining. However they won't have a
415 local ipv6 loopback configured, so "::1" will force an error.
416 */
417 pn_listener_t *l6 = pn_listener();
418 pn_proactor_listen(p, l6, "::1:0", 4);
419 pn_event_type_t e = p.run();
420 if (e == PN_LISTENER_OPEN && !pn_condition_is_set(h.last_condition)) {
421 EXPECT_CONNECT(l6, "::1"); /* v6->v6 */
422 EXPECT_CONNECT(l6, ""); /* local->v6 */
423 EXPECT_CONNECT(l, "::1"); /* v6->all */
424 pn_listener_close(l6);
425 } else {
426 WARN("skip IPv6 tests: %s %s" << e << *h.last_condition);
427 }
428
429 pn_listener_close(l);
430 pn_listener_close(l4);
431 }
432
433 /* Make sure we clean up released connections and open sockets
434 * correctly */
435 TEST_CASE("proactor_release_free") {
436 common_handler h;
437 proactor p(&h);
438
439 pn_listener_t *l = p.listen();
440 REQUIRE_RUN(p, PN_LISTENER_OPEN);
441 /* leave one connection to the proactor */
442 pn_connection_t *c = p.connect(l);
443 REQUIRE(c != nullptr);
444 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
445 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
446
447 {
448 /* release c1 and free immediately */
449 auto_free<pn_connection_t, pn_connection_free> c1(p.connect(l));
450 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
451 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
452 pn_proactor_release_connection(c1);
453 }
454 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
455 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
456
457 /* release c2 and but don't free till after proactor free */
458 auto_free<pn_connection_t, pn_connection_free> c2(p.connect(l));
459 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
460 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
461 pn_proactor_release_connection(c2);
462 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
463 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
464
465 // OK to free a listener/connection that was never used by a
466 // proactor.
467 pn_listener_free(pn_listener());
468 pn_connection_free(pn_connection());
469 }
470
471 #define SSL_FILE(NAME) CMAKE_CURRENT_SOURCE_DIR "/ssl-certs/" NAME
472 #define SSL_PW "tserverpw"
473 /* Windows vs. OpenSSL certificates */
474 #if defined(_WIN32)
475 #define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.p12")
476 #define SET_CREDENTIALS(DOMAIN, NAME) \
477 pn_ssl_domain_set_credentials(DOMAIN, SSL_FILE(NAME "-full.p12"), "", SSL_PW)
478 #else
479 #define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.pem")
480 #define SET_CREDENTIALS(DOMAIN, NAME) \
481 pn_ssl_domain_set_credentials(DOMAIN, CERTIFICATE(NAME), \
482 SSL_FILE(NAME "-private-key.pem"), SSL_PW)
483 #endif
484
485 namespace {
486
487 struct ssl_handler : public common_handler {
488 auto_free<pn_ssl_domain_t, pn_ssl_domain_free> ssl_domain;
489
490 ssl_handler(pn_ssl_domain_t *d) : ssl_domain(d) {}
491
492 bool handle(pn_event_t *e) {
493 switch (pn_event_type(e)) {
494
495 case PN_CONNECTION_BOUND:
496 CHECK(0 == pn_ssl_init(pn_ssl(pn_event_transport(e)), ssl_domain, NULL));
497 return false;
498
499 case PN_CONNECTION_REMOTE_OPEN: {
500 pn_ssl_t *ssl = pn_ssl(pn_event_transport(e));
501 CHECK(ssl);
502 char protocol[256];
503 CHECK(pn_ssl_get_protocol_name(ssl, protocol, sizeof(protocol)));
504 CHECK_THAT(protocol, Contains("TLS"));
505 pn_connection_t *c = pn_event_connection(e);
506 if (pn_connection_state(c) & PN_LOCAL_ACTIVE) {
507 pn_connection_close(c); // Client closes on completion.
508 } else {
509 pn_connection_open(c); // Server returns the OPEN
510 }
511 return true;
512 }
513 default:
514 return common_handler::handle(e);
515 }
516 }
517 };
518
519 } // namespace
520
521 /* Test various SSL connections between proactors*/
522 TEST_CASE("proactor_ssl") {
523 if (!pn_ssl_present()) {
524 WARN("Skip SSL tests, not available");
525 return;
526 }
527
528 ssl_handler client(pn_ssl_domain(PN_SSL_MODE_CLIENT));
529 ssl_handler server(pn_ssl_domain(PN_SSL_MODE_SERVER));
530 CHECK(0 == SET_CREDENTIALS(server.ssl_domain, "tserver"));
531 proactor p;
532 common_handler listener(&server); // Use server for accepted connections
533 pn_listener_t *l = p.listen(":0", &listener);
534 REQUIRE_RUN(p, PN_LISTENER_OPEN);
535
536 /* Not Anonymous by default */
537 p.connect(l, &client);
538 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
539 CHECK_THAT(*client.last_condition,
540 cond_matches("amqp:connection:framing-error", "SSL"));
541 int errs = 1;
542 int closes = 0;
543 while (errs < 2 && closes < 2) {
544 pn_event_type_t et = p.run(PN_TRANSPORT_CLOSED);
545 switch(et) {
546 case PN_TRANSPORT_ERROR: errs++; break;
547 case PN_TRANSPORT_CLOSED: closes++; break;
548 default:
549 FAIL( "bad stop event " << pn_event_type_name(et)) ;
550 break;
551 }
552 }
553
554 /* Deliberate use of Anonymous */
555 pn_ssl_domain_t *cd = client.ssl_domain;
556 REQUIRE(0 == pn_ssl_domain_set_peer_authentication(
557 cd, PN_SSL_ANONYMOUS_PEER, NULL));
558 pn_connection_t *c = pn_connection();
559 p.connect(l, &client, c);
560 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
561 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
562 CHECK_THAT(*server.last_condition, cond_empty());
563 CHECK_THAT(*client.last_condition, cond_empty());
564 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
565 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
566
567 /* Verify peer with good hostname */
568 REQUIRE(0 == pn_ssl_domain_set_trusted_ca_db(cd, CERTIFICATE("tserver")));
569 REQUIRE(0 == pn_ssl_domain_set_peer_authentication(
570 cd, PN_SSL_VERIFY_PEER_NAME, NULL));
571 c = pn_connection();
572 pn_connection_set_hostname(c, "test_server");
573 p.connect(l, &client, c);
574 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
575 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
576 CHECK_THAT(*server.last_condition, cond_empty());
577 CHECK_THAT(*client.last_condition, cond_empty());
578 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
579 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
580
581 /* Verify peer with bad hostname */
582 c = pn_connection();
583 pn_connection_set_hostname(c, "wrongname");
584 p.connect(l, &client, c);
585 REQUIRE_RUN(p, PN_TRANSPORT_ERROR);
586 CHECK_THAT(*client.last_condition,
587 cond_matches("amqp:connection:framing-error", "SSL"));
588 errs = 1;
589 closes = 0;
590 while (errs < 2 && closes < 2) {
591 pn_event_type_t et = p.run(PN_TRANSPORT_CLOSED);
592 switch(et) {
593 case PN_TRANSPORT_ERROR: errs++; break;
594 case PN_TRANSPORT_CLOSED: closes++; break;
595 default:
596 FAIL( "bad stop event " << pn_event_type_name(et)) ;
597 break;
598 }
599 }
600
601 /* Can ignore bad hostname */
602 REQUIRE(0 == pn_ssl_domain_set_peer_authentication(
603 cd, PN_SSL_VERIFY_PEER, NULL));
604 c = pn_connection();
605 pn_connection_set_hostname(c, "wrongname");
606 p.connect(l, &client, c);
607 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
608 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
609 CHECK_THAT(*server.last_condition, cond_empty());
610 CHECK_THAT(*client.last_condition, cond_empty());
611 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
612 REQUIRE_RUN(p, PN_TRANSPORT_CLOSED);
613 }
614
615 TEST_CASE("proactor_addr") {
616 /* Test the address formatter */
617 CHECK(1 == pn_proactor_addr(NULL, 0, "", ""));
618 CHECK(7 == pn_proactor_addr(NULL, 0, "foo", "bar"));
619
620 char addr[PN_MAX_ADDR];
621 pn_proactor_addr(addr, sizeof(addr), "foo", "bar");
622 CHECK_THAT("foo:bar", Equals(addr));
623 pn_proactor_addr(addr, sizeof(addr), "foo", "");
624 CHECK_THAT("foo:", Equals(addr));
625 pn_proactor_addr(addr, sizeof(addr), "foo", NULL);
626 CHECK_THAT("foo:", Equals(addr));
627 pn_proactor_addr(addr, sizeof(addr), "", "bar");
628 CHECK_THAT(":bar", Equals(addr));
629 pn_proactor_addr(addr, sizeof(addr), NULL, "bar");
630 CHECK_THAT(":bar", Equals(addr));
631 pn_proactor_addr(addr, sizeof(addr), "1:2:3:4", "5");
632 CHECK_THAT("1:2:3:4:5", Equals(addr));
633 pn_proactor_addr(addr, sizeof(addr), "1:2:3:4", "");
634 CHECK_THAT("1:2:3:4:", Equals(addr));
635 pn_proactor_addr(addr, sizeof(addr), "1:2:3:4", NULL);
636 CHECK_THAT("1:2:3:4:", Equals(addr));
637 }
638
639 /* Test pn_proactor_addr functions */
640
641 TEST_CASE("proactor_netaddr") {
642 common_handler h;
643 proactor p(&h);
644 /* Use IPv4 to get consistent results all platforms */
645 pn_listener_t *l = p.listen("127.0.0.1:0");
646 REQUIRE_RUN(p, PN_LISTENER_OPEN);
647 pn_connection_t *c = p.connect(l);
648 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
649 REQUIRE_RUN(p, PN_CONNECTION_REMOTE_OPEN);
650
651 // client remote, client local, server remote and server
652 // local address strings
653 char cr[1024], cl[1024], sr[1024], sl[1024];
654
655 pn_transport_t *ct = pn_connection_transport(c);
656 const pn_netaddr_t *na = pn_transport_remote_addr(ct);
657 pn_netaddr_str(na, cr, sizeof(cr));
658 CHECK_THAT(cr, Contains(listening_port(l)));
659
660 pn_connection_t *s = h.connection; /* server side of the connection */
661
662 pn_transport_t *st = pn_connection_transport(s);
663 pn_netaddr_str(pn_transport_local_addr(st), sl, sizeof(sl));
664 CHECK_THAT(cr, Equals(sl)); /* client remote == server local */
665
666 pn_netaddr_str(pn_transport_local_addr(ct), cl, sizeof(cl));
667 pn_netaddr_str(pn_transport_remote_addr(st), sr, sizeof(sr));
668 CHECK_THAT(cl, Equals(sr)); /* client local == server remote */
669
670 char host[PN_MAX_ADDR] = "";
671 char serv[PN_MAX_ADDR] = "";
672 CHECK(0 == pn_netaddr_host_port(na, host, sizeof(host), serv, sizeof(serv)));
673 CHECK_THAT("127.0.0.1", Equals(host));
674 CHECK(listening_port(l) == serv);
675
676 /* Make sure you can use NULL, 0 to get length of address
677 * string without a crash */
678 size_t len = pn_netaddr_str(pn_transport_local_addr(ct), NULL, 0);
679 CHECK(strlen(cl) == len);
680 }
681
682 TEST_CASE("proactor_parse_addr") {
683 char buf[1024];
684 const char *host, *port;
685
686 CHECK(0 == pni_parse_addr("foo:bar", buf, sizeof(buf), &host, &port));
687 CHECK_THAT("foo", Equals(host));
688 CHECK_THAT("bar", Equals(port));
689
690 CHECK(0 == pni_parse_addr("foo:", buf, sizeof(buf), &host, &port));
691 CHECK_THAT("foo", Equals(host));
692 CHECK_THAT("5672", Equals(port));
693
694 CHECK(0 == pni_parse_addr(":bar", buf, sizeof(buf), &host, &port));
695 CHECK(NULL == host);
696 CHECK_THAT("bar", Equals(port));
697
698 CHECK(0 == pni_parse_addr(":", buf, sizeof(buf), &host, &port));
699 CHECK(NULL == host);
700 CHECK_THAT("5672", Equals(port));
701
702 CHECK(0 == pni_parse_addr(":amqps", buf, sizeof(buf), &host, &port));
703 CHECK_THAT("5671", Equals(port));
704
705 CHECK(0 == pni_parse_addr(":amqp", buf, sizeof(buf), &host, &port));
706 CHECK_THAT("5672", Equals(port));
707
708 CHECK(0 == pni_parse_addr("::1:2:3", buf, sizeof(buf), &host, &port));
709 CHECK_THAT("::1:2", Equals(host));
710 CHECK_THAT("3", Equals(port));
711
712 CHECK(0 == pni_parse_addr(":::", buf, sizeof(buf), &host, &port));
713 CHECK_THAT("::", Equals(host));
714 CHECK_THAT("5672", Equals(port));
715
716 CHECK(0 == pni_parse_addr("", buf, sizeof(buf), &host, &port));
717 CHECK(NULL == host);
718 CHECK_THAT("5672", Equals(port));
719 }
720
721 /* Test pn_proactor_disconnect */
722 TEST_CASE("proactor_disconnect") {
723 common_handler ch, sh;
724 proactor client(&ch), server(&sh);
725
726 // Start two listeners on the server
727 pn_listener_t *l = server.listen();
728 REQUIRE_RUN(server, PN_LISTENER_OPEN);
729 pn_listener_t *l2 = server.listen();
730 REQUIRE(l2 != nullptr);
731 REQUIRE_RUN(server, PN_LISTENER_OPEN);
732
733 // Two connections from client
734 pn_connection_t *c = client.connect(l);
735 REQUIRE(c != nullptr);
736 CHECK_CORUN(client, server, PN_CONNECTION_REMOTE_OPEN);
737 pn_connection_t *c2 = client.connect(l);
738 REQUIRE(c2 != nullptr);
739 CHECK_CORUN(client, server, PN_CONNECTION_REMOTE_OPEN);
740
741 /* Disconnect the client proactor */
742 auto_free<pn_condition_t, pn_condition_free> cond(pn_condition());
743 pn_condition_format(cond, "test-name", "test-description");
744 pn_proactor_disconnect(client, cond);
745
746 /* Verify expected client side first */
747 CHECK_CORUN(client, server, PN_TRANSPORT_ERROR);
748 CHECK_THAT(*client.handler->last_condition,
749 cond_matches("test-name", "test-description"));
750 CHECK_CORUN(client, server, PN_TRANSPORT_ERROR);
751 CHECK_THAT(*client.handler->last_condition,
752 cond_matches("test-name", "test-description"));
753 REQUIRE_RUN(client, PN_PROACTOR_INACTIVE);
754
755 /* Now check server sees the disconnects */
756 CHECK_CORUN(server, client, PN_TRANSPORT_ERROR);
757 CHECK_THAT(*server.handler->last_condition,
758 cond_matches("amqp:connection:framing-error", "aborted"));
759 CHECK_CORUN(server, client, PN_TRANSPORT_ERROR);
760 CHECK_THAT(*server.handler->last_condition,
761 cond_matches("amqp:connection:framing-error", "aborted"));
762
763 /* Now disconnect the server end (the listeners) */
764 pn_proactor_disconnect(server, NULL);
765 REQUIRE_RUN(server, PN_LISTENER_CLOSE);
766 REQUIRE_RUN(server, PN_LISTENER_CLOSE);
767 REQUIRE_RUN(server, PN_PROACTOR_INACTIVE);
768
769 /* Make sure the proactors are still functional */
770 pn_listener_t *l3 = server.listen();
771 REQUIRE_RUN(server, PN_LISTENER_OPEN);
772 client.connect(l3);
773 CHECK_CORUN(client, server, PN_CONNECTION_REMOTE_OPEN);
774 }
775
776 namespace {
777 const size_t FRAME = 512; /* Smallest legal frame */
778 const ssize_t CHUNK = (FRAME + FRAME / 2); /* Chunk overflows frame */
779 const size_t BODY = (CHUNK * 3 + CHUNK / 2); /* Body doesn't fit into chunks */
780 } // namespace
781
782 struct message_stream_handler : public common_handler {
783 pn_link_t *sender;
784 pn_delivery_t *dlv;
785 pn_rwbytes_t send_buf, recv_buf;
786 ssize_t size, sent, received;
787 bool complete;
788
789 message_stream_handler()
790 : sender(), dlv(), send_buf(), recv_buf(), size(), sent(), received(),
791 complete() {}
792
793 bool handle(pn_event_t *e) {
794 switch (pn_event_type(e)) {
795 case PN_CONNECTION_BOUND:
796 pn_transport_set_max_frame(pn_event_transport(e), FRAME);
797 return false;
798
799 case PN_SESSION_INIT:
800 pn_session_set_incoming_capacity(pn_event_session(e),
801 FRAME); /* Single frame incoming */
802 pn_session_set_outgoing_window(pn_event_session(e),
803 1); /* Single frame outgoing */
804 return false;
805
806 case PN_LINK_REMOTE_OPEN:
807 common_handler::handle(e);
808 if (pn_link_is_receiver(pn_event_link(e))) {
809 pn_link_flow(pn_event_link(e), 1);
810 } else {
811 sender = pn_event_link(e);
812 }
813 return false;
814
815 case PN_LINK_FLOW: /* Start a delivery */
816 if (pn_link_is_sender(pn_event_link(e)) && !dlv) {
817 dlv = pn_delivery(pn_event_link(e), pn_dtag("x", 1));
818 }
819 return false;
820
821 case PN_CONNECTION_WAKE: { /* Send a chunk */
822 ssize_t remains = size - sent;
823 ssize_t n = (CHUNK < remains) ? CHUNK : remains;
824 CHECK(n == pn_link_send(sender, send_buf.start + sent, n));
825 sent += n;
826 if (sent == size) {
827 CHECK(pn_link_advance(sender));
828 }
829 return false;
830 }
831
832 case PN_DELIVERY: { /* Receive a delivery - smaller than a
833 chunk? */
834 pn_delivery_t *dlv = pn_event_delivery(e);
835 if (pn_delivery_readable(dlv)) {
836 ssize_t n = pn_delivery_pending(dlv);
837 rwbytes_ensure(&recv_buf, received + n);
838 REQUIRE(n ==
839 pn_link_recv(pn_event_link(e), recv_buf.start + received, n));
840 received += n;
841 }
842 complete = !pn_delivery_partial(dlv);
843 return true;
844 }
845 default:
846 return common_handler::handle(e);
847 }
848 }
849 };
850
851 /* Test sending/receiving a message in chunks */
852 TEST_CASE("proactor_message_stream") {
853 message_stream_handler h;
854 proactor p(&h);
855
856 pn_listener_t *l = p.listen();
857 REQUIRE_RUN(p, PN_LISTENER_OPEN);
858
859 /* Encode a large (not very) message to send in chunks */
860 auto_free<pn_message_t, pn_message_free> m(pn_message());
861 pn_data_put_binary(pn_message_body(m), pn_bytes(std::string(BODY, 'x')));
862 h.size = pn_message_encode2(m, &h.send_buf);
863
864 pn_connection_t *c = p.connect(l);
865 pn_session_t *ssn = pn_session(c);
866 pn_session_open(ssn);
867 pn_link_t *snd = pn_sender(ssn, "x");
868 pn_link_open(snd);
869 REQUIRE_RUN(p, PN_LINK_FLOW);
870
871 /* Send and receive the message in chunks */
872 do {
873 pn_connection_wake(c); /* Initiate send/receive of one chunk */
874 do { /* May be multiple receives for one send */
875 REQUIRE_RUN(p, PN_DELIVERY);
876 } while (h.received < h.sent);
877 } while (!h.complete);
878 CHECK(h.received == h.size);
879 CHECK(h.sent == h.size);
880 CHECK(!memcmp(h.send_buf.start, h.recv_buf.start, h.size));
881
882 free(h.send_buf.start);
883 free(h.recv_buf.start);
884 }