test_util.c 138 KB

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