test_util.c 124 KB

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