test_channel.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. struct testcase_t channel_tests[] = {
  943. { "inbound_cell", test_channel_inbound_cell, TT_FORK,
  944. NULL, NULL },
  945. { "outbound_cell", test_channel_outbound_cell, TT_FORK,
  946. NULL, NULL },
  947. { "id_map", test_channel_id_map, TT_FORK,
  948. NULL, NULL },
  949. { "lifecycle", test_channel_lifecycle, TT_FORK,
  950. NULL, NULL },
  951. { "lifecycle_2", test_channel_lifecycle_2, TT_FORK,
  952. NULL, NULL },
  953. { "dumpstats", test_channel_dumpstats, TT_FORK,
  954. NULL, NULL },
  955. END_OF_TESTCASES
  956. };