test_channel.c 47 KB

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