test_channelpadding.c 37 KB

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