test_circuituse.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_OR;
  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_origin_purpose(void *arg)
  45. {
  46. (void)arg;
  47. circuit_t *circ = tor_malloc(sizeof(circuit_t));
  48. circ->purpose = CIRCUIT_PURPOSE_OR;
  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_unusable_for_new_conns(void *arg)
  55. {
  56. (void)arg;
  57. circuit_t *circ = dummy_origin_circuit_new(30);
  58. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  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_returns_false_for_onehop_tunnel(void *arg)
  65. {
  66. (void)arg;
  67. circuit_t *circ = dummy_origin_circuit_new(30);
  68. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  69. oc->build_state = tor_malloc(sizeof(cpath_build_state_t));
  70. oc->build_state->onehop_tunnel = 1;
  71. tt_int_op(0, ==, circuit_is_available_for_use(circ));
  72. done:
  73. tor_free(circ);
  74. }
  75. static void
  76. test_circuit_is_available_for_use_returns_true_for_clean_circuit(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(sizeof(cpath_build_state_t));
  82. oc->build_state->onehop_tunnel = 0;
  83. tt_int_op(1, ==, circuit_is_available_for_use(circ));
  84. done:
  85. tor_free(circ);
  86. }
  87. static int
  88. mock_circuit_all_predicted_ports_handled(time_t now,
  89. int *need_uptime,
  90. int *need_capacity)
  91. {
  92. (void)now;
  93. if (need_uptime && need_capacity)
  94. return 0;
  95. return 1;
  96. }
  97. static consensus_path_type_t
  98. mock_router_have_unknown_consensus_path(void)
  99. {
  100. return CONSENSUS_PATH_UNKNOWN;
  101. }
  102. static consensus_path_type_t
  103. mock_router_have_exit_consensus_path(void)
  104. {
  105. return CONSENSUS_PATH_EXIT;
  106. }
  107. static void
  108. test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg)
  109. {
  110. (void)arg;
  111. MOCK(circuit_all_predicted_ports_handled,
  112. mock_circuit_all_predicted_ports_handled);
  113. int needs_uptime = 1;
  114. int needs_capacity = 0;
  115. time_t now = time(NULL);
  116. tt_int_op(0, ==, needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  117. done:
  118. UNMOCK(circuit_all_predicted_ports_handled);
  119. }
  120. static void
  121. test_needs_exit_circuits_ret_false_for_non_exit_consensus_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 = 1;
  128. MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  129. time_t now = time(NULL);
  130. tt_int_op(0, ==, needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  131. done:
  132. UNMOCK(circuit_all_predicted_ports_handled);
  133. UNMOCK(router_have_consensus_path);
  134. }
  135. static void
  136. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
  137. {
  138. (void)arg;
  139. MOCK(circuit_all_predicted_ports_handled,
  140. mock_circuit_all_predicted_ports_handled);
  141. int needs_uptime = 1;
  142. int needs_capacity = 1;
  143. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  144. time_t now = time(NULL);
  145. tt_int_op(1, ==, 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_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
  152. {
  153. (void)arg;
  154. MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  155. tt_int_op(0, ==, needs_circuits_for_build(0));
  156. done: ;
  157. }
  158. static void
  159. test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
  160. {
  161. (void)arg;
  162. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  163. tt_int_op(0, ==, needs_circuits_for_build(13));
  164. done:
  165. UNMOCK(router_have_consensus_path);
  166. }
  167. static void
  168. test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
  169. {
  170. (void)arg;
  171. MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  172. tt_int_op(1, ==, needs_circuits_for_build(0));
  173. done:
  174. UNMOCK(router_have_consensus_path);
  175. }
  176. struct testcase_t circuituse_tests[] = {
  177. { "marked",
  178. test_circuit_is_available_for_use_ret_false_when_marked_for_close,
  179. TT_FORK, NULL, NULL
  180. },
  181. { "timestamp",
  182. test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
  183. TT_FORK, NULL, NULL
  184. },
  185. { "non_general",
  186. test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
  187. TT_FORK, NULL, NULL
  188. },
  189. { "origin",
  190. test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
  191. TT_FORK, NULL, NULL
  192. },
  193. { "clean",
  194. test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
  195. TT_FORK, NULL, NULL
  196. },
  197. { "onehop",
  198. test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
  199. TT_FORK, NULL, NULL
  200. },
  201. { "clean_circ",
  202. test_circuit_is_available_for_use_returns_true_for_clean_circuit,
  203. TT_FORK, NULL, NULL
  204. },
  205. { "exit_f",
  206. test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
  207. TT_FORK, NULL, NULL
  208. },
  209. { "exit_t",
  210. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  211. TT_FORK, NULL, NULL
  212. },
  213. { "non_exit",
  214. test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
  215. TT_FORK, NULL, NULL
  216. },
  217. { "true",
  218. test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  219. TT_FORK, NULL, NULL
  220. },
  221. { "consensus_path_unknown",
  222. test_needs_circuits_for_build_ret_false_consensus_path_unknown,
  223. TT_FORK, NULL, NULL
  224. },
  225. { "less_than_max",
  226. test_needs_circuits_for_build_ret_false_if_num_less_than_max,
  227. TT_FORK, NULL, NULL
  228. },
  229. { "more_needed",
  230. test_needs_circuits_for_build_returns_true_when_more_are_needed,
  231. TT_FORK, NULL, NULL
  232. },
  233. END_OF_TESTCASES
  234. };