test_channel.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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. /**
  352. * Counter query for scheduler_channel_has_waiting_cells_mock()
  353. */
  354. int
  355. get_mock_scheduler_has_waiting_cells_count(void)
  356. {
  357. return test_has_waiting_cells_count;
  358. }
  359. /**
  360. * Mock for scheduler_channel_has_waiting_cells()
  361. */
  362. void
  363. scheduler_channel_has_waiting_cells_mock(channel_t *ch)
  364. {
  365. (void)ch;
  366. /* Increment counter */
  367. ++test_has_waiting_cells_count;
  368. return;
  369. }
  370. static void
  371. scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
  372. {
  373. (void)ch;
  374. /* Increment counter */
  375. ++test_doesnt_want_writes_count;
  376. return;
  377. }
  378. /**
  379. * Counter query for scheduler_release_channel_mock()
  380. */
  381. int
  382. get_mock_scheduler_release_channel_count(void)
  383. {
  384. return test_releases_count;
  385. }
  386. /**
  387. * Mock for scheduler_release_channel()
  388. */
  389. void
  390. scheduler_release_channel_mock(channel_t *ch)
  391. {
  392. (void)ch;
  393. /* Increment counter */
  394. ++test_releases_count;
  395. return;
  396. }
  397. /**
  398. * Test for channel_dumpstats() and limited test for
  399. * channel_dump_statistics()
  400. */
  401. static void
  402. test_channel_dumpstats(void *arg)
  403. {
  404. channel_t *ch = NULL;
  405. cell_t *cell = NULL;
  406. int old_count;
  407. (void)arg;
  408. /* Mock these for duration of the test */
  409. MOCK(scheduler_channel_doesnt_want_writes,
  410. scheduler_channel_doesnt_want_writes_mock);
  411. MOCK(scheduler_release_channel,
  412. scheduler_release_channel_mock);
  413. /* Set up a new fake channel */
  414. ch = new_fake_channel();
  415. tt_assert(ch);
  416. ch->cmux = circuitmux_alloc();
  417. /* Try to register it */
  418. channel_register(ch);
  419. tt_assert(ch->registered);
  420. /* Set up mock */
  421. dump_statistics_mock_target = ch;
  422. dump_statistics_mock_matches = 0;
  423. MOCK(channel_dump_statistics,
  424. chan_test_channel_dump_statistics_mock);
  425. /* Call channel_dumpstats() */
  426. channel_dumpstats(LOG_DEBUG);
  427. /* Assert that we hit the mock */
  428. tt_int_op(dump_statistics_mock_matches, ==, 1);
  429. /* Close the channel */
  430. channel_mark_for_close(ch);
  431. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  432. chan_test_finish_close(ch);
  433. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  434. /* Try again and hit the finished channel */
  435. channel_dumpstats(LOG_DEBUG);
  436. tt_int_op(dump_statistics_mock_matches, ==, 2);
  437. channel_run_cleanup();
  438. ch = NULL;
  439. /* Now we should hit nothing */
  440. channel_dumpstats(LOG_DEBUG);
  441. tt_int_op(dump_statistics_mock_matches, ==, 2);
  442. /* Unmock */
  443. UNMOCK(channel_dump_statistics);
  444. dump_statistics_mock_target = NULL;
  445. dump_statistics_mock_matches = 0;
  446. /* Now make another channel */
  447. ch = new_fake_channel();
  448. tt_assert(ch);
  449. ch->cmux = circuitmux_alloc();
  450. channel_register(ch);
  451. tt_assert(ch->registered);
  452. /* Lie about its age so dumpstats gets coverage for rate calculations */
  453. ch->timestamp_created = time(NULL) - 30;
  454. tt_assert(ch->timestamp_created > 0);
  455. tt_assert(time(NULL) > ch->timestamp_created);
  456. /* Put cells through it both ways to make the counters non-zero */
  457. cell = tor_malloc_zero(sizeof(*cell));
  458. make_fake_cell(cell);
  459. test_chan_accept_cells = 1;
  460. old_count = test_cells_written;
  461. channel_write_cell(ch, cell);
  462. cell = NULL;
  463. tt_int_op(test_cells_written, ==, old_count + 1);
  464. tt_assert(ch->n_bytes_xmitted > 0);
  465. tt_assert(ch->n_cells_xmitted > 0);
  466. /* Receive path */
  467. channel_set_cell_handlers(ch,
  468. chan_test_cell_handler,
  469. chan_test_var_cell_handler);
  470. tt_ptr_op(channel_get_cell_handler(ch), ==, chan_test_cell_handler);
  471. tt_ptr_op(channel_get_var_cell_handler(ch), ==, chan_test_var_cell_handler);
  472. cell = tor_malloc_zero(sizeof(cell_t));
  473. make_fake_cell(cell);
  474. old_count = test_chan_fixed_cells_recved;
  475. channel_queue_cell(ch, cell);
  476. cell = NULL;
  477. tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
  478. tt_assert(ch->n_bytes_recved > 0);
  479. tt_assert(ch->n_cells_recved > 0);
  480. /* Test channel_dump_statistics */
  481. ch->describe_transport = chan_test_describe_transport;
  482. ch->dumpstats = chan_test_dumpstats;
  483. ch->get_remote_descr = chan_test_get_remote_descr;
  484. ch->is_canonical = chan_test_is_canonical;
  485. old_count = test_dumpstats_calls;
  486. channel_dump_statistics(ch, LOG_DEBUG);
  487. tt_int_op(test_dumpstats_calls, ==, old_count + 1);
  488. /* Close the channel */
  489. channel_mark_for_close(ch);
  490. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  491. chan_test_finish_close(ch);
  492. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  493. channel_run_cleanup();
  494. ch = NULL;
  495. done:
  496. tor_free(cell);
  497. tor_free(ch);
  498. UNMOCK(scheduler_channel_doesnt_want_writes);
  499. UNMOCK(scheduler_release_channel);
  500. return;
  501. }
  502. static void
  503. test_channel_flush(void *arg)
  504. {
  505. channel_t *ch = NULL;
  506. cell_t *cell = NULL;
  507. packed_cell_t *p_cell = NULL;
  508. var_cell_t *v_cell = NULL;
  509. int init_count;
  510. (void)arg;
  511. #ifdef ENABLE_MEMPOOLS
  512. init_cell_pool();
  513. #endif /* ENABLE_MEMPOOLS */
  514. ch = new_fake_channel();
  515. tt_assert(ch);
  516. /* Cache the original count */
  517. init_count = test_cells_written;
  518. /* Stop accepting so we can queue some */
  519. test_chan_accept_cells = 0;
  520. /* Queue a regular cell */
  521. cell = tor_malloc_zero(sizeof(cell_t));
  522. make_fake_cell(cell);
  523. channel_write_cell(ch, cell);
  524. /* It should be queued, so assert that we didn't write it */
  525. tt_int_op(test_cells_written, ==, init_count);
  526. /* Queue a var cell */
  527. v_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  528. make_fake_var_cell(v_cell);
  529. channel_write_var_cell(ch, v_cell);
  530. /* It should be queued, so assert that we didn't write it */
  531. tt_int_op(test_cells_written, ==, init_count);
  532. /* Try a packed cell now */
  533. p_cell = packed_cell_new();
  534. tt_assert(p_cell);
  535. channel_write_packed_cell(ch, p_cell);
  536. /* It should be queued, so assert that we didn't write it */
  537. tt_int_op(test_cells_written, ==, init_count);
  538. /* Now allow writes through again */
  539. test_chan_accept_cells = 1;
  540. /* ...and flush */
  541. channel_flush_cells(ch);
  542. /* All three should have gone through */
  543. tt_int_op(test_cells_written, ==, init_count + 3);
  544. done:
  545. tor_free(ch);
  546. #ifdef ENABLE_MEMPOOLS
  547. free_cell_pool();
  548. #endif /* ENABLE_MEMPOOLS */
  549. return;
  550. }
  551. /**
  552. * Channel flush tests that require cmux mocking
  553. */
  554. static void
  555. test_channel_flushmux(void *arg)
  556. {
  557. channel_t *ch = NULL;
  558. int old_count, q_len_before, q_len_after;
  559. ssize_t result;
  560. (void)arg;
  561. #ifdef ENABLE_MEMPOOLS
  562. init_cell_pool();
  563. #endif /* ENABLE_MEMPOOLS */
  564. /* Install mocks we need for this test */
  565. MOCK(channel_flush_from_first_active_circuit,
  566. chan_test_channel_flush_from_first_active_circuit_mock);
  567. MOCK(circuitmux_num_cells,
  568. chan_test_circuitmux_num_cells_mock);
  569. ch = new_fake_channel();
  570. tt_assert(ch);
  571. ch->cmux = circuitmux_alloc();
  572. old_count = test_cells_written;
  573. test_target_cmux = ch->cmux;
  574. test_cmux_cells = 1;
  575. /* Enable cell acceptance */
  576. test_chan_accept_cells = 1;
  577. result = channel_flush_some_cells(ch, 1);
  578. tt_int_op(result, ==, 1);
  579. tt_int_op(test_cells_written, ==, old_count + 1);
  580. tt_int_op(test_cmux_cells, ==, 0);
  581. /* Now try it without accepting to force them into the queue */
  582. test_chan_accept_cells = 0;
  583. test_cmux_cells = 1;
  584. q_len_before = chan_cell_queue_len(&(ch->outgoing_queue));
  585. result = channel_flush_some_cells(ch, 1);
  586. /* We should not have actually flushed any */
  587. tt_int_op(result, ==, 0);
  588. tt_int_op(test_cells_written, ==, old_count + 1);
  589. /* But we should have gotten to the fake cellgen loop */
  590. tt_int_op(test_cmux_cells, ==, 0);
  591. /* ...and we should have a queued cell */
  592. q_len_after = chan_cell_queue_len(&(ch->outgoing_queue));
  593. tt_int_op(q_len_after, ==, q_len_before + 1);
  594. /* Now accept cells again and drain the queue */
  595. test_chan_accept_cells = 1;
  596. channel_flush_cells(ch);
  597. tt_int_op(test_cells_written, ==, old_count + 2);
  598. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  599. test_target_cmux = NULL;
  600. test_cmux_cells = 0;
  601. done:
  602. tor_free(ch);
  603. UNMOCK(channel_flush_from_first_active_circuit);
  604. UNMOCK(circuitmux_num_cells);
  605. test_chan_accept_cells = 0;
  606. #ifdef ENABLE_MEMPOOLS
  607. free_cell_pool();
  608. #endif /* ENABLE_MEMPOOLS */
  609. return;
  610. }
  611. static void
  612. test_channel_incoming(void *arg)
  613. {
  614. channel_t *ch = NULL;
  615. cell_t *cell = NULL;
  616. var_cell_t *var_cell = NULL;
  617. int old_count;
  618. (void)arg;
  619. /* Mock these for duration of the test */
  620. MOCK(scheduler_channel_doesnt_want_writes,
  621. scheduler_channel_doesnt_want_writes_mock);
  622. MOCK(scheduler_release_channel,
  623. scheduler_release_channel_mock);
  624. /* Accept cells to lower layer */
  625. test_chan_accept_cells = 1;
  626. /* Use default overhead factor */
  627. test_overhead_estimate = 1.0f;
  628. ch = new_fake_channel();
  629. tt_assert(ch);
  630. /* Start it off in OPENING */
  631. ch->state = CHANNEL_STATE_OPENING;
  632. /* We'll need a cmux */
  633. ch->cmux = circuitmux_alloc();
  634. /* Install incoming cell handlers */
  635. channel_set_cell_handlers(ch,
  636. chan_test_cell_handler,
  637. chan_test_var_cell_handler);
  638. /* Test cell handler getters */
  639. tt_ptr_op(channel_get_cell_handler(ch), ==, chan_test_cell_handler);
  640. tt_ptr_op(channel_get_var_cell_handler(ch), ==, chan_test_var_cell_handler);
  641. /* Try to register it */
  642. channel_register(ch);
  643. tt_assert(ch->registered);
  644. /* Open it */
  645. channel_change_state(ch, CHANNEL_STATE_OPEN);
  646. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  647. /* Receive a fixed cell */
  648. cell = tor_malloc_zero(sizeof(cell_t));
  649. make_fake_cell(cell);
  650. old_count = test_chan_fixed_cells_recved;
  651. channel_queue_cell(ch, cell);
  652. cell = NULL;
  653. tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
  654. /* Receive a variable-size cell */
  655. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  656. make_fake_var_cell(var_cell);
  657. old_count = test_chan_var_cells_recved;
  658. channel_queue_var_cell(ch, var_cell);
  659. var_cell = NULL;
  660. tt_int_op(test_chan_var_cells_recved, ==, old_count + 1);
  661. /* Close it */
  662. channel_mark_for_close(ch);
  663. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  664. chan_test_finish_close(ch);
  665. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  666. channel_run_cleanup();
  667. ch = NULL;
  668. done:
  669. tor_free(ch);
  670. tor_free(cell);
  671. tor_free(var_cell);
  672. UNMOCK(scheduler_channel_doesnt_want_writes);
  673. UNMOCK(scheduler_release_channel);
  674. return;
  675. }
  676. /**
  677. * Normal channel lifecycle test:
  678. *
  679. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  680. */
  681. static void
  682. test_channel_lifecycle(void *arg)
  683. {
  684. channel_t *ch1 = NULL, *ch2 = NULL;
  685. cell_t *cell = NULL;
  686. int old_count, init_doesnt_want_writes_count;
  687. int init_releases_count;
  688. (void)arg;
  689. /* Mock these for the whole lifecycle test */
  690. MOCK(scheduler_channel_doesnt_want_writes,
  691. scheduler_channel_doesnt_want_writes_mock);
  692. MOCK(scheduler_release_channel,
  693. scheduler_release_channel_mock);
  694. /* Cache some initial counter values */
  695. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  696. init_releases_count = test_releases_count;
  697. /* Accept cells to lower layer */
  698. test_chan_accept_cells = 1;
  699. /* Use default overhead factor */
  700. test_overhead_estimate = 1.0f;
  701. ch1 = new_fake_channel();
  702. tt_assert(ch1);
  703. /* Start it off in OPENING */
  704. ch1->state = CHANNEL_STATE_OPENING;
  705. /* We'll need a cmux */
  706. ch1->cmux = circuitmux_alloc();
  707. /* Try to register it */
  708. channel_register(ch1);
  709. tt_assert(ch1->registered);
  710. /* Try to write a cell through (should queue) */
  711. cell = tor_malloc_zero(sizeof(cell_t));
  712. make_fake_cell(cell);
  713. old_count = test_cells_written;
  714. channel_write_cell(ch1, cell);
  715. tt_int_op(old_count, ==, test_cells_written);
  716. /* Move it to OPEN and flush */
  717. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  718. /* Queue should drain */
  719. tt_int_op(old_count + 1, ==, test_cells_written);
  720. /* Get another one */
  721. ch2 = new_fake_channel();
  722. tt_assert(ch2);
  723. ch2->state = CHANNEL_STATE_OPENING;
  724. ch2->cmux = circuitmux_alloc();
  725. /* Register */
  726. channel_register(ch2);
  727. tt_assert(ch2->registered);
  728. /* Check counters */
  729. tt_int_op(test_doesnt_want_writes_count, ==, init_doesnt_want_writes_count);
  730. tt_int_op(test_releases_count, ==, init_releases_count);
  731. /* Move ch1 to MAINT */
  732. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  733. tt_int_op(test_doesnt_want_writes_count, ==,
  734. init_doesnt_want_writes_count + 1);
  735. tt_int_op(test_releases_count, ==, init_releases_count);
  736. /* Move ch2 to OPEN */
  737. channel_change_state(ch2, CHANNEL_STATE_OPEN);
  738. tt_int_op(test_doesnt_want_writes_count, ==,
  739. init_doesnt_want_writes_count + 1);
  740. tt_int_op(test_releases_count, ==, init_releases_count);
  741. /* Move ch1 back to OPEN */
  742. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  743. tt_int_op(test_doesnt_want_writes_count, ==,
  744. init_doesnt_want_writes_count + 1);
  745. tt_int_op(test_releases_count, ==, init_releases_count);
  746. /* Mark ch2 for close */
  747. channel_mark_for_close(ch2);
  748. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSING);
  749. tt_int_op(test_doesnt_want_writes_count, ==,
  750. init_doesnt_want_writes_count + 1);
  751. tt_int_op(test_releases_count, ==, init_releases_count + 1);
  752. /* Shut down channels */
  753. channel_free_all();
  754. ch1 = ch2 = NULL;
  755. tt_int_op(test_doesnt_want_writes_count, ==,
  756. init_doesnt_want_writes_count + 1);
  757. /* channel_free() calls scheduler_release_channel() */
  758. tt_int_op(test_releases_count, ==, init_releases_count + 4);
  759. done:
  760. tor_free(ch1);
  761. tor_free(ch2);
  762. UNMOCK(scheduler_channel_doesnt_want_writes);
  763. UNMOCK(scheduler_release_channel);
  764. return;
  765. }
  766. /**
  767. * Weird channel lifecycle test:
  768. *
  769. * OPENING->CLOSING->CLOSED
  770. * OPENING->OPEN->CLOSING->ERROR
  771. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  772. * OPENING->OPEN->MAINT->CLOSING->ERROR
  773. */
  774. static void
  775. test_channel_lifecycle_2(void *arg)
  776. {
  777. channel_t *ch = NULL;
  778. (void)arg;
  779. /* Mock these for the whole lifecycle test */
  780. MOCK(scheduler_channel_doesnt_want_writes,
  781. scheduler_channel_doesnt_want_writes_mock);
  782. MOCK(scheduler_release_channel,
  783. scheduler_release_channel_mock);
  784. /* Accept cells to lower layer */
  785. test_chan_accept_cells = 1;
  786. /* Use default overhead factor */
  787. test_overhead_estimate = 1.0f;
  788. ch = new_fake_channel();
  789. tt_assert(ch);
  790. /* Start it off in OPENING */
  791. ch->state = CHANNEL_STATE_OPENING;
  792. /* The full lifecycle test needs a cmux */
  793. ch->cmux = circuitmux_alloc();
  794. /* Try to register it */
  795. channel_register(ch);
  796. tt_assert(ch->registered);
  797. /* Try to close it */
  798. channel_mark_for_close(ch);
  799. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  800. /* Finish closing it */
  801. chan_test_finish_close(ch);
  802. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  803. channel_run_cleanup();
  804. ch = NULL;
  805. /* Now try OPENING->OPEN->CLOSING->ERROR */
  806. ch = new_fake_channel();
  807. tt_assert(ch);
  808. ch->state = CHANNEL_STATE_OPENING;
  809. ch->cmux = circuitmux_alloc();
  810. channel_register(ch);
  811. tt_assert(ch->registered);
  812. /* Finish opening it */
  813. channel_change_state(ch, CHANNEL_STATE_OPEN);
  814. /* Error exit from lower layer */
  815. chan_test_error(ch);
  816. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  817. chan_test_finish_close(ch);
  818. tt_int_op(ch->state, ==, CHANNEL_STATE_ERROR);
  819. channel_run_cleanup();
  820. ch = NULL;
  821. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  822. ch = new_fake_channel();
  823. tt_assert(ch);
  824. ch->state = CHANNEL_STATE_OPENING;
  825. ch->cmux = circuitmux_alloc();
  826. channel_register(ch);
  827. tt_assert(ch->registered);
  828. /* Finish opening it */
  829. channel_change_state(ch, CHANNEL_STATE_OPEN);
  830. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  831. /* Go to maintenance state */
  832. channel_change_state(ch, CHANNEL_STATE_MAINT);
  833. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  834. /* Lower layer close */
  835. channel_mark_for_close(ch);
  836. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  837. /* Finish */
  838. chan_test_finish_close(ch);
  839. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  840. channel_run_cleanup();
  841. ch = NULL;
  842. /*
  843. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  844. * maintenance state
  845. */
  846. ch = new_fake_channel();
  847. tt_assert(ch);
  848. ch->state = CHANNEL_STATE_OPENING;
  849. ch->cmux = circuitmux_alloc();
  850. channel_register(ch);
  851. tt_assert(ch->registered);
  852. /* Finish opening it */
  853. channel_change_state(ch, CHANNEL_STATE_OPEN);
  854. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  855. /* Go to maintenance state */
  856. channel_change_state(ch, CHANNEL_STATE_MAINT);
  857. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  858. /* Lower layer close */
  859. channel_close_from_lower_layer(ch);
  860. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  861. /* Finish */
  862. chan_test_finish_close(ch);
  863. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  864. channel_run_cleanup();
  865. ch = NULL;
  866. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  867. ch = new_fake_channel();
  868. tt_assert(ch);
  869. ch->state = CHANNEL_STATE_OPENING;
  870. ch->cmux = circuitmux_alloc();
  871. channel_register(ch);
  872. tt_assert(ch->registered);
  873. /* Finish opening it */
  874. channel_change_state(ch, CHANNEL_STATE_OPEN);
  875. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  876. /* Go to maintenance state */
  877. channel_change_state(ch, CHANNEL_STATE_MAINT);
  878. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  879. /* Lower layer close */
  880. chan_test_error(ch);
  881. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  882. /* Finish */
  883. chan_test_finish_close(ch);
  884. tt_int_op(ch->state, ==, CHANNEL_STATE_ERROR);
  885. channel_run_cleanup();
  886. ch = NULL;
  887. /* Shut down channels */
  888. channel_free_all();
  889. done:
  890. tor_free(ch);
  891. UNMOCK(scheduler_channel_doesnt_want_writes);
  892. UNMOCK(scheduler_release_channel);
  893. return;
  894. }
  895. static void
  896. test_channel_multi(void *arg)
  897. {
  898. channel_t *ch1 = NULL, *ch2 = NULL;
  899. uint64_t global_queue_estimate;
  900. cell_t *cell = NULL;
  901. (void)arg;
  902. /* Accept cells to lower layer */
  903. test_chan_accept_cells = 1;
  904. /* Use default overhead factor */
  905. test_overhead_estimate = 1.0f;
  906. ch1 = new_fake_channel();
  907. tt_assert(ch1);
  908. ch2 = new_fake_channel();
  909. tt_assert(ch2);
  910. /* Initial queue size update */
  911. channel_update_xmit_queue_size(ch1);
  912. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  913. channel_update_xmit_queue_size(ch2);
  914. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  915. global_queue_estimate = channel_get_global_queue_estimate();
  916. tt_u64_op(global_queue_estimate, ==, 0);
  917. /* Queue some cells, check queue estimates */
  918. cell = tor_malloc_zero(sizeof(cell_t));
  919. make_fake_cell(cell);
  920. channel_write_cell(ch1, cell);
  921. cell = tor_malloc_zero(sizeof(cell_t));
  922. make_fake_cell(cell);
  923. channel_write_cell(ch2, cell);
  924. channel_update_xmit_queue_size(ch1);
  925. channel_update_xmit_queue_size(ch2);
  926. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  927. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  928. global_queue_estimate = channel_get_global_queue_estimate();
  929. tt_u64_op(global_queue_estimate, ==, 0);
  930. /* Stop accepting cells at lower layer */
  931. test_chan_accept_cells = 0;
  932. /* Queue some cells and check queue estimates */
  933. cell = tor_malloc_zero(sizeof(cell_t));
  934. make_fake_cell(cell);
  935. channel_write_cell(ch1, cell);
  936. channel_update_xmit_queue_size(ch1);
  937. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  938. global_queue_estimate = channel_get_global_queue_estimate();
  939. tt_u64_op(global_queue_estimate, ==, 512);
  940. cell = tor_malloc_zero(sizeof(cell_t));
  941. make_fake_cell(cell);
  942. channel_write_cell(ch2, cell);
  943. channel_update_xmit_queue_size(ch2);
  944. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 512);
  945. global_queue_estimate = channel_get_global_queue_estimate();
  946. tt_u64_op(global_queue_estimate, ==, 1024);
  947. /* Allow cells through again */
  948. test_chan_accept_cells = 1;
  949. /* Flush chan 2 */
  950. channel_flush_cells(ch2);
  951. /* Update and check queue sizes */
  952. channel_update_xmit_queue_size(ch1);
  953. channel_update_xmit_queue_size(ch2);
  954. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  955. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  956. global_queue_estimate = channel_get_global_queue_estimate();
  957. tt_u64_op(global_queue_estimate, ==, 512);
  958. /* Flush chan 1 */
  959. channel_flush_cells(ch1);
  960. /* Update and check queue sizes */
  961. channel_update_xmit_queue_size(ch1);
  962. channel_update_xmit_queue_size(ch2);
  963. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  964. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  965. global_queue_estimate = channel_get_global_queue_estimate();
  966. tt_u64_op(global_queue_estimate, ==, 0);
  967. /* Now block again */
  968. test_chan_accept_cells = 0;
  969. /* Queue some cells */
  970. cell = tor_malloc_zero(sizeof(cell_t));
  971. make_fake_cell(cell);
  972. channel_write_cell(ch1, cell);
  973. cell = tor_malloc_zero(sizeof(cell_t));
  974. make_fake_cell(cell);
  975. channel_write_cell(ch2, cell);
  976. /* Check the estimates */
  977. channel_update_xmit_queue_size(ch1);
  978. channel_update_xmit_queue_size(ch2);
  979. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  980. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 512);
  981. global_queue_estimate = channel_get_global_queue_estimate();
  982. tt_u64_op(global_queue_estimate, ==, 1024);
  983. /* Now close channel 2; it should be subtracted from the global queue */
  984. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  985. channel_mark_for_close(ch2);
  986. UNMOCK(scheduler_release_channel);
  987. global_queue_estimate = channel_get_global_queue_estimate();
  988. tt_u64_op(global_queue_estimate, ==, 512);
  989. /*
  990. * Since the fake channels aren't registered, channel_free_all() can't
  991. * see them properly.
  992. */
  993. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  994. channel_mark_for_close(ch1);
  995. UNMOCK(scheduler_release_channel);
  996. global_queue_estimate = channel_get_global_queue_estimate();
  997. tt_u64_op(global_queue_estimate, ==, 0);
  998. /* Now free everything */
  999. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1000. channel_free_all();
  1001. UNMOCK(scheduler_release_channel);
  1002. done:
  1003. tor_free(ch1);
  1004. tor_free(ch2);
  1005. return;
  1006. }
  1007. /**
  1008. * Check some hopefully-impossible edge cases in the channel queue we
  1009. * can only trigger by doing evil things to the queue directly.
  1010. */
  1011. static void
  1012. test_channel_queue_impossible(void *arg)
  1013. {
  1014. channel_t *ch = NULL;
  1015. cell_t *cell = NULL;
  1016. packed_cell_t *packed_cell = NULL;
  1017. var_cell_t *var_cell = NULL;
  1018. int old_count;
  1019. cell_queue_entry_t *q = NULL;
  1020. uint64_t global_queue_estimate;
  1021. /* Cache the global queue size (see below) */
  1022. global_queue_estimate = channel_get_global_queue_estimate();
  1023. (void)arg;
  1024. #ifdef ENABLE_MEMPOOLS
  1025. init_cell_pool();
  1026. #endif /* ENABLE_MEMPOOLS */
  1027. packed_cell = packed_cell_new();
  1028. tt_assert(packed_cell);
  1029. ch = new_fake_channel();
  1030. tt_assert(ch);
  1031. /* We test queueing here; tell it not to accept cells */
  1032. test_chan_accept_cells = 0;
  1033. /* ...and keep it from trying to flush the queue */
  1034. ch->state = CHANNEL_STATE_MAINT;
  1035. /* Cache the cell written count */
  1036. old_count = test_cells_written;
  1037. /* Assert that the queue is initially empty */
  1038. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1039. /* Get a fresh cell and write it to the channel*/
  1040. cell = tor_malloc_zero(sizeof(cell_t));
  1041. make_fake_cell(cell);
  1042. channel_write_cell(ch, cell);
  1043. /* Now it should be queued */
  1044. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1045. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1046. tt_assert(q);
  1047. if (q) {
  1048. tt_int_op(q->type, ==, CELL_QUEUE_FIXED);
  1049. tt_ptr_op(q->u.fixed.cell, ==, cell);
  1050. }
  1051. /* Do perverse things to it */
  1052. tor_free(q->u.fixed.cell);
  1053. q->u.fixed.cell = NULL;
  1054. /*
  1055. * Now change back to open with channel_change_state() and assert that it
  1056. * gets thrown away properly.
  1057. */
  1058. test_chan_accept_cells = 1;
  1059. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1060. tt_assert(test_cells_written == old_count);
  1061. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1062. /* Same thing but for a var_cell */
  1063. test_chan_accept_cells = 0;
  1064. ch->state = CHANNEL_STATE_MAINT;
  1065. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1066. make_fake_var_cell(var_cell);
  1067. channel_write_var_cell(ch, var_cell);
  1068. /* Check that it's queued */
  1069. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1070. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1071. tt_assert(q);
  1072. if (q) {
  1073. tt_int_op(q->type, ==, CELL_QUEUE_VAR);
  1074. tt_ptr_op(q->u.var.var_cell, ==, var_cell);
  1075. }
  1076. /* Remove the cell from the queue entry */
  1077. tor_free(q->u.var.var_cell);
  1078. q->u.var.var_cell = NULL;
  1079. /* Let it drain and check that the bad entry is discarded */
  1080. test_chan_accept_cells = 1;
  1081. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1082. tt_assert(test_cells_written == old_count);
  1083. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1084. /* Same thing with a packed_cell */
  1085. test_chan_accept_cells = 0;
  1086. ch->state = CHANNEL_STATE_MAINT;
  1087. packed_cell = packed_cell_new();
  1088. tt_assert(packed_cell);
  1089. channel_write_packed_cell(ch, packed_cell);
  1090. /* Check that it's queued */
  1091. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1092. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1093. tt_assert(q);
  1094. if (q) {
  1095. tt_int_op(q->type, ==, CELL_QUEUE_PACKED);
  1096. tt_ptr_op(q->u.packed.packed_cell, ==, packed_cell);
  1097. }
  1098. /* Remove the cell from the queue entry */
  1099. packed_cell_free(q->u.packed.packed_cell);
  1100. q->u.packed.packed_cell = NULL;
  1101. /* Let it drain and check that the bad entry is discarded */
  1102. test_chan_accept_cells = 1;
  1103. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1104. tt_assert(test_cells_written == old_count);
  1105. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1106. /* Unknown cell type case */
  1107. test_chan_accept_cells = 0;
  1108. ch->state = CHANNEL_STATE_MAINT;
  1109. cell = tor_malloc_zero(sizeof(cell_t));
  1110. make_fake_cell(cell);
  1111. channel_write_cell(ch, cell);
  1112. /* Check that it's queued */
  1113. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1114. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1115. tt_assert(q);
  1116. if (q) {
  1117. tt_int_op(q->type, ==, CELL_QUEUE_FIXED);
  1118. tt_ptr_op(q->u.fixed.cell, ==, cell);
  1119. }
  1120. /* Clobber it, including the queue entry type */
  1121. tor_free(q->u.fixed.cell);
  1122. q->u.fixed.cell = NULL;
  1123. q->type = CELL_QUEUE_PACKED + 1;
  1124. /* Let it drain and check that the bad entry is discarded */
  1125. test_chan_accept_cells = 1;
  1126. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1127. tt_assert(test_cells_written == old_count);
  1128. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1129. done:
  1130. tor_free(ch);
  1131. #ifdef ENABLE_MEMPOOLS
  1132. free_cell_pool();
  1133. #endif /* ENABLE_MEMPOOLS */
  1134. /*
  1135. * Doing that meant that we couldn't correctly adjust the queue size
  1136. * for the var cell, so manually reset the global queue size estimate
  1137. * so the next test doesn't break if we run with --no-fork.
  1138. */
  1139. estimated_total_queue_size = global_queue_estimate;
  1140. return;
  1141. }
  1142. static void
  1143. test_channel_queue_size(void *arg)
  1144. {
  1145. channel_t *ch = NULL;
  1146. cell_t *cell = NULL;
  1147. int n, old_count;
  1148. uint64_t global_queue_estimate;
  1149. (void)arg;
  1150. ch = new_fake_channel();
  1151. tt_assert(ch);
  1152. /* Initial queue size update */
  1153. channel_update_xmit_queue_size(ch);
  1154. tt_int_op(ch->bytes_queued_for_xmit, ==, 0);
  1155. global_queue_estimate = channel_get_global_queue_estimate();
  1156. tt_u64_op(global_queue_estimate, ==, 0);
  1157. /* Test the call-through to our fake lower layer */
  1158. n = channel_num_cells_writeable(ch);
  1159. /* chan_test_num_cells_writeable() always returns 32 */
  1160. tt_int_op(n, ==, 32);
  1161. /*
  1162. * Now we queue some cells and check that channel_num_cells_writeable()
  1163. * adjusts properly
  1164. */
  1165. /* tell it not to accept cells */
  1166. test_chan_accept_cells = 0;
  1167. /* ...and keep it from trying to flush the queue */
  1168. ch->state = CHANNEL_STATE_MAINT;
  1169. /* Get a fresh cell */
  1170. cell = tor_malloc_zero(sizeof(cell_t));
  1171. make_fake_cell(cell);
  1172. old_count = test_cells_written;
  1173. channel_write_cell(ch, cell);
  1174. /* Assert that it got queued, not written through, correctly */
  1175. tt_int_op(test_cells_written, ==, old_count);
  1176. /* Now check chan_test_num_cells_writeable() again */
  1177. n = channel_num_cells_writeable(ch);
  1178. tt_int_op(n, ==, 0); /* Should return 0 since we're in CHANNEL_STATE_MAINT */
  1179. /* Update queue size estimates */
  1180. channel_update_xmit_queue_size(ch);
  1181. /* One cell, times an overhead factor of 1.0 */
  1182. tt_int_op(ch->bytes_queued_for_xmit, ==, 512);
  1183. /* Try a different overhead factor */
  1184. test_overhead_estimate = 0.5f;
  1185. /* This one should be ignored since it's below 1.0 */
  1186. channel_update_xmit_queue_size(ch);
  1187. tt_int_op(ch->bytes_queued_for_xmit, ==, 512);
  1188. /* Now try a larger one */
  1189. test_overhead_estimate = 2.0f;
  1190. channel_update_xmit_queue_size(ch);
  1191. tt_int_op(ch->bytes_queued_for_xmit, ==, 1024);
  1192. /* Go back to 1.0 */
  1193. test_overhead_estimate = 1.0f;
  1194. channel_update_xmit_queue_size(ch);
  1195. tt_int_op(ch->bytes_queued_for_xmit, ==, 512);
  1196. /* Check the global estimate too */
  1197. global_queue_estimate = channel_get_global_queue_estimate();
  1198. tt_int_op(global_queue_estimate, ==, 512);
  1199. /* Go to open */
  1200. old_count = test_cells_written;
  1201. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1202. /*
  1203. * It should try to write, but we aren't accepting cells right now, so
  1204. * it'll requeue
  1205. */
  1206. tt_int_op(test_cells_written, ==, old_count);
  1207. /* Check the queue size again */
  1208. channel_update_xmit_queue_size(ch);
  1209. tt_int_op(ch->bytes_queued_for_xmit, ==, 512);
  1210. global_queue_estimate = channel_get_global_queue_estimate();
  1211. tt_int_op(global_queue_estimate, ==, 512);
  1212. /*
  1213. * Now the cell is in the queue, and we're open, so we should get 31
  1214. * writeable cells.
  1215. */
  1216. n = channel_num_cells_writeable(ch);
  1217. tt_int_op(n, ==, 31);
  1218. /* Accept cells again */
  1219. test_chan_accept_cells = 1;
  1220. /* ...and re-process the queue */
  1221. old_count = test_cells_written;
  1222. channel_flush_cells(ch);
  1223. tt_int_op(test_cells_written, ==, old_count + 1);
  1224. /* Should have 32 writeable now */
  1225. n = channel_num_cells_writeable(ch);
  1226. tt_int_op(n, ==, 32);
  1227. /* Should have queue size estimate of zero */
  1228. channel_update_xmit_queue_size(ch);
  1229. tt_int_op(ch->bytes_queued_for_xmit, ==, 0);
  1230. global_queue_estimate = channel_get_global_queue_estimate();
  1231. tt_int_op(global_queue_estimate, ==, 0);
  1232. /* Okay, now we're done with this one */
  1233. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1234. channel_mark_for_close(ch);
  1235. UNMOCK(scheduler_release_channel);
  1236. done:
  1237. tor_free(ch);
  1238. return;
  1239. }
  1240. static void
  1241. test_channel_write(void *arg)
  1242. {
  1243. channel_t *ch = NULL;
  1244. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  1245. packed_cell_t *packed_cell = NULL;
  1246. var_cell_t *var_cell =
  1247. tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1248. int old_count;
  1249. (void)arg;
  1250. #ifdef ENABLE_MEMPOOLS
  1251. init_cell_pool();
  1252. #endif /* ENABLE_MEMPOOLS */
  1253. packed_cell = packed_cell_new();
  1254. tt_assert(packed_cell);
  1255. ch = new_fake_channel();
  1256. tt_assert(ch);
  1257. make_fake_cell(cell);
  1258. make_fake_var_cell(var_cell);
  1259. /* Tell it to accept cells */
  1260. test_chan_accept_cells = 1;
  1261. old_count = test_cells_written;
  1262. channel_write_cell(ch, cell);
  1263. tt_assert(test_cells_written == old_count + 1);
  1264. channel_write_var_cell(ch, var_cell);
  1265. tt_assert(test_cells_written == old_count + 2);
  1266. channel_write_packed_cell(ch, packed_cell);
  1267. tt_assert(test_cells_written == old_count + 3);
  1268. /* Now we test queueing; tell it not to accept cells */
  1269. test_chan_accept_cells = 0;
  1270. /* ...and keep it from trying to flush the queue */
  1271. ch->state = CHANNEL_STATE_MAINT;
  1272. /* Get a fresh cell */
  1273. cell = tor_malloc_zero(sizeof(cell_t));
  1274. make_fake_cell(cell);
  1275. old_count = test_cells_written;
  1276. channel_write_cell(ch, cell);
  1277. tt_assert(test_cells_written == old_count);
  1278. /*
  1279. * Now change back to open with channel_change_state() and assert that it
  1280. * gets drained from the queue.
  1281. */
  1282. test_chan_accept_cells = 1;
  1283. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1284. tt_assert(test_cells_written == old_count + 1);
  1285. /*
  1286. * Check the note destroy case
  1287. */
  1288. cell = tor_malloc_zero(sizeof(cell_t));
  1289. make_fake_cell(cell);
  1290. cell->command = CELL_DESTROY;
  1291. /* Set up the mock */
  1292. MOCK(channel_note_destroy_not_pending,
  1293. channel_note_destroy_not_pending_mock);
  1294. old_count = test_destroy_not_pending_calls;
  1295. channel_write_cell(ch, cell);
  1296. tt_assert(test_destroy_not_pending_calls == old_count + 1);
  1297. /* Now send a non-destroy and check we don't call it */
  1298. cell = tor_malloc_zero(sizeof(cell_t));
  1299. make_fake_cell(cell);
  1300. channel_write_cell(ch, cell);
  1301. tt_assert(test_destroy_not_pending_calls == old_count + 1);
  1302. UNMOCK(channel_note_destroy_not_pending);
  1303. /*
  1304. * Now switch it to CLOSING so we can test the discard-cells case
  1305. * in the channel_write_*() functions.
  1306. */
  1307. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1308. channel_mark_for_close(ch);
  1309. UNMOCK(scheduler_release_channel);
  1310. /* Send cells that will drop in the closing state */
  1311. old_count = test_cells_written;
  1312. cell = tor_malloc_zero(sizeof(cell_t));
  1313. make_fake_cell(cell);
  1314. channel_write_cell(ch, cell);
  1315. tt_assert(test_cells_written == old_count);
  1316. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1317. make_fake_var_cell(var_cell);
  1318. channel_write_var_cell(ch, var_cell);
  1319. tt_assert(test_cells_written == old_count);
  1320. packed_cell = packed_cell_new();
  1321. channel_write_packed_cell(ch, packed_cell);
  1322. tt_assert(test_cells_written == old_count);
  1323. #ifdef ENABLE_MEMPOOLS
  1324. free_cell_pool();
  1325. #endif /* ENABLE_MEMPOOLS */
  1326. done:
  1327. tor_free(ch);
  1328. return;
  1329. }
  1330. struct testcase_t channel_tests[] = {
  1331. { "dumpstats", test_channel_dumpstats, TT_FORK, NULL, NULL },
  1332. { "flush", test_channel_flush, TT_FORK, NULL, NULL },
  1333. { "flushmux", test_channel_flushmux, TT_FORK, NULL, NULL },
  1334. { "incoming", test_channel_incoming, TT_FORK, NULL, NULL },
  1335. { "lifecycle", test_channel_lifecycle, TT_FORK, NULL, NULL },
  1336. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK, NULL, NULL },
  1337. { "multi", test_channel_multi, TT_FORK, NULL, NULL },
  1338. { "queue_impossible", test_channel_queue_impossible, TT_FORK, NULL, NULL },
  1339. { "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
  1340. { "write", test_channel_write, TT_FORK, NULL, NULL },
  1341. END_OF_TESTCASES
  1342. };