test_channel.c 23 KB

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