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