test_circuitlist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* Copyright (c) 2013-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define CIRCUITBUILD_PRIVATE
  5. #define CIRCUITLIST_PRIVATE
  6. #include "or.h"
  7. #include "channel.h"
  8. #include "circuitbuild.h"
  9. #include "circuitlist.h"
  10. #include "test.h"
  11. #include "log_test_helpers.h"
  12. static channel_t *
  13. new_fake_channel(void)
  14. {
  15. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  16. channel_init(chan);
  17. return chan;
  18. }
  19. static struct {
  20. int ncalls;
  21. void *cmux;
  22. void *circ;
  23. cell_direction_t dir;
  24. } cam;
  25. static void
  26. circuitmux_attach_mock(circuitmux_t *cmux, circuit_t *circ,
  27. cell_direction_t dir)
  28. {
  29. ++cam.ncalls;
  30. cam.cmux = cmux;
  31. cam.circ = circ;
  32. cam.dir = dir;
  33. }
  34. static struct {
  35. int ncalls;
  36. void *cmux;
  37. void *circ;
  38. } cdm;
  39. static void
  40. circuitmux_detach_mock(circuitmux_t *cmux, circuit_t *circ)
  41. {
  42. ++cdm.ncalls;
  43. cdm.cmux = cmux;
  44. cdm.circ = circ;
  45. }
  46. #define GOT_CMUX_ATTACH(mux_, circ_, dir_) do { \
  47. tt_int_op(cam.ncalls, OP_EQ, 1); \
  48. tt_ptr_op(cam.cmux, OP_EQ, (mux_)); \
  49. tt_ptr_op(cam.circ, OP_EQ, (circ_)); \
  50. tt_int_op(cam.dir, OP_EQ, (dir_)); \
  51. memset(&cam, 0, sizeof(cam)); \
  52. } while (0)
  53. #define GOT_CMUX_DETACH(mux_, circ_) do { \
  54. tt_int_op(cdm.ncalls, OP_EQ, 1); \
  55. tt_ptr_op(cdm.cmux, OP_EQ, (mux_)); \
  56. tt_ptr_op(cdm.circ, OP_EQ, (circ_)); \
  57. memset(&cdm, 0, sizeof(cdm)); \
  58. } while (0)
  59. static void
  60. test_clist_maps(void *arg)
  61. {
  62. channel_t *ch1 = new_fake_channel();
  63. channel_t *ch2 = new_fake_channel();
  64. channel_t *ch3 = new_fake_channel();
  65. or_circuit_t *or_c1=NULL, *or_c2=NULL;
  66. (void) arg;
  67. MOCK(circuitmux_attach_circuit, circuitmux_attach_mock);
  68. MOCK(circuitmux_detach_circuit, circuitmux_detach_mock);
  69. memset(&cam, 0, sizeof(cam));
  70. memset(&cdm, 0, sizeof(cdm));
  71. tt_assert(ch1);
  72. tt_assert(ch2);
  73. tt_assert(ch3);
  74. ch1->cmux = tor_malloc(1);
  75. ch2->cmux = tor_malloc(1);
  76. ch3->cmux = tor_malloc(1);
  77. or_c1 = or_circuit_new(100, ch2);
  78. tt_assert(or_c1);
  79. GOT_CMUX_ATTACH(ch2->cmux, or_c1, CELL_DIRECTION_IN);
  80. tt_int_op(or_c1->p_circ_id, OP_EQ, 100);
  81. tt_ptr_op(or_c1->p_chan, OP_EQ, ch2);
  82. or_c2 = or_circuit_new(100, ch1);
  83. tt_assert(or_c2);
  84. GOT_CMUX_ATTACH(ch1->cmux, or_c2, CELL_DIRECTION_IN);
  85. tt_int_op(or_c2->p_circ_id, OP_EQ, 100);
  86. tt_ptr_op(or_c2->p_chan, OP_EQ, ch1);
  87. circuit_set_n_circid_chan(TO_CIRCUIT(or_c1), 200, ch1);
  88. GOT_CMUX_ATTACH(ch1->cmux, or_c1, CELL_DIRECTION_OUT);
  89. circuit_set_n_circid_chan(TO_CIRCUIT(or_c2), 200, ch2);
  90. GOT_CMUX_ATTACH(ch2->cmux, or_c2, CELL_DIRECTION_OUT);
  91. tt_ptr_op(circuit_get_by_circid_channel(200, ch1), OP_EQ, TO_CIRCUIT(or_c1));
  92. tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, TO_CIRCUIT(or_c2));
  93. tt_ptr_op(circuit_get_by_circid_channel(100, ch2), OP_EQ, TO_CIRCUIT(or_c1));
  94. /* Try the same thing again, to test the "fast" path. */
  95. tt_ptr_op(circuit_get_by_circid_channel(100, ch2), OP_EQ, TO_CIRCUIT(or_c1));
  96. tt_assert(circuit_id_in_use_on_channel(100, ch2));
  97. tt_assert(! circuit_id_in_use_on_channel(101, ch2));
  98. /* Try changing the circuitid and channel of that circuit. */
  99. circuit_set_p_circid_chan(or_c1, 500, ch3);
  100. GOT_CMUX_DETACH(ch2->cmux, TO_CIRCUIT(or_c1));
  101. GOT_CMUX_ATTACH(ch3->cmux, TO_CIRCUIT(or_c1), CELL_DIRECTION_IN);
  102. tt_ptr_op(circuit_get_by_circid_channel(100, ch2), OP_EQ, NULL);
  103. tt_assert(! circuit_id_in_use_on_channel(100, ch2));
  104. tt_ptr_op(circuit_get_by_circid_channel(500, ch3), OP_EQ, TO_CIRCUIT(or_c1));
  105. /* Now let's see about destroy handling. */
  106. tt_assert(! circuit_id_in_use_on_channel(205, ch2));
  107. tt_assert(circuit_id_in_use_on_channel(200, ch2));
  108. channel_note_destroy_pending(ch2, 200);
  109. channel_note_destroy_pending(ch2, 205);
  110. channel_note_destroy_pending(ch1, 100);
  111. tt_assert(circuit_id_in_use_on_channel(205, ch2))
  112. tt_assert(circuit_id_in_use_on_channel(200, ch2));
  113. tt_assert(circuit_id_in_use_on_channel(100, ch1));
  114. tt_assert(TO_CIRCUIT(or_c2)->n_delete_pending != 0);
  115. tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, TO_CIRCUIT(or_c2));
  116. tt_ptr_op(circuit_get_by_circid_channel(100, ch1), OP_EQ, TO_CIRCUIT(or_c2));
  117. /* Okay, now free ch2 and make sure that the circuit ID is STILL not
  118. * usable, because we haven't declared the destroy to be nonpending */
  119. tt_int_op(cdm.ncalls, OP_EQ, 0);
  120. circuit_free(TO_CIRCUIT(or_c2));
  121. or_c2 = NULL; /* prevent free */
  122. tt_int_op(cdm.ncalls, OP_EQ, 2);
  123. memset(&cdm, 0, sizeof(cdm));
  124. tt_assert(circuit_id_in_use_on_channel(200, ch2));
  125. tt_assert(circuit_id_in_use_on_channel(100, ch1));
  126. tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, NULL);
  127. tt_ptr_op(circuit_get_by_circid_channel(100, ch1), OP_EQ, NULL);
  128. /* Now say that the destroy is nonpending */
  129. channel_note_destroy_not_pending(ch2, 200);
  130. tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, NULL);
  131. channel_note_destroy_not_pending(ch1, 100);
  132. tt_ptr_op(circuit_get_by_circid_channel(100, ch1), OP_EQ, NULL);
  133. tt_assert(! circuit_id_in_use_on_channel(200, ch2));
  134. tt_assert(! circuit_id_in_use_on_channel(100, ch1));
  135. done:
  136. if (or_c1)
  137. circuit_free(TO_CIRCUIT(or_c1));
  138. if (or_c2)
  139. circuit_free(TO_CIRCUIT(or_c2));
  140. if (ch1)
  141. tor_free(ch1->cmux);
  142. if (ch2)
  143. tor_free(ch2->cmux);
  144. if (ch3)
  145. tor_free(ch3->cmux);
  146. tor_free(ch1);
  147. tor_free(ch2);
  148. tor_free(ch3);
  149. UNMOCK(circuitmux_attach_circuit);
  150. UNMOCK(circuitmux_detach_circuit);
  151. }
  152. static void
  153. test_rend_token_maps(void *arg)
  154. {
  155. or_circuit_t *c1, *c2, *c3, *c4;
  156. const uint8_t tok1[REND_TOKEN_LEN] = "The cat can't tell y";
  157. const uint8_t tok2[REND_TOKEN_LEN] = "ou its name, and it ";
  158. const uint8_t tok3[REND_TOKEN_LEN] = "doesn't really care.";
  159. /* -- Adapted from a quote by Fredrik Lundh. */
  160. (void)arg;
  161. (void)tok1; //xxxx
  162. c1 = or_circuit_new(0, NULL);
  163. c2 = or_circuit_new(0, NULL);
  164. c3 = or_circuit_new(0, NULL);
  165. c4 = or_circuit_new(0, NULL);
  166. /* Make sure we really filled up the tok* variables */
  167. tt_int_op(tok1[REND_TOKEN_LEN-1], OP_EQ, 'y');
  168. tt_int_op(tok2[REND_TOKEN_LEN-1], OP_EQ, ' ');
  169. tt_int_op(tok3[REND_TOKEN_LEN-1], OP_EQ, '.');
  170. /* No maps; nothing there. */
  171. tt_ptr_op(NULL, OP_EQ, circuit_get_rendezvous(tok1));
  172. tt_ptr_op(NULL, OP_EQ, circuit_get_intro_point(tok1));
  173. circuit_set_rendezvous_cookie(c1, tok1);
  174. circuit_set_intro_point_digest(c2, tok2);
  175. tt_ptr_op(NULL, OP_EQ, circuit_get_rendezvous(tok3));
  176. tt_ptr_op(NULL, OP_EQ, circuit_get_intro_point(tok3));
  177. tt_ptr_op(NULL, OP_EQ, circuit_get_rendezvous(tok2));
  178. tt_ptr_op(NULL, OP_EQ, circuit_get_intro_point(tok1));
  179. /* Without purpose set, we don't get the circuits */
  180. tt_ptr_op(NULL, OP_EQ, circuit_get_rendezvous(tok1));
  181. tt_ptr_op(NULL, OP_EQ, circuit_get_intro_point(tok2));
  182. c1->base_.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
  183. c2->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
  184. /* Okay, make sure they show up now. */
  185. tt_ptr_op(c1, OP_EQ, circuit_get_rendezvous(tok1));
  186. tt_ptr_op(c2, OP_EQ, circuit_get_intro_point(tok2));
  187. /* Two items at the same place with the same token. */
  188. c3->base_.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
  189. circuit_set_rendezvous_cookie(c3, tok2);
  190. tt_ptr_op(c2, OP_EQ, circuit_get_intro_point(tok2));
  191. tt_ptr_op(c3, OP_EQ, circuit_get_rendezvous(tok2));
  192. /* Marking a circuit makes it not get returned any more */
  193. circuit_mark_for_close(TO_CIRCUIT(c1), END_CIRC_REASON_FINISHED);
  194. tt_ptr_op(NULL, OP_EQ, circuit_get_rendezvous(tok1));
  195. circuit_free(TO_CIRCUIT(c1));
  196. c1 = NULL;
  197. /* Freeing a circuit makes it not get returned any more. */
  198. circuit_free(TO_CIRCUIT(c2));
  199. c2 = NULL;
  200. tt_ptr_op(NULL, OP_EQ, circuit_get_intro_point(tok2));
  201. /* c3 -- are you still there? */
  202. tt_ptr_op(c3, OP_EQ, circuit_get_rendezvous(tok2));
  203. /* Change its cookie. This never happens in Tor per se, but hey. */
  204. c3->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
  205. circuit_set_intro_point_digest(c3, tok3);
  206. tt_ptr_op(NULL, OP_EQ, circuit_get_rendezvous(tok2));
  207. tt_ptr_op(c3, OP_EQ, circuit_get_intro_point(tok3));
  208. /* Now replace c3 with c4. */
  209. c4->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
  210. circuit_set_intro_point_digest(c4, tok3);
  211. tt_ptr_op(c4, OP_EQ, circuit_get_intro_point(tok3));
  212. tt_ptr_op(c3->rendinfo, OP_EQ, NULL);
  213. tt_ptr_op(c4->rendinfo, OP_NE, NULL);
  214. tt_mem_op(c4->rendinfo, OP_EQ, tok3, REND_TOKEN_LEN);
  215. /* Now clear c4's cookie. */
  216. circuit_set_intro_point_digest(c4, NULL);
  217. tt_ptr_op(c4->rendinfo, OP_EQ, NULL);
  218. tt_ptr_op(NULL, OP_EQ, circuit_get_intro_point(tok3));
  219. done:
  220. if (c1)
  221. circuit_free(TO_CIRCUIT(c1));
  222. if (c2)
  223. circuit_free(TO_CIRCUIT(c2));
  224. if (c3)
  225. circuit_free(TO_CIRCUIT(c3));
  226. if (c4)
  227. circuit_free(TO_CIRCUIT(c4));
  228. }
  229. static void
  230. mock_channel_dump_statistics(channel_t *chan, int severity)
  231. {
  232. (void)chan;
  233. (void)severity;
  234. }
  235. static void
  236. test_pick_circid(void *arg)
  237. {
  238. bitarray_t *ba = NULL;
  239. channel_t *chan1, *chan2;
  240. circid_t circid;
  241. int i;
  242. (void) arg;
  243. MOCK(channel_dump_statistics, mock_channel_dump_statistics);
  244. chan1 = tor_malloc_zero(sizeof(channel_t));
  245. chan2 = tor_malloc_zero(sizeof(channel_t));
  246. chan2->wide_circ_ids = 1;
  247. chan1->cmux = circuitmux_alloc();
  248. chan2->cmux = circuitmux_alloc();
  249. /* CIRC_ID_TYPE_NEITHER is supposed to create a warning. */
  250. chan1->circ_id_type = CIRC_ID_TYPE_NEITHER;
  251. setup_full_capture_of_logs(LOG_WARN);
  252. tt_int_op(0, OP_EQ, get_unique_circ_id_by_chan(chan1));
  253. expect_single_log_msg_containing("Trying to pick a circuit ID for a "
  254. "connection from a client with no identity.");
  255. teardown_capture_of_logs();
  256. /* Basic tests, with no collisions */
  257. chan1->circ_id_type = CIRC_ID_TYPE_LOWER;
  258. for (i = 0; i < 50; ++i) {
  259. circid = get_unique_circ_id_by_chan(chan1);
  260. tt_uint_op(0, OP_LT, circid);
  261. tt_uint_op(circid, OP_LT, (1<<15));
  262. }
  263. chan1->circ_id_type = CIRC_ID_TYPE_HIGHER;
  264. for (i = 0; i < 50; ++i) {
  265. circid = get_unique_circ_id_by_chan(chan1);
  266. tt_uint_op((1<<15), OP_LT, circid);
  267. tt_uint_op(circid, OP_LT, (1<<16));
  268. }
  269. chan2->circ_id_type = CIRC_ID_TYPE_LOWER;
  270. for (i = 0; i < 50; ++i) {
  271. circid = get_unique_circ_id_by_chan(chan2);
  272. tt_uint_op(0, OP_LT, circid);
  273. tt_uint_op(circid, OP_LT, (1u<<31));
  274. }
  275. chan2->circ_id_type = CIRC_ID_TYPE_HIGHER;
  276. for (i = 0; i < 50; ++i) {
  277. circid = get_unique_circ_id_by_chan(chan2);
  278. tt_uint_op((1u<<31), OP_LT, circid);
  279. }
  280. /* Now make sure that we can behave well when we are full up on circuits */
  281. chan1->circ_id_type = CIRC_ID_TYPE_LOWER;
  282. chan2->circ_id_type = CIRC_ID_TYPE_LOWER;
  283. chan1->wide_circ_ids = chan2->wide_circ_ids = 0;
  284. ba = bitarray_init_zero((1<<15));
  285. for (i = 0; i < (1<<15); ++i) {
  286. circid = get_unique_circ_id_by_chan(chan1);
  287. if (circid == 0) {
  288. tt_int_op(i, OP_GT, (1<<14));
  289. break;
  290. }
  291. tt_uint_op(circid, OP_LT, (1<<15));
  292. tt_assert(! bitarray_is_set(ba, circid));
  293. bitarray_set(ba, circid);
  294. channel_mark_circid_unusable(chan1, circid);
  295. }
  296. tt_int_op(i, OP_LT, (1<<15));
  297. /* Make sure that being full on chan1 does not interfere with chan2 */
  298. for (i = 0; i < 100; ++i) {
  299. circid = get_unique_circ_id_by_chan(chan2);
  300. tt_uint_op(circid, OP_GT, 0);
  301. tt_uint_op(circid, OP_LT, (1<<15));
  302. channel_mark_circid_unusable(chan2, circid);
  303. }
  304. done:
  305. if (chan1)
  306. circuitmux_free(chan1->cmux);
  307. if (chan2)
  308. circuitmux_free(chan2->cmux);
  309. tor_free(chan1);
  310. tor_free(chan2);
  311. bitarray_free(ba);
  312. circuit_free_all();
  313. teardown_capture_of_logs();
  314. UNMOCK(channel_dump_statistics);
  315. }
  316. struct testcase_t circuitlist_tests[] = {
  317. { "maps", test_clist_maps, TT_FORK, NULL, NULL },
  318. { "rend_token_maps", test_rend_token_maps, TT_FORK, NULL, NULL },
  319. { "pick_circid", test_pick_circid, TT_FORK, NULL, NULL },
  320. END_OF_TESTCASES
  321. };