test_channel.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /* Copyright (c) 2013, 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. #include "circuitlist.h"
  9. #include "circuitmux.h"
  10. /* For var_cell_free */
  11. #include "connection_or.h"
  12. /* For packed_cell stuff */
  13. #define RELAY_PRIVATE
  14. #include "relay.h"
  15. /* For init/free stuff */
  16. #include "scheduler.h"
  17. /* Test suite stuff */
  18. #include "test.h"
  19. #include "fakechans.h"
  20. /* This comes from channel.c */
  21. extern uint64_t estimated_total_queue_size;
  22. static int test_chan_accept_cells = 0;
  23. static int test_chan_fixed_cells_recved = 0;
  24. static int test_chan_var_cells_recved = 0;
  25. static int test_cells_written = 0;
  26. static int test_destroy_not_pending_calls = 0;
  27. static int test_doesnt_want_writes_count = 0;
  28. static int test_dumpstats_calls = 0;
  29. static int test_has_waiting_cells_count = 0;
  30. static double test_overhead_estimate = 1.0f;
  31. static int test_releases_count = 0;
  32. static circuitmux_t *test_target_cmux = NULL;
  33. static unsigned int test_cmux_cells = 0;
  34. static channel_t *dump_statistics_mock_target = NULL;
  35. static int dump_statistics_mock_matches = 0;
  36. static void chan_test_channel_dump_statistics_mock(
  37. channel_t *chan, int severity);
  38. static int chan_test_channel_flush_from_first_active_circuit_mock(
  39. channel_t *chan, int max);
  40. static unsigned int chan_test_circuitmux_num_cells_mock(circuitmux_t *cmux);
  41. static void channel_note_destroy_not_pending_mock(channel_t *ch,
  42. circid_t circid);
  43. static void chan_test_cell_handler(channel_t *ch,
  44. cell_t *cell);
  45. static const char * chan_test_describe_transport(channel_t *ch);
  46. static void chan_test_dumpstats(channel_t *ch, int severity);
  47. static void chan_test_var_cell_handler(channel_t *ch,
  48. var_cell_t *var_cell);
  49. static void chan_test_close(channel_t *ch);
  50. static void chan_test_error(channel_t *ch);
  51. static void chan_test_finish_close(channel_t *ch);
  52. static const char * chan_test_get_remote_descr(channel_t *ch, int flags);
  53. static int chan_test_is_canonical(channel_t *ch, int req);
  54. static size_t chan_test_num_bytes_queued(channel_t *ch);
  55. static int chan_test_num_cells_writeable(channel_t *ch);
  56. static int chan_test_write_cell(channel_t *ch, cell_t *cell);
  57. static int chan_test_write_packed_cell(channel_t *ch,
  58. packed_cell_t *packed_cell);
  59. static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
  60. static void scheduler_channel_doesnt_want_writes_mock(channel_t *ch);
  61. static void test_channel_dumpstats(void *arg);
  62. static void test_channel_flush(void *arg);
  63. static void test_channel_flushmux(void *arg);
  64. static void test_channel_incoming(void *arg);
  65. static void test_channel_lifecycle(void *arg);
  66. static void test_channel_multi(void *arg);
  67. static void test_channel_queue_size(void *arg);
  68. static void test_channel_write(void *arg);
  69. static void
  70. channel_note_destroy_not_pending_mock(channel_t *ch,
  71. circid_t circid)
  72. {
  73. (void)ch;
  74. (void)circid;
  75. ++test_destroy_not_pending_calls;
  76. }
  77. static const char *
  78. chan_test_describe_transport(channel_t *ch)
  79. {
  80. tt_assert(ch != NULL);
  81. done:
  82. return "Fake channel for unit tests";
  83. }
  84. /**
  85. * Mock for channel_dump_statistics(); if the channel matches the
  86. * target, bump a counter - otherwise ignore.
  87. */
  88. static void
  89. chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
  90. {
  91. tt_assert(chan != NULL);
  92. (void)severity;
  93. if (chan != NULL && chan == dump_statistics_mock_target) {
  94. ++dump_statistics_mock_matches;
  95. }
  96. done:
  97. return;
  98. }
  99. /**
  100. * If the target cmux is the cmux for chan, make fake cells up to the
  101. * target number of cells and write them to chan. Otherwise, invoke
  102. * the real channel_flush_from_first_active_circuit().
  103. */
  104. static int
  105. chan_test_channel_flush_from_first_active_circuit_mock(channel_t *chan,
  106. int max)
  107. {
  108. int result = 0, c = 0;
  109. packed_cell_t *cell = NULL;
  110. tt_assert(chan != NULL);
  111. if (test_target_cmux != NULL &&
  112. test_target_cmux == chan->cmux) {
  113. while (c <= max && test_cmux_cells > 0) {
  114. cell = packed_cell_new();
  115. channel_write_packed_cell(chan, cell);
  116. ++c;
  117. --test_cmux_cells;
  118. }
  119. result = c;
  120. } else {
  121. result = channel_flush_from_first_active_circuit__real(chan, max);
  122. }
  123. done:
  124. return result;
  125. }
  126. /**
  127. * If we have a target cmux set and this matches it, lie about how
  128. * many cells we have according to the number indicated; otherwise
  129. * pass to the real circuitmux_num_cells().
  130. */
  131. static unsigned int
  132. chan_test_circuitmux_num_cells_mock(circuitmux_t *cmux)
  133. {
  134. unsigned int result = 0;
  135. tt_assert(cmux != NULL);
  136. if (cmux != NULL) {
  137. if (cmux == test_target_cmux) {
  138. result = test_cmux_cells;
  139. } else {
  140. result = circuitmux_num_cells__real(cmux);
  141. }
  142. }
  143. done:
  144. return result;
  145. }
  146. /*
  147. * Handle an incoming fixed-size cell for unit tests
  148. */
  149. static void
  150. chan_test_cell_handler(channel_t *ch,
  151. cell_t *cell)
  152. {
  153. tt_assert(ch);
  154. tt_assert(cell);
  155. tor_free(cell);
  156. ++test_chan_fixed_cells_recved;
  157. done:
  158. return;
  159. }
  160. /*
  161. * Fake transport-specific stats call
  162. */
  163. static void
  164. chan_test_dumpstats(channel_t *ch, int severity)
  165. {
  166. tt_assert(ch != NULL);
  167. (void)severity;
  168. ++test_dumpstats_calls;
  169. done:
  170. return;
  171. }
  172. /*
  173. * Handle an incoming variable-size cell for unit tests
  174. */
  175. static void
  176. chan_test_var_cell_handler(channel_t *ch,
  177. var_cell_t *var_cell)
  178. {
  179. tt_assert(ch);
  180. tt_assert(var_cell);
  181. tor_free(var_cell);
  182. ++test_chan_var_cells_recved;
  183. done:
  184. return;
  185. }
  186. static void
  187. chan_test_close(channel_t *ch)
  188. {
  189. tt_assert(ch);
  190. done:
  191. return;
  192. }
  193. /*
  194. * Close a channel through the error path
  195. */
  196. static void
  197. chan_test_error(channel_t *ch)
  198. {
  199. tt_assert(ch);
  200. tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
  201. ch->state == CHANNEL_STATE_ERROR ||
  202. ch->state == CHANNEL_STATE_CLOSED));
  203. channel_close_for_error(ch);
  204. done:
  205. return;
  206. }
  207. /*
  208. * Finish closing a channel from CHANNEL_STATE_CLOSING
  209. */
  210. static void
  211. chan_test_finish_close(channel_t *ch)
  212. {
  213. tt_assert(ch);
  214. tt_assert(ch->state == CHANNEL_STATE_CLOSING);
  215. channel_closed(ch);
  216. done:
  217. return;
  218. }
  219. static const char *
  220. chan_test_get_remote_descr(channel_t *ch, int flags)
  221. {
  222. tt_assert(ch);
  223. tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), ==, 0);
  224. done:
  225. return "Fake channel for unit tests; no real endpoint";
  226. }
  227. static double
  228. chan_test_get_overhead_estimate(channel_t *ch)
  229. {
  230. tt_assert(ch);
  231. done:
  232. return test_overhead_estimate;
  233. }
  234. static int
  235. chan_test_is_canonical(channel_t *ch, int req)
  236. {
  237. tt_assert(ch != NULL);
  238. tt_assert(req == 0 || req == 1);
  239. done:
  240. /* Fake channels are always canonical */
  241. return 1;
  242. }
  243. static size_t
  244. chan_test_num_bytes_queued(channel_t *ch)
  245. {
  246. tt_assert(ch);
  247. done:
  248. return 0;
  249. }
  250. static int
  251. chan_test_num_cells_writeable(channel_t *ch)
  252. {
  253. tt_assert(ch);
  254. done:
  255. return 32;
  256. }
  257. static int
  258. chan_test_write_cell(channel_t *ch, cell_t *cell)
  259. {
  260. int rv = 0;
  261. tt_assert(ch);
  262. tt_assert(cell);
  263. if (test_chan_accept_cells) {
  264. /* Free the cell and bump the counter */
  265. tor_free(cell);
  266. ++test_cells_written;
  267. rv = 1;
  268. }
  269. /* else return 0, we didn't accept it */
  270. done:
  271. return rv;
  272. }
  273. static int
  274. chan_test_write_packed_cell(channel_t *ch,
  275. packed_cell_t *packed_cell)
  276. {
  277. int rv = 0;
  278. tt_assert(ch);
  279. tt_assert(packed_cell);
  280. if (test_chan_accept_cells) {
  281. /* Free the cell and bump the counter */
  282. packed_cell_free(packed_cell);
  283. ++test_cells_written;
  284. rv = 1;
  285. }
  286. /* else return 0, we didn't accept it */
  287. done:
  288. return rv;
  289. }
  290. static int
  291. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  292. {
  293. int rv = 0;
  294. tt_assert(ch);
  295. tt_assert(var_cell);
  296. if (test_chan_accept_cells) {
  297. /* Free the cell and bump the counter */
  298. var_cell_free(var_cell);
  299. ++test_cells_written;
  300. rv = 1;
  301. }
  302. /* else return 0, we didn't accept it */
  303. done:
  304. return rv;
  305. }
  306. /**
  307. * Fill out c with a new fake cell for test suite use
  308. */
  309. void
  310. make_fake_cell(cell_t *c)
  311. {
  312. tt_assert(c != NULL);
  313. c->circ_id = 1;
  314. c->command = CELL_RELAY;
  315. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  316. done:
  317. return;
  318. }
  319. /**
  320. * Fill out c with a new fake var_cell for test suite use
  321. */
  322. void
  323. make_fake_var_cell(var_cell_t *c)
  324. {
  325. tt_assert(c != NULL);
  326. c->circ_id = 1;
  327. c->command = CELL_VERSIONS;
  328. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  329. memset(c->payload, 0, c->payload_len);
  330. done:
  331. return;
  332. }
  333. /**
  334. * Set up a new fake channel for the test suite
  335. */
  336. channel_t *
  337. new_fake_channel(void)
  338. {
  339. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  340. channel_init(chan);
  341. chan->close = chan_test_close;
  342. chan->get_overhead_estimate = chan_test_get_overhead_estimate;
  343. chan->num_bytes_queued = chan_test_num_bytes_queued;
  344. chan->num_cells_writeable = chan_test_num_cells_writeable;
  345. chan->write_cell = chan_test_write_cell;
  346. chan->write_packed_cell = chan_test_write_packed_cell;
  347. chan->write_var_cell = chan_test_write_var_cell;
  348. chan->state = CHANNEL_STATE_OPEN;
  349. return chan;
  350. }
  351. void
  352. free_fake_channel(channel_t *chan)
  353. {
  354. cell_queue_entry_t *cell, *cell_tmp;
  355. if (! chan)
  356. return;
  357. if (chan->cmux)
  358. circuitmux_free(chan->cmux);
  359. TOR_SIMPLEQ_FOREACH_SAFE(cell, &chan->incoming_queue, next, cell_tmp) {
  360. cell_queue_entry_free(cell, 0);
  361. }
  362. TOR_SIMPLEQ_FOREACH_SAFE(cell, &chan->outgoing_queue, next, cell_tmp) {
  363. cell_queue_entry_free(cell, 0);
  364. }
  365. tor_free(chan);
  366. }
  367. /**
  368. * Counter query for scheduler_channel_has_waiting_cells_mock()
  369. */
  370. int
  371. get_mock_scheduler_has_waiting_cells_count(void)
  372. {
  373. return test_has_waiting_cells_count;
  374. }
  375. /**
  376. * Mock for scheduler_channel_has_waiting_cells()
  377. */
  378. void
  379. scheduler_channel_has_waiting_cells_mock(channel_t *ch)
  380. {
  381. (void)ch;
  382. /* Increment counter */
  383. ++test_has_waiting_cells_count;
  384. return;
  385. }
  386. static void
  387. scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
  388. {
  389. (void)ch;
  390. /* Increment counter */
  391. ++test_doesnt_want_writes_count;
  392. return;
  393. }
  394. /**
  395. * Counter query for scheduler_release_channel_mock()
  396. */
  397. int
  398. get_mock_scheduler_release_channel_count(void)
  399. {
  400. return test_releases_count;
  401. }
  402. /**
  403. * Mock for scheduler_release_channel()
  404. */
  405. void
  406. scheduler_release_channel_mock(channel_t *ch)
  407. {
  408. (void)ch;
  409. /* Increment counter */
  410. ++test_releases_count;
  411. return;
  412. }
  413. /**
  414. * Test for channel_dumpstats() and limited test for
  415. * channel_dump_statistics()
  416. */
  417. static void
  418. test_channel_dumpstats(void *arg)
  419. {
  420. channel_t *ch = NULL;
  421. cell_t *cell = NULL;
  422. int old_count;
  423. (void)arg;
  424. /* Mock these for duration of the test */
  425. MOCK(scheduler_channel_doesnt_want_writes,
  426. scheduler_channel_doesnt_want_writes_mock);
  427. MOCK(scheduler_release_channel,
  428. scheduler_release_channel_mock);
  429. /* Set up a new fake channel */
  430. ch = new_fake_channel();
  431. tt_assert(ch);
  432. ch->cmux = circuitmux_alloc();
  433. /* Try to register it */
  434. channel_register(ch);
  435. tt_assert(ch->registered);
  436. /* Set up mock */
  437. dump_statistics_mock_target = ch;
  438. dump_statistics_mock_matches = 0;
  439. MOCK(channel_dump_statistics,
  440. chan_test_channel_dump_statistics_mock);
  441. /* Call channel_dumpstats() */
  442. channel_dumpstats(LOG_DEBUG);
  443. /* Assert that we hit the mock */
  444. tt_int_op(dump_statistics_mock_matches, ==, 1);
  445. /* Close the channel */
  446. channel_mark_for_close(ch);
  447. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  448. chan_test_finish_close(ch);
  449. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  450. /* Try again and hit the finished channel */
  451. channel_dumpstats(LOG_DEBUG);
  452. tt_int_op(dump_statistics_mock_matches, ==, 2);
  453. channel_run_cleanup();
  454. ch = NULL;
  455. /* Now we should hit nothing */
  456. channel_dumpstats(LOG_DEBUG);
  457. tt_int_op(dump_statistics_mock_matches, ==, 2);
  458. /* Unmock */
  459. UNMOCK(channel_dump_statistics);
  460. dump_statistics_mock_target = NULL;
  461. dump_statistics_mock_matches = 0;
  462. /* Now make another channel */
  463. ch = new_fake_channel();
  464. tt_assert(ch);
  465. ch->cmux = circuitmux_alloc();
  466. channel_register(ch);
  467. tt_assert(ch->registered);
  468. /* Lie about its age so dumpstats gets coverage for rate calculations */
  469. ch->timestamp_created = time(NULL) - 30;
  470. tt_assert(ch->timestamp_created > 0);
  471. tt_assert(time(NULL) > ch->timestamp_created);
  472. /* Put cells through it both ways to make the counters non-zero */
  473. cell = tor_malloc_zero(sizeof(*cell));
  474. make_fake_cell(cell);
  475. test_chan_accept_cells = 1;
  476. old_count = test_cells_written;
  477. channel_write_cell(ch, cell);
  478. cell = NULL;
  479. tt_int_op(test_cells_written, ==, old_count + 1);
  480. tt_assert(ch->n_bytes_xmitted > 0);
  481. tt_assert(ch->n_cells_xmitted > 0);
  482. /* Receive path */
  483. channel_set_cell_handlers(ch,
  484. chan_test_cell_handler,
  485. chan_test_var_cell_handler);
  486. tt_ptr_op(channel_get_cell_handler(ch), ==, chan_test_cell_handler);
  487. tt_ptr_op(channel_get_var_cell_handler(ch), ==, chan_test_var_cell_handler);
  488. cell = tor_malloc_zero(sizeof(cell_t));
  489. make_fake_cell(cell);
  490. old_count = test_chan_fixed_cells_recved;
  491. channel_queue_cell(ch, cell);
  492. cell = NULL;
  493. tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
  494. tt_assert(ch->n_bytes_recved > 0);
  495. tt_assert(ch->n_cells_recved > 0);
  496. /* Test channel_dump_statistics */
  497. ch->describe_transport = chan_test_describe_transport;
  498. ch->dumpstats = chan_test_dumpstats;
  499. ch->get_remote_descr = chan_test_get_remote_descr;
  500. ch->is_canonical = chan_test_is_canonical;
  501. old_count = test_dumpstats_calls;
  502. channel_dump_statistics(ch, LOG_DEBUG);
  503. tt_int_op(test_dumpstats_calls, ==, old_count + 1);
  504. /* Close the channel */
  505. channel_mark_for_close(ch);
  506. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  507. chan_test_finish_close(ch);
  508. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  509. channel_run_cleanup();
  510. ch = NULL;
  511. done:
  512. tor_free(cell);
  513. free_fake_channel(ch);
  514. UNMOCK(scheduler_channel_doesnt_want_writes);
  515. UNMOCK(scheduler_release_channel);
  516. return;
  517. }
  518. static void
  519. test_channel_flush(void *arg)
  520. {
  521. channel_t *ch = NULL;
  522. cell_t *cell = NULL;
  523. packed_cell_t *p_cell = NULL;
  524. var_cell_t *v_cell = NULL;
  525. int init_count;
  526. (void)arg;
  527. #ifdef ENABLE_MEMPOOLS
  528. init_cell_pool();
  529. #endif /* ENABLE_MEMPOOLS */
  530. ch = new_fake_channel();
  531. tt_assert(ch);
  532. /* Cache the original count */
  533. init_count = test_cells_written;
  534. /* Stop accepting so we can queue some */
  535. test_chan_accept_cells = 0;
  536. /* Queue a regular cell */
  537. cell = tor_malloc_zero(sizeof(cell_t));
  538. make_fake_cell(cell);
  539. channel_write_cell(ch, cell);
  540. /* It should be queued, so assert that we didn't write it */
  541. tt_int_op(test_cells_written, ==, init_count);
  542. /* Queue a var cell */
  543. v_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  544. make_fake_var_cell(v_cell);
  545. channel_write_var_cell(ch, v_cell);
  546. /* It should be queued, so assert that we didn't write it */
  547. tt_int_op(test_cells_written, ==, init_count);
  548. /* Try a packed cell now */
  549. p_cell = packed_cell_new();
  550. tt_assert(p_cell);
  551. channel_write_packed_cell(ch, p_cell);
  552. /* It should be queued, so assert that we didn't write it */
  553. tt_int_op(test_cells_written, ==, init_count);
  554. /* Now allow writes through again */
  555. test_chan_accept_cells = 1;
  556. /* ...and flush */
  557. channel_flush_cells(ch);
  558. /* All three should have gone through */
  559. tt_int_op(test_cells_written, ==, init_count + 3);
  560. done:
  561. tor_free(ch);
  562. #ifdef ENABLE_MEMPOOLS
  563. free_cell_pool();
  564. #endif /* ENABLE_MEMPOOLS */
  565. return;
  566. }
  567. /**
  568. * Channel flush tests that require cmux mocking
  569. */
  570. static void
  571. test_channel_flushmux(void *arg)
  572. {
  573. channel_t *ch = NULL;
  574. int old_count, q_len_before, q_len_after;
  575. ssize_t result;
  576. (void)arg;
  577. #ifdef ENABLE_MEMPOOLS
  578. init_cell_pool();
  579. #endif /* ENABLE_MEMPOOLS */
  580. /* Install mocks we need for this test */
  581. MOCK(channel_flush_from_first_active_circuit,
  582. chan_test_channel_flush_from_first_active_circuit_mock);
  583. MOCK(circuitmux_num_cells,
  584. chan_test_circuitmux_num_cells_mock);
  585. ch = new_fake_channel();
  586. tt_assert(ch);
  587. ch->cmux = circuitmux_alloc();
  588. old_count = test_cells_written;
  589. test_target_cmux = ch->cmux;
  590. test_cmux_cells = 1;
  591. /* Enable cell acceptance */
  592. test_chan_accept_cells = 1;
  593. result = channel_flush_some_cells(ch, 1);
  594. tt_int_op(result, ==, 1);
  595. tt_int_op(test_cells_written, ==, old_count + 1);
  596. tt_int_op(test_cmux_cells, ==, 0);
  597. /* Now try it without accepting to force them into the queue */
  598. test_chan_accept_cells = 0;
  599. test_cmux_cells = 1;
  600. q_len_before = chan_cell_queue_len(&(ch->outgoing_queue));
  601. result = channel_flush_some_cells(ch, 1);
  602. /* We should not have actually flushed any */
  603. tt_int_op(result, ==, 0);
  604. tt_int_op(test_cells_written, ==, old_count + 1);
  605. /* But we should have gotten to the fake cellgen loop */
  606. tt_int_op(test_cmux_cells, ==, 0);
  607. /* ...and we should have a queued cell */
  608. q_len_after = chan_cell_queue_len(&(ch->outgoing_queue));
  609. tt_int_op(q_len_after, ==, q_len_before + 1);
  610. /* Now accept cells again and drain the queue */
  611. test_chan_accept_cells = 1;
  612. channel_flush_cells(ch);
  613. tt_int_op(test_cells_written, ==, old_count + 2);
  614. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  615. test_target_cmux = NULL;
  616. test_cmux_cells = 0;
  617. done:
  618. if (ch)
  619. circuitmux_free(ch->cmux);
  620. tor_free(ch);
  621. UNMOCK(channel_flush_from_first_active_circuit);
  622. UNMOCK(circuitmux_num_cells);
  623. test_chan_accept_cells = 0;
  624. #ifdef ENABLE_MEMPOOLS
  625. free_cell_pool();
  626. #endif /* ENABLE_MEMPOOLS */
  627. return;
  628. }
  629. static void
  630. test_channel_incoming(void *arg)
  631. {
  632. channel_t *ch = NULL;
  633. cell_t *cell = NULL;
  634. var_cell_t *var_cell = NULL;
  635. int old_count;
  636. (void)arg;
  637. /* Mock these for duration of the test */
  638. MOCK(scheduler_channel_doesnt_want_writes,
  639. scheduler_channel_doesnt_want_writes_mock);
  640. MOCK(scheduler_release_channel,
  641. scheduler_release_channel_mock);
  642. /* Accept cells to lower layer */
  643. test_chan_accept_cells = 1;
  644. /* Use default overhead factor */
  645. test_overhead_estimate = 1.0f;
  646. ch = new_fake_channel();
  647. tt_assert(ch);
  648. /* Start it off in OPENING */
  649. ch->state = CHANNEL_STATE_OPENING;
  650. /* We'll need a cmux */
  651. ch->cmux = circuitmux_alloc();
  652. /* Install incoming cell handlers */
  653. channel_set_cell_handlers(ch,
  654. chan_test_cell_handler,
  655. chan_test_var_cell_handler);
  656. /* Test cell handler getters */
  657. tt_ptr_op(channel_get_cell_handler(ch), ==, chan_test_cell_handler);
  658. tt_ptr_op(channel_get_var_cell_handler(ch), ==, chan_test_var_cell_handler);
  659. /* Try to register it */
  660. channel_register(ch);
  661. tt_assert(ch->registered);
  662. /* Open it */
  663. channel_change_state(ch, CHANNEL_STATE_OPEN);
  664. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  665. /* Receive a fixed cell */
  666. cell = tor_malloc_zero(sizeof(cell_t));
  667. make_fake_cell(cell);
  668. old_count = test_chan_fixed_cells_recved;
  669. channel_queue_cell(ch, cell);
  670. cell = NULL;
  671. tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
  672. /* Receive a variable-size cell */
  673. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  674. make_fake_var_cell(var_cell);
  675. old_count = test_chan_var_cells_recved;
  676. channel_queue_var_cell(ch, var_cell);
  677. var_cell = NULL;
  678. tt_int_op(test_chan_var_cells_recved, ==, old_count + 1);
  679. /* Close it */
  680. channel_mark_for_close(ch);
  681. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  682. chan_test_finish_close(ch);
  683. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  684. channel_run_cleanup();
  685. ch = NULL;
  686. done:
  687. free_fake_channel(ch);
  688. tor_free(cell);
  689. tor_free(var_cell);
  690. UNMOCK(scheduler_channel_doesnt_want_writes);
  691. UNMOCK(scheduler_release_channel);
  692. return;
  693. }
  694. /**
  695. * Normal channel lifecycle test:
  696. *
  697. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  698. */
  699. static void
  700. test_channel_lifecycle(void *arg)
  701. {
  702. channel_t *ch1 = NULL, *ch2 = NULL;
  703. cell_t *cell = NULL;
  704. int old_count, init_doesnt_want_writes_count;
  705. int init_releases_count;
  706. (void)arg;
  707. /* Mock these for the whole lifecycle test */
  708. MOCK(scheduler_channel_doesnt_want_writes,
  709. scheduler_channel_doesnt_want_writes_mock);
  710. MOCK(scheduler_release_channel,
  711. scheduler_release_channel_mock);
  712. /* Cache some initial counter values */
  713. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  714. init_releases_count = test_releases_count;
  715. /* Accept cells to lower layer */
  716. test_chan_accept_cells = 1;
  717. /* Use default overhead factor */
  718. test_overhead_estimate = 1.0f;
  719. ch1 = new_fake_channel();
  720. tt_assert(ch1);
  721. /* Start it off in OPENING */
  722. ch1->state = CHANNEL_STATE_OPENING;
  723. /* We'll need a cmux */
  724. ch1->cmux = circuitmux_alloc();
  725. /* Try to register it */
  726. channel_register(ch1);
  727. tt_assert(ch1->registered);
  728. /* Try to write a cell through (should queue) */
  729. cell = tor_malloc_zero(sizeof(cell_t));
  730. make_fake_cell(cell);
  731. old_count = test_cells_written;
  732. channel_write_cell(ch1, cell);
  733. tt_int_op(old_count, ==, test_cells_written);
  734. /* Move it to OPEN and flush */
  735. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  736. /* Queue should drain */
  737. tt_int_op(old_count + 1, ==, test_cells_written);
  738. /* Get another one */
  739. ch2 = new_fake_channel();
  740. tt_assert(ch2);
  741. ch2->state = CHANNEL_STATE_OPENING;
  742. ch2->cmux = circuitmux_alloc();
  743. /* Register */
  744. channel_register(ch2);
  745. tt_assert(ch2->registered);
  746. /* Check counters */
  747. tt_int_op(test_doesnt_want_writes_count, ==, init_doesnt_want_writes_count);
  748. tt_int_op(test_releases_count, ==, init_releases_count);
  749. /* Move ch1 to MAINT */
  750. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  751. tt_int_op(test_doesnt_want_writes_count, ==,
  752. init_doesnt_want_writes_count + 1);
  753. tt_int_op(test_releases_count, ==, init_releases_count);
  754. /* Move ch2 to OPEN */
  755. channel_change_state(ch2, CHANNEL_STATE_OPEN);
  756. tt_int_op(test_doesnt_want_writes_count, ==,
  757. init_doesnt_want_writes_count + 1);
  758. tt_int_op(test_releases_count, ==, init_releases_count);
  759. /* Move ch1 back to OPEN */
  760. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  761. tt_int_op(test_doesnt_want_writes_count, ==,
  762. init_doesnt_want_writes_count + 1);
  763. tt_int_op(test_releases_count, ==, init_releases_count);
  764. /* Mark ch2 for close */
  765. channel_mark_for_close(ch2);
  766. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSING);
  767. tt_int_op(test_doesnt_want_writes_count, ==,
  768. init_doesnt_want_writes_count + 1);
  769. tt_int_op(test_releases_count, ==, init_releases_count + 1);
  770. /* Shut down channels */
  771. channel_free_all();
  772. ch1 = ch2 = NULL;
  773. tt_int_op(test_doesnt_want_writes_count, ==,
  774. init_doesnt_want_writes_count + 1);
  775. /* channel_free() calls scheduler_release_channel() */
  776. tt_int_op(test_releases_count, ==, init_releases_count + 4);
  777. done:
  778. free_fake_channel(ch1);
  779. free_fake_channel(ch2);
  780. UNMOCK(scheduler_channel_doesnt_want_writes);
  781. UNMOCK(scheduler_release_channel);
  782. return;
  783. }
  784. /**
  785. * Weird channel lifecycle test:
  786. *
  787. * OPENING->CLOSING->CLOSED
  788. * OPENING->OPEN->CLOSING->ERROR
  789. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  790. * OPENING->OPEN->MAINT->CLOSING->ERROR
  791. */
  792. static void
  793. test_channel_lifecycle_2(void *arg)
  794. {
  795. channel_t *ch = NULL;
  796. (void)arg;
  797. /* Mock these for the whole lifecycle test */
  798. MOCK(scheduler_channel_doesnt_want_writes,
  799. scheduler_channel_doesnt_want_writes_mock);
  800. MOCK(scheduler_release_channel,
  801. scheduler_release_channel_mock);
  802. /* Accept cells to lower layer */
  803. test_chan_accept_cells = 1;
  804. /* Use default overhead factor */
  805. test_overhead_estimate = 1.0f;
  806. ch = new_fake_channel();
  807. tt_assert(ch);
  808. /* Start it off in OPENING */
  809. ch->state = CHANNEL_STATE_OPENING;
  810. /* The full lifecycle test needs a cmux */
  811. ch->cmux = circuitmux_alloc();
  812. /* Try to register it */
  813. channel_register(ch);
  814. tt_assert(ch->registered);
  815. /* Try to close it */
  816. channel_mark_for_close(ch);
  817. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  818. /* Finish closing it */
  819. chan_test_finish_close(ch);
  820. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  821. channel_run_cleanup();
  822. ch = NULL;
  823. /* Now try OPENING->OPEN->CLOSING->ERROR */
  824. ch = new_fake_channel();
  825. tt_assert(ch);
  826. ch->state = CHANNEL_STATE_OPENING;
  827. ch->cmux = circuitmux_alloc();
  828. channel_register(ch);
  829. tt_assert(ch->registered);
  830. /* Finish opening it */
  831. channel_change_state(ch, CHANNEL_STATE_OPEN);
  832. /* Error exit from lower layer */
  833. chan_test_error(ch);
  834. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  835. chan_test_finish_close(ch);
  836. tt_int_op(ch->state, ==, CHANNEL_STATE_ERROR);
  837. channel_run_cleanup();
  838. ch = NULL;
  839. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  840. ch = new_fake_channel();
  841. tt_assert(ch);
  842. ch->state = CHANNEL_STATE_OPENING;
  843. ch->cmux = circuitmux_alloc();
  844. channel_register(ch);
  845. tt_assert(ch->registered);
  846. /* Finish opening it */
  847. channel_change_state(ch, CHANNEL_STATE_OPEN);
  848. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  849. /* Go to maintenance state */
  850. channel_change_state(ch, CHANNEL_STATE_MAINT);
  851. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  852. /* Lower layer close */
  853. channel_mark_for_close(ch);
  854. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  855. /* Finish */
  856. chan_test_finish_close(ch);
  857. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  858. channel_run_cleanup();
  859. ch = NULL;
  860. /*
  861. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  862. * maintenance state
  863. */
  864. ch = new_fake_channel();
  865. tt_assert(ch);
  866. ch->state = CHANNEL_STATE_OPENING;
  867. ch->cmux = circuitmux_alloc();
  868. channel_register(ch);
  869. tt_assert(ch->registered);
  870. /* Finish opening it */
  871. channel_change_state(ch, CHANNEL_STATE_OPEN);
  872. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  873. /* Go to maintenance state */
  874. channel_change_state(ch, CHANNEL_STATE_MAINT);
  875. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  876. /* Lower layer close */
  877. channel_close_from_lower_layer(ch);
  878. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  879. /* Finish */
  880. chan_test_finish_close(ch);
  881. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  882. channel_run_cleanup();
  883. ch = NULL;
  884. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  885. ch = new_fake_channel();
  886. tt_assert(ch);
  887. ch->state = CHANNEL_STATE_OPENING;
  888. ch->cmux = circuitmux_alloc();
  889. channel_register(ch);
  890. tt_assert(ch->registered);
  891. /* Finish opening it */
  892. channel_change_state(ch, CHANNEL_STATE_OPEN);
  893. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  894. /* Go to maintenance state */
  895. channel_change_state(ch, CHANNEL_STATE_MAINT);
  896. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  897. /* Lower layer close */
  898. chan_test_error(ch);
  899. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  900. /* Finish */
  901. chan_test_finish_close(ch);
  902. tt_int_op(ch->state, ==, CHANNEL_STATE_ERROR);
  903. channel_run_cleanup();
  904. ch = NULL;
  905. /* Shut down channels */
  906. channel_free_all();
  907. done:
  908. tor_free(ch);
  909. UNMOCK(scheduler_channel_doesnt_want_writes);
  910. UNMOCK(scheduler_release_channel);
  911. return;
  912. }
  913. static void
  914. test_channel_multi(void *arg)
  915. {
  916. channel_t *ch1 = NULL, *ch2 = NULL;
  917. uint64_t global_queue_estimate;
  918. cell_t *cell = NULL;
  919. (void)arg;
  920. /* Accept cells to lower layer */
  921. test_chan_accept_cells = 1;
  922. /* Use default overhead factor */
  923. test_overhead_estimate = 1.0f;
  924. ch1 = new_fake_channel();
  925. tt_assert(ch1);
  926. ch2 = new_fake_channel();
  927. tt_assert(ch2);
  928. /* Initial queue size update */
  929. channel_update_xmit_queue_size(ch1);
  930. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  931. channel_update_xmit_queue_size(ch2);
  932. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  933. global_queue_estimate = channel_get_global_queue_estimate();
  934. tt_u64_op(global_queue_estimate, ==, 0);
  935. /* Queue some cells, check queue estimates */
  936. cell = tor_malloc_zero(sizeof(cell_t));
  937. make_fake_cell(cell);
  938. channel_write_cell(ch1, cell);
  939. cell = tor_malloc_zero(sizeof(cell_t));
  940. make_fake_cell(cell);
  941. channel_write_cell(ch2, cell);
  942. channel_update_xmit_queue_size(ch1);
  943. channel_update_xmit_queue_size(ch2);
  944. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  945. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  946. global_queue_estimate = channel_get_global_queue_estimate();
  947. tt_u64_op(global_queue_estimate, ==, 0);
  948. /* Stop accepting cells at lower layer */
  949. test_chan_accept_cells = 0;
  950. /* Queue some cells and check queue estimates */
  951. cell = tor_malloc_zero(sizeof(cell_t));
  952. make_fake_cell(cell);
  953. channel_write_cell(ch1, cell);
  954. channel_update_xmit_queue_size(ch1);
  955. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  956. global_queue_estimate = channel_get_global_queue_estimate();
  957. tt_u64_op(global_queue_estimate, ==, 512);
  958. cell = tor_malloc_zero(sizeof(cell_t));
  959. make_fake_cell(cell);
  960. channel_write_cell(ch2, cell);
  961. channel_update_xmit_queue_size(ch2);
  962. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 512);
  963. global_queue_estimate = channel_get_global_queue_estimate();
  964. tt_u64_op(global_queue_estimate, ==, 1024);
  965. /* Allow cells through again */
  966. test_chan_accept_cells = 1;
  967. /* Flush chan 2 */
  968. channel_flush_cells(ch2);
  969. /* Update and check queue sizes */
  970. channel_update_xmit_queue_size(ch1);
  971. channel_update_xmit_queue_size(ch2);
  972. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  973. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  974. global_queue_estimate = channel_get_global_queue_estimate();
  975. tt_u64_op(global_queue_estimate, ==, 512);
  976. /* Flush chan 1 */
  977. channel_flush_cells(ch1);
  978. /* Update and check queue sizes */
  979. channel_update_xmit_queue_size(ch1);
  980. channel_update_xmit_queue_size(ch2);
  981. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  982. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  983. global_queue_estimate = channel_get_global_queue_estimate();
  984. tt_u64_op(global_queue_estimate, ==, 0);
  985. /* Now block again */
  986. test_chan_accept_cells = 0;
  987. /* Queue some cells */
  988. cell = tor_malloc_zero(sizeof(cell_t));
  989. make_fake_cell(cell);
  990. channel_write_cell(ch1, cell);
  991. cell = tor_malloc_zero(sizeof(cell_t));
  992. make_fake_cell(cell);
  993. channel_write_cell(ch2, cell);
  994. cell = NULL;
  995. /* Check the estimates */
  996. channel_update_xmit_queue_size(ch1);
  997. channel_update_xmit_queue_size(ch2);
  998. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  999. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 512);
  1000. global_queue_estimate = channel_get_global_queue_estimate();
  1001. tt_u64_op(global_queue_estimate, ==, 1024);
  1002. /* Now close channel 2; it should be subtracted from the global queue */
  1003. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1004. channel_mark_for_close(ch2);
  1005. UNMOCK(scheduler_release_channel);
  1006. global_queue_estimate = channel_get_global_queue_estimate();
  1007. tt_u64_op(global_queue_estimate, ==, 512);
  1008. /*
  1009. * Since the fake channels aren't registered, channel_free_all() can't
  1010. * see them properly.
  1011. */
  1012. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1013. channel_mark_for_close(ch1);
  1014. UNMOCK(scheduler_release_channel);
  1015. global_queue_estimate = channel_get_global_queue_estimate();
  1016. tt_u64_op(global_queue_estimate, ==, 0);
  1017. /* Now free everything */
  1018. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1019. channel_free_all();
  1020. UNMOCK(scheduler_release_channel);
  1021. done:
  1022. free_fake_channel(ch1);
  1023. free_fake_channel(ch2);
  1024. return;
  1025. }
  1026. /**
  1027. * Check some hopefully-impossible edge cases in the channel queue we
  1028. * can only trigger by doing evil things to the queue directly.
  1029. */
  1030. static void
  1031. test_channel_queue_impossible(void *arg)
  1032. {
  1033. channel_t *ch = NULL;
  1034. cell_t *cell = NULL;
  1035. packed_cell_t *packed_cell = NULL;
  1036. var_cell_t *var_cell = NULL;
  1037. int old_count;
  1038. cell_queue_entry_t *q = NULL;
  1039. uint64_t global_queue_estimate;
  1040. uintptr_t cellintptr;
  1041. /* Cache the global queue size (see below) */
  1042. global_queue_estimate = channel_get_global_queue_estimate();
  1043. (void)arg;
  1044. #ifdef ENABLE_MEMPOOLS
  1045. init_cell_pool();
  1046. #endif /* ENABLE_MEMPOOLS */
  1047. ch = new_fake_channel();
  1048. tt_assert(ch);
  1049. /* We test queueing here; tell it not to accept cells */
  1050. test_chan_accept_cells = 0;
  1051. /* ...and keep it from trying to flush the queue */
  1052. ch->state = CHANNEL_STATE_MAINT;
  1053. /* Cache the cell written count */
  1054. old_count = test_cells_written;
  1055. /* Assert that the queue is initially empty */
  1056. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1057. /* Get a fresh cell and write it to the channel*/
  1058. cell = tor_malloc_zero(sizeof(cell_t));
  1059. make_fake_cell(cell);
  1060. cellintptr = (uintptr_t)(void*)cell;
  1061. channel_write_cell(ch, cell);
  1062. /* Now it should be queued */
  1063. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1064. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1065. tt_assert(q);
  1066. if (q) {
  1067. tt_int_op(q->type, ==, CELL_QUEUE_FIXED);
  1068. tt_assert((uintptr_t)q->u.fixed.cell == cellintptr);
  1069. }
  1070. /* Do perverse things to it */
  1071. tor_free(q->u.fixed.cell);
  1072. q->u.fixed.cell = NULL;
  1073. /*
  1074. * Now change back to open with channel_change_state() and assert that it
  1075. * gets thrown away properly.
  1076. */
  1077. test_chan_accept_cells = 1;
  1078. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1079. tt_assert(test_cells_written == old_count);
  1080. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1081. /* Same thing but for a var_cell */
  1082. test_chan_accept_cells = 0;
  1083. ch->state = CHANNEL_STATE_MAINT;
  1084. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1085. make_fake_var_cell(var_cell);
  1086. cellintptr = (uintptr_t)(void*)var_cell;
  1087. channel_write_var_cell(ch, var_cell);
  1088. /* Check that it's queued */
  1089. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1090. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1091. tt_assert(q);
  1092. if (q) {
  1093. tt_int_op(q->type, ==, CELL_QUEUE_VAR);
  1094. tt_assert((uintptr_t)q->u.var.var_cell == cellintptr);
  1095. }
  1096. /* Remove the cell from the queue entry */
  1097. tor_free(q->u.var.var_cell);
  1098. q->u.var.var_cell = NULL;
  1099. /* Let it drain and check that the bad entry is discarded */
  1100. test_chan_accept_cells = 1;
  1101. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1102. tt_assert(test_cells_written == old_count);
  1103. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1104. /* Same thing with a packed_cell */
  1105. test_chan_accept_cells = 0;
  1106. ch->state = CHANNEL_STATE_MAINT;
  1107. packed_cell = packed_cell_new();
  1108. tt_assert(packed_cell);
  1109. cellintptr = (uintptr_t)(void*)packed_cell;
  1110. channel_write_packed_cell(ch, packed_cell);
  1111. /* Check that it's queued */
  1112. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1113. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1114. tt_assert(q);
  1115. if (q) {
  1116. tt_int_op(q->type, ==, CELL_QUEUE_PACKED);
  1117. tt_assert((uintptr_t)q->u.packed.packed_cell == cellintptr);
  1118. }
  1119. /* Remove the cell from the queue entry */
  1120. packed_cell_free(q->u.packed.packed_cell);
  1121. q->u.packed.packed_cell = NULL;
  1122. /* Let it drain and check that the bad entry is discarded */
  1123. test_chan_accept_cells = 1;
  1124. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1125. tt_assert(test_cells_written == old_count);
  1126. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1127. /* Unknown cell type case */
  1128. test_chan_accept_cells = 0;
  1129. ch->state = CHANNEL_STATE_MAINT;
  1130. cell = tor_malloc_zero(sizeof(cell_t));
  1131. make_fake_cell(cell);
  1132. channel_write_cell(ch, cell);
  1133. /* Check that it's queued */
  1134. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1135. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1136. tt_assert(q);
  1137. if (q) {
  1138. tt_int_op(q->type, ==, CELL_QUEUE_FIXED);
  1139. tt_ptr_op(q->u.fixed.cell, ==, cell);
  1140. }
  1141. /* Clobber it, including the queue entry type */
  1142. tor_free(q->u.fixed.cell);
  1143. q->u.fixed.cell = NULL;
  1144. q->type = CELL_QUEUE_PACKED + 1;
  1145. /* Let it drain and check that the bad entry is discarded */
  1146. test_chan_accept_cells = 1;
  1147. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1148. tt_assert(test_cells_written == old_count);
  1149. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1150. done:
  1151. free_fake_channel(ch);
  1152. #ifdef ENABLE_MEMPOOLS
  1153. free_cell_pool();
  1154. #endif /* ENABLE_MEMPOOLS */
  1155. /*
  1156. * Doing that meant that we couldn't correctly adjust the queue size
  1157. * for the var cell, so manually reset the global queue size estimate
  1158. * so the next test doesn't break if we run with --no-fork.
  1159. */
  1160. estimated_total_queue_size = global_queue_estimate;
  1161. return;
  1162. }
  1163. static void
  1164. test_channel_queue_size(void *arg)
  1165. {
  1166. channel_t *ch = NULL;
  1167. cell_t *cell = NULL;
  1168. int n, old_count;
  1169. uint64_t global_queue_estimate;
  1170. (void)arg;
  1171. ch = new_fake_channel();
  1172. tt_assert(ch);
  1173. /* Initial queue size update */
  1174. channel_update_xmit_queue_size(ch);
  1175. tt_u64_op(ch->bytes_queued_for_xmit, ==, 0);
  1176. global_queue_estimate = channel_get_global_queue_estimate();
  1177. tt_u64_op(global_queue_estimate, ==, 0);
  1178. /* Test the call-through to our fake lower layer */
  1179. n = channel_num_cells_writeable(ch);
  1180. /* chan_test_num_cells_writeable() always returns 32 */
  1181. tt_int_op(n, ==, 32);
  1182. /*
  1183. * Now we queue some cells and check that channel_num_cells_writeable()
  1184. * adjusts properly
  1185. */
  1186. /* tell it not to accept cells */
  1187. test_chan_accept_cells = 0;
  1188. /* ...and keep it from trying to flush the queue */
  1189. ch->state = CHANNEL_STATE_MAINT;
  1190. /* Get a fresh cell */
  1191. cell = tor_malloc_zero(sizeof(cell_t));
  1192. make_fake_cell(cell);
  1193. old_count = test_cells_written;
  1194. channel_write_cell(ch, cell);
  1195. /* Assert that it got queued, not written through, correctly */
  1196. tt_int_op(test_cells_written, ==, old_count);
  1197. /* Now check chan_test_num_cells_writeable() again */
  1198. n = channel_num_cells_writeable(ch);
  1199. tt_int_op(n, ==, 0); /* Should return 0 since we're in CHANNEL_STATE_MAINT */
  1200. /* Update queue size estimates */
  1201. channel_update_xmit_queue_size(ch);
  1202. /* One cell, times an overhead factor of 1.0 */
  1203. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1204. /* Try a different overhead factor */
  1205. test_overhead_estimate = 0.5f;
  1206. /* This one should be ignored since it's below 1.0 */
  1207. channel_update_xmit_queue_size(ch);
  1208. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1209. /* Now try a larger one */
  1210. test_overhead_estimate = 2.0f;
  1211. channel_update_xmit_queue_size(ch);
  1212. tt_u64_op(ch->bytes_queued_for_xmit, ==, 1024);
  1213. /* Go back to 1.0 */
  1214. test_overhead_estimate = 1.0f;
  1215. channel_update_xmit_queue_size(ch);
  1216. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1217. /* Check the global estimate too */
  1218. global_queue_estimate = channel_get_global_queue_estimate();
  1219. tt_u64_op(global_queue_estimate, ==, 512);
  1220. /* Go to open */
  1221. old_count = test_cells_written;
  1222. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1223. /*
  1224. * It should try to write, but we aren't accepting cells right now, so
  1225. * it'll requeue
  1226. */
  1227. tt_int_op(test_cells_written, ==, old_count);
  1228. /* Check the queue size again */
  1229. channel_update_xmit_queue_size(ch);
  1230. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1231. global_queue_estimate = channel_get_global_queue_estimate();
  1232. tt_u64_op(global_queue_estimate, ==, 512);
  1233. /*
  1234. * Now the cell is in the queue, and we're open, so we should get 31
  1235. * writeable cells.
  1236. */
  1237. n = channel_num_cells_writeable(ch);
  1238. tt_int_op(n, ==, 31);
  1239. /* Accept cells again */
  1240. test_chan_accept_cells = 1;
  1241. /* ...and re-process the queue */
  1242. old_count = test_cells_written;
  1243. channel_flush_cells(ch);
  1244. tt_int_op(test_cells_written, ==, old_count + 1);
  1245. /* Should have 32 writeable now */
  1246. n = channel_num_cells_writeable(ch);
  1247. tt_int_op(n, ==, 32);
  1248. /* Should have queue size estimate of zero */
  1249. channel_update_xmit_queue_size(ch);
  1250. tt_u64_op(ch->bytes_queued_for_xmit, ==, 0);
  1251. global_queue_estimate = channel_get_global_queue_estimate();
  1252. tt_u64_op(global_queue_estimate, ==, 0);
  1253. /* Okay, now we're done with this one */
  1254. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1255. channel_mark_for_close(ch);
  1256. UNMOCK(scheduler_release_channel);
  1257. done:
  1258. free_fake_channel(ch);
  1259. return;
  1260. }
  1261. static void
  1262. test_channel_write(void *arg)
  1263. {
  1264. channel_t *ch = NULL;
  1265. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  1266. packed_cell_t *packed_cell = NULL;
  1267. var_cell_t *var_cell =
  1268. tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1269. int old_count;
  1270. (void)arg;
  1271. #ifdef ENABLE_MEMPOOLS
  1272. init_cell_pool();
  1273. #endif /* ENABLE_MEMPOOLS */
  1274. packed_cell = packed_cell_new();
  1275. tt_assert(packed_cell);
  1276. ch = new_fake_channel();
  1277. tt_assert(ch);
  1278. make_fake_cell(cell);
  1279. make_fake_var_cell(var_cell);
  1280. /* Tell it to accept cells */
  1281. test_chan_accept_cells = 1;
  1282. old_count = test_cells_written;
  1283. channel_write_cell(ch, cell);
  1284. cell = NULL;
  1285. tt_assert(test_cells_written == old_count + 1);
  1286. channel_write_var_cell(ch, var_cell);
  1287. var_cell = NULL;
  1288. tt_assert(test_cells_written == old_count + 2);
  1289. channel_write_packed_cell(ch, packed_cell);
  1290. packed_cell = NULL;
  1291. tt_assert(test_cells_written == old_count + 3);
  1292. /* Now we test queueing; tell it not to accept cells */
  1293. test_chan_accept_cells = 0;
  1294. /* ...and keep it from trying to flush the queue */
  1295. ch->state = CHANNEL_STATE_MAINT;
  1296. /* Get a fresh cell */
  1297. cell = tor_malloc_zero(sizeof(cell_t));
  1298. make_fake_cell(cell);
  1299. old_count = test_cells_written;
  1300. channel_write_cell(ch, cell);
  1301. tt_assert(test_cells_written == old_count);
  1302. /*
  1303. * Now change back to open with channel_change_state() and assert that it
  1304. * gets drained from the queue.
  1305. */
  1306. test_chan_accept_cells = 1;
  1307. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1308. tt_assert(test_cells_written == old_count + 1);
  1309. /*
  1310. * Check the note destroy case
  1311. */
  1312. cell = tor_malloc_zero(sizeof(cell_t));
  1313. make_fake_cell(cell);
  1314. cell->command = CELL_DESTROY;
  1315. /* Set up the mock */
  1316. MOCK(channel_note_destroy_not_pending,
  1317. channel_note_destroy_not_pending_mock);
  1318. old_count = test_destroy_not_pending_calls;
  1319. channel_write_cell(ch, cell);
  1320. tt_assert(test_destroy_not_pending_calls == old_count + 1);
  1321. /* Now send a non-destroy and check we don't call it */
  1322. cell = tor_malloc_zero(sizeof(cell_t));
  1323. make_fake_cell(cell);
  1324. channel_write_cell(ch, cell);
  1325. tt_assert(test_destroy_not_pending_calls == old_count + 1);
  1326. UNMOCK(channel_note_destroy_not_pending);
  1327. /*
  1328. * Now switch it to CLOSING so we can test the discard-cells case
  1329. * in the channel_write_*() functions.
  1330. */
  1331. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1332. channel_mark_for_close(ch);
  1333. UNMOCK(scheduler_release_channel);
  1334. /* Send cells that will drop in the closing state */
  1335. old_count = test_cells_written;
  1336. cell = tor_malloc_zero(sizeof(cell_t));
  1337. make_fake_cell(cell);
  1338. channel_write_cell(ch, cell);
  1339. cell = NULL;
  1340. tt_assert(test_cells_written == old_count);
  1341. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1342. make_fake_var_cell(var_cell);
  1343. channel_write_var_cell(ch, var_cell);
  1344. var_cell = NULL;
  1345. tt_assert(test_cells_written == old_count);
  1346. packed_cell = packed_cell_new();
  1347. channel_write_packed_cell(ch, packed_cell);
  1348. packed_cell = NULL;
  1349. tt_assert(test_cells_written == old_count);
  1350. #ifdef ENABLE_MEMPOOLS
  1351. free_cell_pool();
  1352. #endif /* ENABLE_MEMPOOLS */
  1353. done:
  1354. free_fake_channel(ch);
  1355. tor_free(var_cell);
  1356. tor_free(cell);
  1357. packed_cell_free(packed_cell);
  1358. return;
  1359. }
  1360. struct testcase_t channel_tests[] = {
  1361. { "dumpstats", test_channel_dumpstats, TT_FORK, NULL, NULL },
  1362. { "flush", test_channel_flush, TT_FORK, NULL, NULL },
  1363. { "flushmux", test_channel_flushmux, TT_FORK, NULL, NULL },
  1364. { "incoming", test_channel_incoming, TT_FORK, NULL, NULL },
  1365. { "lifecycle", test_channel_lifecycle, TT_FORK, NULL, NULL },
  1366. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK, NULL, NULL },
  1367. { "multi", test_channel_multi, TT_FORK, NULL, NULL },
  1368. { "queue_impossible", test_channel_queue_impossible, TT_FORK, NULL, NULL },
  1369. { "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
  1370. { "write", test_channel_write, TT_FORK, NULL, NULL },
  1371. END_OF_TESTCASES
  1372. };