test_channelpadding.c 40 KB

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