test_util.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define COMPAT_PRIVATE
  7. #define CONTROL_PRIVATE
  8. #define MEMPOOL_PRIVATE
  9. #define UTIL_PRIVATE
  10. #include "or.h"
  11. #include "config.h"
  12. #include "control.h"
  13. #include "test.h"
  14. #ifdef ENABLE_MEMPOOLS
  15. #include "mempool.h"
  16. #endif /* ENABLE_MEMPOOLS */
  17. #include "memarea.h"
  18. #include "util_process.h"
  19. #ifdef _WIN32
  20. #include <tchar.h>
  21. #endif
  22. #include <math.h>
  23. #include <ctype.h>
  24. /* XXXX this is a minimal wrapper to make the unit tests compile with the
  25. * changed tor_timegm interface. */
  26. static time_t
  27. tor_timegm_wrapper(const struct tm *tm)
  28. {
  29. time_t t;
  30. if (tor_timegm(tm, &t) < 0)
  31. return -1;
  32. return t;
  33. }
  34. #define tor_timegm tor_timegm_wrapper
  35. static void
  36. test_util_read_until_eof_impl(const char *fname, size_t file_len,
  37. size_t read_limit)
  38. {
  39. char *fifo_name = NULL;
  40. char *test_str = NULL;
  41. char *str = NULL;
  42. size_t sz = 9999999;
  43. int fd = -1;
  44. int r;
  45. fifo_name = tor_strdup(get_fname(fname));
  46. test_str = tor_malloc(file_len);
  47. crypto_rand(test_str, file_len);
  48. r = write_bytes_to_file(fifo_name, test_str, file_len, 1);
  49. tt_int_op(r, ==, 0);
  50. fd = open(fifo_name, O_RDONLY|O_BINARY);
  51. tt_int_op(fd, >=, 0);
  52. str = read_file_to_str_until_eof(fd, read_limit, &sz);
  53. tt_assert(str != NULL);
  54. if (read_limit < file_len)
  55. tt_int_op(sz, ==, read_limit);
  56. else
  57. tt_int_op(sz, ==, file_len);
  58. tt_mem_op(test_str, ==, str, sz);
  59. tt_int_op(str[sz], ==, '\0');
  60. done:
  61. unlink(fifo_name);
  62. tor_free(fifo_name);
  63. tor_free(test_str);
  64. tor_free(str);
  65. if (fd >= 0)
  66. close(fd);
  67. }
  68. static void
  69. test_util_read_file_eof_tiny_limit(void *arg)
  70. {
  71. (void)arg;
  72. // purposely set limit shorter than what we wrote to the FIFO to
  73. // test the maximum, and that it puts the NUL in the right spot
  74. test_util_read_until_eof_impl("tor_test_fifo_tiny", 5, 4);
  75. }
  76. static void
  77. test_util_read_file_eof_one_loop_a(void *arg)
  78. {
  79. (void)arg;
  80. test_util_read_until_eof_impl("tor_test_fifo_1ka", 1024, 1023);
  81. }
  82. static void
  83. test_util_read_file_eof_one_loop_b(void *arg)
  84. {
  85. (void)arg;
  86. test_util_read_until_eof_impl("tor_test_fifo_1kb", 1024, 1024);
  87. }
  88. static void
  89. test_util_read_file_eof_two_loops(void *arg)
  90. {
  91. (void)arg;
  92. // write more than 1024 bytes to the FIFO to test two passes through
  93. // the loop in the method; if the re-alloc size is changed this
  94. // should be updated as well.
  95. test_util_read_until_eof_impl("tor_test_fifo_2k", 2048, 10000);
  96. }
  97. static void
  98. test_util_read_file_eof_two_loops_b(void *arg)
  99. {
  100. (void)arg;
  101. test_util_read_until_eof_impl("tor_test_fifo_2kb", 2048, 2048);
  102. }
  103. static void
  104. test_util_read_file_eof_zero_bytes(void *arg)
  105. {
  106. (void)arg;
  107. // zero-byte fifo
  108. test_util_read_until_eof_impl("tor_test_fifo_empty", 0, 10000);
  109. }
  110. /* Test the basic expected behaviour for write_chunks_to_file.
  111. * NOTE: This will need to be updated if we ever change the tempfile location
  112. * or extension */
  113. static void
  114. test_util_write_chunks_to_file(void *arg)
  115. {
  116. char *fname = NULL;
  117. char *tempname = NULL;
  118. char *str = NULL;
  119. int r;
  120. struct stat st;
  121. /* These should be two different sizes to ensure the data is different
  122. * between the data file and the temp file's 'known string' */
  123. int temp_str_len = 1024;
  124. int data_str_len = 512;
  125. char *data_str = tor_malloc(data_str_len);
  126. char *temp_str = tor_malloc(temp_str_len);
  127. smartlist_t *chunks = smartlist_new();
  128. sized_chunk_t c = {data_str, data_str_len/2};
  129. sized_chunk_t c2 = {data_str + data_str_len/2, data_str_len/2};
  130. (void)arg;
  131. crypto_rand(temp_str, temp_str_len);
  132. crypto_rand(data_str, data_str_len);
  133. // Ensure it can write multiple chunks
  134. smartlist_add(chunks, &c);
  135. smartlist_add(chunks, &c2);
  136. /*
  137. * Check if it writes using a tempfile
  138. */
  139. fname = tor_strdup(get_fname("write_chunks_with_tempfile"));
  140. tor_asprintf(&tempname, "%s.tmp", fname);
  141. // write a known string to a file where the tempfile will be
  142. r = write_bytes_to_file(tempname, temp_str, temp_str_len, 1);
  143. tt_int_op(r, ==, 0);
  144. // call write_chunks_to_file
  145. r = write_chunks_to_file(fname, chunks, 1, 0);
  146. tt_int_op(r, ==, 0);
  147. // assert the file has been written (expected size)
  148. str = read_file_to_str(fname, RFTS_BIN, &st);
  149. tt_assert(str != NULL);
  150. tt_u64_op((uint64_t)st.st_size, ==, data_str_len);
  151. tt_mem_op(data_str, ==, str, data_str_len);
  152. tor_free(str);
  153. // assert that the tempfile is removed (should not leave artifacts)
  154. str = read_file_to_str(tempname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  155. tt_assert(str == NULL);
  156. // Remove old testfile for second test
  157. r = unlink(fname);
  158. tt_int_op(r, ==, 0);
  159. tor_free(fname);
  160. tor_free(tempname);
  161. /*
  162. * Check if it skips using a tempfile with flags
  163. */
  164. fname = tor_strdup(get_fname("write_chunks_with_no_tempfile"));
  165. tor_asprintf(&tempname, "%s.tmp", fname);
  166. // write a known string to a file where the tempfile will be
  167. r = write_bytes_to_file(tempname, temp_str, temp_str_len, 1);
  168. tt_int_op(r, ==, 0);
  169. // call write_chunks_to_file with no_tempfile = true
  170. r = write_chunks_to_file(fname, chunks, 1, 1);
  171. tt_int_op(r, ==, 0);
  172. // assert the file has been written (expected size)
  173. str = read_file_to_str(fname, RFTS_BIN, &st);
  174. tt_assert(str != NULL);
  175. tt_u64_op((uint64_t)st.st_size, ==, data_str_len);
  176. tt_mem_op(data_str, ==, str, data_str_len);
  177. tor_free(str);
  178. // assert the tempfile still contains the known string
  179. str = read_file_to_str(tempname, RFTS_BIN, &st);
  180. tt_assert(str != NULL);
  181. tt_u64_op((uint64_t)st.st_size, ==, temp_str_len);
  182. tt_mem_op(temp_str, ==, str, temp_str_len);
  183. done:
  184. unlink(fname);
  185. unlink(tempname);
  186. smartlist_free(chunks);
  187. tor_free(fname);
  188. tor_free(tempname);
  189. tor_free(str);
  190. tor_free(data_str);
  191. tor_free(temp_str);
  192. }
  193. static void
  194. test_util_time(void *arg)
  195. {
  196. struct timeval start, end;
  197. struct tm a_time;
  198. char timestr[128];
  199. time_t t_res;
  200. int i;
  201. struct timeval tv;
  202. /* Test tv_udiff */
  203. (void)arg;
  204. start.tv_sec = 5;
  205. start.tv_usec = 5000;
  206. end.tv_sec = 5;
  207. end.tv_usec = 5000;
  208. tt_int_op(0L,==, tv_udiff(&start, &end));
  209. end.tv_usec = 7000;
  210. tt_int_op(2000L,==, tv_udiff(&start, &end));
  211. end.tv_sec = 6;
  212. tt_int_op(1002000L,==, tv_udiff(&start, &end));
  213. end.tv_usec = 0;
  214. tt_int_op(995000L,==, tv_udiff(&start, &end));
  215. end.tv_sec = 4;
  216. tt_int_op(-1005000L,==, tv_udiff(&start, &end));
  217. /* Test tor_timegm */
  218. /* The test values here are confirmed to be correct on a platform
  219. * with a working timegm. */
  220. a_time.tm_year = 2003-1900;
  221. a_time.tm_mon = 7;
  222. a_time.tm_mday = 30;
  223. a_time.tm_hour = 6;
  224. a_time.tm_min = 14;
  225. a_time.tm_sec = 55;
  226. tt_int_op((time_t) 1062224095UL,==, tor_timegm(&a_time));
  227. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  228. tt_int_op((time_t) 1093846495UL,==, tor_timegm(&a_time));
  229. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  230. a_time.tm_mday = 10;
  231. tt_int_op((time_t) 1076393695UL,==, tor_timegm(&a_time));
  232. a_time.tm_mon = 0;
  233. a_time.tm_mday = 10;
  234. tt_int_op((time_t) 1073715295UL,==, tor_timegm(&a_time));
  235. a_time.tm_mon = 12; /* Wrong month, it's 0-based */
  236. a_time.tm_mday = 10;
  237. tt_int_op((time_t) -1,==, tor_timegm(&a_time));
  238. a_time.tm_mon = -1; /* Wrong month */
  239. a_time.tm_mday = 10;
  240. tt_int_op((time_t) -1,==, tor_timegm(&a_time));
  241. /* Test {format,parse}_rfc1123_time */
  242. format_rfc1123_time(timestr, 0);
  243. tt_str_op("Thu, 01 Jan 1970 00:00:00 GMT",==, timestr);
  244. format_rfc1123_time(timestr, (time_t)1091580502UL);
  245. tt_str_op("Wed, 04 Aug 2004 00:48:22 GMT",==, timestr);
  246. t_res = 0;
  247. i = parse_rfc1123_time(timestr, &t_res);
  248. tt_int_op(0,==, i);
  249. tt_int_op(t_res,==, (time_t)1091580502UL);
  250. /* The timezone doesn't matter */
  251. t_res = 0;
  252. tt_int_op(0,==, parse_rfc1123_time("Wed, 04 Aug 2004 00:48:22 ZUL", &t_res));
  253. tt_int_op(t_res,==, (time_t)1091580502UL);
  254. tt_int_op(-1,==,
  255. parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  256. tt_int_op(-1,==,
  257. parse_rfc1123_time("Wed, 32 Mar 2011 00:00:00 GMT", &t_res));
  258. tt_int_op(-1,==,
  259. parse_rfc1123_time("Wed, 30 Mar 2011 24:00:00 GMT", &t_res));
  260. tt_int_op(-1,==,
  261. parse_rfc1123_time("Wed, 30 Mar 2011 23:60:00 GMT", &t_res));
  262. tt_int_op(-1,==,
  263. parse_rfc1123_time("Wed, 30 Mar 2011 23:59:62 GMT", &t_res));
  264. tt_int_op(-1,==,
  265. parse_rfc1123_time("Wed, 30 Mar 1969 23:59:59 GMT", &t_res));
  266. tt_int_op(-1,==,
  267. parse_rfc1123_time("Wed, 30 Ene 2011 23:59:59 GMT", &t_res));
  268. tt_int_op(-1,==,
  269. parse_rfc1123_time("Wed, 30 Mar 2011 23:59:59 GM", &t_res));
  270. #if 0
  271. /* This fails, I imagine it's important and should be fixed? */
  272. test_eq(-1, parse_rfc1123_time("Wed, 29 Feb 2011 16:00:00 GMT", &t_res));
  273. /* Why is this string valid (ie. the test fails because it doesn't
  274. return -1)? */
  275. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 2011 23:59:61 GMT", &t_res));
  276. #endif
  277. /* Test parse_iso_time */
  278. t_res = 0;
  279. i = parse_iso_time("", &t_res);
  280. tt_int_op(-1,==, i);
  281. t_res = 0;
  282. i = parse_iso_time("2004-08-32 00:48:22", &t_res);
  283. tt_int_op(-1,==, i);
  284. t_res = 0;
  285. i = parse_iso_time("1969-08-03 00:48:22", &t_res);
  286. tt_int_op(-1,==, i);
  287. t_res = 0;
  288. i = parse_iso_time("2004-08-04 00:48:22", &t_res);
  289. tt_int_op(0,==, i);
  290. tt_int_op(t_res,==, (time_t)1091580502UL);
  291. t_res = 0;
  292. i = parse_iso_time("2004-8-4 0:48:22", &t_res);
  293. tt_int_op(0,==, i);
  294. tt_int_op(t_res,==, (time_t)1091580502UL);
  295. tt_int_op(-1,==, parse_iso_time("2004-08-zz 99-99x99 GMT", &t_res));
  296. tt_int_op(-1,==, parse_iso_time("2011-03-32 00:00:00 GMT", &t_res));
  297. tt_int_op(-1,==, parse_iso_time("2011-03-30 24:00:00 GMT", &t_res));
  298. tt_int_op(-1,==, parse_iso_time("2011-03-30 23:60:00 GMT", &t_res));
  299. tt_int_op(-1,==, parse_iso_time("2011-03-30 23:59:62 GMT", &t_res));
  300. tt_int_op(-1,==, parse_iso_time("1969-03-30 23:59:59 GMT", &t_res));
  301. tt_int_op(-1,==, parse_iso_time("2011-00-30 23:59:59 GMT", &t_res));
  302. tt_int_op(-1,==, parse_iso_time("2147483647-08-29 14:00:00", &t_res));
  303. tt_int_op(-1,==, parse_iso_time("2011-03-30 23:59", &t_res));
  304. /* Test tor_gettimeofday */
  305. end.tv_sec = 4;
  306. end.tv_usec = 999990;
  307. start.tv_sec = 1;
  308. start.tv_usec = 500;
  309. tor_gettimeofday(&start);
  310. /* now make sure time works. */
  311. tor_gettimeofday(&end);
  312. /* We might've timewarped a little. */
  313. tt_int_op(tv_udiff(&start, &end), >=, -5000);
  314. /* Test format_iso_time */
  315. tv.tv_sec = (time_t)1326296338;
  316. tv.tv_usec = 3060;
  317. format_iso_time(timestr, (time_t)tv.tv_sec);
  318. tt_str_op("2012-01-11 15:38:58",==, timestr);
  319. /* The output of format_local_iso_time will vary by timezone, and setting
  320. our timezone for testing purposes would be a nontrivial flaky pain.
  321. Skip this test for now.
  322. format_local_iso_time(timestr, tv.tv_sec);
  323. test_streq("2012-01-11 10:38:58", timestr);
  324. */
  325. format_iso_time_nospace(timestr, (time_t)tv.tv_sec);
  326. tt_str_op("2012-01-11T15:38:58",==, timestr);
  327. tt_int_op(strlen(timestr),==, ISO_TIME_LEN);
  328. format_iso_time_nospace_usec(timestr, &tv);
  329. tt_str_op("2012-01-11T15:38:58.003060",==, timestr);
  330. tt_int_op(strlen(timestr),==, ISO_TIME_USEC_LEN);
  331. done:
  332. ;
  333. }
  334. static void
  335. test_util_parse_http_time(void *arg)
  336. {
  337. struct tm a_time;
  338. char b[ISO_TIME_LEN+1];
  339. (void)arg;
  340. #define T(s) do { \
  341. format_iso_time(b, tor_timegm(&a_time)); \
  342. tt_str_op(b, ==, (s)); \
  343. b[0]='\0'; \
  344. } while (0)
  345. /* Test parse_http_time */
  346. tt_int_op(-1,==,
  347. parse_http_time("", &a_time));
  348. tt_int_op(-1,==,
  349. parse_http_time("Sunday, 32 Aug 2004 00:48:22 GMT", &a_time));
  350. tt_int_op(-1,==,
  351. parse_http_time("Sunday, 3 Aug 1869 00:48:22 GMT", &a_time));
  352. tt_int_op(-1,==,
  353. parse_http_time("Sunday, 32-Aug-94 00:48:22 GMT", &a_time));
  354. tt_int_op(-1,==,
  355. parse_http_time("Sunday, 3-Ago-04 00:48:22", &a_time));
  356. tt_int_op(-1,==,
  357. parse_http_time("Sunday, August the third", &a_time));
  358. tt_int_op(-1,==,
  359. parse_http_time("Wednesday,,04 Aug 1994 00:48:22 GMT", &a_time));
  360. tt_int_op(0,==,
  361. parse_http_time("Wednesday, 04 Aug 1994 00:48:22 GMT", &a_time));
  362. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  363. T("1994-08-04 00:48:22");
  364. tt_int_op(0,==,
  365. parse_http_time("Wednesday, 4 Aug 1994 0:48:22 GMT", &a_time));
  366. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  367. T("1994-08-04 00:48:22");
  368. tt_int_op(0,==,
  369. parse_http_time("Miercoles, 4 Aug 1994 0:48:22 GMT", &a_time));
  370. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  371. T("1994-08-04 00:48:22");
  372. tt_int_op(0,==,
  373. parse_http_time("Wednesday, 04-Aug-94 00:48:22 GMT", &a_time));
  374. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  375. T("1994-08-04 00:48:22");
  376. tt_int_op(0,==, parse_http_time("Wednesday, 4-Aug-94 0:48:22 GMT", &a_time));
  377. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  378. T("1994-08-04 00:48:22");
  379. tt_int_op(0,==, parse_http_time("Miercoles, 4-Aug-94 0:48:22 GMT", &a_time));
  380. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  381. T("1994-08-04 00:48:22");
  382. tt_int_op(0,==, parse_http_time("Wed Aug 04 00:48:22 1994", &a_time));
  383. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  384. T("1994-08-04 00:48:22");
  385. tt_int_op(0,==, parse_http_time("Wed Aug 4 0:48:22 1994", &a_time));
  386. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  387. T("1994-08-04 00:48:22");
  388. tt_int_op(0,==, parse_http_time("Mie Aug 4 0:48:22 1994", &a_time));
  389. tt_int_op((time_t)775961302UL,==, tor_timegm(&a_time));
  390. T("1994-08-04 00:48:22");
  391. tt_int_op(0,==, parse_http_time("Sun, 1 Jan 2012 00:00:00 GMT", &a_time));
  392. tt_int_op((time_t)1325376000UL,==, tor_timegm(&a_time));
  393. T("2012-01-01 00:00:00");
  394. tt_int_op(0,==, parse_http_time("Mon, 31 Dec 2012 00:00:00 GMT", &a_time));
  395. tt_int_op((time_t)1356912000UL,==, tor_timegm(&a_time));
  396. T("2012-12-31 00:00:00");
  397. tt_int_op(-1,==, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
  398. tt_int_op(-1,==, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
  399. tt_int_op(-1,==, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
  400. tt_int_op(-1,==, parse_http_time("2011-03-30 23:60:00 GMT", &a_time));
  401. tt_int_op(-1,==, parse_http_time("2011-03-30 23:59:62 GMT", &a_time));
  402. tt_int_op(-1,==, parse_http_time("1969-03-30 23:59:59 GMT", &a_time));
  403. tt_int_op(-1,==, parse_http_time("2011-00-30 23:59:59 GMT", &a_time));
  404. tt_int_op(-1,==, parse_http_time("2011-03-30 23:59", &a_time));
  405. #undef T
  406. done:
  407. ;
  408. }
  409. static void
  410. test_util_config_line(void *arg)
  411. {
  412. char buf[1024];
  413. char *k=NULL, *v=NULL;
  414. const char *str;
  415. /* Test parse_config_line_from_str */
  416. (void)arg;
  417. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  418. "k2\n"
  419. "k3 \n" "\n" " \n" "#comment\n"
  420. "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
  421. "kseven \"a quoted 'string\"\n"
  422. "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
  423. "k9 a line that\\\n spans two lines.\n\n"
  424. "k10 more than\\\n one contin\\\nuation\n"
  425. "k11 \\\ncontinuation at the start\n"
  426. "k12 line with a\\\n#comment\n embedded\n"
  427. "k13\\\ncontinuation at the very start\n"
  428. "k14 a line that has a comment and # ends with a slash \\\n"
  429. "k15 this should be the next new line\n"
  430. "k16 a line that has a comment and # ends without a slash \n"
  431. "k17 this should be the next new line\n"
  432. , sizeof(buf));
  433. str = buf;
  434. str = parse_config_line_from_str(str, &k, &v);
  435. tt_str_op(k,==, "k");
  436. tt_str_op(v,==, "v");
  437. tor_free(k); tor_free(v);
  438. tt_assert(!strcmpstart(str, "key value with"));
  439. str = parse_config_line_from_str(str, &k, &v);
  440. tt_str_op(k,==, "key");
  441. tt_str_op(v,==, "value with spaces");
  442. tor_free(k); tor_free(v);
  443. tt_assert(!strcmpstart(str, "keykey"));
  444. str = parse_config_line_from_str(str, &k, &v);
  445. tt_str_op(k,==, "keykey");
  446. tt_str_op(v,==, "val");
  447. tor_free(k); tor_free(v);
  448. tt_assert(!strcmpstart(str, "k2\n"));
  449. str = parse_config_line_from_str(str, &k, &v);
  450. tt_str_op(k,==, "k2");
  451. tt_str_op(v,==, "");
  452. tor_free(k); tor_free(v);
  453. tt_assert(!strcmpstart(str, "k3 \n"));
  454. str = parse_config_line_from_str(str, &k, &v);
  455. tt_str_op(k,==, "k3");
  456. tt_str_op(v,==, "");
  457. tor_free(k); tor_free(v);
  458. tt_assert(!strcmpstart(str, "#comment"));
  459. str = parse_config_line_from_str(str, &k, &v);
  460. tt_str_op(k,==, "k4");
  461. tt_str_op(v,==, "");
  462. tor_free(k); tor_free(v);
  463. tt_assert(!strcmpstart(str, "k5#abc"));
  464. str = parse_config_line_from_str(str, &k, &v);
  465. tt_str_op(k,==, "k5");
  466. tt_str_op(v,==, "");
  467. tor_free(k); tor_free(v);
  468. tt_assert(!strcmpstart(str, "k6"));
  469. str = parse_config_line_from_str(str, &k, &v);
  470. tt_str_op(k,==, "k6");
  471. tt_str_op(v,==, "val");
  472. tor_free(k); tor_free(v);
  473. tt_assert(!strcmpstart(str, "kseven"));
  474. str = parse_config_line_from_str(str, &k, &v);
  475. tt_str_op(k,==, "kseven");
  476. tt_str_op(v,==, "a quoted \'string");
  477. tor_free(k); tor_free(v);
  478. tt_assert(!strcmpstart(str, "k8 "));
  479. str = parse_config_line_from_str(str, &k, &v);
  480. tt_str_op(k,==, "k8");
  481. tt_str_op(v,==, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
  482. tor_free(k); tor_free(v);
  483. str = parse_config_line_from_str(str, &k, &v);
  484. tt_str_op(k,==, "k9");
  485. tt_str_op(v,==, "a line that spans two lines.");
  486. tor_free(k); tor_free(v);
  487. str = parse_config_line_from_str(str, &k, &v);
  488. tt_str_op(k,==, "k10");
  489. tt_str_op(v,==, "more than one continuation");
  490. tor_free(k); tor_free(v);
  491. str = parse_config_line_from_str(str, &k, &v);
  492. tt_str_op(k,==, "k11");
  493. tt_str_op(v,==, "continuation at the start");
  494. tor_free(k); tor_free(v);
  495. str = parse_config_line_from_str(str, &k, &v);
  496. tt_str_op(k,==, "k12");
  497. tt_str_op(v,==, "line with a embedded");
  498. tor_free(k); tor_free(v);
  499. str = parse_config_line_from_str(str, &k, &v);
  500. tt_str_op(k,==, "k13");
  501. tt_str_op(v,==, "continuation at the very start");
  502. tor_free(k); tor_free(v);
  503. str = parse_config_line_from_str(str, &k, &v);
  504. tt_str_op(k,==, "k14");
  505. tt_str_op(v,==, "a line that has a comment and" );
  506. tor_free(k); tor_free(v);
  507. str = parse_config_line_from_str(str, &k, &v);
  508. tt_str_op(k,==, "k15");
  509. tt_str_op(v,==, "this should be the next new line");
  510. tor_free(k); tor_free(v);
  511. str = parse_config_line_from_str(str, &k, &v);
  512. tt_str_op(k,==, "k16");
  513. tt_str_op(v,==, "a line that has a comment and" );
  514. tor_free(k); tor_free(v);
  515. str = parse_config_line_from_str(str, &k, &v);
  516. tt_str_op(k,==, "k17");
  517. tt_str_op(v,==, "this should be the next new line");
  518. tor_free(k); tor_free(v);
  519. tt_str_op(str,==, "");
  520. done:
  521. tor_free(k);
  522. tor_free(v);
  523. }
  524. static void
  525. test_util_config_line_quotes(void *arg)
  526. {
  527. char buf1[1024];
  528. char buf2[128];
  529. char buf3[128];
  530. char buf4[128];
  531. char *k=NULL, *v=NULL;
  532. const char *str;
  533. /* Test parse_config_line_from_str */
  534. (void)arg;
  535. strlcpy(buf1, "kTrailingSpace \"quoted value\" \n"
  536. "kTrailingGarbage \"quoted value\"trailing garbage\n"
  537. , sizeof(buf1));
  538. strlcpy(buf2, "kTrailingSpaceAndGarbage \"quoted value\" trailing space+g\n"
  539. , sizeof(buf2));
  540. strlcpy(buf3, "kMultilineTrailingSpace \"mline\\ \nvalue w/ trailing sp\"\n"
  541. , sizeof(buf3));
  542. strlcpy(buf4, "kMultilineNoTrailingBackslash \"naked multiline\nvalue\"\n"
  543. , sizeof(buf4));
  544. str = buf1;
  545. str = parse_config_line_from_str(str, &k, &v);
  546. tt_str_op(k,==, "kTrailingSpace");
  547. tt_str_op(v,==, "quoted value");
  548. tor_free(k); tor_free(v);
  549. str = parse_config_line_from_str(str, &k, &v);
  550. tt_ptr_op(str,==, NULL);
  551. tor_free(k); tor_free(v);
  552. str = buf2;
  553. str = parse_config_line_from_str(str, &k, &v);
  554. tt_ptr_op(str,==, NULL);
  555. tor_free(k); tor_free(v);
  556. str = buf3;
  557. str = parse_config_line_from_str(str, &k, &v);
  558. tt_ptr_op(str,==, NULL);
  559. tor_free(k); tor_free(v);
  560. str = buf4;
  561. str = parse_config_line_from_str(str, &k, &v);
  562. tt_ptr_op(str,==, NULL);
  563. tor_free(k); tor_free(v);
  564. done:
  565. tor_free(k);
  566. tor_free(v);
  567. }
  568. static void
  569. test_util_config_line_comment_character(void *arg)
  570. {
  571. char buf[1024];
  572. char *k=NULL, *v=NULL;
  573. const char *str;
  574. /* Test parse_config_line_from_str */
  575. (void)arg;
  576. strlcpy(buf, "k1 \"# in quotes\"\n"
  577. "k2 some value # some comment\n"
  578. "k3 /home/user/myTorNetwork#2\n" /* Testcase for #1323 */
  579. , sizeof(buf));
  580. str = buf;
  581. str = parse_config_line_from_str(str, &k, &v);
  582. tt_str_op(k,==, "k1");
  583. tt_str_op(v,==, "# in quotes");
  584. tor_free(k); tor_free(v);
  585. str = parse_config_line_from_str(str, &k, &v);
  586. tt_str_op(k,==, "k2");
  587. tt_str_op(v,==, "some value");
  588. tor_free(k); tor_free(v);
  589. tt_str_op(str,==, "k3 /home/user/myTorNetwork#2\n");
  590. #if 0
  591. str = parse_config_line_from_str(str, &k, &v);
  592. test_streq(k, "k3");
  593. test_streq(v, "/home/user/myTorNetwork#2");
  594. tor_free(k); tor_free(v);
  595. test_streq(str, "");
  596. #endif
  597. done:
  598. tor_free(k);
  599. tor_free(v);
  600. }
  601. static void
  602. test_util_config_line_escaped_content(void *arg)
  603. {
  604. char buf1[1024];
  605. char buf2[128];
  606. char buf3[128];
  607. char buf4[128];
  608. char buf5[128];
  609. char buf6[128];
  610. char *k=NULL, *v=NULL;
  611. const char *str;
  612. /* Test parse_config_line_from_str */
  613. (void)arg;
  614. strlcpy(buf1, "HexadecimalLower \"\\x2a\"\n"
  615. "HexadecimalUpper \"\\x2A\"\n"
  616. "HexadecimalUpperX \"\\X2A\"\n"
  617. "Octal \"\\52\"\n"
  618. "Newline \"\\n\"\n"
  619. "Tab \"\\t\"\n"
  620. "CarriageReturn \"\\r\"\n"
  621. "DoubleQuote \"\\\"\"\n"
  622. "SimpleQuote \"\\'\"\n"
  623. "Backslash \"\\\\\"\n"
  624. "Mix \"This is a \\\"star\\\":\\t\\'\\x2a\\'\\nAnd second line\"\n"
  625. , sizeof(buf1));
  626. strlcpy(buf2, "BrokenEscapedContent \"\\a\"\n"
  627. , sizeof(buf2));
  628. strlcpy(buf3, "BrokenEscapedContent \"\\x\"\n"
  629. , sizeof(buf3));
  630. strlcpy(buf4, "BrokenOctal \"\\8\"\n"
  631. , sizeof(buf4));
  632. strlcpy(buf5, "BrokenHex \"\\xg4\"\n"
  633. , sizeof(buf5));
  634. strlcpy(buf6, "BrokenEscape \"\\"
  635. , sizeof(buf6));
  636. str = buf1;
  637. str = parse_config_line_from_str(str, &k, &v);
  638. tt_str_op(k,==, "HexadecimalLower");
  639. tt_str_op(v,==, "*");
  640. tor_free(k); tor_free(v);
  641. str = parse_config_line_from_str(str, &k, &v);
  642. tt_str_op(k,==, "HexadecimalUpper");
  643. tt_str_op(v,==, "*");
  644. tor_free(k); tor_free(v);
  645. str = parse_config_line_from_str(str, &k, &v);
  646. tt_str_op(k,==, "HexadecimalUpperX");
  647. tt_str_op(v,==, "*");
  648. tor_free(k); tor_free(v);
  649. str = parse_config_line_from_str(str, &k, &v);
  650. tt_str_op(k,==, "Octal");
  651. tt_str_op(v,==, "*");
  652. tor_free(k); tor_free(v);
  653. str = parse_config_line_from_str(str, &k, &v);
  654. tt_str_op(k,==, "Newline");
  655. tt_str_op(v,==, "\n");
  656. tor_free(k); tor_free(v);
  657. str = parse_config_line_from_str(str, &k, &v);
  658. tt_str_op(k,==, "Tab");
  659. tt_str_op(v,==, "\t");
  660. tor_free(k); tor_free(v);
  661. str = parse_config_line_from_str(str, &k, &v);
  662. tt_str_op(k,==, "CarriageReturn");
  663. tt_str_op(v,==, "\r");
  664. tor_free(k); tor_free(v);
  665. str = parse_config_line_from_str(str, &k, &v);
  666. tt_str_op(k,==, "DoubleQuote");
  667. tt_str_op(v,==, "\"");
  668. tor_free(k); tor_free(v);
  669. str = parse_config_line_from_str(str, &k, &v);
  670. tt_str_op(k,==, "SimpleQuote");
  671. tt_str_op(v,==, "'");
  672. tor_free(k); tor_free(v);
  673. str = parse_config_line_from_str(str, &k, &v);
  674. tt_str_op(k,==, "Backslash");
  675. tt_str_op(v,==, "\\");
  676. tor_free(k); tor_free(v);
  677. str = parse_config_line_from_str(str, &k, &v);
  678. tt_str_op(k,==, "Mix");
  679. tt_str_op(v,==, "This is a \"star\":\t'*'\nAnd second line");
  680. tor_free(k); tor_free(v);
  681. tt_str_op(str,==, "");
  682. str = buf2;
  683. str = parse_config_line_from_str(str, &k, &v);
  684. tt_ptr_op(str,==, NULL);
  685. tor_free(k); tor_free(v);
  686. str = buf3;
  687. str = parse_config_line_from_str(str, &k, &v);
  688. tt_ptr_op(str,==, NULL);
  689. tor_free(k); tor_free(v);
  690. str = buf4;
  691. str = parse_config_line_from_str(str, &k, &v);
  692. tt_ptr_op(str,==, NULL);
  693. tor_free(k); tor_free(v);
  694. #if 0
  695. str = buf5;
  696. str = parse_config_line_from_str(str, &k, &v);
  697. tt_ptr_op(str, ==, NULL);
  698. tor_free(k); tor_free(v);
  699. #endif
  700. str = buf6;
  701. str = parse_config_line_from_str(str, &k, &v);
  702. tt_ptr_op(str,==, NULL);
  703. tor_free(k); tor_free(v);
  704. done:
  705. tor_free(k);
  706. tor_free(v);
  707. }
  708. #ifndef _WIN32
  709. static void
  710. test_util_expand_filename(void *arg)
  711. {
  712. char *str;
  713. (void)arg;
  714. setenv("HOME", "/home/itv", 1); /* For "internal test value" */
  715. str = expand_filename("");
  716. tt_str_op("",==, str);
  717. tor_free(str);
  718. str = expand_filename("/normal/path");
  719. tt_str_op("/normal/path",==, str);
  720. tor_free(str);
  721. str = expand_filename("/normal/trailing/path/");
  722. tt_str_op("/normal/trailing/path/",==, str);
  723. tor_free(str);
  724. str = expand_filename("~");
  725. tt_str_op("/home/itv/",==, str);
  726. tor_free(str);
  727. str = expand_filename("$HOME/nodice");
  728. tt_str_op("$HOME/nodice",==, str);
  729. tor_free(str);
  730. str = expand_filename("~/");
  731. tt_str_op("/home/itv/",==, str);
  732. tor_free(str);
  733. str = expand_filename("~/foobarqux");
  734. tt_str_op("/home/itv/foobarqux",==, str);
  735. tor_free(str);
  736. str = expand_filename("~/../../etc/passwd");
  737. tt_str_op("/home/itv/../../etc/passwd",==, str);
  738. tor_free(str);
  739. str = expand_filename("~/trailing/");
  740. tt_str_op("/home/itv/trailing/",==, str);
  741. tor_free(str);
  742. /* Ideally we'd test ~anotheruser, but that's shady to test (we'd
  743. have to somehow inject/fake the get_user_homedir call) */
  744. /* $HOME ending in a trailing slash */
  745. setenv("HOME", "/home/itv/", 1);
  746. str = expand_filename("~");
  747. tt_str_op("/home/itv/",==, str);
  748. tor_free(str);
  749. str = expand_filename("~/");
  750. tt_str_op("/home/itv/",==, str);
  751. tor_free(str);
  752. str = expand_filename("~/foo");
  753. tt_str_op("/home/itv/foo",==, str);
  754. tor_free(str);
  755. /* Try with empty $HOME */
  756. setenv("HOME", "", 1);
  757. str = expand_filename("~");
  758. tt_str_op("/",==, str);
  759. tor_free(str);
  760. str = expand_filename("~/");
  761. tt_str_op("/",==, str);
  762. tor_free(str);
  763. str = expand_filename("~/foobar");
  764. tt_str_op("/foobar",==, str);
  765. tor_free(str);
  766. /* Try with $HOME unset */
  767. unsetenv("HOME");
  768. str = expand_filename("~");
  769. tt_str_op("/",==, str);
  770. tor_free(str);
  771. str = expand_filename("~/");
  772. tt_str_op("/",==, str);
  773. tor_free(str);
  774. str = expand_filename("~/foobar");
  775. tt_str_op("/foobar",==, str);
  776. tor_free(str);
  777. done:
  778. tor_free(str);
  779. }
  780. #endif
  781. /** Test tor_escape_str_for_pt_args(). */
  782. static void
  783. test_util_escape_string_socks(void *arg)
  784. {
  785. char *escaped_string = NULL;
  786. /** Simple backslash escape. */
  787. (void)arg;
  788. escaped_string = tor_escape_str_for_pt_args("This is a backslash: \\",";\\");
  789. tt_assert(escaped_string);
  790. tt_str_op(escaped_string,==, "This is a backslash: \\\\");
  791. tor_free(escaped_string);
  792. /** Simple semicolon escape. */
  793. escaped_string = tor_escape_str_for_pt_args("First rule:Do not use ;",";\\");
  794. tt_assert(escaped_string);
  795. tt_str_op(escaped_string,==, "First rule:Do not use \\;");
  796. tor_free(escaped_string);
  797. /** Empty string. */
  798. escaped_string = tor_escape_str_for_pt_args("", ";\\");
  799. tt_assert(escaped_string);
  800. tt_str_op(escaped_string,==, "");
  801. tor_free(escaped_string);
  802. /** Escape all characters. */
  803. escaped_string = tor_escape_str_for_pt_args(";\\;\\", ";\\");
  804. tt_assert(escaped_string);
  805. tt_str_op(escaped_string,==, "\\;\\\\\\;\\\\");
  806. tor_free(escaped_string);
  807. escaped_string = tor_escape_str_for_pt_args(";", ";\\");
  808. tt_assert(escaped_string);
  809. tt_str_op(escaped_string,==, "\\;");
  810. tor_free(escaped_string);
  811. done:
  812. tor_free(escaped_string);
  813. }
  814. static void
  815. test_util_string_is_key_value(void *ptr)
  816. {
  817. (void)ptr;
  818. tt_assert(string_is_key_value(LOG_WARN, "key=value"));
  819. tt_assert(string_is_key_value(LOG_WARN, "k=v"));
  820. tt_assert(string_is_key_value(LOG_WARN, "key="));
  821. tt_assert(string_is_key_value(LOG_WARN, "x="));
  822. tt_assert(string_is_key_value(LOG_WARN, "xx="));
  823. tt_assert(!string_is_key_value(LOG_WARN, "=value"));
  824. tt_assert(!string_is_key_value(LOG_WARN, "=x"));
  825. tt_assert(!string_is_key_value(LOG_WARN, "="));
  826. /* ??? */
  827. /* tt_assert(!string_is_key_value(LOG_WARN, "===")); */
  828. done:
  829. ;
  830. }
  831. /** Test basic string functionality. */
  832. static void
  833. test_util_strmisc(void *arg)
  834. {
  835. char buf[1024];
  836. int i;
  837. char *cp, *cp_tmp = NULL;
  838. /* Test strl operations */
  839. (void)arg;
  840. tt_int_op(5,==, strlcpy(buf, "Hello", 0));
  841. tt_int_op(5,==, strlcpy(buf, "Hello", 10));
  842. tt_str_op(buf,==, "Hello");
  843. tt_int_op(5,==, strlcpy(buf, "Hello", 6));
  844. tt_str_op(buf,==, "Hello");
  845. tt_int_op(5,==, strlcpy(buf, "Hello", 5));
  846. tt_str_op(buf,==, "Hell");
  847. strlcpy(buf, "Hello", sizeof(buf));
  848. tt_int_op(10,==, strlcat(buf, "Hello", 5));
  849. /* Test strstrip() */
  850. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  851. tor_strstrip(buf, ",!");
  852. tt_str_op(buf,==, "Testing 1 2 3");
  853. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  854. tor_strstrip(buf, "!? ");
  855. tt_str_op(buf,==, "Testing123");
  856. strlcpy(buf, "!!!Testing 1 2 3??", sizeof(buf));
  857. tor_strstrip(buf, "!? ");
  858. tt_str_op(buf,==, "Testing123");
  859. /* Test parse_long */
  860. /* Empty/zero input */
  861. tt_int_op(0L,==, tor_parse_long("",10,0,100,&i,NULL));
  862. tt_int_op(0,==, i);
  863. tt_int_op(0L,==, tor_parse_long("0",10,0,100,&i,NULL));
  864. tt_int_op(1,==, i);
  865. /* Normal cases */
  866. tt_int_op(10L,==, tor_parse_long("10",10,0,100,&i,NULL));
  867. tt_int_op(1,==, i);
  868. tt_int_op(10L,==, tor_parse_long("10",10,0,10,&i,NULL));
  869. tt_int_op(1,==, i);
  870. tt_int_op(10L,==, tor_parse_long("10",10,10,100,&i,NULL));
  871. tt_int_op(1,==, i);
  872. tt_int_op(-50L,==, tor_parse_long("-50",10,-100,100,&i,NULL));
  873. tt_int_op(1,==, i);
  874. tt_int_op(-50L,==, tor_parse_long("-50",10,-100,0,&i,NULL));
  875. tt_int_op(1,==, i);
  876. tt_int_op(-50L,==, tor_parse_long("-50",10,-50,0,&i,NULL));
  877. tt_int_op(1,==, i);
  878. /* Extra garbage */
  879. tt_int_op(0L,==, tor_parse_long("10m",10,0,100,&i,NULL));
  880. tt_int_op(0,==, i);
  881. tt_int_op(0L,==, tor_parse_long("-50 plus garbage",10,-100,100,&i,NULL));
  882. tt_int_op(0,==, i);
  883. tt_int_op(10L,==, tor_parse_long("10m",10,0,100,&i,&cp));
  884. tt_int_op(1,==, i);
  885. tt_str_op(cp,==, "m");
  886. tt_int_op(-50L,==, tor_parse_long("-50 plus garbage",10,-100,100,&i,&cp));
  887. tt_int_op(1,==, i);
  888. tt_str_op(cp,==, " plus garbage");
  889. /* Out of bounds */
  890. tt_int_op(0L,==, tor_parse_long("10",10,50,100,&i,NULL));
  891. tt_int_op(0,==, i);
  892. tt_int_op(0L,==, tor_parse_long("-50",10,0,100,&i,NULL));
  893. tt_int_op(0,==, i);
  894. /* Base different than 10 */
  895. tt_int_op(2L,==, tor_parse_long("10",2,0,100,NULL,NULL));
  896. tt_int_op(0L,==, tor_parse_long("2",2,0,100,NULL,NULL));
  897. tt_int_op(0L,==, tor_parse_long("10",-2,0,100,NULL,NULL));
  898. tt_int_op(68284L,==, tor_parse_long("10abc",16,0,70000,NULL,NULL));
  899. tt_int_op(68284L,==, tor_parse_long("10ABC",16,0,70000,NULL,NULL));
  900. tt_int_op(0,==, tor_parse_long("10ABC",-1,0,70000,&i,NULL));
  901. tt_int_op(i,==, 0);
  902. /* Test parse_ulong */
  903. tt_int_op(0UL,==, tor_parse_ulong("",10,0,100,NULL,NULL));
  904. tt_int_op(0UL,==, tor_parse_ulong("0",10,0,100,NULL,NULL));
  905. tt_int_op(10UL,==, tor_parse_ulong("10",10,0,100,NULL,NULL));
  906. tt_int_op(0UL,==, tor_parse_ulong("10",10,50,100,NULL,NULL));
  907. tt_int_op(10UL,==, tor_parse_ulong("10",10,0,10,NULL,NULL));
  908. tt_int_op(10UL,==, tor_parse_ulong("10",10,10,100,NULL,NULL));
  909. tt_int_op(0UL,==, tor_parse_ulong("8",8,0,100,NULL,NULL));
  910. tt_int_op(50UL,==, tor_parse_ulong("50",10,50,100,NULL,NULL));
  911. tt_int_op(0UL,==, tor_parse_ulong("-50",10,-100,100,NULL,NULL));
  912. tt_int_op(0UL,==, tor_parse_ulong("50",-1,50,100,&i,NULL));
  913. tt_int_op(0,==, i);
  914. /* Test parse_uint64 */
  915. tt_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  916. tt_int_op(1,==, i);
  917. tt_str_op(cp,==, " x");
  918. tt_assert(U64_LITERAL(12345678901) ==
  919. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  920. tt_int_op(1,==, i);
  921. tt_str_op(cp,==, "");
  922. tt_assert(U64_LITERAL(0) ==
  923. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  924. tt_int_op(0,==, i);
  925. tt_assert(U64_LITERAL(0) ==
  926. tor_parse_uint64("123",-1,0,INT32_MAX, &i, &cp));
  927. tt_int_op(0,==, i);
  928. {
  929. /* Test parse_double */
  930. double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
  931. tt_int_op(1,==, i);
  932. tt_assert(DBL_TO_U64(d) == 10);
  933. d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
  934. tt_int_op(1,==, i);
  935. tt_assert(DBL_TO_U64(d) == 0);
  936. d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
  937. tt_int_op(0,==, i);
  938. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
  939. tt_int_op(0,==, i);
  940. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
  941. tt_int_op(1,==, i);
  942. d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
  943. tt_int_op(1,==, i);
  944. tt_assert(DBL_TO_U64(d) == 0);
  945. d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
  946. tt_int_op(1,==, i);
  947. tt_int_op(-10.0,==, d);
  948. }
  949. {
  950. /* Test tor_parse_* where we overflow/underflow the underlying type. */
  951. /* This string should overflow 64-bit ints. */
  952. #define TOOBIG "100000000000000000000000000"
  953. tt_int_op(0L,==, tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  954. tt_int_op(i,==, 0);
  955. tt_int_op(0L,==,
  956. tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  957. tt_int_op(i,==, 0);
  958. tt_int_op(0UL,==, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
  959. tt_int_op(i,==, 0);
  960. tt_u64_op(U64_LITERAL(0), ==, tor_parse_uint64(TOOBIG, 10,
  961. 0, UINT64_MAX, &i, NULL));
  962. tt_int_op(i,==, 0);
  963. }
  964. /* Test snprintf */
  965. /* Returning -1 when there's not enough room in the output buffer */
  966. tt_int_op(-1,==, tor_snprintf(buf, 0, "Foo"));
  967. tt_int_op(-1,==, tor_snprintf(buf, 2, "Foo"));
  968. tt_int_op(-1,==, tor_snprintf(buf, 3, "Foo"));
  969. tt_int_op(-1,!=, tor_snprintf(buf, 4, "Foo"));
  970. /* Always NUL-terminate the output */
  971. tor_snprintf(buf, 5, "abcdef");
  972. tt_int_op(0,==, buf[4]);
  973. tor_snprintf(buf, 10, "abcdef");
  974. tt_int_op(0,==, buf[6]);
  975. /* uint64 */
  976. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  977. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  978. tt_str_op("x!12345678901!x",==, buf);
  979. /* Test str{,case}cmpstart */
  980. tt_assert(strcmpstart("abcdef", "abcdef")==0);
  981. tt_assert(strcmpstart("abcdef", "abc")==0);
  982. tt_assert(strcmpstart("abcdef", "abd")<0);
  983. tt_assert(strcmpstart("abcdef", "abb")>0);
  984. tt_assert(strcmpstart("ab", "abb")<0);
  985. tt_assert(strcmpstart("ab", "")==0);
  986. tt_assert(strcmpstart("ab", "ab ")<0);
  987. tt_assert(strcasecmpstart("abcdef", "abCdEF")==0);
  988. tt_assert(strcasecmpstart("abcDeF", "abc")==0);
  989. tt_assert(strcasecmpstart("abcdef", "Abd")<0);
  990. tt_assert(strcasecmpstart("Abcdef", "abb")>0);
  991. tt_assert(strcasecmpstart("ab", "Abb")<0);
  992. tt_assert(strcasecmpstart("ab", "")==0);
  993. tt_assert(strcasecmpstart("ab", "ab ")<0);
  994. /* Test str{,case}cmpend */
  995. tt_assert(strcmpend("abcdef", "abcdef")==0);
  996. tt_assert(strcmpend("abcdef", "def")==0);
  997. tt_assert(strcmpend("abcdef", "deg")<0);
  998. tt_assert(strcmpend("abcdef", "dee")>0);
  999. tt_assert(strcmpend("ab", "aab")>0);
  1000. tt_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  1001. tt_assert(strcasecmpend("abcdef", "dEF")==0);
  1002. tt_assert(strcasecmpend("abcdef", "Deg")<0);
  1003. tt_assert(strcasecmpend("abcDef", "dee")>0);
  1004. tt_assert(strcasecmpend("AB", "abb")<0);
  1005. /* Test digest_is_zero */
  1006. memset(buf,0,20);
  1007. buf[20] = 'x';
  1008. tt_assert(tor_digest_is_zero(buf));
  1009. buf[19] = 'x';
  1010. tt_assert(!tor_digest_is_zero(buf));
  1011. /* Test mem_is_zero */
  1012. memset(buf,0,128);
  1013. buf[128] = 'x';
  1014. tt_assert(tor_mem_is_zero(buf, 10));
  1015. tt_assert(tor_mem_is_zero(buf, 20));
  1016. tt_assert(tor_mem_is_zero(buf, 128));
  1017. tt_assert(!tor_mem_is_zero(buf, 129));
  1018. buf[60] = (char)255;
  1019. tt_assert(!tor_mem_is_zero(buf, 128));
  1020. buf[0] = (char)1;
  1021. tt_assert(!tor_mem_is_zero(buf, 10));
  1022. /* Test 'escaped' */
  1023. tt_assert(NULL == escaped(NULL));
  1024. tt_str_op("\"\"",==, escaped(""));
  1025. tt_str_op("\"abcd\"",==, escaped("abcd"));
  1026. tt_str_op("\"\\\\ \\n\\r\\t\\\"\\'\"",==, escaped("\\ \n\r\t\"'"));
  1027. tt_str_op("\"unnecessary \\'backslashes\\'\"",==,
  1028. escaped("unnecessary \'backslashes\'"));
  1029. /* Non-printable characters appear as octal */
  1030. tt_str_op("\"z\\001abc\\277d\"",==, escaped("z\001abc\277d"));
  1031. tt_str_op("\"z\\336\\255 ;foo\"",==, escaped("z\xde\xad\x20;foo"));
  1032. /* Test strndup and memdup */
  1033. {
  1034. const char *s = "abcdefghijklmnopqrstuvwxyz";
  1035. cp_tmp = tor_strndup(s, 30);
  1036. tt_str_op(cp_tmp,==, s); /* same string, */
  1037. tt_ptr_op(cp_tmp,!=,s); /* but different pointers. */
  1038. tor_free(cp_tmp);
  1039. cp_tmp = tor_strndup(s, 5);
  1040. tt_str_op(cp_tmp,==, "abcde");
  1041. tor_free(cp_tmp);
  1042. s = "a\0b\0c\0d\0e\0";
  1043. cp_tmp = tor_memdup(s,10);
  1044. tt_mem_op(cp_tmp,==, s, 10); /* same ram, */
  1045. tt_ptr_op(cp_tmp,!=,s); /* but different pointers. */
  1046. tor_free(cp_tmp);
  1047. }
  1048. /* Test str-foo functions */
  1049. cp_tmp = tor_strdup("abcdef");
  1050. tt_assert(tor_strisnonupper(cp_tmp));
  1051. cp_tmp[3] = 'D';
  1052. tt_assert(!tor_strisnonupper(cp_tmp));
  1053. tor_strupper(cp_tmp);
  1054. tt_str_op(cp_tmp,==, "ABCDEF");
  1055. tor_strlower(cp_tmp);
  1056. tt_str_op(cp_tmp,==, "abcdef");
  1057. tt_assert(tor_strisnonupper(cp_tmp));
  1058. tt_assert(tor_strisprint(cp_tmp));
  1059. cp_tmp[3] = 3;
  1060. tt_assert(!tor_strisprint(cp_tmp));
  1061. tor_free(cp_tmp);
  1062. /* Test memmem and memstr */
  1063. {
  1064. const char *haystack = "abcde";
  1065. tt_assert(!tor_memmem(haystack, 5, "ef", 2));
  1066. tt_ptr_op(tor_memmem(haystack, 5, "cd", 2),==, haystack + 2);
  1067. tt_ptr_op(tor_memmem(haystack, 5, "cde", 3),==, haystack + 2);
  1068. tt_assert(!tor_memmem(haystack, 4, "cde", 3));
  1069. haystack = "ababcad";
  1070. tt_ptr_op(tor_memmem(haystack, 7, "abc", 3),==, haystack + 2);
  1071. tt_ptr_op(tor_memmem(haystack, 7, "ad", 2),==, haystack + 5);
  1072. tt_ptr_op(tor_memmem(haystack, 7, "cad", 3),==, haystack + 4);
  1073. tt_assert(!tor_memmem(haystack, 7, "dadad", 5));
  1074. tt_assert(!tor_memmem(haystack, 7, "abcdefghij", 10));
  1075. /* memstr */
  1076. tt_ptr_op(tor_memstr(haystack, 7, "abc"),==, haystack + 2);
  1077. tt_ptr_op(tor_memstr(haystack, 7, "cad"),==, haystack + 4);
  1078. tt_assert(!tor_memstr(haystack, 6, "cad"));
  1079. tt_assert(!tor_memstr(haystack, 7, "cadd"));
  1080. tt_assert(!tor_memstr(haystack, 7, "fe"));
  1081. tt_assert(!tor_memstr(haystack, 7, "ababcade"));
  1082. }
  1083. /* Test hex_str */
  1084. {
  1085. char binary_data[68];
  1086. size_t i;
  1087. for (i = 0; i < sizeof(binary_data); ++i)
  1088. binary_data[i] = i;
  1089. tt_str_op(hex_str(binary_data, 0),==, "");
  1090. tt_str_op(hex_str(binary_data, 1),==, "00");
  1091. tt_str_op(hex_str(binary_data, 17),==,
  1092. "000102030405060708090A0B0C0D0E0F10");
  1093. tt_str_op(hex_str(binary_data, 32),==,
  1094. "000102030405060708090A0B0C0D0E0F"
  1095. "101112131415161718191A1B1C1D1E1F");
  1096. tt_str_op(hex_str(binary_data, 34),==,
  1097. "000102030405060708090A0B0C0D0E0F"
  1098. "101112131415161718191A1B1C1D1E1F");
  1099. /* Repeat these tests for shorter strings after longer strings
  1100. have been tried, to make sure we're correctly terminating strings */
  1101. tt_str_op(hex_str(binary_data, 1),==, "00");
  1102. tt_str_op(hex_str(binary_data, 0),==, "");
  1103. }
  1104. /* Test strcmp_opt */
  1105. tt_int_op(strcmp_opt("", "foo"), <, 0);
  1106. tt_int_op(strcmp_opt("", ""), ==, 0);
  1107. tt_int_op(strcmp_opt("foo", ""), >, 0);
  1108. tt_int_op(strcmp_opt(NULL, ""), <, 0);
  1109. tt_int_op(strcmp_opt(NULL, NULL), ==, 0);
  1110. tt_int_op(strcmp_opt("", NULL), >, 0);
  1111. tt_int_op(strcmp_opt(NULL, "foo"), <, 0);
  1112. tt_int_op(strcmp_opt("foo", NULL), >, 0);
  1113. /* Test strcmp_len */
  1114. tt_int_op(strcmp_len("foo", "bar", 3), >, 0);
  1115. tt_int_op(strcmp_len("foo", "bar", 2), <, 0); /* First len, then lexical */
  1116. tt_int_op(strcmp_len("foo2", "foo1", 4), >, 0);
  1117. tt_int_op(strcmp_len("foo2", "foo1", 3), <, 0); /* Really stop at len */
  1118. tt_int_op(strcmp_len("foo2", "foo", 3), ==, 0); /* Really stop at len */
  1119. tt_int_op(strcmp_len("blah", "", 4), >, 0);
  1120. tt_int_op(strcmp_len("blah", "", 0), ==, 0);
  1121. done:
  1122. tor_free(cp_tmp);
  1123. }
  1124. static void
  1125. test_util_pow2(void *arg)
  1126. {
  1127. /* Test tor_log2(). */
  1128. (void)arg;
  1129. tt_int_op(tor_log2(64),==, 6);
  1130. tt_int_op(tor_log2(65),==, 6);
  1131. tt_int_op(tor_log2(63),==, 5);
  1132. tt_int_op(tor_log2(0),==, 0);/* incorrect mathematically, but as specified */
  1133. tt_int_op(tor_log2(1),==, 0);
  1134. tt_int_op(tor_log2(2),==, 1);
  1135. tt_int_op(tor_log2(3),==, 1);
  1136. tt_int_op(tor_log2(4),==, 2);
  1137. tt_int_op(tor_log2(5),==, 2);
  1138. tt_int_op(tor_log2(U64_LITERAL(40000000000000000)),==, 55);
  1139. tt_int_op(tor_log2(UINT64_MAX),==, 63);
  1140. /* Test round_to_power_of_2 */
  1141. tt_u64_op(round_to_power_of_2(120), ==, 128);
  1142. tt_u64_op(round_to_power_of_2(128), ==, 128);
  1143. tt_u64_op(round_to_power_of_2(130), ==, 128);
  1144. tt_u64_op(round_to_power_of_2(U64_LITERAL(40000000000000000)), ==,
  1145. U64_LITERAL(1)<<55);
  1146. tt_u64_op(round_to_power_of_2(U64_LITERAL(0xffffffffffffffff)), ==,
  1147. U64_LITERAL(1)<<63);
  1148. tt_u64_op(round_to_power_of_2(0), ==, 1);
  1149. tt_u64_op(round_to_power_of_2(1), ==, 1);
  1150. tt_u64_op(round_to_power_of_2(2), ==, 2);
  1151. tt_u64_op(round_to_power_of_2(3), ==, 2);
  1152. tt_u64_op(round_to_power_of_2(4), ==, 4);
  1153. tt_u64_op(round_to_power_of_2(5), ==, 4);
  1154. tt_u64_op(round_to_power_of_2(6), ==, 4);
  1155. tt_u64_op(round_to_power_of_2(7), ==, 8);
  1156. done:
  1157. ;
  1158. }
  1159. /** mutex for thread test to stop the threads hitting data at the same time. */
  1160. static tor_mutex_t *thread_test_mutex_ = NULL;
  1161. /** mutexes for the thread test to make sure that the threads have to
  1162. * interleave somewhat. */
  1163. static tor_mutex_t *thread_test_start1_ = NULL,
  1164. *thread_test_start2_ = NULL;
  1165. /** Shared strmap for the thread test. */
  1166. static strmap_t *thread_test_strmap_ = NULL;
  1167. /** The name of thread1 for the thread test */
  1168. static char *thread1_name_ = NULL;
  1169. /** The name of thread2 for the thread test */
  1170. static char *thread2_name_ = NULL;
  1171. static void thread_test_func_(void* _s) ATTR_NORETURN;
  1172. /** How many iterations have the threads in the unit test run? */
  1173. static int t1_count = 0, t2_count = 0;
  1174. /** Helper function for threading unit tests: This function runs in a
  1175. * subthread. It grabs its own mutex (start1 or start2) to make sure that it
  1176. * should start, then it repeatedly alters _test_thread_strmap protected by
  1177. * thread_test_mutex_. */
  1178. static void
  1179. thread_test_func_(void* _s)
  1180. {
  1181. char *s = _s;
  1182. int i, *count;
  1183. tor_mutex_t *m;
  1184. char buf[64];
  1185. char **cp;
  1186. if (!strcmp(s, "thread 1")) {
  1187. m = thread_test_start1_;
  1188. cp = &thread1_name_;
  1189. count = &t1_count;
  1190. } else {
  1191. m = thread_test_start2_;
  1192. cp = &thread2_name_;
  1193. count = &t2_count;
  1194. }
  1195. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  1196. *cp = tor_strdup(buf);
  1197. tor_mutex_acquire(m);
  1198. for (i=0; i<10000; ++i) {
  1199. tor_mutex_acquire(thread_test_mutex_);
  1200. strmap_set(thread_test_strmap_, "last to run", *cp);
  1201. ++*count;
  1202. tor_mutex_release(thread_test_mutex_);
  1203. }
  1204. tor_mutex_acquire(thread_test_mutex_);
  1205. strmap_set(thread_test_strmap_, s, *cp);
  1206. tor_mutex_release(thread_test_mutex_);
  1207. tor_mutex_release(m);
  1208. spawn_exit();
  1209. }
  1210. /** Run unit tests for threading logic. */
  1211. static void
  1212. test_util_threads(void *arg)
  1213. {
  1214. char *s1 = NULL, *s2 = NULL;
  1215. int done = 0, timedout = 0;
  1216. time_t started;
  1217. #ifndef _WIN32
  1218. struct timeval tv;
  1219. tv.tv_sec=0;
  1220. tv.tv_usec=100*1000;
  1221. #endif
  1222. (void)arg;
  1223. thread_test_mutex_ = tor_mutex_new();
  1224. thread_test_start1_ = tor_mutex_new();
  1225. thread_test_start2_ = tor_mutex_new();
  1226. thread_test_strmap_ = strmap_new();
  1227. s1 = tor_strdup("thread 1");
  1228. s2 = tor_strdup("thread 2");
  1229. tor_mutex_acquire(thread_test_start1_);
  1230. tor_mutex_acquire(thread_test_start2_);
  1231. spawn_func(thread_test_func_, s1);
  1232. spawn_func(thread_test_func_, s2);
  1233. tor_mutex_release(thread_test_start2_);
  1234. tor_mutex_release(thread_test_start1_);
  1235. started = time(NULL);
  1236. while (!done) {
  1237. tor_mutex_acquire(thread_test_mutex_);
  1238. strmap_assert_ok(thread_test_strmap_);
  1239. if (strmap_get(thread_test_strmap_, "thread 1") &&
  1240. strmap_get(thread_test_strmap_, "thread 2")) {
  1241. done = 1;
  1242. } else if (time(NULL) > started + 150) {
  1243. timedout = done = 1;
  1244. }
  1245. tor_mutex_release(thread_test_mutex_);
  1246. #ifndef _WIN32
  1247. /* Prevent the main thread from starving the worker threads. */
  1248. select(0, NULL, NULL, NULL, &tv);
  1249. #endif
  1250. }
  1251. tor_mutex_acquire(thread_test_start1_);
  1252. tor_mutex_release(thread_test_start1_);
  1253. tor_mutex_acquire(thread_test_start2_);
  1254. tor_mutex_release(thread_test_start2_);
  1255. tor_mutex_free(thread_test_mutex_);
  1256. if (timedout) {
  1257. printf("\nTimed out: %d %d", t1_count, t2_count);
  1258. tt_assert(strmap_get(thread_test_strmap_, "thread 1"));
  1259. tt_assert(strmap_get(thread_test_strmap_, "thread 2"));
  1260. tt_assert(!timedout);
  1261. }
  1262. /* different thread IDs. */
  1263. tt_assert(strcmp(strmap_get(thread_test_strmap_, "thread 1"),
  1264. strmap_get(thread_test_strmap_, "thread 2")));
  1265. tt_assert(!strcmp(strmap_get(thread_test_strmap_, "thread 1"),
  1266. strmap_get(thread_test_strmap_, "last to run")) ||
  1267. !strcmp(strmap_get(thread_test_strmap_, "thread 2"),
  1268. strmap_get(thread_test_strmap_, "last to run")));
  1269. done:
  1270. tor_free(s1);
  1271. tor_free(s2);
  1272. tor_free(thread1_name_);
  1273. tor_free(thread2_name_);
  1274. if (thread_test_strmap_)
  1275. strmap_free(thread_test_strmap_, NULL);
  1276. if (thread_test_start1_)
  1277. tor_mutex_free(thread_test_start1_);
  1278. if (thread_test_start2_)
  1279. tor_mutex_free(thread_test_start2_);
  1280. }
  1281. /** Run unit tests for compression functions */
  1282. static void
  1283. test_util_gzip(void *arg)
  1284. {
  1285. char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  1286. const char *ccp2;
  1287. size_t len1, len2;
  1288. tor_zlib_state_t *state = NULL;
  1289. (void)arg;
  1290. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  1291. tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  1292. if (is_gzip_supported()) {
  1293. tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1294. GZIP_METHOD));
  1295. tt_assert(buf2);
  1296. tt_assert(len1 < strlen(buf1));
  1297. tt_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  1298. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1299. GZIP_METHOD, 1, LOG_INFO));
  1300. tt_assert(buf3);
  1301. tt_int_op(strlen(buf1) + 1,==, len2);
  1302. tt_str_op(buf1,==, buf3);
  1303. tor_free(buf2);
  1304. tor_free(buf3);
  1305. }
  1306. tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1307. ZLIB_METHOD));
  1308. tt_assert(buf2);
  1309. tt_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  1310. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1311. ZLIB_METHOD, 1, LOG_INFO));
  1312. tt_assert(buf3);
  1313. tt_int_op(strlen(buf1) + 1,==, len2);
  1314. tt_str_op(buf1,==, buf3);
  1315. /* Check whether we can uncompress concatenated, compressed strings. */
  1316. tor_free(buf3);
  1317. buf2 = tor_reallocarray(buf2, len1, 2);
  1318. memcpy(buf2+len1, buf2, len1);
  1319. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  1320. ZLIB_METHOD, 1, LOG_INFO));
  1321. tt_int_op((strlen(buf1)+1)*2,==, len2);
  1322. tt_mem_op(buf3,==,
  1323. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  1324. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  1325. (strlen(buf1)+1)*2);
  1326. tor_free(buf1);
  1327. tor_free(buf2);
  1328. tor_free(buf3);
  1329. /* Check whether we can uncompress partial strings. */
  1330. buf1 =
  1331. tor_strdup("String with low redundancy that won't be compressed much.");
  1332. tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1333. ZLIB_METHOD));
  1334. tt_assert(len1>16);
  1335. /* when we allow an incomplete string, we should succeed.*/
  1336. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1337. ZLIB_METHOD, 0, LOG_INFO));
  1338. tt_assert(len2 > 5);
  1339. buf3[len2]='\0';
  1340. tt_assert(!strcmpstart(buf1, buf3));
  1341. /* when we demand a complete string, this must fail. */
  1342. tor_free(buf3);
  1343. tt_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1344. ZLIB_METHOD, 1, LOG_INFO));
  1345. tt_assert(!buf3);
  1346. /* Now, try streaming compression. */
  1347. tor_free(buf1);
  1348. tor_free(buf2);
  1349. tor_free(buf3);
  1350. state = tor_zlib_new(1, ZLIB_METHOD);
  1351. tt_assert(state);
  1352. cp1 = buf1 = tor_malloc(1024);
  1353. len1 = 1024;
  1354. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  1355. len2 = 21;
  1356. tt_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  1357. == TOR_ZLIB_OK);
  1358. tt_int_op(0,==, len2); /* Make sure we compressed it all. */
  1359. tt_assert(cp1 > buf1);
  1360. len2 = 0;
  1361. cp2 = cp1;
  1362. tt_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  1363. == TOR_ZLIB_DONE);
  1364. tt_int_op(0,==, len2);
  1365. tt_assert(cp1 > cp2); /* Make sure we really added something. */
  1366. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  1367. ZLIB_METHOD, 1, LOG_WARN));
  1368. tt_str_op(buf3,==,"ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  1369. tt_int_op(21,==, len2);
  1370. done:
  1371. if (state)
  1372. tor_zlib_free(state);
  1373. tor_free(buf2);
  1374. tor_free(buf3);
  1375. tor_free(buf1);
  1376. }
  1377. /** Run unit tests for mmap() wrapper functionality. */
  1378. static void
  1379. test_util_mmap(void *arg)
  1380. {
  1381. char *fname1 = tor_strdup(get_fname("mapped_1"));
  1382. char *fname2 = tor_strdup(get_fname("mapped_2"));
  1383. char *fname3 = tor_strdup(get_fname("mapped_3"));
  1384. const size_t buflen = 17000;
  1385. char *buf = tor_malloc(17000);
  1386. tor_mmap_t *mapping = NULL;
  1387. (void)arg;
  1388. crypto_rand(buf, buflen);
  1389. mapping = tor_mmap_file(fname1);
  1390. tt_assert(! mapping);
  1391. write_str_to_file(fname1, "Short file.", 1);
  1392. mapping = tor_mmap_file(fname1);
  1393. tt_assert(mapping);
  1394. tt_int_op(mapping->size,==, strlen("Short file."));
  1395. tt_str_op(mapping->data,==, "Short file.");
  1396. #ifdef _WIN32
  1397. tt_int_op(0, ==, tor_munmap_file(mapping));
  1398. mapping = NULL;
  1399. tt_assert(unlink(fname1) == 0);
  1400. #else
  1401. /* make sure we can unlink. */
  1402. tt_assert(unlink(fname1) == 0);
  1403. tt_str_op(mapping->data,==, "Short file.");
  1404. tt_int_op(0, ==, tor_munmap_file(mapping));
  1405. mapping = NULL;
  1406. #endif
  1407. /* Now a zero-length file. */
  1408. write_str_to_file(fname1, "", 1);
  1409. mapping = tor_mmap_file(fname1);
  1410. tt_ptr_op(mapping,==, NULL);
  1411. tt_int_op(ERANGE,==, errno);
  1412. unlink(fname1);
  1413. /* Make sure that we fail to map a no-longer-existent file. */
  1414. mapping = tor_mmap_file(fname1);
  1415. tt_assert(! mapping);
  1416. /* Now try a big file that stretches across a few pages and isn't aligned */
  1417. write_bytes_to_file(fname2, buf, buflen, 1);
  1418. mapping = tor_mmap_file(fname2);
  1419. tt_assert(mapping);
  1420. tt_int_op(mapping->size,==, buflen);
  1421. tt_mem_op(mapping->data,==, buf, buflen);
  1422. tt_int_op(0, ==, tor_munmap_file(mapping));
  1423. mapping = NULL;
  1424. /* Now try a big aligned file. */
  1425. write_bytes_to_file(fname3, buf, 16384, 1);
  1426. mapping = tor_mmap_file(fname3);
  1427. tt_assert(mapping);
  1428. tt_int_op(mapping->size,==, 16384);
  1429. tt_mem_op(mapping->data,==, buf, 16384);
  1430. tt_int_op(0, ==, tor_munmap_file(mapping));
  1431. mapping = NULL;
  1432. done:
  1433. unlink(fname1);
  1434. unlink(fname2);
  1435. unlink(fname3);
  1436. tor_free(fname1);
  1437. tor_free(fname2);
  1438. tor_free(fname3);
  1439. tor_free(buf);
  1440. tor_munmap_file(mapping);
  1441. }
  1442. /** Run unit tests for escaping/unescaping data for use by controllers. */
  1443. static void
  1444. test_util_control_formats(void *arg)
  1445. {
  1446. char *out = NULL;
  1447. const char *inp =
  1448. "..This is a test\r\n.of the emergency \n..system.\r\n\rZ.\r\n";
  1449. size_t sz;
  1450. (void)arg;
  1451. sz = read_escaped_data(inp, strlen(inp), &out);
  1452. tt_str_op(out,==,
  1453. ".This is a test\nof the emergency \n.system.\n\rZ.\n");
  1454. tt_int_op(sz,==, strlen(out));
  1455. done:
  1456. tor_free(out);
  1457. }
  1458. #define test_feq(value1,value2) do { \
  1459. double v1 = (value1), v2=(value2); \
  1460. double tf_diff = v1-v2; \
  1461. double tf_tolerance = ((v1+v2)/2.0)/1e8; \
  1462. if (tf_diff<0) tf_diff=-tf_diff; \
  1463. if (tf_tolerance<0) tf_tolerance=-tf_tolerance; \
  1464. if (tf_diff<tf_tolerance) { \
  1465. TT_BLATHER(("%s ~~ %s: %f ~~ %f",#value1,#value2,v1,v2)); \
  1466. } else { \
  1467. TT_FAIL(("%s ~~ %s: %f != %f",#value1,#value2,v1,v2)); \
  1468. } \
  1469. } while (0)
  1470. static void
  1471. test_util_sscanf(void *arg)
  1472. {
  1473. unsigned u1, u2, u3;
  1474. unsigned long ulng;
  1475. char s1[20], s2[10], s3[10], ch;
  1476. int r;
  1477. long lng1,lng2;
  1478. int int1, int2;
  1479. double d1,d2,d3,d4;
  1480. /* Simple tests (malformed patterns, literal matching, ...) */
  1481. (void)arg;
  1482. tt_int_op(-1,==, tor_sscanf("123", "%i", &r)); /* %i is not supported */
  1483. tt_int_op(-1,==,
  1484. tor_sscanf("wrong", "%5c", s1)); /* %c cannot have a number. */
  1485. tt_int_op(-1,==, tor_sscanf("hello", "%s", s1)); /* %s needs a number. */
  1486. tt_int_op(-1,==, tor_sscanf("prettylongstring", "%999999s", s1));
  1487. #if 0
  1488. /* GCC thinks these two are illegal. */
  1489. test_eq(-1, tor_sscanf("prettylongstring", "%0s", s1));
  1490. test_eq(0, tor_sscanf("prettylongstring", "%10s", NULL));
  1491. #endif
  1492. /* No '%'-strings: always "success" */
  1493. tt_int_op(0,==, tor_sscanf("hello world", "hello world"));
  1494. tt_int_op(0,==, tor_sscanf("hello world", "good bye"));
  1495. /* Excess data */
  1496. tt_int_op(0,==,
  1497. tor_sscanf("hello 3", "%u", &u1)); /* have to match the start */
  1498. tt_int_op(0,==, tor_sscanf(" 3 hello", "%u", &u1));
  1499. tt_int_op(0,==,
  1500. tor_sscanf(" 3 hello", "%2u", &u1)); /* not even in this case */
  1501. tt_int_op(1,==,
  1502. tor_sscanf("3 hello", "%u", &u1)); /* but trailing is alright */
  1503. /* Numbers (ie. %u) */
  1504. tt_int_op(0,==,
  1505. tor_sscanf("hello world 3", "hello worlb %u", &u1)); /* d vs b */
  1506. tt_int_op(1,==, tor_sscanf("12345", "%u", &u1));
  1507. tt_int_op(12345u,==, u1);
  1508. tt_int_op(1,==, tor_sscanf("12346 ", "%u", &u1));
  1509. tt_int_op(12346u,==, u1);
  1510. tt_int_op(0,==, tor_sscanf(" 12347", "%u", &u1));
  1511. tt_int_op(1,==, tor_sscanf(" 12348", " %u", &u1));
  1512. tt_int_op(12348u,==, u1);
  1513. tt_int_op(1,==, tor_sscanf("0", "%u", &u1));
  1514. tt_int_op(0u,==, u1);
  1515. tt_int_op(1,==, tor_sscanf("0000", "%u", &u2));
  1516. tt_int_op(0u,==, u2);
  1517. tt_int_op(0,==, tor_sscanf("", "%u", &u1)); /* absent number */
  1518. tt_int_op(0,==, tor_sscanf("A", "%u", &u1)); /* bogus number */
  1519. tt_int_op(0,==, tor_sscanf("-1", "%u", &u1)); /* negative number */
  1520. /* Numbers with size (eg. %2u) */
  1521. tt_int_op(0,==, tor_sscanf("-1", "%2u", &u1));
  1522. tt_int_op(2,==, tor_sscanf("123456", "%2u%u", &u1, &u2));
  1523. tt_int_op(12u,==, u1);
  1524. tt_int_op(3456u,==, u2);
  1525. tt_int_op(1,==, tor_sscanf("123456", "%8u", &u1));
  1526. tt_int_op(123456u,==, u1);
  1527. tt_int_op(1,==, tor_sscanf("123457 ", "%8u", &u1));
  1528. tt_int_op(123457u,==, u1);
  1529. tt_int_op(0,==, tor_sscanf(" 123456", "%8u", &u1));
  1530. tt_int_op(3,==, tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3));
  1531. tt_int_op(12u,==, u1);
  1532. tt_int_op(3u,==, u2);
  1533. tt_int_op(456u,==, u3);
  1534. tt_int_op(3,==,
  1535. tor_sscanf("67:8:099", "%2u:%2u:%3u", &u1, &u2, &u3)); /* 0s */
  1536. tt_int_op(67u,==, u1);
  1537. tt_int_op(8u,==, u2);
  1538. tt_int_op(99u,==, u3);
  1539. /* %u does not match space.*/
  1540. tt_int_op(2,==, tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3));
  1541. tt_int_op(12u,==, u1);
  1542. tt_int_op(3u,==, u2);
  1543. /* %u does not match negative numbers. */
  1544. tt_int_op(2,==, tor_sscanf("67:8:-9", "%2u:%2u:%3u", &u1, &u2, &u3));
  1545. tt_int_op(67u,==, u1);
  1546. tt_int_op(8u,==, u2);
  1547. /* Arbitrary amounts of 0-padding are okay */
  1548. tt_int_op(3,==, tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u",
  1549. &u1, &u2, &u3));
  1550. tt_int_op(12u,==, u1);
  1551. tt_int_op(3u,==, u2);
  1552. tt_int_op(99u,==, u3);
  1553. /* Hex (ie. %x) */
  1554. tt_int_op(3,==, tor_sscanf("1234 02aBcdEf ff", "%x %x %x", &u1, &u2, &u3));
  1555. tt_int_op(0x1234,==, u1);
  1556. tt_int_op(0x2ABCDEF,==, u2);
  1557. tt_int_op(0xFF,==, u3);
  1558. /* Width works on %x */
  1559. tt_int_op(3,==, tor_sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3));
  1560. tt_int_op(0xf00d,==, u1);
  1561. tt_int_op(0xcafe,==, u2);
  1562. tt_int_op(444,==, u3);
  1563. /* Literal '%' (ie. '%%') */
  1564. tt_int_op(1,==, tor_sscanf("99% fresh", "%3u%% fresh", &u1));
  1565. tt_int_op(99,==, u1);
  1566. tt_int_op(0,==, tor_sscanf("99 fresh", "%% %3u %s", &u1, s1));
  1567. tt_int_op(1,==, tor_sscanf("99 fresh", "%3u%% %s", &u1, s1));
  1568. tt_int_op(2,==, tor_sscanf("99 fresh", "%3u %5s %%", &u1, s1));
  1569. tt_int_op(99,==, u1);
  1570. tt_str_op(s1,==, "fresh");
  1571. tt_int_op(1,==, tor_sscanf("% boo", "%% %3s", s1));
  1572. tt_str_op("boo",==, s1);
  1573. /* Strings (ie. %s) */
  1574. tt_int_op(2,==, tor_sscanf("hello", "%3s%7s", s1, s2));
  1575. tt_str_op(s1,==, "hel");
  1576. tt_str_op(s2,==, "lo");
  1577. tt_int_op(2,==, tor_sscanf("WD40", "%2s%u", s3, &u1)); /* %s%u */
  1578. tt_str_op(s3,==, "WD");
  1579. tt_int_op(40,==, u1);
  1580. tt_int_op(2,==, tor_sscanf("WD40", "%3s%u", s3, &u1)); /* %s%u */
  1581. tt_str_op(s3,==, "WD4");
  1582. tt_int_op(0,==, u1);
  1583. tt_int_op(2,==, tor_sscanf("76trombones", "%6u%9s", &u1, s1)); /* %u%s */
  1584. tt_int_op(76,==, u1);
  1585. tt_str_op(s1,==, "trombones");
  1586. tt_int_op(1,==, tor_sscanf("prettylongstring", "%999s", s1));
  1587. tt_str_op(s1,==, "prettylongstring");
  1588. /* %s doesn't eat spaces */
  1589. tt_int_op(2,==, tor_sscanf("hello world", "%9s %9s", s1, s2));
  1590. tt_str_op(s1,==, "hello");
  1591. tt_str_op(s2,==, "world");
  1592. tt_int_op(2,==, tor_sscanf("bye world?", "%9s %9s", s1, s2));
  1593. tt_str_op(s1,==, "bye");
  1594. tt_str_op(s2,==, "");
  1595. tt_int_op(3,==,
  1596. tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3)); /* %s can be empty. */
  1597. tt_str_op(s1,==, "hi");
  1598. tt_str_op(s2,==, "");
  1599. tt_str_op(s3,==, "");
  1600. tt_int_op(3,==, tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
  1601. tt_int_op(4,==,
  1602. tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
  1603. tt_int_op(' ',==, ch);
  1604. r = tor_sscanf("12345 -67890 -1", "%d %ld %d", &int1, &lng1, &int2);
  1605. tt_int_op(r,==, 3);
  1606. tt_int_op(int1,==, 12345);
  1607. tt_int_op(lng1,==, -67890);
  1608. tt_int_op(int2,==, -1);
  1609. #if SIZEOF_INT == 4
  1610. /* %u */
  1611. /* UINT32_MAX should work */
  1612. tt_int_op(1,==, tor_sscanf("4294967295", "%u", &u1));
  1613. tt_int_op(4294967295U,==, u1);
  1614. /* But UINT32_MAX + 1 shouldn't work */
  1615. tt_int_op(0,==, tor_sscanf("4294967296", "%u", &u1));
  1616. /* but parsing only 9... */
  1617. tt_int_op(1,==, tor_sscanf("4294967296", "%9u", &u1));
  1618. tt_int_op(429496729U,==, u1);
  1619. /* %x */
  1620. /* UINT32_MAX should work */
  1621. tt_int_op(1,==, tor_sscanf("FFFFFFFF", "%x", &u1));
  1622. tt_int_op(0xFFFFFFFF,==, u1);
  1623. /* But UINT32_MAX + 1 shouldn't work */
  1624. tt_int_op(0,==, tor_sscanf("100000000", "%x", &u1));
  1625. /* %d */
  1626. /* INT32_MIN and INT32_MAX should work */
  1627. r = tor_sscanf("-2147483648. 2147483647.", "%d. %d.", &int1, &int2);
  1628. tt_int_op(r,==, 2);
  1629. tt_int_op(int1,==, -2147483647 - 1);
  1630. tt_int_op(int2,==, 2147483647);
  1631. /* But INT32_MIN - 1 and INT32_MAX + 1 shouldn't work */
  1632. r = tor_sscanf("-2147483649.", "%d.", &int1);
  1633. tt_int_op(r,==, 0);
  1634. r = tor_sscanf("2147483648.", "%d.", &int1);
  1635. tt_int_op(r,==, 0);
  1636. /* and the first failure stops further processing */
  1637. r = tor_sscanf("-2147483648. 2147483648.",
  1638. "%d. %d.", &int1, &int2);
  1639. tt_int_op(r,==, 1);
  1640. r = tor_sscanf("-2147483649. 2147483647.",
  1641. "%d. %d.", &int1, &int2);
  1642. tt_int_op(r,==, 0);
  1643. r = tor_sscanf("2147483648. -2147483649.",
  1644. "%d. %d.", &int1, &int2);
  1645. tt_int_op(r,==, 0);
  1646. #elif SIZEOF_INT == 8
  1647. /* %u */
  1648. /* UINT64_MAX should work */
  1649. tt_int_op(1,==, tor_sscanf("18446744073709551615", "%u", &u1));
  1650. tt_int_op(18446744073709551615U,==, u1);
  1651. /* But UINT64_MAX + 1 shouldn't work */
  1652. tt_int_op(0,==, tor_sscanf("18446744073709551616", "%u", &u1));
  1653. /* but parsing only 19... */
  1654. tt_int_op(1,==, tor_sscanf("18446744073709551616", "%19u", &u1));
  1655. tt_int_op(1844674407370955161U,==, u1);
  1656. /* %x */
  1657. /* UINT64_MAX should work */
  1658. tt_int_op(1,==, tor_sscanf("FFFFFFFFFFFFFFFF", "%x", &u1));
  1659. tt_int_op(0xFFFFFFFFFFFFFFFF,==, u1);
  1660. /* But UINT64_MAX + 1 shouldn't work */
  1661. tt_int_op(0,==, tor_sscanf("10000000000000000", "%x", &u1));
  1662. /* %d */
  1663. /* INT64_MIN and INT64_MAX should work */
  1664. r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
  1665. "%d. %d.", &int1, &int2);
  1666. tt_int_op(r,==, 2);
  1667. tt_int_op(int1,==, -9223372036854775807 - 1);
  1668. tt_int_op(int2,==, 9223372036854775807);
  1669. /* But INT64_MIN - 1 and INT64_MAX + 1 shouldn't work */
  1670. r = tor_sscanf("-9223372036854775809.", "%d.", &int1);
  1671. tt_int_op(r,==, 0);
  1672. r = tor_sscanf("9223372036854775808.", "%d.", &int1);
  1673. tt_int_op(r,==, 0);
  1674. /* and the first failure stops further processing */
  1675. r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
  1676. "%d. %d.", &int1, &int2);
  1677. tt_int_op(r,==, 1);
  1678. r = tor_sscanf("-9223372036854775809. 9223372036854775807.",
  1679. "%d. %d.", &int1, &int2);
  1680. tt_int_op(r,==, 0);
  1681. r = tor_sscanf("9223372036854775808. -9223372036854775809.",
  1682. "%d. %d.", &int1, &int2);
  1683. tt_int_op(r,==, 0);
  1684. #endif
  1685. #if SIZEOF_LONG == 4
  1686. /* %lu */
  1687. /* UINT32_MAX should work */
  1688. tt_int_op(1,==, tor_sscanf("4294967295", "%lu", &ulng));
  1689. tt_int_op(4294967295UL,==, ulng);
  1690. /* But UINT32_MAX + 1 shouldn't work */
  1691. tt_int_op(0,==, tor_sscanf("4294967296", "%lu", &ulng));
  1692. /* but parsing only 9... */
  1693. tt_int_op(1,==, tor_sscanf("4294967296", "%9lu", &ulng));
  1694. tt_int_op(429496729UL,==, ulng);
  1695. /* %lx */
  1696. /* UINT32_MAX should work */
  1697. tt_int_op(1,==, tor_sscanf("FFFFFFFF", "%lx", &ulng));
  1698. tt_int_op(0xFFFFFFFFUL,==, ulng);
  1699. /* But UINT32_MAX + 1 shouldn't work */
  1700. tt_int_op(0,==, tor_sscanf("100000000", "%lx", &ulng));
  1701. /* %ld */
  1702. /* INT32_MIN and INT32_MAX should work */
  1703. r = tor_sscanf("-2147483648. 2147483647.", "%ld. %ld.", &lng1, &lng2);
  1704. tt_int_op(r,==, 2);
  1705. tt_int_op(lng1,==, -2147483647L - 1L);
  1706. tt_int_op(lng2,==, 2147483647L);
  1707. /* But INT32_MIN - 1 and INT32_MAX + 1 shouldn't work */
  1708. r = tor_sscanf("-2147483649.", "%ld.", &lng1);
  1709. tt_int_op(r,==, 0);
  1710. r = tor_sscanf("2147483648.", "%ld.", &lng1);
  1711. tt_int_op(r,==, 0);
  1712. /* and the first failure stops further processing */
  1713. r = tor_sscanf("-2147483648. 2147483648.",
  1714. "%ld. %ld.", &lng1, &lng2);
  1715. tt_int_op(r,==, 1);
  1716. r = tor_sscanf("-2147483649. 2147483647.",
  1717. "%ld. %ld.", &lng1, &lng2);
  1718. tt_int_op(r,==, 0);
  1719. r = tor_sscanf("2147483648. -2147483649.",
  1720. "%ld. %ld.", &lng1, &lng2);
  1721. tt_int_op(r,==, 0);
  1722. #elif SIZEOF_LONG == 8
  1723. /* %lu */
  1724. /* UINT64_MAX should work */
  1725. tt_int_op(1,==, tor_sscanf("18446744073709551615", "%lu", &ulng));
  1726. tt_int_op(18446744073709551615UL,==, ulng);
  1727. /* But UINT64_MAX + 1 shouldn't work */
  1728. tt_int_op(0,==, tor_sscanf("18446744073709551616", "%lu", &ulng));
  1729. /* but parsing only 19... */
  1730. tt_int_op(1,==, tor_sscanf("18446744073709551616", "%19lu", &ulng));
  1731. tt_int_op(1844674407370955161UL,==, ulng);
  1732. /* %lx */
  1733. /* UINT64_MAX should work */
  1734. tt_int_op(1,==, tor_sscanf("FFFFFFFFFFFFFFFF", "%lx", &ulng));
  1735. tt_int_op(0xFFFFFFFFFFFFFFFFUL,==, ulng);
  1736. /* But UINT64_MAX + 1 shouldn't work */
  1737. tt_int_op(0,==, tor_sscanf("10000000000000000", "%lx", &ulng));
  1738. /* %ld */
  1739. /* INT64_MIN and INT64_MAX should work */
  1740. r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
  1741. "%ld. %ld.", &lng1, &lng2);
  1742. tt_int_op(r,==, 2);
  1743. tt_int_op(lng1,==, -9223372036854775807L - 1L);
  1744. tt_int_op(lng2,==, 9223372036854775807L);
  1745. /* But INT64_MIN - 1 and INT64_MAX + 1 shouldn't work */
  1746. r = tor_sscanf("-9223372036854775809.", "%ld.", &lng1);
  1747. tt_int_op(r,==, 0);
  1748. r = tor_sscanf("9223372036854775808.", "%ld.", &lng1);
  1749. tt_int_op(r,==, 0);
  1750. /* and the first failure stops further processing */
  1751. r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
  1752. "%ld. %ld.", &lng1, &lng2);
  1753. tt_int_op(r,==, 1);
  1754. r = tor_sscanf("-9223372036854775809. 9223372036854775807.",
  1755. "%ld. %ld.", &lng1, &lng2);
  1756. tt_int_op(r,==, 0);
  1757. r = tor_sscanf("9223372036854775808. -9223372036854775809.",
  1758. "%ld. %ld.", &lng1, &lng2);
  1759. tt_int_op(r,==, 0);
  1760. #endif
  1761. r = tor_sscanf("123.456 .000007 -900123123.2000787 00003.2",
  1762. "%lf %lf %lf %lf", &d1,&d2,&d3,&d4);
  1763. tt_int_op(r,==, 4);
  1764. test_feq(d1, 123.456);
  1765. test_feq(d2, .000007);
  1766. test_feq(d3, -900123123.2000787);
  1767. test_feq(d4, 3.2);
  1768. done:
  1769. ;
  1770. }
  1771. #define tt_char_op(a,op,b) tt_assert_op_type(a,op,b,char,"%c")
  1772. #define tt_ci_char_op(a,op,b) tt_char_op(tolower(a),op,tolower(b))
  1773. #ifndef HAVE_STRNLEN
  1774. static size_t
  1775. strnlen(const char *s, size_t len)
  1776. {
  1777. const char *p = memchr(s, 0, len);
  1778. if (!p)
  1779. return len;
  1780. return p - s;
  1781. }
  1782. #endif
  1783. static void
  1784. test_util_format_time_interval(void *arg)
  1785. {
  1786. /* use the same sized buffer and integers as tor uses */
  1787. #define DBUF_SIZE 64
  1788. char dbuf[DBUF_SIZE];
  1789. #define T_ "%ld"
  1790. long sec, min, hour, day;
  1791. /* we don't care about the exact spelling of the
  1792. * second(s), minute(s), hour(s), day(s) labels */
  1793. #define LABEL_SIZE 21
  1794. #define L_ "%20s"
  1795. char label_s[LABEL_SIZE];
  1796. char label_m[LABEL_SIZE];
  1797. char label_h[LABEL_SIZE];
  1798. char label_d[LABEL_SIZE];
  1799. #define TL_ T_ " " L_
  1800. int r;
  1801. (void)arg;
  1802. /* In these tests, we're not picky about
  1803. * spelling or abbreviations */
  1804. /* seconds: 0, 1, 9, 10, 59 */
  1805. /* ignore exact spelling of "second(s)"*/
  1806. format_time_interval(dbuf, sizeof(dbuf), 0);
  1807. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1808. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  1809. tt_int_op(r,==, 2);
  1810. tt_ci_char_op(label_s[0],==, 's');
  1811. tt_int_op(sec,==, 0);
  1812. format_time_interval(dbuf, sizeof(dbuf), 1);
  1813. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1814. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  1815. tt_int_op(r,==, 2);
  1816. tt_ci_char_op(label_s[0],==, 's');
  1817. tt_int_op(sec,==, 1);
  1818. format_time_interval(dbuf, sizeof(dbuf), 10);
  1819. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1820. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  1821. tt_int_op(r,==, 2);
  1822. tt_ci_char_op(label_s[0],==, 's');
  1823. tt_int_op(sec,==, 10);
  1824. format_time_interval(dbuf, sizeof(dbuf), 59);
  1825. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1826. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  1827. tt_int_op(r,==, 2);
  1828. tt_ci_char_op(label_s[0],==, 's');
  1829. tt_int_op(sec,==, 59);
  1830. /* negative seconds are reported as their absolute value */
  1831. format_time_interval(dbuf, sizeof(dbuf), -4);
  1832. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1833. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  1834. tt_int_op(r,==, 2);
  1835. tt_ci_char_op(label_s[0],==, 's');
  1836. tt_int_op(sec,==, 4);
  1837. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1838. format_time_interval(dbuf, sizeof(dbuf), -32);
  1839. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1840. r = tor_sscanf(dbuf, TL_, &sec, label_s);
  1841. tt_int_op(r,==, 2);
  1842. tt_ci_char_op(label_s[0],==, 's');
  1843. tt_int_op(sec,==, 32);
  1844. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1845. /* minutes: 1:00, 1:01, 1:59, 2:00, 2:01, 59:59 */
  1846. /* ignore trailing "0 second(s)", if present */
  1847. format_time_interval(dbuf, sizeof(dbuf), 60);
  1848. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1849. r = tor_sscanf(dbuf, TL_, &min, label_m);
  1850. tt_int_op(r,==, 2);
  1851. tt_ci_char_op(label_m[0],==, 'm');
  1852. tt_int_op(min,==, 1);
  1853. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1854. /* ignore exact spelling of "minute(s)," and "second(s)" */
  1855. format_time_interval(dbuf, sizeof(dbuf), 60 + 1);
  1856. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1857. r = tor_sscanf(dbuf, TL_ " " TL_,
  1858. &min, label_m, &sec, label_s);
  1859. tt_int_op(r,==, 4);
  1860. tt_int_op(min,==, 1);
  1861. tt_ci_char_op(label_m[0],==, 'm');
  1862. tt_int_op(sec,==, 1);
  1863. tt_ci_char_op(label_s[0],==, 's');
  1864. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1865. format_time_interval(dbuf, sizeof(dbuf), 60*2 - 1);
  1866. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1867. r = tor_sscanf(dbuf, TL_ " " TL_,
  1868. &min, label_m, &sec, label_s);
  1869. tt_int_op(r,==, 4);
  1870. tt_int_op(min,==, 1);
  1871. tt_ci_char_op(label_m[0],==, 'm');
  1872. tt_int_op(sec,==, 59);
  1873. tt_ci_char_op(label_s[0],==, 's');
  1874. /* ignore trailing "0 second(s)", if present */
  1875. format_time_interval(dbuf, sizeof(dbuf), 60*2);
  1876. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1877. r = tor_sscanf(dbuf, TL_, &min, label_m);
  1878. tt_int_op(r,==, 2);
  1879. tt_int_op(min,==, 2);
  1880. tt_ci_char_op(label_m[0],==, 'm');
  1881. /* ignore exact spelling of "minute(s)," and "second(s)" */
  1882. format_time_interval(dbuf, sizeof(dbuf), 60*2 + 1);
  1883. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1884. r = tor_sscanf(dbuf, TL_ " " TL_,
  1885. &min, label_m, &sec, label_s);
  1886. tt_int_op(r,==, 4);
  1887. tt_int_op(min,==, 2);
  1888. tt_ci_char_op(label_m[0],==, 'm');
  1889. tt_int_op(sec,==, 1);
  1890. tt_ci_char_op(label_s[0],==, 's');
  1891. format_time_interval(dbuf, sizeof(dbuf), 60*60 - 1);
  1892. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1893. r = tor_sscanf(dbuf, TL_ " " TL_,
  1894. &min, label_m, &sec, label_s);
  1895. tt_int_op(r,==, 4);
  1896. tt_int_op(min,==, 59);
  1897. tt_ci_char_op(label_m[0],==, 'm');
  1898. tt_int_op(sec,==, 59);
  1899. tt_ci_char_op(label_s[0],==, 's');
  1900. /* negative minutes are reported as their absolute value */
  1901. /* ignore trailing "0 second(s)", if present */
  1902. format_time_interval(dbuf, sizeof(dbuf), -3*60);
  1903. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1904. r = tor_sscanf(dbuf, TL_, &min, label_m);
  1905. tt_int_op(r,==, 2);
  1906. tt_int_op(min,==, 3);
  1907. tt_ci_char_op(label_m[0],==, 'm');
  1908. /* ignore exact spelling of "minute(s)," and "second(s)" */
  1909. format_time_interval(dbuf, sizeof(dbuf), -96);
  1910. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1911. r = tor_sscanf(dbuf, TL_ " " TL_,
  1912. &min, label_m, &sec, label_s);
  1913. tt_int_op(r,==, 4);
  1914. tt_int_op(min,==, 1);
  1915. tt_ci_char_op(label_m[0],==, 'm');
  1916. tt_int_op(sec,==, 36);
  1917. tt_ci_char_op(label_s[0],==, 's');
  1918. format_time_interval(dbuf, sizeof(dbuf), -2815);
  1919. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1920. r = tor_sscanf(dbuf, TL_ " " TL_,
  1921. &min, label_m, &sec, label_s);
  1922. tt_int_op(r,==, 4);
  1923. tt_int_op(min,==, 46);
  1924. tt_ci_char_op(label_m[0],==, 'm');
  1925. tt_int_op(sec,==, 55);
  1926. tt_ci_char_op(label_s[0],==, 's');
  1927. /* hours: 1:00, 1:00:01, 1:01, 23:59, 23:59:59 */
  1928. /* always ignore trailing seconds, if present */
  1929. /* ignore trailing "0 minute(s)" etc., if present */
  1930. format_time_interval(dbuf, sizeof(dbuf), 60*60);
  1931. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1932. r = tor_sscanf(dbuf, TL_, &hour, label_h);
  1933. tt_int_op(r,==, 2);
  1934. tt_int_op(hour,==, 1);
  1935. tt_ci_char_op(label_h[0],==, 'h');
  1936. format_time_interval(dbuf, sizeof(dbuf), 60*60 + 1);
  1937. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1938. r = tor_sscanf(dbuf, TL_, &hour, label_h);
  1939. tt_int_op(r,==, 2);
  1940. tt_int_op(hour,==, 1);
  1941. tt_ci_char_op(label_h[0],==, 'h');
  1942. /* ignore exact spelling of "hour(s)," etc. */
  1943. format_time_interval(dbuf, sizeof(dbuf), 60*60 + 60);
  1944. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1945. r = tor_sscanf(dbuf, TL_ " " TL_,
  1946. &hour, label_h, &min, label_m);
  1947. tt_int_op(r,==, 4);
  1948. tt_int_op(hour,==, 1);
  1949. tt_ci_char_op(label_h[0],==, 'h');
  1950. tt_int_op(min,==, 1);
  1951. tt_ci_char_op(label_m[0],==, 'm');
  1952. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 - 60);
  1953. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1954. r = tor_sscanf(dbuf, TL_ " " TL_,
  1955. &hour, label_h, &min, label_m);
  1956. tt_int_op(r,==, 4);
  1957. tt_int_op(hour,==, 23);
  1958. tt_ci_char_op(label_h[0],==, 'h');
  1959. tt_int_op(min,==, 59);
  1960. tt_ci_char_op(label_m[0],==, 'm');
  1961. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 - 1);
  1962. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1963. r = tor_sscanf(dbuf, TL_ " " TL_,
  1964. &hour, label_h, &min, label_m);
  1965. tt_int_op(r,==, 4);
  1966. tt_int_op(hour,==, 23);
  1967. tt_ci_char_op(label_h[0],==, 'h');
  1968. tt_int_op(min,==, 59);
  1969. tt_ci_char_op(label_m[0],==, 'm');
  1970. /* negative hours are reported as their absolute value */
  1971. /* ignore exact spelling of "hour(s)," etc., if present */
  1972. format_time_interval(dbuf, sizeof(dbuf), -2*60*60);
  1973. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1974. r = tor_sscanf(dbuf, TL_, &hour, label_h);
  1975. tt_int_op(r,==, 2);
  1976. tt_int_op(hour,==, 2);
  1977. tt_ci_char_op(label_h[0],==, 'h');
  1978. format_time_interval(dbuf, sizeof(dbuf), -75804);
  1979. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1980. r = tor_sscanf(dbuf, TL_ " " TL_,
  1981. &hour, label_h, &min, label_m);
  1982. tt_int_op(r,==, 4);
  1983. tt_int_op(hour,==, 21);
  1984. tt_ci_char_op(label_h[0],==, 'h');
  1985. tt_int_op(min,==, 3);
  1986. tt_ci_char_op(label_m[0],==, 'm');
  1987. /* days: 1:00, 1:00:00:01, 1:00:01, 1:01 */
  1988. /* always ignore trailing seconds, if present */
  1989. /* ignore trailing "0 hours(s)" etc., if present */
  1990. format_time_interval(dbuf, sizeof(dbuf), 24*60*60);
  1991. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1992. r = tor_sscanf(dbuf, TL_, &day, label_d);
  1993. tt_int_op(r,==, 2);
  1994. tt_int_op(day,==, 1);
  1995. tt_ci_char_op(label_d[0],==, 'd');
  1996. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 1);
  1997. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  1998. r = tor_sscanf(dbuf, TL_, &day, label_d);
  1999. tt_int_op(r,==, 2);
  2000. tt_int_op(day,==, 1);
  2001. tt_ci_char_op(label_d[0],==, 'd');
  2002. /* ignore exact spelling of "days(s)," etc. */
  2003. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 60);
  2004. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2005. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2006. &day, label_d, &hour, label_h, &min, label_m);
  2007. if (r == -1) {
  2008. /* ignore 0 hours(s), if present */
  2009. r = tor_sscanf(dbuf, TL_ " " TL_,
  2010. &day, label_d, &min, label_m);
  2011. }
  2012. tt_assert(r == 4 || r == 6);
  2013. tt_int_op(day,==, 1);
  2014. tt_ci_char_op(label_d[0],==, 'd');
  2015. if (r == 6) {
  2016. tt_int_op(hour,==, 0);
  2017. tt_ci_char_op(label_h[0],==, 'h');
  2018. }
  2019. tt_int_op(min,==, 1);
  2020. tt_ci_char_op(label_m[0],==, 'm');
  2021. /* ignore trailing "0 minutes(s)" etc., if present */
  2022. format_time_interval(dbuf, sizeof(dbuf), 24*60*60 + 60*60);
  2023. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2024. r = tor_sscanf(dbuf, TL_ " " TL_,
  2025. &day, label_d, &hour, label_h);
  2026. tt_int_op(r,==, 4);
  2027. tt_int_op(day,==, 1);
  2028. tt_ci_char_op(label_d[0],==, 'd');
  2029. tt_int_op(hour,==, 1);
  2030. tt_ci_char_op(label_h[0],==, 'h');
  2031. /* negative days are reported as their absolute value */
  2032. format_time_interval(dbuf, sizeof(dbuf), -21936184);
  2033. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2034. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2035. &day, label_d, &hour, label_h, &min, label_m);
  2036. tt_int_op(r,==, 6);
  2037. tt_int_op(day,==, 253);
  2038. tt_ci_char_op(label_d[0],==, 'd');
  2039. tt_int_op(hour,==, 21);
  2040. tt_ci_char_op(label_h[0],==, 'h');
  2041. tt_int_op(min,==, 23);
  2042. tt_ci_char_op(label_m[0],==, 'm');
  2043. /* periods > 1 year are reported in days (warn?) */
  2044. /* ignore exact spelling of "days(s)," etc., if present */
  2045. format_time_interval(dbuf, sizeof(dbuf), 758635154);
  2046. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2047. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2048. &day, label_d, &hour, label_h, &min, label_m);
  2049. tt_int_op(r,==, 6);
  2050. tt_int_op(day,==, 8780);
  2051. tt_ci_char_op(label_d[0],==, 'd');
  2052. tt_int_op(hour,==, 11);
  2053. tt_ci_char_op(label_h[0],==, 'h');
  2054. tt_int_op(min,==, 59);
  2055. tt_ci_char_op(label_m[0],==, 'm');
  2056. /* negative periods > 1 year are reported in days (warn?) */
  2057. format_time_interval(dbuf, sizeof(dbuf), -1427014922);
  2058. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2059. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2060. &day, label_d, &hour, label_h, &min, label_m);
  2061. tt_int_op(r,==, 6);
  2062. tt_int_op(day,==, 16516);
  2063. tt_ci_char_op(label_d[0],==, 'd');
  2064. tt_int_op(hour,==, 9);
  2065. tt_ci_char_op(label_h[0],==, 'h');
  2066. tt_int_op(min,==, 2);
  2067. tt_ci_char_op(label_m[0],==, 'm');
  2068. #if SIZEOF_LONG == 4 || SIZEOF_LONG == 8
  2069. /* We can try INT32_MIN/MAX */
  2070. /* Always ignore second(s) */
  2071. /* INT32_MAX */
  2072. format_time_interval(dbuf, sizeof(dbuf), 2147483647);
  2073. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2074. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2075. &day, label_d, &hour, label_h, &min, label_m);
  2076. tt_int_op(r,==, 6);
  2077. tt_int_op(day,==, 24855);
  2078. tt_ci_char_op(label_d[0],==, 'd');
  2079. tt_int_op(hour,==, 3);
  2080. tt_ci_char_op(label_h[0],==, 'h');
  2081. tt_int_op(min,==, 14);
  2082. tt_ci_char_op(label_m[0],==, 'm');
  2083. /* and 7 seconds - ignored */
  2084. /* INT32_MIN: check that we get the absolute value of interval,
  2085. * which doesn't actually fit in int32_t.
  2086. * We expect INT32_MAX or INT32_MAX + 1 with 64 bit longs */
  2087. format_time_interval(dbuf, sizeof(dbuf), -2147483647L - 1L);
  2088. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2089. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2090. &day, label_d, &hour, label_h, &min, label_m);
  2091. tt_int_op(r,==, 6);
  2092. tt_int_op(day,==, 24855);
  2093. tt_ci_char_op(label_d[0],==, 'd');
  2094. tt_int_op(hour,==, 3);
  2095. tt_ci_char_op(label_h[0],==, 'h');
  2096. tt_int_op(min,==, 14);
  2097. tt_ci_char_op(label_m[0],==, 'm');
  2098. /* and 7 or 8 seconds - ignored */
  2099. #endif
  2100. #if SIZEOF_LONG == 8
  2101. /* We can try INT64_MIN/MAX */
  2102. /* Always ignore second(s) */
  2103. /* INT64_MAX */
  2104. format_time_interval(dbuf, sizeof(dbuf), 9223372036854775807L);
  2105. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2106. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2107. &day, label_d, &hour, label_h, &min, label_m);
  2108. tt_int_op(r,==, 6);
  2109. tt_int_op(day,==, 106751991167300L);
  2110. tt_ci_char_op(label_d[0],==, 'd');
  2111. tt_int_op(hour,==, 15);
  2112. tt_ci_char_op(label_h[0],==, 'h');
  2113. tt_int_op(min,==, 30);
  2114. tt_ci_char_op(label_m[0],==, 'm');
  2115. /* and 7 seconds - ignored */
  2116. /* INT64_MIN: check that we get the absolute value of interval,
  2117. * which doesn't actually fit in int64_t.
  2118. * We expect INT64_MAX */
  2119. format_time_interval(dbuf, sizeof(dbuf),
  2120. -9223372036854775807L - 1L);
  2121. tt_int_op(strnlen(dbuf, DBUF_SIZE),<=, DBUF_SIZE - 1);
  2122. r = tor_sscanf(dbuf, TL_ " " TL_ " " TL_,
  2123. &day, label_d, &hour, label_h, &min, label_m);
  2124. tt_int_op(r,==, 6);
  2125. tt_int_op(day,==, 106751991167300L);
  2126. tt_ci_char_op(label_d[0],==, 'd');
  2127. tt_int_op(hour,==, 15);
  2128. tt_ci_char_op(label_h[0],==, 'h');
  2129. tt_int_op(min,==, 30);
  2130. tt_ci_char_op(label_m[0],==, 'm');
  2131. /* and 7 or 8 seconds - ignored */
  2132. #endif
  2133. done:
  2134. ;
  2135. }
  2136. #undef tt_char_op
  2137. #undef tt_ci_char_op
  2138. #undef DBUF_SIZE
  2139. #undef T_
  2140. #undef LABEL_SIZE
  2141. #undef L_
  2142. #undef TL_
  2143. static void
  2144. test_util_path_is_relative(void *arg)
  2145. {
  2146. /* OS-independent tests */
  2147. (void)arg;
  2148. tt_int_op(1,==, path_is_relative(""));
  2149. tt_int_op(1,==, path_is_relative("dir"));
  2150. tt_int_op(1,==, path_is_relative("dir/"));
  2151. tt_int_op(1,==, path_is_relative("./dir"));
  2152. tt_int_op(1,==, path_is_relative("../dir"));
  2153. tt_int_op(0,==, path_is_relative("/"));
  2154. tt_int_op(0,==, path_is_relative("/dir"));
  2155. tt_int_op(0,==, path_is_relative("/dir/"));
  2156. /* Windows */
  2157. #ifdef _WIN32
  2158. /* I don't have Windows so I can't test this, hence the "#ifdef
  2159. 0". These are tests that look useful, so please try to get them
  2160. running and uncomment if it all works as it should */
  2161. tt_int_op(1,==, path_is_relative("dir"));
  2162. tt_int_op(1,==, path_is_relative("dir\\"));
  2163. tt_int_op(1,==, path_is_relative("dir\\a:"));
  2164. tt_int_op(1,==, path_is_relative("dir\\a:\\"));
  2165. tt_int_op(1,==, path_is_relative("http:\\dir"));
  2166. tt_int_op(0,==, path_is_relative("\\dir"));
  2167. tt_int_op(0,==, path_is_relative("a:\\dir"));
  2168. tt_int_op(0,==, path_is_relative("z:\\dir"));
  2169. #endif
  2170. done:
  2171. ;
  2172. }
  2173. #ifdef ENABLE_MEMPOOLS
  2174. /** Run unittests for memory pool allocator */
  2175. static void
  2176. test_util_mempool(void *arg)
  2177. {
  2178. mp_pool_t *pool = NULL;
  2179. smartlist_t *allocated = NULL;
  2180. int i;
  2181. (void)arg;
  2182. pool = mp_pool_new(1, 100);
  2183. tt_assert(pool);
  2184. tt_assert(pool->new_chunk_capacity >= 100);
  2185. tt_assert(pool->item_alloc_size >= sizeof(void*)+1);
  2186. mp_pool_destroy(pool);
  2187. pool = NULL;
  2188. pool = mp_pool_new(241, 2500);
  2189. tt_assert(pool);
  2190. tt_assert(pool->new_chunk_capacity >= 10);
  2191. tt_assert(pool->item_alloc_size >= sizeof(void*)+241);
  2192. tt_int_op(pool->item_alloc_size & 0x03,==, 0);
  2193. tt_assert(pool->new_chunk_capacity < 60);
  2194. allocated = smartlist_new();
  2195. for (i = 0; i < 20000; ++i) {
  2196. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  2197. void *m = mp_pool_get(pool);
  2198. memset(m, 0x09, 241);
  2199. smartlist_add(allocated, m);
  2200. //printf("%d: %p\n", i, m);
  2201. //mp_pool_assert_ok(pool);
  2202. } else {
  2203. int idx = crypto_rand_int(smartlist_len(allocated));
  2204. void *m = smartlist_get(allocated, idx);
  2205. //printf("%d: free %p\n", i, m);
  2206. smartlist_del(allocated, idx);
  2207. mp_pool_release(m);
  2208. //mp_pool_assert_ok(pool);
  2209. }
  2210. if (crypto_rand_int(777)==0)
  2211. mp_pool_clean(pool, 1, 1);
  2212. if (i % 777)
  2213. mp_pool_assert_ok(pool);
  2214. }
  2215. done:
  2216. if (allocated) {
  2217. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  2218. mp_pool_assert_ok(pool);
  2219. mp_pool_clean(pool, 0, 0);
  2220. mp_pool_assert_ok(pool);
  2221. smartlist_free(allocated);
  2222. }
  2223. if (pool)
  2224. mp_pool_destroy(pool);
  2225. }
  2226. #endif /* ENABLE_MEMPOOLS */
  2227. /** Run unittests for memory area allocator */
  2228. static void
  2229. test_util_memarea(void *arg)
  2230. {
  2231. memarea_t *area = memarea_new();
  2232. char *p1, *p2, *p3, *p1_orig;
  2233. void *malloced_ptr = NULL;
  2234. int i;
  2235. (void)arg;
  2236. tt_assert(area);
  2237. p1_orig = p1 = memarea_alloc(area,64);
  2238. p2 = memarea_alloc_zero(area,52);
  2239. p3 = memarea_alloc(area,11);
  2240. tt_assert(memarea_owns_ptr(area, p1));
  2241. tt_assert(memarea_owns_ptr(area, p2));
  2242. tt_assert(memarea_owns_ptr(area, p3));
  2243. /* Make sure we left enough space. */
  2244. tt_assert(p1+64 <= p2);
  2245. tt_assert(p2+52 <= p3);
  2246. /* Make sure we aligned. */
  2247. tt_int_op(((uintptr_t)p1) % sizeof(void*),==, 0);
  2248. tt_int_op(((uintptr_t)p2) % sizeof(void*),==, 0);
  2249. tt_int_op(((uintptr_t)p3) % sizeof(void*),==, 0);
  2250. tt_assert(!memarea_owns_ptr(area, p3+8192));
  2251. tt_assert(!memarea_owns_ptr(area, p3+30));
  2252. tt_assert(tor_mem_is_zero(p2, 52));
  2253. /* Make sure we don't overalign. */
  2254. p1 = memarea_alloc(area, 1);
  2255. p2 = memarea_alloc(area, 1);
  2256. tt_ptr_op(p1+sizeof(void*),==, p2);
  2257. {
  2258. malloced_ptr = tor_malloc(64);
  2259. tt_assert(!memarea_owns_ptr(area, malloced_ptr));
  2260. tor_free(malloced_ptr);
  2261. }
  2262. /* memarea_memdup */
  2263. {
  2264. malloced_ptr = tor_malloc(64);
  2265. crypto_rand((char*)malloced_ptr, 64);
  2266. p1 = memarea_memdup(area, malloced_ptr, 64);
  2267. tt_assert(p1 != malloced_ptr);
  2268. tt_mem_op(p1,==, malloced_ptr, 64);
  2269. tor_free(malloced_ptr);
  2270. }
  2271. /* memarea_strdup. */
  2272. p1 = memarea_strdup(area,"");
  2273. p2 = memarea_strdup(area, "abcd");
  2274. tt_assert(p1);
  2275. tt_assert(p2);
  2276. tt_str_op(p1,==, "");
  2277. tt_str_op(p2,==, "abcd");
  2278. /* memarea_strndup. */
  2279. {
  2280. const char *s = "Ad ogni porta batte la morte e grida: il nome!";
  2281. /* (From Turandot, act 3.) */
  2282. size_t len = strlen(s);
  2283. p1 = memarea_strndup(area, s, 1000);
  2284. p2 = memarea_strndup(area, s, 10);
  2285. tt_str_op(p1,==, s);
  2286. tt_assert(p2 >= p1 + len + 1);
  2287. tt_mem_op(s,==, p2, 10);
  2288. tt_int_op(p2[10],==, '\0');
  2289. p3 = memarea_strndup(area, s, len);
  2290. tt_str_op(p3,==, s);
  2291. p3 = memarea_strndup(area, s, len-1);
  2292. tt_mem_op(s,==, p3, len-1);
  2293. tt_int_op(p3[len-1],==, '\0');
  2294. }
  2295. memarea_clear(area);
  2296. p1 = memarea_alloc(area, 1);
  2297. tt_ptr_op(p1,==, p1_orig);
  2298. memarea_clear(area);
  2299. /* Check for running over an area's size. */
  2300. for (i = 0; i < 512; ++i) {
  2301. p1 = memarea_alloc(area, crypto_rand_int(5)+1);
  2302. tt_assert(memarea_owns_ptr(area, p1));
  2303. }
  2304. memarea_assert_ok(area);
  2305. /* Make sure we can allocate a too-big object. */
  2306. p1 = memarea_alloc_zero(area, 9000);
  2307. p2 = memarea_alloc_zero(area, 16);
  2308. tt_assert(memarea_owns_ptr(area, p1));
  2309. tt_assert(memarea_owns_ptr(area, p2));
  2310. done:
  2311. memarea_drop_all(area);
  2312. tor_free(malloced_ptr);
  2313. }
  2314. /** Run unit tests for utility functions to get file names relative to
  2315. * the data directory. */
  2316. static void
  2317. test_util_datadir(void *arg)
  2318. {
  2319. char buf[1024];
  2320. char *f = NULL;
  2321. char *temp_dir = NULL;
  2322. (void)arg;
  2323. temp_dir = get_datadir_fname(NULL);
  2324. f = get_datadir_fname("state");
  2325. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
  2326. tt_str_op(f,==, buf);
  2327. tor_free(f);
  2328. f = get_datadir_fname2("cache", "thingy");
  2329. tor_snprintf(buf, sizeof(buf),
  2330. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
  2331. tt_str_op(f,==, buf);
  2332. tor_free(f);
  2333. f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
  2334. tor_snprintf(buf, sizeof(buf),
  2335. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
  2336. tt_str_op(f,==, buf);
  2337. tor_free(f);
  2338. f = get_datadir_fname_suffix("cache", ".foo");
  2339. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
  2340. temp_dir);
  2341. tt_str_op(f,==, buf);
  2342. done:
  2343. tor_free(f);
  2344. tor_free(temp_dir);
  2345. }
  2346. static void
  2347. test_util_strtok(void *arg)
  2348. {
  2349. char buf[128];
  2350. char buf2[128];
  2351. int i;
  2352. char *cp1, *cp2;
  2353. (void)arg;
  2354. for (i = 0; i < 3; i++) {
  2355. const char *pad1="", *pad2="";
  2356. switch (i) {
  2357. case 0:
  2358. break;
  2359. case 1:
  2360. pad1 = " ";
  2361. pad2 = "!";
  2362. break;
  2363. case 2:
  2364. pad1 = " ";
  2365. pad2 = ";!";
  2366. break;
  2367. }
  2368. tor_snprintf(buf, sizeof(buf), "%s", pad1);
  2369. tor_snprintf(buf2, sizeof(buf2), "%s", pad2);
  2370. tt_assert(NULL == tor_strtok_r_impl(buf, " ", &cp1));
  2371. tt_assert(NULL == tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  2372. tor_snprintf(buf, sizeof(buf),
  2373. "%sGraved on the dark in gestures of descent%s", pad1, pad1);
  2374. tor_snprintf(buf2, sizeof(buf2),
  2375. "%sthey.seemed;;their!.own;most.perfect;monument%s",pad2,pad2);
  2376. /* -- "Year's End", Richard Wilbur */
  2377. tt_str_op("Graved",==, tor_strtok_r_impl(buf, " ", &cp1));
  2378. tt_str_op("they",==, tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  2379. #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
  2380. #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
  2381. tt_str_op("on",==, S1());
  2382. tt_str_op("the",==, S1());
  2383. tt_str_op("dark",==, S1());
  2384. tt_str_op("seemed",==, S2());
  2385. tt_str_op("their",==, S2());
  2386. tt_str_op("own",==, S2());
  2387. tt_str_op("in",==, S1());
  2388. tt_str_op("gestures",==, S1());
  2389. tt_str_op("of",==, S1());
  2390. tt_str_op("most",==, S2());
  2391. tt_str_op("perfect",==, S2());
  2392. tt_str_op("descent",==, S1());
  2393. tt_str_op("monument",==, S2());
  2394. tt_ptr_op(NULL,==, S1());
  2395. tt_ptr_op(NULL,==, S2());
  2396. }
  2397. buf[0] = 0;
  2398. tt_ptr_op(NULL,==, tor_strtok_r_impl(buf, " ", &cp1));
  2399. tt_ptr_op(NULL,==, tor_strtok_r_impl(buf, "!", &cp1));
  2400. strlcpy(buf, "Howdy!", sizeof(buf));
  2401. tt_str_op("Howdy",==, tor_strtok_r_impl(buf, "!", &cp1));
  2402. tt_ptr_op(NULL,==, tor_strtok_r_impl(NULL, "!", &cp1));
  2403. strlcpy(buf, " ", sizeof(buf));
  2404. tt_ptr_op(NULL,==, tor_strtok_r_impl(buf, " ", &cp1));
  2405. strlcpy(buf, " ", sizeof(buf));
  2406. tt_ptr_op(NULL,==, tor_strtok_r_impl(buf, " ", &cp1));
  2407. strlcpy(buf, "something ", sizeof(buf));
  2408. tt_str_op("something",==, tor_strtok_r_impl(buf, " ", &cp1));
  2409. tt_ptr_op(NULL,==, tor_strtok_r_impl(NULL, ";", &cp1));
  2410. done:
  2411. ;
  2412. }
  2413. static void
  2414. test_util_find_str_at_start_of_line(void *ptr)
  2415. {
  2416. const char *long_string =
  2417. "howdy world. how are you? i hope it's fine.\n"
  2418. "hello kitty\n"
  2419. "third line";
  2420. char *line2 = strchr(long_string,'\n')+1;
  2421. char *line3 = strchr(line2,'\n')+1;
  2422. const char *short_string = "hello kitty\n"
  2423. "second line\n";
  2424. char *short_line2 = strchr(short_string,'\n')+1;
  2425. (void)ptr;
  2426. tt_ptr_op(long_string,==, find_str_at_start_of_line(long_string, ""));
  2427. tt_ptr_op(NULL,==, find_str_at_start_of_line(short_string, "nonsense"));
  2428. tt_ptr_op(NULL,==, find_str_at_start_of_line(long_string, "nonsense"));
  2429. tt_ptr_op(NULL,==, find_str_at_start_of_line(long_string, "\n"));
  2430. tt_ptr_op(NULL,==, find_str_at_start_of_line(long_string, "how "));
  2431. tt_ptr_op(NULL,==, find_str_at_start_of_line(long_string, "kitty"));
  2432. tt_ptr_op(long_string,==, find_str_at_start_of_line(long_string, "h"));
  2433. tt_ptr_op(long_string,==, find_str_at_start_of_line(long_string, "how"));
  2434. tt_ptr_op(line2,==, find_str_at_start_of_line(long_string, "he"));
  2435. tt_ptr_op(line2,==, find_str_at_start_of_line(long_string, "hell"));
  2436. tt_ptr_op(line2,==, find_str_at_start_of_line(long_string, "hello k"));
  2437. tt_ptr_op(line2,==, find_str_at_start_of_line(long_string, "hello kitty\n"));
  2438. tt_ptr_op(line2,==,
  2439. find_str_at_start_of_line(long_string, "hello kitty\nt"));
  2440. tt_ptr_op(line3,==, find_str_at_start_of_line(long_string, "third"));
  2441. tt_ptr_op(line3,==, find_str_at_start_of_line(long_string, "third line"));
  2442. tt_ptr_op(NULL,==, find_str_at_start_of_line(long_string, "third line\n"));
  2443. tt_ptr_op(short_line2,==, find_str_at_start_of_line(short_string,
  2444. "second line\n"));
  2445. done:
  2446. ;
  2447. }
  2448. static void
  2449. test_util_string_is_C_identifier(void *ptr)
  2450. {
  2451. (void)ptr;
  2452. tt_int_op(1,==, string_is_C_identifier("string_is_C_identifier"));
  2453. tt_int_op(1,==, string_is_C_identifier("_string_is_C_identifier"));
  2454. tt_int_op(1,==, string_is_C_identifier("_"));
  2455. tt_int_op(1,==, string_is_C_identifier("i"));
  2456. tt_int_op(1,==, string_is_C_identifier("_____"));
  2457. tt_int_op(1,==, string_is_C_identifier("__00__"));
  2458. tt_int_op(1,==, string_is_C_identifier("__init__"));
  2459. tt_int_op(1,==, string_is_C_identifier("_0"));
  2460. tt_int_op(1,==, string_is_C_identifier("_0string_is_C_identifier"));
  2461. tt_int_op(1,==, string_is_C_identifier("_0"));
  2462. tt_int_op(0,==, string_is_C_identifier("0_string_is_C_identifier"));
  2463. tt_int_op(0,==, string_is_C_identifier("0"));
  2464. tt_int_op(0,==, string_is_C_identifier(""));
  2465. tt_int_op(0,==, string_is_C_identifier(";"));
  2466. tt_int_op(0,==, string_is_C_identifier("i;"));
  2467. tt_int_op(0,==, string_is_C_identifier("_;"));
  2468. tt_int_op(0,==, string_is_C_identifier("í"));
  2469. tt_int_op(0,==, string_is_C_identifier("ñ"));
  2470. done:
  2471. ;
  2472. }
  2473. static void
  2474. test_util_asprintf(void *ptr)
  2475. {
  2476. #define LOREMIPSUM \
  2477. "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
  2478. char *cp=NULL, *cp2=NULL;
  2479. int r;
  2480. (void)ptr;
  2481. /* simple string */
  2482. r = tor_asprintf(&cp, "simple string 100%% safe");
  2483. tt_assert(cp);
  2484. tt_str_op("simple string 100% safe",==, cp);
  2485. tt_int_op(strlen(cp),==, r);
  2486. tor_free(cp);
  2487. /* empty string */
  2488. r = tor_asprintf(&cp, "%s", "");
  2489. tt_assert(cp);
  2490. tt_str_op("",==, cp);
  2491. tt_int_op(strlen(cp),==, r);
  2492. tor_free(cp);
  2493. /* numbers (%i) */
  2494. r = tor_asprintf(&cp, "I like numbers-%2i, %i, etc.", -1, 2);
  2495. tt_assert(cp);
  2496. tt_str_op("I like numbers--1, 2, etc.",==, cp);
  2497. tt_int_op(strlen(cp),==, r);
  2498. /* don't free cp; next test uses it. */
  2499. /* numbers (%d) */
  2500. r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
  2501. tt_assert(cp2);
  2502. tt_int_op(strlen(cp2),==, r);
  2503. tt_str_op("First=101, Second=202",==, cp2);
  2504. tt_assert(cp != cp2);
  2505. tor_free(cp);
  2506. tor_free(cp2);
  2507. /* Glass-box test: a string exactly 128 characters long. */
  2508. r = tor_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
  2509. tt_assert(cp);
  2510. tt_int_op(128,==, r);
  2511. tt_int_op(cp[128], ==, '\0');
  2512. tt_str_op("Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM,==, cp);
  2513. tor_free(cp);
  2514. /* String longer than 128 characters */
  2515. r = tor_asprintf(&cp, "1: %s 2: %s 3: %s",
  2516. LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
  2517. tt_assert(cp);
  2518. tt_int_op(strlen(cp),==, r);
  2519. tt_str_op("1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM,==, cp);
  2520. done:
  2521. tor_free(cp);
  2522. tor_free(cp2);
  2523. }
  2524. static void
  2525. test_util_listdir(void *ptr)
  2526. {
  2527. smartlist_t *dir_contents = NULL;
  2528. char *fname1=NULL, *fname2=NULL, *fname3=NULL, *dir1=NULL, *dirname=NULL;
  2529. int r;
  2530. (void)ptr;
  2531. fname1 = tor_strdup(get_fname("hopscotch"));
  2532. fname2 = tor_strdup(get_fname("mumblety-peg"));
  2533. fname3 = tor_strdup(get_fname(".hidden-file"));
  2534. dir1 = tor_strdup(get_fname("some-directory"));
  2535. dirname = tor_strdup(get_fname(NULL));
  2536. tt_int_op(0,==, write_str_to_file(fname1, "X\n", 0));
  2537. tt_int_op(0,==, write_str_to_file(fname2, "Y\n", 0));
  2538. tt_int_op(0,==, write_str_to_file(fname3, "Z\n", 0));
  2539. #ifdef _WIN32
  2540. r = mkdir(dir1);
  2541. #else
  2542. r = mkdir(dir1, 0700);
  2543. #endif
  2544. if (r) {
  2545. fprintf(stderr, "Can't create directory %s:", dir1);
  2546. perror("");
  2547. exit(1);
  2548. }
  2549. dir_contents = tor_listdir(dirname);
  2550. tt_assert(dir_contents);
  2551. /* make sure that each filename is listed. */
  2552. tt_assert(smartlist_contains_string_case(dir_contents, "hopscotch"));
  2553. tt_assert(smartlist_contains_string_case(dir_contents, "mumblety-peg"));
  2554. tt_assert(smartlist_contains_string_case(dir_contents, ".hidden-file"));
  2555. tt_assert(smartlist_contains_string_case(dir_contents, "some-directory"));
  2556. tt_assert(!smartlist_contains_string(dir_contents, "."));
  2557. tt_assert(!smartlist_contains_string(dir_contents, ".."));
  2558. done:
  2559. tor_free(fname1);
  2560. tor_free(fname2);
  2561. tor_free(fname3);
  2562. tor_free(dir1);
  2563. tor_free(dirname);
  2564. if (dir_contents) {
  2565. SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp));
  2566. smartlist_free(dir_contents);
  2567. }
  2568. }
  2569. static void
  2570. test_util_parent_dir(void *ptr)
  2571. {
  2572. char *cp;
  2573. (void)ptr;
  2574. #define T(output,expect_ok,input) \
  2575. do { \
  2576. int ok; \
  2577. cp = tor_strdup(input); \
  2578. ok = get_parent_directory(cp); \
  2579. tt_int_op(expect_ok, ==, ok); \
  2580. if (ok==0) \
  2581. tt_str_op(output, ==, cp); \
  2582. tor_free(cp); \
  2583. } while (0);
  2584. T("/home/wombat", 0, "/home/wombat/knish");
  2585. T("/home/wombat", 0, "/home/wombat/knish/");
  2586. T("/home/wombat", 0, "/home/wombat/knish///");
  2587. T("./home/wombat", 0, "./home/wombat/knish/");
  2588. T("/", 0, "/home");
  2589. T("/", 0, "/home//");
  2590. T(".", 0, "./wombat");
  2591. T(".", 0, "./wombat/");
  2592. T(".", 0, "./wombat//");
  2593. T("wombat", 0, "wombat/foo");
  2594. T("wombat/..", 0, "wombat/../foo");
  2595. T("wombat/../", 0, "wombat/..//foo"); /* Is this correct? */
  2596. T("wombat/.", 0, "wombat/./foo");
  2597. T("wombat/./", 0, "wombat/.//foo"); /* Is this correct? */
  2598. T("wombat", 0, "wombat/..//");
  2599. T("wombat", 0, "wombat/foo/");
  2600. T("wombat", 0, "wombat/.foo");
  2601. T("wombat", 0, "wombat/.foo/");
  2602. T("wombat", -1, "");
  2603. T("w", -1, "");
  2604. T("wombat", 0, "wombat/knish");
  2605. T("/", 0, "/");
  2606. T("/", 0, "////");
  2607. done:
  2608. tor_free(cp);
  2609. }
  2610. static void
  2611. test_util_ftruncate(void *ptr)
  2612. {
  2613. char *buf = NULL;
  2614. const char *fname;
  2615. int fd = -1;
  2616. const char *message = "Hello world";
  2617. const char *message2 = "Hola mundo";
  2618. struct stat st;
  2619. (void) ptr;
  2620. fname = get_fname("ftruncate");
  2621. fd = tor_open_cloexec(fname, O_WRONLY|O_CREAT, 0600);
  2622. tt_int_op(fd, >=, 0);
  2623. /* Make the file be there. */
  2624. tt_int_op(strlen(message), ==, write_all(fd, message, strlen(message), 0));
  2625. tt_int_op((int)tor_fd_getpos(fd), ==, strlen(message));
  2626. tt_int_op(0, ==, fstat(fd, &st));
  2627. tt_int_op((int)st.st_size, ==, strlen(message));
  2628. /* Truncate and see if it got truncated */
  2629. tt_int_op(0, ==, tor_ftruncate(fd));
  2630. tt_int_op((int)tor_fd_getpos(fd), ==, 0);
  2631. tt_int_op(0, ==, fstat(fd, &st));
  2632. tt_int_op((int)st.st_size, ==, 0);
  2633. /* Replace, and see if it got replaced */
  2634. tt_int_op(strlen(message2), ==,
  2635. write_all(fd, message2, strlen(message2), 0));
  2636. tt_int_op((int)tor_fd_getpos(fd), ==, strlen(message2));
  2637. tt_int_op(0, ==, fstat(fd, &st));
  2638. tt_int_op((int)st.st_size, ==, strlen(message2));
  2639. close(fd);
  2640. fd = -1;
  2641. buf = read_file_to_str(fname, 0, NULL);
  2642. tt_str_op(message2, ==, buf);
  2643. done:
  2644. if (fd >= 0)
  2645. close(fd);
  2646. tor_free(buf);
  2647. }
  2648. #ifdef _WIN32
  2649. static void
  2650. test_util_load_win_lib(void *ptr)
  2651. {
  2652. HANDLE h = load_windows_system_library(_T("advapi32.dll"));
  2653. (void) ptr;
  2654. tt_assert(h);
  2655. done:
  2656. if (h)
  2657. FreeLibrary(h);
  2658. }
  2659. #endif
  2660. #ifndef _WIN32
  2661. static void
  2662. clear_hex_errno(char *hex_errno)
  2663. {
  2664. memset(hex_errno, '\0', HEX_ERRNO_SIZE + 1);
  2665. }
  2666. static void
  2667. test_util_exit_status(void *ptr)
  2668. {
  2669. /* Leave an extra byte for a \0 so we can do string comparison */
  2670. char hex_errno[HEX_ERRNO_SIZE + 1];
  2671. int n;
  2672. (void)ptr;
  2673. clear_hex_errno(hex_errno);
  2674. tt_str_op("",==, hex_errno);
  2675. clear_hex_errno(hex_errno);
  2676. n = format_helper_exit_status(0, 0, hex_errno);
  2677. tt_str_op("0/0\n",==, hex_errno);
  2678. tt_int_op(n,==, strlen(hex_errno));
  2679. #if SIZEOF_INT == 4
  2680. clear_hex_errno(hex_errno);
  2681. n = format_helper_exit_status(0, 0x7FFFFFFF, hex_errno);
  2682. tt_str_op("0/7FFFFFFF\n",==, hex_errno);
  2683. tt_int_op(n,==, strlen(hex_errno));
  2684. clear_hex_errno(hex_errno);
  2685. n = format_helper_exit_status(0xFF, -0x80000000, hex_errno);
  2686. tt_str_op("FF/-80000000\n",==, hex_errno);
  2687. tt_int_op(n,==, strlen(hex_errno));
  2688. tt_int_op(n,==, HEX_ERRNO_SIZE);
  2689. #elif SIZEOF_INT == 8
  2690. clear_hex_errno(hex_errno);
  2691. n = format_helper_exit_status(0, 0x7FFFFFFFFFFFFFFF, hex_errno);
  2692. tt_str_op("0/7FFFFFFFFFFFFFFF\n",==, hex_errno);
  2693. tt_int_op(n,==, strlen(hex_errno));
  2694. clear_hex_errno(hex_errno);
  2695. n = format_helper_exit_status(0xFF, -0x8000000000000000, hex_errno);
  2696. tt_str_op("FF/-8000000000000000\n",==, hex_errno);
  2697. tt_int_op(n,==, strlen(hex_errno));
  2698. tt_int_op(n,==, HEX_ERRNO_SIZE);
  2699. #endif
  2700. clear_hex_errno(hex_errno);
  2701. n = format_helper_exit_status(0x7F, 0, hex_errno);
  2702. tt_str_op("7F/0\n",==, hex_errno);
  2703. tt_int_op(n,==, strlen(hex_errno));
  2704. clear_hex_errno(hex_errno);
  2705. n = format_helper_exit_status(0x08, -0x242, hex_errno);
  2706. tt_str_op("8/-242\n",==, hex_errno);
  2707. tt_int_op(n,==, strlen(hex_errno));
  2708. clear_hex_errno(hex_errno);
  2709. tt_str_op("",==, hex_errno);
  2710. done:
  2711. ;
  2712. }
  2713. #endif
  2714. #ifndef _WIN32
  2715. /* Check that fgets with a non-blocking pipe returns partial lines and sets
  2716. * EAGAIN, returns full lines and sets no error, and returns NULL on EOF and
  2717. * sets no error */
  2718. static void
  2719. test_util_fgets_eagain(void *ptr)
  2720. {
  2721. int test_pipe[2] = {-1, -1};
  2722. int retval;
  2723. ssize_t retlen;
  2724. char *retptr;
  2725. FILE *test_stream = NULL;
  2726. char buf[4] = { 0 };
  2727. (void)ptr;
  2728. errno = 0;
  2729. /* Set up a pipe to test on */
  2730. retval = pipe(test_pipe);
  2731. tt_int_op(retval, ==, 0);
  2732. /* Set up the read-end to be non-blocking */
  2733. retval = fcntl(test_pipe[0], F_SETFL, O_NONBLOCK);
  2734. tt_int_op(retval, ==, 0);
  2735. /* Open it as a stdio stream */
  2736. test_stream = fdopen(test_pipe[0], "r");
  2737. tt_ptr_op(test_stream, !=, NULL);
  2738. /* Send in a partial line */
  2739. retlen = write(test_pipe[1], "A", 1);
  2740. tt_int_op(retlen, ==, 1);
  2741. retptr = fgets(buf, sizeof(buf), test_stream);
  2742. tt_int_op(errno, ==, EAGAIN);
  2743. tt_ptr_op(retptr, ==, buf);
  2744. tt_str_op(buf, ==, "A");
  2745. errno = 0;
  2746. /* Send in the rest */
  2747. retlen = write(test_pipe[1], "B\n", 2);
  2748. tt_int_op(retlen, ==, 2);
  2749. retptr = fgets(buf, sizeof(buf), test_stream);
  2750. tt_int_op(errno, ==, 0);
  2751. tt_ptr_op(retptr, ==, buf);
  2752. tt_str_op(buf, ==, "B\n");
  2753. errno = 0;
  2754. /* Send in a full line */
  2755. retlen = write(test_pipe[1], "CD\n", 3);
  2756. tt_int_op(retlen, ==, 3);
  2757. retptr = fgets(buf, sizeof(buf), test_stream);
  2758. tt_int_op(errno, ==, 0);
  2759. tt_ptr_op(retptr, ==, buf);
  2760. tt_str_op(buf, ==, "CD\n");
  2761. errno = 0;
  2762. /* Send in a partial line */
  2763. retlen = write(test_pipe[1], "E", 1);
  2764. tt_int_op(retlen, ==, 1);
  2765. retptr = fgets(buf, sizeof(buf), test_stream);
  2766. tt_int_op(errno, ==, EAGAIN);
  2767. tt_ptr_op(retptr, ==, buf);
  2768. tt_str_op(buf, ==, "E");
  2769. errno = 0;
  2770. /* Send in the rest */
  2771. retlen = write(test_pipe[1], "F\n", 2);
  2772. tt_int_op(retlen, ==, 2);
  2773. retptr = fgets(buf, sizeof(buf), test_stream);
  2774. tt_int_op(errno, ==, 0);
  2775. tt_ptr_op(retptr, ==, buf);
  2776. tt_str_op(buf, ==, "F\n");
  2777. errno = 0;
  2778. /* Send in a full line and close */
  2779. retlen = write(test_pipe[1], "GH", 2);
  2780. tt_int_op(retlen, ==, 2);
  2781. retval = close(test_pipe[1]);
  2782. tt_int_op(retval, ==, 0);
  2783. test_pipe[1] = -1;
  2784. retptr = fgets(buf, sizeof(buf), test_stream);
  2785. tt_int_op(errno, ==, 0);
  2786. tt_ptr_op(retptr, ==, buf);
  2787. tt_str_op(buf, ==, "GH");
  2788. errno = 0;
  2789. /* Check for EOF */
  2790. retptr = fgets(buf, sizeof(buf), test_stream);
  2791. tt_int_op(errno, ==, 0);
  2792. tt_ptr_op(retptr, ==, NULL);
  2793. retval = feof(test_stream);
  2794. tt_int_op(retval, !=, 0);
  2795. errno = 0;
  2796. /* Check that buf is unchanged according to C99 and C11 */
  2797. tt_str_op(buf, ==, "GH");
  2798. done:
  2799. if (test_stream != NULL)
  2800. fclose(test_stream);
  2801. if (test_pipe[0] != -1)
  2802. close(test_pipe[0]);
  2803. if (test_pipe[1] != -1)
  2804. close(test_pipe[1]);
  2805. }
  2806. #endif
  2807. #ifndef BUILDDIR
  2808. #define BUILDDIR "."
  2809. #endif
  2810. #ifdef _WIN32
  2811. #define notify_pending_waitpid_callbacks() STMT_NIL
  2812. #define TEST_CHILD "test-child.exe"
  2813. #define EOL "\r\n"
  2814. #else
  2815. #define TEST_CHILD (BUILDDIR "/src/test/test-child")
  2816. #define EOL "\n"
  2817. #endif
  2818. #ifdef _WIN32
  2819. /* I've assumed Windows doesn't have the gap between fork and exec
  2820. * that causes the race condition on unix-like platforms */
  2821. #define MATCH_PROCESS_STATUS(s1,s2) ((s1) == (s2))
  2822. #else
  2823. /* work around a race condition of the timing of SIGCHLD handler updates
  2824. * to the process_handle's fields, and checks of those fields
  2825. *
  2826. * TODO: Once we can signal failure to exec, change PROCESS_STATUS_RUNNING to
  2827. * PROCESS_STATUS_ERROR (and similarly with *_OR_NOTRUNNING) */
  2828. #define PROCESS_STATUS_RUNNING_OR_NOTRUNNING (PROCESS_STATUS_RUNNING+1)
  2829. #define IS_RUNNING_OR_NOTRUNNING(s) \
  2830. ((s) == PROCESS_STATUS_RUNNING || (s) == PROCESS_STATUS_NOTRUNNING)
  2831. /* well, this is ugly */
  2832. #define MATCH_PROCESS_STATUS(s1,s2) \
  2833. ( (s1) == (s2) \
  2834. ||((s1) == PROCESS_STATUS_RUNNING_OR_NOTRUNNING \
  2835. && IS_RUNNING_OR_NOTRUNNING(s2)) \
  2836. ||((s2) == PROCESS_STATUS_RUNNING_OR_NOTRUNNING \
  2837. && IS_RUNNING_OR_NOTRUNNING(s1)))
  2838. #endif // _WIN32
  2839. /** Helper function for testing tor_spawn_background */
  2840. static void
  2841. run_util_spawn_background(const char *argv[], const char *expected_out,
  2842. const char *expected_err, int expected_exit,
  2843. int expected_status)
  2844. {
  2845. int retval, exit_code;
  2846. ssize_t pos;
  2847. process_handle_t *process_handle=NULL;
  2848. char stdout_buf[100], stderr_buf[100];
  2849. int status;
  2850. /* Start the program */
  2851. #ifdef _WIN32
  2852. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  2853. #else
  2854. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  2855. #endif
  2856. notify_pending_waitpid_callbacks();
  2857. /* the race condition doesn't affect status,
  2858. * because status isn't updated by the SIGCHLD handler,
  2859. * but we still need to handle PROCESS_STATUS_RUNNING_OR_NOTRUNNING */
  2860. tt_assert(MATCH_PROCESS_STATUS(expected_status, status));
  2861. if (status == PROCESS_STATUS_ERROR) {
  2862. tt_ptr_op(process_handle, ==, NULL);
  2863. return;
  2864. }
  2865. tt_assert(process_handle != NULL);
  2866. /* When a spawned process forks, fails, then exits very quickly,
  2867. * (this typically occurs when exec fails)
  2868. * there is a race condition between the SIGCHLD handler
  2869. * updating the process_handle's fields, and this test
  2870. * checking the process status in those fields.
  2871. * The SIGCHLD update can occur before or after the code below executes.
  2872. * This causes intermittent failures in spawn_background_fail(),
  2873. * typically when the machine is under load.
  2874. * We use PROCESS_STATUS_RUNNING_OR_NOTRUNNING to avoid this issue. */
  2875. /* the race condition affects the change in
  2876. * process_handle->status from RUNNING to NOTRUNNING */
  2877. tt_assert(MATCH_PROCESS_STATUS(expected_status, process_handle->status));
  2878. #ifndef _WIN32
  2879. notify_pending_waitpid_callbacks();
  2880. /* the race condition affects the change in
  2881. * process_handle->waitpid_cb to NULL,
  2882. * so we skip the check if expected_status is ambiguous,
  2883. * that is, PROCESS_STATUS_RUNNING_OR_NOTRUNNING */
  2884. tt_assert(process_handle->waitpid_cb != NULL
  2885. || expected_status == PROCESS_STATUS_RUNNING_OR_NOTRUNNING);
  2886. #endif
  2887. #ifdef _WIN32
  2888. tt_assert(process_handle->stdout_pipe != INVALID_HANDLE_VALUE);
  2889. tt_assert(process_handle->stderr_pipe != INVALID_HANDLE_VALUE);
  2890. #else
  2891. tt_assert(process_handle->stdout_pipe >= 0);
  2892. tt_assert(process_handle->stderr_pipe >= 0);
  2893. #endif
  2894. /* Check stdout */
  2895. pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
  2896. sizeof(stdout_buf) - 1);
  2897. tt_assert(pos >= 0);
  2898. stdout_buf[pos] = '\0';
  2899. tt_int_op(strlen(expected_out),==, pos);
  2900. tt_str_op(expected_out,==, stdout_buf);
  2901. notify_pending_waitpid_callbacks();
  2902. /* Check it terminated correctly */
  2903. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  2904. tt_int_op(PROCESS_EXIT_EXITED,==, retval);
  2905. tt_int_op(expected_exit,==, exit_code);
  2906. // TODO: Make test-child exit with something other than 0
  2907. #ifndef _WIN32
  2908. notify_pending_waitpid_callbacks();
  2909. tt_ptr_op(process_handle->waitpid_cb, ==, NULL);
  2910. #endif
  2911. /* Check stderr */
  2912. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  2913. sizeof(stderr_buf) - 1);
  2914. tt_assert(pos >= 0);
  2915. stderr_buf[pos] = '\0';
  2916. tt_str_op(expected_err,==, stderr_buf);
  2917. tt_int_op(strlen(expected_err),==, pos);
  2918. notify_pending_waitpid_callbacks();
  2919. done:
  2920. if (process_handle)
  2921. tor_process_handle_destroy(process_handle, 1);
  2922. }
  2923. /** Check that we can launch a process and read the output */
  2924. static void
  2925. test_util_spawn_background_ok(void *ptr)
  2926. {
  2927. const char *argv[] = {TEST_CHILD, "--test", NULL};
  2928. const char *expected_out = "OUT"EOL "--test"EOL "SLEEPING"EOL "DONE" EOL;
  2929. const char *expected_err = "ERR"EOL;
  2930. (void)ptr;
  2931. run_util_spawn_background(argv, expected_out, expected_err, 0,
  2932. PROCESS_STATUS_RUNNING);
  2933. }
  2934. /** Check that failing to find the executable works as expected */
  2935. static void
  2936. test_util_spawn_background_fail(void *ptr)
  2937. {
  2938. const char *argv[] = {BUILDDIR "/src/test/no-such-file", "--test", NULL};
  2939. const char *expected_err = "";
  2940. char expected_out[1024];
  2941. char code[32];
  2942. #ifdef _WIN32
  2943. const int expected_status = PROCESS_STATUS_ERROR;
  2944. #else
  2945. /* TODO: Once we can signal failure to exec, set this to be
  2946. * PROCESS_STATUS_RUNNING_OR_ERROR */
  2947. const int expected_status = PROCESS_STATUS_RUNNING_OR_NOTRUNNING;
  2948. #endif
  2949. memset(expected_out, 0xf0, sizeof(expected_out));
  2950. memset(code, 0xf0, sizeof(code));
  2951. (void)ptr;
  2952. tor_snprintf(code, sizeof(code), "%x/%x",
  2953. 9 /* CHILD_STATE_FAILEXEC */ , ENOENT);
  2954. tor_snprintf(expected_out, sizeof(expected_out),
  2955. "ERR: Failed to spawn background process - code %s\n", code);
  2956. run_util_spawn_background(argv, expected_out, expected_err, 255,
  2957. expected_status);
  2958. }
  2959. /** Test that reading from a handle returns a partial read rather than
  2960. * blocking */
  2961. static void
  2962. test_util_spawn_background_partial_read_impl(int exit_early)
  2963. {
  2964. const int expected_exit = 0;
  2965. const int expected_status = PROCESS_STATUS_RUNNING;
  2966. int retval, exit_code;
  2967. ssize_t pos = -1;
  2968. process_handle_t *process_handle=NULL;
  2969. int status;
  2970. char stdout_buf[100], stderr_buf[100];
  2971. const char *argv[] = {TEST_CHILD, "--test", NULL};
  2972. const char *expected_out[] = { "OUT" EOL "--test" EOL "SLEEPING" EOL,
  2973. "DONE" EOL,
  2974. NULL };
  2975. const char *expected_err = "ERR" EOL;
  2976. #ifndef _WIN32
  2977. int eof = 0;
  2978. #endif
  2979. int expected_out_ctr;
  2980. if (exit_early) {
  2981. argv[1] = "--hang";
  2982. expected_out[0] = "OUT"EOL "--hang"EOL "SLEEPING" EOL;
  2983. }
  2984. /* Start the program */
  2985. #ifdef _WIN32
  2986. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  2987. #else
  2988. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  2989. #endif
  2990. tt_int_op(expected_status,==, status);
  2991. tt_assert(process_handle);
  2992. tt_int_op(expected_status,==, process_handle->status);
  2993. /* Check stdout */
  2994. for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
  2995. #ifdef _WIN32
  2996. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  2997. sizeof(stdout_buf) - 1, NULL);
  2998. #else
  2999. /* Check that we didn't read the end of file last time */
  3000. tt_assert(!eof);
  3001. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  3002. sizeof(stdout_buf) - 1, NULL, &eof);
  3003. #endif
  3004. log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
  3005. /* We would have blocked, keep on trying */
  3006. if (0 == pos)
  3007. continue;
  3008. tt_assert(pos > 0);
  3009. stdout_buf[pos] = '\0';
  3010. tt_str_op(expected_out[expected_out_ctr],==, stdout_buf);
  3011. tt_int_op(strlen(expected_out[expected_out_ctr]),==, pos);
  3012. expected_out_ctr++;
  3013. }
  3014. if (exit_early) {
  3015. tor_process_handle_destroy(process_handle, 1);
  3016. process_handle = NULL;
  3017. goto done;
  3018. }
  3019. /* The process should have exited without writing more */
  3020. #ifdef _WIN32
  3021. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  3022. sizeof(stdout_buf) - 1,
  3023. process_handle);
  3024. tt_int_op(0,==, pos);
  3025. #else
  3026. if (!eof) {
  3027. /* We should have got all the data, but maybe not the EOF flag */
  3028. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  3029. sizeof(stdout_buf) - 1,
  3030. process_handle, &eof);
  3031. tt_int_op(0,==, pos);
  3032. tt_assert(eof);
  3033. }
  3034. /* Otherwise, we got the EOF on the last read */
  3035. #endif
  3036. /* Check it terminated correctly */
  3037. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  3038. tt_int_op(PROCESS_EXIT_EXITED,==, retval);
  3039. tt_int_op(expected_exit,==, exit_code);
  3040. // TODO: Make test-child exit with something other than 0
  3041. /* Check stderr */
  3042. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  3043. sizeof(stderr_buf) - 1);
  3044. tt_assert(pos >= 0);
  3045. stderr_buf[pos] = '\0';
  3046. tt_str_op(expected_err,==, stderr_buf);
  3047. tt_int_op(strlen(expected_err),==, pos);
  3048. done:
  3049. tor_process_handle_destroy(process_handle, 1);
  3050. }
  3051. static void
  3052. test_util_spawn_background_partial_read(void *arg)
  3053. {
  3054. (void)arg;
  3055. test_util_spawn_background_partial_read_impl(0);
  3056. }
  3057. static void
  3058. test_util_spawn_background_exit_early(void *arg)
  3059. {
  3060. (void)arg;
  3061. test_util_spawn_background_partial_read_impl(1);
  3062. }
  3063. static void
  3064. test_util_spawn_background_waitpid_notify(void *arg)
  3065. {
  3066. int retval, exit_code;
  3067. process_handle_t *process_handle=NULL;
  3068. int status;
  3069. int ms_timer;
  3070. const char *argv[] = {TEST_CHILD, "--fast", NULL};
  3071. (void) arg;
  3072. #ifdef _WIN32
  3073. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  3074. #else
  3075. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  3076. #endif
  3077. tt_int_op(status, ==, PROCESS_STATUS_RUNNING);
  3078. tt_ptr_op(process_handle, !=, NULL);
  3079. /* We're not going to look at the stdout/stderr output this time. Instead,
  3080. * we're testing whether notify_pending_waitpid_calbacks() can report the
  3081. * process exit (on unix) and/or whether tor_get_exit_code() can notice it
  3082. * (on windows) */
  3083. #ifndef _WIN32
  3084. ms_timer = 30*1000;
  3085. tt_ptr_op(process_handle->waitpid_cb, !=, NULL);
  3086. while (process_handle->waitpid_cb && ms_timer > 0) {
  3087. tor_sleep_msec(100);
  3088. ms_timer -= 100;
  3089. notify_pending_waitpid_callbacks();
  3090. }
  3091. tt_int_op(ms_timer, >, 0);
  3092. tt_ptr_op(process_handle->waitpid_cb, ==, NULL);
  3093. #endif
  3094. ms_timer = 30*1000;
  3095. while (((retval = tor_get_exit_code(process_handle, 0, &exit_code))
  3096. == PROCESS_EXIT_RUNNING) && ms_timer > 0) {
  3097. tor_sleep_msec(100);
  3098. ms_timer -= 100;
  3099. }
  3100. tt_int_op(ms_timer, >, 0);
  3101. tt_int_op(retval, ==, PROCESS_EXIT_EXITED);
  3102. done:
  3103. tor_process_handle_destroy(process_handle, 1);
  3104. }
  3105. #undef TEST_CHILD
  3106. #undef EOL
  3107. #undef MATCH_PROCESS_STATUS
  3108. #ifndef _WIN32
  3109. #undef PROCESS_STATUS_RUNNING_OR_NOTRUNNING
  3110. #undef IS_RUNNING_OR_NOTRUNNING
  3111. #endif
  3112. /**
  3113. * Test for format_hex_number_sigsafe()
  3114. */
  3115. static void
  3116. test_util_format_hex_number(void *ptr)
  3117. {
  3118. int i, len;
  3119. char buf[33];
  3120. const struct {
  3121. const char *str;
  3122. unsigned int x;
  3123. } test_data[] = {
  3124. {"0", 0},
  3125. {"1", 1},
  3126. {"273A", 0x273a},
  3127. {"FFFF", 0xffff},
  3128. {"7FFFFFFF", 0x7fffffff},
  3129. {"FFFFFFFF", 0xffffffff},
  3130. #if UINT_MAX >= 0xffffffff
  3131. {"31BC421D", 0x31bc421d},
  3132. {"FFFFFFFF", 0xffffffff},
  3133. #endif
  3134. {NULL, 0}
  3135. };
  3136. (void)ptr;
  3137. for (i = 0; test_data[i].str != NULL; ++i) {
  3138. len = format_hex_number_sigsafe(test_data[i].x, buf, sizeof(buf));
  3139. tt_int_op(len,!=, 0);
  3140. tt_int_op(len,==, strlen(buf));
  3141. tt_str_op(buf,==, test_data[i].str);
  3142. }
  3143. tt_int_op(4,==, format_hex_number_sigsafe(0xffff, buf, 5));
  3144. tt_str_op(buf,==, "FFFF");
  3145. tt_int_op(0,==, format_hex_number_sigsafe(0xffff, buf, 4));
  3146. tt_int_op(0,==, format_hex_number_sigsafe(0, buf, 1));
  3147. done:
  3148. return;
  3149. }
  3150. /**
  3151. * Test for format_hex_number_sigsafe()
  3152. */
  3153. static void
  3154. test_util_format_dec_number(void *ptr)
  3155. {
  3156. int i, len;
  3157. char buf[33];
  3158. const struct {
  3159. const char *str;
  3160. unsigned int x;
  3161. } test_data[] = {
  3162. {"0", 0},
  3163. {"1", 1},
  3164. {"1234", 1234},
  3165. {"12345678", 12345678},
  3166. {"99999999", 99999999},
  3167. {"100000000", 100000000},
  3168. {"4294967295", 4294967295u},
  3169. #if UINT_MAX > 0xffffffff
  3170. {"18446744073709551615", 18446744073709551615u },
  3171. #endif
  3172. {NULL, 0}
  3173. };
  3174. (void)ptr;
  3175. for (i = 0; test_data[i].str != NULL; ++i) {
  3176. len = format_dec_number_sigsafe(test_data[i].x, buf, sizeof(buf));
  3177. tt_int_op(len,!=, 0);
  3178. tt_int_op(len,==, strlen(buf));
  3179. tt_str_op(buf,==, test_data[i].str);
  3180. len = format_dec_number_sigsafe(test_data[i].x, buf,
  3181. (int)(strlen(test_data[i].str) + 1));
  3182. tt_int_op(len,==, strlen(buf));
  3183. tt_str_op(buf,==, test_data[i].str);
  3184. }
  3185. tt_int_op(4,==, format_dec_number_sigsafe(7331, buf, 5));
  3186. tt_str_op(buf,==, "7331");
  3187. tt_int_op(0,==, format_dec_number_sigsafe(7331, buf, 4));
  3188. tt_int_op(1,==, format_dec_number_sigsafe(0, buf, 2));
  3189. tt_int_op(0,==, format_dec_number_sigsafe(0, buf, 1));
  3190. done:
  3191. return;
  3192. }
  3193. /**
  3194. * Test that we can properly format a Windows command line
  3195. */
  3196. static void
  3197. test_util_join_win_cmdline(void *ptr)
  3198. {
  3199. /* Based on some test cases from "Parsing C++ Command-Line Arguments" in
  3200. * MSDN but we don't exercise all quoting rules because tor_join_win_cmdline
  3201. * will try to only generate simple cases for the child process to parse;
  3202. * i.e. we never embed quoted strings in arguments. */
  3203. const char *argvs[][4] = {
  3204. {"a", "bb", "CCC", NULL}, // Normal
  3205. {NULL, NULL, NULL, NULL}, // Empty argument list
  3206. {"", NULL, NULL, NULL}, // Empty argument
  3207. {"\"a", "b\"b", "CCC\"", NULL}, // Quotes
  3208. {"a\tbc", "dd dd", "E", NULL}, // Whitespace
  3209. {"a\\\\\\b", "de fg", "H", NULL}, // Backslashes
  3210. {"a\\\"b", "\\c", "D\\", NULL}, // Backslashes before quote
  3211. {"a\\\\b c", "d", "E", NULL}, // Backslashes not before quote
  3212. { NULL } // Terminator
  3213. };
  3214. const char *cmdlines[] = {
  3215. "a bb CCC",
  3216. "",
  3217. "\"\"",
  3218. "\\\"a b\\\"b CCC\\\"",
  3219. "\"a\tbc\" \"dd dd\" E",
  3220. "a\\\\\\b \"de fg\" H",
  3221. "a\\\\\\\"b \\c D\\",
  3222. "\"a\\\\b c\" d E",
  3223. NULL // Terminator
  3224. };
  3225. int i;
  3226. char *joined_argv = NULL;
  3227. (void)ptr;
  3228. for (i=0; cmdlines[i]!=NULL; i++) {
  3229. log_info(LD_GENERAL, "Joining argvs[%d], expecting <%s>", i, cmdlines[i]);
  3230. joined_argv = tor_join_win_cmdline(argvs[i]);
  3231. tt_str_op(cmdlines[i],==, joined_argv);
  3232. tor_free(joined_argv);
  3233. }
  3234. done:
  3235. tor_free(joined_argv);
  3236. }
  3237. #define MAX_SPLIT_LINE_COUNT 4
  3238. struct split_lines_test_t {
  3239. const char *orig_line; // Line to be split (may contain \0's)
  3240. int orig_length; // Length of orig_line
  3241. const char *split_line[MAX_SPLIT_LINE_COUNT]; // Split lines
  3242. };
  3243. /**
  3244. * Test that we properly split a buffer into lines
  3245. */
  3246. static void
  3247. test_util_split_lines(void *ptr)
  3248. {
  3249. /* Test cases. orig_line of last test case must be NULL.
  3250. * The last element of split_line[i] must be NULL. */
  3251. struct split_lines_test_t tests[] = {
  3252. {"", 0, {NULL}},
  3253. {"foo", 3, {"foo", NULL}},
  3254. {"\n\rfoo\n\rbar\r\n", 12, {"foo", "bar", NULL}},
  3255. {"fo o\r\nb\tar", 10, {"fo o", "b.ar", NULL}},
  3256. {"\x0f""f\0o\0\n\x01""b\0r\0\r", 12, {".f.o.", ".b.r.", NULL}},
  3257. {"line 1\r\nline 2", 14, {"line 1", "line 2", NULL}},
  3258. {"line 1\r\n\r\nline 2", 16, {"line 1", "line 2", NULL}},
  3259. {"line 1\r\n\r\r\r\nline 2", 18, {"line 1", "line 2", NULL}},
  3260. {"line 1\r\n\n\n\n\rline 2", 18, {"line 1", "line 2", NULL}},
  3261. {"line 1\r\n\r\t\r\nline 3", 18, {"line 1", ".", "line 3", NULL}},
  3262. {"\n\t\r\t\nline 3", 11, {".", ".", "line 3", NULL}},
  3263. {NULL, 0, { NULL }}
  3264. };
  3265. int i, j;
  3266. char *orig_line=NULL;
  3267. smartlist_t *sl=NULL;
  3268. (void)ptr;
  3269. for (i=0; tests[i].orig_line; i++) {
  3270. sl = smartlist_new();
  3271. /* Allocate space for string and trailing NULL */
  3272. orig_line = tor_memdup(tests[i].orig_line, tests[i].orig_length + 1);
  3273. tor_split_lines(sl, orig_line, tests[i].orig_length);
  3274. j = 0;
  3275. log_info(LD_GENERAL, "Splitting test %d of length %d",
  3276. i, tests[i].orig_length);
  3277. SMARTLIST_FOREACH_BEGIN(sl, const char *, line) {
  3278. /* Check we have not got too many lines */
  3279. tt_int_op(MAX_SPLIT_LINE_COUNT, >, j);
  3280. /* Check that there actually should be a line here */
  3281. tt_assert(tests[i].split_line[j] != NULL);
  3282. log_info(LD_GENERAL, "Line %d of test %d, should be <%s>",
  3283. j, i, tests[i].split_line[j]);
  3284. /* Check that the line is as expected */
  3285. tt_str_op(line,==, tests[i].split_line[j]);
  3286. j++;
  3287. } SMARTLIST_FOREACH_END(line);
  3288. /* Check that we didn't miss some lines */
  3289. tt_ptr_op(NULL,==, tests[i].split_line[j]);
  3290. tor_free(orig_line);
  3291. smartlist_free(sl);
  3292. sl = NULL;
  3293. }
  3294. done:
  3295. tor_free(orig_line);
  3296. smartlist_free(sl);
  3297. }
  3298. static void
  3299. test_util_di_ops(void *arg)
  3300. {
  3301. #define LT -1
  3302. #define GT 1
  3303. #define EQ 0
  3304. const struct {
  3305. const char *a; int want_sign; const char *b;
  3306. } examples[] = {
  3307. { "Foo", EQ, "Foo" },
  3308. { "foo", GT, "bar", },
  3309. { "foobar", EQ ,"foobar" },
  3310. { "foobar", LT, "foobaw" },
  3311. { "foobar", GT, "f00bar" },
  3312. { "foobar", GT, "boobar" },
  3313. { "", EQ, "" },
  3314. { NULL, 0, NULL },
  3315. };
  3316. int i;
  3317. (void)arg;
  3318. for (i = 0; examples[i].a; ++i) {
  3319. size_t len = strlen(examples[i].a);
  3320. int eq1, eq2, neq1, neq2, cmp1, cmp2;
  3321. tt_int_op(len,==, strlen(examples[i].b));
  3322. /* We do all of the operations, with operands in both orders. */
  3323. eq1 = tor_memeq(examples[i].a, examples[i].b, len);
  3324. eq2 = tor_memeq(examples[i].b, examples[i].a, len);
  3325. neq1 = tor_memneq(examples[i].a, examples[i].b, len);
  3326. neq2 = tor_memneq(examples[i].b, examples[i].a, len);
  3327. cmp1 = tor_memcmp(examples[i].a, examples[i].b, len);
  3328. cmp2 = tor_memcmp(examples[i].b, examples[i].a, len);
  3329. /* Check for correctness of cmp1 */
  3330. if (cmp1 < 0 && examples[i].want_sign != LT)
  3331. TT_DIE(("Assertion failed."));
  3332. else if (cmp1 > 0 && examples[i].want_sign != GT)
  3333. TT_DIE(("Assertion failed."));
  3334. else if (cmp1 == 0 && examples[i].want_sign != EQ)
  3335. TT_DIE(("Assertion failed."));
  3336. /* Check for consistency of everything else with cmp1 */
  3337. tt_int_op(eq1,==, eq2);
  3338. tt_int_op(neq1,==, neq2);
  3339. tt_int_op(cmp1,==, -cmp2);
  3340. tt_int_op(eq1,==, cmp1 == 0);
  3341. tt_int_op(neq1,==, !eq1);
  3342. }
  3343. {
  3344. uint8_t zz = 0;
  3345. uint8_t ii = 0;
  3346. int z;
  3347. /* exhaustively test tor_memeq and tor_memcmp
  3348. * against each possible single-byte numeric difference
  3349. * some arithmetic bugs only appear with certain bit patterns */
  3350. for (z = 0; z < 256; z++) {
  3351. for (i = 0; i < 256; i++) {
  3352. ii = (uint8_t)i;
  3353. zz = (uint8_t)z;
  3354. tt_int_op(tor_memeq(&zz, &ii, 1),==, zz == ii);
  3355. tt_int_op(tor_memcmp(&zz, &ii, 1) > 0 ? GT : EQ,==, zz > ii ? GT : EQ);
  3356. tt_int_op(tor_memcmp(&ii, &zz, 1) < 0 ? LT : EQ,==, ii < zz ? LT : EQ);
  3357. }
  3358. }
  3359. }
  3360. tt_int_op(1, ==, safe_mem_is_zero("", 0));
  3361. tt_int_op(1, ==, safe_mem_is_zero("", 1));
  3362. tt_int_op(0, ==, safe_mem_is_zero("a", 1));
  3363. tt_int_op(0, ==, safe_mem_is_zero("a", 2));
  3364. tt_int_op(0, ==, safe_mem_is_zero("\0a", 2));
  3365. tt_int_op(1, ==, safe_mem_is_zero("\0\0a", 2));
  3366. tt_int_op(1, ==, safe_mem_is_zero("\0\0\0\0\0\0\0\0", 8));
  3367. tt_int_op(1, ==, safe_mem_is_zero("\0\0\0\0\0\0\0\0a", 8));
  3368. tt_int_op(0, ==, safe_mem_is_zero("\0\0\0\0\0\0\0\0a", 9));
  3369. done:
  3370. ;
  3371. }
  3372. /**
  3373. * Test counting high bits
  3374. */
  3375. static void
  3376. test_util_n_bits_set(void *ptr)
  3377. {
  3378. (void)ptr;
  3379. tt_int_op(0,==, n_bits_set_u8(0));
  3380. tt_int_op(1,==, n_bits_set_u8(1));
  3381. tt_int_op(3,==, n_bits_set_u8(7));
  3382. tt_int_op(1,==, n_bits_set_u8(8));
  3383. tt_int_op(2,==, n_bits_set_u8(129));
  3384. tt_int_op(8,==, n_bits_set_u8(255));
  3385. done:
  3386. ;
  3387. }
  3388. /**
  3389. * Test LHS whitespace (and comment) eater
  3390. */
  3391. static void
  3392. test_util_eat_whitespace(void *ptr)
  3393. {
  3394. const char ws[] = { ' ', '\t', '\r' }; /* Except NL */
  3395. char str[80];
  3396. size_t i;
  3397. (void)ptr;
  3398. /* Try one leading ws */
  3399. strlcpy(str, "fuubaar", sizeof(str));
  3400. for (i = 0; i < sizeof(ws); ++i) {
  3401. str[0] = ws[i];
  3402. tt_ptr_op(str + 1,==, eat_whitespace(str));
  3403. tt_ptr_op(str + 1,==, eat_whitespace_eos(str, str + strlen(str)));
  3404. tt_ptr_op(str + 1,==, eat_whitespace_no_nl(str));
  3405. tt_ptr_op(str + 1,==, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3406. }
  3407. str[0] = '\n';
  3408. tt_ptr_op(str + 1,==, eat_whitespace(str));
  3409. tt_ptr_op(str + 1,==, eat_whitespace_eos(str, str + strlen(str)));
  3410. tt_ptr_op(str,==, eat_whitespace_no_nl(str));
  3411. tt_ptr_op(str,==, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3412. /* Empty string */
  3413. strlcpy(str, "", sizeof(str));
  3414. tt_ptr_op(str,==, eat_whitespace(str));
  3415. tt_ptr_op(str,==, eat_whitespace_eos(str, str));
  3416. tt_ptr_op(str,==, eat_whitespace_no_nl(str));
  3417. tt_ptr_op(str,==, eat_whitespace_eos_no_nl(str, str));
  3418. /* Only ws */
  3419. strlcpy(str, " \t\r\n", sizeof(str));
  3420. tt_ptr_op(str + strlen(str),==, eat_whitespace(str));
  3421. tt_ptr_op(str + strlen(str),==, eat_whitespace_eos(str, str + strlen(str)));
  3422. tt_ptr_op(str + strlen(str) - 1,==,
  3423. eat_whitespace_no_nl(str));
  3424. tt_ptr_op(str + strlen(str) - 1,==,
  3425. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3426. strlcpy(str, " \t\r ", sizeof(str));
  3427. tt_ptr_op(str + strlen(str),==, eat_whitespace(str));
  3428. tt_ptr_op(str + strlen(str),==,
  3429. eat_whitespace_eos(str, str + strlen(str)));
  3430. tt_ptr_op(str + strlen(str),==, eat_whitespace_no_nl(str));
  3431. tt_ptr_op(str + strlen(str),==,
  3432. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3433. /* Multiple ws */
  3434. strlcpy(str, "fuubaar", sizeof(str));
  3435. for (i = 0; i < sizeof(ws); ++i)
  3436. str[i] = ws[i];
  3437. tt_ptr_op(str + sizeof(ws),==, eat_whitespace(str));
  3438. tt_ptr_op(str + sizeof(ws),==, eat_whitespace_eos(str, str + strlen(str)));
  3439. tt_ptr_op(str + sizeof(ws),==, eat_whitespace_no_nl(str));
  3440. tt_ptr_op(str + sizeof(ws),==,
  3441. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3442. /* Eat comment */
  3443. strlcpy(str, "# Comment \n No Comment", sizeof(str));
  3444. tt_str_op("No Comment",==, eat_whitespace(str));
  3445. tt_str_op("No Comment",==, eat_whitespace_eos(str, str + strlen(str)));
  3446. tt_ptr_op(str,==, eat_whitespace_no_nl(str));
  3447. tt_ptr_op(str,==, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3448. /* Eat comment & ws mix */
  3449. strlcpy(str, " # \t Comment \n\t\nNo Comment", sizeof(str));
  3450. tt_str_op("No Comment",==, eat_whitespace(str));
  3451. tt_str_op("No Comment",==, eat_whitespace_eos(str, str + strlen(str)));
  3452. tt_ptr_op(str + 1,==, eat_whitespace_no_nl(str));
  3453. tt_ptr_op(str + 1,==, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3454. /* Eat entire comment */
  3455. strlcpy(str, "#Comment", sizeof(str));
  3456. tt_ptr_op(str + strlen(str),==, eat_whitespace(str));
  3457. tt_ptr_op(str + strlen(str),==, eat_whitespace_eos(str, str + strlen(str)));
  3458. tt_ptr_op(str,==, eat_whitespace_no_nl(str));
  3459. tt_ptr_op(str,==, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3460. /* Blank line, then comment */
  3461. strlcpy(str, " \t\n # Comment", sizeof(str));
  3462. tt_ptr_op(str + strlen(str),==, eat_whitespace(str));
  3463. tt_ptr_op(str + strlen(str),==, eat_whitespace_eos(str, str + strlen(str)));
  3464. tt_ptr_op(str + 2,==, eat_whitespace_no_nl(str));
  3465. tt_ptr_op(str + 2,==, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  3466. done:
  3467. ;
  3468. }
  3469. /** Return a newly allocated smartlist containing the lines of text in
  3470. * <b>lines</b>. The returned strings are heap-allocated, and must be
  3471. * freed by the caller.
  3472. *
  3473. * XXXX? Move to container.[hc] ? */
  3474. static smartlist_t *
  3475. smartlist_new_from_text_lines(const char *lines)
  3476. {
  3477. smartlist_t *sl = smartlist_new();
  3478. char *last_line;
  3479. smartlist_split_string(sl, lines, "\n", 0, 0);
  3480. last_line = smartlist_pop_last(sl);
  3481. if (last_line != NULL && *last_line != '\0') {
  3482. smartlist_add(sl, last_line);
  3483. } else {
  3484. tor_free(last_line);
  3485. }
  3486. return sl;
  3487. }
  3488. /** Test smartlist_new_from_text_lines */
  3489. static void
  3490. test_util_sl_new_from_text_lines(void *ptr)
  3491. {
  3492. (void)ptr;
  3493. { /* Normal usage */
  3494. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz\n");
  3495. int sl_len = smartlist_len(sl);
  3496. tt_want_int_op(sl_len, ==, 3);
  3497. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  3498. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), ==, "bar");
  3499. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), ==, "baz");
  3500. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  3501. smartlist_free(sl);
  3502. }
  3503. { /* No final newline */
  3504. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz");
  3505. int sl_len = smartlist_len(sl);
  3506. tt_want_int_op(sl_len, ==, 3);
  3507. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  3508. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), ==, "bar");
  3509. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), ==, "baz");
  3510. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  3511. smartlist_free(sl);
  3512. }
  3513. { /* No newlines */
  3514. smartlist_t *sl = smartlist_new_from_text_lines("foo");
  3515. int sl_len = smartlist_len(sl);
  3516. tt_want_int_op(sl_len, ==, 1);
  3517. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  3518. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  3519. smartlist_free(sl);
  3520. }
  3521. { /* No text at all */
  3522. smartlist_t *sl = smartlist_new_from_text_lines("");
  3523. int sl_len = smartlist_len(sl);
  3524. tt_want_int_op(sl_len, ==, 0);
  3525. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  3526. smartlist_free(sl);
  3527. }
  3528. }
  3529. static void
  3530. test_util_envnames(void *ptr)
  3531. {
  3532. (void) ptr;
  3533. tt_assert(environment_variable_names_equal("abc", "abc"));
  3534. tt_assert(environment_variable_names_equal("abc", "abc="));
  3535. tt_assert(environment_variable_names_equal("abc", "abc=def"));
  3536. tt_assert(environment_variable_names_equal("abc=def", "abc"));
  3537. tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
  3538. tt_assert(environment_variable_names_equal("abc", "abc"));
  3539. tt_assert(environment_variable_names_equal("abc", "abc="));
  3540. tt_assert(environment_variable_names_equal("abc", "abc=def"));
  3541. tt_assert(environment_variable_names_equal("abc=def", "abc"));
  3542. tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
  3543. tt_assert(!environment_variable_names_equal("abc", "abcd"));
  3544. tt_assert(!environment_variable_names_equal("abc=", "abcd"));
  3545. tt_assert(!environment_variable_names_equal("abc=", "abcd"));
  3546. tt_assert(!environment_variable_names_equal("abc=", "def"));
  3547. tt_assert(!environment_variable_names_equal("abc=", "def="));
  3548. tt_assert(!environment_variable_names_equal("abc=x", "def=x"));
  3549. tt_assert(!environment_variable_names_equal("", "a=def"));
  3550. /* A bit surprising. */
  3551. tt_assert(environment_variable_names_equal("", "=def"));
  3552. tt_assert(environment_variable_names_equal("=y", "=x"));
  3553. done:
  3554. ;
  3555. }
  3556. /** Test process_environment_make */
  3557. static void
  3558. test_util_make_environment(void *ptr)
  3559. {
  3560. const char *env_vars_string =
  3561. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  3562. "HOME=/home/foozer\n";
  3563. const char expected_windows_env_block[] =
  3564. "HOME=/home/foozer\000"
  3565. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\000"
  3566. "\000";
  3567. size_t expected_windows_env_block_len =
  3568. sizeof(expected_windows_env_block) - 1;
  3569. smartlist_t *env_vars = smartlist_new_from_text_lines(env_vars_string);
  3570. smartlist_t *env_vars_sorted = smartlist_new();
  3571. smartlist_t *env_vars_in_unixoid_env_block_sorted = smartlist_new();
  3572. process_environment_t *env;
  3573. (void)ptr;
  3574. env = process_environment_make(env_vars);
  3575. /* Check that the Windows environment block is correct. */
  3576. tt_want(tor_memeq(expected_windows_env_block, env->windows_environment_block,
  3577. expected_windows_env_block_len));
  3578. /* Now for the Unixoid environment block. We don't care which order
  3579. * these environment variables are in, so we sort both lists first. */
  3580. smartlist_add_all(env_vars_sorted, env_vars);
  3581. {
  3582. char **v;
  3583. for (v = env->unixoid_environment_block; *v; ++v) {
  3584. smartlist_add(env_vars_in_unixoid_env_block_sorted, *v);
  3585. }
  3586. }
  3587. smartlist_sort_strings(env_vars_sorted);
  3588. smartlist_sort_strings(env_vars_in_unixoid_env_block_sorted);
  3589. tt_want_int_op(smartlist_len(env_vars_sorted), ==,
  3590. smartlist_len(env_vars_in_unixoid_env_block_sorted));
  3591. {
  3592. int len = smartlist_len(env_vars_sorted);
  3593. int i;
  3594. if (smartlist_len(env_vars_in_unixoid_env_block_sorted) < len) {
  3595. len = smartlist_len(env_vars_in_unixoid_env_block_sorted);
  3596. }
  3597. for (i = 0; i < len; ++i) {
  3598. tt_want_str_op(smartlist_get(env_vars_sorted, i), ==,
  3599. smartlist_get(env_vars_in_unixoid_env_block_sorted, i));
  3600. }
  3601. }
  3602. /* Clean up. */
  3603. smartlist_free(env_vars_in_unixoid_env_block_sorted);
  3604. smartlist_free(env_vars_sorted);
  3605. SMARTLIST_FOREACH(env_vars, char *, x, tor_free(x));
  3606. smartlist_free(env_vars);
  3607. process_environment_free(env);
  3608. }
  3609. /** Test set_environment_variable_in_smartlist */
  3610. static void
  3611. test_util_set_env_var_in_sl(void *ptr)
  3612. {
  3613. /* The environment variables in these strings are in arbitrary
  3614. * order; we sort the resulting lists before comparing them.
  3615. *
  3616. * (They *will not* end up in the order shown in
  3617. * expected_resulting_env_vars_string.) */
  3618. const char *base_env_vars_string =
  3619. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  3620. "HOME=/home/foozer\n"
  3621. "TERM=xterm\n"
  3622. "SHELL=/bin/ksh\n"
  3623. "USER=foozer\n"
  3624. "LOGNAME=foozer\n"
  3625. "USERNAME=foozer\n"
  3626. "LANG=en_US.utf8\n"
  3627. ;
  3628. const char *new_env_vars_string =
  3629. "TERM=putty\n"
  3630. "DISPLAY=:18.0\n"
  3631. ;
  3632. const char *expected_resulting_env_vars_string =
  3633. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  3634. "HOME=/home/foozer\n"
  3635. "TERM=putty\n"
  3636. "SHELL=/bin/ksh\n"
  3637. "USER=foozer\n"
  3638. "LOGNAME=foozer\n"
  3639. "USERNAME=foozer\n"
  3640. "LANG=en_US.utf8\n"
  3641. "DISPLAY=:18.0\n"
  3642. ;
  3643. smartlist_t *merged_env_vars =
  3644. smartlist_new_from_text_lines(base_env_vars_string);
  3645. smartlist_t *new_env_vars =
  3646. smartlist_new_from_text_lines(new_env_vars_string);
  3647. smartlist_t *expected_resulting_env_vars =
  3648. smartlist_new_from_text_lines(expected_resulting_env_vars_string);
  3649. /* Elements of merged_env_vars are heap-allocated, and must be
  3650. * freed. Some of them are (or should) be freed by
  3651. * set_environment_variable_in_smartlist.
  3652. *
  3653. * Elements of new_env_vars are heap-allocated, but are copied into
  3654. * merged_env_vars, so they are not freed separately at the end of
  3655. * the function.
  3656. *
  3657. * Elements of expected_resulting_env_vars are heap-allocated, and
  3658. * must be freed. */
  3659. (void)ptr;
  3660. SMARTLIST_FOREACH(new_env_vars, char *, env_var,
  3661. set_environment_variable_in_smartlist(merged_env_vars,
  3662. env_var,
  3663. tor_free_,
  3664. 1));
  3665. smartlist_sort_strings(merged_env_vars);
  3666. smartlist_sort_strings(expected_resulting_env_vars);
  3667. tt_want_int_op(smartlist_len(merged_env_vars), ==,
  3668. smartlist_len(expected_resulting_env_vars));
  3669. {
  3670. int len = smartlist_len(merged_env_vars);
  3671. int i;
  3672. if (smartlist_len(expected_resulting_env_vars) < len) {
  3673. len = smartlist_len(expected_resulting_env_vars);
  3674. }
  3675. for (i = 0; i < len; ++i) {
  3676. tt_want_str_op(smartlist_get(merged_env_vars, i), ==,
  3677. smartlist_get(expected_resulting_env_vars, i));
  3678. }
  3679. }
  3680. /* Clean up. */
  3681. SMARTLIST_FOREACH(merged_env_vars, char *, x, tor_free(x));
  3682. smartlist_free(merged_env_vars);
  3683. smartlist_free(new_env_vars);
  3684. SMARTLIST_FOREACH(expected_resulting_env_vars, char *, x, tor_free(x));
  3685. smartlist_free(expected_resulting_env_vars);
  3686. }
  3687. static void
  3688. test_util_weak_random(void *arg)
  3689. {
  3690. int i, j, n[16];
  3691. tor_weak_rng_t rng;
  3692. (void) arg;
  3693. tor_init_weak_random(&rng, (unsigned)time(NULL));
  3694. for (i = 1; i <= 256; ++i) {
  3695. for (j=0;j<100;++j) {
  3696. int r = tor_weak_random_range(&rng, i);
  3697. tt_int_op(0, <=, r);
  3698. tt_int_op(r, <, i);
  3699. }
  3700. }
  3701. memset(n,0,sizeof(n));
  3702. for (j=0;j<8192;++j) {
  3703. n[tor_weak_random_range(&rng, 16)]++;
  3704. }
  3705. for (i=0;i<16;++i)
  3706. tt_int_op(n[i], >, 0);
  3707. done:
  3708. ;
  3709. }
  3710. static void
  3711. test_util_mathlog(void *arg)
  3712. {
  3713. double d;
  3714. (void) arg;
  3715. d = tor_mathlog(2.718281828);
  3716. tt_double_op(fabs(d - 1.0), <, .000001);
  3717. d = tor_mathlog(10);
  3718. tt_double_op(fabs(d - 2.30258509), <, .000001);
  3719. done:
  3720. ;
  3721. }
  3722. static void
  3723. test_util_round_to_next_multiple_of(void *arg)
  3724. {
  3725. (void)arg;
  3726. tt_assert(round_uint64_to_next_multiple_of(0,1) == 0);
  3727. tt_assert(round_uint64_to_next_multiple_of(0,7) == 0);
  3728. tt_assert(round_uint64_to_next_multiple_of(99,1) == 99);
  3729. tt_assert(round_uint64_to_next_multiple_of(99,7) == 105);
  3730. tt_assert(round_uint64_to_next_multiple_of(99,9) == 99);
  3731. done:
  3732. ;
  3733. }
  3734. static void
  3735. test_util_strclear(void *arg)
  3736. {
  3737. static const char *vals[] = { "", "a", "abcdef", "abcdefgh", NULL };
  3738. int i;
  3739. char *v = NULL;
  3740. (void)arg;
  3741. for (i = 0; vals[i]; ++i) {
  3742. size_t n;
  3743. v = tor_strdup(vals[i]);
  3744. n = strlen(v);
  3745. tor_strclear(v);
  3746. tt_assert(tor_mem_is_zero(v, n+1));
  3747. tor_free(v);
  3748. }
  3749. done:
  3750. tor_free(v);
  3751. }
  3752. #define UTIL_LEGACY(name) \
  3753. { #name, test_util_ ## name , 0, NULL, NULL }
  3754. #define UTIL_TEST(name, flags) \
  3755. { #name, test_util_ ## name, flags, NULL, NULL }
  3756. #ifdef FD_CLOEXEC
  3757. #define CAN_CHECK_CLOEXEC
  3758. static int
  3759. fd_is_cloexec(tor_socket_t fd)
  3760. {
  3761. int flags = fcntl(fd, F_GETFD, 0);
  3762. return (flags & FD_CLOEXEC) == FD_CLOEXEC;
  3763. }
  3764. #endif
  3765. #ifndef _WIN32
  3766. #define CAN_CHECK_NONBLOCK
  3767. static int
  3768. fd_is_nonblocking(tor_socket_t fd)
  3769. {
  3770. int flags = fcntl(fd, F_GETFL, 0);
  3771. return (flags & O_NONBLOCK) == O_NONBLOCK;
  3772. }
  3773. #endif
  3774. static void
  3775. test_util_socket(void *arg)
  3776. {
  3777. tor_socket_t fd1 = TOR_INVALID_SOCKET;
  3778. tor_socket_t fd2 = TOR_INVALID_SOCKET;
  3779. tor_socket_t fd3 = TOR_INVALID_SOCKET;
  3780. tor_socket_t fd4 = TOR_INVALID_SOCKET;
  3781. int n = get_n_open_sockets();
  3782. TT_BLATHER(("Starting with %d open sockets.", n));
  3783. (void)arg;
  3784. fd1 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 0, 0);
  3785. fd2 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 0, 1);
  3786. tt_assert(SOCKET_OK(fd1));
  3787. tt_assert(SOCKET_OK(fd2));
  3788. tt_int_op(get_n_open_sockets(), ==, n + 2);
  3789. //fd3 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 1, 0);
  3790. //fd4 = tor_open_socket_with_extensions(AF_INET, SOCK_STREAM, 0, 1, 1);
  3791. fd3 = tor_open_socket(AF_INET, SOCK_STREAM, 0);
  3792. fd4 = tor_open_socket_nonblocking(AF_INET, SOCK_STREAM, 0);
  3793. tt_assert(SOCKET_OK(fd3));
  3794. tt_assert(SOCKET_OK(fd4));
  3795. tt_int_op(get_n_open_sockets(), ==, n + 4);
  3796. #ifdef CAN_CHECK_CLOEXEC
  3797. tt_int_op(fd_is_cloexec(fd1), ==, 0);
  3798. tt_int_op(fd_is_cloexec(fd2), ==, 0);
  3799. tt_int_op(fd_is_cloexec(fd3), ==, 1);
  3800. tt_int_op(fd_is_cloexec(fd4), ==, 1);
  3801. #endif
  3802. #ifdef CAN_CHECK_NONBLOCK
  3803. tt_int_op(fd_is_nonblocking(fd1), ==, 0);
  3804. tt_int_op(fd_is_nonblocking(fd2), ==, 1);
  3805. tt_int_op(fd_is_nonblocking(fd3), ==, 0);
  3806. tt_int_op(fd_is_nonblocking(fd4), ==, 1);
  3807. #endif
  3808. tor_close_socket(fd1);
  3809. tor_close_socket(fd2);
  3810. fd1 = fd2 = TOR_INVALID_SOCKET;
  3811. tt_int_op(get_n_open_sockets(), ==, n + 2);
  3812. tor_close_socket(fd3);
  3813. tor_close_socket(fd4);
  3814. fd3 = fd4 = TOR_INVALID_SOCKET;
  3815. tt_int_op(get_n_open_sockets(), ==, n);
  3816. done:
  3817. if (SOCKET_OK(fd1))
  3818. tor_close_socket(fd1);
  3819. if (SOCKET_OK(fd2))
  3820. tor_close_socket(fd2);
  3821. if (SOCKET_OK(fd3))
  3822. tor_close_socket(fd3);
  3823. if (SOCKET_OK(fd4))
  3824. tor_close_socket(fd4);
  3825. }
  3826. static void *
  3827. socketpair_test_setup(const struct testcase_t *testcase)
  3828. {
  3829. return testcase->setup_data;
  3830. }
  3831. static int
  3832. socketpair_test_cleanup(const struct testcase_t *testcase, void *ptr)
  3833. {
  3834. (void)testcase;
  3835. (void)ptr;
  3836. return 1;
  3837. }
  3838. static const struct testcase_setup_t socketpair_setup = {
  3839. socketpair_test_setup, socketpair_test_cleanup
  3840. };
  3841. /* Test for socketpair and ersatz_socketpair(). We test them both, since
  3842. * the latter is a tolerably good way to exersize tor_accept_socket(). */
  3843. static void
  3844. test_util_socketpair(void *arg)
  3845. {
  3846. const int ersatz = !strcmp(arg, "1");
  3847. int (*const tor_socketpair_fn)(int, int, int, tor_socket_t[2]) =
  3848. ersatz ? tor_ersatz_socketpair : tor_socketpair;
  3849. int n = get_n_open_sockets();
  3850. tor_socket_t fds[2] = {TOR_INVALID_SOCKET, TOR_INVALID_SOCKET};
  3851. const int family = AF_UNIX;
  3852. tt_int_op(0, ==, tor_socketpair_fn(family, SOCK_STREAM, 0, fds));
  3853. tt_assert(SOCKET_OK(fds[0]));
  3854. tt_assert(SOCKET_OK(fds[1]));
  3855. tt_int_op(get_n_open_sockets(), ==, n + 2);
  3856. #ifdef CAN_CHECK_CLOEXEC
  3857. tt_int_op(fd_is_cloexec(fds[0]), ==, 1);
  3858. tt_int_op(fd_is_cloexec(fds[1]), ==, 1);
  3859. #endif
  3860. #ifdef CAN_CHECK_NONBLOCK
  3861. tt_int_op(fd_is_nonblocking(fds[0]), ==, 0);
  3862. tt_int_op(fd_is_nonblocking(fds[1]), ==, 0);
  3863. #endif
  3864. done:
  3865. if (SOCKET_OK(fds[0]))
  3866. tor_close_socket(fds[0]);
  3867. if (SOCKET_OK(fds[1]))
  3868. tor_close_socket(fds[1]);
  3869. }
  3870. static void
  3871. test_util_max_mem(void *arg)
  3872. {
  3873. size_t memory1, memory2;
  3874. int r, r2;
  3875. (void) arg;
  3876. r = get_total_system_memory(&memory1);
  3877. r2 = get_total_system_memory(&memory2);
  3878. tt_int_op(r, ==, r2);
  3879. tt_uint_op(memory2, ==, memory1);
  3880. TT_BLATHER(("System memory: "U64_FORMAT, U64_PRINTF_ARG(memory1)));
  3881. if (r==0) {
  3882. /* You have at least a megabyte. */
  3883. tt_uint_op(memory1, >, (1<<20));
  3884. } else {
  3885. /* You do not have a petabyte. */
  3886. #if SIZEOF_SIZE_T == SIZEOF_UINT64_T
  3887. tt_uint_op(memory1, <, (U64_LITERAL(1)<<50));
  3888. #endif
  3889. }
  3890. done:
  3891. ;
  3892. }
  3893. struct testcase_t util_tests[] = {
  3894. UTIL_LEGACY(time),
  3895. UTIL_TEST(parse_http_time, 0),
  3896. UTIL_LEGACY(config_line),
  3897. UTIL_LEGACY(config_line_quotes),
  3898. UTIL_LEGACY(config_line_comment_character),
  3899. UTIL_LEGACY(config_line_escaped_content),
  3900. #ifndef _WIN32
  3901. UTIL_LEGACY(expand_filename),
  3902. #endif
  3903. UTIL_LEGACY(escape_string_socks),
  3904. UTIL_LEGACY(string_is_key_value),
  3905. UTIL_LEGACY(strmisc),
  3906. UTIL_LEGACY(pow2),
  3907. UTIL_LEGACY(gzip),
  3908. UTIL_LEGACY(datadir),
  3909. #ifdef ENABLE_MEMPOOLS
  3910. UTIL_LEGACY(mempool),
  3911. #endif
  3912. UTIL_LEGACY(memarea),
  3913. UTIL_LEGACY(control_formats),
  3914. UTIL_LEGACY(mmap),
  3915. UTIL_LEGACY(threads),
  3916. UTIL_LEGACY(sscanf),
  3917. UTIL_LEGACY(format_time_interval),
  3918. UTIL_LEGACY(path_is_relative),
  3919. UTIL_LEGACY(strtok),
  3920. UTIL_LEGACY(di_ops),
  3921. UTIL_TEST(round_to_next_multiple_of, 0),
  3922. UTIL_TEST(strclear, 0),
  3923. UTIL_TEST(find_str_at_start_of_line, 0),
  3924. UTIL_TEST(string_is_C_identifier, 0),
  3925. UTIL_TEST(asprintf, 0),
  3926. UTIL_TEST(listdir, 0),
  3927. UTIL_TEST(parent_dir, 0),
  3928. UTIL_TEST(ftruncate, 0),
  3929. #ifdef _WIN32
  3930. UTIL_TEST(load_win_lib, 0),
  3931. #endif
  3932. #ifndef _WIN32
  3933. UTIL_TEST(exit_status, 0),
  3934. UTIL_TEST(fgets_eagain, 0),
  3935. #endif
  3936. UTIL_TEST(spawn_background_ok, 0),
  3937. UTIL_TEST(spawn_background_fail, 0),
  3938. UTIL_TEST(spawn_background_partial_read, 0),
  3939. UTIL_TEST(spawn_background_exit_early, 0),
  3940. UTIL_TEST(spawn_background_waitpid_notify, 0),
  3941. UTIL_TEST(format_hex_number, 0),
  3942. UTIL_TEST(format_dec_number, 0),
  3943. UTIL_TEST(join_win_cmdline, 0),
  3944. UTIL_TEST(split_lines, 0),
  3945. UTIL_TEST(n_bits_set, 0),
  3946. UTIL_TEST(eat_whitespace, 0),
  3947. UTIL_TEST(sl_new_from_text_lines, 0),
  3948. UTIL_TEST(envnames, 0),
  3949. UTIL_TEST(make_environment, 0),
  3950. UTIL_TEST(set_env_var_in_sl, 0),
  3951. UTIL_TEST(read_file_eof_tiny_limit, 0),
  3952. UTIL_TEST(read_file_eof_one_loop_a, 0),
  3953. UTIL_TEST(read_file_eof_one_loop_b, 0),
  3954. UTIL_TEST(read_file_eof_two_loops, 0),
  3955. UTIL_TEST(read_file_eof_two_loops_b, 0),
  3956. UTIL_TEST(read_file_eof_zero_bytes, 0),
  3957. UTIL_TEST(write_chunks_to_file, 0),
  3958. UTIL_TEST(mathlog, 0),
  3959. UTIL_TEST(weak_random, 0),
  3960. UTIL_TEST(socket, TT_FORK),
  3961. { "socketpair", test_util_socketpair, TT_FORK, &socketpair_setup,
  3962. (void*)"0" },
  3963. { "socketpair_ersatz", test_util_socketpair, TT_FORK,
  3964. &socketpair_setup, (void*)"1" },
  3965. UTIL_TEST(max_mem, 0),
  3966. END_OF_TESTCASES
  3967. };