test_channelpadding.c 31 KB

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