test_channel.c 47 KB

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