test_util.c 86 KB

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