test_channel.c 25 KB

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