test_channelpadding.c 30 KB

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