test_channelpadding.c 35 KB

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