test_util.c 121 KB

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