test_channel.c 47 KB

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