test_channelpadding.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define MAINLOOP_PRIVATE
  5. #define NETWORKSTATUS_PRIVATE
  6. #define TOR_TIMERS_PRIVATE
  7. #include "core/or/or.h"
  8. #include "test/test.h"
  9. #include "lib/testsupport/testsupport.h"
  10. #include "core/mainloop/connection.h"
  11. #include "core/or/connection_or.h"
  12. #include "core/or/channel.h"
  13. #include "core/or/channeltls.h"
  14. #include "core/or/channelpadding.h"
  15. #include "lib/evloop/compat_libevent.h"
  16. #include "app/config/config.h"
  17. #include "lib/time/compat_time.h"
  18. #include "core/mainloop/mainloop.h"
  19. #include "feature/nodelist/networkstatus.h"
  20. #include "test/log_test_helpers.h"
  21. #include "lib/tls/tortls.h"
  22. #include "lib/evloop/timers.h"
  23. #include "lib/container/buffers.h"
  24. #include "core/or/cell_st.h"
  25. #include "feature/nodelist/networkstatus_st.h"
  26. #include "core/or/or_connection_st.h"
  27. #include "feature/nodelist/routerstatus_st.h"
  28. int channelpadding_get_netflow_inactive_timeout_ms(channel_t *chan);
  29. int64_t channelpadding_compute_time_until_pad_for_netflow(channel_t *chan);
  30. int channelpadding_send_disable_command(channel_t*);
  31. int channelpadding_find_timerslot(channel_t *chan);
  32. void test_channelpadding_timers(void *arg);
  33. void test_channelpadding_consensus(void *arg);
  34. void test_channelpadding_negotiation(void *arg);
  35. void test_channelpadding_decide_to_pad_channel(void *arg);
  36. void test_channelpadding_killonehop(void *arg);
  37. void dummy_nop_timer(void);
  38. #define NSEC_PER_MSEC (1000*1000)
  39. /* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */
  40. static int fake_tortls = 0; /* Bleh... */
  41. static int dont_stop_libevent = 0;
  42. // From test_channel.c
  43. channel_t * new_fake_channel(void);
  44. void free_fake_channel(channel_t*);
  45. static int
  46. mock_channel_has_queued_writes(channel_t *chan)
  47. {
  48. (void)chan;
  49. return 0;
  50. }
  51. static int tried_to_write_cell = 0;
  52. static channel_t *relay1_relay2;
  53. static channel_t *relay2_relay1;
  54. static channel_t *relay3_client;
  55. static channel_t *client_relay3;
  56. static int
  57. mock_channel_write_cell_relay2(channel_t *chan, cell_t *cell)
  58. {
  59. (void)chan;
  60. tried_to_write_cell++;
  61. channel_tls_handle_cell(cell, ((channel_tls_t*)relay1_relay2)->conn);
  62. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  63. return 0;
  64. }
  65. static int
  66. mock_channel_write_cell_relay1(channel_t *chan, cell_t *cell)
  67. {
  68. (void)chan;
  69. tried_to_write_cell++;
  70. channel_tls_handle_cell(cell, ((channel_tls_t*)relay2_relay1)->conn);
  71. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  72. return 0;
  73. }
  74. static int
  75. mock_channel_write_cell_relay3(channel_t *chan, cell_t *cell)
  76. {
  77. (void)chan;
  78. tried_to_write_cell++;
  79. channel_tls_handle_cell(cell, ((channel_tls_t*)client_relay3)->conn);
  80. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  81. return 0;
  82. }
  83. static int
  84. mock_channel_write_cell_client(channel_t *chan, cell_t *cell)
  85. {
  86. (void)chan;
  87. tried_to_write_cell++;
  88. channel_tls_handle_cell(cell, ((channel_tls_t*)relay3_client)->conn);
  89. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  90. return 0;
  91. }
  92. static int
  93. mock_channel_write_cell(channel_t *chan, cell_t *cell)
  94. {
  95. tried_to_write_cell++;
  96. channel_tls_handle_cell(cell, ((channel_tls_t*)chan)->conn);
  97. if (!dont_stop_libevent)
  98. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  99. return 0;
  100. }
  101. static void
  102. setup_fake_connection_for_channel(channel_tls_t *chan)
  103. {
  104. or_connection_t *conn = (or_connection_t*)connection_new(CONN_TYPE_OR,
  105. AF_INET);
  106. conn->base_.conn_array_index = smartlist_len(connection_array);
  107. smartlist_add(connection_array, conn);
  108. conn->chan = chan;
  109. chan->conn = conn;
  110. conn->base_.magic = OR_CONNECTION_MAGIC;
  111. conn->base_.state = OR_CONN_STATE_OPEN;
  112. conn->base_.type = CONN_TYPE_OR;
  113. conn->base_.socket_family = AF_INET;
  114. conn->base_.address = tor_strdup("<fake>");
  115. conn->base_.port = 4242;
  116. conn->tls = (tor_tls_t *)((void *)(&fake_tortls));
  117. conn->link_proto = MIN_LINK_PROTO_FOR_CHANNEL_PADDING;
  118. connection_or_set_canonical(conn, 1);
  119. }
  120. static channel_tls_t *
  121. new_fake_channeltls(uint8_t id)
  122. {
  123. channel_tls_t *chan = tor_realloc(new_fake_channel(), sizeof(channel_tls_t));
  124. chan->base_.magic = TLS_CHAN_MAGIC;
  125. setup_fake_connection_for_channel(chan);
  126. chan->base_.channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  127. chan->base_.has_queued_writes = mock_channel_has_queued_writes;
  128. chan->base_.write_cell = mock_channel_write_cell;
  129. chan->base_.padding_enabled = 1;
  130. chan->base_.identity_digest[0] = id;
  131. channel_register(&chan->base_);
  132. return chan;
  133. }
  134. static void
  135. free_fake_channeltls(channel_tls_t *chan)
  136. {
  137. channel_unregister(&chan->base_);
  138. tor_free(((channel_tls_t*)chan)->conn->base_.address);
  139. buf_free(((channel_tls_t*)chan)->conn->base_.inbuf);
  140. buf_free(((channel_tls_t*)chan)->conn->base_.outbuf);
  141. tor_free(((channel_tls_t*)chan)->conn);
  142. timer_free(chan->base_.padding_timer);
  143. channel_handle_free(chan->base_.timer_handle);
  144. channel_handles_clear(&chan->base_);
  145. free_fake_channel(&chan->base_);
  146. return;
  147. }
  148. static void
  149. setup_mock_consensus(void)
  150. {
  151. current_md_consensus = current_ns_consensus
  152. = tor_malloc_zero(sizeof(networkstatus_t));
  153. current_md_consensus->net_params = smartlist_new();
  154. current_md_consensus->routerstatus_list = smartlist_new();
  155. channelpadding_new_consensus_params(current_md_consensus);
  156. }
  157. static void
  158. free_mock_consensus(void)
  159. {
  160. SMARTLIST_FOREACH(current_md_consensus->routerstatus_list, void *, r,
  161. tor_free(r));
  162. smartlist_free(current_md_consensus->routerstatus_list);
  163. smartlist_free(current_ns_consensus->net_params);
  164. tor_free(current_ns_consensus);
  165. }
  166. static void
  167. setup_mock_network(void)
  168. {
  169. routerstatus_t *relay;
  170. if (!connection_array)
  171. connection_array = smartlist_new();
  172. relay1_relay2 = (channel_t*)new_fake_channeltls(2);
  173. relay1_relay2->write_cell = mock_channel_write_cell_relay1;
  174. channel_timestamp_active(relay1_relay2);
  175. relay = tor_malloc_zero(sizeof(routerstatus_t));
  176. relay->identity_digest[0] = 1;
  177. smartlist_add(current_md_consensus->routerstatus_list, relay);
  178. relay2_relay1 = (channel_t*)new_fake_channeltls(1);
  179. relay2_relay1->write_cell = mock_channel_write_cell_relay2;
  180. channel_timestamp_active(relay2_relay1);
  181. relay = tor_malloc_zero(sizeof(routerstatus_t));
  182. relay->identity_digest[0] = 2;
  183. smartlist_add(current_md_consensus->routerstatus_list, relay);
  184. relay3_client = (channel_t*)new_fake_channeltls(0);
  185. relay3_client->write_cell = mock_channel_write_cell_relay3;
  186. relay3_client->is_client = 1;
  187. channel_timestamp_active(relay3_client);
  188. relay = tor_malloc_zero(sizeof(routerstatus_t));
  189. relay->identity_digest[0] = 3;
  190. smartlist_add(current_md_consensus->routerstatus_list, relay);
  191. client_relay3 = (channel_t*)new_fake_channeltls(3);
  192. client_relay3->write_cell = mock_channel_write_cell_client;
  193. channel_timestamp_active(client_relay3);
  194. channel_do_open_actions(relay1_relay2);
  195. channel_do_open_actions(relay2_relay1);
  196. channel_do_open_actions(relay3_client);
  197. channel_do_open_actions(client_relay3);
  198. }
  199. static void
  200. free_mock_network(void)
  201. {
  202. free_fake_channeltls((channel_tls_t*)relay1_relay2);
  203. free_fake_channeltls((channel_tls_t*)relay2_relay1);
  204. free_fake_channeltls((channel_tls_t*)relay3_client);
  205. free_fake_channeltls((channel_tls_t*)client_relay3);
  206. smartlist_free(connection_array);
  207. }
  208. static void
  209. dummy_timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
  210. {
  211. (void)t; (void)arg; (void)now_mono;
  212. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  213. return;
  214. }
  215. // This hack adds a dummy timer so that the libevent base loop
  216. // actually returns when we don't expect any timers to fire. Otherwise,
  217. // the global_timer_event gets scheduled an hour from now, and the
  218. // base loop never returns.
  219. void
  220. dummy_nop_timer(void)
  221. {
  222. tor_timer_t *dummy_timer = timer_new(dummy_timer_cb, NULL);
  223. struct timeval timeout;
  224. timeout.tv_sec = 1;
  225. timeout.tv_usec = 0;
  226. timer_schedule(dummy_timer, &timeout);
  227. tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
  228. timer_free(dummy_timer);
  229. }
  230. #define CHANNELPADDING_MAX_TIMERS 25
  231. #define CHANNELS_TO_TEST (CHANNELPADDING_MAX_TIMERS*4)
  232. /**
  233. * Tests to ensure that we handle more than the max number of pending
  234. * timers properly.
  235. */
  236. void
  237. test_channelpadding_timers(void *arg)
  238. {
  239. channelpadding_decision_t decision;
  240. channel_t *chans[CHANNELS_TO_TEST];
  241. (void)arg;
  242. tor_libevent_postfork();
  243. if (!connection_array)
  244. connection_array = smartlist_new();
  245. monotime_init();
  246. monotime_enable_test_mocking();
  247. uint64_t nsec_mock = 1;
  248. monotime_set_mock_time_nsec(nsec_mock);
  249. monotime_coarse_set_mock_time_nsec(nsec_mock);
  250. timers_initialize();
  251. channelpadding_new_consensus_params(NULL);
  252. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  253. chans[i] = (channel_t*)new_fake_channeltls(0);
  254. channel_timestamp_active(chans[i]);
  255. }
  256. for (int j = 0; j < 2; j++) {
  257. tried_to_write_cell = 0;
  258. int i = 0;
  259. monotime_coarse_t now;
  260. monotime_coarse_get(&now);
  261. /* This loop fills our timerslot array with timers of increasing time
  262. * until they fire */
  263. for (; i < CHANNELPADDING_MAX_TIMERS; i++) {
  264. monotime_coarse_add_msec(&chans[i]->next_padding_time,
  265. &now, 10 + i*4);
  266. decision = channelpadding_decide_to_pad_channel(chans[i]);
  267. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  268. tt_assert(chans[i]->pending_padding_callback);
  269. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  270. }
  271. /* This loop should add timers to the first position in the timerslot
  272. * array, since its timeout is before all other timers. */
  273. for (; i < CHANNELS_TO_TEST/3; i++) {
  274. monotime_coarse_add_msec(&chans[i]->next_padding_time,
  275. &now, 1);
  276. decision = channelpadding_decide_to_pad_channel(chans[i]);
  277. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  278. tt_assert(chans[i]->pending_padding_callback);
  279. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  280. }
  281. /* This loop should add timers to our existing lists in a weak
  282. * pseudorandom pattern. It ensures that the lists can grow with multiple
  283. * timers in them. */
  284. for (; i < CHANNELS_TO_TEST/2; i++) {
  285. monotime_coarse_add_msec(&chans[i]->next_padding_time,
  286. &now, 10 + i*3 % CHANNELPADDING_MAX_TIMERS);
  287. decision = channelpadding_decide_to_pad_channel(chans[i]);
  288. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  289. tt_assert(chans[i]->pending_padding_callback);
  290. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  291. }
  292. /* This loop should add timers to the last position in the timerslot
  293. * array, since its timeout is after all other timers. */
  294. for (; i < CHANNELS_TO_TEST; i++) {
  295. monotime_coarse_add_msec(&chans[i]->next_padding_time,
  296. &now, 500 + i % CHANNELPADDING_MAX_TIMERS);
  297. decision = channelpadding_decide_to_pad_channel(chans[i]);
  298. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  299. tt_assert(chans[i]->pending_padding_callback);
  300. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  301. }
  302. // Wait for the timers and then kill the event loop.
  303. nsec_mock += 1001 * NSEC_PER_MSEC;
  304. monotime_coarse_set_mock_time_nsec(nsec_mock);
  305. monotime_set_mock_time_nsec(nsec_mock);
  306. timers_run_pending();
  307. tt_int_op(tried_to_write_cell, OP_EQ, CHANNELS_TO_TEST);
  308. // Test that we have no pending callbacks and all empty slots now
  309. for (i = 0; i < CHANNELS_TO_TEST; i++) {
  310. tt_assert(!chans[i]->pending_padding_callback);
  311. }
  312. }
  313. done:
  314. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  315. free_fake_channeltls((channel_tls_t*)chans[i]);
  316. }
  317. smartlist_free(connection_array);
  318. timers_shutdown();
  319. monotime_disable_test_mocking();
  320. channel_free_all();
  321. return;
  322. }
  323. void
  324. test_channelpadding_killonehop(void *arg)
  325. {
  326. channelpadding_decision_t decision;
  327. int64_t new_time;
  328. (void)arg;
  329. tor_libevent_postfork();
  330. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  331. monotime_init();
  332. monotime_enable_test_mocking();
  333. monotime_set_mock_time_nsec(1);
  334. monotime_coarse_set_mock_time_nsec(1);
  335. new_time = 1;
  336. timers_initialize();
  337. setup_mock_consensus();
  338. setup_mock_network();
  339. /* Do we disable padding if rsos is enabled, and the consensus says don't
  340. * pad? */
  341. monotime_coarse_t now;
  342. monotime_coarse_get(&now);
  343. // First, test that padding works if either is enabled
  344. smartlist_clear(current_md_consensus->net_params);
  345. channelpadding_new_consensus_params(current_md_consensus);
  346. relay3_client->padding_enabled = 1;
  347. client_relay3->padding_enabled = 1;
  348. tried_to_write_cell = 0;
  349. get_options_mutable()->ORPort_set = 0;
  350. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  351. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  352. monotime_coarse_add_msec(&client_relay3->next_padding_time, &now, 100);
  353. decision = channelpadding_decide_to_pad_channel(client_relay3);
  354. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  355. tt_assert(client_relay3->pending_padding_callback);
  356. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  357. decision = channelpadding_decide_to_pad_channel(client_relay3);
  358. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  359. // Wait for the timer
  360. new_time += 101 * NSEC_PER_MSEC;
  361. monotime_coarse_set_mock_time_nsec(new_time);
  362. monotime_set_mock_time_nsec(new_time);
  363. monotime_coarse_get(&now);
  364. timers_run_pending();
  365. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  366. tt_assert(!client_relay3->pending_padding_callback);
  367. // Then test disabling each via consensus param
  368. smartlist_add(current_md_consensus->net_params,
  369. (void*)"nf_pad_single_onion=0");
  370. channelpadding_new_consensus_params(current_md_consensus);
  371. // Before the client tries to pad, the relay will still pad:
  372. tried_to_write_cell = 0;
  373. monotime_coarse_add_msec(&relay3_client->next_padding_time, &now, 100);
  374. get_options_mutable()->ORPort_set = 1;
  375. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  376. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  377. decision = channelpadding_decide_to_pad_channel(relay3_client);
  378. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  379. tt_assert(relay3_client->pending_padding_callback);
  380. // Wait for the timer
  381. new_time += 101 * NSEC_PER_MSEC;
  382. monotime_coarse_set_mock_time_nsec(new_time);
  383. monotime_set_mock_time_nsec(new_time);
  384. monotime_coarse_get(&now);
  385. timers_run_pending();
  386. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  387. tt_assert(!client_relay3->pending_padding_callback);
  388. // Test client side (it should stop immediately)
  389. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  390. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  391. /* For the relay to receive the negotiate: */
  392. get_options_mutable()->ORPort_set = 1;
  393. decision = channelpadding_decide_to_pad_channel(client_relay3);
  394. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  395. tt_assert(!client_relay3->pending_padding_callback);
  396. // Test relay side (it should have gotten the negotiation to disable)
  397. get_options_mutable()->ORPort_set = 1;
  398. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  399. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  400. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  401. CHANNELPADDING_WONTPAD);
  402. tt_assert(!relay3_client->padding_enabled);
  403. done:
  404. free_mock_consensus();
  405. free_mock_network();
  406. tor_free(relay);
  407. timers_shutdown();
  408. monotime_disable_test_mocking();
  409. channel_free_all();
  410. }
  411. void
  412. test_channelpadding_consensus(void *arg)
  413. {
  414. channelpadding_decision_t decision;
  415. or_options_t *options = get_options_mutable();
  416. int64_t val;
  417. int64_t new_time;
  418. (void)arg;
  419. tor_libevent_postfork();
  420. /*
  421. * Params tested:
  422. * nf_pad_before_usage
  423. * nf_pad_relays
  424. * nf_ito_low
  425. * nf_ito_high
  426. *
  427. * Plan:
  428. * 1. Padding can be completely disabled via consensus
  429. * 2. Negotiation can't re-enable consensus-disabled padding
  430. * 3. Negotiation can't increase padding from relays beyond
  431. * consensus defaults
  432. * 4. Relay-to-relay padding can be enabled/disabled in consensus
  433. * 5. Can enable/disable padding before actually using a connection
  434. * 6. Can we control circ and TLS conn lifetime from the consensus?
  435. */
  436. channel_t *chan;
  437. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  438. monotime_enable_test_mocking();
  439. monotime_set_mock_time_nsec(1);
  440. monotime_coarse_set_mock_time_nsec(1);
  441. new_time = 1;
  442. monotime_coarse_t now;
  443. monotime_coarse_get(&now);
  444. timers_initialize();
  445. if (!connection_array)
  446. connection_array = smartlist_new();
  447. chan = (channel_t*)new_fake_channeltls(0);
  448. channel_timestamp_active(chan);
  449. setup_mock_consensus();
  450. get_options_mutable()->ORPort_set = 1;
  451. /* Test 1: Padding can be completely disabled via consensus */
  452. tried_to_write_cell = 0;
  453. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  454. decision = channelpadding_decide_to_pad_channel(chan);
  455. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  456. tt_assert(chan->pending_padding_callback);
  457. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  458. decision = channelpadding_decide_to_pad_channel(chan);
  459. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  460. // Wait for the timer
  461. new_time += 101*NSEC_PER_MSEC;
  462. monotime_coarse_set_mock_time_nsec(new_time);
  463. monotime_set_mock_time_nsec(new_time);
  464. monotime_coarse_get(&now);
  465. timers_run_pending();
  466. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  467. tt_assert(!chan->pending_padding_callback);
  468. smartlist_add(current_md_consensus->net_params,
  469. (void*)"nf_ito_low=0");
  470. smartlist_add(current_md_consensus->net_params,
  471. (void*)"nf_ito_high=0");
  472. get_options_mutable()->ConnectionPadding = 1;
  473. channelpadding_new_consensus_params(current_md_consensus);
  474. decision = channelpadding_decide_to_pad_channel(chan);
  475. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  476. tt_assert(!chan->pending_padding_callback);
  477. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  478. tt_i64_op(val, OP_EQ, 0);
  479. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  480. tt_i64_op(val, OP_EQ, -2);
  481. /* Test 2: Negotiation can't re-enable consensus-disabled padding */
  482. channelpadding_send_enable_command(chan, 100, 200);
  483. tried_to_write_cell = 0;
  484. decision = channelpadding_decide_to_pad_channel(chan);
  485. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  486. tt_assert(!chan->pending_padding_callback);
  487. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  488. tt_i64_op(val, OP_EQ, 0);
  489. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  490. tt_i64_op(val, OP_EQ, -2);
  491. tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
  492. smartlist_clear(current_md_consensus->net_params);
  493. /* Test 3: Negotiation can't increase padding from relays beyond consensus
  494. * values */
  495. smartlist_add(current_md_consensus->net_params,
  496. (void*)"nf_ito_low=100");
  497. smartlist_add(current_md_consensus->net_params,
  498. (void*)"nf_ito_high=200");
  499. channelpadding_new_consensus_params(current_md_consensus);
  500. tried_to_write_cell = 0;
  501. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  502. decision = channelpadding_decide_to_pad_channel(chan);
  503. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  504. tt_assert(chan->pending_padding_callback);
  505. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  506. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  507. tt_i64_op(val, OP_GE, 100);
  508. tt_i64_op(val, OP_LE, 200);
  509. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  510. tt_i64_op(val, OP_LE, 200);
  511. // Wait for the timer
  512. new_time += 201*NSEC_PER_MSEC;
  513. monotime_set_mock_time_nsec(new_time);
  514. monotime_coarse_set_mock_time_nsec(new_time);
  515. monotime_coarse_get(&now);
  516. timers_run_pending();
  517. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  518. tt_assert(!chan->pending_padding_callback);
  519. smartlist_clear(current_md_consensus->net_params);
  520. smartlist_add(current_md_consensus->net_params,
  521. (void*)"nf_ito_low=1500");
  522. smartlist_add(current_md_consensus->net_params,
  523. (void*)"nf_ito_high=4500");
  524. channelpadding_new_consensus_params(current_md_consensus);
  525. channelpadding_send_enable_command(chan, 100, 200);
  526. tried_to_write_cell = 0;
  527. decision = channelpadding_decide_to_pad_channel(chan);
  528. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  529. tt_assert(!chan->pending_padding_callback);
  530. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  531. tt_i64_op(val, OP_GE, 1500);
  532. tt_i64_op(val, OP_LE, 4500);
  533. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  534. tt_i64_op(val, OP_LE, 4500);
  535. /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
  536. /* Make this channel a relay's channel */
  537. memcpy(relay->identity_digest,
  538. ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
  539. smartlist_add(current_md_consensus->routerstatus_list, relay);
  540. relay = NULL; /* Prevent double-free */
  541. tried_to_write_cell = 0;
  542. decision = channelpadding_decide_to_pad_channel(chan);
  543. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  544. tt_assert(!chan->pending_padding_callback);
  545. smartlist_add(current_md_consensus->net_params,
  546. (void*)"nf_pad_relays=1");
  547. channelpadding_new_consensus_params(current_md_consensus);
  548. decision = channelpadding_decide_to_pad_channel(chan);
  549. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  550. tt_assert(!chan->pending_padding_callback);
  551. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  552. tt_i64_op(val, OP_GE, 1500);
  553. tt_i64_op(val, OP_LE, 4500);
  554. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  555. tt_i64_op(val, OP_LE, 4500);
  556. /* Test 5: If we disable padding before channel usage, does that work? */
  557. smartlist_add(current_md_consensus->net_params,
  558. (void*)"nf_pad_before_usage=0");
  559. channelpadding_new_consensus_params(current_md_consensus);
  560. tried_to_write_cell = 0;
  561. decision = channelpadding_decide_to_pad_channel(chan);
  562. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  563. tt_assert(!chan->pending_padding_callback);
  564. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  565. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  566. tt_i64_op(val, OP_GE, 180);
  567. tt_i64_op(val, OP_LE, 180+90);
  568. val = channelpadding_get_channel_idle_timeout(chan, 0);
  569. tt_i64_op(val, OP_GE, 180);
  570. tt_i64_op(val, OP_LE, 180+90);
  571. options->ReducedConnectionPadding = 1;
  572. val = channelpadding_get_channel_idle_timeout(chan, 0);
  573. tt_i64_op(val, OP_GE, 180/2);
  574. tt_i64_op(val, OP_LE, (180+90)/2);
  575. options->ReducedConnectionPadding = 0;
  576. options->ORPort_set = 1;
  577. smartlist_add(current_md_consensus->net_params,
  578. (void*)"nf_conntimeout_relays=600");
  579. channelpadding_new_consensus_params(current_md_consensus);
  580. val = channelpadding_get_channel_idle_timeout(chan, 1);
  581. tt_i64_op(val, OP_GE, 450);
  582. tt_i64_op(val, OP_LE, 750);
  583. val = channelpadding_get_circuits_available_timeout();
  584. tt_i64_op(val, OP_GE, 30*60);
  585. tt_i64_op(val, OP_LE, 30*60*2);
  586. options->ReducedConnectionPadding = 1;
  587. smartlist_add(current_md_consensus->net_params,
  588. (void*)"nf_conntimeout_clients=600");
  589. channelpadding_new_consensus_params(current_md_consensus);
  590. val = channelpadding_get_circuits_available_timeout();
  591. tt_i64_op(val, OP_GE, 600/2);
  592. tt_i64_op(val, OP_LE, 600*2/2);
  593. options->ReducedConnectionPadding = 0;
  594. options->CircuitsAvailableTimeout = 24*60*60;
  595. val = channelpadding_get_circuits_available_timeout();
  596. tt_i64_op(val, OP_GE, 24*60*60);
  597. tt_i64_op(val, OP_LE, 24*60*60*2);
  598. done:
  599. tor_free(relay);
  600. free_mock_consensus();
  601. free_fake_channeltls((channel_tls_t*)chan);
  602. smartlist_free(connection_array);
  603. timers_shutdown();
  604. monotime_disable_test_mocking();
  605. channel_free_all();
  606. return;
  607. }
  608. void
  609. test_channelpadding_negotiation(void *arg)
  610. {
  611. channelpadding_negotiate_t disable;
  612. cell_t cell;
  613. channelpadding_decision_t decision;
  614. int val;
  615. (void)arg;
  616. /* Plan:
  617. * 1. Clients reject negotiation, relays accept it.
  618. * * Bridges accept negotiation from their clients,
  619. * but not from relays.
  620. * 2. Torrc options can override client-side negotiation
  621. * 3. Test a version issue in channelpadidng cell
  622. * 4. Test channelpadding_reduced_padding
  623. */
  624. monotime_init();
  625. monotime_enable_test_mocking();
  626. monotime_set_mock_time_nsec(1);
  627. monotime_coarse_set_mock_time_nsec(1);
  628. timers_initialize();
  629. setup_mock_consensus();
  630. setup_mock_network();
  631. /* Test case #1: Do the right things ignore negotiation? */
  632. /* relay-to-client case: */
  633. channelpadding_send_disable_command(relay3_client);
  634. tt_assert(client_relay3->padding_enabled);
  635. /* client-to-relay case: */
  636. get_options_mutable()->ORPort_set = 1;
  637. channelpadding_disable_padding_on_channel(client_relay3);
  638. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  639. CHANNELPADDING_WONTPAD);
  640. tt_assert(!relay3_client->padding_enabled);
  641. relay3_client->padding_enabled = 1;
  642. client_relay3->padding_enabled = 1;
  643. /* Bridge case from relay */
  644. get_options_mutable()->BridgeRelay = 1;
  645. channelpadding_disable_padding_on_channel(relay2_relay1);
  646. tt_assert(relay1_relay2->padding_enabled);
  647. /* Bridge case from client */
  648. channelpadding_disable_padding_on_channel(client_relay3);
  649. tt_assert(!relay3_client->padding_enabled);
  650. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  651. CHANNELPADDING_WONTPAD);
  652. relay3_client->padding_enabled = 1;
  653. client_relay3->padding_enabled = 1;
  654. get_options_mutable()->BridgeRelay = 0;
  655. get_options_mutable()->ORPort_set = 0;
  656. /* Test case #2: Torrc options */
  657. /* ConnectionPadding auto; Relay doesn't support us */
  658. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  659. relay3_client->padding_enabled = 0;
  660. tried_to_write_cell = 0;
  661. decision = channelpadding_decide_to_pad_channel(relay3_client);
  662. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  663. tt_assert(!relay3_client->pending_padding_callback);
  664. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  665. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  666. relay3_client->padding_enabled = 1;
  667. /* ConnectionPadding 1; Relay doesn't support us */
  668. get_options_mutable()->ConnectionPadding = 1;
  669. tried_to_write_cell = 0;
  670. decision = channelpadding_decide_to_pad_channel(client_relay3);
  671. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  672. tt_assert(!client_relay3->pending_padding_callback);
  673. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  674. get_options_mutable()->ConnectionPadding = 0;
  675. /* Test case #3: Test a version issue in channelpadding cell */
  676. get_options_mutable()->ORPort_set = 1;
  677. client_relay3->padding_enabled = 1;
  678. relay3_client->padding_enabled = 1;
  679. memset(&cell, 0, sizeof(cell_t));
  680. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  681. cell.command = CELL_PADDING_NEGOTIATE;
  682. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  683. disable.version = 1;
  684. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  685. client_relay3->write_cell(client_relay3, &cell);
  686. tt_assert(relay3_client->padding_enabled);
  687. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  688. OP_EQ, -1);
  689. tt_assert(client_relay3->padding_enabled);
  690. disable.version = 0;
  691. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  692. client_relay3->write_cell(client_relay3, &cell);
  693. tt_assert(!relay3_client->padding_enabled);
  694. /* Test case 4: Reducing padding actually reduces it */
  695. relay3_client->padding_enabled = 1;
  696. client_relay3->padding_enabled = 1;
  697. decision = channelpadding_decide_to_pad_channel(relay3_client);
  698. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  699. channelpadding_reduce_padding_on_channel(client_relay3);
  700. tried_to_write_cell = 0;
  701. decision = channelpadding_decide_to_pad_channel(relay3_client);
  702. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  703. get_options_mutable()->ORPort_set = 0;
  704. decision = channelpadding_decide_to_pad_channel(client_relay3);
  705. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  706. tt_assert(!client_relay3->pending_padding_callback);
  707. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  708. tt_int_op(val, OP_GE, 9000);
  709. tt_int_op(val, OP_LE, 14000);
  710. int64_t val64 =
  711. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  712. tt_i64_op(val64, OP_LE, 14000);
  713. done:
  714. free_mock_network();
  715. free_mock_consensus();
  716. timers_shutdown();
  717. monotime_disable_test_mocking();
  718. channel_free_all();
  719. return;
  720. }
  721. void
  722. test_channelpadding_decide_to_pad_channel(void *arg)
  723. {
  724. channelpadding_decision_t decision;
  725. /**
  726. * Test case plan:
  727. *
  728. * 1. Channel that has "sent a packet" before the timeout.
  729. * + We should decide to pad later
  730. * 2. Channel that has not "sent a packet" before the timeout:
  731. * 2a. Not within 1.1s of the timeout.
  732. * + We should decide to pad later
  733. * 2b. Within 1.1s of the timemout.
  734. * + We should schedule padding
  735. * + We should get feedback that we wrote a cell
  736. * 2c. Within 0.1s of the timeout.
  737. * + We should schedule padding
  738. * + We should get feedback that we wrote a cell
  739. * 2d. Channel that asks to pad while timeout is scheduled
  740. * + We should schedule padding
  741. * + We should get feedback that we wrote a cell
  742. * 2e. 0s of the timeout
  743. * + We should send padding immediately
  744. * + We should get feedback that we wrote a cell
  745. * 2f. <0s of the timeout
  746. * + We should send padding immediately
  747. * + We should get feedback that we wrote a cell
  748. * 3. Channel that sends a packet while timeout is scheduled
  749. * + We should not get feedback that we wrote a cell
  750. * 4. Channel that closes while timeout is scheduled
  751. * + We should not get feedback that we wrote a cell
  752. * 5. Make sure the channel still would work if repaired
  753. * + We should be able to schedule padding and resend
  754. * 6. Channel is not used for full circuits
  755. * 7. Channel that disappears while timeout is scheduled
  756. * + We should not send padding
  757. */
  758. channel_t *chan;
  759. int64_t new_time;
  760. if (!connection_array)
  761. connection_array = smartlist_new();
  762. (void)arg;
  763. tor_libevent_postfork();
  764. monotime_init();
  765. monotime_enable_test_mocking();
  766. monotime_set_mock_time_nsec(1);
  767. monotime_coarse_set_mock_time_nsec(1);
  768. new_time = 1;
  769. monotime_coarse_t now;
  770. monotime_coarse_get(&now);
  771. timers_initialize();
  772. setup_full_capture_of_logs(LOG_WARN);
  773. channelpadding_new_consensus_params(NULL);
  774. chan = (channel_t*)new_fake_channeltls(0);
  775. channel_timestamp_active(chan);
  776. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  777. tried_to_write_cell = 0;
  778. decision = channelpadding_decide_to_pad_channel(chan);
  779. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  780. tt_assert(!chan->pending_padding_callback);
  781. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  782. /* Test case #2a: > 1.1s until timeout */
  783. tried_to_write_cell = 0;
  784. monotime_coarse_add_msec(&chan->next_padding_time, &now, 1200);
  785. decision = channelpadding_decide_to_pad_channel(chan);
  786. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  787. tt_assert(!chan->pending_padding_callback);
  788. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  789. /* Test case #2b: >= 1.0s until timeout */
  790. tried_to_write_cell = 0;
  791. monotime_coarse_add_msec(&chan->next_padding_time, &now, 1000);
  792. decision = channelpadding_decide_to_pad_channel(chan);
  793. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  794. tt_assert(chan->pending_padding_callback);
  795. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  796. // Set up a timer for the <0 case below.
  797. monotime_coarse_t now_minus_100s;
  798. monotime_coarse_add_msec(&now_minus_100s, &now, 900);
  799. // Wait for the timer from case #2b
  800. new_time += 1000*NSEC_PER_MSEC;
  801. monotime_set_mock_time_nsec(new_time);
  802. monotime_coarse_set_mock_time_nsec(new_time);
  803. monotime_coarse_get(&now);
  804. timers_run_pending();
  805. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  806. tt_assert(!chan->pending_padding_callback);
  807. /* Test case #2c: > 0.1s until timeout */
  808. tried_to_write_cell = 0;
  809. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  810. decision = channelpadding_decide_to_pad_channel(chan);
  811. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  812. tt_assert(chan->pending_padding_callback);
  813. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  814. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  815. decision = channelpadding_decide_to_pad_channel(chan);
  816. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  817. // Wait for the timer
  818. new_time += 101*NSEC_PER_MSEC;
  819. monotime_coarse_set_mock_time_nsec(new_time);
  820. monotime_set_mock_time_nsec(new_time);
  821. monotime_coarse_get(&now);
  822. timers_run_pending();
  823. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  824. tt_assert(!chan->pending_padding_callback);
  825. /* Test case #2e: 0s until timeout */
  826. tried_to_write_cell = 0;
  827. monotime_coarse_add_msec(&chan->next_padding_time, &now, 0);
  828. decision = channelpadding_decide_to_pad_channel(chan);
  829. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  830. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  831. tt_assert(!chan->pending_padding_callback);
  832. /* Test case #2f: <0s until timeout */
  833. tried_to_write_cell = 0;
  834. monotime_coarse_add_msec(&chan->next_padding_time, &now_minus_100s, 0);
  835. decision = channelpadding_decide_to_pad_channel(chan);
  836. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  837. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  838. tt_assert(!chan->pending_padding_callback);
  839. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  840. tried_to_write_cell = 0;
  841. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  842. decision = channelpadding_decide_to_pad_channel(chan);
  843. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  844. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  845. tt_assert(chan->pending_padding_callback);
  846. // Pretend the channel sent a packet
  847. channel_timestamp_active(chan);
  848. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  849. // Wait for the timer
  850. new_time += 101*NSEC_PER_MSEC;
  851. monotime_coarse_set_mock_time_nsec(new_time);
  852. monotime_set_mock_time_nsec(new_time);
  853. monotime_coarse_get(&now);
  854. timers_run_pending();
  855. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  856. tt_assert(!chan->pending_padding_callback);
  857. /* Test case #4: Channel that closes while a timeout is scheduled */
  858. tried_to_write_cell = 0;
  859. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  860. decision = channelpadding_decide_to_pad_channel(chan);
  861. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  862. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  863. tt_assert(chan->pending_padding_callback);
  864. // Pretend the channel is temporarily down
  865. chan->state = CHANNEL_STATE_MAINT;
  866. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  867. new_time += 101*NSEC_PER_MSEC;
  868. monotime_coarse_set_mock_time_nsec(new_time);
  869. monotime_set_mock_time_nsec(new_time);
  870. monotime_coarse_get(&now);
  871. timers_run_pending();
  872. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  873. tt_assert(!chan->pending_padding_callback);
  874. chan->state = CHANNEL_STATE_OPEN;
  875. /* Test case #5: Make sure previous test case didn't break everything */
  876. tried_to_write_cell = 0;
  877. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  878. decision = channelpadding_decide_to_pad_channel(chan);
  879. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  880. tt_assert(chan->pending_padding_callback);
  881. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  882. // Wait for the timer
  883. new_time += 101*NSEC_PER_MSEC;
  884. monotime_coarse_set_mock_time_nsec(new_time);
  885. monotime_set_mock_time_nsec(new_time);
  886. monotime_coarse_get(&now);
  887. timers_run_pending();
  888. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  889. tt_assert(!chan->pending_padding_callback);
  890. /* Test case #6. Channel is not used for full circuits */
  891. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  892. decision = channelpadding_decide_to_pad_channel(chan);
  893. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  894. tt_assert(!chan->pending_padding_callback);
  895. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  896. /* Test case #7. Channel is closed while timeout is scheduled.
  897. *
  898. * NOTE: This test deliberately breaks the channel callback mechanism.
  899. * It must be last.
  900. */
  901. tried_to_write_cell = 0;
  902. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  903. decision = channelpadding_decide_to_pad_channel(chan);
  904. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  905. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  906. tt_assert(chan->pending_padding_callback);
  907. // Close the connection while the timer is scheduled
  908. free_fake_channeltls((channel_tls_t*)chan);
  909. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  910. new_time = 101*NSEC_PER_MSEC;
  911. monotime_coarse_set_mock_time_nsec(new_time);
  912. monotime_set_mock_time_nsec(new_time);
  913. monotime_coarse_get(&now);
  914. timers_run_pending();
  915. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  916. done:
  917. smartlist_free(connection_array);
  918. teardown_capture_of_logs();
  919. monotime_disable_test_mocking();
  920. timers_shutdown();
  921. channel_free_all();
  922. return;
  923. }
  924. #define TEST_CHANNELPADDING(name, flags) \
  925. { #name, test_##name, (flags), NULL, NULL }
  926. struct testcase_t channelpadding_tests[] = {
  927. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  928. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  929. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  930. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  931. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  932. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  933. END_OF_TESTCASES
  934. };