test_circuitmux.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (c) 2013-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define CIRCUITMUX_PRIVATE
  5. #define CIRCUITMUX_EWMA_PRIVATE
  6. #define RELAY_PRIVATE
  7. #include "or/or.h"
  8. #include "or/channel.h"
  9. #include "or/circuitmux.h"
  10. #include "or/circuitmux_ewma.h"
  11. #include "or/relay.h"
  12. #include "or/scheduler.h"
  13. #include "test/test.h"
  14. #include "or/destroy_cell_queue_st.h"
  15. /* XXXX duplicated function from test_circuitlist.c */
  16. static channel_t *
  17. new_fake_channel(void)
  18. {
  19. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  20. channel_init(chan);
  21. return chan;
  22. }
  23. static int
  24. has_queued_writes(channel_t *c)
  25. {
  26. (void) c;
  27. return 1;
  28. }
  29. /** Test destroy cell queue with no interference from other queues. */
  30. static void
  31. test_cmux_destroy_cell_queue(void *arg)
  32. {
  33. circuitmux_t *cmux = NULL;
  34. channel_t *ch = NULL;
  35. circuit_t *circ = NULL;
  36. destroy_cell_queue_t *cq = NULL;
  37. packed_cell_t *pc = NULL;
  38. destroy_cell_t *dc = NULL;
  39. scheduler_init();
  40. (void) arg;
  41. cmux = circuitmux_alloc();
  42. tt_assert(cmux);
  43. ch = new_fake_channel();
  44. circuitmux_set_policy(cmux, &ewma_policy);
  45. ch->has_queued_writes = has_queued_writes;
  46. ch->wide_circ_ids = 1;
  47. circ = circuitmux_get_first_active_circuit(cmux, &cq);
  48. tt_ptr_op(circ, OP_EQ, NULL);
  49. tt_ptr_op(cq, OP_EQ, NULL);
  50. circuitmux_append_destroy_cell(ch, cmux, 100, 10);
  51. circuitmux_append_destroy_cell(ch, cmux, 190, 6);
  52. circuitmux_append_destroy_cell(ch, cmux, 30, 1);
  53. tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 3);
  54. circ = circuitmux_get_first_active_circuit(cmux, &cq);
  55. tt_ptr_op(circ, OP_EQ, NULL);
  56. tt_assert(cq);
  57. tt_int_op(cq->n, OP_EQ, 3);
  58. dc = destroy_cell_queue_pop(cq);
  59. tt_assert(dc);
  60. tt_uint_op(dc->circid, OP_EQ, 100);
  61. tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 2);
  62. done:
  63. circuitmux_free(cmux);
  64. channel_free(ch);
  65. packed_cell_free(pc);
  66. tor_free(dc);
  67. }
  68. static void
  69. test_cmux_compute_ticks(void *arg)
  70. {
  71. const int64_t NS_PER_S = 1000 * 1000 * 1000;
  72. const int64_t START_NS = UINT64_C(1217709000)*NS_PER_S;
  73. int64_t now;
  74. double rem;
  75. unsigned tick;
  76. (void)arg;
  77. circuitmux_ewma_free_all();
  78. monotime_enable_test_mocking();
  79. monotime_coarse_set_mock_time_nsec(START_NS);
  80. cell_ewma_initialize_ticks();
  81. const unsigned tick_zero = cell_ewma_get_current_tick_and_fraction(&rem);
  82. tt_double_op(rem, OP_GT, -1e-9);
  83. tt_double_op(rem, OP_LT, 1e-9);
  84. /* 1.5 second later and we should still be in the same tick. */
  85. now = START_NS + NS_PER_S + NS_PER_S/2;
  86. monotime_coarse_set_mock_time_nsec(now);
  87. tick = cell_ewma_get_current_tick_and_fraction(&rem);
  88. tt_uint_op(tick, OP_EQ, tick_zero);
  89. tt_double_op(rem, OP_GT, .149999999);
  90. tt_double_op(rem, OP_LT, .150000001);
  91. /* 25 second later and we should be in another tick. */
  92. now = START_NS + NS_PER_S * 25;
  93. monotime_coarse_set_mock_time_nsec(now);
  94. tick = cell_ewma_get_current_tick_and_fraction(&rem);
  95. tt_uint_op(tick, OP_EQ, tick_zero + 2);
  96. tt_double_op(rem, OP_GT, .499999999);
  97. tt_double_op(rem, OP_LT, .500000001);
  98. done:
  99. ;
  100. }
  101. struct testcase_t circuitmux_tests[] = {
  102. { "destroy_cell_queue", test_cmux_destroy_cell_queue, TT_FORK, NULL, NULL },
  103. { "compute_ticks", test_cmux_compute_ticks, TT_FORK, NULL, NULL },
  104. END_OF_TESTCASES
  105. };