test_circuitpadding.c 104 KB

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