test_channel.c 47 KB

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