test_circuitpadding.c 113 KB

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