test_circuitstats.c 6.5 KB

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