test_channelpadding.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. relay = NULL; /* Prevent double-free */
  598. tried_to_write_cell = 0;
  599. decision = channelpadding_decide_to_pad_channel(chan);
  600. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  601. tt_assert(!chan->pending_padding_callback);
  602. smartlist_add(current_md_consensus->net_params,
  603. (void*)"nf_pad_relays=1");
  604. channelpadding_new_consensus_params(current_md_consensus);
  605. decision = channelpadding_decide_to_pad_channel(chan);
  606. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  607. tt_assert(!chan->pending_padding_callback);
  608. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  609. tt_i64_op(val, OP_GE, 1500);
  610. tt_i64_op(val, OP_LE, 4500);
  611. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  612. tt_i64_op(val, OP_LE, 4500);
  613. /* Test 5: If we disable padding before channel usage, does that work? */
  614. smartlist_add(current_md_consensus->net_params,
  615. (void*)"nf_pad_before_usage=0");
  616. channelpadding_new_consensus_params(current_md_consensus);
  617. tried_to_write_cell = 0;
  618. decision = channelpadding_decide_to_pad_channel(chan);
  619. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  620. tt_assert(!chan->pending_padding_callback);
  621. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  622. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  623. tt_i64_op(val, OP_GE, 180);
  624. tt_i64_op(val, OP_LE, 180+90);
  625. val = channelpadding_get_channel_idle_timeout(chan, 0);
  626. tt_i64_op(val, OP_GE, 180);
  627. tt_i64_op(val, OP_LE, 180+90);
  628. options->ReducedConnectionPadding = 1;
  629. val = channelpadding_get_channel_idle_timeout(chan, 0);
  630. tt_i64_op(val, OP_GE, 180/2);
  631. tt_i64_op(val, OP_LE, (180+90)/2);
  632. options->ReducedConnectionPadding = 0;
  633. options->ORPort_set = 1;
  634. smartlist_add(current_md_consensus->net_params,
  635. (void*)"nf_conntimeout_relays=600");
  636. channelpadding_new_consensus_params(current_md_consensus);
  637. val = channelpadding_get_channel_idle_timeout(chan, 1);
  638. tt_i64_op(val, OP_GE, 450);
  639. tt_i64_op(val, OP_LE, 750);
  640. val = channelpadding_get_circuits_available_timeout();
  641. tt_i64_op(val, OP_GE, 30*60);
  642. tt_i64_op(val, OP_LE, 30*60*2);
  643. options->ReducedConnectionPadding = 1;
  644. smartlist_add(current_md_consensus->net_params,
  645. (void*)"nf_conntimeout_clients=600");
  646. channelpadding_new_consensus_params(current_md_consensus);
  647. val = channelpadding_get_circuits_available_timeout();
  648. tt_i64_op(val, OP_GE, 600/2);
  649. tt_i64_op(val, OP_LE, 600*2/2);
  650. options->ReducedConnectionPadding = 0;
  651. options->CircuitsAvailableTimeout = 24*60*60;
  652. val = channelpadding_get_circuits_available_timeout();
  653. tt_i64_op(val, OP_GE, 24*60*60);
  654. tt_i64_op(val, OP_LE, 24*60*60*2);
  655. done:
  656. tor_free(relay);
  657. free_mock_consensus();
  658. free_fake_channeltls((channel_tls_t*)chan);
  659. smartlist_free(connection_array);
  660. timers_shutdown();
  661. monotime_disable_test_mocking();
  662. channel_free_all();
  663. return;
  664. }
  665. void
  666. test_channelpadding_negotiation(void *arg)
  667. {
  668. channelpadding_negotiate_t disable;
  669. cell_t cell;
  670. channelpadding_decision_t decision;
  671. int val;
  672. (void)arg;
  673. /* Plan:
  674. * 1. Clients reject negotiation, relays accept it.
  675. * * Bridges accept negotiation from their clients,
  676. * but not from relays.
  677. * 2. Torrc options can override client-side negotiation
  678. * 3. Test a version issue in channelpadidng cell
  679. * 4. Test channelpadding_reduced_padding
  680. */
  681. monotime_init();
  682. monotime_enable_test_mocking();
  683. monotime_set_mock_time_nsec(1);
  684. monotime_coarse_set_mock_time_nsec(1);
  685. timers_initialize();
  686. setup_mock_consensus();
  687. setup_mock_network();
  688. /* Test case #1: Do the right things ignore negotiation? */
  689. /* relay-to-client case: */
  690. channelpadding_send_disable_command(relay3_client);
  691. tt_assert(client_relay3->padding_enabled);
  692. /* client-to-relay case: */
  693. get_options_mutable()->ORPort_set = 1;
  694. channelpadding_disable_padding_on_channel(client_relay3);
  695. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  696. CHANNELPADDING_WONTPAD);
  697. tt_assert(!relay3_client->padding_enabled);
  698. relay3_client->padding_enabled = 1;
  699. client_relay3->padding_enabled = 1;
  700. /* Bridge case from relay */
  701. get_options_mutable()->BridgeRelay = 1;
  702. channelpadding_disable_padding_on_channel(relay2_relay1);
  703. tt_assert(relay1_relay2->padding_enabled);
  704. /* Bridge case from client */
  705. channelpadding_disable_padding_on_channel(client_relay3);
  706. tt_assert(!relay3_client->padding_enabled);
  707. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  708. CHANNELPADDING_WONTPAD);
  709. relay3_client->padding_enabled = 1;
  710. client_relay3->padding_enabled = 1;
  711. get_options_mutable()->BridgeRelay = 0;
  712. get_options_mutable()->ORPort_set = 0;
  713. /* Test case #2: Torrc options */
  714. /* ConnectionPadding auto; Relay doesn't support us */
  715. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  716. relay3_client->padding_enabled = 0;
  717. tried_to_write_cell = 0;
  718. decision = channelpadding_decide_to_pad_channel(relay3_client);
  719. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  720. tt_assert(!relay3_client->pending_padding_callback);
  721. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  722. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  723. relay3_client->padding_enabled = 1;
  724. /* ConnectionPadding 1; Relay doesn't support us */
  725. get_options_mutable()->ConnectionPadding = 1;
  726. tried_to_write_cell = 0;
  727. decision = channelpadding_decide_to_pad_channel(client_relay3);
  728. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  729. tt_assert(!client_relay3->pending_padding_callback);
  730. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  731. get_options_mutable()->ConnectionPadding = 0;
  732. /* Test case #3: Test a version issue in channelpadding cell */
  733. get_options_mutable()->ORPort_set = 1;
  734. client_relay3->padding_enabled = 1;
  735. relay3_client->padding_enabled = 1;
  736. memset(&cell, 0, sizeof(cell_t));
  737. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  738. cell.command = CELL_PADDING_NEGOTIATE;
  739. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  740. disable.version = 1;
  741. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  742. client_relay3->write_cell(client_relay3, &cell);
  743. tt_assert(relay3_client->padding_enabled);
  744. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  745. OP_EQ, -1);
  746. tt_assert(client_relay3->padding_enabled);
  747. disable.version = 0;
  748. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  749. client_relay3->write_cell(client_relay3, &cell);
  750. tt_assert(!relay3_client->padding_enabled);
  751. /* Test case 4: Reducing padding actually reduces it */
  752. relay3_client->padding_enabled = 1;
  753. client_relay3->padding_enabled = 1;
  754. decision = channelpadding_decide_to_pad_channel(relay3_client);
  755. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  756. channelpadding_reduce_padding_on_channel(client_relay3);
  757. tried_to_write_cell = 0;
  758. decision = channelpadding_decide_to_pad_channel(relay3_client);
  759. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  760. get_options_mutable()->ORPort_set = 0;
  761. decision = channelpadding_decide_to_pad_channel(client_relay3);
  762. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  763. tt_assert(!client_relay3->pending_padding_callback);
  764. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  765. tt_int_op(val, OP_GE, 9000);
  766. tt_int_op(val, OP_LE, 14000);
  767. int64_t val64 =
  768. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  769. tt_i64_op(val64, OP_LE, 14000);
  770. done:
  771. free_mock_network();
  772. free_mock_consensus();
  773. timers_shutdown();
  774. monotime_disable_test_mocking();
  775. channel_free_all();
  776. return;
  777. }
  778. void
  779. test_channelpadding_decide_to_pad_channel(void *arg)
  780. {
  781. channelpadding_decision_t decision;
  782. /**
  783. * Test case plan:
  784. *
  785. * 1. Channel that has "sent a packet" before the timeout.
  786. * + We should decide to pad later
  787. * 2. Channel that has not "sent a packet" before the timeout:
  788. * 2a. Not within 1.1s of the timeout.
  789. * + We should decide to pad later
  790. * 2b. Within 1.1s of the timemout.
  791. * + We should schedule padding
  792. * + We should get feedback that we wrote a cell
  793. * 2c. Within 0.1s of the timeout.
  794. * + We should schedule padding
  795. * + We should get feedback that we wrote a cell
  796. * 2d. Channel that asks to pad while timeout is scheduled
  797. * + We should schedule padding
  798. * + We should get feedback that we wrote a cell
  799. * 2e. 0s of the timeout
  800. * + We should send padding immediately
  801. * + We should get feedback that we wrote a cell
  802. * 2f. <0s of the timeout
  803. * + We should send padding immediately
  804. * + We should get feedback that we wrote a cell
  805. * 3. Channel that sends a packet while timeout is scheduled
  806. * + We should not get feedback that we wrote a cell
  807. * 4. Channel that closes while timeout is scheduled
  808. * + We should not get feedback that we wrote a cell
  809. * 5. Make sure the channel still would work if repaired
  810. * + We should be able to schedule padding and resend
  811. * 6. Channel is not used for full circuits
  812. * 7. Channel that disappears while timeout is scheduled
  813. * + We should not send padding
  814. */
  815. channel_t *chan;
  816. int64_t new_time;
  817. if (!connection_array)
  818. connection_array = smartlist_new();
  819. (void)arg;
  820. tor_libevent_postfork();
  821. monotime_init();
  822. monotime_enable_test_mocking();
  823. monotime_set_mock_time_nsec(1);
  824. monotime_coarse_set_mock_time_nsec(1);
  825. new_time = 1;
  826. monotime_coarse_t now;
  827. monotime_coarse_get(&now);
  828. timers_initialize();
  829. setup_full_capture_of_logs(LOG_WARN);
  830. channelpadding_new_consensus_params(NULL);
  831. chan = (channel_t*)new_fake_channeltls(0);
  832. channel_timestamp_active(chan);
  833. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  834. tried_to_write_cell = 0;
  835. decision = channelpadding_decide_to_pad_channel(chan);
  836. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  837. tt_assert(!chan->pending_padding_callback);
  838. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  839. /* Test case #2a: > 1.1s until timeout */
  840. tried_to_write_cell = 0;
  841. monotime_coarse_add_msec(&chan->next_padding_time, &now, 1200);
  842. decision = channelpadding_decide_to_pad_channel(chan);
  843. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  844. tt_assert(!chan->pending_padding_callback);
  845. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  846. /* Test case #2b: >= 1.0s until timeout */
  847. tried_to_write_cell = 0;
  848. monotime_coarse_add_msec(&chan->next_padding_time, &now, 1000);
  849. decision = channelpadding_decide_to_pad_channel(chan);
  850. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  851. tt_assert(chan->pending_padding_callback);
  852. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  853. // Set up a timer for the <0 case below.
  854. monotime_coarse_t now_minus_100s;
  855. monotime_coarse_add_msec(&now_minus_100s, &now, 900);
  856. // Wait for the timer from case #2b
  857. new_time += 1000*NSEC_PER_MSEC;
  858. monotime_set_mock_time_nsec(new_time);
  859. monotime_coarse_set_mock_time_nsec(new_time);
  860. monotime_coarse_get(&now);
  861. timers_run_pending();
  862. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  863. tt_assert(!chan->pending_padding_callback);
  864. /* Test case #2c: > 0.1s until timeout */
  865. tried_to_write_cell = 0;
  866. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  867. decision = channelpadding_decide_to_pad_channel(chan);
  868. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  869. tt_assert(chan->pending_padding_callback);
  870. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  871. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  872. decision = channelpadding_decide_to_pad_channel(chan);
  873. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  874. // Wait for the timer
  875. new_time += 101*NSEC_PER_MSEC;
  876. monotime_coarse_set_mock_time_nsec(new_time);
  877. monotime_set_mock_time_nsec(new_time);
  878. monotime_coarse_get(&now);
  879. timers_run_pending();
  880. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  881. tt_assert(!chan->pending_padding_callback);
  882. /* Test case #2e: 0s until timeout */
  883. tried_to_write_cell = 0;
  884. monotime_coarse_add_msec(&chan->next_padding_time, &now, 0);
  885. decision = channelpadding_decide_to_pad_channel(chan);
  886. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  887. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  888. tt_assert(!chan->pending_padding_callback);
  889. /* Test case #2f: <0s until timeout */
  890. tried_to_write_cell = 0;
  891. monotime_coarse_add_msec(&chan->next_padding_time, &now_minus_100s, 0);
  892. decision = channelpadding_decide_to_pad_channel(chan);
  893. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  894. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  895. tt_assert(!chan->pending_padding_callback);
  896. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  897. tried_to_write_cell = 0;
  898. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  899. decision = channelpadding_decide_to_pad_channel(chan);
  900. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  901. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  902. tt_assert(chan->pending_padding_callback);
  903. // Pretend the channel sent a packet
  904. channel_timestamp_active(chan);
  905. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  906. // Wait for the timer
  907. new_time += 101*NSEC_PER_MSEC;
  908. monotime_coarse_set_mock_time_nsec(new_time);
  909. monotime_set_mock_time_nsec(new_time);
  910. monotime_coarse_get(&now);
  911. timers_run_pending();
  912. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  913. tt_assert(!chan->pending_padding_callback);
  914. /* Test case #4: Channel that closes while a timeout is scheduled */
  915. tried_to_write_cell = 0;
  916. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  917. decision = channelpadding_decide_to_pad_channel(chan);
  918. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  919. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  920. tt_assert(chan->pending_padding_callback);
  921. // Pretend the channel is temporarily down
  922. chan->state = CHANNEL_STATE_MAINT;
  923. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  924. new_time += 101*NSEC_PER_MSEC;
  925. monotime_coarse_set_mock_time_nsec(new_time);
  926. monotime_set_mock_time_nsec(new_time);
  927. monotime_coarse_get(&now);
  928. timers_run_pending();
  929. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  930. tt_assert(!chan->pending_padding_callback);
  931. chan->state = CHANNEL_STATE_OPEN;
  932. /* Test case #5: Make sure previous test case didn't break everything */
  933. tried_to_write_cell = 0;
  934. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  935. decision = channelpadding_decide_to_pad_channel(chan);
  936. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  937. tt_assert(chan->pending_padding_callback);
  938. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  939. // Wait for the timer
  940. new_time += 101*NSEC_PER_MSEC;
  941. monotime_coarse_set_mock_time_nsec(new_time);
  942. monotime_set_mock_time_nsec(new_time);
  943. monotime_coarse_get(&now);
  944. timers_run_pending();
  945. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  946. tt_assert(!chan->pending_padding_callback);
  947. /* Test case #6. Channel is not used for full circuits */
  948. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  949. decision = channelpadding_decide_to_pad_channel(chan);
  950. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  951. tt_assert(!chan->pending_padding_callback);
  952. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  953. /* Test case #7. Channel is closed while timeout is scheduled.
  954. *
  955. * NOTE: This test deliberately breaks the channel callback mechanism.
  956. * It must be last.
  957. */
  958. tried_to_write_cell = 0;
  959. monotime_coarse_add_msec(&chan->next_padding_time, &now, 100);
  960. decision = channelpadding_decide_to_pad_channel(chan);
  961. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  962. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  963. tt_assert(chan->pending_padding_callback);
  964. // Close the connection while the timer is scheduled
  965. free_fake_channeltls((channel_tls_t*)chan);
  966. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  967. new_time = 101*NSEC_PER_MSEC;
  968. monotime_coarse_set_mock_time_nsec(new_time);
  969. monotime_set_mock_time_nsec(new_time);
  970. monotime_coarse_get(&now);
  971. timers_run_pending();
  972. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  973. done:
  974. smartlist_free(connection_array);
  975. teardown_capture_of_logs();
  976. monotime_disable_test_mocking();
  977. timers_shutdown();
  978. channel_free_all();
  979. return;
  980. }
  981. #define TEST_CHANNELPADDING(name, flags) \
  982. { #name, test_##name, (flags), NULL, NULL }
  983. struct testcase_t channelpadding_tests[] = {
  984. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  985. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  986. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  987. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  988. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  989. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  990. END_OF_TESTCASES
  991. };