test_channel.c 28 KB

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