test_circuituse.c 7.2 KB

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