test_channel.c 43 KB

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