test_circuitstats.c 6.5 KB

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