test_relay.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #define CIRCUITBUILD_PRIVATE
  5. #include "circuitbuild.h"
  6. #define RELAY_PRIVATE
  7. #include "relay.h"
  8. /* For init/free stuff */
  9. #include "scheduler.h"
  10. /* Test suite stuff */
  11. #include "test.h"
  12. #include "fakechans.h"
  13. static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
  14. static void test_relay_append_cell_to_circuit_queue(void *arg);
  15. static or_circuit_t *
  16. new_fake_orcirc(channel_t *nchan, channel_t *pchan)
  17. {
  18. or_circuit_t *orcirc = NULL;
  19. circuit_t *circ = NULL;
  20. orcirc = tor_malloc_zero(sizeof(*orcirc));
  21. circ = &(orcirc->base_);
  22. circ->magic = OR_CIRCUIT_MAGIC;
  23. circ->n_chan = nchan;
  24. circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
  25. circ->n_mux = NULL; /* ?? */
  26. cell_queue_init(&(circ->n_chan_cells));
  27. circ->n_hop = NULL;
  28. circ->streams_blocked_on_n_chan = 0;
  29. circ->streams_blocked_on_p_chan = 0;
  30. circ->n_delete_pending = 0;
  31. circ->p_delete_pending = 0;
  32. circ->received_destroy = 0;
  33. circ->state = CIRCUIT_STATE_OPEN;
  34. circ->purpose = CIRCUIT_PURPOSE_OR;
  35. circ->package_window = CIRCWINDOW_START_MAX;
  36. circ->deliver_window = CIRCWINDOW_START_MAX;
  37. circ->n_chan_create_cell = NULL;
  38. orcirc->p_chan = pchan;
  39. orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
  40. cell_queue_init(&(orcirc->p_chan_cells));
  41. return orcirc;
  42. }
  43. static void
  44. test_relay_append_cell_to_circuit_queue(void *arg)
  45. {
  46. channel_t *nchan = NULL, *pchan = NULL;
  47. or_circuit_t *orcirc = NULL;
  48. cell_t *cell = NULL;
  49. int old_count, new_count;
  50. (void)arg;
  51. /* Make fake channels to be nchan and pchan for the circuit */
  52. nchan = new_fake_channel();
  53. tt_assert(nchan);
  54. pchan = new_fake_channel();
  55. tt_assert(pchan);
  56. /* Make a fake orcirc */
  57. orcirc = new_fake_orcirc(nchan, pchan);
  58. tt_assert(orcirc);
  59. circuitmux_attach_circuit(nchan->cmux, TO_CIRCUIT(orcirc),
  60. CELL_DIRECTION_OUT);
  61. circuitmux_attach_circuit(pchan->cmux, TO_CIRCUIT(orcirc),
  62. CELL_DIRECTION_IN);
  63. /* Make a cell */
  64. cell = tor_malloc_zero(sizeof(cell_t));
  65. make_fake_cell(cell);
  66. MOCK(scheduler_channel_has_waiting_cells,
  67. scheduler_channel_has_waiting_cells_mock);
  68. /* Append it */
  69. old_count = get_mock_scheduler_has_waiting_cells_count();
  70. append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), nchan, cell,
  71. CELL_DIRECTION_OUT, 0);
  72. new_count = get_mock_scheduler_has_waiting_cells_count();
  73. tt_int_op(new_count, OP_EQ, old_count + 1);
  74. /* Now try the reverse direction */
  75. old_count = get_mock_scheduler_has_waiting_cells_count();
  76. append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), pchan, cell,
  77. CELL_DIRECTION_IN, 0);
  78. new_count = get_mock_scheduler_has_waiting_cells_count();
  79. tt_int_op(new_count, OP_EQ, old_count + 1);
  80. UNMOCK(scheduler_channel_has_waiting_cells);
  81. /* Get rid of the fake channels */
  82. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  83. channel_mark_for_close(nchan);
  84. channel_mark_for_close(pchan);
  85. UNMOCK(scheduler_release_channel);
  86. /* Shut down channels */
  87. channel_free_all();
  88. done:
  89. tor_free(cell);
  90. if (orcirc) {
  91. circuitmux_detach_circuit(nchan->cmux, TO_CIRCUIT(orcirc));
  92. circuitmux_detach_circuit(pchan->cmux, TO_CIRCUIT(orcirc));
  93. cell_queue_clear(&orcirc->base_.n_chan_cells);
  94. cell_queue_clear(&orcirc->p_chan_cells);
  95. }
  96. tor_free(orcirc);
  97. free_fake_channel(nchan);
  98. free_fake_channel(pchan);
  99. return;
  100. }
  101. struct testcase_t relay_tests[] = {
  102. { "append_cell_to_circuit_queue", test_relay_append_cell_to_circuit_queue,
  103. TT_FORK, NULL, NULL },
  104. END_OF_TESTCASES
  105. };