test_channelpadding.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define MAIN_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/main.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 tor2webmode or rsos are enabled, and
  340. * the consensus says don't pad? */
  341. /* Ensure we can kill tor2web and rsos padding if we want. */
  342. // First, test that padding works if either is enabled
  343. smartlist_clear(current_md_consensus->net_params);
  344. channelpadding_new_consensus_params(current_md_consensus);
  345. monotime_coarse_t now;
  346. monotime_coarse_get(&now);
  347. tried_to_write_cell = 0;
  348. get_options_mutable()->Tor2webMode = 1;
  349. monotime_coarse_add_msec(&client_relay3->next_padding_time, &now, 100);
  350. decision = channelpadding_decide_to_pad_channel(client_relay3);
  351. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  352. tt_assert(client_relay3->pending_padding_callback);
  353. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  354. decision = channelpadding_decide_to_pad_channel(client_relay3);
  355. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  356. // Wait for the timer
  357. new_time += 101*NSEC_PER_MSEC;
  358. monotime_coarse_set_mock_time_nsec(new_time);
  359. monotime_set_mock_time_nsec(new_time);
  360. monotime_coarse_get(&now);
  361. timers_run_pending();
  362. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  363. tt_assert(!client_relay3->pending_padding_callback);
  364. // Then test disabling each via consensus param
  365. smartlist_add(current_md_consensus->net_params,
  366. (void*)"nf_pad_tor2web=0");
  367. channelpadding_new_consensus_params(current_md_consensus);
  368. // Before the client tries to pad, the relay will still pad:
  369. tried_to_write_cell = 0;
  370. monotime_coarse_add_msec(&relay3_client->next_padding_time, &now, 100);
  371. get_options_mutable()->ORPort_set = 1;
  372. get_options_mutable()->Tor2webMode = 0;
  373. decision = channelpadding_decide_to_pad_channel(relay3_client);
  374. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  375. tt_assert(relay3_client->pending_padding_callback);
  376. // Wait for the timer
  377. new_time += 101*NSEC_PER_MSEC;
  378. monotime_coarse_set_mock_time_nsec(new_time);
  379. monotime_set_mock_time_nsec(new_time);
  380. monotime_coarse_get(&now);
  381. timers_run_pending();
  382. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  383. tt_assert(!client_relay3->pending_padding_callback);
  384. // Test client side (it should stop immediately, but send a negotiate)
  385. tried_to_write_cell = 0;
  386. tt_assert(relay3_client->padding_enabled);
  387. tt_assert(client_relay3->padding_enabled);
  388. get_options_mutable()->Tor2webMode = 1;
  389. /* For the relay to receive the negotiate: */
  390. get_options_mutable()->ORPort_set = 1;
  391. decision = channelpadding_decide_to_pad_channel(client_relay3);
  392. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  393. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  394. tt_assert(!client_relay3->pending_padding_callback);
  395. tt_assert(!relay3_client->padding_enabled);
  396. // Test relay side (it should have gotten the negotiation to disable)
  397. get_options_mutable()->ORPort_set = 1;
  398. get_options_mutable()->Tor2webMode = 0;
  399. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  400. CHANNELPADDING_WONTPAD);
  401. tt_assert(!relay3_client->padding_enabled);
  402. /* Repeat for SOS */
  403. // First, test that padding works if either is enabled
  404. smartlist_clear(current_md_consensus->net_params);
  405. channelpadding_new_consensus_params(current_md_consensus);
  406. relay3_client->padding_enabled = 1;
  407. client_relay3->padding_enabled = 1;
  408. tried_to_write_cell = 0;
  409. get_options_mutable()->ORPort_set = 0;
  410. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  411. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  412. monotime_coarse_add_msec(&client_relay3->next_padding_time, &now, 100);
  413. decision = channelpadding_decide_to_pad_channel(client_relay3);
  414. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  415. tt_assert(client_relay3->pending_padding_callback);
  416. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  417. decision = channelpadding_decide_to_pad_channel(client_relay3);
  418. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  419. // Wait for the timer
  420. new_time += 101 * NSEC_PER_MSEC;
  421. monotime_coarse_set_mock_time_nsec(new_time);
  422. monotime_set_mock_time_nsec(new_time);
  423. monotime_coarse_get(&now);
  424. timers_run_pending();
  425. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  426. tt_assert(!client_relay3->pending_padding_callback);
  427. // Then test disabling each via consensus param
  428. smartlist_add(current_md_consensus->net_params,
  429. (void*)"nf_pad_single_onion=0");
  430. channelpadding_new_consensus_params(current_md_consensus);
  431. // Before the client tries to pad, the relay will still pad:
  432. tried_to_write_cell = 0;
  433. monotime_coarse_add_msec(&relay3_client->next_padding_time, &now, 100);
  434. get_options_mutable()->ORPort_set = 1;
  435. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  436. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  437. decision = channelpadding_decide_to_pad_channel(relay3_client);
  438. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  439. tt_assert(relay3_client->pending_padding_callback);
  440. // Wait for the timer
  441. new_time += 101 * NSEC_PER_MSEC;
  442. monotime_coarse_set_mock_time_nsec(new_time);
  443. monotime_set_mock_time_nsec(new_time);
  444. monotime_coarse_get(&now);
  445. timers_run_pending();
  446. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  447. tt_assert(!client_relay3->pending_padding_callback);
  448. // Test client side (it should stop immediately)
  449. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  450. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  451. /* For the relay to receive the negotiate: */
  452. get_options_mutable()->ORPort_set = 1;
  453. decision = channelpadding_decide_to_pad_channel(client_relay3);
  454. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  455. tt_assert(!client_relay3->pending_padding_callback);
  456. // Test relay side (it should have gotten the negotiation to disable)
  457. get_options_mutable()->ORPort_set = 1;
  458. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  459. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  460. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  461. CHANNELPADDING_WONTPAD);
  462. tt_assert(!relay3_client->padding_enabled);
  463. done:
  464. free_mock_consensus();
  465. free_mock_network();
  466. tor_free(relay);
  467. timers_shutdown();
  468. monotime_disable_test_mocking();
  469. channel_free_all();
  470. }
  471. void
  472. test_channelpadding_consensus(void *arg)
  473. {
  474. channelpadding_decision_t decision;
  475. or_options_t *options = get_options_mutable();
  476. int64_t val;
  477. int64_t new_time;
  478. (void)arg;
  479. tor_libevent_postfork();
  480. /*
  481. * Params tested:
  482. * nf_pad_before_usage
  483. * nf_pad_relays
  484. * nf_ito_low
  485. * nf_ito_high
  486. *
  487. * Plan:
  488. * 1. Padding can be completely disabled via consensus
  489. * 2. Negotiation can't re-enable consensus-disabled padding
  490. * 3. Negotiation can't increase padding from relays beyond
  491. * consensus defaults
  492. * 4. Relay-to-relay padding can be enabled/disabled in consensus
  493. * 5. Can enable/disable padding before actually using a connection
  494. * 6. Can we control circ and TLS conn lifetime from the consensus?
  495. */
  496. channel_t *chan;
  497. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  498. monotime_enable_test_mocking();
  499. monotime_set_mock_time_nsec(1);
  500. monotime_coarse_set_mock_time_nsec(1);
  501. new_time = 1;
  502. monotime_coarse_t now;
  503. monotime_coarse_get(&now);
  504. timers_initialize();
  505. if (!connection_array)
  506. connection_array = smartlist_new();
  507. chan = (channel_t*)new_fake_channeltls(0);
  508. channel_timestamp_active(chan);
  509. setup_mock_consensus();
  510. get_options_mutable()->ORPort_set = 1;
  511. /* Test 1: Padding can be completely disabled via consensus */
  512. tried_to_write_cell = 0;
  513. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  514. decision = channelpadding_decide_to_pad_channel(chan);
  515. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  516. tt_assert(chan->pending_padding_callback);
  517. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  518. decision = channelpadding_decide_to_pad_channel(chan);
  519. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  520. // Wait for the timer
  521. new_time += 101*NSEC_PER_MSEC;
  522. monotime_coarse_set_mock_time_nsec(new_time);
  523. monotime_set_mock_time_nsec(new_time);
  524. monotime_coarse_get(&now);
  525. timers_run_pending();
  526. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  527. tt_assert(!chan->pending_padding_callback);
  528. smartlist_add(current_md_consensus->net_params,
  529. (void*)"nf_ito_low=0");
  530. smartlist_add(current_md_consensus->net_params,
  531. (void*)"nf_ito_high=0");
  532. get_options_mutable()->ConnectionPadding = 1;
  533. channelpadding_new_consensus_params(current_md_consensus);
  534. decision = channelpadding_decide_to_pad_channel(chan);
  535. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  536. tt_assert(!chan->pending_padding_callback);
  537. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  538. tt_i64_op(val, OP_EQ, 0);
  539. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  540. tt_i64_op(val, OP_EQ, -2);
  541. /* Test 2: Negotiation can't re-enable consensus-disabled padding */
  542. channelpadding_send_enable_command(chan, 100, 200);
  543. tried_to_write_cell = 0;
  544. decision = channelpadding_decide_to_pad_channel(chan);
  545. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  546. tt_assert(!chan->pending_padding_callback);
  547. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  548. tt_i64_op(val, OP_EQ, 0);
  549. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  550. tt_i64_op(val, OP_EQ, -2);
  551. tt_assert(monotime_coarse_is_zero(&chan->next_padding_time));
  552. smartlist_clear(current_md_consensus->net_params);
  553. /* Test 3: Negotiation can't increase padding from relays beyond consensus
  554. * values */
  555. smartlist_add(current_md_consensus->net_params,
  556. (void*)"nf_ito_low=100");
  557. smartlist_add(current_md_consensus->net_params,
  558. (void*)"nf_ito_high=200");
  559. channelpadding_new_consensus_params(current_md_consensus);
  560. tried_to_write_cell = 0;
  561. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  562. decision = channelpadding_decide_to_pad_channel(chan);
  563. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  564. tt_assert(chan->pending_padding_callback);
  565. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  566. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  567. tt_i64_op(val, OP_GE, 100);
  568. tt_i64_op(val, OP_LE, 200);
  569. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  570. tt_i64_op(val, OP_LE, 200);
  571. // Wait for the timer
  572. new_time += 201*NSEC_PER_MSEC;
  573. monotime_set_mock_time_nsec(new_time);
  574. monotime_coarse_set_mock_time_nsec(new_time);
  575. monotime_coarse_get(&now);
  576. timers_run_pending();
  577. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  578. tt_assert(!chan->pending_padding_callback);
  579. smartlist_clear(current_md_consensus->net_params);
  580. smartlist_add(current_md_consensus->net_params,
  581. (void*)"nf_ito_low=1500");
  582. smartlist_add(current_md_consensus->net_params,
  583. (void*)"nf_ito_high=4500");
  584. channelpadding_new_consensus_params(current_md_consensus);
  585. channelpadding_send_enable_command(chan, 100, 200);
  586. tried_to_write_cell = 0;
  587. decision = channelpadding_decide_to_pad_channel(chan);
  588. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  589. tt_assert(!chan->pending_padding_callback);
  590. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  591. tt_i64_op(val, OP_GE, 1500);
  592. tt_i64_op(val, OP_LE, 4500);
  593. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  594. tt_i64_op(val, OP_LE, 4500);
  595. /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
  596. /* Make this channel a relay's channel */
  597. memcpy(relay->identity_digest,
  598. ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
  599. smartlist_add(current_md_consensus->routerstatus_list, relay);
  600. relay = NULL; /* Prevent double-free */
  601. tried_to_write_cell = 0;
  602. decision = channelpadding_decide_to_pad_channel(chan);
  603. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  604. tt_assert(!chan->pending_padding_callback);
  605. smartlist_add(current_md_consensus->net_params,
  606. (void*)"nf_pad_relays=1");
  607. channelpadding_new_consensus_params(current_md_consensus);
  608. decision = channelpadding_decide_to_pad_channel(chan);
  609. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  610. tt_assert(!chan->pending_padding_callback);
  611. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  612. tt_i64_op(val, OP_GE, 1500);
  613. tt_i64_op(val, OP_LE, 4500);
  614. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  615. tt_i64_op(val, OP_LE, 4500);
  616. /* Test 5: If we disable padding before channel usage, does that work? */
  617. smartlist_add(current_md_consensus->net_params,
  618. (void*)"nf_pad_before_usage=0");
  619. channelpadding_new_consensus_params(current_md_consensus);
  620. tried_to_write_cell = 0;
  621. decision = channelpadding_decide_to_pad_channel(chan);
  622. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  623. tt_assert(!chan->pending_padding_callback);
  624. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  625. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  626. tt_i64_op(val, OP_GE, 180);
  627. tt_i64_op(val, OP_LE, 180+90);
  628. val = channelpadding_get_channel_idle_timeout(chan, 0);
  629. tt_i64_op(val, OP_GE, 180);
  630. tt_i64_op(val, OP_LE, 180+90);
  631. options->ReducedConnectionPadding = 1;
  632. val = channelpadding_get_channel_idle_timeout(chan, 0);
  633. tt_i64_op(val, OP_GE, 180/2);
  634. tt_i64_op(val, OP_LE, (180+90)/2);
  635. options->ReducedConnectionPadding = 0;
  636. options->ORPort_set = 1;
  637. smartlist_add(current_md_consensus->net_params,
  638. (void*)"nf_conntimeout_relays=600");
  639. channelpadding_new_consensus_params(current_md_consensus);
  640. val = channelpadding_get_channel_idle_timeout(chan, 1);
  641. tt_i64_op(val, OP_GE, 450);
  642. tt_i64_op(val, OP_LE, 750);
  643. val = channelpadding_get_circuits_available_timeout();
  644. tt_i64_op(val, OP_GE, 30*60);
  645. tt_i64_op(val, OP_LE, 30*60*2);
  646. options->ReducedConnectionPadding = 1;
  647. smartlist_add(current_md_consensus->net_params,
  648. (void*)"nf_conntimeout_clients=600");
  649. channelpadding_new_consensus_params(current_md_consensus);
  650. val = channelpadding_get_circuits_available_timeout();
  651. tt_i64_op(val, OP_GE, 600/2);
  652. tt_i64_op(val, OP_LE, 600*2/2);
  653. options->ReducedConnectionPadding = 0;
  654. options->CircuitsAvailableTimeout = 24*60*60;
  655. val = channelpadding_get_circuits_available_timeout();
  656. tt_i64_op(val, OP_GE, 24*60*60);
  657. tt_i64_op(val, OP_LE, 24*60*60*2);
  658. done:
  659. tor_free(relay);
  660. free_mock_consensus();
  661. free_fake_channeltls((channel_tls_t*)chan);
  662. smartlist_free(connection_array);
  663. timers_shutdown();
  664. monotime_disable_test_mocking();
  665. channel_free_all();
  666. return;
  667. }
  668. void
  669. test_channelpadding_negotiation(void *arg)
  670. {
  671. channelpadding_negotiate_t disable;
  672. cell_t cell;
  673. channelpadding_decision_t decision;
  674. int val;
  675. (void)arg;
  676. /* Plan:
  677. * 1. Clients reject negotiation, relays accept it.
  678. * * Bridges accept negotiation from their clients,
  679. * but not from relays.
  680. * 2. Torrc options can override client-side negotiation
  681. * 3. Test a version issue in channelpadidng cell
  682. * 4. Test channelpadding_reduced_padding
  683. */
  684. monotime_init();
  685. monotime_enable_test_mocking();
  686. monotime_set_mock_time_nsec(1);
  687. monotime_coarse_set_mock_time_nsec(1);
  688. timers_initialize();
  689. setup_mock_consensus();
  690. setup_mock_network();
  691. /* Test case #1: Do the right things ignore negotiation? */
  692. /* relay-to-client case: */
  693. channelpadding_send_disable_command(relay3_client);
  694. tt_assert(client_relay3->padding_enabled);
  695. /* client-to-relay case: */
  696. get_options_mutable()->ORPort_set = 1;
  697. channelpadding_disable_padding_on_channel(client_relay3);
  698. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  699. CHANNELPADDING_WONTPAD);
  700. tt_assert(!relay3_client->padding_enabled);
  701. relay3_client->padding_enabled = 1;
  702. client_relay3->padding_enabled = 1;
  703. /* Bridge case from relay */
  704. get_options_mutable()->BridgeRelay = 1;
  705. channelpadding_disable_padding_on_channel(relay2_relay1);
  706. tt_assert(relay1_relay2->padding_enabled);
  707. /* Bridge case from client */
  708. channelpadding_disable_padding_on_channel(client_relay3);
  709. tt_assert(!relay3_client->padding_enabled);
  710. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  711. CHANNELPADDING_WONTPAD);
  712. relay3_client->padding_enabled = 1;
  713. client_relay3->padding_enabled = 1;
  714. get_options_mutable()->BridgeRelay = 0;
  715. get_options_mutable()->ORPort_set = 0;
  716. /* Test case #2: Torrc options */
  717. /* ConnectionPadding auto; Relay doesn't support us */
  718. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  719. relay3_client->padding_enabled = 0;
  720. tried_to_write_cell = 0;
  721. decision = channelpadding_decide_to_pad_channel(relay3_client);
  722. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  723. tt_assert(!relay3_client->pending_padding_callback);
  724. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  725. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  726. relay3_client->padding_enabled = 1;
  727. /* ConnectionPadding 1; Relay doesn't support us */
  728. get_options_mutable()->ConnectionPadding = 1;
  729. tried_to_write_cell = 0;
  730. decision = channelpadding_decide_to_pad_channel(client_relay3);
  731. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  732. tt_assert(!client_relay3->pending_padding_callback);
  733. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  734. get_options_mutable()->ConnectionPadding = 0;
  735. /* Test case #3: Test a version issue in channelpadding cell */
  736. get_options_mutable()->ORPort_set = 1;
  737. client_relay3->padding_enabled = 1;
  738. relay3_client->padding_enabled = 1;
  739. memset(&cell, 0, sizeof(cell_t));
  740. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  741. cell.command = CELL_PADDING_NEGOTIATE;
  742. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  743. disable.version = 1;
  744. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  745. client_relay3->write_cell(client_relay3, &cell);
  746. tt_assert(relay3_client->padding_enabled);
  747. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  748. OP_EQ, -1);
  749. tt_assert(client_relay3->padding_enabled);
  750. disable.version = 0;
  751. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  752. client_relay3->write_cell(client_relay3, &cell);
  753. tt_assert(!relay3_client->padding_enabled);
  754. /* Test case 4: Reducing padding actually reduces it */
  755. relay3_client->padding_enabled = 1;
  756. client_relay3->padding_enabled = 1;
  757. decision = channelpadding_decide_to_pad_channel(relay3_client);
  758. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  759. channelpadding_reduce_padding_on_channel(client_relay3);
  760. tried_to_write_cell = 0;
  761. decision = channelpadding_decide_to_pad_channel(relay3_client);
  762. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  763. get_options_mutable()->ORPort_set = 0;
  764. decision = channelpadding_decide_to_pad_channel(client_relay3);
  765. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  766. tt_assert(!client_relay3->pending_padding_callback);
  767. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  768. tt_int_op(val, OP_GE, 9000);
  769. tt_int_op(val, OP_LE, 14000);
  770. int64_t val64 =
  771. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  772. tt_i64_op(val64, OP_LE, 14000);
  773. done:
  774. free_mock_network();
  775. free_mock_consensus();
  776. timers_shutdown();
  777. monotime_disable_test_mocking();
  778. channel_free_all();
  779. return;
  780. }
  781. void
  782. test_channelpadding_decide_to_pad_channel(void *arg)
  783. {
  784. channelpadding_decision_t decision;
  785. /**
  786. * Test case plan:
  787. *
  788. * 1. Channel that has "sent a packet" before the timeout.
  789. * + We should decide to pad later
  790. * 2. Channel that has not "sent a packet" before the timeout:
  791. * 2a. Not within 1.1s of the timeout.
  792. * + We should decide to pad later
  793. * 2b. Within 1.1s of the timemout.
  794. * + We should schedule padding
  795. * + We should get feedback that we wrote a cell
  796. * 2c. Within 0.1s of the timeout.
  797. * + We should schedule padding
  798. * + We should get feedback that we wrote a cell
  799. * 2d. Channel that asks to pad while timeout is scheduled
  800. * + We should schedule padding
  801. * + We should get feedback that we wrote a cell
  802. * 2e. 0s of the timeout
  803. * + We should send padding immediately
  804. * + We should get feedback that we wrote a cell
  805. * 2f. <0s of the timeout
  806. * + We should send padding immediately
  807. * + We should get feedback that we wrote a cell
  808. * 3. Channel that sends a packet while timeout is scheduled
  809. * + We should not get feedback that we wrote a cell
  810. * 4. Channel that closes while timeout is scheduled
  811. * + We should not get feedback that we wrote a cell
  812. * 5. Make sure the channel still would work if repaired
  813. * + We should be able to schedule padding and resend
  814. * 6. Channel is not used for full circuits
  815. * 7. Channel that disappears while timeout is scheduled
  816. * + We should not send padding
  817. */
  818. channel_t *chan;
  819. int64_t new_time;
  820. if (!connection_array)
  821. connection_array = smartlist_new();
  822. (void)arg;
  823. tor_libevent_postfork();
  824. monotime_init();
  825. monotime_enable_test_mocking();
  826. monotime_set_mock_time_nsec(1);
  827. monotime_coarse_set_mock_time_nsec(1);
  828. new_time = 1;
  829. monotime_coarse_t now;
  830. monotime_coarse_get(&now);
  831. timers_initialize();
  832. setup_full_capture_of_logs(LOG_WARN);
  833. channelpadding_new_consensus_params(NULL);
  834. chan = (channel_t*)new_fake_channeltls(0);
  835. channel_timestamp_active(chan);
  836. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  837. tried_to_write_cell = 0;
  838. decision = channelpadding_decide_to_pad_channel(chan);
  839. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  840. tt_assert(!chan->pending_padding_callback);
  841. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  842. /* Test case #2a: > 1.1s until timeout */
  843. tried_to_write_cell = 0;
  844. monotime_coarse_add_msec(&chan->next_padding_time, &now, 1200);
  845. decision = channelpadding_decide_to_pad_channel(chan);
  846. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  847. tt_assert(!chan->pending_padding_callback);
  848. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  849. /* Test case #2b: >= 1.0s until timeout */
  850. tried_to_write_cell = 0;
  851. monotime_coarse_add_msec(&chan->next_padding_time, &now, 1000);
  852. decision = channelpadding_decide_to_pad_channel(chan);
  853. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  854. tt_assert(chan->pending_padding_callback);
  855. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  856. // Set up a timer for the <0 case below.
  857. monotime_coarse_t now_minus_100s;
  858. monotime_coarse_add_msec(&now_minus_100s, &now, 900);
  859. // Wait for the timer from case #2b
  860. new_time += 1000*NSEC_PER_MSEC;
  861. monotime_set_mock_time_nsec(new_time);
  862. monotime_coarse_set_mock_time_nsec(new_time);
  863. monotime_coarse_get(&now);
  864. timers_run_pending();
  865. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  866. tt_assert(!chan->pending_padding_callback);
  867. /* Test case #2c: > 0.1s until timeout */
  868. tried_to_write_cell = 0;
  869. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  870. decision = channelpadding_decide_to_pad_channel(chan);
  871. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  872. tt_assert(chan->pending_padding_callback);
  873. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  874. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  875. decision = channelpadding_decide_to_pad_channel(chan);
  876. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  877. // Wait for the timer
  878. new_time += 101*NSEC_PER_MSEC;
  879. monotime_coarse_set_mock_time_nsec(new_time);
  880. monotime_set_mock_time_nsec(new_time);
  881. monotime_coarse_get(&now);
  882. timers_run_pending();
  883. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  884. tt_assert(!chan->pending_padding_callback);
  885. /* Test case #2e: 0s until timeout */
  886. tried_to_write_cell = 0;
  887. monotime_coarse_add_msec(&chan->next_padding_time, &now, 0);
  888. decision = channelpadding_decide_to_pad_channel(chan);
  889. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  890. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  891. tt_assert(!chan->pending_padding_callback);
  892. /* Test case #2f: <0s until timeout */
  893. tried_to_write_cell = 0;
  894. monotime_coarse_add_msec(&chan->next_padding_time, &now_minus_100s, 0);
  895. decision = channelpadding_decide_to_pad_channel(chan);
  896. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  897. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  898. tt_assert(!chan->pending_padding_callback);
  899. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  900. tried_to_write_cell = 0;
  901. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  902. decision = channelpadding_decide_to_pad_channel(chan);
  903. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  904. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  905. tt_assert(chan->pending_padding_callback);
  906. // Pretend the channel sent a packet
  907. channel_timestamp_active(chan);
  908. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  909. // Wait for the timer
  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. tt_assert(!chan->pending_padding_callback);
  917. /* Test case #4: Channel that closes while a timeout is scheduled */
  918. tried_to_write_cell = 0;
  919. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  920. decision = channelpadding_decide_to_pad_channel(chan);
  921. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  922. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  923. tt_assert(chan->pending_padding_callback);
  924. // Pretend the channel is temporarily down
  925. chan->state = CHANNEL_STATE_MAINT;
  926. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  927. new_time += 101*NSEC_PER_MSEC;
  928. monotime_coarse_set_mock_time_nsec(new_time);
  929. monotime_set_mock_time_nsec(new_time);
  930. monotime_coarse_get(&now);
  931. timers_run_pending();
  932. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  933. tt_assert(!chan->pending_padding_callback);
  934. chan->state = CHANNEL_STATE_OPEN;
  935. /* Test case #5: Make sure previous test case didn't break everything */
  936. tried_to_write_cell = 0;
  937. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  938. decision = channelpadding_decide_to_pad_channel(chan);
  939. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  940. tt_assert(chan->pending_padding_callback);
  941. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  942. // Wait for the timer
  943. new_time += 101*NSEC_PER_MSEC;
  944. monotime_coarse_set_mock_time_nsec(new_time);
  945. monotime_set_mock_time_nsec(new_time);
  946. monotime_coarse_get(&now);
  947. timers_run_pending();
  948. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  949. tt_assert(!chan->pending_padding_callback);
  950. /* Test case #6. Channel is not used for full circuits */
  951. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  952. decision = channelpadding_decide_to_pad_channel(chan);
  953. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  954. tt_assert(!chan->pending_padding_callback);
  955. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  956. /* Test case #7. Channel is closed while timeout is scheduled.
  957. *
  958. * NOTE: This test deliberately breaks the channel callback mechanism.
  959. * It must be last.
  960. */
  961. tried_to_write_cell = 0;
  962. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  963. decision = channelpadding_decide_to_pad_channel(chan);
  964. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  965. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  966. tt_assert(chan->pending_padding_callback);
  967. // Close the connection while the timer is scheduled
  968. free_fake_channeltls((channel_tls_t*)chan);
  969. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  970. new_time = 101*NSEC_PER_MSEC;
  971. monotime_coarse_set_mock_time_nsec(new_time);
  972. monotime_set_mock_time_nsec(new_time);
  973. monotime_coarse_get(&now);
  974. timers_run_pending();
  975. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  976. done:
  977. smartlist_free(connection_array);
  978. teardown_capture_of_logs();
  979. monotime_disable_test_mocking();
  980. timers_shutdown();
  981. channel_free_all();
  982. return;
  983. }
  984. #define TEST_CHANNELPADDING(name, flags) \
  985. { #name, test_##name, (flags), NULL, NULL }
  986. struct testcase_t channelpadding_tests[] = {
  987. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  988. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  989. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  990. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  991. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  992. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  993. END_OF_TESTCASES
  994. };