test_channel.c 49 KB

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