test_circuitmux.c 3.2 KB

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