test_util.c 121 KB

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