test_channelpadding.c 40 KB

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