test_channel.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. #ifdef ENABLE_MEMPOOLS
  528. init_cell_pool();
  529. #endif /* ENABLE_MEMPOOLS */
  530. ch = new_fake_channel();
  531. tt_assert(ch);
  532. /* Cache the original count */
  533. init_count = test_cells_written;
  534. /* Stop accepting so we can queue some */
  535. test_chan_accept_cells = 0;
  536. /* Queue a regular cell */
  537. cell = tor_malloc_zero(sizeof(cell_t));
  538. make_fake_cell(cell);
  539. channel_write_cell(ch, cell);
  540. /* It should be queued, so assert that we didn't write it */
  541. tt_int_op(test_cells_written, ==, init_count);
  542. /* Queue a var cell */
  543. v_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  544. make_fake_var_cell(v_cell);
  545. channel_write_var_cell(ch, v_cell);
  546. /* It should be queued, so assert that we didn't write it */
  547. tt_int_op(test_cells_written, ==, init_count);
  548. /* Try a packed cell now */
  549. p_cell = packed_cell_new();
  550. tt_assert(p_cell);
  551. channel_write_packed_cell(ch, p_cell);
  552. /* It should be queued, so assert that we didn't write it */
  553. tt_int_op(test_cells_written, ==, init_count);
  554. /* Now allow writes through again */
  555. test_chan_accept_cells = 1;
  556. /* ...and flush */
  557. channel_flush_cells(ch);
  558. /* All three should have gone through */
  559. tt_int_op(test_cells_written, ==, init_count + 3);
  560. done:
  561. tor_free(ch);
  562. #ifdef ENABLE_MEMPOOLS
  563. free_cell_pool();
  564. #endif /* ENABLE_MEMPOOLS */
  565. return;
  566. }
  567. /**
  568. * Channel flush tests that require cmux mocking
  569. */
  570. static void
  571. test_channel_flushmux(void *arg)
  572. {
  573. channel_t *ch = NULL;
  574. int old_count, q_len_before, q_len_after;
  575. ssize_t result;
  576. (void)arg;
  577. #ifdef ENABLE_MEMPOOLS
  578. init_cell_pool();
  579. #endif /* ENABLE_MEMPOOLS */
  580. /* Install mocks we need for this test */
  581. MOCK(channel_flush_from_first_active_circuit,
  582. chan_test_channel_flush_from_first_active_circuit_mock);
  583. MOCK(circuitmux_num_cells,
  584. chan_test_circuitmux_num_cells_mock);
  585. ch = new_fake_channel();
  586. tt_assert(ch);
  587. ch->cmux = circuitmux_alloc();
  588. old_count = test_cells_written;
  589. test_target_cmux = ch->cmux;
  590. test_cmux_cells = 1;
  591. /* Enable cell acceptance */
  592. test_chan_accept_cells = 1;
  593. result = channel_flush_some_cells(ch, 1);
  594. tt_int_op(result, ==, 1);
  595. tt_int_op(test_cells_written, ==, old_count + 1);
  596. tt_int_op(test_cmux_cells, ==, 0);
  597. /* Now try it without accepting to force them into the queue */
  598. test_chan_accept_cells = 0;
  599. test_cmux_cells = 1;
  600. q_len_before = chan_cell_queue_len(&(ch->outgoing_queue));
  601. result = channel_flush_some_cells(ch, 1);
  602. /* We should not have actually flushed any */
  603. tt_int_op(result, ==, 0);
  604. tt_int_op(test_cells_written, ==, old_count + 1);
  605. /* But we should have gotten to the fake cellgen loop */
  606. tt_int_op(test_cmux_cells, ==, 0);
  607. /* ...and we should have a queued cell */
  608. q_len_after = chan_cell_queue_len(&(ch->outgoing_queue));
  609. tt_int_op(q_len_after, ==, q_len_before + 1);
  610. /* Now accept cells again and drain the queue */
  611. test_chan_accept_cells = 1;
  612. channel_flush_cells(ch);
  613. tt_int_op(test_cells_written, ==, old_count + 2);
  614. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  615. test_target_cmux = NULL;
  616. test_cmux_cells = 0;
  617. done:
  618. if (ch)
  619. circuitmux_free(ch->cmux);
  620. tor_free(ch);
  621. UNMOCK(channel_flush_from_first_active_circuit);
  622. UNMOCK(circuitmux_num_cells);
  623. test_chan_accept_cells = 0;
  624. #ifdef ENABLE_MEMPOOLS
  625. free_cell_pool();
  626. #endif /* ENABLE_MEMPOOLS */
  627. return;
  628. }
  629. static void
  630. test_channel_incoming(void *arg)
  631. {
  632. channel_t *ch = NULL;
  633. cell_t *cell = NULL;
  634. var_cell_t *var_cell = NULL;
  635. int old_count;
  636. (void)arg;
  637. /* Mock these for duration of the test */
  638. MOCK(scheduler_channel_doesnt_want_writes,
  639. scheduler_channel_doesnt_want_writes_mock);
  640. MOCK(scheduler_release_channel,
  641. scheduler_release_channel_mock);
  642. /* Accept cells to lower layer */
  643. test_chan_accept_cells = 1;
  644. /* Use default overhead factor */
  645. test_overhead_estimate = 1.0f;
  646. ch = new_fake_channel();
  647. tt_assert(ch);
  648. /* Start it off in OPENING */
  649. ch->state = CHANNEL_STATE_OPENING;
  650. /* We'll need a cmux */
  651. ch->cmux = circuitmux_alloc();
  652. /* Install incoming cell handlers */
  653. channel_set_cell_handlers(ch,
  654. chan_test_cell_handler,
  655. chan_test_var_cell_handler);
  656. /* Test cell handler getters */
  657. tt_ptr_op(channel_get_cell_handler(ch), ==, chan_test_cell_handler);
  658. tt_ptr_op(channel_get_var_cell_handler(ch), ==, chan_test_var_cell_handler);
  659. /* Try to register it */
  660. channel_register(ch);
  661. tt_assert(ch->registered);
  662. /* Open it */
  663. channel_change_state(ch, CHANNEL_STATE_OPEN);
  664. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  665. /* Receive a fixed cell */
  666. cell = tor_malloc_zero(sizeof(cell_t));
  667. make_fake_cell(cell);
  668. old_count = test_chan_fixed_cells_recved;
  669. channel_queue_cell(ch, cell);
  670. cell = NULL;
  671. tt_int_op(test_chan_fixed_cells_recved, ==, old_count + 1);
  672. /* Receive a variable-size cell */
  673. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  674. make_fake_var_cell(var_cell);
  675. old_count = test_chan_var_cells_recved;
  676. channel_queue_var_cell(ch, var_cell);
  677. var_cell = NULL;
  678. tt_int_op(test_chan_var_cells_recved, ==, old_count + 1);
  679. /* Close it */
  680. channel_mark_for_close(ch);
  681. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  682. chan_test_finish_close(ch);
  683. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  684. channel_run_cleanup();
  685. ch = NULL;
  686. done:
  687. free_fake_channel(ch);
  688. tor_free(cell);
  689. tor_free(var_cell);
  690. UNMOCK(scheduler_channel_doesnt_want_writes);
  691. UNMOCK(scheduler_release_channel);
  692. return;
  693. }
  694. /**
  695. * Normal channel lifecycle test:
  696. *
  697. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  698. */
  699. static void
  700. test_channel_lifecycle(void *arg)
  701. {
  702. channel_t *ch1 = NULL, *ch2 = NULL;
  703. cell_t *cell = NULL;
  704. int old_count, init_doesnt_want_writes_count;
  705. int init_releases_count;
  706. (void)arg;
  707. /* Mock these for the whole lifecycle test */
  708. MOCK(scheduler_channel_doesnt_want_writes,
  709. scheduler_channel_doesnt_want_writes_mock);
  710. MOCK(scheduler_release_channel,
  711. scheduler_release_channel_mock);
  712. /* Cache some initial counter values */
  713. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  714. init_releases_count = test_releases_count;
  715. /* Accept cells to lower layer */
  716. test_chan_accept_cells = 1;
  717. /* Use default overhead factor */
  718. test_overhead_estimate = 1.0f;
  719. ch1 = new_fake_channel();
  720. tt_assert(ch1);
  721. /* Start it off in OPENING */
  722. ch1->state = CHANNEL_STATE_OPENING;
  723. /* We'll need a cmux */
  724. ch1->cmux = circuitmux_alloc();
  725. /* Try to register it */
  726. channel_register(ch1);
  727. tt_assert(ch1->registered);
  728. /* Try to write a cell through (should queue) */
  729. cell = tor_malloc_zero(sizeof(cell_t));
  730. make_fake_cell(cell);
  731. old_count = test_cells_written;
  732. channel_write_cell(ch1, cell);
  733. tt_int_op(old_count, ==, test_cells_written);
  734. /* Move it to OPEN and flush */
  735. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  736. /* Queue should drain */
  737. tt_int_op(old_count + 1, ==, test_cells_written);
  738. /* Get another one */
  739. ch2 = new_fake_channel();
  740. tt_assert(ch2);
  741. ch2->state = CHANNEL_STATE_OPENING;
  742. ch2->cmux = circuitmux_alloc();
  743. /* Register */
  744. channel_register(ch2);
  745. tt_assert(ch2->registered);
  746. /* Check counters */
  747. tt_int_op(test_doesnt_want_writes_count, ==, init_doesnt_want_writes_count);
  748. tt_int_op(test_releases_count, ==, init_releases_count);
  749. /* Move ch1 to MAINT */
  750. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  751. tt_int_op(test_doesnt_want_writes_count, ==,
  752. init_doesnt_want_writes_count + 1);
  753. tt_int_op(test_releases_count, ==, init_releases_count);
  754. /* Move ch2 to OPEN */
  755. channel_change_state(ch2, CHANNEL_STATE_OPEN);
  756. tt_int_op(test_doesnt_want_writes_count, ==,
  757. init_doesnt_want_writes_count + 1);
  758. tt_int_op(test_releases_count, ==, init_releases_count);
  759. /* Move ch1 back to OPEN */
  760. channel_change_state(ch1, CHANNEL_STATE_OPEN);
  761. tt_int_op(test_doesnt_want_writes_count, ==,
  762. init_doesnt_want_writes_count + 1);
  763. tt_int_op(test_releases_count, ==, init_releases_count);
  764. /* Mark ch2 for close */
  765. channel_mark_for_close(ch2);
  766. tt_int_op(ch2->state, ==, CHANNEL_STATE_CLOSING);
  767. tt_int_op(test_doesnt_want_writes_count, ==,
  768. init_doesnt_want_writes_count + 1);
  769. tt_int_op(test_releases_count, ==, init_releases_count + 1);
  770. /* Shut down channels */
  771. channel_free_all();
  772. ch1 = ch2 = NULL;
  773. tt_int_op(test_doesnt_want_writes_count, ==,
  774. init_doesnt_want_writes_count + 1);
  775. /* channel_free() calls scheduler_release_channel() */
  776. tt_int_op(test_releases_count, ==, init_releases_count + 4);
  777. done:
  778. free_fake_channel(ch1);
  779. free_fake_channel(ch2);
  780. UNMOCK(scheduler_channel_doesnt_want_writes);
  781. UNMOCK(scheduler_release_channel);
  782. return;
  783. }
  784. /**
  785. * Weird channel lifecycle test:
  786. *
  787. * OPENING->CLOSING->CLOSED
  788. * OPENING->OPEN->CLOSING->ERROR
  789. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  790. * OPENING->OPEN->MAINT->CLOSING->ERROR
  791. */
  792. static void
  793. test_channel_lifecycle_2(void *arg)
  794. {
  795. channel_t *ch = NULL;
  796. (void)arg;
  797. /* Mock these for the whole lifecycle test */
  798. MOCK(scheduler_channel_doesnt_want_writes,
  799. scheduler_channel_doesnt_want_writes_mock);
  800. MOCK(scheduler_release_channel,
  801. scheduler_release_channel_mock);
  802. /* Accept cells to lower layer */
  803. test_chan_accept_cells = 1;
  804. /* Use default overhead factor */
  805. test_overhead_estimate = 1.0f;
  806. ch = new_fake_channel();
  807. tt_assert(ch);
  808. /* Start it off in OPENING */
  809. ch->state = CHANNEL_STATE_OPENING;
  810. /* The full lifecycle test needs a cmux */
  811. ch->cmux = circuitmux_alloc();
  812. /* Try to register it */
  813. channel_register(ch);
  814. tt_assert(ch->registered);
  815. /* Try to close it */
  816. channel_mark_for_close(ch);
  817. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  818. /* Finish closing it */
  819. chan_test_finish_close(ch);
  820. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  821. channel_run_cleanup();
  822. ch = NULL;
  823. /* Now try OPENING->OPEN->CLOSING->ERROR */
  824. ch = new_fake_channel();
  825. tt_assert(ch);
  826. ch->state = CHANNEL_STATE_OPENING;
  827. ch->cmux = circuitmux_alloc();
  828. channel_register(ch);
  829. tt_assert(ch->registered);
  830. /* Finish opening it */
  831. channel_change_state(ch, CHANNEL_STATE_OPEN);
  832. /* Error exit from lower layer */
  833. chan_test_error(ch);
  834. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  835. chan_test_finish_close(ch);
  836. tt_int_op(ch->state, ==, CHANNEL_STATE_ERROR);
  837. channel_run_cleanup();
  838. ch = NULL;
  839. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  840. ch = new_fake_channel();
  841. tt_assert(ch);
  842. ch->state = CHANNEL_STATE_OPENING;
  843. ch->cmux = circuitmux_alloc();
  844. channel_register(ch);
  845. tt_assert(ch->registered);
  846. /* Finish opening it */
  847. channel_change_state(ch, CHANNEL_STATE_OPEN);
  848. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  849. /* Go to maintenance state */
  850. channel_change_state(ch, CHANNEL_STATE_MAINT);
  851. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  852. /* Lower layer close */
  853. channel_mark_for_close(ch);
  854. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  855. /* Finish */
  856. chan_test_finish_close(ch);
  857. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  858. channel_run_cleanup();
  859. ch = NULL;
  860. /*
  861. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  862. * maintenance state
  863. */
  864. ch = new_fake_channel();
  865. tt_assert(ch);
  866. ch->state = CHANNEL_STATE_OPENING;
  867. ch->cmux = circuitmux_alloc();
  868. channel_register(ch);
  869. tt_assert(ch->registered);
  870. /* Finish opening it */
  871. channel_change_state(ch, CHANNEL_STATE_OPEN);
  872. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  873. /* Go to maintenance state */
  874. channel_change_state(ch, CHANNEL_STATE_MAINT);
  875. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  876. /* Lower layer close */
  877. channel_close_from_lower_layer(ch);
  878. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  879. /* Finish */
  880. chan_test_finish_close(ch);
  881. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSED);
  882. channel_run_cleanup();
  883. ch = NULL;
  884. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  885. ch = new_fake_channel();
  886. tt_assert(ch);
  887. ch->state = CHANNEL_STATE_OPENING;
  888. ch->cmux = circuitmux_alloc();
  889. channel_register(ch);
  890. tt_assert(ch->registered);
  891. /* Finish opening it */
  892. channel_change_state(ch, CHANNEL_STATE_OPEN);
  893. tt_int_op(ch->state, ==, CHANNEL_STATE_OPEN);
  894. /* Go to maintenance state */
  895. channel_change_state(ch, CHANNEL_STATE_MAINT);
  896. tt_int_op(ch->state, ==, CHANNEL_STATE_MAINT);
  897. /* Lower layer close */
  898. chan_test_error(ch);
  899. tt_int_op(ch->state, ==, CHANNEL_STATE_CLOSING);
  900. /* Finish */
  901. chan_test_finish_close(ch);
  902. tt_int_op(ch->state, ==, CHANNEL_STATE_ERROR);
  903. channel_run_cleanup();
  904. ch = NULL;
  905. /* Shut down channels */
  906. channel_free_all();
  907. done:
  908. tor_free(ch);
  909. UNMOCK(scheduler_channel_doesnt_want_writes);
  910. UNMOCK(scheduler_release_channel);
  911. return;
  912. }
  913. static void
  914. test_channel_multi(void *arg)
  915. {
  916. channel_t *ch1 = NULL, *ch2 = NULL;
  917. uint64_t global_queue_estimate;
  918. cell_t *cell = NULL;
  919. (void)arg;
  920. /* Accept cells to lower layer */
  921. test_chan_accept_cells = 1;
  922. /* Use default overhead factor */
  923. test_overhead_estimate = 1.0f;
  924. ch1 = new_fake_channel();
  925. tt_assert(ch1);
  926. ch2 = new_fake_channel();
  927. tt_assert(ch2);
  928. /* Initial queue size update */
  929. channel_update_xmit_queue_size(ch1);
  930. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  931. channel_update_xmit_queue_size(ch2);
  932. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  933. global_queue_estimate = channel_get_global_queue_estimate();
  934. tt_u64_op(global_queue_estimate, ==, 0);
  935. /* Queue some cells, check queue estimates */
  936. cell = tor_malloc_zero(sizeof(cell_t));
  937. make_fake_cell(cell);
  938. channel_write_cell(ch1, cell);
  939. cell = tor_malloc_zero(sizeof(cell_t));
  940. make_fake_cell(cell);
  941. channel_write_cell(ch2, cell);
  942. channel_update_xmit_queue_size(ch1);
  943. channel_update_xmit_queue_size(ch2);
  944. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  945. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  946. global_queue_estimate = channel_get_global_queue_estimate();
  947. tt_u64_op(global_queue_estimate, ==, 0);
  948. /* Stop accepting cells at lower layer */
  949. test_chan_accept_cells = 0;
  950. /* Queue some cells and check queue estimates */
  951. cell = tor_malloc_zero(sizeof(cell_t));
  952. make_fake_cell(cell);
  953. channel_write_cell(ch1, cell);
  954. channel_update_xmit_queue_size(ch1);
  955. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  956. global_queue_estimate = channel_get_global_queue_estimate();
  957. tt_u64_op(global_queue_estimate, ==, 512);
  958. cell = tor_malloc_zero(sizeof(cell_t));
  959. make_fake_cell(cell);
  960. channel_write_cell(ch2, cell);
  961. channel_update_xmit_queue_size(ch2);
  962. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 512);
  963. global_queue_estimate = channel_get_global_queue_estimate();
  964. tt_u64_op(global_queue_estimate, ==, 1024);
  965. /* Allow cells through again */
  966. test_chan_accept_cells = 1;
  967. /* Flush chan 2 */
  968. channel_flush_cells(ch2);
  969. /* Update and check queue sizes */
  970. channel_update_xmit_queue_size(ch1);
  971. channel_update_xmit_queue_size(ch2);
  972. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  973. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  974. global_queue_estimate = channel_get_global_queue_estimate();
  975. tt_u64_op(global_queue_estimate, ==, 512);
  976. /* Flush chan 1 */
  977. channel_flush_cells(ch1);
  978. /* Update and check queue sizes */
  979. channel_update_xmit_queue_size(ch1);
  980. channel_update_xmit_queue_size(ch2);
  981. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 0);
  982. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 0);
  983. global_queue_estimate = channel_get_global_queue_estimate();
  984. tt_u64_op(global_queue_estimate, ==, 0);
  985. /* Now block again */
  986. test_chan_accept_cells = 0;
  987. /* Queue some cells */
  988. cell = tor_malloc_zero(sizeof(cell_t));
  989. make_fake_cell(cell);
  990. channel_write_cell(ch1, cell);
  991. cell = tor_malloc_zero(sizeof(cell_t));
  992. make_fake_cell(cell);
  993. channel_write_cell(ch2, cell);
  994. cell = NULL;
  995. /* Check the estimates */
  996. channel_update_xmit_queue_size(ch1);
  997. channel_update_xmit_queue_size(ch2);
  998. tt_u64_op(ch1->bytes_queued_for_xmit, ==, 512);
  999. tt_u64_op(ch2->bytes_queued_for_xmit, ==, 512);
  1000. global_queue_estimate = channel_get_global_queue_estimate();
  1001. tt_u64_op(global_queue_estimate, ==, 1024);
  1002. /* Now close channel 2; it should be subtracted from the global queue */
  1003. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1004. channel_mark_for_close(ch2);
  1005. UNMOCK(scheduler_release_channel);
  1006. global_queue_estimate = channel_get_global_queue_estimate();
  1007. tt_u64_op(global_queue_estimate, ==, 512);
  1008. /*
  1009. * Since the fake channels aren't registered, channel_free_all() can't
  1010. * see them properly.
  1011. */
  1012. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1013. channel_mark_for_close(ch1);
  1014. UNMOCK(scheduler_release_channel);
  1015. global_queue_estimate = channel_get_global_queue_estimate();
  1016. tt_u64_op(global_queue_estimate, ==, 0);
  1017. /* Now free everything */
  1018. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1019. channel_free_all();
  1020. UNMOCK(scheduler_release_channel);
  1021. done:
  1022. free_fake_channel(ch1);
  1023. free_fake_channel(ch2);
  1024. return;
  1025. }
  1026. /**
  1027. * Check some hopefully-impossible edge cases in the channel queue we
  1028. * can only trigger by doing evil things to the queue directly.
  1029. */
  1030. static void
  1031. test_channel_queue_impossible(void *arg)
  1032. {
  1033. channel_t *ch = NULL;
  1034. cell_t *cell = NULL;
  1035. packed_cell_t *packed_cell = NULL;
  1036. var_cell_t *var_cell = NULL;
  1037. int old_count;
  1038. cell_queue_entry_t *q = NULL;
  1039. uint64_t global_queue_estimate;
  1040. uintptr_t cellintptr;
  1041. /* Cache the global queue size (see below) */
  1042. global_queue_estimate = channel_get_global_queue_estimate();
  1043. (void)arg;
  1044. #ifdef ENABLE_MEMPOOLS
  1045. init_cell_pool();
  1046. #endif /* ENABLE_MEMPOOLS */
  1047. ch = new_fake_channel();
  1048. tt_assert(ch);
  1049. /* We test queueing here; tell it not to accept cells */
  1050. test_chan_accept_cells = 0;
  1051. /* ...and keep it from trying to flush the queue */
  1052. ch->state = CHANNEL_STATE_MAINT;
  1053. /* Cache the cell written count */
  1054. old_count = test_cells_written;
  1055. /* Assert that the queue is initially empty */
  1056. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1057. /* Get a fresh cell and write it to the channel*/
  1058. cell = tor_malloc_zero(sizeof(cell_t));
  1059. make_fake_cell(cell);
  1060. cellintptr = (uintptr_t)(void*)cell;
  1061. channel_write_cell(ch, cell);
  1062. /* Now it should be queued */
  1063. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1064. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1065. tt_assert(q);
  1066. if (q) {
  1067. tt_int_op(q->type, ==, CELL_QUEUE_FIXED);
  1068. tt_assert((uintptr_t)q->u.fixed.cell == cellintptr);
  1069. }
  1070. /* Do perverse things to it */
  1071. tor_free(q->u.fixed.cell);
  1072. q->u.fixed.cell = NULL;
  1073. /*
  1074. * Now change back to open with channel_change_state() and assert that it
  1075. * gets thrown away properly.
  1076. */
  1077. test_chan_accept_cells = 1;
  1078. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1079. tt_assert(test_cells_written == old_count);
  1080. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1081. /* Same thing but for a var_cell */
  1082. test_chan_accept_cells = 0;
  1083. ch->state = CHANNEL_STATE_MAINT;
  1084. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1085. make_fake_var_cell(var_cell);
  1086. cellintptr = (uintptr_t)(void*)var_cell;
  1087. channel_write_var_cell(ch, var_cell);
  1088. /* Check that it's queued */
  1089. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1090. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1091. tt_assert(q);
  1092. if (q) {
  1093. tt_int_op(q->type, ==, CELL_QUEUE_VAR);
  1094. tt_assert((uintptr_t)q->u.var.var_cell == cellintptr);
  1095. }
  1096. /* Remove the cell from the queue entry */
  1097. tor_free(q->u.var.var_cell);
  1098. q->u.var.var_cell = NULL;
  1099. /* Let it drain and check that the bad entry is discarded */
  1100. test_chan_accept_cells = 1;
  1101. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1102. tt_assert(test_cells_written == old_count);
  1103. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1104. /* Same thing with a packed_cell */
  1105. test_chan_accept_cells = 0;
  1106. ch->state = CHANNEL_STATE_MAINT;
  1107. packed_cell = packed_cell_new();
  1108. tt_assert(packed_cell);
  1109. cellintptr = (uintptr_t)(void*)packed_cell;
  1110. channel_write_packed_cell(ch, packed_cell);
  1111. /* Check that it's queued */
  1112. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1113. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1114. tt_assert(q);
  1115. if (q) {
  1116. tt_int_op(q->type, ==, CELL_QUEUE_PACKED);
  1117. tt_assert((uintptr_t)q->u.packed.packed_cell == cellintptr);
  1118. }
  1119. /* Remove the cell from the queue entry */
  1120. packed_cell_free(q->u.packed.packed_cell);
  1121. q->u.packed.packed_cell = NULL;
  1122. /* Let it drain and check that the bad entry is discarded */
  1123. test_chan_accept_cells = 1;
  1124. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1125. tt_assert(test_cells_written == old_count);
  1126. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1127. /* Unknown cell type case */
  1128. test_chan_accept_cells = 0;
  1129. ch->state = CHANNEL_STATE_MAINT;
  1130. cell = tor_malloc_zero(sizeof(cell_t));
  1131. make_fake_cell(cell);
  1132. cellintptr = (uintptr_t)(void*)cell;
  1133. channel_write_cell(ch, cell);
  1134. /* Check that it's queued */
  1135. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 1);
  1136. q = TOR_SIMPLEQ_FIRST(&(ch->outgoing_queue));
  1137. tt_assert(q);
  1138. if (q) {
  1139. tt_int_op(q->type, ==, CELL_QUEUE_FIXED);
  1140. tt_assert((uintptr_t)q->u.fixed.cell == cellintptr);
  1141. }
  1142. /* Clobber it, including the queue entry type */
  1143. tor_free(q->u.fixed.cell);
  1144. q->u.fixed.cell = NULL;
  1145. q->type = CELL_QUEUE_PACKED + 1;
  1146. /* Let it drain and check that the bad entry is discarded */
  1147. test_chan_accept_cells = 1;
  1148. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1149. tt_assert(test_cells_written == old_count);
  1150. tt_int_op(chan_cell_queue_len(&(ch->outgoing_queue)), ==, 0);
  1151. done:
  1152. free_fake_channel(ch);
  1153. #ifdef ENABLE_MEMPOOLS
  1154. free_cell_pool();
  1155. #endif /* ENABLE_MEMPOOLS */
  1156. /*
  1157. * Doing that meant that we couldn't correctly adjust the queue size
  1158. * for the var cell, so manually reset the global queue size estimate
  1159. * so the next test doesn't break if we run with --no-fork.
  1160. */
  1161. estimated_total_queue_size = global_queue_estimate;
  1162. return;
  1163. }
  1164. static void
  1165. test_channel_queue_size(void *arg)
  1166. {
  1167. channel_t *ch = NULL;
  1168. cell_t *cell = NULL;
  1169. int n, old_count;
  1170. uint64_t global_queue_estimate;
  1171. (void)arg;
  1172. ch = new_fake_channel();
  1173. tt_assert(ch);
  1174. /* Initial queue size update */
  1175. channel_update_xmit_queue_size(ch);
  1176. tt_u64_op(ch->bytes_queued_for_xmit, ==, 0);
  1177. global_queue_estimate = channel_get_global_queue_estimate();
  1178. tt_u64_op(global_queue_estimate, ==, 0);
  1179. /* Test the call-through to our fake lower layer */
  1180. n = channel_num_cells_writeable(ch);
  1181. /* chan_test_num_cells_writeable() always returns 32 */
  1182. tt_int_op(n, ==, 32);
  1183. /*
  1184. * Now we queue some cells and check that channel_num_cells_writeable()
  1185. * adjusts properly
  1186. */
  1187. /* tell it not to accept cells */
  1188. test_chan_accept_cells = 0;
  1189. /* ...and keep it from trying to flush the queue */
  1190. ch->state = CHANNEL_STATE_MAINT;
  1191. /* Get a fresh cell */
  1192. cell = tor_malloc_zero(sizeof(cell_t));
  1193. make_fake_cell(cell);
  1194. old_count = test_cells_written;
  1195. channel_write_cell(ch, cell);
  1196. /* Assert that it got queued, not written through, correctly */
  1197. tt_int_op(test_cells_written, ==, old_count);
  1198. /* Now check chan_test_num_cells_writeable() again */
  1199. n = channel_num_cells_writeable(ch);
  1200. tt_int_op(n, ==, 0); /* Should return 0 since we're in CHANNEL_STATE_MAINT */
  1201. /* Update queue size estimates */
  1202. channel_update_xmit_queue_size(ch);
  1203. /* One cell, times an overhead factor of 1.0 */
  1204. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1205. /* Try a different overhead factor */
  1206. test_overhead_estimate = 0.5f;
  1207. /* This one should be ignored since it's below 1.0 */
  1208. channel_update_xmit_queue_size(ch);
  1209. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1210. /* Now try a larger one */
  1211. test_overhead_estimate = 2.0f;
  1212. channel_update_xmit_queue_size(ch);
  1213. tt_u64_op(ch->bytes_queued_for_xmit, ==, 1024);
  1214. /* Go back to 1.0 */
  1215. test_overhead_estimate = 1.0f;
  1216. channel_update_xmit_queue_size(ch);
  1217. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1218. /* Check the global estimate too */
  1219. global_queue_estimate = channel_get_global_queue_estimate();
  1220. tt_u64_op(global_queue_estimate, ==, 512);
  1221. /* Go to open */
  1222. old_count = test_cells_written;
  1223. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1224. /*
  1225. * It should try to write, but we aren't accepting cells right now, so
  1226. * it'll requeue
  1227. */
  1228. tt_int_op(test_cells_written, ==, old_count);
  1229. /* Check the queue size again */
  1230. channel_update_xmit_queue_size(ch);
  1231. tt_u64_op(ch->bytes_queued_for_xmit, ==, 512);
  1232. global_queue_estimate = channel_get_global_queue_estimate();
  1233. tt_u64_op(global_queue_estimate, ==, 512);
  1234. /*
  1235. * Now the cell is in the queue, and we're open, so we should get 31
  1236. * writeable cells.
  1237. */
  1238. n = channel_num_cells_writeable(ch);
  1239. tt_int_op(n, ==, 31);
  1240. /* Accept cells again */
  1241. test_chan_accept_cells = 1;
  1242. /* ...and re-process the queue */
  1243. old_count = test_cells_written;
  1244. channel_flush_cells(ch);
  1245. tt_int_op(test_cells_written, ==, old_count + 1);
  1246. /* Should have 32 writeable now */
  1247. n = channel_num_cells_writeable(ch);
  1248. tt_int_op(n, ==, 32);
  1249. /* Should have queue size estimate of zero */
  1250. channel_update_xmit_queue_size(ch);
  1251. tt_u64_op(ch->bytes_queued_for_xmit, ==, 0);
  1252. global_queue_estimate = channel_get_global_queue_estimate();
  1253. tt_u64_op(global_queue_estimate, ==, 0);
  1254. /* Okay, now we're done with this one */
  1255. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1256. channel_mark_for_close(ch);
  1257. UNMOCK(scheduler_release_channel);
  1258. done:
  1259. free_fake_channel(ch);
  1260. return;
  1261. }
  1262. static void
  1263. test_channel_write(void *arg)
  1264. {
  1265. channel_t *ch = NULL;
  1266. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  1267. packed_cell_t *packed_cell = NULL;
  1268. var_cell_t *var_cell =
  1269. tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1270. int old_count;
  1271. (void)arg;
  1272. #ifdef ENABLE_MEMPOOLS
  1273. init_cell_pool();
  1274. #endif /* ENABLE_MEMPOOLS */
  1275. packed_cell = packed_cell_new();
  1276. tt_assert(packed_cell);
  1277. ch = new_fake_channel();
  1278. tt_assert(ch);
  1279. make_fake_cell(cell);
  1280. make_fake_var_cell(var_cell);
  1281. /* Tell it to accept cells */
  1282. test_chan_accept_cells = 1;
  1283. old_count = test_cells_written;
  1284. channel_write_cell(ch, cell);
  1285. cell = NULL;
  1286. tt_assert(test_cells_written == old_count + 1);
  1287. channel_write_var_cell(ch, var_cell);
  1288. var_cell = NULL;
  1289. tt_assert(test_cells_written == old_count + 2);
  1290. channel_write_packed_cell(ch, packed_cell);
  1291. packed_cell = NULL;
  1292. tt_assert(test_cells_written == old_count + 3);
  1293. /* Now we test queueing; tell it not to accept cells */
  1294. test_chan_accept_cells = 0;
  1295. /* ...and keep it from trying to flush the queue */
  1296. ch->state = CHANNEL_STATE_MAINT;
  1297. /* Get a fresh cell */
  1298. cell = tor_malloc_zero(sizeof(cell_t));
  1299. make_fake_cell(cell);
  1300. old_count = test_cells_written;
  1301. channel_write_cell(ch, cell);
  1302. tt_assert(test_cells_written == old_count);
  1303. /*
  1304. * Now change back to open with channel_change_state() and assert that it
  1305. * gets drained from the queue.
  1306. */
  1307. test_chan_accept_cells = 1;
  1308. channel_change_state(ch, CHANNEL_STATE_OPEN);
  1309. tt_assert(test_cells_written == old_count + 1);
  1310. /*
  1311. * Check the note destroy case
  1312. */
  1313. cell = tor_malloc_zero(sizeof(cell_t));
  1314. make_fake_cell(cell);
  1315. cell->command = CELL_DESTROY;
  1316. /* Set up the mock */
  1317. MOCK(channel_note_destroy_not_pending,
  1318. channel_note_destroy_not_pending_mock);
  1319. old_count = test_destroy_not_pending_calls;
  1320. channel_write_cell(ch, cell);
  1321. tt_assert(test_destroy_not_pending_calls == old_count + 1);
  1322. /* Now send a non-destroy and check we don't call it */
  1323. cell = tor_malloc_zero(sizeof(cell_t));
  1324. make_fake_cell(cell);
  1325. channel_write_cell(ch, cell);
  1326. tt_assert(test_destroy_not_pending_calls == old_count + 1);
  1327. UNMOCK(channel_note_destroy_not_pending);
  1328. /*
  1329. * Now switch it to CLOSING so we can test the discard-cells case
  1330. * in the channel_write_*() functions.
  1331. */
  1332. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  1333. channel_mark_for_close(ch);
  1334. UNMOCK(scheduler_release_channel);
  1335. /* Send cells that will drop in the closing state */
  1336. old_count = test_cells_written;
  1337. cell = tor_malloc_zero(sizeof(cell_t));
  1338. make_fake_cell(cell);
  1339. channel_write_cell(ch, cell);
  1340. cell = NULL;
  1341. tt_assert(test_cells_written == old_count);
  1342. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  1343. make_fake_var_cell(var_cell);
  1344. channel_write_var_cell(ch, var_cell);
  1345. var_cell = NULL;
  1346. tt_assert(test_cells_written == old_count);
  1347. packed_cell = packed_cell_new();
  1348. channel_write_packed_cell(ch, packed_cell);
  1349. packed_cell = NULL;
  1350. tt_assert(test_cells_written == old_count);
  1351. #ifdef ENABLE_MEMPOOLS
  1352. free_cell_pool();
  1353. #endif /* ENABLE_MEMPOOLS */
  1354. done:
  1355. free_fake_channel(ch);
  1356. tor_free(var_cell);
  1357. tor_free(cell);
  1358. packed_cell_free(packed_cell);
  1359. return;
  1360. }
  1361. struct testcase_t channel_tests[] = {
  1362. { "dumpstats", test_channel_dumpstats, TT_FORK, NULL, NULL },
  1363. { "flush", test_channel_flush, TT_FORK, NULL, NULL },
  1364. { "flushmux", test_channel_flushmux, TT_FORK, NULL, NULL },
  1365. { "incoming", test_channel_incoming, TT_FORK, NULL, NULL },
  1366. { "lifecycle", test_channel_lifecycle, TT_FORK, NULL, NULL },
  1367. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK, NULL, NULL },
  1368. { "multi", test_channel_multi, TT_FORK, NULL, NULL },
  1369. { "queue_impossible", test_channel_queue_impossible, TT_FORK, NULL, NULL },
  1370. { "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
  1371. { "write", test_channel_write, TT_FORK, NULL, NULL },
  1372. END_OF_TESTCASES
  1373. };