test_util.c 148 KB

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