test_channelpadding.c 40 KB

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