test_circuituse.c 7.4 KB

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