test_channelpadding.c 32 KB

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