| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "./pn_test.hpp" |
| 23 |
|
| 24 |
#include <proton/engine.h> |
| 25 |
|
| 26 |
using namespace pn_test; |
| 27 |
|
| 28 |
// push data from one transport to another |
| 29 |
static int xfer(pn_transport_t *src, pn_transport_t *dest) { |
| 30 |
ssize_t out = pn_transport_pending(src); |
| 31 |
if (out > 0) { |
| 32 |
ssize_t in = pn_transport_capacity(dest); |
| 33 |
if (in > 0) { |
| 34 |
size_t count = (size_t)((out < in) ? out : in); |
| 35 |
pn_transport_push(dest, pn_transport_head(src), count); |
| 36 |
pn_transport_pop(src, count); |
| 37 |
return (int)count; |
| 38 |
} |
| 39 |
} |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
// transfer all available data between two transports |
| 44 |
static int pump(pn_transport_t *t1, pn_transport_t *t2) { |
| 45 |
int total = 0; |
| 46 |
int work; |
| 47 |
do { |
| 48 |
work = xfer(t1, t2) + xfer(t2, t1); |
| 49 |
total += work; |
| 50 |
} while (work); |
| 51 |
return total; |
| 52 |
} |
| 53 |
|
| 54 |
// handle state changes of the endpoints |
| 55 |
static void process_endpoints(pn_connection_t *conn) { |
| 56 |
pn_session_t *ssn = pn_session_head(conn, PN_LOCAL_UNINIT); |
| 57 |
while (ssn) { |
| 58 |
// fprintf(stderr, "Opening session %p\n", (void*)ssn); |
| 59 |
pn_session_open(ssn); |
| 60 |
ssn = pn_session_next(ssn, PN_LOCAL_UNINIT); |
| 61 |
} |
| 62 |
|
| 63 |
pn_link_t *link = pn_link_head(conn, PN_LOCAL_UNINIT); |
| 64 |
while (link) { |
| 65 |
pn_link_open(link); |
| 66 |
link = pn_link_next(link, PN_LOCAL_UNINIT); |
| 67 |
} |
| 68 |
|
| 69 |
link = pn_link_head(conn, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); |
| 70 |
while (link) { |
| 71 |
pn_link_close(link); |
| 72 |
link = pn_link_next(link, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); |
| 73 |
} |
| 74 |
|
| 75 |
ssn = pn_session_head(conn, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); |
| 76 |
while (ssn) { |
| 77 |
pn_session_close(ssn); |
| 78 |
ssn = pn_session_next(ssn, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// bring up a session and a link between the two connections |
| 83 |
static void test_setup(pn_connection_t *c1, pn_transport_t *t1, |
| 84 |
pn_connection_t *c2, pn_transport_t *t2) { |
| 85 |
pn_connection_open(c1); |
| 86 |
pn_connection_open(c2); |
| 87 |
|
| 88 |
pn_session_t *s1 = pn_session(c1); |
| 89 |
pn_session_open(s1); |
| 90 |
|
| 91 |
pn_link_t *tx = pn_sender(s1, "sender"); |
| 92 |
pn_link_open(tx); |
| 93 |
|
| 94 |
while (pump(t1, t2)) { |
| 95 |
process_endpoints(c1); |
| 96 |
process_endpoints(c2); |
| 97 |
} |
| 98 |
|
| 99 |
// session and link should be up, c2 should have a receiver link: |
| 100 |
|
| 101 |
REQUIRE(pn_session_state(s1) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 102 |
REQUIRE(pn_link_state(tx) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 103 |
|
| 104 |
pn_link_t *rx = pn_link_head(c2, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 105 |
REQUIRE(rx); |
| 106 |
REQUIRE(pn_link_is_receiver(rx)); |
| 107 |
} |
| 108 |
|
| 109 |
// test that free'ing the connection should free all contained |
| 110 |
// resources (session, links, deliveries) |
| 111 |
TEST_CASE("engine_free_connection") { |
| 112 |
pn_connection_t *c1 = pn_connection(); |
| 113 |
pn_transport_t *t1 = pn_transport(); |
| 114 |
pn_transport_bind(t1, c1); |
| 115 |
|
| 116 |
pn_connection_t *c2 = pn_connection(); |
| 117 |
pn_transport_t *t2 = pn_transport(); |
| 118 |
pn_transport_set_server(t2); |
| 119 |
pn_transport_bind(t2, c2); |
| 120 |
|
| 121 |
// pn_transport_trace(t1, PN_TRACE_FRM); |
| 122 |
test_setup(c1, t1, c2, t2); |
| 123 |
|
| 124 |
pn_link_t *tx = pn_link_head(c1, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 125 |
REQUIRE(tx); |
| 126 |
pn_link_t *rx = pn_link_head(c2, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 127 |
REQUIRE(rx); |
| 128 |
|
| 129 |
// transfer some data across the link: |
| 130 |
pn_link_flow(rx, 10); |
| 131 |
pn_delivery_t *d1 = pn_delivery(tx, pn_dtag("tag-1", 6)); |
| 132 |
while (pump(t1, t2)) { |
| 133 |
process_endpoints(c1); |
| 134 |
process_endpoints(c2); |
| 135 |
} |
| 136 |
REQUIRE(pn_delivery_writable(d1)); |
| 137 |
pn_link_send(tx, "ABC", 4); |
| 138 |
pn_link_advance(tx); |
| 139 |
|
| 140 |
// now free the connection, but keep processing the transport |
| 141 |
process_endpoints(c1); |
| 142 |
pn_connection_free(c1); |
| 143 |
while (pump(t1, t2)) { |
| 144 |
process_endpoints(c2); |
| 145 |
} |
| 146 |
|
| 147 |
// delivery should have transfered: |
| 148 |
REQUIRE(pn_link_current(rx)); |
| 149 |
REQUIRE(pn_delivery_readable(pn_link_current(rx))); |
| 150 |
|
| 151 |
pn_transport_unbind(t1); |
| 152 |
pn_transport_free(t1); |
| 153 |
|
| 154 |
pn_connection_free(c2); |
| 155 |
pn_transport_unbind(t2); |
| 156 |
pn_transport_free(t2); |
| 157 |
} |
| 158 |
|
| 159 |
TEST_CASE("engine_free_session") { |
| 160 |
pn_connection_t *c1 = pn_connection(); |
| 161 |
pn_transport_t *t1 = pn_transport(); |
| 162 |
pn_transport_bind(t1, c1); |
| 163 |
|
| 164 |
pn_connection_t *c2 = pn_connection(); |
| 165 |
pn_transport_t *t2 = pn_transport(); |
| 166 |
pn_transport_set_server(t2); |
| 167 |
pn_transport_bind(t2, c2); |
| 168 |
|
| 169 |
// pn_transport_trace(t1, PN_TRACE_FRM); |
| 170 |
test_setup(c1, t1, c2, t2); |
| 171 |
|
| 172 |
pn_session_t *ssn = pn_session_head(c1, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 173 |
REQUIRE(ssn); |
| 174 |
pn_link_t *tx = pn_link_head(c1, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 175 |
REQUIRE(tx); |
| 176 |
pn_link_t *rx = pn_link_head(c2, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 177 |
REQUIRE(rx); |
| 178 |
|
| 179 |
// prepare for transfer: request some credit |
| 180 |
pn_link_flow(rx, 10); |
| 181 |
pn_delivery_t *d1 = pn_delivery(tx, pn_dtag("tag-1", 6)); |
| 182 |
while (pump(t1, t2)) { |
| 183 |
process_endpoints(c1); |
| 184 |
process_endpoints(c2); |
| 185 |
} |
| 186 |
REQUIRE(pn_delivery_writable(d1)); |
| 187 |
|
| 188 |
// send some data, but also close the session: |
| 189 |
pn_link_send(tx, "ABC", 4); |
| 190 |
pn_link_advance(tx); |
| 191 |
|
| 192 |
pn_session_close(ssn); |
| 193 |
pn_session_free(ssn); |
| 194 |
|
| 195 |
while (pump(t1, t2)) { |
| 196 |
process_endpoints(c1); |
| 197 |
process_endpoints(c2); |
| 198 |
} |
| 199 |
|
| 200 |
// delivery should have transfered: |
| 201 |
REQUIRE(pn_link_current(rx)); |
| 202 |
REQUIRE(pn_delivery_readable(pn_link_current(rx))); |
| 203 |
|
| 204 |
// c2's session should see the close: |
| 205 |
pn_session_t *ssn2 = pn_session_head(c2, 0); |
| 206 |
REQUIRE(ssn2); |
| 207 |
REQUIRE(pn_session_state(ssn2) == (PN_LOCAL_CLOSED | PN_REMOTE_CLOSED)); |
| 208 |
|
| 209 |
pn_transport_unbind(t1); |
| 210 |
pn_transport_free(t1); |
| 211 |
pn_connection_free(c1); |
| 212 |
|
| 213 |
pn_transport_unbind(t2); |
| 214 |
pn_transport_free(t2); |
| 215 |
pn_connection_free(c2); |
| 216 |
} |
| 217 |
|
| 218 |
TEST_CASE("engine_free_link)") { |
| 219 |
pn_connection_t *c1 = pn_connection(); |
| 220 |
pn_transport_t *t1 = pn_transport(); |
| 221 |
pn_transport_bind(t1, c1); |
| 222 |
|
| 223 |
pn_connection_t *c2 = pn_connection(); |
| 224 |
pn_transport_t *t2 = pn_transport(); |
| 225 |
pn_transport_set_server(t2); |
| 226 |
pn_transport_bind(t2, c2); |
| 227 |
|
| 228 |
// pn_transport_trace(t1, PN_TRACE_FRM); |
| 229 |
test_setup(c1, t1, c2, t2); |
| 230 |
|
| 231 |
pn_link_t *tx = pn_link_head(c1, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 232 |
REQUIRE(tx); |
| 233 |
pn_link_t *rx = pn_link_head(c2, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 234 |
REQUIRE(rx); |
| 235 |
|
| 236 |
// prepare for transfer: request some credit |
| 237 |
pn_link_flow(rx, 10); |
| 238 |
pn_delivery_t *d1 = pn_delivery(tx, pn_dtag("tag-1", 6)); |
| 239 |
while (pump(t1, t2)) { |
| 240 |
process_endpoints(c1); |
| 241 |
process_endpoints(c2); |
| 242 |
} |
| 243 |
REQUIRE(pn_delivery_writable(d1)); |
| 244 |
|
| 245 |
// send some data, then close and destroy the link: |
| 246 |
pn_link_send(tx, "ABC", 4); |
| 247 |
pn_link_advance(tx); |
| 248 |
|
| 249 |
pn_link_close(tx); |
| 250 |
pn_link_free(tx); |
| 251 |
|
| 252 |
while (pump(t1, t2)) { |
| 253 |
process_endpoints(c1); |
| 254 |
process_endpoints(c2); |
| 255 |
} |
| 256 |
|
| 257 |
// the data transfer will complete and the link close |
| 258 |
// should have been sent to the peer |
| 259 |
REQUIRE(pn_link_current(rx)); |
| 260 |
REQUIRE(pn_link_state(rx) == (PN_LOCAL_CLOSED | PN_REMOTE_CLOSED)); |
| 261 |
|
| 262 |
pn_transport_unbind(t1); |
| 263 |
pn_transport_free(t1); |
| 264 |
pn_connection_free(c1); |
| 265 |
|
| 266 |
pn_transport_unbind(t2); |
| 267 |
pn_transport_free(t2); |
| 268 |
pn_connection_free(c2); |
| 269 |
} |
| 270 |
|
| 271 |
// regression test fo PROTON-1466 - confusion between links with prefix names |
| 272 |
TEST_CASE("engine_link_name_prefix)") { |
| 273 |
pn_connection_t *c1 = pn_connection(); |
| 274 |
pn_transport_t *t1 = pn_transport(); |
| 275 |
pn_transport_bind(t1, c1); |
| 276 |
|
| 277 |
pn_connection_t *c2 = pn_connection(); |
| 278 |
pn_transport_t *t2 = pn_transport(); |
| 279 |
pn_transport_set_server(t2); |
| 280 |
pn_transport_bind(t2, c2); |
| 281 |
|
| 282 |
pn_connection_open(c1); |
| 283 |
pn_connection_open(c2); |
| 284 |
|
| 285 |
pn_session_t *s1 = pn_session(c1); |
| 286 |
pn_session_open(s1); |
| 287 |
|
| 288 |
pn_link_t *l = pn_receiver(s1, "l"); |
| 289 |
pn_link_open(l); |
| 290 |
pn_link_t *lll = pn_receiver(s1, "lll"); |
| 291 |
pn_link_open(lll); |
| 292 |
pn_link_t *ll = pn_receiver(s1, "ll"); |
| 293 |
pn_link_open(ll); |
| 294 |
|
| 295 |
while (pump(t1, t2)) { |
| 296 |
process_endpoints(c1); |
| 297 |
process_endpoints(c2); |
| 298 |
} |
| 299 |
|
| 300 |
// session and link should be up, c2 should have a receiver link: |
| 301 |
REQUIRE(pn_session_state(s1) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 302 |
REQUIRE(pn_link_state(l) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 303 |
REQUIRE(pn_link_state(lll) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 304 |
REQUIRE(pn_link_state(ll) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 305 |
|
| 306 |
pn_link_t *r = pn_link_head(c2, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 307 |
REQUIRE(std::string("l") == pn_link_name(r)); |
| 308 |
r = pn_link_next(r, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 309 |
REQUIRE(std::string("lll") == pn_link_name(r)); |
| 310 |
r = pn_link_next(r, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 311 |
REQUIRE(std::string("ll") == pn_link_name(r)); |
| 312 |
|
| 313 |
pn_transport_unbind(t1); |
| 314 |
pn_transport_free(t1); |
| 315 |
pn_connection_free(c1); |
| 316 |
|
| 317 |
pn_transport_unbind(t2); |
| 318 |
pn_transport_free(t2); |
| 319 |
pn_connection_free(c2); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
TEST_CASE("link_properties)") { |
| 324 |
pn_connection_t *c1 = pn_connection(); |
| 325 |
pn_transport_t *t1 = pn_transport(); |
| 326 |
pn_transport_bind(t1, c1); |
| 327 |
|
| 328 |
pn_connection_t *c2 = pn_connection(); |
| 329 |
pn_transport_t *t2 = pn_transport(); |
| 330 |
pn_transport_set_server(t2); |
| 331 |
pn_transport_bind(t2, c2); |
| 332 |
|
| 333 |
pn_connection_open(c1); |
| 334 |
pn_connection_open(c2); |
| 335 |
|
| 336 |
pn_session_t *s1 = pn_session(c1); |
| 337 |
pn_session_open(s1); |
| 338 |
|
| 339 |
pn_link_t *rx = pn_receiver(s1, "props"); |
| 340 |
pn_data_t *props = pn_link_properties(rx); |
| 341 |
REQUIRE(props != NULL); |
| 342 |
|
| 343 |
pn_data_clear(props); |
| 344 |
pn_data_fill(props, "{S[iii]SI}", "foo", 1, 987, 3, "bar", 965); |
| 345 |
pn_link_open(rx); |
| 346 |
|
| 347 |
while (pump(t1, t2)) { |
| 348 |
process_endpoints(c1); |
| 349 |
process_endpoints(c2); |
| 350 |
} |
| 351 |
|
| 352 |
// session and link should be up, c2 should have a sender link: |
| 353 |
REQUIRE(pn_link_state(rx) == (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 354 |
REQUIRE(pn_link_remote_properties(rx) == NULL); |
| 355 |
|
| 356 |
pn_link_t *tx = pn_link_head(c2, (PN_LOCAL_ACTIVE | PN_REMOTE_ACTIVE)); |
| 357 |
|
| 358 |
REQUIRE(pn_link_remote_properties(tx) != NULL); |
| 359 |
CHECK("{\"foo\"=[1, 987, 3], \"bar\"=965}" == pn_test::inspect(pn_link_remote_properties(tx))); |
| 360 |
|
| 361 |
pn_transport_unbind(t1); |
| 362 |
pn_transport_free(t1); |
| 363 |
pn_connection_free(c1); |
| 364 |
|
| 365 |
pn_transport_unbind(t2); |
| 366 |
pn_transport_free(t2); |
| 367 |
pn_connection_free(c2); |
| 368 |
} |