test_channel.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. static int test_chan_accept_cells = 0;
  21. static int test_cells_written = 0;
  22. static int test_destroy_not_pending_calls = 0;
  23. static int test_doesnt_want_writes_count = 0;
  24. static int test_has_waiting_cells_count = 0;
  25. static double test_overhead_estimate = 1.0f;
  26. static int test_releases_count = 0;
  27. static void channel_note_destroy_not_pending_mock(channel_t *ch,
  28. circid_t circid);
  29. static void chan_test_close(channel_t *ch);
  30. static void chan_test_error(channel_t *ch);
  31. static void chan_test_finish_close(channel_t *ch);
  32. static size_t chan_test_num_bytes_queued(channel_t *ch);
  33. static int chan_test_num_cells_writeable(channel_t *ch);
  34. static int chan_test_write_cell(channel_t *ch, cell_t *cell);
  35. static int chan_test_write_packed_cell(channel_t *ch,
  36. packed_cell_t *packed_cell);
  37. static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
  38. static void scheduler_channel_doesnt_want_writes_mock(channel_t *ch);
  39. static void test_channel_flush(void *arg);
  40. static void test_channel_lifecycle(void *arg);
  41. static void test_channel_multi(void *arg);
  42. static void test_channel_queue_size(void *arg);
  43. static void test_channel_write(void *arg);
  44. static void
  45. channel_note_destroy_not_pending_mock(channel_t *ch,
  46. circid_t circid)
  47. {
  48. (void)ch;
  49. (void)circid;
  50. ++test_destroy_not_pending_calls;
  51. }
  52. static void
  53. chan_test_close(channel_t *ch)
  54. {
  55. test_assert(ch);
  56. done:
  57. return;
  58. }
  59. /*
  60. * Close a channel through the error path
  61. */
  62. static void
  63. chan_test_error(channel_t *ch)
  64. {
  65. test_assert(ch);
  66. test_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
  67. ch->state == CHANNEL_STATE_ERROR ||
  68. ch->state == CHANNEL_STATE_CLOSED));
  69. channel_close_for_error(ch);
  70. done:
  71. return;
  72. }
  73. /*
  74. * Finish closing a channel from CHANNEL_STATE_CLOSING
  75. */
  76. static void
  77. chan_test_finish_close(channel_t *ch)
  78. {
  79. test_assert(ch);
  80. test_assert(ch->state == CHANNEL_STATE_CLOSING);
  81. channel_closed(ch);
  82. done:
  83. return;
  84. }
  85. static double
  86. chan_test_get_overhead_estimate(channel_t *ch)
  87. {
  88. test_assert(ch);
  89. done:
  90. return test_overhead_estimate;
  91. }
  92. static size_t
  93. chan_test_num_bytes_queued(channel_t *ch)
  94. {
  95. test_assert(ch);
  96. done:
  97. return 0;
  98. }
  99. static int
  100. chan_test_num_cells_writeable(channel_t *ch)
  101. {
  102. test_assert(ch);
  103. done:
  104. return 32;
  105. }
  106. static int
  107. chan_test_write_cell(channel_t *ch, cell_t *cell)
  108. {
  109. int rv = 0;
  110. test_assert(ch);
  111. test_assert(cell);
  112. if (test_chan_accept_cells) {
  113. /* Free the cell and bump the counter */
  114. tor_free(cell);
  115. ++test_cells_written;
  116. rv = 1;
  117. }
  118. /* else return 0, we didn't accept it */
  119. done:
  120. return rv;
  121. }
  122. static int
  123. chan_test_write_packed_cell(channel_t *ch,
  124. packed_cell_t *packed_cell)
  125. {
  126. int rv = 0;
  127. test_assert(ch);
  128. test_assert(packed_cell);
  129. if (test_chan_accept_cells) {
  130. /* Free the cell and bump the counter */
  131. packed_cell_free(packed_cell);
  132. ++test_cells_written;
  133. rv = 1;
  134. }
  135. /* else return 0, we didn't accept it */
  136. done:
  137. return rv;
  138. }
  139. static int
  140. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  141. {
  142. int rv = 0;
  143. test_assert(ch);
  144. test_assert(var_cell);
  145. if (test_chan_accept_cells) {
  146. /* Free the cell and bump the counter */
  147. var_cell_free(var_cell);
  148. ++test_cells_written;
  149. rv = 1;
  150. }
  151. /* else return 0, we didn't accept it */
  152. done:
  153. return rv;
  154. }
  155. /**
  156. * Fill out c with a new fake cell for test suite use
  157. */
  158. void
  159. make_fake_cell(cell_t *c)
  160. {
  161. test_assert(c != NULL);
  162. c->circ_id = 1;
  163. c->command = CELL_RELAY;
  164. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  165. done:
  166. return;
  167. }
  168. /**
  169. * Fill out c with a new fake var_cell for test suite use
  170. */
  171. void
  172. make_fake_var_cell(var_cell_t *c)
  173. {
  174. test_assert(c != NULL);
  175. c->circ_id = 1;
  176. c->command = CELL_VERSIONS;
  177. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  178. memset(c->payload, 0, c->payload_len);
  179. done:
  180. return;
  181. }
  182. /**
  183. * Set up a new fake channel for the test suite
  184. */
  185. channel_t *
  186. new_fake_channel(void)
  187. {
  188. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  189. channel_init(chan);
  190. chan->close = chan_test_close;
  191. chan->get_overhead_estimate = chan_test_get_overhead_estimate;
  192. chan->num_bytes_queued = chan_test_num_bytes_queued;
  193. chan->num_cells_writeable = chan_test_num_cells_writeable;
  194. chan->write_cell = chan_test_write_cell;
  195. chan->write_packed_cell = chan_test_write_packed_cell;
  196. chan->write_var_cell = chan_test_write_var_cell;
  197. chan->state = CHANNEL_STATE_OPEN;
  198. return chan;
  199. }
  200. /**
  201. * Counter query for scheduler_channel_has_waiting_cells_mock()
  202. */
  203. int
  204. get_mock_scheduler_has_waiting_cells_count(void)
  205. {
  206. return test_has_waiting_cells_count;
  207. }
  208. /**
  209. * Mock for scheduler_channel_has_waiting_cells()
  210. */
  211. void
  212. scheduler_channel_has_waiting_cells_mock(channel_t *ch)
  213. {
  214. (void)ch;
  215. /* Increment counter */
  216. ++test_has_waiting_cells_count;
  217. return;
  218. }
  219. static void
  220. scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
  221. {
  222. (void)ch;
  223. /* Increment counter */
  224. ++test_doesnt_want_writes_count;
  225. return;
  226. }
  227. /**
  228. * Counter query for scheduler_release_channel_mock()
  229. */
  230. int
  231. get_mock_scheduler_release_channel_count(void)
  232. {
  233. return test_releases_count;
  234. }
  235. /**
  236. * Mock for scheduler_release_channel()
  237. */
  238. void
  239. scheduler_release_channel_mock(channel_t *ch)
  240. {
  241. (void)ch;
  242. /* Increment counter */
  243. ++test_releases_count;
  244. return;
  245. }
  246. static void
  247. test_channel_flush(void *arg)
  248. {
  249. channel_t *ch = NULL;
  250. cell_t *cell = NULL;
  251. packed_cell_t *p_cell = NULL;
  252. var_cell_t *v_cell = NULL;
  253. int init_count;
  254. (void)arg;
  255. init_cell_pool();
  256. ch = new_fake_channel();
  257. test_assert(ch);
  258. /* Cache the original count */
  259. init_count = test_cells_written;
  260. /* Stop accepting so we can queue some */
  261. test_chan_accept_cells = 0;
  262. /* Queue a regular cell */
  263. cell = tor_malloc_zero(sizeof(cell_t));
  264. make_fake_cell(cell);
  265. channel_write_cell(ch, cell);
  266. /* It should be queued, so assert that we didn't write it */
  267. test_eq(test_cells_written, init_count);
  268. /* Queue a var cell */
  269. v_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  270. make_fake_var_cell(v_cell);
  271. channel_write_var_cell(ch, v_cell);
  272. /* It should be queued, so assert that we didn't write it */
  273. test_eq(test_cells_written, init_count);
  274. /* Try a packed cell now */
  275. p_cell = packed_cell_new();
  276. test_assert(p_cell);
  277. channel_write_packed_cell(ch, p_cell);
  278. /* It should be queued, so assert that we didn't write it */
  279. test_eq(test_cells_written, init_count);
  280. /* Now allow writes through again */
  281. test_chan_accept_cells = 1;
  282. /* ...and flush */
  283. channel_flush_cells(ch);
  284. /* All three should have gone through */
  285. test_eq(test_cells_written, init_count + 3);
  286. done:
  287. tor_free(ch);
  288. free_cell_pool();
  289. return;
  290. }
  291. /**
  292. * Normal channel lifecycle test:
  293. *
  294. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  295. */
  296. static void
  297. test_channel_lifecycle(void *arg)
  298. {
  299. channel_t *ch1 = NULL, *ch2 = NULL;
  300. cell_t *cell = NULL;
  301. int old_count, init_doesnt_want_writes_count;
  302. int init_releases_count;
  303. (void)arg;
  304. /* Mock these for the whole lifecycle test */
  305. MOCK(scheduler_channel_doesnt_want_writes,
  306. scheduler_channel_doesnt_want_writes_mock);
  307. MOCK(scheduler_release_channel,
  308. scheduler_release_channel_mock);
  309. /* Cache some initial counter values */
  310. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  311. init_releases_count = test_releases_count;
  312. /* Accept cells to lower layer */
  313. test_chan_accept_cells = 1;
  314. /* Use default overhead factor */
  315. test_overhead_estimate = 1.0f;
  316. ch1 = new_fake_channel();
  317. test_assert(ch1);
  318. /* Start it off in OPENING */
  319. ch1->state = CHANNEL_STATE_OPENING;
  320. /* Try to register it */
  321. channel_register(ch1);
  322. test_assert(ch1->registered);
  323. /* Try to write a cell through (should queue) */
  324. cell = tor_malloc_zero(sizeof(cell_t));
  325. make_fake_cell(cell);
  326. old_count = test_cells_written;
  327. channel_write_cell(ch1, cell);
  328. test_eq(old_count, test_cells_written);
  329. /* Move it to OPEN and flush */
  330. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  331. /* Queue should drain */
  332. test_eq(old_count + 1, test_cells_written);
  333. /* Get another one */
  334. ch2 = new_fake_channel();
  335. test_assert(ch2);
  336. ch2->state = CHANNEL_STATE_OPENING;
  337. /* Register */
  338. channel_register(ch2);
  339. test_assert(ch2->registered);
  340. /* Check counters */
  341. test_eq(test_doesnt_want_writes_count, init_doesnt_want_writes_count);
  342. test_eq(test_releases_count, init_releases_count);
  343. /* Move ch1 to MAINT */
  344. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  345. test_eq(test_doesnt_want_writes_count, init_doesnt_want_writes_count + 1);
  346. test_eq(test_releases_count, init_releases_count);
  347. /* Move ch2 to OPEN */
  348. channel_change_state(ch2, CHANNEL_STATE_OPEN);
  349. test_eq(test_doesnt_want_writes_count, init_doesnt_want_writes_count + 1);
  350. test_eq(test_releases_count, init_releases_count);
  351. /* Move ch1 back to OPEN */
  352. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  353. test_eq(test_doesnt_want_writes_count, init_doesnt_want_writes_count + 1);
  354. test_eq(test_releases_count, init_releases_count);
  355. /* Mark ch2 for close */
  356. channel_mark_for_close(ch2);
  357. test_eq(ch2->state, CHANNEL_STATE_CLOSING);
  358. test_eq(test_doesnt_want_writes_count, init_doesnt_want_writes_count + 1);
  359. test_eq(test_releases_count, init_releases_count + 1);
  360. /* Shut down channels */
  361. channel_free_all();
  362. ch1 = ch2 = NULL;
  363. test_eq(test_doesnt_want_writes_count, init_doesnt_want_writes_count + 1);
  364. /* channel_free() calls scheduler_release_channel() */
  365. test_eq(test_releases_count, init_releases_count + 4);
  366. done:
  367. tor_free(ch1);
  368. tor_free(ch2);
  369. UNMOCK(scheduler_channel_doesnt_want_writes);
  370. UNMOCK(scheduler_release_channel);
  371. return;
  372. }
  373. /**
  374. * Weird channel lifecycle test:
  375. *
  376. * OPENING->CLOSING->CLOSED
  377. * OPENING->OPEN->CLOSING->ERROR
  378. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  379. * OPENING->OPEN->MAINT->CLOSING->ERROR
  380. */
  381. static void
  382. test_channel_lifecycle_2(void *arg)
  383. {
  384. channel_t *ch = NULL;
  385. (void)arg;
  386. /* Mock these for the whole lifecycle test */
  387. MOCK(scheduler_channel_doesnt_want_writes,
  388. scheduler_channel_doesnt_want_writes_mock);
  389. MOCK(scheduler_release_channel,
  390. scheduler_release_channel_mock);
  391. /* Accept cells to lower layer */
  392. test_chan_accept_cells = 1;
  393. /* Use default overhead factor */
  394. test_overhead_estimate = 1.0f;
  395. ch = new_fake_channel();
  396. test_assert(ch);
  397. /* Start it off in OPENING */
  398. ch->state = CHANNEL_STATE_OPENING;
  399. /* The full lifecycle test needs a cmux */
  400. ch->cmux = circuitmux_alloc();
  401. /* Try to register it */
  402. channel_register(ch);
  403. test_assert(ch->registered);
  404. /* Try to close it */
  405. channel_mark_for_close(ch);
  406. test_eq(ch->state, CHANNEL_STATE_CLOSING);
  407. /* Finish closing it */
  408. chan_test_finish_close(ch);
  409. test_eq(ch->state, CHANNEL_STATE_CLOSED);
  410. channel_run_cleanup();
  411. ch = NULL;
  412. /* Now try OPENING->OPEN->CLOSING->ERROR */
  413. ch = new_fake_channel();
  414. test_assert(ch);
  415. ch->state = CHANNEL_STATE_OPENING;
  416. ch->cmux = circuitmux_alloc();
  417. channel_register(ch);
  418. test_assert(ch->registered);
  419. /* Finish opening it */
  420. channel_change_state(ch, CHANNEL_STATE_OPEN);
  421. /* Error exit from lower layer */
  422. chan_test_error(ch);
  423. test_eq(ch->state, CHANNEL_STATE_CLOSING);
  424. chan_test_finish_close(ch);
  425. test_eq(ch->state, CHANNEL_STATE_ERROR);
  426. channel_run_cleanup();
  427. ch = NULL;
  428. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  429. ch = new_fake_channel();
  430. test_assert(ch);
  431. ch->state = CHANNEL_STATE_OPENING;
  432. ch->cmux = circuitmux_alloc();
  433. channel_register(ch);
  434. test_assert(ch->registered);
  435. /* Finish opening it */
  436. channel_change_state(ch, CHANNEL_STATE_OPEN);
  437. test_eq(ch->state, CHANNEL_STATE_OPEN);
  438. /* Go to maintenance state */
  439. channel_change_state(ch, CHANNEL_STATE_MAINT);
  440. test_eq(ch->state, CHANNEL_STATE_MAINT);
  441. /* Lower layer close */
  442. channel_mark_for_close(ch);
  443. test_eq(ch->state, CHANNEL_STATE_CLOSING);
  444. /* Finish */
  445. chan_test_finish_close(ch);
  446. test_eq(ch->state, CHANNEL_STATE_CLOSED);
  447. channel_run_cleanup();
  448. ch = NULL;
  449. /*
  450. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  451. * maintenance state
  452. */
  453. ch = new_fake_channel();
  454. test_assert(ch);
  455. ch->state = CHANNEL_STATE_OPENING;
  456. ch->cmux = circuitmux_alloc();
  457. channel_register(ch);
  458. test_assert(ch->registered);
  459. /* Finish opening it */
  460. channel_change_state(ch, CHANNEL_STATE_OPEN);
  461. test_eq(ch->state, CHANNEL_STATE_OPEN);
  462. /* Go to maintenance state */
  463. channel_change_state(ch, CHANNEL_STATE_MAINT);
  464. test_eq(ch->state, CHANNEL_STATE_MAINT);
  465. /* Lower layer close */
  466. channel_close_from_lower_layer(ch);
  467. test_eq(ch->state, CHANNEL_STATE_CLOSING);
  468. /* Finish */
  469. chan_test_finish_close(ch);
  470. test_eq(ch->state, CHANNEL_STATE_CLOSED);
  471. channel_run_cleanup();
  472. ch = NULL;
  473. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  474. ch = new_fake_channel();
  475. test_assert(ch);
  476. ch->state = CHANNEL_STATE_OPENING;
  477. ch->cmux = circuitmux_alloc();
  478. channel_register(ch);
  479. test_assert(ch->registered);
  480. /* Finish opening it */
  481. channel_change_state(ch, CHANNEL_STATE_OPEN);
  482. test_eq(ch->state, CHANNEL_STATE_OPEN);
  483. /* Go to maintenance state */
  484. channel_change_state(ch, CHANNEL_STATE_MAINT);
  485. test_eq(ch->state, CHANNEL_STATE_MAINT);
  486. /* Lower layer close */
  487. chan_test_error(ch);
  488. test_eq(ch->state, CHANNEL_STATE_CLOSING);
  489. /* Finish */
  490. chan_test_finish_close(ch);
  491. test_eq(ch->state, CHANNEL_STATE_ERROR);
  492. channel_run_cleanup();
  493. ch = NULL;
  494. /* Shut down channels */
  495. channel_free_all();
  496. done:
  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_multi(void *arg)
  504. {
  505. channel_t *ch1 = NULL, *ch2 = NULL;
  506. uint64_t global_queue_estimate;
  507. cell_t *cell = NULL;
  508. (void)arg;
  509. /* Accept cells to lower layer */
  510. test_chan_accept_cells = 1;
  511. /* Use default overhead factor */
  512. test_overhead_estimate = 1.0f;
  513. ch1 = new_fake_channel();
  514. test_assert(ch1);
  515. ch2 = new_fake_channel();
  516. test_assert(ch2);
  517. /* Initial queue size update */
  518. channel_update_xmit_queue_size(ch1);
  519. test_eq(ch1->bytes_queued_for_xmit, 0);
  520. channel_update_xmit_queue_size(ch2);
  521. test_eq(ch2->bytes_queued_for_xmit, 0);
  522. global_queue_estimate = channel_get_global_queue_estimate();
  523. test_eq(global_queue_estimate, 0);
  524. /* Queue some cells, check queue estimates */
  525. cell = tor_malloc_zero(sizeof(cell_t));
  526. make_fake_cell(cell);
  527. channel_write_cell(ch1, cell);
  528. cell = tor_malloc_zero(sizeof(cell_t));
  529. make_fake_cell(cell);
  530. channel_write_cell(ch2, cell);
  531. channel_update_xmit_queue_size(ch1);
  532. channel_update_xmit_queue_size(ch2);
  533. test_eq(ch1->bytes_queued_for_xmit, 0);
  534. test_eq(ch2->bytes_queued_for_xmit, 0);
  535. global_queue_estimate = channel_get_global_queue_estimate();
  536. test_eq(global_queue_estimate, 0);
  537. /* Stop accepting cells at lower layer */
  538. test_chan_accept_cells = 0;
  539. /* Queue some cells and check queue estimates */
  540. cell = tor_malloc_zero(sizeof(cell_t));
  541. make_fake_cell(cell);
  542. channel_write_cell(ch1, cell);
  543. channel_update_xmit_queue_size(ch1);
  544. test_eq(ch1->bytes_queued_for_xmit, 512);
  545. global_queue_estimate = channel_get_global_queue_estimate();
  546. test_eq(global_queue_estimate, 512);
  547. cell = tor_malloc_zero(sizeof(cell_t));
  548. make_fake_cell(cell);
  549. channel_write_cell(ch2, cell);
  550. channel_update_xmit_queue_size(ch2);
  551. test_eq(ch2->bytes_queued_for_xmit, 512);
  552. global_queue_estimate = channel_get_global_queue_estimate();
  553. test_eq(global_queue_estimate, 1024);
  554. /* Allow cells through again */
  555. test_chan_accept_cells = 1;
  556. /* Flush chan 2 */
  557. channel_flush_cells(ch2);
  558. /* Update and check queue sizes */
  559. channel_update_xmit_queue_size(ch1);
  560. channel_update_xmit_queue_size(ch2);
  561. test_eq(ch1->bytes_queued_for_xmit, 512);
  562. test_eq(ch2->bytes_queued_for_xmit, 0);
  563. global_queue_estimate = channel_get_global_queue_estimate();
  564. test_eq(global_queue_estimate, 512);
  565. /* Flush chan 1 */
  566. channel_flush_cells(ch1);
  567. /* Update and check queue sizes */
  568. channel_update_xmit_queue_size(ch1);
  569. channel_update_xmit_queue_size(ch2);
  570. test_eq(ch1->bytes_queued_for_xmit, 0);
  571. test_eq(ch2->bytes_queued_for_xmit, 0);
  572. global_queue_estimate = channel_get_global_queue_estimate();
  573. test_eq(global_queue_estimate, 0);
  574. /* Now block again */
  575. test_chan_accept_cells = 0;
  576. /* Queue some cells */
  577. cell = tor_malloc_zero(sizeof(cell_t));
  578. make_fake_cell(cell);
  579. channel_write_cell(ch1, cell);
  580. cell = tor_malloc_zero(sizeof(cell_t));
  581. make_fake_cell(cell);
  582. channel_write_cell(ch2, cell);
  583. /* Check the estimates */
  584. channel_update_xmit_queue_size(ch1);
  585. channel_update_xmit_queue_size(ch2);
  586. test_eq(ch1->bytes_queued_for_xmit, 512);
  587. test_eq(ch2->bytes_queued_for_xmit, 512);
  588. global_queue_estimate = channel_get_global_queue_estimate();
  589. test_eq(global_queue_estimate, 1024);
  590. /* Now close channel 2; it should be subtracted from the global queue */
  591. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  592. channel_mark_for_close(ch2);
  593. UNMOCK(scheduler_release_channel);
  594. global_queue_estimate = channel_get_global_queue_estimate();
  595. test_eq(global_queue_estimate, 512);
  596. /*
  597. * Since the fake channels aren't registered, channel_free_all() can't
  598. * see them properly.
  599. */
  600. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  601. channel_mark_for_close(ch1);
  602. UNMOCK(scheduler_release_channel);
  603. global_queue_estimate = channel_get_global_queue_estimate();
  604. test_eq(global_queue_estimate, 0);
  605. /* Now free everything */
  606. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  607. channel_free_all();
  608. UNMOCK(scheduler_release_channel);
  609. done:
  610. tor_free(ch1);
  611. tor_free(ch2);
  612. return;
  613. }
  614. /**
  615. * Check some hopefully-impossible edge cases in the channel queue we
  616. * can only trigger by doing evil things to the queue directly.
  617. */
  618. static void
  619. test_channel_queue_impossible(void *arg)
  620. {
  621. channel_t *ch = NULL;
  622. cell_t *cell = NULL;
  623. packed_cell_t *packed_cell = NULL;
  624. var_cell_t *var_cell = NULL;
  625. int old_count;
  626. cell_queue_entry_t *q = NULL;
  627. (void)arg;
  628. init_cell_pool();
  629. packed_cell = packed_cell_new();
  630. test_assert(packed_cell);
  631. ch = new_fake_channel();
  632. test_assert(ch);
  633. /* We test queueing here; tell it not to accept cells */
  634. test_chan_accept_cells = 0;
  635. /* ...and keep it from trying to flush the queue */
  636. ch->state = CHANNEL_STATE_MAINT;
  637. /* Cache the cell written count */
  638. old_count = test_cells_written;
  639. /* Assert that the queue is initially empty */
  640. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)), 0);
  641. /* Get a fresh cell and write it to the channel*/
  642. cell = tor_malloc_zero(sizeof(cell_t));
  643. make_fake_cell(cell);
  644. channel_write_cell(ch, cell);
  645. /* Now it should be queued */
  646. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)),1);
  647. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  648. test_assert(q);
  649. if (q) {
  650. test_eq(q->type, CELL_QUEUE_FIXED);
  651. test_eq(q->u.fixed.cell, cell);
  652. }
  653. /* Do perverse things to it */
  654. tor_free(q->u.fixed.cell);
  655. q->u.fixed.cell = NULL;
  656. /*
  657. * Now change back to open with channel_change_state() and assert that it
  658. * gets thrown away properly.
  659. */
  660. test_chan_accept_cells = 1;
  661. channel_change_state(ch, CHANNEL_STATE_OPEN);
  662. test_assert(test_cells_written == old_count);
  663. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)), 0);
  664. /* Same thing but for a var_cell */
  665. test_chan_accept_cells = 0;
  666. ch->state = CHANNEL_STATE_MAINT;
  667. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  668. make_fake_var_cell(var_cell);
  669. channel_write_var_cell(ch, var_cell);
  670. /* Check that it's queued */
  671. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)), 1);
  672. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  673. test_assert(q);
  674. if (q) {
  675. test_eq(q->type, CELL_QUEUE_VAR);
  676. test_eq(q->u.var.var_cell, var_cell);
  677. }
  678. /* Remove the cell from the queue entry */
  679. tor_free(q->u.var.var_cell);
  680. q->u.var.var_cell = NULL;
  681. /* Let it drain and check that the bad entry is discarded */
  682. test_chan_accept_cells = 1;
  683. channel_change_state(ch, CHANNEL_STATE_OPEN);
  684. test_assert(test_cells_written == old_count);
  685. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)), 0);
  686. /* Same thing with a packed_cell */
  687. test_chan_accept_cells = 0;
  688. ch->state = CHANNEL_STATE_MAINT;
  689. packed_cell = packed_cell_new();
  690. test_assert(packed_cell);
  691. channel_write_packed_cell(ch, packed_cell);
  692. /* Check that it's queued */
  693. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)), 1);
  694. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  695. test_assert(q);
  696. if (q) {
  697. test_eq(q->type, CELL_QUEUE_PACKED);
  698. test_eq(q->u.packed.packed_cell, packed_cell);
  699. }
  700. /* Remove the cell from the queue entry */
  701. packed_cell_free(q->u.packed.packed_cell);
  702. q->u.packed.packed_cell = NULL;
  703. /* Let it drain and check that the bad entry is discarded */
  704. test_chan_accept_cells = 1;
  705. channel_change_state(ch, CHANNEL_STATE_OPEN);
  706. test_assert(test_cells_written == old_count);
  707. test_eq(chan_cell_queue_len(&(ch->outgoing_queue)), 0);
  708. done:
  709. tor_free(ch);
  710. free_cell_pool();
  711. return;
  712. }
  713. static void
  714. test_channel_queue_size(void *arg)
  715. {
  716. channel_t *ch = NULL;
  717. cell_t *cell = NULL;
  718. int n, old_count;
  719. uint64_t global_queue_estimate;
  720. (void)arg;
  721. ch = new_fake_channel();
  722. test_assert(ch);
  723. /* Initial queue size update */
  724. channel_update_xmit_queue_size(ch);
  725. test_eq(ch->bytes_queued_for_xmit, 0);
  726. global_queue_estimate = channel_get_global_queue_estimate();
  727. test_eq(global_queue_estimate, 0);
  728. /* Test the call-through to our fake lower layer */
  729. n = channel_num_cells_writeable(ch);
  730. /* chan_test_num_cells_writeable() always returns 32 */
  731. test_eq(n, 32);
  732. /*
  733. * Now we queue some cells and check that channel_num_cells_writeable()
  734. * adjusts properly
  735. */
  736. /* tell it not to accept cells */
  737. test_chan_accept_cells = 0;
  738. /* ...and keep it from trying to flush the queue */
  739. ch->state = CHANNEL_STATE_MAINT;
  740. /* Get a fresh cell */
  741. cell = tor_malloc_zero(sizeof(cell_t));
  742. make_fake_cell(cell);
  743. old_count = test_cells_written;
  744. channel_write_cell(ch, cell);
  745. /* Assert that it got queued, not written through, correctly */
  746. test_eq(test_cells_written, old_count);
  747. /* Now check chan_test_num_cells_writeable() again */
  748. n = channel_num_cells_writeable(ch);
  749. test_eq(n, 0); /* Should return 0 since we're in CHANNEL_STATE_MAINT */
  750. /* Update queue size estimates */
  751. channel_update_xmit_queue_size(ch);
  752. /* One cell, times an overhead factor of 1.0 */
  753. test_eq(ch->bytes_queued_for_xmit, 512);
  754. /* Try a different overhead factor */
  755. test_overhead_estimate = 0.5f;
  756. /* This one should be ignored since it's below 1.0 */
  757. channel_update_xmit_queue_size(ch);
  758. test_eq(ch->bytes_queued_for_xmit, 512);
  759. /* Now try a larger one */
  760. test_overhead_estimate = 2.0f;
  761. channel_update_xmit_queue_size(ch);
  762. test_eq(ch->bytes_queued_for_xmit, 1024);
  763. /* Go back to 1.0 */
  764. test_overhead_estimate = 1.0f;
  765. channel_update_xmit_queue_size(ch);
  766. test_eq(ch->bytes_queued_for_xmit, 512);
  767. /* Check the global estimate too */
  768. global_queue_estimate = channel_get_global_queue_estimate();
  769. test_eq(global_queue_estimate, 512);
  770. /* Go to open */
  771. old_count = test_cells_written;
  772. channel_change_state(ch, CHANNEL_STATE_OPEN);
  773. /*
  774. * It should try to write, but we aren't accepting cells right now, so
  775. * it'll requeue
  776. */
  777. test_eq(test_cells_written, old_count);
  778. /* Check the queue size again */
  779. channel_update_xmit_queue_size(ch);
  780. test_eq(ch->bytes_queued_for_xmit, 512);
  781. global_queue_estimate = channel_get_global_queue_estimate();
  782. test_eq(global_queue_estimate, 512);
  783. /*
  784. * Now the cell is in the queue, and we're open, so we should get 31
  785. * writeable cells.
  786. */
  787. n = channel_num_cells_writeable(ch);
  788. test_eq(n, 31);
  789. /* Accept cells again */
  790. test_chan_accept_cells = 1;
  791. /* ...and re-process the queue */
  792. old_count = test_cells_written;
  793. channel_flush_cells(ch);
  794. test_eq(test_cells_written, old_count + 1);
  795. /* Should have 32 writeable now */
  796. n = channel_num_cells_writeable(ch);
  797. test_eq(n, 32);
  798. /* Should have queue size estimate of zero */
  799. channel_update_xmit_queue_size(ch);
  800. test_eq(ch->bytes_queued_for_xmit, 0);
  801. global_queue_estimate = channel_get_global_queue_estimate();
  802. test_eq(global_queue_estimate, 0);
  803. /* Okay, now we're done with this one */
  804. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  805. channel_mark_for_close(ch);
  806. UNMOCK(scheduler_release_channel);
  807. done:
  808. tor_free(ch);
  809. return;
  810. }
  811. static void
  812. test_channel_write(void *arg)
  813. {
  814. channel_t *ch = NULL;
  815. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  816. packed_cell_t *packed_cell = NULL;
  817. var_cell_t *var_cell =
  818. tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  819. int old_count;
  820. (void)arg;
  821. init_cell_pool();
  822. packed_cell = packed_cell_new();
  823. test_assert(packed_cell);
  824. ch = new_fake_channel();
  825. test_assert(ch);
  826. make_fake_cell(cell);
  827. make_fake_var_cell(var_cell);
  828. /* Tell it to accept cells */
  829. test_chan_accept_cells = 1;
  830. old_count = test_cells_written;
  831. channel_write_cell(ch, cell);
  832. test_assert(test_cells_written == old_count + 1);
  833. channel_write_var_cell(ch, var_cell);
  834. test_assert(test_cells_written == old_count + 2);
  835. channel_write_packed_cell(ch, packed_cell);
  836. test_assert(test_cells_written == old_count + 3);
  837. /* Now we test queueing; tell it not to accept cells */
  838. test_chan_accept_cells = 0;
  839. /* ...and keep it from trying to flush the queue */
  840. ch->state = CHANNEL_STATE_MAINT;
  841. /* Get a fresh cell */
  842. cell = tor_malloc_zero(sizeof(cell_t));
  843. make_fake_cell(cell);
  844. old_count = test_cells_written;
  845. channel_write_cell(ch, cell);
  846. test_assert(test_cells_written == old_count);
  847. /*
  848. * Now change back to open with channel_change_state() and assert that it
  849. * gets drained from the queue.
  850. */
  851. test_chan_accept_cells = 1;
  852. channel_change_state(ch, CHANNEL_STATE_OPEN);
  853. test_assert(test_cells_written == old_count + 1);
  854. /*
  855. * Check the note destroy case
  856. */
  857. cell = tor_malloc_zero(sizeof(cell_t));
  858. make_fake_cell(cell);
  859. cell->command = CELL_DESTROY;
  860. /* Set up the mock */
  861. MOCK(channel_note_destroy_not_pending,
  862. channel_note_destroy_not_pending_mock);
  863. old_count = test_destroy_not_pending_calls;
  864. channel_write_cell(ch, cell);
  865. test_assert(test_destroy_not_pending_calls == old_count + 1);
  866. /* Now send a non-destroy and check we don't call it */
  867. cell = tor_malloc_zero(sizeof(cell_t));
  868. make_fake_cell(cell);
  869. channel_write_cell(ch, cell);
  870. test_assert(test_destroy_not_pending_calls == old_count + 1);
  871. UNMOCK(channel_note_destroy_not_pending);
  872. /*
  873. * Now switch it to CLOSING so we can test the discard-cells case
  874. * in the channel_write_*() functions.
  875. */
  876. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  877. channel_mark_for_close(ch);
  878. UNMOCK(scheduler_release_channel);
  879. /* Send cells that will drop in the closing state */
  880. old_count = test_cells_written;
  881. cell = tor_malloc_zero(sizeof(cell_t));
  882. make_fake_cell(cell);
  883. channel_write_cell(ch, cell);
  884. test_assert(test_cells_written == old_count);
  885. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  886. make_fake_var_cell(var_cell);
  887. channel_write_var_cell(ch, var_cell);
  888. test_assert(test_cells_written == old_count);
  889. packed_cell = packed_cell_new();
  890. channel_write_packed_cell(ch, packed_cell);
  891. test_assert(test_cells_written == old_count);
  892. free_cell_pool();
  893. done:
  894. tor_free(ch);
  895. return;
  896. }
  897. struct testcase_t channel_tests[] = {
  898. { "flush", test_channel_flush, TT_FORK, NULL, NULL },
  899. { "lifecycle", test_channel_lifecycle, TT_FORK, NULL, NULL },
  900. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK, NULL, NULL },
  901. { "multi", test_channel_multi, TT_FORK, NULL, NULL },
  902. { "queue_impossible", test_channel_queue_impossible, TT_FORK, NULL, NULL },
  903. { "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
  904. { "write", test_channel_write, TT_FORK, NULL, NULL },
  905. END_OF_TESTCASES
  906. };