test_channel.c 47 KB

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