test_channel.c 41 KB

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