test_channelpadding.c 39 KB

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