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