test_channel.c 20 KB

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