test_circuitstats.c 6.7 KB

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