test_circuitlist.c 11 KB

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