test_util.c 145 KB

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