test_channelpadding.c 30 KB

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