test_relay.c 3.7 KB

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