test_channel.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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. #define CIRCUITLIST_PRIVATE
  9. #include "circuitlist.h"
  10. #include "circuitmux.h"
  11. #include "circuitmux_ewma.h"
  12. /* For var_cell_free */
  13. #include "connection_or.h"
  14. #include "crypto_rand.h"
  15. /* For packed_cell stuff */
  16. #define RELAY_PRIVATE
  17. #include "relay.h"
  18. /* For init/free stuff */
  19. #include "scheduler.h"
  20. #include "networkstatus.h"
  21. #include "networkstatus_st.h"
  22. #include "origin_circuit_st.h"
  23. #include "routerstatus_st.h"
  24. /* Test suite stuff */
  25. #include "log_test_helpers.h"
  26. #include "test.h"
  27. #include "fakechans.h"
  28. static int test_chan_accept_cells = 0;
  29. static int test_chan_fixed_cells_recved = 0;
  30. static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
  31. static int test_chan_var_cells_recved = 0;
  32. static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
  33. static int test_cells_written = 0;
  34. static int test_doesnt_want_writes_count = 0;
  35. static int test_dumpstats_calls = 0;
  36. static int test_has_waiting_cells_count = 0;
  37. static int test_releases_count = 0;
  38. static channel_t *dump_statistics_mock_target = NULL;
  39. static int dump_statistics_mock_matches = 0;
  40. static int test_close_called = 0;
  41. static int test_chan_should_be_canonical = 0;
  42. static int test_chan_should_match_target = 0;
  43. static int test_chan_canonical_should_be_reliable = 0;
  44. static int test_chan_listener_close_fn_called = 0;
  45. static int test_chan_listener_fn_called = 0;
  46. static const char *
  47. chan_test_describe_transport(channel_t *ch)
  48. {
  49. tt_ptr_op(ch, OP_NE, NULL);
  50. done:
  51. return "Fake channel for unit tests";
  52. }
  53. /**
  54. * Mock for channel_dump_statistics(); if the channel matches the
  55. * target, bump a counter - otherwise ignore.
  56. */
  57. static void
  58. chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
  59. {
  60. tt_ptr_op(chan, OP_NE, NULL);
  61. (void)severity;
  62. if (chan != NULL && chan == dump_statistics_mock_target) {
  63. ++dump_statistics_mock_matches;
  64. }
  65. done:
  66. return;
  67. }
  68. /*
  69. * Handle an incoming fixed-size cell for unit tests
  70. */
  71. static void
  72. chan_test_cell_handler(channel_t *chan, cell_t *cell)
  73. {
  74. tt_assert(chan);
  75. tt_assert(cell);
  76. test_chan_last_seen_fixed_cell_ptr = cell;
  77. ++test_chan_fixed_cells_recved;
  78. done:
  79. return;
  80. }
  81. /*
  82. * Fake transport-specific stats call
  83. */
  84. static void
  85. chan_test_dumpstats(channel_t *ch, int severity)
  86. {
  87. tt_ptr_op(ch, OP_NE, NULL);
  88. (void)severity;
  89. ++test_dumpstats_calls;
  90. done:
  91. return;
  92. }
  93. /*
  94. * Handle an incoming variable-size cell for unit tests
  95. */
  96. static void
  97. chan_test_var_cell_handler(channel_t *ch,
  98. var_cell_t *var_cell)
  99. {
  100. tt_assert(ch);
  101. tt_assert(var_cell);
  102. test_chan_last_seen_var_cell_ptr = var_cell;
  103. ++test_chan_var_cells_recved;
  104. done:
  105. return;
  106. }
  107. static void
  108. chan_test_close(channel_t *ch)
  109. {
  110. tt_assert(ch);
  111. ++test_close_called;
  112. done:
  113. return;
  114. }
  115. /*
  116. * Close a channel through the error path
  117. */
  118. static void
  119. chan_test_error(channel_t *ch)
  120. {
  121. tt_assert(ch);
  122. tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
  123. ch->state == CHANNEL_STATE_ERROR ||
  124. ch->state == CHANNEL_STATE_CLOSED));
  125. channel_close_for_error(ch);
  126. done:
  127. return;
  128. }
  129. /*
  130. * Finish closing a channel from CHANNEL_STATE_CLOSING
  131. */
  132. static void
  133. chan_test_finish_close(channel_t *ch)
  134. {
  135. tt_assert(ch);
  136. tt_assert(ch->state == CHANNEL_STATE_CLOSING);
  137. channel_closed(ch);
  138. done:
  139. return;
  140. }
  141. static const char *
  142. chan_test_get_remote_descr(channel_t *ch, int flags)
  143. {
  144. tt_assert(ch);
  145. tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), OP_EQ, 0);
  146. done:
  147. return "Fake channel for unit tests; no real endpoint";
  148. }
  149. static int
  150. chan_test_num_cells_writeable(channel_t *ch)
  151. {
  152. tt_assert(ch);
  153. done:
  154. return 32;
  155. }
  156. static int
  157. chan_test_write_packed_cell(channel_t *ch,
  158. packed_cell_t *packed_cell)
  159. {
  160. int rv = 0;
  161. tt_assert(ch);
  162. tt_assert(packed_cell);
  163. if (test_chan_accept_cells) {
  164. /* Free the cell and bump the counter */
  165. ++test_cells_written;
  166. rv = 1;
  167. }
  168. /* else return 0, we didn't accept it */
  169. done:
  170. return rv;
  171. }
  172. static int
  173. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  174. {
  175. int rv = 0;
  176. tt_assert(ch);
  177. tt_assert(var_cell);
  178. if (test_chan_accept_cells) {
  179. /* Free the cell and bump the counter */
  180. var_cell_free(var_cell);
  181. ++test_cells_written;
  182. rv = 1;
  183. }
  184. /* else return 0, we didn't accept it */
  185. done:
  186. return rv;
  187. }
  188. /**
  189. * Fill out c with a new fake cell for test suite use
  190. */
  191. void
  192. make_fake_cell(cell_t *c)
  193. {
  194. tt_ptr_op(c, OP_NE, NULL);
  195. c->circ_id = 1;
  196. c->command = CELL_RELAY;
  197. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  198. done:
  199. return;
  200. }
  201. /**
  202. * Fill out c with a new fake var_cell for test suite use
  203. */
  204. void
  205. make_fake_var_cell(var_cell_t *c)
  206. {
  207. tt_ptr_op(c, OP_NE, NULL);
  208. c->circ_id = 1;
  209. c->command = CELL_VERSIONS;
  210. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  211. memset(c->payload, 0, c->payload_len);
  212. done:
  213. return;
  214. }
  215. /**
  216. * Set up a new fake channel for the test suite
  217. */
  218. channel_t *
  219. new_fake_channel(void)
  220. {
  221. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  222. channel_init(chan);
  223. chan->close = chan_test_close;
  224. chan->num_cells_writeable = chan_test_num_cells_writeable;
  225. chan->get_remote_descr = chan_test_get_remote_descr;
  226. chan->write_packed_cell = chan_test_write_packed_cell;
  227. chan->write_var_cell = chan_test_write_var_cell;
  228. chan->state = CHANNEL_STATE_OPEN;
  229. chan->cmux = circuitmux_alloc();
  230. circuitmux_set_policy(chan->cmux, &ewma_policy);
  231. return chan;
  232. }
  233. void
  234. free_fake_channel(channel_t *chan)
  235. {
  236. if (! chan)
  237. return;
  238. if (chan->cmux)
  239. circuitmux_free(chan->cmux);
  240. tor_free(chan);
  241. }
  242. /**
  243. * Counter query for scheduler_channel_has_waiting_cells_mock()
  244. */
  245. int
  246. get_mock_scheduler_has_waiting_cells_count(void)
  247. {
  248. return test_has_waiting_cells_count;
  249. }
  250. /**
  251. * Mock for scheduler_channel_has_waiting_cells()
  252. */
  253. void
  254. scheduler_channel_has_waiting_cells_mock(channel_t *ch)
  255. {
  256. (void)ch;
  257. /* Increment counter */
  258. ++test_has_waiting_cells_count;
  259. return;
  260. }
  261. static void
  262. scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
  263. {
  264. (void)ch;
  265. /* Increment counter */
  266. ++test_doesnt_want_writes_count;
  267. return;
  268. }
  269. /**
  270. * Mock for scheduler_release_channel()
  271. */
  272. void
  273. scheduler_release_channel_mock(channel_t *ch)
  274. {
  275. (void)ch;
  276. /* Increment counter */
  277. ++test_releases_count;
  278. return;
  279. }
  280. static int
  281. test_chan_is_canonical(channel_t *chan, int req)
  282. {
  283. tor_assert(chan);
  284. if (req && test_chan_canonical_should_be_reliable) {
  285. return 1;
  286. }
  287. if (test_chan_should_be_canonical) {
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. static int
  293. test_chan_matches_target(channel_t *chan, const tor_addr_t *target)
  294. {
  295. (void) chan;
  296. (void) target;
  297. if (test_chan_should_match_target) {
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. static void
  303. test_chan_listener_close(channel_listener_t *chan)
  304. {
  305. (void) chan;
  306. ++test_chan_listener_close_fn_called;
  307. return;
  308. }
  309. static void
  310. test_chan_listener_fn(channel_listener_t *listener, channel_t *chan)
  311. {
  312. (void) listener;
  313. (void) chan;
  314. ++test_chan_listener_fn_called;
  315. return;
  316. }
  317. static const char *
  318. test_chan_listener_describe_transport(channel_listener_t *chan)
  319. {
  320. (void) chan;
  321. return "Fake listener channel.";
  322. }
  323. /**
  324. * Test for channel_dumpstats() and limited test for
  325. * channel_dump_statistics()
  326. */
  327. static void
  328. test_channel_dumpstats(void *arg)
  329. {
  330. channel_t *ch = NULL;
  331. cell_t *cell = NULL;
  332. packed_cell_t *p_cell = NULL;
  333. int old_count;
  334. (void)arg;
  335. /* Mock these for duration of the test */
  336. MOCK(scheduler_channel_doesnt_want_writes,
  337. scheduler_channel_doesnt_want_writes_mock);
  338. MOCK(scheduler_release_channel,
  339. scheduler_release_channel_mock);
  340. /* Set up a new fake channel */
  341. ch = new_fake_channel();
  342. tt_assert(ch);
  343. /* Try to register it */
  344. channel_register(ch);
  345. tt_assert(ch->registered);
  346. /* Set up mock */
  347. dump_statistics_mock_target = ch;
  348. dump_statistics_mock_matches = 0;
  349. MOCK(channel_dump_statistics,
  350. chan_test_channel_dump_statistics_mock);
  351. /* Call channel_dumpstats() */
  352. channel_dumpstats(LOG_DEBUG);
  353. /* Assert that we hit the mock */
  354. tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
  355. /* Close the channel */
  356. channel_mark_for_close(ch);
  357. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  358. chan_test_finish_close(ch);
  359. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  360. /* Try again and hit the finished channel */
  361. channel_dumpstats(LOG_DEBUG);
  362. tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
  363. channel_run_cleanup();
  364. ch = NULL;
  365. /* Now we should hit nothing */
  366. channel_dumpstats(LOG_DEBUG);
  367. tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
  368. /* Unmock */
  369. UNMOCK(channel_dump_statistics);
  370. dump_statistics_mock_target = NULL;
  371. dump_statistics_mock_matches = 0;
  372. /* Now make another channel */
  373. ch = new_fake_channel();
  374. tt_assert(ch);
  375. channel_register(ch);
  376. tt_int_op(ch->registered, OP_EQ, 1);
  377. /* Lie about its age so dumpstats gets coverage for rate calculations */
  378. ch->timestamp_created = time(NULL) - 30;
  379. tt_int_op(ch->timestamp_created, OP_GT, 0);
  380. tt_int_op(time(NULL), OP_GT, ch->timestamp_created);
  381. /* Put cells through it both ways to make the counters non-zero */
  382. p_cell = packed_cell_new();
  383. test_chan_accept_cells = 1;
  384. old_count = test_cells_written;
  385. channel_write_packed_cell(ch, p_cell);
  386. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  387. tt_u64_op(ch->n_bytes_xmitted, OP_GT, 0);
  388. tt_u64_op(ch->n_cells_xmitted, OP_GT, 0);
  389. /* Receive path */
  390. channel_set_cell_handlers(ch,
  391. chan_test_cell_handler,
  392. chan_test_var_cell_handler);
  393. tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
  394. tt_ptr_op(channel_get_var_cell_handler(ch), OP_EQ,
  395. chan_test_var_cell_handler);
  396. cell = tor_malloc_zero(sizeof(*cell));
  397. old_count = test_chan_fixed_cells_recved;
  398. channel_process_cell(ch, cell);
  399. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  400. tt_u64_op(ch->n_bytes_recved, OP_GT, 0);
  401. tt_u64_op(ch->n_cells_recved, OP_GT, 0);
  402. /* Test channel_dump_statistics */
  403. ch->describe_transport = chan_test_describe_transport;
  404. ch->dumpstats = chan_test_dumpstats;
  405. test_chan_should_be_canonical = 1;
  406. ch->is_canonical = test_chan_is_canonical;
  407. old_count = test_dumpstats_calls;
  408. channel_dump_statistics(ch, LOG_DEBUG);
  409. tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
  410. /* Close the channel */
  411. channel_mark_for_close(ch);
  412. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  413. chan_test_finish_close(ch);
  414. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  415. channel_run_cleanup();
  416. ch = NULL;
  417. done:
  418. free_fake_channel(ch);
  419. tor_free(cell);
  420. UNMOCK(scheduler_channel_doesnt_want_writes);
  421. UNMOCK(scheduler_release_channel);
  422. return;
  423. }
  424. /* Test outbound cell. The callstack is:
  425. * channel_flush_some_cells()
  426. * -> channel_flush_from_first_active_circuit()
  427. * -> channel_write_packed_cell()
  428. * -> write_packed_cell()
  429. * -> chan->write_packed_cell() fct ptr.
  430. *
  431. * This test goes from a cell in a circuit up to the channel write handler
  432. * that should put them on the connection outbuf. */
  433. static void
  434. test_channel_outbound_cell(void *arg)
  435. {
  436. int old_count;
  437. channel_t *chan = NULL;
  438. packed_cell_t *p_cell = NULL, *p_cell2 = NULL;
  439. origin_circuit_t *circ = NULL;
  440. cell_queue_t *queue;
  441. (void) arg;
  442. /* Set the test time to be mocked, since this test assumes that no
  443. * time will pass, ewma values will not need to be re-scaled, and so on */
  444. monotime_enable_test_mocking();
  445. monotime_set_mock_time_nsec(U64_LITERAL(1000000000) * 12345);
  446. cmux_ewma_set_options(NULL,NULL);
  447. /* The channel will be freed so we need to hijack this so the scheduler
  448. * doesn't get confused. */
  449. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  450. /* Accept cells to lower layer */
  451. test_chan_accept_cells = 1;
  452. /* Setup a valid circuit to queue a cell. */
  453. circ = origin_circuit_new();
  454. tt_assert(circ);
  455. /* Circuit needs an origin purpose to be considered origin. */
  456. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  457. TO_CIRCUIT(circ)->n_circ_id = 42;
  458. /* This is the outbound test so use the next channel queue. */
  459. queue = &TO_CIRCUIT(circ)->n_chan_cells;
  460. /* Setup packed cell to queue on the circuit. */
  461. p_cell = packed_cell_new();
  462. tt_assert(p_cell);
  463. p_cell2 = packed_cell_new();
  464. tt_assert(p_cell2);
  465. /* Setup a channel to put the circuit on. */
  466. chan = new_fake_channel();
  467. tt_assert(chan);
  468. chan->state = CHANNEL_STATE_OPENING;
  469. channel_change_state_open(chan);
  470. /* Outbound channel. */
  471. channel_mark_outgoing(chan);
  472. /* Try to register it so we can clean it through the channel cleanup
  473. * process. */
  474. channel_register(chan);
  475. tt_int_op(chan->registered, OP_EQ, 1);
  476. /* Set EWMA policy so we can pick it when flushing. */
  477. circuitmux_set_policy(chan->cmux, &ewma_policy);
  478. tt_ptr_op(circuitmux_get_policy(chan->cmux), OP_EQ, &ewma_policy);
  479. /* Register circuit to the channel circid map which will attach the circuit
  480. * to the channel's cmux as well. */
  481. circuit_set_n_circid_chan(TO_CIRCUIT(circ), 42, chan);
  482. tt_int_op(channel_num_circuits(chan), OP_EQ, 1);
  483. /* Test the cmux state. */
  484. tt_ptr_op(TO_CIRCUIT(circ)->n_mux, OP_EQ, chan->cmux);
  485. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  486. OP_EQ, 1);
  487. /* Flush the channel without any cell on it. */
  488. old_count = test_cells_written;
  489. ssize_t flushed = channel_flush_some_cells(chan, 1);
  490. tt_i64_op(flushed, OP_EQ, 0);
  491. tt_int_op(test_cells_written, OP_EQ, old_count);
  492. tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
  493. tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 0);
  494. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
  495. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  496. OP_EQ, 0);
  497. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 0);
  498. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, 0);
  499. /* Queue cell onto the next queue that is the outbound direction. Than
  500. * update its cmux so the circuit can be picked when flushing cells. */
  501. cell_queue_append(queue, p_cell);
  502. p_cell = NULL;
  503. tt_int_op(queue->n, OP_EQ, 1);
  504. cell_queue_append(queue, p_cell2);
  505. p_cell2 = NULL;
  506. tt_int_op(queue->n, OP_EQ, 2);
  507. update_circuit_on_cmux(TO_CIRCUIT(circ), CELL_DIRECTION_OUT);
  508. tt_int_op(circuitmux_num_active_circuits(chan->cmux), OP_EQ, 1);
  509. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 2);
  510. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  511. OP_EQ, 1);
  512. /* From this point on, we have a queued cell on an active circuit attached
  513. * to the channel's cmux. */
  514. /* Flush the first cell. This is going to go down the call stack. */
  515. old_count = test_cells_written;
  516. flushed = channel_flush_some_cells(chan, 1);
  517. tt_i64_op(flushed, OP_EQ, 1);
  518. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  519. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 1);
  520. tt_int_op(channel_more_to_flush(chan), OP_EQ, 1);
  521. /* Circuit should remain active because there is a second cell queued. */
  522. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  523. OP_EQ, 1);
  524. /* Should still be attached. */
  525. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  526. OP_EQ, 1);
  527. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 1);
  528. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0));
  529. /* Flush second cell. This is going to go down the call stack. */
  530. old_count = test_cells_written;
  531. flushed = channel_flush_some_cells(chan, 1);
  532. tt_i64_op(flushed, OP_EQ, 1);
  533. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  534. tt_int_op(circuitmux_num_cells(chan->cmux), OP_EQ, 0);
  535. tt_int_op(channel_more_to_flush(chan), OP_EQ, 0);
  536. /* No more cells should make the circuit inactive. */
  537. tt_int_op(circuitmux_is_circuit_active(chan->cmux, TO_CIRCUIT(circ)),
  538. OP_EQ, 0);
  539. /* Should still be attached. */
  540. tt_int_op(circuitmux_is_circuit_attached(chan->cmux, TO_CIRCUIT(circ)),
  541. OP_EQ, 1);
  542. tt_u64_op(chan->n_cells_xmitted, OP_EQ, 2);
  543. tt_u64_op(chan->n_bytes_xmitted, OP_EQ, get_cell_network_size(0) * 2);
  544. done:
  545. if (circ) {
  546. circuit_free_(TO_CIRCUIT(circ));
  547. }
  548. tor_free(p_cell);
  549. channel_free_all();
  550. UNMOCK(scheduler_release_channel);
  551. monotime_disable_test_mocking();
  552. }
  553. /* Test inbound cell. The callstack is:
  554. * channel_process_cell()
  555. * -> chan->cell_handler()
  556. *
  557. * This test is about checking if we can process an inbound cell down to the
  558. * channel handler. */
  559. static void
  560. test_channel_inbound_cell(void *arg)
  561. {
  562. channel_t *chan = NULL;
  563. cell_t *cell = NULL;
  564. int old_count;
  565. (void) arg;
  566. /* The channel will be freed so we need to hijack this so the scheduler
  567. * doesn't get confused. */
  568. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  569. /* Accept cells to lower layer */
  570. test_chan_accept_cells = 1;
  571. chan = new_fake_channel();
  572. tt_assert(chan);
  573. /* Start it off in OPENING */
  574. chan->state = CHANNEL_STATE_OPENING;
  575. /* Try to register it */
  576. channel_register(chan);
  577. tt_int_op(chan->registered, OP_EQ, 1);
  578. /* Open it */
  579. channel_change_state_open(chan);
  580. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
  581. tt_int_op(chan->has_been_open, OP_EQ, 1);
  582. /* Receive a cell now. */
  583. cell = tor_malloc_zero(sizeof(*cell));
  584. make_fake_cell(cell);
  585. old_count = test_chan_fixed_cells_recved;
  586. channel_process_cell(chan, cell);
  587. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
  588. tt_assert(monotime_coarse_is_zero(&chan->timestamp_xfer));
  589. tt_u64_op(chan->timestamp_active, OP_EQ, 0);
  590. tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
  591. /* Setup incoming cell handlers. We don't care about var cell, the channel
  592. * layers is not handling those. */
  593. channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
  594. tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
  595. /* Now process the cell, we should see it. */
  596. old_count = test_chan_fixed_cells_recved;
  597. channel_process_cell(chan, cell);
  598. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  599. /* We should have a series of timestamp set. */
  600. tt_assert(!monotime_coarse_is_zero(&chan->timestamp_xfer));
  601. tt_u64_op(chan->timestamp_active, OP_NE, 0);
  602. tt_u64_op(chan->timestamp_recv, OP_NE, 0);
  603. tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
  604. tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
  605. tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
  606. /* Close it */
  607. old_count = test_close_called;
  608. channel_mark_for_close(chan);
  609. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
  610. tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
  611. tt_int_op(test_close_called, OP_EQ, old_count + 1);
  612. /* This closes the channe so it calls in the scheduler, make sure of it. */
  613. old_count = test_releases_count;
  614. chan_test_finish_close(chan);
  615. tt_int_op(test_releases_count, OP_EQ, old_count + 1);
  616. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
  617. /* The channel will be free, lets make sure it is not accessible. */
  618. uint64_t chan_id = chan->global_identifier;
  619. tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
  620. channel_run_cleanup();
  621. chan = channel_find_by_global_id(chan_id);
  622. tt_assert(chan == NULL);
  623. done:
  624. tor_free(cell);
  625. UNMOCK(scheduler_release_channel);
  626. }
  627. /**
  628. * Normal channel lifecycle test:
  629. *
  630. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  631. */
  632. static void
  633. test_channel_lifecycle(void *arg)
  634. {
  635. channel_t *ch1 = NULL, *ch2 = NULL;
  636. packed_cell_t *p_cell = NULL;
  637. int old_count, init_doesnt_want_writes_count;
  638. int init_releases_count;
  639. (void)arg;
  640. /* Mock these for the whole lifecycle test */
  641. MOCK(scheduler_channel_doesnt_want_writes,
  642. scheduler_channel_doesnt_want_writes_mock);
  643. MOCK(scheduler_release_channel,
  644. scheduler_release_channel_mock);
  645. /* Cache some initial counter values */
  646. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  647. init_releases_count = test_releases_count;
  648. /* Accept cells to lower layer */
  649. test_chan_accept_cells = 1;
  650. ch1 = new_fake_channel();
  651. tt_assert(ch1);
  652. /* Start it off in OPENING */
  653. ch1->state = CHANNEL_STATE_OPENING;
  654. /* Try to register it */
  655. channel_register(ch1);
  656. tt_assert(ch1->registered);
  657. /* Try to write a cell through (should queue) */
  658. p_cell = packed_cell_new();
  659. old_count = test_cells_written;
  660. channel_write_packed_cell(ch1, p_cell);
  661. tt_int_op(old_count, OP_EQ, test_cells_written);
  662. /* Move it to OPEN and flush */
  663. channel_change_state_open(ch1);
  664. /* Get another one */
  665. ch2 = new_fake_channel();
  666. tt_assert(ch2);
  667. ch2->state = CHANNEL_STATE_OPENING;
  668. /* Register */
  669. channel_register(ch2);
  670. tt_assert(ch2->registered);
  671. /* Check counters */
  672. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  673. init_doesnt_want_writes_count);
  674. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  675. /* Move ch1 to MAINT */
  676. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  677. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  678. init_doesnt_want_writes_count + 1);
  679. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  680. /* Move ch2 to OPEN */
  681. channel_change_state_open(ch2);
  682. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  683. init_doesnt_want_writes_count + 1);
  684. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  685. /* Move ch1 back to OPEN */
  686. channel_change_state_open(ch1);
  687. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  688. init_doesnt_want_writes_count + 1);
  689. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  690. /* Mark ch2 for close */
  691. channel_mark_for_close(ch2);
  692. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  693. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  694. init_doesnt_want_writes_count + 1);
  695. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
  696. /* Shut down channels */
  697. channel_free_all();
  698. ch1 = ch2 = NULL;
  699. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  700. init_doesnt_want_writes_count + 1);
  701. /* channel_free() calls scheduler_release_channel() */
  702. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
  703. done:
  704. free_fake_channel(ch1);
  705. free_fake_channel(ch2);
  706. UNMOCK(scheduler_channel_doesnt_want_writes);
  707. UNMOCK(scheduler_release_channel);
  708. }
  709. /**
  710. * Weird channel lifecycle test:
  711. *
  712. * OPENING->CLOSING->CLOSED
  713. * OPENING->OPEN->CLOSING->ERROR
  714. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  715. * OPENING->OPEN->MAINT->CLOSING->ERROR
  716. */
  717. static void
  718. test_channel_lifecycle_2(void *arg)
  719. {
  720. channel_t *ch = NULL;
  721. (void)arg;
  722. /* Mock these for the whole lifecycle test */
  723. MOCK(scheduler_channel_doesnt_want_writes,
  724. scheduler_channel_doesnt_want_writes_mock);
  725. MOCK(scheduler_release_channel,
  726. scheduler_release_channel_mock);
  727. /* Accept cells to lower layer */
  728. test_chan_accept_cells = 1;
  729. ch = new_fake_channel();
  730. tt_assert(ch);
  731. /* Start it off in OPENING */
  732. ch->state = CHANNEL_STATE_OPENING;
  733. /* Try to register it */
  734. channel_register(ch);
  735. tt_assert(ch->registered);
  736. /* Try to close it */
  737. channel_mark_for_close(ch);
  738. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  739. /* Finish closing it */
  740. chan_test_finish_close(ch);
  741. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  742. channel_run_cleanup();
  743. ch = NULL;
  744. /* Now try OPENING->OPEN->CLOSING->ERROR */
  745. ch = new_fake_channel();
  746. tt_assert(ch);
  747. ch->state = CHANNEL_STATE_OPENING;
  748. channel_register(ch);
  749. tt_assert(ch->registered);
  750. /* Finish opening it */
  751. channel_change_state_open(ch);
  752. /* Error exit from lower layer */
  753. chan_test_error(ch);
  754. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  755. chan_test_finish_close(ch);
  756. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  757. channel_run_cleanup();
  758. ch = NULL;
  759. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  760. ch = new_fake_channel();
  761. tt_assert(ch);
  762. ch->state = CHANNEL_STATE_OPENING;
  763. channel_register(ch);
  764. tt_assert(ch->registered);
  765. /* Finish opening it */
  766. channel_change_state_open(ch);
  767. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  768. /* Go to maintenance state */
  769. channel_change_state(ch, CHANNEL_STATE_MAINT);
  770. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  771. /* Lower layer close */
  772. channel_mark_for_close(ch);
  773. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  774. /* Finish */
  775. chan_test_finish_close(ch);
  776. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  777. channel_run_cleanup();
  778. ch = NULL;
  779. /*
  780. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  781. * maintenance state
  782. */
  783. ch = new_fake_channel();
  784. tt_assert(ch);
  785. ch->state = CHANNEL_STATE_OPENING;
  786. channel_register(ch);
  787. tt_assert(ch->registered);
  788. /* Finish opening it */
  789. channel_change_state_open(ch);
  790. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  791. /* Go to maintenance state */
  792. channel_change_state(ch, CHANNEL_STATE_MAINT);
  793. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  794. /* Lower layer close */
  795. channel_close_from_lower_layer(ch);
  796. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  797. /* Finish */
  798. chan_test_finish_close(ch);
  799. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  800. channel_run_cleanup();
  801. ch = NULL;
  802. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  803. ch = new_fake_channel();
  804. tt_assert(ch);
  805. ch->state = CHANNEL_STATE_OPENING;
  806. channel_register(ch);
  807. tt_assert(ch->registered);
  808. /* Finish opening it */
  809. channel_change_state_open(ch);
  810. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  811. /* Go to maintenance state */
  812. channel_change_state(ch, CHANNEL_STATE_MAINT);
  813. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  814. /* Lower layer close */
  815. chan_test_error(ch);
  816. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  817. /* Finish */
  818. chan_test_finish_close(ch);
  819. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  820. channel_run_cleanup();
  821. ch = NULL;
  822. /* Shut down channels */
  823. channel_free_all();
  824. done:
  825. tor_free(ch);
  826. UNMOCK(scheduler_channel_doesnt_want_writes);
  827. UNMOCK(scheduler_release_channel);
  828. return;
  829. }
  830. static void
  831. test_channel_id_map(void *arg)
  832. {
  833. (void)arg;
  834. #define N_CHAN 6
  835. char rsa_id[N_CHAN][DIGEST_LEN];
  836. ed25519_public_key_t *ed_id[N_CHAN];
  837. channel_t *chan[N_CHAN];
  838. int i;
  839. ed25519_public_key_t ed_zero;
  840. memset(&ed_zero, 0, sizeof(ed_zero));
  841. tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
  842. for (i = 0; i < N_CHAN; ++i) {
  843. crypto_rand(rsa_id[i], DIGEST_LEN);
  844. ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
  845. crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
  846. }
  847. /* For channel 3, have no Ed identity. */
  848. tor_free(ed_id[3]);
  849. /* Channel 2 and 4 have same ROSA identity */
  850. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  851. /* Channel 2 and 4 and 5 have same RSA identity */
  852. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  853. memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
  854. /* Channels 2 and 5 have same Ed25519 identity */
  855. memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
  856. for (i = 0; i < N_CHAN; ++i) {
  857. chan[i] = new_fake_channel();
  858. channel_register(chan[i]);
  859. channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
  860. }
  861. /* Lookup by RSA id only */
  862. tt_ptr_op(chan[0], OP_EQ,
  863. channel_find_by_remote_identity(rsa_id[0], NULL));
  864. tt_ptr_op(chan[1], OP_EQ,
  865. channel_find_by_remote_identity(rsa_id[1], NULL));
  866. tt_ptr_op(chan[3], OP_EQ,
  867. channel_find_by_remote_identity(rsa_id[3], NULL));
  868. channel_t *ch;
  869. ch = channel_find_by_remote_identity(rsa_id[2], NULL);
  870. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  871. ch = channel_next_with_rsa_identity(ch);
  872. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  873. ch = channel_next_with_rsa_identity(ch);
  874. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  875. ch = channel_next_with_rsa_identity(ch);
  876. tt_ptr_op(ch, OP_EQ, NULL);
  877. /* As above, but with zero Ed25519 ID (meaning "any ID") */
  878. tt_ptr_op(chan[0], OP_EQ,
  879. channel_find_by_remote_identity(rsa_id[0], &ed_zero));
  880. tt_ptr_op(chan[1], OP_EQ,
  881. channel_find_by_remote_identity(rsa_id[1], &ed_zero));
  882. tt_ptr_op(chan[3], OP_EQ,
  883. channel_find_by_remote_identity(rsa_id[3], &ed_zero));
  884. ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
  885. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  886. ch = channel_next_with_rsa_identity(ch);
  887. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  888. ch = channel_next_with_rsa_identity(ch);
  889. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  890. ch = channel_next_with_rsa_identity(ch);
  891. tt_ptr_op(ch, OP_EQ, NULL);
  892. /* Lookup nonexistent RSA identity */
  893. tt_ptr_op(NULL, OP_EQ,
  894. channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
  895. /* Look up by full identity pair */
  896. tt_ptr_op(chan[0], OP_EQ,
  897. channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
  898. tt_ptr_op(chan[1], OP_EQ,
  899. channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
  900. tt_ptr_op(chan[3], OP_EQ,
  901. channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
  902. tt_ptr_op(chan[4], OP_EQ,
  903. channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
  904. ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
  905. tt_assert(ch == chan[2] || ch == chan[5]);
  906. /* Look up RSA identity with wrong ed25519 identity */
  907. tt_ptr_op(NULL, OP_EQ,
  908. channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
  909. tt_ptr_op(NULL, OP_EQ,
  910. channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
  911. tt_ptr_op(NULL, OP_EQ,
  912. channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
  913. done:
  914. for (i = 0; i < N_CHAN; ++i) {
  915. channel_clear_identity_digest(chan[i]);
  916. channel_unregister(chan[i]);
  917. free_fake_channel(chan[i]);
  918. tor_free(ed_id[i]);
  919. }
  920. #undef N_CHAN
  921. }
  922. static void
  923. test_channel_state(void *arg)
  924. {
  925. (void) arg;
  926. /* Test state validity. */
  927. tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSED), OP_EQ, 1);
  928. tt_int_op(channel_state_is_valid(CHANNEL_STATE_CLOSING), OP_EQ, 1);
  929. tt_int_op(channel_state_is_valid(CHANNEL_STATE_ERROR), OP_EQ, 1);
  930. tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPEN), OP_EQ, 1);
  931. tt_int_op(channel_state_is_valid(CHANNEL_STATE_OPENING), OP_EQ, 1);
  932. tt_int_op(channel_state_is_valid(CHANNEL_STATE_MAINT), OP_EQ, 1);
  933. tt_int_op(channel_state_is_valid(CHANNEL_STATE_LAST), OP_EQ, 0);
  934. tt_int_op(channel_state_is_valid(INT_MAX), OP_EQ, 0);
  935. /* Test listener state validity. */
  936. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSED),
  937. OP_EQ, 1);
  938. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LISTENING),
  939. OP_EQ, 1);
  940. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_CLOSING),
  941. OP_EQ, 1);
  942. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_ERROR),
  943. OP_EQ, 1);
  944. tt_int_op(channel_listener_state_is_valid(CHANNEL_LISTENER_STATE_LAST),
  945. OP_EQ, 0);
  946. tt_int_op(channel_listener_state_is_valid(INT_MAX), OP_EQ, 0);
  947. /* Test state transition. */
  948. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
  949. CHANNEL_STATE_OPENING), OP_EQ, 1);
  950. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSED,
  951. CHANNEL_STATE_ERROR), OP_EQ, 0);
  952. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  953. CHANNEL_STATE_ERROR), OP_EQ, 1);
  954. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  955. CHANNEL_STATE_CLOSED), OP_EQ, 1);
  956. tt_int_op(channel_state_can_transition(CHANNEL_STATE_CLOSING,
  957. CHANNEL_STATE_OPEN), OP_EQ, 0);
  958. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  959. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  960. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  961. CHANNEL_STATE_ERROR), OP_EQ, 1);
  962. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  963. CHANNEL_STATE_OPEN), OP_EQ, 1);
  964. tt_int_op(channel_state_can_transition(CHANNEL_STATE_MAINT,
  965. CHANNEL_STATE_OPENING), OP_EQ, 0);
  966. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  967. CHANNEL_STATE_OPEN), OP_EQ, 1);
  968. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  969. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  970. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPENING,
  971. CHANNEL_STATE_ERROR), OP_EQ, 1);
  972. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  973. CHANNEL_STATE_ERROR), OP_EQ, 1);
  974. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  975. CHANNEL_STATE_CLOSING), OP_EQ, 1);
  976. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  977. CHANNEL_STATE_ERROR), OP_EQ, 1);
  978. tt_int_op(channel_state_can_transition(CHANNEL_STATE_OPEN,
  979. CHANNEL_STATE_MAINT), OP_EQ, 1);
  980. tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST,
  981. CHANNEL_STATE_MAINT), OP_EQ, 0);
  982. tt_int_op(channel_state_can_transition(CHANNEL_STATE_LAST, INT_MAX),
  983. OP_EQ, 0);
  984. /* Test listener state transition. */
  985. tt_int_op(channel_listener_state_can_transition(
  986. CHANNEL_LISTENER_STATE_CLOSED,
  987. CHANNEL_LISTENER_STATE_LISTENING),
  988. OP_EQ, 1);
  989. tt_int_op(channel_listener_state_can_transition(
  990. CHANNEL_LISTENER_STATE_CLOSED,
  991. CHANNEL_LISTENER_STATE_ERROR),
  992. OP_EQ, 0);
  993. tt_int_op(channel_listener_state_can_transition(
  994. CHANNEL_LISTENER_STATE_CLOSING,
  995. CHANNEL_LISTENER_STATE_CLOSED),
  996. OP_EQ, 1);
  997. tt_int_op(channel_listener_state_can_transition(
  998. CHANNEL_LISTENER_STATE_CLOSING,
  999. CHANNEL_LISTENER_STATE_ERROR),
  1000. OP_EQ, 1);
  1001. tt_int_op(channel_listener_state_can_transition(
  1002. CHANNEL_LISTENER_STATE_ERROR,
  1003. CHANNEL_LISTENER_STATE_CLOSING),
  1004. OP_EQ, 0);
  1005. tt_int_op(channel_listener_state_can_transition(
  1006. CHANNEL_LISTENER_STATE_LISTENING,
  1007. CHANNEL_LISTENER_STATE_CLOSING),
  1008. OP_EQ, 1);
  1009. tt_int_op(channel_listener_state_can_transition(
  1010. CHANNEL_LISTENER_STATE_LISTENING,
  1011. CHANNEL_LISTENER_STATE_ERROR),
  1012. OP_EQ, 1);
  1013. tt_int_op(channel_listener_state_can_transition(
  1014. CHANNEL_LISTENER_STATE_LAST,
  1015. INT_MAX),
  1016. OP_EQ, 0);
  1017. /* Test state string. */
  1018. tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSING), OP_EQ,
  1019. "closing");
  1020. tt_str_op(channel_state_to_string(CHANNEL_STATE_ERROR), OP_EQ,
  1021. "channel error");
  1022. tt_str_op(channel_state_to_string(CHANNEL_STATE_CLOSED), OP_EQ,
  1023. "closed");
  1024. tt_str_op(channel_state_to_string(CHANNEL_STATE_OPEN), OP_EQ,
  1025. "open");
  1026. tt_str_op(channel_state_to_string(CHANNEL_STATE_OPENING), OP_EQ,
  1027. "opening");
  1028. tt_str_op(channel_state_to_string(CHANNEL_STATE_MAINT), OP_EQ,
  1029. "temporarily suspended for maintenance");
  1030. tt_str_op(channel_state_to_string(CHANNEL_STATE_LAST), OP_EQ,
  1031. "unknown or invalid channel state");
  1032. tt_str_op(channel_state_to_string(INT_MAX), OP_EQ,
  1033. "unknown or invalid channel state");
  1034. /* Test listener state string. */
  1035. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_CLOSING),
  1036. OP_EQ, "closing");
  1037. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_ERROR),
  1038. OP_EQ, "channel listener error");
  1039. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LISTENING),
  1040. OP_EQ, "listening");
  1041. tt_str_op(channel_listener_state_to_string(CHANNEL_LISTENER_STATE_LAST),
  1042. OP_EQ, "unknown or invalid channel listener state");
  1043. tt_str_op(channel_listener_state_to_string(INT_MAX),
  1044. OP_EQ, "unknown or invalid channel listener state");
  1045. done:
  1046. ;
  1047. }
  1048. static networkstatus_t *mock_ns = NULL;
  1049. static networkstatus_t *
  1050. mock_networkstatus_get_latest_consensus(void)
  1051. {
  1052. return mock_ns;
  1053. }
  1054. static void
  1055. test_channel_duplicates(void *arg)
  1056. {
  1057. channel_t *chan = NULL;
  1058. routerstatus_t rs;
  1059. (void) arg;
  1060. setup_full_capture_of_logs(LOG_INFO);
  1061. /* Try a flat call with channel nor connections. */
  1062. channel_check_for_duplicates();
  1063. expect_log_msg_containing(
  1064. "Found 0 connections to 0 relays. Found 0 current canonical "
  1065. "connections, in 0 of which we were a non-canonical peer. "
  1066. "0 relays had more than 1 connection, 0 had more than 2, and "
  1067. "0 had more than 4 connections.");
  1068. mock_ns = tor_malloc_zero(sizeof(*mock_ns));
  1069. mock_ns->routerstatus_list = smartlist_new();
  1070. MOCK(networkstatus_get_latest_consensus,
  1071. mock_networkstatus_get_latest_consensus);
  1072. chan = new_fake_channel();
  1073. tt_assert(chan);
  1074. chan->is_canonical = test_chan_is_canonical;
  1075. memset(chan->identity_digest, 'A', sizeof(chan->identity_digest));
  1076. channel_add_to_digest_map(chan);
  1077. tt_ptr_op(channel_find_by_remote_identity(chan->identity_digest, NULL),
  1078. OP_EQ, chan);
  1079. /* No relay has been associated with this channel. */
  1080. channel_check_for_duplicates();
  1081. expect_log_msg_containing(
  1082. "Found 0 connections to 0 relays. Found 0 current canonical "
  1083. "connections, in 0 of which we were a non-canonical peer. "
  1084. "0 relays had more than 1 connection, 0 had more than 2, and "
  1085. "0 had more than 4 connections.");
  1086. /* Associate relay to this connection in the consensus. */
  1087. memset(&rs, 0, sizeof(rs));
  1088. memset(rs.identity_digest, 'A', sizeof(rs.identity_digest));
  1089. smartlist_add(mock_ns->routerstatus_list, &rs);
  1090. /* Non opened channel. */
  1091. chan->state = CHANNEL_STATE_CLOSING;
  1092. channel_check_for_duplicates();
  1093. expect_log_msg_containing(
  1094. "Found 0 connections to 0 relays. Found 0 current canonical "
  1095. "connections, in 0 of which we were a non-canonical peer. "
  1096. "0 relays had more than 1 connection, 0 had more than 2, and "
  1097. "0 had more than 4 connections.");
  1098. chan->state = CHANNEL_STATE_OPEN;
  1099. channel_check_for_duplicates();
  1100. expect_log_msg_containing(
  1101. "Found 1 connections to 1 relays. Found 0 current canonical "
  1102. "connections, in 0 of which we were a non-canonical peer. "
  1103. "0 relays had more than 1 connection, 0 had more than 2, and "
  1104. "0 had more than 4 connections.");
  1105. test_chan_should_be_canonical = 1;
  1106. channel_check_for_duplicates();
  1107. expect_log_msg_containing(
  1108. "Found 1 connections to 1 relays. Found 1 current canonical "
  1109. "connections, in 1 of which we were a non-canonical peer. "
  1110. "0 relays had more than 1 connection, 0 had more than 2, and "
  1111. "0 had more than 4 connections.");
  1112. teardown_capture_of_logs();
  1113. done:
  1114. free_fake_channel(chan);
  1115. smartlist_clear(mock_ns->routerstatus_list);
  1116. networkstatus_vote_free(mock_ns);
  1117. UNMOCK(networkstatus_get_latest_consensus);
  1118. }
  1119. static void
  1120. test_channel_for_extend(void *arg)
  1121. {
  1122. channel_t *chan1 = NULL, *chan2 = NULL;
  1123. channel_t *ret_chan = NULL;
  1124. char digest[DIGEST_LEN];
  1125. ed25519_public_key_t ed_id;
  1126. tor_addr_t addr;
  1127. const char *msg;
  1128. int launch;
  1129. time_t now = time(NULL);
  1130. (void) arg;
  1131. memset(digest, 'A', sizeof(digest));
  1132. memset(&ed_id, 'B', sizeof(ed_id));
  1133. chan1 = new_fake_channel();
  1134. tt_assert(chan1);
  1135. /* Need to be registered to get added to the id map. */
  1136. channel_register(chan1);
  1137. tt_int_op(chan1->registered, OP_EQ, 1);
  1138. /* We need those for the test. */
  1139. chan1->is_canonical = test_chan_is_canonical;
  1140. chan1->matches_target = test_chan_matches_target;
  1141. chan1->timestamp_created = now - 9;
  1142. chan2 = new_fake_channel();
  1143. tt_assert(chan2);
  1144. /* Need to be registered to get added to the id map. */
  1145. channel_register(chan2);
  1146. tt_int_op(chan2->registered, OP_EQ, 1);
  1147. /* We need those for the test. */
  1148. chan2->is_canonical = test_chan_is_canonical;
  1149. chan2->matches_target = test_chan_matches_target;
  1150. /* Make it older than chan1. */
  1151. chan2->timestamp_created = chan1->timestamp_created - 1;
  1152. /* Set channel identities and add it to the channel map. The last one to be
  1153. * added is made the first one in the list so the lookup will always return
  1154. * that one first. */
  1155. channel_set_identity_digest(chan2, digest, &ed_id);
  1156. channel_set_identity_digest(chan1, digest, &ed_id);
  1157. tt_ptr_op(channel_find_by_remote_identity(digest, NULL), OP_EQ, chan1);
  1158. tt_ptr_op(channel_find_by_remote_identity(digest, &ed_id), OP_EQ, chan1);
  1159. /* The expected result is chan2 because it is older than chan1. */
  1160. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1161. tt_assert(ret_chan);
  1162. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1163. tt_int_op(launch, OP_EQ, 0);
  1164. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1165. /* Switch that around from previous test. */
  1166. chan2->timestamp_created = chan1->timestamp_created + 1;
  1167. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1168. tt_assert(ret_chan);
  1169. tt_ptr_op(ret_chan, OP_EQ, chan1);
  1170. tt_int_op(launch, OP_EQ, 0);
  1171. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1172. /* Same creation time, num circuits will be used and they both have 0 so the
  1173. * channel 2 should be picked due to how channel_is_better() work. */
  1174. chan2->timestamp_created = chan1->timestamp_created;
  1175. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1176. tt_assert(ret_chan);
  1177. tt_ptr_op(ret_chan, OP_EQ, chan1);
  1178. tt_int_op(launch, OP_EQ, 0);
  1179. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1180. /* For the rest of the tests, we need channel 1 to be the older. */
  1181. chan2->timestamp_created = chan1->timestamp_created + 1;
  1182. /* Condemned the older channel. */
  1183. chan1->state = CHANNEL_STATE_CLOSING;
  1184. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1185. tt_assert(ret_chan);
  1186. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1187. tt_int_op(launch, OP_EQ, 0);
  1188. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1189. chan1->state = CHANNEL_STATE_OPEN;
  1190. /* Make the older channel a client one. */
  1191. channel_mark_client(chan1);
  1192. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1193. tt_assert(ret_chan);
  1194. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1195. tt_int_op(launch, OP_EQ, 0);
  1196. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1197. channel_clear_client(chan1);
  1198. /* Non matching ed identity with valid digest. */
  1199. ed25519_public_key_t dumb_ed_id;
  1200. memset(&dumb_ed_id, 0, sizeof(dumb_ed_id));
  1201. ret_chan = channel_get_for_extend(digest, &dumb_ed_id, &addr, &msg,
  1202. &launch);
  1203. tt_assert(!ret_chan);
  1204. tt_str_op(msg, OP_EQ, "Not connected. Connecting.");
  1205. tt_int_op(launch, OP_EQ, 1);
  1206. /* Opening channel, we'll check if the target address matches. */
  1207. test_chan_should_match_target = 1;
  1208. chan1->state = CHANNEL_STATE_OPENING;
  1209. chan2->state = CHANNEL_STATE_OPENING;
  1210. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1211. tt_assert(!ret_chan);
  1212. tt_str_op(msg, OP_EQ, "Connection in progress; waiting.");
  1213. tt_int_op(launch, OP_EQ, 0);
  1214. chan1->state = CHANNEL_STATE_OPEN;
  1215. chan2->state = CHANNEL_STATE_OPEN;
  1216. /* Mark channel 1 as bad for circuits. */
  1217. channel_mark_bad_for_new_circs(chan1);
  1218. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1219. tt_assert(ret_chan);
  1220. tt_ptr_op(ret_chan, OP_EQ, chan2);
  1221. tt_int_op(launch, OP_EQ, 0);
  1222. tt_str_op(msg, OP_EQ, "Connection is fine; using it.");
  1223. chan1->is_bad_for_new_circs = 0;
  1224. /* Mark both channels as unusable. */
  1225. channel_mark_bad_for_new_circs(chan1);
  1226. channel_mark_bad_for_new_circs(chan2);
  1227. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1228. tt_assert(!ret_chan);
  1229. tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
  1230. " Launching a new one.");
  1231. tt_int_op(launch, OP_EQ, 1);
  1232. chan1->is_bad_for_new_circs = 0;
  1233. chan2->is_bad_for_new_circs = 0;
  1234. /* Non canonical channels. */
  1235. test_chan_should_match_target = 0;
  1236. test_chan_canonical_should_be_reliable = 1;
  1237. ret_chan = channel_get_for_extend(digest, &ed_id, &addr, &msg, &launch);
  1238. tt_assert(!ret_chan);
  1239. tt_str_op(msg, OP_EQ, "Connections all too old, or too non-canonical. "
  1240. " Launching a new one.");
  1241. tt_int_op(launch, OP_EQ, 1);
  1242. done:
  1243. free_fake_channel(chan1);
  1244. free_fake_channel(chan2);
  1245. }
  1246. static void
  1247. test_channel_listener(void *arg)
  1248. {
  1249. int old_count;
  1250. time_t now = time(NULL);
  1251. channel_listener_t *chan = NULL;
  1252. (void) arg;
  1253. chan = tor_malloc_zero(sizeof(*chan));
  1254. tt_assert(chan);
  1255. channel_init_listener(chan);
  1256. tt_u64_op(chan->global_identifier, OP_EQ, 1);
  1257. tt_int_op(chan->timestamp_created, OP_GE, now);
  1258. chan->close = test_chan_listener_close;
  1259. /* Register it. At this point, it is not open so it will be put in the
  1260. * finished list. */
  1261. channel_listener_register(chan);
  1262. tt_int_op(chan->registered, OP_EQ, 1);
  1263. channel_listener_unregister(chan);
  1264. /* Register it as listening now thus active. */
  1265. chan->state = CHANNEL_LISTENER_STATE_LISTENING;
  1266. channel_listener_register(chan);
  1267. tt_int_op(chan->registered, OP_EQ, 1);
  1268. /* Set the listener function. */
  1269. channel_listener_set_listener_fn(chan, test_chan_listener_fn);
  1270. tt_ptr_op(chan->listener, OP_EQ, test_chan_listener_fn);
  1271. /* Put a channel in the listener incoming list and queue it.
  1272. * function. By doing this, the listener() handler will be called. */
  1273. channel_t *in_chan = new_fake_channel();
  1274. old_count = test_chan_listener_fn_called;
  1275. channel_listener_queue_incoming(chan, in_chan);
  1276. free_fake_channel(in_chan);
  1277. tt_int_op(test_chan_listener_fn_called, OP_EQ, old_count + 1);
  1278. /* Put listener channel in CLOSING state. */
  1279. old_count = test_chan_listener_close_fn_called;
  1280. channel_listener_mark_for_close(chan);
  1281. tt_int_op(test_chan_listener_close_fn_called, OP_EQ, old_count + 1);
  1282. channel_listener_change_state(chan, CHANNEL_LISTENER_STATE_CLOSED);
  1283. /* Dump stats so we at least hit the code path. */
  1284. chan->describe_transport = test_chan_listener_describe_transport;
  1285. /* There is a check for "now > timestamp_created" when dumping the stats so
  1286. * make sure we go in. */
  1287. chan->timestamp_created = now - 10;
  1288. channel_listener_dump_statistics(chan, LOG_INFO);
  1289. done:
  1290. channel_free_all();
  1291. }
  1292. struct testcase_t channel_tests[] = {
  1293. { "inbound_cell", test_channel_inbound_cell, TT_FORK,
  1294. NULL, NULL },
  1295. { "outbound_cell", test_channel_outbound_cell, TT_FORK,
  1296. NULL, NULL },
  1297. { "id_map", test_channel_id_map, TT_FORK,
  1298. NULL, NULL },
  1299. { "lifecycle", test_channel_lifecycle, TT_FORK,
  1300. NULL, NULL },
  1301. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
  1302. NULL, NULL },
  1303. { "dumpstats", test_channel_dumpstats, TT_FORK,
  1304. NULL, NULL },
  1305. { "state", test_channel_state, TT_FORK,
  1306. NULL, NULL },
  1307. { "duplicates", test_channel_duplicates, TT_FORK,
  1308. NULL, NULL },
  1309. { "get_channel_for_extend", test_channel_for_extend, TT_FORK,
  1310. NULL, NULL },
  1311. { "listener", test_channel_listener, TT_FORK,
  1312. NULL, NULL },
  1313. END_OF_TESTCASES
  1314. };