test_channel.c 43 KB

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