test_channel.c 38 KB

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