test_channelpadding.c 40 KB

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