test_circuituse.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define CIRCUITLIST_PRIVATE
  6. #include "core/or/or.h"
  7. #include "test/test.h"
  8. #include "test/test_helpers.h"
  9. #include "app/config/config.h"
  10. #include "core/or/circuitlist.h"
  11. #include "core/or/circuituse.h"
  12. #include "core/or/circuitbuild.h"
  13. #include "feature/nodelist/nodelist.h"
  14. #include "core/or/cpath_build_state_st.h"
  15. #include "core/or/origin_circuit_st.h"
  16. static void
  17. test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg)
  18. {
  19. (void)arg;
  20. circuit_t *circ = tor_malloc(sizeof(circuit_t));
  21. circ->marked_for_close = 1;
  22. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  23. done:
  24. tor_free(circ);
  25. }
  26. static void
  27. test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg)
  28. {
  29. (void)arg;
  30. circuit_t *circ = tor_malloc(sizeof(circuit_t));
  31. circ->timestamp_dirty = 1;
  32. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  33. done:
  34. tor_free(circ);
  35. }
  36. static void
  37. test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg)
  38. {
  39. (void)arg;
  40. circuit_t *circ = tor_malloc(sizeof(circuit_t));
  41. circ->purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
  42. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  43. done:
  44. tor_free(circ);
  45. }
  46. static void
  47. test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg)
  48. {
  49. (void)arg;
  50. circuit_t *circ = tor_malloc(sizeof(circuit_t));
  51. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  52. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  53. done:
  54. tor_free(circ);
  55. }
  56. static void
  57. test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg)
  58. {
  59. (void)arg;
  60. circuit_t *circ = tor_malloc(sizeof(circuit_t));
  61. circ->purpose = CIRCUIT_PURPOSE_OR;
  62. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  63. done:
  64. tor_free(circ);
  65. }
  66. static void
  67. test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg)
  68. {
  69. (void)arg;
  70. circuit_t *circ = dummy_origin_circuit_new(30);
  71. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  72. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  73. done:
  74. circuit_free(circ);
  75. }
  76. static void
  77. test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg)
  78. {
  79. (void)arg;
  80. circuit_t *circ = dummy_origin_circuit_new(30);
  81. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  82. oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  83. oc->build_state->onehop_tunnel = 1;
  84. tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  85. done:
  86. circuit_free(circ);
  87. }
  88. static void
  89. test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg)
  90. {
  91. (void)arg;
  92. circuit_t *circ = dummy_origin_circuit_new(30);
  93. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  94. oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  95. oc->build_state->onehop_tunnel = 0;
  96. tt_int_op(1, OP_EQ, circuit_is_available_for_use(circ));
  97. done:
  98. circuit_free(circ);
  99. }
  100. static int
  101. mock_circuit_all_predicted_ports_handled(time_t now,
  102. int *need_uptime,
  103. int *need_capacity)
  104. {
  105. (void)now;
  106. if (need_uptime && need_capacity)
  107. return 0;
  108. return 1;
  109. }
  110. static consensus_path_type_t
  111. mock_router_have_unknown_consensus_path(void)
  112. {
  113. return CONSENSUS_PATH_UNKNOWN;
  114. }
  115. static consensus_path_type_t
  116. mock_router_have_exit_consensus_path(void)
  117. {
  118. return CONSENSUS_PATH_EXIT;
  119. }
  120. static void
  121. test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg)
  122. {
  123. (void)arg;
  124. MOCK(circuit_all_predicted_ports_handled,
  125. mock_circuit_all_predicted_ports_handled);
  126. int needs_uptime = 1;
  127. int needs_capacity = 0;
  128. time_t now = time(NULL);
  129. tt_int_op(0, OP_EQ,
  130. needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  131. done:
  132. UNMOCK(circuit_all_predicted_ports_handled);
  133. }
  134. static void
  135. test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg)
  136. {
  137. (void)arg;
  138. MOCK(circuit_all_predicted_ports_handled,
  139. mock_circuit_all_predicted_ports_handled);
  140. int needs_uptime = 1;
  141. int needs_capacity = 1;
  142. MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  143. time_t now = time(NULL);
  144. tt_int_op(0, OP_EQ,
  145. needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  146. done:
  147. UNMOCK(circuit_all_predicted_ports_handled);
  148. UNMOCK(router_have_consensus_path);
  149. }
  150. static void
  151. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
  152. {
  153. (void)arg;
  154. MOCK(circuit_all_predicted_ports_handled,
  155. mock_circuit_all_predicted_ports_handled);
  156. int needs_uptime = 1;
  157. int needs_capacity = 1;
  158. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  159. time_t now = time(NULL);
  160. tt_int_op(1, OP_EQ,
  161. needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  162. done:
  163. UNMOCK(circuit_all_predicted_ports_handled);
  164. UNMOCK(router_have_consensus_path);
  165. }
  166. static void
  167. test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
  168. {
  169. (void)arg;
  170. MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  171. tt_int_op(0, OP_EQ, needs_circuits_for_build(0));
  172. done: ;
  173. }
  174. static void
  175. test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
  176. {
  177. (void)arg;
  178. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  179. tt_int_op(0, OP_EQ, needs_circuits_for_build(13));
  180. done:
  181. UNMOCK(router_have_consensus_path);
  182. }
  183. static void
  184. test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
  185. {
  186. (void)arg;
  187. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  188. tt_int_op(1, OP_EQ, needs_circuits_for_build(0));
  189. done:
  190. UNMOCK(router_have_consensus_path);
  191. }
  192. struct testcase_t circuituse_tests[] = {
  193. { "marked",
  194. test_circuit_is_available_for_use_ret_false_when_marked_for_close,
  195. TT_FORK, NULL, NULL
  196. },
  197. { "timestamp",
  198. test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
  199. TT_FORK, NULL, NULL
  200. },
  201. { "non_general",
  202. test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
  203. TT_FORK, NULL, NULL
  204. },
  205. { "non_general",
  206. test_circuit_is_available_for_use_ret_false_for_non_general_origin,
  207. TT_FORK, NULL, NULL
  208. },
  209. { "origin",
  210. test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
  211. TT_FORK, NULL, NULL
  212. },
  213. { "clean",
  214. test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
  215. TT_FORK, NULL, NULL
  216. },
  217. { "onehop",
  218. test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
  219. TT_FORK, NULL, NULL
  220. },
  221. { "clean_circ",
  222. test_circuit_is_available_for_use_returns_true_for_clean_circuit,
  223. TT_FORK, NULL, NULL
  224. },
  225. { "exit_f",
  226. test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
  227. TT_FORK, NULL, NULL
  228. },
  229. { "exit_t",
  230. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  231. TT_FORK, NULL, NULL
  232. },
  233. { "non_exit",
  234. test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
  235. TT_FORK, NULL, NULL
  236. },
  237. { "true",
  238. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  239. TT_FORK, NULL, NULL
  240. },
  241. { "consensus_path_unknown",
  242. test_needs_circuits_for_build_ret_false_consensus_path_unknown,
  243. TT_FORK, NULL, NULL
  244. },
  245. { "less_than_max",
  246. test_needs_circuits_for_build_ret_false_if_num_less_than_max,
  247. TT_FORK, NULL, NULL
  248. },
  249. { "more_needed",
  250. test_needs_circuits_for_build_returns_true_when_more_are_needed,
  251. TT_FORK, NULL, NULL
  252. },
  253. END_OF_TESTCASES
  254. };