test_circuitmux.c 3.3 KB

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