test_channelpadding.c 38 KB

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