test_channel.c 50 KB

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