test_util.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  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. start.tv_sec = 5;
  25. start.tv_usec = 5000;
  26. end.tv_sec = 5;
  27. end.tv_usec = 5000;
  28. test_eq(0L, tv_udiff(&start, &end));
  29. end.tv_usec = 7000;
  30. test_eq(2000L, tv_udiff(&start, &end));
  31. end.tv_sec = 6;
  32. test_eq(1002000L, tv_udiff(&start, &end));
  33. end.tv_usec = 0;
  34. test_eq(995000L, tv_udiff(&start, &end));
  35. end.tv_sec = 4;
  36. test_eq(-1005000L, tv_udiff(&start, &end));
  37. end.tv_usec = 999990;
  38. start.tv_sec = 1;
  39. start.tv_usec = 500;
  40. /* The test values here are confirmed to be correct on a platform
  41. * with a working timegm. */
  42. a_time.tm_year = 2003-1900;
  43. a_time.tm_mon = 7;
  44. a_time.tm_mday = 30;
  45. a_time.tm_hour = 6;
  46. a_time.tm_min = 14;
  47. a_time.tm_sec = 55;
  48. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  49. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  50. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  51. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  52. a_time.tm_mday = 10;
  53. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  54. format_rfc1123_time(timestr, 0);
  55. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  56. format_rfc1123_time(timestr, (time_t)1091580502UL);
  57. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  58. t_res = 0;
  59. i = parse_rfc1123_time(timestr, &t_res);
  60. test_eq(i,0);
  61. test_eq(t_res, (time_t)1091580502UL);
  62. test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  63. tor_gettimeofday(&start);
  64. /* now make sure time works. */
  65. tor_gettimeofday(&end);
  66. /* We might've timewarped a little. */
  67. tt_int_op(tv_udiff(&start, &end), >=, -5000);
  68. /* Now let's check some format_iso_time variants */
  69. tv.tv_sec = (time_t)1326296338;
  70. tv.tv_usec = 3060;
  71. format_iso_time(timestr, tv.tv_sec);
  72. test_streq("2012-01-11 15:38:58", timestr);
  73. /* The output of format_local_iso_time will vary by timezone, and setting
  74. our timezone for testing purposes would be a nontrivial flaky pain.
  75. Skip this test for now.
  76. format_local_iso_time(timestr, tv.tv_sec);
  77. test_streq("2012-01-11 10:38:58", timestr);
  78. */
  79. format_iso_time_nospace(timestr, tv.tv_sec);
  80. test_streq("2012-01-11T15:38:58", timestr);
  81. test_eq(strlen(timestr), ISO_TIME_LEN);
  82. format_iso_time_nospace_usec(timestr, &tv);
  83. test_streq("2012-01-11T15:38:58.003060", timestr);
  84. test_eq(strlen(timestr), ISO_TIME_USEC_LEN);
  85. done:
  86. ;
  87. }
  88. static void
  89. test_util_config_line(void)
  90. {
  91. char buf[1024];
  92. char *k=NULL, *v=NULL;
  93. const char *str;
  94. /* Test parse_config_line_from_str */
  95. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  96. "k2\n"
  97. "k3 \n" "\n" " \n" "#comment\n"
  98. "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
  99. "kseven \"a quoted 'string\"\n"
  100. "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
  101. "k9 a line that\\\n spans two lines.\n\n"
  102. "k10 more than\\\n one contin\\\nuation\n"
  103. "k11 \\\ncontinuation at the start\n"
  104. "k12 line with a\\\n#comment\n embedded\n"
  105. "k13\\\ncontinuation at the very start\n"
  106. "k14 a line that has a comment and # ends with a slash \\\n"
  107. "k15 this should be the next new line\n"
  108. "k16 a line that has a comment and # ends without a slash \n"
  109. "k17 this should be the next new line\n"
  110. , sizeof(buf));
  111. str = buf;
  112. str = parse_config_line_from_str(str, &k, &v);
  113. test_streq(k, "k");
  114. test_streq(v, "v");
  115. tor_free(k); tor_free(v);
  116. test_assert(!strcmpstart(str, "key value with"));
  117. str = parse_config_line_from_str(str, &k, &v);
  118. test_streq(k, "key");
  119. test_streq(v, "value with spaces");
  120. tor_free(k); tor_free(v);
  121. test_assert(!strcmpstart(str, "keykey"));
  122. str = parse_config_line_from_str(str, &k, &v);
  123. test_streq(k, "keykey");
  124. test_streq(v, "val");
  125. tor_free(k); tor_free(v);
  126. test_assert(!strcmpstart(str, "k2\n"));
  127. str = parse_config_line_from_str(str, &k, &v);
  128. test_streq(k, "k2");
  129. test_streq(v, "");
  130. tor_free(k); tor_free(v);
  131. test_assert(!strcmpstart(str, "k3 \n"));
  132. str = parse_config_line_from_str(str, &k, &v);
  133. test_streq(k, "k3");
  134. test_streq(v, "");
  135. tor_free(k); tor_free(v);
  136. test_assert(!strcmpstart(str, "#comment"));
  137. str = parse_config_line_from_str(str, &k, &v);
  138. test_streq(k, "k4");
  139. test_streq(v, "");
  140. tor_free(k); tor_free(v);
  141. test_assert(!strcmpstart(str, "k5#abc"));
  142. str = parse_config_line_from_str(str, &k, &v);
  143. test_streq(k, "k5");
  144. test_streq(v, "");
  145. tor_free(k); tor_free(v);
  146. test_assert(!strcmpstart(str, "k6"));
  147. str = parse_config_line_from_str(str, &k, &v);
  148. test_streq(k, "k6");
  149. test_streq(v, "val");
  150. tor_free(k); tor_free(v);
  151. test_assert(!strcmpstart(str, "kseven"));
  152. str = parse_config_line_from_str(str, &k, &v);
  153. test_streq(k, "kseven");
  154. test_streq(v, "a quoted \'string");
  155. tor_free(k); tor_free(v);
  156. test_assert(!strcmpstart(str, "k8 "));
  157. str = parse_config_line_from_str(str, &k, &v);
  158. test_streq(k, "k8");
  159. test_streq(v, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
  160. tor_free(k); tor_free(v);
  161. str = parse_config_line_from_str(str, &k, &v);
  162. test_streq(k, "k9");
  163. test_streq(v, "a line that spans two lines.");
  164. tor_free(k); tor_free(v);
  165. str = parse_config_line_from_str(str, &k, &v);
  166. test_streq(k, "k10");
  167. test_streq(v, "more than one continuation");
  168. tor_free(k); tor_free(v);
  169. str = parse_config_line_from_str(str, &k, &v);
  170. test_streq(k, "k11");
  171. test_streq(v, "continuation at the start");
  172. tor_free(k); tor_free(v);
  173. str = parse_config_line_from_str(str, &k, &v);
  174. test_streq(k, "k12");
  175. test_streq(v, "line with a embedded");
  176. tor_free(k); tor_free(v);
  177. str = parse_config_line_from_str(str, &k, &v);
  178. test_streq(k, "k13");
  179. test_streq(v, "continuation at the very start");
  180. tor_free(k); tor_free(v);
  181. str = parse_config_line_from_str(str, &k, &v);
  182. test_streq(k, "k14");
  183. test_streq(v, "a line that has a comment and" );
  184. tor_free(k); tor_free(v);
  185. str = parse_config_line_from_str(str, &k, &v);
  186. test_streq(k, "k15");
  187. test_streq(v, "this should be the next new line");
  188. tor_free(k); tor_free(v);
  189. str = parse_config_line_from_str(str, &k, &v);
  190. test_streq(k, "k16");
  191. test_streq(v, "a line that has a comment and" );
  192. tor_free(k); tor_free(v);
  193. str = parse_config_line_from_str(str, &k, &v);
  194. test_streq(k, "k17");
  195. test_streq(v, "this should be the next new line");
  196. tor_free(k); tor_free(v);
  197. test_streq(str, "");
  198. done:
  199. tor_free(k);
  200. tor_free(v);
  201. }
  202. static void
  203. test_util_config_line_quotes(void)
  204. {
  205. char buf1[1024];
  206. char buf2[128];
  207. char buf3[128];
  208. char buf4[128];
  209. char *k=NULL, *v=NULL;
  210. const char *str;
  211. /* Test parse_config_line_from_str */
  212. strlcpy(buf1, "kTrailingSpace \"quoted value\" \n"
  213. "kTrailingGarbage \"quoted value\"trailing garbage\n"
  214. , sizeof(buf1));
  215. strlcpy(buf2, "kTrailingSpaceAndGarbage \"quoted value\" trailing space+g\n"
  216. , sizeof(buf2));
  217. strlcpy(buf3, "kMultilineTrailingSpace \"mline\\ \nvalue w/ trailing sp\"\n"
  218. , sizeof(buf3));
  219. strlcpy(buf4, "kMultilineNoTrailingBackslash \"naked multiline\nvalue\"\n"
  220. , sizeof(buf4));
  221. str = buf1;
  222. str = parse_config_line_from_str(str, &k, &v);
  223. test_streq(k, "kTrailingSpace");
  224. test_streq(v, "quoted value");
  225. tor_free(k); tor_free(v);
  226. str = parse_config_line_from_str(str, &k, &v);
  227. test_eq_ptr(str, NULL);
  228. tor_free(k); tor_free(v);
  229. str = buf2;
  230. str = parse_config_line_from_str(str, &k, &v);
  231. test_eq_ptr(str, NULL);
  232. tor_free(k); tor_free(v);
  233. str = buf3;
  234. str = parse_config_line_from_str(str, &k, &v);
  235. test_eq_ptr(str, NULL);
  236. tor_free(k); tor_free(v);
  237. str = buf4;
  238. str = parse_config_line_from_str(str, &k, &v);
  239. test_eq_ptr(str, NULL);
  240. tor_free(k); tor_free(v);
  241. done:
  242. tor_free(k);
  243. tor_free(v);
  244. }
  245. static void
  246. test_util_config_line_escaped_content(void)
  247. {
  248. char buf1[1024];
  249. char buf2[128];
  250. char buf3[128];
  251. char buf4[128];
  252. char *k=NULL, *v=NULL;
  253. const char *str;
  254. /* Test parse_config_line_from_str */
  255. strlcpy(buf1, "HexadecimalLower \"\\x2a\"\n"
  256. "HexadecimalUpper \"\\x2A\"\n"
  257. "HexadecimalUpperX \"\\X2A\"\n"
  258. "Octal \"\\52\"\n"
  259. "Newline \"\\n\"\n"
  260. "Tab \"\\t\"\n"
  261. "CarriageReturn \"\\r\"\n"
  262. "Quote \"\\\"\"\n"
  263. "Backslash \"\\\\\"\n"
  264. , sizeof(buf1));
  265. strlcpy(buf2, "BrokenEscapedContent \"\\a\"\n"
  266. , sizeof(buf2));
  267. strlcpy(buf3, "BrokenEscapedContent \"\\x\"\n"
  268. , sizeof(buf3));
  269. strlcpy(buf4, "BrokenOctal \"\\8\"\n"
  270. , sizeof(buf4));
  271. str = buf1;
  272. str = parse_config_line_from_str(str, &k, &v);
  273. test_streq(k, "HexadecimalLower");
  274. test_streq(v, "*");
  275. tor_free(k); tor_free(v);
  276. str = parse_config_line_from_str(str, &k, &v);
  277. test_streq(k, "HexadecimalUpper");
  278. test_streq(v, "*");
  279. tor_free(k); tor_free(v);
  280. str = parse_config_line_from_str(str, &k, &v);
  281. test_streq(k, "HexadecimalUpperX");
  282. test_streq(v, "*");
  283. tor_free(k); tor_free(v);
  284. str = parse_config_line_from_str(str, &k, &v);
  285. test_streq(k, "Octal");
  286. test_streq(v, "*");
  287. tor_free(k); tor_free(v);
  288. str = parse_config_line_from_str(str, &k, &v);
  289. test_streq(k, "Newline");
  290. test_streq(v, "\n");
  291. tor_free(k); tor_free(v);
  292. str = parse_config_line_from_str(str, &k, &v);
  293. test_streq(k, "Tab");
  294. test_streq(v, "\t");
  295. tor_free(k); tor_free(v);
  296. str = parse_config_line_from_str(str, &k, &v);
  297. test_streq(k, "CarriageReturn");
  298. test_streq(v, "\r");
  299. tor_free(k); tor_free(v);
  300. str = parse_config_line_from_str(str, &k, &v);
  301. test_streq(k, "Quote");
  302. test_streq(v, "\"");
  303. tor_free(k); tor_free(v);
  304. str = parse_config_line_from_str(str, &k, &v);
  305. test_streq(k, "Backslash");
  306. test_streq(v, "\\");
  307. tor_free(k); tor_free(v);
  308. str = buf2;
  309. str = parse_config_line_from_str(str, &k, &v);
  310. test_eq_ptr(str, NULL);
  311. tor_free(k); tor_free(v);
  312. str = buf3;
  313. str = parse_config_line_from_str(str, &k, &v);
  314. test_eq_ptr(str, NULL);
  315. tor_free(k); tor_free(v);
  316. str = buf4;
  317. str = parse_config_line_from_str(str, &k, &v);
  318. test_eq_ptr(str, NULL);
  319. tor_free(k); tor_free(v);
  320. done:
  321. tor_free(k);
  322. tor_free(v);
  323. }
  324. /** Test basic string functionality. */
  325. static void
  326. test_util_strmisc(void)
  327. {
  328. char buf[1024];
  329. int i;
  330. char *cp;
  331. /* Tests for corner cases of strl operations */
  332. test_eq(5, strlcpy(buf, "Hello", 0));
  333. strlcpy(buf, "Hello", sizeof(buf));
  334. test_eq(10, strlcat(buf, "Hello", 5));
  335. /* Test tor_strstrip() */
  336. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  337. tor_strstrip(buf, ",!");
  338. test_streq(buf, "Testing 1 2 3");
  339. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  340. tor_strstrip(buf, "!? ");
  341. test_streq(buf, "Testing123");
  342. /* Test tor_parse_long. */
  343. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  344. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  345. test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
  346. /* Test tor_parse_ulong */
  347. test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  348. test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
  349. /* Test tor_parse_uint64. */
  350. test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  351. test_assert(i == 1);
  352. test_streq(cp, " x");
  353. test_assert(U64_LITERAL(12345678901) ==
  354. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  355. test_assert(i == 1);
  356. test_streq(cp, "");
  357. test_assert(U64_LITERAL(0) ==
  358. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  359. test_assert(i == 0);
  360. {
  361. /* Test tor_parse_double. */
  362. double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
  363. test_assert(i == 1);
  364. test_assert(DBL_TO_U64(d) == 10);
  365. d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
  366. test_assert(i == 1);
  367. test_assert(DBL_TO_U64(d) == 0);
  368. d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
  369. test_assert(i == 0);
  370. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
  371. test_assert(i == 0);
  372. d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
  373. test_assert(i == 1);
  374. d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
  375. test_assert(i == 1);
  376. }
  377. /* Test failing snprintf cases */
  378. test_eq(-1, tor_snprintf(buf, 0, "Foo"));
  379. test_eq(-1, tor_snprintf(buf, 2, "Foo"));
  380. /* Test printf with uint64 */
  381. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  382. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  383. test_streq(buf, "x!12345678901!x");
  384. /* Test for strcmpstart and strcmpend. */
  385. test_assert(strcmpstart("abcdef", "abcdef")==0);
  386. test_assert(strcmpstart("abcdef", "abc")==0);
  387. test_assert(strcmpstart("abcdef", "abd")<0);
  388. test_assert(strcmpstart("abcdef", "abb")>0);
  389. test_assert(strcmpstart("ab", "abb")<0);
  390. test_assert(strcmpend("abcdef", "abcdef")==0);
  391. test_assert(strcmpend("abcdef", "def")==0);
  392. test_assert(strcmpend("abcdef", "deg")<0);
  393. test_assert(strcmpend("abcdef", "dee")>0);
  394. test_assert(strcmpend("ab", "abb")<0);
  395. test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  396. test_assert(strcasecmpend("abcdef", "dEF")==0);
  397. test_assert(strcasecmpend("abcDEf", "deg")<0);
  398. test_assert(strcasecmpend("abcdef", "DEE")>0);
  399. test_assert(strcasecmpend("ab", "abB")<0);
  400. /* Test mem_is_zero */
  401. memset(buf,0,128);
  402. buf[128] = 'x';
  403. test_assert(tor_digest_is_zero(buf));
  404. test_assert(tor_mem_is_zero(buf, 10));
  405. test_assert(tor_mem_is_zero(buf, 20));
  406. test_assert(tor_mem_is_zero(buf, 128));
  407. test_assert(!tor_mem_is_zero(buf, 129));
  408. buf[60] = (char)255;
  409. test_assert(!tor_mem_is_zero(buf, 128));
  410. buf[0] = (char)1;
  411. test_assert(!tor_mem_is_zero(buf, 10));
  412. /* Test 'escaped' */
  413. test_streq("\"\"", escaped(""));
  414. test_streq("\"abcd\"", escaped("abcd"));
  415. test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
  416. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  417. test_assert(NULL == escaped(NULL));
  418. /* Test strndup and memdup */
  419. {
  420. const char *s = "abcdefghijklmnopqrstuvwxyz";
  421. cp = tor_strndup(s, 30);
  422. test_streq(cp, s); /* same string, */
  423. test_neq(cp, s); /* but different pointers. */
  424. tor_free(cp);
  425. cp = tor_strndup(s, 5);
  426. test_streq(cp, "abcde");
  427. tor_free(cp);
  428. s = "a\0b\0c\0d\0e\0";
  429. cp = tor_memdup(s,10);
  430. test_memeq(cp, s, 10); /* same ram, */
  431. test_neq(cp, s); /* but different pointers. */
  432. tor_free(cp);
  433. }
  434. /* Test str-foo functions */
  435. cp = tor_strdup("abcdef");
  436. test_assert(tor_strisnonupper(cp));
  437. cp[3] = 'D';
  438. test_assert(!tor_strisnonupper(cp));
  439. tor_strupper(cp);
  440. test_streq(cp, "ABCDEF");
  441. test_assert(tor_strisprint(cp));
  442. cp[3] = 3;
  443. test_assert(!tor_strisprint(cp));
  444. tor_free(cp);
  445. /* Test memmem and memstr */
  446. {
  447. const char *haystack = "abcde";
  448. tt_assert(!tor_memmem(haystack, 5, "ef", 2));
  449. test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
  450. test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
  451. haystack = "ababcad";
  452. test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
  453. test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
  454. test_assert(!tor_memstr(haystack, 7, "fe"));
  455. test_assert(!tor_memstr(haystack, 7, "longerthantheoriginal"));
  456. }
  457. /* Test wrap_string */
  458. {
  459. smartlist_t *sl = smartlist_new();
  460. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  461. 10, "", "");
  462. cp = smartlist_join_strings(sl, "", 0, NULL);
  463. test_streq(cp,
  464. "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
  465. tor_free(cp);
  466. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  467. smartlist_clear(sl);
  468. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  469. 16, "### ", "# ");
  470. cp = smartlist_join_strings(sl, "", 0, NULL);
  471. test_streq(cp,
  472. "### This is a\n# test of string\n# wrapping\n# functionality:\n"
  473. "# woot.\n");
  474. tor_free(cp);
  475. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  476. smartlist_free(sl);
  477. }
  478. /* Test hex_str */
  479. {
  480. char binary_data[64];
  481. size_t i;
  482. for (i = 0; i < sizeof(binary_data); ++i)
  483. binary_data[i] = i;
  484. test_streq(hex_str(binary_data, 0), "");
  485. test_streq(hex_str(binary_data, 1), "00");
  486. test_streq(hex_str(binary_data, 17), "000102030405060708090A0B0C0D0E0F10");
  487. test_streq(hex_str(binary_data, 32),
  488. "000102030405060708090A0B0C0D0E0F"
  489. "101112131415161718191A1B1C1D1E1F");
  490. }
  491. done:
  492. ;
  493. }
  494. static void
  495. test_util_pow2(void)
  496. {
  497. /* Test tor_log2(). */
  498. test_eq(tor_log2(64), 6);
  499. test_eq(tor_log2(65), 6);
  500. test_eq(tor_log2(63), 5);
  501. test_eq(tor_log2(1), 0);
  502. test_eq(tor_log2(2), 1);
  503. test_eq(tor_log2(3), 1);
  504. test_eq(tor_log2(4), 2);
  505. test_eq(tor_log2(5), 2);
  506. test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
  507. test_eq(tor_log2(UINT64_MAX), 63);
  508. /* Test round_to_power_of_2 */
  509. test_eq(round_to_power_of_2(120), 128);
  510. test_eq(round_to_power_of_2(128), 128);
  511. test_eq(round_to_power_of_2(130), 128);
  512. test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
  513. U64_LITERAL(1)<<55);
  514. test_eq(round_to_power_of_2(0), 2);
  515. done:
  516. ;
  517. }
  518. /** mutex for thread test to stop the threads hitting data at the same time. */
  519. static tor_mutex_t *_thread_test_mutex = NULL;
  520. /** mutexes for the thread test to make sure that the threads have to
  521. * interleave somewhat. */
  522. static tor_mutex_t *_thread_test_start1 = NULL,
  523. *_thread_test_start2 = NULL;
  524. /** Shared strmap for the thread test. */
  525. static strmap_t *_thread_test_strmap = NULL;
  526. /** The name of thread1 for the thread test */
  527. static char *_thread1_name = NULL;
  528. /** The name of thread2 for the thread test */
  529. static char *_thread2_name = NULL;
  530. static void _thread_test_func(void* _s) ATTR_NORETURN;
  531. /** How many iterations have the threads in the unit test run? */
  532. static int t1_count = 0, t2_count = 0;
  533. /** Helper function for threading unit tests: This function runs in a
  534. * subthread. It grabs its own mutex (start1 or start2) to make sure that it
  535. * should start, then it repeatedly alters _test_thread_strmap protected by
  536. * _thread_test_mutex. */
  537. static void
  538. _thread_test_func(void* _s)
  539. {
  540. char *s = _s;
  541. int i, *count;
  542. tor_mutex_t *m;
  543. char buf[64];
  544. char **cp;
  545. if (!strcmp(s, "thread 1")) {
  546. m = _thread_test_start1;
  547. cp = &_thread1_name;
  548. count = &t1_count;
  549. } else {
  550. m = _thread_test_start2;
  551. cp = &_thread2_name;
  552. count = &t2_count;
  553. }
  554. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  555. *cp = tor_strdup(buf);
  556. tor_mutex_acquire(m);
  557. for (i=0; i<10000; ++i) {
  558. tor_mutex_acquire(_thread_test_mutex);
  559. strmap_set(_thread_test_strmap, "last to run", *cp);
  560. ++*count;
  561. tor_mutex_release(_thread_test_mutex);
  562. }
  563. tor_mutex_acquire(_thread_test_mutex);
  564. strmap_set(_thread_test_strmap, s, *cp);
  565. tor_mutex_release(_thread_test_mutex);
  566. tor_mutex_release(m);
  567. spawn_exit();
  568. }
  569. /** Run unit tests for threading logic. */
  570. static void
  571. test_util_threads(void)
  572. {
  573. char *s1 = NULL, *s2 = NULL;
  574. int done = 0, timedout = 0;
  575. time_t started;
  576. #ifndef _WIN32
  577. struct timeval tv;
  578. tv.tv_sec=0;
  579. tv.tv_usec=10;
  580. #endif
  581. #ifndef TOR_IS_MULTITHREADED
  582. /* Skip this test if we aren't threading. We should be threading most
  583. * everywhere by now. */
  584. if (1)
  585. return;
  586. #endif
  587. _thread_test_mutex = tor_mutex_new();
  588. _thread_test_start1 = tor_mutex_new();
  589. _thread_test_start2 = tor_mutex_new();
  590. _thread_test_strmap = strmap_new();
  591. s1 = tor_strdup("thread 1");
  592. s2 = tor_strdup("thread 2");
  593. tor_mutex_acquire(_thread_test_start1);
  594. tor_mutex_acquire(_thread_test_start2);
  595. spawn_func(_thread_test_func, s1);
  596. spawn_func(_thread_test_func, s2);
  597. tor_mutex_release(_thread_test_start2);
  598. tor_mutex_release(_thread_test_start1);
  599. started = time(NULL);
  600. while (!done) {
  601. tor_mutex_acquire(_thread_test_mutex);
  602. strmap_assert_ok(_thread_test_strmap);
  603. if (strmap_get(_thread_test_strmap, "thread 1") &&
  604. strmap_get(_thread_test_strmap, "thread 2")) {
  605. done = 1;
  606. } else if (time(NULL) > started + 25) {
  607. timedout = done = 1;
  608. }
  609. tor_mutex_release(_thread_test_mutex);
  610. #ifndef _WIN32
  611. /* Prevent the main thread from starving the worker threads. */
  612. select(0, NULL, NULL, NULL, &tv);
  613. #endif
  614. }
  615. tor_mutex_acquire(_thread_test_start1);
  616. tor_mutex_release(_thread_test_start1);
  617. tor_mutex_acquire(_thread_test_start2);
  618. tor_mutex_release(_thread_test_start2);
  619. tor_mutex_free(_thread_test_mutex);
  620. if (timedout) {
  621. printf("\nTimed out: %d %d", t1_count, t2_count);
  622. test_assert(strmap_get(_thread_test_strmap, "thread 1"));
  623. test_assert(strmap_get(_thread_test_strmap, "thread 2"));
  624. test_assert(!timedout);
  625. }
  626. /* different thread IDs. */
  627. test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  628. strmap_get(_thread_test_strmap, "thread 2")));
  629. test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  630. strmap_get(_thread_test_strmap, "last to run")) ||
  631. !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
  632. strmap_get(_thread_test_strmap, "last to run")));
  633. done:
  634. tor_free(s1);
  635. tor_free(s2);
  636. tor_free(_thread1_name);
  637. tor_free(_thread2_name);
  638. if (_thread_test_strmap)
  639. strmap_free(_thread_test_strmap, NULL);
  640. if (_thread_test_start1)
  641. tor_mutex_free(_thread_test_start1);
  642. if (_thread_test_start2)
  643. tor_mutex_free(_thread_test_start2);
  644. }
  645. /** Run unit tests for compression functions */
  646. static void
  647. test_util_gzip(void)
  648. {
  649. char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  650. const char *ccp2;
  651. size_t len1, len2;
  652. tor_zlib_state_t *state = NULL;
  653. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  654. test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  655. if (is_gzip_supported()) {
  656. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  657. GZIP_METHOD));
  658. test_assert(buf2);
  659. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  660. test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  661. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  662. GZIP_METHOD, 1, LOG_INFO));
  663. test_assert(buf3);
  664. test_streq(buf1,buf3);
  665. tor_free(buf2);
  666. tor_free(buf3);
  667. }
  668. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  669. ZLIB_METHOD));
  670. test_assert(buf2);
  671. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  672. test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  673. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  674. ZLIB_METHOD, 1, LOG_INFO));
  675. test_assert(buf3);
  676. test_streq(buf1,buf3);
  677. /* Check whether we can uncompress concatenated, compressed strings. */
  678. tor_free(buf3);
  679. buf2 = tor_realloc(buf2, len1*2);
  680. memcpy(buf2+len1, buf2, len1);
  681. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  682. ZLIB_METHOD, 1, LOG_INFO));
  683. test_eq(len2, (strlen(buf1)+1)*2);
  684. test_memeq(buf3,
  685. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  686. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  687. (strlen(buf1)+1)*2);
  688. tor_free(buf1);
  689. tor_free(buf2);
  690. tor_free(buf3);
  691. /* Check whether we can uncompress partial strings. */
  692. buf1 =
  693. tor_strdup("String with low redundancy that won't be compressed much.");
  694. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  695. ZLIB_METHOD));
  696. tt_assert(len1>16);
  697. /* when we allow an incomplete string, we should succeed.*/
  698. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  699. ZLIB_METHOD, 0, LOG_INFO));
  700. buf3[len2]='\0';
  701. tt_assert(len2 > 5);
  702. tt_assert(!strcmpstart(buf1, buf3));
  703. /* when we demand a complete string, this must fail. */
  704. tor_free(buf3);
  705. tt_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  706. ZLIB_METHOD, 1, LOG_INFO));
  707. tt_assert(!buf3);
  708. /* Now, try streaming compression. */
  709. tor_free(buf1);
  710. tor_free(buf2);
  711. tor_free(buf3);
  712. state = tor_zlib_new(1, ZLIB_METHOD);
  713. tt_assert(state);
  714. cp1 = buf1 = tor_malloc(1024);
  715. len1 = 1024;
  716. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  717. len2 = 21;
  718. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  719. == TOR_ZLIB_OK);
  720. test_eq(len2, 0); /* Make sure we compressed it all. */
  721. test_assert(cp1 > buf1);
  722. len2 = 0;
  723. cp2 = cp1;
  724. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  725. == TOR_ZLIB_DONE);
  726. test_eq(len2, 0);
  727. test_assert(cp1 > cp2); /* Make sure we really added something. */
  728. tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  729. ZLIB_METHOD, 1, LOG_WARN));
  730. test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  731. done:
  732. if (state)
  733. tor_zlib_free(state);
  734. tor_free(buf2);
  735. tor_free(buf3);
  736. tor_free(buf1);
  737. }
  738. /** Run unit tests for mmap() wrapper functionality. */
  739. static void
  740. test_util_mmap(void)
  741. {
  742. char *fname1 = tor_strdup(get_fname("mapped_1"));
  743. char *fname2 = tor_strdup(get_fname("mapped_2"));
  744. char *fname3 = tor_strdup(get_fname("mapped_3"));
  745. const size_t buflen = 17000;
  746. char *buf = tor_malloc(17000);
  747. tor_mmap_t *mapping = NULL;
  748. crypto_rand(buf, buflen);
  749. mapping = tor_mmap_file(fname1);
  750. test_assert(! mapping);
  751. write_str_to_file(fname1, "Short file.", 1);
  752. write_bytes_to_file(fname2, buf, buflen, 1);
  753. write_bytes_to_file(fname3, buf, 16384, 1);
  754. mapping = tor_mmap_file(fname1);
  755. test_assert(mapping);
  756. test_eq(mapping->size, strlen("Short file."));
  757. test_streq(mapping->data, "Short file.");
  758. #ifdef _WIN32
  759. tor_munmap_file(mapping);
  760. mapping = NULL;
  761. test_assert(unlink(fname1) == 0);
  762. #else
  763. /* make sure we can unlink. */
  764. test_assert(unlink(fname1) == 0);
  765. test_streq(mapping->data, "Short file.");
  766. tor_munmap_file(mapping);
  767. mapping = NULL;
  768. #endif
  769. /* Now a zero-length file. */
  770. write_str_to_file(fname1, "", 1);
  771. mapping = tor_mmap_file(fname1);
  772. test_eq(mapping, NULL);
  773. test_eq(ERANGE, errno);
  774. unlink(fname1);
  775. /* Make sure that we fail to map a no-longer-existent file. */
  776. mapping = tor_mmap_file(fname1);
  777. test_assert(mapping == NULL);
  778. /* Now try a big file that stretches across a few pages and isn't aligned */
  779. mapping = tor_mmap_file(fname2);
  780. test_assert(mapping);
  781. test_eq(mapping->size, buflen);
  782. test_memeq(mapping->data, buf, buflen);
  783. tor_munmap_file(mapping);
  784. mapping = NULL;
  785. /* Now try a big aligned file. */
  786. mapping = tor_mmap_file(fname3);
  787. test_assert(mapping);
  788. test_eq(mapping->size, 16384);
  789. test_memeq(mapping->data, buf, 16384);
  790. tor_munmap_file(mapping);
  791. mapping = NULL;
  792. done:
  793. unlink(fname1);
  794. unlink(fname2);
  795. unlink(fname3);
  796. tor_free(fname1);
  797. tor_free(fname2);
  798. tor_free(fname3);
  799. tor_free(buf);
  800. if (mapping)
  801. tor_munmap_file(mapping);
  802. }
  803. /** Run unit tests for escaping/unescaping data for use by controllers. */
  804. static void
  805. test_util_control_formats(void)
  806. {
  807. char *out = NULL;
  808. const char *inp =
  809. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  810. size_t sz;
  811. sz = read_escaped_data(inp, strlen(inp), &out);
  812. test_streq(out,
  813. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  814. test_eq(sz, strlen(out));
  815. done:
  816. tor_free(out);
  817. }
  818. static void
  819. test_util_sscanf(void)
  820. {
  821. unsigned u1, u2, u3;
  822. char s1[10], s2[10], s3[10], ch;
  823. int r;
  824. r = tor_sscanf("hello world", "hello world"); /* String match: success */
  825. test_eq(r, 0);
  826. r = tor_sscanf("hello world 3", "hello worlb %u", &u1); /* String fail */
  827. test_eq(r, 0);
  828. r = tor_sscanf("12345", "%u", &u1); /* Simple number */
  829. test_eq(r, 1);
  830. test_eq(u1, 12345u);
  831. r = tor_sscanf("", "%u", &u1); /* absent number */
  832. test_eq(r, 0);
  833. r = tor_sscanf("A", "%u", &u1); /* bogus number */
  834. test_eq(r, 0);
  835. r = tor_sscanf("4294967295", "%u", &u1); /* UINT32_MAX should work. */
  836. test_eq(r, 1);
  837. test_eq(u1, 4294967295u);
  838. r = tor_sscanf("4294967296", "%u", &u1); /* Always say -1 at 32 bits. */
  839. test_eq(r, 0);
  840. r = tor_sscanf("123456", "%2u%u", &u1, &u2); /* Width */
  841. test_eq(r, 2);
  842. test_eq(u1, 12u);
  843. test_eq(u2, 3456u);
  844. r = tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3); /* separators */
  845. test_eq(r, 3);
  846. test_eq(u1, 12u);
  847. test_eq(u2, 3u);
  848. test_eq(u3, 456u);
  849. r = tor_sscanf("12:3:045", "%2u:%2u:%3u", &u1, &u2, &u3); /* 0s */
  850. test_eq(r, 3);
  851. test_eq(u1, 12u);
  852. test_eq(u2, 3u);
  853. test_eq(u3, 45u);
  854. /* %u does not match space.*/
  855. r = tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3);
  856. test_eq(r, 2);
  857. /* %u does not match negative numbers. */
  858. r = tor_sscanf("12:3:-4", "%2u:%2u:%3u", &u1, &u2, &u3);
  859. test_eq(r, 2);
  860. /* Arbitrary amounts of 0-padding are okay */
  861. r = tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3);
  862. test_eq(r, 3);
  863. test_eq(u1, 12u);
  864. test_eq(u2, 3u);
  865. test_eq(u3, 99u);
  866. /* %x should work. */
  867. r = tor_sscanf("1234 02aBcdEf", "%x %x", &u1, &u2);
  868. test_eq(r, 2);
  869. test_eq(u1, 0x1234);
  870. test_eq(u2, 0x2ABCDEF);
  871. /* Width works on %x */
  872. r = tor_sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3);
  873. test_eq(r, 3);
  874. test_eq(u1, 0xf00d);
  875. test_eq(u2, 0xcafe);
  876. test_eq(u3, 444);
  877. r = tor_sscanf("99% fresh", "%3u%% fresh", &u1); /* percents are scannable.*/
  878. test_eq(r, 1);
  879. test_eq(u1, 99);
  880. r = tor_sscanf("hello", "%s", s1); /* %s needs a number. */
  881. test_eq(r, -1);
  882. r = tor_sscanf("hello", "%3s%7s", s1, s2); /* %s matches characters. */
  883. test_eq(r, 2);
  884. test_streq(s1, "hel");
  885. test_streq(s2, "lo");
  886. r = tor_sscanf("WD40", "%2s%u", s3, &u1); /* %s%u */
  887. test_eq(r, 2);
  888. test_streq(s3, "WD");
  889. test_eq(u1, 40);
  890. r = tor_sscanf("76trombones", "%6u%9s", &u1, s1); /* %u%s */
  891. test_eq(r, 2);
  892. test_eq(u1, 76);
  893. test_streq(s1, "trombones");
  894. r = tor_sscanf("hello world", "%9s %9s", s1, s2); /* %s doesn't eat space. */
  895. test_eq(r, 2);
  896. test_streq(s1, "hello");
  897. test_streq(s2, "world");
  898. r = tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3); /* %s can be empty. */
  899. test_eq(r, 3);
  900. test_streq(s1, "hi");
  901. test_streq(s2, "");
  902. test_streq(s3, "");
  903. r = tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
  904. test_eq(r, 3);
  905. r = tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
  906. test_eq(r, 4);
  907. done:
  908. ;
  909. }
  910. /** Run unittests for memory pool allocator */
  911. static void
  912. test_util_mempool(void)
  913. {
  914. mp_pool_t *pool = NULL;
  915. smartlist_t *allocated = NULL;
  916. int i;
  917. pool = mp_pool_new(1, 100);
  918. test_assert(pool);
  919. test_assert(pool->new_chunk_capacity >= 100);
  920. test_assert(pool->item_alloc_size >= sizeof(void*)+1);
  921. mp_pool_destroy(pool);
  922. pool = NULL;
  923. pool = mp_pool_new(241, 2500);
  924. test_assert(pool);
  925. test_assert(pool->new_chunk_capacity >= 10);
  926. test_assert(pool->item_alloc_size >= sizeof(void*)+241);
  927. test_eq(pool->item_alloc_size & 0x03, 0);
  928. test_assert(pool->new_chunk_capacity < 60);
  929. allocated = smartlist_new();
  930. for (i = 0; i < 20000; ++i) {
  931. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  932. void *m = mp_pool_get(pool);
  933. memset(m, 0x09, 241);
  934. smartlist_add(allocated, m);
  935. //printf("%d: %p\n", i, m);
  936. //mp_pool_assert_ok(pool);
  937. } else {
  938. int idx = crypto_rand_int(smartlist_len(allocated));
  939. void *m = smartlist_get(allocated, idx);
  940. //printf("%d: free %p\n", i, m);
  941. smartlist_del(allocated, idx);
  942. mp_pool_release(m);
  943. //mp_pool_assert_ok(pool);
  944. }
  945. if (crypto_rand_int(777)==0)
  946. mp_pool_clean(pool, 1, 1);
  947. if (i % 777)
  948. mp_pool_assert_ok(pool);
  949. }
  950. done:
  951. if (allocated) {
  952. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  953. mp_pool_assert_ok(pool);
  954. mp_pool_clean(pool, 0, 0);
  955. mp_pool_assert_ok(pool);
  956. smartlist_free(allocated);
  957. }
  958. if (pool)
  959. mp_pool_destroy(pool);
  960. }
  961. /** Run unittests for memory area allocator */
  962. static void
  963. test_util_memarea(void)
  964. {
  965. memarea_t *area = memarea_new();
  966. char *p1, *p2, *p3, *p1_orig;
  967. void *malloced_ptr = NULL;
  968. int i;
  969. test_assert(area);
  970. p1_orig = p1 = memarea_alloc(area,64);
  971. p2 = memarea_alloc_zero(area,52);
  972. p3 = memarea_alloc(area,11);
  973. test_assert(memarea_owns_ptr(area, p1));
  974. test_assert(memarea_owns_ptr(area, p2));
  975. test_assert(memarea_owns_ptr(area, p3));
  976. /* Make sure we left enough space. */
  977. test_assert(p1+64 <= p2);
  978. test_assert(p2+52 <= p3);
  979. /* Make sure we aligned. */
  980. test_eq(((uintptr_t)p1) % sizeof(void*), 0);
  981. test_eq(((uintptr_t)p2) % sizeof(void*), 0);
  982. test_eq(((uintptr_t)p3) % sizeof(void*), 0);
  983. test_assert(!memarea_owns_ptr(area, p3+8192));
  984. test_assert(!memarea_owns_ptr(area, p3+30));
  985. test_assert(tor_mem_is_zero(p2, 52));
  986. /* Make sure we don't overalign. */
  987. p1 = memarea_alloc(area, 1);
  988. p2 = memarea_alloc(area, 1);
  989. test_eq(p1+sizeof(void*), p2);
  990. {
  991. malloced_ptr = tor_malloc(64);
  992. test_assert(!memarea_owns_ptr(area, malloced_ptr));
  993. tor_free(malloced_ptr);
  994. }
  995. /* memarea_memdup */
  996. {
  997. malloced_ptr = tor_malloc(64);
  998. crypto_rand((char*)malloced_ptr, 64);
  999. p1 = memarea_memdup(area, malloced_ptr, 64);
  1000. test_assert(p1 != malloced_ptr);
  1001. test_memeq(p1, malloced_ptr, 64);
  1002. tor_free(malloced_ptr);
  1003. }
  1004. /* memarea_strdup. */
  1005. p1 = memarea_strdup(area,"");
  1006. p2 = memarea_strdup(area, "abcd");
  1007. test_assert(p1);
  1008. test_assert(p2);
  1009. test_streq(p1, "");
  1010. test_streq(p2, "abcd");
  1011. /* memarea_strndup. */
  1012. {
  1013. const char *s = "Ad ogni porta batte la morte e grida: il nome!";
  1014. /* (From Turandot, act 3.) */
  1015. size_t len = strlen(s);
  1016. p1 = memarea_strndup(area, s, 1000);
  1017. p2 = memarea_strndup(area, s, 10);
  1018. test_streq(p1, s);
  1019. test_assert(p2 >= p1 + len + 1);
  1020. test_memeq(s, p2, 10);
  1021. test_eq(p2[10], '\0');
  1022. p3 = memarea_strndup(area, s, len);
  1023. test_streq(p3, s);
  1024. p3 = memarea_strndup(area, s, len-1);
  1025. test_memeq(s, p3, len-1);
  1026. test_eq(p3[len-1], '\0');
  1027. }
  1028. memarea_clear(area);
  1029. p1 = memarea_alloc(area, 1);
  1030. test_eq(p1, p1_orig);
  1031. memarea_clear(area);
  1032. /* Check for running over an area's size. */
  1033. for (i = 0; i < 512; ++i) {
  1034. p1 = memarea_alloc(area, crypto_rand_int(5)+1);
  1035. test_assert(memarea_owns_ptr(area, p1));
  1036. }
  1037. memarea_assert_ok(area);
  1038. /* Make sure we can allocate a too-big object. */
  1039. p1 = memarea_alloc_zero(area, 9000);
  1040. p2 = memarea_alloc_zero(area, 16);
  1041. test_assert(memarea_owns_ptr(area, p1));
  1042. test_assert(memarea_owns_ptr(area, p2));
  1043. done:
  1044. memarea_drop_all(area);
  1045. tor_free(malloced_ptr);
  1046. }
  1047. /** Run unit tests for utility functions to get file names relative to
  1048. * the data directory. */
  1049. static void
  1050. test_util_datadir(void)
  1051. {
  1052. char buf[1024];
  1053. char *f = NULL;
  1054. char *temp_dir = NULL;
  1055. temp_dir = get_datadir_fname(NULL);
  1056. f = get_datadir_fname("state");
  1057. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
  1058. test_streq(f, buf);
  1059. tor_free(f);
  1060. f = get_datadir_fname2("cache", "thingy");
  1061. tor_snprintf(buf, sizeof(buf),
  1062. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
  1063. test_streq(f, buf);
  1064. tor_free(f);
  1065. f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
  1066. tor_snprintf(buf, sizeof(buf),
  1067. "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
  1068. test_streq(f, buf);
  1069. tor_free(f);
  1070. f = get_datadir_fname_suffix("cache", ".foo");
  1071. tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
  1072. temp_dir);
  1073. test_streq(f, buf);
  1074. done:
  1075. tor_free(f);
  1076. tor_free(temp_dir);
  1077. }
  1078. static void
  1079. test_util_strtok(void)
  1080. {
  1081. char buf[128];
  1082. char buf2[128];
  1083. char *cp1, *cp2;
  1084. strlcpy(buf, "Graved on the dark in gestures of descent", sizeof(buf));
  1085. strlcpy(buf2, "they.seemed;their!own;most.perfect;monument", sizeof(buf2));
  1086. /* -- "Year's End", Richard Wilbur */
  1087. test_streq("Graved", tor_strtok_r_impl(buf, " ", &cp1));
  1088. test_streq("they", tor_strtok_r_impl(buf2, ".!..;!", &cp2));
  1089. #define S1() tor_strtok_r_impl(NULL, " ", &cp1)
  1090. #define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
  1091. test_streq("on", S1());
  1092. test_streq("the", S1());
  1093. test_streq("dark", S1());
  1094. test_streq("seemed", S2());
  1095. test_streq("their", S2());
  1096. test_streq("own", S2());
  1097. test_streq("in", S1());
  1098. test_streq("gestures", S1());
  1099. test_streq("of", S1());
  1100. test_streq("most", S2());
  1101. test_streq("perfect", S2());
  1102. test_streq("descent", S1());
  1103. test_streq("monument", S2());
  1104. test_assert(NULL == S1());
  1105. test_assert(NULL == S2());
  1106. done:
  1107. ;
  1108. }
  1109. static void
  1110. test_util_find_str_at_start_of_line(void *ptr)
  1111. {
  1112. const char *long_string =
  1113. "hello world. hello world. hello hello. howdy.\n"
  1114. "hello hello world\n";
  1115. (void)ptr;
  1116. /* not-found case. */
  1117. tt_assert(! find_str_at_start_of_line(long_string, "fred"));
  1118. /* not-found case where haystack doesn't end with \n */
  1119. tt_assert(! find_str_at_start_of_line("foobar\nbaz", "fred"));
  1120. /* start-of-string case */
  1121. tt_assert(long_string ==
  1122. find_str_at_start_of_line(long_string, "hello world."));
  1123. /* start-of-line case */
  1124. tt_assert(strchr(long_string,'\n')+1 ==
  1125. find_str_at_start_of_line(long_string, "hello hello"));
  1126. done:
  1127. ;
  1128. }
  1129. static void
  1130. test_util_asprintf(void *ptr)
  1131. {
  1132. #define LOREMIPSUM \
  1133. "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
  1134. char *cp=NULL, *cp2=NULL;
  1135. int r;
  1136. (void)ptr;
  1137. /* empty string. */
  1138. r = tor_asprintf(&cp, "%s", "");
  1139. tt_assert(cp);
  1140. tt_int_op(r, ==, strlen(cp));
  1141. tt_str_op(cp, ==, "");
  1142. /* Short string with some printing in it. */
  1143. r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
  1144. tt_assert(cp2);
  1145. tt_int_op(r, ==, strlen(cp2));
  1146. tt_str_op(cp2, ==, "First=101, Second=202");
  1147. tt_assert(cp != cp2);
  1148. tor_free(cp);
  1149. tor_free(cp2);
  1150. /* Glass-box test: a string exactly 128 characters long. */
  1151. r = tor_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
  1152. tt_assert(cp);
  1153. tt_int_op(r, ==, 128);
  1154. tt_assert(cp[128] == '\0');
  1155. tt_str_op(cp, ==,
  1156. "Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM);
  1157. tor_free(cp);
  1158. /* String longer than 128 characters */
  1159. r = tor_asprintf(&cp, "1: %s 2: %s 3: %s",
  1160. LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
  1161. tt_assert(cp);
  1162. tt_int_op(r, ==, strlen(cp));
  1163. tt_str_op(cp, ==, "1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM);
  1164. done:
  1165. tor_free(cp);
  1166. tor_free(cp2);
  1167. }
  1168. static void
  1169. test_util_listdir(void *ptr)
  1170. {
  1171. smartlist_t *dir_contents = NULL;
  1172. char *fname1=NULL, *fname2=NULL, *dirname=NULL;
  1173. (void)ptr;
  1174. fname1 = tor_strdup(get_fname("hopscotch"));
  1175. fname2 = tor_strdup(get_fname("mumblety-peg"));
  1176. dirname = tor_strdup(get_fname(NULL));
  1177. tt_int_op(write_str_to_file(fname1, "X\n", 0), ==, 0);
  1178. tt_int_op(write_str_to_file(fname2, "Y\n", 0), ==, 0);
  1179. dir_contents = tor_listdir(dirname);
  1180. tt_assert(dir_contents);
  1181. /* make sure that each filename is listed. */
  1182. tt_assert(smartlist_string_isin_case(dir_contents, "hopscotch"));
  1183. tt_assert(smartlist_string_isin_case(dir_contents, "mumblety-peg"));
  1184. tt_assert(!smartlist_string_isin(dir_contents, "."));
  1185. tt_assert(!smartlist_string_isin(dir_contents, ".."));
  1186. done:
  1187. tor_free(fname1);
  1188. tor_free(fname2);
  1189. tor_free(dirname);
  1190. if (dir_contents) {
  1191. SMARTLIST_FOREACH(dir_contents, char *, cp, tor_free(cp));
  1192. smartlist_free(dir_contents);
  1193. }
  1194. }
  1195. static void
  1196. test_util_parent_dir(void *ptr)
  1197. {
  1198. char *cp;
  1199. (void)ptr;
  1200. #define T(input,expect_ok,output) \
  1201. do { \
  1202. int ok; \
  1203. cp = tor_strdup(input); \
  1204. ok = get_parent_directory(cp); \
  1205. tt_int_op(ok, ==, expect_ok); \
  1206. if (ok==0) \
  1207. tt_str_op(cp, ==, output); \
  1208. tor_free(cp); \
  1209. } while (0);
  1210. T("/home/wombat/knish", 0, "/home/wombat");
  1211. T("/home/wombat/knish/", 0, "/home/wombat");
  1212. T("./home/wombat/knish/", 0, "./home/wombat");
  1213. T("./wombat", 0, ".");
  1214. T("", -1, "");
  1215. T("/", -1, "");
  1216. T("////", -1, "");
  1217. done:
  1218. tor_free(cp);
  1219. }
  1220. #ifdef _WIN32
  1221. static void
  1222. test_util_load_win_lib(void *ptr)
  1223. {
  1224. HANDLE h = load_windows_system_library("advapi32.dll");
  1225. (void) ptr;
  1226. tt_assert(h);
  1227. done:
  1228. if (h)
  1229. CloseHandle(h);
  1230. }
  1231. #endif
  1232. static void
  1233. clear_hex_errno(char *hex_errno)
  1234. {
  1235. memset(hex_errno, '\0', HEX_ERRNO_SIZE + 1);
  1236. }
  1237. static void
  1238. test_util_exit_status(void *ptr)
  1239. {
  1240. /* Leave an extra byte for a \0 so we can do string comparison */
  1241. char hex_errno[HEX_ERRNO_SIZE + 1];
  1242. (void)ptr;
  1243. clear_hex_errno(hex_errno);
  1244. format_helper_exit_status(0, 0, hex_errno);
  1245. tt_str_op(hex_errno, ==, " 0/0\n");
  1246. clear_hex_errno(hex_errno);
  1247. format_helper_exit_status(0, 0x7FFFFFFF, hex_errno);
  1248. tt_str_op(hex_errno, ==, " 0/7FFFFFFF\n");
  1249. clear_hex_errno(hex_errno);
  1250. format_helper_exit_status(0xFF, -0x80000000, hex_errno);
  1251. tt_str_op(hex_errno, ==, "FF/-80000000\n");
  1252. clear_hex_errno(hex_errno);
  1253. format_helper_exit_status(0x7F, 0, hex_errno);
  1254. tt_str_op(hex_errno, ==, " 7F/0\n");
  1255. clear_hex_errno(hex_errno);
  1256. format_helper_exit_status(0x08, -0x242, hex_errno);
  1257. tt_str_op(hex_errno, ==, " 8/-242\n");
  1258. done:
  1259. ;
  1260. }
  1261. #ifndef _WIN32
  1262. /** Check that fgets waits until a full line, and not return a partial line, on
  1263. * a EAGAIN with a non-blocking pipe */
  1264. static void
  1265. test_util_fgets_eagain(void *ptr)
  1266. {
  1267. int test_pipe[2] = {-1, -1};
  1268. int retval;
  1269. ssize_t retlen;
  1270. char *retptr;
  1271. FILE *test_stream = NULL;
  1272. char buf[10];
  1273. (void)ptr;
  1274. /* Set up a pipe to test on */
  1275. retval = pipe(test_pipe);
  1276. tt_int_op(retval, >=, 0);
  1277. /* Set up the read-end to be non-blocking */
  1278. retval = fcntl(test_pipe[0], F_SETFL, O_NONBLOCK);
  1279. tt_int_op(retval, >=, 0);
  1280. /* Open it as a stdio stream */
  1281. test_stream = fdopen(test_pipe[0], "r");
  1282. tt_ptr_op(test_stream, !=, NULL);
  1283. /* Send in a partial line */
  1284. retlen = write(test_pipe[1], "A", 1);
  1285. tt_int_op(retlen, ==, 1);
  1286. retptr = fgets(buf, sizeof(buf), test_stream);
  1287. tt_want(retptr == NULL);
  1288. tt_int_op(errno, ==, EAGAIN);
  1289. /* Send in the rest */
  1290. retlen = write(test_pipe[1], "B\n", 2);
  1291. tt_int_op(retlen, ==, 2);
  1292. retptr = fgets(buf, sizeof(buf), test_stream);
  1293. tt_ptr_op(retptr, ==, buf);
  1294. tt_str_op(buf, ==, "AB\n");
  1295. /* Send in a full line */
  1296. retlen = write(test_pipe[1], "CD\n", 3);
  1297. tt_int_op(retlen, ==, 3);
  1298. retptr = fgets(buf, sizeof(buf), test_stream);
  1299. tt_ptr_op(retptr, ==, buf);
  1300. tt_str_op(buf, ==, "CD\n");
  1301. /* Send in a partial line */
  1302. retlen = write(test_pipe[1], "E", 1);
  1303. tt_int_op(retlen, ==, 1);
  1304. retptr = fgets(buf, sizeof(buf), test_stream);
  1305. tt_ptr_op(retptr, ==, NULL);
  1306. tt_int_op(errno, ==, EAGAIN);
  1307. /* Send in the rest */
  1308. retlen = write(test_pipe[1], "F\n", 2);
  1309. tt_int_op(retlen, ==, 2);
  1310. retptr = fgets(buf, sizeof(buf), test_stream);
  1311. tt_ptr_op(retptr, ==, buf);
  1312. tt_str_op(buf, ==, "EF\n");
  1313. /* Send in a full line and close */
  1314. retlen = write(test_pipe[1], "GH", 2);
  1315. tt_int_op(retlen, ==, 2);
  1316. retval = close(test_pipe[1]);
  1317. test_pipe[1] = -1;
  1318. tt_int_op(retval, ==, 0);
  1319. retptr = fgets(buf, sizeof(buf), test_stream);
  1320. tt_ptr_op(retptr, ==, buf);
  1321. tt_str_op(buf, ==, "GH");
  1322. /* Check for EOF */
  1323. retptr = fgets(buf, sizeof(buf), test_stream);
  1324. tt_ptr_op(retptr, ==, NULL);
  1325. tt_int_op(feof(test_stream), >, 0);
  1326. done:
  1327. if (test_stream != NULL)
  1328. fclose(test_stream);
  1329. if (test_pipe[0] != -1)
  1330. close(test_pipe[0]);
  1331. if (test_pipe[1] != -1)
  1332. close(test_pipe[1]);
  1333. }
  1334. #endif
  1335. /** Helper function for testing tor_spawn_background */
  1336. static void
  1337. run_util_spawn_background(const char *argv[], const char *expected_out,
  1338. const char *expected_err, int expected_exit,
  1339. int expected_status)
  1340. {
  1341. int retval, exit_code;
  1342. ssize_t pos;
  1343. process_handle_t *process_handle=NULL;
  1344. char stdout_buf[100], stderr_buf[100];
  1345. int status;
  1346. /* Start the program */
  1347. #ifdef _WIN32
  1348. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  1349. #else
  1350. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  1351. #endif
  1352. tt_int_op(status, ==, expected_status);
  1353. if (status == PROCESS_STATUS_ERROR)
  1354. return;
  1355. tt_assert(process_handle != NULL);
  1356. tt_int_op(process_handle->status, ==, expected_status);
  1357. tt_int_op(process_handle->stdout_pipe, >, 0);
  1358. tt_int_op(process_handle->stderr_pipe, >, 0);
  1359. /* Check stdout */
  1360. pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
  1361. sizeof(stdout_buf) - 1);
  1362. tt_assert(pos >= 0);
  1363. stdout_buf[pos] = '\0';
  1364. tt_str_op(stdout_buf, ==, expected_out);
  1365. tt_int_op(pos, ==, strlen(expected_out));
  1366. /* Check it terminated correctly */
  1367. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  1368. tt_int_op(retval, ==, PROCESS_EXIT_EXITED);
  1369. tt_int_op(exit_code, ==, expected_exit);
  1370. // TODO: Make test-child exit with something other than 0
  1371. /* Check stderr */
  1372. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  1373. sizeof(stderr_buf) - 1);
  1374. tt_assert(pos >= 0);
  1375. stderr_buf[pos] = '\0';
  1376. tt_str_op(stderr_buf, ==, expected_err);
  1377. tt_int_op(pos, ==, strlen(expected_err));
  1378. done:
  1379. if (process_handle)
  1380. tor_process_handle_destroy(process_handle, 1);
  1381. }
  1382. /** Check that we can launch a process and read the output */
  1383. static void
  1384. test_util_spawn_background_ok(void *ptr)
  1385. {
  1386. #ifdef _WIN32
  1387. const char *argv[] = {"test-child.exe", "--test", NULL};
  1388. const char *expected_out = "OUT\r\n--test\r\nSLEEPING\r\nDONE\r\n";
  1389. const char *expected_err = "ERR\r\n";
  1390. #else
  1391. const char *argv[] = {BUILDDIR "/src/test/test-child", "--test", NULL};
  1392. const char *expected_out = "OUT\n--test\nSLEEPING\nDONE\n";
  1393. const char *expected_err = "ERR\n";
  1394. #endif
  1395. (void)ptr;
  1396. run_util_spawn_background(argv, expected_out, expected_err, 0,
  1397. PROCESS_STATUS_RUNNING);
  1398. }
  1399. /** Check that failing to find the executable works as expected */
  1400. static void
  1401. test_util_spawn_background_fail(void *ptr)
  1402. {
  1403. const char *argv[] = {BUILDDIR "/src/test/no-such-file", "--test", NULL};
  1404. const char *expected_err = "";
  1405. char expected_out[1024];
  1406. char code[32];
  1407. #ifdef _WIN32
  1408. const int expected_status = PROCESS_STATUS_ERROR;
  1409. #else
  1410. /* TODO: Once we can signal failure to exec, set this to be
  1411. * PROCESS_STATUS_ERROR */
  1412. const int expected_status = PROCESS_STATUS_RUNNING;
  1413. #endif
  1414. (void)ptr;
  1415. tor_snprintf(code, sizeof(code), "%x/%x",
  1416. 9 /* CHILD_STATE_FAILEXEC */ , ENOENT);
  1417. tor_snprintf(expected_out, sizeof(expected_out),
  1418. "ERR: Failed to spawn background process - code %12s\n", code);
  1419. run_util_spawn_background(argv, expected_out, expected_err, 255,
  1420. expected_status);
  1421. }
  1422. /** Test that reading from a handle returns a partial read rather than
  1423. * blocking */
  1424. static void
  1425. test_util_spawn_background_partial_read(void *ptr)
  1426. {
  1427. const int expected_exit = 0;
  1428. const int expected_status = PROCESS_STATUS_RUNNING;
  1429. int retval, exit_code;
  1430. ssize_t pos = -1;
  1431. process_handle_t *process_handle=NULL;
  1432. int status;
  1433. char stdout_buf[100], stderr_buf[100];
  1434. #ifdef _WIN32
  1435. const char *argv[] = {"test-child.exe", "--test", NULL};
  1436. const char *expected_out[] = { "OUT\r\n--test\r\nSLEEPING\r\n",
  1437. "DONE\r\n",
  1438. NULL };
  1439. const char *expected_err = "ERR\r\n";
  1440. #else
  1441. const char *argv[] = {BUILDDIR "/src/test/test-child", "--test", NULL};
  1442. const char *expected_out[] = { "OUT\n--test\nSLEEPING\n",
  1443. "DONE\n",
  1444. NULL };
  1445. const char *expected_err = "ERR\n";
  1446. int eof = 0;
  1447. #endif
  1448. int expected_out_ctr;
  1449. (void)ptr;
  1450. /* Start the program */
  1451. #ifdef _WIN32
  1452. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  1453. #else
  1454. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  1455. #endif
  1456. tt_int_op(status, ==, expected_status);
  1457. tt_assert(process_handle);
  1458. tt_int_op(process_handle->status, ==, expected_status);
  1459. /* Check stdout */
  1460. for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
  1461. #ifdef _WIN32
  1462. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  1463. sizeof(stdout_buf) - 1, NULL);
  1464. #else
  1465. /* Check that we didn't read the end of file last time */
  1466. tt_assert(!eof);
  1467. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  1468. sizeof(stdout_buf) - 1, NULL, &eof);
  1469. #endif
  1470. log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
  1471. /* We would have blocked, keep on trying */
  1472. if (0 == pos)
  1473. continue;
  1474. tt_int_op(pos, >, 0);
  1475. stdout_buf[pos] = '\0';
  1476. tt_str_op(stdout_buf, ==, expected_out[expected_out_ctr]);
  1477. tt_int_op(pos, ==, strlen(expected_out[expected_out_ctr]));
  1478. expected_out_ctr++;
  1479. }
  1480. /* The process should have exited without writing more */
  1481. #ifdef _WIN32
  1482. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  1483. sizeof(stdout_buf) - 1,
  1484. process_handle);
  1485. tt_int_op(pos, ==, 0);
  1486. #else
  1487. if (!eof) {
  1488. /* We should have got all the data, but maybe not the EOF flag */
  1489. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  1490. sizeof(stdout_buf) - 1,
  1491. process_handle, &eof);
  1492. tt_int_op(pos, ==, 0);
  1493. tt_assert(eof);
  1494. }
  1495. /* Otherwise, we got the EOF on the last read */
  1496. #endif
  1497. /* Check it terminated correctly */
  1498. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  1499. tt_int_op(retval, ==, PROCESS_EXIT_EXITED);
  1500. tt_int_op(exit_code, ==, expected_exit);
  1501. // TODO: Make test-child exit with something other than 0
  1502. /* Check stderr */
  1503. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  1504. sizeof(stderr_buf) - 1);
  1505. tt_assert(pos >= 0);
  1506. stderr_buf[pos] = '\0';
  1507. tt_str_op(stderr_buf, ==, expected_err);
  1508. tt_int_op(pos, ==, strlen(expected_err));
  1509. done:
  1510. tor_process_handle_destroy(process_handle, 1);
  1511. }
  1512. /**
  1513. * Test that we can properly format q Windows command line
  1514. */
  1515. static void
  1516. test_util_join_win_cmdline(void *ptr)
  1517. {
  1518. /* Based on some test cases from "Parsing C++ Command-Line Arguments" in
  1519. * MSDN but we don't exercise all quoting rules because tor_join_win_cmdline
  1520. * will try to only generate simple cases for the child process to parse;
  1521. * i.e. we never embed quoted strings in arguments. */
  1522. const char *argvs[][4] = {
  1523. {"a", "bb", "CCC", NULL}, // Normal
  1524. {NULL, NULL, NULL, NULL}, // Empty argument list
  1525. {"", NULL, NULL, NULL}, // Empty argument
  1526. {"\"a", "b\"b", "CCC\"", NULL}, // Quotes
  1527. {"a\tbc", "dd dd", "E", NULL}, // Whitespace
  1528. {"a\\\\\\b", "de fg", "H", NULL}, // Backslashes
  1529. {"a\\\"b", "\\c", "D\\", NULL}, // Backslashes before quote
  1530. {"a\\\\b c", "d", "E", NULL}, // Backslashes not before quote
  1531. { NULL } // Terminator
  1532. };
  1533. const char *cmdlines[] = {
  1534. "a bb CCC",
  1535. "",
  1536. "\"\"",
  1537. "\\\"a b\\\"b CCC\\\"",
  1538. "\"a\tbc\" \"dd dd\" E",
  1539. "a\\\\\\b \"de fg\" H",
  1540. "a\\\\\\\"b \\c D\\",
  1541. "\"a\\\\b c\" d E",
  1542. NULL // Terminator
  1543. };
  1544. int i;
  1545. char *joined_argv;
  1546. (void)ptr;
  1547. for (i=0; cmdlines[i]!=NULL; i++) {
  1548. log_info(LD_GENERAL, "Joining argvs[%d], expecting <%s>", i, cmdlines[i]);
  1549. joined_argv = tor_join_win_cmdline(argvs[i]);
  1550. tt_str_op(joined_argv, ==, cmdlines[i]);
  1551. tor_free(joined_argv);
  1552. }
  1553. done:
  1554. ;
  1555. }
  1556. #define MAX_SPLIT_LINE_COUNT 3
  1557. struct split_lines_test_t {
  1558. const char *orig_line; // Line to be split (may contain \0's)
  1559. int orig_length; // Length of orig_line
  1560. const char *split_line[MAX_SPLIT_LINE_COUNT]; // Split lines
  1561. };
  1562. /**
  1563. * Test that we properly split a buffer into lines
  1564. */
  1565. static void
  1566. test_util_split_lines(void *ptr)
  1567. {
  1568. /* Test cases. orig_line of last test case must be NULL.
  1569. * The last element of split_line[i] must be NULL. */
  1570. struct split_lines_test_t tests[] = {
  1571. {"", 0, {NULL}},
  1572. {"foo", 3, {"foo", NULL}},
  1573. {"\n\rfoo\n\rbar\r\n", 12, {"foo", "bar", NULL}},
  1574. {"fo o\r\nb\tar", 10, {"fo o", "b.ar", NULL}},
  1575. {"\x0f""f\0o\0\n\x01""b\0r\0\r", 12, {".f.o.", ".b.r.", NULL}},
  1576. {NULL, 0, { NULL }}
  1577. };
  1578. int i, j;
  1579. char *orig_line=NULL;
  1580. smartlist_t *sl=NULL;
  1581. (void)ptr;
  1582. for (i=0; tests[i].orig_line; i++) {
  1583. sl = smartlist_new();
  1584. /* Allocate space for string and trailing NULL */
  1585. orig_line = tor_memdup(tests[i].orig_line, tests[i].orig_length + 1);
  1586. tor_split_lines(sl, orig_line, tests[i].orig_length);
  1587. j = 0;
  1588. log_info(LD_GENERAL, "Splitting test %d of length %d",
  1589. i, tests[i].orig_length);
  1590. SMARTLIST_FOREACH(sl, const char *, line,
  1591. {
  1592. /* Check we have not got too many lines */
  1593. tt_int_op(j, <, MAX_SPLIT_LINE_COUNT);
  1594. /* Check that there actually should be a line here */
  1595. tt_assert(tests[i].split_line[j] != NULL);
  1596. log_info(LD_GENERAL, "Line %d of test %d, should be <%s>",
  1597. j, i, tests[i].split_line[j]);
  1598. /* Check that the line is as expected */
  1599. tt_str_op(tests[i].split_line[j], ==, line);
  1600. j++;
  1601. });
  1602. /* Check that we didn't miss some lines */
  1603. tt_assert(tests[i].split_line[j] == NULL);
  1604. tor_free(orig_line);
  1605. smartlist_free(sl);
  1606. sl = NULL;
  1607. }
  1608. done:
  1609. tor_free(orig_line);
  1610. smartlist_free(sl);
  1611. }
  1612. static void
  1613. test_util_di_ops(void)
  1614. {
  1615. #define LT -1
  1616. #define GT 1
  1617. #define EQ 0
  1618. const struct {
  1619. const char *a; int want_sign; const char *b;
  1620. } examples[] = {
  1621. { "Foo", EQ, "Foo" },
  1622. { "foo", GT, "bar", },
  1623. { "foobar", EQ ,"foobar" },
  1624. { "foobar", LT, "foobaw" },
  1625. { "foobar", GT, "f00bar" },
  1626. { "foobar", GT, "boobar" },
  1627. { "", EQ, "" },
  1628. { NULL, 0, NULL },
  1629. };
  1630. int i;
  1631. for (i = 0; examples[i].a; ++i) {
  1632. size_t len = strlen(examples[i].a);
  1633. int eq1, eq2, neq1, neq2, cmp1, cmp2;
  1634. test_eq(len, strlen(examples[i].b));
  1635. /* We do all of the operations, with operands in both orders. */
  1636. eq1 = tor_memeq(examples[i].a, examples[i].b, len);
  1637. eq2 = tor_memeq(examples[i].b, examples[i].a, len);
  1638. neq1 = tor_memneq(examples[i].a, examples[i].b, len);
  1639. neq2 = tor_memneq(examples[i].b, examples[i].a, len);
  1640. cmp1 = tor_memcmp(examples[i].a, examples[i].b, len);
  1641. cmp2 = tor_memcmp(examples[i].b, examples[i].a, len);
  1642. /* Check for correctness of cmp1 */
  1643. if (cmp1 < 0 && examples[i].want_sign != LT)
  1644. test_fail();
  1645. else if (cmp1 > 0 && examples[i].want_sign != GT)
  1646. test_fail();
  1647. else if (cmp1 == 0 && examples[i].want_sign != EQ)
  1648. test_fail();
  1649. /* Check for consistency of everything else with cmp1 */
  1650. test_eq(eq1, eq2);
  1651. test_eq(neq1, neq2);
  1652. test_eq(cmp1, -cmp2);
  1653. test_eq(eq1, cmp1 == 0);
  1654. test_eq(neq1, !eq1);
  1655. }
  1656. done:
  1657. ;
  1658. }
  1659. /**
  1660. * Test counting high bits
  1661. */
  1662. static void
  1663. test_util_n_bits_set(void *ptr)
  1664. {
  1665. (void)ptr;
  1666. test_eq(n_bits_set_u8(0), 0);
  1667. test_eq(n_bits_set_u8(1), 1);
  1668. test_eq(n_bits_set_u8(129), 2);
  1669. test_eq(n_bits_set_u8(255), 8);
  1670. done:
  1671. ;
  1672. }
  1673. /**
  1674. * Test LHS whitespace (and comment) eater
  1675. */
  1676. static void
  1677. test_util_eat_whitespace(void *ptr)
  1678. {
  1679. const char ws[] = { ' ', '\t', '\r' }; /* Except NL */
  1680. char str[80];
  1681. size_t i;
  1682. (void)ptr;
  1683. /* Try one leading ws */
  1684. strcpy(str, "fuubaar");
  1685. for (i = 0; i < sizeof(ws); ++i) {
  1686. str[0] = ws[i];
  1687. test_streq(eat_whitespace(str), str + 1);
  1688. test_streq(eat_whitespace_eos(str, str + strlen(str)), str + 1);
  1689. test_streq(eat_whitespace_eos_no_nl(str, str + strlen(str)), str + 1);
  1690. test_streq(eat_whitespace_no_nl(str), str + 1);
  1691. }
  1692. str[0] = '\n';
  1693. test_streq(eat_whitespace(str), str + 1);
  1694. test_streq(eat_whitespace_eos(str, str + strlen(str)), str + 1);
  1695. /* Empty string */
  1696. strcpy(str, "");
  1697. test_eq_ptr(eat_whitespace(str), str);
  1698. test_eq_ptr(eat_whitespace_eos(str, str), str);
  1699. test_eq_ptr(eat_whitespace_eos_no_nl(str, str), str);
  1700. test_eq_ptr(eat_whitespace_no_nl(str), str);
  1701. /* Only ws */
  1702. strcpy(str, " \t\r\n");
  1703. test_eq_ptr(eat_whitespace(str), str + strlen(str));
  1704. test_eq_ptr(eat_whitespace_eos(str, str + strlen(str)), str + strlen(str));
  1705. strcpy(str, " \t\r ");
  1706. test_eq_ptr(eat_whitespace_no_nl(str), str + strlen(str));
  1707. test_eq_ptr(eat_whitespace_eos_no_nl(str, str + strlen(str)),
  1708. str + strlen(str));
  1709. /* Multiple ws */
  1710. strcpy(str, "fuubaar");
  1711. for (i = 0; i < sizeof(ws); ++i)
  1712. str[i] = ws[i];
  1713. test_streq(eat_whitespace(str), str + sizeof(ws));
  1714. test_streq(eat_whitespace_eos(str, str + strlen(str)), str + sizeof(ws));
  1715. test_streq(eat_whitespace_no_nl(str), str + sizeof(ws));
  1716. test_streq(eat_whitespace_eos_no_nl(str, str + strlen(str)),
  1717. str + sizeof(ws));
  1718. /* Eat comment */
  1719. strcpy(str, "# Comment \n No Comment");
  1720. test_streq(eat_whitespace(str), "No Comment");
  1721. test_streq(eat_whitespace_eos(str, str + strlen(str)), "No Comment");
  1722. /* Eat comment & ws mix */
  1723. strcpy(str, " # \t Comment \n\t\nNo Comment");
  1724. test_streq(eat_whitespace(str), "No Comment");
  1725. test_streq(eat_whitespace_eos(str, str + strlen(str)), "No Comment");
  1726. /* Eat entire comment */
  1727. strcpy(str, "#Comment");
  1728. test_eq_ptr(eat_whitespace(str), str + strlen(str));
  1729. test_eq_ptr(eat_whitespace_eos(str, str + strlen(str)), str + strlen(str));
  1730. done:
  1731. ;
  1732. }
  1733. /** Return a newly allocated smartlist containing the lines of text in
  1734. * <b>lines</b>. The returned strings are heap-allocated, and must be
  1735. * freed by the caller.
  1736. *
  1737. * XXXX? Move to container.[hc] ? */
  1738. static smartlist_t *
  1739. smartlist_new_from_text_lines(const char *lines)
  1740. {
  1741. smartlist_t *sl = smartlist_new();
  1742. char *last_line;
  1743. smartlist_split_string(sl, lines, "\n", 0, 0);
  1744. last_line = smartlist_pop_last(sl);
  1745. if (last_line != NULL && *last_line != '\0') {
  1746. smartlist_add(sl, last_line);
  1747. }
  1748. return sl;
  1749. }
  1750. /** Test smartlist_new_from_text_lines */
  1751. static void
  1752. test_util_sl_new_from_text_lines(void *ptr)
  1753. {
  1754. (void)ptr;
  1755. { /* Normal usage */
  1756. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz\n");
  1757. int sl_len = smartlist_len(sl);
  1758. tt_want_int_op(sl_len, ==, 3);
  1759. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  1760. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), ==, "bar");
  1761. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), ==, "baz");
  1762. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  1763. smartlist_free(sl);
  1764. }
  1765. { /* No final newline */
  1766. smartlist_t *sl = smartlist_new_from_text_lines("foo\nbar\nbaz");
  1767. int sl_len = smartlist_len(sl);
  1768. tt_want_int_op(sl_len, ==, 3);
  1769. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  1770. if (sl_len > 1) tt_want_str_op(smartlist_get(sl, 1), ==, "bar");
  1771. if (sl_len > 2) tt_want_str_op(smartlist_get(sl, 2), ==, "baz");
  1772. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  1773. smartlist_free(sl);
  1774. }
  1775. { /* No newlines */
  1776. smartlist_t *sl = smartlist_new_from_text_lines("foo");
  1777. int sl_len = smartlist_len(sl);
  1778. tt_want_int_op(sl_len, ==, 1);
  1779. if (sl_len > 0) tt_want_str_op(smartlist_get(sl, 0), ==, "foo");
  1780. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  1781. smartlist_free(sl);
  1782. }
  1783. { /* No text at all */
  1784. smartlist_t *sl = smartlist_new_from_text_lines("");
  1785. int sl_len = smartlist_len(sl);
  1786. tt_want_int_op(sl_len, ==, 0);
  1787. SMARTLIST_FOREACH(sl, void *, x, tor_free(x));
  1788. smartlist_free(sl);
  1789. }
  1790. }
  1791. /** Test process_environment_make */
  1792. static void
  1793. test_util_make_environment(void *ptr)
  1794. {
  1795. const char *env_vars_string =
  1796. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  1797. "HOME=/home/foozer\n";
  1798. const char expected_windows_env_block[] =
  1799. "HOME=/home/foozer\000"
  1800. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\000"
  1801. "\000";
  1802. size_t expected_windows_env_block_len =
  1803. sizeof(expected_windows_env_block) - 1;
  1804. smartlist_t *env_vars = smartlist_new_from_text_lines(env_vars_string);
  1805. smartlist_t *env_vars_sorted = smartlist_new();
  1806. smartlist_t *env_vars_in_unixoid_env_block_sorted = smartlist_new();
  1807. process_environment_t *env;
  1808. (void)ptr;
  1809. env = process_environment_make(env_vars);
  1810. /* Check that the Windows environment block is correct. */
  1811. tt_want(tor_memeq(expected_windows_env_block, env->windows_environment_block,
  1812. expected_windows_env_block_len));
  1813. /* Now for the Unixoid environment block. We don't care which order
  1814. * these environment variables are in, so we sort both lists first. */
  1815. smartlist_add_all(env_vars_sorted, env_vars);
  1816. {
  1817. char **v;
  1818. for (v = env->unixoid_environment_block; *v; ++v) {
  1819. smartlist_add(env_vars_in_unixoid_env_block_sorted, *v);
  1820. }
  1821. }
  1822. smartlist_sort_strings(env_vars_sorted);
  1823. smartlist_sort_strings(env_vars_in_unixoid_env_block_sorted);
  1824. tt_want_int_op(smartlist_len(env_vars_sorted), ==,
  1825. smartlist_len(env_vars_in_unixoid_env_block_sorted));
  1826. {
  1827. int len = smartlist_len(env_vars_sorted);
  1828. int i;
  1829. if (smartlist_len(env_vars_in_unixoid_env_block_sorted) < len) {
  1830. len = smartlist_len(env_vars_in_unixoid_env_block_sorted);
  1831. }
  1832. for (i = 0; i < len; ++i) {
  1833. tt_want_str_op(smartlist_get(env_vars_sorted, i), ==,
  1834. smartlist_get(env_vars_in_unixoid_env_block_sorted, i));
  1835. }
  1836. }
  1837. /* Clean up. */
  1838. smartlist_free(env_vars_in_unixoid_env_block_sorted);
  1839. smartlist_free(env_vars_sorted);
  1840. SMARTLIST_FOREACH(env_vars, char *, x, tor_free(x));
  1841. smartlist_free(env_vars);
  1842. process_environment_free(env);
  1843. }
  1844. /** Test set_environment_variable_in_smartlist */
  1845. static void
  1846. test_util_set_env_var_in_sl(void *ptr)
  1847. {
  1848. /* The environment variables in these strings are in arbitrary
  1849. * order; we sort the resulting lists before comparing them.
  1850. *
  1851. * (They *will not* end up in the order shown in
  1852. * expected_resulting_env_vars_string.) */
  1853. const char *base_env_vars_string =
  1854. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  1855. "HOME=/home/foozer\n"
  1856. "TERM=xterm\n"
  1857. "SHELL=/bin/ksh\n"
  1858. "USER=foozer\n"
  1859. "LOGNAME=foozer\n"
  1860. "USERNAME=foozer\n"
  1861. "LANG=en_US.utf8\n"
  1862. ;
  1863. const char *new_env_vars_string =
  1864. "TERM=putty\n"
  1865. "DISPLAY=:18.0\n"
  1866. ;
  1867. const char *expected_resulting_env_vars_string =
  1868. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/bin\n"
  1869. "HOME=/home/foozer\n"
  1870. "TERM=putty\n"
  1871. "SHELL=/bin/ksh\n"
  1872. "USER=foozer\n"
  1873. "LOGNAME=foozer\n"
  1874. "USERNAME=foozer\n"
  1875. "LANG=en_US.utf8\n"
  1876. "DISPLAY=:18.0\n"
  1877. ;
  1878. smartlist_t *merged_env_vars =
  1879. smartlist_new_from_text_lines(base_env_vars_string);
  1880. smartlist_t *new_env_vars =
  1881. smartlist_new_from_text_lines(new_env_vars_string);
  1882. smartlist_t *expected_resulting_env_vars =
  1883. smartlist_new_from_text_lines(expected_resulting_env_vars_string);
  1884. /* Elements of merged_env_vars are heap-allocated, and must be
  1885. * freed. Some of them are (or should) be freed by
  1886. * set_environment_variable_in_smartlist.
  1887. *
  1888. * Elements of new_env_vars are heap-allocated, but are copied into
  1889. * merged_env_vars, so they are not freed separately at the end of
  1890. * the function.
  1891. *
  1892. * Elements of expected_resulting_env_vars are heap-allocated, and
  1893. * must be freed. */
  1894. (void)ptr;
  1895. SMARTLIST_FOREACH(new_env_vars, char *, env_var,
  1896. set_environment_variable_in_smartlist(merged_env_vars,
  1897. env_var,
  1898. _tor_free,
  1899. 1));
  1900. smartlist_sort_strings(merged_env_vars);
  1901. smartlist_sort_strings(expected_resulting_env_vars);
  1902. tt_want_int_op(smartlist_len(merged_env_vars), ==,
  1903. smartlist_len(expected_resulting_env_vars));
  1904. {
  1905. int len = smartlist_len(merged_env_vars);
  1906. int i;
  1907. if (smartlist_len(expected_resulting_env_vars) < len) {
  1908. len = smartlist_len(expected_resulting_env_vars);
  1909. }
  1910. for (i = 0; i < len; ++i) {
  1911. tt_want_str_op(smartlist_get(merged_env_vars, i), ==,
  1912. smartlist_get(expected_resulting_env_vars, i));
  1913. }
  1914. }
  1915. /* Clean up. */
  1916. SMARTLIST_FOREACH(merged_env_vars, char *, x, tor_free(x));
  1917. smartlist_free(merged_env_vars);
  1918. smartlist_free(new_env_vars);
  1919. SMARTLIST_FOREACH(expected_resulting_env_vars, char *, x, tor_free(x));
  1920. smartlist_free(expected_resulting_env_vars);
  1921. }
  1922. #define UTIL_LEGACY(name) \
  1923. { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
  1924. #define UTIL_TEST(name, flags) \
  1925. { #name, test_util_ ## name, flags, NULL, NULL }
  1926. struct testcase_t util_tests[] = {
  1927. UTIL_LEGACY(time),
  1928. UTIL_LEGACY(config_line),
  1929. UTIL_LEGACY(config_line_quotes),
  1930. UTIL_LEGACY(config_line_escaped_content),
  1931. UTIL_LEGACY(strmisc),
  1932. UTIL_LEGACY(pow2),
  1933. UTIL_LEGACY(gzip),
  1934. UTIL_LEGACY(datadir),
  1935. UTIL_LEGACY(mempool),
  1936. UTIL_LEGACY(memarea),
  1937. UTIL_LEGACY(control_formats),
  1938. UTIL_LEGACY(mmap),
  1939. UTIL_LEGACY(threads),
  1940. UTIL_LEGACY(sscanf),
  1941. UTIL_LEGACY(strtok),
  1942. UTIL_LEGACY(di_ops),
  1943. UTIL_TEST(find_str_at_start_of_line, 0),
  1944. UTIL_TEST(asprintf, 0),
  1945. UTIL_TEST(listdir, 0),
  1946. UTIL_TEST(parent_dir, 0),
  1947. #ifdef _WIN32
  1948. UTIL_TEST(load_win_lib, 0),
  1949. #endif
  1950. UTIL_TEST(exit_status, 0),
  1951. #ifndef _WIN32
  1952. UTIL_TEST(fgets_eagain, TT_SKIP),
  1953. #endif
  1954. UTIL_TEST(spawn_background_ok, 0),
  1955. UTIL_TEST(spawn_background_fail, 0),
  1956. UTIL_TEST(spawn_background_partial_read, 0),
  1957. UTIL_TEST(join_win_cmdline, 0),
  1958. UTIL_TEST(split_lines, 0),
  1959. UTIL_TEST(n_bits_set, 0),
  1960. UTIL_TEST(eat_whitespace, 0),
  1961. UTIL_TEST(sl_new_from_text_lines, 0),
  1962. UTIL_TEST(make_environment, 0),
  1963. UTIL_TEST(set_env_var_in_sl, 0),
  1964. END_OF_TESTCASES
  1965. };