test_channelpadding.c 31 KB

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