test_main.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236
  1. /*
  2. * Copyright (c) 2003-2009 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "test.h"
  26. #include "test_utils.h"
  27. #ifdef HAVE_SYS_IOCTL_H
  28. #include <sys/ioctl.h>
  29. #endif
  30. #ifdef HAVE_SYS_TIME_H
  31. #include <sys/time.h>
  32. #endif
  33. #include <errno.h>
  34. #ifdef HAVE_ICONV_H
  35. #include <iconv.h>
  36. #endif
  37. /*
  38. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  39. * As the include guards don't agree, the order of include is important.
  40. */
  41. #ifdef HAVE_LINUX_EXT2_FS_H
  42. #include <linux/ext2_fs.h> /* for Linux file flags */
  43. #endif
  44. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  45. #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
  46. #endif
  47. #ifdef HAVE_LINUX_FS_H
  48. #include <linux/fs.h>
  49. #endif
  50. #include <limits.h>
  51. #include <locale.h>
  52. #ifdef HAVE_SIGNAL_H
  53. #include <signal.h>
  54. #endif
  55. #include <stdarg.h>
  56. #include <time.h>
  57. #ifdef HAVE_SIGNAL_H
  58. #endif
  59. #ifdef HAVE_ACL_LIBACL_H
  60. #include <acl/libacl.h>
  61. #endif
  62. #ifdef HAVE_SYS_TYPES_H
  63. #include <sys/types.h>
  64. #endif
  65. #ifdef HAVE_SYS_ACL_H
  66. #include <sys/acl.h>
  67. #endif
  68. #ifdef HAVE_SYS_EA_H
  69. #include <sys/ea.h>
  70. #endif
  71. #ifdef HAVE_SYS_EXTATTR_H
  72. #include <sys/extattr.h>
  73. #endif
  74. #if HAVE_SYS_XATTR_H
  75. #include <sys/xattr.h>
  76. #elif HAVE_ATTR_XATTR_H
  77. #include <attr/xattr.h>
  78. #endif
  79. #ifdef HAVE_SYS_RICHACL_H
  80. #include <sys/richacl.h>
  81. #endif
  82. #if HAVE_MEMBERSHIP_H
  83. #include <membership.h>
  84. #endif
  85. /*
  86. *
  87. * Windows support routines
  88. *
  89. * Note: Configuration is a tricky issue. Using HAVE_* feature macros
  90. * in the test harness is dangerous because they cover up
  91. * configuration errors. The classic example of this is omitting a
  92. * configure check. If libarchive and libarchive_test both look for
  93. * the same feature macro, such errors are hard to detect. Platform
  94. * macros (e.g., _WIN32 or __GNUC__) are a little better, but can
  95. * easily lead to very messy code. It's best to limit yourself
  96. * to only the most generic programming techniques in the test harness
  97. * and thus avoid conditionals altogether. Where that's not possible,
  98. * try to minimize conditionals by grouping platform-specific tests in
  99. * one place (e.g., test_acl_freebsd) or by adding new assert()
  100. * functions (e.g., assertMakeHardlink()) to cover up platform
  101. * differences. Platform-specific coding in libarchive_test is often
  102. * a symptom that some capability is missing from libarchive itself.
  103. */
  104. #if defined(_WIN32) && !defined(__CYGWIN__)
  105. #include <io.h>
  106. #include <direct.h>
  107. #include <windows.h>
  108. #ifndef F_OK
  109. #define F_OK (0)
  110. #endif
  111. #ifndef S_ISDIR
  112. #define S_ISDIR(m) ((m) & _S_IFDIR)
  113. #endif
  114. #ifndef S_ISREG
  115. #define S_ISREG(m) ((m) & _S_IFREG)
  116. #endif
  117. #if !defined(__BORLANDC__)
  118. #define access _access
  119. #undef chdir
  120. #define chdir _chdir
  121. #endif
  122. #ifndef fileno
  123. #define fileno _fileno
  124. #endif
  125. /*#define fstat _fstat64*/
  126. #if !defined(__BORLANDC__)
  127. #define getcwd _getcwd
  128. #endif
  129. #define lstat stat
  130. /*#define lstat _stat64*/
  131. /*#define stat _stat64*/
  132. #define rmdir _rmdir
  133. #if !defined(__BORLANDC__)
  134. #define strdup _strdup
  135. #define umask _umask
  136. #endif
  137. #define int64_t __int64
  138. #endif
  139. #if defined(HAVE__CrtSetReportMode)
  140. # include <crtdbg.h>
  141. #endif
  142. mode_t umasked(mode_t expected_mode)
  143. {
  144. mode_t mode = umask(0);
  145. umask(mode);
  146. return expected_mode & ~mode;
  147. }
  148. /* Path to working directory for current test */
  149. const char *testworkdir;
  150. #ifdef PROGRAM
  151. /* Pathname of exe to be tested. */
  152. const char *testprogfile;
  153. /* Name of exe to use in printf-formatted command strings. */
  154. /* On Windows, this includes leading/trailing quotes. */
  155. const char *testprog;
  156. #endif
  157. #if defined(_WIN32) && !defined(__CYGWIN__)
  158. static void *GetFunctionKernel32(const char *);
  159. static int my_CreateSymbolicLinkA(const char *, const char *, int);
  160. static int my_CreateHardLinkA(const char *, const char *);
  161. static int my_GetFileInformationByName(const char *,
  162. BY_HANDLE_FILE_INFORMATION *);
  163. typedef struct _REPARSE_DATA_BUFFER {
  164. ULONG ReparseTag;
  165. USHORT ReparseDataLength;
  166. USHORT Reserved;
  167. union {
  168. struct {
  169. USHORT SubstituteNameOffset;
  170. USHORT SubstituteNameLength;
  171. USHORT PrintNameOffset;
  172. USHORT PrintNameLength;
  173. ULONG Flags;
  174. WCHAR PathBuffer[1];
  175. } SymbolicLinkReparseBuffer;
  176. struct {
  177. USHORT SubstituteNameOffset;
  178. USHORT SubstituteNameLength;
  179. USHORT PrintNameOffset;
  180. USHORT PrintNameLength;
  181. WCHAR PathBuffer[1];
  182. } MountPointReparseBuffer;
  183. struct {
  184. UCHAR DataBuffer[1];
  185. } GenericReparseBuffer;
  186. } DUMMYUNIONNAME;
  187. } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
  188. static void *
  189. GetFunctionKernel32(const char *name)
  190. {
  191. static HINSTANCE lib;
  192. static int set;
  193. if (!set) {
  194. set = 1;
  195. lib = LoadLibrary("kernel32.dll");
  196. }
  197. if (lib == NULL) {
  198. fprintf(stderr, "Can't load kernel32.dll?!\n");
  199. exit(1);
  200. }
  201. return (void *)GetProcAddress(lib, name);
  202. }
  203. static int
  204. my_CreateSymbolicLinkA(const char *linkname, const char *target,
  205. int targetIsDir)
  206. {
  207. static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, DWORD);
  208. DWORD attrs;
  209. static int set;
  210. int ret, tmpflags, llen, tlen;
  211. int flags = 0;
  212. char *src, *tgt, *p;
  213. if (!set) {
  214. set = 1;
  215. f = GetFunctionKernel32("CreateSymbolicLinkA");
  216. }
  217. if (f == NULL)
  218. return (0);
  219. tlen = strlen(target);
  220. llen = strlen(linkname);
  221. if (tlen == 0 || llen == 0)
  222. return (0);
  223. tgt = malloc((tlen + 1) * sizeof(char));
  224. if (tgt == NULL)
  225. return (0);
  226. src = malloc((llen + 1) * sizeof(char));
  227. if (src == NULL) {
  228. free(tgt);
  229. return (0);
  230. }
  231. /*
  232. * Translate slashes to backslashes
  233. */
  234. p = src;
  235. while(*linkname != '\0') {
  236. if (*linkname == '/')
  237. *p = '\\';
  238. else
  239. *p = *linkname;
  240. linkname++;
  241. p++;
  242. }
  243. *p = '\0';
  244. p = tgt;
  245. while(*target != '\0') {
  246. if (*target == '/')
  247. *p = '\\';
  248. else
  249. *p = *target;
  250. target++;
  251. p++;
  252. }
  253. *p = '\0';
  254. /*
  255. * Each test has to specify if a file or a directory symlink
  256. * should be created.
  257. */
  258. if (targetIsDir) {
  259. #if defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
  260. flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
  261. #else
  262. flags |= 0x1;
  263. #endif
  264. }
  265. #if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
  266. tmpflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
  267. #else
  268. tmpflags = flags | 0x2;
  269. #endif
  270. /*
  271. * Windows won't overwrite existing links
  272. */
  273. attrs = GetFileAttributesA(linkname);
  274. if (attrs != INVALID_FILE_ATTRIBUTES) {
  275. if (attrs & FILE_ATTRIBUTE_DIRECTORY)
  276. RemoveDirectoryA(linkname);
  277. else
  278. DeleteFileA(linkname);
  279. }
  280. ret = (*f)(src, tgt, tmpflags);
  281. /*
  282. * Prior to Windows 10 the SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
  283. * is not understood
  284. */
  285. if (!ret)
  286. ret = (*f)(src, tgt, flags);
  287. free(src);
  288. free(tgt);
  289. return (ret);
  290. }
  291. static int
  292. my_CreateHardLinkA(const char *linkname, const char *target)
  293. {
  294. static BOOLEAN (WINAPI *f)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
  295. static int set;
  296. if (!set) {
  297. set = 1;
  298. f = GetFunctionKernel32("CreateHardLinkA");
  299. }
  300. return f == NULL ? 0 : (*f)(linkname, target, NULL);
  301. }
  302. static int
  303. my_GetFileInformationByName(const char *path, BY_HANDLE_FILE_INFORMATION *bhfi)
  304. {
  305. HANDLE h;
  306. int r;
  307. memset(bhfi, 0, sizeof(*bhfi));
  308. h = CreateFileA(path, FILE_READ_ATTRIBUTES, 0, NULL,
  309. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  310. if (h == INVALID_HANDLE_VALUE)
  311. return (0);
  312. r = GetFileInformationByHandle(h, bhfi);
  313. CloseHandle(h);
  314. return (r);
  315. }
  316. #endif
  317. #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
  318. static void
  319. invalid_parameter_handler(const wchar_t * expression,
  320. const wchar_t * function, const wchar_t * file,
  321. unsigned int line, uintptr_t pReserved)
  322. {
  323. /* nop */
  324. // Silence unused-parameter compiler warnings.
  325. (void)expression;
  326. (void)function;
  327. (void)file;
  328. (void)line;
  329. (void)pReserved;
  330. }
  331. #endif
  332. /*
  333. *
  334. * OPTIONS FLAGS
  335. *
  336. */
  337. /* Enable core dump on failure. */
  338. static int dump_on_failure = 0;
  339. /* Default is to remove temp dirs and log data for successful tests. */
  340. static int keep_temp_files = 0;
  341. /* Default is to run the specified tests once and report errors. */
  342. static int until_failure = 0;
  343. /* Default is to just report pass/fail for each test. */
  344. static int verbosity = 0;
  345. #define VERBOSITY_SUMMARY_ONLY -1 /* -q */
  346. #define VERBOSITY_PASSFAIL 0 /* Default */
  347. #define VERBOSITY_LIGHT_REPORT 1 /* -v */
  348. #define VERBOSITY_FULL 2 /* -vv */
  349. /* A few places generate even more output for verbosity > VERBOSITY_FULL,
  350. * mostly for debugging the test harness itself. */
  351. /* Cumulative count of assertion failures. */
  352. static int failures = 0;
  353. /* Cumulative count of reported skips. */
  354. static int skips = 0;
  355. /* Cumulative count of assertions checked. */
  356. static int assertions = 0;
  357. /* Directory where uuencoded reference files can be found. */
  358. static const char *refdir;
  359. /*
  360. * Report log information selectively to console and/or disk log.
  361. */
  362. static int log_console = 0;
  363. static FILE *logfile;
  364. static void __LA_PRINTFLIKE(1, 0)
  365. vlogprintf(const char *fmt, va_list ap)
  366. {
  367. #ifdef va_copy
  368. va_list lfap;
  369. va_copy(lfap, ap);
  370. #endif
  371. if (log_console)
  372. vfprintf(stdout, fmt, ap);
  373. if (logfile != NULL)
  374. #ifdef va_copy
  375. vfprintf(logfile, fmt, lfap);
  376. va_end(lfap);
  377. #else
  378. vfprintf(logfile, fmt, ap);
  379. #endif
  380. }
  381. static void __LA_PRINTFLIKE(1, 2)
  382. logprintf(const char *fmt, ...)
  383. {
  384. va_list ap;
  385. va_start(ap, fmt);
  386. vlogprintf(fmt, ap);
  387. va_end(ap);
  388. }
  389. /* Set up a message to display only if next assertion fails. */
  390. static char msgbuff[4096];
  391. static const char *msg, *nextmsg;
  392. void
  393. failure(const char *fmt, ...)
  394. {
  395. va_list ap;
  396. if (fmt == NULL) {
  397. nextmsg = NULL;
  398. } else {
  399. va_start(ap, fmt);
  400. vsnprintf(msgbuff, sizeof(msgbuff), fmt, ap);
  401. va_end(ap);
  402. nextmsg = msgbuff;
  403. }
  404. }
  405. /*
  406. * Copy arguments into file-local variables.
  407. * This was added to permit vararg assert() functions without needing
  408. * variadic wrapper macros. Turns out that the vararg capability is almost
  409. * never used, so almost all of the vararg assertions can be simplified
  410. * by removing the vararg capability and reworking the wrapper macro to
  411. * pass __FILE__, __LINE__ directly into the function instead of using
  412. * this hook. I suspect this machinery is used so rarely that we
  413. * would be better off just removing it entirely. That would simplify
  414. * the code here noticeably.
  415. */
  416. static const char *skipping_filename;
  417. static int skipping_line;
  418. void skipping_setup(const char *filename, int line)
  419. {
  420. skipping_filename = filename;
  421. skipping_line = line;
  422. }
  423. /* Called at the beginning of each assert() function. */
  424. static void
  425. assertion_count(const char *file, int line)
  426. {
  427. (void)file; /* UNUSED */
  428. (void)line; /* UNUSED */
  429. ++assertions;
  430. /* Proper handling of "failure()" message. */
  431. msg = nextmsg;
  432. nextmsg = NULL;
  433. /* Uncomment to print file:line after every assertion.
  434. * Verbose, but occasionally useful in tracking down crashes. */
  435. /* printf("Checked %s:%d\n", file, line); */
  436. }
  437. /*
  438. * For each test source file, we remember how many times each
  439. * assertion was reported. Cleared before each new test,
  440. * used by test_summarize().
  441. */
  442. static struct line {
  443. int count;
  444. int skip;
  445. } failed_lines[10000];
  446. static const char *failed_filename;
  447. /* Count this failure, setup up log destination and handle initial report. */
  448. static void __LA_PRINTFLIKE(3, 4)
  449. failure_start(const char *filename, int line, const char *fmt, ...)
  450. {
  451. va_list ap;
  452. /* Record another failure for this line. */
  453. ++failures;
  454. failed_filename = filename;
  455. failed_lines[line].count++;
  456. /* Determine whether to log header to console. */
  457. switch (verbosity) {
  458. case VERBOSITY_LIGHT_REPORT:
  459. log_console = (failed_lines[line].count < 2);
  460. break;
  461. default:
  462. log_console = (verbosity >= VERBOSITY_FULL);
  463. }
  464. /* Log file:line header for this failure */
  465. va_start(ap, fmt);
  466. #if _MSC_VER
  467. logprintf("%s(%d): ", filename, line);
  468. #else
  469. logprintf("%s:%d: ", filename, line);
  470. #endif
  471. vlogprintf(fmt, ap);
  472. va_end(ap);
  473. logprintf("\n");
  474. if (msg != NULL && msg[0] != '\0') {
  475. logprintf(" Description: %s\n", msg);
  476. msg = NULL;
  477. }
  478. /* Determine whether to log details to console. */
  479. if (verbosity == VERBOSITY_LIGHT_REPORT)
  480. log_console = 0;
  481. }
  482. /* Complete reporting of failed tests. */
  483. /*
  484. * The 'extra' hook here is used by libarchive to include libarchive
  485. * error messages with assertion failures. It could also be used
  486. * to add strerror() output, for example. Just define the EXTRA_DUMP()
  487. * macro appropriately.
  488. */
  489. static void
  490. failure_finish(void *extra)
  491. {
  492. (void)extra; /* UNUSED (maybe) */
  493. #ifdef EXTRA_DUMP
  494. if (extra != NULL) {
  495. logprintf(" errno: %d\n", EXTRA_ERRNO(extra));
  496. logprintf(" detail: %s\n", EXTRA_DUMP(extra));
  497. }
  498. #endif
  499. if (dump_on_failure) {
  500. fprintf(stderr,
  501. " *** forcing core dump so failure can be debugged ***\n");
  502. abort();
  503. }
  504. }
  505. /* Inform user that we're skipping some checks. */
  506. void
  507. test_skipping(const char *fmt, ...)
  508. {
  509. char buff[1024];
  510. va_list ap;
  511. va_start(ap, fmt);
  512. vsnprintf(buff, sizeof(buff), fmt, ap);
  513. va_end(ap);
  514. /* Use failure() message if set. */
  515. msg = nextmsg;
  516. nextmsg = NULL;
  517. /* failure_start() isn't quite right, but is awfully convenient. */
  518. failure_start(skipping_filename, skipping_line, "SKIPPING: %s", buff);
  519. --failures; /* Undo failures++ in failure_start() */
  520. /* Don't failure_finish() here. */
  521. /* Mark as skip, so doesn't count as failed test. */
  522. failed_lines[skipping_line].skip = 1;
  523. ++skips;
  524. }
  525. /*
  526. *
  527. * ASSERTIONS
  528. *
  529. */
  530. /* Generic assert() just displays the failed condition. */
  531. int
  532. assertion_assert(const char *file, int line, int value,
  533. const char *condition, void *extra)
  534. {
  535. assertion_count(file, line);
  536. if (!value) {
  537. failure_start(file, line, "Assertion failed: %s", condition);
  538. failure_finish(extra);
  539. }
  540. return (value);
  541. }
  542. /* chdir() and report any errors */
  543. int
  544. assertion_chdir(const char *file, int line, const char *pathname)
  545. {
  546. assertion_count(file, line);
  547. if (chdir(pathname) == 0)
  548. return (1);
  549. failure_start(file, line, "chdir(\"%s\")", pathname);
  550. failure_finish(NULL);
  551. return (0);
  552. }
  553. /* change file/directory permissions and errors if it fails */
  554. int
  555. assertion_chmod(const char *file, int line, const char *pathname, int mode)
  556. {
  557. assertion_count(file, line);
  558. if (chmod(pathname, mode) == 0)
  559. return (1);
  560. failure_start(file, line, "chmod(\"%s\", %4.o)", pathname, mode);
  561. failure_finish(NULL);
  562. return (0);
  563. }
  564. /* Verify two integers are equal. */
  565. int
  566. assertion_equal_int(const char *file, int line,
  567. long long v1, const char *e1, long long v2, const char *e2, void *extra)
  568. {
  569. assertion_count(file, line);
  570. if (v1 == v2)
  571. return (1);
  572. failure_start(file, line, "%s != %s", e1, e2);
  573. logprintf(" %s=%lld (0x%llx, 0%llo)\n", e1, v1, v1, v1);
  574. logprintf(" %s=%lld (0x%llx, 0%llo)\n", e2, v2, v2, v2);
  575. failure_finish(extra);
  576. return (0);
  577. }
  578. /*
  579. * Utility to convert a single UTF-8 sequence.
  580. */
  581. static int
  582. _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
  583. {
  584. static const char utf8_count[256] = {
  585. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
  586. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
  587. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
  588. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
  589. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
  590. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
  591. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
  592. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
  593. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
  594. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
  595. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
  596. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
  597. 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
  598. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
  599. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
  600. 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
  601. };
  602. int ch;
  603. int cnt;
  604. uint32_t wc;
  605. *pwc = 0;
  606. /* Sanity check. */
  607. if (n == 0)
  608. return (0);
  609. /*
  610. * Decode 1-4 bytes depending on the value of the first byte.
  611. */
  612. ch = (unsigned char)*s;
  613. if (ch == 0)
  614. return (0); /* Standard: return 0 for end-of-string. */
  615. cnt = utf8_count[ch];
  616. /* Invalid sequence or there are not plenty bytes. */
  617. if (n < (size_t)cnt)
  618. return (-1);
  619. /* Make a Unicode code point from a single UTF-8 sequence. */
  620. switch (cnt) {
  621. case 1: /* 1 byte sequence. */
  622. *pwc = ch & 0x7f;
  623. return (cnt);
  624. case 2: /* 2 bytes sequence. */
  625. if ((s[1] & 0xc0) != 0x80) return (-1);
  626. *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
  627. return (cnt);
  628. case 3: /* 3 bytes sequence. */
  629. if ((s[1] & 0xc0) != 0x80) return (-1);
  630. if ((s[2] & 0xc0) != 0x80) return (-1);
  631. wc = ((ch & 0x0f) << 12)
  632. | ((s[1] & 0x3f) << 6)
  633. | (s[2] & 0x3f);
  634. if (wc < 0x800)
  635. return (-1);/* Overlong sequence. */
  636. break;
  637. case 4: /* 4 bytes sequence. */
  638. if (n < 4)
  639. return (-1);
  640. if ((s[1] & 0xc0) != 0x80) return (-1);
  641. if ((s[2] & 0xc0) != 0x80) return (-1);
  642. if ((s[3] & 0xc0) != 0x80) return (-1);
  643. wc = ((ch & 0x07) << 18)
  644. | ((s[1] & 0x3f) << 12)
  645. | ((s[2] & 0x3f) << 6)
  646. | (s[3] & 0x3f);
  647. if (wc < 0x10000)
  648. return (-1);/* Overlong sequence. */
  649. break;
  650. default:
  651. return (-1);
  652. }
  653. /* The code point larger than 0x10FFFF is not legal
  654. * Unicode values. */
  655. if (wc > 0x10FFFF)
  656. return (-1);
  657. /* Correctly gets a Unicode, returns used bytes. */
  658. *pwc = wc;
  659. return (cnt);
  660. }
  661. static void strdump(const char *e, const char *p, int ewidth, int utf8)
  662. {
  663. const char *q = p;
  664. logprintf(" %*s = ", ewidth, e);
  665. if (p == NULL) {
  666. logprintf("NULL\n");
  667. return;
  668. }
  669. logprintf("\"");
  670. while (*p != '\0') {
  671. unsigned int c = 0xff & *p++;
  672. switch (c) {
  673. case '\a': logprintf("\\a"); break;
  674. case '\b': logprintf("\\b"); break;
  675. case '\n': logprintf("\\n"); break;
  676. case '\r': logprintf("\\r"); break;
  677. default:
  678. if (c >= 32 && c < 127)
  679. logprintf("%c", c);
  680. else
  681. logprintf("\\x%02X", c);
  682. }
  683. }
  684. logprintf("\"");
  685. logprintf(" (length %d)", q == NULL ? -1 : (int)strlen(q));
  686. /*
  687. * If the current string is UTF-8, dump its code points.
  688. */
  689. if (utf8) {
  690. size_t len;
  691. uint32_t uc;
  692. int n;
  693. int cnt = 0;
  694. p = q;
  695. len = strlen(p);
  696. logprintf(" [");
  697. while ((n = _utf8_to_unicode(&uc, p, len)) > 0) {
  698. if (p != q)
  699. logprintf(" ");
  700. logprintf("%04X", uc);
  701. p += n;
  702. len -= n;
  703. cnt++;
  704. }
  705. logprintf("]");
  706. logprintf(" (count %d", cnt);
  707. if (n < 0) {
  708. logprintf(",unknown %zu bytes", len);
  709. }
  710. logprintf(")");
  711. }
  712. logprintf("\n");
  713. }
  714. /* Verify two strings are equal, dump them if not. */
  715. int
  716. assertion_equal_string(const char *file, int line,
  717. const char *v1, const char *e1,
  718. const char *v2, const char *e2,
  719. void *extra, int utf8)
  720. {
  721. int l1, l2;
  722. assertion_count(file, line);
  723. if (v1 == v2 || (v1 != NULL && v2 != NULL && strcmp(v1, v2) == 0))
  724. return (1);
  725. failure_start(file, line, "%s != %s", e1, e2);
  726. l1 = (int)strlen(e1);
  727. l2 = (int)strlen(e2);
  728. if (l1 < l2)
  729. l1 = l2;
  730. strdump(e1, v1, l1, utf8);
  731. strdump(e2, v2, l1, utf8);
  732. failure_finish(extra);
  733. return (0);
  734. }
  735. static void
  736. wcsdump(const char *e, const wchar_t *w)
  737. {
  738. logprintf(" %s = ", e);
  739. if (w == NULL) {
  740. logprintf("(null)");
  741. return;
  742. }
  743. logprintf("\"");
  744. while (*w != L'\0') {
  745. unsigned int c = *w++;
  746. if (c >= 32 && c < 127)
  747. logprintf("%c", c);
  748. else if (c < 256)
  749. logprintf("\\x%02X", c);
  750. else if (c < 0x10000)
  751. logprintf("\\u%04X", c);
  752. else
  753. logprintf("\\U%08X", c);
  754. }
  755. logprintf("\"\n");
  756. }
  757. #ifndef HAVE_WCSCMP
  758. static int
  759. wcscmp(const wchar_t *s1, const wchar_t *s2)
  760. {
  761. while (*s1 == *s2++) {
  762. if (*s1++ == L'\0')
  763. return 0;
  764. }
  765. if (*s1 > *--s2)
  766. return 1;
  767. else
  768. return -1;
  769. }
  770. #endif
  771. /* Verify that two wide strings are equal, dump them if not. */
  772. int
  773. assertion_equal_wstring(const char *file, int line,
  774. const wchar_t *v1, const char *e1,
  775. const wchar_t *v2, const char *e2,
  776. void *extra)
  777. {
  778. assertion_count(file, line);
  779. if (v1 == v2)
  780. return (1);
  781. if (v1 != NULL && v2 != NULL && wcscmp(v1, v2) == 0)
  782. return (1);
  783. failure_start(file, line, "%s != %s", e1, e2);
  784. wcsdump(e1, v1);
  785. wcsdump(e2, v2);
  786. failure_finish(extra);
  787. return (0);
  788. }
  789. /*
  790. * Pretty standard hexdump routine. As a bonus, if ref != NULL, then
  791. * any bytes in p that differ from ref will be highlighted with '_'
  792. * before and after the hex value.
  793. */
  794. static void
  795. hexdump(const char *p, const char *ref, size_t l, size_t offset)
  796. {
  797. size_t i, j;
  798. char sep;
  799. if (p == NULL) {
  800. logprintf("(null)\n");
  801. return;
  802. }
  803. for(i=0; i < l; i+=16) {
  804. logprintf("%04x", (unsigned)(i + offset));
  805. sep = ' ';
  806. for (j = 0; j < 16 && i + j < l; j++) {
  807. if (ref != NULL && p[i + j] != ref[i + j])
  808. sep = '_';
  809. logprintf("%c%02x", sep, 0xff & (int)p[i+j]);
  810. if (ref != NULL && p[i + j] == ref[i + j])
  811. sep = ' ';
  812. }
  813. for (; j < 16; j++) {
  814. logprintf("%c ", sep);
  815. sep = ' ';
  816. }
  817. logprintf("%c", sep);
  818. for (j=0; j < 16 && i + j < l; j++) {
  819. int c = p[i + j];
  820. if (c >= ' ' && c <= 126)
  821. logprintf("%c", c);
  822. else
  823. logprintf(".");
  824. }
  825. logprintf("\n");
  826. }
  827. }
  828. /* Verify that two blocks of memory are the same, display the first
  829. * block of differences if they're not. */
  830. int
  831. assertion_equal_mem(const char *file, int line,
  832. const void *_v1, const char *e1,
  833. const void *_v2, const char *e2,
  834. size_t l, const char *ld, void *extra)
  835. {
  836. const char *v1 = (const char *)_v1;
  837. const char *v2 = (const char *)_v2;
  838. size_t offset;
  839. assertion_count(file, line);
  840. if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
  841. return (1);
  842. if (v1 == NULL || v2 == NULL)
  843. return (0);
  844. failure_start(file, line, "%s != %s", e1, e2);
  845. logprintf(" size %s = %d\n", ld, (int)l);
  846. /* Dump 48 bytes (3 lines) so that the first difference is
  847. * in the second line. */
  848. offset = 0;
  849. while (l > 64 && memcmp(v1, v2, 32) == 0) {
  850. /* Two lines agree, so step forward one line. */
  851. v1 += 16;
  852. v2 += 16;
  853. l -= 16;
  854. offset += 16;
  855. }
  856. logprintf(" Dump of %s\n", e1);
  857. hexdump(v1, v2, l < 128 ? l : 128, offset);
  858. logprintf(" Dump of %s\n", e2);
  859. hexdump(v2, v1, l < 128 ? l : 128, offset);
  860. logprintf("\n");
  861. failure_finish(extra);
  862. return (0);
  863. }
  864. /* Verify that a block of memory is filled with the specified byte. */
  865. int
  866. assertion_memory_filled_with(const char *file, int line,
  867. const void *_v1, const char *vd,
  868. size_t l, const char *ld,
  869. char b, const char *bd, void *extra)
  870. {
  871. const char *v1 = (const char *)_v1;
  872. size_t c = 0;
  873. size_t i;
  874. (void)ld; /* UNUSED */
  875. assertion_count(file, line);
  876. for (i = 0; i < l; ++i) {
  877. if (v1[i] == b) {
  878. ++c;
  879. }
  880. }
  881. if (c == l)
  882. return (1);
  883. failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
  884. logprintf(" Only %d bytes were correct\n", (int)c);
  885. failure_finish(extra);
  886. return (0);
  887. }
  888. /* Verify that the named file exists and is empty. */
  889. int
  890. assertion_empty_file(const char *filename, int line, const char *f1)
  891. {
  892. char buff[1024];
  893. struct stat st;
  894. ssize_t s;
  895. FILE *f;
  896. assertion_count(filename, line);
  897. if (stat(f1, &st) != 0) {
  898. failure_start(filename, line, "Stat failed: %s", f1);
  899. failure_finish(NULL);
  900. return (0);
  901. }
  902. if (st.st_size == 0)
  903. return (1);
  904. failure_start(filename, line, "File should be empty: %s", f1);
  905. logprintf(" File size: %d\n", (int)st.st_size);
  906. logprintf(" Contents:\n");
  907. f = fopen(f1, "rb");
  908. if (f == NULL) {
  909. logprintf(" Unable to open %s\n", f1);
  910. } else {
  911. s = ((off_t)sizeof(buff) < st.st_size) ?
  912. (ssize_t)sizeof(buff) : (ssize_t)st.st_size;
  913. s = fread(buff, 1, s, f);
  914. hexdump(buff, NULL, s, 0);
  915. fclose(f);
  916. }
  917. failure_finish(NULL);
  918. return (0);
  919. }
  920. /* Verify that the named file exists and is not empty. */
  921. int
  922. assertion_non_empty_file(const char *filename, int line, const char *f1)
  923. {
  924. struct stat st;
  925. assertion_count(filename, line);
  926. if (stat(f1, &st) != 0) {
  927. failure_start(filename, line, "Stat failed: %s", f1);
  928. failure_finish(NULL);
  929. return (0);
  930. }
  931. if (st.st_size == 0) {
  932. failure_start(filename, line, "File empty: %s", f1);
  933. failure_finish(NULL);
  934. return (0);
  935. }
  936. return (1);
  937. }
  938. /* Verify that two files have the same contents. */
  939. /* TODO: hexdump the first bytes that actually differ. */
  940. int
  941. assertion_equal_file(const char *filename, int line, const char *fn1, const char *fn2)
  942. {
  943. char buff1[1024];
  944. char buff2[1024];
  945. FILE *f1, *f2;
  946. int n1, n2;
  947. assertion_count(filename, line);
  948. f1 = fopen(fn1, "rb");
  949. f2 = fopen(fn2, "rb");
  950. if (f1 == NULL || f2 == NULL) {
  951. if (f1) fclose(f1);
  952. if (f2) fclose(f2);
  953. return (0);
  954. }
  955. for (;;) {
  956. n1 = (int)fread(buff1, 1, sizeof(buff1), f1);
  957. n2 = (int)fread(buff2, 1, sizeof(buff2), f2);
  958. if (n1 != n2)
  959. break;
  960. if (n1 == 0 && n2 == 0) {
  961. fclose(f1);
  962. fclose(f2);
  963. return (1);
  964. }
  965. if (memcmp(buff1, buff2, n1) != 0)
  966. break;
  967. }
  968. fclose(f1);
  969. fclose(f2);
  970. failure_start(filename, line, "Files not identical");
  971. logprintf(" file1=\"%s\"\n", fn1);
  972. logprintf(" file2=\"%s\"\n", fn2);
  973. failure_finish(NULL);
  974. return (0);
  975. }
  976. /* Verify that the named file does exist. */
  977. int
  978. assertion_file_exists(const char *filename, int line, const char *f)
  979. {
  980. assertion_count(filename, line);
  981. #if defined(_WIN32) && !defined(__CYGWIN__)
  982. if (!_access(f, 0))
  983. return (1);
  984. #else
  985. if (!access(f, F_OK))
  986. return (1);
  987. #endif
  988. failure_start(filename, line, "File should exist: %s", f);
  989. failure_finish(NULL);
  990. return (0);
  991. }
  992. /* Verify that the named file doesn't exist. */
  993. int
  994. assertion_file_not_exists(const char *filename, int line, const char *f)
  995. {
  996. assertion_count(filename, line);
  997. #if defined(_WIN32) && !defined(__CYGWIN__)
  998. if (_access(f, 0))
  999. return (1);
  1000. #else
  1001. if (access(f, F_OK))
  1002. return (1);
  1003. #endif
  1004. failure_start(filename, line, "File should not exist: %s", f);
  1005. failure_finish(NULL);
  1006. return (0);
  1007. }
  1008. /* Compare the contents of a file to a block of memory. */
  1009. int
  1010. assertion_file_contents(const char *filename, int line, const void *buff, int s, const char *fn)
  1011. {
  1012. char *contents;
  1013. FILE *f;
  1014. int n;
  1015. assertion_count(filename, line);
  1016. f = fopen(fn, "rb");
  1017. if (f == NULL) {
  1018. failure_start(filename, line,
  1019. "File should exist: %s", fn);
  1020. failure_finish(NULL);
  1021. return (0);
  1022. }
  1023. contents = malloc(s * 2);
  1024. n = (int)fread(contents, 1, s * 2, f);
  1025. fclose(f);
  1026. if (n == s && memcmp(buff, contents, s) == 0) {
  1027. free(contents);
  1028. return (1);
  1029. }
  1030. failure_start(filename, line, "File contents don't match");
  1031. logprintf(" file=\"%s\"\n", fn);
  1032. if (n > 0)
  1033. hexdump(contents, buff, n > 512 ? 512 : n, 0);
  1034. else {
  1035. logprintf(" File empty, contents should be:\n");
  1036. hexdump(buff, NULL, s > 512 ? 512 : s, 0);
  1037. }
  1038. failure_finish(NULL);
  1039. free(contents);
  1040. return (0);
  1041. }
  1042. /* Check the contents of a text file, being tolerant of line endings. */
  1043. int
  1044. assertion_text_file_contents(const char *filename, int line, const char *buff, const char *fn)
  1045. {
  1046. char *contents;
  1047. const char *btxt, *ftxt;
  1048. FILE *f;
  1049. int n, s;
  1050. assertion_count(filename, line);
  1051. f = fopen(fn, "r");
  1052. if (f == NULL) {
  1053. failure_start(filename, line,
  1054. "File doesn't exist: %s", fn);
  1055. failure_finish(NULL);
  1056. return (0);
  1057. }
  1058. s = (int)strlen(buff);
  1059. contents = malloc(s * 2 + 128);
  1060. n = (int)fread(contents, 1, s * 2 + 128 - 1, f);
  1061. if (n >= 0)
  1062. contents[n] = '\0';
  1063. fclose(f);
  1064. /* Compare texts. */
  1065. btxt = buff;
  1066. ftxt = (const char *)contents;
  1067. while (*btxt != '\0' && *ftxt != '\0') {
  1068. if (*btxt == *ftxt) {
  1069. ++btxt;
  1070. ++ftxt;
  1071. continue;
  1072. }
  1073. if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
  1074. /* Pass over different new line characters. */
  1075. ++btxt;
  1076. ftxt += 2;
  1077. continue;
  1078. }
  1079. break;
  1080. }
  1081. if (*btxt == '\0' && *ftxt == '\0') {
  1082. free(contents);
  1083. return (1);
  1084. }
  1085. failure_start(filename, line, "Contents don't match");
  1086. logprintf(" file=\"%s\"\n", fn);
  1087. if (n > 0) {
  1088. hexdump(contents, buff, n, 0);
  1089. logprintf(" expected\n");
  1090. hexdump(buff, contents, s, 0);
  1091. } else {
  1092. logprintf(" File empty, contents should be:\n");
  1093. hexdump(buff, NULL, s, 0);
  1094. }
  1095. failure_finish(NULL);
  1096. free(contents);
  1097. return (0);
  1098. }
  1099. /* Verify that a text file contains the specified lines, regardless of order */
  1100. /* This could be more efficient if we sorted both sets of lines, etc, but
  1101. * since this is used only for testing and only ever deals with a dozen or so
  1102. * lines at a time, this relatively crude approach is just fine. */
  1103. int
  1104. assertion_file_contains_lines_any_order(const char *file, int line,
  1105. const char *pathname, const char *lines[])
  1106. {
  1107. char *buff;
  1108. size_t buff_size;
  1109. size_t expected_count, actual_count, i, j;
  1110. char **expected = NULL;
  1111. char *p, **actual = NULL;
  1112. char c;
  1113. int expected_failure = 0, actual_failure = 0;
  1114. assertion_count(file, line);
  1115. buff = slurpfile(&buff_size, "%s", pathname);
  1116. if (buff == NULL) {
  1117. failure_start(pathname, line, "Can't read file: %s", pathname);
  1118. failure_finish(NULL);
  1119. return (0);
  1120. }
  1121. /* Make a copy of the provided lines and count up the expected
  1122. * file size. */
  1123. for (i = 0; lines[i] != NULL; ++i) {
  1124. }
  1125. expected_count = i;
  1126. if (expected_count) {
  1127. expected = malloc(sizeof(char *) * expected_count);
  1128. if (expected == NULL) {
  1129. failure_start(pathname, line, "Can't allocate memory");
  1130. failure_finish(NULL);
  1131. free(expected);
  1132. free(buff);
  1133. return (0);
  1134. }
  1135. for (i = 0; lines[i] != NULL; ++i) {
  1136. expected[i] = strdup(lines[i]);
  1137. }
  1138. }
  1139. /* Break the file into lines */
  1140. actual_count = 0;
  1141. for (c = '\0', p = buff; p < buff + buff_size; ++p) {
  1142. if (*p == '\x0d' || *p == '\x0a')
  1143. *p = '\0';
  1144. if (c == '\0' && *p != '\0')
  1145. ++actual_count;
  1146. c = *p;
  1147. }
  1148. if (actual_count) {
  1149. actual = calloc(sizeof(char *), actual_count);
  1150. if (actual == NULL) {
  1151. failure_start(pathname, line, "Can't allocate memory");
  1152. failure_finish(NULL);
  1153. free(expected);
  1154. free(buff);
  1155. return (0);
  1156. }
  1157. for (j = 0, p = buff; p < buff + buff_size;
  1158. p += 1 + strlen(p)) {
  1159. if (*p != '\0') {
  1160. actual[j] = p;
  1161. ++j;
  1162. }
  1163. }
  1164. }
  1165. /* Erase matching lines from both lists */
  1166. for (i = 0; i < expected_count; ++i) {
  1167. if (expected[i] == NULL)
  1168. continue;
  1169. for (j = 0; j < actual_count; ++j) {
  1170. if (actual[j] == NULL)
  1171. continue;
  1172. if (strcmp(expected[i], actual[j]) == 0) {
  1173. free(expected[i]);
  1174. expected[i] = NULL;
  1175. actual[j] = NULL;
  1176. break;
  1177. }
  1178. }
  1179. }
  1180. /* If there's anything left, it's a failure */
  1181. for (i = 0; i < expected_count; ++i) {
  1182. if (expected[i] != NULL)
  1183. ++expected_failure;
  1184. }
  1185. for (j = 0; j < actual_count; ++j) {
  1186. if (actual[j] != NULL)
  1187. ++actual_failure;
  1188. }
  1189. if (expected_failure == 0 && actual_failure == 0) {
  1190. free(buff);
  1191. free(expected);
  1192. free(actual);
  1193. return (1);
  1194. }
  1195. failure_start(file, line, "File doesn't match: %s", pathname);
  1196. for (i = 0; i < expected_count; ++i) {
  1197. if (expected[i] != NULL) {
  1198. logprintf(" Expected but not present: %s\n", expected[i]);
  1199. free(expected[i]);
  1200. }
  1201. }
  1202. for (j = 0; j < actual_count; ++j) {
  1203. if (actual[j] != NULL)
  1204. logprintf(" Present but not expected: %s\n", actual[j]);
  1205. }
  1206. failure_finish(NULL);
  1207. free(buff);
  1208. free(expected);
  1209. free(actual);
  1210. return (0);
  1211. }
  1212. /* Verify that a text file does not contains the specified strings */
  1213. int
  1214. assertion_file_contains_no_invalid_strings(const char *file, int line,
  1215. const char *pathname, const char *strings[])
  1216. {
  1217. char *buff;
  1218. int i;
  1219. buff = slurpfile(NULL, "%s", pathname);
  1220. if (buff == NULL) {
  1221. failure_start(file, line, "Can't read file: %s", pathname);
  1222. failure_finish(NULL);
  1223. return (0);
  1224. }
  1225. for (i = 0; strings[i] != NULL; ++i) {
  1226. if (strstr(buff, strings[i]) != NULL) {
  1227. failure_start(file, line, "Invalid string in %s: %s", pathname,
  1228. strings[i]);
  1229. failure_finish(NULL);
  1230. free(buff);
  1231. return(0);
  1232. }
  1233. }
  1234. free(buff);
  1235. return (0);
  1236. }
  1237. /* Test that two paths point to the same file. */
  1238. /* As a side-effect, asserts that both files exist. */
  1239. static int
  1240. is_hardlink(const char *file, int line,
  1241. const char *path1, const char *path2)
  1242. {
  1243. #if defined(_WIN32) && !defined(__CYGWIN__)
  1244. BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
  1245. int r;
  1246. assertion_count(file, line);
  1247. r = my_GetFileInformationByName(path1, &bhfi1);
  1248. if (r == 0) {
  1249. failure_start(file, line, "File %s can't be inspected?", path1);
  1250. failure_finish(NULL);
  1251. return (0);
  1252. }
  1253. r = my_GetFileInformationByName(path2, &bhfi2);
  1254. if (r == 0) {
  1255. failure_start(file, line, "File %s can't be inspected?", path2);
  1256. failure_finish(NULL);
  1257. return (0);
  1258. }
  1259. return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
  1260. && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
  1261. && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow);
  1262. #else
  1263. struct stat st1, st2;
  1264. int r;
  1265. assertion_count(file, line);
  1266. r = lstat(path1, &st1);
  1267. if (r != 0) {
  1268. failure_start(file, line, "File should exist: %s", path1);
  1269. failure_finish(NULL);
  1270. return (0);
  1271. }
  1272. r = lstat(path2, &st2);
  1273. if (r != 0) {
  1274. failure_start(file, line, "File should exist: %s", path2);
  1275. failure_finish(NULL);
  1276. return (0);
  1277. }
  1278. return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
  1279. #endif
  1280. }
  1281. int
  1282. assertion_is_hardlink(const char *file, int line,
  1283. const char *path1, const char *path2)
  1284. {
  1285. if (is_hardlink(file, line, path1, path2))
  1286. return (1);
  1287. failure_start(file, line,
  1288. "Files %s and %s are not hardlinked", path1, path2);
  1289. failure_finish(NULL);
  1290. return (0);
  1291. }
  1292. int
  1293. assertion_is_not_hardlink(const char *file, int line,
  1294. const char *path1, const char *path2)
  1295. {
  1296. if (!is_hardlink(file, line, path1, path2))
  1297. return (1);
  1298. failure_start(file, line,
  1299. "Files %s and %s should not be hardlinked", path1, path2);
  1300. failure_finish(NULL);
  1301. return (0);
  1302. }
  1303. /* Verify a/b/mtime of 'pathname'. */
  1304. /* If 'recent', verify that it's within last 10 seconds. */
  1305. static int
  1306. assertion_file_time(const char *file, int line,
  1307. const char *pathname, long t, long nsec, char type, int recent)
  1308. {
  1309. long long filet, filet_nsec;
  1310. int r;
  1311. #if defined(_WIN32) && !defined(__CYGWIN__)
  1312. #define EPOC_TIME (116444736000000000ULL)
  1313. FILETIME fxtime, fbirthtime, fatime, fmtime;
  1314. ULARGE_INTEGER wintm;
  1315. HANDLE h;
  1316. fxtime.dwLowDateTime = 0;
  1317. fxtime.dwHighDateTime = 0;
  1318. assertion_count(file, line);
  1319. /* Note: FILE_FLAG_BACKUP_SEMANTICS applies to open
  1320. * a directory file. If not, CreateFile() will fail when
  1321. * the pathname is a directory. */
  1322. h = CreateFileA(pathname, FILE_READ_ATTRIBUTES, 0, NULL,
  1323. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  1324. if (h == INVALID_HANDLE_VALUE) {
  1325. failure_start(file, line, "Can't access %s\n", pathname);
  1326. failure_finish(NULL);
  1327. return (0);
  1328. }
  1329. r = GetFileTime(h, &fbirthtime, &fatime, &fmtime);
  1330. switch (type) {
  1331. case 'a': fxtime = fatime; break;
  1332. case 'b': fxtime = fbirthtime; break;
  1333. case 'm': fxtime = fmtime; break;
  1334. }
  1335. CloseHandle(h);
  1336. if (r == 0) {
  1337. failure_start(file, line, "Can't GetFileTime %s\n", pathname);
  1338. failure_finish(NULL);
  1339. return (0);
  1340. }
  1341. wintm.LowPart = fxtime.dwLowDateTime;
  1342. wintm.HighPart = fxtime.dwHighDateTime;
  1343. filet = (wintm.QuadPart - EPOC_TIME) / 10000000;
  1344. filet_nsec = ((wintm.QuadPart - EPOC_TIME) % 10000000) * 100;
  1345. nsec = (nsec / 100) * 100; /* Round the request */
  1346. #else
  1347. struct stat st;
  1348. assertion_count(file, line);
  1349. r = lstat(pathname, &st);
  1350. if (r != 0) {
  1351. failure_start(file, line, "Can't stat %s\n", pathname);
  1352. failure_finish(NULL);
  1353. return (0);
  1354. }
  1355. switch (type) {
  1356. case 'a': filet = st.st_atime; break;
  1357. case 'm': filet = st.st_mtime; break;
  1358. case 'b': filet = 0; break;
  1359. default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
  1360. exit(1);
  1361. }
  1362. #if defined(__FreeBSD__)
  1363. switch (type) {
  1364. case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
  1365. case 'b': filet = st.st_birthtime;
  1366. /* FreeBSD filesystems that don't support birthtime
  1367. * (e.g., UFS1) always return -1 here. */
  1368. if (filet == -1) {
  1369. return (1);
  1370. }
  1371. filet_nsec = st.st_birthtimespec.tv_nsec; break;
  1372. case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
  1373. default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
  1374. exit(1);
  1375. }
  1376. /* FreeBSD generally only stores to microsecond res, so round. */
  1377. filet_nsec = (filet_nsec / 1000) * 1000;
  1378. nsec = (nsec / 1000) * 1000;
  1379. #else
  1380. filet_nsec = nsec = 0; /* Generic POSIX only has whole seconds. */
  1381. if (type == 'b') return (1); /* Generic POSIX doesn't have birthtime */
  1382. #if defined(__HAIKU__)
  1383. if (type == 'a') return (1); /* Haiku doesn't have atime. */
  1384. #endif
  1385. #endif
  1386. #endif
  1387. if (recent) {
  1388. /* Check that requested time is up-to-date. */
  1389. time_t now = time(NULL);
  1390. if (filet < now - 10 || filet > now + 1) {
  1391. failure_start(file, line,
  1392. "File %s has %ctime %lld, %lld seconds ago\n",
  1393. pathname, type, filet, now - filet);
  1394. failure_finish(NULL);
  1395. return (0);
  1396. }
  1397. } else if (filet != t || filet_nsec != nsec) {
  1398. failure_start(file, line,
  1399. "File %s has %ctime %lld.%09lld, expected %ld.%09ld",
  1400. pathname, type, filet, filet_nsec, t, nsec);
  1401. failure_finish(NULL);
  1402. return (0);
  1403. }
  1404. return (1);
  1405. }
  1406. /* Verify atime of 'pathname'. */
  1407. int
  1408. assertion_file_atime(const char *file, int line,
  1409. const char *pathname, long t, long nsec)
  1410. {
  1411. return assertion_file_time(file, line, pathname, t, nsec, 'a', 0);
  1412. }
  1413. /* Verify atime of 'pathname' is up-to-date. */
  1414. int
  1415. assertion_file_atime_recent(const char *file, int line, const char *pathname)
  1416. {
  1417. return assertion_file_time(file, line, pathname, 0, 0, 'a', 1);
  1418. }
  1419. /* Verify birthtime of 'pathname'. */
  1420. int
  1421. assertion_file_birthtime(const char *file, int line,
  1422. const char *pathname, long t, long nsec)
  1423. {
  1424. return assertion_file_time(file, line, pathname, t, nsec, 'b', 0);
  1425. }
  1426. /* Verify birthtime of 'pathname' is up-to-date. */
  1427. int
  1428. assertion_file_birthtime_recent(const char *file, int line,
  1429. const char *pathname)
  1430. {
  1431. return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
  1432. }
  1433. /* Verify mode of 'pathname'. */
  1434. int
  1435. assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
  1436. {
  1437. int mode;
  1438. int r;
  1439. assertion_count(file, line);
  1440. #if defined(_WIN32) && !defined(__CYGWIN__)
  1441. failure_start(file, line, "assertFileMode not yet implemented for Windows");
  1442. (void)mode; /* UNUSED */
  1443. (void)r; /* UNUSED */
  1444. (void)pathname; /* UNUSED */
  1445. (void)expected_mode; /* UNUSED */
  1446. #else
  1447. {
  1448. struct stat st;
  1449. r = lstat(pathname, &st);
  1450. mode = (int)(st.st_mode & 0777);
  1451. }
  1452. if (r == 0 && mode == expected_mode)
  1453. return (1);
  1454. failure_start(file, line, "File %s has mode %o, expected %o",
  1455. pathname, mode, expected_mode);
  1456. #endif
  1457. failure_finish(NULL);
  1458. return (0);
  1459. }
  1460. /* Verify mtime of 'pathname'. */
  1461. int
  1462. assertion_file_mtime(const char *file, int line,
  1463. const char *pathname, long t, long nsec)
  1464. {
  1465. return assertion_file_time(file, line, pathname, t, nsec, 'm', 0);
  1466. }
  1467. /* Verify mtime of 'pathname' is up-to-date. */
  1468. int
  1469. assertion_file_mtime_recent(const char *file, int line, const char *pathname)
  1470. {
  1471. return assertion_file_time(file, line, pathname, 0, 0, 'm', 1);
  1472. }
  1473. /* Verify number of links to 'pathname'. */
  1474. int
  1475. assertion_file_nlinks(const char *file, int line,
  1476. const char *pathname, int nlinks)
  1477. {
  1478. #if defined(_WIN32) && !defined(__CYGWIN__)
  1479. BY_HANDLE_FILE_INFORMATION bhfi;
  1480. int r;
  1481. assertion_count(file, line);
  1482. r = my_GetFileInformationByName(pathname, &bhfi);
  1483. if (r != 0 && bhfi.nNumberOfLinks == (DWORD)nlinks)
  1484. return (1);
  1485. failure_start(file, line, "File %s has %jd links, expected %d",
  1486. pathname, (intmax_t)bhfi.nNumberOfLinks, nlinks);
  1487. failure_finish(NULL);
  1488. return (0);
  1489. #else
  1490. struct stat st;
  1491. int r;
  1492. assertion_count(file, line);
  1493. r = lstat(pathname, &st);
  1494. if (r == 0 && (int)st.st_nlink == nlinks)
  1495. return (1);
  1496. failure_start(file, line, "File %s has %jd links, expected %d",
  1497. pathname, (intmax_t)st.st_nlink, nlinks);
  1498. failure_finish(NULL);
  1499. return (0);
  1500. #endif
  1501. }
  1502. /* Verify size of 'pathname'. */
  1503. int
  1504. assertion_file_size(const char *file, int line, const char *pathname, long size)
  1505. {
  1506. int64_t filesize;
  1507. int r;
  1508. assertion_count(file, line);
  1509. #if defined(_WIN32) && !defined(__CYGWIN__)
  1510. {
  1511. BY_HANDLE_FILE_INFORMATION bhfi;
  1512. r = !my_GetFileInformationByName(pathname, &bhfi);
  1513. filesize = ((int64_t)bhfi.nFileSizeHigh << 32) + bhfi.nFileSizeLow;
  1514. }
  1515. #else
  1516. {
  1517. struct stat st;
  1518. r = lstat(pathname, &st);
  1519. filesize = st.st_size;
  1520. }
  1521. #endif
  1522. if (r == 0 && filesize == size)
  1523. return (1);
  1524. failure_start(file, line, "File %s has size %ld, expected %ld",
  1525. pathname, (long)filesize, (long)size);
  1526. failure_finish(NULL);
  1527. return (0);
  1528. }
  1529. /* Assert that 'pathname' is a dir. If mode >= 0, verify that too. */
  1530. int
  1531. assertion_is_dir(const char *file, int line, const char *pathname, int mode)
  1532. {
  1533. struct stat st;
  1534. int r;
  1535. #if defined(_WIN32) && !defined(__CYGWIN__)
  1536. (void)mode; /* UNUSED */
  1537. #endif
  1538. assertion_count(file, line);
  1539. r = lstat(pathname, &st);
  1540. if (r != 0) {
  1541. failure_start(file, line, "Dir should exist: %s", pathname);
  1542. failure_finish(NULL);
  1543. return (0);
  1544. }
  1545. if (!S_ISDIR(st.st_mode)) {
  1546. failure_start(file, line, "%s is not a dir", pathname);
  1547. failure_finish(NULL);
  1548. return (0);
  1549. }
  1550. #if !defined(_WIN32) || defined(__CYGWIN__)
  1551. /* Windows doesn't handle permissions the same way as POSIX,
  1552. * so just ignore the mode tests. */
  1553. /* TODO: Can we do better here? */
  1554. if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
  1555. failure_start(file, line, "Dir %s has wrong mode", pathname);
  1556. logprintf(" Expected: 0%3o\n", mode);
  1557. logprintf(" Found: 0%3o\n", st.st_mode & 07777);
  1558. failure_finish(NULL);
  1559. return (0);
  1560. }
  1561. #endif
  1562. return (1);
  1563. }
  1564. /* Verify that 'pathname' is a regular file. If 'mode' is >= 0,
  1565. * verify that too. */
  1566. int
  1567. assertion_is_reg(const char *file, int line, const char *pathname, int mode)
  1568. {
  1569. struct stat st;
  1570. int r;
  1571. #if defined(_WIN32) && !defined(__CYGWIN__)
  1572. (void)mode; /* UNUSED */
  1573. #endif
  1574. assertion_count(file, line);
  1575. r = lstat(pathname, &st);
  1576. if (r != 0 || !S_ISREG(st.st_mode)) {
  1577. failure_start(file, line, "File should exist: %s", pathname);
  1578. failure_finish(NULL);
  1579. return (0);
  1580. }
  1581. #if !defined(_WIN32) || defined(__CYGWIN__)
  1582. /* Windows doesn't handle permissions the same way as POSIX,
  1583. * so just ignore the mode tests. */
  1584. /* TODO: Can we do better here? */
  1585. if (mode >= 0 && (mode_t)mode != (st.st_mode & 07777)) {
  1586. failure_start(file, line, "File %s has wrong mode", pathname);
  1587. logprintf(" Expected: 0%3o\n", mode);
  1588. logprintf(" Found: 0%3o\n", st.st_mode & 07777);
  1589. failure_finish(NULL);
  1590. return (0);
  1591. }
  1592. #endif
  1593. return (1);
  1594. }
  1595. /*
  1596. * Check whether 'pathname' is a symbolic link. If 'contents' is
  1597. * non-NULL, verify that the symlink has those contents.
  1598. *
  1599. * On platforms with directory symlinks, set isdir to 0 to test for a file
  1600. * symlink and to 1 to test for a directory symlink. On other platforms
  1601. * the variable is ignored.
  1602. */
  1603. static int
  1604. is_symlink(const char *file, int line,
  1605. const char *pathname, const char *contents, int isdir)
  1606. {
  1607. #if defined(_WIN32) && !defined(__CYGWIN__)
  1608. HANDLE h;
  1609. DWORD inbytes;
  1610. REPARSE_DATA_BUFFER *buf;
  1611. BY_HANDLE_FILE_INFORMATION st;
  1612. size_t len, len2;
  1613. wchar_t *linknamew, *contentsw;
  1614. const char *p;
  1615. char *s, *pn;
  1616. int ret = 0;
  1617. BYTE *indata;
  1618. const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
  1619. FILE_FLAG_OPEN_REPARSE_POINT;
  1620. /* Replace slashes with backslashes in pathname */
  1621. pn = malloc((strlen(pathname) + 1) * sizeof(char));
  1622. p = pathname;
  1623. s = pn;
  1624. while(*p != '\0') {
  1625. if(*p == '/')
  1626. *s = '\\';
  1627. else
  1628. *s = *p;
  1629. p++;
  1630. s++;
  1631. }
  1632. *s = '\0';
  1633. h = CreateFileA(pn, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
  1634. flag, NULL);
  1635. free(pn);
  1636. if (h == INVALID_HANDLE_VALUE) {
  1637. failure_start(file, line, "Can't access %s\n", pathname);
  1638. failure_finish(NULL);
  1639. return (0);
  1640. }
  1641. ret = GetFileInformationByHandle(h, &st);
  1642. if (ret == 0) {
  1643. failure_start(file, line,
  1644. "Can't stat: %s", pathname);
  1645. failure_finish(NULL);
  1646. } else if ((st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
  1647. failure_start(file, line,
  1648. "Not a symlink: %s", pathname);
  1649. failure_finish(NULL);
  1650. ret = 0;
  1651. }
  1652. if (isdir && ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
  1653. failure_start(file, line,
  1654. "Not a directory symlink: %s", pathname);
  1655. failure_finish(NULL);
  1656. ret = 0;
  1657. }
  1658. if (!isdir &&
  1659. ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) {
  1660. failure_start(file, line,
  1661. "Not a file symlink: %s", pathname);
  1662. failure_finish(NULL);
  1663. ret = 0;
  1664. }
  1665. if (ret == 0) {
  1666. CloseHandle(h);
  1667. return (0);
  1668. }
  1669. indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
  1670. ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
  1671. 1024, &inbytes, NULL);
  1672. CloseHandle(h);
  1673. if (ret == 0) {
  1674. free(indata);
  1675. failure_start(file, line,
  1676. "Could not retrieve symlink target: %s", pathname);
  1677. failure_finish(NULL);
  1678. return (0);
  1679. }
  1680. buf = (REPARSE_DATA_BUFFER *) indata;
  1681. if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
  1682. free(indata);
  1683. /* File is not a symbolic link */
  1684. failure_start(file, line,
  1685. "Not a symlink: %s", pathname);
  1686. failure_finish(NULL);
  1687. return (0);
  1688. }
  1689. if (contents == NULL) {
  1690. free(indata);
  1691. return (1);
  1692. }
  1693. len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
  1694. linknamew = malloc(len + sizeof(wchar_t));
  1695. if (linknamew == NULL) {
  1696. free(indata);
  1697. return (0);
  1698. }
  1699. memcpy(linknamew, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
  1700. [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
  1701. free(indata);
  1702. linknamew[len / sizeof(wchar_t)] = L'\0';
  1703. contentsw = malloc(len + sizeof(wchar_t));
  1704. if (contentsw == NULL) {
  1705. free(linknamew);
  1706. return (0);
  1707. }
  1708. len2 = mbsrtowcs(contentsw, &contents, (len + sizeof(wchar_t)
  1709. / sizeof(wchar_t)), NULL);
  1710. if (len2 > 0 && wcscmp(linknamew, contentsw) != 0)
  1711. ret = 1;
  1712. free(linknamew);
  1713. free(contentsw);
  1714. return (ret);
  1715. #else
  1716. char buff[300];
  1717. struct stat st;
  1718. ssize_t linklen;
  1719. int r;
  1720. (void)isdir; /* UNUSED */
  1721. assertion_count(file, line);
  1722. r = lstat(pathname, &st);
  1723. if (r != 0) {
  1724. failure_start(file, line,
  1725. "Symlink should exist: %s", pathname);
  1726. failure_finish(NULL);
  1727. return (0);
  1728. }
  1729. if (!S_ISLNK(st.st_mode))
  1730. return (0);
  1731. if (contents == NULL)
  1732. return (1);
  1733. linklen = readlink(pathname, buff, sizeof(buff) - 1);
  1734. if (linklen < 0) {
  1735. failure_start(file, line, "Can't read symlink %s", pathname);
  1736. failure_finish(NULL);
  1737. return (0);
  1738. }
  1739. buff[linklen] = '\0';
  1740. if (strcmp(buff, contents) != 0)
  1741. return (0);
  1742. return (1);
  1743. #endif
  1744. }
  1745. /* Assert that path is a symlink that (optionally) contains contents. */
  1746. int
  1747. assertion_is_symlink(const char *file, int line,
  1748. const char *path, const char *contents, int isdir)
  1749. {
  1750. if (is_symlink(file, line, path, contents, isdir))
  1751. return (1);
  1752. if (contents)
  1753. failure_start(file, line, "File %s is not a symlink to %s",
  1754. path, contents);
  1755. else
  1756. failure_start(file, line, "File %s is not a symlink", path);
  1757. failure_finish(NULL);
  1758. return (0);
  1759. }
  1760. /* Create a directory and report any errors. */
  1761. int
  1762. assertion_make_dir(const char *file, int line, const char *dirname, int mode)
  1763. {
  1764. assertion_count(file, line);
  1765. #if defined(_WIN32) && !defined(__CYGWIN__)
  1766. (void)mode; /* UNUSED */
  1767. if (0 == _mkdir(dirname))
  1768. return (1);
  1769. #else
  1770. if (0 == mkdir(dirname, mode)) {
  1771. if (0 == chmod(dirname, mode)) {
  1772. assertion_file_mode(file, line, dirname, mode);
  1773. return (1);
  1774. }
  1775. }
  1776. #endif
  1777. failure_start(file, line, "Could not create directory %s", dirname);
  1778. failure_finish(NULL);
  1779. return(0);
  1780. }
  1781. /* Create a file with the specified contents and report any failures. */
  1782. int
  1783. assertion_make_file(const char *file, int line,
  1784. const char *path, int mode, int csize, const void *contents)
  1785. {
  1786. #if defined(_WIN32) && !defined(__CYGWIN__)
  1787. /* TODO: Rework this to set file mode as well. */
  1788. FILE *f;
  1789. (void)mode; /* UNUSED */
  1790. assertion_count(file, line);
  1791. f = fopen(path, "wb");
  1792. if (f == NULL) {
  1793. failure_start(file, line, "Could not create file %s", path);
  1794. failure_finish(NULL);
  1795. return (0);
  1796. }
  1797. if (contents != NULL) {
  1798. size_t wsize;
  1799. if (csize < 0)
  1800. wsize = strlen(contents);
  1801. else
  1802. wsize = (size_t)csize;
  1803. if (wsize != fwrite(contents, 1, wsize, f)) {
  1804. fclose(f);
  1805. failure_start(file, line,
  1806. "Could not write file %s", path);
  1807. failure_finish(NULL);
  1808. return (0);
  1809. }
  1810. }
  1811. fclose(f);
  1812. return (1);
  1813. #else
  1814. int fd;
  1815. assertion_count(file, line);
  1816. fd = open(path, O_CREAT | O_WRONLY, mode >= 0 ? mode : 0644);
  1817. if (fd < 0) {
  1818. failure_start(file, line, "Could not create %s", path);
  1819. failure_finish(NULL);
  1820. return (0);
  1821. }
  1822. #ifdef HAVE_FCHMOD
  1823. if (0 != fchmod(fd, mode))
  1824. #else
  1825. if (0 != chmod(path, mode))
  1826. #endif
  1827. {
  1828. failure_start(file, line, "Could not chmod %s", path);
  1829. failure_finish(NULL);
  1830. close(fd);
  1831. return (0);
  1832. }
  1833. if (contents != NULL) {
  1834. ssize_t wsize;
  1835. if (csize < 0)
  1836. wsize = (ssize_t)strlen(contents);
  1837. else
  1838. wsize = (ssize_t)csize;
  1839. if (wsize != write(fd, contents, wsize)) {
  1840. close(fd);
  1841. failure_start(file, line,
  1842. "Could not write to %s", path);
  1843. failure_finish(NULL);
  1844. close(fd);
  1845. return (0);
  1846. }
  1847. }
  1848. close(fd);
  1849. assertion_file_mode(file, line, path, mode);
  1850. return (1);
  1851. #endif
  1852. }
  1853. /* Create a hardlink and report any failures. */
  1854. int
  1855. assertion_make_hardlink(const char *file, int line,
  1856. const char *newpath, const char *linkto)
  1857. {
  1858. int succeeded;
  1859. assertion_count(file, line);
  1860. #if defined(_WIN32) && !defined(__CYGWIN__)
  1861. succeeded = my_CreateHardLinkA(newpath, linkto);
  1862. #elif HAVE_LINK
  1863. succeeded = !link(linkto, newpath);
  1864. #else
  1865. succeeded = 0;
  1866. #endif
  1867. if (succeeded)
  1868. return (1);
  1869. failure_start(file, line, "Could not create hardlink");
  1870. logprintf(" New link: %s\n", newpath);
  1871. logprintf(" Old name: %s\n", linkto);
  1872. failure_finish(NULL);
  1873. return(0);
  1874. }
  1875. /*
  1876. * Create a symlink and report any failures.
  1877. *
  1878. * Windows symlinks need to know if the target is a directory.
  1879. */
  1880. int
  1881. assertion_make_symlink(const char *file, int line,
  1882. const char *newpath, const char *linkto, int targetIsDir)
  1883. {
  1884. #if defined(_WIN32) && !defined(__CYGWIN__)
  1885. assertion_count(file, line);
  1886. if (my_CreateSymbolicLinkA(newpath, linkto, targetIsDir))
  1887. return (1);
  1888. #elif HAVE_SYMLINK
  1889. (void)targetIsDir; /* UNUSED */
  1890. assertion_count(file, line);
  1891. if (0 == symlink(linkto, newpath))
  1892. return (1);
  1893. #else
  1894. (void)targetIsDir; /* UNUSED */
  1895. #endif
  1896. failure_start(file, line, "Could not create symlink");
  1897. logprintf(" New link: %s\n", newpath);
  1898. logprintf(" Old name: %s\n", linkto);
  1899. failure_finish(NULL);
  1900. return(0);
  1901. }
  1902. /* Set umask, report failures. */
  1903. int
  1904. assertion_umask(const char *file, int line, int mask)
  1905. {
  1906. assertion_count(file, line);
  1907. (void)file; /* UNUSED */
  1908. (void)line; /* UNUSED */
  1909. umask(mask);
  1910. return (1);
  1911. }
  1912. /* Set times, report failures. */
  1913. int
  1914. assertion_utimes(const char *file, int line,
  1915. const char *pathname, long at, long at_nsec, long mt, long mt_nsec)
  1916. {
  1917. int r;
  1918. #if defined(_WIN32) && !defined(__CYGWIN__)
  1919. #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
  1920. + (((nsec)/1000)*10))
  1921. HANDLE h;
  1922. ULARGE_INTEGER wintm;
  1923. FILETIME fatime, fmtime;
  1924. FILETIME *pat, *pmt;
  1925. assertion_count(file, line);
  1926. h = CreateFileA(pathname,GENERIC_READ | GENERIC_WRITE,
  1927. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
  1928. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  1929. if (h == INVALID_HANDLE_VALUE) {
  1930. failure_start(file, line, "Can't access %s\n", pathname);
  1931. failure_finish(NULL);
  1932. return (0);
  1933. }
  1934. if (at > 0 || at_nsec > 0) {
  1935. wintm.QuadPart = WINTIME(at, at_nsec);
  1936. fatime.dwLowDateTime = wintm.LowPart;
  1937. fatime.dwHighDateTime = wintm.HighPart;
  1938. pat = &fatime;
  1939. } else
  1940. pat = NULL;
  1941. if (mt > 0 || mt_nsec > 0) {
  1942. wintm.QuadPart = WINTIME(mt, mt_nsec);
  1943. fmtime.dwLowDateTime = wintm.LowPart;
  1944. fmtime.dwHighDateTime = wintm.HighPart;
  1945. pmt = &fmtime;
  1946. } else
  1947. pmt = NULL;
  1948. if (pat != NULL || pmt != NULL)
  1949. r = SetFileTime(h, NULL, pat, pmt);
  1950. else
  1951. r = 1;
  1952. CloseHandle(h);
  1953. if (r == 0) {
  1954. failure_start(file, line, "Can't SetFileTime %s\n", pathname);
  1955. failure_finish(NULL);
  1956. return (0);
  1957. }
  1958. return (1);
  1959. #else /* defined(_WIN32) && !defined(__CYGWIN__) */
  1960. struct stat st;
  1961. struct timeval times[2];
  1962. #if !defined(__FreeBSD__)
  1963. mt_nsec = at_nsec = 0; /* Generic POSIX only has whole seconds. */
  1964. #endif
  1965. if (mt == 0 && mt_nsec == 0 && at == 0 && at_nsec == 0)
  1966. return (1);
  1967. r = lstat(pathname, &st);
  1968. if (r < 0) {
  1969. failure_start(file, line, "Can't stat %s\n", pathname);
  1970. failure_finish(NULL);
  1971. return (0);
  1972. }
  1973. if (mt == 0 && mt_nsec == 0) {
  1974. mt = st.st_mtime;
  1975. #if defined(__FreeBSD__)
  1976. mt_nsec = st.st_mtimespec.tv_nsec;
  1977. /* FreeBSD generally only stores to microsecond res, so round. */
  1978. mt_nsec = (mt_nsec / 1000) * 1000;
  1979. #endif
  1980. }
  1981. if (at == 0 && at_nsec == 0) {
  1982. at = st.st_atime;
  1983. #if defined(__FreeBSD__)
  1984. at_nsec = st.st_atimespec.tv_nsec;
  1985. /* FreeBSD generally only stores to microsecond res, so round. */
  1986. at_nsec = (at_nsec / 1000) * 1000;
  1987. #endif
  1988. }
  1989. times[1].tv_sec = mt;
  1990. times[1].tv_usec = mt_nsec / 1000;
  1991. times[0].tv_sec = at;
  1992. times[0].tv_usec = at_nsec / 1000;
  1993. #ifdef HAVE_LUTIMES
  1994. r = lutimes(pathname, times);
  1995. #else
  1996. r = utimes(pathname, times);
  1997. #endif
  1998. if (r < 0) {
  1999. failure_start(file, line, "Can't utimes %s\n", pathname);
  2000. failure_finish(NULL);
  2001. return (0);
  2002. }
  2003. return (1);
  2004. #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
  2005. }
  2006. /* Compare file flags */
  2007. int
  2008. assertion_compare_fflags(const char *file, int line, const char *patha,
  2009. const char *pathb, int nomatch)
  2010. {
  2011. #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
  2012. struct stat sa, sb;
  2013. assertion_count(file, line);
  2014. if (stat(patha, &sa) < 0)
  2015. return (0);
  2016. if (stat(pathb, &sb) < 0)
  2017. return (0);
  2018. if (!nomatch && sa.st_flags != sb.st_flags) {
  2019. failure_start(file, line, "File flags should be identical: "
  2020. "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
  2021. sb.st_flags);
  2022. failure_finish(NULL);
  2023. return (0);
  2024. }
  2025. if (nomatch && sa.st_flags == sb.st_flags) {
  2026. failure_start(file, line, "File flags should be different: "
  2027. "%s=%#010x %s=%#010x", patha, sa.st_flags, pathb,
  2028. sb.st_flags);
  2029. failure_finish(NULL);
  2030. return (0);
  2031. }
  2032. #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
  2033. defined(FS_NODUMP_FL)) || \
  2034. (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
  2035. && defined(EXT2_NODUMP_FL))
  2036. int fd, r, flagsa, flagsb;
  2037. assertion_count(file, line);
  2038. fd = open(patha, O_RDONLY | O_NONBLOCK);
  2039. if (fd < 0) {
  2040. failure_start(file, line, "Can't open %s\n", patha);
  2041. failure_finish(NULL);
  2042. return (0);
  2043. }
  2044. r = ioctl(fd,
  2045. #ifdef FS_IOC_GETFLAGS
  2046. FS_IOC_GETFLAGS,
  2047. #else
  2048. EXT2_IOC_GETFLAGS,
  2049. #endif
  2050. &flagsa);
  2051. close(fd);
  2052. if (r < 0) {
  2053. failure_start(file, line, "Can't get flags %s\n", patha);
  2054. failure_finish(NULL);
  2055. return (0);
  2056. }
  2057. fd = open(pathb, O_RDONLY | O_NONBLOCK);
  2058. if (fd < 0) {
  2059. failure_start(file, line, "Can't open %s\n", pathb);
  2060. failure_finish(NULL);
  2061. return (0);
  2062. }
  2063. r = ioctl(fd,
  2064. #ifdef FS_IOC_GETFLAGS
  2065. FS_IOC_GETFLAGS,
  2066. #else
  2067. EXT2_IOC_GETFLAGS,
  2068. #endif
  2069. &flagsb);
  2070. close(fd);
  2071. if (r < 0) {
  2072. failure_start(file, line, "Can't get flags %s\n", pathb);
  2073. failure_finish(NULL);
  2074. return (0);
  2075. }
  2076. if (!nomatch && flagsa != flagsb) {
  2077. failure_start(file, line, "File flags should be identical: "
  2078. "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
  2079. failure_finish(NULL);
  2080. return (0);
  2081. }
  2082. if (nomatch && flagsa == flagsb) {
  2083. failure_start(file, line, "File flags should be different: "
  2084. "%s=%#010x %s=%#010x", patha, flagsa, pathb, flagsb);
  2085. failure_finish(NULL);
  2086. return (0);
  2087. }
  2088. #else
  2089. (void)patha; /* UNUSED */
  2090. (void)pathb; /* UNUSED */
  2091. (void)nomatch; /* UNUSED */
  2092. assertion_count(file, line);
  2093. #endif
  2094. return (1);
  2095. }
  2096. /* Set nodump, report failures. */
  2097. int
  2098. assertion_set_nodump(const char *file, int line, const char *pathname)
  2099. {
  2100. #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
  2101. int r;
  2102. assertion_count(file, line);
  2103. r = chflags(pathname, UF_NODUMP);
  2104. if (r < 0) {
  2105. failure_start(file, line, "Can't set nodump %s\n", pathname);
  2106. failure_finish(NULL);
  2107. return (0);
  2108. }
  2109. #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) && \
  2110. defined(FS_NODUMP_FL)) || \
  2111. (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
  2112. && defined(EXT2_NODUMP_FL))
  2113. int fd, r, flags;
  2114. assertion_count(file, line);
  2115. fd = open(pathname, O_RDONLY | O_NONBLOCK);
  2116. if (fd < 0) {
  2117. failure_start(file, line, "Can't open %s\n", pathname);
  2118. failure_finish(NULL);
  2119. return (0);
  2120. }
  2121. r = ioctl(fd,
  2122. #ifdef FS_IOC_GETFLAGS
  2123. FS_IOC_GETFLAGS,
  2124. #else
  2125. EXT2_IOC_GETFLAGS,
  2126. #endif
  2127. &flags);
  2128. if (r < 0) {
  2129. failure_start(file, line, "Can't get flags %s\n", pathname);
  2130. failure_finish(NULL);
  2131. return (0);
  2132. }
  2133. #ifdef FS_NODUMP_FL
  2134. flags |= FS_NODUMP_FL;
  2135. #else
  2136. flags |= EXT2_NODUMP_FL;
  2137. #endif
  2138. r = ioctl(fd,
  2139. #ifdef FS_IOC_SETFLAGS
  2140. FS_IOC_SETFLAGS,
  2141. #else
  2142. EXT2_IOC_SETFLAGS,
  2143. #endif
  2144. &flags);
  2145. if (r < 0) {
  2146. failure_start(file, line, "Can't set nodump %s\n", pathname);
  2147. failure_finish(NULL);
  2148. return (0);
  2149. }
  2150. close(fd);
  2151. #else
  2152. (void)pathname; /* UNUSED */
  2153. assertion_count(file, line);
  2154. #endif
  2155. return (1);
  2156. }
  2157. #ifdef PROGRAM
  2158. static void assert_version_id(char **qq, size_t *ss)
  2159. {
  2160. char *q = *qq;
  2161. size_t s = *ss;
  2162. /* Version number is a series of digits and periods. */
  2163. while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
  2164. ++q;
  2165. --s;
  2166. }
  2167. if (q[0] == 'd' && q[1] == 'e' && q[2] == 'v') {
  2168. q += 3;
  2169. s -= 3;
  2170. }
  2171. /* Skip a single trailing a,b,c, or d. */
  2172. if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
  2173. ++q;
  2174. /* Version number terminated by space. */
  2175. failure("No space after version: ``%s''", q);
  2176. assert(s > 1);
  2177. failure("No space after version: ``%s''", q);
  2178. assert(*q == ' ');
  2179. ++q; --s;
  2180. *qq = q;
  2181. *ss = s;
  2182. }
  2183. /*
  2184. * Check program version
  2185. */
  2186. void assertVersion(const char *prog, const char *base)
  2187. {
  2188. int r;
  2189. char *p, *q;
  2190. size_t s;
  2191. size_t prog_len = strlen(base);
  2192. r = systemf("%s --version >version.stdout 2>version.stderr", prog);
  2193. if (r != 0)
  2194. r = systemf("%s -W version >version.stdout 2>version.stderr",
  2195. prog);
  2196. failure("Unable to run either %s --version or %s -W version",
  2197. prog, prog);
  2198. if (!assert(r == 0))
  2199. return;
  2200. /* --version should generate nothing to stdout. */
  2201. assertEmptyFile("version.stderr");
  2202. /* Verify format of version message. */
  2203. q = p = slurpfile(&s, "version.stdout");
  2204. /* Version message should start with name of program, then space. */
  2205. assert(s > prog_len + 1);
  2206. failure("Version must start with '%s': ``%s''", base, p);
  2207. if (!assertEqualMem(q, base, prog_len)) {
  2208. free(p);
  2209. return;
  2210. }
  2211. q += prog_len; s -= prog_len;
  2212. assert(*q == ' ');
  2213. q++; s--;
  2214. assert_version_id(&q, &s);
  2215. /* Separator. */
  2216. failure("No `-' between program name and versions: ``%s''", p);
  2217. assertEqualMem(q, "- ", 2);
  2218. q += 2; s -= 2;
  2219. failure("Not long enough for libarchive version: ``%s''", p);
  2220. assert(s > 11);
  2221. failure("Libarchive version must start with `libarchive': ``%s''", p);
  2222. assertEqualMem(q, "libarchive ", 11);
  2223. q += 11; s -= 11;
  2224. assert_version_id(&q, &s);
  2225. /* Skip arbitrary third-party version numbers. */
  2226. while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' ||
  2227. isalnum((unsigned char)*q))) {
  2228. ++q;
  2229. --s;
  2230. }
  2231. /* All terminated by end-of-line. */
  2232. assert(s >= 1);
  2233. /* Skip an optional CR character (e.g., Windows) */
  2234. failure("Version output must end with \\n or \\r\\n");
  2235. if (*q == '\r') { ++q; --s; }
  2236. assertEqualMem(q, "\n", 1);
  2237. free(p);
  2238. }
  2239. #endif /* PROGRAM */
  2240. /*
  2241. *
  2242. * UTILITIES for use by tests.
  2243. *
  2244. */
  2245. /*
  2246. * Check whether platform supports symlinks. This is intended
  2247. * for tests to use in deciding whether to bother testing symlink
  2248. * support; if the platform doesn't support symlinks, there's no point
  2249. * in checking whether the program being tested can create them.
  2250. *
  2251. * Note that the first time this test is called, we actually go out to
  2252. * disk to create and verify a symlink. This is necessary because
  2253. * symlink support is actually a property of a particular filesystem
  2254. * and can thus vary between directories on a single system. After
  2255. * the first call, this returns the cached result from memory, so it's
  2256. * safe to call it as often as you wish.
  2257. */
  2258. int
  2259. canSymlink(void)
  2260. {
  2261. /* Remember the test result */
  2262. static int value = 0, tested = 0;
  2263. if (tested)
  2264. return (value);
  2265. ++tested;
  2266. assertion_make_file(__FILE__, __LINE__, "canSymlink.0", 0644, 1, "a");
  2267. /* Note: Cygwin has its own symlink() emulation that does not
  2268. * use the Win32 CreateSymbolicLink() function. */
  2269. #if defined(_WIN32) && !defined(__CYGWIN__)
  2270. value = my_CreateSymbolicLinkA("canSymlink.1", "canSymlink.0", 0)
  2271. && is_symlink(__FILE__, __LINE__, "canSymlink.1", "canSymlink.0",
  2272. 0);
  2273. #elif HAVE_SYMLINK
  2274. value = (0 == symlink("canSymlink.0", "canSymlink.1"))
  2275. && is_symlink(__FILE__, __LINE__, "canSymlink.1","canSymlink.0",
  2276. 0);
  2277. #endif
  2278. return (value);
  2279. }
  2280. /* Platform-dependent options for hiding the output of a subcommand. */
  2281. #if defined(_WIN32) && !defined(__CYGWIN__)
  2282. static const char *redirectArgs = ">NUL 2>NUL"; /* Win32 cmd.exe */
  2283. #else
  2284. static const char *redirectArgs = ">/dev/null 2>/dev/null"; /* POSIX 'sh' */
  2285. #endif
  2286. /*
  2287. * Can this platform run the bzip2 program?
  2288. */
  2289. int
  2290. canBzip2(void)
  2291. {
  2292. static int tested = 0, value = 0;
  2293. if (!tested) {
  2294. tested = 1;
  2295. if (systemf("bzip2 --help %s", redirectArgs) == 0)
  2296. value = 1;
  2297. }
  2298. return (value);
  2299. }
  2300. /*
  2301. * Can this platform run the grzip program?
  2302. */
  2303. int
  2304. canGrzip(void)
  2305. {
  2306. static int tested = 0, value = 0;
  2307. if (!tested) {
  2308. tested = 1;
  2309. if (systemf("grzip -V %s", redirectArgs) == 0)
  2310. value = 1;
  2311. }
  2312. return (value);
  2313. }
  2314. /*
  2315. * Can this platform run the gzip program?
  2316. */
  2317. int
  2318. canGzip(void)
  2319. {
  2320. static int tested = 0, value = 0;
  2321. if (!tested) {
  2322. tested = 1;
  2323. if (systemf("gzip --help %s", redirectArgs) == 0)
  2324. value = 1;
  2325. }
  2326. return (value);
  2327. }
  2328. /*
  2329. * Can this platform run the lrzip program?
  2330. */
  2331. int
  2332. canRunCommand(const char *cmd)
  2333. {
  2334. static int tested = 0, value = 0;
  2335. if (!tested) {
  2336. tested = 1;
  2337. if (systemf("%s %s", cmd, redirectArgs) == 0)
  2338. value = 1;
  2339. }
  2340. return (value);
  2341. }
  2342. int
  2343. canLrzip(void)
  2344. {
  2345. static int tested = 0, value = 0;
  2346. if (!tested) {
  2347. tested = 1;
  2348. if (systemf("lrzip -V %s", redirectArgs) == 0)
  2349. value = 1;
  2350. }
  2351. return (value);
  2352. }
  2353. /*
  2354. * Can this platform run the lz4 program?
  2355. */
  2356. int
  2357. canLz4(void)
  2358. {
  2359. static int tested = 0, value = 0;
  2360. if (!tested) {
  2361. tested = 1;
  2362. if (systemf("lz4 --help %s", redirectArgs) == 0)
  2363. value = 1;
  2364. }
  2365. return (value);
  2366. }
  2367. /*
  2368. * Can this platform run the zstd program?
  2369. */
  2370. int
  2371. canZstd(void)
  2372. {
  2373. static int tested = 0, value = 0;
  2374. if (!tested) {
  2375. tested = 1;
  2376. if (systemf("zstd --help %s", redirectArgs) == 0)
  2377. value = 1;
  2378. }
  2379. return (value);
  2380. }
  2381. /*
  2382. * Can this platform run the lzip program?
  2383. */
  2384. int
  2385. canLzip(void)
  2386. {
  2387. static int tested = 0, value = 0;
  2388. if (!tested) {
  2389. tested = 1;
  2390. if (systemf("lzip --help %s", redirectArgs) == 0)
  2391. value = 1;
  2392. }
  2393. return (value);
  2394. }
  2395. /*
  2396. * Can this platform run the lzma program?
  2397. */
  2398. int
  2399. canLzma(void)
  2400. {
  2401. static int tested = 0, value = 0;
  2402. if (!tested) {
  2403. tested = 1;
  2404. if (systemf("lzma --help %s", redirectArgs) == 0)
  2405. value = 1;
  2406. }
  2407. return (value);
  2408. }
  2409. /*
  2410. * Can this platform run the lzop program?
  2411. */
  2412. int
  2413. canLzop(void)
  2414. {
  2415. static int tested = 0, value = 0;
  2416. if (!tested) {
  2417. tested = 1;
  2418. if (systemf("lzop --help %s", redirectArgs) == 0)
  2419. value = 1;
  2420. }
  2421. return (value);
  2422. }
  2423. /*
  2424. * Can this platform run the xz program?
  2425. */
  2426. int
  2427. canXz(void)
  2428. {
  2429. static int tested = 0, value = 0;
  2430. if (!tested) {
  2431. tested = 1;
  2432. if (systemf("xz --help %s", redirectArgs) == 0)
  2433. value = 1;
  2434. }
  2435. return (value);
  2436. }
  2437. /*
  2438. * Can this filesystem handle nodump flags.
  2439. */
  2440. int
  2441. canNodump(void)
  2442. {
  2443. #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
  2444. const char *path = "cannodumptest";
  2445. struct stat sb;
  2446. assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
  2447. if (chflags(path, UF_NODUMP) < 0)
  2448. return (0);
  2449. if (stat(path, &sb) < 0)
  2450. return (0);
  2451. if (sb.st_flags & UF_NODUMP)
  2452. return (1);
  2453. #elif (defined(FS_IOC_GETFLAGS) && defined(HAVE_WORKING_FS_IOC_GETFLAGS) \
  2454. && defined(FS_NODUMP_FL)) || \
  2455. (defined(EXT2_IOC_GETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS) \
  2456. && defined(EXT2_NODUMP_FL))
  2457. const char *path = "cannodumptest";
  2458. int fd, r, flags;
  2459. assertion_make_file(__FILE__, __LINE__, path, 0644, 0, NULL);
  2460. fd = open(path, O_RDONLY | O_NONBLOCK);
  2461. if (fd < 0)
  2462. return (0);
  2463. r = ioctl(fd,
  2464. #ifdef FS_IOC_GETFLAGS
  2465. FS_IOC_GETFLAGS,
  2466. #else
  2467. EXT2_IOC_GETFLAGS,
  2468. #endif
  2469. &flags);
  2470. if (r < 0)
  2471. return (0);
  2472. #ifdef FS_NODUMP_FL
  2473. flags |= FS_NODUMP_FL;
  2474. #else
  2475. flags |= EXT2_NODUMP_FL;
  2476. #endif
  2477. r = ioctl(fd,
  2478. #ifdef FS_IOC_SETFLAGS
  2479. FS_IOC_SETFLAGS,
  2480. #else
  2481. EXT2_IOC_SETFLAGS,
  2482. #endif
  2483. &flags);
  2484. if (r < 0)
  2485. return (0);
  2486. close(fd);
  2487. fd = open(path, O_RDONLY | O_NONBLOCK);
  2488. if (fd < 0)
  2489. return (0);
  2490. r = ioctl(fd,
  2491. #ifdef FS_IOC_GETFLAGS
  2492. FS_IOC_GETFLAGS,
  2493. #else
  2494. EXT2_IOC_GETFLAGS,
  2495. #endif
  2496. &flags);
  2497. if (r < 0)
  2498. return (0);
  2499. close(fd);
  2500. #ifdef FS_NODUMP_FL
  2501. if (flags & FS_NODUMP_FL)
  2502. #else
  2503. if (flags & EXT2_NODUMP_FL)
  2504. #endif
  2505. return (1);
  2506. #endif
  2507. return (0);
  2508. }
  2509. /* Get extended attribute value from a path */
  2510. void *
  2511. getXattr(const char *path, const char *name, size_t *sizep)
  2512. {
  2513. void *value = NULL;
  2514. #if ARCHIVE_XATTR_SUPPORT
  2515. ssize_t size;
  2516. #if ARCHIVE_XATTR_LINUX
  2517. size = lgetxattr(path, name, NULL, 0);
  2518. #elif ARCHIVE_XATTR_DARWIN
  2519. size = getxattr(path, name, NULL, 0, 0, XATTR_NOFOLLOW);
  2520. #elif ARCHIVE_XATTR_AIX
  2521. size = lgetea(path, name, NULL, 0);
  2522. #elif ARCHIVE_XATTR_FREEBSD
  2523. size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
  2524. NULL, 0);
  2525. #endif
  2526. if (size >= 0) {
  2527. value = malloc(size);
  2528. #if ARCHIVE_XATTR_LINUX
  2529. size = lgetxattr(path, name, value, size);
  2530. #elif ARCHIVE_XATTR_DARWIN
  2531. size = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
  2532. #elif ARCHIVE_XATTR_AIX
  2533. size = lgetea(path, name, value, size);
  2534. #elif ARCHIVE_XATTR_FREEBSD
  2535. size = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name + 5,
  2536. value, size);
  2537. #endif
  2538. if (size < 0) {
  2539. free(value);
  2540. value = NULL;
  2541. }
  2542. }
  2543. if (size < 0)
  2544. *sizep = 0;
  2545. else
  2546. *sizep = (size_t)size;
  2547. #else /* !ARCHIVE_XATTR_SUPPORT */
  2548. (void)path; /* UNUSED */
  2549. (void)name; /* UNUSED */
  2550. *sizep = 0;
  2551. #endif /* !ARCHIVE_XATTR_SUPPORT */
  2552. return (value);
  2553. }
  2554. /*
  2555. * Set extended attribute on a path
  2556. * Returns 0 on error, 1 on success
  2557. */
  2558. int
  2559. setXattr(const char *path, const char *name, const void *value, size_t size)
  2560. {
  2561. #if ARCHIVE_XATTR_SUPPORT
  2562. #if ARCHIVE_XATTR_LINUX
  2563. if (lsetxattr(path, name, value, size, 0) == 0)
  2564. #elif ARCHIVE_XATTR_DARWIN
  2565. if (setxattr(path, name, value, size, 0, XATTR_NOFOLLOW) == 0)
  2566. #elif ARCHIVE_XATTR_AIX
  2567. if (lsetea(path, name, value, size, 0) == 0)
  2568. #elif ARCHIVE_XATTR_FREEBSD
  2569. if (extattr_set_link(path, EXTATTR_NAMESPACE_USER, name + 5, value,
  2570. size) > -1)
  2571. #else
  2572. if (0)
  2573. #endif
  2574. return (1);
  2575. #else /* !ARCHIVE_XATTR_SUPPORT */
  2576. (void)path; /* UNUSED */
  2577. (void)name; /* UNUSED */
  2578. (void)value; /* UNUSED */
  2579. (void)size; /* UNUSED */
  2580. #endif /* !ARCHIVE_XATTR_SUPPORT */
  2581. return (0);
  2582. }
  2583. #if ARCHIVE_ACL_SUNOS
  2584. /* Fetch ACLs on Solaris using acl() or facl() */
  2585. void *
  2586. sunacl_get(int cmd, int *aclcnt, int fd, const char *path)
  2587. {
  2588. int cnt, cntcmd;
  2589. size_t size;
  2590. void *aclp;
  2591. if (cmd == GETACL) {
  2592. cntcmd = GETACLCNT;
  2593. size = sizeof(aclent_t);
  2594. }
  2595. #if ARCHIVE_ACL_SUNOS_NFS4
  2596. else if (cmd == ACE_GETACL) {
  2597. cntcmd = ACE_GETACLCNT;
  2598. size = sizeof(ace_t);
  2599. }
  2600. #endif
  2601. else {
  2602. errno = EINVAL;
  2603. *aclcnt = -1;
  2604. return (NULL);
  2605. }
  2606. aclp = NULL;
  2607. cnt = -2;
  2608. while (cnt == -2 || (cnt == -1 && errno == ENOSPC)) {
  2609. if (path != NULL)
  2610. cnt = acl(path, cntcmd, 0, NULL);
  2611. else
  2612. cnt = facl(fd, cntcmd, 0, NULL);
  2613. if (cnt > 0) {
  2614. if (aclp == NULL)
  2615. aclp = malloc(cnt * size);
  2616. else
  2617. aclp = realloc(NULL, cnt * size);
  2618. if (aclp != NULL) {
  2619. if (path != NULL)
  2620. cnt = acl(path, cmd, cnt, aclp);
  2621. else
  2622. cnt = facl(fd, cmd, cnt, aclp);
  2623. }
  2624. } else {
  2625. free(aclp);
  2626. aclp = NULL;
  2627. break;
  2628. }
  2629. }
  2630. *aclcnt = cnt;
  2631. return (aclp);
  2632. }
  2633. #endif /* ARCHIVE_ACL_SUNOS */
  2634. /*
  2635. * Set test ACLs on a path
  2636. * Return values:
  2637. * 0: error setting ACLs
  2638. * ARCHIVE_TEST_ACL_TYPE_POSIX1E: POSIX.1E ACLs have been set
  2639. * ARCHIVE_TEST_ACL_TYPE_NFS4: NFSv4 or extended ACLs have been set
  2640. */
  2641. int
  2642. setTestAcl(const char *path)
  2643. {
  2644. #if ARCHIVE_ACL_SUPPORT
  2645. int r = 1;
  2646. #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_DARWIN
  2647. acl_t acl;
  2648. #endif
  2649. #if ARCHIVE_ACL_LIBRICHACL
  2650. struct richacl *richacl;
  2651. #endif
  2652. #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_FREEBSD
  2653. const char *acltext_posix1e = "user:1:rw-,"
  2654. "group:15:r-x,"
  2655. "user::rwx,"
  2656. "group::rwx,"
  2657. "other::r-x,"
  2658. "mask::rwx";
  2659. #elif ARCHIVE_ACL_SUNOS /* Solaris POSIX.1e */
  2660. aclent_t aclp_posix1e[] = {
  2661. { USER_OBJ, -1, 4 | 2 | 1 },
  2662. { USER, 1, 4 | 2 },
  2663. { GROUP_OBJ, -1, 4 | 2 | 1 },
  2664. { GROUP, 15, 4 | 1 },
  2665. { CLASS_OBJ, -1, 4 | 2 | 1 },
  2666. { OTHER_OBJ, -1, 4 | 2 | 1 }
  2667. };
  2668. #endif
  2669. #if ARCHIVE_ACL_FREEBSD /* FreeBSD NFS4 */
  2670. const char *acltext_nfs4 = "user:1:rwpaRcs::allow:1,"
  2671. "group:15:rxaRcs::allow:15,"
  2672. "owner@:rwpxaARWcCos::allow,"
  2673. "group@:rwpxaRcs::allow,"
  2674. "everyone@:rxaRcs::allow";
  2675. #elif ARCHIVE_ACL_LIBRICHACL
  2676. const char *acltext_nfs4 = "owner:rwpxaARWcCoS::mask,"
  2677. "group:rwpxaRcS::mask,"
  2678. "other:rxaRcS::mask,"
  2679. "user:1:rwpaRcS::allow,"
  2680. "group:15:rxaRcS::allow,"
  2681. "owner@:rwpxaARWcCoS::allow,"
  2682. "group@:rwpxaRcS::allow,"
  2683. "everyone@:rxaRcS::allow";
  2684. #elif ARCHIVE_ACL_SUNOS_NFS4 /* Solaris NFS4 */
  2685. ace_t aclp_nfs4[] = {
  2686. { 1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
  2687. ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS | ACE_READ_ACL |
  2688. ACE_SYNCHRONIZE, 0, ACE_ACCESS_ALLOWED_ACE_TYPE },
  2689. { 15, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
  2690. ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
  2691. ACE_IDENTIFIER_GROUP, ACE_ACCESS_ALLOWED_ACE_TYPE },
  2692. { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
  2693. ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_WRITE_ATTRIBUTES |
  2694. ACE_READ_NAMED_ATTRS | ACE_WRITE_NAMED_ATTRS |
  2695. ACE_READ_ACL | ACE_WRITE_ACL | ACE_WRITE_OWNER | ACE_SYNCHRONIZE,
  2696. ACE_OWNER, ACE_ACCESS_ALLOWED_ACE_TYPE },
  2697. { -1, ACE_READ_DATA | ACE_WRITE_DATA | ACE_APPEND_DATA |
  2698. ACE_EXECUTE | ACE_READ_ATTRIBUTES | ACE_READ_NAMED_ATTRS |
  2699. ACE_READ_ACL | ACE_SYNCHRONIZE, ACE_GROUP | ACE_IDENTIFIER_GROUP,
  2700. ACE_ACCESS_ALLOWED_ACE_TYPE },
  2701. { -1, ACE_READ_DATA | ACE_EXECUTE | ACE_READ_ATTRIBUTES |
  2702. ACE_READ_NAMED_ATTRS | ACE_READ_ACL | ACE_SYNCHRONIZE,
  2703. ACE_EVERYONE, ACE_ACCESS_ALLOWED_ACE_TYPE }
  2704. };
  2705. #elif ARCHIVE_ACL_DARWIN /* Mac OS X */
  2706. acl_entry_t aclent;
  2707. acl_permset_t permset;
  2708. const uid_t uid = 1;
  2709. uuid_t uuid;
  2710. int i;
  2711. const acl_perm_t acl_perms[] = {
  2712. ACL_READ_DATA,
  2713. ACL_WRITE_DATA,
  2714. ACL_APPEND_DATA,
  2715. ACL_EXECUTE,
  2716. ACL_READ_ATTRIBUTES,
  2717. ACL_READ_EXTATTRIBUTES,
  2718. ACL_READ_SECURITY,
  2719. #if HAVE_DECL_ACL_SYNCHRONIZE
  2720. ACL_SYNCHRONIZE
  2721. #endif
  2722. };
  2723. #endif /* ARCHIVE_ACL_DARWIN */
  2724. #if ARCHIVE_ACL_FREEBSD
  2725. acl = acl_from_text(acltext_nfs4);
  2726. failure("acl_from_text() error: %s", strerror(errno));
  2727. if (assert(acl != NULL) == 0)
  2728. return (0);
  2729. #elif ARCHIVE_ACL_LIBRICHACL
  2730. richacl = richacl_from_text(acltext_nfs4, NULL, NULL);
  2731. failure("richacl_from_text() error: %s", strerror(errno));
  2732. if (assert(richacl != NULL) == 0)
  2733. return (0);
  2734. #elif ARCHIVE_ACL_DARWIN
  2735. acl = acl_init(1);
  2736. failure("acl_init() error: %s", strerror(errno));
  2737. if (assert(acl != NULL) == 0)
  2738. return (0);
  2739. r = acl_create_entry(&acl, &aclent);
  2740. failure("acl_create_entry() error: %s", strerror(errno));
  2741. if (assertEqualInt(r, 0) == 0)
  2742. goto testacl_free;
  2743. r = acl_set_tag_type(aclent, ACL_EXTENDED_ALLOW);
  2744. failure("acl_set_tag_type() error: %s", strerror(errno));
  2745. if (assertEqualInt(r, 0) == 0)
  2746. goto testacl_free;
  2747. r = acl_get_permset(aclent, &permset);
  2748. failure("acl_get_permset() error: %s", strerror(errno));
  2749. if (assertEqualInt(r, 0) == 0)
  2750. goto testacl_free;
  2751. for (i = 0; i < (int)(sizeof(acl_perms) / sizeof(acl_perms[0])); i++) {
  2752. r = acl_add_perm(permset, acl_perms[i]);
  2753. failure("acl_add_perm() error: %s", strerror(errno));
  2754. if (assertEqualInt(r, 0) == 0)
  2755. goto testacl_free;
  2756. }
  2757. r = acl_set_permset(aclent, permset);
  2758. failure("acl_set_permset() error: %s", strerror(errno));
  2759. if (assertEqualInt(r, 0) == 0)
  2760. goto testacl_free;
  2761. r = mbr_uid_to_uuid(uid, uuid);
  2762. failure("mbr_uid_to_uuid() error: %s", strerror(errno));
  2763. if (assertEqualInt(r, 0) == 0)
  2764. goto testacl_free;
  2765. r = acl_set_qualifier(aclent, uuid);
  2766. failure("acl_set_qualifier() error: %s", strerror(errno));
  2767. if (assertEqualInt(r, 0) == 0)
  2768. goto testacl_free;
  2769. #endif /* ARCHIVE_ACL_DARWIN */
  2770. #if ARCHIVE_ACL_NFS4
  2771. #if ARCHIVE_ACL_FREEBSD
  2772. r = acl_set_file(path, ACL_TYPE_NFS4, acl);
  2773. acl_free(acl);
  2774. #elif ARCHIVE_ACL_LIBRICHACL
  2775. r = richacl_set_file(path, richacl);
  2776. richacl_free(richacl);
  2777. #elif ARCHIVE_ACL_SUNOS_NFS4
  2778. r = acl(path, ACE_SETACL,
  2779. (int)(sizeof(aclp_nfs4)/sizeof(aclp_nfs4[0])), aclp_nfs4);
  2780. #elif ARCHIVE_ACL_DARWIN
  2781. r = acl_set_file(path, ACL_TYPE_EXTENDED, acl);
  2782. acl_free(acl);
  2783. #endif
  2784. if (r == 0)
  2785. return (ARCHIVE_TEST_ACL_TYPE_NFS4);
  2786. #endif /* ARCHIVE_ACL_NFS4 */
  2787. #if ARCHIVE_ACL_POSIX1E
  2788. #if ARCHIVE_ACL_FREEBSD || ARCHIVE_ACL_LIBACL
  2789. acl = acl_from_text(acltext_posix1e);
  2790. failure("acl_from_text() error: %s", strerror(errno));
  2791. if (assert(acl != NULL) == 0)
  2792. return (0);
  2793. r = acl_set_file(path, ACL_TYPE_ACCESS, acl);
  2794. acl_free(acl);
  2795. #elif ARCHIVE_ACL_SUNOS
  2796. r = acl(path, SETACL,
  2797. (int)(sizeof(aclp_posix1e)/sizeof(aclp_posix1e[0])), aclp_posix1e);
  2798. #endif
  2799. if (r == 0)
  2800. return (ARCHIVE_TEST_ACL_TYPE_POSIX1E);
  2801. else
  2802. return (0);
  2803. #endif /* ARCHIVE_ACL_POSIX1E */
  2804. #if ARCHIVE_ACL_DARWIN
  2805. testacl_free:
  2806. acl_free(acl);
  2807. #endif
  2808. #endif /* ARCHIVE_ACL_SUPPORT */
  2809. (void)path; /* UNUSED */
  2810. return (0);
  2811. }
  2812. /*
  2813. * Sleep as needed; useful for verifying disk timestamp changes by
  2814. * ensuring that the wall-clock time has actually changed before we
  2815. * go back to re-read something from disk.
  2816. */
  2817. void
  2818. sleepUntilAfter(time_t t)
  2819. {
  2820. while (t >= time(NULL))
  2821. #if defined(_WIN32) && !defined(__CYGWIN__)
  2822. Sleep(500);
  2823. #else
  2824. sleep(1);
  2825. #endif
  2826. }
  2827. /*
  2828. * Call standard system() call, but build up the command line using
  2829. * sprintf() conventions.
  2830. */
  2831. int
  2832. systemf(const char *fmt, ...)
  2833. {
  2834. char buff[8192];
  2835. va_list ap;
  2836. int r;
  2837. va_start(ap, fmt);
  2838. vsnprintf(buff, sizeof(buff), fmt, ap);
  2839. if (verbosity > VERBOSITY_FULL)
  2840. logprintf("Cmd: %s\n", buff);
  2841. r = system(buff);
  2842. va_end(ap);
  2843. return (r);
  2844. }
  2845. /*
  2846. * Slurp a file into memory for ease of comparison and testing.
  2847. * Returns size of file in 'sizep' if non-NULL, null-terminates
  2848. * data in memory for ease of use.
  2849. */
  2850. char *
  2851. slurpfile(size_t * sizep, const char *fmt, ...)
  2852. {
  2853. char filename[8192];
  2854. struct stat st;
  2855. va_list ap;
  2856. char *p;
  2857. ssize_t bytes_read;
  2858. FILE *f;
  2859. int r;
  2860. va_start(ap, fmt);
  2861. vsnprintf(filename, sizeof(filename), fmt, ap);
  2862. va_end(ap);
  2863. f = fopen(filename, "rb");
  2864. if (f == NULL) {
  2865. /* Note: No error; non-existent file is okay here. */
  2866. return (NULL);
  2867. }
  2868. r = fstat(fileno(f), &st);
  2869. if (r != 0) {
  2870. logprintf("Can't stat file %s\n", filename);
  2871. fclose(f);
  2872. return (NULL);
  2873. }
  2874. p = malloc((size_t)st.st_size + 1);
  2875. if (p == NULL) {
  2876. logprintf("Can't allocate %ld bytes of memory to read file %s\n",
  2877. (long int)st.st_size, filename);
  2878. fclose(f);
  2879. return (NULL);
  2880. }
  2881. bytes_read = fread(p, 1, (size_t)st.st_size, f);
  2882. if (bytes_read < st.st_size) {
  2883. logprintf("Can't read file %s\n", filename);
  2884. fclose(f);
  2885. free(p);
  2886. return (NULL);
  2887. }
  2888. p[st.st_size] = '\0';
  2889. if (sizep != NULL)
  2890. *sizep = (size_t)st.st_size;
  2891. fclose(f);
  2892. return (p);
  2893. }
  2894. /*
  2895. * Slurp a file into memory for ease of comparison and testing.
  2896. * Returns size of file in 'sizep' if non-NULL, null-terminates
  2897. * data in memory for ease of use.
  2898. */
  2899. void
  2900. dumpfile(const char *filename, void *data, size_t len)
  2901. {
  2902. ssize_t bytes_written;
  2903. FILE *f;
  2904. f = fopen(filename, "wb");
  2905. if (f == NULL) {
  2906. logprintf("Can't open file %s for writing\n", filename);
  2907. return;
  2908. }
  2909. bytes_written = fwrite(data, 1, len, f);
  2910. if (bytes_written < (ssize_t)len)
  2911. logprintf("Can't write file %s\n", filename);
  2912. fclose(f);
  2913. }
  2914. /* Read a uuencoded file from the reference directory, decode, and
  2915. * write the result into the current directory. */
  2916. #define VALID_UUDECODE(c) (c >= 32 && c <= 96)
  2917. #define UUDECODE(c) (((c) - 0x20) & 0x3f)
  2918. void
  2919. extract_reference_file(const char *name)
  2920. {
  2921. char buff[1024];
  2922. FILE *in, *out;
  2923. snprintf(buff, sizeof(buff), "%s/%s.uu", refdir, name);
  2924. in = fopen(buff, "r");
  2925. failure("Couldn't open reference file %s", buff);
  2926. assert(in != NULL);
  2927. if (in == NULL)
  2928. return;
  2929. /* Read up to and including the 'begin' line. */
  2930. for (;;) {
  2931. if (fgets(buff, sizeof(buff), in) == NULL) {
  2932. /* TODO: This is a failure. */
  2933. return;
  2934. }
  2935. if (memcmp(buff, "begin ", 6) == 0)
  2936. break;
  2937. }
  2938. /* Now, decode the rest and write it. */
  2939. out = fopen(name, "wb");
  2940. while (fgets(buff, sizeof(buff), in) != NULL) {
  2941. char *p = buff;
  2942. int bytes;
  2943. if (memcmp(buff, "end", 3) == 0)
  2944. break;
  2945. bytes = UUDECODE(*p++);
  2946. while (bytes > 0) {
  2947. int n = 0;
  2948. /* Write out 1-3 bytes from that. */
  2949. assert(VALID_UUDECODE(p[0]));
  2950. assert(VALID_UUDECODE(p[1]));
  2951. n = UUDECODE(*p++) << 18;
  2952. n |= UUDECODE(*p++) << 12;
  2953. fputc(n >> 16, out);
  2954. --bytes;
  2955. if (bytes > 0) {
  2956. assert(VALID_UUDECODE(p[0]));
  2957. n |= UUDECODE(*p++) << 6;
  2958. fputc((n >> 8) & 0xFF, out);
  2959. --bytes;
  2960. }
  2961. if (bytes > 0) {
  2962. assert(VALID_UUDECODE(p[0]));
  2963. n |= UUDECODE(*p++);
  2964. fputc(n & 0xFF, out);
  2965. --bytes;
  2966. }
  2967. }
  2968. }
  2969. fclose(out);
  2970. fclose(in);
  2971. }
  2972. void
  2973. copy_reference_file(const char *name)
  2974. {
  2975. char buff[1024];
  2976. FILE *in, *out;
  2977. size_t rbytes;
  2978. snprintf(buff, sizeof(buff), "%s/%s", refdir, name);
  2979. in = fopen(buff, "rb");
  2980. failure("Couldn't open reference file %s", buff);
  2981. assert(in != NULL);
  2982. if (in == NULL)
  2983. return;
  2984. /* Now, decode the rest and write it. */
  2985. /* Not a lot of error checking here; the input better be right. */
  2986. out = fopen(name, "wb");
  2987. while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
  2988. if (fwrite(buff, 1, rbytes, out) != rbytes) {
  2989. logprintf("Error: fwrite\n");
  2990. break;
  2991. }
  2992. }
  2993. fclose(out);
  2994. fclose(in);
  2995. }
  2996. int
  2997. is_LargeInode(const char *file)
  2998. {
  2999. #if defined(_WIN32) && !defined(__CYGWIN__)
  3000. BY_HANDLE_FILE_INFORMATION bhfi;
  3001. int r;
  3002. r = my_GetFileInformationByName(file, &bhfi);
  3003. if (r != 0)
  3004. return (0);
  3005. return (bhfi.nFileIndexHigh & 0x0000FFFFUL);
  3006. #else
  3007. struct stat st;
  3008. int64_t ino;
  3009. if (stat(file, &st) < 0)
  3010. return (0);
  3011. ino = (int64_t)st.st_ino;
  3012. return (ino > 0xffffffff);
  3013. #endif
  3014. }
  3015. void
  3016. extract_reference_files(const char **names)
  3017. {
  3018. while (names && *names)
  3019. extract_reference_file(*names++);
  3020. }
  3021. #ifndef PROGRAM
  3022. /* Set ACLs */
  3023. int
  3024. assertion_entry_set_acls(const char *file, int line, struct archive_entry *ae,
  3025. struct archive_test_acl_t *acls, int n)
  3026. {
  3027. int i, r, ret;
  3028. assertion_count(file, line);
  3029. ret = 0;
  3030. archive_entry_acl_clear(ae);
  3031. for (i = 0; i < n; i++) {
  3032. r = archive_entry_acl_add_entry(ae,
  3033. acls[i].type, acls[i].permset, acls[i].tag,
  3034. acls[i].qual, acls[i].name);
  3035. if (r != 0) {
  3036. ret = 1;
  3037. failure_start(file, line, "type=%#010x, "
  3038. "permset=%#010x, tag=%d, qual=%d name=%s",
  3039. acls[i].type, acls[i].permset, acls[i].tag,
  3040. acls[i].qual, acls[i].name);
  3041. failure_finish(NULL);
  3042. }
  3043. }
  3044. return (ret);
  3045. }
  3046. static int
  3047. archive_test_acl_match(struct archive_test_acl_t *acl, int type, int permset,
  3048. int tag, int qual, const char *name)
  3049. {
  3050. if (type != acl->type)
  3051. return (0);
  3052. if (permset != acl->permset)
  3053. return (0);
  3054. if (tag != acl->tag)
  3055. return (0);
  3056. if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
  3057. return (1);
  3058. if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
  3059. return (1);
  3060. if (tag == ARCHIVE_ENTRY_ACL_EVERYONE)
  3061. return (1);
  3062. if (tag == ARCHIVE_ENTRY_ACL_OTHER)
  3063. return (1);
  3064. if (qual != acl->qual)
  3065. return (0);
  3066. if (name == NULL) {
  3067. if (acl->name == NULL || acl->name[0] == '\0')
  3068. return (1);
  3069. return (0);
  3070. }
  3071. if (acl->name == NULL) {
  3072. if (name[0] == '\0')
  3073. return (1);
  3074. return (0);
  3075. }
  3076. return (0 == strcmp(name, acl->name));
  3077. }
  3078. /* Compare ACLs */
  3079. int
  3080. assertion_entry_compare_acls(const char *file, int line,
  3081. struct archive_entry *ae, struct archive_test_acl_t *acls, int cnt,
  3082. int want_type, int mode)
  3083. {
  3084. int *marker;
  3085. int i, r, n, ret;
  3086. int type, permset, tag, qual;
  3087. int matched;
  3088. const char *name;
  3089. assertion_count(file, line);
  3090. ret = 0;
  3091. n = 0;
  3092. marker = malloc(sizeof(marker[0]) * cnt);
  3093. for (i = 0; i < cnt; i++) {
  3094. if ((acls[i].type & want_type) != 0) {
  3095. marker[n] = i;
  3096. n++;
  3097. }
  3098. }
  3099. if (n == 0) {
  3100. failure_start(file, line, "No ACL's to compare, type mask: %d",
  3101. want_type);
  3102. return (1);
  3103. }
  3104. while (0 == (r = archive_entry_acl_next(ae, want_type,
  3105. &type, &permset, &tag, &qual, &name))) {
  3106. for (i = 0, matched = 0; i < n && !matched; i++) {
  3107. if (archive_test_acl_match(&acls[marker[i]], type,
  3108. permset, tag, qual, name)) {
  3109. /* We found a match; remove it. */
  3110. marker[i] = marker[n - 1];
  3111. n--;
  3112. matched = 1;
  3113. }
  3114. }
  3115. if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
  3116. && tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
  3117. if (!matched) {
  3118. failure_start(file, line, "No match for "
  3119. "user_obj perm");
  3120. failure_finish(NULL);
  3121. ret = 1;
  3122. }
  3123. if ((permset << 6) != (mode & 0700)) {
  3124. failure_start(file, line, "USER_OBJ permset "
  3125. "(%02o) != user mode (%02o)", permset,
  3126. 07 & (mode >> 6));
  3127. failure_finish(NULL);
  3128. ret = 1;
  3129. }
  3130. } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
  3131. && tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
  3132. if (!matched) {
  3133. failure_start(file, line, "No match for "
  3134. "group_obj perm");
  3135. failure_finish(NULL);
  3136. ret = 1;
  3137. }
  3138. if ((permset << 3) != (mode & 0070)) {
  3139. failure_start(file, line, "GROUP_OBJ permset "
  3140. "(%02o) != group mode (%02o)", permset,
  3141. 07 & (mode >> 3));
  3142. failure_finish(NULL);
  3143. ret = 1;
  3144. }
  3145. } else if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS
  3146. && tag == ARCHIVE_ENTRY_ACL_OTHER) {
  3147. if (!matched) {
  3148. failure_start(file, line, "No match for "
  3149. "other perm");
  3150. failure_finish(NULL);
  3151. ret = 1;
  3152. }
  3153. if ((permset << 0) != (mode & 0007)) {
  3154. failure_start(file, line, "OTHER permset "
  3155. "(%02o) != other mode (%02o)", permset,
  3156. mode & 07);
  3157. failure_finish(NULL);
  3158. ret = 1;
  3159. }
  3160. } else if (matched != 1) {
  3161. failure_start(file, line, "Could not find match for "
  3162. "ACL (type=%#010x,permset=%#010x,tag=%d,qual=%d,"
  3163. "name=``%s'')", type, permset, tag, qual, name);
  3164. failure_finish(NULL);
  3165. ret = 1;
  3166. }
  3167. }
  3168. if (r != ARCHIVE_EOF) {
  3169. failure_start(file, line, "Should not exit before EOF");
  3170. failure_finish(NULL);
  3171. ret = 1;
  3172. }
  3173. if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0 &&
  3174. (mode_t)(mode & 0777) != (archive_entry_mode(ae) & 0777)) {
  3175. failure_start(file, line, "Mode (%02o) and entry mode (%02o) "
  3176. "mismatch", mode, archive_entry_mode(ae));
  3177. failure_finish(NULL);
  3178. ret = 1;
  3179. }
  3180. if (n != 0) {
  3181. failure_start(file, line, "Could not find match for ACL "
  3182. "(type=%#010x,permset=%#010x,tag=%d,qual=%d,name=``%s'')",
  3183. acls[marker[0]].type, acls[marker[0]].permset,
  3184. acls[marker[0]].tag, acls[marker[0]].qual,
  3185. acls[marker[0]].name);
  3186. failure_finish(NULL);
  3187. ret = 1;
  3188. /* Number of ACLs not matched should == 0 */
  3189. }
  3190. free(marker);
  3191. return (ret);
  3192. }
  3193. #endif /* !defined(PROGRAM) */
  3194. /*
  3195. *
  3196. * TEST management
  3197. *
  3198. */
  3199. /*
  3200. * "list.h" is simply created by "grep DEFINE_TEST test_*.c"; it has
  3201. * a line like
  3202. * DEFINE_TEST(test_function)
  3203. * for each test.
  3204. */
  3205. struct test_list_t
  3206. {
  3207. void (*func)(void);
  3208. const char *name;
  3209. int failures;
  3210. };
  3211. /* Use "list.h" to declare all of the test functions. */
  3212. #undef DEFINE_TEST
  3213. #define DEFINE_TEST(name) void name(void);
  3214. #include "list.h"
  3215. /* Use "list.h" to create a list of all tests (functions and names). */
  3216. #undef DEFINE_TEST
  3217. #define DEFINE_TEST(n) { n, #n, 0 },
  3218. static struct test_list_t tests[] = {
  3219. #include "list.h"
  3220. };
  3221. /*
  3222. * Summarize repeated failures in the just-completed test.
  3223. */
  3224. static void
  3225. test_summarize(int failed, int skips_num)
  3226. {
  3227. unsigned int i;
  3228. switch (verbosity) {
  3229. case VERBOSITY_SUMMARY_ONLY:
  3230. printf(failed ? "E" : ".");
  3231. fflush(stdout);
  3232. break;
  3233. case VERBOSITY_PASSFAIL:
  3234. printf(failed ? "FAIL\n" : skips_num ? "ok (S)\n" : "ok\n");
  3235. break;
  3236. }
  3237. log_console = (verbosity == VERBOSITY_LIGHT_REPORT);
  3238. for (i = 0; i < sizeof(failed_lines)/sizeof(failed_lines[0]); i++) {
  3239. if (failed_lines[i].count > 1 && !failed_lines[i].skip)
  3240. logprintf("%s:%d: Summary: Failed %d times\n",
  3241. failed_filename, i, failed_lines[i].count);
  3242. }
  3243. /* Clear the failure history for the next file. */
  3244. failed_filename = NULL;
  3245. memset(failed_lines, 0, sizeof(failed_lines));
  3246. }
  3247. /*
  3248. * Actually run a single test, with appropriate setup and cleanup.
  3249. */
  3250. static int
  3251. test_run(int i, const char *tmpdir)
  3252. {
  3253. #ifdef PATH_MAX
  3254. char workdir[PATH_MAX * 2];
  3255. #else
  3256. char workdir[1024 * 2];
  3257. #endif
  3258. char logfilename[64];
  3259. int failures_before = failures;
  3260. int skips_before = skips;
  3261. int oldumask;
  3262. switch (verbosity) {
  3263. case VERBOSITY_SUMMARY_ONLY: /* No per-test reports at all */
  3264. break;
  3265. case VERBOSITY_PASSFAIL: /* rest of line will include ok/FAIL marker */
  3266. printf("%3d: %-64s", i, tests[i].name);
  3267. fflush(stdout);
  3268. break;
  3269. default: /* Title of test, details will follow */
  3270. printf("%3d: %s\n", i, tests[i].name);
  3271. }
  3272. /* Chdir to the top-level work directory. */
  3273. if (!assertChdir(tmpdir)) {
  3274. fprintf(stderr,
  3275. "ERROR: Can't chdir to top work dir %s\n", tmpdir);
  3276. exit(1);
  3277. }
  3278. /* Create a log file for this test. */
  3279. snprintf(logfilename, sizeof(logfilename), "%s.log", tests[i].name);
  3280. logfile = fopen(logfilename, "w");
  3281. fprintf(logfile, "%s\n\n", tests[i].name);
  3282. /* Chdir() to a work dir for this specific test. */
  3283. snprintf(workdir, sizeof(workdir), "%s/%s", tmpdir, tests[i].name);
  3284. testworkdir = workdir;
  3285. if (!assertMakeDir(testworkdir, 0755)
  3286. || !assertChdir(testworkdir)) {
  3287. fprintf(stderr,
  3288. "ERROR: Can't chdir to work dir %s\n", testworkdir);
  3289. exit(1);
  3290. }
  3291. /* Explicitly reset the locale before each test. */
  3292. setlocale(LC_ALL, "C");
  3293. /* Record the umask before we run the test. */
  3294. umask(oldumask = umask(0));
  3295. /*
  3296. * Run the actual test.
  3297. */
  3298. (*tests[i].func)();
  3299. /*
  3300. * Clean up and report afterwards.
  3301. */
  3302. testworkdir = NULL;
  3303. /* Restore umask */
  3304. umask(oldumask);
  3305. /* Reset locale. */
  3306. setlocale(LC_ALL, "C");
  3307. /* Reset directory. */
  3308. if (!assertChdir(tmpdir)) {
  3309. fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",
  3310. tmpdir);
  3311. exit(1);
  3312. }
  3313. /* Report per-test summaries. */
  3314. tests[i].failures = failures - failures_before;
  3315. test_summarize(tests[i].failures, skips - skips_before);
  3316. /* Close the per-test log file. */
  3317. fclose(logfile);
  3318. logfile = NULL;
  3319. /* If there were no failures, we can remove the work dir and logfile. */
  3320. if (tests[i].failures == 0) {
  3321. if (!keep_temp_files && assertChdir(tmpdir)) {
  3322. #if defined(_WIN32) && !defined(__CYGWIN__)
  3323. /* Make sure not to leave empty directories.
  3324. * Sometimes a processing of closing files used by tests
  3325. * is not done, then rmdir will be failed and it will
  3326. * leave a empty test directory. So we should wait a few
  3327. * seconds and retry rmdir. */
  3328. int r, t;
  3329. for (t = 0; t < 10; t++) {
  3330. if (t > 0)
  3331. Sleep(1000);
  3332. r = systemf("rmdir /S /Q %s", tests[i].name);
  3333. if (r == 0)
  3334. break;
  3335. }
  3336. systemf("del %s", logfilename);
  3337. #else
  3338. systemf("rm -rf %s", tests[i].name);
  3339. systemf("rm %s", logfilename);
  3340. #endif
  3341. }
  3342. }
  3343. /* Return appropriate status. */
  3344. return (tests[i].failures);
  3345. }
  3346. /*
  3347. *
  3348. *
  3349. * MAIN and support routines.
  3350. *
  3351. *
  3352. */
  3353. static void
  3354. usage(const char *program)
  3355. {
  3356. static const int limit = sizeof(tests) / sizeof(tests[0]);
  3357. int i;
  3358. printf("Usage: %s [options] <test> <test> ...\n", program);
  3359. printf("Default is to run all tests.\n");
  3360. printf("Otherwise, specify the numbers of the tests you wish to run.\n");
  3361. printf("Options:\n");
  3362. printf(" -d Dump core after any failure, for debugging.\n");
  3363. printf(" -k Keep all temp files.\n");
  3364. printf(" Default: temp files for successful tests deleted.\n");
  3365. #ifdef PROGRAM
  3366. printf(" -p <path> Path to executable to be tested.\n");
  3367. printf(" Default: path taken from " ENVBASE " environment variable.\n");
  3368. #endif
  3369. printf(" -q Quiet.\n");
  3370. printf(" -r <dir> Path to dir containing reference files.\n");
  3371. printf(" Default: Current directory.\n");
  3372. printf(" -u Keep running specifies tests until one fails.\n");
  3373. printf(" -v Verbose.\n");
  3374. printf("Available tests:\n");
  3375. for (i = 0; i < limit; i++)
  3376. printf(" %d: %s\n", i, tests[i].name);
  3377. exit(1);
  3378. }
  3379. static char *
  3380. get_refdir(const char *d)
  3381. {
  3382. size_t tried_size, buff_size;
  3383. char *buff, *tried, *pwd = NULL, *p = NULL;
  3384. #ifdef PATH_MAX
  3385. buff_size = PATH_MAX;
  3386. #else
  3387. buff_size = 8192;
  3388. #endif
  3389. buff = calloc(buff_size, 1);
  3390. if (buff == NULL) {
  3391. fprintf(stderr, "Unable to allocate memory\n");
  3392. exit(1);
  3393. }
  3394. /* Allocate a buffer to hold the various directories we checked. */
  3395. tried_size = buff_size * 2;
  3396. tried = calloc(tried_size, 1);
  3397. if (tried == NULL) {
  3398. fprintf(stderr, "Unable to allocate memory\n");
  3399. exit(1);
  3400. }
  3401. /* If a dir was specified, try that */
  3402. if (d != NULL) {
  3403. pwd = NULL;
  3404. snprintf(buff, buff_size, "%s", d);
  3405. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3406. if (p != NULL) goto success;
  3407. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3408. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3409. goto failure;
  3410. }
  3411. /* Get the current dir. */
  3412. #if defined(PATH_MAX) && !defined(__GLIBC__)
  3413. pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
  3414. #else
  3415. pwd = getcwd(NULL, 0);
  3416. #endif
  3417. while (pwd[strlen(pwd) - 1] == '\n')
  3418. pwd[strlen(pwd) - 1] = '\0';
  3419. /* Look for a known file. */
  3420. snprintf(buff, buff_size, "%s", pwd);
  3421. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3422. if (p != NULL) goto success;
  3423. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3424. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3425. snprintf(buff, buff_size, "%s/test", pwd);
  3426. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3427. if (p != NULL) goto success;
  3428. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3429. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3430. #if defined(LIBRARY)
  3431. snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
  3432. #else
  3433. snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
  3434. #endif
  3435. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3436. if (p != NULL) goto success;
  3437. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3438. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3439. #if defined(PROGRAM_ALIAS)
  3440. snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
  3441. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3442. if (p != NULL) goto success;
  3443. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3444. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3445. #endif
  3446. if (memcmp(pwd, "/usr/obj", 8) == 0) {
  3447. snprintf(buff, buff_size, "%s", pwd + 8);
  3448. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3449. if (p != NULL) goto success;
  3450. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3451. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3452. snprintf(buff, buff_size, "%s/test", pwd + 8);
  3453. p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
  3454. if (p != NULL) goto success;
  3455. strncat(tried, buff, tried_size - strlen(tried) - 1);
  3456. strncat(tried, "\n", tried_size - strlen(tried) - 1);
  3457. }
  3458. failure:
  3459. printf("Unable to locate known reference file %s\n", KNOWNREF);
  3460. printf(" Checked following directories:\n%s\n", tried);
  3461. printf("Use -r option to specify full path to reference directory\n");
  3462. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  3463. DebugBreak();
  3464. #endif
  3465. exit(1);
  3466. success:
  3467. free(p);
  3468. free(pwd);
  3469. free(tried);
  3470. /* Copy result into a fresh buffer to reduce memory usage. */
  3471. p = strdup(buff);
  3472. free(buff);
  3473. return p;
  3474. }
  3475. /* Filter tests against a glob pattern. Returns non-zero if test matches
  3476. * pattern, zero otherwise. A '^' at the beginning of the pattern negates
  3477. * the return values (i.e. returns zero for a match, non-zero otherwise.
  3478. */
  3479. static int
  3480. test_filter(const char *pattern, const char *test)
  3481. {
  3482. int retval = 0;
  3483. int negate = 0;
  3484. const char *p = pattern;
  3485. const char *t = test;
  3486. if (p[0] == '^')
  3487. {
  3488. negate = 1;
  3489. p++;
  3490. }
  3491. while (1)
  3492. {
  3493. if (p[0] == '\\')
  3494. p++;
  3495. else if (p[0] == '*')
  3496. {
  3497. while (p[0] == '*')
  3498. p++;
  3499. if (p[0] == '\\')
  3500. p++;
  3501. if ((t = strchr(t, p[0])) == 0)
  3502. break;
  3503. }
  3504. if (p[0] != t[0])
  3505. break;
  3506. if (p[0] == '\0') {
  3507. retval = 1;
  3508. break;
  3509. }
  3510. p++;
  3511. t++;
  3512. }
  3513. return (negate) ? !retval : retval;
  3514. }
  3515. static int
  3516. get_test_set(int *test_set, int limit, const char *test)
  3517. {
  3518. int start, end;
  3519. int idx = 0;
  3520. if (test == NULL) {
  3521. /* Default: Run all tests. */
  3522. for (;idx < limit; idx++)
  3523. test_set[idx] = idx;
  3524. return (limit);
  3525. }
  3526. if (*test >= '0' && *test <= '9') {
  3527. const char *vp = test;
  3528. start = 0;
  3529. while (*vp >= '0' && *vp <= '9') {
  3530. start *= 10;
  3531. start += *vp - '0';
  3532. ++vp;
  3533. }
  3534. if (*vp == '\0') {
  3535. end = start;
  3536. } else if (*vp == '-') {
  3537. ++vp;
  3538. if (*vp == '\0') {
  3539. end = limit - 1;
  3540. } else {
  3541. end = 0;
  3542. while (*vp >= '0' && *vp <= '9') {
  3543. end *= 10;
  3544. end += *vp - '0';
  3545. ++vp;
  3546. }
  3547. }
  3548. } else
  3549. return (-1);
  3550. if (start < 0 || end >= limit || start > end)
  3551. return (-1);
  3552. while (start <= end)
  3553. test_set[idx++] = start++;
  3554. } else {
  3555. for (start = 0; start < limit; ++start) {
  3556. const char *name = tests[start].name;
  3557. if (test_filter(test, name))
  3558. test_set[idx++] = start;
  3559. }
  3560. }
  3561. return ((idx == 0)?-1:idx);
  3562. }
  3563. int
  3564. main(int argc, char **argv)
  3565. {
  3566. static const int limit = sizeof(tests) / sizeof(tests[0]);
  3567. int test_set[sizeof(tests) / sizeof(tests[0])];
  3568. int i = 0, j = 0, tests_run = 0, tests_failed = 0, option;
  3569. int testprogdir_len;
  3570. #ifdef PROGRAM
  3571. int tmp2_len;
  3572. #endif
  3573. time_t now;
  3574. struct tm *tmptr;
  3575. #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
  3576. struct tm tmbuf;
  3577. #endif
  3578. char *refdir_alloc = NULL;
  3579. const char *progname;
  3580. char **saved_argv;
  3581. const char *tmp, *option_arg, *p;
  3582. #ifdef PATH_MAX
  3583. char tmpdir[PATH_MAX];
  3584. #else
  3585. char tmpdir[256];
  3586. #endif
  3587. char *pwd, *testprogdir, *tmp2 = NULL, *vlevel = NULL;
  3588. char tmpdir_timestamp[32];
  3589. (void)argc; /* UNUSED */
  3590. /* Get the current dir. */
  3591. #if defined(PATH_MAX) && !defined(__GLIBC__)
  3592. pwd = getcwd(NULL, PATH_MAX);/* Solaris getcwd needs the size. */
  3593. #else
  3594. pwd = getcwd(NULL, 0);
  3595. #endif
  3596. while (pwd[strlen(pwd) - 1] == '\n')
  3597. pwd[strlen(pwd) - 1] = '\0';
  3598. #if defined(HAVE__CrtSetReportMode) && !defined(__WATCOMC__)
  3599. /* To stop to run the default invalid parameter handler. */
  3600. _set_invalid_parameter_handler(invalid_parameter_handler);
  3601. /* Disable annoying assertion message box. */
  3602. _CrtSetReportMode(_CRT_ASSERT, 0);
  3603. #endif
  3604. /*
  3605. * Name of this program, used to build root of our temp directory
  3606. * tree.
  3607. */
  3608. progname = p = argv[0];
  3609. testprogdir_len = strlen(progname) + 1;
  3610. if ((testprogdir = (char *)malloc(testprogdir_len)) == NULL)
  3611. {
  3612. fprintf(stderr, "ERROR: Out of memory.");
  3613. exit(1);
  3614. }
  3615. strncpy(testprogdir, progname, testprogdir_len);
  3616. while (*p != '\0') {
  3617. /* Support \ or / dir separators for Windows compat. */
  3618. if (*p == '/' || *p == '\\')
  3619. {
  3620. progname = p + 1;
  3621. i = j;
  3622. }
  3623. ++p;
  3624. j++;
  3625. }
  3626. testprogdir[i] = '\0';
  3627. #if defined(_WIN32) && !defined(__CYGWIN__)
  3628. if (testprogdir[0] != '/' && testprogdir[0] != '\\' &&
  3629. !(((testprogdir[0] >= 'a' && testprogdir[0] <= 'z') ||
  3630. (testprogdir[0] >= 'A' && testprogdir[0] <= 'Z')) &&
  3631. testprogdir[1] == ':' &&
  3632. (testprogdir[2] == '/' || testprogdir[2] == '\\')))
  3633. #else
  3634. if (testprogdir[0] != '/')
  3635. #endif
  3636. {
  3637. /* Fixup path for relative directories. */
  3638. if ((testprogdir = (char *)realloc(testprogdir,
  3639. strlen(pwd) + 1 + strlen(testprogdir) + 1)) == NULL)
  3640. {
  3641. fprintf(stderr, "ERROR: Out of memory.");
  3642. exit(1);
  3643. }
  3644. memmove(testprogdir + strlen(pwd) + 1, testprogdir,
  3645. strlen(testprogdir) + 1);
  3646. memcpy(testprogdir, pwd, strlen(pwd));
  3647. testprogdir[strlen(pwd)] = '/';
  3648. }
  3649. #ifdef PROGRAM
  3650. /* Get the target program from environment, if available. */
  3651. testprogfile = getenv(ENVBASE);
  3652. #endif
  3653. if (getenv("TMPDIR") != NULL)
  3654. tmp = getenv("TMPDIR");
  3655. else if (getenv("TMP") != NULL)
  3656. tmp = getenv("TMP");
  3657. else if (getenv("TEMP") != NULL)
  3658. tmp = getenv("TEMP");
  3659. else if (getenv("TEMPDIR") != NULL)
  3660. tmp = getenv("TEMPDIR");
  3661. else
  3662. tmp = "/tmp";
  3663. /* Allow -d to be controlled through the environment. */
  3664. if (getenv(ENVBASE "_DEBUG") != NULL)
  3665. dump_on_failure = 1;
  3666. /* Allow -v to be controlled through the environment. */
  3667. if (getenv("_VERBOSITY_LEVEL") != NULL)
  3668. {
  3669. vlevel = getenv("_VERBOSITY_LEVEL");
  3670. verbosity = atoi(vlevel);
  3671. if (verbosity < VERBOSITY_SUMMARY_ONLY || verbosity > VERBOSITY_FULL)
  3672. {
  3673. /* Unsupported verbosity levels are silently ignored */
  3674. vlevel = NULL;
  3675. verbosity = VERBOSITY_PASSFAIL;
  3676. }
  3677. }
  3678. /* Get the directory holding test files from environment. */
  3679. refdir = getenv(ENVBASE "_TEST_FILES");
  3680. /*
  3681. * Parse options, without using getopt(), which isn't available
  3682. * on all platforms.
  3683. */
  3684. ++argv; /* Skip program name */
  3685. while (*argv != NULL) {
  3686. if (**argv != '-')
  3687. break;
  3688. p = *argv++;
  3689. ++p; /* Skip '-' */
  3690. while (*p != '\0') {
  3691. option = *p++;
  3692. option_arg = NULL;
  3693. /* If 'opt' takes an argument, parse that. */
  3694. if (option == 'p' || option == 'r') {
  3695. if (*p != '\0')
  3696. option_arg = p;
  3697. else if (*argv == NULL) {
  3698. fprintf(stderr,
  3699. "Option -%c requires argument.\n",
  3700. option);
  3701. usage(progname);
  3702. } else
  3703. option_arg = *argv++;
  3704. p = ""; /* End of this option word. */
  3705. }
  3706. /* Now, handle the option. */
  3707. switch (option) {
  3708. case 'd':
  3709. dump_on_failure = 1;
  3710. break;
  3711. case 'k':
  3712. keep_temp_files = 1;
  3713. break;
  3714. case 'p':
  3715. #ifdef PROGRAM
  3716. testprogfile = option_arg;
  3717. #else
  3718. fprintf(stderr, "-p option not permitted\n");
  3719. usage(progname);
  3720. #endif
  3721. break;
  3722. case 'q':
  3723. if (!vlevel)
  3724. verbosity--;
  3725. break;
  3726. case 'r':
  3727. refdir = option_arg;
  3728. break;
  3729. case 'u':
  3730. until_failure++;
  3731. break;
  3732. case 'v':
  3733. if (!vlevel)
  3734. verbosity++;
  3735. break;
  3736. default:
  3737. fprintf(stderr, "Unrecognized option '%c'\n",
  3738. option);
  3739. usage(progname);
  3740. }
  3741. }
  3742. }
  3743. /*
  3744. * Sanity-check that our options make sense.
  3745. */
  3746. #ifdef PROGRAM
  3747. if (testprogfile == NULL)
  3748. {
  3749. tmp2_len = strlen(testprogdir) + 1 + strlen(PROGRAM) + 1;
  3750. if ((tmp2 = (char *)malloc(tmp2_len)) == NULL)
  3751. {
  3752. fprintf(stderr, "ERROR: Out of memory.");
  3753. exit(1);
  3754. }
  3755. strncpy(tmp2, testprogdir, tmp2_len);
  3756. strncat(tmp2, "/", tmp2_len);
  3757. strncat(tmp2, PROGRAM, tmp2_len);
  3758. testprogfile = tmp2;
  3759. }
  3760. {
  3761. char *testprg;
  3762. int testprg_len;
  3763. #if defined(_WIN32) && !defined(__CYGWIN__)
  3764. /* Command.com sometimes rejects '/' separators. */
  3765. testprg = strdup(testprogfile);
  3766. for (i = 0; testprg[i] != '\0'; i++) {
  3767. if (testprg[i] == '/')
  3768. testprg[i] = '\\';
  3769. }
  3770. testprogfile = testprg;
  3771. #endif
  3772. /* Quote the name that gets put into shell command lines. */
  3773. testprg_len = strlen(testprogfile) + 3;
  3774. testprg = malloc(testprg_len);
  3775. strncpy(testprg, "\"", testprg_len);
  3776. strncat(testprg, testprogfile, testprg_len);
  3777. strncat(testprg, "\"", testprg_len);
  3778. testprog = testprg;
  3779. }
  3780. #endif
  3781. #if !defined(_WIN32) && defined(SIGPIPE)
  3782. { /* Ignore SIGPIPE signals */
  3783. struct sigaction sa;
  3784. sa.sa_handler = SIG_IGN;
  3785. sigemptyset(&sa.sa_mask);
  3786. sa.sa_flags = 0;
  3787. sigaction(SIGPIPE, &sa, NULL);
  3788. }
  3789. #endif
  3790. /*
  3791. * Create a temp directory for the following tests.
  3792. * Include the time the tests started as part of the name,
  3793. * to make it easier to track the results of multiple tests.
  3794. */
  3795. now = time(NULL);
  3796. for (i = 0; ; i++) {
  3797. #if defined(HAVE_LOCALTIME_S)
  3798. tmptr = localtime_s(&tmbuf, &now) ? NULL : &tmbuf;
  3799. #elif defined(HAVE_LOCALTIME_R)
  3800. tmptr = localtime_r(&now, &tmbuf);
  3801. #else
  3802. tmptr = localtime(&now);
  3803. #endif
  3804. strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
  3805. "%Y-%m-%dT%H.%M.%S", tmptr);
  3806. if ((strlen(tmp) + 1 + strlen(progname) + 1 +
  3807. strlen(tmpdir_timestamp) + 1 + 3) >
  3808. (sizeof(tmpdir) / sizeof(char))) {
  3809. fprintf(stderr,
  3810. "ERROR: Temp directory pathname too long\n");
  3811. exit(1);
  3812. }
  3813. snprintf(tmpdir, sizeof(tmpdir), "%s/%s.%s-%03d", tmp,
  3814. progname, tmpdir_timestamp, i);
  3815. if (assertMakeDir(tmpdir,0755))
  3816. break;
  3817. if (i >= 999) {
  3818. fprintf(stderr,
  3819. "ERROR: Unable to create temp directory %s\n",
  3820. tmpdir);
  3821. exit(1);
  3822. }
  3823. }
  3824. /*
  3825. * If the user didn't specify a directory for locating
  3826. * reference files, try to find the reference files in
  3827. * the "usual places."
  3828. */
  3829. refdir = refdir_alloc = get_refdir(refdir);
  3830. /*
  3831. * Banner with basic information.
  3832. */
  3833. printf("\n");
  3834. printf("If tests fail or crash, details will be in:\n");
  3835. printf(" %s\n", tmpdir);
  3836. printf("\n");
  3837. if (verbosity > VERBOSITY_SUMMARY_ONLY) {
  3838. printf("Reference files will be read from: %s\n", refdir);
  3839. #ifdef PROGRAM
  3840. printf("Running tests on: %s\n", testprog);
  3841. #endif
  3842. printf("Exercising: ");
  3843. fflush(stdout);
  3844. printf("%s\n", EXTRA_VERSION);
  3845. } else {
  3846. printf("Running ");
  3847. fflush(stdout);
  3848. }
  3849. /*
  3850. * Run some or all of the individual tests.
  3851. */
  3852. saved_argv = argv;
  3853. do {
  3854. argv = saved_argv;
  3855. do {
  3856. int test_num;
  3857. test_num = get_test_set(test_set, limit, *argv);
  3858. if (test_num < 0) {
  3859. printf("*** INVALID Test %s\n", *argv);
  3860. free(refdir_alloc);
  3861. free(testprogdir);
  3862. usage(progname);
  3863. return (1);
  3864. }
  3865. for (i = 0; i < test_num; i++) {
  3866. tests_run++;
  3867. if (test_run(test_set[i], tmpdir)) {
  3868. tests_failed++;
  3869. if (until_failure)
  3870. goto finish;
  3871. }
  3872. }
  3873. if (*argv != NULL)
  3874. argv++;
  3875. } while (*argv != NULL);
  3876. } while (until_failure);
  3877. finish:
  3878. /* Must be freed after all tests run */
  3879. free(tmp2);
  3880. free(testprogdir);
  3881. free(pwd);
  3882. /*
  3883. * Report summary statistics.
  3884. */
  3885. if (verbosity > VERBOSITY_SUMMARY_ONLY) {
  3886. printf("\n");
  3887. printf("Totals:\n");
  3888. printf(" Tests run: %8d\n", tests_run);
  3889. printf(" Tests failed: %8d\n", tests_failed);
  3890. printf(" Assertions checked:%8d\n", assertions);
  3891. printf(" Assertions failed: %8d\n", failures);
  3892. printf(" Skips reported: %8d\n", skips);
  3893. }
  3894. if (failures) {
  3895. printf("\n");
  3896. printf("Failing tests:\n");
  3897. for (i = 0; i < limit; ++i) {
  3898. if (tests[i].failures)
  3899. printf(" %d: %s (%d failures)\n", i,
  3900. tests[i].name, tests[i].failures);
  3901. }
  3902. printf("\n");
  3903. printf("Details for failing tests: %s\n", tmpdir);
  3904. printf("\n");
  3905. } else {
  3906. if (verbosity == VERBOSITY_SUMMARY_ONLY)
  3907. printf("\n");
  3908. printf("%d tests passed, no failures\n", tests_run);
  3909. }
  3910. free(refdir_alloc);
  3911. /* If the final tmpdir is empty, we can remove it. */
  3912. /* This should be the usual case when all tests succeed. */
  3913. assertChdir("..");
  3914. rmdir(tmpdir);
  3915. return (tests_failed ? 1 : 0);
  3916. }