test_channel.c 38 KB

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