test_channel.c (50321B)
1 /* Copyright (c) 2013-2021, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 #define CHANNEL_OBJECT_PRIVATE 5 #define CHANNEL_FILE_PRIVATE 6 #include "core/or/or.h" 7 #include "core/or/channel.h" 8 /* For channel_note_destroy_not_pending */ 9 #define CIRCUITLIST_PRIVATE 10 #include "core/or/circuitlist.h" 11 #include "core/or/circuitmux.h" 12 #include "core/or/circuitmux_ewma.h" 13 /* For var_cell_free */ 14 #include "core/or/connection_or.h" 15 #include "lib/crypt_ops/crypto_rand.h" 16 /* For packed_cell stuff */ 17 #define RELAY_PRIVATE 18 #include "core/or/relay.h" 19 /* For channel_tls_t object and private functions. */ 20 #define CHANNEL_OBJECT_PRIVATE 21 #define CHANNELTLS_PRIVATE 22 #include "core/or/channeltls.h" 23 /* For init/free stuff */ 24 #include "core/or/scheduler.h" 25 #include "feature/nodelist/networkstatus.h" 26 27 #include "core/or/cell_st.h" 28 #include "feature/nodelist/networkstatus_st.h" 29 #include "core/or/origin_circuit_st.h" 30 #include "feature/nodelist/routerstatus_st.h" 31 #include "core/or/var_cell_st.h" 32 #include "core/or/or_connection_st.h" 33 #include "lib/net/inaddr.h" 34 35 /* Test suite stuff */ 36 #include "test/log_test_helpers.h" 37 #include "test/test.h" 38 #include "test/fakechans.h" 39 40 static int test_chan_accept_cells = 0; 41 static int test_chan_fixed_cells_recved = 0; 42 static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL; 43 static int test_cells_written = 0; 44 static int test_doesnt_want_writes_count = 0; 45 static int test_dumpstats_calls = 0; 46 static int test_has_waiting_cells_count = 0; 47 static int test_releases_count = 0; 48 static channel_t *dump_statistics_mock_target = NULL; 49 static int dump_statistics_mock_matches = 0; 50 static int test_close_called = 0; 51 static int test_chan_should_be_canonical = 0; 52 static int test_chan_should_match_target = 0; 53 static int test_chan_listener_close_fn_called = 0; 54 static int test_chan_listener_fn_called = 0; 55 56 static const char * 57 chan_test_describe_transport(channel_t *ch) 58 { 59 tt_ptr_op(ch, OP_NE, NULL); 60 61 done: 62 return "Fake channel for unit tests"; 63 } 64 65 /** 66 * Mock for channel_dump_statistics(); if the channel matches the 67 * target, bump a counter - otherwise ignore. 68 */ 69 70 static void 71 chan_test_channel_dump_statistics_mock(channel_t *chan, int severity) 72 { 73 tt_ptr_op(chan, OP_NE, NULL); 74 75 (void)severity; 76 77 if (chan != NULL && chan == dump_statistics_mock_target) { 78 ++dump_statistics_mock_matches; 79 } 80 81 done: 82 return; 83 } 84 85 /* 86 * Handle an incoming fixed-size cell for unit tests 87 */ 88 89 static void 90 chan_test_cell_handler(channel_t *chan, cell_t *cell) 91 { 92 tt_assert(chan); 93 tt_assert(cell); 94 95 test_chan_last_seen_fixed_cell_ptr = cell; 96 ++test_chan_fixed_cells_recved; 97 98 done: 99 return; 100 } 101 102 /* 103 * Fake transport-specific stats call 104 */ 105 106 static void 107 chan_test_dumpstats(channel_t *ch, int severity) 108 { 109 tt_ptr_op(ch, OP_NE, NULL); 110 111 (void)severity; 112 113 ++test_dumpstats_calls; 114 115 done: 116 return; 117 } 118 119 static void 120 chan_test_close(channel_t *ch) 121 { 122 tt_assert(ch); 123 124 ++test_close_called; 125 126 done: 127 return; 128 } 129 130 /* 131 * Close a channel through the error path 132 */ 133 134 static void 135 chan_test_error(channel_t *ch) 136 { 137 tt_assert(ch); 138 tt_assert(!(ch->state == CHANNEL_STATE_CLOSING || 139 ch->state == CHANNEL_STATE_ERROR || 140 ch->state == CHANNEL_STATE_CLOSED)); 141 142 channel_close_for_error(ch); 143 144 done: 145 return; 146 } 147 148 /* 149 * Finish closing a channel from CHANNEL_STATE_CLOSING 150 */ 151 152 static void 153 chan_test_finish_close(channel_t *ch) 154 { 155 tt_assert(ch); 156 tt_assert(ch->state == CHANNEL_STATE_CLOSING); 157 158 channel_closed(ch); 159 160 done: 161 return; 162 } 163 164 static const char * 165 chan_test_describe_peer(const channel_t *ch) 166 { 167 tt_assert(ch); 168 169 done: 170 return "Fake channel for unit tests; no real endpoint"; 171 } 172 173 static int 174 chan_test_get_remote_addr(const channel_t *ch, tor_addr_t *out) 175 { 176 (void)ch; 177 tor_addr_from_ipv4h(out, 0x7f000001); 178 return 1; 179 } 180 181 static int 182 chan_test_num_cells_writeable(channel_t *ch) 183 { 184 tt_assert(ch); 185 186 done: 187 return 32; 188 } 189 190 static int 191 chan_test_write_packed_cell(channel_t *ch, 192 packed_cell_t *packed_cell) 193 { 194 int rv = 0; 195 196 tt_assert(ch); 197 tt_assert(packed_cell); 198 199 if (test_chan_accept_cells) { 200 /* Free the cell and bump the counter */ 201 ++test_cells_written; 202 rv = 1; 203 } 204 /* else return 0, we didn't accept it */ 205 206 done: 207 return rv; 208 } 209 210 static int 211 chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell) 212 { 213 int rv = 0; 214 215 tt_assert(ch); 216 tt_assert(var_cell); 217 218 if (test_chan_accept_cells) { 219 /* Free the cell and bump the counter */ 220 var_cell_free(var_cell); 221 ++test_cells_written; 222 rv = 1; 223 } 224 /* else return 0, we didn't accept it */ 225 226 done: 227 return rv; 228 } 229 230 /** 231 * Fill out c with a new fake cell for test suite use 232 */ 233 234 void 235 make_fake_cell(cell_t *c) 236 { 237 tt_ptr_op(c, OP_NE, NULL); 238 239 c->circ_id = 1; 240 c->command = CELL_RELAY; 241 memset(c->payload, 0, CELL_PAYLOAD_SIZE); 242 243 done: 244 return; 245 } 246 247 /** 248 * Fill out c with a new fake var_cell for test suite use 249 */ 250 251 void 252 make_fake_var_cell(var_cell_t *c) 253 { 254 tt_ptr_op(c, OP_NE, NULL); 255 256 c->circ_id = 1; 257 c->command = CELL_VERSIONS; 258 c->payload_len = CELL_PAYLOAD_SIZE / 2; 259 memset(c->payload, 0, c->payload_len); 260 261 done: 262 return; 263 } 264 265 /** 266 * Set up a new fake channel for the test suite 267 */ 268 269 channel_t * 270 new_fake_channel(void) 271 { 272 channel_t *chan = tor_malloc_zero(sizeof(channel_t)); 273 channel_init(chan); 274 275 chan->close = chan_test_close; 276 chan->num_cells_writeable = chan_test_num_cells_writeable; 277 chan->describe_peer = chan_test_describe_peer; 278 chan->get_remote_addr = chan_test_get_remote_addr; 279 chan->write_packed_cell = chan_test_write_packed_cell; 280 chan->write_var_cell = chan_test_write_var_cell; 281 chan->state = CHANNEL_STATE_OPEN; 282 283 chan->cmux = circuitmux_alloc(); 284 circuitmux_set_policy(chan->cmux, &ewma_policy); 285 286 return chan; 287 } 288 289 void 290 free_fake_channel(channel_t *chan) 291 { 292 if (! chan) 293 return; 294 295 if (chan->cmux) 296 circuitmux_free(chan->cmux); 297 298 tor_free(chan); 299 } 300 301 /** 302 * Counter query for scheduler_channel_has_waiting_cells_mock() 303 */ 304 305 int 306 get_mock_scheduler_has_waiting_cells_count(void) 307 { 308 return test_has_waiting_cells_count; 309 } 310 311 /** 312 * Mock for scheduler_channel_has_waiting_cells() 313 */ 314 315 void 316 scheduler_channel_has_waiting_cells_mock(channel_t *ch) 317 { 318 (void)ch; 319 320 /* Increment counter */ 321 ++test_has_waiting_cells_count; 322 323 return; 324 } 325 326 static void 327 scheduler_channel_doesnt_want_writes_mock(channel_t *ch) 328 { 329 (void)ch; 330 331 /* Increment counter */ 332 ++test_doesnt_want_writes_count; 333 334 return; 335 } 336 337 /** 338 * Mock for scheduler_release_channel() 339 */ 340 341 void 342 scheduler_release_channel_mock(channel_t *ch) 343 { 344 (void)ch; 345 346 /* Increment counter */ 347 ++test_releases_count; 348 349 return; 350 } 351 352 static int 353 test_chan_is_canonical(channel_t *chan) 354 { 355 tor_assert(chan); 356 357 if (test_chan_should_be_canonical) { 358 return 1; 359 } 360 return 0; 361 } 362 363 static int 364 test_chan_matches_target(channel_t *chan, const tor_addr_t *target) 365 { 366 (void) chan; 367 (void) target; 368 369 if (test_chan_should_match_target) { 370 return 1; 371 } 372 return 0; 373 } 374 375 static void 376 test_chan_listener_close(channel_listener_t *chan) 377 { 378 (void) chan; 379 ++test_chan_listener_close_fn_called; 380 return; 381 } 382 383 static void 384 test_chan_listener_fn(channel_listener_t *listener, channel_t *chan) 385 { 386 (void) listener; 387 (void) chan; 388 389 ++test_chan_listener_fn_called; 390 return; 391 } 392 393 static const char * 394 test_chan_listener_describe_transport(channel_listener_t *chan) 395 { 396 (void) chan; 397 return "Fake listener channel."; 398 } 399 400 /** 401 * Test for channel_dumpstats() and limited test for 402 * channel_dump_statistics() 403 */ 404 405 static void 406 test_channel_dumpstats(void *arg) 407 { 408 channel_t *ch = NULL; 409 cell_t *cell = NULL; 410 packed_cell_t *p_cell = NULL; 411 int old_count; 412 413 (void)arg; 414 415 /* Mock these for duration of the test */ 416 MOCK(scheduler_channel_doesnt_want_writes, 417 scheduler_channel_doesnt_want_writes_mock); 418 MOCK(scheduler_release_channel, 419 scheduler_release_channel_mock); 420 421 /* Set up a new fake channel */ 422 ch = new_fake_channel(); 423 tt_assert(ch); 424 425 /* Try to register it */ 426 channel_register(ch); 427 tt_assert(ch->registered); 428 429 /* Set up mock */ 430 dump_statistics_mock_target = ch; 431 dump_statistics_mock_matches = 0; 432 MOCK(channel_dump_statistics, 433 chan_test_channel_dump_statistics_mock); 434 435 /* Call channel_dumpstats() */ 436 channel_dumpstats(LOG_DEBUG); 437 438 /* Assert that we hit the mock */ 439 tt_int_op(dump_statistics_mock_matches, OP_EQ, 1); 440 441 /* Close the channel */ 442 channel_mark_for_close(ch); 443 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 444 chan_test_finish_close(ch); 445 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED); 446 447 /* Try again and hit the finished channel */ 448 channel_dumpstats(LOG_DEBUG); 449 tt_int_op(dump_statistics_mock_matches, OP_EQ, 2); 450 451 channel_run_cleanup(); 452 ch = NULL; 453 454 /* Now we should hit nothing */ 455 channel_dumpstats(LOG_DEBUG); 456 tt_int_op(dump_statistics_mock_matches, OP_EQ, 2); 457 458 /* Unmock */ 459 UNMOCK(channel_dump_statistics); 460 dump_statistics_mock_target = NULL; 461 dump_statistics_mock_matches = 0; 462 463 /* Now make another channel */ 464 ch = new_fake_channel(); 465 tt_assert(ch); 466 channel_register(ch); 467 tt_int_op(ch->registered, OP_EQ, 1); 468 /* Lie about its age so dumpstats gets coverage for rate calculations */ 469 ch->timestamp_created = time(NULL) - 30; 470 tt_int_op(ch->timestamp_created, OP_GT, 0); 471 tt_int_op(time(NULL), OP_GT, ch->timestamp_created); 472 473 /* Put cells through it both ways to make the counters non-zero */ 474 p_cell = packed_cell_new(); 475 test_chan_accept_cells = 1; 476 old_count = test_cells_written; 477 channel_write_packed_cell(ch, p_cell); 478 tt_int_op(test_cells_written, OP_EQ, old_count + 1); 479 tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0); 480 tt_u64_op(ch->n_cells_xmitted, OP_GT, 0); 481 482 /* Receive path */ 483 channel_set_cell_handlers(ch, 484 chan_test_cell_handler); 485 tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler); 486 cell = tor_malloc_zero(sizeof(*cell)); 487 old_count = test_chan_fixed_cells_recved; 488 channel_process_cell(ch, cell); 489 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1); 490 tt_u64_op(ch->n_bytes_recved, OP_GT, 0); 491 tt_u64_op(ch->n_cells_recved, OP_GT, 0); 492 493 /* Test channel_dump_statistics */ 494 ch->describe_transport = chan_test_describe_transport; 495 ch->dumpstats = chan_test_dumpstats; 496 test_chan_should_be_canonical = 1; 497 ch->is_canonical = test_chan_is_canonical; 498 old_count = test_dumpstats_calls; 499 channel_dump_statistics(ch, LOG_DEBUG); 500 tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1); 501 502 /* Close the channel */ 503 channel_mark_for_close(ch); 504 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 505 chan_test_finish_close(ch); 506 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED); 507 channel_run_cleanup(); 508 ch = NULL; 509 510 done: 511 free_fake_channel(ch); 512 tor_free(cell); 513 514 UNMOCK(scheduler_channel_doesnt_want_writes); 515 UNMOCK(scheduler_release_channel); 516 517 return; 518 } 519 520 /* Test outbound cell. The callstack is: 521 * channel_flush_some_cells() 522 * -> channel_flush_from_first_active_circuit() 523 * -> channel_write_packed_cell() 524 * -> write_packed_cell() 525 * -> chan->write_packed_cell() fct ptr. 526 * 527 * This test goes from a cell in a circuit up to the channel write handler 528 * that should put them on the connection outbuf. */ 529 static void 530 test_channel_outbound_cell(void *arg) 531 { 532 int old_count; 533 channel_t *chan = NULL; 534 packed_cell_t *p_cell = NULL, *p_cell2 = NULL; 535 origin_circuit_t *circ = NULL; 536 cell_queue_t *queue; 537 538 (void) arg; 539 540 /* Set the test time to be mocked, since this test assumes that no 541 * time will pass, ewma values will not need to be re-scaled, and so on */ 542 monotime_enable_test_mocking(); 543 monotime_set_mock_time_nsec(UINT64_C(1000000000) * 12345); 544 545 cmux_ewma_set_options(NULL,NULL); 546 547 /* The channel will be freed so we need to hijack this so the scheduler 548 * doesn't get confused. */ 549 MOCK(scheduler_release_channel, scheduler_release_channel_mock); 550 551 /* Accept cells to lower layer */ 552 test_chan_accept_cells = 1; 553 554 /* Setup a valid circuit to queue a cell. */ 555 circ = origin_circuit_new(); 556 tt_assert(circ); 557 /* Circuit needs an origin purpose to be considered origin. */ 558 TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL; 559 TO_CIRCUIT(circ)->n_circ_id = 42; 560 /* This is the outbound test so use the next channel queue. */ 561 queue = &TO_CIRCUIT(circ)->n_chan_cells; 562 /* Setup packed cell to queue on the circuit. */ 563 p_cell = packed_cell_new(); 564 tt_assert(p_cell); 565 p_cell2 = packed_cell_new(); 566 tt_assert(p_cell2); 567 /* Setup a channel to put the circuit on. */ 568 chan = new_fake_channel(); 569 tt_assert(chan); 570 chan->state = CHANNEL_STATE_OPENING; 571 channel_change_state_open(chan); 572 /* Outbound channel. */ 573 channel_mark_outgoing(chan); 574 /* Try to register it so we can clean it through the channel cleanup 575 * process. */ 576 channel_register(chan); 577 tt_int_op(chan->registered, OP_EQ, 1); 578 /* Set EWMA policy so we can pick it when flushing. */ 579 circuitmux_set_policy(chan->cmux, &ewma_policy); 580 tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy); 581 582 /* Register circuit to the channel circid map which will attach the circuit 583 * to the channel's cmux as well. */ 584 circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan); 585 tt_int_op(channel_num_circuits(chan), OP_EQ, 1); 586 /* Test the cmux state. */ 587 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)), 588 OP_EQ, 1); 589 590 /* Flush the channel without any cell on it. */ 591 old_count = test_cells_written; 592 ssize_t flushed = channel_flush_some_cells(chan, 1); 593 tt_i64_op(flushed, OP_EQ, 0); 594 tt_int_op(test_cells_written, OP_EQ, old_count); 595 tt_int_op(channel_more_to_flush(chan), OP_EQ, 0); 596 tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0); 597 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0); 598 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)), 599 OP_EQ, 0); 600 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0); 601 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0); 602 603 /* Queue cell onto the next queue that is the outbound direction. Than 604 * update its cmux so the circuit can be picked when flushing cells. */ 605 cell_queue_append(queue, p_cell); 606 p_cell = NULL; 607 tt_int_op(queue->n, OP_EQ, 1); 608 cell_queue_append(queue, p_cell2); 609 p_cell2 = NULL; 610 tt_int_op(queue->n, OP_EQ, 2); 611 612 update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT); 613 tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1); 614 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2); 615 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)), 616 OP_EQ, 1); 617 618 /* From this point on, we have a queued cell on an active circuit attached 619 * to the channel's cmux. */ 620 621 /* Flush the first cell. This is going to go down the call stack. */ 622 old_count = test_cells_written; 623 flushed = channel_flush_some_cells(chan, 1); 624 tt_i64_op(flushed, OP_EQ, 1); 625 tt_int_op(test_cells_written, OP_EQ, old_count + 1); 626 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1); 627 tt_int_op(channel_more_to_flush(chan), OP_EQ, 1); 628 /* Circuit should remain active because there is a second cell queued. */ 629 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)), 630 OP_EQ, 1); 631 /* Should still be attached. */ 632 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)), 633 OP_EQ, 1); 634 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1); 635 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0)); 636 637 /* Flush second cell. This is going to go down the call stack. */ 638 old_count = test_cells_written; 639 flushed = channel_flush_some_cells(chan, 1); 640 tt_i64_op(flushed, OP_EQ, 1); 641 tt_int_op(test_cells_written, OP_EQ, old_count + 1); 642 tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0); 643 tt_int_op(channel_more_to_flush(chan), OP_EQ, 0); 644 /* No more cells should make the circuit inactive. */ 645 tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)), 646 OP_EQ, 0); 647 /* Should still be attached. */ 648 tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)), 649 OP_EQ, 1); 650 tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2); 651 tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2); 652 653 done: 654 if (circ) { 655 circuit_free_(TO_CIRCUIT(circ)); 656 } 657 tor_free(p_cell); 658 channel_free_all(); 659 UNMOCK(scheduler_release_channel); 660 monotime_disable_test_mocking(); 661 } 662 663 /* Test inbound cell. The callstack is: 664 * channel_process_cell() 665 * -> chan->cell_handler() 666 * 667 * This test is about checking if we can process an inbound cell down to the 668 * channel handler. */ 669 static void 670 test_channel_inbound_cell(void *arg) 671 { 672 channel_t *chan = NULL; 673 cell_t *cell = NULL; 674 int old_count; 675 676 (void) arg; 677 678 /* The channel will be freed so we need to hijack this so the scheduler 679 * doesn't get confused. */ 680 MOCK(scheduler_release_channel, scheduler_release_channel_mock); 681 682 /* Accept cells to lower layer */ 683 test_chan_accept_cells = 1; 684 685 chan = new_fake_channel(); 686 tt_assert(chan); 687 /* Start it off in OPENING */ 688 chan->state = CHANNEL_STATE_OPENING; 689 690 /* Try to register it */ 691 channel_register(chan); 692 tt_int_op(chan->registered, OP_EQ, 1); 693 694 /* Open it */ 695 channel_change_state_open(chan); 696 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN); 697 tt_int_op(chan->has_been_open, OP_EQ, 1); 698 699 /* Receive a cell now. */ 700 cell = tor_malloc_zero(sizeof(*cell)); 701 make_fake_cell(cell); 702 old_count = test_chan_fixed_cells_recved; 703 channel_process_cell(chan, cell); 704 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count); 705 tt_assert(monotime_coarse_is_zero(&chan->timestamp_xfer)); 706 tt_u64_op(chan->timestamp_active, OP_EQ, 0); 707 tt_u64_op(chan->timestamp_recv, OP_EQ, 0); 708 709 /* Setup incoming cell handlers. We don't care about var cell, the channel 710 * layers is not handling those. */ 711 channel_set_cell_handlers(chan, chan_test_cell_handler); 712 tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler); 713 /* Now process the cell, we should see it. */ 714 old_count = test_chan_fixed_cells_recved; 715 channel_process_cell(chan, cell); 716 tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1); 717 /* We should have a series of timestamp set. */ 718 tt_assert(!monotime_coarse_is_zero(&chan->timestamp_xfer)); 719 tt_u64_op(chan->timestamp_active, OP_NE, 0); 720 tt_u64_op(chan->timestamp_recv, OP_NE, 0); 721 tt_assert(monotime_coarse_is_zero(&chan->next_padding_time)); 722 tt_u64_op(chan->n_cells_recved, OP_EQ, 1); 723 tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0)); 724 725 /* Close it */ 726 old_count = test_close_called; 727 channel_mark_for_close(chan); 728 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING); 729 tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED); 730 tt_int_op(test_close_called, OP_EQ, old_count + 1); 731 732 /* This closes the channel so it calls in the scheduler, make sure of it. */ 733 old_count = test_releases_count; 734 chan_test_finish_close(chan); 735 tt_int_op(test_releases_count, OP_EQ, old_count + 1); 736 tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED); 737 738 /* The channel will be free, lets make sure it is not accessible. */ 739 uint64_t chan_id = chan->global_identifier; 740 tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan); 741 channel_run_cleanup(); 742 chan = channel_find_by_global_id(chan_id); 743 tt_assert(chan == NULL); 744 745 done: 746 tor_free(cell); 747 UNMOCK(scheduler_release_channel); 748 } 749 750 /** 751 * Normal channel lifecycle test: 752 * 753 * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED 754 */ 755 756 static void 757 test_channel_lifecycle(void *arg) 758 { 759 channel_t *ch1 = NULL, *ch2 = NULL; 760 packed_cell_t *p_cell = NULL; 761 int old_count, init_doesnt_want_writes_count; 762 int init_releases_count; 763 764 (void)arg; 765 766 /* Mock these for the whole lifecycle test */ 767 MOCK(scheduler_channel_doesnt_want_writes, 768 scheduler_channel_doesnt_want_writes_mock); 769 MOCK(scheduler_release_channel, 770 scheduler_release_channel_mock); 771 772 /* Cache some initial counter values */ 773 init_doesnt_want_writes_count = test_doesnt_want_writes_count; 774 init_releases_count = test_releases_count; 775 776 /* Accept cells to lower layer */ 777 test_chan_accept_cells = 1; 778 779 ch1 = new_fake_channel(); 780 tt_assert(ch1); 781 /* Start it off in OPENING */ 782 ch1->state = CHANNEL_STATE_OPENING; 783 784 /* Try to register it */ 785 channel_register(ch1); 786 tt_assert(ch1->registered); 787 788 /* Try to write a cell through (should queue) */ 789 p_cell = packed_cell_new(); 790 old_count = test_cells_written; 791 channel_write_packed_cell(ch1, p_cell); 792 tt_int_op(old_count, OP_EQ, test_cells_written); 793 794 /* Move it to OPEN and flush */ 795 channel_change_state_open(ch1); 796 797 /* Get another one */ 798 ch2 = new_fake_channel(); 799 tt_assert(ch2); 800 ch2->state = CHANNEL_STATE_OPENING; 801 802 /* Register */ 803 channel_register(ch2); 804 tt_assert(ch2->registered); 805 806 /* Check counters */ 807 tt_int_op(test_doesnt_want_writes_count, OP_EQ, 808 init_doesnt_want_writes_count); 809 tt_int_op(test_releases_count, OP_EQ, init_releases_count); 810 811 /* Move ch1 to MAINT */ 812 channel_change_state(ch1, CHANNEL_STATE_MAINT); 813 tt_int_op(test_doesnt_want_writes_count, OP_EQ, 814 init_doesnt_want_writes_count + 1); 815 tt_int_op(test_releases_count, OP_EQ, init_releases_count); 816 817 /* Move ch2 to OPEN */ 818 channel_change_state_open(ch2); 819 tt_int_op(test_doesnt_want_writes_count, OP_EQ, 820 init_doesnt_want_writes_count + 1); 821 tt_int_op(test_releases_count, OP_EQ, init_releases_count); 822 823 /* Move ch1 back to OPEN */ 824 channel_change_state_open(ch1); 825 tt_int_op(test_doesnt_want_writes_count, OP_EQ, 826 init_doesnt_want_writes_count + 1); 827 tt_int_op(test_releases_count, OP_EQ, init_releases_count); 828 829 /* Mark ch2 for close */ 830 channel_mark_for_close(ch2); 831 tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING); 832 tt_int_op(test_doesnt_want_writes_count, OP_EQ, 833 init_doesnt_want_writes_count + 1); 834 tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1); 835 836 /* Shut down channels */ 837 channel_free_all(); 838 ch1 = ch2 = NULL; 839 tt_int_op(test_doesnt_want_writes_count, OP_EQ, 840 init_doesnt_want_writes_count + 1); 841 /* channel_free() calls scheduler_release_channel() */ 842 tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4); 843 844 done: 845 free_fake_channel(ch1); 846 free_fake_channel(ch2); 847 848 UNMOCK(scheduler_channel_doesnt_want_writes); 849 UNMOCK(scheduler_release_channel); 850 } 851 852 /** 853 * Weird channel lifecycle test: 854 * 855 * OPENING->CLOSING->CLOSED 856 * OPENING->OPEN->CLOSING->ERROR 857 * OPENING->OPEN->MAINT->CLOSING->CLOSED 858 * OPENING->OPEN->MAINT->CLOSING->ERROR 859 */ 860 861 static void 862 test_channel_lifecycle_2(void *arg) 863 { 864 channel_t *ch = NULL; 865 866 (void)arg; 867 868 /* Mock these for the whole lifecycle test */ 869 MOCK(scheduler_channel_doesnt_want_writes, 870 scheduler_channel_doesnt_want_writes_mock); 871 MOCK(scheduler_release_channel, 872 scheduler_release_channel_mock); 873 874 /* Accept cells to lower layer */ 875 test_chan_accept_cells = 1; 876 877 ch = new_fake_channel(); 878 tt_assert(ch); 879 /* Start it off in OPENING */ 880 ch->state = CHANNEL_STATE_OPENING; 881 882 /* Try to register it */ 883 channel_register(ch); 884 tt_assert(ch->registered); 885 886 /* Try to close it */ 887 channel_mark_for_close(ch); 888 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 889 890 /* Finish closing it */ 891 chan_test_finish_close(ch); 892 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED); 893 channel_run_cleanup(); 894 ch = NULL; 895 896 /* Now try OPENING->OPEN->CLOSING->ERROR */ 897 ch = new_fake_channel(); 898 tt_assert(ch); 899 ch->state = CHANNEL_STATE_OPENING; 900 channel_register(ch); 901 tt_assert(ch->registered); 902 903 /* Finish opening it */ 904 channel_change_state_open(ch); 905 906 /* Error exit from lower layer */ 907 chan_test_error(ch); 908 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 909 chan_test_finish_close(ch); 910 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR); 911 channel_run_cleanup(); 912 ch = NULL; 913 914 /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */ 915 ch = new_fake_channel(); 916 tt_assert(ch); 917 ch->state = CHANNEL_STATE_OPENING; 918 channel_register(ch); 919 tt_assert(ch->registered); 920 921 /* Finish opening it */ 922 channel_change_state_open(ch); 923 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN); 924 925 /* Go to maintenance state */ 926 channel_change_state(ch, CHANNEL_STATE_MAINT); 927 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT); 928 929 /* Lower layer close */ 930 channel_mark_for_close(ch); 931 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 932 933 /* Finish */ 934 chan_test_finish_close(ch); 935 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED); 936 channel_run_cleanup(); 937 ch = NULL; 938 939 /* 940 * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during 941 * maintenance state 942 */ 943 ch = new_fake_channel(); 944 tt_assert(ch); 945 ch->state = CHANNEL_STATE_OPENING; 946 channel_register(ch); 947 tt_assert(ch->registered); 948 949 /* Finish opening it */ 950 channel_change_state_open(ch); 951 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN); 952 953 /* Go to maintenance state */ 954 channel_change_state(ch, CHANNEL_STATE_MAINT); 955 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT); 956 957 /* Lower layer close */ 958 channel_close_from_lower_layer(ch); 959 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 960 961 /* Finish */ 962 chan_test_finish_close(ch); 963 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED); 964 channel_run_cleanup(); 965 ch = NULL; 966 967 /* OPENING->OPEN->MAINT->CLOSING->ERROR */ 968 ch = new_fake_channel(); 969 tt_assert(ch); 970 ch->state = CHANNEL_STATE_OPENING; 971 channel_register(ch); 972 tt_assert(ch->registered); 973 974 /* Finish opening it */ 975 channel_change_state_open(ch); 976 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN); 977 978 /* Go to maintenance state */ 979 channel_change_state(ch, CHANNEL_STATE_MAINT); 980 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT); 981 982 /* Lower layer close */ 983 chan_test_error(ch); 984 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING); 985 986 /* Finish */ 987 chan_test_finish_close(ch); 988 tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR); 989 channel_run_cleanup(); 990 ch = NULL; 991 992 /* Shut down channels */ 993 channel_free_all(); 994 995 done: 996 tor_free(ch); 997 998 UNMOCK(scheduler_channel_doesnt_want_writes); 999 UNMOCK(scheduler_release_channel); 1000 1001 return; 1002 } 1003 1004 static void 1005 test_channel_id_map(void *arg) 1006 { 1007 (void)arg; 1008 #define N_CHAN 6 1009 char rsa_id[N_CHAN][DIGEST_LEN]; 1010 ed25519_public_key_t *ed_id[N_CHAN]; 1011 channel_t *chan[N_CHAN]; 1012 int i; 1013 ed25519_public_key_t ed_zero; 1014 memset(&ed_zero, 0, sizeof(ed_zero)); 1015 1016 tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C? 1017 1018 for (i = 0; i < N_CHAN; ++i) { 1019 crypto_rand(rsa_id[i], DIGEST_LEN); 1020 ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i])); 1021 crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey)); 1022 } 1023 1024 /* For channel 3, have no Ed identity. */ 1025 tor_free(ed_id[3]); 1026 1027 /* Channel 2 and 4 have same ROSA identity */ 1028 memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN); 1029 1030 /* Channel 2 and 4 and 5 have same RSA identity */ 1031 memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN); 1032 memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN); 1033 1034 /* Channels 2 and 5 have same Ed25519 identity */ 1035 memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2])); 1036 1037 for (i = 0; i < N_CHAN; ++i) { 1038 chan[i] = new_fake_channel(); 1039 channel_register(chan[i]); 1040 channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]); 1041 } 1042 1043 /* Lookup by RSA id only */ 1044 tt_ptr_op(chan[0], OP_EQ, 1045 channel_find_by_remote_identity(rsa_id[0], NULL)); 1046 tt_ptr_op(chan[1], OP_EQ, 1047 channel_find_by_remote_identity(rsa_id[1], NULL)); 1048 tt_ptr_op(chan[3], OP_EQ, 1049 channel_find_by_remote_identity(rsa_id[3], NULL)); 1050 channel_t *ch; 1051 ch = channel_find_by_remote_identity(rsa_id[2], NULL); 1052 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]); 1053 ch = channel_next_with_rsa_identity(ch); 1054 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]); 1055 ch = channel_next_with_rsa_identity(ch); 1056 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]); 1057 ch = channel_next_with_rsa_identity(ch); 1058 tt_ptr_op(ch, OP_EQ, NULL); 1059 1060 /* As above, but with zero Ed25519 ID (meaning "any ID") */ 1061 tt_ptr_op(chan[0], OP_EQ, 1062 channel_find_by_remote_identity(rsa_id[0], &ed_zero)); 1063 tt_ptr_op(chan[1], OP_EQ, 1064 channel_find_by_remote_identity(rsa_id[1], &ed_zero)); 1065 tt_ptr_op(chan[3], OP_EQ, 1066 channel_find_by_remote_identity(rsa_id[3], &ed_zero)); 1067 ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero); 1068 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]); 1069 ch = channel_next_with_rsa_identity(ch); 1070 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]); 1071 ch = channel_next_with_rsa_identity(ch); 1072 tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]); 1073 ch = channel_next_with_rsa_identity(ch); 1074 tt_ptr_op(ch, OP_EQ, NULL); 1075 1076 /* Lookup nonexistent RSA identity */ 1077 tt_ptr_op(NULL, OP_EQ, 1078 channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL)); 1079 1080 /* Look up by full identity pair */ 1081 tt_ptr_op(chan[0], OP_EQ, 1082 channel_find_by_remote_identity(rsa_id[0], ed_id[0])); 1083 tt_ptr_op(chan[1], OP_EQ, 1084 channel_find_by_remote_identity(rsa_id[1], ed_id[1])); 1085 tt_ptr_op(chan[3], OP_EQ, 1086 channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/)); 1087 tt_ptr_op(chan[4], OP_EQ, 1088 channel_find_by_remote_identity(rsa_id[4], ed_id[4])); 1089 ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]); 1090 tt_assert(ch == chan[2] || ch == chan[5]); 1091 1092 /* Look up RSA identity with wrong ed25519 identity */ 1093 tt_ptr_op(NULL, OP_EQ, 1094 channel_find_by_remote_identity(rsa_id[4], ed_id[0])); 1095 tt_ptr_op(NULL, OP_EQ, 1096 channel_find_by_remote_identity(rsa_id[2], ed_id[1])); 1097 tt_ptr_op(NULL, OP_EQ, 1098 channel_find_by_remote_identity(rsa_id[3], ed_id[1])); 1099 1100 done: 1101 for (i = 0; i < N_CHAN; ++i) { 1102 channel_clear_identity_digest(chan[i]); 1103 channel_unregister(chan[i]); 1104 free_fake_channel(chan[i]); 1105 tor_free(ed_id[i]); 1106 } 1107 #undef N_CHAN 1108 } 1109 1110 static void 1111 test_channel_state(void *arg) 1112 { 1113 (void) arg; 1114 1115 /* Test state validity. */ 1116 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1); 1117 tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1); 1118 tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1); 1119 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1); 1120 tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1); 1121 tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1); 1122 tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0); 1123 tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0); 1124 1125 /* Test listener state validity. */ 1126 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED), 1127 OP_EQ, 1); 1128 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING), 1129 OP_EQ, 1); 1130 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING), 1131 OP_EQ, 1); 1132 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR), 1133 OP_EQ, 1); 1134 tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST), 1135 OP_EQ, 0); 1136 tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0); 1137 1138 /* Test state transition. */ 1139 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED, 1140 CHANNEL_STATE_OPENING), OP_EQ, 1); 1141 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED, 1142 CHANNEL_STATE_ERROR), OP_EQ, 0); 1143 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING, 1144 CHANNEL_STATE_ERROR), OP_EQ, 1); 1145 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING, 1146 CHANNEL_STATE_CLOSED), OP_EQ, 1); 1147 tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING, 1148 CHANNEL_STATE_OPEN), OP_EQ, 0); 1149 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT, 1150 CHANNEL_STATE_CLOSING), OP_EQ, 1); 1151 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT, 1152 CHANNEL_STATE_ERROR), OP_EQ, 1); 1153 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT, 1154 CHANNEL_STATE_OPEN), OP_EQ, 1); 1155 tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT, 1156 CHANNEL_STATE_OPENING), OP_EQ, 0); 1157 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING, 1158 CHANNEL_STATE_OPEN), OP_EQ, 1); 1159 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING, 1160 CHANNEL_STATE_CLOSING), OP_EQ, 1); 1161 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING, 1162 CHANNEL_STATE_ERROR), OP_EQ, 1); 1163 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN, 1164 CHANNEL_STATE_ERROR), OP_EQ, 1); 1165 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN, 1166 CHANNEL_STATE_CLOSING), OP_EQ, 1); 1167 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN, 1168 CHANNEL_STATE_ERROR), OP_EQ, 1); 1169 tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN, 1170 CHANNEL_STATE_MAINT), OP_EQ, 1); 1171 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, 1172 CHANNEL_STATE_MAINT), OP_EQ, 0); 1173 tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX), 1174 OP_EQ, 0); 1175 1176 /* Test listener state transition. */ 1177 tt_int_op(channel_listener_state_can_transition( 1178 CHANNEL_LISTENER_STATE_CLOSED, 1179 CHANNEL_LISTENER_STATE_LISTENING), 1180 OP_EQ, 1); 1181 tt_int_op(channel_listener_state_can_transition( 1182 CHANNEL_LISTENER_STATE_CLOSED, 1183 CHANNEL_LISTENER_STATE_ERROR), 1184 OP_EQ, 0); 1185 1186 tt_int_op(channel_listener_state_can_transition( 1187 CHANNEL_LISTENER_STATE_CLOSING, 1188 CHANNEL_LISTENER_STATE_CLOSED), 1189 OP_EQ, 1); 1190 1191 tt_int_op(channel_listener_state_can_transition( 1192 CHANNEL_LISTENER_STATE_CLOSING, 1193 CHANNEL_LISTENER_STATE_ERROR), 1194 OP_EQ, 1); 1195 tt_int_op(channel_listener_state_can_transition( 1196 CHANNEL_LISTENER_STATE_ERROR, 1197 CHANNEL_LISTENER_STATE_CLOSING), 1198 OP_EQ, 0); 1199 1200 tt_int_op(channel_listener_state_can_transition( 1201 CHANNEL_LISTENER_STATE_LISTENING, 1202 CHANNEL_LISTENER_STATE_CLOSING), 1203 OP_EQ, 1); 1204 tt_int_op(channel_listener_state_can_transition( 1205 CHANNEL_LISTENER_STATE_LISTENING, 1206 CHANNEL_LISTENER_STATE_ERROR), 1207 OP_EQ, 1); 1208 tt_int_op(channel_listener_state_can_transition( 1209 CHANNEL_LISTENER_STATE_LAST, 1210 INT_MAX), 1211 OP_EQ, 0); 1212 1213 /* Test state string. */ 1214 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ, 1215 "closing"); 1216 tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ, 1217 "channel error"); 1218 tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ, 1219 "closed"); 1220 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ, 1221 "open"); 1222 tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ, 1223 "opening"); 1224 tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ, 1225 "temporarily suspended for maintenance"); 1226 tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ, 1227 "unknown or invalid channel state"); 1228 tt_str_op(channel_state_to_string(INT_MAX), OP_EQ, 1229 "unknown or invalid channel state"); 1230 1231 /* Test listener state string. */ 1232 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING), 1233 OP_EQ, "closing"); 1234 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR), 1235 OP_EQ, "channel listener error"); 1236 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING), 1237 OP_EQ, "listening"); 1238 tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST), 1239 OP_EQ, "unknown or invalid channel listener state"); 1240 tt_str_op(channel_listener_state_to_string(INT_MAX), 1241 OP_EQ, "unknown or invalid channel listener state"); 1242 1243 done: 1244 ; 1245 } 1246 1247 static networkstatus_t *mock_ns = NULL; 1248 1249 static networkstatus_t * 1250 mock_networkstatus_get_latest_consensus(void) 1251 { 1252 return mock_ns; 1253 } 1254 1255 static void 1256 test_channel_duplicates(void *arg) 1257 { 1258 channel_t *chan = NULL; 1259 routerstatus_t rs; 1260 1261 (void) arg; 1262 1263 setup_full_capture_of_logs(LOG_INFO); 1264 /* Try a flat call with channel nor connections. */ 1265 channel_check_for_duplicates(); 1266 expect_log_msg_containing( 1267 "Found 0 connections to authorities, 0 connections to 0 relays. " 1268 "Found 0 current canonical connections, " 1269 "in 0 of which we were a non-canonical peer. " 1270 "0 relays had more than 1 connection, 0 had more than 2, and " 1271 "0 had more than 4 connections."); 1272 1273 mock_ns = tor_malloc_zero(sizeof(*mock_ns)); 1274 mock_ns->routerstatus_list = smartlist_new(); 1275 MOCK(networkstatus_get_latest_consensus, 1276 mock_networkstatus_get_latest_consensus); 1277 1278 chan = new_fake_channel(); 1279 tt_assert(chan); 1280 chan->is_canonical = test_chan_is_canonical; 1281 memset(chan->identity_digest, 'A', sizeof(chan->identity_digest)); 1282 channel_add_to_digest_map(chan); 1283 tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL), 1284 OP_EQ, chan); 1285 1286 /* No relay has been associated with this channel. */ 1287 channel_check_for_duplicates(); 1288 expect_log_msg_containing( 1289 "Found 0 connections to authorities, 0 connections to 0 relays. " 1290 "Found 0 current canonical connections, " 1291 "in 0 of which we were a non-canonical peer. " 1292 "0 relays had more than 1 connection, 0 had more than 2, and " 1293 "0 had more than 4 connections."); 1294 1295 /* Associate relay to this connection in the consensus. */ 1296 memset(&rs, 0, sizeof(rs)); 1297 memset(rs.identity_digest, 'A', sizeof(rs.identity_digest)); 1298 smartlist_add(mock_ns->routerstatus_list, &rs); 1299 1300 /* Non opened channel. */ 1301 chan->state = CHANNEL_STATE_CLOSING; 1302 channel_check_for_duplicates(); 1303 expect_log_msg_containing( 1304 "Found 0 connections to authorities, 0 connections to 0 relays. " 1305 "Found 0 current canonical connections, " 1306 "in 0 of which we were a non-canonical peer. " 1307 "0 relays had more than 1 connection, 0 had more than 2, and " 1308 "0 had more than 4 connections."); 1309 chan->state = CHANNEL_STATE_OPEN; 1310 1311 channel_check_for_duplicates(); 1312 expect_log_msg_containing( 1313 "Found 0 connections to authorities, 1 connections to 1 relays. " 1314 "Found 0 current canonical connections, " 1315 "in 0 of which we were a non-canonical peer. " 1316 "0 relays had more than 1 connection, 0 had more than 2, and " 1317 "0 had more than 4 connections."); 1318 1319 test_chan_should_be_canonical = 1; 1320 channel_check_for_duplicates(); 1321 expect_log_msg_containing( 1322 "Found 0 connections to authorities, 1 connections to 1 relays. " 1323 "Found 1 current canonical connections, " 1324 "in 1 of which we were a non-canonical peer. " 1325 "0 relays had more than 1 connection, 0 had more than 2, and " 1326 "0 had more than 4 connections."); 1327 teardown_capture_of_logs(); 1328 1329 done: 1330 free_fake_channel(chan); 1331 smartlist_clear(mock_ns->routerstatus_list); 1332 networkstatus_vote_free(mock_ns); 1333 UNMOCK(networkstatus_get_latest_consensus); 1334 } 1335 1336 static void 1337 test_channel_for_extend(void *arg) 1338 { 1339 channel_t *chan1 = NULL, *chan2 = NULL; 1340 channel_t *ret_chan = NULL; 1341 char digest[DIGEST_LEN]; 1342 ed25519_public_key_t ed_id; 1343 tor_addr_t ipv4_addr, ipv6_addr; 1344 const char *msg; 1345 int launch; 1346 time_t now = time(NULL); 1347 1348 (void) arg; 1349 1350 memset(digest, 'A', sizeof(digest)); 1351 memset(&ed_id, 'B', sizeof(ed_id)); 1352 1353 tor_addr_make_null(&ipv4_addr, AF_INET); 1354 tor_addr_make_null(&ipv6_addr, AF_INET6); 1355 1356 chan1 = new_fake_channel(); 1357 tt_assert(chan1); 1358 /* Need to be registered to get added to the id map. */ 1359 channel_register(chan1); 1360 tt_int_op(chan1->registered, OP_EQ, 1); 1361 /* We need those for the test. */ 1362 chan1->is_canonical = test_chan_is_canonical; 1363 chan1->matches_target = test_chan_matches_target; 1364 chan1->timestamp_created = now - 9; 1365 1366 chan2 = new_fake_channel(); 1367 tt_assert(chan2); 1368 /* Need to be registered to get added to the id map. */ 1369 channel_register(chan2); 1370 tt_int_op(chan2->registered, OP_EQ, 1); 1371 /* We need those for the test. */ 1372 chan2->is_canonical = test_chan_is_canonical; 1373 chan2->matches_target = test_chan_matches_target; 1374 /* Make it older than chan1. */ 1375 chan2->timestamp_created = chan1->timestamp_created - 1; 1376 1377 /* Say it's all canonical. */ 1378 test_chan_should_be_canonical = 1; 1379 1380 /* Set channel identities and add it to the channel map. The last one to be 1381 * added is made the first one in the list so the lookup will always return 1382 * that one first. */ 1383 channel_set_identity_digest(chan2, digest, &ed_id); 1384 channel_set_identity_digest(chan1, digest, &ed_id); 1385 tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1); 1386 tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1); 1387 1388 /* The expected result is chan2 because it is older than chan1. */ 1389 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1390 false, &msg, &launch); 1391 tt_assert(ret_chan); 1392 tt_ptr_op(ret_chan, OP_EQ, chan2); 1393 tt_int_op(launch, OP_EQ, 0); 1394 tt_str_op(msg, OP_EQ, "Connection is fine; using it."); 1395 1396 /* Switch that around from previous test. */ 1397 chan2->timestamp_created = chan1->timestamp_created + 1; 1398 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1399 false, &msg, &launch); 1400 tt_assert(ret_chan); 1401 tt_ptr_op(ret_chan, OP_EQ, chan1); 1402 tt_int_op(launch, OP_EQ, 0); 1403 tt_str_op(msg, OP_EQ, "Connection is fine; using it."); 1404 1405 /* Same creation time, num circuits will be used and they both have 0 so the 1406 * channel 2 should be picked due to how channel_is_better() works. */ 1407 chan2->timestamp_created = chan1->timestamp_created; 1408 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1409 false, &msg, &launch); 1410 tt_assert(ret_chan); 1411 tt_ptr_op(ret_chan, OP_EQ, chan1); 1412 tt_int_op(launch, OP_EQ, 0); 1413 tt_str_op(msg, OP_EQ, "Connection is fine; using it."); 1414 1415 /* For the rest of the tests, we need channel 1 to be the older. */ 1416 chan2->timestamp_created = chan1->timestamp_created + 1; 1417 1418 /* Condemned the older channel. */ 1419 chan1->state = CHANNEL_STATE_CLOSING; 1420 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1421 false, &msg, &launch); 1422 tt_assert(ret_chan); 1423 tt_ptr_op(ret_chan, OP_EQ, chan2); 1424 tt_int_op(launch, OP_EQ, 0); 1425 tt_str_op(msg, OP_EQ, "Connection is fine; using it."); 1426 chan1->state = CHANNEL_STATE_OPEN; 1427 1428 /* Make the older channel a client one. */ 1429 channel_mark_client(chan1); 1430 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1431 false, &msg, &launch); 1432 tt_assert(ret_chan); 1433 tt_ptr_op(ret_chan, OP_EQ, chan2); 1434 tt_int_op(launch, OP_EQ, 0); 1435 tt_str_op(msg, OP_EQ, "Connection is fine; using it."); 1436 channel_clear_client(chan1); 1437 1438 /* Non matching ed identity with valid digest. */ 1439 ed25519_public_key_t dumb_ed_id; 1440 memset(&dumb_ed_id, 0, sizeof(dumb_ed_id)); 1441 ret_chan = channel_get_for_extend(digest, &dumb_ed_id, 1442 &ipv4_addr, &ipv6_addr, 1443 false, &msg, &launch); 1444 tt_assert(!ret_chan); 1445 tt_str_op(msg, OP_EQ, "Not connected. Connecting."); 1446 tt_int_op(launch, OP_EQ, 1); 1447 1448 /* Opening channel, we'll check if the target address matches. */ 1449 test_chan_should_match_target = 1; 1450 chan1->state = CHANNEL_STATE_OPENING; 1451 chan2->state = CHANNEL_STATE_OPENING; 1452 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1453 false, &msg, &launch); 1454 tt_assert(!ret_chan); 1455 tt_str_op(msg, OP_EQ, "Connection in progress; waiting."); 1456 tt_int_op(launch, OP_EQ, 0); 1457 chan1->state = CHANNEL_STATE_OPEN; 1458 chan2->state = CHANNEL_STATE_OPEN; 1459 1460 /* Mark channel 1 as bad for circuits. */ 1461 channel_mark_bad_for_new_circs(chan1); 1462 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1463 false, &msg, &launch); 1464 tt_assert(ret_chan); 1465 tt_ptr_op(ret_chan, OP_EQ, chan2); 1466 tt_int_op(launch, OP_EQ, 0); 1467 tt_str_op(msg, OP_EQ, "Connection is fine; using it."); 1468 chan1->is_bad_for_new_circs = 0; 1469 1470 /* Mark both channels as unusable. */ 1471 channel_mark_bad_for_new_circs(chan1); 1472 channel_mark_bad_for_new_circs(chan2); 1473 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1474 false, &msg, &launch); 1475 tt_assert(!ret_chan); 1476 tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. " 1477 " Launching a new one."); 1478 tt_int_op(launch, OP_EQ, 1); 1479 chan1->is_bad_for_new_circs = 0; 1480 chan2->is_bad_for_new_circs = 0; 1481 1482 /* Non canonical channels. */ 1483 test_chan_should_be_canonical = 0; 1484 test_chan_should_match_target = 0; 1485 ret_chan = channel_get_for_extend(digest, &ed_id, &ipv4_addr, &ipv6_addr, 1486 false, &msg, &launch); 1487 tt_assert(!ret_chan); 1488 tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. " 1489 " Launching a new one."); 1490 tt_int_op(launch, OP_EQ, 1); 1491 1492 done: 1493 free_fake_channel(chan1); 1494 free_fake_channel(chan2); 1495 } 1496 1497 static void 1498 test_channel_listener(void *arg) 1499 { 1500 int old_count; 1501 time_t now = time(NULL); 1502 channel_listener_t *chan = NULL; 1503 1504 (void) arg; 1505 1506 chan = tor_malloc_zero(sizeof(*chan)); 1507 tt_assert(chan); 1508 channel_init_listener(chan); 1509 tt_u64_op(chan->global_identifier, OP_EQ, 1); 1510 tt_int_op(chan->timestamp_created, OP_GE, now); 1511 chan->close = test_chan_listener_close; 1512 1513 /* Register it. At this point, it is not open so it will be put in the 1514 * finished list. */ 1515 channel_listener_register(chan); 1516 tt_int_op(chan->registered, OP_EQ, 1); 1517 channel_listener_unregister(chan); 1518 1519 /* Register it as listening now thus active. */ 1520 chan->state = CHANNEL_LISTENER_STATE_LISTENING; 1521 channel_listener_register(chan); 1522 tt_int_op(chan->registered, OP_EQ, 1); 1523 1524 /* Set the listener function. */ 1525 channel_listener_set_listener_fn(chan, test_chan_listener_fn); 1526 tt_ptr_op(chan->listener, OP_EQ, test_chan_listener_fn); 1527 1528 /* Put a channel in the listener incoming list and queue it. 1529 * function. By doing this, the listener() handler will be called. */ 1530 channel_t *in_chan = new_fake_channel(); 1531 old_count = test_chan_listener_fn_called; 1532 channel_listener_queue_incoming(chan, in_chan); 1533 free_fake_channel(in_chan); 1534 tt_int_op(test_chan_listener_fn_called, OP_EQ, old_count + 1); 1535 1536 /* Put listener channel in CLOSING state. */ 1537 old_count = test_chan_listener_close_fn_called; 1538 channel_listener_mark_for_close(chan); 1539 tt_int_op(test_chan_listener_close_fn_called, OP_EQ, old_count + 1); 1540 channel_listener_change_state(chan, CHANNEL_LISTENER_STATE_CLOSED); 1541 1542 /* Dump stats so we at least hit the code path. */ 1543 chan->describe_transport = test_chan_listener_describe_transport; 1544 /* There is a check for "now > timestamp_created" when dumping the stats so 1545 * make sure we go in. */ 1546 chan->timestamp_created = now - 10; 1547 channel_listener_dump_statistics(chan, LOG_INFO); 1548 1549 done: 1550 if (chan) { 1551 channel_listener_unregister(chan); 1552 tor_free(chan); 1553 } 1554 channel_free_all(); 1555 } 1556 1557 #define TEST_SETUP_MATCHES_ADDR(orcon, addr, src, rv) STMT_BEGIN \ 1558 rv = tor_inet_pton(addr.family, src, &addr.addr); \ 1559 tt_int_op(rv, OP_EQ, 1); \ 1560 orcon->base_.addr = addr; \ 1561 STMT_END; 1562 1563 #define TEST_MATCHES_ADDR(chan, addr4, addr6, rv, exp) STMT_BEGIN \ 1564 rv = channel_matches_target_addr_for_extend(chan, addr4, addr6); \ 1565 tt_int_op(rv, OP_EQ, exp); \ 1566 STMT_END; 1567 1568 static void 1569 test_channel_matches_target_addr_for_extend(void *arg) 1570 { 1571 (void) arg; 1572 1573 channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan)); 1574 or_connection_t *orcon = tor_malloc_zero(sizeof(*orcon)); 1575 channel_t *chan = &(tlschan->base_); 1576 tor_addr_t addr; 1577 int rv; 1578 1579 tlschan->conn = orcon; 1580 channel_tls_common_init(tlschan); 1581 1582 /* Test for IPv4 addresses. */ 1583 addr.family = AF_INET; 1584 TEST_SETUP_MATCHES_ADDR(orcon, addr, "1.2.3.4", rv); 1585 TEST_MATCHES_ADDR(chan, &addr, NULL, rv, 1); 1586 1587 tor_inet_pton(addr.family, "2.5.3.4", &addr.addr); 1588 TEST_MATCHES_ADDR(chan, &addr, NULL, rv, 0); 1589 1590 /* Test for IPv6 addresses. */ 1591 addr.family = AF_INET6; 1592 TEST_SETUP_MATCHES_ADDR(orcon, addr, "3:4:7:1:9:8:09:10", rv); 1593 TEST_MATCHES_ADDR(chan, NULL, &addr, rv, 1); 1594 1595 tor_inet_pton(addr.family, "::", &addr.addr); 1596 TEST_MATCHES_ADDR(chan, NULL, &addr, rv, 0); 1597 1598 done: 1599 circuitmux_clear_policy(chan->cmux); 1600 circuitmux_free(chan->cmux); 1601 tor_free(orcon); 1602 tor_free(tlschan); 1603 } 1604 1605 struct testcase_t channel_tests[] = { 1606 { "inbound_cell", test_channel_inbound_cell, TT_FORK, 1607 NULL, NULL }, 1608 { "outbound_cell", test_channel_outbound_cell, TT_FORK, 1609 NULL, NULL }, 1610 { "id_map", test_channel_id_map, TT_FORK, 1611 NULL, NULL }, 1612 { "lifecycle", test_channel_lifecycle, TT_FORK, 1613 NULL, NULL }, 1614 { "lifecycle_2", test_channel_lifecycle_2, TT_FORK, 1615 NULL, NULL }, 1616 { "dumpstats", test_channel_dumpstats, TT_FORK, 1617 NULL, NULL }, 1618 { "state", test_channel_state, TT_FORK, 1619 NULL, NULL }, 1620 { "duplicates", test_channel_duplicates, TT_FORK, 1621 NULL, NULL }, 1622 { "get_channel_for_extend", test_channel_for_extend, TT_FORK, 1623 NULL, NULL }, 1624 { "listener", test_channel_listener, TT_FORK, 1625 NULL, NULL }, 1626 { "matches_target", test_channel_matches_target_addr_for_extend, TT_FORK, 1627 NULL, NULL }, 1628 END_OF_TESTCASES 1629 };