test_channelpadding.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. */
  304. channel_t *chan;
  305. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  306. monotime_init();
  307. timers_initialize();
  308. connection_array = smartlist_new();
  309. chan = (channel_t*)new_fake_channeltls(0);
  310. channel_timestamp_active(chan);
  311. current_md_consensus = current_ns_consensus
  312. = tor_malloc_zero(sizeof(networkstatus_t));
  313. current_md_consensus->net_params = smartlist_new();
  314. current_md_consensus->routerstatus_list = smartlist_new();
  315. get_options_mutable()->ORPort_set = 1;
  316. /* Test 1: Padding can be completely disabled via consensus */
  317. tried_to_write_cell = 0;
  318. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  319. decision = channelpadding_decide_to_pad_channel(chan);
  320. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  321. tt_assert(chan->pending_padding_callback);
  322. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  323. decision = channelpadding_decide_to_pad_channel(chan);
  324. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  325. // Wait for the timer
  326. event_base_loop(tor_libevent_get_base(), 0);
  327. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  328. tt_assert(!chan->pending_padding_callback);
  329. smartlist_add(current_md_consensus->net_params,
  330. (void*)"nf_ito_low=0");
  331. smartlist_add(current_md_consensus->net_params,
  332. (void*)"nf_ito_high=0");
  333. get_options_mutable()->ConnectionPadding = 1;
  334. decision = channelpadding_decide_to_pad_channel(chan);
  335. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  336. tt_assert(!chan->pending_padding_callback);
  337. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  338. tt_int_op(val, OP_EQ, 0);
  339. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  340. tt_int_op(val, OP_EQ, -2);
  341. /* Test 2: Negotiation can't re-enable consensus-disabled padding */
  342. channelpadding_send_enable_command(chan, 100, 200);
  343. tried_to_write_cell = 0;
  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_int_op(val, OP_EQ, 0);
  349. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  350. tt_int_op(val, OP_EQ, -2);
  351. tt_assert(!chan->next_padding_time_ms);
  352. smartlist_clear(current_md_consensus->net_params);
  353. /* Test 3: Negotiation can't increase padding from relays beyond consensus
  354. * values */
  355. smartlist_add(current_md_consensus->net_params,
  356. (void*)"nf_ito_low=100");
  357. smartlist_add(current_md_consensus->net_params,
  358. (void*)"nf_ito_high=200");
  359. tried_to_write_cell = 0;
  360. decision = channelpadding_decide_to_pad_channel(chan);
  361. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  362. tt_assert(chan->pending_padding_callback);
  363. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  364. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  365. tt_int_op(val, OP_GE, 100);
  366. tt_int_op(val, OP_LE, 200);
  367. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  368. tt_int_op(val, OP_LE, 200);
  369. // Wait for the timer
  370. event_base_loop(tor_libevent_get_base(), 0);
  371. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  372. tt_assert(!chan->pending_padding_callback);
  373. smartlist_clear(current_md_consensus->net_params);
  374. smartlist_add(current_md_consensus->net_params,
  375. (void*)"nf_ito_low=1500");
  376. smartlist_add(current_md_consensus->net_params,
  377. (void*)"nf_ito_high=4500");
  378. channelpadding_send_enable_command(chan, 100, 200);
  379. tried_to_write_cell = 0;
  380. decision = channelpadding_decide_to_pad_channel(chan);
  381. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  382. tt_assert(!chan->pending_padding_callback);
  383. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  384. tt_int_op(val, OP_GE, 1500);
  385. tt_int_op(val, OP_LE, 4500);
  386. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  387. tt_int_op(val, OP_LE, 4500);
  388. /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
  389. /* Make this channel a relay's channel */
  390. memcpy(relay->identity_digest,
  391. ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
  392. smartlist_add(current_md_consensus->routerstatus_list, relay);
  393. tried_to_write_cell = 0;
  394. decision = channelpadding_decide_to_pad_channel(chan);
  395. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  396. tt_assert(!chan->pending_padding_callback);
  397. smartlist_add(current_md_consensus->net_params,
  398. (void*)"nf_pad_relays=1");
  399. decision = channelpadding_decide_to_pad_channel(chan);
  400. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  401. tt_assert(!chan->pending_padding_callback);
  402. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  403. tt_int_op(val, OP_GE, 1500);
  404. tt_int_op(val, OP_LE, 4500);
  405. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  406. tt_int_op(val, OP_LE, 4500);
  407. /* Test 5: If we disable padding before channel usage, does that work? */
  408. smartlist_add(current_md_consensus->net_params,
  409. (void*)"nf_pad_before_usage=0");
  410. tried_to_write_cell = 0;
  411. decision = channelpadding_decide_to_pad_channel(chan);
  412. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  413. tt_assert(!chan->pending_padding_callback);
  414. done:
  415. free_fake_channeltls((channel_tls_t*)chan);
  416. smartlist_free(connection_array);
  417. smartlist_free(current_md_consensus->routerstatus_list);
  418. smartlist_free(current_ns_consensus->net_params);
  419. tor_free(relay);
  420. tor_free(current_ns_consensus);
  421. timers_shutdown();
  422. channel_free_all();
  423. return;
  424. }
  425. void
  426. test_channelpadding_negotiation(void *arg)
  427. {
  428. channelpadding_negotiate_t disable;
  429. cell_t cell;
  430. channelpadding_decision_t decision;
  431. int val;
  432. (void)arg;
  433. /* Plan:
  434. * 1. Clients reject negotiation, relays accept it.
  435. * * Bridges accept negotiation from their clients,
  436. * but not from relays.
  437. * 2. Torrc options can override client-side negotiation
  438. * 3. Test a version issue in channelpadidng cell
  439. * 4. Test channelpadding_reduced_padding
  440. */
  441. monotime_init();
  442. timers_initialize();
  443. setup_mock_network();
  444. /* Test case #1: Do the right things ignore negotiation? */
  445. /* relay-to-client case: */
  446. channelpadding_send_disable_command(relay3_client);
  447. tt_assert(client_relay3->padding_enabled);
  448. /* client-to-relay case: */
  449. get_options_mutable()->ORPort_set = 1;
  450. channelpadding_disable_padding_on_channel(client_relay3);
  451. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  452. CHANNELPADDING_WONTPAD);
  453. tt_assert(!relay3_client->padding_enabled);
  454. relay3_client->padding_enabled = 1;
  455. client_relay3->padding_enabled = 1;
  456. /* Bridge case from relay */
  457. get_options_mutable()->BridgeRelay = 1;
  458. channelpadding_disable_padding_on_channel(relay2_relay1);
  459. tt_assert(relay1_relay2->padding_enabled);
  460. /* Bridge case from client */
  461. channelpadding_disable_padding_on_channel(client_relay3);
  462. tt_assert(!relay3_client->padding_enabled);
  463. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  464. CHANNELPADDING_WONTPAD);
  465. relay3_client->padding_enabled = 1;
  466. client_relay3->padding_enabled = 1;
  467. get_options_mutable()->BridgeRelay = 0;
  468. get_options_mutable()->ORPort_set = 0;
  469. /* Test case #2: Torrc options */
  470. /* ConnectionPadding auto; Relay doesn't suport us */
  471. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  472. relay3_client->padding_enabled = 0;
  473. tried_to_write_cell = 0;
  474. decision = channelpadding_decide_to_pad_channel(relay3_client);
  475. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  476. tt_assert(!relay3_client->pending_padding_callback);
  477. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  478. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  479. relay3_client->padding_enabled = 1;
  480. /* ConnectionPadding 1; Relay doesn't suport us */
  481. get_options_mutable()->ConnectionPadding = 1;
  482. tried_to_write_cell = 0;
  483. decision = channelpadding_decide_to_pad_channel(client_relay3);
  484. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  485. tt_assert(!client_relay3->pending_padding_callback);
  486. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  487. get_options_mutable()->ConnectionPadding = 0;
  488. /* Test case #3: Test a version issue in channelpadding cell */
  489. get_options_mutable()->ORPort_set = 1;
  490. client_relay3->padding_enabled = 1;
  491. relay3_client->padding_enabled = 1;
  492. memset(&cell, 0, sizeof(cell_t));
  493. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  494. cell.command = CELL_PADDING_NEGOTIATE;
  495. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  496. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  497. ((channelpadding_negotiate_t*)cell.payload)->version = 1;
  498. client_relay3->write_cell(client_relay3, &cell);
  499. tt_assert(relay3_client->padding_enabled);
  500. disable.version = 1;
  501. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  502. OP_EQ, -1);
  503. tt_assert(client_relay3->padding_enabled);
  504. ((channelpadding_negotiate_t*)cell.payload)->version = 0;
  505. disable.version = 0;
  506. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  507. client_relay3->write_cell(client_relay3, &cell);
  508. tt_assert(!relay3_client->padding_enabled);
  509. /* Test case 4: Reducing padding actually reduces it */
  510. relay3_client->padding_enabled = 1;
  511. client_relay3->padding_enabled = 1;
  512. decision = channelpadding_decide_to_pad_channel(relay3_client);
  513. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  514. channelpadding_reduce_padding_on_channel(client_relay3);
  515. tried_to_write_cell = 0;
  516. decision = channelpadding_decide_to_pad_channel(relay3_client);
  517. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  518. get_options_mutable()->ORPort_set = 0;
  519. decision = channelpadding_decide_to_pad_channel(client_relay3);
  520. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  521. tt_assert(!client_relay3->pending_padding_callback);
  522. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  523. tt_int_op(val, OP_GE, 9000);
  524. tt_int_op(val, OP_LE, 14000);
  525. val = channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  526. tt_int_op(val, OP_LE, 14000);
  527. done:
  528. free_mock_network();
  529. timers_shutdown();
  530. channel_free_all();
  531. return;
  532. }
  533. void
  534. test_channelpadding_decide_to_pad_channel(void *arg)
  535. {
  536. channelpadding_decision_t decision;
  537. /**
  538. * Test case plan:
  539. *
  540. * 1. Channel that has "sent a packet" before the timeout.
  541. * + We should decide to pad later
  542. * 2. Channel that has not "sent a packet" before the timeout:
  543. * 2a. Not within 1.1s of the timeout.
  544. * + We should decide to pad later
  545. * 2b. Within 1.1s of the timemout.
  546. * + We should schedule padding
  547. * + We should get feedback that we wrote a cell
  548. * 2c. Within 0.1s of the timeout.
  549. * + We should schedule padding
  550. * + We should get feedback that we wrote a cell
  551. * 2d. Channel that asks to pad while timeout is scheduled
  552. * + We should schedule padding
  553. * + We should get feedback that we wrote a cell
  554. * 2e. 0s of the timeout
  555. * + We should send padding immediately
  556. * + We should get feedback that we wrote a cell
  557. * 2f. <0s of the timeout
  558. * + We should send padding immediately
  559. * + We should get feedback that we wrote a cell
  560. * 3. Channel that sends a packet while timeout is scheduled
  561. * + We should not get feedback that we wrote a cell
  562. * 4. Channel that closes while timeout is scheduled
  563. * + We should not get feedback that we wrote a cell
  564. * 5. Make sure the channel still would work if repaired
  565. * + We should be able to schedule padding and resend
  566. * 6. Channel is not used for full circuits
  567. * 7. Channel that disappears while timeout is scheduled
  568. * + We should not send padding
  569. */
  570. channel_t *chan;
  571. connection_array = smartlist_new();
  572. (void)arg;
  573. monotime_init();
  574. timers_initialize();
  575. chan = (channel_t*)new_fake_channeltls(0);
  576. channel_timestamp_active(chan);
  577. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  578. tried_to_write_cell = 0;
  579. decision = channelpadding_decide_to_pad_channel(chan);
  580. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  581. tt_assert(!chan->pending_padding_callback);
  582. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  583. /* Test case #2a: > 1.1s until timeout */
  584. tried_to_write_cell = 0;
  585. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1200;
  586. decision = channelpadding_decide_to_pad_channel(chan);
  587. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  588. tt_assert(!chan->pending_padding_callback);
  589. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  590. /* Test case #2b: >= 1.0s until timeout */
  591. tried_to_write_cell = 0;
  592. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1000;
  593. decision = channelpadding_decide_to_pad_channel(chan);
  594. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  595. tt_assert(chan->pending_padding_callback);
  596. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  597. // Wait for the timer from case #2b
  598. event_base_loop(tor_libevent_get_base(), 0);
  599. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  600. tt_assert(!chan->pending_padding_callback);
  601. /* Test case #2c: > 0.1s until timeout */
  602. tried_to_write_cell = 0;
  603. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  604. decision = channelpadding_decide_to_pad_channel(chan);
  605. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  606. tt_assert(chan->pending_padding_callback);
  607. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  608. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  609. decision = channelpadding_decide_to_pad_channel(chan);
  610. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  611. // Wait for the timer
  612. event_base_loop(tor_libevent_get_base(), 0);
  613. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  614. tt_assert(!chan->pending_padding_callback);
  615. /* Test case #2e: 0s until timeout */
  616. tried_to_write_cell = 0;
  617. chan->next_padding_time_ms = monotime_coarse_absolute_msec();
  618. decision = channelpadding_decide_to_pad_channel(chan);
  619. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  620. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  621. tt_assert(!chan->pending_padding_callback);
  622. /* Test case #2f: <0s until timeout */
  623. tried_to_write_cell = 0;
  624. chan->next_padding_time_ms = monotime_coarse_absolute_msec() - 100;
  625. decision = channelpadding_decide_to_pad_channel(chan);
  626. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  627. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  628. tt_assert(!chan->pending_padding_callback);
  629. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  630. tried_to_write_cell = 0;
  631. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  632. decision = channelpadding_decide_to_pad_channel(chan);
  633. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  634. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  635. tt_assert(chan->pending_padding_callback);
  636. // Pretend the channel sent a packet
  637. channel_timestamp_active(chan);
  638. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  639. dummy_nop_timer();
  640. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  641. tt_assert(!chan->pending_padding_callback);
  642. /* Test case #4: Channel that closes while a timeout is scheduled */
  643. tried_to_write_cell = 0;
  644. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  645. decision = channelpadding_decide_to_pad_channel(chan);
  646. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  647. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  648. tt_assert(chan->pending_padding_callback);
  649. // Pretend the channel is temporarily down
  650. chan->state = CHANNEL_STATE_MAINT;
  651. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  652. dummy_nop_timer();
  653. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  654. tt_assert(!chan->pending_padding_callback);
  655. chan->state = CHANNEL_STATE_OPEN;
  656. /* Test case #5: Make sure previous test case didn't break everything */
  657. tried_to_write_cell = 0;
  658. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  659. decision = channelpadding_decide_to_pad_channel(chan);
  660. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  661. tt_assert(chan->pending_padding_callback);
  662. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  663. // Wait for the timer
  664. event_base_loop(tor_libevent_get_base(), 0);
  665. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  666. tt_assert(!chan->pending_padding_callback);
  667. /* Test case #6. Channel is not used for full circuits */
  668. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  669. decision = channelpadding_decide_to_pad_channel(chan);
  670. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  671. tt_assert(!chan->pending_padding_callback);
  672. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  673. /* Test case #7. Channel is closed while timeout is scheduled.
  674. *
  675. * NOTE: This test deliberately breaks the channel callback mechanism.
  676. * It must be last.
  677. */
  678. tried_to_write_cell = 0;
  679. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  680. decision = channelpadding_decide_to_pad_channel(chan);
  681. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  682. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  683. tt_assert(chan->pending_padding_callback);
  684. // Close the connection while the timer is scheduled
  685. free_fake_channeltls((channel_tls_t*)chan);
  686. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  687. dummy_nop_timer();
  688. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  689. done:
  690. smartlist_free(connection_array);
  691. timers_shutdown();
  692. channel_free_all();
  693. return;
  694. }
  695. #define TEST_CHANNELPADDING(name, flags) \
  696. { #name, test_##name, (flags), NULL, NULL }
  697. struct testcase_t channelpadding_tests[] = {
  698. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  699. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  700. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  701. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  702. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  703. END_OF_TESTCASES
  704. };