test_channelpadding.c 39 KB

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