test_util.c 192 KB

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