test_channel.c 47 KB

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