test_util.c 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define COMPAT_PRIVATE
  7. #define COMPAT_TIME_PRIVATE
  8. #define CONTROL_PRIVATE
  9. #define UTIL_PRIVATE
  10. #define UTIL_MALLOC_PRIVATE
  11. #define SOCKET_PRIVATE
  12. #define PROCESS_WIN32_PRIVATE
  13. #include "lib/testsupport/testsupport.h"
  14. #include "core/or/or.h"
  15. #include "lib/buf/buffers.h"
  16. #include "app/config/config.h"
  17. #include "feature/control/control.h"
  18. #include "feature/client/transports.h"
  19. #include "lib/crypt_ops/crypto_format.h"
  20. #include "lib/crypt_ops/crypto_rand.h"
  21. #include "lib/defs/time.h"
  22. #include "test/test.h"
  23. #include "lib/memarea/memarea.h"
  24. #include "lib/process/waitpid.h"
  25. #include "lib/process/process_win32.h"
  26. #include "test/log_test_helpers.h"
  27. #include "lib/compress/compress.h"
  28. #include "lib/compress/compress_zstd.h"
  29. #include "lib/encoding/keyval.h"
  30. #include "lib/fdio/fdio.h"
  31. #include "lib/fs/winlib.h"
  32. #include "lib/process/env.h"
  33. #include "lib/process/pidfile.h"
  34. #include "lib/intmath/weakrng.h"
  35. #include "lib/thread/numcpus.h"
  36. #include "lib/math/fp.h"
  37. #include "lib/math/laplace.h"
  38. #include "lib/meminfo/meminfo.h"
  39. #include "lib/time/tvdiff.h"
  40. #include "lib/encoding/confline.h"
  41. #include "lib/net/socketpair.h"
  42. #ifdef HAVE_PWD_H
  43. #include <pwd.h>
  44. #endif
  45. #ifdef HAVE_SYS_UTIME_H
  46. #include <sys/utime.h>
  47. #endif
  48. #ifdef HAVE_UTIME_H
  49. #include <utime.h>
  50. #endif
  51. #ifdef HAVE_SYS_STAT_H
  52. #include <sys/stat.h>
  53. #endif
  54. #ifdef HAVE_FCNTL_H
  55. #include <fcntl.h>
  56. #endif
  57. #ifdef HAVE_UNISTD_H
  58. #include <unistd.h>
  59. #endif
  60. #ifdef _WIN32
  61. #include <tchar.h>
  62. #endif
  63. #include <math.h>
  64. #include <ctype.h>
  65. #include <float.h>
  66. #define INFINITY_DBL ((double)INFINITY)
  67. #define NAN_DBL ((double)NAN)
  68. /** Test the tor_isinf() wrapper */
  69. static void
  70. test_tor_isinf(void *arg)
  71. {
  72. (void) arg;
  73. tt_assert(tor_isinf(INFINITY_DBL));
  74. tt_assert(!tor_isinf(NAN_DBL));
  75. tt_assert(!tor_isinf(DBL_EPSILON));
  76. tt_assert(!tor_isinf(DBL_MAX));
  77. tt_assert(!tor_isinf(DBL_MIN));
  78. tt_assert(!tor_isinf(0.0));
  79. tt_assert(!tor_isinf(0.1));
  80. tt_assert(!tor_isinf(3));
  81. tt_assert(!tor_isinf(3.14));
  82. done:
  83. ;
  84. }
  85. /* XXXX this is a minimal wrapper to make the unit tests compile with the
  86. * changed tor_timegm interface. */
  87. static time_t
  88. tor_timegm_wrapper(const struct tm *tm)
  89. {
  90. time_t t;
  91. if (tor_timegm(tm, &t) < 0)
  92. return -1;
  93. return t;
  94. }
  95. #define tor_timegm tor_timegm_wrapper
  96. static void
  97. test_util_read_until_eof_impl(const char *fname, size_t file_len,
  98. size_t read_limit)
  99. {
  100. char *fifo_name = NULL;
  101. char *test_str = NULL;
  102. char *str = NULL;
  103. size_t sz = 9999999;
  104. int fd = -1;
  105. int r;
  106. fifo_name = tor_strdup(get_fname(fname));
  107. test_str = tor_malloc(file_len);
  108. crypto_rand(test_str, file_len);
  109. r = write_bytes_to_file(fifo_name, test_str, file_len, 1);
  110. tt_int_op(r, OP_EQ, 0);
  111. fd = open(fifo_name, O_RDONLY|O_BINARY);
  112. tt_int_op(fd, OP_GE, 0);
  113. str = read_file_to_str_until_eof(fd, read_limit, &sz);
  114. tt_ptr_op(str, OP_NE, NULL);
  115. if (read_limit < file_len)
  116. tt_int_op(sz, OP_EQ, read_limit);
  117. else
  118. tt_int_op(sz, OP_EQ, file_len);
  119. tt_mem_op(test_str, OP_EQ, str, sz);
  120. tt_int_op(str[sz], OP_EQ, '\0');
  121. done:
  122. unlink(fifo_name);
  123. tor_free(fifo_name);
  124. tor_free(test_str);
  125. tor_free(str);
  126. if (fd >= 0)
  127. close(fd);
  128. }
  129. static void
  130. test_util_read_file_eof_tiny_limit(void *arg)
  131. {
  132. (void)arg;
  133. // purposely set limit shorter than what we wrote to the FIFO to
  134. // test the maximum, and that it puts the NUL in the right spot
  135. test_util_read_until_eof_impl("tor_test_fifo_tiny", 5, 4);
  136. }
  137. static void
  138. test_util_read_file_eof_one_loop_a(void *arg)
  139. {
  140. (void)arg;
  141. test_util_read_until_eof_impl("tor_test_fifo_1ka", 1024, 1023);
  142. }
  143. static void
  144. test_util_read_file_eof_one_loop_b(void *arg)
  145. {
  146. (void)arg;
  147. test_util_read_until_eof_impl("tor_test_fifo_1kb", 1024, 1024);
  148. }
  149. static void
  150. test_util_read_file_eof_two_loops(void *arg)
  151. {
  152. (void)arg;
  153. // write more than 1024 bytes to the FIFO to test two passes through
  154. // the loop in the method; if the re-alloc size is changed this
  155. // should be updated as well.
  156. test_util_read_until_eof_impl("tor_test_fifo_2k", 2048, 10000);
  157. }
  158. static void
  159. test_util_read_file_eof_two_loops_b(void *arg)
  160. {
  161. (void)arg;
  162. test_util_read_until_eof_impl("tor_test_fifo_2kb", 2048, 2048);
  163. }
  164. static void
  165. test_util_read_file_eof_zero_bytes(void *arg)
  166. {
  167. (void)arg;
  168. // zero-byte fifo
  169. test_util_read_until_eof_impl("tor_test_fifo_empty", 0, 10000);
  170. }
  171. /* Test the basic expected behaviour for write_chunks_to_file.
  172. * NOTE: This will need to be updated if we ever change the tempfile location
  173. * or extension */
  174. static void
  175. test_util_write_chunks_to_file(void *arg)
  176. {
  177. char *fname = NULL;
  178. char *tempname = NULL;
  179. char *str = NULL;
  180. int r;
  181. struct stat st;
  182. /* These should be two different sizes to ensure the data is different
  183. * between the data file and the temp file's 'known string' */
  184. int temp_str_len = 1024;
  185. int data_str_len = 512;
  186. char *data_str = tor_malloc(data_str_len);
  187. char *temp_str = tor_malloc(temp_str_len);
  188. smartlist_t *chunks = smartlist_new();
  189. sized_chunk_t c = {data_str, data_str_len/2};
  190. sized_chunk_t c2 = {data_str + data_str_len/2, data_str_len/2};
  191. (void)arg;
  192. crypto_rand(temp_str, temp_str_len);
  193. crypto_rand(data_str, data_str_len);
  194. // Ensure it can write multiple chunks
  195. smartlist_add(chunks, &c);
  196. smartlist_add(chunks, &c2);
  197. /*
  198. * Check if it writes using a tempfile
  199. */
  200. fname = tor_strdup(get_fname("write_chunks_with_tempfile"));
  201. tor_asprintf(&tempname, "%s.tmp", fname);
  202. // write a known string to a file where the tempfile will be
  203. r = write_bytes_to_file(tempname, temp_str, temp_str_len, 1);
  204. tt_int_op(r, OP_EQ, 0);
  205. // call write_chunks_to_file
  206. r = write_chunks_to_file(fname, chunks, 1, 0);
  207. tt_int_op(r, OP_EQ, 0);
  208. // assert the file has been written (expected size)
  209. str = read_file_to_str(fname, RFTS_BIN, &st);
  210. tt_assert(str != NULL);
  211. tt_u64_op((uint64_t)st.st_size, OP_EQ, data_str_len);
  212. tt_mem_op(data_str, OP_EQ, str, data_str_len);
  213. tor_free(str);
  214. // assert that the tempfile is removed (should not leave artifacts)
  215. str = read_file_to_str(tempname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  216. tt_assert(str == NULL);
  217. // Remove old testfile for second test
  218. r = unlink(fname);
  219. tt_int_op(r, OP_EQ, 0);
  220. tor_free(fname);
  221. tor_free(tempname);
  222. /*
  223. * Check if it skips using a tempfile with flags
  224. */
  225. fname = tor_strdup(get_fname("write_chunks_with_no_tempfile"));
  226. tor_asprintf(&tempname, "%s.tmp", fname);
  227. // write a known string to a file where the tempfile will be
  228. r = write_bytes_to_file(tempname, temp_str, temp_str_len, 1);
  229. tt_int_op(r, OP_EQ, 0);
  230. // call write_chunks_to_file with no_tempfile = true
  231. r = write_chunks_to_file(fname, chunks, 1, 1);
  232. tt_int_op(r, OP_EQ, 0);
  233. // assert the file has been written (expected size)
  234. str = read_file_to_str(fname, RFTS_BIN, &st);
  235. tt_assert(str != NULL);
  236. tt_u64_op((uint64_t)st.st_size, OP_EQ, data_str_len);
  237. tt_mem_op(data_str, OP_EQ, str, data_str_len);
  238. tor_free(str);
  239. // assert the tempfile still contains the known string
  240. str = read_file_to_str(tempname, RFTS_BIN, &st);
  241. tt_assert(str != NULL);
  242. tt_u64_op((uint64_t)st.st_size, OP_EQ, temp_str_len);
  243. tt_mem_op(temp_str, OP_EQ, str, temp_str_len);
  244. done:
  245. unlink(fname);
  246. unlink(tempname);
  247. smartlist_free(chunks);
  248. tor_free(fname);
  249. tor_free(tempname);
  250. tor_free(str);
  251. tor_free(data_str);
  252. tor_free(temp_str);
  253. }
  254. #define _TFE(a, b, f) tt_int_op((a).f, OP_EQ, (b).f)
  255. /** test the minimum set of struct tm fields needed for a unique epoch value
  256. * this is also the set we use to test tor_timegm */
  257. #define TM_EQUAL(a, b) \
  258. TT_STMT_BEGIN \
  259. _TFE(a, b, tm_year); \
  260. _TFE(a, b, tm_mon ); \
  261. _TFE(a, b, tm_mday); \
  262. _TFE(a, b, tm_hour); \
  263. _TFE(a, b, tm_min ); \
  264. _TFE(a, b, tm_sec ); \
  265. TT_STMT_END
  266. static void
  267. test_util_time(void *arg)
  268. {
  269. struct timeval start, end;
  270. struct tm a_time, b_time;
  271. char timestr[128];
  272. time_t t_res;
  273. int i;
  274. struct timeval tv;
  275. /* Test tv_udiff and tv_mdiff */
  276. (void)arg;
  277. start.tv_sec = 5;
  278. start.tv_usec = 5000;
  279. end.tv_sec = 5;
  280. end.tv_usec = 5000;
  281. tt_int_op(0L,OP_EQ, tv_udiff(&start, &end));
  282. tt_int_op(0L,OP_EQ, tv_mdiff(&start, &end));
  283. tt_int_op(0L,OP_EQ, tv_udiff(&end, &start));
  284. tt_int_op(0L,OP_EQ, tv_mdiff(&end, &start));
  285. end.tv_usec = 7000;
  286. tt_int_op(2000L,OP_EQ, tv_udiff(&start, &end));
  287. tt_int_op(2L,OP_EQ, tv_mdiff(&start, &end));
  288. tt_int_op(-2000L,OP_EQ, tv_udiff(&end, &start));
  289. tt_int_op(-2L,OP_EQ, tv_mdiff(&end, &start));
  290. end.tv_sec = 6;
  291. tt_int_op(1002000L,OP_EQ, tv_udiff(&start, &end));
  292. tt_int_op(1002L,OP_EQ, tv_mdiff(&start, &end));
  293. tt_int_op(-1002000L,OP_EQ, tv_udiff(&end, &start));
  294. tt_int_op(-1002L,OP_EQ, tv_mdiff(&end, &start));
  295. end.tv_usec = 0;
  296. tt_int_op(995000L,OP_EQ, tv_udiff(&start, &end));
  297. tt_int_op(995L,OP_EQ, tv_mdiff(&start, &end));
  298. tt_int_op(-995000L,OP_EQ, tv_udiff(&end, &start));
  299. tt_int_op(-995L,OP_EQ, tv_mdiff(&end, &start));
  300. end.tv_sec = 4;
  301. tt_int_op(-1005000L,OP_EQ, tv_udiff(&start, &end));
  302. tt_int_op(-1005L,OP_EQ, tv_mdiff(&start, &end));
  303. tt_int_op(1005000L,OP_EQ, tv_udiff(&end, &start));
  304. tt_int_op(1005L,OP_EQ, tv_mdiff(&end, &start));
  305. /* Negative tv_sec values, these will break on platforms where tv_sec is
  306. * unsigned */
  307. end.tv_sec = -10;
  308. tt_int_op(-15005000L,OP_EQ, tv_udiff(&start, &end));
  309. tt_int_op(-15005L,OP_EQ, tv_mdiff(&start, &end));
  310. tt_int_op(15005000L,OP_EQ, tv_udiff(&end, &start));
  311. tt_int_op(15005L,OP_EQ, tv_mdiff(&end, &start));
  312. start.tv_sec = -100;
  313. tt_int_op(89995000L,OP_EQ, tv_udiff(&start, &end));
  314. tt_int_op(89995L,OP_EQ, tv_mdiff(&start, &end));
  315. tt_int_op(-89995000L,OP_EQ, tv_udiff(&end, &start));
  316. tt_int_op(-89995L,OP_EQ, tv_mdiff(&end, &start));
  317. /* Test that tv_usec values round away from zero when converted to msec */
  318. start.tv_sec = 0;
  319. start.tv_usec = 0;
  320. end.tv_sec = 10;
  321. end.tv_usec = 499;
  322. tt_int_op(10000499L, OP_EQ, tv_udiff(&start, &end));
  323. tt_int_op(10000L, OP_EQ, tv_mdiff(&start, &end));
  324. tt_int_op(-10000499L, OP_EQ, tv_udiff(&end, &start));
  325. tt_int_op(-10000L, OP_EQ, tv_mdiff(&end, &start));
  326. start.tv_sec = 0;
  327. start.tv_usec = 0;
  328. end.tv_sec = 10;
  329. end.tv_usec = 500;
  330. tt_int_op(10000500L, OP_EQ, tv_udiff(&start, &end));
  331. tt_int_op(10001L, OP_EQ, tv_mdiff(&start, &end));
  332. tt_int_op(-10000500L, OP_EQ, tv_udiff(&end, &start));
  333. tt_int_op(-10000L, OP_EQ, tv_mdiff(&end, &start));
  334. start.tv_sec = 0;
  335. start.tv_usec = 0;
  336. end.tv_sec = 10;
  337. end.tv_usec = 501;
  338. tt_int_op(10000501L, OP_EQ, tv_udiff(&start, &end));
  339. tt_int_op(10001L, OP_EQ, tv_mdiff(&start, &end));
  340. tt_int_op(-10000501L, OP_EQ, tv_udiff(&end, &start));
  341. tt_int_op(-10001L, OP_EQ, tv_mdiff(&end, &start));
  342. /* Overflow conditions */
  343. #ifdef _WIN32
  344. /* Would you believe that tv_sec is a long on windows? Of course you would.*/
  345. #define TV_SEC_MAX LONG_MAX
  346. #define TV_SEC_MIN LONG_MIN
  347. #else
  348. /* Some BSDs have struct timeval.tv_sec 64-bit, but time_t (and long) 32-bit
  349. * Which means TIME_MAX is not actually the maximum value of tv_sec.
  350. * But that's ok for the moment, because the code correctly performs 64-bit
  351. * calculations internally, then catches the overflow. */
  352. #define TV_SEC_MAX TIME_MAX
  353. #define TV_SEC_MIN TIME_MIN
  354. #endif /* defined(_WIN32) */
  355. /* Assume tv_usec is an unsigned integer until proven otherwise */
  356. #define TV_USEC_MAX UINT_MAX
  357. /* Overflows in the result type */
  358. /* All comparisons work */
  359. start.tv_sec = 0;
  360. start.tv_usec = 0;
  361. end.tv_sec = LONG_MAX/1000 - 2;
  362. end.tv_usec = 0;
  363. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  364. tt_int_op(end.tv_sec*1000L, OP_EQ, tv_mdiff(&start, &end));
  365. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  366. tt_int_op(-end.tv_sec*1000L, OP_EQ, tv_mdiff(&end, &start));
  367. start.tv_sec = 0;
  368. start.tv_usec = 0;
  369. end.tv_sec = LONG_MAX/1000000 - 1;
  370. end.tv_usec = 0;
  371. tt_int_op(end.tv_sec*1000000L, OP_EQ, tv_udiff(&start, &end));
  372. tt_int_op(end.tv_sec*1000L, OP_EQ, tv_mdiff(&start, &end));
  373. tt_int_op(-end.tv_sec*1000000L, OP_EQ, tv_udiff(&end, &start));
  374. tt_int_op(-end.tv_sec*1000L, OP_EQ, tv_mdiff(&end, &start));
  375. /* No comparisons work */
  376. start.tv_sec = 0;
  377. start.tv_usec = 0;
  378. end.tv_sec = LONG_MAX/1000 + 1;
  379. end.tv_usec = 0;
  380. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  381. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  382. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  383. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  384. start.tv_sec = 0;
  385. start.tv_usec = 0;
  386. end.tv_sec = LONG_MAX/1000000 + 1;
  387. end.tv_usec = 0;
  388. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  389. tt_int_op(end.tv_sec*1000L, OP_EQ, tv_mdiff(&start, &end));
  390. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  391. tt_int_op(-end.tv_sec*1000L, OP_EQ, tv_mdiff(&end, &start));
  392. start.tv_sec = 0;
  393. start.tv_usec = 0;
  394. end.tv_sec = LONG_MAX/1000;
  395. end.tv_usec = TOR_USEC_PER_SEC;
  396. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  397. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  398. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  399. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  400. start.tv_sec = 0;
  401. start.tv_usec = 0;
  402. end.tv_sec = LONG_MAX/1000000;
  403. end.tv_usec = TOR_USEC_PER_SEC;
  404. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  405. tt_int_op((end.tv_sec + 1)*1000L, OP_EQ, tv_mdiff(&start, &end));
  406. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  407. tt_int_op(-(end.tv_sec + 1)*1000L, OP_EQ, tv_mdiff(&end, &start));
  408. /* Overflows on comparison to zero */
  409. start.tv_sec = 0;
  410. start.tv_usec = 0;
  411. end.tv_sec = TV_SEC_MAX;
  412. end.tv_usec = 0;
  413. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  414. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  415. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  416. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  417. end.tv_sec = TV_SEC_MAX;
  418. end.tv_usec = TOR_USEC_PER_SEC;
  419. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  420. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  421. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  422. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  423. end.tv_sec = 0;
  424. end.tv_usec = TV_USEC_MAX;
  425. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  426. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  427. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  428. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  429. end.tv_sec = TV_SEC_MAX;
  430. end.tv_usec = TV_USEC_MAX;
  431. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  432. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  433. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  434. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  435. end.tv_sec = 0;
  436. end.tv_usec = 0;
  437. start.tv_sec = TV_SEC_MIN;
  438. start.tv_usec = 0;
  439. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  440. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  441. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  442. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  443. start.tv_sec = TV_SEC_MIN;
  444. start.tv_usec = TOR_USEC_PER_SEC;
  445. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  446. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  447. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  448. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  449. start.tv_sec = TV_SEC_MIN;
  450. start.tv_usec = TV_USEC_MAX;
  451. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  452. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  453. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  454. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  455. /* overflows on comparison to maxima / minima */
  456. start.tv_sec = TV_SEC_MIN;
  457. start.tv_usec = 0;
  458. end.tv_sec = TV_SEC_MAX;
  459. end.tv_usec = 0;
  460. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  461. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  462. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  463. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  464. end.tv_sec = TV_SEC_MAX;
  465. end.tv_usec = TOR_USEC_PER_SEC;
  466. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  467. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  468. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  469. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  470. end.tv_sec = TV_SEC_MAX;
  471. end.tv_usec = 0;
  472. start.tv_sec = TV_SEC_MIN;
  473. start.tv_usec = 0;
  474. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  475. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  476. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  477. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  478. start.tv_sec = TV_SEC_MIN;
  479. start.tv_usec = TOR_USEC_PER_SEC;
  480. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  481. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  482. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  483. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  484. /* overflows on comparison to maxima / minima with extra usec */
  485. start.tv_sec = TV_SEC_MIN;
  486. start.tv_usec = TOR_USEC_PER_SEC;
  487. end.tv_sec = TV_SEC_MAX;
  488. end.tv_usec = 0;
  489. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  490. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  491. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  492. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  493. end.tv_sec = TV_SEC_MAX;
  494. end.tv_usec = TOR_USEC_PER_SEC;
  495. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  496. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  497. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  498. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  499. end.tv_sec = TV_SEC_MAX;
  500. end.tv_usec = TOR_USEC_PER_SEC;
  501. start.tv_sec = TV_SEC_MIN;
  502. start.tv_usec = 0;
  503. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  504. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  505. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  506. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  507. start.tv_sec = TV_SEC_MIN;
  508. start.tv_usec = TOR_USEC_PER_SEC;
  509. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&start, &end));
  510. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&start, &end));
  511. tt_int_op(LONG_MAX, OP_EQ, tv_udiff(&end, &start));
  512. tt_int_op(LONG_MAX, OP_EQ, tv_mdiff(&end, &start));
  513. /* Test tor_timegm & tor_gmtime_r */
  514. /* The test values here are confirmed to be correct on a platform
  515. * with a working timegm & gmtime_r. */
  516. /* Start with known-zero a_time and b_time.
  517. * This avoids passing uninitialised values to TM_EQUAL in a_time.
  518. * Zeroing may not be needed for b_time, as long as tor_gmtime_r
  519. * never reads the existing values in the structure.
  520. * But we really don't want intermittently failing tests. */
  521. memset(&a_time, 0, sizeof(struct tm));
  522. memset(&b_time, 0, sizeof(struct tm));
  523. a_time.tm_year = 2003-1900;
  524. a_time.tm_mon = 7;
  525. a_time.tm_mday = 30;
  526. a_time.tm_hour = 6;
  527. a_time.tm_min = 14;
  528. a_time.tm_sec = 55;
  529. t_res = 1062224095UL;
  530. tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
  531. tor_gmtime_r(&t_res, &b_time);
  532. TM_EQUAL(a_time, b_time);
  533. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  534. t_res = 1093846495UL;
  535. tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
  536. tor_gmtime_r(&t_res, &b_time);
  537. TM_EQUAL(a_time, b_time);
  538. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  539. a_time.tm_mday = 10;
  540. t_res = 1076393695UL;
  541. tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
  542. tor_gmtime_r(&t_res, &b_time);
  543. TM_EQUAL(a_time, b_time);
  544. a_time.tm_mon = 0;
  545. t_res = 1073715295UL;
  546. tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
  547. tor_gmtime_r(&t_res, &b_time);
  548. TM_EQUAL(a_time, b_time);
  549. /* This value is in range with 32 bit and 64 bit time_t */
  550. a_time.tm_year = 2037-1900;
  551. t_res = 2115180895UL;
  552. tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
  553. tor_gmtime_r(&t_res, &b_time);
  554. TM_EQUAL(a_time, b_time);
  555. /* This value is out of range with 32 bit time_t, but in range for 64 bit
  556. * time_t */
  557. a_time.tm_year = 2039-1900;
  558. #if SIZEOF_TIME_T == 4
  559. setup_full_capture_of_logs(LOG_WARN);
  560. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  561. expect_single_log_msg_containing("Result does not fit in tor_timegm");
  562. teardown_capture_of_logs();
  563. #elif SIZEOF_TIME_T == 8
  564. t_res = 2178252895UL;
  565. tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
  566. tor_gmtime_r(&t_res, &b_time);
  567. TM_EQUAL(a_time, b_time);
  568. #endif /* SIZEOF_TIME_T == 4 || ... */
  569. /* Test tor_timegm out of range */
  570. /* The below tests will all cause a BUG message, so we capture, suppress,
  571. * and detect. */
  572. #define CAPTURE() do { \
  573. setup_full_capture_of_logs(LOG_WARN); \
  574. } while (0)
  575. #define CHECK_TIMEGM_WARNING(msg) do { \
  576. expect_single_log_msg_containing(msg); \
  577. teardown_capture_of_logs(); \
  578. } while (0)
  579. #define CHECK_TIMEGM_ARG_OUT_OF_RANGE(msg) \
  580. CHECK_TIMEGM_WARNING("Out-of-range argument to tor_timegm")
  581. /* year */
  582. /* Wrong year < 1970 */
  583. a_time.tm_year = 1969-1900;
  584. CAPTURE();
  585. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  586. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  587. a_time.tm_year = -1-1900;
  588. CAPTURE();
  589. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  590. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  591. #if SIZEOF_INT == 4 || SIZEOF_INT == 8
  592. a_time.tm_year = -1*(1 << 16);
  593. CAPTURE();
  594. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  595. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  596. /* one of the smallest tm_year values my 64 bit system supports:
  597. * t_res = -9223372036854775LL without clamping */
  598. a_time.tm_year = -292275055-1900;
  599. CAPTURE();
  600. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  601. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  602. a_time.tm_year = INT32_MIN;
  603. CAPTURE();
  604. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  605. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  606. #endif /* SIZEOF_INT == 4 || SIZEOF_INT == 8 */
  607. #if SIZEOF_INT == 8
  608. a_time.tm_year = -1*(1 << 48);
  609. CAPTURE();
  610. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  611. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  612. /* while unlikely, the system's gmtime(_r) could return
  613. * a "correct" retrospective gregorian negative year value,
  614. * which I'm pretty sure is:
  615. * -1*(2^63)/60/60/24*2000/730485 + 1970 = -292277022657
  616. * 730485 is the number of days in two millennia, including leap days */
  617. a_time.tm_year = -292277022657-1900;
  618. CAPTURE();
  619. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  620. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  621. a_time.tm_year = INT64_MIN;
  622. CAPTURE();
  623. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  624. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  625. #endif /* SIZEOF_INT == 8 */
  626. /* Wrong year >= INT32_MAX - 1900 */
  627. #if SIZEOF_INT == 4 || SIZEOF_INT == 8
  628. a_time.tm_year = INT32_MAX-1900;
  629. CAPTURE();
  630. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  631. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  632. a_time.tm_year = INT32_MAX;
  633. CAPTURE();
  634. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  635. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  636. #endif /* SIZEOF_INT == 4 || SIZEOF_INT == 8 */
  637. #if SIZEOF_INT == 8
  638. /* one of the largest tm_year values my 64 bit system supports */
  639. a_time.tm_year = 292278994-1900;
  640. CAPTURE();
  641. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  642. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  643. /* while unlikely, the system's gmtime(_r) could return
  644. * a "correct" proleptic gregorian year value,
  645. * which I'm pretty sure is:
  646. * (2^63-1)/60/60/24*2000/730485 + 1970 = 292277026596
  647. * 730485 is the number of days in two millennia, including leap days */
  648. a_time.tm_year = 292277026596-1900;
  649. CAPTURE();
  650. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  651. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  652. a_time.tm_year = INT64_MAX-1900;
  653. CAPTURE();
  654. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  655. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  656. a_time.tm_year = INT64_MAX;
  657. CAPTURE();
  658. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  659. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  660. #endif /* SIZEOF_INT == 8 */
  661. /* month */
  662. a_time.tm_year = 2007-1900; /* restore valid year */
  663. a_time.tm_mon = 12; /* Wrong month, it's 0-based */
  664. CAPTURE();
  665. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  666. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  667. a_time.tm_mon = -1; /* Wrong month */
  668. CAPTURE();
  669. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  670. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  671. /* day */
  672. a_time.tm_mon = 6; /* Try July */
  673. a_time.tm_mday = 32; /* Wrong day */
  674. CAPTURE();
  675. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  676. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  677. a_time.tm_mon = 5; /* Try June */
  678. a_time.tm_mday = 31; /* Wrong day */
  679. CAPTURE();
  680. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  681. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  682. a_time.tm_year = 2008-1900; /* Try a leap year */
  683. a_time.tm_mon = 1; /* in feb. */
  684. a_time.tm_mday = 30; /* Wrong day */
  685. CAPTURE();
  686. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  687. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  688. a_time.tm_year = 2011-1900; /* Try a non-leap year */
  689. a_time.tm_mon = 1; /* in feb. */
  690. a_time.tm_mday = 29; /* Wrong day */
  691. CAPTURE();
  692. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  693. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  694. a_time.tm_mday = 0; /* Wrong day, it's 1-based (to be different) */
  695. CAPTURE();
  696. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  697. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  698. /* hour */
  699. a_time.tm_mday = 3; /* restore valid month day */
  700. a_time.tm_hour = 24; /* Wrong hour, it's 0-based */
  701. CAPTURE();
  702. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  703. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  704. a_time.tm_hour = -1; /* Wrong hour */
  705. CAPTURE();
  706. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  707. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  708. /* minute */
  709. a_time.tm_hour = 22; /* restore valid hour */
  710. a_time.tm_min = 60; /* Wrong minute, it's 0-based */
  711. CAPTURE();
  712. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  713. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  714. a_time.tm_min = -1; /* Wrong minute */
  715. CAPTURE();
  716. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  717. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  718. /* second */
  719. a_time.tm_min = 37; /* restore valid minute */
  720. a_time.tm_sec = 61; /* Wrong second: 0-based with leap seconds */
  721. CAPTURE();
  722. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  723. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  724. a_time.tm_sec = -1; /* Wrong second */
  725. CAPTURE();
  726. tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
  727. CHECK_TIMEGM_ARG_OUT_OF_RANGE();
  728. /* Test tor_gmtime_r out of range */
  729. /* time_t < 0 yields a year clamped to 1 or 1970,
  730. * depending on whether the implementation of the system gmtime(_r)
  731. * sets struct tm (1) or not (1970) */
  732. t_res = -1;
  733. tor_gmtime_r(&t_res, &b_time);
  734. tt_assert(b_time.tm_year == (1970-1900) ||
  735. b_time.tm_year == (1969-1900));
  736. if (sizeof(time_t) == 4 || sizeof(time_t) == 8) {
  737. t_res = -1*(1 << 30);
  738. tor_gmtime_r(&t_res, &b_time);
  739. tt_assert(b_time.tm_year == (1970-1900) ||
  740. b_time.tm_year == (1935-1900));
  741. t_res = INT32_MIN;
  742. tor_gmtime_r(&t_res, &b_time);
  743. tt_assert(b_time.tm_year == (1970-1900) ||
  744. b_time.tm_year == (1901-1900));
  745. }
  746. #if SIZEOF_TIME_T == 8
  747. {
  748. /* one of the smallest tm_year values my 64 bit system supports:
  749. * b_time.tm_year == (-292275055LL-1900LL) without clamping */
  750. t_res = -9223372036854775LL;
  751. tor_gmtime_r(&t_res, &b_time);
  752. tt_assert(b_time.tm_year == (1970-1900) ||
  753. b_time.tm_year == (1-1900));
  754. /* while unlikely, the system's gmtime(_r) could return
  755. * a "correct" retrospective gregorian negative year value,
  756. * which I'm pretty sure is:
  757. * -1*(2^63)/60/60/24*2000/730485 + 1970 = -292277022657
  758. * 730485 is the number of days in two millennia, including leap days
  759. * (int64_t)b_time.tm_year == (-292277022657LL-1900LL) without clamping */
  760. t_res = INT64_MIN;
  761. CAPTURE();
  762. tor_gmtime_r(&t_res, &b_time);
  763. if (! (b_time.tm_year == (1970-1900) ||
  764. b_time.tm_year == (1-1900))) {
  765. tt_int_op(b_time.tm_year, OP_EQ, 1970-1900);
  766. }
  767. if (b_time.tm_year != 1970-1900) {
  768. CHECK_TIMEGM_WARNING("Rounding up to ");
  769. } else {
  770. teardown_capture_of_logs();
  771. }
  772. }
  773. {
  774. /* As above, but with localtime. */
  775. t_res = -9223372036854775LL;
  776. tor_localtime_r(&t_res, &b_time);
  777. tt_assert(b_time.tm_year == (1970-1900) ||
  778. b_time.tm_year == (1-1900));
  779. /* while unlikely, the system's gmtime(_r) could return
  780. * a "correct" retrospective gregorian negative year value,
  781. * which I'm pretty sure is:
  782. * -1*(2^63)/60/60/24*2000/730485 + 1970 = -292277022657
  783. * 730485 is the number of days in two millennia, including leap days
  784. * (int64_t)b_time.tm_year == (-292277022657LL-1900LL) without clamping */
  785. t_res = INT64_MIN;
  786. CAPTURE();
  787. tor_localtime_r(&t_res, &b_time);
  788. if (! (b_time.tm_year == (1970-1900) ||
  789. b_time.tm_year == (1-1900))) {
  790. tt_int_op(b_time.tm_year, OP_EQ, 1970-1900);
  791. }
  792. if (b_time.tm_year != 1970-1900) {
  793. CHECK_TIMEGM_WARNING("Rounding up to ");
  794. } else {
  795. teardown_capture_of_logs();
  796. }
  797. }
  798. #endif /* SIZEOF_TIME_T == 8 */
  799. /* time_t >= INT_MAX yields a year clamped to 2037 or 9999,
  800. * depending on whether the implementation of the system gmtime(_r)
  801. * sets struct tm (9999) or not (2037) */
  802. #if SIZEOF_TIME_T == 4 || SIZEOF_TIME_T == 8
  803. {
  804. t_res = 3*(1 << 29);
  805. tor_gmtime_r(&t_res, &b_time);
  806. tt_assert(b_time.tm_year == (2021-1900));
  807. t_res = INT32_MAX;
  808. tor_gmtime_r(&t_res, &b_time);
  809. tt_assert(b_time.tm_year == (2037-1900) ||
  810. b_time.tm_year == (2038-1900));
  811. }
  812. {
  813. /* as above but with localtime. */
  814. t_res = 3*(1 << 29);
  815. tor_localtime_r(&t_res, &b_time);
  816. tt_assert(b_time.tm_year == (2021-1900));
  817. t_res = INT32_MAX;
  818. tor_localtime_r(&t_res, &b_time);
  819. tt_assert(b_time.tm_year == (2037-1900) ||
  820. b_time.tm_year == (2038-1900));
  821. }
  822. #endif /* SIZEOF_TIME_T == 4 || SIZEOF_TIME_T == 8 */
  823. #if SIZEOF_TIME_T == 8
  824. {
  825. /* one of the largest tm_year values my 64 bit system supports:
  826. * b_time.tm_year == (292278994L-1900L) without clamping */
  827. t_res = 9223372036854775LL;
  828. tor_gmtime_r(&t_res, &b_time);
  829. tt_assert(b_time.tm_year == (2037-1900) ||
  830. b_time.tm_year == (9999-1900));
  831. /* while unlikely, the system's gmtime(_r) could return
  832. * a "correct" proleptic gregorian year value,
  833. * which I'm pretty sure is:
  834. * (2^63-1)/60/60/24*2000/730485 + 1970 = 292277026596
  835. * 730485 is the number of days in two millennia, including leap days
  836. * (int64_t)b_time.tm_year == (292277026596L-1900L) without clamping */
  837. t_res = INT64_MAX;
  838. CAPTURE();
  839. tor_gmtime_r(&t_res, &b_time);
  840. CHECK_TIMEGM_WARNING("Rounding down to ");
  841. tt_assert(b_time.tm_year == (2037-1900) ||
  842. b_time.tm_year == (9999-1900));
  843. }
  844. {
  845. /* As above but with localtime. */
  846. t_res = 9223372036854775LL;
  847. tor_localtime_r(&t_res, &b_time);
  848. tt_assert(b_time.tm_year == (2037-1900) ||
  849. b_time.tm_year == (9999-1900));
  850. /* while unlikely, the system's gmtime(_r) could return
  851. * a "correct" proleptic gregorian year value,
  852. * which I'm pretty sure is:
  853. * (2^63-1)/60/60/24*2000/730485 + 1970 = 292277026596
  854. * 730485 is the number of days in two millennia, including leap days
  855. * (int64_t)b_time.tm_year == (292277026596L-1900L) without clamping */
  856. t_res = INT64_MAX;
  857. CAPTURE();
  858. tor_localtime_r(&t_res, &b_time);
  859. CHECK_TIMEGM_WARNING("Rounding down to ");
  860. tt_assert(b_time.tm_year == (2037-1900) ||
  861. b_time.tm_year == (9999-1900));
  862. }
  863. #endif /* SIZEOF_TIME_T == 8 */
  864. /* Test {format,parse}_rfc1123_time */
  865. format_rfc1123_time(timestr, 0);
  866. tt_str_op("Thu, 01 Jan 1970 00:00:00 GMT",OP_EQ, timestr);
  867. format_rfc1123_time(timestr, (time_t)1091580502UL);
  868. tt_str_op("Wed, 04 Aug 2004 00:48:22 GMT",OP_EQ, timestr);
  869. t_res = 0;
  870. i = parse_rfc1123_time(timestr, &t_res);
  871. tt_int_op(0,OP_EQ, i);
  872. tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
  873. /* This value is in range with 32 bit and 64 bit time_t */
  874. format_rfc1123_time(timestr, (time_t)2080000000UL);
  875. tt_str_op("Fri, 30 Nov 2035 01:46:40 GMT",OP_EQ, timestr);
  876. t_res = 0;
  877. i = parse_rfc1123_time(timestr, &t_res);
  878. tt_int_op(0,OP_EQ, i);
  879. tt_int_op(t_res,OP_EQ, (time_t)2080000000UL);
  880. /* This value is out of range with 32 bit time_t, but in range for 64 bit
  881. * time_t */
  882. format_rfc1123_time(timestr, (time_t)2150000000UL);
  883. #if SIZEOF_TIME_T == 4
  884. #if 0
  885. /* Wrapping around will have made it this. */
  886. /* On windows, at least, this is clipped to 1 Jan 1970. ??? */
  887. tt_str_op("Sat, 11 Jan 1902 23:45:04 GMT",OP_EQ, timestr);
  888. #endif
  889. /* Make sure that the right date doesn't parse. */
  890. strlcpy(timestr, "Wed, 17 Feb 2038 06:13:20 GMT", sizeof(timestr));
  891. t_res = 0;
  892. CAPTURE();
  893. i = parse_rfc1123_time(timestr, &t_res);
  894. CHECK_TIMEGM_WARNING("does not fit in tor_timegm");
  895. tt_int_op(-1,OP_EQ, i);
  896. #elif SIZEOF_TIME_T == 8
  897. tt_str_op("Wed, 17 Feb 2038 06:13:20 GMT",OP_EQ, timestr);
  898. t_res = 0;
  899. i = parse_rfc1123_time(timestr, &t_res);
  900. tt_int_op(0,OP_EQ, i);
  901. tt_int_op(t_res,OP_EQ, (time_t)2150000000UL);
  902. #endif /* SIZEOF_TIME_T == 4 || ... */
  903. /* The timezone doesn't matter */
  904. t_res = 0;
  905. tt_int_op(0,OP_EQ,
  906. parse_rfc1123_time("Wed, 04 Aug 2004 00:48:22 ZUL", &t_res));
  907. tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
  908. tt_int_op(-1,OP_EQ,
  909. parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  910. tt_int_op(-1,OP_EQ,
  911. parse_rfc1123_time("Wed, 32 Mar 2011 00:00:00 GMT", &t_res));
  912. tt_int_op(-1,OP_EQ,
  913. parse_rfc1123_time("Wed, 30 Mar 2011 24:00:00 GMT", &t_res));
  914. tt_int_op(-1,OP_EQ,
  915. parse_rfc1123_time("Wed, 30 Mar 2011 23:60:00 GMT", &t_res));
  916. tt_int_op(-1,OP_EQ,
  917. parse_rfc1123_time("Wed, 30 Mar 2011 23:59:62 GMT", &t_res));
  918. tt_int_op(-1,OP_EQ,
  919. parse_rfc1123_time("Wed, 30 Mar 1969 23:59:59 GMT", &t_res));
  920. tt_int_op(-1,OP_EQ,
  921. parse_rfc1123_time("Wed, 30 Ene 2011 23:59:59 GMT", &t_res));
  922. tt_int_op(-1,OP_EQ,
  923. parse_rfc1123_time("Wed, 30 Mar 2011 23:59:59 GM", &t_res));
  924. tt_int_op(-1,OP_EQ,
  925. parse_rfc1123_time("Wed, 30 Mar 1900 23:59:59 GMT", &t_res));
  926. /* Leap year. */
  927. tt_int_op(-1,OP_EQ,
  928. parse_rfc1123_time("Wed, 29 Feb 2011 16:00:00 GMT", &t_res));
  929. tt_int_op(0,OP_EQ,
  930. parse_rfc1123_time("Wed, 29 Feb 2012 16:00:00 GMT", &t_res));
  931. /* Leap second plus one */
  932. tt_int_op(-1,OP_EQ,
  933. parse_rfc1123_time("Wed, 30 Mar 2011 23:59:61 GMT", &t_res));
  934. /* Test parse_iso_time */
  935. t_res = 0;
  936. i = parse_iso_time("", &t_res);
  937. tt_int_op(-1,OP_EQ, i);
  938. t_res = 0;
  939. i = parse_iso_time("2004-08-32 00:48:22", &t_res);
  940. tt_int_op(-1,OP_EQ, i);
  941. t_res = 0;
  942. i = parse_iso_time("1969-08-03 00:48:22", &t_res);
  943. tt_int_op(-1,OP_EQ, i);
  944. t_res = 0;
  945. i = parse_iso_time("2004-08-04 00:48:22", &t_res);
  946. tt_int_op(0,OP_EQ, i);
  947. tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
  948. t_res = 0;
  949. i = parse_iso_time("2004-8-4 0:48:22", &t_res);
  950. tt_int_op(0,OP_EQ, i);
  951. tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
  952. /* This value is in range with 32 bit and 64 bit time_t */
  953. t_res = 0;
  954. i = parse_iso_time("2035-11-30 01:46:40", &t_res);
  955. tt_int_op(0,OP_EQ, i);
  956. tt_int_op(t_res,OP_EQ, (time_t)2080000000UL);
  957. /* This value is out of range with 32 bit time_t, but in range for 64 bit
  958. * time_t */
  959. t_res = 0;
  960. #if SIZEOF_TIME_T == 4
  961. CAPTURE();
  962. i = parse_iso_time("2038-02-17 06:13:20", &t_res);
  963. tt_int_op(-1,OP_EQ, i);
  964. CHECK_TIMEGM_WARNING("does not fit in tor_timegm");
  965. #elif SIZEOF_TIME_T == 8
  966. i = parse_iso_time("2038-02-17 06:13:20", &t_res);
  967. tt_int_op(0,OP_EQ, i);
  968. tt_int_op(t_res,OP_EQ, (time_t)2150000000UL);
  969. #endif /* SIZEOF_TIME_T == 4 || ... */
  970. tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-zz 99-99x99", &t_res));
  971. tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-32 00:00:00", &t_res));
  972. tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 24:00:00", &t_res));
  973. tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 23:60:00", &t_res));
  974. tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 23:59:62", &t_res));
  975. tt_int_op(-1,OP_EQ, parse_iso_time("1969-03-30 23:59:59", &t_res));
  976. tt_int_op(-1,OP_EQ, parse_iso_time("2011-00-30 23:59:59", &t_res));
  977. tt_int_op(-1,OP_EQ, parse_iso_time("2147483647-08-29 14:00:00", &t_res));
  978. tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 23:59", &t_res));
  979. tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-04 00:48:22.100", &t_res));
  980. tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-04 00:48:22XYZ", &t_res));
  981. /* but... that _is_ acceptable if we aren't being strict. */
  982. t_res = 0;
  983. i = parse_iso_time_("2004-08-04 00:48:22XYZ", &t_res, 0, 0);
  984. tt_int_op(0,OP_EQ, i);
  985. tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
  986. /* try nospace variant. */
  987. t_res = 0;
  988. i = parse_iso_time_nospace("2004-08-04T00:48:22", &t_res);
  989. tt_int_op(0,OP_EQ, i);
  990. tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
  991. tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-04T00:48:22", &t_res));
  992. tt_int_op(-1,OP_EQ, parse_iso_time_nospace("2004-08-04 00:48:22", &t_res));
  993. tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-04x00:48:22", &t_res));
  994. tt_int_op(-1,OP_EQ, parse_iso_time_nospace("2004-08-04x00:48:22", &t_res));
  995. /* Test tor_gettimeofday */
  996. end.tv_sec = 4;
  997. end.tv_usec = 999990;
  998. start.tv_sec = 1;
  999. start.tv_usec = 500;
  1000. tor_gettimeofday(&start);
  1001. /* now make sure time works. */
  1002. tor_gettimeofday(&end);
  1003. /* We might've timewarped a little. */
  1004. tt_int_op(tv_udiff(&start, &end), OP_GE, -5000);
  1005. /* Test format_iso_time */
  1006. tv.tv_sec = (time_t)1326296338UL;
  1007. tv.tv_usec = 3060;
  1008. format_iso_time(timestr, (time_t)tv.tv_sec);
  1009. tt_str_op("2012-01-11 15:38:58",OP_EQ, timestr);
  1010. /* The output of format_local_iso_time will vary by timezone, and setting
  1011. our timezone for testing purposes would be a nontrivial flaky pain.
  1012. Skip this test for now.
  1013. format_local_iso_time(timestr, tv.tv_sec);
  1014. test_streq("2012-01-11 10:38:58", timestr);
  1015. */
  1016. format_iso_time_nospace(timestr, (time_t)tv.tv_sec);
  1017. tt_str_op("2012-01-11T15:38:58",OP_EQ, timestr);
  1018. tt_int_op(strlen(timestr),OP_EQ, ISO_TIME_LEN);
  1019. format_iso_time_nospace_usec(timestr, &tv);
  1020. tt_str_op("2012-01-11T15:38:58.003060",OP_EQ, timestr);
  1021. tt_int_op(strlen(timestr),OP_EQ, ISO_TIME_USEC_LEN);
  1022. tv.tv_usec = 0;
  1023. /* This value is in range with 32 bit and 64 bit time_t */
  1024. tv.tv_sec = (time_t)2080000000UL;
  1025. format_iso_time(timestr, (time_t)tv.tv_sec);
  1026. tt_str_op("2035-11-30 01:46:40",OP_EQ, timestr);
  1027. /* This value is out of range with 32 bit time_t, but in range for 64 bit
  1028. * time_t */
  1029. tv.tv_sec = (time_t)2150000000UL;
  1030. format_iso_time(timestr, (time_t)tv.tv_sec);
  1031. #if SIZEOF_TIME_T == 4
  1032. /* format_iso_time should indicate failure on overflow, but it doesn't yet.
  1033. * Hopefully #18480 will improve the failure semantics in this case.
  1034. tt_str_op("2038-02-17 06:13:20",OP_EQ, timestr);
  1035. */
  1036. #elif SIZEOF_TIME_T == 8
  1037. #ifndef _WIN32
  1038. /* This SHOULD work on windows too; see bug #18665 */
  1039. tt_str_op("2038-02-17 06:13:20",OP_EQ, timestr);
  1040. #endif
  1041. #endif /* SIZEOF_TIME_T == 4 || ... */
  1042. #undef CAPTURE
  1043. #undef CHECK_TIMEGM_ARG_OUT_OF_RANGE
  1044. done:
  1045. teardown_capture_of_logs();
  1046. }
  1047. static void
  1048. test_util_parse_http_time(void *arg)
  1049. {
  1050. struct tm a_time;
  1051. char b[ISO_TIME_LEN+1];
  1052. (void)arg;
  1053. #define T(s) do { \
  1054. format_iso_time(b, tor_timegm(&a_time)); \
  1055. tt_str_op(b, OP_EQ, (s)); \
  1056. b[0]='\0'; \
  1057. } while (0)
  1058. /* Test parse_http_time */
  1059. tt_int_op(-1,OP_EQ,
  1060. parse_http_time("", &a_time));
  1061. tt_int_op(-1,OP_EQ,
  1062. parse_http_time("Sunday, 32 Aug 2004 00:48:22 GMT", &a_time));
  1063. tt_int_op(-1,OP_EQ,
  1064. parse_http_time("Sunday, 3 Aug 1869 00:48:22 GMT", &a_time));
  1065. tt_int_op(-1,OP_EQ,
  1066. parse_http_time("Sunday, 32-Aug-94 00:48:22 GMT", &a_time));
  1067. tt_int_op(-1,OP_EQ,
  1068. parse_http_time("Sunday, 3-Ago-04 00:48:22", &a_time));
  1069. tt_int_op(-1,OP_EQ,
  1070. parse_http_time("Sunday, August the third", &a_time));
  1071. tt_int_op(-1,OP_EQ,
  1072. parse_http_time("Wednesday,,04 Aug 1994 00:48:22 GMT", &a_time));
  1073. tt_int_op(0,OP_EQ,
  1074. parse_http_time("Wednesday, 04 Aug 1994 00:48:22 GMT", &a_time));
  1075. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1076. T("1994-08-04 00:48:22");
  1077. tt_int_op(0,OP_EQ,
  1078. parse_http_time("Wednesday, 4 Aug 1994 0:48:22 GMT", &a_time));
  1079. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1080. T("1994-08-04 00:48:22");
  1081. tt_int_op(0,OP_EQ,
  1082. parse_http_time("Miercoles, 4 Aug 1994 0:48:22 GMT", &a_time));
  1083. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1084. T("1994-08-04 00:48:22");
  1085. tt_int_op(0,OP_EQ,
  1086. parse_http_time("Wednesday, 04-Aug-94 00:48:22 GMT", &a_time));
  1087. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1088. T("1994-08-04 00:48:22");
  1089. tt_int_op(0,OP_EQ,
  1090. parse_http_time("Wednesday, 4-Aug-94 0:48:22 GMT", &a_time));
  1091. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1092. T("1994-08-04 00:48:22");
  1093. tt_int_op(0,OP_EQ,
  1094. parse_http_time("Miercoles, 4-Aug-94 0:48:22 GMT", &a_time));
  1095. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1096. T("1994-08-04 00:48:22");
  1097. tt_int_op(0,OP_EQ, parse_http_time("Wed Aug 04 00:48:22 1994", &a_time));
  1098. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1099. T("1994-08-04 00:48:22");
  1100. tt_int_op(0,OP_EQ, parse_http_time("Wed Aug 4 0:48:22 1994", &a_time));
  1101. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1102. T("1994-08-04 00:48:22");
  1103. tt_int_op(0,OP_EQ, parse_http_time("Mie Aug 4 0:48:22 1994", &a_time));
  1104. tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
  1105. T("1994-08-04 00:48:22");
  1106. tt_int_op(0,OP_EQ,parse_http_time("Sun, 1 Jan 2012 00:00:00 GMT", &a_time));
  1107. tt_int_op((time_t)1325376000UL,OP_EQ, tor_timegm(&a_time));
  1108. T("2012-01-01 00:00:00");
  1109. tt_int_op(0,OP_EQ,parse_http_time("Mon, 31 Dec 2012 00:00:00 GMT", &a_time));
  1110. tt_int_op((time_t)1356912000UL,OP_EQ, tor_timegm(&a_time));
  1111. T("2012-12-31 00:00:00");
  1112. /* This value is in range with 32 bit and 64 bit time_t */
  1113. tt_int_op(0,OP_EQ,parse_http_time("Fri, 30 Nov 2035 01:46:40 GMT", &a_time));
  1114. tt_int_op((time_t)2080000000UL,OP_EQ, tor_timegm(&a_time));
  1115. T("2035-11-30 01:46:40");
  1116. /* This value is out of range with 32 bit time_t, but in range for 64 bit
  1117. * time_t */
  1118. #if SIZEOF_TIME_T == 4
  1119. /* parse_http_time should indicate failure on overflow, but it doesn't yet.
  1120. * Hopefully #18480 will improve the failure semantics in this case. */
  1121. setup_full_capture_of_logs(LOG_WARN);
  1122. tt_int_op(0,OP_EQ,parse_http_time("Wed, 17 Feb 2038 06:13:20 GMT", &a_time));
  1123. tt_int_op((time_t)-1,OP_EQ, tor_timegm(&a_time));
  1124. expect_single_log_msg_containing("does not fit in tor_timegm");
  1125. teardown_capture_of_logs();
  1126. #elif SIZEOF_TIME_T == 8
  1127. tt_int_op(0,OP_EQ,parse_http_time("Wed, 17 Feb 2038 06:13:20 GMT", &a_time));
  1128. tt_int_op((time_t)2150000000UL,OP_EQ, tor_timegm(&a_time));
  1129. T("2038-02-17 06:13:20");
  1130. #endif /* SIZEOF_TIME_T == 4 || ... */
  1131. tt_int_op(-1,OP_EQ, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
  1132. tt_int_op(-1,OP_EQ, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
  1133. tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
  1134. tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 23:60:00 GMT", &a_time));
  1135. tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 23:59:62 GMT", &a_time));
  1136. tt_int_op(-1,OP_EQ, parse_http_time("1969-03-30 23:59:59 GMT", &a_time));
  1137. tt_int_op(-1,OP_EQ, parse_http_time("2011-00-30 23:59:59 GMT", &a_time));
  1138. tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 23:59", &a_time));
  1139. #undef T
  1140. done:
  1141. teardown_capture_of_logs();
  1142. }
  1143. static void
  1144. test_util_config_line(void *arg)
  1145. {
  1146. char buf[1024];
  1147. char *k=NULL, *v=NULL;
  1148. const char *str;
  1149. /* Test parse_config_line_from_str */
  1150. (void)arg;
  1151. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  1152. "k2\n"
  1153. "k3 \n" "\n" " \n" "#comment\n"
  1154. "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
  1155. "kseven \"a quoted 'string\"\n"
  1156. "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
  1157. "k9 a line that\\\n spans two lines.\n\n"
  1158. "k10 more than\\\n one contin\\\nuation\n"
  1159. "k11 \\\ncontinuation at the start\n"
  1160. "k12 line with a\\\n#comment\n embedded\n"
  1161. "k13\\\ncontinuation at the very start\n"
  1162. "k14 a line that has a comment and # ends with a slash \\\n"
  1163. "k15 this should be the next new line\n"
  1164. "k16 a line that has a comment and # ends without a slash \n"
  1165. "k17 this should be the next new line\n"
  1166. , sizeof(buf));
  1167. str = buf;
  1168. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1169. tt_str_op(k,OP_EQ, "k");
  1170. tt_str_op(v,OP_EQ, "v");
  1171. tor_free(k); tor_free(v);
  1172. tt_assert(!strcmpstart(str, "key value with"));
  1173. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1174. tt_str_op(k,OP_EQ, "key");
  1175. tt_str_op(v,OP_EQ, "value with spaces");
  1176. tor_free(k); tor_free(v);
  1177. tt_assert(!strcmpstart(str, "keykey"));
  1178. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1179. tt_str_op(k,OP_EQ, "keykey");
  1180. tt_str_op(v,OP_EQ, "val");
  1181. tor_free(k); tor_free(v);
  1182. tt_assert(!strcmpstart(str, "k2\n"));
  1183. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1184. tt_str_op(k,OP_EQ, "k2");
  1185. tt_str_op(v,OP_EQ, "");
  1186. tor_free(k); tor_free(v);
  1187. tt_assert(!strcmpstart(str, "k3 \n"));
  1188. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1189. tt_str_op(k,OP_EQ, "k3");
  1190. tt_str_op(v,OP_EQ, "");
  1191. tor_free(k); tor_free(v);
  1192. tt_assert(!strcmpstart(str, "#comment"));
  1193. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1194. tt_str_op(k,OP_EQ, "k4");
  1195. tt_str_op(v,OP_EQ, "");
  1196. tor_free(k); tor_free(v);
  1197. tt_assert(!strcmpstart(str, "k5#abc"));
  1198. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1199. tt_str_op(k,OP_EQ, "k5");
  1200. tt_str_op(v,OP_EQ, "");
  1201. tor_free(k); tor_free(v);
  1202. tt_assert(!strcmpstart(str, "k6"));
  1203. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1204. tt_str_op(k,OP_EQ, "k6");
  1205. tt_str_op(v,OP_EQ, "val");
  1206. tor_free(k); tor_free(v);
  1207. tt_assert(!strcmpstart(str, "kseven"));
  1208. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1209. tt_str_op(k,OP_EQ, "kseven");
  1210. tt_str_op(v,OP_EQ, "a quoted \'string");
  1211. tor_free(k); tor_free(v);
  1212. tt_assert(!strcmpstart(str, "k8 "));
  1213. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1214. tt_str_op(k,OP_EQ, "k8");
  1215. tt_str_op(v,OP_EQ, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
  1216. tor_free(k); tor_free(v);
  1217. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1218. tt_str_op(k,OP_EQ, "k9");
  1219. tt_str_op(v,OP_EQ, "a line that spans two lines.");
  1220. tor_free(k); tor_free(v);
  1221. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1222. tt_str_op(k,OP_EQ, "k10");
  1223. tt_str_op(v,OP_EQ, "more than one continuation");
  1224. tor_free(k); tor_free(v);
  1225. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1226. tt_str_op(k,OP_EQ, "k11");
  1227. tt_str_op(v,OP_EQ, "continuation at the start");
  1228. tor_free(k); tor_free(v);
  1229. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1230. tt_str_op(k,OP_EQ, "k12");
  1231. tt_str_op(v,OP_EQ, "line with a embedded");
  1232. tor_free(k); tor_free(v);
  1233. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1234. tt_str_op(k,OP_EQ, "k13");
  1235. tt_str_op(v,OP_EQ, "continuation at the very start");
  1236. tor_free(k); tor_free(v);
  1237. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1238. tt_str_op(k,OP_EQ, "k14");
  1239. tt_str_op(v,OP_EQ, "a line that has a comment and" );
  1240. tor_free(k); tor_free(v);
  1241. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1242. tt_str_op(k,OP_EQ, "k15");
  1243. tt_str_op(v,OP_EQ, "this should be the next new line");
  1244. tor_free(k); tor_free(v);
  1245. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1246. tt_str_op(k,OP_EQ, "k16");
  1247. tt_str_op(v,OP_EQ, "a line that has a comment and" );
  1248. tor_free(k); tor_free(v);
  1249. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1250. tt_str_op(k,OP_EQ, "k17");
  1251. tt_str_op(v,OP_EQ, "this should be the next new line");
  1252. tor_free(k); tor_free(v);
  1253. tt_str_op(str,OP_EQ, "");
  1254. done:
  1255. tor_free(k);
  1256. tor_free(v);
  1257. }
  1258. static void
  1259. test_util_config_line_quotes(void *arg)
  1260. {
  1261. char buf1[1024];
  1262. char buf2[128];
  1263. char buf3[128];
  1264. char buf4[128];
  1265. char *k=NULL, *v=NULL;
  1266. const char *str;
  1267. /* Test parse_config_line_from_str */
  1268. (void)arg;
  1269. strlcpy(buf1, "kTrailingSpace \"quoted value\" \n"
  1270. "kTrailingGarbage \"quoted value\"trailing garbage\n"
  1271. , sizeof(buf1));
  1272. strlcpy(buf2, "kTrailingSpaceAndGarbage \"quoted value\" trailing space+g\n"
  1273. , sizeof(buf2));
  1274. strlcpy(buf3, "kMultilineTrailingSpace \"mline\\ \nvalue w/ trailing sp\"\n"
  1275. , sizeof(buf3));
  1276. strlcpy(buf4, "kMultilineNoTrailingBackslash \"naked multiline\nvalue\"\n"
  1277. , sizeof(buf4));
  1278. str = buf1;
  1279. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1280. tt_str_op(k,OP_EQ, "kTrailingSpace");
  1281. tt_str_op(v,OP_EQ, "quoted value");
  1282. tor_free(k); tor_free(v);
  1283. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1284. tt_ptr_op(str,OP_EQ, NULL);
  1285. tor_free(k); tor_free(v);
  1286. str = buf2;
  1287. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1288. tt_ptr_op(str,OP_EQ, NULL);
  1289. tor_free(k); tor_free(v);
  1290. str = buf3;
  1291. const char *err = NULL;
  1292. str = parse_config_line_from_str_verbose(str, &k, &v, &err);
  1293. tt_ptr_op(str,OP_EQ, NULL);
  1294. tor_free(k); tor_free(v);
  1295. tt_str_op(err, OP_EQ, "Invalid escape sequence in quoted string");
  1296. str = buf4;
  1297. err = NULL;
  1298. str = parse_config_line_from_str_verbose(str, &k, &v, &err);
  1299. tt_ptr_op(str,OP_EQ, NULL);
  1300. tor_free(k); tor_free(v);
  1301. tt_str_op(err, OP_EQ, "Invalid escape sequence in quoted string");
  1302. done:
  1303. tor_free(k);
  1304. tor_free(v);
  1305. }
  1306. static void
  1307. test_util_config_line_comment_character(void *arg)
  1308. {
  1309. char buf[1024];
  1310. char *k=NULL, *v=NULL;
  1311. const char *str;
  1312. /* Test parse_config_line_from_str */
  1313. (void)arg;
  1314. strlcpy(buf, "k1 \"# in quotes\"\n"
  1315. "k2 some value # some comment\n"
  1316. "k3 /home/user/myTorNetwork#2\n" /* Testcase for #1323 */
  1317. , sizeof(buf));
  1318. str = buf;
  1319. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1320. tt_str_op(k,OP_EQ, "k1");
  1321. tt_str_op(v,OP_EQ, "# in quotes");
  1322. tor_free(k); tor_free(v);
  1323. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1324. tt_str_op(k,OP_EQ, "k2");
  1325. tt_str_op(v,OP_EQ, "some value");
  1326. tor_free(k); tor_free(v);
  1327. tt_str_op(str,OP_EQ, "k3 /home/user/myTorNetwork#2\n");
  1328. #if 0
  1329. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1330. test_streq(k, "k3");
  1331. test_streq(v, "/home/user/myTorNetwork#2");
  1332. tor_free(k); tor_free(v);
  1333. test_streq(str, "");
  1334. #endif /* 0 */
  1335. done:
  1336. tor_free(k);
  1337. tor_free(v);
  1338. }
  1339. static void
  1340. test_util_config_line_escaped_content(void *arg)
  1341. {
  1342. char buf1[1024];
  1343. char buf2[128];
  1344. char buf3[128];
  1345. char buf4[128];
  1346. char buf5[128];
  1347. char buf6[128];
  1348. char *k=NULL, *v=NULL;
  1349. const char *str;
  1350. /* Test parse_config_line_from_str */
  1351. (void)arg;
  1352. strlcpy(buf1, "HexadecimalLower \"\\x2a\"\n"
  1353. "HexadecimalUpper \"\\x2A\"\n"
  1354. "HexadecimalUpperX \"\\X2A\"\n"
  1355. "Octal \"\\52\"\n"
  1356. "Newline \"\\n\"\n"
  1357. "Tab \"\\t\"\n"
  1358. "CarriageReturn \"\\r\"\n"
  1359. "DoubleQuote \"\\\"\"\n"
  1360. "SimpleQuote \"\\'\"\n"
  1361. "Backslash \"\\\\\"\n"
  1362. "Mix \"This is a \\\"star\\\":\\t\\'\\x2a\\'\\nAnd second line\"\n"
  1363. , sizeof(buf1));
  1364. strlcpy(buf2, "BrokenEscapedContent \"\\a\"\n"
  1365. , sizeof(buf2));
  1366. strlcpy(buf3, "BrokenEscapedContent \"\\x\"\n"
  1367. , sizeof(buf3));
  1368. strlcpy(buf4, "BrokenOctal \"\\8\"\n"
  1369. , sizeof(buf4));
  1370. strlcpy(buf5, "BrokenHex \"\\xg4\"\n"
  1371. , sizeof(buf5));
  1372. strlcpy(buf6, "BrokenEscape \"\\"
  1373. , sizeof(buf6));
  1374. str = buf1;
  1375. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1376. tt_str_op(k,OP_EQ, "HexadecimalLower");
  1377. tt_str_op(v,OP_EQ, "*");
  1378. tor_free(k); tor_free(v);
  1379. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1380. tt_str_op(k,OP_EQ, "HexadecimalUpper");
  1381. tt_str_op(v,OP_EQ, "*");
  1382. tor_free(k); tor_free(v);
  1383. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1384. tt_str_op(k,OP_EQ, "HexadecimalUpperX");
  1385. tt_str_op(v,OP_EQ, "*");
  1386. tor_free(k); tor_free(v);
  1387. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1388. tt_str_op(k,OP_EQ, "Octal");
  1389. tt_str_op(v,OP_EQ, "*");
  1390. tor_free(k); tor_free(v);
  1391. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1392. tt_str_op(k,OP_EQ, "Newline");
  1393. tt_str_op(v,OP_EQ, "\n");
  1394. tor_free(k); tor_free(v);
  1395. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1396. tt_str_op(k,OP_EQ, "Tab");
  1397. tt_str_op(v,OP_EQ, "\t");
  1398. tor_free(k); tor_free(v);
  1399. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1400. tt_str_op(k,OP_EQ, "CarriageReturn");
  1401. tt_str_op(v,OP_EQ, "\r");
  1402. tor_free(k); tor_free(v);
  1403. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1404. tt_str_op(k,OP_EQ, "DoubleQuote");
  1405. tt_str_op(v,OP_EQ, "\"");
  1406. tor_free(k); tor_free(v);
  1407. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1408. tt_str_op(k,OP_EQ, "SimpleQuote");
  1409. tt_str_op(v,OP_EQ, "'");
  1410. tor_free(k); tor_free(v);
  1411. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1412. tt_str_op(k,OP_EQ, "Backslash");
  1413. tt_str_op(v,OP_EQ, "\\");
  1414. tor_free(k); tor_free(v);
  1415. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1416. tt_str_op(k,OP_EQ, "Mix");
  1417. tt_str_op(v,OP_EQ, "This is a \"star\":\t'*'\nAnd second line");
  1418. tor_free(k); tor_free(v);
  1419. tt_str_op(str,OP_EQ, "");
  1420. str = buf2;
  1421. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1422. tt_ptr_op(str,OP_EQ, NULL);
  1423. tor_free(k); tor_free(v);
  1424. str = buf3;
  1425. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1426. tt_ptr_op(str,OP_EQ, NULL);
  1427. tor_free(k); tor_free(v);
  1428. str = buf4;
  1429. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1430. tt_ptr_op(str,OP_EQ, NULL);
  1431. tor_free(k); tor_free(v);
  1432. #if 0
  1433. str = buf5;
  1434. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1435. tt_ptr_op(str, OP_EQ, NULL);
  1436. tor_free(k); tor_free(v);
  1437. #endif /* 0 */
  1438. str = buf6;
  1439. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1440. tt_ptr_op(str,OP_EQ, NULL);
  1441. tor_free(k); tor_free(v);
  1442. /* more things to try. */
  1443. /* Bad hex: */
  1444. strlcpy(buf1, "Foo \"\\x9g\"\n", sizeof(buf1));
  1445. strlcpy(buf2, "Foo \"\\xg0\"\n", sizeof(buf2));
  1446. strlcpy(buf3, "Foo \"\\xf\"\n", sizeof(buf3));
  1447. /* bad escape */
  1448. strlcpy(buf4, "Foo \"\\q\"\n", sizeof(buf4));
  1449. /* missing endquote */
  1450. strlcpy(buf5, "Foo \"hello\n", sizeof(buf5));
  1451. /* extra stuff */
  1452. strlcpy(buf6, "Foo \"hello\" world\n", sizeof(buf6));
  1453. str=buf1;
  1454. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1455. tt_ptr_op(str,OP_EQ, NULL);
  1456. tor_free(k); tor_free(v);
  1457. str=buf2;
  1458. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1459. tt_ptr_op(str,OP_EQ, NULL);
  1460. tor_free(k); tor_free(v);
  1461. str=buf3;
  1462. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1463. tt_ptr_op(str,OP_EQ, NULL);
  1464. tor_free(k); tor_free(v);
  1465. str=buf4;
  1466. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1467. tt_ptr_op(str,OP_EQ, NULL);
  1468. tor_free(k); tor_free(v);
  1469. str=buf5;
  1470. str = parse_config_line_from_str_verbose(str, &k, &v, NULL);
  1471. tt_ptr_op(str,OP_EQ, NULL);
  1472. tor_free(k); tor_free(v);
  1473. str=buf6;
  1474. const char *err = NULL;
  1475. str = parse_config_line_from_str_verbose(str, &k, &v, &err);
  1476. tt_ptr_op(str,OP_EQ, NULL);
  1477. tor_free(k); tor_free(v);
  1478. tt_str_op(err,OP_EQ, "Excess data after quoted string");
  1479. done:
  1480. tor_free(k);
  1481. tor_free(v);
  1482. }
  1483. static void
  1484. test_util_config_line_crlf(void *arg)
  1485. {
  1486. char *k=NULL, *v=NULL;
  1487. const char *err = NULL;
  1488. (void)arg;
  1489. const char *str =
  1490. "Hello world\r\n"
  1491. "Hello \"nice big world\"\r\n";
  1492. str = parse_config_line_from_str_verbose(str, &k, &v, &err);
  1493. tt_assert(str);
  1494. tt_str_op(k,OP_EQ,"Hello");
  1495. tt_str_op(v,OP_EQ,"world");
  1496. tt_ptr_op(err, OP_EQ, NULL);
  1497. tor_free(k); tor_free(v);
  1498. str = parse_config_line_from_str_verbose(str, &k, &v, &err);
  1499. tt_assert(str);
  1500. tt_str_op(k,OP_EQ,"Hello");
  1501. tt_str_op(v,OP_EQ,"nice big world");
  1502. tt_ptr_op(err, OP_EQ, NULL);
  1503. tor_free(k); tor_free(v);
  1504. tt_str_op(str,OP_EQ, "");
  1505. done:
  1506. tor_free(k); tor_free(v);
  1507. }
  1508. #ifndef _WIN32
  1509. static void
  1510. test_util_expand_filename(void *arg)
  1511. {
  1512. char *str;
  1513. (void)arg;
  1514. setenv("HOME", "/home/itv", 1); /* For "internal test value" */
  1515. str = expand_filename("");
  1516. tt_str_op("",OP_EQ, str);
  1517. tor_free(str);
  1518. str = expand_filename("/normal/path");
  1519. tt_str_op("/normal/path",OP_EQ, str);
  1520. tor_free(str);
  1521. str = expand_filename("/normal/trailing/path/");
  1522. tt_str_op("/normal/trailing/path/",OP_EQ, str);
  1523. tor_free(str);
  1524. str = expand_filename("~");
  1525. tt_str_op("/home/itv/",OP_EQ, str);
  1526. tor_free(str);
  1527. str = expand_filename("$HOME/nodice");
  1528. tt_str_op("$HOME/nodice",OP_EQ, str);
  1529. tor_free(str);
  1530. str = expand_filename("~/");
  1531. tt_str_op("/home/itv/",OP_EQ, str);
  1532. tor_free(str);
  1533. str = expand_filename("~/foobarqux");
  1534. tt_str_op("/home/itv/foobarqux",OP_EQ, str);
  1535. tor_free(str);
  1536. str = expand_filename("~/../../etc/passwd");
  1537. tt_str_op("/home/itv/../../etc/passwd",OP_EQ, str);
  1538. tor_free(str);
  1539. str = expand_filename("~/trailing/");
  1540. tt_str_op("/home/itv/trailing/",OP_EQ, str);
  1541. tor_free(str);
  1542. /* Ideally we'd test ~anotheruser, but that's shady to test (we'd
  1543. have to somehow inject/fake the get_user_homedir call) */
  1544. /* $HOME ending in a trailing slash */
  1545. setenv("HOME", "/home/itv/", 1);
  1546. str = expand_filename("~");
  1547. tt_str_op("/home/itv/",OP_EQ, str);
  1548. tor_free(str);
  1549. str = expand_filename("~/");
  1550. tt_str_op("/home/itv/",OP_EQ, str);
  1551. tor_free(str);
  1552. str = expand_filename("~/foo");
  1553. tt_str_op("/home/itv/foo",OP_EQ, str);
  1554. tor_free(str);
  1555. /* Try with empty $HOME */
  1556. setenv("HOME", "", 1);
  1557. str = expand_filename("~");
  1558. tt_str_op("/",OP_EQ, str);
  1559. tor_free(str);
  1560. str = expand_filename("~/");
  1561. tt_str_op("/",OP_EQ, str);
  1562. tor_free(str);
  1563. str = expand_filename("~/foobar");
  1564. tt_str_op("/foobar",OP_EQ, str);
  1565. tor_free(str);
  1566. /* Try with $HOME unset */
  1567. unsetenv("HOME");
  1568. str = expand_filename("~");
  1569. tt_str_op("/",OP_EQ, str);
  1570. tor_free(str);
  1571. str = expand_filename("~/");
  1572. tt_str_op("/",OP_EQ, str);
  1573. tor_free(str);
  1574. str = expand_filename("~/foobar");
  1575. tt_str_op("/foobar",OP_EQ, str);
  1576. tor_free(str);
  1577. done:
  1578. tor_free(str);
  1579. }
  1580. #endif /* !defined(_WIN32) */
  1581. /** Test tor_escape_str_for_pt_args(). */
  1582. static void
  1583. test_util_escape_string_socks(void *arg)
  1584. {
  1585. char *escaped_string = NULL;
  1586. /** Simple backslash escape. */
  1587. (void)arg;
  1588. escaped_string = tor_escape_str_for_pt_args("This is a backslash: \\",";\\");
  1589. tt_assert(escaped_string);
  1590. tt_str_op(escaped_string,OP_EQ, "This is a backslash: \\\\");
  1591. tor_free(escaped_string);
  1592. /** Simple semicolon escape. */
  1593. escaped_string = tor_escape_str_for_pt_args("First rule:Do not use ;",";\\");
  1594. tt_assert(escaped_string);
  1595. tt_str_op(escaped_string,OP_EQ, "First rule:Do not use \\;");
  1596. tor_free(escaped_string);
  1597. /** Empty string. */
  1598. escaped_string = tor_escape_str_for_pt_args("", ";\\");
  1599. tt_assert(escaped_string);
  1600. tt_str_op(escaped_string,OP_EQ, "");
  1601. tor_free(escaped_string);
  1602. /** Escape all characters. */
  1603. escaped_string = tor_escape_str_for_pt_args(";\\;\\", ";\\");
  1604. tt_assert(escaped_string);
  1605. tt_str_op(escaped_string,OP_EQ, "\\;\\\\\\;\\\\");
  1606. tor_free(escaped_string);
  1607. escaped_string = tor_escape_str_for_pt_args(";", ";\\");
  1608. tt_assert(escaped_string);
  1609. tt_str_op(escaped_string,OP_EQ, "\\;");
  1610. tor_free(escaped_string);
  1611. done:
  1612. tor_free(escaped_string);
  1613. }
  1614. static void
  1615. test_util_string_is_key_value(void *ptr)
  1616. {
  1617. (void)ptr;
  1618. tt_assert(string_is_key_value(LOG_WARN, "key=value"));
  1619. tt_assert(string_is_key_value(LOG_WARN, "k=v"));
  1620. tt_assert(string_is_key_value(LOG_WARN, "key="));
  1621. tt_assert(string_is_key_value(LOG_WARN, "x="));
  1622. tt_assert(string_is_key_value(LOG_WARN, "xx="));
  1623. tt_assert(!string_is_key_value(LOG_WARN, "=value"));
  1624. tt_assert(!string_is_key_value(LOG_WARN, "=x"));
  1625. tt_assert(!string_is_key_value(LOG_WARN, "="));
  1626. /* ??? */
  1627. /* tt_assert(!string_is_key_value(LOG_WARN, "===")); */
  1628. done:
  1629. ;
  1630. }
  1631. /** Test basic string functionality. */
  1632. static void
  1633. test_util_strmisc(void *arg)
  1634. {
  1635. char buf[1024];
  1636. char *cp_tmp = NULL;
  1637. /* Test strl operations */
  1638. (void)arg;
  1639. tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 0));
  1640. tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 10));
  1641. tt_str_op(buf,OP_EQ, "Hello");
  1642. tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 6));
  1643. tt_str_op(buf,OP_EQ, "Hello");
  1644. tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 5));
  1645. tt_str_op(buf,OP_EQ, "Hell");
  1646. strlcpy(buf, "Hello", sizeof(buf));
  1647. tt_int_op(10,OP_EQ, strlcat(buf, "Hello", 5));
  1648. /* Test strstrip() */
  1649. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  1650. tor_strstrip(buf, ",!");
  1651. tt_str_op(buf,OP_EQ, "Testing 1 2 3");
  1652. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  1653. tor_strstrip(buf, "!? ");
  1654. tt_str_op(buf,OP_EQ, "Testing123");
  1655. strlcpy(buf, "!!!Testing 1 2 3??", sizeof(buf));
  1656. tor_strstrip(buf, "!? ");
  1657. tt_str_op(buf,OP_EQ, "Testing123");
  1658. /* Test snprintf */
  1659. /* Returning -1 when there's not enough room in the output buffer */
  1660. tt_int_op(-1,OP_EQ, tor_snprintf(buf, 0, "Foo"));
  1661. tt_int_op(-1,OP_EQ, tor_snprintf(buf, 2, "Foo"));
  1662. tt_int_op(-1,OP_EQ, tor_snprintf(buf, 3, "Foo"));
  1663. tt_int_op(-1,OP_NE, tor_snprintf(buf, 4, "Foo"));
  1664. /* Always NUL-terminate the output */
  1665. tor_snprintf(buf, 5, "abcdef");
  1666. tt_int_op(0,OP_EQ, buf[4]);
  1667. tor_snprintf(buf, 10, "abcdef");
  1668. tt_int_op(0,OP_EQ, buf[6]);
  1669. /* uint64 */
  1670. tor_snprintf(buf, sizeof(buf), "x!%"PRIu64"!x",
  1671. (UINT64_C(12345678901)));
  1672. tt_str_op("x!12345678901!x",OP_EQ, buf);
  1673. /* Test str{,case}cmpstart */
  1674. tt_assert(strcmpstart("abcdef", "abcdef")==0);
  1675. tt_assert(strcmpstart("abcdef", "abc")==0);
  1676. tt_assert(strcmpstart("abcdef", "abd")<0);
  1677. tt_assert(strcmpstart("abcdef", "abb")>0);
  1678. tt_assert(strcmpstart("ab", "abb")<0);
  1679. tt_assert(strcmpstart("ab", "")==0);
  1680. tt_assert(strcmpstart("ab", "ab ")<0);
  1681. tt_assert(strcasecmpstart("abcdef", "abCdEF")==0);
  1682. tt_assert(strcasecmpstart("abcDeF", "abc")==0);
  1683. tt_assert(strcasecmpstart("abcdef", "Abd")<0);
  1684. tt_assert(strcasecmpstart("Abcdef", "abb")>0);
  1685. tt_assert(strcasecmpstart("ab", "Abb")<0);
  1686. tt_assert(strcasecmpstart("ab", "")==0);
  1687. tt_assert(strcasecmpstart("ab", "ab ")<0);
  1688. /* Test str{,case}cmpend */
  1689. tt_assert(strcmpend("abcdef", "abcdef")==0);
  1690. tt_assert(strcmpend("abcdef", "def")==0);
  1691. tt_assert(strcmpend("abcdef", "deg")<0);
  1692. tt_assert(strcmpend("abcdef", "dee")>0);
  1693. tt_assert(strcmpend("ab", "aab")>0);
  1694. tt_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  1695. tt_assert(strcasecmpend("abcdef", "dEF")==0);
  1696. tt_assert(strcasecmpend("abcdef", "Deg")<0);
  1697. tt_assert(strcasecmpend("abcDef", "dee")>0);
  1698. tt_assert(strcasecmpend("AB", "abb")<0);
  1699. /* Test digest_is_zero */
  1700. memset(buf,0,20);
  1701. buf[20] = 'x';
  1702. tt_assert(tor_digest_is_zero(buf));
  1703. buf[19] = 'x';
  1704. tt_assert(!tor_digest_is_zero(buf));
  1705. /* Test mem_is_zero */
  1706. memset(buf,0,128);
  1707. buf[128] = 'x';
  1708. tt_assert(tor_mem_is_zero(buf, 10));
  1709. tt_assert(tor_mem_is_zero(buf, 20));
  1710. tt_assert(tor_mem_is_zero(buf, 128));
  1711. tt_assert(!tor_mem_is_zero(buf, 129));
  1712. buf[60] = (char)255;
  1713. tt_assert(!tor_mem_is_zero(buf, 128));
  1714. buf[0] = (char)1;
  1715. tt_assert(!tor_mem_is_zero(buf, 10));
  1716. /* Test 'escaped' */
  1717. tt_ptr_op(escaped(NULL), OP_EQ, NULL);
  1718. tt_str_op("\"\"",OP_EQ, escaped(""));
  1719. tt_str_op("\"abcd\"",OP_EQ, escaped("abcd"));
  1720. tt_str_op("\"\\\\ \\n\\r\\t\\\"\\'\"",OP_EQ, escaped("\\ \n\r\t\"'"));
  1721. tt_str_op("\"unnecessary \\'backslashes\\'\"",OP_EQ,
  1722. escaped("unnecessary \'backslashes\'"));
  1723. /* Non-printable characters appear as octal */
  1724. tt_str_op("\"z\\001abc\\277d\"",OP_EQ, escaped("z\001abc\277d"));
  1725. tt_str_op("\"z\\336\\255 ;foo\"",OP_EQ, escaped("z\xde\xad\x20;foo"));
  1726. /* Other cases of esc_for_log{,_len} */
  1727. cp_tmp = esc_for_log(NULL);
  1728. tt_str_op(cp_tmp, OP_EQ, "(null)");
  1729. tor_free(cp_tmp);
  1730. cp_tmp = esc_for_log_len("abcdefg", 3);
  1731. tt_str_op(cp_tmp, OP_EQ, "\"abc\"");
  1732. tor_free(cp_tmp);
  1733. cp_tmp = esc_for_log_len("abcdefg", 100);
  1734. tt_str_op(cp_tmp, OP_EQ, "\"abcdefg\"");
  1735. tor_free(cp_tmp);
  1736. /* Test strndup and memdup */
  1737. {
  1738. const char *s = "abcdefghijklmnopqrstuvwxyz";
  1739. cp_tmp = tor_strndup(s, 30);
  1740. tt_str_op(cp_tmp,OP_EQ, s); /* same string, */
  1741. tt_ptr_op(cp_tmp,OP_NE,s); /* but different pointers. */
  1742. tor_free(cp_tmp);
  1743. cp_tmp = tor_strndup(s, 5);
  1744. tt_str_op(cp_tmp,OP_EQ, "abcde");
  1745. tor_free(cp_tmp);
  1746. s = "a\0b\0c\0d\0e\0";
  1747. cp_tmp = tor_memdup(s,10);
  1748. tt_mem_op(cp_tmp,OP_EQ, s, 10); /* same ram, */
  1749. tt_ptr_op(cp_tmp,OP_NE,s); /* but different pointers. */
  1750. tor_free(cp_tmp);
  1751. }
  1752. /* Test str-foo functions */
  1753. cp_tmp = tor_strdup("abcdef");
  1754. tt_assert(tor_strisnonupper(cp_tmp));
  1755. cp_tmp[3] = 'D';
  1756. tt_assert(!tor_strisnonupper(cp_tmp));
  1757. tor_strupper(cp_tmp);
  1758. tt_str_op(cp_tmp,OP_EQ, "ABCDEF");
  1759. tor_strlower(cp_tmp);
  1760. tt_str_op(cp_tmp,OP_EQ, "abcdef");
  1761. tt_assert(tor_strisnonupper(cp_tmp));
  1762. tt_assert(tor_strisprint(cp_tmp));
  1763. cp_tmp[3] = 3;
  1764. tt_assert(!tor_strisprint(cp_tmp));
  1765. tor_free(cp_tmp);
  1766. /* Test memmem and memstr */
  1767. {
  1768. const char *haystack = "abcde";
  1769. tt_ptr_op(tor_memmem(haystack, 5, "ef", 2), OP_EQ, NULL);
  1770. tt_ptr_op(tor_memmem(haystack, 5, "cd", 2),OP_EQ, haystack + 2);
  1771. tt_ptr_op(tor_memmem(haystack, 5, "cde", 3),OP_EQ, haystack + 2);
  1772. tt_ptr_op(tor_memmem(haystack, 4, "cde", 3), OP_EQ, NULL);
  1773. haystack = "ababcad";
  1774. tt_ptr_op(tor_memmem(haystack, 7, "abc", 3),OP_EQ, haystack + 2);
  1775. tt_ptr_op(tor_memmem(haystack, 7, "ad", 2),OP_EQ, haystack + 5);
  1776. tt_ptr_op(tor_memmem(haystack, 7, "cad", 3),OP_EQ, haystack + 4);
  1777. tt_ptr_op(tor_memmem(haystack, 7, "dadad", 5), OP_EQ, NULL);
  1778. tt_ptr_op(tor_memmem(haystack, 7, "abcdefghij", 10), OP_EQ, NULL);
  1779. /* memstr */
  1780. tt_ptr_op(tor_memstr(haystack, 7, "abc"),OP_EQ, haystack + 2);
  1781. tt_ptr_op(tor_memstr(haystack, 7, "cad"),OP_EQ, haystack + 4);
  1782. tt_ptr_op(tor_memstr(haystack, 6, "cad"), OP_EQ, NULL);
  1783. tt_ptr_op(tor_memstr(haystack, 7, "cadd"), OP_EQ, NULL);
  1784. tt_ptr_op(tor_memstr(haystack, 7, "fe"), OP_EQ, NULL);
  1785. tt_ptr_op(tor_memstr(haystack, 7, "ababcade"), OP_EQ, NULL);
  1786. }
  1787. /* Test hex_str */
  1788. {
  1789. char binary_data[68];
  1790. size_t idx;
  1791. for (idx = 0; idx < sizeof(binary_data); ++idx)
  1792. binary_data[idx] = idx;
  1793. tt_str_op(hex_str(binary_data, 0),OP_EQ, "");
  1794. tt_str_op(hex_str(binary_data, 1),OP_EQ, "00");
  1795. tt_str_op(hex_str(binary_data, 17),OP_EQ,
  1796. "000102030405060708090A0B0C0D0E0F10");
  1797. tt_str_op(hex_str(binary_data, 32),OP_EQ,
  1798. "000102030405060708090A0B0C0D0E0F"
  1799. "101112131415161718191A1B1C1D1E1F");
  1800. tt_str_op(hex_str(binary_data, 34),OP_EQ,
  1801. "000102030405060708090A0B0C0D0E0F"
  1802. "101112131415161718191A1B1C1D1E1F");
  1803. /* Repeat these tests for shorter strings after longer strings
  1804. have been tried, to make sure we're correctly terminating strings */
  1805. tt_str_op(hex_str(binary_data, 1),OP_EQ, "00");
  1806. tt_str_op(hex_str(binary_data, 0),OP_EQ, "");
  1807. }
  1808. /* Test strcmp_opt */
  1809. tt_int_op(strcmp_opt("", "foo"), OP_LT, 0);
  1810. tt_int_op(strcmp_opt("", ""), OP_EQ, 0);
  1811. tt_int_op(strcmp_opt("foo", ""), OP_GT, 0);
  1812. tt_int_op(strcmp_opt(NULL, ""), OP_LT, 0);
  1813. tt_int_op(strcmp_opt(NULL, NULL), OP_EQ, 0);
  1814. tt_int_op(strcmp_opt("", NULL), OP_GT, 0);
  1815. tt_int_op(strcmp_opt(NULL, "foo"), OP_LT, 0);
  1816. tt_int_op(strcmp_opt("foo", NULL), OP_GT, 0);
  1817. done:
  1818. tor_free(cp_tmp);
  1819. }
  1820. static void
  1821. test_util_parse_integer(void *arg)
  1822. {
  1823. (void)arg;
  1824. int i;
  1825. char *cp;
  1826. /* Test parse_long */
  1827. /* Empty/zero input */
  1828. tt_int_op(0L,OP_EQ, tor_parse_long("",10,0,100,&i,NULL));
  1829. tt_int_op(0,OP_EQ, i);
  1830. tt_int_op(0L,OP_EQ, tor_parse_long("0",10,0,100,&i,NULL));
  1831. tt_int_op(1,OP_EQ, i);
  1832. /* Normal cases */
  1833. tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,100,&i,NULL));
  1834. tt_int_op(1,OP_EQ, i);
  1835. tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,10,&i,NULL));
  1836. tt_int_op(1,OP_EQ, i);
  1837. tt_int_op(10L,OP_EQ, tor_parse_long("10",10,10,100,&i,NULL));
  1838. tt_int_op(1,OP_EQ, i);
  1839. tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,100,&i,NULL));
  1840. tt_int_op(1,OP_EQ, i);
  1841. tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,0,&i,NULL));
  1842. tt_int_op(1,OP_EQ, i);
  1843. tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-50,0,&i,NULL));
  1844. tt_int_op(1,OP_EQ, i);
  1845. /* Extra garbage */
  1846. tt_int_op(0L,OP_EQ, tor_parse_long("10m",10,0,100,&i,NULL));
  1847. tt_int_op(0,OP_EQ, i);
  1848. tt_int_op(0L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,NULL));
  1849. tt_int_op(0,OP_EQ, i);
  1850. tt_int_op(10L,OP_EQ, tor_parse_long("10m",10,0,100,&i,&cp));
  1851. tt_int_op(1,OP_EQ, i);
  1852. tt_str_op(cp,OP_EQ, "m");
  1853. tt_int_op(-50L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,&cp));
  1854. tt_int_op(1,OP_EQ, i);
  1855. tt_str_op(cp,OP_EQ, " plus garbage");
  1856. /* Illogical min max */
  1857. tt_int_op(0L,OP_EQ, tor_parse_long("10",10,50,4,&i,NULL));
  1858. tt_int_op(0,OP_EQ, i);
  1859. tt_int_op(0L,OP_EQ, tor_parse_long("-50",10,100,-100,&i,NULL));
  1860. tt_int_op(0,OP_EQ, i);
  1861. /* Out of bounds */
  1862. tt_int_op(0L,OP_EQ, tor_parse_long("10",10,50,100,&i,NULL));
  1863. tt_int_op(0,OP_EQ, i);
  1864. tt_int_op(0L,OP_EQ, tor_parse_long("-50",10,0,100,&i,NULL));
  1865. tt_int_op(0,OP_EQ, i);
  1866. /* Base different than 10 */
  1867. tt_int_op(2L,OP_EQ, tor_parse_long("10",2,0,100,NULL,NULL));
  1868. tt_int_op(0L,OP_EQ, tor_parse_long("2",2,0,100,NULL,NULL));
  1869. tt_int_op(68284L,OP_EQ, tor_parse_long("10abc",16,0,70000,NULL,NULL));
  1870. tt_int_op(68284L,OP_EQ, tor_parse_long("10ABC",16,0,70000,NULL,NULL));
  1871. tt_int_op(0L,OP_EQ, tor_parse_long("10",-2,0,100,NULL,NULL));
  1872. tt_int_op(0,OP_EQ, tor_parse_long("10ABC",-1,0,70000,&i,NULL));
  1873. tt_int_op(i,OP_EQ, 0);
  1874. /* Test parse_ulong */
  1875. tt_int_op(0UL,OP_EQ, tor_parse_ulong("",10,0,100,NULL,NULL));
  1876. tt_int_op(0UL,OP_EQ, tor_parse_ulong("0",10,0,100,NULL,NULL));
  1877. tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,100,NULL,NULL));
  1878. tt_int_op(0UL,OP_EQ, tor_parse_ulong("10",10,50,100,NULL,NULL));
  1879. tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,10,NULL,NULL));
  1880. tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,10,100,NULL,NULL));
  1881. tt_int_op(0UL,OP_EQ, tor_parse_ulong("8",8,0,100,NULL,NULL));
  1882. tt_int_op(50UL,OP_EQ, tor_parse_ulong("50",10,50,100,NULL,NULL));
  1883. tt_int_op(0UL,OP_EQ, tor_parse_ulong("-50",10,0,100,NULL,NULL));
  1884. tt_int_op(0UL,OP_EQ, tor_parse_ulong("50",-1,50,100,&i,NULL));
  1885. tt_int_op(0,OP_EQ, i);
  1886. tt_int_op(0UL,OP_EQ, tor_parse_ulong("-50",10,0,100,&i,NULL));
  1887. tt_int_op(0,OP_EQ, i);
  1888. /* Test parse_uint64 */
  1889. tt_assert(UINT64_C(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  1890. tt_int_op(1,OP_EQ, i);
  1891. tt_str_op(cp,OP_EQ, " x");
  1892. tt_assert(UINT64_C(12345678901) ==
  1893. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  1894. tt_int_op(1,OP_EQ, i);
  1895. tt_str_op(cp,OP_EQ, "");
  1896. tt_assert(UINT64_C(0) ==
  1897. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  1898. tt_int_op(0,OP_EQ, i);
  1899. tt_assert(UINT64_C(0) ==
  1900. tor_parse_uint64("123",-1,0,INT32_MAX, &i, &cp));
  1901. tt_int_op(0,OP_EQ, i);
  1902. {
  1903. /* Test parse_double */
  1904. double d = tor_parse_double("10", 0, (double)UINT64_MAX,&i,NULL);
  1905. tt_int_op(1,OP_EQ, i);
  1906. tt_assert(((uint64_t)d) == 10);
  1907. d = tor_parse_double("0", 0, (double)UINT64_MAX,&i,NULL);
  1908. tt_int_op(1,OP_EQ, i);
  1909. tt_assert(((uint64_t)d) == 0);
  1910. d = tor_parse_double(" ", 0, (double)UINT64_MAX,&i,NULL);
  1911. tt_double_op(fabs(d), OP_LT, 1e-10);
  1912. tt_int_op(0,OP_EQ, i);
  1913. d = tor_parse_double(".0a", 0, (double)UINT64_MAX,&i,NULL);
  1914. tt_double_op(fabs(d), OP_LT, 1e-10);
  1915. tt_int_op(0,OP_EQ, i);
  1916. d = tor_parse_double(".0a", 0, (double)UINT64_MAX,&i,&cp);
  1917. tt_double_op(fabs(d), OP_LT, 1e-10);
  1918. tt_int_op(1,OP_EQ, i);
  1919. d = tor_parse_double("-.0", 0, (double)UINT64_MAX,&i,NULL);
  1920. tt_int_op(1,OP_EQ, i);
  1921. tt_assert(((uint64_t)d) == 0);
  1922. d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
  1923. tt_int_op(1,OP_EQ, i);
  1924. tt_double_op(fabs(d - -10.0),OP_LT, 1E-12);
  1925. }
  1926. {
  1927. /* Test tor_parse_* where we overflow/underflow the underlying type. */
  1928. /* This string should overflow 64-bit ints. */
  1929. #define TOOBIG "100000000000000000000000000"
  1930. tt_int_op(0L, OP_EQ,
  1931. tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  1932. tt_int_op(i,OP_EQ, 0);
  1933. tt_int_op(0L,OP_EQ,
  1934. tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  1935. tt_int_op(i,OP_EQ, 0);
  1936. tt_int_op(0UL,OP_EQ, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
  1937. tt_int_op(i,OP_EQ, 0);
  1938. tt_u64_op(UINT64_C(0), OP_EQ, tor_parse_uint64(TOOBIG, 10,
  1939. 0, UINT64_MAX, &i, NULL));
  1940. tt_int_op(i,OP_EQ, 0);
  1941. }
  1942. done:
  1943. ;
  1944. }
  1945. static void
  1946. test_util_pow2(void *arg)
  1947. {
  1948. /* Test tor_log2(). */
  1949. (void)arg;
  1950. tt_int_op(tor_log2(64),OP_EQ, 6);
  1951. tt_int_op(tor_log2(65),OP_EQ, 6);
  1952. tt_int_op(tor_log2(63),OP_EQ, 5);
  1953. /* incorrect mathematically, but as specified: */
  1954. tt_int_op(tor_log2(0),OP_EQ, 0);
  1955. tt_int_op(tor_log2(1),OP_EQ, 0);
  1956. tt_int_op(tor_log2(2),OP_EQ, 1);
  1957. tt_int_op(tor_log2(3),OP_EQ, 1);
  1958. tt_int_op(tor_log2(4),OP_EQ, 2);
  1959. tt_int_op(tor_log2(5),OP_EQ, 2);
  1960. tt_int_op(tor_log2(UINT64_C(40000000000000000)),OP_EQ, 55);
  1961. tt_int_op(tor_log2(UINT64_MAX),OP_EQ, 63);
  1962. /* Test round_to_power_of_2 */
  1963. tt_u64_op(round_to_power_of_2(120), OP_EQ, 128);
  1964. tt_u64_op(round_to_power_of_2(128), OP_EQ, 128);
  1965. tt_u64_op(round_to_power_of_2(130), OP_EQ, 128);
  1966. tt_u64_op(round_to_power_of_2(UINT64_C(40000000000000000)), OP_EQ,
  1967. UINT64_C(1)<<55);
  1968. tt_u64_op(round_to_power_of_2(UINT64_C(0xffffffffffffffff)), OP_EQ,
  1969. UINT64_C(1)<<63);
  1970. tt_u64_op(round_to_power_of_2(0), OP_EQ, 1);
  1971. tt_u64_op(round_to_power_of_2(1), OP_EQ, 1);
  1972. tt_u64_op(round_to_power_of_2(2), OP_EQ, 2);
  1973. tt_u64_op(round_to_power_of_2(3), OP_EQ, 2);
  1974. tt_u64_op(round_to_power_of_2(4), OP_EQ, 4);
  1975. tt_u64_op(round_to_power_of_2(5), OP_EQ, 4);
  1976. tt_u64_op(round_to_power_of_2(6), OP_EQ, 4);
  1977. tt_u64_op(round_to_power_of_2(7), OP_EQ, 8);
  1978. done:
  1979. ;
  1980. }
  1981. static void
  1982. test_util_compress_impl(compress_method_t method)
  1983. {
  1984. char *buf1=NULL, *buf2=NULL, *buf3=NULL;
  1985. size_t len1, len2;
  1986. tt_assert(tor_compress_supports_method(method));
  1987. if (method != NO_METHOD) {
  1988. tt_ptr_op(tor_compress_version_str(method), OP_NE, NULL);
  1989. tt_ptr_op(tor_compress_header_version_str(method), OP_NE, NULL);
  1990. }
  1991. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  1992. tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  1993. tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1, method));
  1994. tt_ptr_op(buf2, OP_NE, NULL);
  1995. if (method == NO_METHOD) {
  1996. // The identity transform doesn't actually compress, and it isn't
  1997. // detectable as "the identity transform."
  1998. tt_int_op(len1, OP_EQ, strlen(buf1)+1);
  1999. tt_int_op(detect_compression_method(buf2, len1), OP_EQ, UNKNOWN_METHOD);
  2000. } else {
  2001. tt_int_op(len1, OP_LT, strlen(buf1));
  2002. tt_int_op(detect_compression_method(buf2, len1), OP_EQ, method);
  2003. }
  2004. tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1, method, 1, LOG_INFO));
  2005. tt_ptr_op(buf3, OP_NE, NULL);
  2006. tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
  2007. tt_str_op(buf1, OP_EQ, buf3);
  2008. tt_int_op(buf3[len2], OP_EQ, 0);
  2009. /* Check whether we can uncompress concatenated, compressed strings. */
  2010. tor_free(buf3);
  2011. buf2 = tor_reallocarray(buf2, len1, 2);
  2012. memcpy(buf2+len1, buf2, len1);
  2013. tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1*2, method, 1, LOG_INFO));
  2014. tt_int_op((strlen(buf1)+1)*2, OP_EQ, len2);
  2015. tt_mem_op(buf3, OP_EQ,
  2016. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  2017. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  2018. (strlen(buf1)+1)*2);
  2019. tt_int_op(buf3[len2], OP_EQ, 0);
  2020. /* Check whether we can uncompress partial strings */
  2021. tor_free(buf1);
  2022. tor_free(buf2);
  2023. tor_free(buf3);
  2024. size_t b1len = 1<<10;
  2025. if (method == ZSTD_METHOD) {
  2026. // zstd needs a big input before it starts generating output that it
  2027. // can partially decompress.
  2028. b1len = 1<<18;
  2029. }
  2030. buf1 = tor_malloc(b1len);
  2031. crypto_rand(buf1, b1len);
  2032. tt_assert(!tor_compress(&buf2, &len1, buf1, b1len, method));
  2033. tt_int_op(len1, OP_GT, 16);
  2034. /* when we allow an incomplete output we should succeed.*/
  2035. tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1-16,
  2036. method, 0, LOG_INFO));
  2037. tt_int_op(len2, OP_GT, 5);
  2038. tt_int_op(len2, OP_LE, len1);
  2039. tt_assert(fast_memeq(buf1, buf3, len2));
  2040. tt_int_op(buf3[len2], OP_EQ, 0);
  2041. /* when we demand a complete output from a real compression method, this
  2042. * must fail. */
  2043. tor_free(buf3);
  2044. if (method != NO_METHOD) {
  2045. tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
  2046. method, 1, LOG_INFO));
  2047. tt_ptr_op(buf3, OP_EQ, NULL);
  2048. }
  2049. done:
  2050. tor_free(buf1);
  2051. tor_free(buf2);
  2052. tor_free(buf3);
  2053. }
  2054. static void
  2055. test_util_compress_stream_impl(compress_method_t method,
  2056. compression_level_t level)
  2057. {
  2058. char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  2059. const char *ccp2;
  2060. size_t len1, len2;
  2061. tor_compress_state_t *state = NULL;
  2062. state = tor_compress_new(1, method, level);
  2063. tt_assert(state);
  2064. cp1 = buf1 = tor_malloc(1024);
  2065. len1 = 1024;
  2066. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  2067. len2 = 21;
  2068. tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 0),
  2069. OP_EQ, TOR_COMPRESS_OK);
  2070. tt_int_op(0, OP_EQ, len2); /* Make sure we compressed it all. */
  2071. tt_assert(cp1 > buf1);
  2072. len2 = 0;
  2073. cp2 = cp1;
  2074. tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 1),
  2075. OP_EQ, TOR_COMPRESS_DONE);
  2076. tt_int_op(0, OP_EQ, len2);
  2077. if (method == NO_METHOD) {
  2078. tt_ptr_op(cp1, OP_EQ, cp2);
  2079. } else {
  2080. tt_assert(cp1 > cp2); /* Make sure we really added something. */
  2081. }
  2082. tt_int_op(tor_compress_state_size(state), OP_GT, 0);
  2083. tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
  2084. method, 1, LOG_WARN));
  2085. /* Make sure it compressed right. */
  2086. tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ");
  2087. tt_int_op(21, OP_EQ, len2);
  2088. done:
  2089. if (state)
  2090. tor_compress_free(state);
  2091. tor_free(buf1);
  2092. tor_free(buf2);
  2093. tor_free(buf3);
  2094. }
  2095. /** Setup function for compression tests: handles x-zstd:nostatic
  2096. */
  2097. static void *
  2098. compression_test_setup(const struct testcase_t *testcase)
  2099. {
  2100. tor_assert(testcase->setup_data);
  2101. tor_assert(testcase->setup_data != (void*)TT_SKIP);
  2102. const char *methodname = testcase->setup_data;
  2103. if (!strcmp(methodname, "x-zstd:nostatic")) {
  2104. methodname = "x-zstd";
  2105. tor_zstd_set_static_apis_disabled_for_testing(1);
  2106. }
  2107. return (void *)methodname;
  2108. }
  2109. /** Cleanup for compression tests: disables nostatic */
  2110. static int
  2111. compression_test_cleanup(const struct testcase_t *testcase, void *ptr)
  2112. {
  2113. (void)testcase;
  2114. (void)ptr;
  2115. tor_zstd_set_static_apis_disabled_for_testing(0);
  2116. return 1;
  2117. }
  2118. static const struct testcase_setup_t compress_setup = {
  2119. compression_test_setup, compression_test_cleanup
  2120. };
  2121. /** Run unit tests for compression functions */
  2122. static void
  2123. test_util_compress(void *arg)
  2124. {
  2125. const char *methodname = arg;
  2126. tt_assert(methodname);
  2127. compress_method_t method = compression_method_get_by_name(methodname);
  2128. tt_int_op(method, OP_NE, UNKNOWN_METHOD);
  2129. if (! tor_compress_supports_method(method)) {
  2130. tt_skip();
  2131. }
  2132. compression_level_t levels[] = {
  2133. BEST_COMPRESSION,
  2134. HIGH_COMPRESSION,
  2135. MEDIUM_COMPRESSION,
  2136. LOW_COMPRESSION
  2137. };
  2138. test_util_compress_impl(method);
  2139. for (unsigned l = 0; l < ARRAY_LENGTH(levels); ++l) {
  2140. compression_level_t level = levels[l];
  2141. test_util_compress_stream_impl(method, level);
  2142. }
  2143. done:
  2144. ;
  2145. }
  2146. static void
  2147. test_util_decompress_concatenated_impl(compress_method_t method)
  2148. {
  2149. char input[4096];
  2150. char *c1 = NULL, *c2 = NULL, *c3 = NULL;
  2151. char *result = NULL;
  2152. size_t sz1, sz2, sz3, szr;
  2153. int r;
  2154. crypto_rand(input, sizeof(input));
  2155. /* Compress the input in two chunks. */
  2156. r = tor_compress(&c1, &sz1, input, 2048, method);
  2157. tt_int_op(r, OP_EQ, 0);
  2158. r = tor_compress(&c2, &sz2, input+2048, 2048, method);
  2159. tt_int_op(r, OP_EQ, 0);
  2160. /* concatenate the chunks. */
  2161. sz3 = sz1 + sz2;
  2162. c3 = tor_malloc(sz3);
  2163. memcpy(c3, c1, sz1);
  2164. memcpy(c3+sz1, c2, sz2);
  2165. /* decompress the concatenated result */
  2166. r = tor_uncompress(&result, &szr, c3, sz3, method, 0, LOG_WARN);
  2167. tt_int_op(r, OP_EQ, 0);
  2168. tt_int_op(szr, OP_EQ, sizeof(input));
  2169. tt_mem_op(result, OP_EQ, input, sizeof(input));
  2170. done:
  2171. tor_free(c1);
  2172. tor_free(c2);
  2173. tor_free(c3);
  2174. tor_free(result);
  2175. }
  2176. static void
  2177. test_util_decompress_concatenated(void *arg)
  2178. {
  2179. const char *methodname = arg;
  2180. tt_assert(methodname);
  2181. compress_method_t method = compression_method_get_by_name(methodname);
  2182. tt_int_op(method, OP_NE, UNKNOWN_METHOD);
  2183. if (! tor_compress_supports_method(method)) {
  2184. tt_skip();
  2185. }
  2186. test_util_decompress_concatenated_impl(method);
  2187. done:
  2188. ;
  2189. }
  2190. static void
  2191. test_util_decompress_junk_impl(compress_method_t method)
  2192. {
  2193. char input[4096];
  2194. char *result = NULL, *result2 = NULL;
  2195. size_t szr, szr2, sz;
  2196. int r;
  2197. /* This shouldn't be a compressed string according to any method. */
  2198. strlcpy(input, "This shouldn't be a compressed string by any means.",
  2199. sizeof(input));
  2200. sz = strlen(input);
  2201. setup_capture_of_logs(LOG_WARN);
  2202. r = tor_uncompress(&result, &szr, input, sz, method, 0, LOG_WARN);
  2203. tt_int_op(r, OP_EQ, -1);
  2204. tt_ptr_op(result, OP_EQ, NULL);
  2205. expect_log_msg_containing("Error while uncompressing data: bad input?");
  2206. mock_clean_saved_logs();
  2207. /* Now try again, with a compressed object that starts out good and turns to
  2208. junk. */
  2209. crypto_rand(input, sizeof(input));
  2210. r = tor_compress(&result, &szr, input, sizeof(input), method);
  2211. tt_int_op(r, OP_EQ, 0);
  2212. crypto_rand(result+szr/2, szr-(szr/2)); // trash the 2nd half of the result
  2213. r = tor_uncompress(&result2, &szr2, result, szr, method, 0, LOG_WARN);
  2214. tt_int_op(r, OP_EQ, -1);
  2215. expect_log_msg_containing("Error while uncompressing data: bad input?");
  2216. done:
  2217. teardown_capture_of_logs();
  2218. tor_free(result);
  2219. tor_free(result2);
  2220. }
  2221. static void
  2222. test_util_decompress_junk(void *arg)
  2223. {
  2224. const char *methodname = arg;
  2225. tt_assert(methodname);
  2226. compress_method_t method = compression_method_get_by_name(methodname);
  2227. tt_int_op(method, OP_NE, UNKNOWN_METHOD);
  2228. if (! tor_compress_supports_method(method)) {
  2229. tt_skip();
  2230. }
  2231. test_util_decompress_junk_impl(method);
  2232. done:
  2233. ;
  2234. }
  2235. /* mock replacement for tor_compress_is_compression_bomb that doesn't
  2236. * believe in compression bombs. */
  2237. static int
  2238. mock_is_never_compression_bomb(size_t in, size_t out)
  2239. {
  2240. (void)in;
  2241. (void) out;
  2242. return 0;
  2243. }
  2244. static void
  2245. test_util_decompress_dos_impl(compress_method_t method)
  2246. {
  2247. char *input;
  2248. char *result = NULL, *result2 = NULL;
  2249. size_t szr, szr2;
  2250. int r;
  2251. const size_t big = 1024*1024;
  2252. /* one megabyte of 0s. */
  2253. input = tor_malloc_zero(big);
  2254. /* Compress it into "result": it should fail. */
  2255. setup_full_capture_of_logs(LOG_WARN);
  2256. r = tor_compress(&result, &szr, input, big, method);
  2257. tt_int_op(r, OP_EQ, -1);
  2258. expect_log_msg_containing(
  2259. "other Tors would think this was a compression bomb");
  2260. teardown_capture_of_logs();
  2261. /* Try again, but this time suppress compression-bomb detection */
  2262. MOCK(tor_compress_is_compression_bomb, mock_is_never_compression_bomb);
  2263. r = tor_compress(&result, &szr, input, big, method);
  2264. UNMOCK(tor_compress_is_compression_bomb);
  2265. tt_int_op(r, OP_EQ, 0);
  2266. tt_ptr_op(result, OP_NE, NULL);
  2267. /* We should refuse to uncomrpess it again, since it looks like a
  2268. * compression bomb. */
  2269. setup_capture_of_logs(LOG_WARN);
  2270. r = tor_uncompress(&result2, &szr2, result, szr, method, 0, LOG_WARN);
  2271. tt_int_op(r, OP_EQ, -1);
  2272. expect_log_msg_containing("bomb; abandoning stream");
  2273. done:
  2274. teardown_capture_of_logs();
  2275. tor_free(input);
  2276. tor_free(result);
  2277. tor_free(result2);
  2278. }
  2279. static void
  2280. test_util_decompress_dos(void *arg)
  2281. {
  2282. const char *methodname = arg;
  2283. tt_assert(methodname);
  2284. compress_method_t method = compression_method_get_by_name(methodname);
  2285. tt_int_op(method, OP_NE, UNKNOWN_METHOD);
  2286. if (! tor_compress_supports_method(method)) {
  2287. tt_skip();
  2288. }
  2289. test_util_decompress_dos_impl(method);
  2290. done:
  2291. ;
  2292. }
  2293. static void
  2294. test_util_gzip_compression_bomb(void *arg)
  2295. {
  2296. /* A 'compression bomb' is a very small object that uncompresses to a huge
  2297. * one. Most compression formats support them, but they can be a DOS vector.
  2298. * In Tor we try not to generate them, and we don't accept them.
  2299. */
  2300. (void) arg;
  2301. size_t one_million = 1<<20;
  2302. char *one_mb = tor_malloc_zero(one_million);
  2303. char *result = NULL;
  2304. size_t result_len = 0;
  2305. tor_compress_state_t *state = NULL;
  2306. /* Make sure we can't produce a compression bomb */
  2307. setup_full_capture_of_logs(LOG_WARN);
  2308. tt_int_op(-1, OP_EQ, tor_compress(&result, &result_len,
  2309. one_mb, one_million,
  2310. ZLIB_METHOD));
  2311. expect_single_log_msg_containing(
  2312. "We compressed something and got an insanely high "
  2313. "compression factor; other Tors would think this "
  2314. "was a compression bomb.");
  2315. teardown_capture_of_logs();
  2316. /* Here's a compression bomb that we made manually. */
  2317. const char compression_bomb[1039] =
  2318. { 0x78, 0xDA, 0xED, 0xC1, 0x31, 0x01, 0x00, 0x00, 0x00, 0xC2,
  2319. 0xA0, 0xF5, 0x4F, 0x6D, 0x08, 0x5F, 0xA0 /* .... */ };
  2320. tt_int_op(-1, OP_EQ, tor_uncompress(&result, &result_len,
  2321. compression_bomb, 1039,
  2322. ZLIB_METHOD, 0, LOG_WARN));
  2323. /* Now try streaming that. */
  2324. state = tor_compress_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
  2325. tor_compress_output_t r;
  2326. const char *inp = compression_bomb;
  2327. size_t inlen = 1039;
  2328. do {
  2329. char *outp = one_mb;
  2330. size_t outleft = 4096; /* small on purpose */
  2331. r = tor_compress_process(state, &outp, &outleft, &inp, &inlen, 0);
  2332. tt_int_op(inlen, OP_NE, 0);
  2333. } while (r == TOR_COMPRESS_BUFFER_FULL);
  2334. tt_int_op(r, OP_EQ, TOR_COMPRESS_ERROR);
  2335. done:
  2336. tor_free(one_mb);
  2337. tor_compress_free(state);
  2338. }
  2339. /** Run unit tests for mmap() wrapper functionality. */
  2340. static void
  2341. test_util_mmap(void *arg)
  2342. {
  2343. char *fname1 = tor_strdup(get_fname("mapped_1"));
  2344. char *fname2 = tor_strdup(get_fname("mapped_2"));
  2345. char *fname3 = tor_strdup(get_fname("mapped_3"));
  2346. const size_t buflen = 17000;
  2347. char *buf = tor_malloc(17000);
  2348. tor_mmap_t *mapping = NULL;
  2349. (void)arg;
  2350. crypto_rand(buf, buflen);
  2351. mapping = tor_mmap_file(fname1);
  2352. tt_ptr_op(mapping, OP_EQ, NULL);
  2353. write_str_to_file(fname1, "Short file.", 1);
  2354. mapping = tor_mmap_file(fname1);
  2355. tt_assert(mapping);
  2356. tt_int_op(mapping->size,OP_EQ, strlen("Short file."));
  2357. tt_str_op(mapping->data,OP_EQ, "Short file.");
  2358. #ifdef _WIN32
  2359. tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
  2360. mapping = NULL;
  2361. tt_assert(unlink(fname1) == 0);
  2362. #else
  2363. /* make sure we can unlink. */
  2364. tt_assert(unlink(fname1) == 0);
  2365. tt_str_op(mapping->data,OP_EQ, "Short file.");
  2366. tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
  2367. mapping = NULL;
  2368. #endif /* defined(_WIN32) */
  2369. /* Now a zero-length file. */
  2370. write_str_to_file(fname1, "", 1);
  2371. mapping = tor_mmap_file(fname1);
  2372. tt_ptr_op(mapping,OP_EQ, NULL);
  2373. tt_int_op(ERANGE,OP_EQ, errno);
  2374. unlink(fname1);
  2375. /* Make sure that we fail to map a no-longer-existent file. */
  2376. mapping = tor_mmap_file(fname1);
  2377. tt_ptr_op(mapping, OP_EQ, NULL);
  2378. /* Now try a big file that stretches across a few pages and isn't aligned */
  2379. write_bytes_to_file(fname2, buf, buflen, 1);
  2380. mapping = tor_mmap_file(fname2);
  2381. tt_assert(mapping);
  2382. tt_int_op(mapping->size,OP_EQ, buflen);
  2383. tt_mem_op(mapping->data,OP_EQ, buf, buflen);
  2384. tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
  2385. mapping = NULL;
  2386. /* Now try a big aligned file. */
  2387. write_bytes_to_file(fname3, buf, 16384, 1);
  2388. mapping = tor_mmap_file(fname3);
  2389. tt_assert(mapping);
  2390. tt_int_op(mapping->size,OP_EQ, 16384);
  2391. tt_mem_op(mapping->data,OP_EQ, buf, 16384);
  2392. tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
  2393. mapping = NULL;
  2394. done:
  2395. unlink(fname1);
  2396. unlink(fname2);
  2397. unlink(fname3);
  2398. tor_free(fname1);
  2399. tor_free(fname2);
  2400. tor_free(fname3);
  2401. tor_free(buf);
  2402. tor_munmap_file(mapping);
  2403. }
  2404. /** Run unit tests for escaping/unescaping data for use by controllers. */
  2405. static void
  2406. test_util_control_formats(void *arg)
  2407. {
  2408. char *out = NULL;
  2409. const char *inp =
  2410. "..This is a test\r\n.of the emergency \n..system.\r\n\rZ.\r\n";
  2411. size_t sz;
  2412. (void)arg;
  2413. sz = read_escaped_data(inp, strlen(inp), &out);
  2414. tt_str_op(out,OP_EQ,
  2415. ".This is a test\nof the emergency \n.system.\n\rZ.\n");
  2416. tt_int_op(sz,OP_EQ, strlen(out));
  2417. done:
  2418. tor_free(out);
  2419. }
  2420. #define test_feq(value1,value2) do { \
  2421. double v1 = (value1), v2=(value2); \
  2422. double tf_diff = v1-v2; \
  2423. double tf_tolerance = ((v1+v2)/2.0)/1e8; \
  2424. if (tf_diff<0) tf_diff=-tf_diff; \
  2425. if (tf_tolerance<0) tf_tolerance=-tf_tolerance; \
  2426. if (tf_diff<tf_tolerance) { \
  2427. TT_BLATHER(("%s ~~ %s: %f ~~ %f",#value1,#value2,v1,v2)); \
  2428. } else { \
  2429. TT_FAIL(("%s ~~ %s: %f != %f",#value1,#value2,v1,v2)); \
  2430. } \
  2431. } while (0)
  2432. static void
  2433. test_util_sscanf(void *arg)
  2434. {
  2435. unsigned u1, u2, u3;
  2436. unsigned long ulng;
  2437. char s1[20], s2[10], s3[10], ch, *huge = NULL;
  2438. int r;
  2439. long lng1,lng2;
  2440. int int1, int2;
  2441. double d1,d2,d3,d4;
  2442. /* Simple tests (malformed patterns, literal matching, ...) */
  2443. (void)arg;
  2444. tt_int_op(-1,OP_EQ, tor_sscanf("123", "%i", &r)); /* %i is not supported */
  2445. tt_int_op(-1,OP_EQ,
  2446. tor_sscanf("wrong", "%5c", s1)); /* %c cannot have a number. */
  2447. tt_int_op(-1,OP_EQ, tor_sscanf("hello", "%s", s1)); /* %s needs a number. */
  2448. /* this will fail because we don't allow widths longer than 9999 */
  2449. {
  2450. huge = tor_malloc(1000000);
  2451. r = tor_sscanf("prettylongstring", "%99999s", huge);
  2452. tor_free(huge);
  2453. tt_int_op(-1,OP_EQ, r);
  2454. }
  2455. #if 0
  2456. /* GCC thinks these two are illegal. */
  2457. test_eq(-1, tor_sscanf("prettylongstring", "%0s", s1));
  2458. test_eq(0, tor_sscanf("prettylongstring", "%10s", NULL));
  2459. #endif
  2460. /* No '%'-strings: always "success" */
  2461. tt_int_op(0,OP_EQ, tor_sscanf("hello world", "hello world"));
  2462. tt_int_op(0,OP_EQ, tor_sscanf("hello world", "good bye"));
  2463. /* Excess data */
  2464. tt_int_op(0,OP_EQ,
  2465. tor_sscanf("hello 3", "%u", &u1)); /* have to match the start */
  2466. tt_int_op(0,OP_EQ, tor_sscanf(" 3 hello", "%u", &u1));
  2467. tt_int_op(0,OP_EQ,
  2468. tor_sscanf(" 3 hello", "%2u", &u1)); /* not even in this case */
  2469. tt_int_op(1,OP_EQ,
  2470. tor_sscanf("3 hello", "%u", &u1)); /* but trailing is alright */
  2471. /* Numbers (ie. %u) */
  2472. tt_int_op(0,OP_EQ,
  2473. tor_sscanf("hello world 3", "hello worlb %u", &u1)); /* d vs b */
  2474. tt_int_op(1,OP_EQ, tor_sscanf("12345", "%u", &u1));
  2475. tt_int_op(12345u,OP_EQ, u1);
  2476. tt_int_op(1,OP_EQ, tor_sscanf("12346 ", "%u", &u1));
  2477. tt_int_op(12346u,OP_EQ, u1);
  2478. tt_int_op(0,OP_EQ, tor_sscanf(" 12347", "%u", &u1));
  2479. tt_int_op(1,OP_EQ, tor_sscanf(" 12348", " %u", &u1));
  2480. tt_int_op(12348u,OP_EQ, u1);
  2481. tt_int_op(1,OP_EQ, tor_sscanf("0", "%u", &u1));
  2482. tt_int_op(0u,OP_EQ, u1);
  2483. tt_int_op(1,OP_EQ, tor_sscanf("0000", "%u", &u2));
  2484. tt_int_op(0u,OP_EQ, u2);
  2485. tt_int_op(0,OP_EQ, tor_sscanf("", "%u", &u1)); /* absent number */
  2486. tt_int_op(0,OP_EQ, tor_sscanf("A", "%u", &u1)); /* bogus number */
  2487. tt_int_op(0,OP_EQ, tor_sscanf("-1", "%u", &u1)); /* negative number */
  2488. /* Numbers with size (eg. %2u) */
  2489. tt_int_op(0,OP_EQ, tor_sscanf("-1", "%2u", &u1));
  2490. tt_int_op(2,OP_EQ, tor_sscanf("123456", "%2u%u", &u1, &u2));
  2491. tt_int_op(12u,OP_EQ, u1);
  2492. tt_int_op(3456u,OP_EQ, u2);
  2493. tt_int_op(1,OP_EQ, tor_sscanf("123456", "%8u", &u1));
  2494. tt_int_op(123456u,OP_EQ, u1);
  2495. tt_int_op(1,OP_EQ, tor_sscanf("123457 ", "%8u", &u1));
  2496. tt_int_op(123457u,OP_EQ, u1);
  2497. tt_int_op(0,OP_EQ, tor_sscanf(" 123456", "%8u", &u1));
  2498. tt_int_op(3,OP_EQ, tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3));
  2499. tt_int_op(12u,OP_EQ, u1);
  2500. tt_int_op(3u,OP_EQ, u2);
  2501. tt_int_op(456u,OP_EQ, u3);
  2502. tt_int_op(3,OP_EQ,
  2503. tor_sscanf("67:8:099", "%2u:%2u:%3u", &u1, &u2, &u3)); /* 0s */
  2504. tt_int_op(67u,OP_EQ, u1);
  2505. tt_int_op(8u,OP_EQ, u2);
  2506. tt_int_op(99u,OP_EQ, u3);
  2507. /* %u does not match space.*/
  2508. tt_int_op(2,OP_EQ, tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3));
  2509. tt_int_op(12u,OP_EQ, u1);
  2510. tt_int_op(3u,OP_EQ, u2);
  2511. /* %u does not match negative numbers. */
  2512. tt_int_op(2,OP_EQ, tor_sscanf("67:8:-9", "%2u:%2u:%3u", &u1, &u2, &u3));
  2513. tt_int_op(67u,OP_EQ, u1);
  2514. tt_int_op(8u,OP_EQ, u2);
  2515. /* Arbitrary amounts of 0-padding are okay */
  2516. tt_int_op(3,OP_EQ, tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u",
  2517. &u1, &u2, &u3));
  2518. tt_int_op(12u,OP_EQ, u1);
  2519. tt_int_op(3u,OP_EQ, u2);
  2520. tt_int_op(99u,OP_EQ, u3);
  2521. /* Hex (ie. %x) */
  2522. tt_int_op(3,OP_EQ,
  2523. tor_sscanf("1234 02aBcdEf ff", "%x %x %x", &u1, &u2, &u3));
  2524. tt_int_op(0x1234,OP_EQ, u1);
  2525. tt_int_op(0x2ABCDEF,OP_EQ, u2);
  2526. tt_int_op(0xFF,OP_EQ, u3);
  2527. /* Width works on %x */
  2528. tt_int_op(3,OP_EQ, tor_sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3));
  2529. tt_int_op(0xf00d,OP_EQ, u1);
  2530. tt_int_op(0xcafe,OP_EQ, u2);
  2531. tt_int_op(444,OP_EQ, u3);
  2532. /* Literal '%' (ie. '%%') */
  2533. tt_int_op(1,OP_EQ, tor_sscanf("99% fresh", "%3u%% fresh", &u1));
  2534. tt_int_op(99,OP_EQ, u1);
  2535. tt_int_op(0,OP_EQ, tor_sscanf("99 fresh", "%% %3u %s", &u1, s1));
  2536. tt_int_op(1,OP_EQ, tor_sscanf("99 fresh", "%3u%% %s", &u1, s1));
  2537. tt_int_op(2,OP_EQ, tor_sscanf("99 fresh", "%3u %5s %%", &u1, s1));
  2538. tt_int_op(99,OP_EQ, u1);
  2539. tt_str_op(s1,OP_EQ, "fresh");
  2540. tt_int_op(1,OP_EQ, tor_sscanf("% boo", "%% %3s", s1));
  2541. tt_str_op("boo",OP_EQ, s1);
  2542. /* Strings (ie. %s) */
  2543. tt_int_op(2,OP_EQ, tor_sscanf("hello", "%3s%7s", s1, s2));
  2544. tt_str_op(s1,OP_EQ, "hel");
  2545. tt_str_op(s2,OP_EQ, "lo");
  2546. tt_int_op(2,OP_EQ, tor_sscanf("WD40", "%2s%u", s3, &u1)); /* %s%u */
  2547. tt_str_op(s3,OP_EQ, "WD");
  2548. tt_int_op(40,OP_EQ, u1);
  2549. tt_int_op(2,OP_EQ, tor_sscanf("WD40", "%3s%u", s3, &u1)); /* %s%u */
  2550. tt_str_op(s3,OP_EQ, "WD4");
  2551. tt_int_op(0,OP_EQ, u1);
  2552. tt_int_op(2,OP_EQ, tor_sscanf("76trombones", "%6u%9s", &u1, s1)); /* %u%s */
  2553. tt_int_op(76,OP_EQ, u1);
  2554. tt_str_op(s1,OP_EQ, "trombones");
  2555. {
  2556. huge = tor_malloc(1000);
  2557. r = tor_sscanf("prettylongstring", "%999s", huge);
  2558. tt_int_op(1,OP_EQ, r);
  2559. tt_str_op(huge,OP_EQ, "prettylongstring");
  2560. tor_free(huge);
  2561. }
  2562. /* %s doesn't eat spaces */
  2563. tt_int_op(2,OP_EQ, tor_sscanf("hello world", "%9s %9s", s1, s2));
  2564. tt_str_op(s1,OP_EQ, "hello");
  2565. tt_str_op(s2,OP_EQ, "world");
  2566. tt_int_op(2,OP_EQ, tor_sscanf("bye world?", "%9s %9s", s1, s2));
  2567. tt_str_op(s1,OP_EQ, "bye");
  2568. tt_str_op(s2,OP_EQ, "");
  2569. tt_int_op(3,OP_EQ,
  2570. tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3)); /* %s can be empty. */
  2571. tt_str_op(s1,OP_EQ, "hi");
  2572. tt_str_op(s2,OP_EQ, "");
  2573. tt_str_op(s3,OP_EQ, "");
  2574. tt_int_op(3,OP_EQ, tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
  2575. tt_int_op(4,OP_EQ,
  2576. tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
  2577. tt_int_op(' ',OP_EQ, ch);
  2578. r = tor_sscanf("12345 -67890 -1", "%d %ld %d", &int1, &lng1, &int2);
  2579. tt_int_op(r,OP_EQ, 3);
  2580. tt_int_op(int1,OP_EQ, 12345);
  2581. tt_int_op(lng1,OP_EQ, -67890);
  2582. tt_int_op(int2,OP_EQ, -1);
  2583. #if SIZEOF_INT == 4
  2584. /* %u */
  2585. /* UINT32_MAX should work */
  2586. tt_int_op(1,OP_EQ, tor_sscanf("4294967295", "%u", &u1));
  2587. tt_int_op(4294967295U,OP_EQ, u1);
  2588. /* But UINT32_MAX + 1 shouldn't work */
  2589. tt_int_op(0,OP_EQ, tor_sscanf("4294967296", "%u", &u1));
  2590. /* but parsing only 9... */
  2591. tt_int_op(1,OP_EQ, tor_sscanf("4294967296", "%9u", &u1));
  2592. tt_int_op(429496729U,OP_EQ, u1);
  2593. /* %x */
  2594. /* UINT32_MAX should work */
  2595. tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFF", "%x", &u1));
  2596. tt_int_op(0xFFFFFFFF,OP_EQ, u1);
  2597. /* But UINT32_MAX + 1 shouldn't work */
  2598. tt_int_op(0,OP_EQ, tor_sscanf("100000000", "%x", &u1));
  2599. /* %d */
  2600. /* INT32_MIN and INT32_MAX should work */
  2601. r = tor_sscanf("-2147483648. 2147483647.", "%d. %d.", &int1, &int2);
  2602. tt_int_op(r,OP_EQ, 2);
  2603. tt_int_op(int1,OP_EQ, -2147483647 - 1);
  2604. tt_int_op(int2,OP_EQ, 2147483647);
  2605. /* But INT32_MIN - 1 and INT32_MAX + 1 shouldn't work */
  2606. r = tor_sscanf("-2147483649.", "%d.", &int1);
  2607. tt_int_op(r,OP_EQ, 0);
  2608. r = tor_sscanf("2147483648.", "%d.", &int1);
  2609. tt_int_op(r,OP_EQ, 0);
  2610. /* and the first failure stops further processing */
  2611. r = tor_sscanf("-2147483648. 2147483648.",
  2612. "%d. %d.", &int1, &int2);
  2613. tt_int_op(r,OP_EQ, 1);
  2614. r = tor_sscanf("-2147483649. 2147483647.",
  2615. "%d. %d.", &int1, &int2);
  2616. tt_int_op(r,OP_EQ, 0);
  2617. r = tor_sscanf("2147483648. -2147483649.",
  2618. "%d. %d.", &int1, &int2);
  2619. tt_int_op(r,OP_EQ, 0);
  2620. #elif SIZEOF_INT == 8
  2621. /* %u */
  2622. /* UINT64_MAX should work */
  2623. tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551615", "%u", &u1));
  2624. tt_int_op(18446744073709551615U,OP_EQ, u1);
  2625. /* But UINT64_MAX + 1 shouldn't work */
  2626. tt_int_op(0,OP_EQ, tor_sscanf("18446744073709551616", "%u", &u1));
  2627. /* but parsing only 19... */
  2628. tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551616", "%19u", &u1));
  2629. tt_int_op(1844674407370955161U,OP_EQ, u1);
  2630. /* %x */
  2631. /* UINT64_MAX should work */
  2632. tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFFFFFFFFFF", "%x", &u1));
  2633. tt_int_op(0xFFFFFFFFFFFFFFFF,OP_EQ, u1);
  2634. /* But UINT64_MAX + 1 shouldn't work */
  2635. tt_int_op(0,OP_EQ, tor_sscanf("10000000000000000", "%x", &u1));
  2636. /* %d */
  2637. /* INT64_MIN and INT64_MAX should work */
  2638. r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
  2639. "%d. %d.", &int1, &int2);
  2640. tt_int_op(r,OP_EQ, 2);
  2641. tt_int_op(int1,OP_EQ, -9223372036854775807 - 1);
  2642. tt_int_op(int2,OP_EQ, 9223372036854775807);
  2643. /* But INT64_MIN - 1 and INT64_MAX + 1 shouldn't work */
  2644. r = tor_sscanf("-9223372036854775809.", "%d.", &int1);
  2645. tt_int_op(r,OP_EQ, 0);
  2646. r = tor_sscanf("9223372036854775808.", "%d.", &int1);
  2647. tt_int_op(r,OP_EQ, 0);
  2648. /* and the first failure stops further processing */
  2649. r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
  2650. "%d. %d.", &int1, &int2);
  2651. tt_int_op(r,OP_EQ, 1);
  2652. r = tor_sscanf("-9223372036854775809. 9223372036854775807.",
  2653. "%d. %d.", &int1, &int2);
  2654. tt_int_op(r,OP_EQ, 0);
  2655. r = tor_sscanf("9223372036854775808. -9223372036854775809.",
  2656. "%d. %d.", &int1, &int2);
  2657. tt_int_op(r,OP_EQ, 0);
  2658. #endif /* SIZEOF_INT == 4 || ... */
  2659. #if SIZEOF_LONG == 4
  2660. /* %lu */
  2661. /* UINT32_MAX should work */
  2662. tt_int_op(1,OP_EQ, tor_sscanf("4294967295", "%lu", &ulng));
  2663. tt_int_op(4294967295UL,OP_EQ, ulng);
  2664. /* But UINT32_MAX + 1 shouldn't work */
  2665. tt_int_op(0,OP_EQ, tor_sscanf("4294967296", "%lu", &ulng));
  2666. /* but parsing only 9... */
  2667. tt_int_op(1,OP_EQ, tor_sscanf("4294967296", "%9lu", &ulng));
  2668. tt_int_op(429496729UL,OP_EQ, ulng);
  2669. /* %lx */
  2670. /* UINT32_MAX should work */
  2671. tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFF", "%lx", &ulng));
  2672. tt_int_op(0xFFFFFFFFUL,OP_EQ, ulng);
  2673. /* But UINT32_MAX + 1 shouldn't work */
  2674. tt_int_op(0,OP_EQ, tor_sscanf("100000000", "%lx", &ulng));
  2675. /* %ld */
  2676. /* INT32_MIN and INT32_MAX should work */
  2677. r = tor_sscanf("-2147483648. 2147483647.", "%ld. %ld.", &lng1, &lng2);
  2678. tt_int_op(r,OP_EQ, 2);
  2679. tt_int_op(lng1,OP_EQ, -2147483647L - 1L);
  2680. tt_int_op(lng2,OP_EQ, 2147483647L);
  2681. /* But INT32_MIN - 1 and INT32_MAX + 1 shouldn't work */
  2682. r = tor_sscanf("-2147483649.", "%ld.", &lng1);
  2683. tt_int_op(r,OP_EQ, 0);
  2684. r = tor_sscanf("2147483648.", "%ld.", &lng1);
  2685. tt_int_op(r,OP_EQ, 0);
  2686. /* and the first failure stops further processing */
  2687. r = tor_sscanf("-2147483648. 2147483648.",
  2688. "%ld. %ld.", &lng1, &lng2);
  2689. tt_int_op(r,OP_EQ, 1);
  2690. r = tor_sscanf("-2147483649. 2147483647.",
  2691. "%ld. %ld.", &lng1, &lng2);
  2692. tt_int_op(r,OP_EQ, 0);
  2693. r = tor_sscanf("2147483648. -2147483649.",
  2694. "%ld. %ld.", &lng1, &lng2);
  2695. tt_int_op(r,OP_EQ, 0);
  2696. #elif SIZEOF_LONG == 8
  2697. /* %lu */
  2698. /* UINT64_MAX should work */
  2699. tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551615", "%lu", &ulng));
  2700. tt_int_op(18446744073709551615UL,OP_EQ, ulng);
  2701. /* But UINT64_MAX + 1 shouldn't work */
  2702. tt_int_op(0,OP_EQ, tor_sscanf("18446744073709551616", "%lu", &ulng));
  2703. /* but parsing only 19... */
  2704. tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551616", "%19lu", &ulng));
  2705. tt_int_op(1844674407370955161UL,OP_EQ, ulng);
  2706. /* %lx */
  2707. /* UINT64_MAX should work */
  2708. tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFFFFFFFFFF", "%lx", &ulng));
  2709. tt_int_op(0xFFFFFFFFFFFFFFFFUL,OP_EQ, ulng);
  2710. /* But UINT64_MAX + 1 shouldn't work */
  2711. tt_int_op(0,OP_EQ, tor_sscanf("10000000000000000", "%lx", &ulng));
  2712. /* %ld */
  2713. /* INT64_MIN and INT64_MAX should work */
  2714. r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
  2715. "%ld. %ld.", &lng1, &lng2);
  2716. tt_int_op(r,OP_EQ, 2);
  2717. tt_int_op(lng1,OP_EQ, -9223372036854775807L - 1L);
  2718. tt_int_op(lng2,OP_EQ, 9223372036854775807L);
  2719. /* But INT64_MIN - 1 and INT64_MAX + 1 shouldn't work */
  2720. r = tor_sscanf("-9223372036854775809.", "%ld.", &lng1);
  2721. tt_int_op(r,OP_EQ, 0);
  2722. r = tor_sscanf("9223372036854775808.", "%ld.", &lng1);
  2723. tt_int_op(r,OP_EQ, 0);
  2724. /* and the first failure stops further processing */
  2725. r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
  2726. "%ld. %ld.", &lng1, &lng2);
  2727. tt_int_op(r,OP_EQ, 1);
  2728. r = tor_sscanf("-9223372036854775809. 9223372036854775807.",
  2729. "%ld. %ld.", &lng1, &lng2);
  2730. tt_int_op(r,OP_EQ, 0);
  2731. r = tor_sscanf("9223372036854775808. -9223372036854775809.",
  2732. "%ld. %ld.", &lng1, &lng2);
  2733. tt_int_op(r,OP_EQ, 0);
  2734. #endif /* SIZEOF_LONG == 4 || ... */
  2735. r = tor_sscanf("123.456 .000007 -900123123.2000787 00003.2",
  2736. "%lf %lf %lf %lf", &d1,&d2,&d3,&d4);
  2737. tt_int_op(r,OP_EQ, 4);
  2738. test_feq(d1, 123.456);
  2739. test_feq(d2, .000007);
  2740. test_feq(d3, -900123123.2000787);
  2741. test_feq(d4, 3.2);
  2742. /* missing float */
  2743. r = tor_sscanf("3 ", "%d %lf", &int1, &d1);
  2744. tt_int_op(r, OP_EQ, 1);
  2745. tt_int_op(int1, OP_EQ, 3);
  2746. /* not a float */
  2747. r = tor_sscanf("999 notafloat", "%d %lf", &int1, &d1);
  2748. tt_int_op(r, OP_EQ, 1);
  2749. tt_int_op(int1, OP_EQ, 999);
  2750. /* %s but no buffer. */
  2751. char *nullbuf = NULL;
  2752. r = tor_sscanf("hello", "%3s", nullbuf);
  2753. tt_int_op(r, OP_EQ, 0);
  2754. done:
  2755. tor_free(huge);
  2756. }
  2757. #define tt_char_op(a,op,b) tt_assert_op_type(a,op,b,char,"%c")
  2758. #define tt_ci_char_op(a,op,b) \
  2759. tt_char_op(TOR_TOLOWER((int)a),op,TOR_TOLOWER((int)b))
  2760. #ifndef HAVE_STRNLEN
  2761. static size_t
  2762. strnlen(const char *s, size_t len)
  2763. {
  2764. const char *p = memchr(s, 0, len);
  2765. if (!p)
  2766. return len;
  2767. return p - s;
  2768. }
  2769. #endif /* !defined(HAVE_STRNLEN) */
  2770. static void
  2771. test_util_format_time_interval(void *arg)
  2772. {
  2773. /* use the same sized buffer and integers as tor uses */
  2774. #define DBUF_SIZE 64
  2775. char dbuf[DBUF_SIZE];
  2776. #define T_ "%ld"
  2777. long sec, min, hour, day;
  2778. /* we don't care about the exact spelling of the
  2779. * second(s), minute(s), hour(s), day(s) labels */
  2780. #define LABEL_SIZE 21
  2781. #define L_ "%20s"
  2782. char label_s[LABEL_SIZE];
  2783. char label_m[LABEL_SIZE];
  2784. char label_h[LABEL_SIZE];
  2785. char label_d[LABEL_SIZE];
  2786. #define TL_ T_ " " L_
  2787. int r;
  2788. (void)arg;
  2789. /* In these tests, we're not picky about
  2790. * spelling or abbreviations */
  2791. /* seconds: 0, 1, 9, 10, 59 */
  2792. /* ignore exact spelling of "second(s)"*/
  2793. format_time_interval(dbuf, sizeof(dbuf), 0);
  2794. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2795. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  2796. tt_int_op(r,OP_EQ, 2);
  2797. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2798. tt_int_op(sec,OP_EQ, 0);
  2799. format_time_interval(dbuf, sizeof(dbuf), 1);
  2800. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2801. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  2802. tt_int_op(r,OP_EQ, 2);
  2803. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2804. tt_int_op(sec,OP_EQ, 1);
  2805. format_time_interval(dbuf, sizeof(dbuf), 10);
  2806. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2807. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  2808. tt_int_op(r,OP_EQ, 2);
  2809. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2810. tt_int_op(sec,OP_EQ, 10);
  2811. format_time_interval(dbuf, sizeof(dbuf), 59);
  2812. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2813. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  2814. tt_int_op(r,OP_EQ, 2);
  2815. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2816. tt_int_op(sec,OP_EQ, 59);
  2817. /* negative seconds are reported as their absolute value */
  2818. format_time_interval(dbuf, sizeof(dbuf), -4);
  2819. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2820. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  2821. tt_int_op(r,OP_EQ, 2);
  2822. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2823. tt_int_op(sec,OP_EQ, 4);
  2824. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2825. format_time_interval(dbuf, sizeof(dbuf), -32);
  2826. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2827. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  2828. tt_int_op(r,OP_EQ, 2);
  2829. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2830. tt_int_op(sec,OP_EQ, 32);
  2831. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2832. /* minutes: 1:00, 1:01, 1:59, 2:00, 2:01, 59:59 */
  2833. /* ignore trailing "0 second(s)", if present */
  2834. format_time_interval(dbuf, sizeof(dbuf), 60);
  2835. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2836. r = tor_sscanf(dbuf, TL_, &min, label_m);
  2837. tt_int_op(r,OP_EQ, 2);
  2838. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2839. tt_int_op(min,OP_EQ, 1);
  2840. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2841. /* ignore exact spelling of "minute(s)," and "second(s)" */
  2842. format_time_interval(dbuf, sizeof(dbuf), 60 + 1);
  2843. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2844. r = tor_sscanf(dbuf, TL_ " " TL_,
  2845. &min, label_m, &sec, label_s);
  2846. tt_int_op(r,OP_EQ, 4);
  2847. tt_int_op(min,OP_EQ, 1);
  2848. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2849. tt_int_op(sec,OP_EQ, 1);
  2850. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2851. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2852. format_time_interval(dbuf, sizeof(dbuf), 60*2 - 1);
  2853. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2854. r = tor_sscanf(dbuf, TL_ " " TL_,
  2855. &min, label_m, &sec, label_s);
  2856. tt_int_op(r,OP_EQ, 4);
  2857. tt_int_op(min,OP_EQ, 1);
  2858. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2859. tt_int_op(sec,OP_EQ, 59);
  2860. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2861. /* ignore trailing "0 second(s)", if present */
  2862. format_time_interval(dbuf, sizeof(dbuf), 60*2);
  2863. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2864. r = tor_sscanf(dbuf, TL_, &min, label_m);
  2865. tt_int_op(r,OP_EQ, 2);
  2866. tt_int_op(min,OP_EQ, 2);
  2867. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2868. /* ignore exact spelling of "minute(s)," and "second(s)" */
  2869. format_time_interval(dbuf, sizeof(dbuf), 60*2 + 1);
  2870. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2871. r = tor_sscanf(dbuf, TL_ " " TL_,
  2872. &min, label_m, &sec, label_s);
  2873. tt_int_op(r,OP_EQ, 4);
  2874. tt_int_op(min,OP_EQ, 2);
  2875. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2876. tt_int_op(sec,OP_EQ, 1);
  2877. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2878. format_time_interval(dbuf, sizeof(dbuf), 60*60 - 1);
  2879. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2880. r = tor_sscanf(dbuf, TL_ " " TL_,
  2881. &min, label_m, &sec, label_s);
  2882. tt_int_op(r,OP_EQ, 4);
  2883. tt_int_op(min,OP_EQ, 59);
  2884. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2885. tt_int_op(sec,OP_EQ, 59);
  2886. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2887. /* negative minutes are reported as their absolute value */
  2888. /* ignore trailing "0 second(s)", if present */
  2889. format_time_interval(dbuf, sizeof(dbuf), -3*60);
  2890. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2891. r = tor_sscanf(dbuf, TL_, &min, label_m);
  2892. tt_int_op(r,OP_EQ, 2);
  2893. tt_int_op(min,OP_EQ, 3);
  2894. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2895. /* ignore exact spelling of "minute(s)," and "second(s)" */
  2896. format_time_interval(dbuf, sizeof(dbuf), -96);
  2897. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2898. r = tor_sscanf(dbuf, TL_ " " TL_,
  2899. &min, label_m, &sec, label_s);
  2900. tt_int_op(r,OP_EQ, 4);
  2901. tt_int_op(min,OP_EQ, 1);
  2902. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2903. tt_int_op(sec,OP_EQ, 36);
  2904. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2905. format_time_interval(dbuf, sizeof(dbuf), -2815);
  2906. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2907. r = tor_sscanf(dbuf, TL_ " " TL_,
  2908. &min, label_m, &sec, label_s);
  2909. tt_int_op(r,OP_EQ, 4);
  2910. tt_int_op(min,OP_EQ, 46);
  2911. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2912. tt_int_op(sec,OP_EQ, 55);
  2913. tt_ci_char_op(label_s[0],OP_EQ, 's');
  2914. /* hours: 1:00, 1:00:01, 1:01, 23:59, 23:59:59 */
  2915. /* always ignore trailing seconds, if present */
  2916. /* ignore trailing "0 minute(s)" etc., if present */
  2917. format_time_interval(dbuf, sizeof(dbuf), 60*60);
  2918. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2919. r = tor_sscanf(dbuf, TL_, &hour, label_h);
  2920. tt_int_op(r,OP_EQ, 2);
  2921. tt_int_op(hour,OP_EQ, 1);
  2922. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2923. format_time_interval(dbuf, sizeof(dbuf), 60*60 + 1);
  2924. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2925. r = tor_sscanf(dbuf, TL_, &hour, label_h);
  2926. tt_int_op(r,OP_EQ, 2);
  2927. tt_int_op(hour,OP_EQ, 1);
  2928. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2929. /* ignore exact spelling of "hour(s)," etc. */
  2930. format_time_interval(dbuf, sizeof(dbuf), 60*60 + 60);
  2931. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2932. r = tor_sscanf(dbuf, TL_ " " TL_,
  2933. &hour, label_h, &min, label_m);
  2934. tt_int_op(r,OP_EQ, 4);
  2935. tt_int_op(hour,OP_EQ, 1);
  2936. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2937. tt_int_op(min,OP_EQ, 1);
  2938. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2939. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 - 60);
  2940. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2941. r = tor_sscanf(dbuf, TL_ " " TL_,
  2942. &hour, label_h, &min, label_m);
  2943. tt_int_op(r,OP_EQ, 4);
  2944. tt_int_op(hour,OP_EQ, 23);
  2945. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2946. tt_int_op(min,OP_EQ, 59);
  2947. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2948. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 - 1);
  2949. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2950. r = tor_sscanf(dbuf, TL_ " " TL_,
  2951. &hour, label_h, &min, label_m);
  2952. tt_int_op(r,OP_EQ, 4);
  2953. tt_int_op(hour,OP_EQ, 23);
  2954. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2955. tt_int_op(min,OP_EQ, 59);
  2956. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2957. /* negative hours are reported as their absolute value */
  2958. /* ignore exact spelling of "hour(s)," etc., if present */
  2959. format_time_interval(dbuf, sizeof(dbuf), -2*60*60);
  2960. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2961. r = tor_sscanf(dbuf, TL_, &hour, label_h);
  2962. tt_int_op(r,OP_EQ, 2);
  2963. tt_int_op(hour,OP_EQ, 2);
  2964. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2965. format_time_interval(dbuf, sizeof(dbuf), -75804);
  2966. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2967. r = tor_sscanf(dbuf, TL_ " " TL_,
  2968. &hour, label_h, &min, label_m);
  2969. tt_int_op(r,OP_EQ, 4);
  2970. tt_int_op(hour,OP_EQ, 21);
  2971. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  2972. tt_int_op(min,OP_EQ, 3);
  2973. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  2974. /* days: 1:00, 1:00:00:01, 1:00:01, 1:01 */
  2975. /* always ignore trailing seconds, if present */
  2976. /* ignore trailing "0 hours(s)" etc., if present */
  2977. format_time_interval(dbuf, sizeof(dbuf), 24*60*60);
  2978. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2979. r = tor_sscanf(dbuf, TL_, &day, label_d);
  2980. tt_int_op(r,OP_EQ, 2);
  2981. tt_int_op(day,OP_EQ, 1);
  2982. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  2983. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 1);
  2984. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2985. r = tor_sscanf(dbuf, TL_, &day, label_d);
  2986. tt_int_op(r,OP_EQ, 2);
  2987. tt_int_op(day,OP_EQ, 1);
  2988. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  2989. /* ignore exact spelling of "days(s)," etc. */
  2990. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 60);
  2991. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  2992. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2993. &day, label_d, &hour, label_h, &min, label_m);
  2994. if (r == -1) {
  2995. /* ignore 0 hours(s), if present */
  2996. r = tor_sscanf(dbuf, TL_ " " TL_,
  2997. &day, label_d, &min, label_m);
  2998. }
  2999. tt_assert(r == 4 || r == 6);
  3000. tt_int_op(day,OP_EQ, 1);
  3001. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3002. if (r == 6) {
  3003. tt_int_op(hour,OP_EQ, 0);
  3004. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3005. }
  3006. tt_int_op(min,OP_EQ, 1);
  3007. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3008. /* ignore trailing "0 minutes(s)" etc., if present */
  3009. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 60*60);
  3010. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3011. r = tor_sscanf(dbuf, TL_ " " TL_,
  3012. &day, label_d, &hour, label_h);
  3013. tt_int_op(r,OP_EQ, 4);
  3014. tt_int_op(day,OP_EQ, 1);
  3015. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3016. tt_int_op(hour,OP_EQ, 1);
  3017. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3018. /* negative days are reported as their absolute value */
  3019. format_time_interval(dbuf, sizeof(dbuf), -21936184);
  3020. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3021. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3022. &day, label_d, &hour, label_h, &min, label_m);
  3023. tt_int_op(r,OP_EQ, 6);
  3024. tt_int_op(day,OP_EQ, 253);
  3025. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3026. tt_int_op(hour,OP_EQ, 21);
  3027. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3028. tt_int_op(min,OP_EQ, 23);
  3029. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3030. /* periods > 1 year are reported in days (warn?) */
  3031. /* ignore exact spelling of "days(s)," etc., if present */
  3032. format_time_interval(dbuf, sizeof(dbuf), 758635154);
  3033. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3034. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3035. &day, label_d, &hour, label_h, &min, label_m);
  3036. tt_int_op(r,OP_EQ, 6);
  3037. tt_int_op(day,OP_EQ, 8780);
  3038. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3039. tt_int_op(hour,OP_EQ, 11);
  3040. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3041. tt_int_op(min,OP_EQ, 59);
  3042. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3043. /* negative periods > 1 year are reported in days (warn?) */
  3044. format_time_interval(dbuf, sizeof(dbuf), -1427014922);
  3045. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3046. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3047. &day, label_d, &hour, label_h, &min, label_m);
  3048. tt_int_op(r,OP_EQ, 6);
  3049. tt_int_op(day,OP_EQ, 16516);
  3050. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3051. tt_int_op(hour,OP_EQ, 9);
  3052. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3053. tt_int_op(min,OP_EQ, 2);
  3054. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3055. #if SIZEOF_LONG == 4 || SIZEOF_LONG == 8
  3056. /* We can try INT32_MIN/MAX */
  3057. /* Always ignore second(s) */
  3058. /* INT32_MAX */
  3059. format_time_interval(dbuf, sizeof(dbuf), 2147483647);
  3060. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3061. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3062. &day, label_d, &hour, label_h, &min, label_m);
  3063. tt_int_op(r,OP_EQ, 6);
  3064. tt_int_op(day,OP_EQ, 24855);
  3065. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3066. tt_int_op(hour,OP_EQ, 3);
  3067. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3068. tt_int_op(min,OP_EQ, 14);
  3069. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3070. /* and 7 seconds - ignored */
  3071. /* INT32_MIN: check that we get the absolute value of interval,
  3072. * which doesn't actually fit in int32_t.
  3073. * We expect INT32_MAX or INT32_MAX + 1 with 64 bit longs */
  3074. format_time_interval(dbuf, sizeof(dbuf), -2147483647L - 1L);
  3075. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3076. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3077. &day, label_d, &hour, label_h, &min, label_m);
  3078. tt_int_op(r,OP_EQ, 6);
  3079. tt_int_op(day,OP_EQ, 24855);
  3080. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3081. tt_int_op(hour,OP_EQ, 3);
  3082. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3083. tt_int_op(min,OP_EQ, 14);
  3084. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3085. /* and 7 or 8 seconds - ignored */
  3086. #endif /* SIZEOF_LONG == 4 || SIZEOF_LONG == 8 */
  3087. #if SIZEOF_LONG == 8
  3088. /* We can try INT64_MIN/MAX */
  3089. /* Always ignore second(s) */
  3090. /* INT64_MAX */
  3091. format_time_interval(dbuf, sizeof(dbuf), 9223372036854775807L);
  3092. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3093. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3094. &day, label_d, &hour, label_h, &min, label_m);
  3095. tt_int_op(r,OP_EQ, 6);
  3096. tt_int_op(day,OP_EQ, 106751991167300L);
  3097. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3098. tt_int_op(hour,OP_EQ, 15);
  3099. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3100. tt_int_op(min,OP_EQ, 30);
  3101. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3102. /* and 7 seconds - ignored */
  3103. /* INT64_MIN: check that we get the absolute value of interval,
  3104. * which doesn't actually fit in int64_t.
  3105. * We expect INT64_MAX */
  3106. format_time_interval(dbuf, sizeof(dbuf),
  3107. -9223372036854775807L - 1L);
  3108. tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
  3109. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  3110. &day, label_d, &hour, label_h, &min, label_m);
  3111. tt_int_op(r,OP_EQ, 6);
  3112. tt_int_op(day,OP_EQ, 106751991167300L);
  3113. tt_ci_char_op(label_d[0],OP_EQ, 'd');
  3114. tt_int_op(hour,OP_EQ, 15);
  3115. tt_ci_char_op(label_h[0],OP_EQ, 'h');
  3116. tt_int_op(min,OP_EQ, 30);
  3117. tt_ci_char_op(label_m[0],OP_EQ, 'm');
  3118. /* and 7 or 8 seconds - ignored */
  3119. #endif /* SIZEOF_LONG == 8 */
  3120. done:
  3121. ;
  3122. }
  3123. #undef tt_char_op
  3124. #undef tt_ci_char_op
  3125. #undef DBUF_SIZE
  3126. #undef T_
  3127. #undef LABEL_SIZE
  3128. #undef L_
  3129. #undef TL_
  3130. static void
  3131. test_util_path_is_relative(void *arg)
  3132. {
  3133. /* OS-independent tests */
  3134. (void)arg;
  3135. tt_int_op(1,OP_EQ, path_is_relative(""));
  3136. tt_int_op(1,OP_EQ, path_is_relative("dir"));
  3137. tt_int_op(1,OP_EQ, path_is_relative("dir/"));
  3138. tt_int_op(1,OP_EQ, path_is_relative("./dir"));
  3139. tt_int_op(1,OP_EQ, path_is_relative("../dir"));
  3140. tt_int_op(0,OP_EQ, path_is_relative("/"));
  3141. tt_int_op(0,OP_EQ, path_is_relative("/dir"));
  3142. tt_int_op(0,OP_EQ, path_is_relative("/dir/"));
  3143. /* Windows */
  3144. #ifdef _WIN32
  3145. /* I don't have Windows so I can't test this, hence the "#ifdef
  3146. 0". These are tests that look useful, so please try to get them
  3147. running and uncomment if it all works as it should */
  3148. tt_int_op(1,OP_EQ, path_is_relative("dir"));
  3149. tt_int_op(1,OP_EQ, path_is_relative("dir\\"));
  3150. tt_int_op(1,OP_EQ, path_is_relative("dir\\a:"));
  3151. tt_int_op(1,OP_EQ, path_is_relative("dir\\a:\\"));
  3152. tt_int_op(1,OP_EQ, path_is_relative("http:\\dir"));
  3153. tt_int_op(0,OP_EQ, path_is_relative("\\dir"));
  3154. tt_int_op(0,OP_EQ, path_is_relative("a:\\dir"));
  3155. tt_int_op(0,OP_EQ, path_is_relative("z:\\dir"));
  3156. #endif /* defined(_WIN32) */
  3157. done:
  3158. ;
  3159. }
  3160. /** Run unittests for memory area allocator */
  3161. static void
  3162. test_util_memarea(void *arg)
  3163. {
  3164. memarea_t *area = memarea_new();
  3165. char *p1, *p2, *p3, *p1_orig;
  3166. void *malloced_ptr = NULL;
  3167. int i;
  3168. #ifdef DISABLE_MEMORY_SENTINELS
  3169. /* If memory sentinels are disabled, this whole module is just an alias for
  3170. malloc(), which is free to lay out memory most any way it wants. */
  3171. if (1)
  3172. tt_skip();
  3173. #endif /* defined(DISABLE_MEMORY_SENTINELS) */
  3174. (void)arg;
  3175. tt_assert(area);
  3176. p1_orig = p1 = memarea_alloc(area,64);
  3177. p2 = memarea_alloc_zero(area,52);
  3178. p3 = memarea_alloc(area,11);
  3179. tt_assert(memarea_owns_ptr(area, p1));
  3180. tt_assert(memarea_owns_ptr(area, p2));
  3181. tt_assert(memarea_owns_ptr(area, p3));
  3182. /* Make sure we left enough space. */
  3183. tt_assert(p1+64 <= p2);
  3184. tt_assert(p2+52 <= p3);
  3185. /* Make sure we aligned. */
  3186. tt_int_op(((uintptr_t)p1) % sizeof(void*),OP_EQ, 0);
  3187. tt_int_op(((uintptr_t)p2) % sizeof(void*),OP_EQ, 0);
  3188. tt_int_op(((uintptr_t)p3) % sizeof(void*),OP_EQ, 0);
  3189. tt_assert(!memarea_owns_ptr(area, p3+8192));
  3190. tt_assert(!memarea_owns_ptr(area, p3+30));
  3191. tt_assert(tor_mem_is_zero(p2, 52));
  3192. /* Make sure we don't overalign. */
  3193. p1 = memarea_alloc(area, 1);
  3194. p2 = memarea_alloc(area, 1);
  3195. tt_ptr_op(p1+sizeof(void*),OP_EQ, p2);
  3196. {
  3197. malloced_ptr = tor_malloc(64);
  3198. tt_assert(!memarea_owns_ptr(area, malloced_ptr));
  3199. tor_free(malloced_ptr);
  3200. }
  3201. /* memarea_memdup */
  3202. {
  3203. malloced_ptr = tor_malloc(64);
  3204. crypto_rand((char*)malloced_ptr, 64);
  3205. p1 = memarea_memdup(area, malloced_ptr, 64);
  3206. tt_assert(p1 != malloced_ptr);
  3207. tt_mem_op(p1,OP_EQ, malloced_ptr, 64);
  3208. tor_free(malloced_ptr);
  3209. }
  3210. /* memarea_strdup. */
  3211. p1 = memarea_strdup(area,"");
  3212. p2 = memarea_strdup(area, "abcd");
  3213. tt_assert(p1);
  3214. tt_assert(p2);
  3215. tt_str_op(p1,OP_EQ, "");
  3216. tt_str_op(p2,OP_EQ, "abcd");
  3217. /* memarea_strndup. */
  3218. {
  3219. const char *s = "Ad ogni porta batte la morte e grida: il nome!";
  3220. /* (From Turandot, act 3.) */
  3221. size_t len = strlen(s);
  3222. p1 = memarea_strndup(area, s, 1000);
  3223. p2 = memarea_strndup(area, s, 10);
  3224. tt_str_op(p1,OP_EQ, s);
  3225. tt_assert(p2 >= p1 + len + 1);
  3226. tt_mem_op(s,OP_EQ, p2, 10);
  3227. tt_int_op(p2[10],OP_EQ, '\0');
  3228. p3 = memarea_strndup(area, s, len);
  3229. tt_str_op(p3,OP_EQ, s);
  3230. p3 = memarea_strndup(area, s, len-1);
  3231. tt_mem_op(s,OP_EQ, p3, len-1);
  3232. tt_int_op(p3[len-1],OP_EQ, '\0');
  3233. }
  3234. memarea_clear(area);
  3235. p1 = memarea_alloc(area, 1);
  3236. tt_ptr_op(p1,OP_EQ, p1_orig);
  3237. memarea_clear(area);
  3238. size_t total = 0, initial_allocation, allocation2, dummy;
  3239. memarea_get_stats(area, &initial_allocation, &dummy);
  3240. /* Check for running over an area's size. */
  3241. for (i = 0; i < 4096; ++i) {
  3242. size_t n = crypto_rand_int(6);
  3243. p1 = memarea_alloc(area, n);
  3244. total += n;
  3245. tt_assert(memarea_owns_ptr(area, p1));
  3246. }
  3247. memarea_assert_ok(area);
  3248. memarea_get_stats(area, &allocation2, &dummy);
  3249. /* Make sure we can allocate a too-big object. */
  3250. p1 = memarea_alloc_zero(area, 9000);
  3251. p2 = memarea_alloc_zero(area, 16);
  3252. total += 9000;
  3253. total += 16;
  3254. tt_assert(memarea_owns_ptr(area, p1));
  3255. tt_assert(memarea_owns_ptr(area, p2));
  3256. /* Now test stats... */
  3257. size_t allocated = 0, used = 0;
  3258. memarea_get_stats(area, &allocated, &used);
  3259. tt_int_op(used, OP_LE, allocated);
  3260. tt_int_op(used, OP_GE, total); /* not EQ, because of alignment and headers*/
  3261. tt_int_op(allocated, OP_GT, allocation2);
  3262. tt_int_op(allocation2, OP_GT, initial_allocation);
  3263. memarea_clear(area);
  3264. memarea_get_stats(area, &allocated, &used);
  3265. tt_int_op(used, OP_LT, 128); /* Not 0, because of header */
  3266. tt_int_op(allocated, OP_EQ, initial_allocation);
  3267. done:
  3268. memarea_drop_all(area);
  3269. tor_free(malloced_ptr);
  3270. }
  3271. /** Run unit tests for utility functions to get file names relative to
  3272. * the data directory. */
  3273. static void
  3274. test_util_datadir(void *arg)
  3275. {
  3276. char buf[1024];
  3277. char *f = NULL;
  3278. char *temp_dir = NULL;
  3279. (void)arg;
  3280. temp_dir = get_datadir_fname(NULL);
  3281. f = get_datadir_fname("state");
  3282. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
  3283. tt_str_op(f,OP_EQ, buf);
  3284. tor_free(f);
  3285. f = get_datadir_fname2("cache", "thingy");
  3286. tor_snprintf(buf, sizeof(buf),
  3287. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
  3288. tt_str_op(f,OP_EQ, buf);
  3289. tor_free(f);
  3290. f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
  3291. tor_snprintf(buf, sizeof(buf),
  3292. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
  3293. tt_str_op(f,OP_EQ, buf);
  3294. tor_free(f);
  3295. f = get_datadir_fname_suffix("cache", ".foo");
  3296. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
  3297. temp_dir);
  3298. tt_str_op(f,OP_EQ, buf);
  3299. done:
  3300. tor_free(f);
  3301. tor_free(temp_dir);
  3302. }
  3303. static void
  3304. test_util_strtok(void *arg)
  3305. {
  3306. char buf[128];
  3307. char buf2[128];
  3308. int i;
  3309. char *cp1, *cp2;
  3310. (void)arg;
  3311. for (i = 0; i < 3; i++) {
  3312. const char *pad1="", *pad2="";
  3313. switch (i) {
  3314. case 0:
  3315. break;
  3316. case 1:
  3317. pad1 = " ";
  3318. pad2 = "!";
  3319. break;
  3320. case 2:
  3321. pad1 = " ";
  3322. pad2 = ";!";
  3323. break;
  3324. }
  3325. tor_snprintf(buf, sizeof(buf), "%s", pad1);
  3326. tor_snprintf(buf2, sizeof(buf2), "%s", pad2);
  3327. tt_ptr_op(tor_strtok_r_impl(buf, " ", &cp1), OP_EQ, NULL);
  3328. tt_ptr_op(tor_strtok_r_impl(buf2, ".!..;!", &cp2), OP_EQ, NULL);
  3329. tor_snprintf(buf, sizeof(buf),
  3330. "%sGraved on the dark in gestures of descent%s", pad1, pad1);
  3331. tor_snprintf(buf2, sizeof(buf2),
  3332. "%sthey.seemed;;their!.own;most.perfect;monument%s",pad2,pad2);
  3333. /* -- "Year's End", Richard Wilbur */
  3334. tt_str_op("Graved",OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
  3335. tt_str_op("they",OP_EQ, tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  3336. #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
  3337. #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
  3338. tt_str_op("on",OP_EQ, S1());
  3339. tt_str_op("the",OP_EQ, S1());
  3340. tt_str_op("dark",OP_EQ, S1());
  3341. tt_str_op("seemed",OP_EQ, S2());
  3342. tt_str_op("their",OP_EQ, S2());
  3343. tt_str_op("own",OP_EQ, S2());
  3344. tt_str_op("in",OP_EQ, S1());
  3345. tt_str_op("gestures",OP_EQ, S1());
  3346. tt_str_op("of",OP_EQ, S1());
  3347. tt_str_op("most",OP_EQ, S2());
  3348. tt_str_op("perfect",OP_EQ, S2());
  3349. tt_str_op("descent",OP_EQ, S1());
  3350. tt_str_op("monument",OP_EQ, S2());
  3351. tt_ptr_op(NULL,OP_EQ, S1());
  3352. tt_ptr_op(NULL,OP_EQ, S2());
  3353. }
  3354. buf[0] = 0;
  3355. tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
  3356. tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, "!", &cp1));
  3357. strlcpy(buf, "Howdy!", sizeof(buf));
  3358. tt_str_op("Howdy",OP_EQ, tor_strtok_r_impl(buf, "!", &cp1));
  3359. tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(NULL, "!", &cp1));
  3360. strlcpy(buf, " ", sizeof(buf));
  3361. tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
  3362. strlcpy(buf, " ", sizeof(buf));
  3363. tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
  3364. strlcpy(buf, "something ", sizeof(buf));
  3365. tt_str_op("something",OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
  3366. tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(NULL, ";", &cp1));
  3367. done:
  3368. ;
  3369. }
  3370. static void
  3371. test_util_find_str_at_start_of_line(void *ptr)
  3372. {
  3373. const char *long_string =
  3374. "howdy world. how are you? i hope it's fine.\n"
  3375. "hello kitty\n"
  3376. "third line";
  3377. char *line2 = strchr(long_string,'\n')+1;
  3378. char *line3 = strchr(line2,'\n')+1;
  3379. const char *short_string = "hello kitty\n"
  3380. "second line\n";
  3381. char *short_line2 = strchr(short_string,'\n')+1;
  3382. (void)ptr;
  3383. tt_ptr_op(long_string,OP_EQ, find_str_at_start_of_line(long_string, ""));
  3384. tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(short_string, "nonsense"));
  3385. tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "nonsense"));
  3386. tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "\n"));
  3387. tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "how "));
  3388. tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "kitty"));
  3389. tt_ptr_op(long_string,OP_EQ, find_str_at_start_of_line(long_string, "h"));
  3390. tt_ptr_op(long_string,OP_EQ, find_str_at_start_of_line(long_string, "how"));
  3391. tt_ptr_op(line2,OP_EQ, find_str_at_start_of_line(long_string, "he"));
  3392. tt_ptr_op(line2,OP_EQ, find_str_at_start_of_line(long_string, "hell"));
  3393. tt_ptr_op(line2,OP_EQ, find_str_at_start_of_line(long_string, "hello k"));
  3394. tt_ptr_op(line2,OP_EQ,
  3395. find_str_at_start_of_line(long_string, "hello kitty\n"));
  3396. tt_ptr_op(line2,OP_EQ,
  3397. find_str_at_start_of_line(long_string, "hello kitty\nt"));
  3398. tt_ptr_op(line3,OP_EQ, find_str_at_start_of_line(long_string, "third"));
  3399. tt_ptr_op(line3,OP_EQ, find_str_at_start_of_line(long_string, "third line"));
  3400. tt_ptr_op(NULL, OP_EQ,
  3401. find_str_at_start_of_line(long_string, "third line\n"));
  3402. tt_ptr_op(short_line2,OP_EQ, find_str_at_start_of_line(short_string,
  3403. "second line\n"));
  3404. done:
  3405. ;
  3406. }
  3407. static void
  3408. test_util_string_is_C_identifier(void *ptr)
  3409. {
  3410. (void)ptr;
  3411. tt_int_op(1,OP_EQ, string_is_C_identifier("string_is_C_identifier"));
  3412. tt_int_op(1,OP_EQ, string_is_C_identifier("_string_is_C_identifier"));
  3413. tt_int_op(1,OP_EQ, string_is_C_identifier("_"));
  3414. tt_int_op(1,OP_EQ, string_is_C_identifier("i"));
  3415. tt_int_op(1,OP_EQ, string_is_C_identifier("_____"));
  3416. tt_int_op(1,OP_EQ, string_is_C_identifier("__00__"));
  3417. tt_int_op(1,OP_EQ, string_is_C_identifier("__init__"));
  3418. tt_int_op(1,OP_EQ, string_is_C_identifier("_0"));
  3419. tt_int_op(1,OP_EQ, string_is_C_identifier("_0string_is_C_identifier"));
  3420. tt_int_op(1,OP_EQ, string_is_C_identifier("_0"));
  3421. tt_int_op(0,OP_EQ, string_is_C_identifier("0_string_is_C_identifier"));
  3422. tt_int_op(0,OP_EQ, string_is_C_identifier("0"));
  3423. tt_int_op(0,OP_EQ, string_is_C_identifier(""));
  3424. tt_int_op(0,OP_EQ, string_is_C_identifier(";"));
  3425. tt_int_op(0,OP_EQ, string_is_C_identifier("i;"));
  3426. tt_int_op(0,OP_EQ, string_is_C_identifier("_;"));
  3427. tt_int_op(0,OP_EQ, string_is_C_identifier("í"));
  3428. tt_int_op(0,OP_EQ, string_is_C_identifier("ñ"));
  3429. done:
  3430. ;
  3431. }
  3432. static void
  3433. test_util_string_is_utf8(void *ptr)
  3434. {
  3435. (void)ptr;
  3436. tt_int_op(1, OP_EQ, string_is_utf8(NULL, 0));
  3437. tt_int_op(1, OP_EQ, string_is_utf8("", 1));
  3438. tt_int_op(1, OP_EQ, string_is_utf8("\uFEFF", 3));
  3439. tt_int_op(1, OP_EQ, string_is_utf8("\uFFFE", 3));
  3440. tt_int_op(1, OP_EQ, string_is_utf8("ascii\x7f\n", 7));
  3441. tt_int_op(1, OP_EQ, string_is_utf8("Risqu\u00e9=1", 9));
  3442. /* Test the utf8_no_bom function */
  3443. tt_int_op(0, OP_EQ, string_is_utf8_no_bom("\uFEFF", 3));
  3444. tt_int_op(0, OP_EQ, string_is_utf8_no_bom("\uFFFE", 3));
  3445. tt_int_op(0, OP_EQ, string_is_utf8_no_bom("\uFEFFlove", 7));
  3446. tt_int_op(1, OP_EQ, string_is_utf8_no_bom("loveandrespect",
  3447. strlen("loveandrespect")));
  3448. // Validate exactly 'len' bytes.
  3449. tt_int_op(0, OP_EQ, string_is_utf8("\0\x80", 2));
  3450. tt_int_op(0, OP_EQ, string_is_utf8("Risqu\u00e9=1", 6));
  3451. // Reject sequences with missing bytes.
  3452. tt_int_op(0, OP_EQ, string_is_utf8("\x80", 1));
  3453. tt_int_op(0, OP_EQ, string_is_utf8("\xc2", 1));
  3454. tt_int_op(0, OP_EQ, string_is_utf8("\xc2 ", 2));
  3455. tt_int_op(0, OP_EQ, string_is_utf8("\xe1\x80", 2));
  3456. tt_int_op(0, OP_EQ, string_is_utf8("\xe1\x80 ", 3));
  3457. tt_int_op(0, OP_EQ, string_is_utf8("\xf1\x80\x80", 3));
  3458. tt_int_op(0, OP_EQ, string_is_utf8("\xf1\x80\x80 ", 4));
  3459. // Reject encodings that are overly long.
  3460. tt_int_op(0, OP_EQ, string_is_utf8("\xc1\xbf", 2));
  3461. tt_int_op(1, OP_EQ, string_is_utf8("\xc2\x80", 2));
  3462. tt_int_op(0, OP_EQ, string_is_utf8("\xe0\x9f\xbf", 3));
  3463. tt_int_op(1, OP_EQ, string_is_utf8("\xe0\xa0\x80", 3));
  3464. tt_int_op(0, OP_EQ, string_is_utf8("\xf0\x8f\xbf\xbf", 4));
  3465. tt_int_op(1, OP_EQ, string_is_utf8("\xf0\x90\x80\x80", 4));
  3466. // Reject UTF-16 surrogate halves.
  3467. tt_int_op(1, OP_EQ, string_is_utf8("\xed\x9f\xbf", 3));
  3468. tt_int_op(0, OP_EQ, string_is_utf8("\xed\xa0\x80", 3));
  3469. tt_int_op(0, OP_EQ, string_is_utf8("\xed\xbf\xbf", 3));
  3470. tt_int_op(1, OP_EQ, string_is_utf8("\xee\x80\x80", 3));
  3471. // The maximum legal codepoint, 10FFFF.
  3472. tt_int_op(1, OP_EQ, string_is_utf8("\xf4\x8f\xbf\xbf", 4));
  3473. tt_int_op(0, OP_EQ, string_is_utf8("\xf4\x90\x80\x80", 4));
  3474. done:
  3475. ;
  3476. }
  3477. static void
  3478. test_util_asprintf(void *ptr)
  3479. {
  3480. #define LOREMIPSUM \
  3481. "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
  3482. char *cp=NULL, *cp2=NULL;
  3483. int r;
  3484. (void)ptr;
  3485. /* simple string */
  3486. r = tor_asprintf(&cp, "simple string 100%% safe");
  3487. tt_assert(cp);
  3488. tt_str_op("simple string 100% safe",OP_EQ, cp);
  3489. tt_int_op(strlen(cp),OP_EQ, r);
  3490. tor_free(cp);
  3491. /* empty string */
  3492. r = tor_asprintf(&cp, "%s", "");
  3493. tt_assert(cp);
  3494. tt_str_op("",OP_EQ, cp);
  3495. tt_int_op(strlen(cp),OP_EQ, r);
  3496. tor_free(cp);
  3497. /* numbers (%i) */
  3498. r = tor_asprintf(&cp, "I like numbers-%2i, %i, etc.", -1, 2);
  3499. tt_assert(cp);
  3500. tt_str_op("I like numbers--1, 2, etc.",OP_EQ, cp);
  3501. tt_int_op(strlen(cp),OP_EQ, r);
  3502. /* don't free cp; next test uses it. */
  3503. /* numbers (%d) */
  3504. r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
  3505. tt_assert(cp2);
  3506. tt_int_op(strlen(cp2),OP_EQ, r);
  3507. tt_str_op("First=101, Second=202",OP_EQ, cp2);
  3508. tt_assert(cp != cp2);
  3509. tor_free(cp);
  3510. tor_free(cp2);
  3511. /* Glass-box test: a string exactly 128 characters long. */
  3512. r = tor_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
  3513. tt_assert(cp);
  3514. tt_int_op(128,OP_EQ, r);
  3515. tt_int_op(cp[128], OP_EQ, '\0');
  3516. tt_str_op("Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM,OP_EQ, cp);
  3517. tor_free(cp);
  3518. /* String longer than 128 characters */
  3519. r = tor_asprintf(&cp, "1: %s 2: %s 3: %s",
  3520. LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
  3521. tt_assert(cp);
  3522. tt_int_op(strlen(cp),OP_EQ, r);
  3523. tt_str_op("1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM,OP_EQ, cp);
  3524. done:
  3525. tor_free(cp);
  3526. tor_free(cp2);
  3527. }
  3528. static void
  3529. test_util_listdir(void *ptr)
  3530. {
  3531. smartlist_t *dir_contents = NULL;
  3532. char *fname1=NULL, *fname2=NULL, *fname3=NULL, *dir1=NULL, *dirname=NULL;
  3533. int r;
  3534. (void)ptr;
  3535. fname1 = tor_strdup(get_fname("hopscotch"));
  3536. fname2 = tor_strdup(get_fname("mumblety-peg"));
  3537. fname3 = tor_strdup(get_fname(".hidden-file"));
  3538. dir1 = tor_strdup(get_fname("some-directory"));
  3539. dirname = tor_strdup(get_fname(NULL));
  3540. tt_int_op(0,OP_EQ, write_str_to_file(fname1, "X\n", 0));
  3541. tt_int_op(0,OP_EQ, write_str_to_file(fname2, "Y\n", 0));
  3542. tt_int_op(0,OP_EQ, write_str_to_file(fname3, "Z\n", 0));
  3543. #ifdef _WIN32
  3544. r = mkdir(dir1);
  3545. #else
  3546. r = mkdir(dir1, 0700);
  3547. #endif
  3548. if (r) {
  3549. fprintf(stderr, "Can't create directory %s:", dir1);
  3550. perror("");
  3551. exit(1);
  3552. }
  3553. dir_contents = tor_listdir(dirname);
  3554. tt_assert(dir_contents);
  3555. /* make sure that each filename is listed. */
  3556. tt_assert(smartlist_contains_string_case(dir_contents, "hopscotch"));
  3557. tt_assert(smartlist_contains_string_case(dir_contents, "mumblety-peg"));
  3558. tt_assert(smartlist_contains_string_case(dir_contents, ".hidden-file"));
  3559. tt_assert(smartlist_contains_string_case(dir_contents, "some-directory"));
  3560. tt_assert(!smartlist_contains_string(dir_contents, "."));
  3561. tt_assert(!smartlist_contains_string(dir_contents, ".."));
  3562. done:
  3563. tor_free(fname1);
  3564. tor_free(fname2);
  3565. tor_free(fname3);
  3566. tor_free(dir1);
  3567. tor_free(dirname);
  3568. if (dir_contents) {
  3569. SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp));
  3570. smartlist_free(dir_contents);
  3571. }
  3572. }
  3573. static void
  3574. test_util_parent_dir(void *ptr)
  3575. {
  3576. char *cp;
  3577. (void)ptr;
  3578. #define T(output,expect_ok,input) \
  3579. do { \
  3580. int ok; \
  3581. cp = tor_strdup(input); \
  3582. ok = get_parent_directory(cp); \
  3583. tt_int_op(expect_ok, OP_EQ, ok); \
  3584. if (ok==0) \
  3585. tt_str_op(output, OP_EQ, cp); \
  3586. tor_free(cp); \
  3587. } while (0);
  3588. T("/home/wombat", 0, "/home/wombat/knish");
  3589. T("/home/wombat", 0, "/home/wombat/knish/");
  3590. T("/home/wombat", 0, "/home/wombat/knish///");
  3591. T("./home/wombat", 0, "./home/wombat/knish/");
  3592. T("/", 0, "/home");
  3593. T("/", 0, "/home//");
  3594. T(".", 0, "./wombat");
  3595. T(".", 0, "./wombat/");
  3596. T(".", 0, "./wombat//");
  3597. T("wombat", 0, "wombat/foo");
  3598. T("wombat/..", 0, "wombat/../foo");
  3599. T("wombat/../", 0, "wombat/..//foo"); /* Is this correct? */
  3600. T("wombat/.", 0, "wombat/./foo");
  3601. T("wombat/./", 0, "wombat/.//foo"); /* Is this correct? */
  3602. T("wombat", 0, "wombat/..//");
  3603. T("wombat", 0, "wombat/foo/");
  3604. T("wombat", 0, "wombat/.foo");
  3605. T("wombat", 0, "wombat/.foo/");
  3606. T("wombat", -1, "");
  3607. T("w", -1, "");
  3608. T("wombat", 0, "wombat/knish");
  3609. T("/", 0, "/");
  3610. T("/", 0, "////");
  3611. done:
  3612. tor_free(cp);
  3613. }
  3614. static void
  3615. test_util_ftruncate(void *ptr)
  3616. {
  3617. char *buf = NULL;
  3618. const char *fname;
  3619. int fd = -1;
  3620. const char *message = "Hello world";
  3621. const char *message2 = "Hola mundo";
  3622. struct stat st;
  3623. (void) ptr;
  3624. fname = get_fname("ftruncate");
  3625. fd = tor_open_cloexec(fname, O_WRONLY|O_CREAT, 0600);
  3626. tt_int_op(fd, OP_GE, 0);
  3627. /* Make the file be there. */
  3628. tt_int_op(strlen(message), OP_EQ,
  3629. write_all_to_fd(fd, message, strlen(message)));
  3630. tt_int_op((int)tor_fd_getpos(fd), OP_EQ, strlen(message));
  3631. tt_int_op(0, OP_EQ, fstat(fd, &st));
  3632. tt_int_op((int)st.st_size, OP_EQ, strlen(message));
  3633. /* Truncate and see if it got truncated */
  3634. tt_int_op(0, OP_EQ, tor_ftruncate(fd));
  3635. tt_int_op((int)tor_fd_getpos(fd), OP_EQ, 0);
  3636. tt_int_op(0, OP_EQ, fstat(fd, &st));
  3637. tt_int_op((int)st.st_size, OP_EQ, 0);
  3638. /* Replace, and see if it got replaced */
  3639. tt_int_op(strlen(message2), OP_EQ,
  3640. write_all_to_fd(fd, message2, strlen(message2)));
  3641. tt_int_op((int)tor_fd_getpos(fd), OP_EQ, strlen(message2));
  3642. tt_int_op(0, OP_EQ, fstat(fd, &st));
  3643. tt_int_op((int)st.st_size, OP_EQ, strlen(message2));
  3644. close(fd);
  3645. fd = -1;
  3646. buf = read_file_to_str(fname, 0, NULL);
  3647. tt_str_op(message2, OP_EQ, buf);
  3648. done:
  3649. if (fd >= 0)
  3650. close(fd);
  3651. tor_free(buf);
  3652. }
  3653. static void
  3654. test_util_num_cpus(void *arg)
  3655. {
  3656. (void)arg;
  3657. int num = compute_num_cpus();
  3658. if (num < 0)
  3659. tt_skip();
  3660. tt_int_op(num, OP_GE, 1);
  3661. tt_int_op(num, OP_LE, 16);
  3662. done:
  3663. ;
  3664. }
  3665. #ifdef _WIN32
  3666. static void
  3667. test_util_load_win_lib(void *ptr)
  3668. {
  3669. HANDLE h = load_windows_system_library(_T("advapi32.dll"));
  3670. (void) ptr;
  3671. tt_assert(h);
  3672. done:
  3673. if (h)
  3674. FreeLibrary(h);
  3675. }
  3676. #endif /* defined(_WIN32) */
  3677. /**
  3678. * Test for format_hex_number_sigsafe()
  3679. */
  3680. static void
  3681. test_util_format_hex_number(void *ptr)
  3682. {
  3683. int i, len;
  3684. char buf[33];
  3685. const struct {
  3686. const char *str;
  3687. unsigned int x;
  3688. } test_data[] = {
  3689. {"0", 0},
  3690. {"1", 1},
  3691. {"273A", 0x273a},
  3692. {"FFFF", 0xffff},
  3693. {"7FFFFFFF", 0x7fffffff},
  3694. {"FFFFFFFF", 0xffffffff},
  3695. #if UINT_MAX >= 0xffffffff
  3696. {"31BC421D", 0x31bc421d},
  3697. {"FFFFFFFF", 0xffffffff},
  3698. #endif
  3699. {NULL, 0}
  3700. };
  3701. (void)ptr;
  3702. for (i = 0; test_data[i].str != NULL; ++i) {
  3703. len = format_hex_number_sigsafe(test_data[i].x, buf, sizeof(buf));
  3704. tt_int_op(len,OP_NE, 0);
  3705. tt_int_op(len,OP_EQ, strlen(buf));
  3706. tt_str_op(buf,OP_EQ, test_data[i].str);
  3707. }
  3708. tt_int_op(4,OP_EQ, format_hex_number_sigsafe(0xffff, buf, 5));
  3709. tt_str_op(buf,OP_EQ, "FFFF");
  3710. tt_int_op(0,OP_EQ, format_hex_number_sigsafe(0xffff, buf, 4));
  3711. tt_int_op(0,OP_EQ, format_hex_number_sigsafe(0, buf, 1));
  3712. done:
  3713. return;
  3714. }
  3715. /**
  3716. * Test for format_hex_number_sigsafe()
  3717. */
  3718. static void
  3719. test_util_format_dec_number(void *ptr)
  3720. {
  3721. int i, len;
  3722. char buf[33];
  3723. const struct {
  3724. const char *str;
  3725. unsigned int x;
  3726. } test_data[] = {
  3727. {"0", 0},
  3728. {"1", 1},
  3729. {"1234", 1234},
  3730. {"12345678", 12345678},
  3731. {"99999999", 99999999},
  3732. {"100000000", 100000000},
  3733. {"4294967295", 4294967295u},
  3734. #if UINT_MAX > 0xffffffff
  3735. {"18446744073709551615", 18446744073709551615u },
  3736. #endif
  3737. {NULL, 0}
  3738. };
  3739. (void)ptr;
  3740. for (i = 0; test_data[i].str != NULL; ++i) {
  3741. len = format_dec_number_sigsafe(test_data[i].x, buf, sizeof(buf));
  3742. tt_int_op(len,OP_NE, 0);
  3743. tt_int_op(len,OP_EQ, strlen(buf));
  3744. tt_str_op(buf,OP_EQ, test_data[i].str);
  3745. len = format_dec_number_sigsafe(test_data[i].x, buf,
  3746. (int)(strlen(test_data[i].str) + 1));
  3747. tt_int_op(len,OP_EQ, strlen(buf));
  3748. tt_str_op(buf,OP_EQ, test_data[i].str);
  3749. }
  3750. tt_int_op(4,OP_EQ, format_dec_number_sigsafe(7331, buf, 5));
  3751. tt_str_op(buf,OP_EQ, "7331");
  3752. tt_int_op(0,OP_EQ, format_dec_number_sigsafe(7331, buf, 4));
  3753. tt_int_op(1,OP_EQ, format_dec_number_sigsafe(0, buf, 2));
  3754. tt_int_op(0,OP_EQ, format_dec_number_sigsafe(0, buf, 1));
  3755. done:
  3756. return;
  3757. }
  3758. #define MAX_SPLIT_LINE_COUNT 4
  3759. struct split_lines_test_t {
  3760. const char *orig_line; // Line to be split (may contain \0's)
  3761. int orig_length; // Length of orig_line
  3762. const char *split_line[MAX_SPLIT_LINE_COUNT]; // Split lines
  3763. };
  3764. static void
  3765. test_util_di_ops(void *arg)
  3766. {
  3767. #define LT -1
  3768. #define GT 1
  3769. #define EQ 0
  3770. const struct {
  3771. const char *a; int want_sign; const char *b;
  3772. } examples[] = {
  3773. { "Foo", EQ, "Foo" },
  3774. { "foo", GT, "bar", },
  3775. { "foobar", EQ ,"foobar" },
  3776. { "foobar", LT, "foobaw" },
  3777. { "foobar", GT, "f00bar" },
  3778. { "foobar", GT, "boobar" },
  3779. { "", EQ, "" },
  3780. { NULL, 0, NULL },
  3781. };
  3782. int i;
  3783. (void)arg;
  3784. for (i = 0; examples[i].a; ++i) {
  3785. size_t len = strlen(examples[i].a);
  3786. int eq1, eq2, neq1, neq2, cmp1, cmp2;
  3787. tt_int_op(len,OP_EQ, strlen(examples[i].b));
  3788. /* We do all of the operations, with operands in both orders. */
  3789. eq1 = tor_memeq(examples[i].a, examples[i].b, len);
  3790. eq2 = tor_memeq(examples[i].b, examples[i].a, len);
  3791. neq1 = tor_memneq(examples[i].a, examples[i].b, len);
  3792. neq2 = tor_memneq(examples[i].b, examples[i].a, len);
  3793. cmp1 = tor_memcmp(examples[i].a, examples[i].b, len);
  3794. cmp2 = tor_memcmp(examples[i].b, examples[i].a, len);
  3795. /* Check for correctness of cmp1 */
  3796. if (cmp1 < 0 && examples[i].want_sign != LT)
  3797. TT_DIE(("Assertion failed."));
  3798. else if (cmp1 > 0 && examples[i].want_sign != GT)
  3799. TT_DIE(("Assertion failed."));
  3800. else if (cmp1 == 0 && examples[i].want_sign != EQ)
  3801. TT_DIE(("Assertion failed."));
  3802. /* Check for consistency of everything else with cmp1 */
  3803. tt_int_op(eq1,OP_EQ, eq2);
  3804. tt_int_op(neq1,OP_EQ, neq2);
  3805. tt_int_op(cmp1,OP_EQ, -cmp2);
  3806. tt_int_op(eq1,OP_EQ, cmp1 == 0);
  3807. tt_int_op(neq1,OP_EQ, !eq1);
  3808. }
  3809. {
  3810. uint8_t zz = 0;
  3811. uint8_t ii = 0;
  3812. int z;
  3813. /* exhaustively test tor_memeq and tor_memcmp
  3814. * against each possible single-byte numeric difference
  3815. * some arithmetic bugs only appear with certain bit patterns */
  3816. for (z = 0; z < 256; z++) {
  3817. for (i = 0; i < 256; i++) {
  3818. ii = (uint8_t)i;
  3819. zz = (uint8_t)z;
  3820. tt_int_op(tor_memeq(&zz, &ii, 1),OP_EQ, zz == ii);
  3821. tt_int_op(tor_memcmp(&zz, &ii, 1) > 0 ? GT : EQ,OP_EQ,
  3822. zz > ii ? GT : EQ);
  3823. tt_int_op(tor_memcmp(&ii, &zz, 1) < 0 ? LT : EQ,OP_EQ,
  3824. ii < zz ? LT : EQ);
  3825. }
  3826. }
  3827. }
  3828. tt_int_op(1, OP_EQ, safe_mem_is_zero("", 0));
  3829. tt_int_op(1, OP_EQ, safe_mem_is_zero("", 1));
  3830. tt_int_op(0, OP_EQ, safe_mem_is_zero("a", 1));
  3831. tt_int_op(0, OP_EQ, safe_mem_is_zero("a", 2));
  3832. tt_int_op(0, OP_EQ, safe_mem_is_zero("\0a", 2));
  3833. tt_int_op(1, OP_EQ, safe_mem_is_zero("\0\0a", 2));
  3834. tt_int_op(1, OP_EQ, safe_mem_is_zero("\0\0\0\0\0\0\0\0", 8));
  3835. tt_int_op(1, OP_EQ, safe_mem_is_zero("\0\0\0\0\0\0\0\0a", 8));
  3836. tt_int_op(0, OP_EQ, safe_mem_is_zero("\0\0\0\0\0\0\0\0a", 9));
  3837. done:
  3838. ;
  3839. }
  3840. static void
  3841. test_util_di_map(void *arg)
  3842. {
  3843. (void)arg;
  3844. di_digest256_map_t *dimap = NULL;
  3845. uint8_t key1[] = "Robert Anton Wilson ";
  3846. uint8_t key2[] = "Martin Gardner, _Fads&fallacies";
  3847. uint8_t key3[] = "Tom Lehrer, _Be Prepared_. ";
  3848. uint8_t key4[] = "Ursula Le Guin,_A Wizard of... ";
  3849. char dflt_entry[] = "'You have made a good beginning', but no more";
  3850. tt_int_op(32, OP_EQ, sizeof(key1));
  3851. tt_int_op(32, OP_EQ, sizeof(key2));
  3852. tt_int_op(32, OP_EQ, sizeof(key3));
  3853. tt_ptr_op(dflt_entry, OP_EQ, dimap_search(dimap, key1, dflt_entry));
  3854. char *str1 = tor_strdup("You are precisely as big as what you love"
  3855. " and precisely as small as what you allow"
  3856. " to annoy you.");
  3857. char *str2 = tor_strdup("Let us hope that Lysenko's success in Russia will"
  3858. " serve for many generations to come as another"
  3859. " reminder to the world of how quickly and easily"
  3860. " a science can be corrupted when ignorant"
  3861. " political leaders deem themselves competent"
  3862. " to arbitrate scientific disputes");
  3863. char *str3 = tor_strdup("Don't write naughty words on walls "
  3864. "if you can't spell.");
  3865. dimap_add_entry(&dimap, key1, str1);
  3866. dimap_add_entry(&dimap, key2, str2);
  3867. dimap_add_entry(&dimap, key3, str3);
  3868. tt_ptr_op(str1, OP_EQ, dimap_search(dimap, key1, dflt_entry));
  3869. tt_ptr_op(str3, OP_EQ, dimap_search(dimap, key3, dflt_entry));
  3870. tt_ptr_op(str2, OP_EQ, dimap_search(dimap, key2, dflt_entry));
  3871. tt_ptr_op(dflt_entry, OP_EQ, dimap_search(dimap, key4, dflt_entry));
  3872. done:
  3873. dimap_free(dimap, tor_free_);
  3874. }
  3875. /**
  3876. * Test counting high bits
  3877. */
  3878. static void
  3879. test_util_n_bits_set(void *ptr)
  3880. {
  3881. (void)ptr;
  3882. tt_int_op(0,OP_EQ, n_bits_set_u8(0));
  3883. tt_int_op(1,OP_EQ, n_bits_set_u8(1));
  3884. tt_int_op(3,OP_EQ, n_bits_set_u8(7));
  3885. tt_int_op(1,OP_EQ, n_bits_set_u8(8));
  3886. tt_int_op(2,OP_EQ, n_bits_set_u8(129));
  3887. tt_int_op(8,OP_EQ, n_bits_set_u8(255));
  3888. done:
  3889. ;
  3890. }
  3891. /**
  3892. * Test LHS whitespace (and comment) eater
  3893. */
  3894. static void
  3895. test_util_eat_whitespace(void *ptr)
  3896. {
  3897. const char ws[] = { ' ', '\t', '\r' }; /* Except NL */
  3898. char str[80];
  3899. size_t i;
  3900. (void)ptr;
  3901. /* Try one leading ws */
  3902. strlcpy(str, "fuubaar", sizeof(str));
  3903. for (i = 0; i < sizeof(ws); ++i) {
  3904. str[0] = ws[i];
  3905. tt_ptr_op(str + 1,OP_EQ, eat_whitespace(str));
  3906. tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
  3907. tt_ptr_op(str + 1,OP_EQ, eat_whitespace_no_nl(str));
  3908. tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3909. }
  3910. str[0] = '\n';
  3911. tt_ptr_op(str + 1,OP_EQ, eat_whitespace(str));
  3912. tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
  3913. tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
  3914. tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3915. /* Empty string */
  3916. strlcpy(str, "", sizeof(str));
  3917. tt_ptr_op(str,OP_EQ, eat_whitespace(str));
  3918. tt_ptr_op(str,OP_EQ, eat_whitespace_eos(str, str));
  3919. tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
  3920. tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str));
  3921. /* Only ws */
  3922. strlcpy(str, " \t\r\n", sizeof(str));
  3923. tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
  3924. tt_ptr_op(str + strlen(str),OP_EQ,
  3925. eat_whitespace_eos(str, str + strlen(str)));
  3926. tt_ptr_op(str + strlen(str) - 1,OP_EQ,
  3927. eat_whitespace_no_nl(str));
  3928. tt_ptr_op(str + strlen(str) - 1,OP_EQ,
  3929. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3930. strlcpy(str, " \t\r ", sizeof(str));
  3931. tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
  3932. tt_ptr_op(str + strlen(str),OP_EQ,
  3933. eat_whitespace_eos(str, str + strlen(str)));
  3934. tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace_no_nl(str));
  3935. tt_ptr_op(str + strlen(str),OP_EQ,
  3936. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3937. /* Multiple ws */
  3938. strlcpy(str, "fuubaar", sizeof(str));
  3939. for (i = 0; i < sizeof(ws); ++i)
  3940. str[i] = ws[i];
  3941. tt_ptr_op(str + sizeof(ws),OP_EQ, eat_whitespace(str));
  3942. tt_ptr_op(str + sizeof(ws),OP_EQ,
  3943. eat_whitespace_eos(str, str + strlen(str)));
  3944. tt_ptr_op(str + sizeof(ws),OP_EQ, eat_whitespace_no_nl(str));
  3945. tt_ptr_op(str + sizeof(ws),OP_EQ,
  3946. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3947. /* Eat comment */
  3948. strlcpy(str, "# Comment \n No Comment", sizeof(str));
  3949. tt_str_op("No Comment",OP_EQ, eat_whitespace(str));
  3950. tt_str_op("No Comment",OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
  3951. tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
  3952. tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3953. /* Eat comment & ws mix */
  3954. strlcpy(str, " # \t Comment \n\t\nNo Comment", sizeof(str));
  3955. tt_str_op("No Comment",OP_EQ, eat_whitespace(str));
  3956. tt_str_op("No Comment",OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
  3957. tt_ptr_op(str + 1,OP_EQ, eat_whitespace_no_nl(str));
  3958. tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3959. /* Eat entire comment */
  3960. strlcpy(str, "#Comment", sizeof(str));
  3961. tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
  3962. tt_ptr_op(str + strlen(str),OP_EQ,
  3963. eat_whitespace_eos(str, str + strlen(str)));
  3964. tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
  3965. tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3966. /* Blank line, then comment */
  3967. strlcpy(str, " \t\n # Comment", sizeof(str));
  3968. tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
  3969. tt_ptr_op(str + strlen(str),OP_EQ,
  3970. eat_whitespace_eos(str, str + strlen(str)));
  3971. tt_ptr_op(str + 2,OP_EQ, eat_whitespace_no_nl(str));
  3972. tt_ptr_op(str + 2,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3973. done:
  3974. ;
  3975. }
  3976. /** Return a newly allocated smartlist containing the lines of text in
  3977. * <b>lines</b>. The returned strings are heap-allocated, and must be
  3978. * freed by the caller.
  3979. *
  3980. * XXXX? Move to container.[hc] ? */
  3981. static smartlist_t *
  3982. smartlist_new_from_text_lines(const char *lines)
  3983. {
  3984. smartlist_t *sl = smartlist_new();
  3985. char *last_line;
  3986. smartlist_split_string(sl, lines, "\n", 0, 0);
  3987. last_line = smartlist_pop_last(sl);
  3988. if (last_line != NULL && *last_line != '\0') {
  3989. smartlist_add(sl, last_line);
  3990. } else {
  3991. tor_free(last_line);
  3992. }
  3993. return sl;
  3994. }
  3995. /** Test smartlist_new_from_text_lines */
  3996. static void
  3997. test_util_sl_new_from_text_lines(void *ptr)
  3998. {
  3999. (void)ptr;
  4000. { /* Normal usage */
  4001. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz\n");
  4002. int sl_len = smartlist_len(sl);
  4003. tt_want_int_op(sl_len, OP_EQ, 3);
  4004. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), OP_EQ, "foo");
  4005. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), OP_EQ, "bar");
  4006. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), OP_EQ, "baz");
  4007. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  4008. smartlist_free(sl);
  4009. }
  4010. { /* No final newline */
  4011. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz");
  4012. int sl_len = smartlist_len(sl);
  4013. tt_want_int_op(sl_len, OP_EQ, 3);
  4014. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), OP_EQ, "foo");
  4015. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), OP_EQ, "bar");
  4016. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), OP_EQ, "baz");
  4017. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  4018. smartlist_free(sl);
  4019. }
  4020. { /* No newlines */
  4021. smartlist_t *sl = smartlist_new_from_text_lines("foo");
  4022. int sl_len = smartlist_len(sl);
  4023. tt_want_int_op(sl_len, OP_EQ, 1);
  4024. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), OP_EQ, "foo");
  4025. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  4026. smartlist_free(sl);
  4027. }
  4028. { /* No text at all */
  4029. smartlist_t *sl = smartlist_new_from_text_lines("");
  4030. int sl_len = smartlist_len(sl);
  4031. tt_want_int_op(sl_len, OP_EQ, 0);
  4032. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  4033. smartlist_free(sl);
  4034. }
  4035. }
  4036. static void
  4037. test_util_envnames(void *ptr)
  4038. {
  4039. (void) ptr;
  4040. tt_assert(environment_variable_names_equal("abc", "abc"));
  4041. tt_assert(environment_variable_names_equal("abc", "abc="));
  4042. tt_assert(environment_variable_names_equal("abc", "abc=def"));
  4043. tt_assert(environment_variable_names_equal("abc=def", "abc"));
  4044. tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
  4045. tt_assert(environment_variable_names_equal("abc", "abc"));
  4046. tt_assert(environment_variable_names_equal("abc", "abc="));
  4047. tt_assert(environment_variable_names_equal("abc", "abc=def"));
  4048. tt_assert(environment_variable_names_equal("abc=def", "abc"));
  4049. tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
  4050. tt_assert(!environment_variable_names_equal("abc", "abcd"));
  4051. tt_assert(!environment_variable_names_equal("abc=", "abcd"));
  4052. tt_assert(!environment_variable_names_equal("abc=", "abcd"));
  4053. tt_assert(!environment_variable_names_equal("abc=", "def"));
  4054. tt_assert(!environment_variable_names_equal("abc=", "def="));
  4055. tt_assert(!environment_variable_names_equal("abc=x", "def=x"));
  4056. tt_assert(!environment_variable_names_equal("", "a=def"));
  4057. /* A bit surprising. */
  4058. tt_assert(environment_variable_names_equal("", "=def"));
  4059. tt_assert(environment_variable_names_equal("=y", "=x"));
  4060. done:
  4061. ;
  4062. }
  4063. /** Test process_environment_make */
  4064. static void
  4065. test_util_make_environment(void *ptr)
  4066. {
  4067. const char *env_vars_string =
  4068. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  4069. "HOME=/home/foozer\n";
  4070. const char expected_windows_env_block[] =
  4071. "HOME=/home/foozer\000"
  4072. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\000"
  4073. "\000";
  4074. size_t expected_windows_env_block_len =
  4075. sizeof(expected_windows_env_block) - 1;
  4076. smartlist_t *env_vars = smartlist_new_from_text_lines(env_vars_string);
  4077. smartlist_t *env_vars_sorted = smartlist_new();
  4078. smartlist_t *env_vars_in_unixoid_env_block_sorted = smartlist_new();
  4079. process_environment_t *env;
  4080. (void)ptr;
  4081. env = process_environment_make(env_vars);
  4082. /* Check that the Windows environment block is correct. */
  4083. tt_want(tor_memeq(expected_windows_env_block, env->windows_environment_block,
  4084. expected_windows_env_block_len));
  4085. /* Now for the Unixoid environment block. We don't care which order
  4086. * these environment variables are in, so we sort both lists first. */
  4087. smartlist_add_all(env_vars_sorted, env_vars);
  4088. {
  4089. char **v;
  4090. for (v = env->unixoid_environment_block; *v; ++v) {
  4091. smartlist_add(env_vars_in_unixoid_env_block_sorted, *v);
  4092. }
  4093. }
  4094. smartlist_sort_strings(env_vars_sorted);
  4095. smartlist_sort_strings(env_vars_in_unixoid_env_block_sorted);
  4096. tt_want_int_op(smartlist_len(env_vars_sorted), OP_EQ,
  4097. smartlist_len(env_vars_in_unixoid_env_block_sorted));
  4098. {
  4099. int len = smartlist_len(env_vars_sorted);
  4100. int i;
  4101. if (smartlist_len(env_vars_in_unixoid_env_block_sorted) < len) {
  4102. len = smartlist_len(env_vars_in_unixoid_env_block_sorted);
  4103. }
  4104. for (i = 0; i < len; ++i) {
  4105. tt_want_str_op(smartlist_get(env_vars_sorted, i), OP_EQ,
  4106. smartlist_get(env_vars_in_unixoid_env_block_sorted, i));
  4107. }
  4108. }
  4109. /* Clean up. */
  4110. smartlist_free(env_vars_in_unixoid_env_block_sorted);
  4111. smartlist_free(env_vars_sorted);
  4112. SMARTLIST_FOREACH(env_vars, char *, x, tor_free(x));
  4113. smartlist_free(env_vars);
  4114. process_environment_free(env);
  4115. }
  4116. /** Test set_environment_variable_in_smartlist */
  4117. static void
  4118. test_util_set_env_var_in_sl(void *ptr)
  4119. {
  4120. /* The environment variables in these strings are in arbitrary
  4121. * order; we sort the resulting lists before comparing them.
  4122. *
  4123. * (They *will not* end up in the order shown in
  4124. * expected_resulting_env_vars_string.) */
  4125. const char *base_env_vars_string =
  4126. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  4127. "HOME=/home/foozer\n"
  4128. "TERM=xterm\n"
  4129. "SHELL=/bin/ksh\n"
  4130. "USER=foozer\n"
  4131. "LOGNAME=foozer\n"
  4132. "USERNAME=foozer\n"
  4133. "LANG=en_US.utf8\n"
  4134. ;
  4135. const char *new_env_vars_string =
  4136. "TERM=putty\n"
  4137. "DISPLAY=:18.0\n"
  4138. ;
  4139. const char *expected_resulting_env_vars_string =
  4140. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  4141. "HOME=/home/foozer\n"
  4142. "TERM=putty\n"
  4143. "SHELL=/bin/ksh\n"
  4144. "USER=foozer\n"
  4145. "LOGNAME=foozer\n"
  4146. "USERNAME=foozer\n"
  4147. "LANG=en_US.utf8\n"
  4148. "DISPLAY=:18.0\n"
  4149. ;
  4150. smartlist_t *merged_env_vars =
  4151. smartlist_new_from_text_lines(base_env_vars_string);
  4152. smartlist_t *new_env_vars =
  4153. smartlist_new_from_text_lines(new_env_vars_string);
  4154. smartlist_t *expected_resulting_env_vars =
  4155. smartlist_new_from_text_lines(expected_resulting_env_vars_string);
  4156. /* Elements of merged_env_vars are heap-allocated, and must be
  4157. * freed. Some of them are (or should) be freed by
  4158. * set_environment_variable_in_smartlist.
  4159. *
  4160. * Elements of new_env_vars are heap-allocated, but are copied into
  4161. * merged_env_vars, so they are not freed separately at the end of
  4162. * the function.
  4163. *
  4164. * Elements of expected_resulting_env_vars are heap-allocated, and
  4165. * must be freed. */
  4166. (void)ptr;
  4167. SMARTLIST_FOREACH(new_env_vars, char *, env_var,
  4168. set_environment_variable_in_smartlist(merged_env_vars,
  4169. env_var,
  4170. tor_free_,
  4171. 1));
  4172. smartlist_sort_strings(merged_env_vars);
  4173. smartlist_sort_strings(expected_resulting_env_vars);
  4174. tt_want_int_op(smartlist_len(merged_env_vars), OP_EQ,
  4175. smartlist_len(expected_resulting_env_vars));
  4176. {
  4177. int len = smartlist_len(merged_env_vars);
  4178. int i;
  4179. if (smartlist_len(expected_resulting_env_vars) < len) {
  4180. len = smartlist_len(expected_resulting_env_vars);
  4181. }
  4182. for (i = 0; i < len; ++i) {
  4183. tt_want_str_op(smartlist_get(merged_env_vars, i), OP_EQ,
  4184. smartlist_get(expected_resulting_env_vars, i));
  4185. }
  4186. }
  4187. /* Clean up. */
  4188. SMARTLIST_FOREACH(merged_env_vars, char *, x, tor_free(x));
  4189. smartlist_free(merged_env_vars);
  4190. smartlist_free(new_env_vars);
  4191. SMARTLIST_FOREACH(expected_resulting_env_vars, char *, x, tor_free(x));
  4192. smartlist_free(expected_resulting_env_vars);
  4193. }
  4194. static void
  4195. test_util_weak_random(void *arg)
  4196. {
  4197. int i, j, n[16];
  4198. tor_weak_rng_t rng;
  4199. (void) arg;
  4200. tor_init_weak_random(&rng, (unsigned)time(NULL));
  4201. for (i = 1; i <= 256; ++i) {
  4202. for (j=0;j<100;++j) {
  4203. int r = tor_weak_random_range(&rng, i);
  4204. tt_int_op(0, OP_LE, r);
  4205. tt_int_op(r, OP_LT, i);
  4206. }
  4207. }
  4208. memset(n,0,sizeof(n));
  4209. for (j=0;j<8192;++j) {
  4210. n[tor_weak_random_range(&rng, 16)]++;
  4211. }
  4212. for (i=0;i<16;++i)
  4213. tt_int_op(n[i], OP_GT, 0);
  4214. done:
  4215. ;
  4216. }
  4217. static void
  4218. test_util_mathlog(void *arg)
  4219. {
  4220. double d;
  4221. (void) arg;
  4222. d = tor_mathlog(2.718281828);
  4223. tt_double_op(fabs(d - 1.0), OP_LT, .000001);
  4224. d = tor_mathlog(10);
  4225. tt_double_op(fabs(d - 2.30258509), OP_LT, .000001);
  4226. done:
  4227. ;
  4228. }
  4229. static void
  4230. test_util_fraction(void *arg)
  4231. {
  4232. uint64_t a,b;
  4233. (void)arg;
  4234. a = 99; b = 30;
  4235. simplify_fraction64(&a,&b);
  4236. tt_u64_op(a, OP_EQ, 33);
  4237. tt_u64_op(b, OP_EQ, 10);
  4238. a = 3000000; b = 10000000;
  4239. simplify_fraction64(&a,&b);
  4240. tt_u64_op(a, OP_EQ, 3);
  4241. tt_u64_op(b, OP_EQ, 10);
  4242. a = 0; b = 15;
  4243. simplify_fraction64(&a,&b);
  4244. tt_u64_op(a, OP_EQ, 0);
  4245. tt_u64_op(b, OP_EQ, 1);
  4246. done:
  4247. ;
  4248. }
  4249. static void
  4250. test_util_round_to_next_multiple_of(void *arg)
  4251. {
  4252. (void)arg;
  4253. tt_u64_op(round_uint64_to_next_multiple_of(0,1), OP_EQ, 0);
  4254. tt_u64_op(round_uint64_to_next_multiple_of(0,7), OP_EQ, 0);
  4255. tt_u64_op(round_uint64_to_next_multiple_of(99,1), OP_EQ, 99);
  4256. tt_u64_op(round_uint64_to_next_multiple_of(99,7), OP_EQ, 105);
  4257. tt_u64_op(round_uint64_to_next_multiple_of(99,9), OP_EQ, 99);
  4258. tt_u64_op(round_uint64_to_next_multiple_of(UINT64_MAX,2), OP_EQ,
  4259. UINT64_MAX);
  4260. tt_int_op(round_uint32_to_next_multiple_of(0,1), OP_EQ, 0);
  4261. tt_int_op(round_uint32_to_next_multiple_of(0,7), OP_EQ, 0);
  4262. tt_int_op(round_uint32_to_next_multiple_of(99,1), OP_EQ, 99);
  4263. tt_int_op(round_uint32_to_next_multiple_of(99,7), OP_EQ, 105);
  4264. tt_int_op(round_uint32_to_next_multiple_of(99,9), OP_EQ, 99);
  4265. tt_int_op(round_uint32_to_next_multiple_of(UINT32_MAX,2), OP_EQ,
  4266. UINT32_MAX);
  4267. tt_uint_op(round_to_next_multiple_of(0,1), OP_EQ, 0);
  4268. tt_uint_op(round_to_next_multiple_of(0,7), OP_EQ, 0);
  4269. tt_uint_op(round_to_next_multiple_of(99,1), OP_EQ, 99);
  4270. tt_uint_op(round_to_next_multiple_of(99,7), OP_EQ, 105);
  4271. tt_uint_op(round_to_next_multiple_of(99,9), OP_EQ, 99);
  4272. tt_uint_op(round_to_next_multiple_of(UINT_MAX,2), OP_EQ,
  4273. UINT_MAX);
  4274. done:
  4275. ;
  4276. }
  4277. static void
  4278. test_util_laplace(void *arg)
  4279. {
  4280. /* Sample values produced using Python's SciPy:
  4281. *
  4282. * >>> from scipy.stats import laplace
  4283. * >>> laplace.ppf([-0.01, 0.0, 0.01, 0.5, 0.51, 0.99, 1.0, 1.01],
  4284. ... loc = 24, scale = 24)
  4285. * array([ nan, -inf, -69.88855213, 24. ,
  4286. * 24.48486498, 117.88855213, inf, nan])
  4287. */
  4288. const double mu = 24.0, b = 24.0;
  4289. const double delta_f = 15.0, epsilon = 0.3; /* b = 15.0 / 0.3 = 50.0 */
  4290. (void)arg;
  4291. tt_i64_op(INT64_MIN, OP_EQ, sample_laplace_distribution(mu, b, 0.0));
  4292. tt_i64_op(-69, OP_EQ, sample_laplace_distribution(mu, b, 0.01));
  4293. tt_i64_op(24, OP_EQ, sample_laplace_distribution(mu, b, 0.5));
  4294. tt_i64_op(24, OP_EQ, sample_laplace_distribution(mu, b, 0.51));
  4295. tt_i64_op(117, OP_EQ, sample_laplace_distribution(mu, b, 0.99));
  4296. /* >>> laplace.ppf([0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99],
  4297. * ... loc = 0, scale = 50)
  4298. * array([ -inf, -80.47189562, -34.65735903, 0. ,
  4299. * 34.65735903, 80.47189562, 195.60115027])
  4300. */
  4301. tt_i64_op(INT64_MIN + 20, OP_EQ,
  4302. add_laplace_noise(20, 0.0, delta_f, epsilon));
  4303. tt_i64_op(-60, OP_EQ, add_laplace_noise(20, 0.1, delta_f, epsilon));
  4304. tt_i64_op(-14, OP_EQ, add_laplace_noise(20, 0.25, delta_f, epsilon));
  4305. tt_i64_op(20, OP_EQ, add_laplace_noise(20, 0.5, delta_f, epsilon));
  4306. tt_i64_op(54, OP_EQ, add_laplace_noise(20, 0.75, delta_f, epsilon));
  4307. tt_i64_op(100, OP_EQ, add_laplace_noise(20, 0.9, delta_f, epsilon));
  4308. tt_i64_op(215, OP_EQ, add_laplace_noise(20, 0.99, delta_f, epsilon));
  4309. /* Test extreme values of signal with maximally negative values of noise
  4310. * 1.0000000000000002 is the smallest number > 1
  4311. * 0.0000000000000002 is the double epsilon (error when calculating near 1)
  4312. * this is approximately 1/(2^52)
  4313. * per https://en.wikipedia.org/wiki/Double_precision
  4314. * (let's not descend into the world of subnormals)
  4315. * >>> laplace.ppf([0, 0.0000000000000002], loc = 0, scale = 1)
  4316. * array([ -inf, -35.45506713])
  4317. */
  4318. const double noscale_df = 1.0, noscale_eps = 1.0;
  4319. tt_i64_op(INT64_MIN, OP_EQ,
  4320. add_laplace_noise(0, 0.0, noscale_df, noscale_eps));
  4321. /* is it clipped to INT64_MIN? */
  4322. tt_i64_op(INT64_MIN, OP_EQ,
  4323. add_laplace_noise(-1, 0.0, noscale_df, noscale_eps));
  4324. tt_i64_op(INT64_MIN, OP_EQ,
  4325. add_laplace_noise(INT64_MIN, 0.0,
  4326. noscale_df, noscale_eps));
  4327. /* ... even when scaled? */
  4328. tt_i64_op(INT64_MIN, OP_EQ,
  4329. add_laplace_noise(0, 0.0, delta_f, epsilon));
  4330. tt_i64_op(INT64_MIN, OP_EQ,
  4331. add_laplace_noise(0, 0.0,
  4332. DBL_MAX, 1));
  4333. tt_i64_op(INT64_MIN, OP_EQ,
  4334. add_laplace_noise(INT64_MIN, 0.0,
  4335. DBL_MAX, 1));
  4336. /* does it play nice with INT64_MAX? */
  4337. tt_i64_op((INT64_MIN + INT64_MAX), OP_EQ,
  4338. add_laplace_noise(INT64_MAX, 0.0,
  4339. noscale_df, noscale_eps));
  4340. /* do near-zero fractional values work? */
  4341. const double min_dbl_error = 0.0000000000000002;
  4342. tt_i64_op(-35, OP_EQ,
  4343. add_laplace_noise(0, min_dbl_error,
  4344. noscale_df, noscale_eps));
  4345. tt_i64_op(INT64_MIN, OP_EQ,
  4346. add_laplace_noise(INT64_MIN, min_dbl_error,
  4347. noscale_df, noscale_eps));
  4348. tt_i64_op((-35 + INT64_MAX), OP_EQ,
  4349. add_laplace_noise(INT64_MAX, min_dbl_error,
  4350. noscale_df, noscale_eps));
  4351. tt_i64_op(INT64_MIN, OP_EQ,
  4352. add_laplace_noise(0, min_dbl_error,
  4353. DBL_MAX, 1));
  4354. tt_i64_op((INT64_MAX + INT64_MIN), OP_EQ,
  4355. add_laplace_noise(INT64_MAX, min_dbl_error,
  4356. DBL_MAX, 1));
  4357. tt_i64_op(INT64_MIN, OP_EQ,
  4358. add_laplace_noise(INT64_MIN, min_dbl_error,
  4359. DBL_MAX, 1));
  4360. /* does it play nice with INT64_MAX? */
  4361. tt_i64_op((INT64_MAX - 35), OP_EQ,
  4362. add_laplace_noise(INT64_MAX, min_dbl_error,
  4363. noscale_df, noscale_eps));
  4364. /* Test extreme values of signal with maximally positive values of noise
  4365. * 1.0000000000000002 is the smallest number > 1
  4366. * 0.9999999999999998 is the greatest number < 1 by calculation
  4367. * per https://en.wikipedia.org/wiki/Double_precision
  4368. * >>> laplace.ppf([1.0, 0.9999999999999998], loc = 0, scale = 1)
  4369. * array([inf, 35.35050621])
  4370. * but the function rejects p == 1.0, so we just use max_dbl_lt_one
  4371. */
  4372. const double max_dbl_lt_one = 0.9999999999999998;
  4373. /* do near-one fractional values work? */
  4374. tt_i64_op(35, OP_EQ,
  4375. add_laplace_noise(0, max_dbl_lt_one, noscale_df, noscale_eps));
  4376. /* is it clipped to INT64_MAX? */
  4377. tt_i64_op(INT64_MAX, OP_EQ,
  4378. add_laplace_noise(INT64_MAX - 35, max_dbl_lt_one,
  4379. noscale_df, noscale_eps));
  4380. tt_i64_op(INT64_MAX, OP_EQ,
  4381. add_laplace_noise(INT64_MAX - 34, max_dbl_lt_one,
  4382. noscale_df, noscale_eps));
  4383. tt_i64_op(INT64_MAX, OP_EQ,
  4384. add_laplace_noise(INT64_MAX, max_dbl_lt_one,
  4385. noscale_df, noscale_eps));
  4386. /* ... even when scaled? */
  4387. tt_i64_op(INT64_MAX, OP_EQ,
  4388. add_laplace_noise(INT64_MAX, max_dbl_lt_one,
  4389. delta_f, epsilon));
  4390. tt_i64_op((INT64_MIN + INT64_MAX), OP_EQ,
  4391. add_laplace_noise(INT64_MIN, max_dbl_lt_one,
  4392. DBL_MAX, 1));
  4393. tt_i64_op(INT64_MAX, OP_EQ,
  4394. add_laplace_noise(INT64_MAX, max_dbl_lt_one,
  4395. DBL_MAX, 1));
  4396. /* does it play nice with INT64_MIN? */
  4397. tt_i64_op((INT64_MIN + 35), OP_EQ,
  4398. add_laplace_noise(INT64_MIN, max_dbl_lt_one,
  4399. noscale_df, noscale_eps));
  4400. done:
  4401. ;
  4402. }
  4403. static void
  4404. test_util_clamp_double_to_int64(void *arg)
  4405. {
  4406. (void)arg;
  4407. tt_i64_op(INT64_MIN, OP_EQ, clamp_double_to_int64(-INFINITY_DBL));
  4408. tt_i64_op(INT64_MIN, OP_EQ,
  4409. clamp_double_to_int64(-1.0 * pow(2.0, 64.0) - 1.0));
  4410. tt_i64_op(INT64_MIN, OP_EQ,
  4411. clamp_double_to_int64(-1.0 * pow(2.0, 63.0) - 1.0));
  4412. tt_i64_op(((uint64_t) -1) << 53, OP_EQ,
  4413. clamp_double_to_int64(-1.0 * pow(2.0, 53.0)));
  4414. tt_i64_op((((uint64_t) -1) << 53) + 1, OP_EQ,
  4415. clamp_double_to_int64(-1.0 * pow(2.0, 53.0) + 1.0));
  4416. tt_i64_op(-1, OP_EQ, clamp_double_to_int64(-1.0));
  4417. tt_i64_op(0, OP_EQ, clamp_double_to_int64(-0.9));
  4418. tt_i64_op(0, OP_EQ, clamp_double_to_int64(-0.1));
  4419. tt_i64_op(0, OP_EQ, clamp_double_to_int64(0.0));
  4420. tt_i64_op(0, OP_EQ, clamp_double_to_int64(NAN_DBL));
  4421. tt_i64_op(0, OP_EQ, clamp_double_to_int64(0.1));
  4422. tt_i64_op(0, OP_EQ, clamp_double_to_int64(0.9));
  4423. tt_i64_op(1, OP_EQ, clamp_double_to_int64(1.0));
  4424. tt_i64_op((((int64_t) 1) << 53) - 1, OP_EQ,
  4425. clamp_double_to_int64(pow(2.0, 53.0) - 1.0));
  4426. tt_i64_op(((int64_t) 1) << 53, OP_EQ,
  4427. clamp_double_to_int64(pow(2.0, 53.0)));
  4428. tt_i64_op(INT64_MAX, OP_EQ,
  4429. clamp_double_to_int64(pow(2.0, 63.0)));
  4430. tt_i64_op(INT64_MAX, OP_EQ,
  4431. clamp_double_to_int64(pow(2.0, 64.0)));
  4432. tt_i64_op(INT64_MAX, OP_EQ, clamp_double_to_int64(INFINITY_DBL));
  4433. done:
  4434. ;
  4435. }
  4436. #ifdef FD_CLOEXEC
  4437. #define CAN_CHECK_CLOEXEC
  4438. static int
  4439. fd_is_cloexec(tor_socket_t fd)
  4440. {
  4441. int flags = fcntl(fd, F_GETFD, 0);
  4442. return (flags & FD_CLOEXEC) == FD_CLOEXEC;
  4443. }
  4444. #endif /* defined(FD_CLOEXEC) */
  4445. #ifndef _WIN32
  4446. #define CAN_CHECK_NONBLOCK
  4447. static int
  4448. fd_is_nonblocking(tor_socket_t fd)
  4449. {
  4450. int flags = fcntl(fd, F_GETFL, 0);
  4451. return (flags & O_NONBLOCK) == O_NONBLOCK;
  4452. }
  4453. #endif /* !defined(_WIN32) */
  4454. #define ERRNO_IS_EPROTO(e) (e == SOCK_ERRNO(EPROTONOSUPPORT))
  4455. #define SOCK_ERR_IS_EPROTO(s) ERRNO_IS_EPROTO(tor_socket_errno(s))
  4456. /* Test for tor_open_socket*, using IPv4 or IPv6 depending on arg. */
  4457. static void
  4458. test_util_socket(void *arg)
  4459. {
  4460. const int domain = !strcmp(arg, "4") ? AF_INET : AF_INET6;
  4461. tor_socket_t fd1 = TOR_INVALID_SOCKET;
  4462. tor_socket_t fd2 = TOR_INVALID_SOCKET;
  4463. tor_socket_t fd3 = TOR_INVALID_SOCKET;
  4464. tor_socket_t fd4 = TOR_INVALID_SOCKET;
  4465. int n = get_n_open_sockets();
  4466. TT_BLATHER(("Starting with %d open sockets.", n));
  4467. (void)arg;
  4468. fd1 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 0, 0);
  4469. int err = tor_socket_errno(fd1);
  4470. if (fd1 < 0 && (err == SOCK_ERRNO(EPROTONOSUPPORT) ||
  4471. err == SOCK_ERRNO(EAFNOSUPPORT))) {
  4472. /* Assume we're on an IPv4-only or IPv6-only system, and give up now. */
  4473. goto done;
  4474. }
  4475. fd2 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 0, 1);
  4476. tt_assert(SOCKET_OK(fd1));
  4477. tt_assert(SOCKET_OK(fd2));
  4478. tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
  4479. //fd3 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 1, 0);
  4480. //fd4 = tor_open_socket_with_extensions(domain, SOCK_STREAM, 0, 1, 1);
  4481. fd3 = tor_open_socket(domain, SOCK_STREAM, 0);
  4482. fd4 = tor_open_socket_nonblocking(domain, SOCK_STREAM, 0);
  4483. tt_assert(SOCKET_OK(fd3));
  4484. tt_assert(SOCKET_OK(fd4));
  4485. tt_int_op(get_n_open_sockets(), OP_EQ, n + 4);
  4486. #ifdef CAN_CHECK_CLOEXEC
  4487. tt_int_op(fd_is_cloexec(fd1), OP_EQ, 0);
  4488. tt_int_op(fd_is_cloexec(fd2), OP_EQ, 0);
  4489. tt_int_op(fd_is_cloexec(fd3), OP_EQ, 1);
  4490. tt_int_op(fd_is_cloexec(fd4), OP_EQ, 1);
  4491. #endif /* defined(CAN_CHECK_CLOEXEC) */
  4492. #ifdef CAN_CHECK_NONBLOCK
  4493. tt_int_op(fd_is_nonblocking(fd1), OP_EQ, 0);
  4494. tt_int_op(fd_is_nonblocking(fd2), OP_EQ, 1);
  4495. tt_int_op(fd_is_nonblocking(fd3), OP_EQ, 0);
  4496. tt_int_op(fd_is_nonblocking(fd4), OP_EQ, 1);
  4497. #endif /* defined(CAN_CHECK_NONBLOCK) */
  4498. tor_assert(tor_close_socket == tor_close_socket__real);
  4499. /* we use close_socket__real here so that coverity can tell that we are
  4500. * really closing these sockets. */
  4501. tor_close_socket__real(fd1);
  4502. tor_close_socket__real(fd2);
  4503. fd1 = fd2 = TOR_INVALID_SOCKET;
  4504. tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
  4505. tor_close_socket__real(fd3);
  4506. tor_close_socket__real(fd4);
  4507. fd3 = fd4 = TOR_INVALID_SOCKET;
  4508. tt_int_op(get_n_open_sockets(), OP_EQ, n);
  4509. done:
  4510. if (SOCKET_OK(fd1))
  4511. tor_close_socket__real(fd1);
  4512. if (SOCKET_OK(fd2))
  4513. tor_close_socket__real(fd2);
  4514. if (SOCKET_OK(fd3))
  4515. tor_close_socket__real(fd3);
  4516. if (SOCKET_OK(fd4))
  4517. tor_close_socket__real(fd4);
  4518. }
  4519. #if 0
  4520. static int
  4521. is_there_a_localhost(int family)
  4522. {
  4523. tor_socket_t s;
  4524. s = tor_open_socket(family, SOCK_STREAM, IPPROTO_TCP);
  4525. tor_assert(SOCKET_OK(s));
  4526. int result = 0;
  4527. if (family == AF_INET) {
  4528. struct sockaddr_in s_in;
  4529. memset(&s_in, 0, sizeof(s_in));
  4530. s_in.sin_family = AF_INET;
  4531. s_in.sin_addr.s_addr = htonl(0x7f000001);
  4532. s_in.sin_port = 0;
  4533. if (bind(s, (void*)&s_in, sizeof(s_in)) == 0) {
  4534. result = 1;
  4535. }
  4536. } else if (family == AF_INET6) {
  4537. struct sockaddr_in6 sin6;
  4538. memset(&sin6, 0, sizeof(sin6));
  4539. sin6.sin6_family = AF_INET6;
  4540. sin6.sin6_addr.s6_addr[15] = 1;
  4541. sin6.sin6_port = 0;
  4542. }
  4543. tor_close_socket(s);
  4544. return result;
  4545. }
  4546. #endif /* 0 */
  4547. /* Test for socketpair and ersatz_socketpair(). We test them both, since
  4548. * the latter is a tolerably good way to exercise tor_accept_socket(). */
  4549. static void
  4550. test_util_socketpair(void *arg)
  4551. {
  4552. const int ersatz = !strcmp(arg, "1");
  4553. int (*const tor_socketpair_fn)(int, int, int, tor_socket_t[2]) =
  4554. ersatz ? tor_ersatz_socketpair : tor_socketpair;
  4555. int n = get_n_open_sockets();
  4556. tor_socket_t fds[2] = {TOR_INVALID_SOCKET, TOR_INVALID_SOCKET};
  4557. const int family = AF_UNIX;
  4558. int socketpair_result = 0;
  4559. socketpair_result = tor_socketpair_fn(family, SOCK_STREAM, 0, fds);
  4560. #ifdef __FreeBSD__
  4561. /* If there is no 127.0.0.1, tor_ersatz_socketpair will and must fail.
  4562. * Otherwise, we risk exposing a socketpair on a routable IP address. (Some
  4563. * BSD jails use a routable address for localhost. Fortunately, they have
  4564. * the real AF_UNIX socketpair.) */
  4565. if (ersatz && socketpair_result < 0) {
  4566. /* In my testing, an IPv6-only FreeBSD jail without ::1 returned EINVAL.
  4567. * Assume we're on a machine without 127.0.0.1 or ::1 and give up now. */
  4568. tt_skip();
  4569. }
  4570. #endif /* defined(__FreeBSD__) */
  4571. tt_int_op(0, OP_EQ, socketpair_result);
  4572. tt_assert(SOCKET_OK(fds[0]));
  4573. tt_assert(SOCKET_OK(fds[1]));
  4574. if (ersatz)
  4575. tt_int_op(get_n_open_sockets(), OP_EQ, n);
  4576. else
  4577. tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
  4578. #ifdef CAN_CHECK_CLOEXEC
  4579. tt_int_op(fd_is_cloexec(fds[0]), OP_EQ, !ersatz);
  4580. tt_int_op(fd_is_cloexec(fds[1]), OP_EQ, !ersatz);
  4581. #endif
  4582. #ifdef CAN_CHECK_NONBLOCK
  4583. tt_int_op(fd_is_nonblocking(fds[0]), OP_EQ, 0);
  4584. tt_int_op(fd_is_nonblocking(fds[1]), OP_EQ, 0);
  4585. #endif
  4586. done:
  4587. if (ersatz) {
  4588. if (SOCKET_OK(fds[0]))
  4589. tor_close_socket_simple(fds[0]);
  4590. if (SOCKET_OK(fds[1]))
  4591. tor_close_socket_simple(fds[1]);
  4592. } else {
  4593. if (SOCKET_OK(fds[0]))
  4594. tor_close_socket(fds[0]);
  4595. if (SOCKET_OK(fds[1]))
  4596. tor_close_socket(fds[1]);
  4597. }
  4598. }
  4599. #undef SOCKET_EPROTO
  4600. static void
  4601. test_util_max_mem(void *arg)
  4602. {
  4603. size_t memory1, memory2;
  4604. int r, r2;
  4605. (void) arg;
  4606. r = get_total_system_memory(&memory1);
  4607. r2 = get_total_system_memory(&memory2);
  4608. tt_int_op(r, OP_EQ, r2);
  4609. tt_uint_op(memory2, OP_EQ, memory1);
  4610. TT_BLATHER(("System memory: %"TOR_PRIuSZ, (memory1)));
  4611. if (r==0) {
  4612. /* You have at least a megabyte. */
  4613. tt_uint_op(memory1, OP_GT, (1<<20));
  4614. } else {
  4615. /* You do not have a petabyte. */
  4616. #if SIZEOF_SIZE_T >= 8
  4617. tt_u64_op(memory1, OP_LT, (UINT64_C(1)<<50));
  4618. #endif
  4619. }
  4620. done:
  4621. ;
  4622. }
  4623. static void
  4624. test_util_dest_validation_edgecase(void *arg)
  4625. {
  4626. (void)arg;
  4627. tt_assert(!string_is_valid_dest(NULL));
  4628. tt_assert(!string_is_valid_dest(""));
  4629. done:
  4630. return;
  4631. }
  4632. static void
  4633. test_util_hostname_validation(void *arg)
  4634. {
  4635. (void)arg;
  4636. // Lets try valid hostnames first.
  4637. tt_assert(string_is_valid_nonrfc_hostname("torproject.org"));
  4638. tt_assert(string_is_valid_nonrfc_hostname("ocw.mit.edu"));
  4639. tt_assert(string_is_valid_nonrfc_hostname("i.4cdn.org"));
  4640. tt_assert(string_is_valid_nonrfc_hostname("stanford.edu"));
  4641. tt_assert(string_is_valid_nonrfc_hostname("multiple-words-with-hypens.jp"));
  4642. // Subdomain name cannot start with '-' or '_'.
  4643. tt_assert(!string_is_valid_nonrfc_hostname("-torproject.org"));
  4644. tt_assert(!string_is_valid_nonrfc_hostname("subdomain.-domain.org"));
  4645. tt_assert(!string_is_valid_nonrfc_hostname("-subdomain.domain.org"));
  4646. tt_assert(!string_is_valid_nonrfc_hostname("___abc.org"));
  4647. // Hostnames cannot contain non-alphanumeric characters.
  4648. tt_assert(!string_is_valid_nonrfc_hostname("%%domain.\\org."));
  4649. tt_assert(!string_is_valid_nonrfc_hostname("***x.net"));
  4650. tt_assert(!string_is_valid_nonrfc_hostname("\xff\xffxyz.org"));
  4651. tt_assert(!string_is_valid_nonrfc_hostname("word1 word2.net"));
  4652. // Test workaround for nytimes.com stupidity, technically invalid,
  4653. // but we allow it since they are big, even though they are failing to
  4654. // comply with a ~30 year old standard.
  4655. tt_assert(string_is_valid_nonrfc_hostname("core3_euw1.fabrik.nytimes.com"));
  4656. // Firefox passes FQDNs with trailing '.'s directly to the SOCKS proxy,
  4657. // which is redundant since the spec states DOMAINNAME addresses are fully
  4658. // qualified. While unusual, this should be tollerated.
  4659. tt_assert(string_is_valid_nonrfc_hostname("core9_euw1.fabrik.nytimes.com."));
  4660. tt_assert(!string_is_valid_nonrfc_hostname(
  4661. "..washingtonpost.is.better.com"));
  4662. tt_assert(!string_is_valid_nonrfc_hostname("so.is..ft.com"));
  4663. tt_assert(!string_is_valid_nonrfc_hostname("..."));
  4664. // XXX: do we allow single-label DNS names?
  4665. // We shouldn't for SOCKS (spec says "contains a fully-qualified domain name"
  4666. // but only test pathologically malformed traling '.' cases for now.
  4667. tt_assert(!string_is_valid_nonrfc_hostname("."));
  4668. tt_assert(!string_is_valid_nonrfc_hostname(".."));
  4669. // IP address strings are not hostnames.
  4670. tt_assert(!string_is_valid_nonrfc_hostname("8.8.8.8"));
  4671. tt_assert(!string_is_valid_nonrfc_hostname("[2a00:1450:401b:800::200e]"));
  4672. tt_assert(!string_is_valid_nonrfc_hostname("2a00:1450:401b:800::200e"));
  4673. // We allow alphanumeric TLDs. For discussion, see ticket #25055.
  4674. tt_assert(string_is_valid_nonrfc_hostname("lucky.13"));
  4675. tt_assert(string_is_valid_nonrfc_hostname("luck.y13"));
  4676. tt_assert(string_is_valid_nonrfc_hostname("luck.y13."));
  4677. // We allow punycode TLDs. For examples, see
  4678. // http://data.iana.org/TLD/tlds-alpha-by-domain.txt
  4679. tt_assert(string_is_valid_nonrfc_hostname("example.xn--l1acc"));
  4680. done:
  4681. return;
  4682. }
  4683. static void
  4684. test_util_ipv4_validation(void *arg)
  4685. {
  4686. (void)arg;
  4687. tt_assert(string_is_valid_ipv4_address("192.168.0.1"));
  4688. tt_assert(string_is_valid_ipv4_address("8.8.8.8"));
  4689. tt_assert(!string_is_valid_ipv4_address("abcd"));
  4690. tt_assert(!string_is_valid_ipv4_address("300.300.300.300"));
  4691. tt_assert(!string_is_valid_ipv4_address("8.8."));
  4692. done:
  4693. return;
  4694. }
  4695. static void
  4696. test_util_ipv6_validation(void *arg)
  4697. {
  4698. (void)arg;
  4699. tt_assert(string_is_valid_ipv6_address("2a00:1450:401b:800::200e"));
  4700. tt_assert(!string_is_valid_ipv6_address("11:22::33:44:"));
  4701. done:
  4702. return;
  4703. }
  4704. static void
  4705. test_util_writepid(void *arg)
  4706. {
  4707. (void) arg;
  4708. char *contents = NULL;
  4709. const char *fname = get_fname("tmp_pid");
  4710. unsigned long pid;
  4711. char c;
  4712. write_pidfile(fname);
  4713. contents = read_file_to_str(fname, 0, NULL);
  4714. tt_assert(contents);
  4715. int n = tor_sscanf(contents, "%lu\n%c", &pid, &c);
  4716. tt_int_op(n, OP_EQ, 1);
  4717. #ifdef _WIN32
  4718. tt_uint_op(pid, OP_EQ, _getpid());
  4719. #else
  4720. tt_uint_op(pid, OP_EQ, getpid());
  4721. #endif
  4722. done:
  4723. tor_free(contents);
  4724. }
  4725. static void
  4726. test_util_get_avail_disk_space(void *arg)
  4727. {
  4728. (void) arg;
  4729. int64_t val;
  4730. /* No answer for nonexistent directory */
  4731. val = tor_get_avail_disk_space("/akljasdfklsajdklasjkldjsa");
  4732. tt_i64_op(val, OP_EQ, -1);
  4733. /* Try the current directory */
  4734. val = tor_get_avail_disk_space(".");
  4735. #if !defined(HAVE_STATVFS) && !defined(_WIN32)
  4736. tt_i64_op(val, OP_EQ, -1); /* You don't have an implementation for this */
  4737. #else
  4738. tt_i64_op(val, OP_GT, 0); /* You have some space. */
  4739. tt_i64_op(val, OP_LT, ((int64_t)1)<<56); /* You don't have a zebibyte */
  4740. #endif /* !defined(HAVE_STATVFS) && !defined(_WIN32) */
  4741. done:
  4742. ;
  4743. }
  4744. static void
  4745. test_util_touch_file(void *arg)
  4746. {
  4747. (void) arg;
  4748. const char *fname = get_fname("touch");
  4749. const time_t now = time(NULL);
  4750. struct stat st;
  4751. write_bytes_to_file(fname, "abc", 3, 1);
  4752. tt_int_op(0, OP_EQ, stat(fname, &st));
  4753. /* A subtle point: the filesystem time is not necessarily equal to the
  4754. * system clock time, since one can be using a monotonic clock, or coarse
  4755. * monotonic clock, or whatever. So we might wind up with an mtime a few
  4756. * microseconds ago. Let's just give it a lot of wiggle room. */
  4757. tt_i64_op(st.st_mtime, OP_GE, now - 1);
  4758. const time_t five_sec_ago = now - 5;
  4759. struct utimbuf u = { five_sec_ago, five_sec_ago };
  4760. tt_int_op(0, OP_EQ, utime(fname, &u));
  4761. tt_int_op(0, OP_EQ, stat(fname, &st));
  4762. /* Let's hope that utime/stat give the same second as a round-trip? */
  4763. tt_i64_op(st.st_mtime, OP_EQ, five_sec_ago);
  4764. /* Finally we can touch the file */
  4765. tt_int_op(0, OP_EQ, touch_file(fname));
  4766. tt_int_op(0, OP_EQ, stat(fname, &st));
  4767. tt_i64_op(st.st_mtime, OP_GE, now-1);
  4768. done:
  4769. ;
  4770. }
  4771. #ifndef _WIN32
  4772. static void
  4773. test_util_pwdb(void *arg)
  4774. {
  4775. (void) arg;
  4776. const struct passwd *me = NULL, *me2, *me3;
  4777. char *name = NULL;
  4778. char *dir = NULL;
  4779. /* Uncached case. */
  4780. /* Let's assume that we exist. */
  4781. me = tor_getpwuid(getuid());
  4782. tt_ptr_op(me, OP_NE, NULL);
  4783. name = tor_strdup(me->pw_name);
  4784. /* Uncached case */
  4785. me2 = tor_getpwnam(name);
  4786. tt_ptr_op(me2, OP_NE, NULL);
  4787. tt_int_op(me2->pw_uid, OP_EQ, getuid());
  4788. /* Cached case */
  4789. me3 = tor_getpwuid(getuid());
  4790. tt_ptr_op(me3, OP_NE, NULL);
  4791. tt_str_op(me3->pw_name, OP_EQ, name);
  4792. me3 = tor_getpwnam(name);
  4793. tt_ptr_op(me3, OP_NE, NULL);
  4794. tt_int_op(me3->pw_uid, OP_EQ, getuid());
  4795. dir = get_user_homedir(name);
  4796. tt_ptr_op(dir, OP_NE, NULL);
  4797. /* Try failing cases. First find a user that doesn't exist by name */
  4798. char randbytes[4];
  4799. char badname[9];
  4800. int i, found=0;
  4801. for (i = 0; i < 100; ++i) {
  4802. crypto_rand(randbytes, sizeof(randbytes));
  4803. base16_encode(badname, sizeof(badname), randbytes, sizeof(randbytes));
  4804. if (tor_getpwnam(badname) == NULL) {
  4805. found = 1;
  4806. break;
  4807. }
  4808. }
  4809. tt_assert(found);
  4810. tor_free(dir);
  4811. /* We should do a LOG_ERR */
  4812. setup_full_capture_of_logs(LOG_ERR);
  4813. dir = get_user_homedir(badname);
  4814. tt_ptr_op(dir, OP_EQ, NULL);
  4815. expect_log_msg_containing("not found");
  4816. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  4817. teardown_capture_of_logs();
  4818. /* Now try to find a user that doesn't exist by ID. */
  4819. found = 0;
  4820. for (i = 0; i < 1000; ++i) {
  4821. uid_t u;
  4822. crypto_rand((char*)&u, sizeof(u));
  4823. if (tor_getpwuid(u) == NULL) {
  4824. found = 1;
  4825. break;
  4826. }
  4827. }
  4828. tt_assert(found);
  4829. done:
  4830. tor_free(name);
  4831. tor_free(dir);
  4832. teardown_capture_of_logs();
  4833. }
  4834. #endif /* !defined(_WIN32) */
  4835. static void
  4836. test_util_calloc_check(void *arg)
  4837. {
  4838. (void) arg;
  4839. /* Easy cases that are good. */
  4840. tt_assert(size_mul_check(0,0));
  4841. tt_assert(size_mul_check(0,100));
  4842. tt_assert(size_mul_check(100,0));
  4843. tt_assert(size_mul_check(100,100));
  4844. /* Harder cases that are still good. */
  4845. tt_assert(size_mul_check(SIZE_MAX, 1));
  4846. tt_assert(size_mul_check(1, SIZE_MAX));
  4847. tt_assert(size_mul_check(SIZE_MAX / 10, 9));
  4848. tt_assert(size_mul_check(11, SIZE_MAX / 12));
  4849. const size_t sqrt_size_max_p1 = ((size_t)1) << (sizeof(size_t) * 4);
  4850. tt_assert(size_mul_check(sqrt_size_max_p1, sqrt_size_max_p1 - 1));
  4851. /* Cases that overflow */
  4852. tt_assert(! size_mul_check(SIZE_MAX, 2));
  4853. tt_assert(! size_mul_check(2, SIZE_MAX));
  4854. tt_assert(! size_mul_check(SIZE_MAX / 10, 11));
  4855. tt_assert(! size_mul_check(11, SIZE_MAX / 10));
  4856. tt_assert(! size_mul_check(SIZE_MAX / 8, 9));
  4857. tt_assert(! size_mul_check(sqrt_size_max_p1, sqrt_size_max_p1));
  4858. done:
  4859. ;
  4860. }
  4861. static void
  4862. test_util_monotonic_time(void *arg)
  4863. {
  4864. (void)arg;
  4865. monotime_t mt1, mt2;
  4866. monotime_coarse_t mtc1, mtc2;
  4867. uint64_t nsec1, nsec2, usec1, msec1;
  4868. uint64_t nsecc1, nsecc2, usecc1, msecc1;
  4869. uint32_t stamp1, stamp2;
  4870. monotime_init();
  4871. monotime_get(&mt1);
  4872. monotime_coarse_get(&mtc1);
  4873. nsec1 = monotime_absolute_nsec();
  4874. usec1 = monotime_absolute_usec();
  4875. msec1 = monotime_absolute_msec();
  4876. nsecc1 = monotime_coarse_absolute_nsec();
  4877. usecc1 = monotime_coarse_absolute_usec();
  4878. msecc1 = monotime_coarse_absolute_msec();
  4879. stamp1 = monotime_coarse_to_stamp(&mtc1);
  4880. tor_sleep_msec(200);
  4881. monotime_get(&mt2);
  4882. monotime_coarse_get(&mtc2);
  4883. nsec2 = monotime_absolute_nsec();
  4884. nsecc2 = monotime_coarse_absolute_nsec();
  4885. stamp2 = monotime_coarse_to_stamp(&mtc2);
  4886. /* We need to be a little careful here since we don't know the system load.
  4887. */
  4888. tt_i64_op(monotime_diff_msec(&mt1, &mt2), OP_GE, 175);
  4889. tt_i64_op(monotime_diff_msec(&mt1, &mt2), OP_LT, 1000);
  4890. tt_i64_op(monotime_coarse_diff_msec(&mtc1, &mtc2), OP_GE, 125);
  4891. tt_i64_op(monotime_coarse_diff_msec(&mtc1, &mtc2), OP_LT, 1000);
  4892. tt_u64_op(nsec2-nsec1, OP_GE, 175000000);
  4893. tt_u64_op(nsec2-nsec1, OP_LT, 1000000000);
  4894. tt_u64_op(nsecc2-nsecc1, OP_GE, 125000000);
  4895. tt_u64_op(nsecc2-nsecc1, OP_LT, 1000000000);
  4896. tt_u64_op(msec1, OP_GE, nsec1 / 1000000);
  4897. tt_u64_op(usec1, OP_GE, nsec1 / 1000);
  4898. tt_u64_op(msecc1, OP_GE, nsecc1 / 1000000);
  4899. tt_u64_op(usecc1, OP_GE, nsecc1 / 1000);
  4900. tt_u64_op(msec1, OP_LE, nsec1 / 1000000 + 10);
  4901. tt_u64_op(usec1, OP_LE, nsec1 / 1000 + 10000);
  4902. tt_u64_op(msecc1, OP_LE, nsecc1 / 1000000 + 10);
  4903. tt_u64_op(usecc1, OP_LE, nsecc1 / 1000 + 10000);
  4904. uint64_t coarse_stamp_diff =
  4905. monotime_coarse_stamp_units_to_approx_msec(stamp2-stamp1);
  4906. tt_u64_op(coarse_stamp_diff, OP_GE, 120);
  4907. tt_u64_op(coarse_stamp_diff, OP_LE, 1200);
  4908. {
  4909. uint64_t units = monotime_msec_to_approx_coarse_stamp_units(5000);
  4910. uint64_t ms = monotime_coarse_stamp_units_to_approx_msec(units);
  4911. tt_u64_op(ms, OP_GE, 4950);
  4912. tt_u64_op(ms, OP_LT, 5050);
  4913. }
  4914. done:
  4915. ;
  4916. }
  4917. static void
  4918. test_util_monotonic_time_ratchet(void *arg)
  4919. {
  4920. (void)arg;
  4921. monotime_init();
  4922. monotime_reset_ratchets_for_testing();
  4923. /* win32, performance counter ratchet. */
  4924. tt_i64_op(100, OP_EQ, ratchet_performance_counter(100));
  4925. tt_i64_op(101, OP_EQ, ratchet_performance_counter(101));
  4926. tt_i64_op(2000, OP_EQ, ratchet_performance_counter(2000));
  4927. tt_i64_op(2000, OP_EQ, ratchet_performance_counter(100));
  4928. tt_i64_op(2005, OP_EQ, ratchet_performance_counter(105));
  4929. tt_i64_op(3005, OP_EQ, ratchet_performance_counter(1105));
  4930. tt_i64_op(3005, OP_EQ, ratchet_performance_counter(1000));
  4931. tt_i64_op(3010, OP_EQ, ratchet_performance_counter(1005));
  4932. /* win32, GetTickCounts32 ratchet-and-rollover-detector. */
  4933. const int64_t R = ((int64_t)1) << 32;
  4934. tt_i64_op(5, OP_EQ, ratchet_coarse_performance_counter(5));
  4935. tt_i64_op(1000, OP_EQ, ratchet_coarse_performance_counter(1000));
  4936. tt_i64_op(5+R, OP_EQ, ratchet_coarse_performance_counter(5));
  4937. tt_i64_op(10+R, OP_EQ, ratchet_coarse_performance_counter(10));
  4938. tt_i64_op(4+R*2, OP_EQ, ratchet_coarse_performance_counter(4));
  4939. /* gettimeofday regular ratchet. */
  4940. struct timeval tv_in = {0,0}, tv_out;
  4941. tv_in.tv_usec = 9000;
  4942. ratchet_timeval(&tv_in, &tv_out);
  4943. tt_int_op(tv_out.tv_usec, OP_EQ, 9000);
  4944. tt_i64_op(tv_out.tv_sec, OP_EQ, 0);
  4945. tv_in.tv_sec = 1337;
  4946. tv_in.tv_usec = 0;
  4947. ratchet_timeval(&tv_in, &tv_out);
  4948. tt_int_op(tv_out.tv_usec, OP_EQ, 0);
  4949. tt_i64_op(tv_out.tv_sec, OP_EQ, 1337);
  4950. tv_in.tv_sec = 1336;
  4951. tv_in.tv_usec = 500000;
  4952. ratchet_timeval(&tv_in, &tv_out);
  4953. tt_int_op(tv_out.tv_usec, OP_EQ, 0);
  4954. tt_i64_op(tv_out.tv_sec, OP_EQ, 1337);
  4955. tv_in.tv_sec = 1337;
  4956. tv_in.tv_usec = 0;
  4957. ratchet_timeval(&tv_in, &tv_out);
  4958. tt_int_op(tv_out.tv_usec, OP_EQ, 500000);
  4959. tt_i64_op(tv_out.tv_sec, OP_EQ, 1337);
  4960. tv_in.tv_sec = 1337;
  4961. tv_in.tv_usec = 600000;
  4962. ratchet_timeval(&tv_in, &tv_out);
  4963. tt_int_op(tv_out.tv_usec, OP_EQ, 100000);
  4964. tt_i64_op(tv_out.tv_sec, OP_EQ, 1338);
  4965. tv_in.tv_sec = 1000;
  4966. tv_in.tv_usec = 1000;
  4967. ratchet_timeval(&tv_in, &tv_out);
  4968. tt_int_op(tv_out.tv_usec, OP_EQ, 100000);
  4969. tt_i64_op(tv_out.tv_sec, OP_EQ, 1338);
  4970. tv_in.tv_sec = 2000;
  4971. tv_in.tv_usec = 2000;
  4972. ratchet_timeval(&tv_in, &tv_out);
  4973. tt_int_op(tv_out.tv_usec, OP_EQ, 101000);
  4974. tt_i64_op(tv_out.tv_sec, OP_EQ, 2338);
  4975. done:
  4976. ;
  4977. }
  4978. static void
  4979. test_util_monotonic_time_zero(void *arg)
  4980. {
  4981. (void) arg;
  4982. monotime_t t1;
  4983. monotime_coarse_t ct1;
  4984. monotime_init();
  4985. /* Check 1: The current time is not zero. */
  4986. monotime_get(&t1);
  4987. monotime_coarse_get(&ct1);
  4988. tt_assert(!monotime_is_zero(&t1));
  4989. tt_assert(!monotime_coarse_is_zero(&ct1));
  4990. /* Check 2: The _zero() makes the time zero. */
  4991. monotime_zero(&t1);
  4992. monotime_coarse_zero(&ct1);
  4993. tt_assert(monotime_is_zero(&t1));
  4994. tt_assert(monotime_coarse_is_zero(&ct1));
  4995. done:
  4996. ;
  4997. }
  4998. static void
  4999. test_util_monotonic_time_add_msec(void *arg)
  5000. {
  5001. (void) arg;
  5002. monotime_t t1, t2;
  5003. monotime_coarse_t ct1, ct2;
  5004. monotime_init();
  5005. monotime_get(&t1);
  5006. monotime_coarse_get(&ct1);
  5007. /* adding zero does nothing */
  5008. monotime_add_msec(&t2, &t1, 0);
  5009. monotime_coarse_add_msec(&ct2, &ct1, 0);
  5010. tt_i64_op(monotime_diff_msec(&t1, &t2), OP_EQ, 0);
  5011. tt_i64_op(monotime_coarse_diff_msec(&ct1, &ct2), OP_EQ, 0);
  5012. /* Add 1337 msec; see if the diff function agree */
  5013. monotime_add_msec(&t2, &t1, 1337);
  5014. monotime_coarse_add_msec(&ct2, &ct1, 1337);
  5015. tt_i64_op(monotime_diff_msec(&t1, &t2), OP_EQ, 1337);
  5016. tt_i64_op(monotime_coarse_diff_msec(&ct1, &ct2), OP_EQ, 1337);
  5017. // The 32-bit variant must be within 1% of the regular one.
  5018. tt_int_op(monotime_coarse_diff_msec32_(&ct1, &ct2), OP_GT, 1323);
  5019. tt_int_op(monotime_coarse_diff_msec32_(&ct1, &ct2), OP_LT, 1350);
  5020. /* Add 1337 msec twice more; make sure that any second rollover issues
  5021. * worked. */
  5022. monotime_add_msec(&t2, &t2, 1337);
  5023. monotime_coarse_add_msec(&ct2, &ct2, 1337);
  5024. monotime_add_msec(&t2, &t2, 1337);
  5025. monotime_coarse_add_msec(&ct2, &ct2, 1337);
  5026. tt_i64_op(monotime_diff_msec(&t1, &t2), OP_EQ, 1337*3);
  5027. tt_i64_op(monotime_coarse_diff_msec(&ct1, &ct2), OP_EQ, 1337*3);
  5028. tt_int_op(monotime_coarse_diff_msec32_(&ct1, &ct2), OP_GT, 3970);
  5029. tt_int_op(monotime_coarse_diff_msec32_(&ct1, &ct2), OP_LT, 4051);
  5030. done:
  5031. ;
  5032. }
  5033. static void
  5034. test_util_nowrap_math(void *arg)
  5035. {
  5036. (void)arg;
  5037. tt_u64_op(0, OP_EQ, tor_add_u32_nowrap(0, 0));
  5038. tt_u64_op(1, OP_EQ, tor_add_u32_nowrap(0, 1));
  5039. tt_u64_op(1, OP_EQ, tor_add_u32_nowrap(1, 0));
  5040. tt_u64_op(4, OP_EQ, tor_add_u32_nowrap(2, 2));
  5041. tt_u64_op(UINT32_MAX, OP_EQ, tor_add_u32_nowrap(UINT32_MAX-1, 2));
  5042. tt_u64_op(UINT32_MAX, OP_EQ, tor_add_u32_nowrap(2, UINT32_MAX-1));
  5043. tt_u64_op(UINT32_MAX, OP_EQ, tor_add_u32_nowrap(UINT32_MAX, UINT32_MAX));
  5044. done:
  5045. ;
  5046. }
  5047. static void
  5048. test_util_htonll(void *arg)
  5049. {
  5050. (void)arg;
  5051. #ifdef WORDS_BIGENDIAN
  5052. const uint64_t res_be = 0x8877665544332211;
  5053. #else
  5054. const uint64_t res_le = 0x1122334455667788;
  5055. #endif
  5056. tt_u64_op(0, OP_EQ, tor_htonll(0));
  5057. tt_u64_op(0, OP_EQ, tor_ntohll(0));
  5058. tt_u64_op(UINT64_MAX, OP_EQ, tor_htonll(UINT64_MAX));
  5059. tt_u64_op(UINT64_MAX, OP_EQ, tor_ntohll(UINT64_MAX));
  5060. #ifdef WORDS_BIGENDIAN
  5061. tt_u64_op(res_be, OP_EQ, tor_htonll(0x8877665544332211));
  5062. tt_u64_op(res_be, OP_EQ, tor_ntohll(0x8877665544332211));
  5063. #else
  5064. tt_u64_op(res_le, OP_EQ, tor_htonll(0x8877665544332211));
  5065. tt_u64_op(res_le, OP_EQ, tor_ntohll(0x8877665544332211));
  5066. #endif /* defined(WORDS_BIGENDIAN) */
  5067. done:
  5068. ;
  5069. }
  5070. static void
  5071. test_util_get_unquoted_path(void *arg)
  5072. {
  5073. (void)arg;
  5074. char *r = NULL;
  5075. r = get_unquoted_path("\""); // "
  5076. tt_ptr_op(r, OP_EQ, NULL);
  5077. tor_free(r);
  5078. r = get_unquoted_path("\"\"\""); // """
  5079. tt_ptr_op(r, OP_EQ, NULL);
  5080. tor_free(r);
  5081. r = get_unquoted_path("\\\""); // \"
  5082. tt_ptr_op(r, OP_EQ, NULL);
  5083. tor_free(r);
  5084. r = get_unquoted_path("\\\"\\\""); // \"\"
  5085. tt_ptr_op(r, OP_EQ, NULL);
  5086. tor_free(r);
  5087. r = get_unquoted_path("A\\B\\C\""); // A\B\C"
  5088. tt_ptr_op(r, OP_EQ, NULL);
  5089. tor_free(r);
  5090. r = get_unquoted_path("\"A\\B\\C"); // "A\B\C
  5091. tt_ptr_op(r, OP_EQ, NULL);
  5092. tor_free(r);
  5093. r = get_unquoted_path("\"A\\B\"C\""); // "A\B"C"
  5094. tt_ptr_op(r, OP_EQ, NULL);
  5095. tor_free(r);
  5096. r = get_unquoted_path("A\\B\"C"); // A\B"C
  5097. tt_ptr_op(r, OP_EQ, NULL);
  5098. tor_free(r);
  5099. r = get_unquoted_path("");
  5100. tt_str_op(r, OP_EQ, "");
  5101. tor_free(r);
  5102. r = get_unquoted_path("\"\""); // ""
  5103. tt_str_op(r, OP_EQ, "");
  5104. tor_free(r);
  5105. r = get_unquoted_path("A\\B\\C"); // A\B\C
  5106. tt_str_op(r, OP_EQ, "A\\B\\C"); // A\B\C
  5107. tor_free(r);
  5108. r = get_unquoted_path("\"A\\B\\C\""); // "A\B\C"
  5109. tt_str_op(r, OP_EQ, "A\\B\\C"); // A\B\C
  5110. tor_free(r);
  5111. r = get_unquoted_path("\"\\\""); // "\"
  5112. tt_str_op(r, OP_EQ, "\\"); // \ /* comment to prevent line continuation */
  5113. tor_free(r);
  5114. r = get_unquoted_path("\"\\\"\""); // "\""
  5115. tt_str_op(r, OP_EQ, "\""); // "
  5116. tor_free(r);
  5117. r = get_unquoted_path("\"A\\B\\C\\\"\""); // "A\B\C\""
  5118. tt_str_op(r, OP_EQ, "A\\B\\C\""); // A\B\C"
  5119. tor_free(r);
  5120. r = get_unquoted_path("A\\B\\\"C"); // A\B\"C
  5121. tt_str_op(r, OP_EQ, "A\\B\"C"); // A\B"C
  5122. tor_free(r);
  5123. r = get_unquoted_path("\"A\\B\\\"C\""); // "A\B\"C"
  5124. tt_str_op(r, OP_EQ, "A\\B\"C"); // A\B"C
  5125. done:
  5126. tor_free(r);
  5127. }
  5128. static void
  5129. test_util_log_mallinfo(void *arg)
  5130. {
  5131. (void)arg;
  5132. char *log1 = NULL, *log2 = NULL, *mem = NULL;
  5133. #ifdef HAVE_MALLINFO
  5134. setup_capture_of_logs(LOG_INFO);
  5135. tor_log_mallinfo(LOG_INFO);
  5136. expect_single_log_msg_containing("mallinfo() said: ");
  5137. mock_saved_log_entry_t *lg = smartlist_get(mock_saved_logs(), 0);
  5138. log1 = tor_strdup(lg->generated_msg);
  5139. mock_clean_saved_logs();
  5140. mem = tor_malloc(8192);
  5141. tor_log_mallinfo(LOG_INFO);
  5142. expect_single_log_msg_containing("mallinfo() said: ");
  5143. lg = smartlist_get(mock_saved_logs(), 0);
  5144. log2 = tor_strdup(lg->generated_msg);
  5145. /* Make sure that the amount of used memory increased. */
  5146. const char *used1 = strstr(log1, "uordblks=");
  5147. const char *used2 = strstr(log2, "uordblks=");
  5148. tt_assert(used1);
  5149. tt_assert(used2);
  5150. used1 += strlen("uordblks=");
  5151. used2 += strlen("uordblks=");
  5152. int ok1, ok2;
  5153. char *next1 = NULL, *next2 = NULL;
  5154. uint64_t mem1 = tor_parse_uint64(used1, 10, 0, UINT64_MAX, &ok1, &next1);
  5155. uint64_t mem2 = tor_parse_uint64(used2, 10, 0, UINT64_MAX, &ok2, &next2);
  5156. tt_assert(ok1);
  5157. tt_assert(ok2);
  5158. tt_assert(next1);
  5159. tt_assert(next2);
  5160. if (mem2 == 0) {
  5161. /* This is a fake mallinfo that doesn't actually fill in its outputs. */
  5162. tt_u64_op(mem1, OP_EQ, 0);
  5163. } else {
  5164. tt_u64_op(mem1, OP_LT, mem2);
  5165. }
  5166. #else
  5167. tt_skip();
  5168. #endif
  5169. done:
  5170. teardown_capture_of_logs();
  5171. tor_free(log1);
  5172. tor_free(log2);
  5173. tor_free(mem);
  5174. }
  5175. #define UTIL_LEGACY(name) \
  5176. { #name, test_util_ ## name , 0, NULL, NULL }
  5177. #define UTIL_TEST(name, flags) \
  5178. { #name, test_util_ ## name, flags, NULL, NULL }
  5179. #define COMPRESS(name, identifier) \
  5180. { "compress/" #name, test_util_compress, 0, &compress_setup, \
  5181. (char*)(identifier) }
  5182. #define COMPRESS_CONCAT(name, identifier) \
  5183. { "compress_concat/" #name, test_util_decompress_concatenated, 0, \
  5184. &compress_setup, \
  5185. (char*)(identifier) }
  5186. #define COMPRESS_JUNK(name, identifier) \
  5187. { "compress_junk/" #name, test_util_decompress_junk, 0, \
  5188. &compress_setup, \
  5189. (char*)(identifier) }
  5190. #define COMPRESS_DOS(name, identifier) \
  5191. { "compress_dos/" #name, test_util_decompress_dos, 0, \
  5192. &compress_setup, \
  5193. (char*)(identifier) }
  5194. #ifdef _WIN32
  5195. #define UTIL_TEST_NO_WIN(n, f) { #n, NULL, TT_SKIP, NULL, NULL }
  5196. #define UTIL_TEST_WIN_ONLY(n, f) UTIL_TEST(n, (f))
  5197. #define UTIL_LEGACY_NO_WIN(n) UTIL_TEST_NO_WIN(n, 0)
  5198. #else
  5199. #define UTIL_TEST_NO_WIN(n, f) UTIL_TEST(n, (f))
  5200. #define UTIL_TEST_WIN_ONLY(n, f) { #n, NULL, TT_SKIP, NULL, NULL }
  5201. #define UTIL_LEGACY_NO_WIN(n) UTIL_LEGACY(n)
  5202. #endif /* defined(_WIN32) */
  5203. struct testcase_t util_tests[] = {
  5204. UTIL_LEGACY(time),
  5205. UTIL_TEST(parse_http_time, 0),
  5206. UTIL_LEGACY(config_line),
  5207. UTIL_LEGACY(config_line_quotes),
  5208. UTIL_LEGACY(config_line_comment_character),
  5209. UTIL_LEGACY(config_line_escaped_content),
  5210. UTIL_LEGACY(config_line_crlf),
  5211. UTIL_LEGACY_NO_WIN(expand_filename),
  5212. UTIL_LEGACY(escape_string_socks),
  5213. UTIL_LEGACY(string_is_key_value),
  5214. UTIL_LEGACY(strmisc),
  5215. UTIL_TEST(parse_integer, 0),
  5216. UTIL_LEGACY(pow2),
  5217. COMPRESS(zlib, "deflate"),
  5218. COMPRESS(gzip, "gzip"),
  5219. COMPRESS(lzma, "x-tor-lzma"),
  5220. COMPRESS(zstd, "x-zstd"),
  5221. COMPRESS(zstd_nostatic, "x-zstd:nostatic"),
  5222. COMPRESS(none, "identity"),
  5223. COMPRESS_CONCAT(zlib, "deflate"),
  5224. COMPRESS_CONCAT(gzip, "gzip"),
  5225. COMPRESS_CONCAT(lzma, "x-tor-lzma"),
  5226. COMPRESS_CONCAT(zstd, "x-zstd"),
  5227. COMPRESS_CONCAT(zstd_nostatic, "x-zstd:nostatic"),
  5228. COMPRESS_CONCAT(none, "identity"),
  5229. COMPRESS_JUNK(zlib, "deflate"),
  5230. COMPRESS_JUNK(gzip, "gzip"),
  5231. COMPRESS_JUNK(lzma, "x-tor-lzma"),
  5232. COMPRESS_DOS(zlib, "deflate"),
  5233. COMPRESS_DOS(gzip, "gzip"),
  5234. COMPRESS_DOS(lzma, "x-tor-lzma"),
  5235. COMPRESS_DOS(zstd, "x-zstd"),
  5236. COMPRESS_DOS(zstd_nostatic, "x-zstd:nostatic"),
  5237. UTIL_TEST(gzip_compression_bomb, TT_FORK),
  5238. UTIL_LEGACY(datadir),
  5239. UTIL_LEGACY(memarea),
  5240. UTIL_LEGACY(control_formats),
  5241. UTIL_LEGACY(mmap),
  5242. UTIL_TEST(sscanf, TT_FORK),
  5243. UTIL_LEGACY(format_time_interval),
  5244. UTIL_LEGACY(path_is_relative),
  5245. UTIL_LEGACY(strtok),
  5246. UTIL_LEGACY(di_ops),
  5247. UTIL_TEST(di_map, 0),
  5248. UTIL_TEST(round_to_next_multiple_of, 0),
  5249. UTIL_TEST(laplace, 0),
  5250. UTIL_TEST(clamp_double_to_int64, 0),
  5251. UTIL_TEST(find_str_at_start_of_line, 0),
  5252. UTIL_TEST(string_is_C_identifier, 0),
  5253. UTIL_TEST(string_is_utf8, 0),
  5254. UTIL_TEST(asprintf, 0),
  5255. UTIL_TEST(listdir, 0),
  5256. UTIL_TEST(parent_dir, 0),
  5257. UTIL_TEST(ftruncate, 0),
  5258. UTIL_TEST(nowrap_math, 0),
  5259. UTIL_TEST(num_cpus, 0),
  5260. UTIL_TEST_WIN_ONLY(load_win_lib, 0),
  5261. UTIL_TEST(format_hex_number, 0),
  5262. UTIL_TEST(format_dec_number, 0),
  5263. UTIL_TEST(n_bits_set, 0),
  5264. UTIL_TEST(eat_whitespace, 0),
  5265. UTIL_TEST(sl_new_from_text_lines, 0),
  5266. UTIL_TEST(envnames, 0),
  5267. UTIL_TEST(make_environment, 0),
  5268. UTIL_TEST(set_env_var_in_sl, 0),
  5269. UTIL_TEST(read_file_eof_tiny_limit, 0),
  5270. UTIL_TEST(read_file_eof_one_loop_a, 0),
  5271. UTIL_TEST(read_file_eof_one_loop_b, 0),
  5272. UTIL_TEST(read_file_eof_two_loops, 0),
  5273. UTIL_TEST(read_file_eof_two_loops_b, 0),
  5274. UTIL_TEST(read_file_eof_zero_bytes, 0),
  5275. UTIL_TEST(write_chunks_to_file, 0),
  5276. UTIL_TEST(mathlog, 0),
  5277. UTIL_TEST(fraction, 0),
  5278. UTIL_TEST(weak_random, 0),
  5279. { "tor_isinf", test_tor_isinf, TT_FORK, NULL, NULL },
  5280. { "socket_ipv4", test_util_socket, TT_FORK, &passthrough_setup,
  5281. (void*)"4" },
  5282. { "socket_ipv6", test_util_socket, TT_FORK,
  5283. &passthrough_setup, (void*)"6" },
  5284. { "socketpair", test_util_socketpair, TT_FORK, &passthrough_setup,
  5285. (void*)"0" },
  5286. { "socketpair_ersatz", test_util_socketpair, TT_FORK,
  5287. &passthrough_setup, (void*)"1" },
  5288. UTIL_TEST(max_mem, 0),
  5289. UTIL_TEST(hostname_validation, 0),
  5290. UTIL_TEST(dest_validation_edgecase, 0),
  5291. UTIL_TEST(ipv4_validation, 0),
  5292. UTIL_TEST(ipv6_validation, 0),
  5293. UTIL_TEST(writepid, 0),
  5294. UTIL_TEST(get_avail_disk_space, 0),
  5295. UTIL_TEST(touch_file, 0),
  5296. UTIL_TEST_NO_WIN(pwdb, TT_FORK),
  5297. UTIL_TEST(calloc_check, 0),
  5298. UTIL_TEST(monotonic_time, 0),
  5299. UTIL_TEST(monotonic_time_ratchet, TT_FORK),
  5300. UTIL_TEST(monotonic_time_zero, 0),
  5301. UTIL_TEST(monotonic_time_add_msec, 0),
  5302. UTIL_TEST(htonll, 0),
  5303. UTIL_TEST(get_unquoted_path, 0),
  5304. UTIL_TEST(log_mallinfo, 0),
  5305. END_OF_TESTCASES
  5306. };