test_channelpadding.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. #define TOR_CHANNEL_INTERNAL_
  2. #define MAIN_PRIVATE
  3. #define NETWORKSTATUS_PRIVATE
  4. #include "or.h"
  5. #include "test.h"
  6. #include "testsupport.h"
  7. #include "connection.h"
  8. #include "connection_or.h"
  9. #include "channel.h"
  10. #include "channeltls.h"
  11. #include "channelpadding.h"
  12. #include "compat_libevent.h"
  13. #include "config.h"
  14. #include <event2/event.h>
  15. #include "compat_time.h"
  16. #include "main.h"
  17. #include "networkstatus.h"
  18. #include "log_test_helpers.h"
  19. int channelpadding_get_netflow_inactive_timeout_ms(channel_t *chan);
  20. int64_t channelpadding_compute_time_until_pad_for_netflow(channel_t *chan);
  21. int channelpadding_send_disable_command(channel_t*);
  22. int channelpadding_find_timerslot(channel_t *chan);
  23. void test_channelpadding_timers(void *arg);
  24. void test_channelpadding_consensus(void *arg);
  25. void test_channelpadding_negotiation(void *arg);
  26. void test_channelpadding_decide_to_pad_channel(void *arg);
  27. void 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. tor_libevent_postfork();
  219. connection_array = smartlist_new();
  220. monotime_init();
  221. timers_initialize();
  222. channelpadding_new_consensus_params(NULL);
  223. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  224. chans[i] = (channel_t*)new_fake_channeltls(0);
  225. channel_timestamp_active(chans[i]);
  226. }
  227. for (int j = 0; j < 2; j++) {
  228. tried_to_write_cell = 0;
  229. int i = 0;
  230. /* This loop fills our timerslot array with timers of increasing time
  231. * until they fire */
  232. for (; i < CHANNELPADDING_MAX_TIMERS; i++) {
  233. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec()
  234. + 10 + i*4;
  235. decision = channelpadding_decide_to_pad_channel(chans[i]);
  236. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  237. tt_assert(chans[i]->pending_padding_callback);
  238. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  239. }
  240. /* This loop should add timers to the first position in the timerslot
  241. * array, since its timeout is before all other timers. */
  242. for (; i < CHANNELS_TO_TEST/3; i++) {
  243. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 1;
  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 our existing lists in a weak
  250. * pseudorandom pattern. It ensures that the lists can grow with multiple
  251. * timers in them. */
  252. for (; i < CHANNELS_TO_TEST/2; i++) {
  253. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 10 +
  254. i*3 % CHANNELPADDING_MAX_TIMERS;
  255. decision = channelpadding_decide_to_pad_channel(chans[i]);
  256. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  257. tt_assert(chans[i]->pending_padding_callback);
  258. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  259. }
  260. /* This loop should add timers to the last position in the timerslot
  261. * array, since its timeout is after all other timers. */
  262. for (; i < CHANNELS_TO_TEST; i++) {
  263. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 500 +
  264. i % CHANNELPADDING_MAX_TIMERS;
  265. decision = channelpadding_decide_to_pad_channel(chans[i]);
  266. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  267. tt_assert(chans[i]->pending_padding_callback);
  268. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  269. }
  270. // Wait for the timers and then kill the event loop.
  271. dont_stop_libevent = 1;
  272. dummy_nop_timer();
  273. tt_int_op(tried_to_write_cell, OP_EQ, CHANNELS_TO_TEST);
  274. // Test that we have no pending callbacks and all empty slots now
  275. for (i = 0; i < CHANNELS_TO_TEST; i++) {
  276. tt_assert(!chans[i]->pending_padding_callback);
  277. }
  278. }
  279. done:
  280. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  281. free_fake_channeltls((channel_tls_t*)chans[i]);
  282. }
  283. smartlist_free(connection_array);
  284. timers_shutdown();
  285. channel_free_all();
  286. return;
  287. }
  288. void
  289. test_channelpadding_consensus(void *arg)
  290. {
  291. channelpadding_decision_t decision;
  292. or_options_t *options = get_options_mutable();
  293. int64_t val;
  294. (void)arg;
  295. tor_libevent_postfork();
  296. /*
  297. * Params tested:
  298. * nf_pad_before_usage
  299. * nf_pad_relays
  300. * nf_ito_low
  301. * nf_ito_high
  302. *
  303. * Plan:
  304. * 1. Padding can be completely disabled via consensus
  305. * 2. Negotiation can't re-enable consensus-disabled padding
  306. * 3. Negotiation can't increase padding from relays beyond
  307. * consensus defaults
  308. * 4. Relay-to-relay padding can be enabled/disabled in consensus
  309. * 5. Can enable/disable padding before actually using a connection
  310. * 6. Can we control circ and TLS conn lifetime from the consensus?
  311. */
  312. channel_t *chan;
  313. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  314. monotime_init();
  315. timers_initialize();
  316. connection_array = smartlist_new();
  317. chan = (channel_t*)new_fake_channeltls(0);
  318. channel_timestamp_active(chan);
  319. current_md_consensus = current_ns_consensus
  320. = tor_malloc_zero(sizeof(networkstatus_t));
  321. current_md_consensus->net_params = smartlist_new();
  322. current_md_consensus->routerstatus_list = smartlist_new();
  323. channelpadding_new_consensus_params(current_md_consensus);
  324. get_options_mutable()->ORPort_set = 1;
  325. /* Test 1: Padding can be completely disabled via consensus */
  326. tried_to_write_cell = 0;
  327. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  328. decision = channelpadding_decide_to_pad_channel(chan);
  329. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  330. tt_assert(chan->pending_padding_callback);
  331. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  332. decision = channelpadding_decide_to_pad_channel(chan);
  333. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  334. // Wait for the timer
  335. event_base_loop(tor_libevent_get_base(), 0);
  336. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  337. tt_assert(!chan->pending_padding_callback);
  338. smartlist_add(current_md_consensus->net_params,
  339. (void*)"nf_ito_low=0");
  340. smartlist_add(current_md_consensus->net_params,
  341. (void*)"nf_ito_high=0");
  342. get_options_mutable()->ConnectionPadding = 1;
  343. channelpadding_new_consensus_params(current_md_consensus);
  344. decision = channelpadding_decide_to_pad_channel(chan);
  345. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  346. tt_assert(!chan->pending_padding_callback);
  347. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  348. tt_i64_op(val, OP_EQ, 0);
  349. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  350. tt_i64_op(val, OP_EQ, -2);
  351. /* Test 2: Negotiation can't re-enable consensus-disabled padding */
  352. channelpadding_send_enable_command(chan, 100, 200);
  353. tried_to_write_cell = 0;
  354. decision = channelpadding_decide_to_pad_channel(chan);
  355. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  356. tt_assert(!chan->pending_padding_callback);
  357. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  358. tt_i64_op(val, OP_EQ, 0);
  359. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  360. tt_i64_op(val, OP_EQ, -2);
  361. tt_assert(!chan->next_padding_time_ms);
  362. smartlist_clear(current_md_consensus->net_params);
  363. /* Test 3: Negotiation can't increase padding from relays beyond consensus
  364. * values */
  365. smartlist_add(current_md_consensus->net_params,
  366. (void*)"nf_ito_low=100");
  367. smartlist_add(current_md_consensus->net_params,
  368. (void*)"nf_ito_high=200");
  369. channelpadding_new_consensus_params(current_md_consensus);
  370. tried_to_write_cell = 0;
  371. decision = channelpadding_decide_to_pad_channel(chan);
  372. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  373. tt_assert(chan->pending_padding_callback);
  374. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  375. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  376. tt_i64_op(val, OP_GE, 100);
  377. tt_i64_op(val, OP_LE, 200);
  378. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  379. tt_i64_op(val, OP_LE, 200);
  380. // Wait for the timer
  381. event_base_loop(tor_libevent_get_base(), 0);
  382. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  383. tt_assert(!chan->pending_padding_callback);
  384. smartlist_clear(current_md_consensus->net_params);
  385. smartlist_add(current_md_consensus->net_params,
  386. (void*)"nf_ito_low=1500");
  387. smartlist_add(current_md_consensus->net_params,
  388. (void*)"nf_ito_high=4500");
  389. channelpadding_new_consensus_params(current_md_consensus);
  390. channelpadding_send_enable_command(chan, 100, 200);
  391. tried_to_write_cell = 0;
  392. decision = channelpadding_decide_to_pad_channel(chan);
  393. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  394. tt_assert(!chan->pending_padding_callback);
  395. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  396. tt_i64_op(val, OP_GE, 1500);
  397. tt_i64_op(val, OP_LE, 4500);
  398. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  399. tt_i64_op(val, OP_LE, 4500);
  400. /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
  401. /* Make this channel a relay's channel */
  402. memcpy(relay->identity_digest,
  403. ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
  404. smartlist_add(current_md_consensus->routerstatus_list, relay);
  405. tried_to_write_cell = 0;
  406. decision = channelpadding_decide_to_pad_channel(chan);
  407. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  408. tt_assert(!chan->pending_padding_callback);
  409. smartlist_add(current_md_consensus->net_params,
  410. (void*)"nf_pad_relays=1");
  411. channelpadding_new_consensus_params(current_md_consensus);
  412. decision = channelpadding_decide_to_pad_channel(chan);
  413. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  414. tt_assert(!chan->pending_padding_callback);
  415. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  416. tt_i64_op(val, OP_GE, 1500);
  417. tt_i64_op(val, OP_LE, 4500);
  418. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  419. tt_i64_op(val, OP_LE, 4500);
  420. /* Test 5: If we disable padding before channel usage, does that work? */
  421. smartlist_add(current_md_consensus->net_params,
  422. (void*)"nf_pad_before_usage=0");
  423. channelpadding_new_consensus_params(current_md_consensus);
  424. tried_to_write_cell = 0;
  425. decision = channelpadding_decide_to_pad_channel(chan);
  426. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  427. tt_assert(!chan->pending_padding_callback);
  428. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  429. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  430. tt_i64_op(val, OP_GE, 180);
  431. tt_i64_op(val, OP_LE, 180+90);
  432. val = channelpadding_get_channel_idle_timeout(chan, 0);
  433. tt_i64_op(val, OP_GE, 180);
  434. tt_i64_op(val, OP_LE, 180+90);
  435. options->ReducedConnectionPadding = 1;
  436. val = channelpadding_get_channel_idle_timeout(chan, 0);
  437. tt_i64_op(val, OP_GE, 180/2);
  438. tt_i64_op(val, OP_LE, (180+90)/2);
  439. options->ReducedConnectionPadding = 0;
  440. options->ORPort_set = 1;
  441. smartlist_add(current_md_consensus->net_params,
  442. (void*)"nf_conntimeout_relays=600");
  443. channelpadding_new_consensus_params(current_md_consensus);
  444. val = channelpadding_get_channel_idle_timeout(chan, 1);
  445. tt_i64_op(val, OP_GE, 450);
  446. tt_i64_op(val, OP_LE, 750);
  447. val = channelpadding_get_circuits_available_timeout();
  448. tt_i64_op(val, OP_GE, 30*60);
  449. tt_i64_op(val, OP_LE, 30*60*2);
  450. options->ReducedConnectionPadding = 1;
  451. smartlist_add(current_md_consensus->net_params,
  452. (void*)"nf_conntimeout_clients=600");
  453. channelpadding_new_consensus_params(current_md_consensus);
  454. val = channelpadding_get_circuits_available_timeout();
  455. tt_i64_op(val, OP_GE, 600/2);
  456. tt_i64_op(val, OP_LE, 600*2/2);
  457. options->ReducedConnectionPadding = 0;
  458. options->CircuitsAvailableTimeout = 24*60*60;
  459. val = channelpadding_get_circuits_available_timeout();
  460. tt_i64_op(val, OP_GE, 24*60*60);
  461. tt_i64_op(val, OP_LE, 24*60*60*2);
  462. done:
  463. free_fake_channeltls((channel_tls_t*)chan);
  464. smartlist_free(connection_array);
  465. smartlist_free(current_md_consensus->routerstatus_list);
  466. smartlist_free(current_ns_consensus->net_params);
  467. tor_free(relay);
  468. tor_free(current_ns_consensus);
  469. timers_shutdown();
  470. channel_free_all();
  471. return;
  472. }
  473. void
  474. test_channelpadding_negotiation(void *arg)
  475. {
  476. channelpadding_negotiate_t disable;
  477. cell_t cell;
  478. channelpadding_decision_t decision;
  479. int val;
  480. (void)arg;
  481. /* Plan:
  482. * 1. Clients reject negotiation, relays accept it.
  483. * * Bridges accept negotiation from their clients,
  484. * but not from relays.
  485. * 2. Torrc options can override client-side negotiation
  486. * 3. Test a version issue in channelpadidng cell
  487. * 4. Test channelpadding_reduced_padding
  488. */
  489. monotime_init();
  490. timers_initialize();
  491. setup_mock_network();
  492. /* Test case #1: Do the right things ignore negotiation? */
  493. /* relay-to-client case: */
  494. channelpadding_send_disable_command(relay3_client);
  495. tt_assert(client_relay3->padding_enabled);
  496. /* client-to-relay case: */
  497. get_options_mutable()->ORPort_set = 1;
  498. channelpadding_disable_padding_on_channel(client_relay3);
  499. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  500. CHANNELPADDING_WONTPAD);
  501. tt_assert(!relay3_client->padding_enabled);
  502. relay3_client->padding_enabled = 1;
  503. client_relay3->padding_enabled = 1;
  504. /* Bridge case from relay */
  505. get_options_mutable()->BridgeRelay = 1;
  506. channelpadding_disable_padding_on_channel(relay2_relay1);
  507. tt_assert(relay1_relay2->padding_enabled);
  508. /* Bridge case from client */
  509. channelpadding_disable_padding_on_channel(client_relay3);
  510. tt_assert(!relay3_client->padding_enabled);
  511. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  512. CHANNELPADDING_WONTPAD);
  513. relay3_client->padding_enabled = 1;
  514. client_relay3->padding_enabled = 1;
  515. get_options_mutable()->BridgeRelay = 0;
  516. get_options_mutable()->ORPort_set = 0;
  517. /* Test case #2: Torrc options */
  518. /* ConnectionPadding auto; Relay doesn't suport us */
  519. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  520. relay3_client->padding_enabled = 0;
  521. tried_to_write_cell = 0;
  522. decision = channelpadding_decide_to_pad_channel(relay3_client);
  523. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  524. tt_assert(!relay3_client->pending_padding_callback);
  525. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  526. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  527. relay3_client->padding_enabled = 1;
  528. /* ConnectionPadding 1; Relay doesn't suport us */
  529. get_options_mutable()->ConnectionPadding = 1;
  530. tried_to_write_cell = 0;
  531. decision = channelpadding_decide_to_pad_channel(client_relay3);
  532. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  533. tt_assert(!client_relay3->pending_padding_callback);
  534. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  535. get_options_mutable()->ConnectionPadding = 0;
  536. /* Test case #3: Test a version issue in channelpadding cell */
  537. get_options_mutable()->ORPort_set = 1;
  538. client_relay3->padding_enabled = 1;
  539. relay3_client->padding_enabled = 1;
  540. memset(&cell, 0, sizeof(cell_t));
  541. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  542. cell.command = CELL_PADDING_NEGOTIATE;
  543. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  544. disable.version = 1;
  545. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  546. client_relay3->write_cell(client_relay3, &cell);
  547. tt_assert(relay3_client->padding_enabled);
  548. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  549. OP_EQ, -1);
  550. tt_assert(client_relay3->padding_enabled);
  551. disable.version = 0;
  552. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  553. client_relay3->write_cell(client_relay3, &cell);
  554. tt_assert(!relay3_client->padding_enabled);
  555. /* Test case 4: Reducing padding actually reduces it */
  556. relay3_client->padding_enabled = 1;
  557. client_relay3->padding_enabled = 1;
  558. decision = channelpadding_decide_to_pad_channel(relay3_client);
  559. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  560. channelpadding_reduce_padding_on_channel(client_relay3);
  561. tried_to_write_cell = 0;
  562. decision = channelpadding_decide_to_pad_channel(relay3_client);
  563. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  564. get_options_mutable()->ORPort_set = 0;
  565. decision = channelpadding_decide_to_pad_channel(client_relay3);
  566. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  567. tt_assert(!client_relay3->pending_padding_callback);
  568. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  569. tt_int_op(val, OP_GE, 9000);
  570. tt_int_op(val, OP_LE, 14000);
  571. int64_t val64 =
  572. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  573. tt_i64_op(val64, OP_LE, 14000);
  574. done:
  575. free_mock_network();
  576. timers_shutdown();
  577. channel_free_all();
  578. return;
  579. }
  580. void
  581. test_channelpadding_decide_to_pad_channel(void *arg)
  582. {
  583. channelpadding_decision_t decision;
  584. /**
  585. * Test case plan:
  586. *
  587. * 1. Channel that has "sent a packet" before the timeout.
  588. * + We should decide to pad later
  589. * 2. Channel that has not "sent a packet" before the timeout:
  590. * 2a. Not within 1.1s of the timeout.
  591. * + We should decide to pad later
  592. * 2b. Within 1.1s of the timemout.
  593. * + We should schedule padding
  594. * + We should get feedback that we wrote a cell
  595. * 2c. Within 0.1s of the timeout.
  596. * + We should schedule padding
  597. * + We should get feedback that we wrote a cell
  598. * 2d. Channel that asks to pad while timeout is scheduled
  599. * + We should schedule padding
  600. * + We should get feedback that we wrote a cell
  601. * 2e. 0s of the timeout
  602. * + We should send padding immediately
  603. * + We should get feedback that we wrote a cell
  604. * 2f. <0s of the timeout
  605. * + We should send padding immediately
  606. * + We should get feedback that we wrote a cell
  607. * 3. Channel that sends a packet while timeout is scheduled
  608. * + We should not get feedback that we wrote a cell
  609. * 4. Channel that closes while timeout is scheduled
  610. * + We should not get feedback that we wrote a cell
  611. * 5. Make sure the channel still would work if repaired
  612. * + We should be able to schedule padding and resend
  613. * 6. Channel is not used for full circuits
  614. * 7. Channel that disappears while timeout is scheduled
  615. * + We should not send padding
  616. */
  617. channel_t *chan;
  618. connection_array = smartlist_new();
  619. (void)arg;
  620. tor_libevent_postfork();
  621. monotime_init();
  622. timers_initialize();
  623. setup_full_capture_of_logs(LOG_WARN);
  624. channelpadding_new_consensus_params(NULL);
  625. chan = (channel_t*)new_fake_channeltls(0);
  626. channel_timestamp_active(chan);
  627. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  628. tried_to_write_cell = 0;
  629. decision = channelpadding_decide_to_pad_channel(chan);
  630. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  631. tt_assert(!chan->pending_padding_callback);
  632. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  633. /* Test case #2a: > 1.1s until timeout */
  634. tried_to_write_cell = 0;
  635. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1200;
  636. decision = channelpadding_decide_to_pad_channel(chan);
  637. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  638. tt_assert(!chan->pending_padding_callback);
  639. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  640. /* Test case #2b: >= 1.0s until timeout */
  641. tried_to_write_cell = 0;
  642. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1000;
  643. decision = channelpadding_decide_to_pad_channel(chan);
  644. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  645. tt_assert(chan->pending_padding_callback);
  646. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  647. // Wait for the timer from case #2b
  648. event_base_loop(tor_libevent_get_base(), 0);
  649. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  650. tt_assert(!chan->pending_padding_callback);
  651. /* Test case #2c: > 0.1s until timeout */
  652. tried_to_write_cell = 0;
  653. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  654. decision = channelpadding_decide_to_pad_channel(chan);
  655. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  656. tt_assert(chan->pending_padding_callback);
  657. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  658. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  659. decision = channelpadding_decide_to_pad_channel(chan);
  660. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  661. // Wait for the timer
  662. event_base_loop(tor_libevent_get_base(), 0);
  663. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  664. tt_assert(!chan->pending_padding_callback);
  665. /* Test case #2e: 0s until timeout */
  666. tried_to_write_cell = 0;
  667. chan->next_padding_time_ms = monotime_coarse_absolute_msec();
  668. decision = channelpadding_decide_to_pad_channel(chan);
  669. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  670. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  671. tt_assert(!chan->pending_padding_callback);
  672. /* Test case #2f: <0s until timeout */
  673. tried_to_write_cell = 0;
  674. chan->next_padding_time_ms = monotime_coarse_absolute_msec() - 100;
  675. decision = channelpadding_decide_to_pad_channel(chan);
  676. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  677. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  678. tt_assert(!chan->pending_padding_callback);
  679. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  680. tried_to_write_cell = 0;
  681. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  682. decision = channelpadding_decide_to_pad_channel(chan);
  683. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  684. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  685. tt_assert(chan->pending_padding_callback);
  686. // Pretend the channel sent a packet
  687. channel_timestamp_active(chan);
  688. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  689. dummy_nop_timer();
  690. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  691. tt_assert(!chan->pending_padding_callback);
  692. /* Test case #4: Channel that closes while a timeout is scheduled */
  693. tried_to_write_cell = 0;
  694. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  695. decision = channelpadding_decide_to_pad_channel(chan);
  696. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  697. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  698. tt_assert(chan->pending_padding_callback);
  699. // Pretend the channel is temporarily down
  700. chan->state = CHANNEL_STATE_MAINT;
  701. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  702. dummy_nop_timer();
  703. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  704. tt_assert(!chan->pending_padding_callback);
  705. chan->state = CHANNEL_STATE_OPEN;
  706. /* Test case #5: Make sure previous test case didn't break everything */
  707. tried_to_write_cell = 0;
  708. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  709. decision = channelpadding_decide_to_pad_channel(chan);
  710. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  711. tt_assert(chan->pending_padding_callback);
  712. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  713. // Wait for the timer
  714. event_base_loop(tor_libevent_get_base(), 0);
  715. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  716. tt_assert(!chan->pending_padding_callback);
  717. /* Test case #6. Channel is not used for full circuits */
  718. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  719. decision = channelpadding_decide_to_pad_channel(chan);
  720. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  721. tt_assert(!chan->pending_padding_callback);
  722. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  723. /* Test case #7. Channel is closed while timeout is scheduled.
  724. *
  725. * NOTE: This test deliberately breaks the channel callback mechanism.
  726. * It must be last.
  727. */
  728. tried_to_write_cell = 0;
  729. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  730. decision = channelpadding_decide_to_pad_channel(chan);
  731. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  732. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  733. tt_assert(chan->pending_padding_callback);
  734. // Close the connection while the timer is scheduled
  735. free_fake_channeltls((channel_tls_t*)chan);
  736. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  737. dummy_nop_timer();
  738. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  739. done:
  740. smartlist_free(connection_array);
  741. teardown_capture_of_logs();
  742. timers_shutdown();
  743. channel_free_all();
  744. return;
  745. }
  746. #define TEST_CHANNELPADDING(name, flags) \
  747. { #name, test_##name, (flags), NULL, NULL }
  748. struct testcase_t channelpadding_tests[] = {
  749. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  750. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  751. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  752. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  753. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  754. END_OF_TESTCASES
  755. };