test_circuitstats.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. void test_circuitstats_timeout(void *arg);
  18. void test_circuitstats_hoplen(void *arg);
  19. origin_circuit_t *subtest_fourhop_circuit(struct timeval, int);
  20. origin_circuit_t *add_opened_threehop(void);
  21. origin_circuit_t *build_unopened_fourhop(struct timeval);
  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. tor_free(fakehop);
  70. return or_circ;
  71. }
  72. origin_circuit_t *
  73. subtest_fourhop_circuit(struct timeval circ_start_time, int should_timeout)
  74. {
  75. origin_circuit_t *or_circ = build_unopened_fourhop(circ_start_time);
  76. // Now make them open one at a time and call
  77. // circuit_build_times_handle_completed_hop();
  78. or_circ->cpath->state = CPATH_STATE_OPEN;
  79. circuit_build_times_handle_completed_hop(or_circ);
  80. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 0);
  81. or_circ->cpath->next->state = CPATH_STATE_OPEN;
  82. circuit_build_times_handle_completed_hop(or_circ);
  83. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 0);
  84. // Third hop: We should count it now.
  85. or_circ->cpath->next->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,
  88. !should_timeout); // 1 if counted, 0 otherwise
  89. // Fourth hop: Don't double count
  90. or_circ->cpath->next->next->next->state = CPATH_STATE_OPEN;
  91. circuit_build_times_handle_completed_hop(or_circ);
  92. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ,
  93. !should_timeout);
  94. done:
  95. return or_circ;
  96. }
  97. void
  98. test_circuitstats_hoplen(void *arg)
  99. {
  100. /* Plan:
  101. * 0. Test no other opened circs (relaxed timeout)
  102. * 1. Check >3 hop circ building w/o timeout
  103. * 2. Check >3 hop circs w/ timeouts..
  104. */
  105. struct timeval circ_start_time;
  106. origin_circuit_t *threehop = NULL;
  107. origin_circuit_t *fourhop = NULL;
  108. (void)arg;
  109. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  110. circuit_build_times_init(get_circuit_build_times_mutable());
  111. // Let's set a close_ms to 2X the initial timeout, so we can
  112. // test relaxed functionality (which uses the close_ms timeout)
  113. get_circuit_build_times_mutable()->close_ms *= 2;
  114. tor_gettimeofday(&circ_start_time);
  115. circ_start_time.tv_sec -= 119; // make us hit "relaxed" cutoff
  116. // Test 1: Build a fourhop circuit that should get marked
  117. // as relaxed and eventually counted by circuit_expire_building
  118. // (but not before)
  119. fourhop = subtest_fourhop_circuit(circ_start_time, 0);
  120. tt_int_op(fourhop->relaxed_timeout, OP_EQ, 0);
  121. tt_int_op(marked_for_close, OP_EQ, 0);
  122. circuit_expire_building();
  123. tt_int_op(marked_for_close, OP_EQ, 0);
  124. tt_int_op(fourhop->relaxed_timeout, OP_EQ, 1);
  125. TO_CIRCUIT(fourhop)->timestamp_began.tv_sec -= 119;
  126. circuit_expire_building();
  127. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  128. tt_int_op(marked_for_close, OP_EQ, 1);
  129. circuit_free_(TO_CIRCUIT(fourhop));
  130. circuit_build_times_reset(get_circuit_build_times_mutable());
  131. // Test 2: Add a threehop circuit for non-relaxed timeouts
  132. threehop = add_opened_threehop();
  133. /* This circuit should not timeout */
  134. tor_gettimeofday(&circ_start_time);
  135. circ_start_time.tv_sec -= 59;
  136. fourhop = subtest_fourhop_circuit(circ_start_time, 0);
  137. circuit_expire_building();
  138. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  139. tt_int_op(TO_CIRCUIT(fourhop)->purpose, OP_NE,
  140. CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  141. circuit_free_((circuit_t *)fourhop);
  142. circuit_build_times_reset(get_circuit_build_times_mutable());
  143. /* Test 3: This circuit should now time out and get marked as a
  144. * measurement circuit, but still get counted (and counted only once)
  145. */
  146. circ_start_time.tv_sec -= 2;
  147. fourhop = subtest_fourhop_circuit(circ_start_time, 0);
  148. tt_int_op(TO_CIRCUIT(fourhop)->purpose, OP_EQ,
  149. CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  150. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  151. circuit_expire_building();
  152. tt_int_op(get_circuit_build_times()->total_build_times, OP_EQ, 1);
  153. done:
  154. UNMOCK(circuit_mark_for_close_);
  155. circuit_free_(TO_CIRCUIT(threehop));
  156. circuit_free_(TO_CIRCUIT(fourhop));
  157. circuit_build_times_free_timeouts(get_circuit_build_times_mutable());
  158. }
  159. #define TEST_CIRCUITSTATS(name, flags) \
  160. { #name, test_##name, (flags), NULL, NULL }
  161. struct testcase_t circuitstats_tests[] = {
  162. TEST_CIRCUITSTATS(circuitstats_hoplen, TT_FORK),
  163. END_OF_TESTCASES
  164. };