| 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 "./pn_test.hpp" |
| 21 |
|
| 22 |
#include <proton/codec.h> |
| 23 |
#include <proton/connection.h> |
| 24 |
#include <proton/connection_driver.h> |
| 25 |
#include <proton/delivery.h> |
| 26 |
#include <proton/link.h> |
| 27 |
#include <proton/message.h> |
| 28 |
#include <proton/session.h> |
| 29 |
#include <proton/transport.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
using Catch::Matchers::EndsWith; |
| 34 |
using Catch::Matchers::Equals; |
| 35 |
using namespace pn_test; |
| 36 |
|
| 37 |
namespace { |
| 38 |
|
| 39 |
/* Handler that replies to REMOTE_OPEN, stores the opened object on the handler |
| 40 |
*/ |
| 41 |
struct open_handler : pn_test::handler { |
| 42 |
bool handle(pn_event_t *e) override { |
| 43 |
switch (pn_event_type(e)) { |
| 44 |
case PN_CONNECTION_REMOTE_OPEN: |
| 45 |
connection = pn_event_connection(e); |
| 46 |
pn_connection_open(connection); |
| 47 |
break; |
| 48 |
case PN_SESSION_REMOTE_OPEN: |
| 49 |
session = pn_event_session(e); |
| 50 |
pn_session_open(session); |
| 51 |
break; |
| 52 |
case PN_LINK_REMOTE_OPEN: |
| 53 |
link = pn_event_link(e); |
| 54 |
pn_link_open(link); |
| 55 |
break; |
| 56 |
default: |
| 57 |
break; |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
/* Like open_handler but also reply to REMOTE_CLOSE */ |
| 64 |
struct open_close_handler : public open_handler { |
| 65 |
bool handle(pn_event_t *e) override { |
| 66 |
switch (pn_event_type(e)) { |
| 67 |
case PN_CONNECTION_REMOTE_CLOSE: |
| 68 |
pn_connection_open(pn_event_connection(e)); |
| 69 |
break; |
| 70 |
case PN_SESSION_REMOTE_CLOSE: |
| 71 |
pn_session_open(pn_event_session(e)); |
| 72 |
break; |
| 73 |
case PN_LINK_REMOTE_CLOSE: |
| 74 |
pn_link_close(pn_event_link(e)); |
| 75 |
break; |
| 76 |
default: |
| 77 |
return open_handler::handle(e); |
| 78 |
} |
| 79 |
return false; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
/* open_handler that returns control on PN_DELIVERY and stores the delivery */ |
| 84 |
struct delivery_handler : public open_handler { |
| 85 |
bool handle(pn_event_t *e) { |
| 86 |
switch (pn_event_type(e)) { |
| 87 |
case PN_DELIVERY: { |
| 88 |
delivery = pn_event_delivery(e); |
| 89 |
return true; |
| 90 |
} |
| 91 |
default: |
| 92 |
return open_handler::handle(e); |
| 93 |
} |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
} // namespace |
| 98 |
|
| 99 |
/* Blow-by-blow event verification of a single message transfer */ |
| 100 |
TEST_CASE("driver_message_transfer") { |
| 101 |
open_handler client; |
| 102 |
delivery_handler server; |
| 103 |
pn_test::driver_pair d(client, server); |
| 104 |
|
| 105 |
pn_connection_open(d.client.connection); |
| 106 |
pn_session_t *ssn = pn_session(d.client.connection); |
| 107 |
pn_session_open(ssn); |
| 108 |
pn_link_t *snd = pn_sender(ssn, "x"); |
| 109 |
pn_link_open(snd); |
| 110 |
d.run(); |
| 111 |
|
| 112 |
CHECK_THAT(ETYPES(PN_CONNECTION_INIT, PN_CONNECTION_LOCAL_OPEN, |
| 113 |
PN_SESSION_INIT, PN_SESSION_LOCAL_OPEN, PN_LINK_INIT, |
| 114 |
PN_LINK_LOCAL_OPEN, PN_CONNECTION_BOUND, |
| 115 |
PN_CONNECTION_REMOTE_OPEN, PN_SESSION_REMOTE_OPEN, |
| 116 |
PN_LINK_REMOTE_OPEN), |
| 117 |
Equals(client.log_clear())); |
| 118 |
|
| 119 |
CHECK_THAT(ETYPES(PN_CONNECTION_INIT, PN_CONNECTION_BOUND, |
| 120 |
PN_CONNECTION_REMOTE_OPEN, PN_SESSION_INIT, |
| 121 |
PN_SESSION_REMOTE_OPEN, PN_LINK_INIT, PN_LINK_REMOTE_OPEN, |
| 122 |
PN_CONNECTION_LOCAL_OPEN, PN_TRANSPORT, |
| 123 |
PN_SESSION_LOCAL_OPEN, PN_TRANSPORT, PN_LINK_LOCAL_OPEN, |
| 124 |
PN_TRANSPORT), |
| 125 |
Equals(server.log_clear())); |
| 126 |
|
| 127 |
pn_link_t *rcv = server.link; |
| 128 |
REQUIRE(rcv); |
| 129 |
REQUIRE(pn_link_is_receiver(rcv)); |
| 130 |
pn_link_flow(rcv, 1); |
| 131 |
d.run(); |
| 132 |
CHECK_THAT(ETYPES(PN_LINK_FLOW), Equals(client.log_clear())); |
| 133 |
|
| 134 |
/* Encode and send a message */ |
| 135 |
auto_free<pn_message_t, pn_message_free> m(pn_message()); |
| 136 |
pn_data_put_string(pn_message_body(m), |
| 137 |
pn_bytes("abc")); /* Include trailing NULL */ |
| 138 |
pn_delivery(snd, pn_bytes("x")); |
| 139 |
pn_message_send(m, snd, NULL); |
| 140 |
|
| 141 |
d.run(); |
| 142 |
CHECK_THAT(ETYPES(PN_TRANSPORT, PN_DELIVERY), Equals(server.log_clear())); |
| 143 |
|
| 144 |
/* Receive and decode the message */ |
| 145 |
pn_delivery_t *dlv = server.delivery; |
| 146 |
REQUIRE(dlv); |
| 147 |
auto_free<pn_message_t, pn_message_free> m2(pn_message()); |
| 148 |
pn_rwbytes_t buf2 = {0}; |
| 149 |
message_decode(m2, dlv, &buf2); |
| 150 |
pn_data_t *body = pn_message_body(m2); |
| 151 |
pn_data_rewind(body); |
| 152 |
CHECK(pn_data_next(body)); |
| 153 |
CHECK(PN_STRING == pn_data_type(body)); |
| 154 |
CHECK(3 == pn_data_get_string(pn_message_body(m2)).size); |
| 155 |
CHECK_THAT("abc", Equals(pn_data_get_string(pn_message_body(m2)).start)); |
| 156 |
|
| 157 |
free(buf2.start); |
| 158 |
} |
| 159 |
|
| 160 |
namespace { |
| 161 |
/* Handler that opens a connection and sender link */ |
| 162 |
struct send_client_handler : public pn_test::handler { |
| 163 |
bool handle(pn_event_t *e) { |
| 164 |
switch (pn_event_type(e)) { |
| 165 |
case PN_CONNECTION_LOCAL_OPEN: { |
| 166 |
pn_connection_open(pn_event_connection(e)); |
| 167 |
pn_session_t *ssn = pn_session(pn_event_connection(e)); |
| 168 |
pn_session_open(ssn); |
| 169 |
pn_link_t *snd = pn_sender(ssn, "x"); |
| 170 |
pn_link_open(snd); |
| 171 |
break; |
| 172 |
} |
| 173 |
case PN_LINK_REMOTE_OPEN: { |
| 174 |
link = pn_event_link(e); |
| 175 |
return true; |
| 176 |
} |
| 177 |
default: |
| 178 |
break; |
| 179 |
} |
| 180 |
return false; |
| 181 |
} |
| 182 |
}; |
| 183 |
} // namespace |
| 184 |
|
| 185 |
/* Send a message in pieces, ensure each can be received before the next is sent |
| 186 |
*/ |
| 187 |
TEST_CASE("driver_message_stream") { |
| 188 |
send_client_handler client; |
| 189 |
delivery_handler server; |
| 190 |
pn_test::driver_pair d(client, server); |
| 191 |
|
| 192 |
d.run(); |
| 193 |
pn_link_t *rcv = server.link; |
| 194 |
pn_link_t *snd = client.link; |
| 195 |
pn_link_flow(rcv, 1); |
| 196 |
d.run(); |
| 197 |
CHECK(PN_LINK_FLOW == client.log_last()); |
| 198 |
CHECK(PN_TRANSPORT == server.log_last()); |
| 199 |
|
| 200 |
/* Encode a large (not very) message to send in chunks */ |
| 201 |
auto_free<pn_message_t, pn_message_free> m(pn_message()); |
| 202 |
char body[1024] = {0}; |
| 203 |
pn_data_put_binary(pn_message_body(m), pn_bytes(sizeof(body), body)); |
| 204 |
pn_rwbytes_t buf = {0}; |
| 205 |
ssize_t size = pn_message_encode2(m, &buf); |
| 206 |
|
| 207 |
/* Send and receive the message in chunks */ |
| 208 |
static const ssize_t CHUNK = 100; |
| 209 |
pn_delivery(snd, pn_bytes("x")); |
| 210 |
pn_rwbytes_t buf2 = {0}; |
| 211 |
ssize_t received = 0; |
| 212 |
for (ssize_t i = 0; i < size; i += CHUNK) { |
| 213 |
/* Send a chunk */ |
| 214 |
ssize_t c = (i + CHUNK < size) ? CHUNK : size - i; |
| 215 |
CHECK(c == pn_link_send(snd, buf.start + i, c)); |
| 216 |
d.run(); |
| 217 |
CHECK_THAT(ETYPES(PN_DELIVERY), Equals(server.log_clear())); |
| 218 |
/* Receive a chunk */ |
| 219 |
pn_delivery_t *dlv = server.delivery; |
| 220 |
pn_link_t *l = pn_delivery_link(dlv); |
| 221 |
ssize_t dsize = pn_delivery_pending(dlv); |
| 222 |
rwbytes_ensure(&buf2, received + dsize); |
| 223 |
REQUIRE(dsize == pn_link_recv(l, buf2.start + received, dsize)); |
| 224 |
received += dsize; |
| 225 |
} |
| 226 |
CHECK(pn_link_advance(snd)); |
| 227 |
CHECK(received == size); |
| 228 |
CHECK(!memcmp(buf.start, buf2.start, size)); |
| 229 |
|
| 230 |
free(buf.start); |
| 231 |
free(buf2.start); |
| 232 |
} |
| 233 |
|
| 234 |
// Test aborting a delivery |
| 235 |
TEST_CASE("driver_message_abort") { |
| 236 |
send_client_handler client; |
| 237 |
delivery_handler server; |
| 238 |
pn_test::driver_pair d(client, server); |
| 239 |
|
| 240 |
d.run(); |
| 241 |
pn_link_t *rcv = server.link; |
| 242 |
pn_link_t *snd = client.link; |
| 243 |
char data[100] = {0}; /* Dummy data to send. */ |
| 244 |
char rbuf[sizeof(data)] = {0}; /* Read buffer for incoming data. */ |
| 245 |
|
| 246 |
/* Send 2 frames with data */ |
| 247 |
pn_link_flow(rcv, 1); |
| 248 |
CHECK(1 == pn_link_credit(rcv)); |
| 249 |
d.run(); |
| 250 |
CHECK(1 == pn_link_credit(snd)); |
| 251 |
pn_delivery_t *sd = pn_delivery(snd, pn_bytes("1")); /* Sender delivery */ |
| 252 |
for (size_t i = 0; i < 2; ++i) { |
| 253 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 254 |
d.run(); |
| 255 |
CHECK(server.log_last() == PN_DELIVERY); |
| 256 |
pn_delivery_t *rd = server.delivery; |
| 257 |
CHECK(!pn_delivery_aborted(rd)); |
| 258 |
CHECK(pn_delivery_partial(rd)); |
| 259 |
CHECK(1 == pn_link_credit(rcv)); |
| 260 |
CHECK(sizeof(data) == pn_delivery_pending(rd)); |
| 261 |
CHECK(sizeof(rbuf) == |
| 262 |
pn_link_recv(pn_delivery_link(rd), rbuf, sizeof(rbuf))); |
| 263 |
CHECK(0 == pn_link_recv(pn_delivery_link(rd), rbuf, sizeof(rbuf))); |
| 264 |
CHECK(1 == pn_link_credit(rcv)); |
| 265 |
} |
| 266 |
CHECK(1 == pn_link_credit(snd)); |
| 267 |
/* Abort the delivery */ |
| 268 |
pn_delivery_abort(sd); |
| 269 |
CHECK(0 == pn_link_credit(snd)); |
| 270 |
CHECK(pn_link_current(snd) != sd); /* Settled */ |
| 271 |
d.run(); |
| 272 |
CHECK(PN_DELIVERY == server.log_last()); |
| 273 |
CHECK(0 == pn_link_credit(snd)); |
| 274 |
|
| 275 |
/* Receive the aborted=true frame, should be empty */ |
| 276 |
pn_delivery_t *rd = server.delivery; |
| 277 |
CHECK(pn_delivery_aborted(rd)); |
| 278 |
CHECK(!pn_delivery_partial(rd)); /* Aborted deliveries are never partial */ |
| 279 |
CHECK(pn_delivery_settled(rd)); /* Aborted deliveries are remote settled */ |
| 280 |
CHECK(1 == pn_delivery_pending(rd)); |
| 281 |
CHECK(PN_ABORTED == pn_link_recv(pn_delivery_link(rd), rbuf, sizeof(rbuf))); |
| 282 |
pn_delivery_settle(rd); /* Must be settled locally to free it */ |
| 283 |
|
| 284 |
CHECK(0 == pn_link_credit(snd)); |
| 285 |
CHECK(0 == pn_link_credit(rcv)); |
| 286 |
|
| 287 |
/* Abort a delivery before any data has been framed, should be dropped. */ |
| 288 |
pn_link_flow(rcv, 1); |
| 289 |
CHECK(1 == pn_link_credit(rcv)); |
| 290 |
d.run(); |
| 291 |
client.log_clear(); |
| 292 |
server.log_clear(); |
| 293 |
|
| 294 |
sd = pn_delivery(snd, pn_bytes("x")); |
| 295 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 296 |
pn_delivery_abort(sd); |
| 297 |
CHECK(pn_link_current(snd) != sd); /* Settled, possibly freed */ |
| 298 |
CHECK_FALSE(d.run()); |
| 299 |
CHECK_THAT(server.log_clear(), Equals(etypes())); |
| 300 |
/* Client gets transport/flow after abort to ensure other messages are sent */ |
| 301 |
CHECK_THAT(ETYPES(PN_TRANSPORT, PN_LINK_FLOW), Equals(client.log_clear())); |
| 302 |
/* Aborted delivery consumes no credit */ |
| 303 |
CHECK(1 == pn_link_credit(rcv)); |
| 304 |
CHECK(1 == pn_link_credit(snd)); |
| 305 |
CHECK(0 == pn_session_outgoing_bytes(pn_link_session(snd))); |
| 306 |
} |
| 307 |
|
| 308 |
void send_receive_message(const std::string &tag, pn_test::driver_pair &d) { |
| 309 |
pn_link_t *l = d.client.handler.link; |
| 310 |
CHECKED_IF(pn_link_credit(l) > 0) { |
| 311 |
pn_delivery_t *sd = pn_delivery(l, pn_dtag(tag.data(), tag.size())); |
| 312 |
d.server.handler.delivery = NULL; |
| 313 |
CHECK(pn_delivery_current(sd)); |
| 314 |
CHECK(ssize_t(tag.size()) == pn_link_send(l, tag.data(), tag.size())); |
| 315 |
pn_delivery_settle(sd); |
| 316 |
d.run(); |
| 317 |
pn_delivery_t *rd = d.server.handler.delivery; |
| 318 |
d.server.handler.delivery = NULL; |
| 319 |
CHECKED_IF(rd) { |
| 320 |
CHECK(pn_delivery_current(rd)); |
| 321 |
std::string rbuf(tag.size() * 2, 'x'); |
| 322 |
CHECK(ssize_t(tag.size()) == |
| 323 |
pn_link_recv(pn_delivery_link(rd), &rbuf[0], rbuf.size())); |
| 324 |
rbuf.resize(tag.size()); |
| 325 |
CHECK(tag == rbuf); |
| 326 |
} |
| 327 |
pn_delivery_settle(rd); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
#define SEND_RECEIVE_MESSAGE(TAG, DP) \ |
| 332 |
do { \ |
| 333 |
INFO("in send_receive_message: " << TAG); \ |
| 334 |
send_receive_message(TAG, DP); \ |
| 335 |
} while (0) |
| 336 |
|
| 337 |
// Test mixing aborted and good deliveries, make sure credit is correct. |
| 338 |
TEST_CASE("driver_message_abort_mixed") { |
| 339 |
send_client_handler client; |
| 340 |
delivery_handler server; |
| 341 |
pn_test::driver_pair d(client, server); |
| 342 |
|
| 343 |
d.run(); |
| 344 |
pn_link_t *rcv = server.link; |
| 345 |
pn_link_t *snd = client.link; |
| 346 |
char data[100] = {0}; /* Dummy data to send. */ |
| 347 |
char rbuf[sizeof(data)] = {0}; /* Read buffer for incoming data. */ |
| 348 |
|
| 349 |
/* We will send 3 good messages, interleaved with aborted ones */ |
| 350 |
pn_link_flow(rcv, 5); |
| 351 |
d.run(); |
| 352 |
SEND_RECEIVE_MESSAGE("one", d); |
| 353 |
CHECK(4 == pn_link_credit(snd)); |
| 354 |
CHECK(4 == pn_link_credit(rcv)); |
| 355 |
pn_delivery_t *sd, *rd; |
| 356 |
|
| 357 |
/* Send a frame, then an abort */ |
| 358 |
sd = pn_delivery(snd, pn_bytes("x1")); |
| 359 |
server.delivery = NULL; |
| 360 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 361 |
CHECK(4 == pn_link_credit(snd)); /* Nothing sent yet */ |
| 362 |
d.run(); |
| 363 |
rd = server.delivery; |
| 364 |
REQUIRE(rd); |
| 365 |
CHECK(sizeof(rbuf) == pn_link_recv(pn_delivery_link(rd), rbuf, sizeof(rbuf))); |
| 366 |
|
| 367 |
pn_delivery_abort(sd); |
| 368 |
d.run(); |
| 369 |
CHECK(pn_delivery_aborted(rd)); |
| 370 |
pn_delivery_settle(rd); |
| 371 |
/* Abort after sending data consumes credit */ |
| 372 |
CHECK(3 == pn_link_credit(snd)); |
| 373 |
CHECK(3 == pn_link_credit(rcv)); |
| 374 |
|
| 375 |
SEND_RECEIVE_MESSAGE("two", d); |
| 376 |
CHECK(2 == pn_link_credit(snd)); |
| 377 |
CHECK(2 == pn_link_credit(rcv)); |
| 378 |
|
| 379 |
/* Abort a delivery before any data has been framed, should be dropped. */ |
| 380 |
server.log.clear(); |
| 381 |
sd = pn_delivery(snd, pn_bytes("4")); |
| 382 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 383 |
pn_delivery_abort(sd); |
| 384 |
CHECK(pn_link_current(snd) != sd); /* Advanced */ |
| 385 |
d.run(); |
| 386 |
CHECK_THAT(ETYPES(PN_TRANSPORT), Equals(server.log_clear())); |
| 387 |
/* Aborting wit no frames sent should leave credit untouched */ |
| 388 |
CHECK(2 == pn_link_credit(snd)); |
| 389 |
CHECK(2 == pn_link_credit(rcv)); |
| 390 |
|
| 391 |
SEND_RECEIVE_MESSAGE("three", d); |
| 392 |
CHECK(1 == pn_link_credit(rcv)); |
| 393 |
CHECK(1 == pn_link_credit(snd)); |
| 394 |
} |
| 395 |
|
| 396 |
/* Set capacity and max frame, send a single message */ |
| 397 |
static void set_capacity_and_max_frame(size_t capacity, size_t max_frame, |
| 398 |
pn_test::driver_pair &d, |
| 399 |
const char *data) { |
| 400 |
pn_transport_set_max_frame(d.client.transport, max_frame); |
| 401 |
pn_connection_open(d.client.connection); |
| 402 |
pn_session_t *ssn = pn_session(d.client.connection); |
| 403 |
pn_session_set_incoming_capacity(ssn, capacity); |
| 404 |
pn_session_open(ssn); |
| 405 |
pn_link_t *snd = pn_sender(ssn, "x"); |
| 406 |
pn_link_open(snd); |
| 407 |
d.run(); |
| 408 |
pn_link_flow(d.server.handler.link, 1); |
| 409 |
d.run(); |
| 410 |
if (pn_transport_closed(d.client.transport) || |
| 411 |
pn_transport_closed(d.server.transport)) |
| 412 |
return; |
| 413 |
/* Send a message */ |
| 414 |
auto_free<pn_message_t, pn_message_free> m(pn_message()); |
| 415 |
pn_message_set_address(m, data); |
| 416 |
pn_delivery(snd, pn_bytes("x")); |
| 417 |
pn_message_send(m, snd, NULL); |
| 418 |
d.run(); |
| 419 |
} |
| 420 |
|
| 421 |
/* Test different settings for max-frame, outgoing-window, incoming-capacity */ |
| 422 |
TEST_CASE("driver_session_flow_control") { |
| 423 |
open_handler client; |
| 424 |
delivery_handler server; |
| 425 |
pn_test::driver_pair d(client, server); |
| 426 |
|
| 427 |
auto_free<pn_message_t, pn_message_free> m(pn_message()); |
| 428 |
pn_rwbytes_t buf = {0}; |
| 429 |
|
| 430 |
/* Capacity equal to frame size OK */ |
| 431 |
set_capacity_and_max_frame(1234, 1234, d, "foo"); |
| 432 |
pn_delivery_t *dlv = server.delivery; |
| 433 |
CHECKED_IF(dlv) { |
| 434 |
message_decode(m, dlv, &buf); |
| 435 |
CHECK_THAT("foo", Equals(pn_message_get_address(m))); |
| 436 |
} |
| 437 |
|
| 438 |
/* Capacity bigger than frame size OK */ |
| 439 |
set_capacity_and_max_frame(12345, 1234, d, "foo"); |
| 440 |
dlv = server.delivery; |
| 441 |
CHECKED_IF(dlv) { |
| 442 |
message_decode(m, dlv, &buf); |
| 443 |
CHECK_THAT("foo", Equals(pn_message_get_address(m))); |
| 444 |
} |
| 445 |
|
| 446 |
/* Capacity smaller than frame size is an error */ |
| 447 |
set_capacity_and_max_frame(1234, 12345, d, "foo"); |
| 448 |
CHECK_THAT( |
| 449 |
*client.last_condition, |
| 450 |
cond_matches("amqp:internal-error", |
| 451 |
"session capacity 1234 is less than frame size 12345")); |
| 452 |
free(buf.start); |
| 453 |
} |
| 454 |
|
| 455 |
/* Regression test for https://issues.apache.org/jira/browse/PROTON-1832. |
| 456 |
Make sure we error on attempt to re-attach an already-attached link name. |
| 457 |
No crash or memory error. |
| 458 |
*/ |
| 459 |
TEST_CASE("driver_duplicate_link_server") { |
| 460 |
open_close_handler client, server; |
| 461 |
pn_test::driver_pair d(client, server); |
| 462 |
|
| 463 |
pn_connection_open(d.client.connection); |
| 464 |
pn_session_t *ssn = pn_session(d.client.connection); |
| 465 |
pn_session_open(ssn); |
| 466 |
|
| 467 |
/* Set up link "x" */ |
| 468 |
auto_free<pn_link_t, pn_link_free> x(pn_sender(ssn, "xxx")); |
| 469 |
pn_link_open(x); |
| 470 |
d.run(); |
| 471 |
client.log.clear(); |
| 472 |
server.log.clear(); |
| 473 |
/* Free (but don't close) the link and open a new one to generate the invalid |
| 474 |
* double-attach */ |
| 475 |
pn_link_open(pn_sender(ssn, "xxx")); |
| 476 |
d.run(); |
| 477 |
|
| 478 |
CHECK_THAT(*pn_transport_condition(d.server.transport), |
| 479 |
cond_matches("amqp:invalid-field", "xxx")); |
| 480 |
CHECK_THAT(*pn_connection_remote_condition(d.client.connection), |
| 481 |
cond_matches("amqp:invalid-field", "xxx")); |
| 482 |
|
| 483 |
/* Freeing the link at this point is allowed but caused a crash in |
| 484 |
* transport_unbind with the bug */ |
| 485 |
} |
| 486 |
|
| 487 |
/* Reproducer test for https://issues.apache.org/jira/browse/PROTON-1832. |
| 488 |
Make sure the client does not generate an illegal "attach; attach; detach" |
| 489 |
sequence from a legal "pn_link_open(); pn_link_close(); pn_link_open()" |
| 490 |
sequence |
| 491 |
|
| 492 |
This test is expected to fail till PROTON-1832 is fully fixed |
| 493 |
*/ |
| 494 |
TEST_CASE("driver_duplicate_link_client", "[!hide][!shouldfail]") { |
| 495 |
/* Set up the initial link */ |
| 496 |
open_close_handler client, server; |
| 497 |
pn_test::driver_pair d(client, server); |
| 498 |
|
| 499 |
pn_session_t *ssn = pn_session(d.client.connection); |
| 500 |
pn_session_open(ssn); |
| 501 |
pn_link_t *x = pn_sender(ssn, "x"); |
| 502 |
pn_link_open(x); |
| 503 |
d.run(); |
| 504 |
client.log.clear(); |
| 505 |
server.log.clear(); |
| 506 |
|
| 507 |
/* Close the link and open a new link with same name in the same batch of |
| 508 |
* events. */ |
| 509 |
pn_link_close(x); |
| 510 |
pn_link_open(pn_sender(ssn, "x")); |
| 511 |
d.run(); |
| 512 |
|
| 513 |
CHECK_THAT(ETYPES(PN_LINK_REMOTE_CLOSE, PN_LINK_LOCAL_CLOSE, PN_TRANSPORT, |
| 514 |
PN_LINK_INIT, PN_LINK_REMOTE_OPEN, PN_LINK_LOCAL_OPEN, |
| 515 |
PN_TRANSPORT), |
| 516 |
Equals(server.log_clear())); |
| 517 |
CHECK_THAT(*pn_transport_condition(d.server.transport), cond_empty()); |
| 518 |
|
| 519 |
d.run(); |
| 520 |
CHECK_THAT(ETYPES(PN_LINK_LOCAL_CLOSE, PN_TRANSPORT, PN_LINK_REMOTE_CLOSE, |
| 521 |
PN_LINK_INIT, PN_LINK_LOCAL_OPEN, PN_TRANSPORT, |
| 522 |
PN_LINK_REMOTE_OPEN), |
| 523 |
Equals(client.log_clear())); |
| 524 |
CHECK_THAT(*pn_connection_remote_condition(d.client.connection), |
| 525 |
cond_empty()); |
| 526 |
} |
| 527 |
|
| 528 |
/* Settling an incomplete delivery should not cause an error. |
| 529 |
*/ |
| 530 |
TEST_CASE("driver_settle_incomplete_receiver") { |
| 531 |
send_client_handler client; |
| 532 |
delivery_handler server; |
| 533 |
pn_test::driver_pair d(client, server); |
| 534 |
|
| 535 |
d.run(); |
| 536 |
pn_link_t *rcv = server.link; |
| 537 |
pn_link_t *snd = client.link; |
| 538 |
char data[100] = {0}; /* Dummy data to send. */ |
| 539 |
char rbuf[sizeof(data)] = {0}; /* Read buffer for incoming data. */ |
| 540 |
pn_link_flow(rcv, 1); |
| 541 |
pn_delivery(snd, pn_bytes("1")); /* Prepare to send */ |
| 542 |
d.run(); |
| 543 |
|
| 544 |
/* Send/receive a frame */ |
| 545 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 546 |
server.log_clear(); |
| 547 |
d.run(); |
| 548 |
CHECK_THAT(ETYPES(PN_DELIVERY), Equals(server.log_clear())); |
| 549 |
CHECK(sizeof(data) == pn_link_recv(rcv, rbuf, sizeof(data))); |
| 550 |
d.run(); |
| 551 |
|
| 552 |
/* Settle early while the sender is still sending */ |
| 553 |
pn_delivery_settle(pn_link_current(rcv)); |
| 554 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 555 |
d.run(); |
| 556 |
CHECK_THAT(*pn_connection_remote_condition(d.client.connection), |
| 557 |
cond_empty()); |
| 558 |
CHECK_THAT(*pn_connection_condition(d.server.connection), cond_empty()); |
| 559 |
|
| 560 |
pn_delivery_settle(pn_link_current(snd)); |
| 561 |
|
| 562 |
/* Send/receive a new message, should not cause error */ |
| 563 |
pn_link_flow(rcv, 1); |
| 564 |
d.run(); |
| 565 |
pn_delivery(snd, pn_bytes("2")); /* Prepare to send */ |
| 566 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 567 |
server.log_clear(); |
| 568 |
d.run(); |
| 569 |
CHECK_THAT(ETYPES(PN_DELIVERY), Equals(server.log_clear())); |
| 570 |
CHECK(sizeof(data) == pn_link_recv(rcv, rbuf, sizeof(data))); |
| 571 |
pn_delivery_tag_t tag = pn_delivery_tag(pn_link_current(rcv)); |
| 572 |
CHECK(tag.size == 1); |
| 573 |
CHECK(tag.start[0] == '2'); |
| 574 |
CHECK_THAT(*pn_connection_remote_condition(d.client.connection), |
| 575 |
cond_empty()); |
| 576 |
CHECK_THAT(*pn_connection_condition(d.server.connection), cond_empty()); |
| 577 |
} |
| 578 |
|
| 579 |
/* Empty last frame in streaming message. |
| 580 |
*/ |
| 581 |
TEST_CASE("driver_empty_last_frame") { |
| 582 |
send_client_handler client; |
| 583 |
delivery_handler server; |
| 584 |
pn_test::driver_pair d(client, server); |
| 585 |
|
| 586 |
d.run(); |
| 587 |
pn_link_t *rcv = server.link; |
| 588 |
pn_link_t *snd = client.link; |
| 589 |
char data[100] = {0}; /* Dummy data to send. */ |
| 590 |
char rbuf[sizeof(data)] = {0}; /* Read buffer for incoming data. */ |
| 591 |
pn_link_flow(rcv, 1); |
| 592 |
pn_delivery_t *sd = pn_delivery(snd, pn_bytes("1")); /* Prepare to send */ |
| 593 |
d.run(); |
| 594 |
|
| 595 |
/* Send/receive a frame */ |
| 596 |
CHECK(sizeof(data) == pn_link_send(snd, data, sizeof(data))); |
| 597 |
server.log_clear(); |
| 598 |
d.run(); |
| 599 |
CHECK_THAT(ETYPES(PN_DELIVERY), Equals(server.log_clear())); |
| 600 |
CHECK(sizeof(data) == pn_link_recv(rcv, rbuf, sizeof(data))); |
| 601 |
CHECK(pn_delivery_partial(pn_link_current(rcv))); |
| 602 |
d.run(); |
| 603 |
|
| 604 |
/* Advance after all data transfered over wire. */ |
| 605 |
CHECK(pn_link_advance(snd)); |
| 606 |
server.log_clear(); |
| 607 |
d.run(); |
| 608 |
CHECK_THAT(ETYPES(PN_DELIVERY), Equals(server.log_clear())); |
| 609 |
CHECK(PN_EOS == pn_link_recv(rcv, rbuf, sizeof(data))); |
| 610 |
CHECK(!pn_delivery_partial(pn_link_current(rcv))); |
| 611 |
|
| 612 |
pn_delivery_settle(sd); |
| 613 |
sd = NULL; |
| 614 |
pn_delivery_settle(pn_link_current(rcv)); |
| 615 |
d.run(); |
| 616 |
CHECK_THAT(*pn_connection_remote_condition(d.client.connection), |
| 617 |
cond_empty()); |
| 618 |
CHECK_THAT(*pn_connection_condition(d.server.connection), cond_empty()); |
| 619 |
} |