test_circuituse.c 7.4 KB

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