test_util.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONTROL_PRIVATE
  7. #define MEMPOOL_PRIVATE
  8. #define UTIL_PRIVATE
  9. #include "or.h"
  10. #include "config.h"
  11. #include "control.h"
  12. #include "test.h"
  13. #include "mempool.h"
  14. #include "memarea.h"
  15. #ifdef _WIN32
  16. #include <tchar.h>
  17. #endif
  18. /* XXXX this is a minimal wrapper to make the unit tests compile with the
  19. * changed tor_timegm interface. */
  20. static time_t
  21. tor_timegm_wrapper(const struct tm *tm)
  22. {
  23. time_t t;
  24. if (tor_timegm(tm, &t) < 0)
  25. return -1;
  26. return t;
  27. }
  28. #define tor_timegm tor_timegm_wrapper
  29. static void
  30. test_util_time(void)
  31. {
  32. struct timeval start, end;
  33. struct tm a_time;
  34. char timestr[128];
  35. time_t t_res;
  36. int i;
  37. struct timeval tv;
  38. /* Test tv_udiff */
  39. start.tv_sec = 5;
  40. start.tv_usec = 5000;
  41. end.tv_sec = 5;
  42. end.tv_usec = 5000;
  43. test_eq(0L, tv_udiff(&start, &end));
  44. end.tv_usec = 7000;
  45. test_eq(2000L, tv_udiff(&start, &end));
  46. end.tv_sec = 6;
  47. test_eq(1002000L, tv_udiff(&start, &end));
  48. end.tv_usec = 0;
  49. test_eq(995000L, tv_udiff(&start, &end));
  50. end.tv_sec = 4;
  51. test_eq(-1005000L, tv_udiff(&start, &end));
  52. /* Test tor_timegm */
  53. /* The test values here are confirmed to be correct on a platform
  54. * with a working timegm. */
  55. a_time.tm_year = 2003-1900;
  56. a_time.tm_mon = 7;
  57. a_time.tm_mday = 30;
  58. a_time.tm_hour = 6;
  59. a_time.tm_min = 14;
  60. a_time.tm_sec = 55;
  61. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  62. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  63. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  64. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  65. a_time.tm_mday = 10;
  66. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  67. a_time.tm_mon = 0;
  68. a_time.tm_mday = 10;
  69. test_eq((time_t) 1073715295UL, tor_timegm(&a_time));
  70. a_time.tm_mon = 12; /* Wrong month, it's 0-based */
  71. a_time.tm_mday = 10;
  72. test_eq((time_t) -1, tor_timegm(&a_time));
  73. a_time.tm_mon = -1; /* Wrong month */
  74. a_time.tm_mday = 10;
  75. test_eq((time_t) -1, tor_timegm(&a_time));
  76. /* Test {format,parse}_rfc1123_time */
  77. format_rfc1123_time(timestr, 0);
  78. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  79. format_rfc1123_time(timestr, (time_t)1091580502UL);
  80. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  81. t_res = 0;
  82. i = parse_rfc1123_time(timestr, &t_res);
  83. test_eq(0,i);
  84. test_eq(t_res, (time_t)1091580502UL);
  85. /* The timezone doesn't matter */
  86. t_res = 0;
  87. test_eq(0, parse_rfc1123_time("Wed, 04 Aug 2004 00:48:22 ZUL", &t_res));
  88. test_eq(t_res, (time_t)1091580502UL);
  89. test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  90. test_eq(-1, parse_rfc1123_time("Wed, 32 Mar 2011 00:00:00 GMT", &t_res));
  91. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 2011 24:00:00 GMT", &t_res));
  92. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 2011 23:60:00 GMT", &t_res));
  93. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 2011 23:59:62 GMT", &t_res));
  94. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 1969 23:59:59 GMT", &t_res));
  95. test_eq(-1, parse_rfc1123_time("Wed, 30 Ene 2011 23:59:59 GMT", &t_res));
  96. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 2011 23:59:59 GM", &t_res));
  97. #if 0
  98. /* This fails, I imagine it's important and should be fixed? */
  99. test_eq(-1, parse_rfc1123_time("Wed, 29 Feb 2011 16:00:00 GMT", &t_res));
  100. /* Why is this string valid (ie. the test fails because it doesn't
  101. return -1)? */
  102. test_eq(-1, parse_rfc1123_time("Wed, 30 Mar 2011 23:59:61 GMT", &t_res));
  103. #endif
  104. /* Test parse_iso_time */
  105. t_res = 0;
  106. i = parse_iso_time("", &t_res);
  107. test_eq(-1, i);
  108. t_res = 0;
  109. i = parse_iso_time("2004-08-32 00:48:22", &t_res);
  110. test_eq(-1, i);
  111. t_res = 0;
  112. i = parse_iso_time("1969-08-03 00:48:22", &t_res);
  113. test_eq(-1, i);
  114. t_res = 0;
  115. i = parse_iso_time("2004-08-04 00:48:22", &t_res);
  116. test_eq(0,i);
  117. test_eq(t_res, (time_t)1091580502UL);
  118. t_res = 0;
  119. i = parse_iso_time("2004-8-4 0:48:22", &t_res);
  120. test_eq(0, i);
  121. test_eq(t_res, (time_t)1091580502UL);
  122. test_eq(-1, parse_iso_time("2004-08-zz 99-99x99 GMT", &t_res));
  123. test_eq(-1, parse_iso_time("2011-03-32 00:00:00 GMT", &t_res));
  124. test_eq(-1, parse_iso_time("2011-03-30 24:00:00 GMT", &t_res));
  125. test_eq(-1, parse_iso_time("2011-03-30 23:60:00 GMT", &t_res));
  126. test_eq(-1, parse_iso_time("2011-03-30 23:59:62 GMT", &t_res));
  127. test_eq(-1, parse_iso_time("1969-03-30 23:59:59 GMT", &t_res));
  128. test_eq(-1, parse_iso_time("2011-00-30 23:59:59 GMT", &t_res));
  129. test_eq(-1, parse_iso_time("2011-03-30 23:59", &t_res));
  130. /* Test tor_gettimeofday */
  131. end.tv_sec = 4;
  132. end.tv_usec = 999990;
  133. start.tv_sec = 1;
  134. start.tv_usec = 500;
  135. tor_gettimeofday(&start);
  136. /* now make sure time works. */
  137. tor_gettimeofday(&end);
  138. /* We might've timewarped a little. */
  139. tt_int_op(tv_udiff(&start, &end), >=, -5000);
  140. /* Test format_iso_time */
  141. tv.tv_sec = (time_t)1326296338;
  142. tv.tv_usec = 3060;
  143. format_iso_time(timestr, tv.tv_sec);
  144. test_streq("2012-01-11 15:38:58", timestr);
  145. /* The output of format_local_iso_time will vary by timezone, and setting
  146. our timezone for testing purposes would be a nontrivial flaky pain.
  147. Skip this test for now.
  148. format_local_iso_time(timestr, tv.tv_sec);
  149. test_streq("2012-01-11 10:38:58", timestr);
  150. */
  151. format_iso_time_nospace(timestr, tv.tv_sec);
  152. test_streq("2012-01-11T15:38:58", timestr);
  153. test_eq(strlen(timestr), ISO_TIME_LEN);
  154. format_iso_time_nospace_usec(timestr, &tv);
  155. test_streq("2012-01-11T15:38:58.003060", timestr);
  156. test_eq(strlen(timestr), ISO_TIME_USEC_LEN);
  157. done:
  158. ;
  159. }
  160. static void
  161. test_util_parse_http_time(void *arg)
  162. {
  163. struct tm a_time;
  164. char b[ISO_TIME_LEN+1];
  165. (void)arg;
  166. #define T(s) do { \
  167. format_iso_time(b, tor_timegm(&a_time)); \
  168. tt_str_op(b, ==, (s)); \
  169. b[0]='\0'; \
  170. } while (0)
  171. /* Test parse_http_time */
  172. test_eq(-1, parse_http_time("", &a_time));
  173. test_eq(-1, parse_http_time("Sunday, 32 Aug 2004 00:48:22 GMT", &a_time));
  174. test_eq(-1, parse_http_time("Sunday, 3 Aug 1869 00:48:22 GMT", &a_time));
  175. test_eq(-1, parse_http_time("Sunday, 32-Aug-94 00:48:22 GMT", &a_time));
  176. test_eq(-1, parse_http_time("Sunday, 3-Ago-04 00:48:22", &a_time));
  177. test_eq(-1, parse_http_time("Sunday, August the third", &a_time));
  178. test_eq(-1, parse_http_time("Wednesday,,04 Aug 1994 00:48:22 GMT", &a_time));
  179. test_eq(0, parse_http_time("Wednesday, 04 Aug 1994 00:48:22 GMT", &a_time));
  180. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  181. T("1994-08-04 00:48:22");
  182. test_eq(0, parse_http_time("Wednesday, 4 Aug 1994 0:48:22 GMT", &a_time));
  183. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  184. T("1994-08-04 00:48:22");
  185. test_eq(0, parse_http_time("Miercoles, 4 Aug 1994 0:48:22 GMT", &a_time));
  186. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  187. T("1994-08-04 00:48:22");
  188. test_eq(0, parse_http_time("Wednesday, 04-Aug-94 00:48:22 GMT", &a_time));
  189. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  190. T("1994-08-04 00:48:22");
  191. test_eq(0, parse_http_time("Wednesday, 4-Aug-94 0:48:22 GMT", &a_time));
  192. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  193. T("1994-08-04 00:48:22");
  194. test_eq(0, parse_http_time("Miercoles, 4-Aug-94 0:48:22 GMT", &a_time));
  195. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  196. T("1994-08-04 00:48:22");
  197. test_eq(0, parse_http_time("Wed Aug 04 00:48:22 1994", &a_time));
  198. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  199. T("1994-08-04 00:48:22");
  200. test_eq(0, parse_http_time("Wed Aug 4 0:48:22 1994", &a_time));
  201. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  202. T("1994-08-04 00:48:22");
  203. test_eq(0, parse_http_time("Mie Aug 4 0:48:22 1994", &a_time));
  204. test_eq((time_t)775961302UL, tor_timegm(&a_time));
  205. T("1994-08-04 00:48:22");
  206. test_eq(0, parse_http_time("Sun, 1 Jan 2012 00:00:00 GMT", &a_time));
  207. test_eq((time_t)1325376000UL, tor_timegm(&a_time));
  208. T("2012-01-01 00:00:00");
  209. test_eq(0, parse_http_time("Mon, 31 Dec 2012 00:00:00 GMT", &a_time));
  210. test_eq((time_t)1356912000UL, tor_timegm(&a_time));
  211. T("2012-12-31 00:00:00");
  212. test_eq(-1, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
  213. test_eq(-1, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
  214. test_eq(-1, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
  215. test_eq(-1, parse_http_time("2011-03-30 23:60:00 GMT", &a_time));
  216. test_eq(-1, parse_http_time("2011-03-30 23:59:62 GMT", &a_time));
  217. test_eq(-1, parse_http_time("1969-03-30 23:59:59 GMT", &a_time));
  218. test_eq(-1, parse_http_time("2011-00-30 23:59:59 GMT", &a_time));
  219. test_eq(-1, parse_http_time("2011-03-30 23:59", &a_time));
  220. #undef T
  221. done:
  222. ;
  223. }
  224. static void
  225. test_util_config_line(void)
  226. {
  227. char buf[1024];
  228. char *k=NULL, *v=NULL;
  229. const char *str;
  230. /* Test parse_config_line_from_str */
  231. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  232. "k2\n"
  233. "k3 \n" "\n" " \n" "#comment\n"
  234. "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
  235. "kseven \"a quoted 'string\"\n"
  236. "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
  237. "k9 a line that\\\n spans two lines.\n\n"
  238. "k10 more than\\\n one contin\\\nuation\n"
  239. "k11 \\\ncontinuation at the start\n"
  240. "k12 line with a\\\n#comment\n embedded\n"
  241. "k13\\\ncontinuation at the very start\n"
  242. "k14 a line that has a comment and # ends with a slash \\\n"
  243. "k15 this should be the next new line\n"
  244. "k16 a line that has a comment and # ends without a slash \n"
  245. "k17 this should be the next new line\n"
  246. , sizeof(buf));
  247. str = buf;
  248. str = parse_config_line_from_str(str, &k, &v);
  249. test_streq(k, "k");
  250. test_streq(v, "v");
  251. tor_free(k); tor_free(v);
  252. test_assert(!strcmpstart(str, "key value with"));
  253. str = parse_config_line_from_str(str, &k, &v);
  254. test_streq(k, "key");
  255. test_streq(v, "value with spaces");
  256. tor_free(k); tor_free(v);
  257. test_assert(!strcmpstart(str, "keykey"));
  258. str = parse_config_line_from_str(str, &k, &v);
  259. test_streq(k, "keykey");
  260. test_streq(v, "val");
  261. tor_free(k); tor_free(v);
  262. test_assert(!strcmpstart(str, "k2\n"));
  263. str = parse_config_line_from_str(str, &k, &v);
  264. test_streq(k, "k2");
  265. test_streq(v, "");
  266. tor_free(k); tor_free(v);
  267. test_assert(!strcmpstart(str, "k3 \n"));
  268. str = parse_config_line_from_str(str, &k, &v);
  269. test_streq(k, "k3");
  270. test_streq(v, "");
  271. tor_free(k); tor_free(v);
  272. test_assert(!strcmpstart(str, "#comment"));
  273. str = parse_config_line_from_str(str, &k, &v);
  274. test_streq(k, "k4");
  275. test_streq(v, "");
  276. tor_free(k); tor_free(v);
  277. test_assert(!strcmpstart(str, "k5#abc"));
  278. str = parse_config_line_from_str(str, &k, &v);
  279. test_streq(k, "k5");
  280. test_streq(v, "");
  281. tor_free(k); tor_free(v);
  282. test_assert(!strcmpstart(str, "k6"));
  283. str = parse_config_line_from_str(str, &k, &v);
  284. test_streq(k, "k6");
  285. test_streq(v, "val");
  286. tor_free(k); tor_free(v);
  287. test_assert(!strcmpstart(str, "kseven"));
  288. str = parse_config_line_from_str(str, &k, &v);
  289. test_streq(k, "kseven");
  290. test_streq(v, "a quoted \'string");
  291. tor_free(k); tor_free(v);
  292. test_assert(!strcmpstart(str, "k8 "));
  293. str = parse_config_line_from_str(str, &k, &v);
  294. test_streq(k, "k8");
  295. test_streq(v, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
  296. tor_free(k); tor_free(v);
  297. str = parse_config_line_from_str(str, &k, &v);
  298. test_streq(k, "k9");
  299. test_streq(v, "a line that spans two lines.");
  300. tor_free(k); tor_free(v);
  301. str = parse_config_line_from_str(str, &k, &v);
  302. test_streq(k, "k10");
  303. test_streq(v, "more than one continuation");
  304. tor_free(k); tor_free(v);
  305. str = parse_config_line_from_str(str, &k, &v);
  306. test_streq(k, "k11");
  307. test_streq(v, "continuation at the start");
  308. tor_free(k); tor_free(v);
  309. str = parse_config_line_from_str(str, &k, &v);
  310. test_streq(k, "k12");
  311. test_streq(v, "line with a embedded");
  312. tor_free(k); tor_free(v);
  313. str = parse_config_line_from_str(str, &k, &v);
  314. test_streq(k, "k13");
  315. test_streq(v, "continuation at the very start");
  316. tor_free(k); tor_free(v);
  317. str = parse_config_line_from_str(str, &k, &v);
  318. test_streq(k, "k14");
  319. test_streq(v, "a line that has a comment and" );
  320. tor_free(k); tor_free(v);
  321. str = parse_config_line_from_str(str, &k, &v);
  322. test_streq(k, "k15");
  323. test_streq(v, "this should be the next new line");
  324. tor_free(k); tor_free(v);
  325. str = parse_config_line_from_str(str, &k, &v);
  326. test_streq(k, "k16");
  327. test_streq(v, "a line that has a comment and" );
  328. tor_free(k); tor_free(v);
  329. str = parse_config_line_from_str(str, &k, &v);
  330. test_streq(k, "k17");
  331. test_streq(v, "this should be the next new line");
  332. tor_free(k); tor_free(v);
  333. test_streq(str, "");
  334. done:
  335. tor_free(k);
  336. tor_free(v);
  337. }
  338. static void
  339. test_util_config_line_quotes(void)
  340. {
  341. char buf1[1024];
  342. char buf2[128];
  343. char buf3[128];
  344. char buf4[128];
  345. char *k=NULL, *v=NULL;
  346. const char *str;
  347. /* Test parse_config_line_from_str */
  348. strlcpy(buf1, "kTrailingSpace \"quoted value\" \n"
  349. "kTrailingGarbage \"quoted value\"trailing garbage\n"
  350. , sizeof(buf1));
  351. strlcpy(buf2, "kTrailingSpaceAndGarbage \"quoted value\" trailing space+g\n"
  352. , sizeof(buf2));
  353. strlcpy(buf3, "kMultilineTrailingSpace \"mline\\ \nvalue w/ trailing sp\"\n"
  354. , sizeof(buf3));
  355. strlcpy(buf4, "kMultilineNoTrailingBackslash \"naked multiline\nvalue\"\n"
  356. , sizeof(buf4));
  357. str = buf1;
  358. str = parse_config_line_from_str(str, &k, &v);
  359. test_streq(k, "kTrailingSpace");
  360. test_streq(v, "quoted value");
  361. tor_free(k); tor_free(v);
  362. str = parse_config_line_from_str(str, &k, &v);
  363. test_eq_ptr(str, NULL);
  364. tor_free(k); tor_free(v);
  365. str = buf2;
  366. str = parse_config_line_from_str(str, &k, &v);
  367. test_eq_ptr(str, NULL);
  368. tor_free(k); tor_free(v);
  369. str = buf3;
  370. str = parse_config_line_from_str(str, &k, &v);
  371. test_eq_ptr(str, NULL);
  372. tor_free(k); tor_free(v);
  373. str = buf4;
  374. str = parse_config_line_from_str(str, &k, &v);
  375. test_eq_ptr(str, NULL);
  376. tor_free(k); tor_free(v);
  377. done:
  378. tor_free(k);
  379. tor_free(v);
  380. }
  381. static void
  382. test_util_config_line_comment_character(void)
  383. {
  384. char buf[1024];
  385. char *k=NULL, *v=NULL;
  386. const char *str;
  387. /* Test parse_config_line_from_str */
  388. strlcpy(buf, "k1 \"# in quotes\"\n"
  389. "k2 some value # some comment\n"
  390. "k3 /home/user/myTorNetwork#2\n" /* Testcase for #1323 */
  391. , sizeof(buf));
  392. str = buf;
  393. str = parse_config_line_from_str(str, &k, &v);
  394. test_streq(k, "k1");
  395. test_streq(v, "# in quotes");
  396. tor_free(k); tor_free(v);
  397. str = parse_config_line_from_str(str, &k, &v);
  398. test_streq(k, "k2");
  399. test_streq(v, "some value");
  400. tor_free(k); tor_free(v);
  401. test_streq(str, "k3 /home/user/myTorNetwork#2\n");
  402. #if 0
  403. str = parse_config_line_from_str(str, &k, &v);
  404. test_streq(k, "k3");
  405. test_streq(v, "/home/user/myTorNetwork#2");
  406. tor_free(k); tor_free(v);
  407. test_streq(str, "");
  408. #endif
  409. done:
  410. tor_free(k);
  411. tor_free(v);
  412. }
  413. static void
  414. test_util_config_line_escaped_content(void)
  415. {
  416. char buf1[1024];
  417. char buf2[128];
  418. char buf3[128];
  419. char buf4[128];
  420. char buf5[128];
  421. char buf6[128];
  422. char *k=NULL, *v=NULL;
  423. const char *str;
  424. /* Test parse_config_line_from_str */
  425. strlcpy(buf1, "HexadecimalLower \"\\x2a\"\n"
  426. "HexadecimalUpper \"\\x2A\"\n"
  427. "HexadecimalUpperX \"\\X2A\"\n"
  428. "Octal \"\\52\"\n"
  429. "Newline \"\\n\"\n"
  430. "Tab \"\\t\"\n"
  431. "CarriageReturn \"\\r\"\n"
  432. "DoubleQuote \"\\\"\"\n"
  433. "SimpleQuote \"\\'\"\n"
  434. "Backslash \"\\\\\"\n"
  435. "Mix \"This is a \\\"star\\\":\\t\\'\\x2a\\'\\nAnd second line\"\n"
  436. , sizeof(buf1));
  437. strlcpy(buf2, "BrokenEscapedContent \"\\a\"\n"
  438. , sizeof(buf2));
  439. strlcpy(buf3, "BrokenEscapedContent \"\\x\"\n"
  440. , sizeof(buf3));
  441. strlcpy(buf4, "BrokenOctal \"\\8\"\n"
  442. , sizeof(buf4));
  443. strlcpy(buf5, "BrokenHex \"\\xg4\"\n"
  444. , sizeof(buf5));
  445. strlcpy(buf6, "BrokenEscape \"\\"
  446. , sizeof(buf6));
  447. str = buf1;
  448. str = parse_config_line_from_str(str, &k, &v);
  449. test_streq(k, "HexadecimalLower");
  450. test_streq(v, "*");
  451. tor_free(k); tor_free(v);
  452. str = parse_config_line_from_str(str, &k, &v);
  453. test_streq(k, "HexadecimalUpper");
  454. test_streq(v, "*");
  455. tor_free(k); tor_free(v);
  456. str = parse_config_line_from_str(str, &k, &v);
  457. test_streq(k, "HexadecimalUpperX");
  458. test_streq(v, "*");
  459. tor_free(k); tor_free(v);
  460. str = parse_config_line_from_str(str, &k, &v);
  461. test_streq(k, "Octal");
  462. test_streq(v, "*");
  463. tor_free(k); tor_free(v);
  464. str = parse_config_line_from_str(str, &k, &v);
  465. test_streq(k, "Newline");
  466. test_streq(v, "\n");
  467. tor_free(k); tor_free(v);
  468. str = parse_config_line_from_str(str, &k, &v);
  469. test_streq(k, "Tab");
  470. test_streq(v, "\t");
  471. tor_free(k); tor_free(v);
  472. str = parse_config_line_from_str(str, &k, &v);
  473. test_streq(k, "CarriageReturn");
  474. test_streq(v, "\r");
  475. tor_free(k); tor_free(v);
  476. str = parse_config_line_from_str(str, &k, &v);
  477. test_streq(k, "DoubleQuote");
  478. test_streq(v, "\"");
  479. tor_free(k); tor_free(v);
  480. str = parse_config_line_from_str(str, &k, &v);
  481. test_streq(k, "SimpleQuote");
  482. test_streq(v, "'");
  483. tor_free(k); tor_free(v);
  484. str = parse_config_line_from_str(str, &k, &v);
  485. test_streq(k, "Backslash");
  486. test_streq(v, "\\");
  487. tor_free(k); tor_free(v);
  488. str = parse_config_line_from_str(str, &k, &v);
  489. test_streq(k, "Mix");
  490. test_streq(v, "This is a \"star\":\t'*'\nAnd second line");
  491. tor_free(k); tor_free(v);
  492. test_streq(str, "");
  493. str = buf2;
  494. str = parse_config_line_from_str(str, &k, &v);
  495. test_eq_ptr(str, NULL);
  496. tor_free(k); tor_free(v);
  497. str = buf3;
  498. str = parse_config_line_from_str(str, &k, &v);
  499. test_eq_ptr(str, NULL);
  500. tor_free(k); tor_free(v);
  501. str = buf4;
  502. str = parse_config_line_from_str(str, &k, &v);
  503. test_eq_ptr(str, NULL);
  504. tor_free(k); tor_free(v);
  505. #if 0
  506. str = buf5;
  507. str = parse_config_line_from_str(str, &k, &v);
  508. test_eq_ptr(str, NULL);
  509. tor_free(k); tor_free(v);
  510. #endif
  511. str = buf6;
  512. str = parse_config_line_from_str(str, &k, &v);
  513. test_eq_ptr(str, NULL);
  514. tor_free(k); tor_free(v);
  515. done:
  516. tor_free(k);
  517. tor_free(v);
  518. }
  519. #ifndef _WIN32
  520. static void
  521. test_util_expand_filename(void)
  522. {
  523. char *str;
  524. setenv("HOME", "/home/itv", 1); /* For "internal test value" */
  525. str = expand_filename("");
  526. test_streq("", str);
  527. tor_free(str);
  528. str = expand_filename("/normal/path");
  529. test_streq("/normal/path", str);
  530. tor_free(str);
  531. str = expand_filename("/normal/trailing/path/");
  532. test_streq("/normal/trailing/path/", str);
  533. tor_free(str);
  534. str = expand_filename("~");
  535. test_streq("/home/itv/", str);
  536. tor_free(str);
  537. str = expand_filename("$HOME/nodice");
  538. test_streq("$HOME/nodice", str);
  539. tor_free(str);
  540. str = expand_filename("~/");
  541. test_streq("/home/itv/", str);
  542. tor_free(str);
  543. str = expand_filename("~/foobarqux");
  544. test_streq("/home/itv/foobarqux", str);
  545. tor_free(str);
  546. str = expand_filename("~/../../etc/passwd");
  547. test_streq("/home/itv/../../etc/passwd", str);
  548. tor_free(str);
  549. str = expand_filename("~/trailing/");
  550. test_streq("/home/itv/trailing/", str);
  551. tor_free(str);
  552. /* Ideally we'd test ~anotheruser, but that's shady to test (we'd
  553. have to somehow inject/fake the get_user_homedir call) */
  554. /* $HOME ending in a trailing slash */
  555. setenv("HOME", "/home/itv/", 1);
  556. str = expand_filename("~");
  557. test_streq("/home/itv/", str);
  558. tor_free(str);
  559. str = expand_filename("~/");
  560. test_streq("/home/itv/", str);
  561. tor_free(str);
  562. str = expand_filename("~/foo");
  563. test_streq("/home/itv/foo", str);
  564. tor_free(str);
  565. /* Try with empty $HOME */
  566. setenv("HOME", "", 1);
  567. str = expand_filename("~");
  568. test_streq("/", str);
  569. tor_free(str);
  570. str = expand_filename("~/");
  571. test_streq("/", str);
  572. tor_free(str);
  573. str = expand_filename("~/foobar");
  574. test_streq("/foobar", str);
  575. tor_free(str);
  576. /* Try with $HOME unset */
  577. unsetenv("HOME");
  578. str = expand_filename("~");
  579. test_streq("/", str);
  580. tor_free(str);
  581. str = expand_filename("~/");
  582. test_streq("/", str);
  583. tor_free(str);
  584. str = expand_filename("~/foobar");
  585. test_streq("/foobar", str);
  586. tor_free(str);
  587. done:
  588. tor_free(str);
  589. }
  590. #endif
  591. /** Test basic string functionality. */
  592. static void
  593. test_util_strmisc(void)
  594. {
  595. char buf[1024];
  596. int i;
  597. char *cp;
  598. /* Test strl operations */
  599. test_eq(5, strlcpy(buf, "Hello", 0));
  600. test_eq(5, strlcpy(buf, "Hello", 10));
  601. test_streq(buf, "Hello");
  602. test_eq(5, strlcpy(buf, "Hello", 6));
  603. test_streq(buf, "Hello");
  604. test_eq(5, strlcpy(buf, "Hello", 5));
  605. test_streq(buf, "Hell");
  606. strlcpy(buf, "Hello", sizeof(buf));
  607. test_eq(10, strlcat(buf, "Hello", 5));
  608. /* Test strstrip() */
  609. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  610. tor_strstrip(buf, ",!");
  611. test_streq(buf, "Testing 1 2 3");
  612. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  613. tor_strstrip(buf, "!? ");
  614. test_streq(buf, "Testing123");
  615. strlcpy(buf, "!!!Testing 1 2 3??", sizeof(buf));
  616. tor_strstrip(buf, "!? ");
  617. test_streq(buf, "Testing123");
  618. /* Test parse_long */
  619. /* Empty/zero input */
  620. test_eq(0L, tor_parse_long("",10,0,100,&i,NULL));
  621. test_eq(0, i);
  622. test_eq(0L, tor_parse_long("0",10,0,100,&i,NULL));
  623. test_eq(1, i);
  624. /* Normal cases */
  625. test_eq(10L, tor_parse_long("10",10,0,100,&i,NULL));
  626. test_eq(1, i);
  627. test_eq(10L, tor_parse_long("10",10,0,10,&i,NULL));
  628. test_eq(1, i);
  629. test_eq(10L, tor_parse_long("10",10,10,100,&i,NULL));
  630. test_eq(1, i);
  631. test_eq(-50L, tor_parse_long("-50",10,-100,100,&i,NULL));
  632. test_eq(1, i);
  633. test_eq(-50L, tor_parse_long("-50",10,-100,0,&i,NULL));
  634. test_eq(1, i);
  635. test_eq(-50L, tor_parse_long("-50",10,-50,0,&i,NULL));
  636. test_eq(1, i);
  637. /* Extra garbage */
  638. test_eq(0L, tor_parse_long("10m",10,0,100,&i,NULL));
  639. test_eq(0, i);
  640. test_eq(0L, tor_parse_long("-50 plus garbage",10,-100,100,&i,NULL));
  641. test_eq(0, i);
  642. test_eq(10L, tor_parse_long("10m",10,0,100,&i,&cp));
  643. test_eq(1, i);
  644. test_streq(cp, "m");
  645. test_eq(-50L, tor_parse_long("-50 plus garbage",10,-100,100,&i,&cp));
  646. test_eq(1, i);
  647. test_streq(cp, " plus garbage");
  648. /* Out of bounds */
  649. test_eq(0L, tor_parse_long("10",10,50,100,&i,NULL));
  650. test_eq(0, i);
  651. test_eq(0L, tor_parse_long("-50",10,0,100,&i,NULL));
  652. test_eq(0, i);
  653. /* Base different than 10 */
  654. test_eq(2L, tor_parse_long("10",2,0,100,NULL,NULL));
  655. test_eq(0L, tor_parse_long("2",2,0,100,NULL,NULL));
  656. test_eq(0L, tor_parse_long("10",-2,0,100,NULL,NULL));
  657. test_eq(68284L, tor_parse_long("10abc",16,0,70000,NULL,NULL));
  658. test_eq(68284L, tor_parse_long("10ABC",16,0,70000,NULL,NULL));
  659. /* Test parse_ulong */
  660. test_eq(0UL, tor_parse_ulong("",10,0,100,NULL,NULL));
  661. test_eq(0UL, tor_parse_ulong("0",10,0,100,NULL,NULL));
  662. test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  663. test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
  664. test_eq(10UL, tor_parse_ulong("10",10,0,10,NULL,NULL));
  665. test_eq(10UL, tor_parse_ulong("10",10,10,100,NULL,NULL));
  666. test_eq(0UL, tor_parse_ulong("8",8,0,100,NULL,NULL));
  667. test_eq(50UL, tor_parse_ulong("50",10,50,100,NULL,NULL));
  668. test_eq(0UL, tor_parse_ulong("-50",10,-100,100,NULL,NULL));
  669. /* Test parse_uint64 */
  670. test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  671. test_eq(1, i);
  672. test_streq(cp, " x");
  673. test_assert(U64_LITERAL(12345678901) ==
  674. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  675. test_eq(1, i);
  676. test_streq(cp, "");
  677. test_assert(U64_LITERAL(0) ==
  678. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  679. test_eq(0, i);
  680. {
  681. /* Test parse_double */
  682. double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
  683. test_eq(1, i);
  684. test_assert(DBL_TO_U64(d) == 10);
  685. d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
  686. test_eq(1, i);
  687. test_assert(DBL_TO_U64(d) == 0);
  688. d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
  689. test_eq(0, i);
  690. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
  691. test_eq(0, i);
  692. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
  693. test_eq(1, i);
  694. d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
  695. test_eq(1, i);
  696. test_assert(DBL_TO_U64(d) == 0);
  697. d = tor_parse_double("-10", -100.0, 100.0,&i,NULL);
  698. test_eq(1, i);
  699. test_eq(-10.0, d);
  700. }
  701. {
  702. /* Test tor_parse_* where we overflow/underflow the underlying type. */
  703. /* This string should overflow 64-bit ints. */
  704. #define TOOBIG "100000000000000000000000000"
  705. test_eq(0L, tor_parse_long(TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  706. test_eq(i, 0);
  707. test_eq(0L, tor_parse_long("-"TOOBIG, 10, LONG_MIN, LONG_MAX, &i, NULL));
  708. test_eq(i, 0);
  709. test_eq(0UL, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
  710. test_eq(i, 0);
  711. test_eq(U64_LITERAL(0), tor_parse_uint64(TOOBIG, 10,
  712. 0, UINT64_MAX, &i, NULL));
  713. test_eq(i, 0);
  714. }
  715. /* Test snprintf */
  716. /* Returning -1 when there's not enough room in the output buffer */
  717. test_eq(-1, tor_snprintf(buf, 0, "Foo"));
  718. test_eq(-1, tor_snprintf(buf, 2, "Foo"));
  719. test_eq(-1, tor_snprintf(buf, 3, "Foo"));
  720. test_neq(-1, tor_snprintf(buf, 4, "Foo"));
  721. /* Always NUL-terminate the output */
  722. tor_snprintf(buf, 5, "abcdef");
  723. test_eq(0, buf[4]);
  724. tor_snprintf(buf, 10, "abcdef");
  725. test_eq(0, buf[6]);
  726. /* uint64 */
  727. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  728. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  729. test_streq("x!12345678901!x", buf);
  730. /* Test str{,case}cmpstart */
  731. test_assert(strcmpstart("abcdef", "abcdef")==0);
  732. test_assert(strcmpstart("abcdef", "abc")==0);
  733. test_assert(strcmpstart("abcdef", "abd")<0);
  734. test_assert(strcmpstart("abcdef", "abb")>0);
  735. test_assert(strcmpstart("ab", "abb")<0);
  736. test_assert(strcmpstart("ab", "")==0);
  737. test_assert(strcmpstart("ab", "ab ")<0);
  738. test_assert(strcasecmpstart("abcdef", "abCdEF")==0);
  739. test_assert(strcasecmpstart("abcDeF", "abc")==0);
  740. test_assert(strcasecmpstart("abcdef", "Abd")<0);
  741. test_assert(strcasecmpstart("Abcdef", "abb")>0);
  742. test_assert(strcasecmpstart("ab", "Abb")<0);
  743. test_assert(strcasecmpstart("ab", "")==0);
  744. test_assert(strcasecmpstart("ab", "ab ")<0);
  745. /* Test str{,case}cmpend */
  746. test_assert(strcmpend("abcdef", "abcdef")==0);
  747. test_assert(strcmpend("abcdef", "def")==0);
  748. test_assert(strcmpend("abcdef", "deg")<0);
  749. test_assert(strcmpend("abcdef", "dee")>0);
  750. test_assert(strcmpend("ab", "aab")>0);
  751. test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  752. test_assert(strcasecmpend("abcdef", "dEF")==0);
  753. test_assert(strcasecmpend("abcdef", "Deg")<0);
  754. test_assert(strcasecmpend("abcDef", "dee")>0);
  755. test_assert(strcasecmpend("AB", "abb")<0);
  756. /* Test digest_is_zero */
  757. memset(buf,0,20);
  758. buf[20] = 'x';
  759. test_assert(tor_digest_is_zero(buf));
  760. buf[19] = 'x';
  761. test_assert(!tor_digest_is_zero(buf));
  762. /* Test mem_is_zero */
  763. memset(buf,0,128);
  764. buf[128] = 'x';
  765. test_assert(tor_mem_is_zero(buf, 10));
  766. test_assert(tor_mem_is_zero(buf, 20));
  767. test_assert(tor_mem_is_zero(buf, 128));
  768. test_assert(!tor_mem_is_zero(buf, 129));
  769. buf[60] = (char)255;
  770. test_assert(!tor_mem_is_zero(buf, 128));
  771. buf[0] = (char)1;
  772. test_assert(!tor_mem_is_zero(buf, 10));
  773. /* Test 'escaped' */
  774. test_assert(NULL == escaped(NULL));
  775. test_streq("\"\"", escaped(""));
  776. test_streq("\"abcd\"", escaped("abcd"));
  777. test_streq("\"\\\\ \\n\\r\\t\\\"\\'\"", escaped("\\ \n\r\t\"'"));
  778. test_streq("\"unnecessary \\'backslashes\\'\"",
  779. escaped("unnecessary \'backslashes\'"));
  780. /* Non-printable characters appear as octal */
  781. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  782. test_streq("\"z\\336\\255 ;foo\"", escaped("z\xde\xad\x20;foo"));
  783. /* Test strndup and memdup */
  784. {
  785. const char *s = "abcdefghijklmnopqrstuvwxyz";
  786. cp = tor_strndup(s, 30);
  787. test_streq(cp, s); /* same string, */
  788. test_neq(cp, s); /* but different pointers. */
  789. tor_free(cp);
  790. cp = tor_strndup(s, 5);
  791. test_streq(cp, "abcde");
  792. tor_free(cp);
  793. s = "a\0b\0c\0d\0e\0";
  794. cp = tor_memdup(s,10);
  795. test_memeq(cp, s, 10); /* same ram, */
  796. test_neq(cp, s); /* but different pointers. */
  797. tor_free(cp);
  798. }
  799. /* Test str-foo functions */
  800. cp = tor_strdup("abcdef");
  801. test_assert(tor_strisnonupper(cp));
  802. cp[3] = 'D';
  803. test_assert(!tor_strisnonupper(cp));
  804. tor_strupper(cp);
  805. test_streq(cp, "ABCDEF");
  806. tor_strlower(cp);
  807. test_streq(cp, "abcdef");
  808. test_assert(tor_strisnonupper(cp));
  809. test_assert(tor_strisprint(cp));
  810. cp[3] = 3;
  811. test_assert(!tor_strisprint(cp));
  812. tor_free(cp);
  813. /* Test memmem and memstr */
  814. {
  815. const char *haystack = "abcde";
  816. test_assert(!tor_memmem(haystack, 5, "ef", 2));
  817. test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
  818. test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
  819. test_assert(!tor_memmem(haystack, 4, "cde", 3));
  820. haystack = "ababcad";
  821. test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
  822. /* memstr */
  823. test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
  824. test_eq_ptr(tor_memstr(haystack, 7, "cad"), haystack + 4);
  825. test_assert(!tor_memstr(haystack, 6, "cad"));
  826. test_assert(!tor_memstr(haystack, 7, "cadd"));
  827. test_assert(!tor_memstr(haystack, 7, "fe"));
  828. test_assert(!tor_memstr(haystack, 7, "ababcade"));
  829. }
  830. /* Test wrap_string */
  831. {
  832. smartlist_t *sl = smartlist_new();
  833. wrap_string(sl,
  834. "This is a test of string wrapping functionality: woot. "
  835. "a functionality? w00t w00t...!",
  836. 10, "", "");
  837. cp = smartlist_join_strings(sl, "", 0, NULL);
  838. test_streq(cp,
  839. "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n"
  840. "a\nfunctional\nity? w00t\nw00t...!\n");
  841. tor_free(cp);
  842. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  843. smartlist_clear(sl);
  844. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  845. 16, "### ", "# ");
  846. cp = smartlist_join_strings(sl, "", 0, NULL);
  847. test_streq(cp,
  848. "### This is a\n# test of string\n# wrapping\n# functionality:\n"
  849. "# woot.\n");
  850. tor_free(cp);
  851. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  852. smartlist_clear(sl);
  853. wrap_string(sl, "A test of string wrapping...", 6, "### ", "# ");
  854. cp = smartlist_join_strings(sl, "", 0, NULL);
  855. test_streq(cp,
  856. "### A\n# test\n# of\n# stri\n# ng\n# wrap\n# ping\n# ...\n");
  857. tor_free(cp);
  858. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  859. smartlist_clear(sl);
  860. wrap_string(sl, "Wrapping test", 6, "#### ", "# ");
  861. cp = smartlist_join_strings(sl, "", 0, NULL);
  862. test_streq(cp, "#### W\n# rapp\n# ing\n# test\n");
  863. tor_free(cp);
  864. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  865. smartlist_clear(sl);
  866. wrap_string(sl, "Small test", 6, "### ", "#### ");
  867. cp = smartlist_join_strings(sl, "", 0, NULL);
  868. test_streq(cp, "### Sm\n#### a\n#### l\n#### l\n#### t\n#### e"
  869. "\n#### s\n#### t\n");
  870. tor_free(cp);
  871. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  872. smartlist_clear(sl);
  873. wrap_string(sl, "First null", 6, NULL, "> ");
  874. cp = smartlist_join_strings(sl, "", 0, NULL);
  875. test_streq(cp, "First\n> null\n");
  876. tor_free(cp);
  877. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  878. smartlist_clear(sl);
  879. wrap_string(sl, "Second null", 6, "> ", NULL);
  880. cp = smartlist_join_strings(sl, "", 0, NULL);
  881. test_streq(cp, "> Seco\nnd\nnull\n");
  882. tor_free(cp);
  883. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  884. smartlist_clear(sl);
  885. wrap_string(sl, "Both null", 6, NULL, NULL);
  886. cp = smartlist_join_strings(sl, "", 0, NULL);
  887. test_streq(cp, "Both\nnull\n");
  888. tor_free(cp);
  889. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  890. smartlist_free(sl);
  891. /* Can't test prefixes that have the same length as the line width, because
  892. the function has an assert */
  893. }
  894. /* Test hex_str */
  895. {
  896. char binary_data[68];
  897. size_t i;
  898. for (i = 0; i < sizeof(binary_data); ++i)
  899. binary_data[i] = i;
  900. test_streq(hex_str(binary_data, 0), "");
  901. test_streq(hex_str(binary_data, 1), "00");
  902. test_streq(hex_str(binary_data, 17), "000102030405060708090A0B0C0D0E0F10");
  903. test_streq(hex_str(binary_data, 32),
  904. "000102030405060708090A0B0C0D0E0F"
  905. "101112131415161718191A1B1C1D1E1F");
  906. test_streq(hex_str(binary_data, 34),
  907. "000102030405060708090A0B0C0D0E0F"
  908. "101112131415161718191A1B1C1D1E1F");
  909. /* Repeat these tests for shorter strings after longer strings
  910. have been tried, to make sure we're correctly terminating strings */
  911. test_streq(hex_str(binary_data, 1), "00");
  912. test_streq(hex_str(binary_data, 0), "");
  913. }
  914. /* Test strcmp_opt */
  915. tt_int_op(strcmp_opt("", "foo"), <, 0);
  916. tt_int_op(strcmp_opt("", ""), ==, 0);
  917. tt_int_op(strcmp_opt("foo", ""), >, 0);
  918. tt_int_op(strcmp_opt(NULL, ""), <, 0);
  919. tt_int_op(strcmp_opt(NULL, NULL), ==, 0);
  920. tt_int_op(strcmp_opt("", NULL), >, 0);
  921. tt_int_op(strcmp_opt(NULL, "foo"), <, 0);
  922. tt_int_op(strcmp_opt("foo", NULL), >, 0);
  923. /* Test strcmp_len */
  924. tt_int_op(strcmp_len("foo", "bar", 3), >, 0);
  925. tt_int_op(strcmp_len("foo", "bar", 2), <, 0); /* First len, then lexical */
  926. tt_int_op(strcmp_len("foo2", "foo1", 4), >, 0);
  927. tt_int_op(strcmp_len("foo2", "foo1", 3), <, 0); /* Really stop at len */
  928. tt_int_op(strcmp_len("foo2", "foo", 3), ==, 0); /* Really stop at len */
  929. tt_int_op(strcmp_len("blah", "", 4), >, 0);
  930. tt_int_op(strcmp_len("blah", "", 0), ==, 0);
  931. done:
  932. ;
  933. }
  934. static void
  935. test_util_pow2(void)
  936. {
  937. /* Test tor_log2(). */
  938. test_eq(tor_log2(64), 6);
  939. test_eq(tor_log2(65), 6);
  940. test_eq(tor_log2(63), 5);
  941. test_eq(tor_log2(1), 0);
  942. test_eq(tor_log2(2), 1);
  943. test_eq(tor_log2(3), 1);
  944. test_eq(tor_log2(4), 2);
  945. test_eq(tor_log2(5), 2);
  946. test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
  947. test_eq(tor_log2(UINT64_MAX), 63);
  948. /* Test round_to_power_of_2 */
  949. test_eq(round_to_power_of_2(120), 128);
  950. test_eq(round_to_power_of_2(128), 128);
  951. test_eq(round_to_power_of_2(130), 128);
  952. test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
  953. U64_LITERAL(1)<<55);
  954. test_eq(round_to_power_of_2(0), 2);
  955. done:
  956. ;
  957. }
  958. /** mutex for thread test to stop the threads hitting data at the same time. */
  959. static tor_mutex_t *_thread_test_mutex = NULL;
  960. /** mutexes for the thread test to make sure that the threads have to
  961. * interleave somewhat. */
  962. static tor_mutex_t *_thread_test_start1 = NULL,
  963. *_thread_test_start2 = NULL;
  964. /** Shared strmap for the thread test. */
  965. static strmap_t *_thread_test_strmap = NULL;
  966. /** The name of thread1 for the thread test */
  967. static char *_thread1_name = NULL;
  968. /** The name of thread2 for the thread test */
  969. static char *_thread2_name = NULL;
  970. static void _thread_test_func(void* _s) ATTR_NORETURN;
  971. /** How many iterations have the threads in the unit test run? */
  972. static int t1_count = 0, t2_count = 0;
  973. /** Helper function for threading unit tests: This function runs in a
  974. * subthread. It grabs its own mutex (start1 or start2) to make sure that it
  975. * should start, then it repeatedly alters _test_thread_strmap protected by
  976. * _thread_test_mutex. */
  977. static void
  978. _thread_test_func(void* _s)
  979. {
  980. char *s = _s;
  981. int i, *count;
  982. tor_mutex_t *m;
  983. char buf[64];
  984. char **cp;
  985. if (!strcmp(s, "thread 1")) {
  986. m = _thread_test_start1;
  987. cp = &_thread1_name;
  988. count = &t1_count;
  989. } else {
  990. m = _thread_test_start2;
  991. cp = &_thread2_name;
  992. count = &t2_count;
  993. }
  994. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  995. *cp = tor_strdup(buf);
  996. tor_mutex_acquire(m);
  997. for (i=0; i<10000; ++i) {
  998. tor_mutex_acquire(_thread_test_mutex);
  999. strmap_set(_thread_test_strmap, "last to run", *cp);
  1000. ++*count;
  1001. tor_mutex_release(_thread_test_mutex);
  1002. }
  1003. tor_mutex_acquire(_thread_test_mutex);
  1004. strmap_set(_thread_test_strmap, s, *cp);
  1005. tor_mutex_release(_thread_test_mutex);
  1006. tor_mutex_release(m);
  1007. spawn_exit();
  1008. }
  1009. /** Run unit tests for threading logic. */
  1010. static void
  1011. test_util_threads(void)
  1012. {
  1013. char *s1 = NULL, *s2 = NULL;
  1014. int done = 0, timedout = 0;
  1015. time_t started;
  1016. #ifndef _WIN32
  1017. struct timeval tv;
  1018. tv.tv_sec=0;
  1019. tv.tv_usec=100*1000;
  1020. #endif
  1021. #ifndef TOR_IS_MULTITHREADED
  1022. /* Skip this test if we aren't threading. We should be threading most
  1023. * everywhere by now. */
  1024. if (1)
  1025. return;
  1026. #endif
  1027. _thread_test_mutex = tor_mutex_new();
  1028. _thread_test_start1 = tor_mutex_new();
  1029. _thread_test_start2 = tor_mutex_new();
  1030. _thread_test_strmap = strmap_new();
  1031. s1 = tor_strdup("thread 1");
  1032. s2 = tor_strdup("thread 2");
  1033. tor_mutex_acquire(_thread_test_start1);
  1034. tor_mutex_acquire(_thread_test_start2);
  1035. spawn_func(_thread_test_func, s1);
  1036. spawn_func(_thread_test_func, s2);
  1037. tor_mutex_release(_thread_test_start2);
  1038. tor_mutex_release(_thread_test_start1);
  1039. started = time(NULL);
  1040. while (!done) {
  1041. tor_mutex_acquire(_thread_test_mutex);
  1042. strmap_assert_ok(_thread_test_strmap);
  1043. if (strmap_get(_thread_test_strmap, "thread 1") &&
  1044. strmap_get(_thread_test_strmap, "thread 2")) {
  1045. done = 1;
  1046. } else if (time(NULL) > started + 150) {
  1047. timedout = done = 1;
  1048. }
  1049. tor_mutex_release(_thread_test_mutex);
  1050. #ifndef _WIN32
  1051. /* Prevent the main thread from starving the worker threads. */
  1052. select(0, NULL, NULL, NULL, &tv);
  1053. #endif
  1054. }
  1055. tor_mutex_acquire(_thread_test_start1);
  1056. tor_mutex_release(_thread_test_start1);
  1057. tor_mutex_acquire(_thread_test_start2);
  1058. tor_mutex_release(_thread_test_start2);
  1059. tor_mutex_free(_thread_test_mutex);
  1060. if (timedout) {
  1061. printf("\nTimed out: %d %d", t1_count, t2_count);
  1062. test_assert(strmap_get(_thread_test_strmap, "thread 1"));
  1063. test_assert(strmap_get(_thread_test_strmap, "thread 2"));
  1064. test_assert(!timedout);
  1065. }
  1066. /* different thread IDs. */
  1067. test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  1068. strmap_get(_thread_test_strmap, "thread 2")));
  1069. test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  1070. strmap_get(_thread_test_strmap, "last to run")) ||
  1071. !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
  1072. strmap_get(_thread_test_strmap, "last to run")));
  1073. done:
  1074. tor_free(s1);
  1075. tor_free(s2);
  1076. tor_free(_thread1_name);
  1077. tor_free(_thread2_name);
  1078. if (_thread_test_strmap)
  1079. strmap_free(_thread_test_strmap, NULL);
  1080. if (_thread_test_start1)
  1081. tor_mutex_free(_thread_test_start1);
  1082. if (_thread_test_start2)
  1083. tor_mutex_free(_thread_test_start2);
  1084. }
  1085. /** Run unit tests for compression functions */
  1086. static void
  1087. test_util_gzip(void)
  1088. {
  1089. char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  1090. const char *ccp2;
  1091. size_t len1, len2;
  1092. tor_zlib_state_t *state = NULL;
  1093. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  1094. test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  1095. if (is_gzip_supported()) {
  1096. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1097. GZIP_METHOD));
  1098. test_assert(buf2);
  1099. test_assert(len1 < strlen(buf1));
  1100. test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  1101. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1102. GZIP_METHOD, 1, LOG_INFO));
  1103. test_assert(buf3);
  1104. test_eq(strlen(buf1) + 1, len2);
  1105. test_streq(buf1, buf3);
  1106. tor_free(buf2);
  1107. tor_free(buf3);
  1108. }
  1109. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1110. ZLIB_METHOD));
  1111. test_assert(buf2);
  1112. test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  1113. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1114. ZLIB_METHOD, 1, LOG_INFO));
  1115. test_assert(buf3);
  1116. test_eq(strlen(buf1) + 1, len2);
  1117. test_streq(buf1, buf3);
  1118. /* Check whether we can uncompress concatenated, compressed strings. */
  1119. tor_free(buf3);
  1120. buf2 = tor_realloc(buf2, len1*2);
  1121. memcpy(buf2+len1, buf2, len1);
  1122. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  1123. ZLIB_METHOD, 1, LOG_INFO));
  1124. test_eq((strlen(buf1)+1)*2, len2);
  1125. test_memeq(buf3,
  1126. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  1127. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  1128. (strlen(buf1)+1)*2);
  1129. tor_free(buf1);
  1130. tor_free(buf2);
  1131. tor_free(buf3);
  1132. /* Check whether we can uncompress partial strings. */
  1133. buf1 =
  1134. tor_strdup("String with low redundancy that won't be compressed much.");
  1135. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1136. ZLIB_METHOD));
  1137. tt_assert(len1>16);
  1138. /* when we allow an incomplete string, we should succeed.*/
  1139. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1140. ZLIB_METHOD, 0, LOG_INFO));
  1141. tt_assert(len2 > 5);
  1142. buf3[len2]='\0';
  1143. tt_assert(!strcmpstart(buf1, buf3));
  1144. /* when we demand a complete string, this must fail. */
  1145. tor_free(buf3);
  1146. tt_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1147. ZLIB_METHOD, 1, LOG_INFO));
  1148. tt_assert(!buf3);
  1149. /* Now, try streaming compression. */
  1150. tor_free(buf1);
  1151. tor_free(buf2);
  1152. tor_free(buf3);
  1153. state = tor_zlib_new(1, ZLIB_METHOD);
  1154. tt_assert(state);
  1155. cp1 = buf1 = tor_malloc(1024);
  1156. len1 = 1024;
  1157. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  1158. len2 = 21;
  1159. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  1160. == TOR_ZLIB_OK);
  1161. test_eq(0, len2); /* Make sure we compressed it all. */
  1162. test_assert(cp1 > buf1);
  1163. len2 = 0;
  1164. cp2 = cp1;
  1165. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  1166. == TOR_ZLIB_DONE);
  1167. test_eq(0, len2);
  1168. test_assert(cp1 > cp2); /* Make sure we really added something. */
  1169. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  1170. ZLIB_METHOD, 1, LOG_WARN));
  1171. test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  1172. test_eq(21, len2);
  1173. done:
  1174. if (state)
  1175. tor_zlib_free(state);
  1176. tor_free(buf2);
  1177. tor_free(buf3);
  1178. tor_free(buf1);
  1179. }
  1180. /** Run unit tests for mmap() wrapper functionality. */
  1181. static void
  1182. test_util_mmap(void)
  1183. {
  1184. char *fname1 = tor_strdup(get_fname("mapped_1"));
  1185. char *fname2 = tor_strdup(get_fname("mapped_2"));
  1186. char *fname3 = tor_strdup(get_fname("mapped_3"));
  1187. const size_t buflen = 17000;
  1188. char *buf = tor_malloc(17000);
  1189. tor_mmap_t *mapping = NULL;
  1190. crypto_rand(buf, buflen);
  1191. mapping = tor_mmap_file(fname1);
  1192. test_assert(! mapping);
  1193. write_str_to_file(fname1, "Short file.", 1);
  1194. mapping = tor_mmap_file(fname1);
  1195. test_assert(mapping);
  1196. test_eq(mapping->size, strlen("Short file."));
  1197. test_streq(mapping->data, "Short file.");
  1198. #ifdef _WIN32
  1199. tor_munmap_file(mapping);
  1200. mapping = NULL;
  1201. test_assert(unlink(fname1) == 0);
  1202. #else
  1203. /* make sure we can unlink. */
  1204. test_assert(unlink(fname1) == 0);
  1205. test_streq(mapping->data, "Short file.");
  1206. tor_munmap_file(mapping);
  1207. mapping = NULL;
  1208. #endif
  1209. /* Now a zero-length file. */
  1210. write_str_to_file(fname1, "", 1);
  1211. mapping = tor_mmap_file(fname1);
  1212. test_eq(mapping, NULL);
  1213. test_eq(ERANGE, errno);
  1214. unlink(fname1);
  1215. /* Make sure that we fail to map a no-longer-existent file. */
  1216. mapping = tor_mmap_file(fname1);
  1217. test_assert(! mapping);
  1218. /* Now try a big file that stretches across a few pages and isn't aligned */
  1219. write_bytes_to_file(fname2, buf, buflen, 1);
  1220. mapping = tor_mmap_file(fname2);
  1221. test_assert(mapping);
  1222. test_eq(mapping->size, buflen);
  1223. test_memeq(mapping->data, buf, buflen);
  1224. tor_munmap_file(mapping);
  1225. mapping = NULL;
  1226. /* Now try a big aligned file. */
  1227. write_bytes_to_file(fname3, buf, 16384, 1);
  1228. mapping = tor_mmap_file(fname3);
  1229. test_assert(mapping);
  1230. test_eq(mapping->size, 16384);
  1231. test_memeq(mapping->data, buf, 16384);
  1232. tor_munmap_file(mapping);
  1233. mapping = NULL;
  1234. done:
  1235. unlink(fname1);
  1236. unlink(fname2);
  1237. unlink(fname3);
  1238. tor_free(fname1);
  1239. tor_free(fname2);
  1240. tor_free(fname3);
  1241. tor_free(buf);
  1242. if (mapping)
  1243. tor_munmap_file(mapping);
  1244. }
  1245. /** Run unit tests for escaping/unescaping data for use by controllers. */
  1246. static void
  1247. test_util_control_formats(void)
  1248. {
  1249. char *out = NULL;
  1250. const char *inp =
  1251. "..This is a test\r\n.of the emergency \n..system.\r\n\rZ.\r\n";
  1252. size_t sz;
  1253. sz = read_escaped_data(inp, strlen(inp), &out);
  1254. test_streq(out,
  1255. ".This is a test\nof the emergency \n.system.\n\rZ.\n");
  1256. test_eq(sz, strlen(out));
  1257. done:
  1258. tor_free(out);
  1259. }
  1260. #define test_feq(value1,value2) do { \
  1261. double v1 = (value1), v2=(value2); \
  1262. double tf_diff = v1-v2; \
  1263. double tf_tolerance = ((v1+v2)/2.0)/1e8; \
  1264. if (tf_diff<0) tf_diff=-tf_diff; \
  1265. if (tf_tolerance<0) tf_tolerance=-tf_tolerance; \
  1266. if (tf_diff<tf_tolerance) { \
  1267. TT_BLATHER(("%s ~~ %s: %f ~~ %f",#value1,#value2,v1,v2)); \
  1268. } else { \
  1269. TT_FAIL(("%s ~~ %s: %f != %f",#value1,#value2,v1,v2)); \
  1270. } \
  1271. } while (0)
  1272. static void
  1273. test_util_sscanf(void)
  1274. {
  1275. unsigned u1, u2, u3;
  1276. char s1[20], s2[10], s3[10], ch;
  1277. int r;
  1278. long lng1,lng2;
  1279. int int1, int2;
  1280. double d1,d2,d3,d4;
  1281. /* Simple tests (malformed patterns, literal matching, ...) */
  1282. test_eq(-1, tor_sscanf("123", "%i", &r)); /* %i is not supported */
  1283. test_eq(-1, tor_sscanf("wrong", "%5c", s1)); /* %c cannot have a number. */
  1284. test_eq(-1, tor_sscanf("hello", "%s", s1)); /* %s needs a number. */
  1285. test_eq(-1, tor_sscanf("prettylongstring", "%999999s", s1));
  1286. #if 0
  1287. /* GCC thinks these two are illegal. */
  1288. test_eq(-1, tor_sscanf("prettylongstring", "%0s", s1));
  1289. test_eq(0, tor_sscanf("prettylongstring", "%10s", NULL));
  1290. #endif
  1291. /* No '%'-strings: always "success" */
  1292. test_eq(0, tor_sscanf("hello world", "hello world"));
  1293. test_eq(0, tor_sscanf("hello world", "good bye"));
  1294. /* Excess data */
  1295. test_eq(0, tor_sscanf("hello 3", "%u", &u1)); /* have to match the start */
  1296. test_eq(0, tor_sscanf(" 3 hello", "%u", &u1));
  1297. test_eq(0, tor_sscanf(" 3 hello", "%2u", &u1)); /* not even in this case */
  1298. test_eq(1, tor_sscanf("3 hello", "%u", &u1)); /* but trailing is alright */
  1299. /* Numbers (ie. %u) */
  1300. test_eq(0, tor_sscanf("hello world 3", "hello worlb %u", &u1)); /* d vs b */
  1301. test_eq(1, tor_sscanf("12345", "%u", &u1));
  1302. test_eq(12345u, u1);
  1303. test_eq(1, tor_sscanf("12346 ", "%u", &u1));
  1304. test_eq(12346u, u1);
  1305. test_eq(0, tor_sscanf(" 12347", "%u", &u1));
  1306. test_eq(1, tor_sscanf(" 12348", " %u", &u1));
  1307. test_eq(12348u, u1);
  1308. test_eq(1, tor_sscanf("0", "%u", &u1));
  1309. test_eq(0u, u1);
  1310. test_eq(1, tor_sscanf("0000", "%u", &u2));
  1311. test_eq(0u, u2);
  1312. test_eq(0, tor_sscanf("", "%u", &u1)); /* absent number */
  1313. test_eq(0, tor_sscanf("A", "%u", &u1)); /* bogus number */
  1314. test_eq(0, tor_sscanf("-1", "%u", &u1)); /* negative number */
  1315. test_eq(1, tor_sscanf("4294967295", "%u", &u1)); /* UINT32_MAX should work */
  1316. test_eq(4294967295u, u1);
  1317. test_eq(0, tor_sscanf("4294967296", "%u", &u1)); /* But not at 32 bits */
  1318. test_eq(1, tor_sscanf("4294967296", "%9u", &u1)); /* but parsing only 9... */
  1319. test_eq(429496729u, u1);
  1320. /* Numbers with size (eg. %2u) */
  1321. test_eq(0, tor_sscanf("-1", "%2u", &u1));
  1322. test_eq(2, tor_sscanf("123456", "%2u%u", &u1, &u2));
  1323. test_eq(12u, u1);
  1324. test_eq(3456u, u2);
  1325. test_eq(1, tor_sscanf("123456", "%8u", &u1));
  1326. test_eq(123456u, u1);
  1327. test_eq(1, tor_sscanf("123457 ", "%8u", &u1));
  1328. test_eq(123457u, u1);
  1329. test_eq(0, tor_sscanf(" 123456", "%8u", &u1));
  1330. test_eq(3, tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3));
  1331. test_eq(12u, u1);
  1332. test_eq(3u, u2);
  1333. test_eq(456u, u3);
  1334. test_eq(3, tor_sscanf("67:8:099", "%2u:%2u:%3u", &u1, &u2, &u3)); /* 0s */
  1335. test_eq(67u, u1);
  1336. test_eq(8u, u2);
  1337. test_eq(99u, u3);
  1338. /* %u does not match space.*/
  1339. test_eq(2, tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3));
  1340. test_eq(12u, u1);
  1341. test_eq(3u, u2);
  1342. /* %u does not match negative numbers. */
  1343. test_eq(2, tor_sscanf("67:8:-9", "%2u:%2u:%3u", &u1, &u2, &u3));
  1344. test_eq(67u, u1);
  1345. test_eq(8u, u2);
  1346. /* Arbitrary amounts of 0-padding are okay */
  1347. test_eq(3, tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u",
  1348. &u1, &u2, &u3));
  1349. test_eq(12u, u1);
  1350. test_eq(3u, u2);
  1351. test_eq(99u, u3);
  1352. /* Hex (ie. %x) */
  1353. test_eq(3, tor_sscanf("1234 02aBcdEf ff", "%x %x %x", &u1, &u2, &u3));
  1354. test_eq(0x1234, u1);
  1355. test_eq(0x2ABCDEF, u2);
  1356. test_eq(0xFF, u3);
  1357. /* Width works on %x */
  1358. test_eq(3, tor_sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3));
  1359. test_eq(0xf00d, u1);
  1360. test_eq(0xcafe, u2);
  1361. test_eq(444, u3);
  1362. /* Literal '%' (ie. '%%') */
  1363. test_eq(1, tor_sscanf("99% fresh", "%3u%% fresh", &u1));
  1364. test_eq(99, u1);
  1365. test_eq(0, tor_sscanf("99 fresh", "%% %3u %s", &u1, s1));
  1366. test_eq(1, tor_sscanf("99 fresh", "%3u%% %s", &u1, s1));
  1367. test_eq(2, tor_sscanf("99 fresh", "%3u %5s %%", &u1, s1));
  1368. test_eq(99, u1);
  1369. test_streq(s1, "fresh");
  1370. test_eq(1, tor_sscanf("% boo", "%% %3s", s1));
  1371. test_streq("boo", s1);
  1372. /* Strings (ie. %s) */
  1373. test_eq(2, tor_sscanf("hello", "%3s%7s", s1, s2));
  1374. test_streq(s1, "hel");
  1375. test_streq(s2, "lo");
  1376. test_eq(2, tor_sscanf("WD40", "%2s%u", s3, &u1)); /* %s%u */
  1377. test_streq(s3, "WD");
  1378. test_eq(40, u1);
  1379. test_eq(2, tor_sscanf("WD40", "%3s%u", s3, &u1)); /* %s%u */
  1380. test_streq(s3, "WD4");
  1381. test_eq(0, u1);
  1382. test_eq(2, tor_sscanf("76trombones", "%6u%9s", &u1, s1)); /* %u%s */
  1383. test_eq(76, u1);
  1384. test_streq(s1, "trombones");
  1385. test_eq(1, tor_sscanf("prettylongstring", "%999s", s1));
  1386. test_streq(s1, "prettylongstring");
  1387. /* %s doesn't eat spaces */
  1388. test_eq(2, tor_sscanf("hello world", "%9s %9s", s1, s2));
  1389. test_streq(s1, "hello");
  1390. test_streq(s2, "world");
  1391. test_eq(2, tor_sscanf("bye world?", "%9s %9s", s1, s2));
  1392. test_streq(s1, "bye");
  1393. test_streq(s2, "");
  1394. test_eq(3, tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3)); /* %s can be empty. */
  1395. test_streq(s1, "hi");
  1396. test_streq(s2, "");
  1397. test_streq(s3, "");
  1398. test_eq(3, tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
  1399. test_eq(4, tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
  1400. test_eq(' ', ch);
  1401. r = tor_sscanf("12345 -67890 -1", "%d %ld %d", &int1, &lng1, &int2);
  1402. test_eq(r,3);
  1403. test_eq(int1, 12345);
  1404. test_eq(lng1, -67890);
  1405. test_eq(int2, -1);
  1406. #if SIZEOF_INT == 4
  1407. r = tor_sscanf("-2147483648. 2147483647.", "%d. %d.", &int1, &int2);
  1408. test_eq(r,2);
  1409. test_eq(int1, -2147483647-1);
  1410. test_eq(int2, 2147483647);
  1411. r = tor_sscanf("-2147483679.", "%d.", &int1);
  1412. test_eq(r,0);
  1413. r = tor_sscanf("2147483678.", "%d.", &int1);
  1414. test_eq(r,0);
  1415. #elif SIZEOF_INT == 8
  1416. r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
  1417. "%d. %d.", &int1, &int2);
  1418. test_eq(r,2);
  1419. test_eq(int1, -9223372036854775807-1);
  1420. test_eq(int2, 9223372036854775807);
  1421. r = tor_sscanf("-9223372036854775809.", "%d.", &int1);
  1422. test_eq(r,0);
  1423. r = tor_sscanf("9223372036854775808.", "%d.", &int1);
  1424. test_eq(r,0);
  1425. #endif
  1426. #if SIZEOF_LONG == 4
  1427. r = tor_sscanf("-2147483648. 2147483647.", "%ld. %ld.", &lng1, &lng2);
  1428. test_eq(r,2);
  1429. test_eq(lng1, -2147483647 - 1);
  1430. test_eq(lng2, 2147483647);
  1431. #elif SIZEOF_LONG == 8
  1432. r = tor_sscanf("-9223372036854775808. 9223372036854775807.",
  1433. "%ld. %ld.", &lng1, &lng2);
  1434. test_eq(r,2);
  1435. test_eq(lng1, -9223372036854775807L - 1);
  1436. test_eq(lng2, 9223372036854775807L);
  1437. r = tor_sscanf("-9223372036854775808. 9223372036854775808.",
  1438. "%ld. %ld.", &lng1, &lng2);
  1439. test_eq(r,1);
  1440. r = tor_sscanf("-9223372036854775809. 9223372036854775808.",
  1441. "%ld. %ld.", &lng1, &lng2);
  1442. test_eq(r,0);
  1443. #endif
  1444. r = tor_sscanf("123.456 .000007 -900123123.2000787 00003.2",
  1445. "%lf %lf %lf %lf", &d1,&d2,&d3,&d4);
  1446. test_eq(r,4);
  1447. test_feq(d1, 123.456);
  1448. test_feq(d2, .000007);
  1449. test_feq(d3, -900123123.2000787);
  1450. test_feq(d4, 3.2);
  1451. done:
  1452. ;
  1453. }
  1454. static void
  1455. test_util_path_is_relative(void)
  1456. {
  1457. /* OS-independent tests */
  1458. test_eq(1, path_is_relative(""));
  1459. test_eq(1, path_is_relative("dir"));
  1460. test_eq(1, path_is_relative("dir/"));
  1461. test_eq(1, path_is_relative("./dir"));
  1462. test_eq(1, path_is_relative("../dir"));
  1463. test_eq(0, path_is_relative("/"));
  1464. test_eq(0, path_is_relative("/dir"));
  1465. test_eq(0, path_is_relative("/dir/"));
  1466. /* Windows */
  1467. #ifdef _WIN32
  1468. /* I don't have Windows so I can't test this, hence the "#ifdef
  1469. 0". These are tests that look useful, so please try to get them
  1470. running and uncomment if it all works as it should */
  1471. test_eq(1, path_is_relative("dir"));
  1472. test_eq(1, path_is_relative("dir\\"));
  1473. test_eq(1, path_is_relative("dir\\a:"));
  1474. test_eq(1, path_is_relative("dir\\a:\\"));
  1475. test_eq(1, path_is_relative("http:\\dir"));
  1476. test_eq(0, path_is_relative("\\dir"));
  1477. test_eq(0, path_is_relative("a:\\dir"));
  1478. test_eq(0, path_is_relative("z:\\dir"));
  1479. #endif
  1480. done:
  1481. ;
  1482. }
  1483. /** Run unittests for memory pool allocator */
  1484. static void
  1485. test_util_mempool(void)
  1486. {
  1487. mp_pool_t *pool = NULL;
  1488. smartlist_t *allocated = NULL;
  1489. int i;
  1490. pool = mp_pool_new(1, 100);
  1491. test_assert(pool);
  1492. test_assert(pool->new_chunk_capacity >= 100);
  1493. test_assert(pool->item_alloc_size >= sizeof(void*)+1);
  1494. mp_pool_destroy(pool);
  1495. pool = NULL;
  1496. pool = mp_pool_new(241, 2500);
  1497. test_assert(pool);
  1498. test_assert(pool->new_chunk_capacity >= 10);
  1499. test_assert(pool->item_alloc_size >= sizeof(void*)+241);
  1500. test_eq(pool->item_alloc_size & 0x03, 0);
  1501. test_assert(pool->new_chunk_capacity < 60);
  1502. allocated = smartlist_new();
  1503. for (i = 0; i < 20000; ++i) {
  1504. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  1505. void *m = mp_pool_get(pool);
  1506. memset(m, 0x09, 241);
  1507. smartlist_add(allocated, m);
  1508. //printf("%d: %p\n", i, m);
  1509. //mp_pool_assert_ok(pool);
  1510. } else {
  1511. int idx = crypto_rand_int(smartlist_len(allocated));
  1512. void *m = smartlist_get(allocated, idx);
  1513. //printf("%d: free %p\n", i, m);
  1514. smartlist_del(allocated, idx);
  1515. mp_pool_release(m);
  1516. //mp_pool_assert_ok(pool);
  1517. }
  1518. if (crypto_rand_int(777)==0)
  1519. mp_pool_clean(pool, 1, 1);
  1520. if (i % 777)
  1521. mp_pool_assert_ok(pool);
  1522. }
  1523. done:
  1524. if (allocated) {
  1525. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  1526. mp_pool_assert_ok(pool);
  1527. mp_pool_clean(pool, 0, 0);
  1528. mp_pool_assert_ok(pool);
  1529. smartlist_free(allocated);
  1530. }
  1531. if (pool)
  1532. mp_pool_destroy(pool);
  1533. }
  1534. /** Run unittests for memory area allocator */
  1535. static void
  1536. test_util_memarea(void)
  1537. {
  1538. memarea_t *area = memarea_new();
  1539. char *p1, *p2, *p3, *p1_orig;
  1540. void *malloced_ptr = NULL;
  1541. int i;
  1542. test_assert(area);
  1543. p1_orig = p1 = memarea_alloc(area,64);
  1544. p2 = memarea_alloc_zero(area,52);
  1545. p3 = memarea_alloc(area,11);
  1546. test_assert(memarea_owns_ptr(area, p1));
  1547. test_assert(memarea_owns_ptr(area, p2));
  1548. test_assert(memarea_owns_ptr(area, p3));
  1549. /* Make sure we left enough space. */
  1550. test_assert(p1+64 <= p2);
  1551. test_assert(p2+52 <= p3);
  1552. /* Make sure we aligned. */
  1553. test_eq(((uintptr_t)p1) % sizeof(void*), 0);
  1554. test_eq(((uintptr_t)p2) % sizeof(void*), 0);
  1555. test_eq(((uintptr_t)p3) % sizeof(void*), 0);
  1556. test_assert(!memarea_owns_ptr(area, p3+8192));
  1557. test_assert(!memarea_owns_ptr(area, p3+30));
  1558. test_assert(tor_mem_is_zero(p2, 52));
  1559. /* Make sure we don't overalign. */
  1560. p1 = memarea_alloc(area, 1);
  1561. p2 = memarea_alloc(area, 1);
  1562. test_eq(p1+sizeof(void*), p2);
  1563. {
  1564. malloced_ptr = tor_malloc(64);
  1565. test_assert(!memarea_owns_ptr(area, malloced_ptr));
  1566. tor_free(malloced_ptr);
  1567. }
  1568. /* memarea_memdup */
  1569. {
  1570. malloced_ptr = tor_malloc(64);
  1571. crypto_rand((char*)malloced_ptr, 64);
  1572. p1 = memarea_memdup(area, malloced_ptr, 64);
  1573. test_assert(p1 != malloced_ptr);
  1574. test_memeq(p1, malloced_ptr, 64);
  1575. tor_free(malloced_ptr);
  1576. }
  1577. /* memarea_strdup. */
  1578. p1 = memarea_strdup(area,"");
  1579. p2 = memarea_strdup(area, "abcd");
  1580. test_assert(p1);
  1581. test_assert(p2);
  1582. test_streq(p1, "");
  1583. test_streq(p2, "abcd");
  1584. /* memarea_strndup. */
  1585. {
  1586. const char *s = "Ad ogni porta batte la morte e grida: il nome!";
  1587. /* (From Turandot, act 3.) */
  1588. size_t len = strlen(s);
  1589. p1 = memarea_strndup(area, s, 1000);
  1590. p2 = memarea_strndup(area, s, 10);
  1591. test_streq(p1, s);
  1592. test_assert(p2 >= p1 + len + 1);
  1593. test_memeq(s, p2, 10);
  1594. test_eq(p2[10], '\0');
  1595. p3 = memarea_strndup(area, s, len);
  1596. test_streq(p3, s);
  1597. p3 = memarea_strndup(area, s, len-1);
  1598. test_memeq(s, p3, len-1);
  1599. test_eq(p3[len-1], '\0');
  1600. }
  1601. memarea_clear(area);
  1602. p1 = memarea_alloc(area, 1);
  1603. test_eq(p1, p1_orig);
  1604. memarea_clear(area);
  1605. /* Check for running over an area's size. */
  1606. for (i = 0; i < 512; ++i) {
  1607. p1 = memarea_alloc(area, crypto_rand_int(5)+1);
  1608. test_assert(memarea_owns_ptr(area, p1));
  1609. }
  1610. memarea_assert_ok(area);
  1611. /* Make sure we can allocate a too-big object. */
  1612. p1 = memarea_alloc_zero(area, 9000);
  1613. p2 = memarea_alloc_zero(area, 16);
  1614. test_assert(memarea_owns_ptr(area, p1));
  1615. test_assert(memarea_owns_ptr(area, p2));
  1616. done:
  1617. memarea_drop_all(area);
  1618. tor_free(malloced_ptr);
  1619. }
  1620. /** Run unit tests for utility functions to get file names relative to
  1621. * the data directory. */
  1622. static void
  1623. test_util_datadir(void)
  1624. {
  1625. char buf[1024];
  1626. char *f = NULL;
  1627. char *temp_dir = NULL;
  1628. temp_dir = get_datadir_fname(NULL);
  1629. f = get_datadir_fname("state");
  1630. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
  1631. test_streq(f, buf);
  1632. tor_free(f);
  1633. f = get_datadir_fname2("cache", "thingy");
  1634. tor_snprintf(buf, sizeof(buf),
  1635. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
  1636. test_streq(f, buf);
  1637. tor_free(f);
  1638. f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
  1639. tor_snprintf(buf, sizeof(buf),
  1640. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
  1641. test_streq(f, buf);
  1642. tor_free(f);
  1643. f = get_datadir_fname_suffix("cache", ".foo");
  1644. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
  1645. temp_dir);
  1646. test_streq(f, buf);
  1647. done:
  1648. tor_free(f);
  1649. tor_free(temp_dir);
  1650. }
  1651. static void
  1652. test_util_strtok(void)
  1653. {
  1654. char buf[128];
  1655. char buf2[128];
  1656. int i;
  1657. char *cp1, *cp2;
  1658. for (i = 0; i < 3; i++) {
  1659. const char *pad1="", *pad2="";
  1660. switch (i) {
  1661. case 0:
  1662. break;
  1663. case 1:
  1664. pad1 = " ";
  1665. pad2 = "!";
  1666. break;
  1667. case 2:
  1668. pad1 = " ";
  1669. pad2 = ";!";
  1670. break;
  1671. }
  1672. tor_snprintf(buf, sizeof(buf), "%s", pad1);
  1673. tor_snprintf(buf2, sizeof(buf2), "%s", pad2);
  1674. test_assert(NULL == tor_strtok_r_impl(buf, " ", &cp1));
  1675. test_assert(NULL == tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  1676. tor_snprintf(buf, sizeof(buf),
  1677. "%sGraved on the dark in gestures of descent%s", pad1, pad1);
  1678. tor_snprintf(buf2, sizeof(buf2),
  1679. "%sthey.seemed;;their!.own;most.perfect;monument%s",pad2,pad2);
  1680. /* -- "Year's End", Richard Wilbur */
  1681. test_streq("Graved", tor_strtok_r_impl(buf, " ", &cp1));
  1682. test_streq("they", tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  1683. #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
  1684. #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
  1685. test_streq("on", S1());
  1686. test_streq("the", S1());
  1687. test_streq("dark", S1());
  1688. test_streq("seemed", S2());
  1689. test_streq("their", S2());
  1690. test_streq("own", S2());
  1691. test_streq("in", S1());
  1692. test_streq("gestures", S1());
  1693. test_streq("of", S1());
  1694. test_streq("most", S2());
  1695. test_streq("perfect", S2());
  1696. test_streq("descent", S1());
  1697. test_streq("monument", S2());
  1698. test_eq_ptr(NULL, S1());
  1699. test_eq_ptr(NULL, S2());
  1700. }
  1701. buf[0] = 0;
  1702. test_eq_ptr(NULL, tor_strtok_r_impl(buf, " ", &cp1));
  1703. test_eq_ptr(NULL, tor_strtok_r_impl(buf, "!", &cp1));
  1704. strlcpy(buf, "Howdy!", sizeof(buf));
  1705. test_streq("Howdy", tor_strtok_r_impl(buf, "!", &cp1));
  1706. test_eq_ptr(NULL, tor_strtok_r_impl(NULL, "!", &cp1));
  1707. strlcpy(buf, " ", sizeof(buf));
  1708. test_eq_ptr(NULL, tor_strtok_r_impl(buf, " ", &cp1));
  1709. strlcpy(buf, " ", sizeof(buf));
  1710. test_eq_ptr(NULL, tor_strtok_r_impl(buf, " ", &cp1));
  1711. strlcpy(buf, "something ", sizeof(buf));
  1712. test_streq("something", tor_strtok_r_impl(buf, " ", &cp1));
  1713. test_eq_ptr(NULL, tor_strtok_r_impl(NULL, ";", &cp1));
  1714. done:
  1715. ;
  1716. }
  1717. static void
  1718. test_util_find_str_at_start_of_line(void *ptr)
  1719. {
  1720. const char *long_string =
  1721. "howdy world. how are you? i hope it's fine.\n"
  1722. "hello kitty\n"
  1723. "third line";
  1724. char *line2 = strchr(long_string,'\n')+1;
  1725. char *line3 = strchr(line2,'\n')+1;
  1726. const char *short_string = "hello kitty\n"
  1727. "second line\n";
  1728. char *short_line2 = strchr(short_string,'\n')+1;
  1729. (void)ptr;
  1730. test_eq_ptr(long_string, find_str_at_start_of_line(long_string, ""));
  1731. test_eq_ptr(NULL, find_str_at_start_of_line(short_string, "nonsense"));
  1732. test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "nonsense"));
  1733. test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "\n"));
  1734. test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "how "));
  1735. test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "kitty"));
  1736. test_eq_ptr(long_string, find_str_at_start_of_line(long_string, "h"));
  1737. test_eq_ptr(long_string, find_str_at_start_of_line(long_string, "how"));
  1738. test_eq_ptr(line2, find_str_at_start_of_line(long_string, "he"));
  1739. test_eq_ptr(line2, find_str_at_start_of_line(long_string, "hell"));
  1740. test_eq_ptr(line2, find_str_at_start_of_line(long_string, "hello k"));
  1741. test_eq_ptr(line2, find_str_at_start_of_line(long_string, "hello kitty\n"));
  1742. test_eq_ptr(line2, find_str_at_start_of_line(long_string, "hello kitty\nt"));
  1743. test_eq_ptr(line3, find_str_at_start_of_line(long_string, "third"));
  1744. test_eq_ptr(line3, find_str_at_start_of_line(long_string, "third line"));
  1745. test_eq_ptr(NULL, find_str_at_start_of_line(long_string, "third line\n"));
  1746. test_eq_ptr(short_line2, find_str_at_start_of_line(short_string,
  1747. "second line\n"));
  1748. done:
  1749. ;
  1750. }
  1751. static void
  1752. test_util_string_is_C_identifier(void *ptr)
  1753. {
  1754. (void)ptr;
  1755. test_eq(1, string_is_C_identifier("string_is_C_identifier"));
  1756. test_eq(1, string_is_C_identifier("_string_is_C_identifier"));
  1757. test_eq(1, string_is_C_identifier("_"));
  1758. test_eq(1, string_is_C_identifier("i"));
  1759. test_eq(1, string_is_C_identifier("_____"));
  1760. test_eq(1, string_is_C_identifier("__00__"));
  1761. test_eq(1, string_is_C_identifier("__init__"));
  1762. test_eq(1, string_is_C_identifier("_0"));
  1763. test_eq(1, string_is_C_identifier("_0string_is_C_identifier"));
  1764. test_eq(1, string_is_C_identifier("_0"));
  1765. test_eq(0, string_is_C_identifier("0_string_is_C_identifier"));
  1766. test_eq(0, string_is_C_identifier("0"));
  1767. test_eq(0, string_is_C_identifier(""));
  1768. test_eq(0, string_is_C_identifier(";"));
  1769. test_eq(0, string_is_C_identifier("i;"));
  1770. test_eq(0, string_is_C_identifier("_;"));
  1771. test_eq(0, string_is_C_identifier("í"));
  1772. test_eq(0, string_is_C_identifier("ñ"));
  1773. done:
  1774. ;
  1775. }
  1776. static void
  1777. test_util_asprintf(void *ptr)
  1778. {
  1779. #define LOREMIPSUM \
  1780. "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
  1781. char *cp=NULL, *cp2=NULL;
  1782. int r;
  1783. (void)ptr;
  1784. /* simple string */
  1785. r = tor_asprintf(&cp, "simple string 100%% safe");
  1786. test_assert(cp);
  1787. test_streq("simple string 100% safe", cp);
  1788. test_eq(strlen(cp), r);
  1789. /* empty string */
  1790. r = tor_asprintf(&cp, "%s", "");
  1791. test_assert(cp);
  1792. test_streq("", cp);
  1793. test_eq(strlen(cp), r);
  1794. /* numbers (%i) */
  1795. r = tor_asprintf(&cp, "I like numbers-%2i, %i, etc.", -1, 2);
  1796. test_assert(cp);
  1797. test_streq("I like numbers--1, 2, etc.", cp);
  1798. test_eq(strlen(cp), r);
  1799. /* numbers (%d) */
  1800. r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
  1801. test_assert(cp2);
  1802. test_eq(strlen(cp2), r);
  1803. test_streq("First=101, Second=202", cp2);
  1804. test_assert(cp != cp2);
  1805. tor_free(cp);
  1806. tor_free(cp2);
  1807. /* Glass-box test: a string exactly 128 characters long. */
  1808. r = tor_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
  1809. test_assert(cp);
  1810. test_eq(128, r);
  1811. test_assert(cp[128] == '\0');
  1812. test_streq("Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM, cp);
  1813. tor_free(cp);
  1814. /* String longer than 128 characters */
  1815. r = tor_asprintf(&cp, "1: %s 2: %s 3: %s",
  1816. LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
  1817. test_assert(cp);
  1818. test_eq(strlen(cp), r);
  1819. test_streq("1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM, cp);
  1820. done:
  1821. tor_free(cp);
  1822. tor_free(cp2);
  1823. }
  1824. static void
  1825. test_util_listdir(void *ptr)
  1826. {
  1827. smartlist_t *dir_contents = NULL;
  1828. char *fname1=NULL, *fname2=NULL, *fname3=NULL, *dir1=NULL, *dirname=NULL;
  1829. int r;
  1830. (void)ptr;
  1831. fname1 = tor_strdup(get_fname("hopscotch"));
  1832. fname2 = tor_strdup(get_fname("mumblety-peg"));
  1833. fname3 = tor_strdup(get_fname(".hidden-file"));
  1834. dir1 = tor_strdup(get_fname("some-directory"));
  1835. dirname = tor_strdup(get_fname(NULL));
  1836. test_eq(0, write_str_to_file(fname1, "X\n", 0));
  1837. test_eq(0, write_str_to_file(fname2, "Y\n", 0));
  1838. test_eq(0, write_str_to_file(fname3, "Z\n", 0));
  1839. #ifdef _WIN32
  1840. r = mkdir(dir1);
  1841. #else
  1842. r = mkdir(dir1, 0700);
  1843. #endif
  1844. if (r) {
  1845. fprintf(stderr, "Can't create directory %s:", dir1);
  1846. perror("");
  1847. exit(1);
  1848. }
  1849. dir_contents = tor_listdir(dirname);
  1850. test_assert(dir_contents);
  1851. /* make sure that each filename is listed. */
  1852. test_assert(smartlist_string_isin_case(dir_contents, "hopscotch"));
  1853. test_assert(smartlist_string_isin_case(dir_contents, "mumblety-peg"));
  1854. test_assert(smartlist_string_isin_case(dir_contents, ".hidden-file"));
  1855. test_assert(smartlist_string_isin_case(dir_contents, "some-directory"));
  1856. test_assert(!smartlist_string_isin(dir_contents, "."));
  1857. test_assert(!smartlist_string_isin(dir_contents, ".."));
  1858. done:
  1859. tor_free(fname1);
  1860. tor_free(fname2);
  1861. tor_free(dirname);
  1862. if (dir_contents) {
  1863. SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp));
  1864. smartlist_free(dir_contents);
  1865. }
  1866. }
  1867. static void
  1868. test_util_parent_dir(void *ptr)
  1869. {
  1870. char *cp;
  1871. (void)ptr;
  1872. #define T(output,expect_ok,input) \
  1873. do { \
  1874. int ok; \
  1875. cp = tor_strdup(input); \
  1876. ok = get_parent_directory(cp); \
  1877. tt_int_op(expect_ok, ==, ok); \
  1878. if (ok==0) \
  1879. tt_str_op(output, ==, cp); \
  1880. tor_free(cp); \
  1881. } while (0);
  1882. T("/home/wombat", 0, "/home/wombat/knish");
  1883. T("/home/wombat", 0, "/home/wombat/knish/");
  1884. T("/home/wombat", 0, "/home/wombat/knish///");
  1885. T("./home/wombat", 0, "./home/wombat/knish/");
  1886. T("/", 0, "/home");
  1887. T("/", 0, "/home//");
  1888. T(".", 0, "./wombat");
  1889. T(".", 0, "./wombat/");
  1890. T(".", 0, "./wombat//");
  1891. T("wombat", 0, "wombat/foo");
  1892. T("wombat/..", 0, "wombat/../foo");
  1893. T("wombat/../", 0, "wombat/..//foo"); /* Is this correct? */
  1894. T("wombat/.", 0, "wombat/./foo");
  1895. T("wombat/./", 0, "wombat/.//foo"); /* Is this correct? */
  1896. T("wombat", 0, "wombat/..//");
  1897. T("wombat", 0, "wombat/foo/");
  1898. T("wombat", 0, "wombat/.foo");
  1899. T("wombat", 0, "wombat/.foo/");
  1900. T("wombat", -1, "");
  1901. T("w", -1, "");
  1902. T("wombat", 0, "wombat/knish");
  1903. T("/", 0, "/");
  1904. T("/", 0, "////");
  1905. done:
  1906. tor_free(cp);
  1907. }
  1908. #ifdef _WIN32
  1909. static void
  1910. test_util_load_win_lib(void *ptr)
  1911. {
  1912. HANDLE h = load_windows_system_library(_T("advapi32.dll"));
  1913. (void) ptr;
  1914. tt_assert(h);
  1915. done:
  1916. if (h)
  1917. CloseHandle(h);
  1918. }
  1919. #endif
  1920. static void
  1921. clear_hex_errno(char *hex_errno)
  1922. {
  1923. memset(hex_errno, '\0', HEX_ERRNO_SIZE + 1);
  1924. }
  1925. static void
  1926. test_util_exit_status(void *ptr)
  1927. {
  1928. /* Leave an extra byte for a \0 so we can do string comparison */
  1929. char hex_errno[HEX_ERRNO_SIZE + 1];
  1930. int n;
  1931. (void)ptr;
  1932. clear_hex_errno(hex_errno);
  1933. n = format_helper_exit_status(0, 0, hex_errno);
  1934. test_streq("0/0\n", hex_errno);
  1935. test_eq(n, strlen(hex_errno));
  1936. clear_hex_errno(hex_errno);
  1937. n = format_helper_exit_status(0, 0x7FFFFFFF, hex_errno);
  1938. test_streq("0/7FFFFFFF\n", hex_errno);
  1939. test_eq(n, strlen(hex_errno));
  1940. clear_hex_errno(hex_errno);
  1941. n = format_helper_exit_status(0xFF, -0x80000000, hex_errno);
  1942. test_streq("FF/-80000000\n", hex_errno);
  1943. test_eq(n, strlen(hex_errno));
  1944. clear_hex_errno(hex_errno);
  1945. n = format_helper_exit_status(0x7F, 0, hex_errno);
  1946. test_streq("7F/0\n", hex_errno);
  1947. test_eq(n, strlen(hex_errno));
  1948. clear_hex_errno(hex_errno);
  1949. n = format_helper_exit_status(0x08, -0x242, hex_errno);
  1950. test_streq("8/-242\n", hex_errno);
  1951. test_eq(n, strlen(hex_errno));
  1952. done:
  1953. ;
  1954. }
  1955. #ifndef _WIN32
  1956. /** Check that fgets waits until a full line, and not return a partial line, on
  1957. * a EAGAIN with a non-blocking pipe */
  1958. static void
  1959. test_util_fgets_eagain(void *ptr)
  1960. {
  1961. int test_pipe[2] = {-1, -1};
  1962. int retval;
  1963. ssize_t retlen;
  1964. char *retptr;
  1965. FILE *test_stream = NULL;
  1966. char buf[10];
  1967. (void)ptr;
  1968. /* Set up a pipe to test on */
  1969. retval = pipe(test_pipe);
  1970. tt_int_op(retval, >=, 0);
  1971. /* Set up the read-end to be non-blocking */
  1972. retval = fcntl(test_pipe[0], F_SETFL, O_NONBLOCK);
  1973. tt_int_op(retval, >=, 0);
  1974. /* Open it as a stdio stream */
  1975. test_stream = fdopen(test_pipe[0], "r");
  1976. tt_ptr_op(test_stream, !=, NULL);
  1977. /* Send in a partial line */
  1978. retlen = write(test_pipe[1], "A", 1);
  1979. tt_int_op(retlen, ==, 1);
  1980. retptr = fgets(buf, sizeof(buf), test_stream);
  1981. tt_want(retptr == NULL);
  1982. tt_int_op(errno, ==, EAGAIN);
  1983. /* Send in the rest */
  1984. retlen = write(test_pipe[1], "B\n", 2);
  1985. tt_int_op(retlen, ==, 2);
  1986. retptr = fgets(buf, sizeof(buf), test_stream);
  1987. tt_ptr_op(retptr, ==, buf);
  1988. tt_str_op(buf, ==, "AB\n");
  1989. /* Send in a full line */
  1990. retlen = write(test_pipe[1], "CD\n", 3);
  1991. tt_int_op(retlen, ==, 3);
  1992. retptr = fgets(buf, sizeof(buf), test_stream);
  1993. tt_ptr_op(retptr, ==, buf);
  1994. tt_str_op(buf, ==, "CD\n");
  1995. /* Send in a partial line */
  1996. retlen = write(test_pipe[1], "E", 1);
  1997. tt_int_op(retlen, ==, 1);
  1998. retptr = fgets(buf, sizeof(buf), test_stream);
  1999. tt_ptr_op(retptr, ==, NULL);
  2000. tt_int_op(errno, ==, EAGAIN);
  2001. /* Send in the rest */
  2002. retlen = write(test_pipe[1], "F\n", 2);
  2003. tt_int_op(retlen, ==, 2);
  2004. retptr = fgets(buf, sizeof(buf), test_stream);
  2005. tt_ptr_op(retptr, ==, buf);
  2006. tt_str_op(buf, ==, "EF\n");
  2007. /* Send in a full line and close */
  2008. retlen = write(test_pipe[1], "GH", 2);
  2009. tt_int_op(retlen, ==, 2);
  2010. retval = close(test_pipe[1]);
  2011. test_pipe[1] = -1;
  2012. tt_int_op(retval, ==, 0);
  2013. retptr = fgets(buf, sizeof(buf), test_stream);
  2014. tt_ptr_op(retptr, ==, buf);
  2015. tt_str_op(buf, ==, "GH");
  2016. /* Check for EOF */
  2017. retptr = fgets(buf, sizeof(buf), test_stream);
  2018. tt_ptr_op(retptr, ==, NULL);
  2019. tt_int_op(feof(test_stream), >, 0);
  2020. done:
  2021. if (test_stream != NULL)
  2022. fclose(test_stream);
  2023. if (test_pipe[0] != -1)
  2024. close(test_pipe[0]);
  2025. if (test_pipe[1] != -1)
  2026. close(test_pipe[1]);
  2027. }
  2028. #endif
  2029. /** Helper function for testing tor_spawn_background */
  2030. static void
  2031. run_util_spawn_background(const char *argv[], const char *expected_out,
  2032. const char *expected_err, int expected_exit,
  2033. int expected_status)
  2034. {
  2035. int retval, exit_code;
  2036. ssize_t pos;
  2037. process_handle_t *process_handle=NULL;
  2038. char stdout_buf[100], stderr_buf[100];
  2039. int status;
  2040. /* Start the program */
  2041. #ifdef _WIN32
  2042. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  2043. #else
  2044. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  2045. #endif
  2046. test_eq(expected_status, status);
  2047. if (status == PROCESS_STATUS_ERROR)
  2048. return;
  2049. test_assert(process_handle != NULL);
  2050. test_eq(expected_status, process_handle->status);
  2051. #ifdef _WIN32
  2052. test_assert(process_handle->stdout_pipe != INVALID_HANDLE_VALUE);
  2053. test_assert(process_handle->stderr_pipe != INVALID_HANDLE_VALUE);
  2054. #else
  2055. test_assert(process_handle->stdout_pipe > 0);
  2056. test_assert(process_handle->stderr_pipe > 0);
  2057. #endif
  2058. /* Check stdout */
  2059. pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
  2060. sizeof(stdout_buf) - 1);
  2061. tt_assert(pos >= 0);
  2062. stdout_buf[pos] = '\0';
  2063. test_eq(strlen(expected_out), pos);
  2064. test_streq(expected_out, stdout_buf);
  2065. /* Check it terminated correctly */
  2066. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  2067. test_eq(PROCESS_EXIT_EXITED, retval);
  2068. test_eq(expected_exit, exit_code);
  2069. // TODO: Make test-child exit with something other than 0
  2070. /* Check stderr */
  2071. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  2072. sizeof(stderr_buf) - 1);
  2073. test_assert(pos >= 0);
  2074. stderr_buf[pos] = '\0';
  2075. test_streq(expected_err, stderr_buf);
  2076. test_eq(strlen(expected_err), pos);
  2077. done:
  2078. if (process_handle)
  2079. tor_process_handle_destroy(process_handle, 1);
  2080. }
  2081. /** Check that we can launch a process and read the output */
  2082. static void
  2083. test_util_spawn_background_ok(void *ptr)
  2084. {
  2085. #ifdef _WIN32
  2086. const char *argv[] = {"test-child.exe", "--test", NULL};
  2087. const char *expected_out = "OUT\r\n--test\r\nSLEEPING\r\nDONE\r\n";
  2088. const char *expected_err = "ERR\r\n";
  2089. #else
  2090. const char *argv[] = {BUILDDIR "/src/test/test-child", "--test", NULL};
  2091. const char *expected_out = "OUT\n--test\nSLEEPING\nDONE\n";
  2092. const char *expected_err = "ERR\n";
  2093. #endif
  2094. (void)ptr;
  2095. run_util_spawn_background(argv, expected_out, expected_err, 0,
  2096. PROCESS_STATUS_RUNNING);
  2097. }
  2098. /** Check that failing to find the executable works as expected */
  2099. static void
  2100. test_util_spawn_background_fail(void *ptr)
  2101. {
  2102. #ifndef BUILDDIR
  2103. #define BUILDDIR "."
  2104. #endif
  2105. const char *argv[] = {BUILDDIR "/src/test/no-such-file", "--test", NULL};
  2106. const char *expected_err = "";
  2107. char expected_out[1024];
  2108. char code[32];
  2109. #ifdef _WIN32
  2110. const int expected_status = PROCESS_STATUS_ERROR;
  2111. #else
  2112. /* TODO: Once we can signal failure to exec, set this to be
  2113. * PROCESS_STATUS_ERROR */
  2114. const int expected_status = PROCESS_STATUS_RUNNING;
  2115. #endif
  2116. (void)ptr;
  2117. tor_snprintf(code, sizeof(code), "%x/%x",
  2118. 9 /* CHILD_STATE_FAILEXEC */ , ENOENT);
  2119. tor_snprintf(expected_out, sizeof(expected_out),
  2120. "ERR: Failed to spawn background process - code %s\n", code);
  2121. run_util_spawn_background(argv, expected_out, expected_err, 255,
  2122. expected_status);
  2123. }
  2124. /** Test that reading from a handle returns a partial read rather than
  2125. * blocking */
  2126. static void
  2127. test_util_spawn_background_partial_read(void *ptr)
  2128. {
  2129. const int expected_exit = 0;
  2130. const int expected_status = PROCESS_STATUS_RUNNING;
  2131. int retval, exit_code;
  2132. ssize_t pos = -1;
  2133. process_handle_t *process_handle=NULL;
  2134. int status;
  2135. char stdout_buf[100], stderr_buf[100];
  2136. #ifdef _WIN32
  2137. const char *argv[] = {"test-child.exe", "--test", NULL};
  2138. const char *expected_out[] = { "OUT\r\n--test\r\nSLEEPING\r\n",
  2139. "DONE\r\n",
  2140. NULL };
  2141. const char *expected_err = "ERR\r\n";
  2142. #else
  2143. const char *argv[] = {BUILDDIR "/src/test/test-child", "--test", NULL};
  2144. const char *expected_out[] = { "OUT\n--test\nSLEEPING\n",
  2145. "DONE\n",
  2146. NULL };
  2147. const char *expected_err = "ERR\n";
  2148. int eof = 0;
  2149. #endif
  2150. int expected_out_ctr;
  2151. (void)ptr;
  2152. /* Start the program */
  2153. #ifdef _WIN32
  2154. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  2155. #else
  2156. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  2157. #endif
  2158. test_eq(expected_status, status);
  2159. test_assert(process_handle);
  2160. test_eq(expected_status, process_handle->status);
  2161. /* Check stdout */
  2162. for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
  2163. #ifdef _WIN32
  2164. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  2165. sizeof(stdout_buf) - 1, NULL);
  2166. #else
  2167. /* Check that we didn't read the end of file last time */
  2168. test_assert(!eof);
  2169. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  2170. sizeof(stdout_buf) - 1, NULL, &eof);
  2171. #endif
  2172. log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
  2173. /* We would have blocked, keep on trying */
  2174. if (0 == pos)
  2175. continue;
  2176. test_assert(pos > 0);
  2177. stdout_buf[pos] = '\0';
  2178. test_streq(expected_out[expected_out_ctr], stdout_buf);
  2179. test_eq(strlen(expected_out[expected_out_ctr]), pos);
  2180. expected_out_ctr++;
  2181. }
  2182. /* The process should have exited without writing more */
  2183. #ifdef _WIN32
  2184. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  2185. sizeof(stdout_buf) - 1,
  2186. process_handle);
  2187. test_eq(0, pos);
  2188. #else
  2189. if (!eof) {
  2190. /* We should have got all the data, but maybe not the EOF flag */
  2191. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  2192. sizeof(stdout_buf) - 1,
  2193. process_handle, &eof);
  2194. test_eq(0, pos);
  2195. test_assert(eof);
  2196. }
  2197. /* Otherwise, we got the EOF on the last read */
  2198. #endif
  2199. /* Check it terminated correctly */
  2200. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  2201. test_eq(PROCESS_EXIT_EXITED, retval);
  2202. test_eq(expected_exit, exit_code);
  2203. // TODO: Make test-child exit with something other than 0
  2204. /* Check stderr */
  2205. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  2206. sizeof(stderr_buf) - 1);
  2207. test_assert(pos >= 0);
  2208. stderr_buf[pos] = '\0';
  2209. test_streq(expected_err, stderr_buf);
  2210. test_eq(strlen(expected_err), pos);
  2211. done:
  2212. tor_process_handle_destroy(process_handle, 1);
  2213. }
  2214. /**
  2215. * Test for format_hex_number_for_helper_exit_status()
  2216. */
  2217. static void
  2218. test_util_format_hex_number(void *ptr)
  2219. {
  2220. int i, len;
  2221. char buf[HEX_ERRNO_SIZE + 1];
  2222. const struct {
  2223. const char *str;
  2224. unsigned int x;
  2225. } test_data[] = {
  2226. {"0", 0},
  2227. {"1", 1},
  2228. {"273A", 0x273a},
  2229. {"FFFF", 0xffff},
  2230. #if UINT_MAX >= 0xffffffff
  2231. {"31BC421D", 0x31bc421d},
  2232. {"FFFFFFFF", 0xffffffff},
  2233. #endif
  2234. {NULL, 0}
  2235. };
  2236. (void)ptr;
  2237. for (i = 0; test_data[i].str != NULL; ++i) {
  2238. len = format_hex_number_for_helper_exit_status(test_data[i].x,
  2239. buf, HEX_ERRNO_SIZE);
  2240. test_neq(len, 0);
  2241. buf[len] = '\0';
  2242. test_streq(buf, test_data[i].str);
  2243. }
  2244. done:
  2245. return;
  2246. }
  2247. /**
  2248. * Test that we can properly format q Windows command line
  2249. */
  2250. static void
  2251. test_util_join_win_cmdline(void *ptr)
  2252. {
  2253. /* Based on some test cases from "Parsing C++ Command-Line Arguments" in
  2254. * MSDN but we don't exercise all quoting rules because tor_join_win_cmdline
  2255. * will try to only generate simple cases for the child process to parse;
  2256. * i.e. we never embed quoted strings in arguments. */
  2257. const char *argvs[][4] = {
  2258. {"a", "bb", "CCC", NULL}, // Normal
  2259. {NULL, NULL, NULL, NULL}, // Empty argument list
  2260. {"", NULL, NULL, NULL}, // Empty argument
  2261. {"\"a", "b\"b", "CCC\"", NULL}, // Quotes
  2262. {"a\tbc", "dd dd", "E", NULL}, // Whitespace
  2263. {"a\\\\\\b", "de fg", "H", NULL}, // Backslashes
  2264. {"a\\\"b", "\\c", "D\\", NULL}, // Backslashes before quote
  2265. {"a\\\\b c", "d", "E", NULL}, // Backslashes not before quote
  2266. { NULL } // Terminator
  2267. };
  2268. const char *cmdlines[] = {
  2269. "a bb CCC",
  2270. "",
  2271. "\"\"",
  2272. "\\\"a b\\\"b CCC\\\"",
  2273. "\"a\tbc\" \"dd dd\" E",
  2274. "a\\\\\\b \"de fg\" H",
  2275. "a\\\\\\\"b \\c D\\",
  2276. "\"a\\\\b c\" d E",
  2277. NULL // Terminator
  2278. };
  2279. int i;
  2280. char *joined_argv;
  2281. (void)ptr;
  2282. for (i=0; cmdlines[i]!=NULL; i++) {
  2283. log_info(LD_GENERAL, "Joining argvs[%d], expecting <%s>", i, cmdlines[i]);
  2284. joined_argv = tor_join_win_cmdline(argvs[i]);
  2285. test_streq(cmdlines[i], joined_argv);
  2286. tor_free(joined_argv);
  2287. }
  2288. done:
  2289. ;
  2290. }
  2291. #define MAX_SPLIT_LINE_COUNT 4
  2292. struct split_lines_test_t {
  2293. const char *orig_line; // Line to be split (may contain \0's)
  2294. int orig_length; // Length of orig_line
  2295. const char *split_line[MAX_SPLIT_LINE_COUNT]; // Split lines
  2296. };
  2297. /**
  2298. * Test that we properly split a buffer into lines
  2299. */
  2300. static void
  2301. test_util_split_lines(void *ptr)
  2302. {
  2303. /* Test cases. orig_line of last test case must be NULL.
  2304. * The last element of split_line[i] must be NULL. */
  2305. struct split_lines_test_t tests[] = {
  2306. {"", 0, {NULL}},
  2307. {"foo", 3, {"foo", NULL}},
  2308. {"\n\rfoo\n\rbar\r\n", 12, {"foo", "bar", NULL}},
  2309. {"fo o\r\nb\tar", 10, {"fo o", "b.ar", NULL}},
  2310. {"\x0f""f\0o\0\n\x01""b\0r\0\r", 12, {".f.o.", ".b.r.", NULL}},
  2311. {"line 1\r\nline 2", 14, {"line 1", "line 2", NULL}},
  2312. {"line 1\r\n\r\nline 2", 16, {"line 1", "line 2", NULL}},
  2313. {"line 1\r\n\r\r\r\nline 2", 18, {"line 1", "line 2", NULL}},
  2314. {"line 1\r\n\n\n\n\rline 2", 18, {"line 1", "line 2", NULL}},
  2315. {"line 1\r\n\r\t\r\nline 3", 18, {"line 1", ".", "line 3", NULL}},
  2316. {"\n\t\r\t\nline 3", 11, {".", ".", "line 3", NULL}},
  2317. {NULL, 0, { NULL }}
  2318. };
  2319. int i, j;
  2320. char *orig_line=NULL;
  2321. smartlist_t *sl=NULL;
  2322. (void)ptr;
  2323. for (i=0; tests[i].orig_line; i++) {
  2324. sl = smartlist_new();
  2325. /* Allocate space for string and trailing NULL */
  2326. orig_line = tor_memdup(tests[i].orig_line, tests[i].orig_length + 1);
  2327. tor_split_lines(sl, orig_line, tests[i].orig_length);
  2328. j = 0;
  2329. log_info(LD_GENERAL, "Splitting test %d of length %d",
  2330. i, tests[i].orig_length);
  2331. SMARTLIST_FOREACH_BEGIN(sl, const char *, line) {
  2332. /* Check we have not got too many lines */
  2333. test_assert(j < MAX_SPLIT_LINE_COUNT);
  2334. /* Check that there actually should be a line here */
  2335. test_assert(tests[i].split_line[j] != NULL);
  2336. log_info(LD_GENERAL, "Line %d of test %d, should be <%s>",
  2337. j, i, tests[i].split_line[j]);
  2338. /* Check that the line is as expected */
  2339. test_streq(line, tests[i].split_line[j]);
  2340. j++;
  2341. } SMARTLIST_FOREACH_END(line);
  2342. /* Check that we didn't miss some lines */
  2343. test_eq_ptr(NULL, tests[i].split_line[j]);
  2344. tor_free(orig_line);
  2345. smartlist_free(sl);
  2346. sl = NULL;
  2347. }
  2348. done:
  2349. tor_free(orig_line);
  2350. smartlist_free(sl);
  2351. }
  2352. static void
  2353. test_util_di_ops(void)
  2354. {
  2355. #define LT -1
  2356. #define GT 1
  2357. #define EQ 0
  2358. const struct {
  2359. const char *a; int want_sign; const char *b;
  2360. } examples[] = {
  2361. { "Foo", EQ, "Foo" },
  2362. { "foo", GT, "bar", },
  2363. { "foobar", EQ ,"foobar" },
  2364. { "foobar", LT, "foobaw" },
  2365. { "foobar", GT, "f00bar" },
  2366. { "foobar", GT, "boobar" },
  2367. { "", EQ, "" },
  2368. { NULL, 0, NULL },
  2369. };
  2370. int i;
  2371. for (i = 0; examples[i].a; ++i) {
  2372. size_t len = strlen(examples[i].a);
  2373. int eq1, eq2, neq1, neq2, cmp1, cmp2;
  2374. test_eq(len, strlen(examples[i].b));
  2375. /* We do all of the operations, with operands in both orders. */
  2376. eq1 = tor_memeq(examples[i].a, examples[i].b, len);
  2377. eq2 = tor_memeq(examples[i].b, examples[i].a, len);
  2378. neq1 = tor_memneq(examples[i].a, examples[i].b, len);
  2379. neq2 = tor_memneq(examples[i].b, examples[i].a, len);
  2380. cmp1 = tor_memcmp(examples[i].a, examples[i].b, len);
  2381. cmp2 = tor_memcmp(examples[i].b, examples[i].a, len);
  2382. /* Check for correctness of cmp1 */
  2383. if (cmp1 < 0 && examples[i].want_sign != LT)
  2384. test_fail();
  2385. else if (cmp1 > 0 && examples[i].want_sign != GT)
  2386. test_fail();
  2387. else if (cmp1 == 0 && examples[i].want_sign != EQ)
  2388. test_fail();
  2389. /* Check for consistency of everything else with cmp1 */
  2390. test_eq(eq1, eq2);
  2391. test_eq(neq1, neq2);
  2392. test_eq(cmp1, -cmp2);
  2393. test_eq(eq1, cmp1 == 0);
  2394. test_eq(neq1, !eq1);
  2395. }
  2396. done:
  2397. ;
  2398. }
  2399. /**
  2400. * Test counting high bits
  2401. */
  2402. static void
  2403. test_util_n_bits_set(void *ptr)
  2404. {
  2405. (void)ptr;
  2406. test_eq(0, n_bits_set_u8(0));
  2407. test_eq(1, n_bits_set_u8(1));
  2408. test_eq(3, n_bits_set_u8(7));
  2409. test_eq(1, n_bits_set_u8(8));
  2410. test_eq(2, n_bits_set_u8(129));
  2411. test_eq(8, n_bits_set_u8(255));
  2412. done:
  2413. ;
  2414. }
  2415. /**
  2416. * Test LHS whitespace (and comment) eater
  2417. */
  2418. static void
  2419. test_util_eat_whitespace(void *ptr)
  2420. {
  2421. const char ws[] = { ' ', '\t', '\r' }; /* Except NL */
  2422. char str[80];
  2423. size_t i;
  2424. (void)ptr;
  2425. /* Try one leading ws */
  2426. strcpy(str, "fuubaar");
  2427. for (i = 0; i < sizeof(ws); ++i) {
  2428. str[0] = ws[i];
  2429. test_eq_ptr(str + 1, eat_whitespace(str));
  2430. test_eq_ptr(str + 1, eat_whitespace_eos(str, str + strlen(str)));
  2431. test_eq_ptr(str + 1, eat_whitespace_no_nl(str));
  2432. test_eq_ptr(str + 1, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2433. }
  2434. str[0] = '\n';
  2435. test_eq_ptr(str + 1, eat_whitespace(str));
  2436. test_eq_ptr(str + 1, eat_whitespace_eos(str, str + strlen(str)));
  2437. test_eq_ptr(str, eat_whitespace_no_nl(str));
  2438. test_eq_ptr(str, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2439. /* Empty string */
  2440. strcpy(str, "");
  2441. test_eq_ptr(str, eat_whitespace(str));
  2442. test_eq_ptr(str, eat_whitespace_eos(str, str));
  2443. test_eq_ptr(str, eat_whitespace_no_nl(str));
  2444. test_eq_ptr(str, eat_whitespace_eos_no_nl(str, str));
  2445. /* Only ws */
  2446. strcpy(str, " \t\r\n");
  2447. test_eq_ptr(str + strlen(str), eat_whitespace(str));
  2448. test_eq_ptr(str + strlen(str), eat_whitespace_eos(str, str + strlen(str)));
  2449. test_eq_ptr(str + strlen(str) - 1,
  2450. eat_whitespace_no_nl(str));
  2451. test_eq_ptr(str + strlen(str) - 1,
  2452. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2453. strcpy(str, " \t\r ");
  2454. test_eq_ptr(str + strlen(str), eat_whitespace(str));
  2455. test_eq_ptr(str + strlen(str),
  2456. eat_whitespace_eos(str, str + strlen(str)));
  2457. test_eq_ptr(str + strlen(str), eat_whitespace_no_nl(str));
  2458. test_eq_ptr(str + strlen(str),
  2459. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2460. /* Multiple ws */
  2461. strcpy(str, "fuubaar");
  2462. for (i = 0; i < sizeof(ws); ++i)
  2463. str[i] = ws[i];
  2464. test_eq_ptr(str + sizeof(ws), eat_whitespace(str));
  2465. test_eq_ptr(str + sizeof(ws), eat_whitespace_eos(str, str + strlen(str)));
  2466. test_eq_ptr(str + sizeof(ws), eat_whitespace_no_nl(str));
  2467. test_eq_ptr(str + sizeof(ws),
  2468. eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2469. /* Eat comment */
  2470. strcpy(str, "# Comment \n No Comment");
  2471. test_streq("No Comment", eat_whitespace(str));
  2472. test_streq("No Comment", eat_whitespace_eos(str, str + strlen(str)));
  2473. test_eq_ptr(str, eat_whitespace_no_nl(str));
  2474. test_eq_ptr(str, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2475. /* Eat comment & ws mix */
  2476. strcpy(str, " # \t Comment \n\t\nNo Comment");
  2477. test_streq("No Comment", eat_whitespace(str));
  2478. test_streq("No Comment", eat_whitespace_eos(str, str + strlen(str)));
  2479. test_eq_ptr(str + 1, eat_whitespace_no_nl(str));
  2480. test_eq_ptr(str + 1, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2481. /* Eat entire comment */
  2482. strcpy(str, "#Comment");
  2483. test_eq_ptr(str + strlen(str), eat_whitespace(str));
  2484. test_eq_ptr(str + strlen(str), eat_whitespace_eos(str, str + strlen(str)));
  2485. test_eq_ptr(str, eat_whitespace_no_nl(str));
  2486. test_eq_ptr(str, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2487. /* Blank line, then comment */
  2488. strcpy(str, " \t\n # Comment");
  2489. test_eq_ptr(str + strlen(str), eat_whitespace(str));
  2490. test_eq_ptr(str + strlen(str), eat_whitespace_eos(str, str + strlen(str)));
  2491. test_eq_ptr(str + 2, eat_whitespace_no_nl(str));
  2492. test_eq_ptr(str + 2, eat_whitespace_eos_no_nl(str, str + strlen(str)));
  2493. done:
  2494. ;
  2495. }
  2496. /** Return a newly allocated smartlist containing the lines of text in
  2497. * <b>lines</b>. The returned strings are heap-allocated, and must be
  2498. * freed by the caller.
  2499. *
  2500. * XXXX? Move to container.[hc] ? */
  2501. static smartlist_t *
  2502. smartlist_new_from_text_lines(const char *lines)
  2503. {
  2504. smartlist_t *sl = smartlist_new();
  2505. char *last_line;
  2506. smartlist_split_string(sl, lines, "\n", 0, 0);
  2507. last_line = smartlist_pop_last(sl);
  2508. if (last_line != NULL && *last_line != '\0') {
  2509. smartlist_add(sl, last_line);
  2510. }
  2511. return sl;
  2512. }
  2513. /** Test smartlist_new_from_text_lines */
  2514. static void
  2515. test_util_sl_new_from_text_lines(void *ptr)
  2516. {
  2517. (void)ptr;
  2518. { /* Normal usage */
  2519. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz\n");
  2520. int sl_len = smartlist_len(sl);
  2521. tt_want_int_op(sl_len, ==, 3);
  2522. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  2523. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), ==, "bar");
  2524. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), ==, "baz");
  2525. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  2526. smartlist_free(sl);
  2527. }
  2528. { /* No final newline */
  2529. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz");
  2530. int sl_len = smartlist_len(sl);
  2531. tt_want_int_op(sl_len, ==, 3);
  2532. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  2533. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), ==, "bar");
  2534. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), ==, "baz");
  2535. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  2536. smartlist_free(sl);
  2537. }
  2538. { /* No newlines */
  2539. smartlist_t *sl = smartlist_new_from_text_lines("foo");
  2540. int sl_len = smartlist_len(sl);
  2541. tt_want_int_op(sl_len, ==, 1);
  2542. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  2543. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  2544. smartlist_free(sl);
  2545. }
  2546. { /* No text at all */
  2547. smartlist_t *sl = smartlist_new_from_text_lines("");
  2548. int sl_len = smartlist_len(sl);
  2549. tt_want_int_op(sl_len, ==, 0);
  2550. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  2551. smartlist_free(sl);
  2552. }
  2553. }
  2554. static void
  2555. test_util_envnames(void *ptr)
  2556. {
  2557. (void) ptr;
  2558. tt_assert(environment_variable_names_equal("abc", "abc"));
  2559. tt_assert(environment_variable_names_equal("abc", "abc="));
  2560. tt_assert(environment_variable_names_equal("abc", "abc=def"));
  2561. tt_assert(environment_variable_names_equal("abc=def", "abc"));
  2562. tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
  2563. tt_assert(environment_variable_names_equal("abc", "abc"));
  2564. tt_assert(environment_variable_names_equal("abc", "abc="));
  2565. tt_assert(environment_variable_names_equal("abc", "abc=def"));
  2566. tt_assert(environment_variable_names_equal("abc=def", "abc"));
  2567. tt_assert(environment_variable_names_equal("abc=def", "abc=ghi"));
  2568. tt_assert(!environment_variable_names_equal("abc", "abcd"));
  2569. tt_assert(!environment_variable_names_equal("abc=", "abcd"));
  2570. tt_assert(!environment_variable_names_equal("abc=", "abcd"));
  2571. tt_assert(!environment_variable_names_equal("abc=", "def"));
  2572. tt_assert(!environment_variable_names_equal("abc=", "def="));
  2573. tt_assert(!environment_variable_names_equal("abc=x", "def=x"));
  2574. tt_assert(!environment_variable_names_equal("", "a=def"));
  2575. /* A bit surprising. */
  2576. tt_assert(environment_variable_names_equal("", "=def"));
  2577. tt_assert(environment_variable_names_equal("=y", "=x"));
  2578. done:
  2579. ;
  2580. }
  2581. /** Test process_environment_make */
  2582. static void
  2583. test_util_make_environment(void *ptr)
  2584. {
  2585. const char *env_vars_string =
  2586. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  2587. "HOME=/home/foozer\n";
  2588. const char expected_windows_env_block[] =
  2589. "HOME=/home/foozer\000"
  2590. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\000"
  2591. "\000";
  2592. size_t expected_windows_env_block_len =
  2593. sizeof(expected_windows_env_block) - 1;
  2594. smartlist_t *env_vars = smartlist_new_from_text_lines(env_vars_string);
  2595. smartlist_t *env_vars_sorted = smartlist_new();
  2596. smartlist_t *env_vars_in_unixoid_env_block_sorted = smartlist_new();
  2597. process_environment_t *env;
  2598. (void)ptr;
  2599. env = process_environment_make(env_vars);
  2600. /* Check that the Windows environment block is correct. */
  2601. tt_want(tor_memeq(expected_windows_env_block, env->windows_environment_block,
  2602. expected_windows_env_block_len));
  2603. /* Now for the Unixoid environment block. We don't care which order
  2604. * these environment variables are in, so we sort both lists first. */
  2605. smartlist_add_all(env_vars_sorted, env_vars);
  2606. {
  2607. char **v;
  2608. for (v = env->unixoid_environment_block; *v; ++v) {
  2609. smartlist_add(env_vars_in_unixoid_env_block_sorted, *v);
  2610. }
  2611. }
  2612. smartlist_sort_strings(env_vars_sorted);
  2613. smartlist_sort_strings(env_vars_in_unixoid_env_block_sorted);
  2614. tt_want_int_op(smartlist_len(env_vars_sorted), ==,
  2615. smartlist_len(env_vars_in_unixoid_env_block_sorted));
  2616. {
  2617. int len = smartlist_len(env_vars_sorted);
  2618. int i;
  2619. if (smartlist_len(env_vars_in_unixoid_env_block_sorted) < len) {
  2620. len = smartlist_len(env_vars_in_unixoid_env_block_sorted);
  2621. }
  2622. for (i = 0; i < len; ++i) {
  2623. tt_want_str_op(smartlist_get(env_vars_sorted, i), ==,
  2624. smartlist_get(env_vars_in_unixoid_env_block_sorted, i));
  2625. }
  2626. }
  2627. /* Clean up. */
  2628. smartlist_free(env_vars_in_unixoid_env_block_sorted);
  2629. smartlist_free(env_vars_sorted);
  2630. SMARTLIST_FOREACH(env_vars, char *, x, tor_free(x));
  2631. smartlist_free(env_vars);
  2632. process_environment_free(env);
  2633. }
  2634. /** Test set_environment_variable_in_smartlist */
  2635. static void
  2636. test_util_set_env_var_in_sl(void *ptr)
  2637. {
  2638. /* The environment variables in these strings are in arbitrary
  2639. * order; we sort the resulting lists before comparing them.
  2640. *
  2641. * (They *will not* end up in the order shown in
  2642. * expected_resulting_env_vars_string.) */
  2643. const char *base_env_vars_string =
  2644. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  2645. "HOME=/home/foozer\n"
  2646. "TERM=xterm\n"
  2647. "SHELL=/bin/ksh\n"
  2648. "USER=foozer\n"
  2649. "LOGNAME=foozer\n"
  2650. "USERNAME=foozer\n"
  2651. "LANG=en_US.utf8\n"
  2652. ;
  2653. const char *new_env_vars_string =
  2654. "TERM=putty\n"
  2655. "DISPLAY=:18.0\n"
  2656. ;
  2657. const char *expected_resulting_env_vars_string =
  2658. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  2659. "HOME=/home/foozer\n"
  2660. "TERM=putty\n"
  2661. "SHELL=/bin/ksh\n"
  2662. "USER=foozer\n"
  2663. "LOGNAME=foozer\n"
  2664. "USERNAME=foozer\n"
  2665. "LANG=en_US.utf8\n"
  2666. "DISPLAY=:18.0\n"
  2667. ;
  2668. smartlist_t *merged_env_vars =
  2669. smartlist_new_from_text_lines(base_env_vars_string);
  2670. smartlist_t *new_env_vars =
  2671. smartlist_new_from_text_lines(new_env_vars_string);
  2672. smartlist_t *expected_resulting_env_vars =
  2673. smartlist_new_from_text_lines(expected_resulting_env_vars_string);
  2674. /* Elements of merged_env_vars are heap-allocated, and must be
  2675. * freed. Some of them are (or should) be freed by
  2676. * set_environment_variable_in_smartlist.
  2677. *
  2678. * Elements of new_env_vars are heap-allocated, but are copied into
  2679. * merged_env_vars, so they are not freed separately at the end of
  2680. * the function.
  2681. *
  2682. * Elements of expected_resulting_env_vars are heap-allocated, and
  2683. * must be freed. */
  2684. (void)ptr;
  2685. SMARTLIST_FOREACH(new_env_vars, char *, env_var,
  2686. set_environment_variable_in_smartlist(merged_env_vars,
  2687. env_var,
  2688. _tor_free,
  2689. 1));
  2690. smartlist_sort_strings(merged_env_vars);
  2691. smartlist_sort_strings(expected_resulting_env_vars);
  2692. tt_want_int_op(smartlist_len(merged_env_vars), ==,
  2693. smartlist_len(expected_resulting_env_vars));
  2694. {
  2695. int len = smartlist_len(merged_env_vars);
  2696. int i;
  2697. if (smartlist_len(expected_resulting_env_vars) < len) {
  2698. len = smartlist_len(expected_resulting_env_vars);
  2699. }
  2700. for (i = 0; i < len; ++i) {
  2701. tt_want_str_op(smartlist_get(merged_env_vars, i), ==,
  2702. smartlist_get(expected_resulting_env_vars, i));
  2703. }
  2704. }
  2705. /* Clean up. */
  2706. SMARTLIST_FOREACH(merged_env_vars, char *, x, tor_free(x));
  2707. smartlist_free(merged_env_vars);
  2708. smartlist_free(new_env_vars);
  2709. SMARTLIST_FOREACH(expected_resulting_env_vars, char *, x, tor_free(x));
  2710. smartlist_free(expected_resulting_env_vars);
  2711. }
  2712. #define UTIL_LEGACY(name) \
  2713. { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
  2714. #define UTIL_TEST(name, flags) \
  2715. { #name, test_util_ ## name, flags, NULL, NULL }
  2716. struct testcase_t util_tests[] = {
  2717. UTIL_LEGACY(time),
  2718. UTIL_TEST(parse_http_time, 0),
  2719. UTIL_LEGACY(config_line),
  2720. UTIL_LEGACY(config_line_quotes),
  2721. UTIL_LEGACY(config_line_comment_character),
  2722. UTIL_LEGACY(config_line_escaped_content),
  2723. #ifndef _WIN32
  2724. UTIL_LEGACY(expand_filename),
  2725. #endif
  2726. UTIL_LEGACY(strmisc),
  2727. UTIL_LEGACY(pow2),
  2728. UTIL_LEGACY(gzip),
  2729. UTIL_LEGACY(datadir),
  2730. UTIL_LEGACY(mempool),
  2731. UTIL_LEGACY(memarea),
  2732. UTIL_LEGACY(control_formats),
  2733. UTIL_LEGACY(mmap),
  2734. UTIL_LEGACY(threads),
  2735. UTIL_LEGACY(sscanf),
  2736. UTIL_LEGACY(path_is_relative),
  2737. UTIL_LEGACY(strtok),
  2738. UTIL_LEGACY(di_ops),
  2739. UTIL_TEST(find_str_at_start_of_line, 0),
  2740. UTIL_TEST(string_is_C_identifier, 0),
  2741. UTIL_TEST(asprintf, 0),
  2742. UTIL_TEST(listdir, 0),
  2743. UTIL_TEST(parent_dir, 0),
  2744. #ifdef _WIN32
  2745. UTIL_TEST(load_win_lib, 0),
  2746. #endif
  2747. UTIL_TEST(exit_status, 0),
  2748. #ifndef _WIN32
  2749. UTIL_TEST(fgets_eagain, TT_SKIP),
  2750. #endif
  2751. UTIL_TEST(spawn_background_ok, 0),
  2752. UTIL_TEST(spawn_background_fail, 0),
  2753. UTIL_TEST(spawn_background_partial_read, 0),
  2754. UTIL_TEST(format_hex_number, 0),
  2755. UTIL_TEST(join_win_cmdline, 0),
  2756. UTIL_TEST(split_lines, 0),
  2757. UTIL_TEST(n_bits_set, 0),
  2758. UTIL_TEST(eat_whitespace, 0),
  2759. UTIL_TEST(sl_new_from_text_lines, 0),
  2760. UTIL_TEST(envnames, 0),
  2761. UTIL_TEST(make_environment, 0),
  2762. UTIL_TEST(set_env_var_in_sl, 0),
  2763. END_OF_TESTCASES
  2764. };