test_channelpadding.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. free_mock_consensus();
  633. free_fake_channeltls((channel_tls_t*)chan);
  634. smartlist_free(connection_array);
  635. timers_shutdown();
  636. monotime_disable_test_mocking();
  637. channel_free_all();
  638. return;
  639. }
  640. void
  641. test_channelpadding_negotiation(void *arg)
  642. {
  643. channelpadding_negotiate_t disable;
  644. cell_t cell;
  645. channelpadding_decision_t decision;
  646. int val;
  647. (void)arg;
  648. /* Plan:
  649. * 1. Clients reject negotiation, relays accept it.
  650. * * Bridges accept negotiation from their clients,
  651. * but not from relays.
  652. * 2. Torrc options can override client-side negotiation
  653. * 3. Test a version issue in channelpadidng cell
  654. * 4. Test channelpadding_reduced_padding
  655. */
  656. monotime_init();
  657. monotime_enable_test_mocking();
  658. monotime_set_mock_time_nsec(1);
  659. monotime_coarse_set_mock_time_nsec(1);
  660. timers_initialize();
  661. setup_mock_consensus();
  662. setup_mock_network();
  663. /* Test case #1: Do the right things ignore negotiation? */
  664. /* relay-to-client case: */
  665. channelpadding_send_disable_command(relay3_client);
  666. tt_assert(client_relay3->padding_enabled);
  667. /* client-to-relay case: */
  668. get_options_mutable()->ORPort_set = 1;
  669. channelpadding_disable_padding_on_channel(client_relay3);
  670. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  671. CHANNELPADDING_WONTPAD);
  672. tt_assert(!relay3_client->padding_enabled);
  673. relay3_client->padding_enabled = 1;
  674. client_relay3->padding_enabled = 1;
  675. /* Bridge case from relay */
  676. get_options_mutable()->BridgeRelay = 1;
  677. channelpadding_disable_padding_on_channel(relay2_relay1);
  678. tt_assert(relay1_relay2->padding_enabled);
  679. /* Bridge case from client */
  680. channelpadding_disable_padding_on_channel(client_relay3);
  681. tt_assert(!relay3_client->padding_enabled);
  682. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  683. CHANNELPADDING_WONTPAD);
  684. relay3_client->padding_enabled = 1;
  685. client_relay3->padding_enabled = 1;
  686. get_options_mutable()->BridgeRelay = 0;
  687. get_options_mutable()->ORPort_set = 0;
  688. /* Test case #2: Torrc options */
  689. /* ConnectionPadding auto; Relay doesn't suport us */
  690. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  691. relay3_client->padding_enabled = 0;
  692. tried_to_write_cell = 0;
  693. decision = channelpadding_decide_to_pad_channel(relay3_client);
  694. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  695. tt_assert(!relay3_client->pending_padding_callback);
  696. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  697. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  698. relay3_client->padding_enabled = 1;
  699. /* ConnectionPadding 1; Relay doesn't suport us */
  700. get_options_mutable()->ConnectionPadding = 1;
  701. tried_to_write_cell = 0;
  702. decision = channelpadding_decide_to_pad_channel(client_relay3);
  703. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  704. tt_assert(!client_relay3->pending_padding_callback);
  705. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  706. get_options_mutable()->ConnectionPadding = 0;
  707. /* Test case #3: Test a version issue in channelpadding cell */
  708. get_options_mutable()->ORPort_set = 1;
  709. client_relay3->padding_enabled = 1;
  710. relay3_client->padding_enabled = 1;
  711. memset(&cell, 0, sizeof(cell_t));
  712. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  713. cell.command = CELL_PADDING_NEGOTIATE;
  714. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  715. disable.version = 1;
  716. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  717. client_relay3->write_cell(client_relay3, &cell);
  718. tt_assert(relay3_client->padding_enabled);
  719. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  720. OP_EQ, -1);
  721. tt_assert(client_relay3->padding_enabled);
  722. disable.version = 0;
  723. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  724. client_relay3->write_cell(client_relay3, &cell);
  725. tt_assert(!relay3_client->padding_enabled);
  726. /* Test case 4: Reducing padding actually reduces it */
  727. relay3_client->padding_enabled = 1;
  728. client_relay3->padding_enabled = 1;
  729. decision = channelpadding_decide_to_pad_channel(relay3_client);
  730. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  731. channelpadding_reduce_padding_on_channel(client_relay3);
  732. tried_to_write_cell = 0;
  733. decision = channelpadding_decide_to_pad_channel(relay3_client);
  734. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  735. get_options_mutable()->ORPort_set = 0;
  736. decision = channelpadding_decide_to_pad_channel(client_relay3);
  737. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  738. tt_assert(!client_relay3->pending_padding_callback);
  739. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  740. tt_int_op(val, OP_GE, 9000);
  741. tt_int_op(val, OP_LE, 14000);
  742. int64_t val64 =
  743. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  744. tt_i64_op(val64, OP_LE, 14000);
  745. done:
  746. free_mock_network();
  747. free_mock_consensus();
  748. timers_shutdown();
  749. monotime_disable_test_mocking();
  750. channel_free_all();
  751. return;
  752. }
  753. void
  754. test_channelpadding_decide_to_pad_channel(void *arg)
  755. {
  756. channelpadding_decision_t decision;
  757. /**
  758. * Test case plan:
  759. *
  760. * 1. Channel that has "sent a packet" before the timeout.
  761. * + We should decide to pad later
  762. * 2. Channel that has not "sent a packet" before the timeout:
  763. * 2a. Not within 1.1s of the timeout.
  764. * + We should decide to pad later
  765. * 2b. Within 1.1s of the timemout.
  766. * + We should schedule padding
  767. * + We should get feedback that we wrote a cell
  768. * 2c. Within 0.1s of the timeout.
  769. * + We should schedule padding
  770. * + We should get feedback that we wrote a cell
  771. * 2d. Channel that asks to pad while timeout is scheduled
  772. * + We should schedule padding
  773. * + We should get feedback that we wrote a cell
  774. * 2e. 0s of the timeout
  775. * + We should send padding immediately
  776. * + We should get feedback that we wrote a cell
  777. * 2f. <0s of the timeout
  778. * + We should send padding immediately
  779. * + We should get feedback that we wrote a cell
  780. * 3. Channel that sends a packet while timeout is scheduled
  781. * + We should not get feedback that we wrote a cell
  782. * 4. Channel that closes while timeout is scheduled
  783. * + We should not get feedback that we wrote a cell
  784. * 5. Make sure the channel still would work if repaired
  785. * + We should be able to schedule padding and resend
  786. * 6. Channel is not used for full circuits
  787. * 7. Channel that disappears while timeout is scheduled
  788. * + We should not send padding
  789. */
  790. channel_t *chan;
  791. int64_t new_time;
  792. connection_array = smartlist_new();
  793. (void)arg;
  794. tor_libevent_postfork();
  795. monotime_init();
  796. monotime_enable_test_mocking();
  797. monotime_set_mock_time_nsec(1);
  798. monotime_coarse_set_mock_time_nsec(1);
  799. timers_initialize();
  800. setup_full_capture_of_logs(LOG_WARN);
  801. channelpadding_new_consensus_params(NULL);
  802. chan = (channel_t*)new_fake_channeltls(0);
  803. channel_timestamp_active(chan);
  804. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  805. tried_to_write_cell = 0;
  806. decision = channelpadding_decide_to_pad_channel(chan);
  807. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  808. tt_assert(!chan->pending_padding_callback);
  809. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  810. /* Test case #2a: > 1.1s until timeout */
  811. tried_to_write_cell = 0;
  812. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1200;
  813. decision = channelpadding_decide_to_pad_channel(chan);
  814. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  815. tt_assert(!chan->pending_padding_callback);
  816. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  817. /* Test case #2b: >= 1.0s until timeout */
  818. tried_to_write_cell = 0;
  819. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1000;
  820. decision = channelpadding_decide_to_pad_channel(chan);
  821. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  822. tt_assert(chan->pending_padding_callback);
  823. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  824. // Wait for the timer from case #2b
  825. new_time = (monotime_coarse_absolute_msec() + 1000)*NSEC_PER_MSEC;
  826. monotime_set_mock_time_nsec(new_time);
  827. monotime_coarse_set_mock_time_nsec(new_time);
  828. timers_run_pending();
  829. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  830. tt_assert(!chan->pending_padding_callback);
  831. /* Test case #2c: > 0.1s until timeout */
  832. tried_to_write_cell = 0;
  833. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  834. decision = channelpadding_decide_to_pad_channel(chan);
  835. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  836. tt_assert(chan->pending_padding_callback);
  837. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  838. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  839. decision = channelpadding_decide_to_pad_channel(chan);
  840. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  841. // Wait for the timer
  842. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  843. monotime_coarse_set_mock_time_nsec(new_time);
  844. monotime_set_mock_time_nsec(new_time);
  845. timers_run_pending();
  846. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  847. tt_assert(!chan->pending_padding_callback);
  848. /* Test case #2e: 0s until timeout */
  849. tried_to_write_cell = 0;
  850. chan->next_padding_time_ms = monotime_coarse_absolute_msec();
  851. decision = channelpadding_decide_to_pad_channel(chan);
  852. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  853. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  854. tt_assert(!chan->pending_padding_callback);
  855. /* Test case #2f: <0s until timeout */
  856. tried_to_write_cell = 0;
  857. chan->next_padding_time_ms = monotime_coarse_absolute_msec() - 100;
  858. decision = channelpadding_decide_to_pad_channel(chan);
  859. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  860. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  861. tt_assert(!chan->pending_padding_callback);
  862. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  863. tried_to_write_cell = 0;
  864. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  865. decision = channelpadding_decide_to_pad_channel(chan);
  866. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  867. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  868. tt_assert(chan->pending_padding_callback);
  869. // Pretend the channel sent a packet
  870. channel_timestamp_active(chan);
  871. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  872. // Wait for the timer
  873. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  874. monotime_coarse_set_mock_time_nsec(new_time);
  875. monotime_set_mock_time_nsec(new_time);
  876. timers_run_pending();
  877. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  878. tt_assert(!chan->pending_padding_callback);
  879. /* Test case #4: Channel that closes while a timeout is scheduled */
  880. tried_to_write_cell = 0;
  881. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  882. decision = channelpadding_decide_to_pad_channel(chan);
  883. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  884. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  885. tt_assert(chan->pending_padding_callback);
  886. // Pretend the channel is temporarily down
  887. chan->state = CHANNEL_STATE_MAINT;
  888. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  889. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  890. monotime_coarse_set_mock_time_nsec(new_time);
  891. monotime_set_mock_time_nsec(new_time);
  892. timers_run_pending();
  893. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  894. tt_assert(!chan->pending_padding_callback);
  895. chan->state = CHANNEL_STATE_OPEN;
  896. /* Test case #5: Make sure previous test case didn't break everything */
  897. tried_to_write_cell = 0;
  898. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  899. decision = channelpadding_decide_to_pad_channel(chan);
  900. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  901. tt_assert(chan->pending_padding_callback);
  902. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  903. // Wait for the timer
  904. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  905. monotime_coarse_set_mock_time_nsec(new_time);
  906. monotime_set_mock_time_nsec(new_time);
  907. timers_run_pending();
  908. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  909. tt_assert(!chan->pending_padding_callback);
  910. /* Test case #6. Channel is not used for full circuits */
  911. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  912. decision = channelpadding_decide_to_pad_channel(chan);
  913. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  914. tt_assert(!chan->pending_padding_callback);
  915. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  916. /* Test case #7. Channel is closed while timeout is scheduled.
  917. *
  918. * NOTE: This test deliberately breaks the channel callback mechanism.
  919. * It must be last.
  920. */
  921. tried_to_write_cell = 0;
  922. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  923. decision = channelpadding_decide_to_pad_channel(chan);
  924. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  925. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  926. tt_assert(chan->pending_padding_callback);
  927. // Close the connection while the timer is scheduled
  928. free_fake_channeltls((channel_tls_t*)chan);
  929. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  930. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  931. monotime_coarse_set_mock_time_nsec(new_time);
  932. monotime_set_mock_time_nsec(new_time);
  933. timers_run_pending();
  934. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  935. done:
  936. smartlist_free(connection_array);
  937. teardown_capture_of_logs();
  938. monotime_disable_test_mocking();
  939. timers_shutdown();
  940. channel_free_all();
  941. return;
  942. }
  943. #define TEST_CHANNELPADDING(name, flags) \
  944. { #name, test_##name, (flags), NULL, NULL }
  945. struct testcase_t channelpadding_tests[] = {
  946. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  947. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  948. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  949. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  950. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  951. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  952. END_OF_TESTCASES
  953. };