test_circuitpadding.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. #define TOR_CHANNEL_INTERNAL_
  2. #define TOR_TIMERS_PRIVATE
  3. #define CIRCUITPADDING_PRIVATE
  4. #define NETWORKSTATUS_PRIVATE
  5. #define CRYPT_PATH_PRIVATE
  6. #include "core/or/or.h"
  7. #include "test.h"
  8. #include "lib/testsupport/testsupport.h"
  9. #include "core/or/connection_or.h"
  10. #include "core/or/channel.h"
  11. #include "core/or/channeltls.h"
  12. #include "core/or/crypt_path.h"
  13. #include <event.h>
  14. #include "lib/evloop/compat_libevent.h"
  15. #include "lib/time/compat_time.h"
  16. #include "lib/defs/time.h"
  17. #include "core/or/relay.h"
  18. #include "core/or/circuitlist.h"
  19. #include "core/or/circuitbuild.h"
  20. #include "core/or/circuitpadding.h"
  21. #include "core/mainloop/netstatus.h"
  22. #include "core/crypto/relay_crypto.h"
  23. #include "core/or/protover.h"
  24. #include "feature/nodelist/nodelist.h"
  25. #include "lib/evloop/compat_libevent.h"
  26. #include "app/config/config.h"
  27. #include "feature/nodelist/routerstatus_st.h"
  28. #include "feature/nodelist/networkstatus_st.h"
  29. #include "feature/nodelist/node_st.h"
  30. #include "core/or/cell_st.h"
  31. #include "core/or/crypt_path_st.h"
  32. #include "core/or/or_circuit_st.h"
  33. #include "core/or/origin_circuit_st.h"
  34. /* Start our monotime mocking at 1 second past whatever monotime_init()
  35. * thought the actual wall clock time was, for platforms with bad resolution
  36. * and weird timevalues during monotime_init() before mocking. */
  37. #define MONOTIME_MOCK_START (monotime_absolute_nsec()+\
  38. TOR_NSEC_PER_USEC*TOR_USEC_PER_SEC)
  39. extern smartlist_t *connection_array;
  40. circid_t get_unique_circ_id_by_chan(channel_t *chan);
  41. void helper_create_basic_machine(void);
  42. static void helper_create_conditional_machines(void);
  43. static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
  44. channel_t *new_fake_channel(void);
  45. void test_circuitpadding_negotiation(void *arg);
  46. void test_circuitpadding_wronghop(void *arg);
  47. void test_circuitpadding_conditions(void *arg);
  48. void test_circuitpadding_serialize(void *arg);
  49. void test_circuitpadding_rtt(void *arg);
  50. void test_circuitpadding_tokens(void *arg);
  51. static void
  52. simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
  53. int padding);
  54. void free_fake_orcirc(circuit_t *circ);
  55. void free_fake_origin_circuit(origin_circuit_t *circ);
  56. static int deliver_negotiated = 1;
  57. static int64_t curr_mocked_time;
  58. static node_t padding_node;
  59. static node_t non_padding_node;
  60. static channel_t dummy_channel;
  61. static circpad_machine_spec_t circ_client_machine;
  62. static void
  63. timers_advance_and_run(int64_t msec_update)
  64. {
  65. curr_mocked_time += msec_update*TOR_NSEC_PER_MSEC;
  66. monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  67. monotime_set_mock_time_nsec(curr_mocked_time);
  68. timers_run_pending();
  69. }
  70. static void
  71. nodes_init(void)
  72. {
  73. padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
  74. padding_node.rs->pv.supports_padding = 1;
  75. non_padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
  76. non_padding_node.rs->pv.supports_padding = 0;
  77. }
  78. static void
  79. nodes_free(void)
  80. {
  81. tor_free(padding_node.rs);
  82. tor_free(non_padding_node.rs);
  83. }
  84. static const node_t *
  85. node_get_by_id_mock(const char *identity_digest)
  86. {
  87. if (identity_digest[0] == 1) {
  88. return &padding_node;
  89. } else if (identity_digest[0] == 0) {
  90. return &non_padding_node;
  91. }
  92. return NULL;
  93. }
  94. static or_circuit_t *
  95. new_fake_orcirc(channel_t *nchan, channel_t *pchan)
  96. {
  97. or_circuit_t *orcirc = NULL;
  98. circuit_t *circ = NULL;
  99. crypt_path_t tmp_cpath;
  100. char whatevs_key[CPATH_KEY_MATERIAL_LEN];
  101. orcirc = tor_malloc_zero(sizeof(*orcirc));
  102. circ = &(orcirc->base_);
  103. circ->magic = OR_CIRCUIT_MAGIC;
  104. //circ->n_chan = nchan;
  105. circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
  106. cell_queue_init(&(circ->n_chan_cells));
  107. circ->n_hop = NULL;
  108. circ->streams_blocked_on_n_chan = 0;
  109. circ->streams_blocked_on_p_chan = 0;
  110. circ->n_delete_pending = 0;
  111. circ->p_delete_pending = 0;
  112. circ->received_destroy = 0;
  113. circ->state = CIRCUIT_STATE_OPEN;
  114. circ->purpose = CIRCUIT_PURPOSE_OR;
  115. circ->package_window = CIRCWINDOW_START_MAX;
  116. circ->deliver_window = CIRCWINDOW_START_MAX;
  117. circ->n_chan_create_cell = NULL;
  118. //orcirc->p_chan = pchan;
  119. orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
  120. cell_queue_init(&(orcirc->p_chan_cells));
  121. circuit_set_p_circid_chan(orcirc, orcirc->p_circ_id, pchan);
  122. circuit_set_n_circid_chan(circ, circ->n_circ_id, nchan);
  123. memset(&tmp_cpath, 0, sizeof(tmp_cpath));
  124. if (cpath_init_circuit_crypto(&tmp_cpath, whatevs_key,
  125. sizeof(whatevs_key), 0, 0)<0) {
  126. log_warn(LD_BUG,"Circuit initialization failed");
  127. return NULL;
  128. }
  129. orcirc->crypto = tmp_cpath.pvt_crypto;
  130. return orcirc;
  131. }
  132. void
  133. free_fake_orcirc(circuit_t *circ)
  134. {
  135. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  136. relay_crypto_clear(&orcirc->crypto);
  137. circpad_circuit_free_all_machineinfos(circ);
  138. tor_free(circ);
  139. }
  140. void
  141. free_fake_origin_circuit(origin_circuit_t *circ)
  142. {
  143. circpad_circuit_free_all_machineinfos(TO_CIRCUIT(circ));
  144. circuit_clear_cpath(circ);
  145. tor_free(circ);
  146. }
  147. void dummy_nop_timer(void);
  148. //static int dont_stop_libevent = 0;
  149. static circuit_t *client_side;
  150. static circuit_t *relay_side;
  151. static int n_client_cells = 0;
  152. static int n_relay_cells = 0;
  153. static int
  154. circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
  155. cell_direction_t cell_direction,
  156. crypt_path_t *layer_hint, streamid_t on_stream,
  157. const char *filename, int lineno);
  158. static void
  159. circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
  160. cell_direction_t direction);
  161. static void
  162. circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
  163. cell_direction_t direction)
  164. {
  165. (void)cmux;
  166. (void)circ;
  167. (void)direction;
  168. return;
  169. }
  170. static int
  171. circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
  172. cell_direction_t cell_direction,
  173. crypt_path_t *layer_hint, streamid_t on_stream,
  174. const char *filename, int lineno)
  175. {
  176. (void)cell; (void)on_stream; (void)filename; (void)lineno;
  177. if (circ == client_side) {
  178. if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
  179. // Deliver to relay
  180. circpad_handle_padding_negotiate(relay_side, cell);
  181. } else {
  182. int is_target_hop = circpad_padding_is_from_expected_hop(circ,
  183. layer_hint);
  184. tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_OUT);
  185. tt_int_op(is_target_hop, OP_EQ, 1);
  186. // No need to pretend a padding cell was sent: This event is
  187. // now emitted internally when the circuitpadding code sends them.
  188. //circpad_cell_event_padding_sent(client_side);
  189. // Receive padding cell at middle
  190. circpad_deliver_recognized_relay_cell_events(relay_side,
  191. cell->payload[0], NULL);
  192. }
  193. n_client_cells++;
  194. } else if (circ == relay_side) {
  195. tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_IN);
  196. if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATED) {
  197. // XXX: blah need right layer_hint..
  198. if (deliver_negotiated)
  199. circpad_handle_padding_negotiated(client_side, cell,
  200. TO_ORIGIN_CIRCUIT(client_side)
  201. ->cpath->next);
  202. } else if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
  203. circpad_handle_padding_negotiate(client_side, cell);
  204. } else {
  205. // No need to pretend a padding cell was sent: This event is
  206. // now emitted internally when the circuitpadding code sends them.
  207. //circpad_cell_event_padding_sent(relay_side);
  208. // Receive padding cell at client
  209. circpad_deliver_recognized_relay_cell_events(client_side,
  210. cell->payload[0],
  211. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  212. }
  213. n_relay_cells++;
  214. }
  215. done:
  216. timers_advance_and_run(1);
  217. return 0;
  218. }
  219. // Test reading and writing padding to strings (or options_t + consensus)
  220. void
  221. test_circuitpadding_serialize(void *arg)
  222. {
  223. (void)arg;
  224. }
  225. static signed_error_t
  226. circpad_send_command_to_hop_mock(origin_circuit_t *circ, uint8_t hopnum,
  227. uint8_t relay_command, const uint8_t *payload,
  228. ssize_t payload_len)
  229. {
  230. (void) circ;
  231. (void) hopnum;
  232. (void) relay_command;
  233. (void) payload;
  234. (void) payload_len;
  235. return 0;
  236. }
  237. void
  238. test_circuitpadding_rtt(void *arg)
  239. {
  240. /* Test Plan:
  241. *
  242. * 1. Test RTT measurement server side
  243. * a. test usage of measured RTT
  244. * 2. Test termination of RTT measurement
  245. * a. test non-update of RTT
  246. * 3. Test client side circuit and non-application of RTT..
  247. */
  248. circpad_delay_t rtt_estimate;
  249. int64_t actual_mocked_monotime_start;
  250. (void)arg;
  251. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  252. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  253. dummy_channel.cmux = circuitmux_alloc();
  254. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  255. client_side = TO_CIRCUIT(origin_circuit_new());
  256. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  257. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  258. monotime_init();
  259. monotime_enable_test_mocking();
  260. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  261. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  262. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  263. curr_mocked_time = actual_mocked_monotime_start;
  264. timers_initialize();
  265. circpad_machines_init();
  266. helper_create_basic_machine();
  267. MOCK(circuit_package_relay_cell,
  268. circuit_package_relay_cell_mock);
  269. client_side->padding_machine[0] = &circ_client_machine;
  270. client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
  271. 0);
  272. relay_side->padding_machine[0] = &circ_client_machine;
  273. relay_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,0);
  274. /* Test 1: Test measuring RTT */
  275. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  276. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
  277. timers_advance_and_run(20);
  278. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  279. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  280. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 19000);
  281. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 30000);
  282. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  283. OP_EQ,
  284. relay_side->padding_info[0]->rtt_estimate_usec+
  285. circpad_machine_current_state(
  286. relay_side->padding_info[0])->histogram_edges[0]);
  287. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  288. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  289. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
  290. timers_advance_and_run(20);
  291. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  292. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  293. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  294. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 20000);
  295. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 21000);
  296. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  297. OP_EQ,
  298. relay_side->padding_info[0]->rtt_estimate_usec+
  299. circpad_machine_current_state(
  300. relay_side->padding_info[0])->histogram_edges[0]);
  301. /* Test 2: Termination of RTT measurement (from the previous test) */
  302. tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  303. rtt_estimate = relay_side->padding_info[0]->rtt_estimate_usec;
  304. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  305. timers_advance_and_run(4);
  306. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  307. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_EQ,
  308. rtt_estimate);
  309. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  310. tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  311. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  312. OP_EQ,
  313. relay_side->padding_info[0]->rtt_estimate_usec+
  314. circpad_machine_current_state(
  315. relay_side->padding_info[0])->histogram_edges[0]);
  316. /* Test 3: Make sure client side machine properly ignores RTT */
  317. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  318. tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  319. timers_advance_and_run(20);
  320. circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
  321. tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  322. tt_int_op(client_side->padding_info[0]->rtt_estimate_usec, OP_EQ, 0);
  323. tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
  324. OP_NE, client_side->padding_info[0]->rtt_estimate_usec);
  325. tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
  326. OP_EQ,
  327. circpad_machine_current_state(
  328. client_side->padding_info[0])->histogram_edges[0]);
  329. done:
  330. free_fake_orcirc(relay_side);
  331. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  332. circuitmux_free(dummy_channel.cmux);
  333. timers_shutdown();
  334. monotime_disable_test_mocking();
  335. UNMOCK(circuit_package_relay_cell);
  336. UNMOCK(circuitmux_attach_circuit);
  337. tor_free(circ_client_machine.states);
  338. return;
  339. }
  340. void
  341. helper_create_basic_machine(void)
  342. {
  343. /* Start, burst */
  344. circpad_machine_states_init(&circ_client_machine, 2);
  345. circ_client_machine.states[CIRCPAD_STATE_START].
  346. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  347. circ_client_machine.states[CIRCPAD_STATE_BURST].
  348. next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  349. circ_client_machine.states[CIRCPAD_STATE_BURST].
  350. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  351. circ_client_machine.states[CIRCPAD_STATE_BURST].
  352. next_state[CIRCPAD_EVENT_NONPADDING_SENT] = CIRCPAD_STATE_CANCEL;
  353. circ_client_machine.states[CIRCPAD_STATE_BURST].token_removal =
  354. CIRCPAD_TOKEN_REMOVAL_HIGHER;
  355. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_len = 5;
  356. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 500;
  357. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 2500;
  358. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 5000;
  359. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[3] = 10000;
  360. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[4] = 20000;
  361. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[0] = 1;
  362. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  363. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[2] = 2;
  364. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[3] = 2;
  365. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[4] = 2;
  366. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_total_tokens = 7;
  367. circ_client_machine.states[CIRCPAD_STATE_BURST].use_rtt_estimate = 1;
  368. return;
  369. }
  370. #define BIG_HISTOGRAM_LEN 10
  371. /** Setup a machine with a big histogram */
  372. static void
  373. helper_create_machine_with_big_histogram(circpad_removal_t removal_strategy)
  374. {
  375. const int tokens_per_bin = 2;
  376. /* Start, burst */
  377. circpad_machine_states_init(&circ_client_machine, 2);
  378. circpad_state_t *burst_state =
  379. &circ_client_machine.states[CIRCPAD_STATE_BURST];
  380. circ_client_machine.states[CIRCPAD_STATE_START].
  381. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  382. burst_state->next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  383. burst_state->next_state[CIRCPAD_EVENT_NONPADDING_RECV] =CIRCPAD_STATE_BURST;
  384. burst_state->next_state[CIRCPAD_EVENT_NONPADDING_SENT] =CIRCPAD_STATE_CANCEL;
  385. burst_state->token_removal = CIRCPAD_TOKEN_REMOVAL_HIGHER;
  386. burst_state->histogram_len = BIG_HISTOGRAM_LEN;
  387. int n_tokens = 0;
  388. int i;
  389. for (i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  390. burst_state->histogram[i] = tokens_per_bin;
  391. n_tokens += tokens_per_bin;
  392. }
  393. burst_state->histogram_edges[0] = 0;
  394. burst_state->histogram_edges[1] = 1;
  395. burst_state->histogram_edges[2] = 7;
  396. burst_state->histogram_edges[3] = 15;
  397. burst_state->histogram_edges[4] = 31;
  398. burst_state->histogram_edges[5] = 62;
  399. burst_state->histogram_edges[6] = 125;
  400. burst_state->histogram_edges[7] = 250;
  401. burst_state->histogram_edges[8] = 500;
  402. burst_state->histogram_edges[9] = 1000;
  403. burst_state->histogram_total_tokens = n_tokens;
  404. burst_state->length_dist.type = CIRCPAD_DIST_UNIFORM;
  405. burst_state->length_dist.param1 = n_tokens;
  406. burst_state->length_dist.param2 = n_tokens;
  407. burst_state->max_length = n_tokens;
  408. burst_state->length_includes_nonpadding = 1;
  409. burst_state->use_rtt_estimate = 0;
  410. burst_state->token_removal = removal_strategy;
  411. }
  412. static circpad_decision_t
  413. circpad_machine_schedule_padding_mock(circpad_machine_runtime_t *mi)
  414. {
  415. (void)mi;
  416. return 0;
  417. }
  418. static uint64_t
  419. mock_monotime_absolute_usec(void)
  420. {
  421. return 100;
  422. }
  423. /** Test higher token removal strategy by bin */
  424. static void
  425. test_circuitpadding_token_removal_higher(void *arg)
  426. {
  427. circpad_machine_runtime_t *mi;
  428. (void)arg;
  429. /* Mock it up */
  430. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  431. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  432. /* Setup test environment (time etc.) */
  433. client_side = (circuit_t *)origin_circuit_new();
  434. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  435. monotime_enable_test_mocking();
  436. /* Create test machine */
  437. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_HIGHER);
  438. client_side->padding_machine[0] = &circ_client_machine;
  439. client_side->padding_info[0] =
  440. circpad_circuit_machineinfo_new(client_side, 0);
  441. /* move the machine to the right state */
  442. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  443. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  444. CIRCPAD_STATE_BURST);
  445. /* Get the machine and setup tokens */
  446. mi = client_side->padding_info[0];
  447. tt_assert(mi);
  448. /*************************************************************************/
  449. uint64_t current_time = monotime_absolute_usec();
  450. /* Test left boundaries of each histogram bin: */
  451. const circpad_delay_t bin_left_bounds[] =
  452. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  453. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  454. tt_uint_op(bin_left_bounds[i], OP_EQ,
  455. circpad_histogram_bin_to_usec(mi, i));
  456. }
  457. /* Test right boundaries of each histogram bin: */
  458. const circpad_delay_t bin_right_bounds[] =
  459. {0, 6, 14, 30, 61, 124, 249, 499, 999, CIRCPAD_DELAY_INFINITE-1};
  460. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  461. tt_uint_op(bin_right_bounds[i], OP_EQ,
  462. histogram_get_bin_upper_bound(mi, i));
  463. }
  464. /* Check that all bins have two tokens right now */
  465. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  466. tt_int_op(mi->histogram[i], OP_EQ, 2);
  467. }
  468. /* This is the right order to remove tokens from this histogram. That is, we
  469. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  470. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  471. * the same reason, then from the 5th, etc. */
  472. const int bin_removal_order[] = {4, 5, 6, 7, 8};
  473. unsigned i;
  474. /* Remove all tokens from all bins apart from the infinity bin */
  475. for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
  476. int bin_to_remove = bin_removal_order[i];
  477. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  478. i, bin_to_remove);
  479. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  480. mi->padding_scheduled_at_usec = current_time - 57;
  481. circpad_machine_remove_token(mi);
  482. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  483. mi->padding_scheduled_at_usec = current_time - 57;
  484. circpad_machine_remove_token(mi);
  485. /* Test that we cleaned out this bin. Don't do this in the case of the last
  486. bin since the tokens will get refilled */
  487. if (i != BIG_HISTOGRAM_LEN - 2) {
  488. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  489. }
  490. }
  491. /* Check that all lower bins are not touched */
  492. for (i=0; i < 4 ; i++) {
  493. tt_int_op(mi->histogram[i], OP_EQ, 2);
  494. }
  495. /* Test below the lowest bin, for coverage */
  496. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  497. CIRCPAD_STATE_BURST);
  498. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  499. mi->padding_scheduled_at_usec = current_time;
  500. circpad_machine_remove_token(mi);
  501. tt_int_op(mi->histogram[0], OP_EQ, 1);
  502. done:
  503. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  504. monotime_disable_test_mocking();
  505. tor_free(circ_client_machine.states);
  506. }
  507. /** Test lower token removal strategy by bin */
  508. static void
  509. test_circuitpadding_token_removal_lower(void *arg)
  510. {
  511. circpad_machine_runtime_t *mi;
  512. (void)arg;
  513. /* Mock it up */
  514. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  515. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  516. /* Setup test environment (time etc.) */
  517. client_side = (circuit_t *)origin_circuit_new();
  518. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  519. monotime_enable_test_mocking();
  520. /* Create test machine */
  521. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_LOWER);
  522. client_side->padding_machine[0] = &circ_client_machine;
  523. client_side->padding_info[0] =
  524. circpad_circuit_machineinfo_new(client_side, 0);
  525. /* move the machine to the right state */
  526. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  527. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  528. CIRCPAD_STATE_BURST);
  529. /* Get the machine and setup tokens */
  530. mi = client_side->padding_info[0];
  531. tt_assert(mi);
  532. /*************************************************************************/
  533. uint64_t current_time = monotime_absolute_usec();
  534. /* Test left boundaries of each histogram bin: */
  535. const circpad_delay_t bin_left_bounds[] =
  536. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  537. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  538. tt_uint_op(bin_left_bounds[i], OP_EQ,
  539. circpad_histogram_bin_to_usec(mi, i));
  540. }
  541. /* Check that all bins have two tokens right now */
  542. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  543. tt_int_op(mi->histogram[i], OP_EQ, 2);
  544. }
  545. /* This is the right order to remove tokens from this histogram. That is, we
  546. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  547. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  548. * the same reason, then from the 5th, etc. */
  549. const int bin_removal_order[] = {4, 3, 2, 1, 0};
  550. unsigned i;
  551. /* Remove all tokens from all bins apart from the infinity bin */
  552. for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
  553. int bin_to_remove = bin_removal_order[i];
  554. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  555. i, bin_to_remove);
  556. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  557. mi->padding_scheduled_at_usec = current_time - 57;
  558. circpad_machine_remove_token(mi);
  559. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  560. mi->padding_scheduled_at_usec = current_time - 57;
  561. circpad_machine_remove_token(mi);
  562. /* Test that we cleaned out this bin. Don't do this in the case of the last
  563. bin since the tokens will get refilled */
  564. if (i != BIG_HISTOGRAM_LEN - 2) {
  565. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  566. }
  567. }
  568. /* Check that all higher bins are untouched */
  569. for (i = 5; i < BIG_HISTOGRAM_LEN ; i++) {
  570. tt_int_op(mi->histogram[i], OP_EQ, 2);
  571. }
  572. /* Test above the highest bin, for coverage */
  573. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  574. CIRCPAD_STATE_BURST);
  575. circ_client_machine.states[CIRCPAD_STATE_BURST].
  576. histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
  577. mi->padding_scheduled_at_usec = current_time - 29202;
  578. circpad_machine_remove_token(mi);
  579. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  580. done:
  581. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  582. monotime_disable_test_mocking();
  583. tor_free(circ_client_machine.states);
  584. }
  585. /** Test closest token removal strategy by bin */
  586. static void
  587. test_circuitpadding_closest_token_removal(void *arg)
  588. {
  589. circpad_machine_runtime_t *mi;
  590. (void)arg;
  591. /* Mock it up */
  592. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  593. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  594. /* Setup test environment (time etc.) */
  595. client_side = (circuit_t *)origin_circuit_new();
  596. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  597. monotime_enable_test_mocking();
  598. /* Create test machine */
  599. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST);
  600. client_side->padding_machine[0] = &circ_client_machine;
  601. client_side->padding_info[0] =
  602. circpad_circuit_machineinfo_new(client_side, 0);
  603. /* move the machine to the right state */
  604. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  605. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  606. CIRCPAD_STATE_BURST);
  607. /* Get the machine and setup tokens */
  608. mi = client_side->padding_info[0];
  609. tt_assert(mi);
  610. /*************************************************************************/
  611. uint64_t current_time = monotime_absolute_usec();
  612. /* Test left boundaries of each histogram bin: */
  613. const circpad_delay_t bin_left_bounds[] =
  614. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  615. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  616. tt_uint_op(bin_left_bounds[i], OP_EQ,
  617. circpad_histogram_bin_to_usec(mi, i));
  618. }
  619. /* Check that all bins have two tokens right now */
  620. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  621. tt_int_op(mi->histogram[i], OP_EQ, 2);
  622. }
  623. /* This is the right order to remove tokens from this histogram. That is, we
  624. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  625. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  626. * the same reason, then from the 5th, etc. */
  627. const int bin_removal_order[] = {4, 3, 5, 2, 6, 1, 7, 0, 8, 9};
  628. /* Remove all tokens from all bins apart from the infinity bin */
  629. for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
  630. int bin_to_remove = bin_removal_order[i];
  631. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  632. i, bin_to_remove);
  633. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  634. mi->padding_scheduled_at_usec = current_time - 57;
  635. circpad_machine_remove_token(mi);
  636. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  637. mi->padding_scheduled_at_usec = current_time - 57;
  638. circpad_machine_remove_token(mi);
  639. /* Test that we cleaned out this bin. Don't do this in the case of the last
  640. bin since the tokens will get refilled */
  641. if (i != BIG_HISTOGRAM_LEN - 2) {
  642. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  643. }
  644. }
  645. /* Check that all bins have been refilled */
  646. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  647. tt_int_op(mi->histogram[i], OP_EQ, 2);
  648. }
  649. /* Test below the lowest bin, for coverage */
  650. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  651. CIRCPAD_STATE_BURST);
  652. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  653. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
  654. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
  655. mi->padding_scheduled_at_usec = current_time - 102;
  656. mi->histogram[0] = 0;
  657. circpad_machine_remove_token(mi);
  658. tt_int_op(mi->histogram[1], OP_EQ, 1);
  659. /* Test above the highest bin, for coverage */
  660. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  661. CIRCPAD_STATE_BURST);
  662. mi->padding_scheduled_at_usec = current_time - 29202;
  663. circpad_machine_remove_token(mi);
  664. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  665. done:
  666. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  667. monotime_disable_test_mocking();
  668. tor_free(circ_client_machine.states);
  669. }
  670. /** Test closest token removal strategy with usec */
  671. static void
  672. test_circuitpadding_closest_token_removal_usec(void *arg)
  673. {
  674. circpad_machine_runtime_t *mi;
  675. (void)arg;
  676. /* Mock it up */
  677. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  678. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  679. /* Setup test environment (time etc.) */
  680. client_side = (circuit_t *)origin_circuit_new();
  681. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  682. monotime_enable_test_mocking();
  683. /* Create test machine */
  684. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC);
  685. client_side->padding_machine[0] = &circ_client_machine;
  686. client_side->padding_info[0] =
  687. circpad_circuit_machineinfo_new(client_side, 0);
  688. /* move the machine to the right state */
  689. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  690. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  691. CIRCPAD_STATE_BURST);
  692. /* Get the machine and setup tokens */
  693. mi = client_side->padding_info[0];
  694. tt_assert(mi);
  695. /*************************************************************************/
  696. uint64_t current_time = monotime_absolute_usec();
  697. /* Test left boundaries of each histogram bin: */
  698. const circpad_delay_t bin_left_bounds[] =
  699. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  700. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  701. tt_uint_op(bin_left_bounds[i], OP_EQ,
  702. circpad_histogram_bin_to_usec(mi, i));
  703. }
  704. /* XXX we want to test remove_token_exact and
  705. circpad_machine_remove_closest_token() with usec */
  706. /* Check that all bins have two tokens right now */
  707. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  708. tt_int_op(mi->histogram[i], OP_EQ, 2);
  709. }
  710. /* This is the right order to remove tokens from this histogram. That is, we
  711. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  712. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  713. * the same reason, then from the 5th, etc. */
  714. const int bin_removal_order[] = {4, 3, 5, 2, 1, 0, 6, 7, 8, 9};
  715. /* Remove all tokens from all bins apart from the infinity bin */
  716. for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
  717. int bin_to_remove = bin_removal_order[i];
  718. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  719. i, bin_to_remove);
  720. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  721. mi->padding_scheduled_at_usec = current_time - 57;
  722. circpad_machine_remove_token(mi);
  723. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  724. mi->padding_scheduled_at_usec = current_time - 57;
  725. circpad_machine_remove_token(mi);
  726. /* Test that we cleaned out this bin. Don't do this in the case of the last
  727. bin since the tokens will get refilled */
  728. if (i != BIG_HISTOGRAM_LEN - 2) {
  729. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  730. }
  731. }
  732. /* Check that all bins have been refilled */
  733. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  734. tt_int_op(mi->histogram[i], OP_EQ, 2);
  735. }
  736. /* Test below the lowest bin, for coverage */
  737. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  738. CIRCPAD_STATE_BURST);
  739. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  740. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
  741. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
  742. mi->padding_scheduled_at_usec = current_time - 102;
  743. mi->histogram[0] = 0;
  744. circpad_machine_remove_token(mi);
  745. tt_int_op(mi->histogram[1], OP_EQ, 1);
  746. /* Test above the highest bin, for coverage */
  747. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  748. CIRCPAD_STATE_BURST);
  749. circ_client_machine.states[CIRCPAD_STATE_BURST].
  750. histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
  751. mi->padding_scheduled_at_usec = current_time - 29202;
  752. circpad_machine_remove_token(mi);
  753. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  754. done:
  755. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  756. monotime_disable_test_mocking();
  757. tor_free(circ_client_machine.states);
  758. }
  759. /** Test closest token removal strategy with usec */
  760. static void
  761. test_circuitpadding_token_removal_exact(void *arg)
  762. {
  763. circpad_machine_runtime_t *mi;
  764. (void)arg;
  765. /* Mock it up */
  766. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  767. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  768. /* Setup test environment (time etc.) */
  769. client_side = (circuit_t *)origin_circuit_new();
  770. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  771. monotime_enable_test_mocking();
  772. /* Create test machine */
  773. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_EXACT);
  774. client_side->padding_machine[0] = &circ_client_machine;
  775. client_side->padding_info[0] =
  776. circpad_circuit_machineinfo_new(client_side, 0);
  777. /* move the machine to the right state */
  778. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  779. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  780. CIRCPAD_STATE_BURST);
  781. /* Get the machine and setup tokens */
  782. mi = client_side->padding_info[0];
  783. tt_assert(mi);
  784. /**********************************************************************/
  785. uint64_t current_time = monotime_absolute_usec();
  786. /* Ensure that we will clear out bin #4 with this usec */
  787. mi->padding_scheduled_at_usec = current_time - 57;
  788. tt_int_op(mi->histogram[4], OP_EQ, 2);
  789. circpad_machine_remove_token(mi);
  790. mi->padding_scheduled_at_usec = current_time - 57;
  791. tt_int_op(mi->histogram[4], OP_EQ, 1);
  792. circpad_machine_remove_token(mi);
  793. tt_int_op(mi->histogram[4], OP_EQ, 0);
  794. /* Ensure that we will not remove any other tokens even tho we try to, since
  795. * this is what the exact strategy dictates */
  796. mi->padding_scheduled_at_usec = current_time - 57;
  797. circpad_machine_remove_token(mi);
  798. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  799. if (i != 4) {
  800. tt_int_op(mi->histogram[i], OP_EQ, 2);
  801. }
  802. }
  803. done:
  804. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  805. monotime_disable_test_mocking();
  806. tor_free(circ_client_machine.states);
  807. }
  808. #undef BIG_HISTOGRAM_LEN
  809. void
  810. test_circuitpadding_tokens(void *arg)
  811. {
  812. const circpad_state_t *state;
  813. circpad_machine_runtime_t *mi;
  814. int64_t actual_mocked_monotime_start;
  815. (void)arg;
  816. /** Test plan:
  817. *
  818. * 1. Test symmetry between bin_to_usec and usec_to_bin
  819. * a. Test conversion
  820. * b. Test edge transitions (lower, upper)
  821. * 2. Test remove higher on an empty bin
  822. * a. Normal bin
  823. * b. Infinity bin
  824. * c. Bin 0
  825. * d. No higher
  826. * 3. Test remove lower
  827. * a. Normal bin
  828. * b. Bin 0
  829. * c. No lower
  830. * 4. Test remove closest
  831. * a. Closest lower
  832. * b. Closest higher
  833. * c. Closest 0
  834. * d. Closest Infinity
  835. */
  836. client_side = TO_CIRCUIT(origin_circuit_new());
  837. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  838. monotime_init();
  839. monotime_enable_test_mocking();
  840. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  841. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  842. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  843. curr_mocked_time = actual_mocked_monotime_start;
  844. /* This is needed so that we are not considered to be dormant */
  845. note_user_activity(20);
  846. timers_initialize();
  847. helper_create_basic_machine();
  848. client_side->padding_machine[0] = &circ_client_machine;
  849. client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
  850. 0);
  851. mi = client_side->padding_info[0];
  852. // Pretend a non-padding cell was sent
  853. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  854. circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
  855. /* We have to save the infinity bin because one inf delay
  856. * could have been chosen when we transition to burst */
  857. circpad_hist_token_t inf_bin = mi->histogram[4];
  858. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  859. CIRCPAD_STATE_BURST);
  860. state = circpad_machine_current_state(client_side->padding_info[0]);
  861. // Test 0: convert bin->usec->bin
  862. // Bin 0+1 have different semantics
  863. for (int bin = 0; bin < 2; bin++) {
  864. circpad_delay_t usec =
  865. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  866. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  867. usec);
  868. tt_int_op(bin, OP_EQ, bin2);
  869. }
  870. for (int bin = 2; bin < state->histogram_len-1; bin++) {
  871. circpad_delay_t usec =
  872. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  873. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  874. usec);
  875. tt_int_op(bin, OP_EQ, bin2);
  876. /* Verify we round down */
  877. bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  878. usec+3);
  879. tt_int_op(bin, OP_EQ, bin2);
  880. bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  881. usec-1);
  882. tt_int_op(bin, OP_EQ, bin2+1);
  883. }
  884. // Test 1: converting usec->bin->usec->bin
  885. // Bin 0+1 have different semantics.
  886. for (circpad_delay_t i = 0; i <= state->histogram_edges[0]; i++) {
  887. int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  888. i);
  889. circpad_delay_t usec =
  890. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  891. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  892. usec);
  893. tt_int_op(bin, OP_EQ, bin2);
  894. tt_int_op(i, OP_LE, usec);
  895. }
  896. for (circpad_delay_t i = state->histogram_edges[0]+1;
  897. i <= state->histogram_edges[0] +
  898. state->histogram_edges[state->histogram_len-2]; i++) {
  899. int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  900. i);
  901. circpad_delay_t usec =
  902. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  903. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  904. usec);
  905. tt_int_op(bin, OP_EQ, bin2);
  906. tt_int_op(i, OP_GE, usec);
  907. }
  908. /* 2.a. Normal higher bin */
  909. {
  910. tt_int_op(mi->histogram[2], OP_EQ, 2);
  911. tt_int_op(mi->histogram[3], OP_EQ, 2);
  912. circpad_machine_remove_higher_token(mi,
  913. circpad_histogram_bin_to_usec(mi, 2)+1);
  914. tt_int_op(mi->histogram[3], OP_EQ, 2);
  915. tt_int_op(mi->histogram[2], OP_EQ, 1);
  916. circpad_machine_remove_higher_token(mi,
  917. circpad_histogram_bin_to_usec(mi, 2)+1);
  918. tt_int_op(mi->histogram[2], OP_EQ, 0);
  919. tt_int_op(mi->histogram[3], OP_EQ, 2);
  920. circpad_machine_remove_higher_token(mi,
  921. circpad_histogram_bin_to_usec(mi, 2)+1);
  922. circpad_machine_remove_higher_token(mi,
  923. circpad_histogram_bin_to_usec(mi, 2)+1);
  924. tt_int_op(mi->histogram[3], OP_EQ, 0);
  925. circpad_machine_remove_higher_token(mi,
  926. circpad_histogram_bin_to_usec(mi, 2)+1);
  927. tt_int_op(mi->histogram[3], OP_EQ, 0);
  928. }
  929. /* 2.b. Higher Infinity bin */
  930. {
  931. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  932. circpad_machine_remove_higher_token(mi,
  933. circpad_histogram_bin_to_usec(mi, 2)+1);
  934. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  935. /* Test past the infinity bin */
  936. circpad_machine_remove_higher_token(mi,
  937. circpad_histogram_bin_to_usec(mi, 5)+1000000);
  938. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  939. }
  940. /* 2.c. Bin 0 */
  941. {
  942. tt_int_op(mi->histogram[0], OP_EQ, 0);
  943. mi->histogram[0] = 1;
  944. circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
  945. tt_int_op(mi->histogram[0], OP_EQ, 0);
  946. }
  947. /* Drain the infinity bin and cause a refill */
  948. while (inf_bin != 0) {
  949. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  950. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  951. inf_bin--;
  952. }
  953. circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
  954. // We should have refilled here.
  955. tt_int_op(mi->histogram[4], OP_EQ, 2);
  956. /* 3.a. Bin 0 */
  957. {
  958. tt_int_op(mi->histogram[0], OP_EQ, 1);
  959. circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
  960. tt_int_op(mi->histogram[0], OP_EQ, 0);
  961. }
  962. /* 3.b. Test remove lower normal bin */
  963. {
  964. tt_int_op(mi->histogram[3], OP_EQ, 2);
  965. circpad_machine_remove_lower_token(mi,
  966. circpad_histogram_bin_to_usec(mi, 3)+1);
  967. circpad_machine_remove_lower_token(mi,
  968. circpad_histogram_bin_to_usec(mi, 3)+1);
  969. tt_int_op(mi->histogram[3], OP_EQ, 0);
  970. tt_int_op(mi->histogram[2], OP_EQ, 2);
  971. circpad_machine_remove_lower_token(mi,
  972. circpad_histogram_bin_to_usec(mi, 3)+1);
  973. circpad_machine_remove_lower_token(mi,
  974. circpad_histogram_bin_to_usec(mi, 3)+1);
  975. /* 3.c. No lower */
  976. circpad_machine_remove_lower_token(mi,
  977. circpad_histogram_bin_to_usec(mi, 3)+1);
  978. tt_int_op(mi->histogram[2], OP_EQ, 0);
  979. }
  980. /* 4. Test remove closest
  981. * a. Closest lower
  982. * b. Closest higher
  983. * c. Closest 0
  984. * d. Closest Infinity
  985. */
  986. circpad_machine_setup_tokens(mi);
  987. tt_int_op(mi->histogram[2], OP_EQ, 2);
  988. circpad_machine_remove_closest_token(mi,
  989. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  990. circpad_machine_remove_closest_token(mi,
  991. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  992. tt_int_op(mi->histogram[2], OP_EQ, 0);
  993. tt_int_op(mi->histogram[3], OP_EQ, 2);
  994. circpad_machine_remove_closest_token(mi,
  995. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  996. circpad_machine_remove_closest_token(mi,
  997. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  998. tt_int_op(mi->histogram[3], OP_EQ, 0);
  999. tt_int_op(mi->histogram[0], OP_EQ, 1);
  1000. circpad_machine_remove_closest_token(mi,
  1001. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1002. tt_int_op(mi->histogram[0], OP_EQ, 0);
  1003. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1004. circpad_machine_remove_closest_token(mi,
  1005. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1006. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1007. /* 5. Test remove closest usec
  1008. * a. Closest 0
  1009. * b. Closest lower (below midpoint)
  1010. * c. Closest higher (above midpoint)
  1011. * d. Closest Infinity
  1012. */
  1013. circpad_machine_setup_tokens(mi);
  1014. tt_int_op(mi->histogram[0], OP_EQ, 1);
  1015. circpad_machine_remove_closest_token(mi,
  1016. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1017. tt_int_op(mi->histogram[0], OP_EQ, 0);
  1018. tt_int_op(mi->histogram[2], OP_EQ, 2);
  1019. circpad_machine_remove_closest_token(mi,
  1020. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1021. circpad_machine_remove_closest_token(mi,
  1022. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1023. tt_int_op(mi->histogram[2], OP_EQ, 0);
  1024. tt_int_op(mi->histogram[3], OP_EQ, 2);
  1025. circpad_machine_remove_closest_token(mi,
  1026. circpad_histogram_bin_to_usec(mi, 4), 1);
  1027. circpad_machine_remove_closest_token(mi,
  1028. circpad_histogram_bin_to_usec(mi, 4), 1);
  1029. tt_int_op(mi->histogram[3], OP_EQ, 0);
  1030. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1031. circpad_machine_remove_closest_token(mi,
  1032. circpad_histogram_bin_to_usec(mi, 4), 1);
  1033. circpad_machine_remove_closest_token(mi,
  1034. circpad_histogram_bin_to_usec(mi, 4), 1);
  1035. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1036. // XXX: Need more coverage of the actual usec branches
  1037. done:
  1038. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1039. monotime_disable_test_mocking();
  1040. tor_free(circ_client_machine.states);
  1041. }
  1042. void
  1043. test_circuitpadding_wronghop(void *arg)
  1044. {
  1045. /**
  1046. * Test plan:
  1047. * 1. Padding sent from hop 1 and 3 to client
  1048. * 2. Send negotiated from hop 1 and 3 to client
  1049. * 3. Garbled negotiated cell
  1050. * 4. Padding negotiate sent to client
  1051. * 5. Send negotiate stop command for unknown machine
  1052. * 6. Send negotiated to relay
  1053. * 7. Garbled padding negotiate cell
  1054. */
  1055. (void)arg;
  1056. uint32_t read_bw = 0, overhead_bw = 0;
  1057. cell_t cell;
  1058. signed_error_t ret;
  1059. origin_circuit_t *orig_client;
  1060. int64_t actual_mocked_monotime_start;
  1061. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1062. /* Mock this function so that our cell counting tests don't get confused by
  1063. * padding that gets sent by scheduled timers. */
  1064. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  1065. client_side = (circuit_t *)origin_circuit_new();
  1066. dummy_channel.cmux = circuitmux_alloc();
  1067. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  1068. &dummy_channel);
  1069. orig_client = TO_ORIGIN_CIRCUIT(client_side);
  1070. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1071. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1072. nodes_init();
  1073. monotime_init();
  1074. monotime_enable_test_mocking();
  1075. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1076. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1077. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1078. curr_mocked_time = actual_mocked_monotime_start;
  1079. timers_initialize();
  1080. circpad_machines_init();
  1081. MOCK(node_get_by_id,
  1082. node_get_by_id_mock);
  1083. MOCK(circuit_package_relay_cell,
  1084. circuit_package_relay_cell_mock);
  1085. /* Build three hops */
  1086. simulate_single_hop_extend(client_side, relay_side, 1);
  1087. simulate_single_hop_extend(client_side, relay_side, 1);
  1088. simulate_single_hop_extend(client_side, relay_side, 1);
  1089. /* verify padding was negotiated */
  1090. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1091. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1092. /* verify echo was sent */
  1093. tt_int_op(n_relay_cells, OP_EQ, 1);
  1094. tt_int_op(n_client_cells, OP_EQ, 1);
  1095. read_bw = orig_client->n_delivered_read_circ_bw;
  1096. overhead_bw = orig_client->n_overhead_read_circ_bw;
  1097. /* 1. Test padding from first and third hop */
  1098. circpad_deliver_recognized_relay_cell_events(client_side,
  1099. RELAY_COMMAND_DROP,
  1100. TO_ORIGIN_CIRCUIT(client_side)->cpath);
  1101. tt_int_op(read_bw, OP_EQ,
  1102. orig_client->n_delivered_read_circ_bw);
  1103. tt_int_op(overhead_bw, OP_EQ,
  1104. orig_client->n_overhead_read_circ_bw);
  1105. circpad_deliver_recognized_relay_cell_events(client_side,
  1106. RELAY_COMMAND_DROP,
  1107. TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
  1108. tt_int_op(read_bw, OP_EQ,
  1109. orig_client->n_delivered_read_circ_bw);
  1110. tt_int_op(overhead_bw, OP_EQ,
  1111. orig_client->n_overhead_read_circ_bw);
  1112. circpad_deliver_recognized_relay_cell_events(client_side,
  1113. RELAY_COMMAND_DROP,
  1114. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1115. tt_int_op(read_bw, OP_EQ,
  1116. orig_client->n_delivered_read_circ_bw);
  1117. tt_int_op(overhead_bw, OP_LT,
  1118. orig_client->n_overhead_read_circ_bw);
  1119. /* 2. Test padding negotiated not handled from hops 1,3 */
  1120. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1121. TO_ORIGIN_CIRCUIT(client_side)->cpath);
  1122. tt_int_op(ret, OP_EQ, -1);
  1123. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1124. TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
  1125. tt_int_op(ret, OP_EQ, -1);
  1126. /* 3. Garbled negotiated cell */
  1127. memset(&cell, 255, sizeof(cell));
  1128. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1129. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1130. tt_int_op(ret, OP_EQ, -1);
  1131. /* 4. Test that negotiate is dropped at origin */
  1132. read_bw = orig_client->n_delivered_read_circ_bw;
  1133. overhead_bw = orig_client->n_overhead_read_circ_bw;
  1134. relay_send_command_from_edge(0, relay_side,
  1135. RELAY_COMMAND_PADDING_NEGOTIATE,
  1136. (void*)cell.payload,
  1137. (size_t)3, NULL);
  1138. tt_int_op(read_bw, OP_EQ,
  1139. orig_client->n_delivered_read_circ_bw);
  1140. tt_int_op(overhead_bw, OP_EQ,
  1141. orig_client->n_overhead_read_circ_bw);
  1142. tt_int_op(n_relay_cells, OP_EQ, 2);
  1143. tt_int_op(n_client_cells, OP_EQ, 1);
  1144. /* 5. Test that asking to stop the wrong machine does nothing */
  1145. circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
  1146. 255, 2, CIRCPAD_COMMAND_STOP);
  1147. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  1148. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  1149. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1150. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1151. tt_int_op(n_relay_cells, OP_EQ, 3);
  1152. tt_int_op(n_client_cells, OP_EQ, 2);
  1153. /* 6. Sending negotiated command to relay does nothing */
  1154. ret = circpad_handle_padding_negotiated(relay_side, &cell, NULL);
  1155. tt_int_op(ret, OP_EQ, -1);
  1156. /* 7. Test garbled negotated cell (bad command 255) */
  1157. memset(&cell, 0, sizeof(cell));
  1158. ret = circpad_handle_padding_negotiate(relay_side, &cell);
  1159. tt_int_op(ret, OP_EQ, -1);
  1160. tt_int_op(n_client_cells, OP_EQ, 2);
  1161. /* Test 2: Test no padding */
  1162. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1163. free_fake_orcirc(relay_side);
  1164. client_side = (circuit_t *)origin_circuit_new();
  1165. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  1166. &dummy_channel);
  1167. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1168. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1169. simulate_single_hop_extend(client_side, relay_side, 1);
  1170. simulate_single_hop_extend(client_side, relay_side, 0);
  1171. /* verify no padding was negotiated */
  1172. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1173. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1174. tt_int_op(n_relay_cells, OP_EQ, 3);
  1175. tt_int_op(n_client_cells, OP_EQ, 2);
  1176. /* verify no echo was sent */
  1177. tt_int_op(n_relay_cells, OP_EQ, 3);
  1178. tt_int_op(n_client_cells, OP_EQ, 2);
  1179. /* Finish circuit */
  1180. simulate_single_hop_extend(client_side, relay_side, 1);
  1181. /* Spoof padding negotiated on circuit with no padding */
  1182. circpad_padding_negotiated(relay_side,
  1183. CIRCPAD_MACHINE_CIRC_SETUP,
  1184. CIRCPAD_COMMAND_START,
  1185. CIRCPAD_RESPONSE_OK);
  1186. /* verify no padding was negotiated */
  1187. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1188. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1189. circpad_padding_negotiated(relay_side,
  1190. CIRCPAD_MACHINE_CIRC_SETUP,
  1191. CIRCPAD_COMMAND_START,
  1192. CIRCPAD_RESPONSE_ERR);
  1193. /* verify no padding was negotiated */
  1194. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1195. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1196. done:
  1197. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1198. free_fake_orcirc(relay_side);
  1199. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1200. circuitmux_free(dummy_channel.cmux);
  1201. monotime_disable_test_mocking();
  1202. UNMOCK(node_get_by_id);
  1203. UNMOCK(circuit_package_relay_cell);
  1204. UNMOCK(circuitmux_attach_circuit);
  1205. nodes_free();
  1206. }
  1207. void
  1208. test_circuitpadding_negotiation(void *arg)
  1209. {
  1210. /**
  1211. * Test plan:
  1212. * 1. Test circuit where padding is supported by middle
  1213. * a. Make sure padding negotiation is sent
  1214. * b. Test padding negotiation delivery and parsing
  1215. * 2. Test circuit where padding is unsupported by middle
  1216. * a. Make sure padding negotiation is not sent
  1217. * 3. Test failure to negotiate a machine due to desync.
  1218. */
  1219. int64_t actual_mocked_monotime_start;
  1220. (void)arg;
  1221. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1222. client_side = TO_CIRCUIT(origin_circuit_new());
  1223. dummy_channel.cmux = circuitmux_alloc();
  1224. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1225. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1226. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1227. nodes_init();
  1228. monotime_init();
  1229. monotime_enable_test_mocking();
  1230. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1231. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1232. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1233. curr_mocked_time = actual_mocked_monotime_start;
  1234. timers_initialize();
  1235. circpad_machines_init();
  1236. MOCK(node_get_by_id,
  1237. node_get_by_id_mock);
  1238. MOCK(circuit_package_relay_cell,
  1239. circuit_package_relay_cell_mock);
  1240. /* Build two hops */
  1241. simulate_single_hop_extend(client_side, relay_side, 1);
  1242. simulate_single_hop_extend(client_side, relay_side, 1);
  1243. /* verify padding was negotiated */
  1244. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1245. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1246. /* verify echo was sent */
  1247. tt_int_op(n_relay_cells, OP_EQ, 1);
  1248. tt_int_op(n_client_cells, OP_EQ, 1);
  1249. /* Finish circuit */
  1250. simulate_single_hop_extend(client_side, relay_side, 1);
  1251. /* Test 2: Test no padding */
  1252. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1253. free_fake_orcirc(relay_side);
  1254. client_side = TO_CIRCUIT(origin_circuit_new());
  1255. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1256. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1257. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1258. simulate_single_hop_extend(client_side, relay_side, 1);
  1259. simulate_single_hop_extend(client_side, relay_side, 0);
  1260. /* verify no padding was negotiated */
  1261. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1262. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1263. tt_int_op(n_relay_cells, OP_EQ, 1);
  1264. tt_int_op(n_client_cells, OP_EQ, 1);
  1265. /* verify no echo was sent */
  1266. tt_int_op(n_relay_cells, OP_EQ, 1);
  1267. tt_int_op(n_client_cells, OP_EQ, 1);
  1268. /* Finish circuit */
  1269. simulate_single_hop_extend(client_side, relay_side, 1);
  1270. /* Force negotiate padding. */
  1271. circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
  1272. CIRCPAD_MACHINE_CIRC_SETUP,
  1273. 2, CIRCPAD_COMMAND_START);
  1274. /* verify no padding was negotiated */
  1275. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1276. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1277. /* verify no echo was sent */
  1278. tt_int_op(n_relay_cells, OP_EQ, 1);
  1279. tt_int_op(n_client_cells, OP_EQ, 1);
  1280. /* 3. Test failure to negotiate a machine due to desync */
  1281. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1282. free_fake_orcirc(relay_side);
  1283. client_side = TO_CIRCUIT(origin_circuit_new());
  1284. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1285. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1286. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1287. SMARTLIST_FOREACH(relay_padding_machines,
  1288. circpad_machine_spec_t *,
  1289. m, tor_free(m->states); tor_free(m));
  1290. smartlist_free(relay_padding_machines);
  1291. relay_padding_machines = smartlist_new();
  1292. simulate_single_hop_extend(client_side, relay_side, 1);
  1293. simulate_single_hop_extend(client_side, relay_side, 1);
  1294. /* verify echo was sent */
  1295. tt_int_op(n_client_cells, OP_EQ, 2);
  1296. tt_int_op(n_relay_cells, OP_EQ, 2);
  1297. /* verify no padding was negotiated */
  1298. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1299. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1300. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1301. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1302. done:
  1303. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1304. free_fake_orcirc(relay_side);
  1305. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1306. circuitmux_free(dummy_channel.cmux);
  1307. monotime_disable_test_mocking();
  1308. UNMOCK(node_get_by_id);
  1309. UNMOCK(circuit_package_relay_cell);
  1310. UNMOCK(circuitmux_attach_circuit);
  1311. nodes_free();
  1312. }
  1313. static void
  1314. simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
  1315. int padding)
  1316. {
  1317. char whatevs_key[CPATH_KEY_MATERIAL_LEN];
  1318. char digest[DIGEST_LEN];
  1319. tor_addr_t addr;
  1320. // Pretend a non-padding cell was sent
  1321. circpad_cell_event_nonpadding_sent((circuit_t*)client);
  1322. // Receive extend cell at middle
  1323. circpad_cell_event_nonpadding_received((circuit_t*)mid_relay);
  1324. // Advance time a tiny bit so we can calculate an RTT
  1325. curr_mocked_time += 10 * TOR_NSEC_PER_MSEC;
  1326. monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  1327. monotime_set_mock_time_nsec(curr_mocked_time);
  1328. // Receive extended cell at middle
  1329. circpad_cell_event_nonpadding_sent((circuit_t*)mid_relay);
  1330. // Receive extended cell at first hop
  1331. circpad_cell_event_nonpadding_received((circuit_t*)client);
  1332. // Add a hop to cpath
  1333. crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
  1334. cpath_extend_linked_list(&TO_ORIGIN_CIRCUIT(client)->cpath, hop);
  1335. hop->magic = CRYPT_PATH_MAGIC;
  1336. hop->state = CPATH_STATE_OPEN;
  1337. // add an extend info to indicate if this node supports padding or not.
  1338. // (set the first byte of the digest for our mocked node_get_by_id)
  1339. digest[0] = padding;
  1340. hop->extend_info = extend_info_new(
  1341. padding ? "padding" : "non-padding",
  1342. digest, NULL, NULL, NULL,
  1343. &addr, padding);
  1344. cpath_init_circuit_crypto(hop, whatevs_key, sizeof(whatevs_key), 0, 0);
  1345. hop->package_window = circuit_initial_package_window();
  1346. hop->deliver_window = CIRCWINDOW_START;
  1347. // Signal that the hop was added
  1348. circpad_machine_event_circ_added_hop(TO_ORIGIN_CIRCUIT(client));
  1349. }
  1350. static circpad_machine_spec_t *
  1351. helper_create_conditional_machine(void)
  1352. {
  1353. circpad_machine_spec_t *ret =
  1354. tor_malloc_zero(sizeof(circpad_machine_spec_t));
  1355. /* Start, burst */
  1356. circpad_machine_states_init(ret, 2);
  1357. ret->states[CIRCPAD_STATE_START].
  1358. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1359. ret->states[CIRCPAD_STATE_BURST].
  1360. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1361. ret->states[CIRCPAD_STATE_BURST].
  1362. next_state[CIRCPAD_EVENT_LENGTH_COUNT] = CIRCPAD_STATE_END;
  1363. /* Use EXACT removal strategy, otherwise setup_tokens() does not work */
  1364. ret->states[CIRCPAD_STATE_BURST].token_removal =
  1365. CIRCPAD_TOKEN_REMOVAL_EXACT;
  1366. ret->states[CIRCPAD_STATE_BURST].histogram_len = 3;
  1367. ret->states[CIRCPAD_STATE_BURST].histogram_edges[0] = 0;
  1368. ret->states[CIRCPAD_STATE_BURST].histogram_edges[1] = 1;
  1369. ret->states[CIRCPAD_STATE_BURST].histogram_edges[2] = 1000000;
  1370. ret->states[CIRCPAD_STATE_BURST].histogram[0] = 6;
  1371. ret->states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  1372. ret->states[CIRCPAD_STATE_BURST].histogram[2] = 0;
  1373. ret->states[CIRCPAD_STATE_BURST].histogram_total_tokens = 6;
  1374. ret->states[CIRCPAD_STATE_BURST].use_rtt_estimate = 0;
  1375. ret->states[CIRCPAD_STATE_BURST].length_includes_nonpadding = 1;
  1376. return ret;
  1377. }
  1378. static void
  1379. helper_create_conditional_machines(void)
  1380. {
  1381. circpad_machine_spec_t *add = helper_create_conditional_machine();
  1382. origin_padding_machines = smartlist_new();
  1383. relay_padding_machines = smartlist_new();
  1384. add->machine_num = 2;
  1385. add->is_origin_side = 1;
  1386. add->should_negotiate_end = 1;
  1387. add->target_hopnum = 2;
  1388. /* Let's have this one end after 4 packets */
  1389. add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1390. add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
  1391. add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
  1392. add->states[CIRCPAD_STATE_BURST].max_length = 4;
  1393. add->conditions.requires_vanguards = 0;
  1394. add->conditions.min_hops = 2;
  1395. add->conditions.state_mask = CIRCPAD_CIRC_BUILDING|
  1396. CIRCPAD_CIRC_NO_STREAMS|CIRCPAD_CIRC_HAS_RELAY_EARLY;
  1397. add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  1398. register_padding_machine(add, origin_padding_machines);
  1399. add = helper_create_conditional_machine();
  1400. add->machine_num = 3;
  1401. add->is_origin_side = 1;
  1402. add->should_negotiate_end = 1;
  1403. add->target_hopnum = 2;
  1404. /* Let's have this one end after 4 packets */
  1405. add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1406. add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
  1407. add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
  1408. add->states[CIRCPAD_STATE_BURST].max_length = 4;
  1409. add->conditions.requires_vanguards = 1;
  1410. add->conditions.min_hops = 3;
  1411. add->conditions.state_mask = CIRCPAD_CIRC_OPENED|
  1412. CIRCPAD_CIRC_STREAMS|CIRCPAD_CIRC_HAS_NO_RELAY_EARLY;
  1413. add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  1414. register_padding_machine(add, origin_padding_machines);
  1415. add = helper_create_conditional_machine();
  1416. add->machine_num = 2;
  1417. register_padding_machine(add, relay_padding_machines);
  1418. add = helper_create_conditional_machine();
  1419. add->machine_num = 3;
  1420. register_padding_machine(add, relay_padding_machines);
  1421. }
  1422. void
  1423. test_circuitpadding_conditions(void *arg)
  1424. {
  1425. /**
  1426. * Test plan:
  1427. * 0. Make a few origin and client machines with diff conditions
  1428. * * vanguards, purposes, has_opened circs, no relay early
  1429. * * Client side should_negotiate_end
  1430. * * Length limits
  1431. * 1. Test STATE_END transitions
  1432. * 2. Test new machine after end with same conditions
  1433. * 3. Test new machine due to changed conditions
  1434. * * Esp: built event, no relay early, no streams
  1435. * XXX: Diff test:
  1436. * 1. Test STATE_END with pending timers
  1437. * 2. Test marking a circuit before padding callback fires
  1438. * 3. Test freeing a circuit before padding callback fires
  1439. */
  1440. int64_t actual_mocked_monotime_start;
  1441. (void)arg;
  1442. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1443. nodes_init();
  1444. dummy_channel.cmux = circuitmux_alloc();
  1445. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  1446. &dummy_channel);
  1447. client_side = (circuit_t *)origin_circuit_new();
  1448. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1449. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1450. monotime_init();
  1451. monotime_enable_test_mocking();
  1452. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1453. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1454. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1455. curr_mocked_time = actual_mocked_monotime_start;
  1456. /* This is needed so that we are not considered to be dormant */
  1457. note_user_activity(20);
  1458. timers_initialize();
  1459. helper_create_conditional_machines();
  1460. MOCK(circuit_package_relay_cell,
  1461. circuit_package_relay_cell_mock);
  1462. MOCK(node_get_by_id,
  1463. node_get_by_id_mock);
  1464. /* Simulate extend. This should result in the original machine getting
  1465. * added, since the circuit is not built */
  1466. simulate_single_hop_extend(client_side, relay_side, 1);
  1467. simulate_single_hop_extend(client_side, relay_side, 1);
  1468. /* Verify that machine #2 is added */
  1469. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1470. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1471. /* Deliver a padding cell to the client, to trigger burst state */
  1472. circpad_cell_event_padding_sent(client_side);
  1473. /* This should have trigger length shutdown condition on client.. */
  1474. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1475. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1476. /* Verify machine is gone from both sides */
  1477. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1478. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1479. /* Send another event.. verify machine gets re-added properly
  1480. * (test race with shutdown) */
  1481. simulate_single_hop_extend(client_side, relay_side, 1);
  1482. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1483. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1484. TO_ORIGIN_CIRCUIT(client_side)->p_streams = 0;
  1485. circpad_machine_event_circ_has_no_streams(TO_ORIGIN_CIRCUIT(client_side));
  1486. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1487. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1488. /* Now make the circuit opened and send built event */
  1489. TO_ORIGIN_CIRCUIT(client_side)->has_opened = 1;
  1490. circpad_machine_event_circ_built(TO_ORIGIN_CIRCUIT(client_side));
  1491. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1492. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1493. TO_ORIGIN_CIRCUIT(client_side)->remaining_relay_early_cells = 0;
  1494. circpad_machine_event_circ_has_no_relay_early(
  1495. TO_ORIGIN_CIRCUIT(client_side));
  1496. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1497. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1498. get_options_mutable()->HSLayer2Nodes = (void*)1;
  1499. TO_ORIGIN_CIRCUIT(client_side)->p_streams = (void*)1;
  1500. circpad_machine_event_circ_has_streams(TO_ORIGIN_CIRCUIT(client_side));
  1501. /* Verify different machine is added */
  1502. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1503. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1504. /* Hold off on negotiated */
  1505. deliver_negotiated = 0;
  1506. /* Deliver a padding cell to the client, to trigger burst state */
  1507. circpad_cell_event_padding_sent(client_side);
  1508. /* This should have trigger length shutdown condition on client
  1509. * but not the response for the padding machine */
  1510. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1511. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  1512. /* Verify machine is gone from the relay (but negotiated not back yet */
  1513. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1514. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1515. /* Add another hop and verify it's back */
  1516. simulate_single_hop_extend(client_side, relay_side, 1);
  1517. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1518. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1519. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  1520. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1521. done:
  1522. /* XXX: Free everything */
  1523. return;
  1524. }
  1525. /** Disabled unstable test until #29298 is implemented (see #29122) */
  1526. #if 0
  1527. void
  1528. test_circuitpadding_circuitsetup_machine(void *arg)
  1529. {
  1530. int64_t actual_mocked_monotime_start;
  1531. /**
  1532. * Test case plan:
  1533. *
  1534. * 1. Simulate a normal circuit setup pattern
  1535. * a. Application traffic
  1536. *
  1537. * FIXME: This should focus more on exercising the machine
  1538. * features rather than actual traffic patterns. For example,
  1539. * test cancellation and bins empty/refill
  1540. */
  1541. (void)arg;
  1542. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1543. dummy_channel.cmux = circuitmux_alloc();
  1544. client_side = TO_CIRCUIT(origin_circuit_new());
  1545. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1546. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1547. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1548. nodes_init();
  1549. monotime_init();
  1550. monotime_enable_test_mocking();
  1551. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1552. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1553. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1554. curr_mocked_time = actual_mocked_monotime_start;
  1555. timers_initialize();
  1556. circpad_machines_init();
  1557. MOCK(circuit_package_relay_cell,
  1558. circuit_package_relay_cell_mock);
  1559. MOCK(node_get_by_id,
  1560. node_get_by_id_mock);
  1561. /* Test case #1: Build a 3 hop circuit, then wait and let pad */
  1562. simulate_single_hop_extend(client_side, relay_side, 1);
  1563. simulate_single_hop_extend(client_side, relay_side, 1);
  1564. simulate_single_hop_extend(client_side, relay_side, 1);
  1565. tt_int_op(n_client_cells, OP_EQ, 1);
  1566. tt_int_op(n_relay_cells, OP_EQ, 1);
  1567. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1568. CIRCPAD_STATE_BURST);
  1569. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1570. CIRCPAD_STATE_BURST);
  1571. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1572. OP_NE, 0);
  1573. tt_int_op(relay_side->padding_info[0]->is_padding_timer_scheduled,
  1574. OP_EQ, 0);
  1575. timers_advance_and_run(2000);
  1576. tt_int_op(n_client_cells, OP_EQ, 2);
  1577. tt_int_op(n_relay_cells, OP_EQ, 1);
  1578. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1579. CIRCPAD_STATE_GAP);
  1580. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1581. OP_EQ, 0);
  1582. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1583. OP_NE, 0);
  1584. timers_advance_and_run(5000);
  1585. tt_int_op(n_client_cells, OP_EQ, 2);
  1586. tt_int_op(n_relay_cells, OP_EQ, 2);
  1587. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1588. OP_NE, 0);
  1589. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1590. OP_EQ, 0);
  1591. timers_advance_and_run(2000);
  1592. tt_int_op(n_client_cells, OP_EQ, 3);
  1593. tt_int_op(n_relay_cells, OP_EQ, 2);
  1594. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1595. OP_EQ, 0);
  1596. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1597. OP_NE, 0);
  1598. timers_advance_and_run(5000);
  1599. tt_int_op(n_client_cells, OP_EQ, 3);
  1600. tt_int_op(n_relay_cells, OP_EQ, 3);
  1601. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1602. OP_NE, 0);
  1603. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1604. OP_EQ, 0);
  1605. timers_advance_and_run(2000);
  1606. tt_int_op(n_client_cells, OP_EQ, 4);
  1607. tt_int_op(n_relay_cells, OP_EQ, 3);
  1608. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1609. OP_EQ, 0);
  1610. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1611. OP_NE, 0);
  1612. timers_advance_and_run(5000);
  1613. tt_int_op(n_client_cells, OP_EQ, 4);
  1614. tt_int_op(n_relay_cells, OP_EQ, 4);
  1615. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1616. OP_NE, 0);
  1617. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1618. OP_EQ, 0);
  1619. timers_advance_and_run(2000);
  1620. tt_int_op(n_client_cells, OP_EQ, 5);
  1621. tt_int_op(n_relay_cells, OP_EQ, 4);
  1622. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1623. OP_EQ, 0);
  1624. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1625. OP_NE, 0);
  1626. timers_advance_and_run(5000);
  1627. tt_int_op(n_client_cells, OP_EQ, 5);
  1628. tt_int_op(n_relay_cells, OP_EQ, 5);
  1629. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1630. OP_NE, 0);
  1631. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1632. OP_EQ, 0);
  1633. timers_advance_and_run(2000);
  1634. tt_int_op(n_client_cells, OP_EQ, 6);
  1635. tt_int_op(n_relay_cells, OP_EQ, 5);
  1636. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1637. OP_EQ, 0);
  1638. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1639. OP_NE, 0);
  1640. timers_advance_and_run(5000);
  1641. tt_int_op(n_client_cells, OP_EQ, 6);
  1642. tt_int_op(n_relay_cells, OP_EQ, 6);
  1643. tt_int_op(client_side->padding_info[0]->current_state,
  1644. OP_EQ, CIRCPAD_STATE_END);
  1645. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1646. OP_EQ, 0);
  1647. tt_int_op(relay_side->padding_info[0]->current_state,
  1648. OP_EQ, CIRCPAD_STATE_GAP);
  1649. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1650. OP_EQ, 0);
  1651. /* Verify we can't schedule padding in END state */
  1652. circpad_decision_t ret =
  1653. circpad_machine_schedule_padding(client_side->padding_info[0]);
  1654. tt_int_op(ret, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1655. /* Simulate application traffic */
  1656. circpad_cell_event_nonpadding_sent(client_side);
  1657. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
  1658. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
  1659. circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
  1660. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1661. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1662. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1663. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1664. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1665. tt_int_op(n_client_cells, OP_EQ, 6);
  1666. tt_int_op(n_relay_cells, OP_EQ, 7);
  1667. // Test timer cancellation
  1668. simulate_single_hop_extend(client_side, relay_side, 1);
  1669. simulate_single_hop_extend(client_side, relay_side, 1);
  1670. timers_advance_and_run(5000);
  1671. circpad_cell_event_padding_received(client_side);
  1672. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1673. CIRCPAD_STATE_BURST);
  1674. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1675. CIRCPAD_STATE_GAP);
  1676. tt_int_op(n_client_cells, OP_EQ, 8);
  1677. tt_int_op(n_relay_cells, OP_EQ, 8);
  1678. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1679. OP_NE, 0);
  1680. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1681. OP_NE, 0);
  1682. /* Test timer cancel due to state rules */
  1683. circpad_cell_event_nonpadding_sent(client_side);
  1684. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1685. OP_EQ, 0);
  1686. circpad_cell_event_padding_received(client_side);
  1687. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1688. OP_NE, 0);
  1689. /* Simulate application traffic to cancel timer */
  1690. circpad_cell_event_nonpadding_sent(client_side);
  1691. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
  1692. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
  1693. circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
  1694. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1695. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1696. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1697. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1698. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1699. /* No cells sent, except negotiate end from relay */
  1700. tt_int_op(n_client_cells, OP_EQ, 8);
  1701. tt_int_op(n_relay_cells, OP_EQ, 9);
  1702. /* Test mark for close and free */
  1703. simulate_single_hop_extend(client_side, relay_side, 1);
  1704. simulate_single_hop_extend(client_side, relay_side, 1);
  1705. timers_advance_and_run(5000);
  1706. circpad_cell_event_padding_received(client_side);
  1707. tt_int_op(n_client_cells, OP_EQ, 10);
  1708. tt_int_op(n_relay_cells, OP_EQ, 10);
  1709. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1710. CIRCPAD_STATE_BURST);
  1711. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1712. CIRCPAD_STATE_GAP);
  1713. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1714. OP_NE, 0);
  1715. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1716. OP_NE, 0);
  1717. circuit_mark_for_close(client_side, END_CIRC_REASON_FLAG_REMOTE);
  1718. free_fake_orcirc(relay_side);
  1719. timers_advance_and_run(5000);
  1720. /* No cells sent */
  1721. tt_int_op(n_client_cells, OP_EQ, 10);
  1722. tt_int_op(n_relay_cells, OP_EQ, 10);
  1723. done:
  1724. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1725. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1726. circuitmux_free(dummy_channel.cmux);
  1727. timers_shutdown();
  1728. monotime_disable_test_mocking();
  1729. UNMOCK(circuit_package_relay_cell);
  1730. UNMOCK(circuitmux_attach_circuit);
  1731. return;
  1732. }
  1733. #endif
  1734. /** Helper function: Initializes a padding machine where every state uses the
  1735. * uniform probability distribution. */
  1736. static void
  1737. helper_circpad_circ_distribution_machine_setup(int min, int max)
  1738. {
  1739. circpad_machine_states_init(&circ_client_machine, 7);
  1740. circpad_state_t *zero_st = &circ_client_machine.states[0];
  1741. zero_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 1;
  1742. zero_st->iat_dist.type = CIRCPAD_DIST_UNIFORM;
  1743. /* param2 is upper bound, param1 is lower */
  1744. zero_st->iat_dist.param1 = min;
  1745. zero_st->iat_dist.param2 = max;
  1746. zero_st->dist_added_shift_usec = min;
  1747. zero_st->dist_max_sample_usec = max;
  1748. circpad_state_t *first_st = &circ_client_machine.states[1];
  1749. first_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 2;
  1750. first_st->iat_dist.type = CIRCPAD_DIST_LOGISTIC;
  1751. /* param1 is Mu, param2 is sigma. */
  1752. first_st->iat_dist.param1 = 9;
  1753. first_st->iat_dist.param2 = 3;
  1754. first_st->dist_added_shift_usec = min;
  1755. first_st->dist_max_sample_usec = max;
  1756. circpad_state_t *second_st = &circ_client_machine.states[2];
  1757. second_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 3;
  1758. second_st->iat_dist.type = CIRCPAD_DIST_LOG_LOGISTIC;
  1759. /* param1 is Alpha, param2 is 1.0/Beta */
  1760. second_st->iat_dist.param1 = 1;
  1761. second_st->iat_dist.param2 = 0.5;
  1762. second_st->dist_added_shift_usec = min;
  1763. second_st->dist_max_sample_usec = max;
  1764. circpad_state_t *third_st = &circ_client_machine.states[3];
  1765. third_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 4;
  1766. third_st->iat_dist.type = CIRCPAD_DIST_GEOMETRIC;
  1767. /* param1 is 'p' (success probability) */
  1768. third_st->iat_dist.param1 = 0.2;
  1769. third_st->dist_added_shift_usec = min;
  1770. third_st->dist_max_sample_usec = max;
  1771. circpad_state_t *fourth_st = &circ_client_machine.states[4];
  1772. fourth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 5;
  1773. fourth_st->iat_dist.type = CIRCPAD_DIST_WEIBULL;
  1774. /* param1 is k, param2 is Lambda */
  1775. fourth_st->iat_dist.param1 = 1.5;
  1776. fourth_st->iat_dist.param2 = 1;
  1777. fourth_st->dist_added_shift_usec = min;
  1778. fourth_st->dist_max_sample_usec = max;
  1779. circpad_state_t *fifth_st = &circ_client_machine.states[5];
  1780. fifth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 6;
  1781. fifth_st->iat_dist.type = CIRCPAD_DIST_PARETO;
  1782. /* param1 is sigma, param2 is xi */
  1783. fifth_st->iat_dist.param1 = 1;
  1784. fifth_st->iat_dist.param2 = 5;
  1785. fifth_st->dist_added_shift_usec = min;
  1786. fifth_st->dist_max_sample_usec = max;
  1787. }
  1788. /** Simple test that the padding delays sampled from a uniform distribution
  1789. * actually faill within the uniform distribution range. */
  1790. static void
  1791. test_circuitpadding_sample_distribution(void *arg)
  1792. {
  1793. circpad_machine_runtime_t *mi;
  1794. int n_samples;
  1795. int n_states;
  1796. (void) arg;
  1797. /* mock this function so that we dont actually schedule any padding */
  1798. MOCK(circpad_machine_schedule_padding,
  1799. circpad_machine_schedule_padding_mock);
  1800. /* Initialize a machine with multiple probability distributions */
  1801. circpad_machines_init();
  1802. helper_circpad_circ_distribution_machine_setup(0, 10);
  1803. /* Initialize machine and circuits */
  1804. client_side = TO_CIRCUIT(origin_circuit_new());
  1805. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1806. client_side->padding_machine[0] = &circ_client_machine;
  1807. client_side->padding_info[0] =
  1808. circpad_circuit_machineinfo_new(client_side, 0);
  1809. mi = client_side->padding_info[0];
  1810. /* For every state, sample a bunch of values from the distribution and ensure
  1811. * they fall within range. */
  1812. for (n_states = 0 ; n_states < 6; n_states++) {
  1813. /* Make sure we in the right state */
  1814. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ, n_states);
  1815. for (n_samples = 0; n_samples < 100; n_samples++) {
  1816. circpad_delay_t delay = circpad_machine_sample_delay(mi);
  1817. tt_int_op(delay, OP_GE, 0);
  1818. tt_int_op(delay, OP_LE, 10);
  1819. }
  1820. /* send a non-padding cell to move to the next machine state */
  1821. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  1822. }
  1823. done:
  1824. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1825. UNMOCK(circpad_machine_schedule_padding);
  1826. }
  1827. static circpad_decision_t
  1828. circpad_machine_spec_transition_mock(circpad_machine_runtime_t *mi,
  1829. circpad_event_t event)
  1830. {
  1831. (void) mi;
  1832. (void) event;
  1833. return CIRCPAD_STATE_UNCHANGED;
  1834. }
  1835. /* Test per-machine padding rate limits */
  1836. static void
  1837. test_circuitpadding_machine_rate_limiting(void *arg)
  1838. {
  1839. (void) arg;
  1840. bool retval;
  1841. circpad_machine_runtime_t *mi;
  1842. int i;
  1843. /* Ignore machine transitions for the purposes of this function, we only
  1844. * really care about padding counts */
  1845. MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
  1846. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  1847. /* Setup machine and circuits */
  1848. client_side = TO_CIRCUIT(origin_circuit_new());
  1849. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1850. helper_create_basic_machine();
  1851. client_side->padding_machine[0] = &circ_client_machine;
  1852. client_side->padding_info[0] =
  1853. circpad_circuit_machineinfo_new(client_side, 0);
  1854. mi = client_side->padding_info[0];
  1855. /* Set up the machine info so that we can get through the basic functions */
  1856. mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
  1857. /* First we are going to test the per-machine rate limits */
  1858. circ_client_machine.max_padding_percent = 50;
  1859. circ_client_machine.allowed_padding_count = 100;
  1860. /* Check padding limit, should be fine since we haven't sent anything yet. */
  1861. retval = circpad_machine_reached_padding_limit(mi);
  1862. tt_int_op(retval, OP_EQ, 0);
  1863. /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
  1864. * the rate limit will not trigger */
  1865. for (i=0;i<99;i++) {
  1866. circpad_send_padding_cell_for_callback(mi);
  1867. }
  1868. retval = circpad_machine_reached_padding_limit(mi);
  1869. tt_int_op(retval, OP_EQ, 0);
  1870. /* Now send another padding cell to pass circpad_global_allowed_cells=100,
  1871. and see that the limit will trigger */
  1872. circpad_send_padding_cell_for_callback(mi);
  1873. retval = circpad_machine_reached_padding_limit(mi);
  1874. tt_int_op(retval, OP_EQ, 1);
  1875. retval = circpad_machine_schedule_padding(mi);
  1876. tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1877. /* Cover wrap */
  1878. for (;i<UINT16_MAX;i++) {
  1879. circpad_send_padding_cell_for_callback(mi);
  1880. }
  1881. tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/2+1);
  1882. tt_ptr_op(client_side->padding_info[0], OP_EQ, mi);
  1883. for (i=0;i<UINT16_MAX;i++) {
  1884. circpad_cell_event_nonpadding_sent(client_side);
  1885. }
  1886. tt_int_op(mi->nonpadding_sent, OP_EQ, UINT16_MAX/2);
  1887. tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/4+1);
  1888. done:
  1889. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1890. }
  1891. /* Test global padding rate limits */
  1892. static void
  1893. test_circuitpadding_global_rate_limiting(void *arg)
  1894. {
  1895. (void) arg;
  1896. bool retval;
  1897. circpad_machine_runtime_t *mi;
  1898. int i;
  1899. int64_t actual_mocked_monotime_start;
  1900. /* Ignore machine transitions for the purposes of this function, we only
  1901. * really care about padding counts */
  1902. MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
  1903. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1904. MOCK(circuit_package_relay_cell,
  1905. circuit_package_relay_cell_mock);
  1906. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  1907. monotime_init();
  1908. monotime_enable_test_mocking();
  1909. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1910. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1911. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1912. curr_mocked_time = actual_mocked_monotime_start;
  1913. timers_initialize();
  1914. client_side = (circuit_t *)origin_circuit_new();
  1915. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1916. dummy_channel.cmux = circuitmux_alloc();
  1917. /* Setup machine and circuits */
  1918. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel, &dummy_channel);
  1919. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1920. helper_create_basic_machine();
  1921. relay_side->padding_machine[0] = &circ_client_machine;
  1922. relay_side->padding_info[0] =
  1923. circpad_circuit_machineinfo_new(relay_side, 0);
  1924. mi = relay_side->padding_info[0];
  1925. /* Set up the machine info so that we can get through the basic functions */
  1926. mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
  1927. simulate_single_hop_extend(client_side, relay_side, 1);
  1928. simulate_single_hop_extend(client_side, relay_side, 1);
  1929. /* Now test the global limits by setting up the consensus */
  1930. networkstatus_t vote1;
  1931. vote1.net_params = smartlist_new();
  1932. smartlist_split_string(vote1.net_params,
  1933. "circpad_global_allowed_cells=100 circpad_global_max_padding_pct=50",
  1934. NULL, 0, 0);
  1935. /* Register global limits with the padding subsystem */
  1936. circpad_new_consensus_params(&vote1);
  1937. /* Check padding limit, should be fine since we haven't sent anything yet. */
  1938. retval = circpad_machine_reached_padding_limit(mi);
  1939. tt_int_op(retval, OP_EQ, 0);
  1940. /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
  1941. * the rate limit will not trigger */
  1942. for (i=0;i<99;i++) {
  1943. circpad_send_padding_cell_for_callback(mi);
  1944. }
  1945. retval = circpad_machine_reached_padding_limit(mi);
  1946. tt_int_op(retval, OP_EQ, 0);
  1947. /* Now send another padding cell to pass circpad_global_allowed_cells=100,
  1948. and see that the limit will trigger */
  1949. circpad_send_padding_cell_for_callback(mi);
  1950. retval = circpad_machine_reached_padding_limit(mi);
  1951. tt_int_op(retval, OP_EQ, 1);
  1952. retval = circpad_machine_schedule_padding(mi);
  1953. tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1954. /* Now send 92 non-padding cells to get near the
  1955. * circpad_global_max_padding_pct=50 limit; in particular with 96 non-padding
  1956. * cells, the padding traffic is still 51% of total traffic so limit should
  1957. * trigger */
  1958. for (i=0;i<92;i++) {
  1959. circpad_cell_event_nonpadding_sent(relay_side);
  1960. }
  1961. retval = circpad_machine_reached_padding_limit(mi);
  1962. tt_int_op(retval, OP_EQ, 1);
  1963. /* Send another non-padding cell to bring the padding traffic to 50% of total
  1964. * traffic and get past the limit */
  1965. circpad_cell_event_nonpadding_sent(relay_side);
  1966. retval = circpad_machine_reached_padding_limit(mi);
  1967. tt_int_op(retval, OP_EQ, 0);
  1968. done:
  1969. free_fake_orcirc(relay_side);
  1970. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1971. circuitmux_free(dummy_channel.cmux);
  1972. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  1973. smartlist_free(vote1.net_params);
  1974. }
  1975. #define TEST_CIRCUITPADDING(name, flags) \
  1976. { #name, test_##name, (flags), NULL, NULL }
  1977. struct testcase_t circuitpadding_tests[] = {
  1978. TEST_CIRCUITPADDING(circuitpadding_tokens, TT_FORK),
  1979. TEST_CIRCUITPADDING(circuitpadding_negotiation, TT_FORK),
  1980. TEST_CIRCUITPADDING(circuitpadding_wronghop, TT_FORK),
  1981. /** Disabled unstable test until #29298 is implemented (see #29122) */
  1982. // TEST_CIRCUITPADDING(circuitpadding_circuitsetup_machine, TT_FORK),
  1983. TEST_CIRCUITPADDING(circuitpadding_conditions, TT_FORK),
  1984. TEST_CIRCUITPADDING(circuitpadding_rtt, TT_FORK),
  1985. TEST_CIRCUITPADDING(circuitpadding_sample_distribution, TT_FORK),
  1986. TEST_CIRCUITPADDING(circuitpadding_machine_rate_limiting, TT_FORK),
  1987. TEST_CIRCUITPADDING(circuitpadding_global_rate_limiting, TT_FORK),
  1988. TEST_CIRCUITPADDING(circuitpadding_token_removal_lower, TT_FORK),
  1989. TEST_CIRCUITPADDING(circuitpadding_token_removal_higher, TT_FORK),
  1990. TEST_CIRCUITPADDING(circuitpadding_closest_token_removal, TT_FORK),
  1991. TEST_CIRCUITPADDING(circuitpadding_closest_token_removal_usec, TT_FORK),
  1992. TEST_CIRCUITPADDING(circuitpadding_token_removal_exact, TT_FORK),
  1993. END_OF_TESTCASES
  1994. };