test_channelpadding.c 40 KB

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