test_channel.c 33 KB

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