test_circuitstats.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CIRCUITBUILD_PRIVATE
  4. #define CIRCUITSTATS_PRIVATE
  5. #define CHANNEL_PRIVATE_
  6. #include "or.h"
  7. #include "test.h"
  8. #include "test_helpers.h"
  9. #include "log_test_helpers.h"
  10. #include "config.h"
  11. #include "circuitlist.h"
  12. #include "circuitbuild.h"
  13. #include "circuitstats.h"
  14. #include "circuituse.h"
  15. #include "channel.h"
  16. void test_circuitstats_timeout(void *arg);
  17. void test_circuitstats_hoplen(void *arg);
  18. origin_circuit_t *subtest_fourhop_circuit(struct timeval, int);
  19. origin_circuit_t *add_opened_threehop(void);
  20. origin_circuit_t *build_unopened_fourhop(struct timeval);
  21. void circuit_free(circuit_t *circ);
  22. int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
  23. static int marked_for_close;
  24. /* Mock function because we are not trying to test the close circuit that does
  25. * an awful lot of checks on the circuit object. */
  26. static void
  27. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  28. const char *file)
  29. {
  30. (void) circ;
  31. (void) reason;
  32. (void) line;
  33. (void) file;
  34. marked_for_close = 1;
  35. return;
  36. }
  37. origin_circuit_t *
  38. add_opened_threehop(void)
  39. {
  40. origin_circuit_t *or_circ = origin_circuit_new();
  41. extend_info_t fakehop;
  42. memset(&fakehop, 0, sizeof(fakehop));
  43. TO_CIRCUIT(or_circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  44. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  45. or_circ->build_state->desired_path_len = DEFAULT_ROUTE_LEN;
  46. onion_append_hop(&or_circ->cpath, &fakehop);
  47. onion_append_hop(&or_circ->cpath, &fakehop);
  48. onion_append_hop(&or_circ->cpath, &fakehop);
  49. or_circ->has_opened = 1;
  50. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  51. TO_CIRCUIT(or_circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  52. return or_circ;
  53. }
  54. origin_circuit_t *
  55. build_unopened_fourhop(struct timeval circ_start_time)
  56. {
  57. origin_circuit_t *or_circ = origin_circuit_new();
  58. extend_info_t *fakehop = tor_malloc_zero(sizeof(extend_info_t));
  59. memset(fakehop, 0, sizeof(extend_info_t));
  60. TO_CIRCUIT(or_circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  61. TO_CIRCUIT(or_circ)->timestamp_began = circ_start_time;
  62. TO_CIRCUIT(or_circ)->timestamp_created = circ_start_time;
  63. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  64. or_circ->build_state->desired_path_len = 4;
  65. onion_append_hop(&or_circ->cpath, fakehop);
  66. onion_append_hop(&or_circ->cpath, fakehop);
  67. onion_append_hop(&or_circ->cpath, fakehop);
  68. onion_append_hop(&or_circ->cpath, fakehop);
  69. return or_circ;
  70. }
  71. origin_circuit_t *
  72. subtest_fourhop_circuit(struct timeval circ_start_time, int should_timeout)
  73. {
  74. origin_circuit_t *or_circ = build_unopened_fourhop(circ_start_time);
  75. // Now make them open one at a time and call
  76. // circuit_build_times_handle_completed_hop();
  77. or_circ->cpath->state = CPATH_STATE_OPEN;
  78. circuit_build_times_handle_completed_hop(or_circ);
  79. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 0);
  80. or_circ->cpath->next->state = CPATH_STATE_OPEN;
  81. circuit_build_times_handle_completed_hop(or_circ);
  82. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 0);
  83. // Third hop: We should count it now.
  84. or_circ->cpath->next->next->state = CPATH_STATE_OPEN;
  85. circuit_build_times_handle_completed_hop(or_circ);
  86. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ,
  87. !should_timeout); // 1 if counted, 0 otherwise
  88. // Fourth hop: Don't double count
  89. or_circ->cpath->next->next->next->state = CPATH_STATE_OPEN;
  90. circuit_build_times_handle_completed_hop(or_circ);
  91. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ,
  92. !should_timeout);
  93. done:
  94. return or_circ;
  95. }
  96. void
  97. test_circuitstats_hoplen(void *arg)
  98. {
  99. /* Plan:
  100. * 0. Test no other opened circs (relaxed timeout)
  101. * 1. Check >3 hop circ building w/o timeout
  102. * 2. Check >3 hop circs w/ timeouts..
  103. */
  104. struct timeval circ_start_time;
  105. origin_circuit_t *threehop = NULL;
  106. origin_circuit_t *fourhop = NULL;
  107. (void)arg;
  108. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  109. circuit_build_times_init(get_circuit_build_times_mutable());
  110. // Let's set a close_ms to 2X the initial timeout, so we can
  111. // test relaxed functionality (which uses the close_ms timeout)
  112. get_circuit_build_times_mutable()->close_ms *= 2;
  113. tor_gettimeofday(&circ_start_time);
  114. circ_start_time.tv_sec -= 119; // make us hit "relaxed" cutoff
  115. // Test 1: Build a fourhop circuit that should get marked
  116. // as relaxed and eventually counted by circuit_expire_building
  117. // (but not before)
  118. fourhop = subtest_fourhop_circuit(circ_start_time, 0);
  119. tt_int_op(fourhop->relaxed_timeout, OP_EQ, 0);
  120. tt_int_op(marked_for_close, OP_EQ, 0);
  121. circuit_expire_building();
  122. tt_int_op(marked_for_close, OP_EQ, 0);
  123. tt_int_op(fourhop->relaxed_timeout, OP_EQ, 1);
  124. TO_CIRCUIT(fourhop)->timestamp_began.tv_sec -= 119;
  125. circuit_expire_building();
  126. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  127. tt_int_op(marked_for_close, OP_EQ, 1);
  128. circuit_free(TO_CIRCUIT(fourhop));
  129. circuit_build_times_reset(get_circuit_build_times_mutable());
  130. // Test 2: Add a threehop circuit for non-relaxed timeouts
  131. threehop = add_opened_threehop();
  132. /* This circuit should not timeout */
  133. tor_gettimeofday(&circ_start_time);
  134. circ_start_time.tv_sec -= 59;
  135. fourhop = subtest_fourhop_circuit(circ_start_time, 0);
  136. circuit_expire_building();
  137. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  138. tt_int_op(TO_CIRCUIT(fourhop)->purpose, OP_NE,
  139. CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  140. circuit_free((circuit_t *)fourhop);
  141. circuit_build_times_reset(get_circuit_build_times_mutable());
  142. /* Test 3: This circuit should now time out and get marked as a
  143. * measurement circuit, but still get counted (and counted only once)
  144. */
  145. circ_start_time.tv_sec -= 2;
  146. fourhop = subtest_fourhop_circuit(circ_start_time, 0);
  147. tt_int_op(TO_CIRCUIT(fourhop)->purpose, OP_EQ,
  148. CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  149. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  150. circuit_expire_building();
  151. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  152. done:
  153. UNMOCK(circuit_mark_for_close_);
  154. circuit_free(TO_CIRCUIT(threehop));
  155. circuit_free(TO_CIRCUIT(fourhop));
  156. circuit_build_times_free_timeouts(get_circuit_build_times_mutable());
  157. }
  158. #define TEST_CIRCUITSTATS(name, flags) \
  159. { #name, test_##name, (flags), NULL, NULL }
  160. struct testcase_t circuitstats_tests[] = {
  161. TEST_CIRCUITSTATS(circuitstats_hoplen, TT_FORK),
  162. END_OF_TESTCASES
  163. };