test_channel.c 46 KB

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