test_channelpadding.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. relay = NULL; /* Prevent double-free */
  578. tried_to_write_cell = 0;
  579. decision = channelpadding_decide_to_pad_channel(chan);
  580. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  581. tt_assert(!chan->pending_padding_callback);
  582. smartlist_add(current_md_consensus->net_params,
  583. (void*)"nf_pad_relays=1");
  584. channelpadding_new_consensus_params(current_md_consensus);
  585. decision = channelpadding_decide_to_pad_channel(chan);
  586. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  587. tt_assert(!chan->pending_padding_callback);
  588. val = channelpadding_get_netflow_inactive_timeout_ms(chan);
  589. tt_i64_op(val, OP_GE, 1500);
  590. tt_i64_op(val, OP_LE, 4500);
  591. val = channelpadding_compute_time_until_pad_for_netflow(chan);
  592. tt_i64_op(val, OP_LE, 4500);
  593. /* Test 5: If we disable padding before channel usage, does that work? */
  594. smartlist_add(current_md_consensus->net_params,
  595. (void*)"nf_pad_before_usage=0");
  596. channelpadding_new_consensus_params(current_md_consensus);
  597. tried_to_write_cell = 0;
  598. decision = channelpadding_decide_to_pad_channel(chan);
  599. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  600. tt_assert(!chan->pending_padding_callback);
  601. /* Test 6: Can we control circ and TLS conn lifetime from the consensus? */
  602. val = channelpadding_get_channel_idle_timeout(NULL, 0);
  603. tt_i64_op(val, OP_GE, 180);
  604. tt_i64_op(val, OP_LE, 180+90);
  605. val = channelpadding_get_channel_idle_timeout(chan, 0);
  606. tt_i64_op(val, OP_GE, 180);
  607. tt_i64_op(val, OP_LE, 180+90);
  608. options->ReducedConnectionPadding = 1;
  609. val = channelpadding_get_channel_idle_timeout(chan, 0);
  610. tt_i64_op(val, OP_GE, 180/2);
  611. tt_i64_op(val, OP_LE, (180+90)/2);
  612. options->ReducedConnectionPadding = 0;
  613. options->ORPort_set = 1;
  614. smartlist_add(current_md_consensus->net_params,
  615. (void*)"nf_conntimeout_relays=600");
  616. channelpadding_new_consensus_params(current_md_consensus);
  617. val = channelpadding_get_channel_idle_timeout(chan, 1);
  618. tt_i64_op(val, OP_GE, 450);
  619. tt_i64_op(val, OP_LE, 750);
  620. val = channelpadding_get_circuits_available_timeout();
  621. tt_i64_op(val, OP_GE, 30*60);
  622. tt_i64_op(val, OP_LE, 30*60*2);
  623. options->ReducedConnectionPadding = 1;
  624. smartlist_add(current_md_consensus->net_params,
  625. (void*)"nf_conntimeout_clients=600");
  626. channelpadding_new_consensus_params(current_md_consensus);
  627. val = channelpadding_get_circuits_available_timeout();
  628. tt_i64_op(val, OP_GE, 600/2);
  629. tt_i64_op(val, OP_LE, 600*2/2);
  630. options->ReducedConnectionPadding = 0;
  631. options->CircuitsAvailableTimeout = 24*60*60;
  632. val = channelpadding_get_circuits_available_timeout();
  633. tt_i64_op(val, OP_GE, 24*60*60);
  634. tt_i64_op(val, OP_LE, 24*60*60*2);
  635. done:
  636. tor_free(relay);
  637. free_mock_consensus();
  638. free_fake_channeltls((channel_tls_t*)chan);
  639. smartlist_free(connection_array);
  640. timers_shutdown();
  641. monotime_disable_test_mocking();
  642. channel_free_all();
  643. return;
  644. }
  645. void
  646. test_channelpadding_negotiation(void *arg)
  647. {
  648. channelpadding_negotiate_t disable;
  649. cell_t cell;
  650. channelpadding_decision_t decision;
  651. int val;
  652. (void)arg;
  653. /* Plan:
  654. * 1. Clients reject negotiation, relays accept it.
  655. * * Bridges accept negotiation from their clients,
  656. * but not from relays.
  657. * 2. Torrc options can override client-side negotiation
  658. * 3. Test a version issue in channelpadidng cell
  659. * 4. Test channelpadding_reduced_padding
  660. */
  661. monotime_init();
  662. monotime_enable_test_mocking();
  663. monotime_set_mock_time_nsec(1);
  664. monotime_coarse_set_mock_time_nsec(1);
  665. timers_initialize();
  666. setup_mock_consensus();
  667. setup_mock_network();
  668. /* Test case #1: Do the right things ignore negotiation? */
  669. /* relay-to-client case: */
  670. channelpadding_send_disable_command(relay3_client);
  671. tt_assert(client_relay3->padding_enabled);
  672. /* client-to-relay case: */
  673. get_options_mutable()->ORPort_set = 1;
  674. channelpadding_disable_padding_on_channel(client_relay3);
  675. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  676. CHANNELPADDING_WONTPAD);
  677. tt_assert(!relay3_client->padding_enabled);
  678. relay3_client->padding_enabled = 1;
  679. client_relay3->padding_enabled = 1;
  680. /* Bridge case from relay */
  681. get_options_mutable()->BridgeRelay = 1;
  682. channelpadding_disable_padding_on_channel(relay2_relay1);
  683. tt_assert(relay1_relay2->padding_enabled);
  684. /* Bridge case from client */
  685. channelpadding_disable_padding_on_channel(client_relay3);
  686. tt_assert(!relay3_client->padding_enabled);
  687. tt_int_op(channelpadding_decide_to_pad_channel(relay3_client), OP_EQ,
  688. CHANNELPADDING_WONTPAD);
  689. relay3_client->padding_enabled = 1;
  690. client_relay3->padding_enabled = 1;
  691. get_options_mutable()->BridgeRelay = 0;
  692. get_options_mutable()->ORPort_set = 0;
  693. /* Test case #2: Torrc options */
  694. /* ConnectionPadding auto; Relay doesn't suport us */
  695. ((channel_tls_t*)relay3_client)->conn->link_proto = 4;
  696. relay3_client->padding_enabled = 0;
  697. tried_to_write_cell = 0;
  698. decision = channelpadding_decide_to_pad_channel(relay3_client);
  699. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  700. tt_assert(!relay3_client->pending_padding_callback);
  701. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  702. ((channel_tls_t*)relay3_client)->conn->link_proto = 5;
  703. relay3_client->padding_enabled = 1;
  704. /* ConnectionPadding 1; Relay doesn't suport us */
  705. get_options_mutable()->ConnectionPadding = 1;
  706. tried_to_write_cell = 0;
  707. decision = channelpadding_decide_to_pad_channel(client_relay3);
  708. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  709. tt_assert(!client_relay3->pending_padding_callback);
  710. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  711. get_options_mutable()->ConnectionPadding = 0;
  712. /* Test case #3: Test a version issue in channelpadding cell */
  713. get_options_mutable()->ORPort_set = 1;
  714. client_relay3->padding_enabled = 1;
  715. relay3_client->padding_enabled = 1;
  716. memset(&cell, 0, sizeof(cell_t));
  717. memset(&disable, 0, sizeof(channelpadding_negotiate_t));
  718. cell.command = CELL_PADDING_NEGOTIATE;
  719. channelpadding_negotiate_set_command(&disable, CHANNELPADDING_COMMAND_STOP);
  720. disable.version = 1;
  721. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  722. client_relay3->write_cell(client_relay3, &cell);
  723. tt_assert(relay3_client->padding_enabled);
  724. tt_int_op(channelpadding_update_padding_for_channel(client_relay3, &disable),
  725. OP_EQ, -1);
  726. tt_assert(client_relay3->padding_enabled);
  727. disable.version = 0;
  728. channelpadding_negotiate_encode(cell.payload, CELL_PAYLOAD_SIZE, &disable);
  729. client_relay3->write_cell(client_relay3, &cell);
  730. tt_assert(!relay3_client->padding_enabled);
  731. /* Test case 4: Reducing padding actually reduces it */
  732. relay3_client->padding_enabled = 1;
  733. client_relay3->padding_enabled = 1;
  734. decision = channelpadding_decide_to_pad_channel(relay3_client);
  735. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  736. channelpadding_reduce_padding_on_channel(client_relay3);
  737. tried_to_write_cell = 0;
  738. decision = channelpadding_decide_to_pad_channel(relay3_client);
  739. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  740. get_options_mutable()->ORPort_set = 0;
  741. decision = channelpadding_decide_to_pad_channel(client_relay3);
  742. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  743. tt_assert(!client_relay3->pending_padding_callback);
  744. val = channelpadding_get_netflow_inactive_timeout_ms(client_relay3);
  745. tt_int_op(val, OP_GE, 9000);
  746. tt_int_op(val, OP_LE, 14000);
  747. int64_t val64 =
  748. channelpadding_compute_time_until_pad_for_netflow(client_relay3);
  749. tt_i64_op(val64, OP_LE, 14000);
  750. done:
  751. free_mock_network();
  752. free_mock_consensus();
  753. timers_shutdown();
  754. monotime_disable_test_mocking();
  755. channel_free_all();
  756. return;
  757. }
  758. void
  759. test_channelpadding_decide_to_pad_channel(void *arg)
  760. {
  761. channelpadding_decision_t decision;
  762. /**
  763. * Test case plan:
  764. *
  765. * 1. Channel that has "sent a packet" before the timeout.
  766. * + We should decide to pad later
  767. * 2. Channel that has not "sent a packet" before the timeout:
  768. * 2a. Not within 1.1s of the timeout.
  769. * + We should decide to pad later
  770. * 2b. Within 1.1s of the timemout.
  771. * + We should schedule padding
  772. * + We should get feedback that we wrote a cell
  773. * 2c. Within 0.1s of the timeout.
  774. * + We should schedule padding
  775. * + We should get feedback that we wrote a cell
  776. * 2d. Channel that asks to pad while timeout is scheduled
  777. * + We should schedule padding
  778. * + We should get feedback that we wrote a cell
  779. * 2e. 0s of the timeout
  780. * + We should send padding immediately
  781. * + We should get feedback that we wrote a cell
  782. * 2f. <0s of the timeout
  783. * + We should send padding immediately
  784. * + We should get feedback that we wrote a cell
  785. * 3. Channel that sends a packet while timeout is scheduled
  786. * + We should not get feedback that we wrote a cell
  787. * 4. Channel that closes while timeout is scheduled
  788. * + We should not get feedback that we wrote a cell
  789. * 5. Make sure the channel still would work if repaired
  790. * + We should be able to schedule padding and resend
  791. * 6. Channel is not used for full circuits
  792. * 7. Channel that disappears while timeout is scheduled
  793. * + We should not send padding
  794. */
  795. channel_t *chan;
  796. int64_t new_time;
  797. if (!connection_array)
  798. connection_array = smartlist_new();
  799. (void)arg;
  800. tor_libevent_postfork();
  801. monotime_init();
  802. monotime_enable_test_mocking();
  803. monotime_set_mock_time_nsec(1);
  804. monotime_coarse_set_mock_time_nsec(1);
  805. timers_initialize();
  806. setup_full_capture_of_logs(LOG_WARN);
  807. channelpadding_new_consensus_params(NULL);
  808. chan = (channel_t*)new_fake_channeltls(0);
  809. channel_timestamp_active(chan);
  810. /* Test case #1: Channel that has "sent a packet" before the timeout. */
  811. tried_to_write_cell = 0;
  812. decision = channelpadding_decide_to_pad_channel(chan);
  813. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  814. tt_assert(!chan->pending_padding_callback);
  815. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  816. /* Test case #2a: > 1.1s until timeout */
  817. tried_to_write_cell = 0;
  818. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1200;
  819. decision = channelpadding_decide_to_pad_channel(chan);
  820. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADLATER);
  821. tt_assert(!chan->pending_padding_callback);
  822. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  823. /* Test case #2b: >= 1.0s until timeout */
  824. tried_to_write_cell = 0;
  825. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 1000;
  826. decision = channelpadding_decide_to_pad_channel(chan);
  827. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  828. tt_assert(chan->pending_padding_callback);
  829. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  830. // Wait for the timer from case #2b
  831. new_time = (monotime_coarse_absolute_msec() + 1000)*NSEC_PER_MSEC;
  832. monotime_set_mock_time_nsec(new_time);
  833. monotime_coarse_set_mock_time_nsec(new_time);
  834. timers_run_pending();
  835. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  836. tt_assert(!chan->pending_padding_callback);
  837. /* Test case #2c: > 0.1s until timeout */
  838. tried_to_write_cell = 0;
  839. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  840. decision = channelpadding_decide_to_pad_channel(chan);
  841. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  842. tt_assert(chan->pending_padding_callback);
  843. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  844. /* Test case #2d: Channel that asks to pad while timeout is scheduled */
  845. decision = channelpadding_decide_to_pad_channel(chan);
  846. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED);
  847. // Wait for the timer
  848. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  849. monotime_coarse_set_mock_time_nsec(new_time);
  850. monotime_set_mock_time_nsec(new_time);
  851. timers_run_pending();
  852. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  853. tt_assert(!chan->pending_padding_callback);
  854. /* Test case #2e: 0s until timeout */
  855. tried_to_write_cell = 0;
  856. chan->next_padding_time_ms = monotime_coarse_absolute_msec();
  857. decision = channelpadding_decide_to_pad_channel(chan);
  858. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  859. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  860. tt_assert(!chan->pending_padding_callback);
  861. /* Test case #2f: <0s until timeout */
  862. tried_to_write_cell = 0;
  863. chan->next_padding_time_ms = monotime_coarse_absolute_msec() - 100;
  864. decision = channelpadding_decide_to_pad_channel(chan);
  865. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SENT);
  866. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  867. tt_assert(!chan->pending_padding_callback);
  868. /* Test case #3: Channel that sends a packet while timeout is scheduled */
  869. tried_to_write_cell = 0;
  870. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  871. decision = channelpadding_decide_to_pad_channel(chan);
  872. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  873. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  874. tt_assert(chan->pending_padding_callback);
  875. // Pretend the channel sent a packet
  876. channel_timestamp_active(chan);
  877. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  878. // Wait for the timer
  879. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  880. monotime_coarse_set_mock_time_nsec(new_time);
  881. monotime_set_mock_time_nsec(new_time);
  882. timers_run_pending();
  883. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  884. tt_assert(!chan->pending_padding_callback);
  885. /* Test case #4: Channel that closes while a timeout is scheduled */
  886. tried_to_write_cell = 0;
  887. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  888. decision = channelpadding_decide_to_pad_channel(chan);
  889. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  890. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  891. tt_assert(chan->pending_padding_callback);
  892. // Pretend the channel is temporarily down
  893. chan->state = CHANNEL_STATE_MAINT;
  894. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  895. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  896. monotime_coarse_set_mock_time_nsec(new_time);
  897. monotime_set_mock_time_nsec(new_time);
  898. timers_run_pending();
  899. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  900. tt_assert(!chan->pending_padding_callback);
  901. chan->state = CHANNEL_STATE_OPEN;
  902. /* Test case #5: Make sure previous test case didn't break everything */
  903. tried_to_write_cell = 0;
  904. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  905. decision = channelpadding_decide_to_pad_channel(chan);
  906. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  907. tt_assert(chan->pending_padding_callback);
  908. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  909. // Wait for the timer
  910. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  911. monotime_coarse_set_mock_time_nsec(new_time);
  912. monotime_set_mock_time_nsec(new_time);
  913. timers_run_pending();
  914. tt_int_op(tried_to_write_cell, OP_EQ, 1);
  915. tt_assert(!chan->pending_padding_callback);
  916. /* Test case #6. Channel is not used for full circuits */
  917. chan->channel_usage = CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS;
  918. decision = channelpadding_decide_to_pad_channel(chan);
  919. tt_int_op(decision, OP_EQ, CHANNELPADDING_WONTPAD);
  920. tt_assert(!chan->pending_padding_callback);
  921. chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  922. /* Test case #7. Channel is closed while timeout is scheduled.
  923. *
  924. * NOTE: This test deliberately breaks the channel callback mechanism.
  925. * It must be last.
  926. */
  927. tried_to_write_cell = 0;
  928. chan->next_padding_time_ms = monotime_coarse_absolute_msec() + 100;
  929. decision = channelpadding_decide_to_pad_channel(chan);
  930. tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_SCHEDULED);
  931. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  932. tt_assert(chan->pending_padding_callback);
  933. // Close the connection while the timer is scheduled
  934. free_fake_channeltls((channel_tls_t*)chan);
  935. // We don't expect any timer callbacks here. Make a dummy one to be sure.
  936. new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC;
  937. monotime_coarse_set_mock_time_nsec(new_time);
  938. monotime_set_mock_time_nsec(new_time);
  939. timers_run_pending();
  940. tt_int_op(tried_to_write_cell, OP_EQ, 0);
  941. done:
  942. smartlist_free(connection_array);
  943. teardown_capture_of_logs();
  944. monotime_disable_test_mocking();
  945. timers_shutdown();
  946. channel_free_all();
  947. return;
  948. }
  949. #define TEST_CHANNELPADDING(name, flags) \
  950. { #name, test_##name, (flags), NULL, NULL }
  951. struct testcase_t channelpadding_tests[] = {
  952. //TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, 0),
  953. TEST_CHANNELPADDING(channelpadding_decide_to_pad_channel, TT_FORK),
  954. TEST_CHANNELPADDING(channelpadding_negotiation, TT_FORK),
  955. TEST_CHANNELPADDING(channelpadding_consensus, TT_FORK),
  956. TEST_CHANNELPADDING(channelpadding_killonehop, TT_FORK),
  957. TEST_CHANNELPADDING(channelpadding_timers, TT_FORK),
  958. END_OF_TESTCASES
  959. };