test_channel.c 46 KB

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