test_util.c 121 KB

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