test_channelpadding.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. int64_t new_time;
  236. (void)arg;
  237. tor_libevent_postfork();
  238. if (!connection_array)
  239. connection_array = smartlist_new();
  240. monotime_init();
  241. monotime_enable_test_mocking();
  242. monotime_set_mock_time_nsec(1);
  243. monotime_coarse_set_mock_time_nsec(1);
  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. /* This loop fills our timerslot array with timers of increasing time
  254. * until they fire */
  255. for (; i < CHANNELPADDING_MAX_TIMERS; i++) {
  256. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec()
  257. + 10 + i*4;
  258. decision = channelpadding_decide_to_pad_channel(chans[i]);
  259. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  260. tt_assert(chans[i]->pending_padding_callback);
  261. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  262. }
  263. /* This loop should add timers to the first position in the timerslot
  264. * array, since its timeout is before all other timers. */
  265. for (; i < CHANNELS_TO_TEST/3; i++) {
  266. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 1;
  267. decision = channelpadding_decide_to_pad_channel(chans[i]);
  268. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  269. tt_assert(chans[i]->pending_padding_callback);
  270. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  271. }
  272. /* This loop should add timers to our existing lists in a weak
  273. * pseudorandom pattern. It ensures that the lists can grow with multiple
  274. * timers in them. */
  275. for (; i < CHANNELS_TO_TEST/2; i++) {
  276. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 10 +
  277. i*3 % CHANNELPADDING_MAX_TIMERS;
  278. decision = channelpadding_decide_to_pad_channel(chans[i]);
  279. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  280. tt_assert(chans[i]->pending_padding_callback);
  281. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  282. }
  283. /* This loop should add timers to the last position in the timerslot
  284. * array, since its timeout is after all other timers. */
  285. for (; i < CHANNELS_TO_TEST; i++) {
  286. chans[i]->next_padding_time_ms = monotime_coarse_absolute_msec() + 500 +
  287. i % CHANNELPADDING_MAX_TIMERS;
  288. decision = channelpadding_decide_to_pad_channel(chans[i]);
  289. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  290. tt_assert(chans[i]->pending_padding_callback);
  291. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  292. }
  293. // Wait for the timers and then kill the event loop.
  294. new_time = (monotime_coarse_absolute_msec()+1001)*NSEC_PER_MSEC;
  295. monotime_coarse_set_mock_time_nsec(new_time);
  296. monotime_set_mock_time_nsec(new_time);
  297. timers_run_pending();
  298. tt_int_op(tried_to_write_cell, OP_EQ, CHANNELS_TO_TEST);
  299. // Test that we have no pending callbacks and all empty slots now
  300. for (i = 0; i < CHANNELS_TO_TEST; i++) {
  301. tt_assert(!chans[i]->pending_padding_callback);
  302. }
  303. }
  304. done:
  305. for (int i = 0; i < CHANNELS_TO_TEST; i++) {
  306. free_fake_channeltls((channel_tls_t*)chans[i]);
  307. }
  308. smartlist_free(connection_array);
  309. timers_shutdown();
  310. monotime_disable_test_mocking();
  311. channel_free_all();
  312. return;
  313. }
  314. void
  315. test_channelpadding_killonehop(void *arg)
  316. {
  317. channelpadding_decision_t decision;
  318. int64_t new_time;
  319. (void)arg;
  320. tor_libevent_postfork();
  321. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  322. monotime_init();
  323. monotime_enable_test_mocking();
  324. monotime_set_mock_time_nsec(1);
  325. monotime_coarse_set_mock_time_nsec(1);
  326. timers_initialize();
  327. setup_mock_consensus();
  328. setup_mock_network();
  329. /* Do we disable padding if tor2webmode or rsos are enabled, and
  330. * the consensus says don't pad? */
  331. /* Ensure we can kill tor2web and rsos padding if we want. */
  332. // First, test that padding works if either is enabled
  333. smartlist_clear(current_md_consensus->net_params);
  334. channelpadding_new_consensus_params(current_md_consensus);
  335. tried_to_write_cell = 0;
  336. get_options_mutable()->Tor2webMode = 1;
  337. client_relay3->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  338. decision = channelpadding_decide_to_pad_channel(client_relay3);
  339. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  340. tt_assert(client_relay3->pending_padding_callback);
  341. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  342. decision = channelpadding_decide_to_pad_channel(client_relay3);
  343. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  344. // Wait for the timer
  345. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  346. monotime_coarse_set_mock_time_nsec(new_time);
  347. monotime_set_mock_time_nsec(new_time);
  348. timers_run_pending();
  349. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  350. tt_assert(!client_relay3->pending_padding_callback);
  351. // Then test disabling each via consensus param
  352. smartlist_add(current_md_consensus->net_params,
  353. (void*)"nf_pad_tor2web=0");
  354. channelpadding_new_consensus_params(current_md_consensus);
  355. // Before the client tries to pad, the relay will still pad:
  356. tried_to_write_cell = 0;
  357. relay3_client->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  358. get_options_mutable()->ORPort_set = 1;
  359. get_options_mutable()->Tor2webMode = 0;
  360. decision = channelpadding_decide_to_pad_channel(relay3_client);
  361. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  362. tt_assert(relay3_client->pending_padding_callback);
  363. // Wait for the timer
  364. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  365. monotime_coarse_set_mock_time_nsec(new_time);
  366. monotime_set_mock_time_nsec(new_time);
  367. timers_run_pending();
  368. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  369. tt_assert(!client_relay3->pending_padding_callback);
  370. // Test client side (it should stop immediately, but send a negotiate)
  371. tried_to_write_cell = 0;
  372. tt_assert(relay3_client->padding_enabled);
  373. tt_assert(client_relay3->padding_enabled);
  374. get_options_mutable()->Tor2webMode = 1;
  375. /* For the relay to recieve the negotiate: */
  376. get_options_mutable()->ORPort_set = 1;
  377. decision = channelpadding_decide_to_pad_channel(client_relay3);
  378. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  379. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  380. tt_assert(!client_relay3->pending_padding_callback);
  381. tt_assert(!relay3_client->padding_enabled);
  382. // Test relay side (it should have gotten the negotiation to disable)
  383. get_options_mutable()->ORPort_set = 1;
  384. get_options_mutable()->Tor2webMode = 0;
  385. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  386. CHANNELPADDING_WONTPAD);
  387. tt_assert(!relay3_client->padding_enabled);
  388. /* Repeat for SOS */
  389. // First, test that padding works if either is enabled
  390. smartlist_clear(current_md_consensus->net_params);
  391. channelpadding_new_consensus_params(current_md_consensus);
  392. relay3_client->padding_enabled = 1;
  393. client_relay3->padding_enabled = 1;
  394. tried_to_write_cell = 0;
  395. get_options_mutable()->ORPort_set = 0;
  396. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  397. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  398. client_relay3->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  399. decision = channelpadding_decide_to_pad_channel(client_relay3);
  400. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  401. tt_assert(client_relay3->pending_padding_callback);
  402. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  403. decision = channelpadding_decide_to_pad_channel(client_relay3);
  404. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  405. // Wait for the timer
  406. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  407. monotime_coarse_set_mock_time_nsec(new_time);
  408. monotime_set_mock_time_nsec(new_time);
  409. timers_run_pending();
  410. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  411. tt_assert(!client_relay3->pending_padding_callback);
  412. // Then test disabling each via consensus param
  413. smartlist_add(current_md_consensus->net_params,
  414. (void*)"nf_pad_single_onion=0");
  415. channelpadding_new_consensus_params(current_md_consensus);
  416. // Before the client tries to pad, the relay will still pad:
  417. tried_to_write_cell = 0;
  418. relay3_client->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  419. get_options_mutable()->ORPort_set = 1;
  420. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  421. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  422. decision = channelpadding_decide_to_pad_channel(relay3_client);
  423. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  424. tt_assert(relay3_client->pending_padding_callback);
  425. // Wait for the timer
  426. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  427. monotime_coarse_set_mock_time_nsec(new_time);
  428. monotime_set_mock_time_nsec(new_time);
  429. timers_run_pending();
  430. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  431. tt_assert(!client_relay3->pending_padding_callback);
  432. // Test client side (it should stop immediately)
  433. get_options_mutable()->HiddenServiceSingleHopMode = 1;
  434. get_options_mutable()->HiddenServiceNonAnonymousMode = 1;
  435. /* For the relay to recieve the negotiate: */
  436. get_options_mutable()->ORPort_set = 1;
  437. decision = channelpadding_decide_to_pad_channel(client_relay3);
  438. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  439. tt_assert(!client_relay3->pending_padding_callback);
  440. // Test relay side (it should have gotten the negotiation to disable)
  441. get_options_mutable()->ORPort_set = 1;
  442. get_options_mutable()->HiddenServiceSingleHopMode = 0;
  443. get_options_mutable()->HiddenServiceNonAnonymousMode = 0;
  444. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  445. CHANNELPADDING_WONTPAD);
  446. tt_assert(!relay3_client->padding_enabled);
  447. done:
  448. free_mock_consensus();
  449. free_mock_network();
  450. tor_free(relay);
  451. timers_shutdown();
  452. monotime_disable_test_mocking();
  453. channel_free_all();
  454. }
  455. void
  456. test_channelpadding_consensus(void *arg)
  457. {
  458. channelpadding_decision_t decision;
  459. or_options_t *options = get_options_mutable();
  460. int64_t val;
  461. int64_t new_time;
  462. (void)arg;
  463. tor_libevent_postfork();
  464. /*
  465. * Params tested:
  466. * nf_pad_before_usage
  467. * nf_pad_relays
  468. * nf_ito_low
  469. * nf_ito_high
  470. *
  471. * Plan:
  472. * 1. Padding can be completely disabled via consensus
  473. * 2. Negotiation can't re-enable consensus-disabled padding
  474. * 3. Negotiation can't increase padding from relays beyond
  475. * consensus defaults
  476. * 4. Relay-to-relay padding can be enabled/disabled in consensus
  477. * 5. Can enable/disable padding before actually using a connection
  478. * 6. Can we control circ and TLS conn lifetime from the consensus?
  479. */
  480. channel_t *chan;
  481. routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
  482. monotime_enable_test_mocking();
  483. monotime_set_mock_time_nsec(1);
  484. monotime_coarse_set_mock_time_nsec(1);
  485. timers_initialize();
  486. if (!connection_array)
  487. connection_array = smartlist_new();
  488. chan = (channel_t*)new_fake_channeltls(0);
  489. channel_timestamp_active(chan);
  490. setup_mock_consensus();
  491. get_options_mutable()->ORPort_set = 1;
  492. /* Test 1: Padding can be completely disabled via consensus */
  493. tried_to_write_cell = 0;
  494. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  495. decision = channelpadding_decide_to_pad_channel(chan);
  496. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  497. tt_assert(chan->pending_padding_callback);
  498. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  499. decision = channelpadding_decide_to_pad_channel(chan);
  500. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  501. // Wait for the timer
  502. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  503. monotime_coarse_set_mock_time_nsec(new_time);
  504. monotime_set_mock_time_nsec(new_time);
  505. timers_run_pending();
  506. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  507. tt_assert(!chan->pending_padding_callback);
  508. smartlist_add(current_md_consensus->net_params,
  509. (void*)"nf_ito_low=0");
  510. smartlist_add(current_md_consensus->net_params,
  511. (void*)"nf_ito_high=0");
  512. get_options_mutable()->ConnectionPadding = 1;
  513. channelpadding_new_consensus_params(current_md_consensus);
  514. decision = channelpadding_decide_to_pad_channel(chan);
  515. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  516. tt_assert(!chan->pending_padding_callback);
  517. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  518. tt_i64_op(val, OP_EQ, 0);
  519. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  520. tt_i64_op(val, OP_EQ, -2);
  521. /* Test 2: Negotiation can't re-enable consensus-disabled padding */
  522. channelpadding_send_enable_command(chan, 100, 200);
  523. tried_to_write_cell = 0;
  524. decision = channelpadding_decide_to_pad_channel(chan);
  525. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  526. tt_assert(!chan->pending_padding_callback);
  527. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  528. tt_i64_op(val, OP_EQ, 0);
  529. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  530. tt_i64_op(val, OP_EQ, -2);
  531. tt_assert(!chan->next_padding_time_ms);
  532. smartlist_clear(current_md_consensus->net_params);
  533. /* Test 3: Negotiation can't increase padding from relays beyond consensus
  534. * values */
  535. smartlist_add(current_md_consensus->net_params,
  536. (void*)"nf_ito_low=100");
  537. smartlist_add(current_md_consensus->net_params,
  538. (void*)"nf_ito_high=200");
  539. channelpadding_new_consensus_params(current_md_consensus);
  540. tried_to_write_cell = 0;
  541. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  542. decision = channelpadding_decide_to_pad_channel(chan);
  543. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  544. tt_assert(chan->pending_padding_callback);
  545. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  546. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  547. tt_i64_op(val, OP_GE, 100);
  548. tt_i64_op(val, OP_LE, 200);
  549. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  550. tt_i64_op(val, OP_LE, 200);
  551. // Wait for the timer
  552. new_time = (monotime_coarse_absolute_msec()+201)*NSEC_PER_MSEC;
  553. monotime_set_mock_time_nsec(new_time);
  554. monotime_coarse_set_mock_time_nsec(new_time);
  555. timers_run_pending();
  556. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  557. tt_assert(!chan->pending_padding_callback);
  558. smartlist_clear(current_md_consensus->net_params);
  559. smartlist_add(current_md_consensus->net_params,
  560. (void*)"nf_ito_low=1500");
  561. smartlist_add(current_md_consensus->net_params,
  562. (void*)"nf_ito_high=4500");
  563. channelpadding_new_consensus_params(current_md_consensus);
  564. channelpadding_send_enable_command(chan, 100, 200);
  565. tried_to_write_cell = 0;
  566. decision = channelpadding_decide_to_pad_channel(chan);
  567. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  568. tt_assert(!chan->pending_padding_callback);
  569. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  570. tt_i64_op(val, OP_GE, 1500);
  571. tt_i64_op(val, OP_LE, 4500);
  572. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  573. tt_i64_op(val, OP_LE, 4500);
  574. /* Test 4: Relay-to-relay padding can be enabled/disabled in consensus */
  575. /* Make this channel a relay's channel */
  576. memcpy(relay->identity_digest,
  577. ((channel_tls_t *)chan)->conn->identity_digest, DIGEST_LEN);
  578. smartlist_add(current_md_consensus->routerstatus_list, relay);
  579. tried_to_write_cell = 0;
  580. decision = channelpadding_decide_to_pad_channel(chan);
  581. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  582. tt_assert(!chan->pending_padding_callback);
  583. smartlist_add(current_md_consensus->net_params,
  584. (void*)"nf_pad_relays=1");
  585. channelpadding_new_consensus_params(current_md_consensus);
  586. decision = channelpadding_decide_to_pad_channel(chan);
  587. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  588. tt_assert(!chan->pending_padding_callback);
  589. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  590. tt_i64_op(val, OP_GE, 1500);
  591. tt_i64_op(val, OP_LE, 4500);
  592. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  593. tt_i64_op(val, OP_LE, 4500);
  594. /* Test 5: If we disable padding before channel usage, does that work? */
  595. smartlist_add(current_md_consensus->net_params,
  596. (void*)"nf_pad_before_usage=0");
  597. channelpadding_new_consensus_params(current_md_consensus);
  598. tried_to_write_cell = 0;
  599. decision = channelpadding_decide_to_pad_channel(chan);
  600. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  601. tt_assert(!chan->pending_padding_callback);
  602. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  603. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  604. tt_i64_op(val, OP_GE, 180);
  605. tt_i64_op(val, OP_LE, 180+90);
  606. val = channelpadding_get_channel_idle_timeout(chan, 0);
  607. tt_i64_op(val, OP_GE, 180);
  608. tt_i64_op(val, OP_LE, 180+90);
  609. options->ReducedConnectionPadding = 1;
  610. val = channelpadding_get_channel_idle_timeout(chan, 0);
  611. tt_i64_op(val, OP_GE, 180/2);
  612. tt_i64_op(val, OP_LE, (180+90)/2);
  613. options->ReducedConnectionPadding = 0;
  614. options->ORPort_set = 1;
  615. smartlist_add(current_md_consensus->net_params,
  616. (void*)"nf_conntimeout_relays=600");
  617. channelpadding_new_consensus_params(current_md_consensus);
  618. val = channelpadding_get_channel_idle_timeout(chan, 1);
  619. tt_i64_op(val, OP_GE, 450);
  620. tt_i64_op(val, OP_LE, 750);
  621. val = channelpadding_get_circuits_available_timeout();
  622. tt_i64_op(val, OP_GE, 30*60);
  623. tt_i64_op(val, OP_LE, 30*60*2);
  624. options->ReducedConnectionPadding = 1;
  625. smartlist_add(current_md_consensus->net_params,
  626. (void*)"nf_conntimeout_clients=600");
  627. channelpadding_new_consensus_params(current_md_consensus);
  628. val = channelpadding_get_circuits_available_timeout();
  629. tt_i64_op(val, OP_GE, 600/2);
  630. tt_i64_op(val, OP_LE, 600*2/2);
  631. options->ReducedConnectionPadding = 0;
  632. options->CircuitsAvailableTimeout = 24*60*60;
  633. val = channelpadding_get_circuits_available_timeout();
  634. tt_i64_op(val, OP_GE, 24*60*60);
  635. tt_i64_op(val, OP_LE, 24*60*60*2);
  636. done:
  637. free_mock_consensus();
  638. free_fake_channeltls((channel_tls_t*)chan);
  639. smartlist_free(connection_array);
  640. timers_shutdown();
  641. monotime_disable_test_mocking();
  642. channel_free_all();
  643. return;
  644. }
  645. void
  646. test_channelpadding_negotiation(void *arg)
  647. {
  648. channelpadding_negotiate_t disable;
  649. cell_t cell;
  650. channelpadding_decision_t decision;
  651. int val;
  652. (void)arg;
  653. /* Plan:
  654. * 1. Clients reject negotiation, relays accept it.
  655. * * Bridges accept negotiation from their clients,
  656. * but not from relays.
  657. * 2. Torrc options can override client-side negotiation
  658. * 3. Test a version issue in channelpadidng cell
  659. * 4. Test channelpadding_reduced_padding
  660. */
  661. monotime_init();
  662. monotime_enable_test_mocking();
  663. monotime_set_mock_time_nsec(1);
  664. monotime_coarse_set_mock_time_nsec(1);
  665. timers_initialize();
  666. setup_mock_consensus();
  667. setup_mock_network();
  668. /* Test case #1: Do the right things ignore negotiation? */
  669. /* relay-to-client case: */
  670. channelpadding_send_disable_command(relay3_client);
  671. tt_assert(client_relay3->padding_enabled);
  672. /* client-to-relay case: */
  673. get_options_mutable()->ORPort_set = 1;
  674. channelpadding_disable_padding_on_channel(client_relay3);
  675. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  676. CHANNELPADDING_WONTPAD);
  677. tt_assert(!relay3_client->padding_enabled);
  678. relay3_client->padding_enabled = 1;
  679. client_relay3->padding_enabled = 1;
  680. /* Bridge case from relay */
  681. get_options_mutable()->BridgeRelay = 1;
  682. channelpadding_disable_padding_on_channel(relay2_relay1);
  683. tt_assert(relay1_relay2->padding_enabled);
  684. /* Bridge case from client */
  685. channelpadding_disable_padding_on_channel(client_relay3);
  686. tt_assert(!relay3_client->padding_enabled);
  687. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  688. CHANNELPADDING_WONTPAD);
  689. relay3_client->padding_enabled = 1;
  690. client_relay3->padding_enabled = 1;
  691. get_options_mutable()->BridgeRelay = 0;
  692. get_options_mutable()->ORPort_set = 0;
  693. /* Test case #2: Torrc options */
  694. /* ConnectionPadding auto; Relay doesn't suport us */
  695. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  696. relay3_client->padding_enabled = 0;
  697. tried_to_write_cell = 0;
  698. decision = channelpadding_decide_to_pad_channel(relay3_client);
  699. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  700. tt_assert(!relay3_client->pending_padding_callback);
  701. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  702. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  703. relay3_client->padding_enabled = 1;
  704. /* ConnectionPadding 1; Relay doesn't suport us */
  705. get_options_mutable()->ConnectionPadding = 1;
  706. tried_to_write_cell = 0;
  707. decision = channelpadding_decide_to_pad_channel(client_relay3);
  708. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  709. tt_assert(!client_relay3->pending_padding_callback);
  710. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  711. get_options_mutable()->ConnectionPadding = 0;
  712. /* Test case #3: Test a version issue in channelpadding cell */
  713. get_options_mutable()->ORPort_set = 1;
  714. client_relay3->padding_enabled = 1;
  715. relay3_client->padding_enabled = 1;
  716. memset(&cell, 0, sizeof(cell_t));
  717. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  718. cell.command = CELL_PADDING_NEGOTIATE;
  719. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  720. disable.version = 1;
  721. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  722. client_relay3->write_cell(client_relay3, &cell);
  723. tt_assert(relay3_client->padding_enabled);
  724. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  725. OP_EQ, -1);
  726. tt_assert(client_relay3->padding_enabled);
  727. disable.version = 0;
  728. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  729. client_relay3->write_cell(client_relay3, &cell);
  730. tt_assert(!relay3_client->padding_enabled);
  731. /* Test case 4: Reducing padding actually reduces it */
  732. relay3_client->padding_enabled = 1;
  733. client_relay3->padding_enabled = 1;
  734. decision = channelpadding_decide_to_pad_channel(relay3_client);
  735. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  736. channelpadding_reduce_padding_on_channel(client_relay3);
  737. tried_to_write_cell = 0;
  738. decision = channelpadding_decide_to_pad_channel(relay3_client);
  739. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  740. get_options_mutable()->ORPort_set = 0;
  741. decision = channelpadding_decide_to_pad_channel(client_relay3);
  742. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  743. tt_assert(!client_relay3->pending_padding_callback);
  744. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  745. tt_int_op(val, OP_GE, 9000);
  746. tt_int_op(val, OP_LE, 14000);
  747. int64_t val64 =
  748. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  749. tt_i64_op(val64, OP_LE, 14000);
  750. done:
  751. free_mock_network();
  752. free_mock_consensus();
  753. timers_shutdown();
  754. monotime_disable_test_mocking();
  755. channel_free_all();
  756. return;
  757. }
  758. void
  759. test_channelpadding_decide_to_pad_channel(void *arg)
  760. {
  761. channelpadding_decision_t decision;
  762. /**
  763. * Test case plan:
  764. *
  765. * 1. Channel that has "sent a packet" before the timeout.
  766. * + We should decide to pad later
  767. * 2. Channel that has not "sent a packet" before the timeout:
  768. * 2a. Not within 1.1s of the timeout.
  769. * + We should decide to pad later
  770. * 2b. Within 1.1s of the timemout.
  771. * + We should schedule padding
  772. * + We should get feedback that we wrote a cell
  773. * 2c. Within 0.1s of the timeout.
  774. * + We should schedule padding
  775. * + We should get feedback that we wrote a cell
  776. * 2d. Channel that asks to pad while timeout is scheduled
  777. * + We should schedule padding
  778. * + We should get feedback that we wrote a cell
  779. * 2e. 0s of the timeout
  780. * + We should send padding immediately
  781. * + We should get feedback that we wrote a cell
  782. * 2f. <0s of the timeout
  783. * + We should send padding immediately
  784. * + We should get feedback that we wrote a cell
  785. * 3. Channel that sends a packet while timeout is scheduled
  786. * + We should not get feedback that we wrote a cell
  787. * 4. Channel that closes while timeout is scheduled
  788. * + We should not get feedback that we wrote a cell
  789. * 5. Make sure the channel still would work if repaired
  790. * + We should be able to schedule padding and resend
  791. * 6. Channel is not used for full circuits
  792. * 7. Channel that disappears while timeout is scheduled
  793. * + We should not send padding
  794. */
  795. channel_t *chan;
  796. int64_t new_time;
  797. if (!connection_array)
  798. connection_array = smartlist_new();
  799. (void)arg;
  800. tor_libevent_postfork();
  801. monotime_init();
  802. monotime_enable_test_mocking();
  803. monotime_set_mock_time_nsec(1);
  804. monotime_coarse_set_mock_time_nsec(1);
  805. timers_initialize();
  806. setup_full_capture_of_logs(LOG_WARN);
  807. channelpadding_new_consensus_params(NULL);
  808. chan = (channel_t*)new_fake_channeltls(0);
  809. channel_timestamp_active(chan);
  810. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  811. tried_to_write_cell = 0;
  812. decision = channelpadding_decide_to_pad_channel(chan);
  813. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  814. tt_assert(!chan->pending_padding_callback);
  815. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  816. /* Test case #2a: > 1.1s until timeout */
  817. tried_to_write_cell = 0;
  818. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1200;
  819. decision = channelpadding_decide_to_pad_channel(chan);
  820. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  821. tt_assert(!chan->pending_padding_callback);
  822. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  823. /* Test case #2b: >= 1.0s until timeout */
  824. tried_to_write_cell = 0;
  825. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1000;
  826. decision = channelpadding_decide_to_pad_channel(chan);
  827. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  828. tt_assert(chan->pending_padding_callback);
  829. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  830. // Wait for the timer from case #2b
  831. new_time = (monotime_coarse_absolute_msec() + 1000)*NSEC_PER_MSEC;
  832. monotime_set_mock_time_nsec(new_time);
  833. monotime_coarse_set_mock_time_nsec(new_time);
  834. timers_run_pending();
  835. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  836. tt_assert(!chan->pending_padding_callback);
  837. /* Test case #2c: > 0.1s until timeout */
  838. tried_to_write_cell = 0;
  839. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  840. decision = channelpadding_decide_to_pad_channel(chan);
  841. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  842. tt_assert(chan->pending_padding_callback);
  843. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  844. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  845. decision = channelpadding_decide_to_pad_channel(chan);
  846. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  847. // Wait for the timer
  848. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  849. monotime_coarse_set_mock_time_nsec(new_time);
  850. monotime_set_mock_time_nsec(new_time);
  851. timers_run_pending();
  852. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  853. tt_assert(!chan->pending_padding_callback);
  854. /* Test case #2e: 0s until timeout */
  855. tried_to_write_cell = 0;
  856. chan->next_padding_time_ms = monotime_coarse_absolute_msec();
  857. decision = channelpadding_decide_to_pad_channel(chan);
  858. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  859. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  860. tt_assert(!chan->pending_padding_callback);
  861. /* Test case #2f: <0s until timeout */
  862. tried_to_write_cell = 0;
  863. chan->next_padding_time_ms = monotime_coarse_absolute_msec() - 100;
  864. decision = channelpadding_decide_to_pad_channel(chan);
  865. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  866. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  867. tt_assert(!chan->pending_padding_callback);
  868. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  869. tried_to_write_cell = 0;
  870. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  871. decision = channelpadding_decide_to_pad_channel(chan);
  872. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  873. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  874. tt_assert(chan->pending_padding_callback);
  875. // Pretend the channel sent a packet
  876. channel_timestamp_active(chan);
  877. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  878. // Wait for the timer
  879. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  880. monotime_coarse_set_mock_time_nsec(new_time);
  881. monotime_set_mock_time_nsec(new_time);
  882. timers_run_pending();
  883. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  884. tt_assert(!chan->pending_padding_callback);
  885. /* Test case #4: Channel that closes while a timeout is scheduled */
  886. tried_to_write_cell = 0;
  887. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  888. decision = channelpadding_decide_to_pad_channel(chan);
  889. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  890. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  891. tt_assert(chan->pending_padding_callback);
  892. // Pretend the channel is temporarily down
  893. chan->state = CHANNEL_STATE_MAINT;
  894. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  895. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  896. monotime_coarse_set_mock_time_nsec(new_time);
  897. monotime_set_mock_time_nsec(new_time);
  898. timers_run_pending();
  899. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  900. tt_assert(!chan->pending_padding_callback);
  901. chan->state = CHANNEL_STATE_OPEN;
  902. /* Test case #5: Make sure previous test case didn't break everything */
  903. tried_to_write_cell = 0;
  904. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  905. decision = channelpadding_decide_to_pad_channel(chan);
  906. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  907. tt_assert(chan->pending_padding_callback);
  908. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  909. // Wait for the timer
  910. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  911. monotime_coarse_set_mock_time_nsec(new_time);
  912. monotime_set_mock_time_nsec(new_time);
  913. timers_run_pending();
  914. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  915. tt_assert(!chan->pending_padding_callback);
  916. /* Test case #6. Channel is not used for full circuits */
  917. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  918. decision = channelpadding_decide_to_pad_channel(chan);
  919. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  920. tt_assert(!chan->pending_padding_callback);
  921. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  922. /* Test case #7. Channel is closed while timeout is scheduled.
  923. *
  924. * NOTE: This test deliberately breaks the channel callback mechanism.
  925. * It must be last.
  926. */
  927. tried_to_write_cell = 0;
  928. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  929. decision = channelpadding_decide_to_pad_channel(chan);
  930. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  931. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  932. tt_assert(chan->pending_padding_callback);
  933. // Close the connection while the timer is scheduled
  934. free_fake_channeltls((channel_tls_t*)chan);
  935. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  936. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  937. monotime_coarse_set_mock_time_nsec(new_time);
  938. monotime_set_mock_time_nsec(new_time);
  939. timers_run_pending();
  940. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  941. done:
  942. smartlist_free(connection_array);
  943. teardown_capture_of_logs();
  944. monotime_disable_test_mocking();
  945. timers_shutdown();
  946. channel_free_all();
  947. return;
  948. }
  949. #define TEST_CHANNELPADDING(name, flags) \
  950. { #name, test_##name, (flags), NULL, NULL }
  951. struct testcase_t channelpadding_tests[] = {
  952. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  953. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  954. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  955. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  956. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  957. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  958. END_OF_TESTCASES
  959. };