test_channel.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /* Copyright (c) 2013-2018, 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_ptr_op(TO_CIRCUIT(circ)->n_mux, OP_EQ, chan->cmux);
  487. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  488. OP_EQ, 1);
  489. /* Flush the channel without any cell on it. */
  490. old_count = test_cells_written;
  491. ssize_t flushed = channel_flush_some_cells(chan, 1);
  492. tt_i64_op(flushed, OP_EQ, 0);
  493. tt_int_op(test_cells_written, OP_EQ, old_count);
  494. tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
  495. tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
  496. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
  497. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  498. OP_EQ, 0);
  499. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
  500. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
  501. /* Queue cell onto the next queue that is the outbound direction. Than
  502. * update its cmux so the circuit can be picked when flushing cells. */
  503. cell_queue_append(queue, p_cell);
  504. p_cell = NULL;
  505. tt_int_op(queue->n, OP_EQ, 1);
  506. cell_queue_append(queue, p_cell2);
  507. p_cell2 = NULL;
  508. tt_int_op(queue->n, OP_EQ, 2);
  509. update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
  510. tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
  511. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
  512. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  513. OP_EQ, 1);
  514. /* From this point on, we have a queued cell on an active circuit attached
  515. * to the channel's cmux. */
  516. /* Flush the first cell. This is going to go down the call stack. */
  517. old_count = test_cells_written;
  518. flushed = channel_flush_some_cells(chan, 1);
  519. tt_i64_op(flushed, OP_EQ, 1);
  520. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  521. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
  522. tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
  523. /* Circuit should remain active because there is a second cell queued. */
  524. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  525. OP_EQ, 1);
  526. /* Should still be attached. */
  527. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  528. OP_EQ, 1);
  529. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
  530. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
  531. /* Flush second cell. This is going to go down the call stack. */
  532. old_count = test_cells_written;
  533. flushed = channel_flush_some_cells(chan, 1);
  534. tt_i64_op(flushed, OP_EQ, 1);
  535. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  536. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
  537. tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
  538. /* No more cells should make the circuit inactive. */
  539. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  540. OP_EQ, 0);
  541. /* Should still be attached. */
  542. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  543. OP_EQ, 1);
  544. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
  545. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
  546. done:
  547. if (circ) {
  548. circuit_free_(TO_CIRCUIT(circ));
  549. }
  550. tor_free(p_cell);
  551. channel_free_all();
  552. UNMOCK(scheduler_release_channel);
  553. monotime_disable_test_mocking();
  554. }
  555. /* Test inbound cell. The callstack is:
  556. * channel_process_cell()
  557. * -> chan->cell_handler()
  558. *
  559. * This test is about checking if we can process an inbound cell down to the
  560. * channel handler. */
  561. static void
  562. test_channel_inbound_cell(void *arg)
  563. {
  564. channel_t *chan = NULL;
  565. cell_t *cell = NULL;
  566. int old_count;
  567. (void) arg;
  568. /* The channel will be freed so we need to hijack this so the scheduler
  569. * doesn't get confused. */
  570. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  571. /* Accept cells to lower layer */
  572. test_chan_accept_cells = 1;
  573. chan = new_fake_channel();
  574. tt_assert(chan);
  575. /* Start it off in OPENING */
  576. chan->state = CHANNEL_STATE_OPENING;
  577. /* Try to register it */
  578. channel_register(chan);
  579. tt_int_op(chan->registered, OP_EQ, 1);
  580. /* Open it */
  581. channel_change_state_open(chan);
  582. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
  583. tt_int_op(chan->has_been_open, OP_EQ, 1);
  584. /* Receive a cell now. */
  585. cell = tor_malloc_zero(sizeof(*cell));
  586. make_fake_cell(cell);
  587. old_count = test_chan_fixed_cells_recved;
  588. channel_process_cell(chan, cell);
  589. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
  590. tt_assert(monotime_coarse_is_zero(&chan->timestamp_xfer));
  591. tt_u64_op(chan->timestamp_active, OP_EQ, 0);
  592. tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
  593. /* Setup incoming cell handlers. We don't care about var cell, the channel
  594. * layers is not handling those. */
  595. channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
  596. tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
  597. /* Now process the cell, we should see it. */
  598. old_count = test_chan_fixed_cells_recved;
  599. channel_process_cell(chan, cell);
  600. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  601. /* We should have a series of timestamp set. */
  602. tt_assert(!monotime_coarse_is_zero(&chan->timestamp_xfer));
  603. tt_u64_op(chan->timestamp_active, OP_NE, 0);
  604. tt_u64_op(chan->timestamp_recv, OP_NE, 0);
  605. tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
  606. tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
  607. tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
  608. /* Close it */
  609. old_count = test_close_called;
  610. channel_mark_for_close(chan);
  611. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
  612. tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
  613. tt_int_op(test_close_called, OP_EQ, old_count + 1);
  614. /* This closes the channe so it calls in the scheduler, make sure of it. */
  615. old_count = test_releases_count;
  616. chan_test_finish_close(chan);
  617. tt_int_op(test_releases_count, OP_EQ, old_count + 1);
  618. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
  619. /* The channel will be free, lets make sure it is not accessible. */
  620. uint64_t chan_id = chan->global_identifier;
  621. tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
  622. channel_run_cleanup();
  623. chan = channel_find_by_global_id(chan_id);
  624. tt_assert(chan == NULL);
  625. done:
  626. tor_free(cell);
  627. UNMOCK(scheduler_release_channel);
  628. }
  629. /**
  630. * Normal channel lifecycle test:
  631. *
  632. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  633. */
  634. static void
  635. test_channel_lifecycle(void *arg)
  636. {
  637. channel_t *ch1 = NULL, *ch2 = NULL;
  638. packed_cell_t *p_cell = NULL;
  639. int old_count, init_doesnt_want_writes_count;
  640. int init_releases_count;
  641. (void)arg;
  642. /* Mock these for the whole lifecycle test */
  643. MOCK(scheduler_channel_doesnt_want_writes,
  644. scheduler_channel_doesnt_want_writes_mock);
  645. MOCK(scheduler_release_channel,
  646. scheduler_release_channel_mock);
  647. /* Cache some initial counter values */
  648. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  649. init_releases_count = test_releases_count;
  650. /* Accept cells to lower layer */
  651. test_chan_accept_cells = 1;
  652. ch1 = new_fake_channel();
  653. tt_assert(ch1);
  654. /* Start it off in OPENING */
  655. ch1->state = CHANNEL_STATE_OPENING;
  656. /* Try to register it */
  657. channel_register(ch1);
  658. tt_assert(ch1->registered);
  659. /* Try to write a cell through (should queue) */
  660. p_cell = packed_cell_new();
  661. old_count = test_cells_written;
  662. channel_write_packed_cell(ch1, p_cell);
  663. tt_int_op(old_count, OP_EQ, test_cells_written);
  664. /* Move it to OPEN and flush */
  665. channel_change_state_open(ch1);
  666. /* Get another one */
  667. ch2 = new_fake_channel();
  668. tt_assert(ch2);
  669. ch2->state = CHANNEL_STATE_OPENING;
  670. /* Register */
  671. channel_register(ch2);
  672. tt_assert(ch2->registered);
  673. /* Check counters */
  674. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  675. init_doesnt_want_writes_count);
  676. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  677. /* Move ch1 to MAINT */
  678. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  679. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  680. init_doesnt_want_writes_count + 1);
  681. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  682. /* Move ch2 to OPEN */
  683. channel_change_state_open(ch2);
  684. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  685. init_doesnt_want_writes_count + 1);
  686. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  687. /* Move ch1 back to OPEN */
  688. channel_change_state_open(ch1);
  689. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  690. init_doesnt_want_writes_count + 1);
  691. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  692. /* Mark ch2 for close */
  693. channel_mark_for_close(ch2);
  694. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  695. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  696. init_doesnt_want_writes_count + 1);
  697. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
  698. /* Shut down channels */
  699. channel_free_all();
  700. ch1 = ch2 = NULL;
  701. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  702. init_doesnt_want_writes_count + 1);
  703. /* channel_free() calls scheduler_release_channel() */
  704. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
  705. done:
  706. free_fake_channel(ch1);
  707. free_fake_channel(ch2);
  708. UNMOCK(scheduler_channel_doesnt_want_writes);
  709. UNMOCK(scheduler_release_channel);
  710. }
  711. /**
  712. * Weird channel lifecycle test:
  713. *
  714. * OPENING->CLOSING->CLOSED
  715. * OPENING->OPEN->CLOSING->ERROR
  716. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  717. * OPENING->OPEN->MAINT->CLOSING->ERROR
  718. */
  719. static void
  720. test_channel_lifecycle_2(void *arg)
  721. {
  722. channel_t *ch = NULL;
  723. (void)arg;
  724. /* Mock these for the whole lifecycle test */
  725. MOCK(scheduler_channel_doesnt_want_writes,
  726. scheduler_channel_doesnt_want_writes_mock);
  727. MOCK(scheduler_release_channel,
  728. scheduler_release_channel_mock);
  729. /* Accept cells to lower layer */
  730. test_chan_accept_cells = 1;
  731. ch = new_fake_channel();
  732. tt_assert(ch);
  733. /* Start it off in OPENING */
  734. ch->state = CHANNEL_STATE_OPENING;
  735. /* Try to register it */
  736. channel_register(ch);
  737. tt_assert(ch->registered);
  738. /* Try to close it */
  739. channel_mark_for_close(ch);
  740. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  741. /* Finish closing it */
  742. chan_test_finish_close(ch);
  743. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  744. channel_run_cleanup();
  745. ch = NULL;
  746. /* Now try OPENING->OPEN->CLOSING->ERROR */
  747. ch = new_fake_channel();
  748. tt_assert(ch);
  749. ch->state = CHANNEL_STATE_OPENING;
  750. channel_register(ch);
  751. tt_assert(ch->registered);
  752. /* Finish opening it */
  753. channel_change_state_open(ch);
  754. /* Error exit from lower layer */
  755. chan_test_error(ch);
  756. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  757. chan_test_finish_close(ch);
  758. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  759. channel_run_cleanup();
  760. ch = NULL;
  761. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  762. ch = new_fake_channel();
  763. tt_assert(ch);
  764. ch->state = CHANNEL_STATE_OPENING;
  765. channel_register(ch);
  766. tt_assert(ch->registered);
  767. /* Finish opening it */
  768. channel_change_state_open(ch);
  769. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  770. /* Go to maintenance state */
  771. channel_change_state(ch, CHANNEL_STATE_MAINT);
  772. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  773. /* Lower layer close */
  774. channel_mark_for_close(ch);
  775. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  776. /* Finish */
  777. chan_test_finish_close(ch);
  778. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  779. channel_run_cleanup();
  780. ch = NULL;
  781. /*
  782. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  783. * maintenance state
  784. */
  785. ch = new_fake_channel();
  786. tt_assert(ch);
  787. ch->state = CHANNEL_STATE_OPENING;
  788. channel_register(ch);
  789. tt_assert(ch->registered);
  790. /* Finish opening it */
  791. channel_change_state_open(ch);
  792. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  793. /* Go to maintenance state */
  794. channel_change_state(ch, CHANNEL_STATE_MAINT);
  795. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  796. /* Lower layer close */
  797. channel_close_from_lower_layer(ch);
  798. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  799. /* Finish */
  800. chan_test_finish_close(ch);
  801. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  802. channel_run_cleanup();
  803. ch = NULL;
  804. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  805. ch = new_fake_channel();
  806. tt_assert(ch);
  807. ch->state = CHANNEL_STATE_OPENING;
  808. channel_register(ch);
  809. tt_assert(ch->registered);
  810. /* Finish opening it */
  811. channel_change_state_open(ch);
  812. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  813. /* Go to maintenance state */
  814. channel_change_state(ch, CHANNEL_STATE_MAINT);
  815. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  816. /* Lower layer close */
  817. chan_test_error(ch);
  818. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  819. /* Finish */
  820. chan_test_finish_close(ch);
  821. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  822. channel_run_cleanup();
  823. ch = NULL;
  824. /* Shut down channels */
  825. channel_free_all();
  826. done:
  827. tor_free(ch);
  828. UNMOCK(scheduler_channel_doesnt_want_writes);
  829. UNMOCK(scheduler_release_channel);
  830. return;
  831. }
  832. static void
  833. test_channel_id_map(void *arg)
  834. {
  835. (void)arg;
  836. #define N_CHAN 6
  837. char rsa_id[N_CHAN][DIGEST_LEN];
  838. ed25519_public_key_t *ed_id[N_CHAN];
  839. channel_t *chan[N_CHAN];
  840. int i;
  841. ed25519_public_key_t ed_zero;
  842. memset(&ed_zero, 0, sizeof(ed_zero));
  843. tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
  844. for (i = 0; i < N_CHAN; ++i) {
  845. crypto_rand(rsa_id[i], DIGEST_LEN);
  846. ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
  847. crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
  848. }
  849. /* For channel 3, have no Ed identity. */
  850. tor_free(ed_id[3]);
  851. /* Channel 2 and 4 have same ROSA identity */
  852. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  853. /* Channel 2 and 4 and 5 have same RSA identity */
  854. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  855. memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
  856. /* Channels 2 and 5 have same Ed25519 identity */
  857. memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
  858. for (i = 0; i < N_CHAN; ++i) {
  859. chan[i] = new_fake_channel();
  860. channel_register(chan[i]);
  861. channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
  862. }
  863. /* Lookup by RSA id only */
  864. tt_ptr_op(chan[0], OP_EQ,
  865. channel_find_by_remote_identity(rsa_id[0], NULL));
  866. tt_ptr_op(chan[1], OP_EQ,
  867. channel_find_by_remote_identity(rsa_id[1], NULL));
  868. tt_ptr_op(chan[3], OP_EQ,
  869. channel_find_by_remote_identity(rsa_id[3], NULL));
  870. channel_t *ch;
  871. ch = channel_find_by_remote_identity(rsa_id[2], NULL);
  872. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  873. ch = channel_next_with_rsa_identity(ch);
  874. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  875. ch = channel_next_with_rsa_identity(ch);
  876. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  877. ch = channel_next_with_rsa_identity(ch);
  878. tt_ptr_op(ch, OP_EQ, NULL);
  879. /* As above, but with zero Ed25519 ID (meaning "any ID") */
  880. tt_ptr_op(chan[0], OP_EQ,
  881. channel_find_by_remote_identity(rsa_id[0], &ed_zero));
  882. tt_ptr_op(chan[1], OP_EQ,
  883. channel_find_by_remote_identity(rsa_id[1], &ed_zero));
  884. tt_ptr_op(chan[3], OP_EQ,
  885. channel_find_by_remote_identity(rsa_id[3], &ed_zero));
  886. ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
  887. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  888. ch = channel_next_with_rsa_identity(ch);
  889. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  890. ch = channel_next_with_rsa_identity(ch);
  891. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  892. ch = channel_next_with_rsa_identity(ch);
  893. tt_ptr_op(ch, OP_EQ, NULL);
  894. /* Lookup nonexistent RSA identity */
  895. tt_ptr_op(NULL, OP_EQ,
  896. channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
  897. /* Look up by full identity pair */
  898. tt_ptr_op(chan[0], OP_EQ,
  899. channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
  900. tt_ptr_op(chan[1], OP_EQ,
  901. channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
  902. tt_ptr_op(chan[3], OP_EQ,
  903. channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
  904. tt_ptr_op(chan[4], OP_EQ,
  905. channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
  906. ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
  907. tt_assert(ch == chan[2] || ch == chan[5]);
  908. /* Look up RSA identity with wrong ed25519 identity */
  909. tt_ptr_op(NULL, OP_EQ,
  910. channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
  911. tt_ptr_op(NULL, OP_EQ,
  912. channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
  913. tt_ptr_op(NULL, OP_EQ,
  914. channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
  915. done:
  916. for (i = 0; i < N_CHAN; ++i) {
  917. channel_clear_identity_digest(chan[i]);
  918. channel_unregister(chan[i]);
  919. free_fake_channel(chan[i]);
  920. tor_free(ed_id[i]);
  921. }
  922. #undef N_CHAN
  923. }
  924. static void
  925. test_channel_state(void *arg)
  926. {
  927. (void) arg;
  928. /* Test state validity. */
  929. tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1);
  930. tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1);
  931. tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1);
  932. tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1);
  933. tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1);
  934. tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1);
  935. tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0);
  936. tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0);
  937. /* Test listener state validity. */
  938. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED),
  939. OP_EQ, 1);
  940. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING),
  941. OP_EQ, 1);
  942. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING),
  943. OP_EQ, 1);
  944. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR),
  945. OP_EQ, 1);
  946. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST),
  947. OP_EQ, 0);
  948. tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0);
  949. /* Test state transition. */
  950. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
  951. CHANNEL_STATE_OPENING), OP_EQ, 1);
  952. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
  953. CHANNEL_STATE_ERROR), OP_EQ, 0);
  954. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  955. CHANNEL_STATE_ERROR), OP_EQ, 1);
  956. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  957. CHANNEL_STATE_CLOSED), OP_EQ, 1);
  958. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  959. CHANNEL_STATE_OPEN), OP_EQ, 0);
  960. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  961. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  962. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  963. CHANNEL_STATE_ERROR), OP_EQ, 1);
  964. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  965. CHANNEL_STATE_OPEN), OP_EQ, 1);
  966. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  967. CHANNEL_STATE_OPENING), OP_EQ, 0);
  968. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  969. CHANNEL_STATE_OPEN), OP_EQ, 1);
  970. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  971. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  972. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  973. CHANNEL_STATE_ERROR), 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_CLOSING), OP_EQ, 1);
  978. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  979. CHANNEL_STATE_ERROR), OP_EQ, 1);
  980. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  981. CHANNEL_STATE_MAINT), OP_EQ, 1);
  982. tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST,
  983. CHANNEL_STATE_MAINT), OP_EQ, 0);
  984. tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX),
  985. OP_EQ, 0);
  986. /* Test listener state transition. */
  987. tt_int_op(channel_listener_state_can_transition(
  988. CHANNEL_LISTENER_STATE_CLOSED,
  989. CHANNEL_LISTENER_STATE_LISTENING),
  990. OP_EQ, 1);
  991. tt_int_op(channel_listener_state_can_transition(
  992. CHANNEL_LISTENER_STATE_CLOSED,
  993. CHANNEL_LISTENER_STATE_ERROR),
  994. OP_EQ, 0);
  995. tt_int_op(channel_listener_state_can_transition(
  996. CHANNEL_LISTENER_STATE_CLOSING,
  997. CHANNEL_LISTENER_STATE_CLOSED),
  998. OP_EQ, 1);
  999. tt_int_op(channel_listener_state_can_transition(
  1000. CHANNEL_LISTENER_STATE_CLOSING,
  1001. CHANNEL_LISTENER_STATE_ERROR),
  1002. OP_EQ, 1);
  1003. tt_int_op(channel_listener_state_can_transition(
  1004. CHANNEL_LISTENER_STATE_ERROR,
  1005. CHANNEL_LISTENER_STATE_CLOSING),
  1006. OP_EQ, 0);
  1007. tt_int_op(channel_listener_state_can_transition(
  1008. CHANNEL_LISTENER_STATE_LISTENING,
  1009. CHANNEL_LISTENER_STATE_CLOSING),
  1010. OP_EQ, 1);
  1011. tt_int_op(channel_listener_state_can_transition(
  1012. CHANNEL_LISTENER_STATE_LISTENING,
  1013. CHANNEL_LISTENER_STATE_ERROR),
  1014. OP_EQ, 1);
  1015. tt_int_op(channel_listener_state_can_transition(
  1016. CHANNEL_LISTENER_STATE_LAST,
  1017. INT_MAX),
  1018. OP_EQ, 0);
  1019. /* Test state string. */
  1020. tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ,
  1021. "closing");
  1022. tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ,
  1023. "channel error");
  1024. tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ,
  1025. "closed");
  1026. tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ,
  1027. "open");
  1028. tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ,
  1029. "opening");
  1030. tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ,
  1031. "temporarily suspended for maintenance");
  1032. tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ,
  1033. "unknown or invalid channel state");
  1034. tt_str_op(channel_state_to_string(INT_MAX), OP_EQ,
  1035. "unknown or invalid channel state");
  1036. /* Test listener state string. */
  1037. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING),
  1038. OP_EQ, "closing");
  1039. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR),
  1040. OP_EQ, "channel listener error");
  1041. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING),
  1042. OP_EQ, "listening");
  1043. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST),
  1044. OP_EQ, "unknown or invalid channel listener state");
  1045. tt_str_op(channel_listener_state_to_string(INT_MAX),
  1046. OP_EQ, "unknown or invalid channel listener state");
  1047. done:
  1048. ;
  1049. }
  1050. static networkstatus_t *mock_ns = NULL;
  1051. static networkstatus_t *
  1052. mock_networkstatus_get_latest_consensus(void)
  1053. {
  1054. return mock_ns;
  1055. }
  1056. static void
  1057. test_channel_duplicates(void *arg)
  1058. {
  1059. channel_t *chan = NULL;
  1060. routerstatus_t rs;
  1061. (void) arg;
  1062. setup_full_capture_of_logs(LOG_INFO);
  1063. /* Try a flat call with channel nor connections. */
  1064. channel_check_for_duplicates();
  1065. expect_log_msg_containing(
  1066. "Found 0 connections to 0 relays. Found 0 current canonical "
  1067. "connections, in 0 of which we were a non-canonical peer. "
  1068. "0 relays had more than 1 connection, 0 had more than 2, and "
  1069. "0 had more than 4 connections.");
  1070. mock_ns = tor_malloc_zero(sizeof(*mock_ns));
  1071. mock_ns->routerstatus_list = smartlist_new();
  1072. MOCK(networkstatus_get_latest_consensus,
  1073. mock_networkstatus_get_latest_consensus);
  1074. chan = new_fake_channel();
  1075. tt_assert(chan);
  1076. chan->is_canonical = test_chan_is_canonical;
  1077. memset(chan->identity_digest, 'A', sizeof(chan->identity_digest));
  1078. channel_add_to_digest_map(chan);
  1079. tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL),
  1080. OP_EQ, chan);
  1081. /* No relay has been associated with this channel. */
  1082. channel_check_for_duplicates();
  1083. expect_log_msg_containing(
  1084. "Found 0 connections to 0 relays. Found 0 current canonical "
  1085. "connections, in 0 of which we were a non-canonical peer. "
  1086. "0 relays had more than 1 connection, 0 had more than 2, and "
  1087. "0 had more than 4 connections.");
  1088. /* Associate relay to this connection in the consensus. */
  1089. memset(&rs, 0, sizeof(rs));
  1090. memset(rs.identity_digest, 'A', sizeof(rs.identity_digest));
  1091. smartlist_add(mock_ns->routerstatus_list, &rs);
  1092. /* Non opened channel. */
  1093. chan->state = CHANNEL_STATE_CLOSING;
  1094. channel_check_for_duplicates();
  1095. expect_log_msg_containing(
  1096. "Found 0 connections to 0 relays. Found 0 current canonical "
  1097. "connections, in 0 of which we were a non-canonical peer. "
  1098. "0 relays had more than 1 connection, 0 had more than 2, and "
  1099. "0 had more than 4 connections.");
  1100. chan->state = CHANNEL_STATE_OPEN;
  1101. channel_check_for_duplicates();
  1102. expect_log_msg_containing(
  1103. "Found 1 connections to 1 relays. Found 0 current canonical "
  1104. "connections, in 0 of which we were a non-canonical peer. "
  1105. "0 relays had more than 1 connection, 0 had more than 2, and "
  1106. "0 had more than 4 connections.");
  1107. test_chan_should_be_canonical = 1;
  1108. channel_check_for_duplicates();
  1109. expect_log_msg_containing(
  1110. "Found 1 connections to 1 relays. Found 1 current canonical "
  1111. "connections, in 1 of which we were a non-canonical peer. "
  1112. "0 relays had more than 1 connection, 0 had more than 2, and "
  1113. "0 had more than 4 connections.");
  1114. teardown_capture_of_logs();
  1115. done:
  1116. free_fake_channel(chan);
  1117. smartlist_clear(mock_ns->routerstatus_list);
  1118. networkstatus_vote_free(mock_ns);
  1119. UNMOCK(networkstatus_get_latest_consensus);
  1120. }
  1121. static void
  1122. test_channel_for_extend(void *arg)
  1123. {
  1124. channel_t *chan1 = NULL, *chan2 = NULL;
  1125. channel_t *ret_chan = NULL;
  1126. char digest[DIGEST_LEN];
  1127. ed25519_public_key_t ed_id;
  1128. tor_addr_t addr;
  1129. const char *msg;
  1130. int launch;
  1131. time_t now = time(NULL);
  1132. (void) arg;
  1133. memset(digest, 'A', sizeof(digest));
  1134. memset(&ed_id, 'B', sizeof(ed_id));
  1135. chan1 = new_fake_channel();
  1136. tt_assert(chan1);
  1137. /* Need to be registered to get added to the id map. */
  1138. channel_register(chan1);
  1139. tt_int_op(chan1->registered, OP_EQ, 1);
  1140. /* We need those for the test. */
  1141. chan1->is_canonical = test_chan_is_canonical;
  1142. chan1->matches_target = test_chan_matches_target;
  1143. chan1->timestamp_created = now - 9;
  1144. chan2 = new_fake_channel();
  1145. tt_assert(chan2);
  1146. /* Need to be registered to get added to the id map. */
  1147. channel_register(chan2);
  1148. tt_int_op(chan2->registered, OP_EQ, 1);
  1149. /* We need those for the test. */
  1150. chan2->is_canonical = test_chan_is_canonical;
  1151. chan2->matches_target = test_chan_matches_target;
  1152. /* Make it older than chan1. */
  1153. chan2->timestamp_created = chan1->timestamp_created - 1;
  1154. /* Set channel identities and add it to the channel map. The last one to be
  1155. * added is made the first one in the list so the lookup will always return
  1156. * that one first. */
  1157. channel_set_identity_digest(chan2, digest, &ed_id);
  1158. channel_set_identity_digest(chan1, digest, &ed_id);
  1159. tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1);
  1160. tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1);
  1161. /* The expected result is chan2 because it is older than chan1. */
  1162. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1163. tt_assert(ret_chan);
  1164. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1165. tt_int_op(launch, OP_EQ, 0);
  1166. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1167. /* Switch that around from previous test. */
  1168. chan2->timestamp_created = chan1->timestamp_created + 1;
  1169. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1170. tt_assert(ret_chan);
  1171. tt_ptr_op(ret_chan, OP_EQ, chan1);
  1172. tt_int_op(launch, OP_EQ, 0);
  1173. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1174. /* Same creation time, num circuits will be used and they both have 0 so the
  1175. * channel 2 should be picked due to how channel_is_better() work. */
  1176. chan2->timestamp_created = chan1->timestamp_created;
  1177. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1178. tt_assert(ret_chan);
  1179. tt_ptr_op(ret_chan, OP_EQ, chan1);
  1180. tt_int_op(launch, OP_EQ, 0);
  1181. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1182. /* For the rest of the tests, we need channel 1 to be the older. */
  1183. chan2->timestamp_created = chan1->timestamp_created + 1;
  1184. /* Condemned the older channel. */
  1185. chan1->state = CHANNEL_STATE_CLOSING;
  1186. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1187. tt_assert(ret_chan);
  1188. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1189. tt_int_op(launch, OP_EQ, 0);
  1190. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1191. chan1->state = CHANNEL_STATE_OPEN;
  1192. /* Make the older channel a client one. */
  1193. channel_mark_client(chan1);
  1194. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1195. tt_assert(ret_chan);
  1196. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1197. tt_int_op(launch, OP_EQ, 0);
  1198. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1199. channel_clear_client(chan1);
  1200. /* Non matching ed identity with valid digest. */
  1201. ed25519_public_key_t dumb_ed_id;
  1202. memset(&dumb_ed_id, 0, sizeof(dumb_ed_id));
  1203. ret_chan = channel_get_for_extend(digest, &dumb_ed_id, &addr, &msg,
  1204. &launch);
  1205. tt_assert(!ret_chan);
  1206. tt_str_op(msg, OP_EQ, "Not connected. Connecting.");
  1207. tt_int_op(launch, OP_EQ, 1);
  1208. /* Opening channel, we'll check if the target address matches. */
  1209. test_chan_should_match_target = 1;
  1210. chan1->state = CHANNEL_STATE_OPENING;
  1211. chan2->state = CHANNEL_STATE_OPENING;
  1212. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1213. tt_assert(!ret_chan);
  1214. tt_str_op(msg, OP_EQ, "Connection in progress; waiting.");
  1215. tt_int_op(launch, OP_EQ, 0);
  1216. chan1->state = CHANNEL_STATE_OPEN;
  1217. chan2->state = CHANNEL_STATE_OPEN;
  1218. /* Mark channel 1 as bad for circuits. */
  1219. channel_mark_bad_for_new_circs(chan1);
  1220. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1221. tt_assert(ret_chan);
  1222. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1223. tt_int_op(launch, OP_EQ, 0);
  1224. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1225. chan1->is_bad_for_new_circs = 0;
  1226. /* Mark both channels as unusable. */
  1227. channel_mark_bad_for_new_circs(chan1);
  1228. channel_mark_bad_for_new_circs(chan2);
  1229. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1230. tt_assert(!ret_chan);
  1231. tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
  1232. " Launching a new one.");
  1233. tt_int_op(launch, OP_EQ, 1);
  1234. chan1->is_bad_for_new_circs = 0;
  1235. chan2->is_bad_for_new_circs = 0;
  1236. /* Non canonical channels. */
  1237. test_chan_should_match_target = 0;
  1238. test_chan_canonical_should_be_reliable = 1;
  1239. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1240. tt_assert(!ret_chan);
  1241. tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
  1242. " Launching a new one.");
  1243. tt_int_op(launch, OP_EQ, 1);
  1244. done:
  1245. free_fake_channel(chan1);
  1246. free_fake_channel(chan2);
  1247. }
  1248. static void
  1249. test_channel_listener(void *arg)
  1250. {
  1251. int old_count;
  1252. time_t now = time(NULL);
  1253. channel_listener_t *chan = NULL;
  1254. (void) arg;
  1255. chan = tor_malloc_zero(sizeof(*chan));
  1256. tt_assert(chan);
  1257. channel_init_listener(chan);
  1258. tt_u64_op(chan->global_identifier, OP_EQ, 1);
  1259. tt_int_op(chan->timestamp_created, OP_GE, now);
  1260. chan->close = test_chan_listener_close;
  1261. /* Register it. At this point, it is not open so it will be put in the
  1262. * finished list. */
  1263. channel_listener_register(chan);
  1264. tt_int_op(chan->registered, OP_EQ, 1);
  1265. channel_listener_unregister(chan);
  1266. /* Register it as listening now thus active. */
  1267. chan->state = CHANNEL_LISTENER_STATE_LISTENING;
  1268. channel_listener_register(chan);
  1269. tt_int_op(chan->registered, OP_EQ, 1);
  1270. /* Set the listener function. */
  1271. channel_listener_set_listener_fn(chan, test_chan_listener_fn);
  1272. tt_ptr_op(chan->listener, OP_EQ, test_chan_listener_fn);
  1273. /* Put a channel in the listener incoming list and queue it.
  1274. * function. By doing this, the listener() handler will be called. */
  1275. channel_t *in_chan = new_fake_channel();
  1276. old_count = test_chan_listener_fn_called;
  1277. channel_listener_queue_incoming(chan, in_chan);
  1278. free_fake_channel(in_chan);
  1279. tt_int_op(test_chan_listener_fn_called, OP_EQ, old_count + 1);
  1280. /* Put listener channel in CLOSING state. */
  1281. old_count = test_chan_listener_close_fn_called;
  1282. channel_listener_mark_for_close(chan);
  1283. tt_int_op(test_chan_listener_close_fn_called, OP_EQ, old_count + 1);
  1284. channel_listener_change_state(chan, CHANNEL_LISTENER_STATE_CLOSED);
  1285. /* Dump stats so we at least hit the code path. */
  1286. chan->describe_transport = test_chan_listener_describe_transport;
  1287. /* There is a check for "now > timestamp_created" when dumping the stats so
  1288. * make sure we go in. */
  1289. chan->timestamp_created = now - 10;
  1290. channel_listener_dump_statistics(chan, LOG_INFO);
  1291. done:
  1292. channel_free_all();
  1293. }
  1294. struct testcase_t channel_tests[] = {
  1295. { "inbound_cell", test_channel_inbound_cell, TT_FORK,
  1296. NULL, NULL },
  1297. { "outbound_cell", test_channel_outbound_cell, TT_FORK,
  1298. NULL, NULL },
  1299. { "id_map", test_channel_id_map, TT_FORK,
  1300. NULL, NULL },
  1301. { "lifecycle", test_channel_lifecycle, TT_FORK,
  1302. NULL, NULL },
  1303. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
  1304. NULL, NULL },
  1305. { "dumpstats", test_channel_dumpstats, TT_FORK,
  1306. NULL, NULL },
  1307. { "state", test_channel_state, TT_FORK,
  1308. NULL, NULL },
  1309. { "duplicates", test_channel_duplicates, TT_FORK,
  1310. NULL, NULL },
  1311. { "get_channel_for_extend", test_channel_for_extend, TT_FORK,
  1312. NULL, NULL },
  1313. { "listener", test_channel_listener, TT_FORK,
  1314. NULL, NULL },
  1315. END_OF_TESTCASES
  1316. };