test_channel.c 47 KB

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