123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016 |
- /* Copyright (c) 2001-2004, Roger Dingledine.
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2015, The Tor Project, Inc. */
- /* See LICENSE for licensing information */
- #include "orconfig.h"
- #define COMPAT_PRIVATE
- #define CONTROL_PRIVATE
- #define MEMPOOL_PRIVATE
- #define UTIL_PRIVATE
- #include "or.h"
- #include "config.h"
- #include "control.h"
- #include "test.h"
- #ifdef ENABLE_MEMPOOLS
- #include "mempool.h"
- #endif /* ENABLE_MEMPOOLS */
- #include "memarea.h"
- #include "util_process.h"
- #ifdef _WIN32
- #include <tchar.h>
- #endif
- #include <math.h>
- #include <ctype.h>
- /* XXXX this is a minimal wrapper to make the unit tests compile with the
- * changed tor_timegm interface. */
- static time_t
- tor_timegm_wrapper(const struct tm *tm)
- {
- time_t t;
- if (tor_timegm(tm, &t) < 0)
- return -1;
- return t;
- }
- #define tor_timegm tor_timegm_wrapper
- static void
- test_util_read_until_eof_impl(const char *fname, size_t file_len,
- size_t read_limit)
- {
- char *fifo_name = NULL;
- char *test_str = NULL;
- char *str = NULL;
- size_t sz = 9999999;
- int fd = -1;
- int r;
- fifo_name = tor_strdup(get_fname(fname));
- test_str = tor_malloc(file_len);
- crypto_rand(test_str, file_len);
- r = write_bytes_to_file(fifo_name, test_str, file_len, 1);
- tt_int_op(r, OP_EQ, 0);
- fd = open(fifo_name, O_RDONLY|O_BINARY);
- tt_int_op(fd, OP_GE, 0);
- str = read_file_to_str_until_eof(fd, read_limit, &sz);
- tt_assert(str != NULL);
- if (read_limit < file_len)
- tt_int_op(sz, OP_EQ, read_limit);
- else
- tt_int_op(sz, OP_EQ, file_len);
- tt_mem_op(test_str, OP_EQ, str, sz);
- tt_int_op(str[sz], OP_EQ, '\0');
- done:
- unlink(fifo_name);
- tor_free(fifo_name);
- tor_free(test_str);
- tor_free(str);
- if (fd >= 0)
- close(fd);
- }
- static void
- test_util_read_file_eof_tiny_limit(void *arg)
- {
- (void)arg;
- // purposely set limit shorter than what we wrote to the FIFO to
- // test the maximum, and that it puts the NUL in the right spot
- test_util_read_until_eof_impl("tor_test_fifo_tiny", 5, 4);
- }
- static void
- test_util_read_file_eof_one_loop_a(void *arg)
- {
- (void)arg;
- test_util_read_until_eof_impl("tor_test_fifo_1ka", 1024, 1023);
- }
- static void
- test_util_read_file_eof_one_loop_b(void *arg)
- {
- (void)arg;
- test_util_read_until_eof_impl("tor_test_fifo_1kb", 1024, 1024);
- }
- static void
- test_util_read_file_eof_two_loops(void *arg)
- {
- (void)arg;
- // write more than 1024 bytes to the FIFO to test two passes through
- // the loop in the method; if the re-alloc size is changed this
- // should be updated as well.
- test_util_read_until_eof_impl("tor_test_fifo_2k", 2048, 10000);
- }
- static void
- test_util_read_file_eof_two_loops_b(void *arg)
- {
- (void)arg;
- test_util_read_until_eof_impl("tor_test_fifo_2kb", 2048, 2048);
- }
- static void
- test_util_read_file_eof_zero_bytes(void *arg)
- {
- (void)arg;
- // zero-byte fifo
- test_util_read_until_eof_impl("tor_test_fifo_empty", 0, 10000);
- }
- /* Test the basic expected behaviour for write_chunks_to_file.
- * NOTE: This will need to be updated if we ever change the tempfile location
- * or extension */
- static void
- test_util_write_chunks_to_file(void *arg)
- {
- char *fname = NULL;
- char *tempname = NULL;
- char *str = NULL;
- int r;
- struct stat st;
- /* These should be two different sizes to ensure the data is different
- * between the data file and the temp file's 'known string' */
- int temp_str_len = 1024;
- int data_str_len = 512;
- char *data_str = tor_malloc(data_str_len);
- char *temp_str = tor_malloc(temp_str_len);
- smartlist_t *chunks = smartlist_new();
- sized_chunk_t c = {data_str, data_str_len/2};
- sized_chunk_t c2 = {data_str + data_str_len/2, data_str_len/2};
- (void)arg;
- crypto_rand(temp_str, temp_str_len);
- crypto_rand(data_str, data_str_len);
- // Ensure it can write multiple chunks
- smartlist_add(chunks, &c);
- smartlist_add(chunks, &c2);
- /*
- * Check if it writes using a tempfile
- */
- fname = tor_strdup(get_fname("write_chunks_with_tempfile"));
- tor_asprintf(&tempname, "%s.tmp", fname);
- // write a known string to a file where the tempfile will be
- r = write_bytes_to_file(tempname, temp_str, temp_str_len, 1);
- tt_int_op(r, OP_EQ, 0);
- // call write_chunks_to_file
- r = write_chunks_to_file(fname, chunks, 1, 0);
- tt_int_op(r, OP_EQ, 0);
- // assert the file has been written (expected size)
- str = read_file_to_str(fname, RFTS_BIN, &st);
- tt_assert(str != NULL);
- tt_u64_op((uint64_t)st.st_size, OP_EQ, data_str_len);
- tt_mem_op(data_str, OP_EQ, str, data_str_len);
- tor_free(str);
- // assert that the tempfile is removed (should not leave artifacts)
- str = read_file_to_str(tempname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
- tt_assert(str == NULL);
- // Remove old testfile for second test
- r = unlink(fname);
- tt_int_op(r, OP_EQ, 0);
- tor_free(fname);
- tor_free(tempname);
- /*
- * Check if it skips using a tempfile with flags
- */
- fname = tor_strdup(get_fname("write_chunks_with_no_tempfile"));
- tor_asprintf(&tempname, "%s.tmp", fname);
- // write a known string to a file where the tempfile will be
- r = write_bytes_to_file(tempname, temp_str, temp_str_len, 1);
- tt_int_op(r, OP_EQ, 0);
- // call write_chunks_to_file with no_tempfile = true
- r = write_chunks_to_file(fname, chunks, 1, 1);
- tt_int_op(r, OP_EQ, 0);
- // assert the file has been written (expected size)
- str = read_file_to_str(fname, RFTS_BIN, &st);
- tt_assert(str != NULL);
- tt_u64_op((uint64_t)st.st_size, OP_EQ, data_str_len);
- tt_mem_op(data_str, OP_EQ, str, data_str_len);
- tor_free(str);
- // assert the tempfile still contains the known string
- str = read_file_to_str(tempname, RFTS_BIN, &st);
- tt_assert(str != NULL);
- tt_u64_op((uint64_t)st.st_size, OP_EQ, temp_str_len);
- tt_mem_op(temp_str, OP_EQ, str, temp_str_len);
- done:
- unlink(fname);
- unlink(tempname);
- smartlist_free(chunks);
- tor_free(fname);
- tor_free(tempname);
- tor_free(str);
- tor_free(data_str);
- tor_free(temp_str);
- }
- #define _TFE(a, b, f) tt_int_op((a).f, OP_EQ, (b).f)
- /** test the minimum set of struct tm fields needed for a unique epoch value
- * this is also the set we use to test tor_timegm */
- #define TM_EQUAL(a, b) \
- TT_STMT_BEGIN \
- _TFE(a, b, tm_year); \
- _TFE(a, b, tm_mon ); \
- _TFE(a, b, tm_mday); \
- _TFE(a, b, tm_hour); \
- _TFE(a, b, tm_min ); \
- _TFE(a, b, tm_sec ); \
- TT_STMT_END
- static void
- test_util_time(void *arg)
- {
- struct timeval start, end;
- struct tm a_time, b_time;
- char timestr[128];
- time_t t_res;
- int i;
- struct timeval tv;
- /* Test tv_udiff */
- (void)arg;
- start.tv_sec = 5;
- start.tv_usec = 5000;
- end.tv_sec = 5;
- end.tv_usec = 5000;
- tt_int_op(0L,OP_EQ, tv_udiff(&start, &end));
- end.tv_usec = 7000;
- tt_int_op(2000L,OP_EQ, tv_udiff(&start, &end));
- end.tv_sec = 6;
- tt_int_op(1002000L,OP_EQ, tv_udiff(&start, &end));
- end.tv_usec = 0;
- tt_int_op(995000L,OP_EQ, tv_udiff(&start, &end));
- end.tv_sec = 4;
- tt_int_op(-1005000L,OP_EQ, tv_udiff(&start, &end));
- /* Test tor_timegm & tor_gmtime_r */
- /* The test values here are confirmed to be correct on a platform
- * with a working timegm & gmtime_r. */
- /* Start with known-zero a_time and b_time.
- * This avoids passing uninitialised values to TM_EQUAL in a_time.
- * Zeroing may not be needed for b_time, as long as tor_gmtime_r
- * never reads the existing values in the structure.
- * But we really don't want intermittently failing tests. */
- memset(&a_time, 0, sizeof(struct tm));
- memset(&b_time, 0, sizeof(struct tm));
- a_time.tm_year = 2003-1900;
- a_time.tm_mon = 7;
- a_time.tm_mday = 30;
- a_time.tm_hour = 6;
- a_time.tm_min = 14;
- a_time.tm_sec = 55;
- t_res = 1062224095UL;
- tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
- tor_gmtime_r(&t_res, &b_time);
- TM_EQUAL(a_time, b_time);
- a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
- t_res = 1093846495UL;
- tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
- tor_gmtime_r(&t_res, &b_time);
- TM_EQUAL(a_time, b_time);
- a_time.tm_mon = 1; /* Try a leap year, in feb. */
- a_time.tm_mday = 10;
- t_res = 1076393695UL;
- tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
- tor_gmtime_r(&t_res, &b_time);
- TM_EQUAL(a_time, b_time);
- a_time.tm_mon = 0;
- t_res = 1073715295UL;
- tt_int_op(t_res, OP_EQ, tor_timegm(&a_time));
- tor_gmtime_r(&t_res, &b_time);
- TM_EQUAL(a_time, b_time);
- /* Test tor_timegm out of range */
- /* year */
- /* Wrong year < 1970 */
- a_time.tm_year = 1969-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = -1-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- #if SIZEOF_INT == 4 || SIZEOF_INT == 8
- a_time.tm_year = -1*(1 << 16);
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* one of the smallest tm_year values my 64 bit system supports:
- * t_res = -9223372036854775LL without clamping */
- a_time.tm_year = -292275055-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = INT32_MIN;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- #endif
- #if SIZEOF_INT == 8
- a_time.tm_year = -1*(1 << 48);
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* while unlikely, the system's gmtime(_r) could return
- * a "correct" retrospective gregorian negative year value,
- * which I'm pretty sure is:
- * -1*(2^63)/60/60/24*2000/730485 + 1970 = -292277022657
- * 730485 is the number of days in two millenia, including leap days */
- a_time.tm_year = -292277022657-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = INT64_MIN;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- #endif
- /* Wrong year >= INT32_MAX - 1900 */
- #if SIZEOF_INT == 4 || SIZEOF_INT == 8
- a_time.tm_year = INT32_MAX-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = INT32_MAX;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- #endif
- #if SIZEOF_INT == 8
- /* one of the largest tm_year values my 64 bit system supports */
- a_time.tm_year = 292278994-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* while unlikely, the system's gmtime(_r) could return
- * a "correct" proleptic gregorian year value,
- * which I'm pretty sure is:
- * (2^63-1)/60/60/24*2000/730485 + 1970 = 292277026596
- * 730485 is the number of days in two millenia, including leap days */
- a_time.tm_year = 292277026596-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = INT64_MAX-1900;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = INT64_MAX;
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- #endif
- /* month */
- a_time.tm_year = 2007-1900; /* restore valid year */
- a_time.tm_mon = 12; /* Wrong month, it's 0-based */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_mon = -1; /* Wrong month */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* day */
- a_time.tm_mon = 6; /* Try July */
- a_time.tm_mday = 32; /* Wrong day */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_mon = 5; /* Try June */
- a_time.tm_mday = 31; /* Wrong day */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = 2008-1900; /* Try a leap year */
- a_time.tm_mon = 1; /* in feb. */
- a_time.tm_mday = 30; /* Wrong day */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_year = 2011-1900; /* Try a non-leap year */
- a_time.tm_mon = 1; /* in feb. */
- a_time.tm_mday = 29; /* Wrong day */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_mday = 0; /* Wrong day, it's 1-based (to be different) */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* hour */
- a_time.tm_mday = 3; /* restore valid month day */
- a_time.tm_hour = 24; /* Wrong hour, it's 0-based */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_hour = -1; /* Wrong hour */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* minute */
- a_time.tm_hour = 22; /* restore valid hour */
- a_time.tm_min = 60; /* Wrong minute, it's 0-based */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_min = -1; /* Wrong minute */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* second */
- a_time.tm_min = 37; /* restore valid minute */
- a_time.tm_sec = 61; /* Wrong second: 0-based with leap seconds */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- a_time.tm_sec = -1; /* Wrong second */
- tt_int_op((time_t) -1,OP_EQ, tor_timegm(&a_time));
- /* Test tor_gmtime_r out of range */
- /* time_t < 0 yields a year clamped to 1 or 1970,
- * depending on whether the implementation of the system gmtime(_r)
- * sets struct tm (1) or not (1970) */
- t_res = -1;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (1970-1900) ||
- b_time.tm_year == (1969-1900));
- if (sizeof(time_t) == 4 || sizeof(time_t) == 8) {
- t_res = -1*(1 << 30);
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (1970-1900) ||
- b_time.tm_year == (1935-1900));
- t_res = INT32_MIN;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (1970-1900) ||
- b_time.tm_year == (1901-1900));
- }
- #if SIZEOF_TIME_T == 8
- {
- /* one of the smallest tm_year values my 64 bit system supports:
- * b_time.tm_year == (-292275055LL-1900LL) without clamping */
- t_res = -9223372036854775LL;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (1970-1900) ||
- b_time.tm_year == (1-1900));
- /* while unlikely, the system's gmtime(_r) could return
- * a "correct" retrospective gregorian negative year value,
- * which I'm pretty sure is:
- * -1*(2^63)/60/60/24*2000/730485 + 1970 = -292277022657
- * 730485 is the number of days in two millenia, including leap days
- * (int64_t)b_time.tm_year == (-292277022657LL-1900LL) without clamping */
- t_res = INT64_MIN;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (1970-1900) ||
- b_time.tm_year == (1-1900));
- }
- #endif
- /* time_t >= INT_MAX yields a year clamped to 2037 or 9999,
- * depending on whether the implementation of the system gmtime(_r)
- * sets struct tm (9999) or not (2037) */
- #if SIZEOF_TIME_T == 4 || SIZEOF_TIME_T == 8
- {
- t_res = 3*(1 << 29);
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (2021-1900));
- t_res = INT32_MAX;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (2037-1900) ||
- b_time.tm_year == (2038-1900));
- }
- #endif
- #if SIZEOF_TIME_T == 8
- {
- /* one of the largest tm_year values my 64 bit system supports:
- * b_time.tm_year == (292278994L-1900L) without clamping */
- t_res = 9223372036854775LL;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (2037-1900) ||
- b_time.tm_year == (9999-1900));
- /* while unlikely, the system's gmtime(_r) could return
- * a "correct" proleptic gregorian year value,
- * which I'm pretty sure is:
- * (2^63-1)/60/60/24*2000/730485 + 1970 = 292277026596
- * 730485 is the number of days in two millenia, including leap days
- * (int64_t)b_time.tm_year == (292277026596L-1900L) without clamping */
- t_res = INT64_MAX;
- tor_gmtime_r(&t_res, &b_time);
- tt_assert(b_time.tm_year == (2037-1900) ||
- b_time.tm_year == (9999-1900));
- }
- #endif
- /* Test {format,parse}_rfc1123_time */
- format_rfc1123_time(timestr, 0);
- tt_str_op("Thu, 01 Jan 1970 00:00:00 GMT",OP_EQ, timestr);
- format_rfc1123_time(timestr, (time_t)1091580502UL);
- tt_str_op("Wed, 04 Aug 2004 00:48:22 GMT",OP_EQ, timestr);
- t_res = 0;
- i = parse_rfc1123_time(timestr, &t_res);
- tt_int_op(0,OP_EQ, i);
- tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
- /* The timezone doesn't matter */
- t_res = 0;
- tt_int_op(0,OP_EQ,
- parse_rfc1123_time("Wed, 04 Aug 2004 00:48:22 ZUL", &t_res));
- tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 32 Mar 2011 00:00:00 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Mar 2011 24:00:00 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Mar 2011 23:60:00 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Mar 2011 23:59:62 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Mar 1969 23:59:59 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Ene 2011 23:59:59 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Mar 2011 23:59:59 GM", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 29 Feb 2011 16:00:00 GMT", &t_res));
- tt_int_op(-1,OP_EQ,
- parse_rfc1123_time("Wed, 30 Mar 2011 23:59:61 GMT", &t_res));
- /* Test parse_iso_time */
- t_res = 0;
- i = parse_iso_time("", &t_res);
- tt_int_op(-1,OP_EQ, i);
- t_res = 0;
- i = parse_iso_time("2004-08-32 00:48:22", &t_res);
- tt_int_op(-1,OP_EQ, i);
- t_res = 0;
- i = parse_iso_time("1969-08-03 00:48:22", &t_res);
- tt_int_op(-1,OP_EQ, i);
- t_res = 0;
- i = parse_iso_time("2004-08-04 00:48:22", &t_res);
- tt_int_op(0,OP_EQ, i);
- tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
- t_res = 0;
- i = parse_iso_time("2004-8-4 0:48:22", &t_res);
- tt_int_op(0,OP_EQ, i);
- tt_int_op(t_res,OP_EQ, (time_t)1091580502UL);
- tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-zz 99-99x99", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-32 00:00:00", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 24:00:00", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 23:60:00", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 23:59:62", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("1969-03-30 23:59:59", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2011-00-30 23:59:59", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2147483647-08-29 14:00:00", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2011-03-30 23:59", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-04 00:48:22.100", &t_res));
- tt_int_op(-1,OP_EQ, parse_iso_time("2004-08-04 00:48:22XYZ", &t_res));
- /* Test tor_gettimeofday */
- end.tv_sec = 4;
- end.tv_usec = 999990;
- start.tv_sec = 1;
- start.tv_usec = 500;
- tor_gettimeofday(&start);
- /* now make sure time works. */
- tor_gettimeofday(&end);
- /* We might've timewarped a little. */
- tt_int_op(tv_udiff(&start, &end), OP_GE, -5000);
- /* Test format_iso_time */
- tv.tv_sec = (time_t)1326296338;
- tv.tv_usec = 3060;
- format_iso_time(timestr, (time_t)tv.tv_sec);
- tt_str_op("2012-01-11 15:38:58",OP_EQ, timestr);
- /* The output of format_local_iso_time will vary by timezone, and setting
- our timezone for testing purposes would be a nontrivial flaky pain.
- Skip this test for now.
- format_local_iso_time(timestr, tv.tv_sec);
- test_streq("2012-01-11 10:38:58", timestr);
- */
- format_iso_time_nospace(timestr, (time_t)tv.tv_sec);
- tt_str_op("2012-01-11T15:38:58",OP_EQ, timestr);
- tt_int_op(strlen(timestr),OP_EQ, ISO_TIME_LEN);
- format_iso_time_nospace_usec(timestr, &tv);
- tt_str_op("2012-01-11T15:38:58.003060",OP_EQ, timestr);
- tt_int_op(strlen(timestr),OP_EQ, ISO_TIME_USEC_LEN);
- done:
- ;
- }
- static void
- test_util_parse_http_time(void *arg)
- {
- struct tm a_time;
- char b[ISO_TIME_LEN+1];
- (void)arg;
- #define T(s) do { \
- format_iso_time(b, tor_timegm(&a_time)); \
- tt_str_op(b, OP_EQ, (s)); \
- b[0]='\0'; \
- } while (0)
- /* Test parse_http_time */
- tt_int_op(-1,OP_EQ,
- parse_http_time("", &a_time));
- tt_int_op(-1,OP_EQ,
- parse_http_time("Sunday, 32 Aug 2004 00:48:22 GMT", &a_time));
- tt_int_op(-1,OP_EQ,
- parse_http_time("Sunday, 3 Aug 1869 00:48:22 GMT", &a_time));
- tt_int_op(-1,OP_EQ,
- parse_http_time("Sunday, 32-Aug-94 00:48:22 GMT", &a_time));
- tt_int_op(-1,OP_EQ,
- parse_http_time("Sunday, 3-Ago-04 00:48:22", &a_time));
- tt_int_op(-1,OP_EQ,
- parse_http_time("Sunday, August the third", &a_time));
- tt_int_op(-1,OP_EQ,
- parse_http_time("Wednesday,,04 Aug 1994 00:48:22 GMT", &a_time));
- tt_int_op(0,OP_EQ,
- parse_http_time("Wednesday, 04 Aug 1994 00:48:22 GMT", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ,
- parse_http_time("Wednesday, 4 Aug 1994 0:48:22 GMT", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ,
- parse_http_time("Miercoles, 4 Aug 1994 0:48:22 GMT", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ,
- parse_http_time("Wednesday, 04-Aug-94 00:48:22 GMT", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ,
- parse_http_time("Wednesday, 4-Aug-94 0:48:22 GMT", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ,
- parse_http_time("Miercoles, 4-Aug-94 0:48:22 GMT", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ, parse_http_time("Wed Aug 04 00:48:22 1994", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ, parse_http_time("Wed Aug 4 0:48:22 1994", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ, parse_http_time("Mie Aug 4 0:48:22 1994", &a_time));
- tt_int_op((time_t)775961302UL,OP_EQ, tor_timegm(&a_time));
- T("1994-08-04 00:48:22");
- tt_int_op(0,OP_EQ,parse_http_time("Sun, 1 Jan 2012 00:00:00 GMT", &a_time));
- tt_int_op((time_t)1325376000UL,OP_EQ, tor_timegm(&a_time));
- T("2012-01-01 00:00:00");
- tt_int_op(0,OP_EQ,parse_http_time("Mon, 31 Dec 2012 00:00:00 GMT", &a_time));
- tt_int_op((time_t)1356912000UL,OP_EQ, tor_timegm(&a_time));
- T("2012-12-31 00:00:00");
- tt_int_op(-1,OP_EQ, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 23:60:00 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 23:59:62 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("1969-03-30 23:59:59 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("2011-00-30 23:59:59 GMT", &a_time));
- tt_int_op(-1,OP_EQ, parse_http_time("2011-03-30 23:59", &a_time));
- #undef T
- done:
- ;
- }
- static void
- test_util_config_line(void *arg)
- {
- char buf[1024];
- char *k=NULL, *v=NULL;
- const char *str;
- /* Test parse_config_line_from_str */
- (void)arg;
- strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
- "k2\n"
- "k3 \n" "\n" " \n" "#comment\n"
- "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
- "kseven \"a quoted 'string\"\n"
- "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
- "k9 a line that\\\n spans two lines.\n\n"
- "k10 more than\\\n one contin\\\nuation\n"
- "k11 \\\ncontinuation at the start\n"
- "k12 line with a\\\n#comment\n embedded\n"
- "k13\\\ncontinuation at the very start\n"
- "k14 a line that has a comment and # ends with a slash \\\n"
- "k15 this should be the next new line\n"
- "k16 a line that has a comment and # ends without a slash \n"
- "k17 this should be the next new line\n"
- , sizeof(buf));
- str = buf;
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k");
- tt_str_op(v,OP_EQ, "v");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "key value with"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "key");
- tt_str_op(v,OP_EQ, "value with spaces");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "keykey"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "keykey");
- tt_str_op(v,OP_EQ, "val");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "k2\n"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k2");
- tt_str_op(v,OP_EQ, "");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "k3 \n"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k3");
- tt_str_op(v,OP_EQ, "");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "#comment"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k4");
- tt_str_op(v,OP_EQ, "");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "k5#abc"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k5");
- tt_str_op(v,OP_EQ, "");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "k6"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k6");
- tt_str_op(v,OP_EQ, "val");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "kseven"));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "kseven");
- tt_str_op(v,OP_EQ, "a quoted \'string");
- tor_free(k); tor_free(v);
- tt_assert(!strcmpstart(str, "k8 "));
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k8");
- tt_str_op(v,OP_EQ, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k9");
- tt_str_op(v,OP_EQ, "a line that spans two lines.");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k10");
- tt_str_op(v,OP_EQ, "more than one continuation");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k11");
- tt_str_op(v,OP_EQ, "continuation at the start");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k12");
- tt_str_op(v,OP_EQ, "line with a embedded");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k13");
- tt_str_op(v,OP_EQ, "continuation at the very start");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k14");
- tt_str_op(v,OP_EQ, "a line that has a comment and" );
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k15");
- tt_str_op(v,OP_EQ, "this should be the next new line");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k16");
- tt_str_op(v,OP_EQ, "a line that has a comment and" );
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k17");
- tt_str_op(v,OP_EQ, "this should be the next new line");
- tor_free(k); tor_free(v);
- tt_str_op(str,OP_EQ, "");
- done:
- tor_free(k);
- tor_free(v);
- }
- static void
- test_util_config_line_quotes(void *arg)
- {
- char buf1[1024];
- char buf2[128];
- char buf3[128];
- char buf4[128];
- char *k=NULL, *v=NULL;
- const char *str;
- /* Test parse_config_line_from_str */
- (void)arg;
- strlcpy(buf1, "kTrailingSpace \"quoted value\" \n"
- "kTrailingGarbage \"quoted value\"trailing garbage\n"
- , sizeof(buf1));
- strlcpy(buf2, "kTrailingSpaceAndGarbage \"quoted value\" trailing space+g\n"
- , sizeof(buf2));
- strlcpy(buf3, "kMultilineTrailingSpace \"mline\\ \nvalue w/ trailing sp\"\n"
- , sizeof(buf3));
- strlcpy(buf4, "kMultilineNoTrailingBackslash \"naked multiline\nvalue\"\n"
- , sizeof(buf4));
- str = buf1;
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "kTrailingSpace");
- tt_str_op(v,OP_EQ, "quoted value");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- str = buf2;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- str = buf3;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- str = buf4;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- done:
- tor_free(k);
- tor_free(v);
- }
- static void
- test_util_config_line_comment_character(void *arg)
- {
- char buf[1024];
- char *k=NULL, *v=NULL;
- const char *str;
- /* Test parse_config_line_from_str */
- (void)arg;
- strlcpy(buf, "k1 \"# in quotes\"\n"
- "k2 some value # some comment\n"
- "k3 /home/user/myTorNetwork#2\n" /* Testcase for #1323 */
- , sizeof(buf));
- str = buf;
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k1");
- tt_str_op(v,OP_EQ, "# in quotes");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "k2");
- tt_str_op(v,OP_EQ, "some value");
- tor_free(k); tor_free(v);
- tt_str_op(str,OP_EQ, "k3 /home/user/myTorNetwork#2\n");
- #if 0
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k3");
- test_streq(v, "/home/user/myTorNetwork#2");
- tor_free(k); tor_free(v);
- test_streq(str, "");
- #endif
- done:
- tor_free(k);
- tor_free(v);
- }
- static void
- test_util_config_line_escaped_content(void *arg)
- {
- char buf1[1024];
- char buf2[128];
- char buf3[128];
- char buf4[128];
- char buf5[128];
- char buf6[128];
- char *k=NULL, *v=NULL;
- const char *str;
- /* Test parse_config_line_from_str */
- (void)arg;
- strlcpy(buf1, "HexadecimalLower \"\\x2a\"\n"
- "HexadecimalUpper \"\\x2A\"\n"
- "HexadecimalUpperX \"\\X2A\"\n"
- "Octal \"\\52\"\n"
- "Newline \"\\n\"\n"
- "Tab \"\\t\"\n"
- "CarriageReturn \"\\r\"\n"
- "DoubleQuote \"\\\"\"\n"
- "SimpleQuote \"\\'\"\n"
- "Backslash \"\\\\\"\n"
- "Mix \"This is a \\\"star\\\":\\t\\'\\x2a\\'\\nAnd second line\"\n"
- , sizeof(buf1));
- strlcpy(buf2, "BrokenEscapedContent \"\\a\"\n"
- , sizeof(buf2));
- strlcpy(buf3, "BrokenEscapedContent \"\\x\"\n"
- , sizeof(buf3));
- strlcpy(buf4, "BrokenOctal \"\\8\"\n"
- , sizeof(buf4));
- strlcpy(buf5, "BrokenHex \"\\xg4\"\n"
- , sizeof(buf5));
- strlcpy(buf6, "BrokenEscape \"\\"
- , sizeof(buf6));
- str = buf1;
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "HexadecimalLower");
- tt_str_op(v,OP_EQ, "*");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "HexadecimalUpper");
- tt_str_op(v,OP_EQ, "*");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "HexadecimalUpperX");
- tt_str_op(v,OP_EQ, "*");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "Octal");
- tt_str_op(v,OP_EQ, "*");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "Newline");
- tt_str_op(v,OP_EQ, "\n");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "Tab");
- tt_str_op(v,OP_EQ, "\t");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "CarriageReturn");
- tt_str_op(v,OP_EQ, "\r");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "DoubleQuote");
- tt_str_op(v,OP_EQ, "\"");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "SimpleQuote");
- tt_str_op(v,OP_EQ, "'");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "Backslash");
- tt_str_op(v,OP_EQ, "\\");
- tor_free(k); tor_free(v);
- str = parse_config_line_from_str(str, &k, &v);
- tt_str_op(k,OP_EQ, "Mix");
- tt_str_op(v,OP_EQ, "This is a \"star\":\t'*'\nAnd second line");
- tor_free(k); tor_free(v);
- tt_str_op(str,OP_EQ, "");
- str = buf2;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- str = buf3;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- str = buf4;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- #if 0
- str = buf5;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str, OP_EQ, NULL);
- tor_free(k); tor_free(v);
- #endif
- str = buf6;
- str = parse_config_line_from_str(str, &k, &v);
- tt_ptr_op(str,OP_EQ, NULL);
- tor_free(k); tor_free(v);
- done:
- tor_free(k);
- tor_free(v);
- }
- #ifndef _WIN32
- static void
- test_util_expand_filename(void *arg)
- {
- char *str;
- (void)arg;
- setenv("HOME", "/home/itv", 1); /* For "internal test value" */
- str = expand_filename("");
- tt_str_op("",OP_EQ, str);
- tor_free(str);
- str = expand_filename("/normal/path");
- tt_str_op("/normal/path",OP_EQ, str);
- tor_free(str);
- str = expand_filename("/normal/trailing/path/");
- tt_str_op("/normal/trailing/path/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~");
- tt_str_op("/home/itv/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("$HOME/nodice");
- tt_str_op("$HOME/nodice",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/");
- tt_str_op("/home/itv/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/foobarqux");
- tt_str_op("/home/itv/foobarqux",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/../../etc/passwd");
- tt_str_op("/home/itv/../../etc/passwd",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/trailing/");
- tt_str_op("/home/itv/trailing/",OP_EQ, str);
- tor_free(str);
- /* Ideally we'd test ~anotheruser, but that's shady to test (we'd
- have to somehow inject/fake the get_user_homedir call) */
- /* $HOME ending in a trailing slash */
- setenv("HOME", "/home/itv/", 1);
- str = expand_filename("~");
- tt_str_op("/home/itv/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/");
- tt_str_op("/home/itv/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/foo");
- tt_str_op("/home/itv/foo",OP_EQ, str);
- tor_free(str);
- /* Try with empty $HOME */
- setenv("HOME", "", 1);
- str = expand_filename("~");
- tt_str_op("/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/");
- tt_str_op("/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/foobar");
- tt_str_op("/foobar",OP_EQ, str);
- tor_free(str);
- /* Try with $HOME unset */
- unsetenv("HOME");
- str = expand_filename("~");
- tt_str_op("/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/");
- tt_str_op("/",OP_EQ, str);
- tor_free(str);
- str = expand_filename("~/foobar");
- tt_str_op("/foobar",OP_EQ, str);
- tor_free(str);
- done:
- tor_free(str);
- }
- #endif
- /** Test tor_escape_str_for_pt_args(). */
- static void
- test_util_escape_string_socks(void *arg)
- {
- char *escaped_string = NULL;
- /** Simple backslash escape. */
- (void)arg;
- escaped_string = tor_escape_str_for_pt_args("This is a backslash: \\",";\\");
- tt_assert(escaped_string);
- tt_str_op(escaped_string,OP_EQ, "This is a backslash: \\\\");
- tor_free(escaped_string);
- /** Simple semicolon escape. */
- escaped_string = tor_escape_str_for_pt_args("First rule:Do not use ;",";\\");
- tt_assert(escaped_string);
- tt_str_op(escaped_string,OP_EQ, "First rule:Do not use \\;");
- tor_free(escaped_string);
- /** Empty string. */
- escaped_string = tor_escape_str_for_pt_args("", ";\\");
- tt_assert(escaped_string);
- tt_str_op(escaped_string,OP_EQ, "");
- tor_free(escaped_string);
- /** Escape all characters. */
- escaped_string = tor_escape_str_for_pt_args(";\\;\\", ";\\");
- tt_assert(escaped_string);
- tt_str_op(escaped_string,OP_EQ, "\\;\\\\\\;\\\\");
- tor_free(escaped_string);
- escaped_string = tor_escape_str_for_pt_args(";", ";\\");
- tt_assert(escaped_string);
- tt_str_op(escaped_string,OP_EQ, "\\;");
- tor_free(escaped_string);
- done:
- tor_free(escaped_string);
- }
- static void
- test_util_string_is_key_value(void *ptr)
- {
- (void)ptr;
- tt_assert(string_is_key_value(LOG_WARN, "key=value"));
- tt_assert(string_is_key_value(LOG_WARN, "k=v"));
- tt_assert(string_is_key_value(LOG_WARN, "key="));
- tt_assert(string_is_key_value(LOG_WARN, "x="));
- tt_assert(string_is_key_value(LOG_WARN, "xx="));
- tt_assert(!string_is_key_value(LOG_WARN, "=value"));
- tt_assert(!string_is_key_value(LOG_WARN, "=x"));
- tt_assert(!string_is_key_value(LOG_WARN, "="));
- /* ??? */
- /* tt_assert(!string_is_key_value(LOG_WARN, "===")); */
- done:
- ;
- }
- /** Test basic string functionality. */
- static void
- test_util_strmisc(void *arg)
- {
- char buf[1024];
- int i;
- char *cp, *cp_tmp = NULL;
- /* Test strl operations */
- (void)arg;
- tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 0));
- tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 10));
- tt_str_op(buf,OP_EQ, "Hello");
- tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 6));
- tt_str_op(buf,OP_EQ, "Hello");
- tt_int_op(5,OP_EQ, strlcpy(buf, "Hello", 5));
- tt_str_op(buf,OP_EQ, "Hell");
- strlcpy(buf, "Hello", sizeof(buf));
- tt_int_op(10,OP_EQ, strlcat(buf, "Hello", 5));
- /* Test strstrip() */
- strlcpy(buf, "Testing 1 2 3", sizeof(buf));
- tor_strstrip(buf, ",!");
- tt_str_op(buf,OP_EQ, "Testing 1 2 3");
- strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
- tor_strstrip(buf, "!? ");
- tt_str_op(buf,OP_EQ, "Testing123");
- strlcpy(buf, "!!!Testing 1 2 3??", sizeof(buf));
- tor_strstrip(buf, "!? ");
- tt_str_op(buf,OP_EQ, "Testing123");
- /* Test parse_long */
- /* Empty/zero input */
- tt_int_op(0L,OP_EQ, tor_parse_long("",10,0,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(0L,OP_EQ, tor_parse_long("0",10,0,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- /* Normal cases */
- tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(10L,OP_EQ, tor_parse_long("10",10,0,10,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(10L,OP_EQ, tor_parse_long("10",10,10,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,100,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-100,0,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50",10,-50,0,&i,NULL));
- tt_int_op(1,OP_EQ, i);
- /* Extra garbage */
- tt_int_op(0L,OP_EQ, tor_parse_long("10m",10,0,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(0L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(10L,OP_EQ, tor_parse_long("10m",10,0,100,&i,&cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, "m");
- tt_int_op(-50L,OP_EQ, tor_parse_long("-50 plus garbage",10,-100,100,&i,&cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, " plus garbage");
- /* Out of bounds */
- tt_int_op(0L,OP_EQ, tor_parse_long("10",10,50,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- tt_int_op(0L,OP_EQ, tor_parse_long("-50",10,0,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- /* Base different than 10 */
- tt_int_op(2L,OP_EQ, tor_parse_long("10",2,0,100,NULL,NULL));
- tt_int_op(0L,OP_EQ, tor_parse_long("2",2,0,100,NULL,NULL));
- tt_int_op(0L,OP_EQ, tor_parse_long("10",-2,0,100,NULL,NULL));
- tt_int_op(68284L,OP_EQ, tor_parse_long("10abc",16,0,70000,NULL,NULL));
- tt_int_op(68284L,OP_EQ, tor_parse_long("10ABC",16,0,70000,NULL,NULL));
- tt_int_op(0,OP_EQ, tor_parse_long("10ABC",-1,0,70000,&i,NULL));
- tt_int_op(i,OP_EQ, 0);
- /* Test parse_ulong */
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("",10,0,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("0",10,0,100,NULL,NULL));
- tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("10",10,50,100,NULL,NULL));
- tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,0,10,NULL,NULL));
- tt_int_op(10UL,OP_EQ, tor_parse_ulong("10",10,10,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("8",8,0,100,NULL,NULL));
- tt_int_op(50UL,OP_EQ, tor_parse_ulong("50",10,50,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("-50",10,-100,100,NULL,NULL));
- tt_int_op(0UL,OP_EQ, tor_parse_ulong("50",-1,50,100,&i,NULL));
- tt_int_op(0,OP_EQ, i);
- /* Test parse_uint64 */
- tt_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, " x");
- tt_assert(U64_LITERAL(12345678901) ==
- tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
- tt_int_op(1,OP_EQ, i);
- tt_str_op(cp,OP_EQ, "");
- tt_assert(U64_LITERAL(0) ==
- tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
- tt_int_op(0,OP_EQ, i);
- tt_assert(U64_LITERAL(0) ==
- tor_parse_uint64("123",-1,0,INT32_MAX, &i, &cp));
- tt_int_op(0,OP_EQ, i);
- {
- /* Test parse_double */
- double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_assert(DBL_TO_U64(d) == 10);
- d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_assert(DBL_TO_U64(d) == 0);
- d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
- tt_int_op(0,OP_EQ, i);
- d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
- tt_int_op(0,OP_EQ, i);
- d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
- tt_int_op(1,OP_EQ, i);
- d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_assert(DBL_TO_U64(d) == 0);
- d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
- tt_int_op(1,OP_EQ, i);
- tt_int_op(-10.0,OP_EQ, d);
- }
- {
- /* Test tor_parse_* where we overflow/underflow the underlying type. */
- /* This string should overflow 64-bit ints. */
- #define TOOBIG "100000000000000000000000000"
- tt_int_op(0L, OP_EQ,
- tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- tt_int_op(0L,OP_EQ,
- tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- tt_int_op(0UL,OP_EQ, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- tt_u64_op(U64_LITERAL(0), OP_EQ, tor_parse_uint64(TOOBIG, 10,
- 0, UINT64_MAX, &i, NULL));
- tt_int_op(i,OP_EQ, 0);
- }
- /* Test snprintf */
- /* Returning -1 when there's not enough room in the output buffer */
- tt_int_op(-1,OP_EQ, tor_snprintf(buf, 0, "Foo"));
- tt_int_op(-1,OP_EQ, tor_snprintf(buf, 2, "Foo"));
- tt_int_op(-1,OP_EQ, tor_snprintf(buf, 3, "Foo"));
- tt_int_op(-1,OP_NE, tor_snprintf(buf, 4, "Foo"));
- /* Always NUL-terminate the output */
- tor_snprintf(buf, 5, "abcdef");
- tt_int_op(0,OP_EQ, buf[4]);
- tor_snprintf(buf, 10, "abcdef");
- tt_int_op(0,OP_EQ, buf[6]);
- /* uint64 */
- tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
- U64_PRINTF_ARG(U64_LITERAL(12345678901)));
- tt_str_op("x!12345678901!x",OP_EQ, buf);
- /* Test str{,case}cmpstart */
- tt_assert(strcmpstart("abcdef", "abcdef")==0);
- tt_assert(strcmpstart("abcdef", "abc")==0);
- tt_assert(strcmpstart("abcdef", "abd")<0);
- tt_assert(strcmpstart("abcdef", "abb")>0);
- tt_assert(strcmpstart("ab", "abb")<0);
- tt_assert(strcmpstart("ab", "")==0);
- tt_assert(strcmpstart("ab", "ab ")<0);
- tt_assert(strcasecmpstart("abcdef", "abCdEF")==0);
- tt_assert(strcasecmpstart("abcDeF", "abc")==0);
- tt_assert(strcasecmpstart("abcdef", "Abd")<0);
- tt_assert(strcasecmpstart("Abcdef", "abb")>0);
- tt_assert(strcasecmpstart("ab", "Abb")<0);
- tt_assert(strcasecmpstart("ab", "")==0);
- tt_assert(strcasecmpstart("ab", "ab ")<0);
- /* Test str{,case}cmpend */
- tt_assert(strcmpend("abcdef", "abcdef")==0);
- tt_assert(strcmpend("abcdef", "def")==0);
- tt_assert(strcmpend("abcdef", "deg")<0);
- tt_assert(strcmpend("abcdef", "dee")>0);
- tt_assert(strcmpend("ab", "aab")>0);
- tt_assert(strcasecmpend("AbcDEF", "abcdef")==0);
- tt_assert(strcasecmpend("abcdef", "dEF")==0);
- tt_assert(strcasecmpend("abcdef", "Deg")<0);
- tt_assert(strcasecmpend("abcDef", "dee")>0);
- tt_assert(strcasecmpend("AB", "abb")<0);
- /* Test digest_is_zero */
- memset(buf,0,20);
- buf[20] = 'x';
- tt_assert(tor_digest_is_zero(buf));
- buf[19] = 'x';
- tt_assert(!tor_digest_is_zero(buf));
- /* Test mem_is_zero */
- memset(buf,0,128);
- buf[128] = 'x';
- tt_assert(tor_mem_is_zero(buf, 10));
- tt_assert(tor_mem_is_zero(buf, 20));
- tt_assert(tor_mem_is_zero(buf, 128));
- tt_assert(!tor_mem_is_zero(buf, 129));
- buf[60] = (char)255;
- tt_assert(!tor_mem_is_zero(buf, 128));
- buf[0] = (char)1;
- tt_assert(!tor_mem_is_zero(buf, 10));
- /* Test 'escaped' */
- tt_assert(NULL == escaped(NULL));
- tt_str_op("\"\"",OP_EQ, escaped(""));
- tt_str_op("\"abcd\"",OP_EQ, escaped("abcd"));
- tt_str_op("\"\\\\ \\n\\r\\t\\\"\\'\"",OP_EQ, escaped("\\ \n\r\t\"'"));
- tt_str_op("\"unnecessary \\'backslashes\\'\"",OP_EQ,
- escaped("unnecessary \'backslashes\'"));
- /* Non-printable characters appear as octal */
- tt_str_op("\"z\\001abc\\277d\"",OP_EQ, escaped("z\001abc\277d"));
- tt_str_op("\"z\\336\\255 ;foo\"",OP_EQ, escaped("z\xde\xad\x20;foo"));
- /* Test strndup and memdup */
- {
- const char *s = "abcdefghijklmnopqrstuvwxyz";
- cp_tmp = tor_strndup(s, 30);
- tt_str_op(cp_tmp,OP_EQ, s); /* same string, */
- tt_ptr_op(cp_tmp,OP_NE,s); /* but different pointers. */
- tor_free(cp_tmp);
- cp_tmp = tor_strndup(s, 5);
- tt_str_op(cp_tmp,OP_EQ, "abcde");
- tor_free(cp_tmp);
- s = "a\0b\0c\0d\0e\0";
- cp_tmp = tor_memdup(s,10);
- tt_mem_op(cp_tmp,OP_EQ, s, 10); /* same ram, */
- tt_ptr_op(cp_tmp,OP_NE,s); /* but different pointers. */
- tor_free(cp_tmp);
- }
- /* Test str-foo functions */
- cp_tmp = tor_strdup("abcdef");
- tt_assert(tor_strisnonupper(cp_tmp));
- cp_tmp[3] = 'D';
- tt_assert(!tor_strisnonupper(cp_tmp));
- tor_strupper(cp_tmp);
- tt_str_op(cp_tmp,OP_EQ, "ABCDEF");
- tor_strlower(cp_tmp);
- tt_str_op(cp_tmp,OP_EQ, "abcdef");
- tt_assert(tor_strisnonupper(cp_tmp));
- tt_assert(tor_strisprint(cp_tmp));
- cp_tmp[3] = 3;
- tt_assert(!tor_strisprint(cp_tmp));
- tor_free(cp_tmp);
- /* Test memmem and memstr */
- {
- const char *haystack = "abcde";
- tt_assert(!tor_memmem(haystack, 5, "ef", 2));
- tt_ptr_op(tor_memmem(haystack, 5, "cd", 2),OP_EQ, haystack + 2);
- tt_ptr_op(tor_memmem(haystack, 5, "cde", 3),OP_EQ, haystack + 2);
- tt_assert(!tor_memmem(haystack, 4, "cde", 3));
- haystack = "ababcad";
- tt_ptr_op(tor_memmem(haystack, 7, "abc", 3),OP_EQ, haystack + 2);
- tt_ptr_op(tor_memmem(haystack, 7, "ad", 2),OP_EQ, haystack + 5);
- tt_ptr_op(tor_memmem(haystack, 7, "cad", 3),OP_EQ, haystack + 4);
- tt_assert(!tor_memmem(haystack, 7, "dadad", 5));
- tt_assert(!tor_memmem(haystack, 7, "abcdefghij", 10));
- /* memstr */
- tt_ptr_op(tor_memstr(haystack, 7, "abc"),OP_EQ, haystack + 2);
- tt_ptr_op(tor_memstr(haystack, 7, "cad"),OP_EQ, haystack + 4);
- tt_assert(!tor_memstr(haystack, 6, "cad"));
- tt_assert(!tor_memstr(haystack, 7, "cadd"));
- tt_assert(!tor_memstr(haystack, 7, "fe"));
- tt_assert(!tor_memstr(haystack, 7, "ababcade"));
- }
- /* Test hex_str */
- {
- char binary_data[68];
- size_t i;
- for (i = 0; i < sizeof(binary_data); ++i)
- binary_data[i] = i;
- tt_str_op(hex_str(binary_data, 0),OP_EQ, "");
- tt_str_op(hex_str(binary_data, 1),OP_EQ, "00");
- tt_str_op(hex_str(binary_data, 17),OP_EQ,
- "000102030405060708090A0B0C0D0E0F10");
- tt_str_op(hex_str(binary_data, 32),OP_EQ,
- "000102030405060708090A0B0C0D0E0F"
- "101112131415161718191A1B1C1D1E1F");
- tt_str_op(hex_str(binary_data, 34),OP_EQ,
- "000102030405060708090A0B0C0D0E0F"
- "101112131415161718191A1B1C1D1E1F");
- /* Repeat these tests for shorter strings after longer strings
- have been tried, to make sure we're correctly terminating strings */
- tt_str_op(hex_str(binary_data, 1),OP_EQ, "00");
- tt_str_op(hex_str(binary_data, 0),OP_EQ, "");
- }
- /* Test strcmp_opt */
- tt_int_op(strcmp_opt("", "foo"), OP_LT, 0);
- tt_int_op(strcmp_opt("", ""), OP_EQ, 0);
- tt_int_op(strcmp_opt("foo", ""), OP_GT, 0);
- tt_int_op(strcmp_opt(NULL, ""), OP_LT, 0);
- tt_int_op(strcmp_opt(NULL, NULL), OP_EQ, 0);
- tt_int_op(strcmp_opt("", NULL), OP_GT, 0);
- tt_int_op(strcmp_opt(NULL, "foo"), OP_LT, 0);
- tt_int_op(strcmp_opt("foo", NULL), OP_GT, 0);
- /* Test strcmp_len */
- tt_int_op(strcmp_len("foo", "bar", 3), OP_GT, 0);
- tt_int_op(strcmp_len("foo", "bar", 2), OP_LT, 0);
- tt_int_op(strcmp_len("foo2", "foo1", 4), OP_GT, 0);
- tt_int_op(strcmp_len("foo2", "foo1", 3), OP_LT, 0); /* Really stop at len */
- tt_int_op(strcmp_len("foo2", "foo", 3), OP_EQ, 0); /* Really stop at len */
- tt_int_op(strcmp_len("blah", "", 4), OP_GT, 0);
- tt_int_op(strcmp_len("blah", "", 0), OP_EQ, 0);
- done:
- tor_free(cp_tmp);
- }
- static void
- test_util_pow2(void *arg)
- {
- /* Test tor_log2(). */
- (void)arg;
- tt_int_op(tor_log2(64),OP_EQ, 6);
- tt_int_op(tor_log2(65),OP_EQ, 6);
- tt_int_op(tor_log2(63),OP_EQ, 5);
- /* incorrect mathematically, but as specified: */
- tt_int_op(tor_log2(0),OP_EQ, 0);
- tt_int_op(tor_log2(1),OP_EQ, 0);
- tt_int_op(tor_log2(2),OP_EQ, 1);
- tt_int_op(tor_log2(3),OP_EQ, 1);
- tt_int_op(tor_log2(4),OP_EQ, 2);
- tt_int_op(tor_log2(5),OP_EQ, 2);
- tt_int_op(tor_log2(U64_LITERAL(40000000000000000)),OP_EQ, 55);
- tt_int_op(tor_log2(UINT64_MAX),OP_EQ, 63);
- /* Test round_to_power_of_2 */
- tt_u64_op(round_to_power_of_2(120), OP_EQ, 128);
- tt_u64_op(round_to_power_of_2(128), OP_EQ, 128);
- tt_u64_op(round_to_power_of_2(130), OP_EQ, 128);
- tt_u64_op(round_to_power_of_2(U64_LITERAL(40000000000000000)), OP_EQ,
- U64_LITERAL(1)<<55);
- tt_u64_op(round_to_power_of_2(U64_LITERAL(0xffffffffffffffff)), OP_EQ,
- U64_LITERAL(1)<<63);
- tt_u64_op(round_to_power_of_2(0), OP_EQ, 1);
- tt_u64_op(round_to_power_of_2(1), OP_EQ, 1);
- tt_u64_op(round_to_power_of_2(2), OP_EQ, 2);
- tt_u64_op(round_to_power_of_2(3), OP_EQ, 2);
- tt_u64_op(round_to_power_of_2(4), OP_EQ, 4);
- tt_u64_op(round_to_power_of_2(5), OP_EQ, 4);
- tt_u64_op(round_to_power_of_2(6), OP_EQ, 4);
- tt_u64_op(round_to_power_of_2(7), OP_EQ, 8);
- done:
- ;
- }
- /** mutex for thread test to stop the threads hitting data at the same time. */
- static tor_mutex_t *thread_test_mutex_ = NULL;
- /** mutexes for the thread test to make sure that the threads have to
- * interleave somewhat. */
- static tor_mutex_t *thread_test_start1_ = NULL,
- *thread_test_start2_ = NULL;
- /** Shared strmap for the thread test. */
- static strmap_t *thread_test_strmap_ = NULL;
- /** The name of thread1 for the thread test */
- static char *thread1_name_ = NULL;
- /** The name of thread2 for the thread test */
- static char *thread2_name_ = NULL;
- static void thread_test_func_(void* _s) ATTR_NORETURN;
- /** How many iterations have the threads in the unit test run? */
- static int t1_count = 0, t2_count = 0;
- /** Helper function for threading unit tests: This function runs in a
- * subthread. It grabs its own mutex (start1 or start2) to make sure that it
- * should start, then it repeatedly alters _test_thread_strmap protected by
- * thread_test_mutex_. */
- static void
- thread_test_func_(void* _s)
- {
- char *s = _s;
- int i, *count;
- tor_mutex_t *m;
- char buf[64];
- char **cp;
- if (!strcmp(s, "thread 1")) {
- m = thread_test_start1_;
- cp = &thread1_name_;
- count = &t1_count;
- } else {
- m = thread_test_start2_;
- cp = &thread2_name_;
- count = &t2_count;
- }
- tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
- *cp = tor_strdup(buf);
- tor_mutex_acquire(m);
- for (i=0; i<10000; ++i) {
- tor_mutex_acquire(thread_test_mutex_);
- strmap_set(thread_test_strmap_, "last to run", *cp);
- ++*count;
- tor_mutex_release(thread_test_mutex_);
- }
- tor_mutex_acquire(thread_test_mutex_);
- strmap_set(thread_test_strmap_, s, *cp);
- tor_mutex_release(thread_test_mutex_);
- tor_mutex_release(m);
- spawn_exit();
- }
- /** Run unit tests for threading logic. */
- static void
- test_util_threads(void *arg)
- {
- char *s1 = NULL, *s2 = NULL;
- int done = 0, timedout = 0;
- time_t started;
- #ifndef _WIN32
- struct timeval tv;
- tv.tv_sec=0;
- tv.tv_usec=100*1000;
- #endif
- (void)arg;
- thread_test_mutex_ = tor_mutex_new();
- thread_test_start1_ = tor_mutex_new();
- thread_test_start2_ = tor_mutex_new();
- thread_test_strmap_ = strmap_new();
- s1 = tor_strdup("thread 1");
- s2 = tor_strdup("thread 2");
- tor_mutex_acquire(thread_test_start1_);
- tor_mutex_acquire(thread_test_start2_);
- spawn_func(thread_test_func_, s1);
- spawn_func(thread_test_func_, s2);
- tor_mutex_release(thread_test_start2_);
- tor_mutex_release(thread_test_start1_);
- started = time(NULL);
- while (!done) {
- tor_mutex_acquire(thread_test_mutex_);
- strmap_assert_ok(thread_test_strmap_);
- if (strmap_get(thread_test_strmap_, "thread 1") &&
- strmap_get(thread_test_strmap_, "thread 2")) {
- done = 1;
- } else if (time(NULL) > started + 150) {
- timedout = done = 1;
- }
- tor_mutex_release(thread_test_mutex_);
- #ifndef _WIN32
- /* Prevent the main thread from starving the worker threads. */
- select(0, NULL, NULL, NULL, &tv);
- #endif
- }
- tor_mutex_acquire(thread_test_start1_);
- tor_mutex_release(thread_test_start1_);
- tor_mutex_acquire(thread_test_start2_);
- tor_mutex_release(thread_test_start2_);
- tor_mutex_free(thread_test_mutex_);
- if (timedout) {
- printf("\nTimed out: %d %d", t1_count, t2_count);
- tt_assert(strmap_get(thread_test_strmap_, "thread 1"));
- tt_assert(strmap_get(thread_test_strmap_, "thread 2"));
- tt_assert(!timedout);
- }
- /* different thread IDs. */
- tt_assert(strcmp(strmap_get(thread_test_strmap_, "thread 1"),
- strmap_get(thread_test_strmap_, "thread 2")));
- tt_assert(!strcmp(strmap_get(thread_test_strmap_, "thread 1"),
- strmap_get(thread_test_strmap_, "last to run")) ||
- !strcmp(strmap_get(thread_test_strmap_, "thread 2"),
- strmap_get(thread_test_strmap_, "last to run")));
- done:
- tor_free(s1);
- tor_free(s2);
- tor_free(thread1_name_);
- tor_free(thread2_name_);
- if (thread_test_strmap_)
- strmap_free(thread_test_strmap_, NULL);
- if (thread_test_start1_)
- tor_mutex_free(thread_test_start1_);
- if (thread_test_start2_)
- tor_mutex_free(thread_test_start2_);
- }
- /** Run unit tests for compression functions */
- static void
- test_util_gzip(void *arg)
- {
- char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
- const char *ccp2;
- size_t len1, len2;
- tor_zlib_state_t *state = NULL;
- (void)arg;
- buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
- tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
- if (is_gzip_supported()) {
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- GZIP_METHOD));
- tt_assert(buf2);
- tt_assert(len1 < strlen(buf1));
- tt_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- GZIP_METHOD, 1, LOG_INFO));
- tt_assert(buf3);
- tt_int_op(strlen(buf1) + 1,OP_EQ, len2);
- tt_str_op(buf1,OP_EQ, buf3);
- tor_free(buf2);
- tor_free(buf3);
- }
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- ZLIB_METHOD));
- tt_assert(buf2);
- tt_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- ZLIB_METHOD, 1, LOG_INFO));
- tt_assert(buf3);
- tt_int_op(strlen(buf1) + 1,OP_EQ, len2);
- tt_str_op(buf1,OP_EQ, buf3);
- /* Check whether we can uncompress concatenated, compressed strings. */
- tor_free(buf3);
- buf2 = tor_reallocarray(buf2, len1, 2);
- memcpy(buf2+len1, buf2, len1);
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
- ZLIB_METHOD, 1, LOG_INFO));
- tt_int_op((strlen(buf1)+1)*2,OP_EQ, len2);
- tt_mem_op(buf3,OP_EQ,
- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
- (strlen(buf1)+1)*2);
- tor_free(buf1);
- tor_free(buf2);
- tor_free(buf3);
- /* Check whether we can uncompress partial strings. */
- buf1 =
- tor_strdup("String with low redundancy that won't be compressed much.");
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- ZLIB_METHOD));
- tt_assert(len1>16);
- /* when we allow an incomplete string, we should succeed.*/
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
- ZLIB_METHOD, 0, LOG_INFO));
- tt_assert(len2 > 5);
- buf3[len2]='\0';
- tt_assert(!strcmpstart(buf1, buf3));
- /* when we demand a complete string, this must fail. */
- tor_free(buf3);
- tt_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
- ZLIB_METHOD, 1, LOG_INFO));
- tt_assert(!buf3);
- /* Now, try streaming compression. */
- tor_free(buf1);
- tor_free(buf2);
- tor_free(buf3);
- state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
- tt_assert(state);
- cp1 = buf1 = tor_malloc(1024);
- len1 = 1024;
- ccp2 = "ABCDEFGHIJABCDEFGHIJ";
- len2 = 21;
- tt_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
- == TOR_ZLIB_OK);
- tt_int_op(0,OP_EQ, len2); /* Make sure we compressed it all. */
- tt_assert(cp1 > buf1);
- len2 = 0;
- cp2 = cp1;
- tt_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
- == TOR_ZLIB_DONE);
- tt_int_op(0,OP_EQ, len2);
- tt_assert(cp1 > cp2); /* Make sure we really added something. */
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
- ZLIB_METHOD, 1, LOG_WARN));
- /* Make sure it compressed right. */
- tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ");
- tt_int_op(21,OP_EQ, len2);
- done:
- if (state)
- tor_zlib_free(state);
- tor_free(buf2);
- tor_free(buf3);
- tor_free(buf1);
- }
- /** Run unit tests for mmap() wrapper functionality. */
- static void
- test_util_mmap(void *arg)
- {
- char *fname1 = tor_strdup(get_fname("mapped_1"));
- char *fname2 = tor_strdup(get_fname("mapped_2"));
- char *fname3 = tor_strdup(get_fname("mapped_3"));
- const size_t buflen = 17000;
- char *buf = tor_malloc(17000);
- tor_mmap_t *mapping = NULL;
- (void)arg;
- crypto_rand(buf, buflen);
- mapping = tor_mmap_file(fname1);
- tt_assert(! mapping);
- write_str_to_file(fname1, "Short file.", 1);
- mapping = tor_mmap_file(fname1);
- tt_assert(mapping);
- tt_int_op(mapping->size,OP_EQ, strlen("Short file."));
- tt_str_op(mapping->data,OP_EQ, "Short file.");
- #ifdef _WIN32
- tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
- mapping = NULL;
- tt_assert(unlink(fname1) == 0);
- #else
- /* make sure we can unlink. */
- tt_assert(unlink(fname1) == 0);
- tt_str_op(mapping->data,OP_EQ, "Short file.");
- tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
- mapping = NULL;
- #endif
- /* Now a zero-length file. */
- write_str_to_file(fname1, "", 1);
- mapping = tor_mmap_file(fname1);
- tt_ptr_op(mapping,OP_EQ, NULL);
- tt_int_op(ERANGE,OP_EQ, errno);
- unlink(fname1);
- /* Make sure that we fail to map a no-longer-existent file. */
- mapping = tor_mmap_file(fname1);
- tt_assert(! mapping);
- /* Now try a big file that stretches across a few pages and isn't aligned */
- write_bytes_to_file(fname2, buf, buflen, 1);
- mapping = tor_mmap_file(fname2);
- tt_assert(mapping);
- tt_int_op(mapping->size,OP_EQ, buflen);
- tt_mem_op(mapping->data,OP_EQ, buf, buflen);
- tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
- mapping = NULL;
- /* Now try a big aligned file. */
- write_bytes_to_file(fname3, buf, 16384, 1);
- mapping = tor_mmap_file(fname3);
- tt_assert(mapping);
- tt_int_op(mapping->size,OP_EQ, 16384);
- tt_mem_op(mapping->data,OP_EQ, buf, 16384);
- tt_int_op(0, OP_EQ, tor_munmap_file(mapping));
- mapping = NULL;
- done:
- unlink(fname1);
- unlink(fname2);
- unlink(fname3);
- tor_free(fname1);
- tor_free(fname2);
- tor_free(fname3);
- tor_free(buf);
- tor_munmap_file(mapping);
- }
- /** Run unit tests for escaping/unescaping data for use by controllers. */
- static void
- test_util_control_formats(void *arg)
- {
- char *out = NULL;
- const char *inp =
- "..This is a test\r\n.of the emergency \n..system.\r\n\rZ.\r\n";
- size_t sz;
- (void)arg;
- sz = read_escaped_data(inp, strlen(inp), &out);
- tt_str_op(out,OP_EQ,
- ".This is a test\nof the emergency \n.system.\n\rZ.\n");
- tt_int_op(sz,OP_EQ, strlen(out));
- done:
- tor_free(out);
- }
- #define test_feq(value1,value2) do { \
- double v1 = (value1), v2=(value2); \
- double tf_diff = v1-v2; \
- double tf_tolerance = ((v1+v2)/2.0)/1e8; \
- if (tf_diff<0) tf_diff=-tf_diff; \
- if (tf_tolerance<0) tf_tolerance=-tf_tolerance; \
- if (tf_diff<tf_tolerance) { \
- TT_BLATHER(("%s ~~ %s: %f ~~ %f",#value1,#value2,v1,v2)); \
- } else { \
- TT_FAIL(("%s ~~ %s: %f != %f",#value1,#value2,v1,v2)); \
- } \
- } while (0)
- static void
- test_util_sscanf(void *arg)
- {
- unsigned u1, u2, u3;
- unsigned long ulng;
- char s1[20], s2[10], s3[10], ch;
- int r;
- long lng1,lng2;
- int int1, int2;
- double d1,d2,d3,d4;
- /* Simple tests (malformed patterns, literal matching, ...) */
- (void)arg;
- tt_int_op(-1,OP_EQ, tor_sscanf("123", "%i", &r)); /* %i is not supported */
- tt_int_op(-1,OP_EQ,
- tor_sscanf("wrong", "%5c", s1)); /* %c cannot have a number. */
- tt_int_op(-1,OP_EQ, tor_sscanf("hello", "%s", s1)); /* %s needs a number. */
- tt_int_op(-1,OP_EQ, tor_sscanf("prettylongstring", "%999999s", s1));
- #if 0
- /* GCC thinks these two are illegal. */
- test_eq(-1, tor_sscanf("prettylongstring", "%0s", s1));
- test_eq(0, tor_sscanf("prettylongstring", "%10s", NULL));
- #endif
- /* No '%'-strings: always "success" */
- tt_int_op(0,OP_EQ, tor_sscanf("hello world", "hello world"));
- tt_int_op(0,OP_EQ, tor_sscanf("hello world", "good bye"));
- /* Excess data */
- tt_int_op(0,OP_EQ,
- tor_sscanf("hello 3", "%u", &u1)); /* have to match the start */
- tt_int_op(0,OP_EQ, tor_sscanf(" 3 hello", "%u", &u1));
- tt_int_op(0,OP_EQ,
- tor_sscanf(" 3 hello", "%2u", &u1)); /* not even in this case */
- tt_int_op(1,OP_EQ,
- tor_sscanf("3 hello", "%u", &u1)); /* but trailing is alright */
- /* Numbers (ie. %u) */
- tt_int_op(0,OP_EQ,
- tor_sscanf("hello world 3", "hello worlb %u", &u1)); /* d vs b */
- tt_int_op(1,OP_EQ, tor_sscanf("12345", "%u", &u1));
- tt_int_op(12345u,OP_EQ, u1);
- tt_int_op(1,OP_EQ, tor_sscanf("12346 ", "%u", &u1));
- tt_int_op(12346u,OP_EQ, u1);
- tt_int_op(0,OP_EQ, tor_sscanf(" 12347", "%u", &u1));
- tt_int_op(1,OP_EQ, tor_sscanf(" 12348", " %u", &u1));
- tt_int_op(12348u,OP_EQ, u1);
- tt_int_op(1,OP_EQ, tor_sscanf("0", "%u", &u1));
- tt_int_op(0u,OP_EQ, u1);
- tt_int_op(1,OP_EQ, tor_sscanf("0000", "%u", &u2));
- tt_int_op(0u,OP_EQ, u2);
- tt_int_op(0,OP_EQ, tor_sscanf("", "%u", &u1)); /* absent number */
- tt_int_op(0,OP_EQ, tor_sscanf("A", "%u", &u1)); /* bogus number */
- tt_int_op(0,OP_EQ, tor_sscanf("-1", "%u", &u1)); /* negative number */
- /* Numbers with size (eg. %2u) */
- tt_int_op(0,OP_EQ, tor_sscanf("-1", "%2u", &u1));
- tt_int_op(2,OP_EQ, tor_sscanf("123456", "%2u%u", &u1, &u2));
- tt_int_op(12u,OP_EQ, u1);
- tt_int_op(3456u,OP_EQ, u2);
- tt_int_op(1,OP_EQ, tor_sscanf("123456", "%8u", &u1));
- tt_int_op(123456u,OP_EQ, u1);
- tt_int_op(1,OP_EQ, tor_sscanf("123457 ", "%8u", &u1));
- tt_int_op(123457u,OP_EQ, u1);
- tt_int_op(0,OP_EQ, tor_sscanf(" 123456", "%8u", &u1));
- tt_int_op(3,OP_EQ, tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3));
- tt_int_op(12u,OP_EQ, u1);
- tt_int_op(3u,OP_EQ, u2);
- tt_int_op(456u,OP_EQ, u3);
- tt_int_op(3,OP_EQ,
- tor_sscanf("67:8:099", "%2u:%2u:%3u", &u1, &u2, &u3)); /* 0s */
- tt_int_op(67u,OP_EQ, u1);
- tt_int_op(8u,OP_EQ, u2);
- tt_int_op(99u,OP_EQ, u3);
- /* %u does not match space.*/
- tt_int_op(2,OP_EQ, tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3));
- tt_int_op(12u,OP_EQ, u1);
- tt_int_op(3u,OP_EQ, u2);
- /* %u does not match negative numbers. */
- tt_int_op(2,OP_EQ, tor_sscanf("67:8:-9", "%2u:%2u:%3u", &u1, &u2, &u3));
- tt_int_op(67u,OP_EQ, u1);
- tt_int_op(8u,OP_EQ, u2);
- /* Arbitrary amounts of 0-padding are okay */
- tt_int_op(3,OP_EQ, tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u",
- &u1, &u2, &u3));
- tt_int_op(12u,OP_EQ, u1);
- tt_int_op(3u,OP_EQ, u2);
- tt_int_op(99u,OP_EQ, u3);
- /* Hex (ie. %x) */
- tt_int_op(3,OP_EQ,
- tor_sscanf("1234 02aBcdEf ff", "%x %x %x", &u1, &u2, &u3));
- tt_int_op(0x1234,OP_EQ, u1);
- tt_int_op(0x2ABCDEF,OP_EQ, u2);
- tt_int_op(0xFF,OP_EQ, u3);
- /* Width works on %x */
- tt_int_op(3,OP_EQ, tor_sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3));
- tt_int_op(0xf00d,OP_EQ, u1);
- tt_int_op(0xcafe,OP_EQ, u2);
- tt_int_op(444,OP_EQ, u3);
- /* Literal '%' (ie. '%%') */
- tt_int_op(1,OP_EQ, tor_sscanf("99% fresh", "%3u%% fresh", &u1));
- tt_int_op(99,OP_EQ, u1);
- tt_int_op(0,OP_EQ, tor_sscanf("99 fresh", "%% %3u %s", &u1, s1));
- tt_int_op(1,OP_EQ, tor_sscanf("99 fresh", "%3u%% %s", &u1, s1));
- tt_int_op(2,OP_EQ, tor_sscanf("99 fresh", "%3u %5s %%", &u1, s1));
- tt_int_op(99,OP_EQ, u1);
- tt_str_op(s1,OP_EQ, "fresh");
- tt_int_op(1,OP_EQ, tor_sscanf("% boo", "%% %3s", s1));
- tt_str_op("boo",OP_EQ, s1);
- /* Strings (ie. %s) */
- tt_int_op(2,OP_EQ, tor_sscanf("hello", "%3s%7s", s1, s2));
- tt_str_op(s1,OP_EQ, "hel");
- tt_str_op(s2,OP_EQ, "lo");
- tt_int_op(2,OP_EQ, tor_sscanf("WD40", "%2s%u", s3, &u1)); /* %s%u */
- tt_str_op(s3,OP_EQ, "WD");
- tt_int_op(40,OP_EQ, u1);
- tt_int_op(2,OP_EQ, tor_sscanf("WD40", "%3s%u", s3, &u1)); /* %s%u */
- tt_str_op(s3,OP_EQ, "WD4");
- tt_int_op(0,OP_EQ, u1);
- tt_int_op(2,OP_EQ, tor_sscanf("76trombones", "%6u%9s", &u1, s1)); /* %u%s */
- tt_int_op(76,OP_EQ, u1);
- tt_str_op(s1,OP_EQ, "trombones");
- tt_int_op(1,OP_EQ, tor_sscanf("prettylongstring", "%999s", s1));
- tt_str_op(s1,OP_EQ, "prettylongstring");
- /* %s doesn't eat spaces */
- tt_int_op(2,OP_EQ, tor_sscanf("hello world", "%9s %9s", s1, s2));
- tt_str_op(s1,OP_EQ, "hello");
- tt_str_op(s2,OP_EQ, "world");
- tt_int_op(2,OP_EQ, tor_sscanf("bye world?", "%9s %9s", s1, s2));
- tt_str_op(s1,OP_EQ, "bye");
- tt_str_op(s2,OP_EQ, "");
- tt_int_op(3,OP_EQ,
- tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3)); /* %s can be empty. */
- tt_str_op(s1,OP_EQ, "hi");
- tt_str_op(s2,OP_EQ, "");
- tt_str_op(s3,OP_EQ, "");
- tt_int_op(3,OP_EQ, tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
- tt_int_op(4,OP_EQ,
- tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
- tt_int_op(' ',OP_EQ, ch);
- r = tor_sscanf("12345 -67890 -1", "%d %ld %d", &int1, &lng1, &int2);
- tt_int_op(r,OP_EQ, 3);
- tt_int_op(int1,OP_EQ, 12345);
- tt_int_op(lng1,OP_EQ, -67890);
- tt_int_op(int2,OP_EQ, -1);
- #if SIZEOF_INT == 4
- /* %u */
- /* UINT32_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("4294967295", "%u", &u1));
- tt_int_op(4294967295U,OP_EQ, u1);
- /* But UINT32_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("4294967296", "%u", &u1));
- /* but parsing only 9... */
- tt_int_op(1,OP_EQ, tor_sscanf("4294967296", "%9u", &u1));
- tt_int_op(429496729U,OP_EQ, u1);
- /* %x */
- /* UINT32_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFF", "%x", &u1));
- tt_int_op(0xFFFFFFFF,OP_EQ, u1);
- /* But UINT32_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("100000000", "%x", &u1));
- /* %d */
- /* INT32_MIN and INT32_MAX should work */
- r = tor_sscanf("-2147483648. 2147483647.", "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(int1,OP_EQ, -2147483647 - 1);
- tt_int_op(int2,OP_EQ, 2147483647);
- /* But INT32_MIN - 1 and INT32_MAX + 1 shouldn't work */
- r = tor_sscanf("-2147483649.", "%d.", &int1);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("2147483648.", "%d.", &int1);
- tt_int_op(r,OP_EQ, 0);
- /* and the first failure stops further processing */
- r = tor_sscanf("-2147483648. 2147483648.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 1);
- r = tor_sscanf("-2147483649. 2147483647.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("2147483648. -2147483649.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 0);
- #elif SIZEOF_INT == 8
- /* %u */
- /* UINT64_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551615", "%u", &u1));
- tt_int_op(18446744073709551615U,OP_EQ, u1);
- /* But UINT64_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("18446744073709551616", "%u", &u1));
- /* but parsing only 19... */
- tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551616", "%19u", &u1));
- tt_int_op(1844674407370955161U,OP_EQ, u1);
- /* %x */
- /* UINT64_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFFFFFFFFFF", "%x", &u1));
- tt_int_op(0xFFFFFFFFFFFFFFFF,OP_EQ, u1);
- /* But UINT64_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("10000000000000000", "%x", &u1));
- /* %d */
- /* INT64_MIN and INT64_MAX should work */
- r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(int1,OP_EQ, -9223372036854775807 - 1);
- tt_int_op(int2,OP_EQ, 9223372036854775807);
- /* But INT64_MIN - 1 and INT64_MAX + 1 shouldn't work */
- r = tor_sscanf("-9223372036854775809.", "%d.", &int1);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("9223372036854775808.", "%d.", &int1);
- tt_int_op(r,OP_EQ, 0);
- /* and the first failure stops further processing */
- r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 1);
- r = tor_sscanf("-9223372036854775809. 9223372036854775807.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("9223372036854775808. -9223372036854775809.",
- "%d. %d.", &int1, &int2);
- tt_int_op(r,OP_EQ, 0);
- #endif
- #if SIZEOF_LONG == 4
- /* %lu */
- /* UINT32_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("4294967295", "%lu", &ulng));
- tt_int_op(4294967295UL,OP_EQ, ulng);
- /* But UINT32_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("4294967296", "%lu", &ulng));
- /* but parsing only 9... */
- tt_int_op(1,OP_EQ, tor_sscanf("4294967296", "%9lu", &ulng));
- tt_int_op(429496729UL,OP_EQ, ulng);
- /* %lx */
- /* UINT32_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFF", "%lx", &ulng));
- tt_int_op(0xFFFFFFFFUL,OP_EQ, ulng);
- /* But UINT32_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("100000000", "%lx", &ulng));
- /* %ld */
- /* INT32_MIN and INT32_MAX should work */
- r = tor_sscanf("-2147483648. 2147483647.", "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(lng1,OP_EQ, -2147483647L - 1L);
- tt_int_op(lng2,OP_EQ, 2147483647L);
- /* But INT32_MIN - 1 and INT32_MAX + 1 shouldn't work */
- r = tor_sscanf("-2147483649.", "%ld.", &lng1);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("2147483648.", "%ld.", &lng1);
- tt_int_op(r,OP_EQ, 0);
- /* and the first failure stops further processing */
- r = tor_sscanf("-2147483648. 2147483648.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 1);
- r = tor_sscanf("-2147483649. 2147483647.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("2147483648. -2147483649.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 0);
- #elif SIZEOF_LONG == 8
- /* %lu */
- /* UINT64_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551615", "%lu", &ulng));
- tt_int_op(18446744073709551615UL,OP_EQ, ulng);
- /* But UINT64_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("18446744073709551616", "%lu", &ulng));
- /* but parsing only 19... */
- tt_int_op(1,OP_EQ, tor_sscanf("18446744073709551616", "%19lu", &ulng));
- tt_int_op(1844674407370955161UL,OP_EQ, ulng);
- /* %lx */
- /* UINT64_MAX should work */
- tt_int_op(1,OP_EQ, tor_sscanf("FFFFFFFFFFFFFFFF", "%lx", &ulng));
- tt_int_op(0xFFFFFFFFFFFFFFFFUL,OP_EQ, ulng);
- /* But UINT64_MAX + 1 shouldn't work */
- tt_int_op(0,OP_EQ, tor_sscanf("10000000000000000", "%lx", &ulng));
- /* %ld */
- /* INT64_MIN and INT64_MAX should work */
- r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(lng1,OP_EQ, -9223372036854775807L - 1L);
- tt_int_op(lng2,OP_EQ, 9223372036854775807L);
- /* But INT64_MIN - 1 and INT64_MAX + 1 shouldn't work */
- r = tor_sscanf("-9223372036854775809.", "%ld.", &lng1);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("9223372036854775808.", "%ld.", &lng1);
- tt_int_op(r,OP_EQ, 0);
- /* and the first failure stops further processing */
- r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 1);
- r = tor_sscanf("-9223372036854775809. 9223372036854775807.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 0);
- r = tor_sscanf("9223372036854775808. -9223372036854775809.",
- "%ld. %ld.", &lng1, &lng2);
- tt_int_op(r,OP_EQ, 0);
- #endif
- r = tor_sscanf("123.456 .000007 -900123123.2000787 00003.2",
- "%lf %lf %lf %lf", &d1,&d2,&d3,&d4);
- tt_int_op(r,OP_EQ, 4);
- test_feq(d1, 123.456);
- test_feq(d2, .000007);
- test_feq(d3, -900123123.2000787);
- test_feq(d4, 3.2);
- done:
- ;
- }
- #define tt_char_op(a,op,b) tt_assert_op_type(a,op,b,char,"%c")
- #define tt_ci_char_op(a,op,b) tt_char_op(tolower(a),op,tolower(b))
- #ifndef HAVE_STRNLEN
- static size_t
- strnlen(const char *s, size_t len)
- {
- const char *p = memchr(s, 0, len);
- if (!p)
- return len;
- return p - s;
- }
- #endif
- static void
- test_util_format_time_interval(void *arg)
- {
- /* use the same sized buffer and integers as tor uses */
- #define DBUF_SIZE 64
- char dbuf[DBUF_SIZE];
- #define T_ "%ld"
- long sec, min, hour, day;
- /* we don't care about the exact spelling of the
- * second(s), minute(s), hour(s), day(s) labels */
- #define LABEL_SIZE 21
- #define L_ "%20s"
- char label_s[LABEL_SIZE];
- char label_m[LABEL_SIZE];
- char label_h[LABEL_SIZE];
- char label_d[LABEL_SIZE];
- #define TL_ T_ " " L_
- int r;
- (void)arg;
- /* In these tests, we're not picky about
- * spelling or abbreviations */
- /* seconds: 0, 1, 9, 10, 59 */
- /* ignore exact spelling of "second(s)"*/
- format_time_interval(dbuf, sizeof(dbuf), 0);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &sec, label_s);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(sec,OP_EQ, 0);
- format_time_interval(dbuf, sizeof(dbuf), 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &sec, label_s);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(sec,OP_EQ, 1);
- format_time_interval(dbuf, sizeof(dbuf), 10);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &sec, label_s);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(sec,OP_EQ, 10);
- format_time_interval(dbuf, sizeof(dbuf), 59);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &sec, label_s);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(sec,OP_EQ, 59);
- /* negative seconds are reported as their absolute value */
- format_time_interval(dbuf, sizeof(dbuf), -4);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &sec, label_s);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(sec,OP_EQ, 4);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- format_time_interval(dbuf, sizeof(dbuf), -32);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &sec, label_s);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(sec,OP_EQ, 32);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- /* minutes: 1:00, 1:01, 1:59, 2:00, 2:01, 59:59 */
- /* ignore trailing "0 second(s)", if present */
- format_time_interval(dbuf, sizeof(dbuf), 60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &min, label_m);
- tt_int_op(r,OP_EQ, 2);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(min,OP_EQ, 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- /* ignore exact spelling of "minute(s)," and "second(s)" */
- format_time_interval(dbuf, sizeof(dbuf), 60 + 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &min, label_m, &sec, label_s);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(min,OP_EQ, 1);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(sec,OP_EQ, 1);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- format_time_interval(dbuf, sizeof(dbuf), 60*2 - 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &min, label_m, &sec, label_s);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(min,OP_EQ, 1);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(sec,OP_EQ, 59);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- /* ignore trailing "0 second(s)", if present */
- format_time_interval(dbuf, sizeof(dbuf), 60*2);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &min, label_m);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(min,OP_EQ, 2);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* ignore exact spelling of "minute(s)," and "second(s)" */
- format_time_interval(dbuf, sizeof(dbuf), 60*2 + 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &min, label_m, &sec, label_s);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(min,OP_EQ, 2);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(sec,OP_EQ, 1);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- format_time_interval(dbuf, sizeof(dbuf), 60*60 - 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &min, label_m, &sec, label_s);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(min,OP_EQ, 59);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(sec,OP_EQ, 59);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- /* negative minutes are reported as their absolute value */
- /* ignore trailing "0 second(s)", if present */
- format_time_interval(dbuf, sizeof(dbuf), -3*60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &min, label_m);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(min,OP_EQ, 3);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* ignore exact spelling of "minute(s)," and "second(s)" */
- format_time_interval(dbuf, sizeof(dbuf), -96);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &min, label_m, &sec, label_s);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(min,OP_EQ, 1);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(sec,OP_EQ, 36);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- format_time_interval(dbuf, sizeof(dbuf), -2815);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &min, label_m, &sec, label_s);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(min,OP_EQ, 46);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- tt_int_op(sec,OP_EQ, 55);
- tt_ci_char_op(label_s[0],OP_EQ, 's');
- /* hours: 1:00, 1:00:01, 1:01, 23:59, 23:59:59 */
- /* always ignore trailing seconds, if present */
- /* ignore trailing "0 minute(s)" etc., if present */
- format_time_interval(dbuf, sizeof(dbuf), 60*60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &hour, label_h);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(hour,OP_EQ, 1);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- format_time_interval(dbuf, sizeof(dbuf), 60*60 + 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &hour, label_h);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(hour,OP_EQ, 1);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- /* ignore exact spelling of "hour(s)," etc. */
- format_time_interval(dbuf, sizeof(dbuf), 60*60 + 60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(hour,OP_EQ, 1);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 1);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- format_time_interval(dbuf, sizeof(dbuf), 24*60*60 - 60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(hour,OP_EQ, 23);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 59);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- format_time_interval(dbuf, sizeof(dbuf), 24*60*60 - 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(hour,OP_EQ, 23);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 59);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* negative hours are reported as their absolute value */
- /* ignore exact spelling of "hour(s)," etc., if present */
- format_time_interval(dbuf, sizeof(dbuf), -2*60*60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &hour, label_h);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(hour,OP_EQ, 2);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- format_time_interval(dbuf, sizeof(dbuf), -75804);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(hour,OP_EQ, 21);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 3);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* days: 1:00, 1:00:00:01, 1:00:01, 1:01 */
- /* always ignore trailing seconds, if present */
- /* ignore trailing "0 hours(s)" etc., if present */
- format_time_interval(dbuf, sizeof(dbuf), 24*60*60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &day, label_d);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(day,OP_EQ, 1);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 1);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_, &day, label_d);
- tt_int_op(r,OP_EQ, 2);
- tt_int_op(day,OP_EQ, 1);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- /* ignore exact spelling of "days(s)," etc. */
- format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- if (r == -1) {
- /* ignore 0 hours(s), if present */
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &day, label_d, &min, label_m);
- }
- tt_assert(r == 4 || r == 6);
- tt_int_op(day,OP_EQ, 1);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- if (r == 6) {
- tt_int_op(hour,OP_EQ, 0);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- }
- tt_int_op(min,OP_EQ, 1);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* ignore trailing "0 minutes(s)" etc., if present */
- format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 60*60);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_,
- &day, label_d, &hour, label_h);
- tt_int_op(r,OP_EQ, 4);
- tt_int_op(day,OP_EQ, 1);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 1);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- /* negative days are reported as their absolute value */
- format_time_interval(dbuf, sizeof(dbuf), -21936184);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 253);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 21);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 23);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* periods > 1 year are reported in days (warn?) */
- /* ignore exact spelling of "days(s)," etc., if present */
- format_time_interval(dbuf, sizeof(dbuf), 758635154);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 8780);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 11);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 59);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* negative periods > 1 year are reported in days (warn?) */
- format_time_interval(dbuf, sizeof(dbuf), -1427014922);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 16516);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 9);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 2);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- #if SIZEOF_LONG == 4 || SIZEOF_LONG == 8
- /* We can try INT32_MIN/MAX */
- /* Always ignore second(s) */
- /* INT32_MAX */
- format_time_interval(dbuf, sizeof(dbuf), 2147483647);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 24855);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 3);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 14);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* and 7 seconds - ignored */
- /* INT32_MIN: check that we get the absolute value of interval,
- * which doesn't actually fit in int32_t.
- * We expect INT32_MAX or INT32_MAX + 1 with 64 bit longs */
- format_time_interval(dbuf, sizeof(dbuf), -2147483647L - 1L);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 24855);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 3);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 14);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* and 7 or 8 seconds - ignored */
- #endif
- #if SIZEOF_LONG == 8
- /* We can try INT64_MIN/MAX */
- /* Always ignore second(s) */
- /* INT64_MAX */
- format_time_interval(dbuf, sizeof(dbuf), 9223372036854775807L);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 106751991167300L);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 15);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 30);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* and 7 seconds - ignored */
- /* INT64_MIN: check that we get the absolute value of interval,
- * which doesn't actually fit in int64_t.
- * We expect INT64_MAX */
- format_time_interval(dbuf, sizeof(dbuf),
- -9223372036854775807L - 1L);
- tt_int_op(strnlen(dbuf, DBUF_SIZE),OP_LE, DBUF_SIZE - 1);
- r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
- &day, label_d, &hour, label_h, &min, label_m);
- tt_int_op(r,OP_EQ, 6);
- tt_int_op(day,OP_EQ, 106751991167300L);
- tt_ci_char_op(label_d[0],OP_EQ, 'd');
- tt_int_op(hour,OP_EQ, 15);
- tt_ci_char_op(label_h[0],OP_EQ, 'h');
- tt_int_op(min,OP_EQ, 30);
- tt_ci_char_op(label_m[0],OP_EQ, 'm');
- /* and 7 or 8 seconds - ignored */
- #endif
- done:
- ;
- }
- #undef tt_char_op
- #undef tt_ci_char_op
- #undef DBUF_SIZE
- #undef T_
- #undef LABEL_SIZE
- #undef L_
- #undef TL_
- static void
- test_util_path_is_relative(void *arg)
- {
- /* OS-independent tests */
- (void)arg;
- tt_int_op(1,OP_EQ, path_is_relative(""));
- tt_int_op(1,OP_EQ, path_is_relative("dir"));
- tt_int_op(1,OP_EQ, path_is_relative("dir/"));
- tt_int_op(1,OP_EQ, path_is_relative("./dir"));
- tt_int_op(1,OP_EQ, path_is_relative("../dir"));
- tt_int_op(0,OP_EQ, path_is_relative("/"));
- tt_int_op(0,OP_EQ, path_is_relative("/dir"));
- tt_int_op(0,OP_EQ, path_is_relative("/dir/"));
- /* Windows */
- #ifdef _WIN32
- /* I don't have Windows so I can't test this, hence the "#ifdef
- 0". These are tests that look useful, so please try to get them
- running and uncomment if it all works as it should */
- tt_int_op(1,OP_EQ, path_is_relative("dir"));
- tt_int_op(1,OP_EQ, path_is_relative("dir\\"));
- tt_int_op(1,OP_EQ, path_is_relative("dir\\a:"));
- tt_int_op(1,OP_EQ, path_is_relative("dir\\a:\\"));
- tt_int_op(1,OP_EQ, path_is_relative("http:\\dir"));
- tt_int_op(0,OP_EQ, path_is_relative("\\dir"));
- tt_int_op(0,OP_EQ, path_is_relative("a:\\dir"));
- tt_int_op(0,OP_EQ, path_is_relative("z:\\dir"));
- #endif
- done:
- ;
- }
- #ifdef ENABLE_MEMPOOLS
- /** Run unittests for memory pool allocator */
- static void
- test_util_mempool(void *arg)
- {
- mp_pool_t *pool = NULL;
- smartlist_t *allocated = NULL;
- int i;
- (void)arg;
- pool = mp_pool_new(1, 100);
- tt_assert(pool);
- tt_assert(pool->new_chunk_capacity >= 100);
- tt_assert(pool->item_alloc_size >= sizeof(void*)+1);
- mp_pool_destroy(pool);
- pool = NULL;
- pool = mp_pool_new(241, 2500);
- tt_assert(pool);
- tt_assert(pool->new_chunk_capacity >= 10);
- tt_assert(pool->item_alloc_size >= sizeof(void*)+241);
- tt_int_op(pool->item_alloc_size & 0x03,OP_EQ, 0);
- tt_assert(pool->new_chunk_capacity < 60);
- allocated = smartlist_new();
- for (i = 0; i < 20000; ++i) {
- if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
- void *m = mp_pool_get(pool);
- memset(m, 0x09, 241);
- smartlist_add(allocated, m);
- //printf("%d: %p\n", i, m);
- //mp_pool_assert_ok(pool);
- } else {
- int idx = crypto_rand_int(smartlist_len(allocated));
- void *m = smartlist_get(allocated, idx);
- //printf("%d: free %p\n", i, m);
- smartlist_del(allocated, idx);
- mp_pool_release(m);
- //mp_pool_assert_ok(pool);
- }
- if (crypto_rand_int(777)==0)
- mp_pool_clean(pool, 1, 1);
- if (i % 777)
- mp_pool_assert_ok(pool);
- }
- done:
- if (allocated) {
- SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
- mp_pool_assert_ok(pool);
- mp_pool_clean(pool, 0, 0);
- mp_pool_assert_ok(pool);
- smartlist_free(allocated);
- }
- if (pool)
- mp_pool_destroy(pool);
- }
- #endif /* ENABLE_MEMPOOLS */
- /** Run unittests for memory area allocator */
- static void
- test_util_memarea(void *arg)
- {
- memarea_t *area = memarea_new();
- char *p1, *p2, *p3, *p1_orig;
- void *malloced_ptr = NULL;
- int i;
- (void)arg;
- tt_assert(area);
- p1_orig = p1 = memarea_alloc(area,64);
- p2 = memarea_alloc_zero(area,52);
- p3 = memarea_alloc(area,11);
- tt_assert(memarea_owns_ptr(area, p1));
- tt_assert(memarea_owns_ptr(area, p2));
- tt_assert(memarea_owns_ptr(area, p3));
- /* Make sure we left enough space. */
- tt_assert(p1+64 <= p2);
- tt_assert(p2+52 <= p3);
- /* Make sure we aligned. */
- tt_int_op(((uintptr_t)p1) % sizeof(void*),OP_EQ, 0);
- tt_int_op(((uintptr_t)p2) % sizeof(void*),OP_EQ, 0);
- tt_int_op(((uintptr_t)p3) % sizeof(void*),OP_EQ, 0);
- tt_assert(!memarea_owns_ptr(area, p3+8192));
- tt_assert(!memarea_owns_ptr(area, p3+30));
- tt_assert(tor_mem_is_zero(p2, 52));
- /* Make sure we don't overalign. */
- p1 = memarea_alloc(area, 1);
- p2 = memarea_alloc(area, 1);
- tt_ptr_op(p1+sizeof(void*),OP_EQ, p2);
- {
- malloced_ptr = tor_malloc(64);
- tt_assert(!memarea_owns_ptr(area, malloced_ptr));
- tor_free(malloced_ptr);
- }
- /* memarea_memdup */
- {
- malloced_ptr = tor_malloc(64);
- crypto_rand((char*)malloced_ptr, 64);
- p1 = memarea_memdup(area, malloced_ptr, 64);
- tt_assert(p1 != malloced_ptr);
- tt_mem_op(p1,OP_EQ, malloced_ptr, 64);
- tor_free(malloced_ptr);
- }
- /* memarea_strdup. */
- p1 = memarea_strdup(area,"");
- p2 = memarea_strdup(area, "abcd");
- tt_assert(p1);
- tt_assert(p2);
- tt_str_op(p1,OP_EQ, "");
- tt_str_op(p2,OP_EQ, "abcd");
- /* memarea_strndup. */
- {
- const char *s = "Ad ogni porta batte la morte e grida: il nome!";
- /* (From Turandot, act 3.) */
- size_t len = strlen(s);
- p1 = memarea_strndup(area, s, 1000);
- p2 = memarea_strndup(area, s, 10);
- tt_str_op(p1,OP_EQ, s);
- tt_assert(p2 >= p1 + len + 1);
- tt_mem_op(s,OP_EQ, p2, 10);
- tt_int_op(p2[10],OP_EQ, '\0');
- p3 = memarea_strndup(area, s, len);
- tt_str_op(p3,OP_EQ, s);
- p3 = memarea_strndup(area, s, len-1);
- tt_mem_op(s,OP_EQ, p3, len-1);
- tt_int_op(p3[len-1],OP_EQ, '\0');
- }
- memarea_clear(area);
- p1 = memarea_alloc(area, 1);
- tt_ptr_op(p1,OP_EQ, p1_orig);
- memarea_clear(area);
- /* Check for running over an area's size. */
- for (i = 0; i < 512; ++i) {
- p1 = memarea_alloc(area, crypto_rand_int(5)+1);
- tt_assert(memarea_owns_ptr(area, p1));
- }
- memarea_assert_ok(area);
- /* Make sure we can allocate a too-big object. */
- p1 = memarea_alloc_zero(area, 9000);
- p2 = memarea_alloc_zero(area, 16);
- tt_assert(memarea_owns_ptr(area, p1));
- tt_assert(memarea_owns_ptr(area, p2));
- done:
- memarea_drop_all(area);
- tor_free(malloced_ptr);
- }
- /** Run unit tests for utility functions to get file names relative to
- * the data directory. */
- static void
- test_util_datadir(void *arg)
- {
- char buf[1024];
- char *f = NULL;
- char *temp_dir = NULL;
- (void)arg;
- temp_dir = get_datadir_fname(NULL);
- f = get_datadir_fname("state");
- tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
- tt_str_op(f,OP_EQ, buf);
- tor_free(f);
- f = get_datadir_fname2("cache", "thingy");
- tor_snprintf(buf, sizeof(buf),
- "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
- tt_str_op(f,OP_EQ, buf);
- tor_free(f);
- f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
- tor_snprintf(buf, sizeof(buf),
- "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
- tt_str_op(f,OP_EQ, buf);
- tor_free(f);
- f = get_datadir_fname_suffix("cache", ".foo");
- tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
- temp_dir);
- tt_str_op(f,OP_EQ, buf);
- done:
- tor_free(f);
- tor_free(temp_dir);
- }
- static void
- test_util_strtok(void *arg)
- {
- char buf[128];
- char buf2[128];
- int i;
- char *cp1, *cp2;
- (void)arg;
- for (i = 0; i < 3; i++) {
- const char *pad1="", *pad2="";
- switch (i) {
- case 0:
- break;
- case 1:
- pad1 = " ";
- pad2 = "!";
- break;
- case 2:
- pad1 = " ";
- pad2 = ";!";
- break;
- }
- tor_snprintf(buf, sizeof(buf), "%s", pad1);
- tor_snprintf(buf2, sizeof(buf2), "%s", pad2);
- tt_assert(NULL == tor_strtok_r_impl(buf, " ", &cp1));
- tt_assert(NULL == tor_strtok_r_impl(buf2, ".!..;!", &cp2));
- tor_snprintf(buf, sizeof(buf),
- "%sGraved on the dark in gestures of descent%s", pad1, pad1);
- tor_snprintf(buf2, sizeof(buf2),
- "%sthey.seemed;;their!.own;most.perfect;monument%s",pad2,pad2);
- /* -- "Year's End", Richard Wilbur */
- tt_str_op("Graved",OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
- tt_str_op("they",OP_EQ, tor_strtok_r_impl(buf2, ".!..;!", &cp2));
- #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
- #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
- tt_str_op("on",OP_EQ, S1());
- tt_str_op("the",OP_EQ, S1());
- tt_str_op("dark",OP_EQ, S1());
- tt_str_op("seemed",OP_EQ, S2());
- tt_str_op("their",OP_EQ, S2());
- tt_str_op("own",OP_EQ, S2());
- tt_str_op("in",OP_EQ, S1());
- tt_str_op("gestures",OP_EQ, S1());
- tt_str_op("of",OP_EQ, S1());
- tt_str_op("most",OP_EQ, S2());
- tt_str_op("perfect",OP_EQ, S2());
- tt_str_op("descent",OP_EQ, S1());
- tt_str_op("monument",OP_EQ, S2());
- tt_ptr_op(NULL,OP_EQ, S1());
- tt_ptr_op(NULL,OP_EQ, S2());
- }
- buf[0] = 0;
- tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
- tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, "!", &cp1));
- strlcpy(buf, "Howdy!", sizeof(buf));
- tt_str_op("Howdy",OP_EQ, tor_strtok_r_impl(buf, "!", &cp1));
- tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(NULL, "!", &cp1));
- strlcpy(buf, " ", sizeof(buf));
- tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
- strlcpy(buf, " ", sizeof(buf));
- tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
- strlcpy(buf, "something ", sizeof(buf));
- tt_str_op("something",OP_EQ, tor_strtok_r_impl(buf, " ", &cp1));
- tt_ptr_op(NULL,OP_EQ, tor_strtok_r_impl(NULL, ";", &cp1));
- done:
- ;
- }
- static void
- test_util_find_str_at_start_of_line(void *ptr)
- {
- const char *long_string =
- "howdy world. how are you? i hope it's fine.\n"
- "hello kitty\n"
- "third line";
- char *line2 = strchr(long_string,'\n')+1;
- char *line3 = strchr(line2,'\n')+1;
- const char *short_string = "hello kitty\n"
- "second line\n";
- char *short_line2 = strchr(short_string,'\n')+1;
- (void)ptr;
- tt_ptr_op(long_string,OP_EQ, find_str_at_start_of_line(long_string, ""));
- tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(short_string, "nonsense"));
- tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "nonsense"));
- tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "\n"));
- tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "how "));
- tt_ptr_op(NULL,OP_EQ, find_str_at_start_of_line(long_string, "kitty"));
- tt_ptr_op(long_string,OP_EQ, find_str_at_start_of_line(long_string, "h"));
- tt_ptr_op(long_string,OP_EQ, find_str_at_start_of_line(long_string, "how"));
- tt_ptr_op(line2,OP_EQ, find_str_at_start_of_line(long_string, "he"));
- tt_ptr_op(line2,OP_EQ, find_str_at_start_of_line(long_string, "hell"));
- tt_ptr_op(line2,OP_EQ, find_str_at_start_of_line(long_string, "hello k"));
- tt_ptr_op(line2,OP_EQ,
- find_str_at_start_of_line(long_string, "hello kitty\n"));
- tt_ptr_op(line2,OP_EQ,
- find_str_at_start_of_line(long_string, "hello kitty\nt"));
- tt_ptr_op(line3,OP_EQ, find_str_at_start_of_line(long_string, "third"));
- tt_ptr_op(line3,OP_EQ, find_str_at_start_of_line(long_string, "third line"));
- tt_ptr_op(NULL, OP_EQ,
- find_str_at_start_of_line(long_string, "third line\n"));
- tt_ptr_op(short_line2,OP_EQ, find_str_at_start_of_line(short_string,
- "second line\n"));
- done:
- ;
- }
- static void
- test_util_string_is_C_identifier(void *ptr)
- {
- (void)ptr;
- tt_int_op(1,OP_EQ, string_is_C_identifier("string_is_C_identifier"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("_string_is_C_identifier"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("_"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("i"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("_____"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("__00__"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("__init__"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("_0"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("_0string_is_C_identifier"));
- tt_int_op(1,OP_EQ, string_is_C_identifier("_0"));
- tt_int_op(0,OP_EQ, string_is_C_identifier("0_string_is_C_identifier"));
- tt_int_op(0,OP_EQ, string_is_C_identifier("0"));
- tt_int_op(0,OP_EQ, string_is_C_identifier(""));
- tt_int_op(0,OP_EQ, string_is_C_identifier(";"));
- tt_int_op(0,OP_EQ, string_is_C_identifier("i;"));
- tt_int_op(0,OP_EQ, string_is_C_identifier("_;"));
- tt_int_op(0,OP_EQ, string_is_C_identifier("í"));
- tt_int_op(0,OP_EQ, string_is_C_identifier("ñ"));
- done:
- ;
- }
- static void
- test_util_asprintf(void *ptr)
- {
- #define LOREMIPSUM \
- "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
- char *cp=NULL, *cp2=NULL;
- int r;
- (void)ptr;
- /* simple string */
- r = tor_asprintf(&cp, "simple string 100%% safe");
- tt_assert(cp);
- tt_str_op("simple string 100% safe",OP_EQ, cp);
- tt_int_op(strlen(cp),OP_EQ, r);
- tor_free(cp);
- /* empty string */
- r = tor_asprintf(&cp, "%s", "");
- tt_assert(cp);
- tt_str_op("",OP_EQ, cp);
- tt_int_op(strlen(cp),OP_EQ, r);
- tor_free(cp);
- /* numbers (%i) */
- r = tor_asprintf(&cp, "I like numbers-%2i, %i, etc.", -1, 2);
- tt_assert(cp);
- tt_str_op("I like numbers--1, 2, etc.",OP_EQ, cp);
- tt_int_op(strlen(cp),OP_EQ, r);
- /* don't free cp; next test uses it. */
- /* numbers (%d) */
- r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
- tt_assert(cp2);
- tt_int_op(strlen(cp2),OP_EQ, r);
- tt_str_op("First=101, Second=202",OP_EQ, cp2);
- tt_assert(cp != cp2);
- tor_free(cp);
- tor_free(cp2);
- /* Glass-box test: a string exactly 128 characters long. */
- r = tor_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
- tt_assert(cp);
- tt_int_op(128,OP_EQ, r);
- tt_int_op(cp[128], OP_EQ, '\0');
- tt_str_op("Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM,OP_EQ, cp);
- tor_free(cp);
- /* String longer than 128 characters */
- r = tor_asprintf(&cp, "1: %s 2: %s 3: %s",
- LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
- tt_assert(cp);
- tt_int_op(strlen(cp),OP_EQ, r);
- tt_str_op("1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM,OP_EQ, cp);
- done:
- tor_free(cp);
- tor_free(cp2);
- }
- static void
- test_util_listdir(void *ptr)
- {
- smartlist_t *dir_contents = NULL;
- char *fname1=NULL, *fname2=NULL, *fname3=NULL, *dir1=NULL, *dirname=NULL;
- int r;
- (void)ptr;
- fname1 = tor_strdup(get_fname("hopscotch"));
- fname2 = tor_strdup(get_fname("mumblety-peg"));
- fname3 = tor_strdup(get_fname(".hidden-file"));
- dir1 = tor_strdup(get_fname("some-directory"));
- dirname = tor_strdup(get_fname(NULL));
- tt_int_op(0,OP_EQ, write_str_to_file(fname1, "X\n", 0));
- tt_int_op(0,OP_EQ, write_str_to_file(fname2, "Y\n", 0));
- tt_int_op(0,OP_EQ, write_str_to_file(fname3, "Z\n", 0));
- #ifdef _WIN32
- r = mkdir(dir1);
- #else
- r = mkdir(dir1, 0700);
- #endif
- if (r) {
- fprintf(stderr, "Can't create directory %s:", dir1);
- perror("");
- exit(1);
- }
- dir_contents = tor_listdir(dirname);
- tt_assert(dir_contents);
- /* make sure that each filename is listed. */
- tt_assert(smartlist_contains_string_case(dir_contents, "hopscotch"));
- tt_assert(smartlist_contains_string_case(dir_contents, "mumblety-peg"));
- tt_assert(smartlist_contains_string_case(dir_contents, ".hidden-file"));
- tt_assert(smartlist_contains_string_case(dir_contents, "some-directory"));
- tt_assert(!smartlist_contains_string(dir_contents, "."));
- tt_assert(!smartlist_contains_string(dir_contents, ".."));
- done:
- tor_free(fname1);
- tor_free(fname2);
- tor_free(fname3);
- tor_free(dir1);
- tor_free(dirname);
- if (dir_contents) {
- SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp));
- smartlist_free(dir_contents);
- }
- }
- static void
- test_util_parent_dir(void *ptr)
- {
- char *cp;
- (void)ptr;
- #define T(output,expect_ok,input) \
- do { \
- int ok; \
- cp = tor_strdup(input); \
- ok = get_parent_directory(cp); \
- tt_int_op(expect_ok, OP_EQ, ok); \
- if (ok==0) \
- tt_str_op(output, OP_EQ, cp); \
- tor_free(cp); \
- } while (0);
- T("/home/wombat", 0, "/home/wombat/knish");
- T("/home/wombat", 0, "/home/wombat/knish/");
- T("/home/wombat", 0, "/home/wombat/knish///");
- T("./home/wombat", 0, "./home/wombat/knish/");
- T("/", 0, "/home");
- T("/", 0, "/home//");
- T(".", 0, "./wombat");
- T(".", 0, "./wombat/");
- T(".", 0, "./wombat//");
- T("wombat", 0, "wombat/foo");
- T("wombat/..", 0, "wombat/../foo");
- T("wombat/../", 0, "wombat/..//foo"); /* Is this correct? */
- T("wombat/.", 0, "wombat/./foo");
- T("wombat/./", 0, "wombat/.//foo"); /* Is this correct? */
- T("wombat", 0, "wombat/..//");
- T("wombat", 0, "wombat/foo/");
- T("wombat", 0, "wombat/.foo");
- T("wombat", 0, "wombat/.foo/");
- T("wombat", -1, "");
- T("w", -1, "");
- T("wombat", 0, "wombat/knish");
- T("/", 0, "/");
- T("/", 0, "////");
- done:
- tor_free(cp);
- }
- static void
- test_util_ftruncate(void *ptr)
- {
- char *buf = NULL;
- const char *fname;
- int fd = -1;
- const char *message = "Hello world";
- const char *message2 = "Hola mundo";
- struct stat st;
- (void) ptr;
- fname = get_fname("ftruncate");
- fd = tor_open_cloexec(fname, O_WRONLY|O_CREAT, 0600);
- tt_int_op(fd, OP_GE, 0);
- /* Make the file be there. */
- tt_int_op(strlen(message), OP_EQ, write_all(fd, message, strlen(message),0));
- tt_int_op((int)tor_fd_getpos(fd), OP_EQ, strlen(message));
- tt_int_op(0, OP_EQ, fstat(fd, &st));
- tt_int_op((int)st.st_size, OP_EQ, strlen(message));
- /* Truncate and see if it got truncated */
- tt_int_op(0, OP_EQ, tor_ftruncate(fd));
- tt_int_op((int)tor_fd_getpos(fd), OP_EQ, 0);
- tt_int_op(0, OP_EQ, fstat(fd, &st));
- tt_int_op((int)st.st_size, OP_EQ, 0);
- /* Replace, and see if it got replaced */
- tt_int_op(strlen(message2), OP_EQ,
- write_all(fd, message2, strlen(message2), 0));
- tt_int_op((int)tor_fd_getpos(fd), OP_EQ, strlen(message2));
- tt_int_op(0, OP_EQ, fstat(fd, &st));
- tt_int_op((int)st.st_size, OP_EQ, strlen(message2));
- close(fd);
- fd = -1;
- buf = read_file_to_str(fname, 0, NULL);
- tt_str_op(message2, OP_EQ, buf);
- done:
- if (fd >= 0)
- close(fd);
- tor_free(buf);
- }
- #ifdef _WIN32
- static void
- test_util_load_win_lib(void *ptr)
- {
- HANDLE h = load_windows_system_library(_T("advapi32.dll"));
- (void) ptr;
- tt_assert(h);
- done:
- if (h)
- FreeLibrary(h);
- }
- #endif
- #ifndef _WIN32
- static void
- clear_hex_errno(char *hex_errno)
- {
- memset(hex_errno, '\0', HEX_ERRNO_SIZE + 1);
- }
- static void
- test_util_exit_status(void *ptr)
- {
- /* Leave an extra byte for a \0 so we can do string comparison */
- char hex_errno[HEX_ERRNO_SIZE + 1];
- int n;
- (void)ptr;
- clear_hex_errno(hex_errno);
- tt_str_op("",OP_EQ, hex_errno);
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0, 0, hex_errno);
- tt_str_op("0/0\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- #if SIZEOF_INT == 4
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0, 0x7FFFFFFF, hex_errno);
- tt_str_op("0/7FFFFFFF\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0xFF, -0x80000000, hex_errno);
- tt_str_op("FF/-80000000\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- tt_int_op(n,OP_EQ, HEX_ERRNO_SIZE);
- #elif SIZEOF_INT == 8
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0, 0x7FFFFFFFFFFFFFFF, hex_errno);
- tt_str_op("0/7FFFFFFFFFFFFFFF\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0xFF, -0x8000000000000000, hex_errno);
- tt_str_op("FF/-8000000000000000\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- tt_int_op(n,OP_EQ, HEX_ERRNO_SIZE);
- #endif
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0x7F, 0, hex_errno);
- tt_str_op("7F/0\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- clear_hex_errno(hex_errno);
- n = format_helper_exit_status(0x08, -0x242, hex_errno);
- tt_str_op("8/-242\n",OP_EQ, hex_errno);
- tt_int_op(n,OP_EQ, strlen(hex_errno));
- clear_hex_errno(hex_errno);
- tt_str_op("",OP_EQ, hex_errno);
- done:
- ;
- }
- #endif
- #ifndef _WIN32
- /* Check that fgets with a non-blocking pipe returns partial lines and sets
- * EAGAIN, returns full lines and sets no error, and returns NULL on EOF and
- * sets no error */
- static void
- test_util_fgets_eagain(void *ptr)
- {
- int test_pipe[2] = {-1, -1};
- int retval;
- ssize_t retlen;
- char *retptr;
- FILE *test_stream = NULL;
- char buf[4] = { 0 };
- (void)ptr;
- errno = 0;
- /* Set up a pipe to test on */
- retval = pipe(test_pipe);
- tt_int_op(retval, OP_EQ, 0);
- /* Set up the read-end to be non-blocking */
- retval = fcntl(test_pipe[0], F_SETFL, O_NONBLOCK);
- tt_int_op(retval, OP_EQ, 0);
- /* Open it as a stdio stream */
- test_stream = fdopen(test_pipe[0], "r");
- tt_ptr_op(test_stream, OP_NE, NULL);
- /* Send in a partial line */
- retlen = write(test_pipe[1], "A", 1);
- tt_int_op(retlen, OP_EQ, 1);
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, EAGAIN);
- tt_ptr_op(retptr, OP_EQ, buf);
- tt_str_op(buf, OP_EQ, "A");
- errno = 0;
- /* Send in the rest */
- retlen = write(test_pipe[1], "B\n", 2);
- tt_int_op(retlen, OP_EQ, 2);
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, 0);
- tt_ptr_op(retptr, OP_EQ, buf);
- tt_str_op(buf, OP_EQ, "B\n");
- errno = 0;
- /* Send in a full line */
- retlen = write(test_pipe[1], "CD\n", 3);
- tt_int_op(retlen, OP_EQ, 3);
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, 0);
- tt_ptr_op(retptr, OP_EQ, buf);
- tt_str_op(buf, OP_EQ, "CD\n");
- errno = 0;
- /* Send in a partial line */
- retlen = write(test_pipe[1], "E", 1);
- tt_int_op(retlen, OP_EQ, 1);
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, EAGAIN);
- tt_ptr_op(retptr, OP_EQ, buf);
- tt_str_op(buf, OP_EQ, "E");
- errno = 0;
- /* Send in the rest */
- retlen = write(test_pipe[1], "F\n", 2);
- tt_int_op(retlen, OP_EQ, 2);
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, 0);
- tt_ptr_op(retptr, OP_EQ, buf);
- tt_str_op(buf, OP_EQ, "F\n");
- errno = 0;
- /* Send in a full line and close */
- retlen = write(test_pipe[1], "GH", 2);
- tt_int_op(retlen, OP_EQ, 2);
- retval = close(test_pipe[1]);
- tt_int_op(retval, OP_EQ, 0);
- test_pipe[1] = -1;
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, 0);
- tt_ptr_op(retptr, OP_EQ, buf);
- tt_str_op(buf, OP_EQ, "GH");
- errno = 0;
- /* Check for EOF */
- retptr = fgets(buf, sizeof(buf), test_stream);
- tt_int_op(errno, OP_EQ, 0);
- tt_ptr_op(retptr, OP_EQ, NULL);
- retval = feof(test_stream);
- tt_int_op(retval, OP_NE, 0);
- errno = 0;
- /* Check that buf is unchanged according to C99 and C11 */
- tt_str_op(buf, OP_EQ, "GH");
- done:
- if (test_stream != NULL)
- fclose(test_stream);
- if (test_pipe[0] != -1)
- close(test_pipe[0]);
- if (test_pipe[1] != -1)
- close(test_pipe[1]);
- }
- #endif
- #ifndef BUILDDIR
- #define BUILDDIR "."
- #endif
- #ifdef _WIN32
- #define notify_pending_waitpid_callbacks() STMT_NIL
- #define TEST_CHILD "test-child.exe"
- #define EOL "\r\n"
- #else
- #define TEST_CHILD (BUILDDIR "/src/test/test-child")
- #define EOL "\n"
- #endif
- #ifdef _WIN32
- /* I've assumed Windows doesn't have the gap between fork and exec
- * that causes the race condition on unix-like platforms */
- #define MATCH_PROCESS_STATUS(s1,s2) ((s1) == (s2))
- #else
- /* work around a race condition of the timing of SIGCHLD handler updates
- * to the process_handle's fields, and checks of those fields
- *
- * TODO: Once we can signal failure to exec, change PROCESS_STATUS_RUNNING to
- * PROCESS_STATUS_ERROR (and similarly with *_OR_NOTRUNNING) */
- #define PROCESS_STATUS_RUNNING_OR_NOTRUNNING (PROCESS_STATUS_RUNNING+1)
- #define IS_RUNNING_OR_NOTRUNNING(s) \
- ((s) == PROCESS_STATUS_RUNNING || (s) == PROCESS_STATUS_NOTRUNNING)
- /* well, this is ugly */
- #define MATCH_PROCESS_STATUS(s1,s2) \
- ( (s1) == (s2) \
- ||((s1) == PROCESS_STATUS_RUNNING_OR_NOTRUNNING \
- && IS_RUNNING_OR_NOTRUNNING(s2)) \
- ||((s2) == PROCESS_STATUS_RUNNING_OR_NOTRUNNING \
- && IS_RUNNING_OR_NOTRUNNING(s1)))
- #endif // _WIN32
- /** Helper function for testing tor_spawn_background */
- static void
- run_util_spawn_background(const char *argv[], const char *expected_out,
- const char *expected_err, int expected_exit,
- int expected_status)
- {
- int retval, exit_code;
- ssize_t pos;
- process_handle_t *process_handle=NULL;
- char stdout_buf[100], stderr_buf[100];
- int status;
- /* Start the program */
- #ifdef _WIN32
- status = tor_spawn_background(NULL, argv, NULL, &process_handle);
- #else
- status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
- #endif
- notify_pending_waitpid_callbacks();
- /* the race condition doesn't affect status,
- * because status isn't updated by the SIGCHLD handler,
- * but we still need to handle PROCESS_STATUS_RUNNING_OR_NOTRUNNING */
- tt_assert(MATCH_PROCESS_STATUS(expected_status, status));
- if (status == PROCESS_STATUS_ERROR) {
- tt_ptr_op(process_handle, OP_EQ, NULL);
- return;
- }
- tt_assert(process_handle != NULL);
- /* When a spawned process forks, fails, then exits very quickly,
- * (this typically occurs when exec fails)
- * there is a race condition between the SIGCHLD handler
- * updating the process_handle's fields, and this test
- * checking the process status in those fields.
- * The SIGCHLD update can occur before or after the code below executes.
- * This causes intermittent failures in spawn_background_fail(),
- * typically when the machine is under load.
- * We use PROCESS_STATUS_RUNNING_OR_NOTRUNNING to avoid this issue. */
- /* the race condition affects the change in
- * process_handle->status from RUNNING to NOTRUNNING */
- tt_assert(MATCH_PROCESS_STATUS(expected_status, process_handle->status));
- #ifndef _WIN32
- notify_pending_waitpid_callbacks();
- /* the race condition affects the change in
- * process_handle->waitpid_cb to NULL,
- * so we skip the check if expected_status is ambiguous,
- * that is, PROCESS_STATUS_RUNNING_OR_NOTRUNNING */
- tt_assert(process_handle->waitpid_cb != NULL
- || expected_status == PROCESS_STATUS_RUNNING_OR_NOTRUNNING);
- #endif
- #ifdef _WIN32
- tt_assert(process_handle->stdout_pipe != INVALID_HANDLE_VALUE);
- tt_assert(process_handle->stderr_pipe != INVALID_HANDLE_VALUE);
- #else
- tt_assert(process_handle->stdout_pipe >= 0);
- tt_assert(process_handle->stderr_pipe >= 0);
- #endif
- /* Check stdout */
- pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
- sizeof(stdout_buf) - 1);
- tt_assert(pos >= 0);
- stdout_buf[pos] = '\0';
- tt_int_op(strlen(expected_out),OP_EQ, pos);
- tt_str_op(expected_out,OP_EQ, stdout_buf);
- notify_pending_waitpid_callbacks();
- /* Check it terminated correctly */
- retval = tor_get_exit_code(process_handle, 1, &exit_code);
- tt_int_op(PROCESS_EXIT_EXITED,OP_EQ, retval);
- tt_int_op(expected_exit,OP_EQ, exit_code);
- // TODO: Make test-child exit with something other than 0
- #ifndef _WIN32
- notify_pending_waitpid_callbacks();
- tt_ptr_op(process_handle->waitpid_cb, OP_EQ, NULL);
- #endif
- /* Check stderr */
- pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
- sizeof(stderr_buf) - 1);
- tt_assert(pos >= 0);
- stderr_buf[pos] = '\0';
- tt_str_op(expected_err,OP_EQ, stderr_buf);
- tt_int_op(strlen(expected_err),OP_EQ, pos);
- notify_pending_waitpid_callbacks();
- done:
- if (process_handle)
- tor_process_handle_destroy(process_handle, 1);
- }
- /** Check that we can launch a process and read the output */
- static void
- test_util_spawn_background_ok(void *ptr)
- {
- const char *argv[] = {TEST_CHILD, "--test", NULL};
- const char *expected_out = "OUT"EOL "--test"EOL "SLEEPING"EOL "DONE" EOL;
- const char *expected_err = "ERR"EOL;
- (void)ptr;
- run_util_spawn_background(argv, expected_out, expected_err, 0,
- PROCESS_STATUS_RUNNING);
- }
- /** Check that failing to find the executable works as expected */
- static void
- test_util_spawn_background_fail(void *ptr)
- {
- const char *argv[] = {BUILDDIR "/src/test/no-such-file", "--test", NULL};
- const char *expected_err = "";
- char expected_out[1024];
- char code[32];
- #ifdef _WIN32
- const int expected_status = PROCESS_STATUS_ERROR;
- #else
- /* TODO: Once we can signal failure to exec, set this to be
- * PROCESS_STATUS_RUNNING_OR_ERROR */
- const int expected_status = PROCESS_STATUS_RUNNING_OR_NOTRUNNING;
- #endif
- memset(expected_out, 0xf0, sizeof(expected_out));
- memset(code, 0xf0, sizeof(code));
- (void)ptr;
- tor_snprintf(code, sizeof(code), "%x/%x",
- 9 /* CHILD_STATE_FAILEXEC */ , ENOENT);
- tor_snprintf(expected_out, sizeof(expected_out),
- "ERR: Failed to spawn background process - code %s\n", code);
- run_util_spawn_background(argv, expected_out, expected_err, 255,
- expected_status);
- }
- /** Test that reading from a handle returns a partial read rather than
- * blocking */
- static void
- test_util_spawn_background_partial_read_impl(int exit_early)
- {
- const int expected_exit = 0;
- const int expected_status = PROCESS_STATUS_RUNNING;
- int retval, exit_code;
- ssize_t pos = -1;
- process_handle_t *process_handle=NULL;
- int status;
- char stdout_buf[100], stderr_buf[100];
- const char *argv[] = {TEST_CHILD, "--test", NULL};
- const char *expected_out[] = { "OUT" EOL "--test" EOL "SLEEPING" EOL,
- "DONE" EOL,
- NULL };
- const char *expected_err = "ERR" EOL;
- #ifndef _WIN32
- int eof = 0;
- #endif
- int expected_out_ctr;
- if (exit_early) {
- argv[1] = "--hang";
- expected_out[0] = "OUT"EOL "--hang"EOL "SLEEPING" EOL;
- }
- /* Start the program */
- #ifdef _WIN32
- status = tor_spawn_background(NULL, argv, NULL, &process_handle);
- #else
- status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
- #endif
- tt_int_op(expected_status,OP_EQ, status);
- tt_assert(process_handle);
- tt_int_op(expected_status,OP_EQ, process_handle->status);
- /* Check stdout */
- for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
- #ifdef _WIN32
- pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
- sizeof(stdout_buf) - 1, NULL);
- #else
- /* Check that we didn't read the end of file last time */
- tt_assert(!eof);
- pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
- sizeof(stdout_buf) - 1, NULL, &eof);
- #endif
- log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
- /* We would have blocked, keep on trying */
- if (0 == pos)
- continue;
- tt_assert(pos > 0);
- stdout_buf[pos] = '\0';
- tt_str_op(expected_out[expected_out_ctr],OP_EQ, stdout_buf);
- tt_int_op(strlen(expected_out[expected_out_ctr]),OP_EQ, pos);
- expected_out_ctr++;
- }
- if (exit_early) {
- tor_process_handle_destroy(process_handle, 1);
- process_handle = NULL;
- goto done;
- }
- /* The process should have exited without writing more */
- #ifdef _WIN32
- pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
- sizeof(stdout_buf) - 1,
- process_handle);
- tt_int_op(0,OP_EQ, pos);
- #else
- if (!eof) {
- /* We should have got all the data, but maybe not the EOF flag */
- pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
- sizeof(stdout_buf) - 1,
- process_handle, &eof);
- tt_int_op(0,OP_EQ, pos);
- tt_assert(eof);
- }
- /* Otherwise, we got the EOF on the last read */
- #endif
- /* Check it terminated correctly */
- retval = tor_get_exit_code(process_handle, 1, &exit_code);
- tt_int_op(PROCESS_EXIT_EXITED,OP_EQ, retval);
- tt_int_op(expected_exit,OP_EQ, exit_code);
- // TODO: Make test-child exit with something other than 0
- /* Check stderr */
- pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
- sizeof(stderr_buf) - 1);
- tt_assert(pos >= 0);
- stderr_buf[pos] = '\0';
- tt_str_op(expected_err,OP_EQ, stderr_buf);
- tt_int_op(strlen(expected_err),OP_EQ, pos);
- done:
- tor_process_handle_destroy(process_handle, 1);
- }
- static void
- test_util_spawn_background_partial_read(void *arg)
- {
- (void)arg;
- test_util_spawn_background_partial_read_impl(0);
- }
- static void
- test_util_spawn_background_exit_early(void *arg)
- {
- (void)arg;
- test_util_spawn_background_partial_read_impl(1);
- }
- static void
- test_util_spawn_background_waitpid_notify(void *arg)
- {
- int retval, exit_code;
- process_handle_t *process_handle=NULL;
- int status;
- int ms_timer;
- const char *argv[] = {TEST_CHILD, "--fast", NULL};
- (void) arg;
- #ifdef _WIN32
- status = tor_spawn_background(NULL, argv, NULL, &process_handle);
- #else
- status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
- #endif
- tt_int_op(status, OP_EQ, PROCESS_STATUS_RUNNING);
- tt_ptr_op(process_handle, OP_NE, NULL);
- /* We're not going to look at the stdout/stderr output this time. Instead,
- * we're testing whether notify_pending_waitpid_calbacks() can report the
- * process exit (on unix) and/or whether tor_get_exit_code() can notice it
- * (on windows) */
- #ifndef _WIN32
- ms_timer = 30*1000;
- tt_ptr_op(process_handle->waitpid_cb, OP_NE, NULL);
- while (process_handle->waitpid_cb && ms_timer > 0) {
- tor_sleep_msec(100);
- ms_timer -= 100;
- notify_pending_waitpid_callbacks();
- }
- tt_int_op(ms_timer, OP_GT, 0);
- tt_ptr_op(process_handle->waitpid_cb, OP_EQ, NULL);
- #endif
- ms_timer = 30*1000;
- while (((retval = tor_get_exit_code(process_handle, 0, &exit_code))
- == PROCESS_EXIT_RUNNING) && ms_timer > 0) {
- tor_sleep_msec(100);
- ms_timer -= 100;
- }
- tt_int_op(ms_timer, OP_GT, 0);
- tt_int_op(retval, OP_EQ, PROCESS_EXIT_EXITED);
- done:
- tor_process_handle_destroy(process_handle, 1);
- }
- #undef TEST_CHILD
- #undef EOL
- #undef MATCH_PROCESS_STATUS
- #ifndef _WIN32
- #undef PROCESS_STATUS_RUNNING_OR_NOTRUNNING
- #undef IS_RUNNING_OR_NOTRUNNING
- #endif
- /**
- * Test for format_hex_number_sigsafe()
- */
- static void
- test_util_format_hex_number(void *ptr)
- {
- int i, len;
- char buf[33];
- const struct {
- const char *str;
- unsigned int x;
- } test_data[] = {
- {"0", 0},
- {"1", 1},
- {"273A", 0x273a},
- {"FFFF", 0xffff},
- {"7FFFFFFF", 0x7fffffff},
- {"FFFFFFFF", 0xffffffff},
- #if UINT_MAX >= 0xffffffff
- {"31BC421D", 0x31bc421d},
- {"FFFFFFFF", 0xffffffff},
- #endif
- {NULL, 0}
- };
- (void)ptr;
- for (i = 0; test_data[i].str != NULL; ++i) {
- len = format_hex_number_sigsafe(test_data[i].x, buf, sizeof(buf));
- tt_int_op(len,OP_NE, 0);
- tt_int_op(len,OP_EQ, strlen(buf));
- tt_str_op(buf,OP_EQ, test_data[i].str);
- }
- tt_int_op(4,OP_EQ, format_hex_number_sigsafe(0xffff, buf, 5));
- tt_str_op(buf,OP_EQ, "FFFF");
- tt_int_op(0,OP_EQ, format_hex_number_sigsafe(0xffff, buf, 4));
- tt_int_op(0,OP_EQ, format_hex_number_sigsafe(0, buf, 1));
- done:
- return;
- }
- /**
- * Test for format_hex_number_sigsafe()
- */
- static void
- test_util_format_dec_number(void *ptr)
- {
- int i, len;
- char buf[33];
- const struct {
- const char *str;
- unsigned int x;
- } test_data[] = {
- {"0", 0},
- {"1", 1},
- {"1234", 1234},
- {"12345678", 12345678},
- {"99999999", 99999999},
- {"100000000", 100000000},
- {"4294967295", 4294967295u},
- #if UINT_MAX > 0xffffffff
- {"18446744073709551615", 18446744073709551615u },
- #endif
- {NULL, 0}
- };
- (void)ptr;
- for (i = 0; test_data[i].str != NULL; ++i) {
- len = format_dec_number_sigsafe(test_data[i].x, buf, sizeof(buf));
- tt_int_op(len,OP_NE, 0);
- tt_int_op(len,OP_EQ, strlen(buf));
- tt_str_op(buf,OP_EQ, test_data[i].str);
- len = format_dec_number_sigsafe(test_data[i].x, buf,
- (int)(strlen(test_data[i].str) + 1));
- tt_int_op(len,OP_EQ, strlen(buf));
- tt_str_op(buf,OP_EQ, test_data[i].str);
- }
- tt_int_op(4,OP_EQ, format_dec_number_sigsafe(7331, buf, 5));
- tt_str_op(buf,OP_EQ, "7331");
- tt_int_op(0,OP_EQ, format_dec_number_sigsafe(7331, buf, 4));
- tt_int_op(1,OP_EQ, format_dec_number_sigsafe(0, buf, 2));
- tt_int_op(0,OP_EQ, format_dec_number_sigsafe(0, buf, 1));
- done:
- return;
- }
- /**
- * Test that we can properly format a Windows command line
- */
- static void
- test_util_join_win_cmdline(void *ptr)
- {
- /* Based on some test cases from "Parsing C++ Command-Line Arguments" in
- * MSDN but we don't exercise all quoting rules because tor_join_win_cmdline
- * will try to only generate simple cases for the child process to parse;
- * i.e. we never embed quoted strings in arguments. */
- const char *argvs[][4] = {
- {"a", "bb", "CCC", NULL}, // Normal
- {NULL, NULL, NULL, NULL}, // Empty argument list
- {"", NULL, NULL, NULL}, // Empty argument
- {"\"a", "b\"b", "CCC\"", NULL}, // Quotes
- {"a\tbc", "dd dd", "E", NULL}, // Whitespace
- {"a\\\\\\b", "de fg", "H", NULL}, // Backslashes
- {"a\\\"b", "\\c", "D\\", NULL}, // Backslashes before quote
- {"a\\\\b c", "d", "E", NULL}, // Backslashes not before quote
- { NULL } // Terminator
- };
- const char *cmdlines[] = {
- "a bb CCC",
- "",
- "\"\"",
- "\\\"a b\\\"b CCC\\\"",
- "\"a\tbc\" \"dd dd\" E",
- "a\\\\\\b \"de fg\" H",
- "a\\\\\\\"b \\c D\\",
- "\"a\\\\b c\" d E",
- NULL // Terminator
- };
- int i;
- char *joined_argv = NULL;
- (void)ptr;
- for (i=0; cmdlines[i]!=NULL; i++) {
- log_info(LD_GENERAL, "Joining argvs[%d], expecting <%s>", i, cmdlines[i]);
- joined_argv = tor_join_win_cmdline(argvs[i]);
- tt_str_op(cmdlines[i],OP_EQ, joined_argv);
- tor_free(joined_argv);
- }
- done:
- tor_free(joined_argv);
- }
- #define MAX_SPLIT_LINE_COUNT 4
- struct split_lines_test_t {
- const char *orig_line; // Line to be split (may contain \0's)
- int orig_length; // Length of orig_line
- const char *split_line[MAX_SPLIT_LINE_COUNT]; // Split lines
- };
- /**
- * Test that we properly split a buffer into lines
- */
- static void
- test_util_split_lines(void *ptr)
- {
- /* Test cases. orig_line of last test case must be NULL.
- * The last element of split_line[i] must be NULL. */
- struct split_lines_test_t tests[] = {
- {"", 0, {NULL}},
- {"foo", 3, {"foo", NULL}},
- {"\n\rfoo\n\rbar\r\n", 12, {"foo", "bar", NULL}},
- {"fo o\r\nb\tar", 10, {"fo o", "b.ar", NULL}},
- {"\x0f""f\0o\0\n\x01""b\0r\0\r", 12, {".f.o.", ".b.r.", NULL}},
- {"line 1\r\nline 2", 14, {"line 1", "line 2", NULL}},
- {"line 1\r\n\r\nline 2", 16, {"line 1", "line 2", NULL}},
- {"line 1\r\n\r\r\r\nline 2", 18, {"line 1", "line 2", NULL}},
- {"line 1\r\n\n\n\n\rline 2", 18, {"line 1", "line 2", NULL}},
- {"line 1\r\n\r\t\r\nline 3", 18, {"line 1", ".", "line 3", NULL}},
- {"\n\t\r\t\nline 3", 11, {".", ".", "line 3", NULL}},
- {NULL, 0, { NULL }}
- };
- int i, j;
- char *orig_line=NULL;
- smartlist_t *sl=NULL;
- (void)ptr;
- for (i=0; tests[i].orig_line; i++) {
- sl = smartlist_new();
- /* Allocate space for string and trailing NULL */
- orig_line = tor_memdup(tests[i].orig_line, tests[i].orig_length + 1);
- tor_split_lines(sl, orig_line, tests[i].orig_length);
- j = 0;
- log_info(LD_GENERAL, "Splitting test %d of length %d",
- i, tests[i].orig_length);
- SMARTLIST_FOREACH_BEGIN(sl, const char *, line) {
- /* Check we have not got too many lines */
- tt_int_op(MAX_SPLIT_LINE_COUNT, OP_GT, j);
- /* Check that there actually should be a line here */
- tt_assert(tests[i].split_line[j] != NULL);
- log_info(LD_GENERAL, "Line %d of test %d, should be <%s>",
- j, i, tests[i].split_line[j]);
- /* Check that the line is as expected */
- tt_str_op(line,OP_EQ, tests[i].split_line[j]);
- j++;
- } SMARTLIST_FOREACH_END(line);
- /* Check that we didn't miss some lines */
- tt_ptr_op(NULL,OP_EQ, tests[i].split_line[j]);
- tor_free(orig_line);
- smartlist_free(sl);
- sl = NULL;
- }
- done:
- tor_free(orig_line);
- smartlist_free(sl);
- }
- static void
- test_util_di_ops(void *arg)
- {
- #define LT -1
- #define GT 1
- #define EQ 0
- const struct {
- const char *a; int want_sign; const char *b;
- } examples[] = {
- { "Foo", EQ, "Foo" },
- { "foo", GT, "bar", },
- { "foobar", EQ ,"foobar" },
- { "foobar", LT, "foobaw" },
- { "foobar", GT, "f00bar" },
- { "foobar", GT, "boobar" },
- { "", EQ, "" },
- { NULL, 0, NULL },
- };
- int i;
- (void)arg;
- for (i = 0; examples[i].a; ++i) {
- size_t len = strlen(examples[i].a);
- int eq1, eq2, neq1, neq2, cmp1, cmp2;
- tt_int_op(len,OP_EQ, strlen(examples[i].b));
- /* We do all of the operations, with operands in both orders. */
- eq1 = tor_memeq(examples[i].a, examples[i].b, len);
- eq2 = tor_memeq(examples[i].b, examples[i].a, len);
- neq1 = tor_memneq(examples[i].a, examples[i].b, len);
- neq2 = tor_memneq(examples[i].b, examples[i].a, len);
- cmp1 = tor_memcmp(examples[i].a, examples[i].b, len);
- cmp2 = tor_memcmp(examples[i].b, examples[i].a, len);
- /* Check for correctness of cmp1 */
- if (cmp1 < 0 && examples[i].want_sign != LT)
- TT_DIE(("Assertion failed."));
- else if (cmp1 > 0 && examples[i].want_sign != GT)
- TT_DIE(("Assertion failed."));
- else if (cmp1 == 0 && examples[i].want_sign != EQ)
- TT_DIE(("Assertion failed."));
- /* Check for consistency of everything else with cmp1 */
- tt_int_op(eq1,OP_EQ, eq2);
- tt_int_op(neq1,OP_EQ, neq2);
- tt_int_op(cmp1,OP_EQ, -cmp2);
- tt_int_op(eq1,OP_EQ, cmp1 == 0);
- tt_int_op(neq1,OP_EQ, !eq1);
- }
- {
- uint8_t zz = 0;
- uint8_t ii = 0;
- int z;
- /* exhaustively test tor_memeq and tor_memcmp
- * against each possible single-byte numeric difference
- * some arithmetic bugs only appear with certain bit patterns */
- for (z = 0; z < 256; z++) {
- for (i = 0; i < 256; i++) {
- ii = (uint8_t)i;
- zz = (uint8_t)z;
- tt_int_op(tor_memeq(&zz, &ii, 1),OP_EQ, zz == ii);
- tt_int_op(tor_memcmp(&zz, &ii, 1) > 0 ? GT : EQ,OP_EQ,
- zz > ii ? GT : EQ);
- tt_int_op(tor_memcmp(&ii, &zz, 1) < 0 ? LT : EQ,OP_EQ,
- ii < zz ? LT : EQ);
- }
- }
- }
- tt_int_op(1, OP_EQ, safe_mem_is_zero("", 0));
- tt_int_op(1, OP_EQ, safe_mem_is_zero("", 1));
- tt_int_op(0, OP_EQ, safe_mem_is_zero("a", 1));
- tt_int_op(0, OP_EQ, safe_mem_is_zero("a", 2));
- tt_int_op(0, OP_EQ, safe_mem_is_zero("\0a", 2));
- tt_int_op(1, OP_EQ, safe_mem_is_zero("\0\0a", 2));
- tt_int_op(1, OP_EQ, safe_mem_is_zero("\0\0\0\0\0\0\0\0", 8));
- tt_int_op(1, OP_EQ, safe_mem_is_zero("\0\0\0\0\0\0\0\0a", 8));
- tt_int_op(0, OP_EQ, safe_mem_is_zero("\0\0\0\0\0\0\0\0a", 9));
- done:
- ;
- }
- /**
- * Test counting high bits
- */
- static void
- test_util_n_bits_set(void *ptr)
- {
- (void)ptr;
- tt_int_op(0,OP_EQ, n_bits_set_u8(0));
- tt_int_op(1,OP_EQ, n_bits_set_u8(1));
- tt_int_op(3,OP_EQ, n_bits_set_u8(7));
- tt_int_op(1,OP_EQ, n_bits_set_u8(8));
- tt_int_op(2,OP_EQ, n_bits_set_u8(129));
- tt_int_op(8,OP_EQ, n_bits_set_u8(255));
- done:
- ;
- }
- /**
- * Test LHS whitespace (and comment) eater
- */
- static void
- test_util_eat_whitespace(void *ptr)
- {
- const char ws[] = { ' ', '\t', '\r' }; /* Except NL */
- char str[80];
- size_t i;
- (void)ptr;
- /* Try one leading ws */
- strlcpy(str, "fuubaar", sizeof(str));
- for (i = 0; i < sizeof(ws); ++i) {
- str[0] = ws[i];
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
- }
- str[0] = '\n';
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
- /* Empty string */
- strlcpy(str, "", sizeof(str));
- tt_ptr_op(str,OP_EQ, eat_whitespace(str));
- tt_ptr_op(str,OP_EQ, eat_whitespace_eos(str, str));
- tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str));
- /* Only ws */
- strlcpy(str, " \t\r\n", sizeof(str));
- tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + strlen(str),OP_EQ,
- eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str + strlen(str) - 1,OP_EQ,
- eat_whitespace_no_nl(str));
- tt_ptr_op(str + strlen(str) - 1,OP_EQ,
- eat_whitespace_eos_no_nl(str, str + strlen(str)));
- strlcpy(str, " \t\r ", sizeof(str));
- tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + strlen(str),OP_EQ,
- eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str + strlen(str),OP_EQ,
- eat_whitespace_eos_no_nl(str, str + strlen(str)));
- /* Multiple ws */
- strlcpy(str, "fuubaar", sizeof(str));
- for (i = 0; i < sizeof(ws); ++i)
- str[i] = ws[i];
- tt_ptr_op(str + sizeof(ws),OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + sizeof(ws),OP_EQ,
- eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str + sizeof(ws),OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str + sizeof(ws),OP_EQ,
- eat_whitespace_eos_no_nl(str, str + strlen(str)));
- /* Eat comment */
- strlcpy(str, "# Comment \n No Comment", sizeof(str));
- tt_str_op("No Comment",OP_EQ, eat_whitespace(str));
- tt_str_op("No Comment",OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
- /* Eat comment & ws mix */
- strlcpy(str, " # \t Comment \n\t\nNo Comment", sizeof(str));
- tt_str_op("No Comment",OP_EQ, eat_whitespace(str));
- tt_str_op("No Comment",OP_EQ, eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str + 1,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
- /* Eat entire comment */
- strlcpy(str, "#Comment", sizeof(str));
- tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + strlen(str),OP_EQ,
- eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
- /* Blank line, then comment */
- strlcpy(str, " \t\n # Comment", sizeof(str));
- tt_ptr_op(str + strlen(str),OP_EQ, eat_whitespace(str));
- tt_ptr_op(str + strlen(str),OP_EQ,
- eat_whitespace_eos(str, str + strlen(str)));
- tt_ptr_op(str + 2,OP_EQ, eat_whitespace_no_nl(str));
- tt_ptr_op(str + 2,OP_EQ, eat_whitespace_eos_no_nl(str, str + strlen(str)));
- done:
- ;
- }
- /** Return a newly allocated smartlist containing the lines of text in
- * <b>lines</b>. The returned strings are heap-allocated, and must be
- * freed by the caller.
- *
- * XXXX? Move to container.[hc] ? */
- static smartlist_t *
- smartlist_new_from_text_lines(const char *lines)
- {
- smartlist_t *sl = smartlist_new();
- char *last_line;
- smartlist_split_string(sl, lines, "\n", 0, 0);
- last_line = smartlist_pop_last(sl);
- if (last_line != NULL && *last_line != '\0') {
- smartlist_add(sl, last_line);
- } else {
- tor_free(last_line);
- }
- return sl;
- }
- /** Test smartlist_new_from_text_lines */
- static void
- test_util_sl_new_from_text_lines(void *ptr)
- {
- (void)ptr;
- { /* Normal usage */
- smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz\n");
- int sl_len = smartlist_len(sl);
- tt_want_int_op(sl_len, OP_EQ, 3);
- if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), OP_EQ, "foo");
- if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), OP_EQ, "bar");
- if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), OP_EQ, "baz");
- SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
- smartlist_free(sl);
- }
- { /* No final newline */
- smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz");
- int sl_len = smartlist_len(sl);
- tt_want_int_op(sl_len, OP_EQ, 3);
- if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), OP_EQ, "foo");
- if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), OP_EQ, "bar");
- if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), OP_EQ, "baz");
- SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
- smartlist_free(sl);
- }
- { /* No newlines */
- smartlist_t *sl = smartlist_new_from_text_lines("foo");
- int sl_len = smartlist_len(sl);
- tt_want_int_op(sl_len, OP_EQ, 1);
- if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), OP_EQ, "foo");
- SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
- smartlist_free(sl);
- }
- { /* No text at all */
- smartlist_t *sl = smartlist_new_from_text_lines("");
- int sl_len = smartlist_len(sl);
- tt_want_int_op(sl_len, OP_EQ, 0);
- SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
- smartlist_free(sl);
- }
- }
- static void
- test_util_envnames(void *ptr)
- {
- (void) ptr;
- tt_assert(environment_variable_names_equal("abc", "abc"));
- tt_assert(environment_variable_names_equal("abc", "abc="));
- tt_assert(environment_variable_names_equal("abc", "abc=def"));
- tt_assert(environment_variable_names_equal("abc=def", "abc"));
- tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
- tt_assert(environment_variable_names_equal("abc", "abc"));
- tt_assert(environment_variable_names_equal("abc", "abc="));
- tt_assert(environment_variable_names_equal("abc", "abc=def"));
- tt_assert(environment_variable_names_equal("abc=def", "abc"));
- tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
- tt_assert(!environment_variable_names_equal("abc", "abcd"));
- tt_assert(!environment_variable_names_equal("abc=", "abcd"));
- tt_assert(!environment_variable_names_equal("abc=", "abcd"));
- tt_assert(!environment_variable_names_equal("abc=", "def"));
- tt_assert(!environment_variable_names_equal("abc=", "def="));
- tt_assert(!environment_variable_names_equal("abc=x", "def=x"));
- tt_assert(!environment_variable_names_equal("", "a=def"));
- /* A bit surprising. */
- tt_assert(environment_variable_names_equal("", "=def"));
- tt_assert(environment_variable_names_equal("=y", "=x"));
- done:
- ;
- }
- /** Test process_environment_make */
- static void
- test_util_make_environment(void *ptr)
- {
- const char *env_vars_string =
- "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
- "HOME=/home/foozer\n";
- const char expected_windows_env_block[] =
- "HOME=/home/foozer\000"
- "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\000"
- "\000";
- size_t expected_windows_env_block_len =
- sizeof(expected_windows_env_block) - 1;
- smartlist_t *env_vars = smartlist_new_from_text_lines(env_vars_string);
- smartlist_t *env_vars_sorted = smartlist_new();
- smartlist_t *env_vars_in_unixoid_env_block_sorted = smartlist_new();
- process_environment_t *env;
- (void)ptr;
- env = process_environment_make(env_vars);
- /* Check that the Windows environment block is correct. */
- tt_want(tor_memeq(expected_windows_env_block, env->windows_environment_block,
- expected_windows_env_block_len));
- /* Now for the Unixoid environment block. We don't care which order
- * these environment variables are in, so we sort both lists first. */
- smartlist_add_all(env_vars_sorted, env_vars);
- {
- char **v;
- for (v = env->unixoid_environment_block; *v; ++v) {
- smartlist_add(env_vars_in_unixoid_env_block_sorted, *v);
- }
- }
- smartlist_sort_strings(env_vars_sorted);
- smartlist_sort_strings(env_vars_in_unixoid_env_block_sorted);
- tt_want_int_op(smartlist_len(env_vars_sorted), OP_EQ,
- smartlist_len(env_vars_in_unixoid_env_block_sorted));
- {
- int len = smartlist_len(env_vars_sorted);
- int i;
- if (smartlist_len(env_vars_in_unixoid_env_block_sorted) < len) {
- len = smartlist_len(env_vars_in_unixoid_env_block_sorted);
- }
- for (i = 0; i < len; ++i) {
- tt_want_str_op(smartlist_get(env_vars_sorted, i), OP_EQ,
- smartlist_get(env_vars_in_unixoid_env_block_sorted, i));
- }
- }
- /* Clean up. */
- smartlist_free(env_vars_in_unixoid_env_block_sorted);
- smartlist_free(env_vars_sorted);
- SMARTLIST_FOREACH(env_vars, char *, x, tor_free(x));
- smartlist_free(env_vars);
- process_environment_free(env);
- }
- /** Test set_environment_variable_in_smartlist */
- static void
- test_util_set_env_var_in_sl(void *ptr)
- {
- /* The environment variables in these strings are in arbitrary
- * order; we sort the resulting lists before comparing them.
- *
- * (They *will not* end up in the order shown in
- * expected_resulting_env_vars_string.) */
- const char *base_env_vars_string =
- "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
- "HOME=/home/foozer\n"
- "TERM=xterm\n"
- "SHELL=/bin/ksh\n"
- "USER=foozer\n"
- "LOGNAME=foozer\n"
- "USERNAME=foozer\n"
- "LANG=en_US.utf8\n"
- ;
- const char *new_env_vars_string =
- "TERM=putty\n"
- "DISPLAY=:18.0\n"
- ;
- const char *expected_resulting_env_vars_string =
- "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
- "HOME=/home/foozer\n"
- "TERM=putty\n"
- "SHELL=/bin/ksh\n"
- "USER=foozer\n"
- "LOGNAME=foozer\n"
- "USERNAME=foozer\n"
- "LANG=en_US.utf8\n"
- "DISPLAY=:18.0\n"
- ;
- smartlist_t *merged_env_vars =
- smartlist_new_from_text_lines(base_env_vars_string);
- smartlist_t *new_env_vars =
- smartlist_new_from_text_lines(new_env_vars_string);
- smartlist_t *expected_resulting_env_vars =
- smartlist_new_from_text_lines(expected_resulting_env_vars_string);
- /* Elements of merged_env_vars are heap-allocated, and must be
- * freed. Some of them are (or should) be freed by
- * set_environment_variable_in_smartlist.
- *
- * Elements of new_env_vars are heap-allocated, but are copied into
- * merged_env_vars, so they are not freed separately at the end of
- * the function.
- *
- * Elements of expected_resulting_env_vars are heap-allocated, and
- * must be freed. */
- (void)ptr;
- SMARTLIST_FOREACH(new_env_vars, char *, env_var,
- set_environment_variable_in_smartlist(merged_env_vars,
- env_var,
- tor_free_,
- 1));
- smartlist_sort_strings(merged_env_vars);
- smartlist_sort_strings(expected_resulting_env_vars);
- tt_want_int_op(smartlist_len(merged_env_vars), OP_EQ,
- smartlist_len(expected_resulting_env_vars));
- {
- int len = smartlist_len(merged_env_vars);
- int i;
- if (smartlist_len(expected_resulting_env_vars) < len) {
- len = smartlist_len(expected_resulting_env_vars);
- }
- for (i = 0; i < len; ++i) {
- tt_want_str_op(smartlist_get(merged_env_vars, i), OP_EQ,
- smartlist_get(expected_resulting_env_vars, i));
- }
- }
- /* Clean up. */
- SMARTLIST_FOREACH(merged_env_vars, char *, x, tor_free(x));
- smartlist_free(merged_env_vars);
- smartlist_free(new_env_vars);
- SMARTLIST_FOREACH(expected_resulting_env_vars, char *, x, tor_free(x));
- smartlist_free(expected_resulting_env_vars);
- }
- static void
- test_util_weak_random(void *arg)
- {
- int i, j, n[16];
- tor_weak_rng_t rng;
- (void) arg;
- tor_init_weak_random(&rng, (unsigned)time(NULL));
- for (i = 1; i <= 256; ++i) {
- for (j=0;j<100;++j) {
- int r = tor_weak_random_range(&rng, i);
- tt_int_op(0, OP_LE, r);
- tt_int_op(r, OP_LT, i);
- }
- }
- memset(n,0,sizeof(n));
- for (j=0;j<8192;++j) {
- n[tor_weak_random_range(&rng, 16)]++;
- }
- for (i=0;i<16;++i)
- tt_int_op(n[i], OP_GT, 0);
- done:
- ;
- }
- static void
- test_util_mathlog(void *arg)
- {
- double d;
- (void) arg;
- d = tor_mathlog(2.718281828);
- tt_double_op(fabs(d - 1.0), OP_LT, .000001);
- d = tor_mathlog(10);
- tt_double_op(fabs(d - 2.30258509), OP_LT, .000001);
- done:
- ;
- }
- static void
- test_util_round_to_next_multiple_of(void *arg)
- {
- (void)arg;
- tt_assert(round_uint64_to_next_multiple_of(0,1) == 0);
- tt_assert(round_uint64_to_next_multiple_of(0,7) == 0);
- tt_assert(round_uint64_to_next_multiple_of(99,1) == 99);
- tt_assert(round_uint64_to_next_multiple_of(99,7) == 105);
- tt_assert(round_uint64_to_next_multiple_of(99,9) == 99);
- tt_assert(round_int64_to_next_multiple_of(0,1) == 0);
- tt_assert(round_int64_to_next_multiple_of(0,7) == 0);
- tt_assert(round_int64_to_next_multiple_of(99,1) == 99);
- tt_assert(round_int64_to_next_multiple_of(99,7) == 105);
- tt_assert(round_int64_to_next_multiple_of(99,9) == 99);
- tt_assert(round_int64_to_next_multiple_of(-99,1) == -99);
- tt_assert(round_int64_to_next_multiple_of(-99,7) == -98);
- tt_assert(round_int64_to_next_multiple_of(-99,9) == -99);
- tt_assert(round_int64_to_next_multiple_of(INT64_MIN,2) == INT64_MIN);
- tt_assert(round_int64_to_next_multiple_of(INT64_MAX,2) ==
- INT64_MAX-INT64_MAX%2);
- done:
- ;
- }
- static void
- test_util_laplace(void *arg)
- {
- /* Sample values produced using Python's SciPy:
- *
- * >>> from scipy.stats import laplace
- * >>> laplace.ppf([-0.01, 0.0, 0.01, 0.5, 0.51, 0.99, 1.0, 1.01],
- ... loc = 24, scale = 24)
- * array([ nan, -inf, -69.88855213, 24. ,
- * 24.48486498, 117.88855213, inf, nan])
- */
- const double mu = 24.0, b = 24.0;
- const double delta_f = 15.0, epsilon = 0.3; /* b = 15.0 / 0.3 = 50.0 */
- (void)arg;
- tt_assert(isinf(sample_laplace_distribution(mu, b, 0.0)));
- test_feq(-69.88855213, sample_laplace_distribution(mu, b, 0.01));
- test_feq(24.0, sample_laplace_distribution(mu, b, 0.5));
- test_feq(24.48486498, sample_laplace_distribution(mu, b, 0.51));
- test_feq(117.88855213, sample_laplace_distribution(mu, b, 0.99));
- /* >>> laplace.ppf([0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99],
- * ... loc = 0, scale = 50)
- * array([ -inf, -80.47189562, -34.65735903, 0. ,
- * 34.65735903, 80.47189562, 195.60115027])
- */
- tt_assert(INT64_MIN + 20 ==
- add_laplace_noise(20, 0.0, delta_f, epsilon));
- tt_assert(-60 == add_laplace_noise(20, 0.1, delta_f, epsilon));
- tt_assert(-14 == add_laplace_noise(20, 0.25, delta_f, epsilon));
- tt_assert(20 == add_laplace_noise(20, 0.5, delta_f, epsilon));
- tt_assert(54 == add_laplace_noise(20, 0.75, delta_f, epsilon));
- tt_assert(100 == add_laplace_noise(20, 0.9, delta_f, epsilon));
- tt_assert(215 == add_laplace_noise(20, 0.99, delta_f, epsilon));
- done:
- ;
- }
- static void
- test_util_strclear(void *arg)
- {
- static const char *vals[] = { "", "a", "abcdef", "abcdefgh", NULL };
- int i;
- char *v = NULL;
- (void)arg;
- for (i = 0; vals[i]; ++i) {
- size_t n;
- v = tor_strdup(vals[i]);
- n = strlen(v);
- tor_strclear(v);
- tt_assert(tor_mem_is_zero(v, n+1));
- tor_free(v);
- }
- done:
- tor_free(v);
- }
- #define UTIL_LEGACY(name) \
- { #name, test_util_ ## name , 0, NULL, NULL }
- #define UTIL_TEST(name, flags) \
- { #name, test_util_ ## name, flags, NULL, NULL }
- #ifdef FD_CLOEXEC
- #define CAN_CHECK_CLOEXEC
- static int
- fd_is_cloexec(tor_socket_t fd)
- {
- int flags = fcntl(fd, F_GETFD, 0);
- return (flags & FD_CLOEXEC) == FD_CLOEXEC;
- }
- #endif
- #ifndef _WIN32
- #define CAN_CHECK_NONBLOCK
- static int
- fd_is_nonblocking(tor_socket_t fd)
- {
- int flags = fcntl(fd, F_GETFL, 0);
- return (flags & O_NONBLOCK) == O_NONBLOCK;
- }
- #endif
- static void
- test_util_socket(void *arg)
- {
- tor_socket_t fd1 = TOR_INVALID_SOCKET;
- tor_socket_t fd2 = TOR_INVALID_SOCKET;
- tor_socket_t fd3 = TOR_INVALID_SOCKET;
- tor_socket_t fd4 = TOR_INVALID_SOCKET;
- int n = get_n_open_sockets();
- TT_BLATHER(("Starting with %d open sockets.", n));
- (void)arg;
- fd1 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 0, 0);
- fd2 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 0, 1);
- tt_assert(SOCKET_OK(fd1));
- tt_assert(SOCKET_OK(fd2));
- tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
- //fd3 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 1, 0);
- //fd4 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 1, 1);
- fd3 = tor_open_socket(AF_INET, SOCK_STREAM, 0);
- fd4 = tor_open_socket_nonblocking(AF_INET, SOCK_STREAM, 0);
- tt_assert(SOCKET_OK(fd3));
- tt_assert(SOCKET_OK(fd4));
- tt_int_op(get_n_open_sockets(), OP_EQ, n + 4);
- #ifdef CAN_CHECK_CLOEXEC
- tt_int_op(fd_is_cloexec(fd1), OP_EQ, 0);
- tt_int_op(fd_is_cloexec(fd2), OP_EQ, 0);
- tt_int_op(fd_is_cloexec(fd3), OP_EQ, 1);
- tt_int_op(fd_is_cloexec(fd4), OP_EQ, 1);
- #endif
- #ifdef CAN_CHECK_NONBLOCK
- tt_int_op(fd_is_nonblocking(fd1), OP_EQ, 0);
- tt_int_op(fd_is_nonblocking(fd2), OP_EQ, 1);
- tt_int_op(fd_is_nonblocking(fd3), OP_EQ, 0);
- tt_int_op(fd_is_nonblocking(fd4), OP_EQ, 1);
- #endif
- tor_close_socket(fd1);
- tor_close_socket(fd2);
- fd1 = fd2 = TOR_INVALID_SOCKET;
- tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
- tor_close_socket(fd3);
- tor_close_socket(fd4);
- fd3 = fd4 = TOR_INVALID_SOCKET;
- tt_int_op(get_n_open_sockets(), OP_EQ, n);
- done:
- if (SOCKET_OK(fd1))
- tor_close_socket(fd1);
- if (SOCKET_OK(fd2))
- tor_close_socket(fd2);
- if (SOCKET_OK(fd3))
- tor_close_socket(fd3);
- if (SOCKET_OK(fd4))
- tor_close_socket(fd4);
- }
- static void *
- socketpair_test_setup(const struct testcase_t *testcase)
- {
- return testcase->setup_data;
- }
- static int
- socketpair_test_cleanup(const struct testcase_t *testcase, void *ptr)
- {
- (void)testcase;
- (void)ptr;
- return 1;
- }
- static const struct testcase_setup_t socketpair_setup = {
- socketpair_test_setup, socketpair_test_cleanup
- };
- /* Test for socketpair and ersatz_socketpair(). We test them both, since
- * the latter is a tolerably good way to exersize tor_accept_socket(). */
- static void
- test_util_socketpair(void *arg)
- {
- const int ersatz = !strcmp(arg, "1");
- int (*const tor_socketpair_fn)(int, int, int, tor_socket_t[2]) =
- ersatz ? tor_ersatz_socketpair : tor_socketpair;
- int n = get_n_open_sockets();
- tor_socket_t fds[2] = {TOR_INVALID_SOCKET, TOR_INVALID_SOCKET};
- const int family = AF_UNIX;
- tt_int_op(0, OP_EQ, tor_socketpair_fn(family, SOCK_STREAM, 0, fds));
- tt_assert(SOCKET_OK(fds[0]));
- tt_assert(SOCKET_OK(fds[1]));
- tt_int_op(get_n_open_sockets(), OP_EQ, n + 2);
- #ifdef CAN_CHECK_CLOEXEC
- tt_int_op(fd_is_cloexec(fds[0]), OP_EQ, 1);
- tt_int_op(fd_is_cloexec(fds[1]), OP_EQ, 1);
- #endif
- #ifdef CAN_CHECK_NONBLOCK
- tt_int_op(fd_is_nonblocking(fds[0]), OP_EQ, 0);
- tt_int_op(fd_is_nonblocking(fds[1]), OP_EQ, 0);
- #endif
- done:
- if (SOCKET_OK(fds[0]))
- tor_close_socket(fds[0]);
- if (SOCKET_OK(fds[1]))
- tor_close_socket(fds[1]);
- }
- static void
- test_util_max_mem(void *arg)
- {
- size_t memory1, memory2;
- int r, r2;
- (void) arg;
- r = get_total_system_memory(&memory1);
- r2 = get_total_system_memory(&memory2);
- tt_int_op(r, OP_EQ, r2);
- tt_uint_op(memory2, OP_EQ, memory1);
- TT_BLATHER(("System memory: "U64_FORMAT, U64_PRINTF_ARG(memory1)));
- if (r==0) {
- /* You have at least a megabyte. */
- tt_uint_op(memory1, OP_GT, (1<<20));
- } else {
- /* You do not have a petabyte. */
- #if SIZEOF_SIZE_T == SIZEOF_UINT64_T
- tt_u64_op(memory1, OP_LT, (U64_LITERAL(1)<<50));
- #endif
- }
- done:
- ;
- }
- static void
- test_util_hostname_validation(void *arg)
- {
- (void)arg;
- // Lets try valid hostnames first.
- tt_assert(string_is_valid_hostname("torproject.org"));
- tt_assert(string_is_valid_hostname("ocw.mit.edu"));
- tt_assert(string_is_valid_hostname("i.4cdn.org"));
- tt_assert(string_is_valid_hostname("stanford.edu"));
- tt_assert(string_is_valid_hostname("multiple-words-with-hypens.jp"));
- // Subdomain name cannot start with '-'.
- tt_assert(!string_is_valid_hostname("-torproject.org"));
- tt_assert(!string_is_valid_hostname("subdomain.-domain.org"));
- tt_assert(!string_is_valid_hostname("-subdomain.domain.org"));
- // Hostnames cannot contain non-alphanumeric characters.
- tt_assert(!string_is_valid_hostname("%%domain.\\org."));
- tt_assert(!string_is_valid_hostname("***x.net"));
- tt_assert(!string_is_valid_hostname("___abc.org"));
- tt_assert(!string_is_valid_hostname("\xff\xffxyz.org"));
- tt_assert(!string_is_valid_hostname("word1 word2.net"));
- // XXX: do we allow single-label DNS names?
- done:
- return;
- }
- static void
- test_util_ipv4_validation(void *arg)
- {
- (void)arg;
- tt_assert(string_is_valid_ipv4_address("192.168.0.1"));
- tt_assert(string_is_valid_ipv4_address("8.8.8.8"));
- tt_assert(!string_is_valid_ipv4_address("abcd"));
- tt_assert(!string_is_valid_ipv4_address("300.300.300.300"));
- tt_assert(!string_is_valid_ipv4_address("8.8."));
- done:
- return;
- }
- static void
- test_util_writepid(void *arg)
- {
- (void) arg;
- char *contents = NULL;
- const char *fname = get_fname("tmp_pid");
- unsigned long pid;
- char c;
- write_pidfile(fname);
- contents = read_file_to_str(fname, 0, NULL);
- tt_assert(contents);
- int n = sscanf(contents, "%lu\n%c", &pid, &c);
- tt_int_op(n, OP_EQ, 1);
- tt_uint_op(pid, OP_EQ,
- #ifdef _WIN32
- _getpid()
- #else
- getpid()
- #endif
- );
- done:
- tor_free(contents);
- }
- struct testcase_t util_tests[] = {
- UTIL_LEGACY(time),
- UTIL_TEST(parse_http_time, 0),
- UTIL_LEGACY(config_line),
- UTIL_LEGACY(config_line_quotes),
- UTIL_LEGACY(config_line_comment_character),
- UTIL_LEGACY(config_line_escaped_content),
- #ifndef _WIN32
- UTIL_LEGACY(expand_filename),
- #endif
- UTIL_LEGACY(escape_string_socks),
- UTIL_LEGACY(string_is_key_value),
- UTIL_LEGACY(strmisc),
- UTIL_LEGACY(pow2),
- UTIL_LEGACY(gzip),
- UTIL_LEGACY(datadir),
- #ifdef ENABLE_MEMPOOLS
- UTIL_LEGACY(mempool),
- #endif
- UTIL_LEGACY(memarea),
- UTIL_LEGACY(control_formats),
- UTIL_LEGACY(mmap),
- UTIL_LEGACY(threads),
- UTIL_LEGACY(sscanf),
- UTIL_LEGACY(format_time_interval),
- UTIL_LEGACY(path_is_relative),
- UTIL_LEGACY(strtok),
- UTIL_LEGACY(di_ops),
- UTIL_TEST(round_to_next_multiple_of, 0),
- UTIL_TEST(laplace, 0),
- UTIL_TEST(strclear, 0),
- UTIL_TEST(find_str_at_start_of_line, 0),
- UTIL_TEST(string_is_C_identifier, 0),
- UTIL_TEST(asprintf, 0),
- UTIL_TEST(listdir, 0),
- UTIL_TEST(parent_dir, 0),
- UTIL_TEST(ftruncate, 0),
- #ifdef _WIN32
- UTIL_TEST(load_win_lib, 0),
- #endif
- #ifndef _WIN32
- UTIL_TEST(exit_status, 0),
- UTIL_TEST(fgets_eagain, 0),
- #endif
- UTIL_TEST(spawn_background_ok, 0),
- UTIL_TEST(spawn_background_fail, 0),
- UTIL_TEST(spawn_background_partial_read, 0),
- UTIL_TEST(spawn_background_exit_early, 0),
- UTIL_TEST(spawn_background_waitpid_notify, 0),
- UTIL_TEST(format_hex_number, 0),
- UTIL_TEST(format_dec_number, 0),
- UTIL_TEST(join_win_cmdline, 0),
- UTIL_TEST(split_lines, 0),
- UTIL_TEST(n_bits_set, 0),
- UTIL_TEST(eat_whitespace, 0),
- UTIL_TEST(sl_new_from_text_lines, 0),
- UTIL_TEST(envnames, 0),
- UTIL_TEST(make_environment, 0),
- UTIL_TEST(set_env_var_in_sl, 0),
- UTIL_TEST(read_file_eof_tiny_limit, 0),
- UTIL_TEST(read_file_eof_one_loop_a, 0),
- UTIL_TEST(read_file_eof_one_loop_b, 0),
- UTIL_TEST(read_file_eof_two_loops, 0),
- UTIL_TEST(read_file_eof_two_loops_b, 0),
- UTIL_TEST(read_file_eof_zero_bytes, 0),
- UTIL_TEST(write_chunks_to_file, 0),
- UTIL_TEST(mathlog, 0),
- UTIL_TEST(weak_random, 0),
- UTIL_TEST(socket, TT_FORK),
- { "socketpair", test_util_socketpair, TT_FORK, &socketpair_setup,
- (void*)"0" },
- { "socketpair_ersatz", test_util_socketpair, TT_FORK,
- &socketpair_setup, (void*)"1" },
- UTIL_TEST(max_mem, 0),
- UTIL_TEST(hostname_validation, 0),
- UTIL_TEST(ipv4_validation, 0),
- UTIL_TEST(writepid, 0),
- END_OF_TESTCASES
- };
|