test_channel.c 41 KB

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