test_util.c 137 KB

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