test_circuituse.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, 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, ==, needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  128. done:
  129. UNMOCK(circuit_all_predicted_ports_handled);
  130. }
  131. static void
  132. test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg)
  133. {
  134. (void)arg;
  135. MOCK(circuit_all_predicted_ports_handled,
  136. mock_circuit_all_predicted_ports_handled);
  137. int needs_uptime = 1;
  138. int needs_capacity = 1;
  139. MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  140. time_t now = time(NULL);
  141. tt_int_op(0, ==, needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  142. done:
  143. UNMOCK(circuit_all_predicted_ports_handled);
  144. UNMOCK(router_have_consensus_path);
  145. }
  146. static void
  147. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
  148. {
  149. (void)arg;
  150. MOCK(circuit_all_predicted_ports_handled,
  151. mock_circuit_all_predicted_ports_handled);
  152. int needs_uptime = 1;
  153. int needs_capacity = 1;
  154. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  155. time_t now = time(NULL);
  156. tt_int_op(1, ==, needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  157. done:
  158. UNMOCK(circuit_all_predicted_ports_handled);
  159. UNMOCK(router_have_consensus_path);
  160. }
  161. static void
  162. test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
  163. {
  164. (void)arg;
  165. MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  166. tt_int_op(0, ==, needs_circuits_for_build(0));
  167. done: ;
  168. }
  169. static void
  170. test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
  171. {
  172. (void)arg;
  173. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  174. tt_int_op(0, ==, needs_circuits_for_build(13));
  175. done:
  176. UNMOCK(router_have_consensus_path);
  177. }
  178. static void
  179. test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
  180. {
  181. (void)arg;
  182. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  183. tt_int_op(1, ==, needs_circuits_for_build(0));
  184. done:
  185. UNMOCK(router_have_consensus_path);
  186. }
  187. struct testcase_t circuituse_tests[] = {
  188. { "marked",
  189. test_circuit_is_available_for_use_ret_false_when_marked_for_close,
  190. TT_FORK, NULL, NULL
  191. },
  192. { "timestamp",
  193. test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
  194. TT_FORK, NULL, NULL
  195. },
  196. { "non_general",
  197. test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
  198. TT_FORK, NULL, NULL
  199. },
  200. { "non_general",
  201. test_circuit_is_available_for_use_ret_false_for_non_general_origin,
  202. TT_FORK, NULL, NULL
  203. },
  204. { "origin",
  205. test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
  206. TT_FORK, NULL, NULL
  207. },
  208. { "clean",
  209. test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
  210. TT_FORK, NULL, NULL
  211. },
  212. { "onehop",
  213. test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
  214. TT_FORK, NULL, NULL
  215. },
  216. { "clean_circ",
  217. test_circuit_is_available_for_use_returns_true_for_clean_circuit,
  218. TT_FORK, NULL, NULL
  219. },
  220. { "exit_f",
  221. test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
  222. TT_FORK, NULL, NULL
  223. },
  224. { "exit_t",
  225. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  226. TT_FORK, NULL, NULL
  227. },
  228. { "non_exit",
  229. test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
  230. TT_FORK, NULL, NULL
  231. },
  232. { "true",
  233. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  234. TT_FORK, NULL, NULL
  235. },
  236. { "consensus_path_unknown",
  237. test_needs_circuits_for_build_ret_false_consensus_path_unknown,
  238. TT_FORK, NULL, NULL
  239. },
  240. { "less_than_max",
  241. test_needs_circuits_for_build_ret_false_if_num_less_than_max,
  242. TT_FORK, NULL, NULL
  243. },
  244. { "more_needed",
  245. test_needs_circuits_for_build_returns_true_when_more_are_needed,
  246. TT_FORK, NULL, NULL
  247. },
  248. END_OF_TESTCASES
  249. };