test_channel.c 42 KB

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