test_channelpadding.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. #define TOR_CHANNEL_INTERNAL_
  2. #define MAIN_PRIVATE
  3. #define NETWORKSTATUS_PRIVATE
  4. #define TOR_TIMERS_PRIVATE
  5. #include "or.h"
  6. #include "test.h"
  7. #include "testsupport.h"
  8. #include "connection.h"
  9. #include "connection_or.h"
  10. #include "channel.h"
  11. #include "channeltls.h"
  12. #include "channelpadding.h"
  13. #include "compat_libevent.h"
  14. #include "config.h"
  15. #include <event2/event.h>
  16. #include "compat_time.h"
  17. #include "main.h"
  18. #include "networkstatus.h"
  19. #include "log_test_helpers.h"
  20. int channelpadding_get_netflow_inactive_timeout_ms(channel_t *chan);
  21. int64_t channelpadding_compute_time_until_pad_for_netflow(channel_t *chan);
  22. int channelpadding_send_disable_command(channel_t*);
  23. int channelpadding_find_timerslot(channel_t *chan);
  24. void test_channelpadding_timers(void *arg);
  25. void test_channelpadding_consensus(void *arg);
  26. void test_channelpadding_negotiation(void *arg);
  27. void test_channelpadding_decide_to_pad_channel(void *arg);
  28. void test_channelpadding_killonehop(void *arg);
  29. void dummy_nop_timer(void);
  30. #define NSEC_PER_MSEC (1000*1000)
  31. /* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */
  32. static int fake_tortls = 0; /* Bleh... */
  33. static int dont_stop_libevent = 0;
  34. // From test_channel.c
  35. channel_t * new_fake_channel(void);
  36. void free_fake_channel(channel_t*);
  37. static int
  38. mock_channel_has_queued_writes(channel_t *chan)
  39. {
  40. (void)chan;
  41. return 0;
  42. }
  43. static int tried_to_write_cell = 0;
  44. static channel_t *relay1_relay2;
  45. static channel_t *relay2_relay1;
  46. static channel_t *relay3_client;
  47. static channel_t *client_relay3;
  48. static int
  49. mock_channel_write_cell_relay2(channel_t *chan, cell_t *cell)
  50. {
  51. (void)chan;
  52. tried_to_write_cell++;
  53. channel_tls_handle_cell(cell, ((channel_tls_t*)relay1_relay2)->conn);
  54. event_base_loopbreak(tor_libevent_get_base());
  55. return 0;
  56. }
  57. static int
  58. mock_channel_write_cell_relay1(channel_t *chan, cell_t *cell)
  59. {
  60. (void)chan;
  61. tried_to_write_cell++;
  62. channel_tls_handle_cell(cell, ((channel_tls_t*)relay2_relay1)->conn);
  63. event_base_loopbreak(tor_libevent_get_base());
  64. return 0;
  65. }
  66. static int
  67. mock_channel_write_cell_relay3(channel_t *chan, cell_t *cell)
  68. {
  69. (void)chan;
  70. tried_to_write_cell++;
  71. channel_tls_handle_cell(cell, ((channel_tls_t*)client_relay3)->conn);
  72. event_base_loopbreak(tor_libevent_get_base());
  73. return 0;
  74. }
  75. static int
  76. mock_channel_write_cell_client(channel_t *chan, cell_t *cell)
  77. {
  78. (void)chan;
  79. tried_to_write_cell++;
  80. channel_tls_handle_cell(cell, ((channel_tls_t*)relay3_client)->conn);
  81. event_base_loopbreak(tor_libevent_get_base());
  82. return 0;
  83. }
  84. static int
  85. mock_channel_write_cell(channel_t *chan, cell_t *cell)
  86. {
  87. tried_to_write_cell++;
  88. channel_tls_handle_cell(cell, ((channel_tls_t*)chan)->conn);
  89. if (!dont_stop_libevent)
  90. event_base_loopbreak(tor_libevent_get_base());
  91. return 0;
  92. }
  93. static void
  94. setup_fake_connection_for_channel(channel_tls_t *chan)
  95. {
  96. or_connection_t *conn = (or_connection_t*)connection_new(CONN_TYPE_OR,
  97. AF_INET);
  98. conn->base_.conn_array_index = smartlist_len(connection_array);
  99. smartlist_add(connection_array, conn);
  100. conn->chan = chan;
  101. chan->conn = conn;
  102. conn->base_.magic = OR_CONNECTION_MAGIC;
  103. conn->base_.state = OR_CONN_STATE_OPEN;
  104. conn->base_.type = CONN_TYPE_OR;
  105. conn->base_.socket_family = AF_INET;
  106. conn->base_.address = tor_strdup("<fake>");
  107. conn->base_.port = 4242;
  108. conn->tls = (tor_tls_t *)((void *)(&fake_tortls));
  109. conn->link_proto = MIN_LINK_PROTO_FOR_CHANNEL_PADDING;
  110. connection_or_set_canonical(conn, 1);
  111. }
  112. static channel_tls_t *
  113. new_fake_channeltls(uint8_t id)
  114. {
  115. channel_tls_t *chan = tor_realloc(new_fake_channel(), sizeof(channel_tls_t));
  116. chan->base_.magic = TLS_CHAN_MAGIC;
  117. setup_fake_connection_for_channel(chan);
  118. chan->base_.channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  119. chan->base_.has_queued_writes = mock_channel_has_queued_writes;
  120. chan->base_.write_cell = mock_channel_write_cell;
  121. chan->base_.padding_enabled = 1;
  122. chan->base_.identity_digest[0] = id;
  123. channel_register(&chan->base_);
  124. return chan;
  125. }
  126. static void
  127. free_fake_channeltls(channel_tls_t *chan)
  128. {
  129. channel_unregister(&chan->base_);
  130. tor_free(((channel_tls_t*)chan)->conn->base_.address);
  131. buf_free(((channel_tls_t*)chan)->conn->base_.inbuf);
  132. buf_free(((channel_tls_t*)chan)->conn->base_.outbuf);
  133. tor_free(((channel_tls_t*)chan)->conn);
  134. timer_free(chan->base_.padding_timer);
  135. channel_handle_free(chan->base_.timer_handle);
  136. channel_handles_clear(&chan->base_);
  137. free_fake_channel(&chan->base_);
  138. return;
  139. }
  140. static void
  141. setup_mock_consensus(void)
  142. {
  143. current_md_consensus = current_ns_consensus
  144. = tor_malloc_zero(sizeof(networkstatus_t));
  145. current_md_consensus->net_params = smartlist_new();
  146. current_md_consensus->routerstatus_list = smartlist_new();
  147. channelpadding_new_consensus_params(current_md_consensus);
  148. }
  149. static void
  150. free_mock_consensus(void)
  151. {
  152. SMARTLIST_FOREACH(current_md_consensus->routerstatus_list, void *, r,
  153. tor_free(r));
  154. smartlist_free(current_md_consensus->routerstatus_list);
  155. smartlist_free(current_ns_consensus->net_params);
  156. tor_free(current_ns_consensus);
  157. }
  158. static void
  159. setup_mock_network(void)
  160. {
  161. routerstatus_t *relay;
  162. connection_array = smartlist_new();
  163. relay1_relay2 = (channel_t*)new_fake_channeltls(2);
  164. relay1_relay2->write_cell = mock_channel_write_cell_relay1;
  165. channel_timestamp_active(relay1_relay2);
  166. relay = tor_malloc_zero(sizeof(routerstatus_t));
  167. relay->identity_digest[0] = 1;
  168. smartlist_add(current_md_consensus->routerstatus_list, relay);
  169. relay2_relay1 = (channel_t*)new_fake_channeltls(1);
  170. relay2_relay1->write_cell = mock_channel_write_cell_relay2;
  171. channel_timestamp_active(relay2_relay1);
  172. relay = tor_malloc_zero(sizeof(routerstatus_t));
  173. relay->identity_digest[0] = 2;
  174. smartlist_add(current_md_consensus->routerstatus_list, relay);
  175. relay3_client = (channel_t*)new_fake_channeltls(0);
  176. relay3_client->write_cell = mock_channel_write_cell_relay3;
  177. relay3_client->is_client = 1;
  178. channel_timestamp_active(relay3_client);
  179. relay = tor_malloc_zero(sizeof(routerstatus_t));
  180. relay->identity_digest[0] = 3;
  181. smartlist_add(current_md_consensus->routerstatus_list, relay);
  182. client_relay3 = (channel_t*)new_fake_channeltls(3);
  183. client_relay3->write_cell = mock_channel_write_cell_client;
  184. channel_timestamp_active(client_relay3);
  185. channel_do_open_actions(relay1_relay2);
  186. channel_do_open_actions(relay2_relay1);
  187. channel_do_open_actions(relay3_client);
  188. channel_do_open_actions(client_relay3);
  189. }
  190. static void
  191. free_mock_network(void)
  192. {
  193. free_fake_channeltls((channel_tls_t*)relay1_relay2);
  194. free_fake_channeltls((channel_tls_t*)relay2_relay1);
  195. free_fake_channeltls((channel_tls_t*)relay3_client);
  196. free_fake_channeltls((channel_tls_t*)client_relay3);
  197. smartlist_free(connection_array);
  198. }
  199. static void
  200. dummy_timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
  201. {
  202. (void)t; (void)arg; (void)now_mono;
  203. event_base_loopbreak(tor_libevent_get_base());
  204. return;
  205. }
  206. // This hack adds a dummy timer so that the libevent base loop
  207. // actually returns when we don't expect any timers to fire. Otherwise,
  208. // the global_timer_event gets scheduled an hour from now, and the
  209. // base loop never returns.
  210. void
  211. dummy_nop_timer(void)
  212. {
  213. tor_timer_t *dummy_timer = timer_new(dummy_timer_cb, NULL);
  214. struct timeval timeout;
  215. timeout.tv_sec = 1;
  216. timeout.tv_usec = 0;
  217. timer_schedule(dummy_timer, &timeout);
  218. event_base_loop(tor_libevent_get_base(), 0);
  219. timer_free(dummy_timer);
  220. }
  221. #define CHANNELPADDING_MAX_TIMERS 25
  222. #define CHANNELS_TO_TEST (CHANNELPADDING_MAX_TIMERS*4)
  223. /**
  224. * Tests to ensure that we handle more than the max number of pending
  225. * timers properly.
  226. */
  227. void
  228. test_channelpadding_timers(void *arg)
  229. {
  230. channelpadding_decision_t decision;
  231. channel_t *chans[CHANNELS_TO_TEST];
  232. int64_t new_time;
  233. (void)arg;
  234. tor_libevent_postfork();
  235. connection_array = smartlist_new();
  236. monotime_init();
  237. monotime_enable_test_mocking();
  238. monotime_set_mock_time_nsec(1);
  239. monotime_coarse_set_mock_time_nsec(1);
  240. timers_initialize();
  241. channelpadding_new_consensus_params(NULL);
  242. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  243. chans[i] = (channel_t*)new_fake_channeltls(0);
  244. channel_timestamp_active(chans[i]);
  245. }
  246. for (int j = 0; j < 2; j++) {
  247. tried_to_write_cell = 0;
  248. int i = 0;
  249. /* This loop fills our timerslot array with timers of increasing time
  250. * until they fire */
  251. for (; i < CHANNELPADDING_MAX_TIMERS; i++) {
  252. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec()
  253. + 10 + i*4;
  254. decision = channelpadding_decide_to_pad_channel(chans[i]);
  255. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  256. tt_assert(chans[i]->pending_padding_callback);
  257. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  258. }
  259. /* This loop should add timers to the first position in the timerslot
  260. * array, since its timeout is before all other timers. */
  261. for (; i < CHANNELS_TO_TEST/3; i++) {
  262. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 1;
  263. decision = channelpadding_decide_to_pad_channel(chans[i]);
  264. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  265. tt_assert(chans[i]->pending_padding_callback);
  266. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  267. }
  268. /* This loop should add timers to our existing lists in a weak
  269. * pseudorandom pattern. It ensures that the lists can grow with multiple
  270. * timers in them. */
  271. for (; i < CHANNELS_TO_TEST/2; i++) {
  272. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 10 +
  273. i*3 % CHANNELPADDING_MAX_TIMERS;
  274. decision = channelpadding_decide_to_pad_channel(chans[i]);
  275. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  276. tt_assert(chans[i]->pending_padding_callback);
  277. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  278. }
  279. /* This loop should add timers to the last position in the timerslot
  280. * array, since its timeout is after all other timers. */
  281. for (; i < CHANNELS_TO_TEST; i++) {
  282. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 500 +
  283. i % CHANNELPADDING_MAX_TIMERS;
  284. decision = channelpadding_decide_to_pad_channel(chans[i]);
  285. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  286. tt_assert(chans[i]->pending_padding_callback);
  287. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  288. }
  289. // Wait for the timers and then kill the event loop.
  290. new_time = (monotime_coarse_absolute_msec()+1001)*NSEC_PER_MSEC;
  291. monotime_coarse_set_mock_time_nsec(new_time);
  292. monotime_set_mock_time_nsec(new_time);
  293. timers_run_pending();
  294. tt_int_op(tried_to_write_cell, OP_EQ, CHANNELS_TO_TEST);
  295. // Test that we have no pending callbacks and all empty slots now
  296. for (i = 0; i < CHANNELS_TO_TEST; i++) {
  297. tt_assert(!chans[i]->pending_padding_callback);
  298. }
  299. }
  300. done:
  301. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  302. free_fake_channeltls((channel_tls_t*)chans[i]);
  303. }
  304. smartlist_free(connection_array);
  305. timers_shutdown();
  306. monotime_disable_test_mocking();
  307. channel_free_all();
  308. return;
  309. }
  310. void
  311. test_channelpadding_killonehop(void *arg)
  312. {
  313. channelpadding_decision_t decision;
  314. int64_t new_time;
  315. (void)arg;
  316. tor_libevent_postfork();
  317. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  318. monotime_init();
  319. monotime_enable_test_mocking();
  320. monotime_set_mock_time_nsec(1);
  321. monotime_coarse_set_mock_time_nsec(1);
  322. timers_initialize();
  323. setup_mock_consensus();
  324. setup_mock_network();
  325. /* Do we disable padding if tor2webmode or rsos are enabled, and
  326. * the consensus says don't pad? */
  327. /* Ensure we can kill tor2web and rsos padding if we want. */
  328. // First, test that padding works if either is enabled
  329. smartlist_clear(current_md_consensus->net_params);
  330. channelpadding_new_consensus_params(current_md_consensus);
  331. tried_to_write_cell = 0;
  332. get_options_mutable()->Tor2webMode = 1;
  333. client_relay3->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  334. decision = channelpadding_decide_to_pad_channel(client_relay3);
  335. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  336. tt_assert(client_relay3->pending_padding_callback);
  337. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  338. decision = channelpadding_decide_to_pad_channel(client_relay3);
  339. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  340. // Wait for the timer
  341. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  342. monotime_coarse_set_mock_time_nsec(new_time);
  343. monotime_set_mock_time_nsec(new_time);
  344. timers_run_pending();
  345. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  346. tt_assert(!client_relay3->pending_padding_callback);
  347. // Then test disabling each via consensus param
  348. smartlist_add(current_md_consensus->net_params,
  349. (void*)"nf_pad_tor2web=0");
  350. channelpadding_new_consensus_params(current_md_consensus);
  351. // Before the client tries to pad, the relay will still pad:
  352. tried_to_write_cell = 0;
  353. relay3_client->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  354. get_options_mutable()->ORPort_set = 1;
  355. get_options_mutable()->Tor2webMode = 0;
  356. decision = channelpadding_decide_to_pad_channel(relay3_client);
  357. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  358. tt_assert(relay3_client->pending_padding_callback);
  359. // Wait for the timer
  360. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  361. monotime_coarse_set_mock_time_nsec(new_time);
  362. monotime_set_mock_time_nsec(new_time);
  363. timers_run_pending();
  364. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  365. tt_assert(!client_relay3->pending_padding_callback);
  366. // Test client side (it should stop immediately, but send a negotiate)
  367. tried_to_write_cell = 0;
  368. tt_assert(relay3_client->padding_enabled);
  369. tt_assert(client_relay3->padding_enabled);
  370. get_options_mutable()->Tor2webMode = 1;
  371. /* For the relay to recieve the negotiate: */
  372. get_options_mutable()->ORPort_set = 1;
  373. decision = channelpadding_decide_to_pad_channel(client_relay3);
  374. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  375. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  376. tt_assert(!client_relay3->pending_padding_callback);
  377. tt_assert(!relay3_client->padding_enabled);
  378. // Test relay side (it should have gotten the negotiation to disable)
  379. get_options_mutable()->ORPort_set = 1;
  380. get_options_mutable()->Tor2webMode = 0;
  381. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  382. CHANNELPADDING_WONTPAD);
  383. tt_assert(!relay3_client->padding_enabled);
  384. /* Repeat for SOS */
  385. // First, test that padding works if either is enabled
  386. smartlist_clear(current_md_consensus->net_params);
  387. channelpadding_new_consensus_params(current_md_consensus);
  388. relay3_client->padding_enabled = 1;
  389. client_relay3->padding_enabled = 1;
  390. tried_to_write_cell = 0;
  391. get_options_mutable()->ORPort_set = 0;
  392. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  393. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  394. client_relay3->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  395. decision = channelpadding_decide_to_pad_channel(client_relay3);
  396. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  397. tt_assert(client_relay3->pending_padding_callback);
  398. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  399. decision = channelpadding_decide_to_pad_channel(client_relay3);
  400. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  401. // Wait for the timer
  402. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  403. monotime_coarse_set_mock_time_nsec(new_time);
  404. monotime_set_mock_time_nsec(new_time);
  405. timers_run_pending();
  406. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  407. tt_assert(!client_relay3->pending_padding_callback);
  408. // Then test disabling each via consensus param
  409. smartlist_add(current_md_consensus->net_params,
  410. (void*)"nf_pad_single_onion=0");
  411. channelpadding_new_consensus_params(current_md_consensus);
  412. // Before the client tries to pad, the relay will still pad:
  413. tried_to_write_cell = 0;
  414. relay3_client->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  415. get_options_mutable()->ORPort_set = 1;
  416. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  417. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  418. decision = channelpadding_decide_to_pad_channel(relay3_client);
  419. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  420. tt_assert(relay3_client->pending_padding_callback);
  421. // Wait for the timer
  422. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  423. monotime_coarse_set_mock_time_nsec(new_time);
  424. monotime_set_mock_time_nsec(new_time);
  425. timers_run_pending();
  426. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  427. tt_assert(!client_relay3->pending_padding_callback);
  428. // Test client side (it should stop immediately)
  429. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  430. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  431. /* For the relay to recieve the negotiate: */
  432. get_options_mutable()->ORPort_set = 1;
  433. decision = channelpadding_decide_to_pad_channel(client_relay3);
  434. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  435. tt_assert(!client_relay3->pending_padding_callback);
  436. // Test relay side (it should have gotten the negotiation to disable)
  437. get_options_mutable()->ORPort_set = 1;
  438. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  439. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  440. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  441. CHANNELPADDING_WONTPAD);
  442. tt_assert(!relay3_client->padding_enabled);
  443. done:
  444. free_mock_consensus();
  445. free_mock_network();
  446. tor_free(relay);
  447. timers_shutdown();
  448. monotime_disable_test_mocking();
  449. channel_free_all();
  450. }
  451. void
  452. test_channelpadding_consensus(void *arg)
  453. {
  454. channelpadding_decision_t decision;
  455. or_options_t *options = get_options_mutable();
  456. int64_t val;
  457. int64_t new_time;
  458. (void)arg;
  459. tor_libevent_postfork();
  460. /*
  461. * Params tested:
  462. * nf_pad_before_usage
  463. * nf_pad_relays
  464. * nf_ito_low
  465. * nf_ito_high
  466. *
  467. * Plan:
  468. * 1. Padding can be completely disabled via consensus
  469. * 2. Negotiation can't re-enable consensus-disabled padding
  470. * 3. Negotiation can't increase padding from relays beyond
  471. * consensus defaults
  472. * 4. Relay-to-relay padding can be enabled/disabled in consensus
  473. * 5. Can enable/disable padding before actually using a connection
  474. * 6. Can we control circ and TLS conn lifetime from the consensus?
  475. */
  476. channel_t *chan;
  477. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  478. monotime_enable_test_mocking();
  479. monotime_set_mock_time_nsec(1);
  480. monotime_coarse_set_mock_time_nsec(1);
  481. timers_initialize();
  482. connection_array = smartlist_new();
  483. chan = (channel_t*)new_fake_channeltls(0);
  484. channel_timestamp_active(chan);
  485. setup_mock_consensus();
  486. get_options_mutable()->ORPort_set = 1;
  487. /* Test 1: Padding can be completely disabled via consensus */
  488. tried_to_write_cell = 0;
  489. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  490. decision = channelpadding_decide_to_pad_channel(chan);
  491. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  492. tt_assert(chan->pending_padding_callback);
  493. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  494. decision = channelpadding_decide_to_pad_channel(chan);
  495. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  496. // Wait for the timer
  497. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  498. monotime_coarse_set_mock_time_nsec(new_time);
  499. monotime_set_mock_time_nsec(new_time);
  500. timers_run_pending();
  501. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  502. tt_assert(!chan->pending_padding_callback);
  503. smartlist_add(current_md_consensus->net_params,
  504. (void*)"nf_ito_low=0");
  505. smartlist_add(current_md_consensus->net_params,
  506. (void*)"nf_ito_high=0");
  507. get_options_mutable()->ConnectionPadding = 1;
  508. channelpadding_new_consensus_params(current_md_consensus);
  509. decision = channelpadding_decide_to_pad_channel(chan);
  510. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  511. tt_assert(!chan->pending_padding_callback);
  512. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  513. tt_i64_op(val, OP_EQ, 0);
  514. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  515. tt_i64_op(val, OP_EQ, -2);
  516. /* Test 2: Negotiation can't re-enable consensus-disabled padding */
  517. channelpadding_send_enable_command(chan, 100, 200);
  518. tried_to_write_cell = 0;
  519. decision = channelpadding_decide_to_pad_channel(chan);
  520. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  521. tt_assert(!chan->pending_padding_callback);
  522. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  523. tt_i64_op(val, OP_EQ, 0);
  524. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  525. tt_i64_op(val, OP_EQ, -2);
  526. tt_assert(!chan->next_padding_time_ms);
  527. smartlist_clear(current_md_consensus->net_params);
  528. /* Test 3: Negotiation can't increase padding from relays beyond consensus
  529. * values */
  530. smartlist_add(current_md_consensus->net_params,
  531. (void*)"nf_ito_low=100");
  532. smartlist_add(current_md_consensus->net_params,
  533. (void*)"nf_ito_high=200");
  534. channelpadding_new_consensus_params(current_md_consensus);
  535. tried_to_write_cell = 0;
  536. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  537. decision = channelpadding_decide_to_pad_channel(chan);
  538. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  539. tt_assert(chan->pending_padding_callback);
  540. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  541. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  542. tt_i64_op(val, OP_GE, 100);
  543. tt_i64_op(val, OP_LE, 200);
  544. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  545. tt_i64_op(val, OP_LE, 200);
  546. // Wait for the timer
  547. new_time = (monotime_coarse_absolute_msec()+201)*NSEC_PER_MSEC;
  548. monotime_set_mock_time_nsec(new_time);
  549. monotime_coarse_set_mock_time_nsec(new_time);
  550. timers_run_pending();
  551. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  552. tt_assert(!chan->pending_padding_callback);
  553. smartlist_clear(current_md_consensus->net_params);
  554. smartlist_add(current_md_consensus->net_params,
  555. (void*)"nf_ito_low=1500");
  556. smartlist_add(current_md_consensus->net_params,
  557. (void*)"nf_ito_high=4500");
  558. channelpadding_new_consensus_params(current_md_consensus);
  559. channelpadding_send_enable_command(chan, 100, 200);
  560. tried_to_write_cell = 0;
  561. decision = channelpadding_decide_to_pad_channel(chan);
  562. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  563. tt_assert(!chan->pending_padding_callback);
  564. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  565. tt_i64_op(val, OP_GE, 1500);
  566. tt_i64_op(val, OP_LE, 4500);
  567. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  568. tt_i64_op(val, OP_LE, 4500);
  569. /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
  570. /* Make this channel a relay's channel */
  571. memcpy(relay->identity_digest,
  572. ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
  573. smartlist_add(current_md_consensus->routerstatus_list, relay);
  574. tried_to_write_cell = 0;
  575. decision = channelpadding_decide_to_pad_channel(chan);
  576. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  577. tt_assert(!chan->pending_padding_callback);
  578. smartlist_add(current_md_consensus->net_params,
  579. (void*)"nf_pad_relays=1");
  580. channelpadding_new_consensus_params(current_md_consensus);
  581. decision = channelpadding_decide_to_pad_channel(chan);
  582. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  583. tt_assert(!chan->pending_padding_callback);
  584. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  585. tt_i64_op(val, OP_GE, 1500);
  586. tt_i64_op(val, OP_LE, 4500);
  587. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  588. tt_i64_op(val, OP_LE, 4500);
  589. /* Test 5: If we disable padding before channel usage, does that work? */
  590. smartlist_add(current_md_consensus->net_params,
  591. (void*)"nf_pad_before_usage=0");
  592. channelpadding_new_consensus_params(current_md_consensus);
  593. tried_to_write_cell = 0;
  594. decision = channelpadding_decide_to_pad_channel(chan);
  595. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  596. tt_assert(!chan->pending_padding_callback);
  597. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  598. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  599. tt_i64_op(val, OP_GE, 180);
  600. tt_i64_op(val, OP_LE, 180+90);
  601. val = channelpadding_get_channel_idle_timeout(chan, 0);
  602. tt_i64_op(val, OP_GE, 180);
  603. tt_i64_op(val, OP_LE, 180+90);
  604. options->ReducedConnectionPadding = 1;
  605. val = channelpadding_get_channel_idle_timeout(chan, 0);
  606. tt_i64_op(val, OP_GE, 180/2);
  607. tt_i64_op(val, OP_LE, (180+90)/2);
  608. options->ReducedConnectionPadding = 0;
  609. options->ORPort_set = 1;
  610. smartlist_add(current_md_consensus->net_params,
  611. (void*)"nf_conntimeout_relays=600");
  612. channelpadding_new_consensus_params(current_md_consensus);
  613. val = channelpadding_get_channel_idle_timeout(chan, 1);
  614. tt_i64_op(val, OP_GE, 450);
  615. tt_i64_op(val, OP_LE, 750);
  616. val = channelpadding_get_circuits_available_timeout();
  617. tt_i64_op(val, OP_GE, 30*60);
  618. tt_i64_op(val, OP_LE, 30*60*2);
  619. options->ReducedConnectionPadding = 1;
  620. smartlist_add(current_md_consensus->net_params,
  621. (void*)"nf_conntimeout_clients=600");
  622. channelpadding_new_consensus_params(current_md_consensus);
  623. val = channelpadding_get_circuits_available_timeout();
  624. tt_i64_op(val, OP_GE, 600/2);
  625. tt_i64_op(val, OP_LE, 600*2/2);
  626. options->ReducedConnectionPadding = 0;
  627. options->CircuitsAvailableTimeout = 24*60*60;
  628. val = channelpadding_get_circuits_available_timeout();
  629. tt_i64_op(val, OP_GE, 24*60*60);
  630. tt_i64_op(val, OP_LE, 24*60*60*2);
  631. done:
  632. tor_free(relay);
  633. free_mock_consensus();
  634. free_fake_channeltls((channel_tls_t*)chan);
  635. smartlist_free(connection_array);
  636. timers_shutdown();
  637. monotime_disable_test_mocking();
  638. channel_free_all();
  639. return;
  640. }
  641. void
  642. test_channelpadding_negotiation(void *arg)
  643. {
  644. channelpadding_negotiate_t disable;
  645. cell_t cell;
  646. channelpadding_decision_t decision;
  647. int val;
  648. (void)arg;
  649. /* Plan:
  650. * 1. Clients reject negotiation, relays accept it.
  651. * * Bridges accept negotiation from their clients,
  652. * but not from relays.
  653. * 2. Torrc options can override client-side negotiation
  654. * 3. Test a version issue in channelpadidng cell
  655. * 4. Test channelpadding_reduced_padding
  656. */
  657. monotime_init();
  658. monotime_enable_test_mocking();
  659. monotime_set_mock_time_nsec(1);
  660. monotime_coarse_set_mock_time_nsec(1);
  661. timers_initialize();
  662. setup_mock_consensus();
  663. setup_mock_network();
  664. /* Test case #1: Do the right things ignore negotiation? */
  665. /* relay-to-client case: */
  666. channelpadding_send_disable_command(relay3_client);
  667. tt_assert(client_relay3->padding_enabled);
  668. /* client-to-relay case: */
  669. get_options_mutable()->ORPort_set = 1;
  670. channelpadding_disable_padding_on_channel(client_relay3);
  671. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  672. CHANNELPADDING_WONTPAD);
  673. tt_assert(!relay3_client->padding_enabled);
  674. relay3_client->padding_enabled = 1;
  675. client_relay3->padding_enabled = 1;
  676. /* Bridge case from relay */
  677. get_options_mutable()->BridgeRelay = 1;
  678. channelpadding_disable_padding_on_channel(relay2_relay1);
  679. tt_assert(relay1_relay2->padding_enabled);
  680. /* Bridge case from client */
  681. channelpadding_disable_padding_on_channel(client_relay3);
  682. tt_assert(!relay3_client->padding_enabled);
  683. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  684. CHANNELPADDING_WONTPAD);
  685. relay3_client->padding_enabled = 1;
  686. client_relay3->padding_enabled = 1;
  687. get_options_mutable()->BridgeRelay = 0;
  688. get_options_mutable()->ORPort_set = 0;
  689. /* Test case #2: Torrc options */
  690. /* ConnectionPadding auto; Relay doesn't suport us */
  691. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  692. relay3_client->padding_enabled = 0;
  693. tried_to_write_cell = 0;
  694. decision = channelpadding_decide_to_pad_channel(relay3_client);
  695. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  696. tt_assert(!relay3_client->pending_padding_callback);
  697. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  698. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  699. relay3_client->padding_enabled = 1;
  700. /* ConnectionPadding 1; Relay doesn't suport us */
  701. get_options_mutable()->ConnectionPadding = 1;
  702. tried_to_write_cell = 0;
  703. decision = channelpadding_decide_to_pad_channel(client_relay3);
  704. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  705. tt_assert(!client_relay3->pending_padding_callback);
  706. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  707. get_options_mutable()->ConnectionPadding = 0;
  708. /* Test case #3: Test a version issue in channelpadding cell */
  709. get_options_mutable()->ORPort_set = 1;
  710. client_relay3->padding_enabled = 1;
  711. relay3_client->padding_enabled = 1;
  712. memset(&cell, 0, sizeof(cell_t));
  713. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  714. cell.command = CELL_PADDING_NEGOTIATE;
  715. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  716. disable.version = 1;
  717. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  718. client_relay3->write_cell(client_relay3, &cell);
  719. tt_assert(relay3_client->padding_enabled);
  720. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  721. OP_EQ, -1);
  722. tt_assert(client_relay3->padding_enabled);
  723. disable.version = 0;
  724. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  725. client_relay3->write_cell(client_relay3, &cell);
  726. tt_assert(!relay3_client->padding_enabled);
  727. /* Test case 4: Reducing padding actually reduces it */
  728. relay3_client->padding_enabled = 1;
  729. client_relay3->padding_enabled = 1;
  730. decision = channelpadding_decide_to_pad_channel(relay3_client);
  731. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  732. channelpadding_reduce_padding_on_channel(client_relay3);
  733. tried_to_write_cell = 0;
  734. decision = channelpadding_decide_to_pad_channel(relay3_client);
  735. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  736. get_options_mutable()->ORPort_set = 0;
  737. decision = channelpadding_decide_to_pad_channel(client_relay3);
  738. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  739. tt_assert(!client_relay3->pending_padding_callback);
  740. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  741. tt_int_op(val, OP_GE, 9000);
  742. tt_int_op(val, OP_LE, 14000);
  743. int64_t val64 =
  744. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  745. tt_i64_op(val64, OP_LE, 14000);
  746. done:
  747. free_mock_network();
  748. free_mock_consensus();
  749. timers_shutdown();
  750. monotime_disable_test_mocking();
  751. channel_free_all();
  752. return;
  753. }
  754. void
  755. test_channelpadding_decide_to_pad_channel(void *arg)
  756. {
  757. channelpadding_decision_t decision;
  758. /**
  759. * Test case plan:
  760. *
  761. * 1. Channel that has "sent a packet" before the timeout.
  762. * + We should decide to pad later
  763. * 2. Channel that has not "sent a packet" before the timeout:
  764. * 2a. Not within 1.1s of the timeout.
  765. * + We should decide to pad later
  766. * 2b. Within 1.1s of the timemout.
  767. * + We should schedule padding
  768. * + We should get feedback that we wrote a cell
  769. * 2c. Within 0.1s of the timeout.
  770. * + We should schedule padding
  771. * + We should get feedback that we wrote a cell
  772. * 2d. Channel that asks to pad while timeout is scheduled
  773. * + We should schedule padding
  774. * + We should get feedback that we wrote a cell
  775. * 2e. 0s of the timeout
  776. * + We should send padding immediately
  777. * + We should get feedback that we wrote a cell
  778. * 2f. <0s of the timeout
  779. * + We should send padding immediately
  780. * + We should get feedback that we wrote a cell
  781. * 3. Channel that sends a packet while timeout is scheduled
  782. * + We should not get feedback that we wrote a cell
  783. * 4. Channel that closes while timeout is scheduled
  784. * + We should not get feedback that we wrote a cell
  785. * 5. Make sure the channel still would work if repaired
  786. * + We should be able to schedule padding and resend
  787. * 6. Channel is not used for full circuits
  788. * 7. Channel that disappears while timeout is scheduled
  789. * + We should not send padding
  790. */
  791. channel_t *chan;
  792. int64_t new_time;
  793. connection_array = smartlist_new();
  794. (void)arg;
  795. tor_libevent_postfork();
  796. monotime_init();
  797. monotime_enable_test_mocking();
  798. monotime_set_mock_time_nsec(1);
  799. monotime_coarse_set_mock_time_nsec(1);
  800. timers_initialize();
  801. setup_full_capture_of_logs(LOG_WARN);
  802. channelpadding_new_consensus_params(NULL);
  803. chan = (channel_t*)new_fake_channeltls(0);
  804. channel_timestamp_active(chan);
  805. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  806. tried_to_write_cell = 0;
  807. decision = channelpadding_decide_to_pad_channel(chan);
  808. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  809. tt_assert(!chan->pending_padding_callback);
  810. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  811. /* Test case #2a: > 1.1s until timeout */
  812. tried_to_write_cell = 0;
  813. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1200;
  814. decision = channelpadding_decide_to_pad_channel(chan);
  815. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  816. tt_assert(!chan->pending_padding_callback);
  817. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  818. /* Test case #2b: >= 1.0s until timeout */
  819. tried_to_write_cell = 0;
  820. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1000;
  821. decision = channelpadding_decide_to_pad_channel(chan);
  822. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  823. tt_assert(chan->pending_padding_callback);
  824. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  825. // Wait for the timer from case #2b
  826. new_time = (monotime_coarse_absolute_msec() + 1000)*NSEC_PER_MSEC;
  827. monotime_set_mock_time_nsec(new_time);
  828. monotime_coarse_set_mock_time_nsec(new_time);
  829. timers_run_pending();
  830. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  831. tt_assert(!chan->pending_padding_callback);
  832. /* Test case #2c: > 0.1s until timeout */
  833. tried_to_write_cell = 0;
  834. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  835. decision = channelpadding_decide_to_pad_channel(chan);
  836. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  837. tt_assert(chan->pending_padding_callback);
  838. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  839. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  840. decision = channelpadding_decide_to_pad_channel(chan);
  841. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  842. // Wait for the timer
  843. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  844. monotime_coarse_set_mock_time_nsec(new_time);
  845. monotime_set_mock_time_nsec(new_time);
  846. timers_run_pending();
  847. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  848. tt_assert(!chan->pending_padding_callback);
  849. /* Test case #2e: 0s until timeout */
  850. tried_to_write_cell = 0;
  851. chan->next_padding_time_ms = monotime_coarse_absolute_msec();
  852. decision = channelpadding_decide_to_pad_channel(chan);
  853. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  854. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  855. tt_assert(!chan->pending_padding_callback);
  856. /* Test case #2f: <0s until timeout */
  857. tried_to_write_cell = 0;
  858. chan->next_padding_time_ms = monotime_coarse_absolute_msec() - 100;
  859. decision = channelpadding_decide_to_pad_channel(chan);
  860. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  861. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  862. tt_assert(!chan->pending_padding_callback);
  863. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  864. tried_to_write_cell = 0;
  865. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  866. decision = channelpadding_decide_to_pad_channel(chan);
  867. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  868. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  869. tt_assert(chan->pending_padding_callback);
  870. // Pretend the channel sent a packet
  871. channel_timestamp_active(chan);
  872. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  873. // Wait for the timer
  874. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  875. monotime_coarse_set_mock_time_nsec(new_time);
  876. monotime_set_mock_time_nsec(new_time);
  877. timers_run_pending();
  878. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  879. tt_assert(!chan->pending_padding_callback);
  880. /* Test case #4: Channel that closes while a timeout is scheduled */
  881. tried_to_write_cell = 0;
  882. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  883. decision = channelpadding_decide_to_pad_channel(chan);
  884. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  885. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  886. tt_assert(chan->pending_padding_callback);
  887. // Pretend the channel is temporarily down
  888. chan->state = CHANNEL_STATE_MAINT;
  889. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  890. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  891. monotime_coarse_set_mock_time_nsec(new_time);
  892. monotime_set_mock_time_nsec(new_time);
  893. timers_run_pending();
  894. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  895. tt_assert(!chan->pending_padding_callback);
  896. chan->state = CHANNEL_STATE_OPEN;
  897. /* Test case #5: Make sure previous test case didn't break everything */
  898. tried_to_write_cell = 0;
  899. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  900. decision = channelpadding_decide_to_pad_channel(chan);
  901. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  902. tt_assert(chan->pending_padding_callback);
  903. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  904. // Wait for the timer
  905. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  906. monotime_coarse_set_mock_time_nsec(new_time);
  907. monotime_set_mock_time_nsec(new_time);
  908. timers_run_pending();
  909. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  910. tt_assert(!chan->pending_padding_callback);
  911. /* Test case #6. Channel is not used for full circuits */
  912. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  913. decision = channelpadding_decide_to_pad_channel(chan);
  914. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  915. tt_assert(!chan->pending_padding_callback);
  916. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  917. /* Test case #7. Channel is closed while timeout is scheduled.
  918. *
  919. * NOTE: This test deliberately breaks the channel callback mechanism.
  920. * It must be last.
  921. */
  922. tried_to_write_cell = 0;
  923. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  924. decision = channelpadding_decide_to_pad_channel(chan);
  925. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  926. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  927. tt_assert(chan->pending_padding_callback);
  928. // Close the connection while the timer is scheduled
  929. free_fake_channeltls((channel_tls_t*)chan);
  930. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  931. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  932. monotime_coarse_set_mock_time_nsec(new_time);
  933. monotime_set_mock_time_nsec(new_time);
  934. timers_run_pending();
  935. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  936. done:
  937. smartlist_free(connection_array);
  938. teardown_capture_of_logs();
  939. monotime_disable_test_mocking();
  940. timers_shutdown();
  941. channel_free_all();
  942. return;
  943. }
  944. #define TEST_CHANNELPADDING(name, flags) \
  945. { #name, test_##name, (flags), NULL, NULL }
  946. struct testcase_t channelpadding_tests[] = {
  947. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  948. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  949. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  950. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  951. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  952. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  953. END_OF_TESTCASES
  954. };