test_channel.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /* Copyright (c) 2013-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define CHANNEL_PRIVATE_
  5. #include "or.h"
  6. #include "channel.h"
  7. /* For channel_note_destroy_not_pending */
  8. #include "circuitlist.h"
  9. #include "circuitmux.h"
  10. /* For var_cell_free */
  11. #include "connection_or.h"
  12. /* For packed_cell stuff */
  13. #define RELAY_PRIVATE
  14. #include "relay.h"
  15. /* For init/free stuff */
  16. #include "scheduler.h"
  17. /* Test suite stuff */
  18. #include "test.h"
  19. #include "fakechans.h"
  20. static int test_chan_accept_cells = 0;
  21. static int test_chan_fixed_cells_recved = 0;
  22. static cell_t * test_chan_last_seen_fixed_cell_ptr = NULL;
  23. static int test_chan_var_cells_recved = 0;
  24. static var_cell_t * test_chan_last_seen_var_cell_ptr = NULL;
  25. static int test_cells_written = 0;
  26. static int test_doesnt_want_writes_count = 0;
  27. static int test_dumpstats_calls = 0;
  28. static int test_has_waiting_cells_count = 0;
  29. static double test_overhead_estimate = 1.0;
  30. static int test_releases_count = 0;
  31. static channel_t *dump_statistics_mock_target = NULL;
  32. static int dump_statistics_mock_matches = 0;
  33. static int test_close_called = 0;
  34. static void chan_test_channel_dump_statistics_mock(
  35. channel_t *chan, int severity);
  36. static const char * chan_test_describe_transport(channel_t *ch);
  37. static void chan_test_dumpstats(channel_t *ch, int severity);
  38. static void chan_test_var_cell_handler(channel_t *ch,
  39. var_cell_t *var_cell);
  40. static void chan_test_error(channel_t *ch);
  41. static void chan_test_finish_close(channel_t *ch);
  42. static const char * chan_test_get_remote_descr(channel_t *ch, int flags);
  43. static int chan_test_is_canonical(channel_t *ch, int req);
  44. static size_t chan_test_num_bytes_queued(channel_t *ch);
  45. static int chan_test_num_cells_writeable(channel_t *ch);
  46. static int chan_test_write_cell(channel_t *ch, cell_t *cell);
  47. static int chan_test_write_packed_cell(channel_t *ch,
  48. packed_cell_t *packed_cell);
  49. static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
  50. static void scheduler_channel_doesnt_want_writes_mock(channel_t *ch);
  51. static void test_channel_dumpstats(void *arg);
  52. static void test_channel_lifecycle(void *arg);
  53. static const char *
  54. chan_test_describe_transport(channel_t *ch)
  55. {
  56. tt_ptr_op(ch, OP_NE, NULL);
  57. done:
  58. return "Fake channel for unit tests";
  59. }
  60. /**
  61. * Mock for channel_dump_statistics(); if the channel matches the
  62. * target, bump a counter - otherwise ignore.
  63. */
  64. static void
  65. chan_test_channel_dump_statistics_mock(channel_t *chan, int severity)
  66. {
  67. tt_ptr_op(chan, OP_NE, NULL);
  68. (void)severity;
  69. if (chan != NULL && chan == dump_statistics_mock_target) {
  70. ++dump_statistics_mock_matches;
  71. }
  72. done:
  73. return;
  74. }
  75. /*
  76. * Handle an incoming fixed-size cell for unit tests
  77. */
  78. static void
  79. chan_test_cell_handler(channel_t *chan, cell_t *cell)
  80. {
  81. tt_assert(chan);
  82. tt_assert(cell);
  83. test_chan_last_seen_fixed_cell_ptr = cell;
  84. ++test_chan_fixed_cells_recved;
  85. done:
  86. return;
  87. }
  88. /*
  89. * Fake transport-specific stats call
  90. */
  91. static void
  92. chan_test_dumpstats(channel_t *ch, int severity)
  93. {
  94. tt_ptr_op(ch, OP_NE, NULL);
  95. (void)severity;
  96. ++test_dumpstats_calls;
  97. done:
  98. return;
  99. }
  100. /*
  101. * Handle an incoming variable-size cell for unit tests
  102. */
  103. static void
  104. chan_test_var_cell_handler(channel_t *ch,
  105. var_cell_t *var_cell)
  106. {
  107. tt_assert(ch);
  108. tt_assert(var_cell);
  109. test_chan_last_seen_var_cell_ptr = var_cell;
  110. ++test_chan_var_cells_recved;
  111. done:
  112. return;
  113. }
  114. static void
  115. chan_test_close(channel_t *ch)
  116. {
  117. tt_assert(ch);
  118. ++test_close_called;
  119. done:
  120. return;
  121. }
  122. /*
  123. * Close a channel through the error path
  124. */
  125. static void
  126. chan_test_error(channel_t *ch)
  127. {
  128. tt_assert(ch);
  129. tt_assert(!(ch->state == CHANNEL_STATE_CLOSING ||
  130. ch->state == CHANNEL_STATE_ERROR ||
  131. ch->state == CHANNEL_STATE_CLOSED));
  132. channel_close_for_error(ch);
  133. done:
  134. return;
  135. }
  136. /*
  137. * Finish closing a channel from CHANNEL_STATE_CLOSING
  138. */
  139. static void
  140. chan_test_finish_close(channel_t *ch)
  141. {
  142. tt_assert(ch);
  143. tt_assert(ch->state == CHANNEL_STATE_CLOSING);
  144. channel_closed(ch);
  145. done:
  146. return;
  147. }
  148. static const char *
  149. chan_test_get_remote_descr(channel_t *ch, int flags)
  150. {
  151. tt_assert(ch);
  152. tt_int_op(flags & ~(GRD_FLAG_ORIGINAL | GRD_FLAG_ADDR_ONLY), OP_EQ, 0);
  153. done:
  154. return "Fake channel for unit tests; no real endpoint";
  155. }
  156. static double
  157. chan_test_get_overhead_estimate(channel_t *ch)
  158. {
  159. tt_assert(ch);
  160. done:
  161. return test_overhead_estimate;
  162. }
  163. static int
  164. chan_test_is_canonical(channel_t *ch, int req)
  165. {
  166. tt_ptr_op(ch, OP_NE, NULL);
  167. tt_assert(req == 0 || req == 1);
  168. done:
  169. /* Fake channels are always canonical */
  170. return 1;
  171. }
  172. static size_t
  173. chan_test_num_bytes_queued(channel_t *ch)
  174. {
  175. tt_assert(ch);
  176. done:
  177. return 0;
  178. }
  179. static int
  180. chan_test_num_cells_writeable(channel_t *ch)
  181. {
  182. tt_assert(ch);
  183. done:
  184. return 32;
  185. }
  186. static int
  187. chan_test_write_cell(channel_t *ch, cell_t *cell)
  188. {
  189. int rv = 0;
  190. tt_assert(ch);
  191. tt_assert(cell);
  192. if (test_chan_accept_cells) {
  193. /* Free the cell and bump the counter */
  194. tor_free(cell);
  195. ++test_cells_written;
  196. rv = 1;
  197. }
  198. /* else return 0, we didn't accept it */
  199. done:
  200. return rv;
  201. }
  202. static int
  203. chan_test_write_packed_cell(channel_t *ch,
  204. packed_cell_t *packed_cell)
  205. {
  206. int rv = 0;
  207. tt_assert(ch);
  208. tt_assert(packed_cell);
  209. if (test_chan_accept_cells) {
  210. /* Free the cell and bump the counter */
  211. packed_cell_free(packed_cell);
  212. ++test_cells_written;
  213. rv = 1;
  214. }
  215. /* else return 0, we didn't accept it */
  216. done:
  217. return rv;
  218. }
  219. static int
  220. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  221. {
  222. int rv = 0;
  223. tt_assert(ch);
  224. tt_assert(var_cell);
  225. if (test_chan_accept_cells) {
  226. /* Free the cell and bump the counter */
  227. var_cell_free(var_cell);
  228. ++test_cells_written;
  229. rv = 1;
  230. }
  231. /* else return 0, we didn't accept it */
  232. done:
  233. return rv;
  234. }
  235. /**
  236. * Fill out c with a new fake cell for test suite use
  237. */
  238. void
  239. make_fake_cell(cell_t *c)
  240. {
  241. tt_ptr_op(c, OP_NE, NULL);
  242. c->circ_id = 1;
  243. c->command = CELL_RELAY;
  244. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  245. done:
  246. return;
  247. }
  248. /**
  249. * Fill out c with a new fake var_cell for test suite use
  250. */
  251. void
  252. make_fake_var_cell(var_cell_t *c)
  253. {
  254. tt_ptr_op(c, OP_NE, NULL);
  255. c->circ_id = 1;
  256. c->command = CELL_VERSIONS;
  257. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  258. memset(c->payload, 0, c->payload_len);
  259. done:
  260. return;
  261. }
  262. /**
  263. * Set up a new fake channel for the test suite
  264. */
  265. channel_t *
  266. new_fake_channel(void)
  267. {
  268. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  269. channel_init(chan);
  270. chan->close = chan_test_close;
  271. chan->get_overhead_estimate = chan_test_get_overhead_estimate;
  272. chan->get_remote_descr = chan_test_get_remote_descr;
  273. chan->num_bytes_queued = chan_test_num_bytes_queued;
  274. chan->num_cells_writeable = chan_test_num_cells_writeable;
  275. chan->write_cell = chan_test_write_cell;
  276. chan->write_packed_cell = chan_test_write_packed_cell;
  277. chan->write_var_cell = chan_test_write_var_cell;
  278. chan->state = CHANNEL_STATE_OPEN;
  279. chan->cmux = circuitmux_alloc();
  280. return chan;
  281. }
  282. void
  283. free_fake_channel(channel_t *chan)
  284. {
  285. if (! chan)
  286. return;
  287. if (chan->cmux)
  288. circuitmux_free(chan->cmux);
  289. tor_free(chan);
  290. }
  291. /**
  292. * Counter query for scheduler_channel_has_waiting_cells_mock()
  293. */
  294. int
  295. get_mock_scheduler_has_waiting_cells_count(void)
  296. {
  297. return test_has_waiting_cells_count;
  298. }
  299. /**
  300. * Mock for scheduler_channel_has_waiting_cells()
  301. */
  302. void
  303. scheduler_channel_has_waiting_cells_mock(channel_t *ch)
  304. {
  305. (void)ch;
  306. /* Increment counter */
  307. ++test_has_waiting_cells_count;
  308. return;
  309. }
  310. static void
  311. scheduler_channel_doesnt_want_writes_mock(channel_t *ch)
  312. {
  313. (void)ch;
  314. /* Increment counter */
  315. ++test_doesnt_want_writes_count;
  316. return;
  317. }
  318. /**
  319. * Counter query for scheduler_release_channel_mock()
  320. */
  321. int
  322. get_mock_scheduler_release_channel_count(void)
  323. {
  324. return test_releases_count;
  325. }
  326. /**
  327. * Mock for scheduler_release_channel()
  328. */
  329. void
  330. scheduler_release_channel_mock(channel_t *ch)
  331. {
  332. (void)ch;
  333. /* Increment counter */
  334. ++test_releases_count;
  335. return;
  336. }
  337. /**
  338. * Test for channel_dumpstats() and limited test for
  339. * channel_dump_statistics()
  340. */
  341. static void
  342. test_channel_dumpstats(void *arg)
  343. {
  344. channel_t *ch = NULL;
  345. packed_cell_t *p_cell = NULL;
  346. int old_count;
  347. (void)arg;
  348. /* Mock these for duration of the test */
  349. MOCK(scheduler_channel_doesnt_want_writes,
  350. scheduler_channel_doesnt_want_writes_mock);
  351. MOCK(scheduler_release_channel,
  352. scheduler_release_channel_mock);
  353. /* Set up a new fake channel */
  354. ch = new_fake_channel();
  355. tt_assert(ch);
  356. ch->cmux = circuitmux_alloc();
  357. /* Try to register it */
  358. channel_register(ch);
  359. tt_assert(ch->registered);
  360. /* Set up mock */
  361. dump_statistics_mock_target = ch;
  362. dump_statistics_mock_matches = 0;
  363. MOCK(channel_dump_statistics,
  364. chan_test_channel_dump_statistics_mock);
  365. /* Call channel_dumpstats() */
  366. channel_dumpstats(LOG_DEBUG);
  367. /* Assert that we hit the mock */
  368. tt_int_op(dump_statistics_mock_matches, OP_EQ, 1);
  369. /* Close the channel */
  370. channel_mark_for_close(ch);
  371. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  372. chan_test_finish_close(ch);
  373. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  374. /* Try again and hit the finished channel */
  375. channel_dumpstats(LOG_DEBUG);
  376. tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
  377. channel_run_cleanup();
  378. ch = NULL;
  379. /* Now we should hit nothing */
  380. channel_dumpstats(LOG_DEBUG);
  381. tt_int_op(dump_statistics_mock_matches, OP_EQ, 2);
  382. /* Unmock */
  383. UNMOCK(channel_dump_statistics);
  384. dump_statistics_mock_target = NULL;
  385. dump_statistics_mock_matches = 0;
  386. /* Now make another channel */
  387. ch = new_fake_channel();
  388. tt_assert(ch);
  389. ch->cmux = circuitmux_alloc();
  390. channel_register(ch);
  391. tt_assert(ch->registered);
  392. /* Lie about its age so dumpstats gets coverage for rate calculations */
  393. ch->timestamp_created = time(NULL) - 30;
  394. tt_assert(ch->timestamp_created > 0);
  395. tt_assert(time(NULL) > ch->timestamp_created);
  396. /* Put cells through it both ways to make the counters non-zero */
  397. p_cell = packed_cell_new();
  398. test_chan_accept_cells = 1;
  399. old_count = test_cells_written;
  400. channel_write_packed_cell(ch, p_cell);
  401. tt_int_op(test_cells_written, OP_EQ, old_count + 1);
  402. tt_assert(ch->n_bytes_xmitted > 0);
  403. tt_assert(ch->n_cells_xmitted > 0);
  404. /* Receive path */
  405. channel_set_cell_handlers(ch,
  406. chan_test_cell_handler,
  407. chan_test_var_cell_handler);
  408. tt_ptr_op(channel_get_cell_handler(ch), OP_EQ, chan_test_cell_handler);
  409. tt_ptr_op(channel_get_var_cell_handler(ch), OP_EQ,
  410. chan_test_var_cell_handler);
  411. p_cell = packed_cell_new();
  412. old_count = test_chan_fixed_cells_recved;
  413. channel_write_packed_cell(ch, p_cell);
  414. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  415. tt_assert(ch->n_bytes_recved > 0);
  416. tt_assert(ch->n_cells_recved > 0);
  417. /* Test channel_dump_statistics */
  418. ch->describe_transport = chan_test_describe_transport;
  419. ch->dumpstats = chan_test_dumpstats;
  420. ch->is_canonical = chan_test_is_canonical;
  421. old_count = test_dumpstats_calls;
  422. channel_dump_statistics(ch, LOG_DEBUG);
  423. tt_int_op(test_dumpstats_calls, OP_EQ, old_count + 1);
  424. /* Close the channel */
  425. channel_mark_for_close(ch);
  426. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  427. chan_test_finish_close(ch);
  428. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  429. channel_run_cleanup();
  430. ch = NULL;
  431. done:
  432. free_fake_channel(ch);
  433. UNMOCK(scheduler_channel_doesnt_want_writes);
  434. UNMOCK(scheduler_release_channel);
  435. return;
  436. }
  437. /* Test inbound cell. The callstack is:
  438. * channel_process_cell()
  439. * -> chan->cell_handler()
  440. *
  441. * This test is about checking if we can process an inbound cell down to the
  442. * channel handler. */
  443. static void
  444. test_channel_inbound_cell(void *arg)
  445. {
  446. channel_t *chan = NULL;
  447. cell_t *cell = NULL;
  448. int old_count;
  449. (void) arg;
  450. /* The channel will be freed so we need to hijack this so the scheduler
  451. * doesn't get confused. */
  452. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  453. /* Accept cells to lower layer */
  454. test_chan_accept_cells = 1;
  455. /* Use default overhead factor */
  456. test_overhead_estimate = 1.0;
  457. chan = new_fake_channel();
  458. tt_assert(chan);
  459. /* Start it off in OPENING */
  460. chan->state = CHANNEL_STATE_OPENING;
  461. /* Try to register it */
  462. channel_register(chan);
  463. tt_int_op(chan->registered, OP_EQ, 1);
  464. /* Open it */
  465. channel_change_state_open(chan);
  466. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_OPEN);
  467. tt_int_op(chan->has_been_open, OP_EQ, 1);
  468. /* Receive a cell now. */
  469. cell = tor_malloc_zero(sizeof(*cell));
  470. make_fake_cell(cell);
  471. old_count = test_chan_fixed_cells_recved;
  472. channel_process_cell(chan, cell);
  473. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count);
  474. tt_u64_op(chan->timestamp_xfer_ms, OP_EQ, 0);
  475. tt_u64_op(chan->timestamp_active, OP_EQ, 0);
  476. tt_u64_op(chan->timestamp_recv, OP_EQ, 0);
  477. /* Setup incoming cell handlers. We don't care about var cell, the channel
  478. * layers is not handling those. */
  479. channel_set_cell_handlers(chan, chan_test_cell_handler, NULL);
  480. tt_ptr_op(chan->cell_handler, OP_EQ, chan_test_cell_handler);
  481. /* Now process the cell, we should see it. */
  482. old_count = test_chan_fixed_cells_recved;
  483. channel_process_cell(chan, cell);
  484. tt_int_op(test_chan_fixed_cells_recved, OP_EQ, old_count + 1);
  485. /* We should have a series of timestamp set. */
  486. tt_u64_op(chan->timestamp_xfer_ms, OP_NE, 0);
  487. tt_u64_op(chan->timestamp_active, OP_NE, 0);
  488. tt_u64_op(chan->timestamp_recv, OP_NE, 0);
  489. tt_u64_op(chan->next_padding_time_ms, OP_EQ, 0);
  490. tt_u64_op(chan->n_cells_recved, OP_EQ, 1);
  491. tt_u64_op(chan->n_bytes_recved, OP_EQ, get_cell_network_size(0));
  492. /* Close it */
  493. old_count = test_close_called;
  494. channel_mark_for_close(chan);
  495. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSING);
  496. tt_int_op(chan->reason_for_closing, OP_EQ, CHANNEL_CLOSE_REQUESTED);
  497. tt_int_op(test_close_called, OP_EQ, old_count + 1);
  498. /* This closes the channe so it calls in the scheduler, make sure of it. */
  499. old_count = test_releases_count;
  500. chan_test_finish_close(chan);
  501. tt_int_op(test_releases_count, OP_EQ, old_count + 1);
  502. tt_int_op(chan->state, OP_EQ, CHANNEL_STATE_CLOSED);
  503. /* The channel will be free, lets make sure it is not accessible. */
  504. uint64_t chan_id = chan->global_identifier;
  505. tt_ptr_op(channel_find_by_global_id(chan_id), OP_EQ, chan);
  506. channel_run_cleanup();
  507. chan = channel_find_by_global_id(chan_id);
  508. tt_assert(chan == NULL);
  509. done:
  510. tor_free(cell);
  511. UNMOCK(scheduler_release_channel);
  512. }
  513. /**
  514. * Normal channel lifecycle test:
  515. *
  516. * OPENING->OPEN->MAINT->OPEN->CLOSING->CLOSED
  517. */
  518. static void
  519. test_channel_lifecycle(void *arg)
  520. {
  521. channel_t *ch1 = NULL, *ch2 = NULL;
  522. packed_cell_t *p_cell = NULL;
  523. int old_count, init_doesnt_want_writes_count;
  524. int init_releases_count;
  525. (void)arg;
  526. /* Mock these for the whole lifecycle test */
  527. MOCK(scheduler_channel_doesnt_want_writes,
  528. scheduler_channel_doesnt_want_writes_mock);
  529. MOCK(scheduler_release_channel,
  530. scheduler_release_channel_mock);
  531. /* Cache some initial counter values */
  532. init_doesnt_want_writes_count = test_doesnt_want_writes_count;
  533. init_releases_count = test_releases_count;
  534. /* Accept cells to lower layer */
  535. test_chan_accept_cells = 1;
  536. /* Use default overhead factor */
  537. test_overhead_estimate = 1.0;
  538. ch1 = new_fake_channel();
  539. tt_assert(ch1);
  540. /* Start it off in OPENING */
  541. ch1->state = CHANNEL_STATE_OPENING;
  542. /* We'll need a cmux */
  543. ch1->cmux = circuitmux_alloc();
  544. /* Try to register it */
  545. channel_register(ch1);
  546. tt_assert(ch1->registered);
  547. /* Try to write a cell through (should queue) */
  548. p_cell = packed_cell_new();
  549. old_count = test_cells_written;
  550. channel_write_packed_cell(ch1, p_cell);
  551. tt_int_op(old_count, OP_EQ, test_cells_written);
  552. /* Move it to OPEN and flush */
  553. channel_change_state_open(ch1);
  554. /* Queue should drain */
  555. tt_int_op(old_count + 1, OP_EQ, test_cells_written);
  556. /* Get another one */
  557. ch2 = new_fake_channel();
  558. tt_assert(ch2);
  559. ch2->state = CHANNEL_STATE_OPENING;
  560. ch2->cmux = circuitmux_alloc();
  561. /* Register */
  562. channel_register(ch2);
  563. tt_assert(ch2->registered);
  564. /* Check counters */
  565. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  566. init_doesnt_want_writes_count);
  567. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  568. /* Move ch1 to MAINT */
  569. channel_change_state(ch1, CHANNEL_STATE_MAINT);
  570. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  571. init_doesnt_want_writes_count + 1);
  572. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  573. /* Move ch2 to OPEN */
  574. channel_change_state_open(ch2);
  575. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  576. init_doesnt_want_writes_count + 1);
  577. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  578. /* Move ch1 back to OPEN */
  579. channel_change_state_open(ch1);
  580. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  581. init_doesnt_want_writes_count + 1);
  582. tt_int_op(test_releases_count, OP_EQ, init_releases_count);
  583. /* Mark ch2 for close */
  584. channel_mark_for_close(ch2);
  585. tt_int_op(ch2->state, OP_EQ, CHANNEL_STATE_CLOSING);
  586. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  587. init_doesnt_want_writes_count + 1);
  588. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 1);
  589. /* Shut down channels */
  590. channel_free_all();
  591. ch1 = ch2 = NULL;
  592. tt_int_op(test_doesnt_want_writes_count, OP_EQ,
  593. init_doesnt_want_writes_count + 1);
  594. /* channel_free() calls scheduler_release_channel() */
  595. tt_int_op(test_releases_count, OP_EQ, init_releases_count + 4);
  596. done:
  597. free_fake_channel(ch1);
  598. free_fake_channel(ch2);
  599. UNMOCK(scheduler_channel_doesnt_want_writes);
  600. UNMOCK(scheduler_release_channel);
  601. return;
  602. }
  603. /**
  604. * Weird channel lifecycle test:
  605. *
  606. * OPENING->CLOSING->CLOSED
  607. * OPENING->OPEN->CLOSING->ERROR
  608. * OPENING->OPEN->MAINT->CLOSING->CLOSED
  609. * OPENING->OPEN->MAINT->CLOSING->ERROR
  610. */
  611. static void
  612. test_channel_lifecycle_2(void *arg)
  613. {
  614. channel_t *ch = NULL;
  615. (void)arg;
  616. /* Mock these for the whole lifecycle test */
  617. MOCK(scheduler_channel_doesnt_want_writes,
  618. scheduler_channel_doesnt_want_writes_mock);
  619. MOCK(scheduler_release_channel,
  620. scheduler_release_channel_mock);
  621. /* Accept cells to lower layer */
  622. test_chan_accept_cells = 1;
  623. /* Use default overhead factor */
  624. test_overhead_estimate = 1.0;
  625. ch = new_fake_channel();
  626. tt_assert(ch);
  627. /* Start it off in OPENING */
  628. ch->state = CHANNEL_STATE_OPENING;
  629. /* The full lifecycle test needs a cmux */
  630. ch->cmux = circuitmux_alloc();
  631. /* Try to register it */
  632. channel_register(ch);
  633. tt_assert(ch->registered);
  634. /* Try to close it */
  635. channel_mark_for_close(ch);
  636. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  637. /* Finish closing it */
  638. chan_test_finish_close(ch);
  639. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  640. channel_run_cleanup();
  641. ch = NULL;
  642. /* Now try OPENING->OPEN->CLOSING->ERROR */
  643. ch = new_fake_channel();
  644. tt_assert(ch);
  645. ch->state = CHANNEL_STATE_OPENING;
  646. ch->cmux = circuitmux_alloc();
  647. channel_register(ch);
  648. tt_assert(ch->registered);
  649. /* Finish opening it */
  650. channel_change_state_open(ch);
  651. /* Error exit from lower layer */
  652. chan_test_error(ch);
  653. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  654. chan_test_finish_close(ch);
  655. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  656. channel_run_cleanup();
  657. ch = NULL;
  658. /* OPENING->OPEN->MAINT->CLOSING->CLOSED close from maintenance state */
  659. ch = new_fake_channel();
  660. tt_assert(ch);
  661. ch->state = CHANNEL_STATE_OPENING;
  662. ch->cmux = circuitmux_alloc();
  663. channel_register(ch);
  664. tt_assert(ch->registered);
  665. /* Finish opening it */
  666. channel_change_state_open(ch);
  667. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  668. /* Go to maintenance state */
  669. channel_change_state(ch, CHANNEL_STATE_MAINT);
  670. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  671. /* Lower layer close */
  672. channel_mark_for_close(ch);
  673. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  674. /* Finish */
  675. chan_test_finish_close(ch);
  676. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  677. channel_run_cleanup();
  678. ch = NULL;
  679. /*
  680. * OPENING->OPEN->MAINT->CLOSING->CLOSED lower-layer close during
  681. * maintenance state
  682. */
  683. ch = new_fake_channel();
  684. tt_assert(ch);
  685. ch->state = CHANNEL_STATE_OPENING;
  686. ch->cmux = circuitmux_alloc();
  687. channel_register(ch);
  688. tt_assert(ch->registered);
  689. /* Finish opening it */
  690. channel_change_state_open(ch);
  691. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  692. /* Go to maintenance state */
  693. channel_change_state(ch, CHANNEL_STATE_MAINT);
  694. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  695. /* Lower layer close */
  696. channel_close_from_lower_layer(ch);
  697. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  698. /* Finish */
  699. chan_test_finish_close(ch);
  700. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSED);
  701. channel_run_cleanup();
  702. ch = NULL;
  703. /* OPENING->OPEN->MAINT->CLOSING->ERROR */
  704. ch = new_fake_channel();
  705. tt_assert(ch);
  706. ch->state = CHANNEL_STATE_OPENING;
  707. ch->cmux = circuitmux_alloc();
  708. channel_register(ch);
  709. tt_assert(ch->registered);
  710. /* Finish opening it */
  711. channel_change_state_open(ch);
  712. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_OPEN);
  713. /* Go to maintenance state */
  714. channel_change_state(ch, CHANNEL_STATE_MAINT);
  715. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_MAINT);
  716. /* Lower layer close */
  717. chan_test_error(ch);
  718. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_CLOSING);
  719. /* Finish */
  720. chan_test_finish_close(ch);
  721. tt_int_op(ch->state, OP_EQ, CHANNEL_STATE_ERROR);
  722. channel_run_cleanup();
  723. ch = NULL;
  724. /* Shut down channels */
  725. channel_free_all();
  726. done:
  727. tor_free(ch);
  728. UNMOCK(scheduler_channel_doesnt_want_writes);
  729. UNMOCK(scheduler_release_channel);
  730. return;
  731. }
  732. static void
  733. test_channel_id_map(void *arg)
  734. {
  735. (void)arg;
  736. #define N_CHAN 6
  737. char rsa_id[N_CHAN][DIGEST_LEN];
  738. ed25519_public_key_t *ed_id[N_CHAN];
  739. channel_t *chan[N_CHAN];
  740. int i;
  741. ed25519_public_key_t ed_zero;
  742. memset(&ed_zero, 0, sizeof(ed_zero));
  743. tt_int_op(DIGEST_LEN, OP_EQ, sizeof(rsa_id[0])); // Do I remember C?
  744. for (i = 0; i < N_CHAN; ++i) {
  745. crypto_rand(rsa_id[i], DIGEST_LEN);
  746. ed_id[i] = tor_malloc_zero(sizeof(*ed_id[i]));
  747. crypto_rand((char*)ed_id[i]->pubkey, sizeof(ed_id[i]->pubkey));
  748. }
  749. /* For channel 3, have no Ed identity. */
  750. tor_free(ed_id[3]);
  751. /* Channel 2 and 4 have same ROSA identity */
  752. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  753. /* Channel 2 and 4 and 5 have same RSA identity */
  754. memcpy(rsa_id[4], rsa_id[2], DIGEST_LEN);
  755. memcpy(rsa_id[5], rsa_id[2], DIGEST_LEN);
  756. /* Channels 2 and 5 have same Ed25519 identity */
  757. memcpy(ed_id[5], ed_id[2], sizeof(*ed_id[2]));
  758. for (i = 0; i < N_CHAN; ++i) {
  759. chan[i] = new_fake_channel();
  760. channel_register(chan[i]);
  761. channel_set_identity_digest(chan[i], rsa_id[i], ed_id[i]);
  762. }
  763. /* Lookup by RSA id only */
  764. tt_ptr_op(chan[0], OP_EQ,
  765. channel_find_by_remote_identity(rsa_id[0], NULL));
  766. tt_ptr_op(chan[1], OP_EQ,
  767. channel_find_by_remote_identity(rsa_id[1], NULL));
  768. tt_ptr_op(chan[3], OP_EQ,
  769. channel_find_by_remote_identity(rsa_id[3], NULL));
  770. channel_t *ch;
  771. ch = channel_find_by_remote_identity(rsa_id[2], NULL);
  772. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  773. ch = channel_next_with_rsa_identity(ch);
  774. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  775. ch = channel_next_with_rsa_identity(ch);
  776. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  777. ch = channel_next_with_rsa_identity(ch);
  778. tt_ptr_op(ch, OP_EQ, NULL);
  779. /* As above, but with zero Ed25519 ID (meaning "any ID") */
  780. tt_ptr_op(chan[0], OP_EQ,
  781. channel_find_by_remote_identity(rsa_id[0], &ed_zero));
  782. tt_ptr_op(chan[1], OP_EQ,
  783. channel_find_by_remote_identity(rsa_id[1], &ed_zero));
  784. tt_ptr_op(chan[3], OP_EQ,
  785. channel_find_by_remote_identity(rsa_id[3], &ed_zero));
  786. ch = channel_find_by_remote_identity(rsa_id[2], &ed_zero);
  787. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  788. ch = channel_next_with_rsa_identity(ch);
  789. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  790. ch = channel_next_with_rsa_identity(ch);
  791. tt_assert(ch == chan[2] || ch == chan[4] || ch == chan[5]);
  792. ch = channel_next_with_rsa_identity(ch);
  793. tt_ptr_op(ch, OP_EQ, NULL);
  794. /* Lookup nonexistent RSA identity */
  795. tt_ptr_op(NULL, OP_EQ,
  796. channel_find_by_remote_identity("!!!!!!!!!!!!!!!!!!!!", NULL));
  797. /* Look up by full identity pair */
  798. tt_ptr_op(chan[0], OP_EQ,
  799. channel_find_by_remote_identity(rsa_id[0], ed_id[0]));
  800. tt_ptr_op(chan[1], OP_EQ,
  801. channel_find_by_remote_identity(rsa_id[1], ed_id[1]));
  802. tt_ptr_op(chan[3], OP_EQ,
  803. channel_find_by_remote_identity(rsa_id[3], ed_id[3] /*NULL*/));
  804. tt_ptr_op(chan[4], OP_EQ,
  805. channel_find_by_remote_identity(rsa_id[4], ed_id[4]));
  806. ch = channel_find_by_remote_identity(rsa_id[2], ed_id[2]);
  807. tt_assert(ch == chan[2] || ch == chan[5]);
  808. /* Look up RSA identity with wrong ed25519 identity */
  809. tt_ptr_op(NULL, OP_EQ,
  810. channel_find_by_remote_identity(rsa_id[4], ed_id[0]));
  811. tt_ptr_op(NULL, OP_EQ,
  812. channel_find_by_remote_identity(rsa_id[2], ed_id[1]));
  813. tt_ptr_op(NULL, OP_EQ,
  814. channel_find_by_remote_identity(rsa_id[3], ed_id[1]));
  815. done:
  816. for (i = 0; i < N_CHAN; ++i) {
  817. channel_clear_identity_digest(chan[i]);
  818. channel_unregister(chan[i]);
  819. free_fake_channel(chan[i]);
  820. tor_free(ed_id[i]);
  821. }
  822. #undef N_CHAN
  823. }
  824. struct testcase_t channel_tests[] = {
  825. { "inbound_cell", test_channel_inbound_cell, TT_FORK,
  826. NULL, NULL },
  827. { "id_map", test_channel_id_map, TT_FORK,
  828. NULL, NULL },
  829. /* NOT WORKING TEST. */
  830. { "dumpstats", test_channel_dumpstats, TT_FORK,
  831. NULL, NULL },
  832. { "lifecycle", test_channel_lifecycle, TT_FORK,
  833. NULL, NULL },
  834. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
  835. NULL, NULL },
  836. END_OF_TESTCASES
  837. };