test_relay.c 3.8 KB

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