test_relay.c 3.7 KB

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