test_channel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #include "test.h"
  16. static int test_chan_accept_cells = 0;
  17. static int test_cells_written = 0;
  18. static int test_destroy_not_pending_calls = 0;
  19. static double test_overhead_estimate = 1.0f;
  20. static void channel_note_destroy_not_pending_mock(channel_t *ch,
  21. circid_t circid);
  22. static void chan_test_close(channel_t *ch);
  23. static size_t chan_test_num_bytes_queued(channel_t *ch);
  24. static int chan_test_num_cells_writeable(channel_t *ch);
  25. static int chan_test_write_cell(channel_t *ch, cell_t *cell);
  26. static int chan_test_write_packed_cell(channel_t *ch,
  27. packed_cell_t *packed_cell);
  28. static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
  29. static void make_fake_cell(cell_t *c);
  30. static void make_fake_var_cell(var_cell_t *c);
  31. static channel_t * new_fake_channel(void);
  32. static void scheduler_release_channel_mock(channel_t *ch);
  33. static void test_channel_multi(void *arg);
  34. static void test_channel_queue_size(void *arg);
  35. static void test_channel_write(void *arg);
  36. static void
  37. channel_note_destroy_not_pending_mock(channel_t *ch,
  38. circid_t circid)
  39. {
  40. (void)ch;
  41. (void)circid;
  42. ++test_destroy_not_pending_calls;
  43. }
  44. static void
  45. chan_test_close(channel_t *ch)
  46. {
  47. test_assert(ch);
  48. done:
  49. return;
  50. }
  51. static double
  52. chan_test_get_overhead_estimate(channel_t *ch)
  53. {
  54. test_assert(ch);
  55. done:
  56. return test_overhead_estimate;
  57. }
  58. static size_t
  59. chan_test_num_bytes_queued(channel_t *ch)
  60. {
  61. test_assert(ch);
  62. done:
  63. return 0;
  64. }
  65. static int
  66. chan_test_num_cells_writeable(channel_t *ch)
  67. {
  68. test_assert(ch);
  69. done:
  70. return 32;
  71. }
  72. static int
  73. chan_test_write_cell(channel_t *ch, cell_t *cell)
  74. {
  75. int rv = 0;
  76. test_assert(ch);
  77. test_assert(cell);
  78. if (test_chan_accept_cells) {
  79. /* Free the cell and bump the counter */
  80. tor_free(cell);
  81. ++test_cells_written;
  82. rv = 1;
  83. }
  84. /* else return 0, we didn't accept it */
  85. done:
  86. return rv;
  87. }
  88. static int
  89. chan_test_write_packed_cell(channel_t *ch,
  90. packed_cell_t *packed_cell)
  91. {
  92. int rv = 0;
  93. test_assert(ch);
  94. test_assert(packed_cell);
  95. if (test_chan_accept_cells) {
  96. /* Free the cell and bump the counter */
  97. packed_cell_free(packed_cell);
  98. ++test_cells_written;
  99. rv = 1;
  100. }
  101. /* else return 0, we didn't accept it */
  102. done:
  103. return rv;
  104. }
  105. static int
  106. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  107. {
  108. int rv = 0;
  109. test_assert(ch);
  110. test_assert(var_cell);
  111. if (test_chan_accept_cells) {
  112. /* Free the cell and bump the counter */
  113. var_cell_free(var_cell);
  114. ++test_cells_written;
  115. rv = 1;
  116. }
  117. /* else return 0, we didn't accept it */
  118. done:
  119. return rv;
  120. }
  121. static void
  122. make_fake_cell(cell_t *c)
  123. {
  124. test_assert(c != NULL);
  125. c->circ_id = 1;
  126. c->command = CELL_RELAY;
  127. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  128. done:
  129. return;
  130. }
  131. static void
  132. make_fake_var_cell(var_cell_t *c)
  133. {
  134. test_assert(c != NULL);
  135. c->circ_id = 1;
  136. c->command = CELL_VERSIONS;
  137. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  138. memset(c->payload, 0, c->payload_len);
  139. done:
  140. return;
  141. }
  142. static channel_t *
  143. new_fake_channel(void)
  144. {
  145. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  146. channel_init(chan);
  147. chan->close = chan_test_close;
  148. chan->get_overhead_estimate = chan_test_get_overhead_estimate;
  149. chan->num_bytes_queued = chan_test_num_bytes_queued;
  150. chan->num_cells_writeable = chan_test_num_cells_writeable;
  151. chan->write_cell = chan_test_write_cell;
  152. chan->write_packed_cell = chan_test_write_packed_cell;
  153. chan->write_var_cell = chan_test_write_var_cell;
  154. chan->state = CHANNEL_STATE_OPEN;
  155. return chan;
  156. }
  157. static void
  158. scheduler_release_channel_mock(channel_t *ch)
  159. {
  160. (void)ch;
  161. /* Increment counter */
  162. ++test_releases_count;
  163. return;
  164. }
  165. static void
  166. test_channel_multi(void *arg)
  167. {
  168. channel_t *ch1 = NULL, *ch2 = NULL;
  169. uint64_t global_queue_estimate;
  170. cell_t *cell = NULL;
  171. (void)arg;
  172. /* Accept cells to lower layer */
  173. test_chan_accept_cells = 1;
  174. /* Use default overhead factor */
  175. test_overhead_estimate = 1.0f;
  176. ch1 = new_fake_channel();
  177. test_assert(ch1);
  178. ch2 = new_fake_channel();
  179. test_assert(ch2);
  180. /* Initial queue size update */
  181. channel_update_xmit_queue_size(ch1);
  182. test_eq(ch1->bytes_queued_for_xmit, 0);
  183. channel_update_xmit_queue_size(ch2);
  184. test_eq(ch2->bytes_queued_for_xmit, 0);
  185. global_queue_estimate = channel_get_global_queue_estimate();
  186. test_eq(global_queue_estimate, 0);
  187. /* Queue some cells, check queue estimates */
  188. cell = tor_malloc_zero(sizeof(cell_t));
  189. make_fake_cell(cell);
  190. channel_write_cell(ch1, cell);
  191. cell = tor_malloc_zero(sizeof(cell_t));
  192. make_fake_cell(cell);
  193. channel_write_cell(ch2, cell);
  194. channel_update_xmit_queue_size(ch1);
  195. channel_update_xmit_queue_size(ch2);
  196. test_eq(ch1->bytes_queued_for_xmit, 0);
  197. test_eq(ch2->bytes_queued_for_xmit, 0);
  198. global_queue_estimate = channel_get_global_queue_estimate();
  199. test_eq(global_queue_estimate, 0);
  200. /* Stop accepting cells at lower layer */
  201. test_chan_accept_cells = 0;
  202. /* Queue some cells and check queue estimates */
  203. cell = tor_malloc_zero(sizeof(cell_t));
  204. make_fake_cell(cell);
  205. channel_write_cell(ch1, cell);
  206. channel_update_xmit_queue_size(ch1);
  207. test_eq(ch1->bytes_queued_for_xmit, 512);
  208. global_queue_estimate = channel_get_global_queue_estimate();
  209. test_eq(global_queue_estimate, 512);
  210. cell = tor_malloc_zero(sizeof(cell_t));
  211. make_fake_cell(cell);
  212. channel_write_cell(ch2, cell);
  213. channel_update_xmit_queue_size(ch2);
  214. test_eq(ch2->bytes_queued_for_xmit, 512);
  215. global_queue_estimate = channel_get_global_queue_estimate();
  216. test_eq(global_queue_estimate, 1024);
  217. /* Allow cells through again */
  218. test_chan_accept_cells = 1;
  219. /* Flush chan 2 */
  220. channel_flush_cells(ch2);
  221. /* Update and check queue sizes */
  222. channel_update_xmit_queue_size(ch1);
  223. channel_update_xmit_queue_size(ch2);
  224. test_eq(ch1->bytes_queued_for_xmit, 512);
  225. test_eq(ch2->bytes_queued_for_xmit, 0);
  226. global_queue_estimate = channel_get_global_queue_estimate();
  227. test_eq(global_queue_estimate, 512);
  228. /* Flush chan 1 */
  229. channel_flush_cells(ch1);
  230. /* Update and check queue sizes */
  231. channel_update_xmit_queue_size(ch1);
  232. channel_update_xmit_queue_size(ch2);
  233. test_eq(ch1->bytes_queued_for_xmit, 0);
  234. test_eq(ch2->bytes_queued_for_xmit, 0);
  235. global_queue_estimate = channel_get_global_queue_estimate();
  236. test_eq(global_queue_estimate, 0);
  237. /* Now block again */
  238. test_chan_accept_cells = 0;
  239. /* Queue some cells */
  240. cell = tor_malloc_zero(sizeof(cell_t));
  241. make_fake_cell(cell);
  242. channel_write_cell(ch1, cell);
  243. cell = tor_malloc_zero(sizeof(cell_t));
  244. make_fake_cell(cell);
  245. channel_write_cell(ch2, cell);
  246. /* Check the estimates */
  247. channel_update_xmit_queue_size(ch1);
  248. channel_update_xmit_queue_size(ch2);
  249. test_eq(ch1->bytes_queued_for_xmit, 512);
  250. test_eq(ch2->bytes_queued_for_xmit, 512);
  251. global_queue_estimate = channel_get_global_queue_estimate();
  252. test_eq(global_queue_estimate, 1024);
  253. /* Now close channel 2; it should be subtracted from the global queue */
  254. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  255. channel_mark_for_close(ch2);
  256. UNMOCK(scheduler_release_channel);
  257. global_queue_estimate = channel_get_global_queue_estimate();
  258. test_eq(global_queue_estimate, 512);
  259. /* Now free everything */
  260. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  261. channel_free_all();
  262. UNMOCK(scheduler_release_channel);
  263. done:
  264. tor_free(ch1);
  265. tor_free(ch2);
  266. return;
  267. }
  268. static void
  269. test_channel_queue_size(void *arg)
  270. {
  271. channel_t *ch = NULL;
  272. cell_t *cell = NULL;
  273. int n, old_count;
  274. uint64_t global_queue_estimate;
  275. (void)arg;
  276. ch = new_fake_channel();
  277. test_assert(ch);
  278. /* Initial queue size update */
  279. channel_update_xmit_queue_size(ch);
  280. test_eq(ch->bytes_queued_for_xmit, 0);
  281. global_queue_estimate = channel_get_global_queue_estimate();
  282. test_eq(global_queue_estimate, 0);
  283. /* Test the call-through to our fake lower layer */
  284. n = channel_num_cells_writeable(ch);
  285. /* chan_test_num_cells_writeable() always returns 32 */
  286. test_eq(n, 32);
  287. /*
  288. * Now we queue some cells and check that channel_num_cells_writeable()
  289. * adjusts properly
  290. */
  291. /* tell it not to accept cells */
  292. test_chan_accept_cells = 0;
  293. /* ...and keep it from trying to flush the queue */
  294. ch->state = CHANNEL_STATE_MAINT;
  295. /* Get a fresh cell */
  296. cell = tor_malloc_zero(sizeof(cell_t));
  297. make_fake_cell(cell);
  298. old_count = test_cells_written;
  299. channel_write_cell(ch, cell);
  300. /* Assert that it got queued, not written through, correctly */
  301. test_eq(test_cells_written, old_count);
  302. /* Now check chan_test_num_cells_writeable() again */
  303. n = channel_num_cells_writeable(ch);
  304. test_eq(n, 0); /* Should return 0 since we're in CHANNEL_STATE_MAINT */
  305. /* Update queue size estimates */
  306. channel_update_xmit_queue_size(ch);
  307. /* One cell, times an overhead factor of 1.0 */
  308. test_eq(ch->bytes_queued_for_xmit, 512);
  309. /* Try a different overhead factor */
  310. test_overhead_estimate = 0.5f;
  311. /* This one should be ignored since it's below 1.0 */
  312. channel_update_xmit_queue_size(ch);
  313. test_eq(ch->bytes_queued_for_xmit, 512);
  314. /* Now try a larger one */
  315. test_overhead_estimate = 2.0f;
  316. channel_update_xmit_queue_size(ch);
  317. test_eq(ch->bytes_queued_for_xmit, 1024);
  318. /* Go back to 1.0 */
  319. test_overhead_estimate = 1.0f;
  320. channel_update_xmit_queue_size(ch);
  321. test_eq(ch->bytes_queued_for_xmit, 512);
  322. /* Check the global estimate too */
  323. global_queue_estimate = channel_get_global_queue_estimate();
  324. test_eq(global_queue_estimate, 512);
  325. /* Go to open */
  326. old_count = test_cells_written;
  327. channel_change_state(ch, CHANNEL_STATE_OPEN);
  328. /*
  329. * It should try to write, but we aren't accepting cells right now, so
  330. * it'll requeue
  331. */
  332. test_eq(test_cells_written, old_count);
  333. /* Check the queue size again */
  334. channel_update_xmit_queue_size(ch);
  335. test_eq(ch->bytes_queued_for_xmit, 512);
  336. global_queue_estimate = channel_get_global_queue_estimate();
  337. test_eq(global_queue_estimate, 512);
  338. /*
  339. * Now the cell is in the queue, and we're open, so we should get 31
  340. * writeable cells.
  341. */
  342. n = channel_num_cells_writeable(ch);
  343. test_eq(n, 31);
  344. /* Accept cells again */
  345. test_chan_accept_cells = 1;
  346. /* ...and re-process the queue */
  347. old_count = test_cells_written;
  348. channel_flush_cells(ch);
  349. test_eq(test_cells_written, old_count + 1);
  350. /* Should have 32 writeable now */
  351. n = channel_num_cells_writeable(ch);
  352. test_eq(n, 32);
  353. /* Should have queue size estimate of zero */
  354. channel_update_xmit_queue_size(ch);
  355. test_eq(ch->bytes_queued_for_xmit, 0);
  356. global_queue_estimate = channel_get_global_queue_estimate();
  357. test_eq(global_queue_estimate, 0);
  358. /* Okay, now we're done with this one */
  359. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  360. channel_mark_for_close(ch);
  361. UNMOCK(scheduler_release_channel);
  362. done:
  363. tor_free(ch);
  364. return;
  365. }
  366. static void
  367. test_channel_write(void *arg)
  368. {
  369. channel_t *ch = NULL;
  370. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  371. packed_cell_t *packed_cell = NULL;
  372. var_cell_t *var_cell =
  373. tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  374. int old_count;
  375. (void)arg;
  376. init_cell_pool();
  377. packed_cell = packed_cell_new();
  378. test_assert(packed_cell);
  379. ch = new_fake_channel();
  380. test_assert(ch);
  381. make_fake_cell(cell);
  382. make_fake_var_cell(var_cell);
  383. /* Tell it to accept cells */
  384. test_chan_accept_cells = 1;
  385. old_count = test_cells_written;
  386. channel_write_cell(ch, cell);
  387. test_assert(test_cells_written == old_count + 1);
  388. channel_write_var_cell(ch, var_cell);
  389. test_assert(test_cells_written == old_count + 2);
  390. channel_write_packed_cell(ch, packed_cell);
  391. test_assert(test_cells_written == old_count + 3);
  392. /* Now we test queueing; tell it not to accept cells */
  393. test_chan_accept_cells = 0;
  394. /* ...and keep it from trying to flush the queue */
  395. ch->state = CHANNEL_STATE_MAINT;
  396. /* Get a fresh cell */
  397. cell = tor_malloc_zero(sizeof(cell_t));
  398. make_fake_cell(cell);
  399. old_count = test_cells_written;
  400. channel_write_cell(ch, cell);
  401. test_assert(test_cells_written == old_count);
  402. /*
  403. * Now change back to open with channel_change_state() and assert that it
  404. * gets drained from the queue.
  405. */
  406. test_chan_accept_cells = 1;
  407. channel_change_state(ch, CHANNEL_STATE_OPEN);
  408. test_assert(test_cells_written == old_count + 1);
  409. /*
  410. * Check the note destroy case
  411. */
  412. cell = tor_malloc_zero(sizeof(cell_t));
  413. make_fake_cell(cell);
  414. cell->command = CELL_DESTROY;
  415. /* Set up the mock */
  416. MOCK(channel_note_destroy_not_pending,
  417. channel_note_destroy_not_pending_mock);
  418. old_count = test_destroy_not_pending_calls;
  419. channel_write_cell(ch, cell);
  420. test_assert(test_destroy_not_pending_calls == old_count + 1);
  421. /* Now send a non-destroy and check we don't call it */
  422. cell = tor_malloc_zero(sizeof(cell_t));
  423. make_fake_cell(cell);
  424. channel_write_cell(ch, cell);
  425. test_assert(test_destroy_not_pending_calls == old_count + 1);
  426. UNMOCK(channel_note_destroy_not_pending);
  427. /*
  428. * Now switch it to CLOSING so we can test the discard-cells case
  429. * in the channel_write_*() functions.
  430. */
  431. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  432. channel_mark_for_close(ch);
  433. UNMOCK(scheduler_release_channel);
  434. /* Send cells that will drop in the closing state */
  435. old_count = test_cells_written;
  436. cell = tor_malloc_zero(sizeof(cell_t));
  437. make_fake_cell(cell);
  438. channel_write_cell(ch, cell);
  439. test_assert(test_cells_written == old_count);
  440. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  441. make_fake_var_cell(var_cell);
  442. channel_write_var_cell(ch, var_cell);
  443. test_assert(test_cells_written == old_count);
  444. packed_cell = packed_cell_new();
  445. channel_write_packed_cell(ch, packed_cell);
  446. test_assert(test_cells_written == old_count);
  447. free_cell_pool();
  448. done:
  449. tor_free(ch);
  450. return;
  451. }
  452. struct testcase_t channel_tests[] = {
  453. { "multi", test_channel_multi, TT_FORK, NULL, NULL },
  454. { "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
  455. { "write", test_channel_write, TT_FORK, NULL, NULL },
  456. END_OF_TESTCASES
  457. };