test_channel.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. /* Copyright (c) 2013-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define CHANNEL_PRIVATE_
  5. #include "or.h"
  6. #include "channel.h"
  7. /* For channel_note_destroy_not_pending */
  8. #define CIRCUITLIST_PRIVATE
  9. #include "circuitlist.h"
  10. #include "circuitmux.h"
  11. #include "circuitmux_ewma.h"
  12. /* For var_cell_free */
  13. #include "connection_or.h"
  14. /* For packed_cell stuff */
  15. #define RELAY_PRIVATE
  16. #include "relay.h"
  17. /* For init/free stuff */
  18. #include "scheduler.h"
  19. #include "networkstatus.h"
  20. /* Test suite stuff */
  21. #include "log_test_helpers.h"
  22. #include "test.h"
  23. #include "fakechans.h"
  24. static int test_chan_accept_cells = 0;
  25. static int test_chan_fixed_cells_recved = 0;
  26. static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
  27. static int test_chan_var_cells_recved = 0;
  28. static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
  29. static int test_cells_written = 0;
  30. static int test_doesnt_want_writes_count = 0;
  31. static int test_dumpstats_calls = 0;
  32. static int test_has_waiting_cells_count = 0;
  33. static double test_overhead_estimate = 1.0;
  34. static int test_releases_count = 0;
  35. static channel_t *dump_statistics_mock_target = NULL;
  36. static int dump_statistics_mock_matches = 0;
  37. static int test_close_called = 0;
  38. static int test_chan_should_be_canonical = 0;
  39. static int test_chan_should_match_target = 0;
  40. static int test_chan_canonical_should_be_reliable = 0;
  41. static void chan_test_channel_dump_statistics_mock(
  42. channel_t *chan, int severity);
  43. static const char * chan_test_describe_transport(channel_t *ch);
  44. static void chan_test_dumpstats(channel_t *ch, int severity);
  45. static void chan_test_var_cell_handler(channel_t *ch,
  46. var_cell_t *var_cell);
  47. static void chan_test_error(channel_t *ch);
  48. static void chan_test_finish_close(channel_t *ch);
  49. static const char * chan_test_get_remote_descr(channel_t *ch, int flags);
  50. static int chan_test_is_canonical(channel_t *ch, int req);
  51. static size_t chan_test_num_bytes_queued(channel_t *ch);
  52. static int chan_test_num_cells_writeable(channel_t *ch);
  53. static int chan_test_write_cell(channel_t *ch, cell_t *cell);
  54. static int chan_test_write_packed_cell(channel_t *ch,
  55. packed_cell_t *packed_cell);
  56. static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
  57. static void scheduler_channel_doesnt_want_writes_mock(channel_t *ch);
  58. static void test_channel_dumpstats(void *arg);
  59. static void test_channel_lifecycle(void *arg);
  60. static const char *
  61. chan_test_describe_transport(channel_t *ch)
  62. {
  63. tt_ptr_op(ch, OP_NE, NULL);
  64. done:
  65. return "Fake channel for unit tests";
  66. }
  67. /**
  68. * Mock for channel_dump_statistics(); if the channel matches the
  69. * target, bump a counter - otherwise ignore.
  70. */
  71. static void
  72. chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
  73. {
  74. tt_ptr_op(chan, OP_NE, NULL);
  75. (void)severity;
  76. if (chan != NULL && chan == dump_statistics_mock_target) {
  77. ++dump_statistics_mock_matches;
  78. }
  79. done:
  80. return;
  81. }
  82. /*
  83. * Handle an incoming fixed-size cell for unit tests
  84. */
  85. static void
  86. chan_test_cell_handler(channel_t *chan, cell_t *cell)
  87. {
  88. tt_assert(chan);
  89. tt_assert(cell);
  90. test_chan_last_seen_fixed_cell_ptr = cell;
  91. ++test_chan_fixed_cells_recved;
  92. done:
  93. return;
  94. }
  95. /*
  96. * Fake transport-specific stats call
  97. */
  98. static void
  99. chan_test_dumpstats(channel_t *ch, int severity)
  100. {
  101. tt_ptr_op(ch, OP_NE, NULL);
  102. (void)severity;
  103. ++test_dumpstats_calls;
  104. done:
  105. return;
  106. }
  107. /*
  108. * Handle an incoming variable-size cell for unit tests
  109. */
  110. static void
  111. chan_test_var_cell_handler(channel_t *ch,
  112. var_cell_t *var_cell)
  113. {
  114. tt_assert(ch);
  115. tt_assert(var_cell);
  116. test_chan_last_seen_var_cell_ptr = var_cell;
  117. ++test_chan_var_cells_recved;
  118. done:
  119. return;
  120. }
  121. static void
  122. chan_test_close(channel_t *ch)
  123. {
  124. tt_assert(ch);
  125. ++test_close_called;
  126. done:
  127. return;
  128. }
  129. /*
  130. * Close a channel through the error path
  131. */
  132. static void
  133. chan_test_error(channel_t *ch)
  134. {
  135. tt_assert(ch);
  136. tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
  137. ch->state == CHANNEL_STATE_ERROR ||
  138. ch->state == CHANNEL_STATE_CLOSED));
  139. channel_close_for_error(ch);
  140. done:
  141. return;
  142. }
  143. /*
  144. * Finish closing a channel from CHANNEL_STATE_CLOSING
  145. */
  146. static void
  147. chan_test_finish_close(channel_t *ch)
  148. {
  149. tt_assert(ch);
  150. tt_assert(ch->state == CHANNEL_STATE_CLOSING);
  151. channel_closed(ch);
  152. done:
  153. return;
  154. }
  155. static const char *
  156. chan_test_get_remote_descr(channel_t *ch, int flags)
  157. {
  158. tt_assert(ch);
  159. tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), OP_EQ, 0);
  160. done:
  161. return "Fake channel for unit tests; no real endpoint";
  162. }
  163. static double
  164. chan_test_get_overhead_estimate(channel_t *ch)
  165. {
  166. tt_assert(ch);
  167. done:
  168. return test_overhead_estimate;
  169. }
  170. static int
  171. chan_test_is_canonical(channel_t *ch, int req)
  172. {
  173. tt_ptr_op(ch, OP_NE, NULL);
  174. tt_assert(req == 0 || req == 1);
  175. done:
  176. /* Fake channels are always canonical */
  177. return 1;
  178. }
  179. static size_t
  180. chan_test_num_bytes_queued(channel_t *ch)
  181. {
  182. tt_assert(ch);
  183. done:
  184. return 0;
  185. }
  186. static int
  187. chan_test_num_cells_writeable(channel_t *ch)
  188. {
  189. tt_assert(ch);
  190. done:
  191. return 32;
  192. }
  193. static int
  194. chan_test_write_cell(channel_t *ch, cell_t *cell)
  195. {
  196. int rv = 0;
  197. tt_assert(ch);
  198. tt_assert(cell);
  199. if (test_chan_accept_cells) {
  200. /* Free the cell and bump the counter */
  201. tor_free(cell);
  202. ++test_cells_written;
  203. rv = 1;
  204. }
  205. /* else return 0, we didn't accept it */
  206. done:
  207. return rv;
  208. }
  209. static int
  210. chan_test_write_packed_cell(channel_t *ch,
  211. packed_cell_t *packed_cell)
  212. {
  213. int rv = 0;
  214. tt_assert(ch);
  215. tt_assert(packed_cell);
  216. if (test_chan_accept_cells) {
  217. /* Free the cell and bump the counter */
  218. packed_cell_free(packed_cell);
  219. ++test_cells_written;
  220. rv = 1;
  221. }
  222. /* else return 0, we didn't accept it */
  223. done:
  224. return rv;
  225. }
  226. static int
  227. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  228. {
  229. int rv = 0;
  230. tt_assert(ch);
  231. tt_assert(var_cell);
  232. if (test_chan_accept_cells) {
  233. /* Free the cell and bump the counter */
  234. var_cell_free(var_cell);
  235. ++test_cells_written;
  236. rv = 1;
  237. }
  238. /* else return 0, we didn't accept it */
  239. done:
  240. return rv;
  241. }
  242. /**
  243. * Fill out c with a new fake cell for test suite use
  244. */
  245. void
  246. make_fake_cell(cell_t *c)
  247. {
  248. tt_ptr_op(c, OP_NE, NULL);
  249. c->circ_id = 1;
  250. c->command = CELL_RELAY;
  251. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  252. done:
  253. return;
  254. }
  255. /**
  256. * Fill out c with a new fake var_cell for test suite use
  257. */
  258. void
  259. make_fake_var_cell(var_cell_t *c)
  260. {
  261. tt_ptr_op(c, OP_NE, NULL);
  262. c->circ_id = 1;
  263. c->command = CELL_VERSIONS;
  264. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  265. memset(c->payload, 0, c->payload_len);
  266. done:
  267. return;
  268. }
  269. /**
  270. * Set up a new fake channel for the test suite
  271. */
  272. channel_t *
  273. new_fake_channel(void)
  274. {
  275. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  276. channel_init(chan);
  277. chan->close = chan_test_close;
  278. chan->get_overhead_estimate = chan_test_get_overhead_estimate;
  279. chan->get_remote_descr = chan_test_get_remote_descr;
  280. chan->num_bytes_queued = chan_test_num_bytes_queued;
  281. chan->num_cells_writeable = chan_test_num_cells_writeable;
  282. chan->write_cell = chan_test_write_cell;
  283. chan->write_packed_cell = chan_test_write_packed_cell;
  284. chan->write_var_cell = chan_test_write_var_cell;
  285. chan->state = CHANNEL_STATE_OPEN;
  286. chan->cmux = circuitmux_alloc();
  287. return chan;
  288. }
  289. void
  290. free_fake_channel(channel_t *chan)
  291. {
  292. if (! chan)
  293. return;
  294. if (chan->cmux)
  295. circuitmux_free(chan->cmux);
  296. tor_free(chan);
  297. }
  298. /**
  299. * Counter query for scheduler_channel_has_waiting_cells_mock()
  300. */
  301. int
  302. get_mock_scheduler_has_waiting_cells_count(void)
  303. {
  304. return test_has_waiting_cells_count;
  305. }
  306. /**
  307. * Mock for scheduler_channel_has_waiting_cells()
  308. */
  309. void
  310. scheduler_channel_has_waiting_cells_mock(channel_t *ch)
  311. {
  312. (void)ch;
  313. /* Increment counter */
  314. ++test_has_waiting_cells_count;
  315. return;
  316. }
  317. static void
  318. scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
  319. {
  320. (void)ch;
  321. /* Increment counter */
  322. ++test_doesnt_want_writes_count;
  323. return;
  324. }
  325. /**
  326. * Counter query for scheduler_release_channel_mock()
  327. */
  328. int
  329. get_mock_scheduler_release_channel_count(void)
  330. {
  331. return test_releases_count;
  332. }
  333. /**
  334. * Mock for scheduler_release_channel()
  335. */
  336. void
  337. scheduler_release_channel_mock(channel_t *ch)
  338. {
  339. (void)ch;
  340. /* Increment counter */
  341. ++test_releases_count;
  342. return;
  343. }
  344. static int
  345. test_chan_is_canonical(channel_t *chan, int req)
  346. {
  347. tor_assert(chan);
  348. if (req && test_chan_canonical_should_be_reliable) {
  349. return 1;
  350. }
  351. if (test_chan_should_be_canonical) {
  352. return 1;
  353. }
  354. return 0;
  355. }
  356. static int
  357. test_chan_matches_target(channel_t *chan, const tor_addr_t *target)
  358. {
  359. (void) chan;
  360. (void) target;
  361. if (test_chan_should_match_target) {
  362. return 1;
  363. }
  364. return 0;
  365. }
  366. /**
  367. * Test for channel_dumpstats() and limited test for
  368. * channel_dump_statistics()
  369. */
  370. static void
  371. test_channel_dumpstats(void *arg)
  372. {
  373. channel_t *ch = NULL;
  374. cell_t *cell = NULL;
  375. packed_cell_t *p_cell = NULL;
  376. int old_count;
  377. (void)arg;
  378. /* Mock these for duration of the test */
  379. MOCK(scheduler_channel_doesnt_want_writes,
  380. scheduler_channel_doesnt_want_writes_mock);
  381. MOCK(scheduler_release_channel,
  382. scheduler_release_channel_mock);
  383. /* Set up a new fake channel */
  384. ch = new_fake_channel();
  385. tt_assert(ch);
  386. /* Try to register it */
  387. channel_register(ch);
  388. tt_assert(ch->registered);
  389. /* Set up mock */
  390. dump_statistics_mock_target = ch;
  391. dump_statistics_mock_matches = 0;
  392. MOCK(channel_dump_statistics,
  393. chan_test_channel_dump_statistics_mock);
  394. /* Call channel_dumpstats() */
  395. channel_dumpstats(LOG_DEBUG);
  396. /* Assert that we hit the mock */
  397. tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
  398. /* Close the channel */
  399. channel_mark_for_close(ch);
  400. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  401. chan_test_finish_close(ch);
  402. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  403. /* Try again and hit the finished channel */
  404. channel_dumpstats(LOG_DEBUG);
  405. tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
  406. channel_run_cleanup();
  407. ch = NULL;
  408. /* Now we should hit nothing */
  409. channel_dumpstats(LOG_DEBUG);
  410. tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
  411. /* Unmock */
  412. UNMOCK(channel_dump_statistics);
  413. dump_statistics_mock_target = NULL;
  414. dump_statistics_mock_matches = 0;
  415. /* Now make another channel */
  416. ch = new_fake_channel();
  417. tt_assert(ch);
  418. channel_register(ch);
  419. tt_int_op(ch->registered, OP_EQ, 1);
  420. /* Lie about its age so dumpstats gets coverage for rate calculations */
  421. ch->timestamp_created = time(NULL) - 30;
  422. tt_int_op(ch->timestamp_created, OP_GT, 0);
  423. tt_int_op(time(NULL), OP_GT, ch->timestamp_created);
  424. /* Put cells through it both ways to make the counters non-zero */
  425. p_cell = packed_cell_new();
  426. test_chan_accept_cells = 1;
  427. old_count = test_cells_written;
  428. channel_write_packed_cell(ch, p_cell);
  429. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  430. tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0);
  431. tt_u64_op(ch->n_cells_xmitted, OP_GT, 0);
  432. /* Receive path */
  433. channel_set_cell_handlers(ch,
  434. chan_test_cell_handler,
  435. chan_test_var_cell_handler);
  436. tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
  437. tt_ptr_op(channel_get_var_cell_handler(ch), OP_EQ,
  438. chan_test_var_cell_handler);
  439. cell = tor_malloc_zero(sizeof(*cell));
  440. old_count = test_chan_fixed_cells_recved;
  441. channel_process_cell(ch, cell);
  442. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  443. tt_u64_op(ch->n_bytes_recved, OP_GT, 0);
  444. tt_u64_op(ch->n_cells_recved, OP_GT, 0);
  445. /* Test channel_dump_statistics */
  446. ch->describe_transport = chan_test_describe_transport;
  447. ch->dumpstats = chan_test_dumpstats;
  448. ch->is_canonical = chan_test_is_canonical;
  449. old_count = test_dumpstats_calls;
  450. channel_dump_statistics(ch, LOG_DEBUG);
  451. tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
  452. /* Close the channel */
  453. channel_mark_for_close(ch);
  454. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  455. chan_test_finish_close(ch);
  456. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  457. channel_run_cleanup();
  458. ch = NULL;
  459. done:
  460. free_fake_channel(ch);
  461. tor_free(cell);
  462. UNMOCK(scheduler_channel_doesnt_want_writes);
  463. UNMOCK(scheduler_release_channel);
  464. return;
  465. }
  466. /* Test outbound cell. The callstack is:
  467. * channel_flush_some_cells()
  468. * -> channel_flush_from_first_active_circuit()
  469. * -> channel_write_packed_cell()
  470. * -> write_packed_cell()
  471. * -> chan->write_packed_cell() fct ptr.
  472. *
  473. * This test goes from a cell in a circuit up to the channel write handler
  474. * that should put them on the connection outbuf. */
  475. static void
  476. test_channel_outbound_cell(void *arg)
  477. {
  478. int old_count;
  479. channel_t *chan = NULL;
  480. packed_cell_t *p_cell = NULL, *p_cell2 = NULL;
  481. origin_circuit_t *circ = NULL;
  482. cell_queue_t *queue;
  483. (void) arg;
  484. /* The channel will be freed so we need to hijack this so the scheduler
  485. * doesn't get confused. */
  486. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  487. /* Accept cells to lower layer */
  488. test_chan_accept_cells = 1;
  489. /* Use default overhead factor */
  490. test_overhead_estimate = 1.0;
  491. /* Setup a valid circuit to queue a cell. */
  492. circ = origin_circuit_new();
  493. tt_assert(circ);
  494. /* Circuit needs an origin purpose to be considered origin. */
  495. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  496. TO_CIRCUIT(circ)->n_circ_id = 42;
  497. /* This is the outbound test so use the next channel queue. */
  498. queue = &TO_CIRCUIT(circ)->n_chan_cells;
  499. /* Setup packed cell to queue on the circuit. */
  500. p_cell = packed_cell_new();
  501. tt_assert(p_cell);
  502. p_cell2 = packed_cell_new();
  503. tt_assert(p_cell2);
  504. /* Setup a channel to put the circuit on. */
  505. chan = new_fake_channel();
  506. tt_assert(chan);
  507. chan->state = CHANNEL_STATE_OPENING;
  508. channel_change_state_open(chan);
  509. /* Outbound channel. */
  510. channel_mark_outgoing(chan);
  511. /* Try to register it so we can clean it through the channel cleanup
  512. * process. */
  513. channel_register(chan);
  514. tt_int_op(chan->registered, OP_EQ, 1);
  515. /* Set EWMA policy so we can pick it when flushing. */
  516. channel_set_cmux_policy_everywhere(&ewma_policy);
  517. tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy);
  518. /* Register circuit to the channel circid map which will attach the circuit
  519. * to the channel's cmux as well. */
  520. circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan);
  521. tt_int_op(channel_num_circuits(chan), OP_EQ, 1);
  522. tt_assert(!TO_CIRCUIT(circ)->next_active_on_n_chan);
  523. tt_assert(!TO_CIRCUIT(circ)->prev_active_on_n_chan);
  524. /* Test the cmux state. */
  525. tt_ptr_op(TO_CIRCUIT(circ)->n_mux, OP_EQ, chan->cmux);
  526. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  527. OP_EQ, 1);
  528. /* Flush the channel without any cell on it. */
  529. old_count = test_cells_written;
  530. ssize_t flushed = channel_flush_some_cells(chan, 1);
  531. tt_i64_op(flushed, OP_EQ, 0);
  532. tt_int_op(test_cells_written, OP_EQ, old_count);
  533. tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
  534. tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
  535. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
  536. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  537. OP_EQ, 0);
  538. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
  539. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
  540. /* Queue cell onto the next queue that is the outbound direction. Than
  541. * update its cmux so the circuit can be picked when flushing cells. */
  542. cell_queue_append(queue, p_cell);
  543. p_cell = NULL;
  544. tt_int_op(queue->n, OP_EQ, 1);
  545. cell_queue_append(queue, p_cell2);
  546. p_cell2 = NULL;
  547. tt_int_op(queue->n, OP_EQ, 2);
  548. update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
  549. tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
  550. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
  551. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  552. OP_EQ, 1);
  553. /* From this point on, we have a queued cell on an active circuit attached
  554. * to the channel's cmux. */
  555. /* Flush the first cell. This is going to go down the call stack. */
  556. old_count = test_cells_written;
  557. flushed = channel_flush_some_cells(chan, 1);
  558. tt_i64_op(flushed, OP_EQ, 1);
  559. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  560. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
  561. tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
  562. /* Circuit should remain active because there is a second cell queued. */
  563. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  564. OP_EQ, 1);
  565. /* Should still be attached. */
  566. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  567. OP_EQ, 1);
  568. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
  569. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
  570. /* Flush second cell. This is going to go down the call stack. */
  571. old_count = test_cells_written;
  572. flushed = channel_flush_some_cells(chan, 1);
  573. tt_i64_op(flushed, OP_EQ, 1);
  574. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  575. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
  576. tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
  577. /* No more cells should make the circuit inactive. */
  578. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  579. OP_EQ, 0);
  580. /* Should still be attached. */
  581. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  582. OP_EQ, 1);
  583. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
  584. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
  585. done:
  586. if (circ) {
  587. circuit_free(TO_CIRCUIT(circ));
  588. }
  589. tor_free(p_cell);
  590. channel_free_all();
  591. UNMOCK(scheduler_release_channel);
  592. }
  593. /* Test inbound cell. The callstack is:
  594. * channel_process_cell()
  595. * -> chan->cell_handler()
  596. *
  597. * This test is about checking if we can process an inbound cell down to the
  598. * channel handler. */
  599. static void
  600. test_channel_inbound_cell(void *arg)
  601. {
  602. channel_t *chan = NULL;
  603. cell_t *cell = NULL;
  604. int old_count;
  605. (void) arg;
  606. /* The channel will be freed so we need to hijack this so the scheduler
  607. * doesn't get confused. */
  608. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  609. /* Accept cells to lower layer */
  610. test_chan_accept_cells = 1;
  611. /* Use default overhead factor */
  612. test_overhead_estimate = 1.0;
  613. chan = new_fake_channel();
  614. tt_assert(chan);
  615. /* Start it off in OPENING */
  616. chan->state = CHANNEL_STATE_OPENING;
  617. /* Try to register it */
  618. channel_register(chan);
  619. tt_int_op(chan->registered, OP_EQ, 1);
  620. /* Open it */
  621. channel_change_state_open(chan);
  622. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
  623. tt_int_op(chan->has_been_open, OP_EQ, 1);
  624. /* Receive a cell now. */
  625. cell = tor_malloc_zero(sizeof(*cell));
  626. make_fake_cell(cell);
  627. old_count = test_chan_fixed_cells_recved;
  628. channel_process_cell(chan, cell);
  629. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
  630. tt_u64_op(chan->timestamp_xfer_ms, OP_EQ, 0);
  631. tt_u64_op(chan->timestamp_active, OP_EQ, 0);
  632. tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
  633. /* Setup incoming cell handlers. We don't care about var cell, the channel
  634. * layers is not handling those. */
  635. channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
  636. tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
  637. /* Now process the cell, we should see it. */
  638. old_count = test_chan_fixed_cells_recved;
  639. channel_process_cell(chan, cell);
  640. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  641. /* We should have a series of timestamp set. */
  642. tt_u64_op(chan->timestamp_xfer_ms, OP_NE, 0);
  643. tt_u64_op(chan->timestamp_active, OP_NE, 0);
  644. tt_u64_op(chan->timestamp_recv, OP_NE, 0);
  645. tt_u64_op(chan->next_padding_time_ms, OP_EQ, 0);
  646. tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
  647. tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
  648. /* Close it */
  649. old_count = test_close_called;
  650. channel_mark_for_close(chan);
  651. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
  652. tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
  653. tt_int_op(test_close_called, OP_EQ, old_count + 1);
  654. /* This closes the channe so it calls in the scheduler, make sure of it. */
  655. old_count = test_releases_count;
  656. chan_test_finish_close(chan);
  657. tt_int_op(test_releases_count, OP_EQ, old_count + 1);
  658. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
  659. /* The channel will be free, lets make sure it is not accessible. */
  660. uint64_t chan_id = chan->global_identifier;
  661. tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
  662. channel_run_cleanup();
  663. chan = channel_find_by_global_id(chan_id);
  664. tt_assert(chan == NULL);
  665. done:
  666. tor_free(cell);
  667. UNMOCK(scheduler_release_channel);
  668. }
  669. /**
  670. * Normal channel lifecycle test:
  671. *
  672. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  673. */
  674. static void
  675. test_channel_lifecycle(void *arg)
  676. {
  677. channel_t *ch1 = NULL, *ch2 = NULL;
  678. packed_cell_t *p_cell = NULL;
  679. int old_count, init_doesnt_want_writes_count;
  680. int init_releases_count;
  681. (void)arg;
  682. /* Mock these for the whole lifecycle test */
  683. MOCK(scheduler_channel_doesnt_want_writes,
  684. scheduler_channel_doesnt_want_writes_mock);
  685. MOCK(scheduler_release_channel,
  686. scheduler_release_channel_mock);
  687. /* Cache some initial counter values */
  688. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  689. init_releases_count = test_releases_count;
  690. /* Accept cells to lower layer */
  691. test_chan_accept_cells = 1;
  692. /* Use default overhead factor */
  693. test_overhead_estimate = 1.0;
  694. ch1 = new_fake_channel();
  695. tt_assert(ch1);
  696. /* Start it off in OPENING */
  697. ch1->state = CHANNEL_STATE_OPENING;
  698. /* Try to register it */
  699. channel_register(ch1);
  700. tt_assert(ch1->registered);
  701. /* Try to write a cell through (should queue) */
  702. p_cell = packed_cell_new();
  703. old_count = test_cells_written;
  704. channel_write_packed_cell(ch1, p_cell);
  705. tt_int_op(old_count, OP_EQ, test_cells_written);
  706. /* Move it to OPEN and flush */
  707. channel_change_state_open(ch1);
  708. /* Get another one */
  709. ch2 = new_fake_channel();
  710. tt_assert(ch2);
  711. ch2->state = CHANNEL_STATE_OPENING;
  712. /* Register */
  713. channel_register(ch2);
  714. tt_assert(ch2->registered);
  715. /* Check counters */
  716. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  717. init_doesnt_want_writes_count);
  718. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  719. /* Move ch1 to MAINT */
  720. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  721. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  722. init_doesnt_want_writes_count + 1);
  723. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  724. /* Move ch2 to OPEN */
  725. channel_change_state_open(ch2);
  726. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  727. init_doesnt_want_writes_count + 1);
  728. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  729. /* Move ch1 back to OPEN */
  730. channel_change_state_open(ch1);
  731. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  732. init_doesnt_want_writes_count + 1);
  733. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  734. /* Mark ch2 for close */
  735. channel_mark_for_close(ch2);
  736. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  737. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  738. init_doesnt_want_writes_count + 1);
  739. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
  740. /* Shut down channels */
  741. channel_free_all();
  742. ch1 = ch2 = NULL;
  743. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  744. init_doesnt_want_writes_count + 1);
  745. /* channel_free() calls scheduler_release_channel() */
  746. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
  747. done:
  748. free_fake_channel(ch1);
  749. free_fake_channel(ch2);
  750. tor_free(p_cell);
  751. UNMOCK(scheduler_channel_doesnt_want_writes);
  752. UNMOCK(scheduler_release_channel);
  753. }
  754. /**
  755. * Weird channel lifecycle test:
  756. *
  757. * OPENING->CLOSING->CLOSED
  758. * OPENING->OPEN->CLOSING->ERROR
  759. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  760. * OPENING->OPEN->MAINT->CLOSING->ERROR
  761. */
  762. static void
  763. test_channel_lifecycle_2(void *arg)
  764. {
  765. channel_t *ch = NULL;
  766. (void)arg;
  767. /* Mock these for the whole lifecycle test */
  768. MOCK(scheduler_channel_doesnt_want_writes,
  769. scheduler_channel_doesnt_want_writes_mock);
  770. MOCK(scheduler_release_channel,
  771. scheduler_release_channel_mock);
  772. /* Accept cells to lower layer */
  773. test_chan_accept_cells = 1;
  774. /* Use default overhead factor */
  775. test_overhead_estimate = 1.0;
  776. ch = new_fake_channel();
  777. tt_assert(ch);
  778. /* Start it off in OPENING */
  779. ch->state = CHANNEL_STATE_OPENING;
  780. /* Try to register it */
  781. channel_register(ch);
  782. tt_assert(ch->registered);
  783. /* Try to close it */
  784. channel_mark_for_close(ch);
  785. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  786. /* Finish closing it */
  787. chan_test_finish_close(ch);
  788. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  789. channel_run_cleanup();
  790. ch = NULL;
  791. /* Now try OPENING->OPEN->CLOSING->ERROR */
  792. ch = new_fake_channel();
  793. tt_assert(ch);
  794. ch->state = CHANNEL_STATE_OPENING;
  795. channel_register(ch);
  796. tt_assert(ch->registered);
  797. /* Finish opening it */
  798. channel_change_state_open(ch);
  799. /* Error exit from lower layer */
  800. chan_test_error(ch);
  801. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  802. chan_test_finish_close(ch);
  803. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  804. channel_run_cleanup();
  805. ch = NULL;
  806. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  807. ch = new_fake_channel();
  808. tt_assert(ch);
  809. ch->state = CHANNEL_STATE_OPENING;
  810. channel_register(ch);
  811. tt_assert(ch->registered);
  812. /* Finish opening it */
  813. channel_change_state_open(ch);
  814. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  815. /* Go to maintenance state */
  816. channel_change_state(ch, CHANNEL_STATE_MAINT);
  817. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  818. /* Lower layer close */
  819. channel_mark_for_close(ch);
  820. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  821. /* Finish */
  822. chan_test_finish_close(ch);
  823. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  824. channel_run_cleanup();
  825. ch = NULL;
  826. /*
  827. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  828. * maintenance state
  829. */
  830. ch = new_fake_channel();
  831. tt_assert(ch);
  832. ch->state = CHANNEL_STATE_OPENING;
  833. channel_register(ch);
  834. tt_assert(ch->registered);
  835. /* Finish opening it */
  836. channel_change_state_open(ch);
  837. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  838. /* Go to maintenance state */
  839. channel_change_state(ch, CHANNEL_STATE_MAINT);
  840. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  841. /* Lower layer close */
  842. channel_close_from_lower_layer(ch);
  843. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  844. /* Finish */
  845. chan_test_finish_close(ch);
  846. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  847. channel_run_cleanup();
  848. ch = NULL;
  849. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  850. ch = new_fake_channel();
  851. tt_assert(ch);
  852. ch->state = CHANNEL_STATE_OPENING;
  853. channel_register(ch);
  854. tt_assert(ch->registered);
  855. /* Finish opening it */
  856. channel_change_state_open(ch);
  857. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  858. /* Go to maintenance state */
  859. channel_change_state(ch, CHANNEL_STATE_MAINT);
  860. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  861. /* Lower layer close */
  862. chan_test_error(ch);
  863. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  864. /* Finish */
  865. chan_test_finish_close(ch);
  866. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  867. channel_run_cleanup();
  868. ch = NULL;
  869. /* Shut down channels */
  870. channel_free_all();
  871. done:
  872. tor_free(ch);
  873. UNMOCK(scheduler_channel_doesnt_want_writes);
  874. UNMOCK(scheduler_release_channel);
  875. return;
  876. }
  877. static void
  878. test_channel_id_map(void *arg)
  879. {
  880. (void)arg;
  881. #define N_CHAN 6
  882. char rsa_id[N_CHAN][DIGEST_LEN];
  883. ed25519_public_key_t *ed_id[N_CHAN];
  884. channel_t *chan[N_CHAN];
  885. int i;
  886. ed25519_public_key_t ed_zero;
  887. memset(&ed_zero, 0, sizeof(ed_zero));
  888. tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
  889. for (i = 0; i < N_CHAN; ++i) {
  890. crypto_rand(rsa_id[i], DIGEST_LEN);
  891. ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
  892. crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
  893. }
  894. /* For channel 3, have no Ed identity. */
  895. tor_free(ed_id[3]);
  896. /* Channel 2 and 4 have same ROSA identity */
  897. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  898. /* Channel 2 and 4 and 5 have same RSA identity */
  899. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  900. memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
  901. /* Channels 2 and 5 have same Ed25519 identity */
  902. memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
  903. for (i = 0; i < N_CHAN; ++i) {
  904. chan[i] = new_fake_channel();
  905. channel_register(chan[i]);
  906. channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
  907. }
  908. /* Lookup by RSA id only */
  909. tt_ptr_op(chan[0], OP_EQ,
  910. channel_find_by_remote_identity(rsa_id[0], NULL));
  911. tt_ptr_op(chan[1], OP_EQ,
  912. channel_find_by_remote_identity(rsa_id[1], NULL));
  913. tt_ptr_op(chan[3], OP_EQ,
  914. channel_find_by_remote_identity(rsa_id[3], NULL));
  915. channel_t *ch;
  916. ch = channel_find_by_remote_identity(rsa_id[2], NULL);
  917. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  918. ch = channel_next_with_rsa_identity(ch);
  919. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  920. ch = channel_next_with_rsa_identity(ch);
  921. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  922. ch = channel_next_with_rsa_identity(ch);
  923. tt_ptr_op(ch, OP_EQ, NULL);
  924. /* As above, but with zero Ed25519 ID (meaning "any ID") */
  925. tt_ptr_op(chan[0], OP_EQ,
  926. channel_find_by_remote_identity(rsa_id[0], &ed_zero));
  927. tt_ptr_op(chan[1], OP_EQ,
  928. channel_find_by_remote_identity(rsa_id[1], &ed_zero));
  929. tt_ptr_op(chan[3], OP_EQ,
  930. channel_find_by_remote_identity(rsa_id[3], &ed_zero));
  931. ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
  932. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  933. ch = channel_next_with_rsa_identity(ch);
  934. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  935. ch = channel_next_with_rsa_identity(ch);
  936. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  937. ch = channel_next_with_rsa_identity(ch);
  938. tt_ptr_op(ch, OP_EQ, NULL);
  939. /* Lookup nonexistent RSA identity */
  940. tt_ptr_op(NULL, OP_EQ,
  941. channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
  942. /* Look up by full identity pair */
  943. tt_ptr_op(chan[0], OP_EQ,
  944. channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
  945. tt_ptr_op(chan[1], OP_EQ,
  946. channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
  947. tt_ptr_op(chan[3], OP_EQ,
  948. channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
  949. tt_ptr_op(chan[4], OP_EQ,
  950. channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
  951. ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
  952. tt_assert(ch == chan[2] || ch == chan[5]);
  953. /* Look up RSA identity with wrong ed25519 identity */
  954. tt_ptr_op(NULL, OP_EQ,
  955. channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
  956. tt_ptr_op(NULL, OP_EQ,
  957. channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
  958. tt_ptr_op(NULL, OP_EQ,
  959. channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
  960. done:
  961. for (i = 0; i < N_CHAN; ++i) {
  962. channel_clear_identity_digest(chan[i]);
  963. channel_unregister(chan[i]);
  964. free_fake_channel(chan[i]);
  965. tor_free(ed_id[i]);
  966. }
  967. #undef N_CHAN
  968. }
  969. static void
  970. test_channel_state(void *arg)
  971. {
  972. (void) arg;
  973. /* Test state validity. */
  974. tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1);
  975. tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1);
  976. tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1);
  977. tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1);
  978. tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1);
  979. tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1);
  980. tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0);
  981. tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0);
  982. /* Test listener state validity. */
  983. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED),
  984. OP_EQ, 1);
  985. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING),
  986. OP_EQ, 1);
  987. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING),
  988. OP_EQ, 1);
  989. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR),
  990. OP_EQ, 1);
  991. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST),
  992. OP_EQ, 0);
  993. tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0);
  994. /* Test state transition. */
  995. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
  996. CHANNEL_STATE_OPENING), OP_EQ, 1);
  997. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
  998. CHANNEL_STATE_ERROR), OP_EQ, 0);
  999. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  1000. CHANNEL_STATE_ERROR), OP_EQ, 1);
  1001. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  1002. CHANNEL_STATE_CLOSED), OP_EQ, 1);
  1003. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  1004. CHANNEL_STATE_OPEN), OP_EQ, 0);
  1005. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  1006. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  1007. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  1008. CHANNEL_STATE_ERROR), OP_EQ, 1);
  1009. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  1010. CHANNEL_STATE_OPEN), OP_EQ, 1);
  1011. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  1012. CHANNEL_STATE_OPENING), OP_EQ, 0);
  1013. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  1014. CHANNEL_STATE_OPEN), OP_EQ, 1);
  1015. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  1016. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  1017. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  1018. CHANNEL_STATE_ERROR), OP_EQ, 1);
  1019. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  1020. CHANNEL_STATE_ERROR), OP_EQ, 1);
  1021. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  1022. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  1023. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  1024. CHANNEL_STATE_ERROR), OP_EQ, 1);
  1025. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  1026. CHANNEL_STATE_MAINT), OP_EQ, 1);
  1027. tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST,
  1028. CHANNEL_STATE_MAINT), OP_EQ, 0);
  1029. tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX),
  1030. OP_EQ, 0);
  1031. /* Test listener state transition. */
  1032. tt_int_op(channel_listener_state_can_transition(
  1033. CHANNEL_LISTENER_STATE_CLOSED,
  1034. CHANNEL_LISTENER_STATE_LISTENING),
  1035. OP_EQ, 1);
  1036. tt_int_op(channel_listener_state_can_transition(
  1037. CHANNEL_LISTENER_STATE_CLOSED,
  1038. CHANNEL_LISTENER_STATE_ERROR),
  1039. OP_EQ, 0);
  1040. tt_int_op(channel_listener_state_can_transition(
  1041. CHANNEL_LISTENER_STATE_CLOSING,
  1042. CHANNEL_LISTENER_STATE_CLOSED),
  1043. OP_EQ, 1);
  1044. tt_int_op(channel_listener_state_can_transition(
  1045. CHANNEL_LISTENER_STATE_CLOSING,
  1046. CHANNEL_LISTENER_STATE_ERROR),
  1047. OP_EQ, 1);
  1048. tt_int_op(channel_listener_state_can_transition(
  1049. CHANNEL_LISTENER_STATE_ERROR,
  1050. CHANNEL_LISTENER_STATE_CLOSING),
  1051. OP_EQ, 0);
  1052. tt_int_op(channel_listener_state_can_transition(
  1053. CHANNEL_LISTENER_STATE_LISTENING,
  1054. CHANNEL_LISTENER_STATE_CLOSING),
  1055. OP_EQ, 1);
  1056. tt_int_op(channel_listener_state_can_transition(
  1057. CHANNEL_LISTENER_STATE_LISTENING,
  1058. CHANNEL_LISTENER_STATE_ERROR),
  1059. OP_EQ, 1);
  1060. tt_int_op(channel_listener_state_can_transition(
  1061. CHANNEL_LISTENER_STATE_LAST,
  1062. INT_MAX),
  1063. OP_EQ, 0);
  1064. /* Test state string. */
  1065. tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ,
  1066. "closing");
  1067. tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ,
  1068. "channel error");
  1069. tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ,
  1070. "closed");
  1071. tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ,
  1072. "open");
  1073. tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ,
  1074. "opening");
  1075. tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ,
  1076. "temporarily suspended for maintenance");
  1077. tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ,
  1078. "unknown or invalid channel state");
  1079. tt_str_op(channel_state_to_string(INT_MAX), OP_EQ,
  1080. "unknown or invalid channel state");
  1081. /* Test listener state string. */
  1082. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING),
  1083. OP_EQ, "closing");
  1084. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR),
  1085. OP_EQ, "channel listener error");
  1086. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING),
  1087. OP_EQ, "listening");
  1088. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST),
  1089. OP_EQ, "unknown or invalid channel listener state");
  1090. tt_str_op(channel_listener_state_to_string(INT_MAX),
  1091. OP_EQ, "unknown or invalid channel listener state");
  1092. done:
  1093. ;
  1094. }
  1095. static networkstatus_t *mock_ns = NULL;
  1096. static networkstatus_t *
  1097. mock_networkstatus_get_latest_consensus(void)
  1098. {
  1099. return mock_ns;
  1100. }
  1101. static void
  1102. test_channel_duplicates(void *arg)
  1103. {
  1104. channel_t *chan = NULL;
  1105. routerstatus_t rs;
  1106. (void) arg;
  1107. setup_full_capture_of_logs(LOG_INFO);
  1108. /* Try a flat call with channel nor connections. */
  1109. channel_check_for_duplicates();
  1110. expect_log_msg_containing(
  1111. "Found 0 connections to 0 relays. Found 0 current canonical "
  1112. "connections, in 0 of which we were a non-canonical peer. "
  1113. "0 relays had more than 1 connection, 0 had more than 2, and "
  1114. "0 had more than 4 connections.");
  1115. mock_ns = tor_malloc_zero(sizeof(*mock_ns));
  1116. mock_ns->routerstatus_list = smartlist_new();
  1117. MOCK(networkstatus_get_latest_consensus,
  1118. mock_networkstatus_get_latest_consensus);
  1119. chan = new_fake_channel();
  1120. tt_assert(chan);
  1121. chan->is_canonical = test_chan_is_canonical;
  1122. memset(chan->identity_digest, 'A', sizeof(chan->identity_digest));
  1123. channel_add_to_digest_map(chan);
  1124. tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL),
  1125. OP_EQ, chan);
  1126. /* No relay has been associated with this channel. */
  1127. channel_check_for_duplicates();
  1128. expect_log_msg_containing(
  1129. "Found 0 connections to 0 relays. Found 0 current canonical "
  1130. "connections, in 0 of which we were a non-canonical peer. "
  1131. "0 relays had more than 1 connection, 0 had more than 2, and "
  1132. "0 had more than 4 connections.");
  1133. /* Associate relay to this connection in the consensus. */
  1134. memset(&rs, 0, sizeof(rs));
  1135. memset(rs.identity_digest, 'A', sizeof(rs.identity_digest));
  1136. smartlist_add(mock_ns->routerstatus_list, &rs);
  1137. /* Non opened channel. */
  1138. chan->state = CHANNEL_STATE_CLOSING;
  1139. channel_check_for_duplicates();
  1140. expect_log_msg_containing(
  1141. "Found 0 connections to 0 relays. Found 0 current canonical "
  1142. "connections, in 0 of which we were a non-canonical peer. "
  1143. "0 relays had more than 1 connection, 0 had more than 2, and "
  1144. "0 had more than 4 connections.");
  1145. chan->state = CHANNEL_STATE_OPEN;
  1146. channel_check_for_duplicates();
  1147. expect_log_msg_containing(
  1148. "Found 1 connections to 1 relays. Found 0 current canonical "
  1149. "connections, in 0 of which we were a non-canonical peer. "
  1150. "0 relays had more than 1 connection, 0 had more than 2, and "
  1151. "0 had more than 4 connections.");
  1152. test_chan_should_be_canonical = 1;
  1153. channel_check_for_duplicates();
  1154. expect_log_msg_containing(
  1155. "Found 1 connections to 1 relays. Found 1 current canonical "
  1156. "connections, in 1 of which we were a non-canonical peer. "
  1157. "0 relays had more than 1 connection, 0 had more than 2, and "
  1158. "0 had more than 4 connections.");
  1159. teardown_capture_of_logs();
  1160. done:
  1161. free_fake_channel(chan);
  1162. smartlist_clear(mock_ns->routerstatus_list);
  1163. networkstatus_vote_free(mock_ns);
  1164. UNMOCK(networkstatus_get_latest_consensus);
  1165. }
  1166. static void
  1167. test_channel_for_extend(void *arg)
  1168. {
  1169. channel_t *chan1 = NULL, *chan2 = NULL;
  1170. channel_t *ret_chan = NULL;
  1171. char digest[DIGEST_LEN];
  1172. ed25519_public_key_t ed_id;
  1173. tor_addr_t addr;
  1174. const char *msg;
  1175. int launch;
  1176. time_t now = time(NULL);
  1177. (void) arg;
  1178. memset(digest, 'A', sizeof(digest));
  1179. memset(&ed_id, 'B', sizeof(ed_id));
  1180. chan1 = new_fake_channel();
  1181. tt_assert(chan1);
  1182. /* Need to be registered to get added to the id map. */
  1183. channel_register(chan1);
  1184. tt_int_op(chan1->registered, OP_EQ, 1);
  1185. /* We need those for the test. */
  1186. chan1->is_canonical = test_chan_is_canonical;
  1187. chan1->matches_target = test_chan_matches_target;
  1188. chan1->timestamp_created = now - 9;
  1189. chan2 = new_fake_channel();
  1190. tt_assert(chan2);
  1191. /* Need to be registered to get added to the id map. */
  1192. channel_register(chan2);
  1193. tt_int_op(chan2->registered, OP_EQ, 1);
  1194. /* We need those for the test. */
  1195. chan2->is_canonical = test_chan_is_canonical;
  1196. chan2->matches_target = test_chan_matches_target;
  1197. /* Make it older than chan1. */
  1198. chan2->timestamp_created = chan1->timestamp_created - 1;
  1199. /* Set channel identities and add it to the channel map. The last one to be
  1200. * added is made the first one in the list so the lookup will always return
  1201. * that one first. */
  1202. channel_set_identity_digest(chan2, digest, &ed_id);
  1203. channel_set_identity_digest(chan1, digest, &ed_id);
  1204. tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1);
  1205. tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1);
  1206. /* The expected result is chan2 because it is older than chan1. */
  1207. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1208. tt_assert(ret_chan);
  1209. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1210. tt_int_op(launch, OP_EQ, 0);
  1211. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1212. /* Switch that around from previous test. */
  1213. chan2->timestamp_created = chan1->timestamp_created + 1;
  1214. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1215. tt_assert(ret_chan);
  1216. tt_ptr_op(ret_chan, OP_EQ, chan1);
  1217. tt_int_op(launch, OP_EQ, 0);
  1218. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1219. /* Same creation time, num circuits will be used and they both have 0 so the
  1220. * channel 2 should be picked due to how channel_is_better() work. */
  1221. chan2->timestamp_created = chan1->timestamp_created;
  1222. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1223. tt_assert(ret_chan);
  1224. tt_ptr_op(ret_chan, OP_EQ, chan1);
  1225. tt_int_op(launch, OP_EQ, 0);
  1226. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1227. /* For the rest of the tests, we need channel 1 to be the older. */
  1228. chan2->timestamp_created = chan1->timestamp_created + 1;
  1229. /* Condemned the older channel. */
  1230. chan1->state = CHANNEL_STATE_CLOSING;
  1231. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1232. tt_assert(ret_chan);
  1233. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1234. tt_int_op(launch, OP_EQ, 0);
  1235. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1236. chan1->state = CHANNEL_STATE_OPEN;
  1237. /* Make the older channel a client one. */
  1238. channel_mark_client(chan1);
  1239. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1240. tt_assert(ret_chan);
  1241. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1242. tt_int_op(launch, OP_EQ, 0);
  1243. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1244. channel_clear_client(chan1);
  1245. /* Non matching ed identity with valid digest. */
  1246. ed25519_public_key_t dumb_ed_id = {0};
  1247. ret_chan = channel_get_for_extend(digest, &dumb_ed_id, &addr, &msg,
  1248. &launch);
  1249. tt_assert(!ret_chan);
  1250. tt_str_op(msg, OP_EQ, "Not connected. Connecting.");
  1251. tt_int_op(launch, OP_EQ, 1);
  1252. /* Opening channel, we'll check if the target address matches. */
  1253. test_chan_should_match_target = 1;
  1254. chan1->state = CHANNEL_STATE_OPENING;
  1255. chan2->state = CHANNEL_STATE_OPENING;
  1256. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1257. tt_assert(!ret_chan);
  1258. tt_str_op(msg, OP_EQ, "Connection in progress; waiting.");
  1259. tt_int_op(launch, OP_EQ, 0);
  1260. chan1->state = CHANNEL_STATE_OPEN;
  1261. chan2->state = CHANNEL_STATE_OPEN;
  1262. /* Mark channel 1 as bad for circuits. */
  1263. channel_mark_bad_for_new_circs(chan1);
  1264. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1265. tt_assert(ret_chan);
  1266. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1267. tt_int_op(launch, OP_EQ, 0);
  1268. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1269. chan1->is_bad_for_new_circs = 0;
  1270. /* Mark both channels as unusable. */
  1271. channel_mark_bad_for_new_circs(chan1);
  1272. channel_mark_bad_for_new_circs(chan2);
  1273. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1274. tt_assert(!ret_chan);
  1275. tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
  1276. " Launching a new one.");
  1277. tt_int_op(launch, OP_EQ, 1);
  1278. chan1->is_bad_for_new_circs = 0;
  1279. chan2->is_bad_for_new_circs = 0;
  1280. /* Non canonical channels. */
  1281. test_chan_should_match_target = 0;
  1282. test_chan_canonical_should_be_reliable = 1;
  1283. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1284. tt_assert(!ret_chan);
  1285. tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
  1286. " Launching a new one.");
  1287. tt_int_op(launch, OP_EQ, 1);
  1288. done:
  1289. free_fake_channel(chan1);
  1290. free_fake_channel(chan2);
  1291. }
  1292. struct testcase_t channel_tests[] = {
  1293. { "inbound_cell", test_channel_inbound_cell, TT_FORK,
  1294. NULL, NULL },
  1295. { "outbound_cell", test_channel_outbound_cell, TT_FORK,
  1296. NULL, NULL },
  1297. { "id_map", test_channel_id_map, TT_FORK,
  1298. NULL, NULL },
  1299. { "lifecycle", test_channel_lifecycle, TT_FORK,
  1300. NULL, NULL },
  1301. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
  1302. NULL, NULL },
  1303. { "dumpstats", test_channel_dumpstats, TT_FORK,
  1304. NULL, NULL },
  1305. { "state", test_channel_state, TT_FORK,
  1306. NULL, NULL },
  1307. { "duplicates", test_channel_duplicates, TT_FORK,
  1308. NULL, NULL },
  1309. { "get_channel_for_extend", test_channel_for_extend, TT_FORK,
  1310. NULL, NULL },
  1311. END_OF_TESTCASES
  1312. };