runtest.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471
  1. /*
  2. * runtest.c: C program to run libxml2 regression tests without
  3. * requiring make or Python, and reducing platform dependancies
  4. * to a strict minimum.
  5. *
  6. * To compile on Unixes:
  7. * cc -o runtest `xml2-config --cflags` runtest.c `xml2-config --libs` -lpthread
  8. *
  9. * See Copyright for the status of this software.
  10. *
  11. * daniel@veillard.com
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include "libxml.h"
  15. #else
  16. #include <stdio.h>
  17. #endif
  18. #if !defined(_WIN32) || defined(__CYGWIN__)
  19. #include <unistd.h>
  20. #endif
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <libxml/parser.h>
  26. #include <libxml/tree.h>
  27. #include <libxml/uri.h>
  28. #ifdef LIBXML_OUTPUT_ENABLED
  29. #ifdef LIBXML_READER_ENABLED
  30. #include <libxml/xmlreader.h>
  31. #endif
  32. #ifdef LIBXML_XINCLUDE_ENABLED
  33. #include <libxml/xinclude.h>
  34. #endif
  35. #ifdef LIBXML_XPATH_ENABLED
  36. #include <libxml/xpath.h>
  37. #include <libxml/xpathInternals.h>
  38. #ifdef LIBXML_XPTR_ENABLED
  39. #include <libxml/xpointer.h>
  40. #endif
  41. #endif
  42. #ifdef LIBXML_SCHEMAS_ENABLED
  43. #include <libxml/relaxng.h>
  44. #include <libxml/xmlschemas.h>
  45. #include <libxml/xmlschemastypes.h>
  46. #endif
  47. #ifdef LIBXML_PATTERN_ENABLED
  48. #include <libxml/pattern.h>
  49. #endif
  50. #ifdef LIBXML_C14N_ENABLED
  51. #include <libxml/c14n.h>
  52. #endif
  53. #ifdef LIBXML_HTML_ENABLED
  54. #include <libxml/HTMLparser.h>
  55. #include <libxml/HTMLtree.h>
  56. /*
  57. * pseudo flag for the unification of HTML and XML tests
  58. */
  59. #define XML_PARSE_HTML 1 << 24
  60. #endif
  61. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
  62. #include <libxml/globals.h>
  63. #include <libxml/threads.h>
  64. #include <libxml/parser.h>
  65. #include <libxml/catalog.h>
  66. #include <string.h>
  67. #endif
  68. /*
  69. * O_BINARY is just for Windows compatibility - if it isn't defined
  70. * on this system, avoid any compilation error
  71. */
  72. #ifdef O_BINARY
  73. #define RD_FLAGS O_RDONLY | O_BINARY
  74. #else
  75. #define RD_FLAGS O_RDONLY
  76. #endif
  77. typedef int (*functest) (const char *filename, const char *result,
  78. const char *error, int options);
  79. typedef struct testDesc testDesc;
  80. typedef testDesc *testDescPtr;
  81. struct testDesc {
  82. const char *desc; /* descripton of the test */
  83. functest func; /* function implementing the test */
  84. const char *in; /* glob to path for input files */
  85. const char *out; /* output directory */
  86. const char *suffix;/* suffix for output files */
  87. const char *err; /* suffix for error output files */
  88. int options; /* parser options for the test */
  89. };
  90. static int checkTestFile(const char *filename);
  91. #if defined(_WIN32) && !defined(__CYGWIN__)
  92. #include <windows.h>
  93. #include <io.h>
  94. typedef struct
  95. {
  96. size_t gl_pathc; /* Count of paths matched so far */
  97. char **gl_pathv; /* List of matched pathnames. */
  98. size_t gl_offs; /* Slots to reserve in 'gl_pathv'. */
  99. } glob_t;
  100. #define GLOB_DOOFFS 0
  101. static int glob(const char *pattern, int flags,
  102. int errfunc(const char *epath, int eerrno),
  103. glob_t *pglob) {
  104. glob_t *ret;
  105. WIN32_FIND_DATA FindFileData;
  106. HANDLE hFind;
  107. unsigned int nb_paths = 0;
  108. char directory[500];
  109. int len;
  110. if ((pattern == NULL) || (pglob == NULL)) return(-1);
  111. strncpy(directory, pattern, 499);
  112. for (len = strlen(directory);len >= 0;len--) {
  113. if (directory[len] == '/') {
  114. len++;
  115. directory[len] = 0;
  116. break;
  117. }
  118. }
  119. if (len <= 0)
  120. len = 0;
  121. ret = pglob;
  122. memset(ret, 0, sizeof(glob_t));
  123. hFind = FindFirstFileA(pattern, &FindFileData);
  124. if (hFind == INVALID_HANDLE_VALUE)
  125. return(0);
  126. nb_paths = 20;
  127. ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
  128. if (ret->gl_pathv == NULL) {
  129. FindClose(hFind);
  130. return(-1);
  131. }
  132. strncpy(directory + len, FindFileData.cFileName, 499 - len);
  133. ret->gl_pathv[ret->gl_pathc] = strdup(directory);
  134. if (ret->gl_pathv[ret->gl_pathc] == NULL)
  135. goto done;
  136. ret->gl_pathc++;
  137. while(FindNextFileA(hFind, &FindFileData)) {
  138. if (FindFileData.cFileName[0] == '.')
  139. continue;
  140. if (ret->gl_pathc + 2 > nb_paths) {
  141. char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
  142. if (tmp == NULL)
  143. break;
  144. ret->gl_pathv = tmp;
  145. nb_paths *= 2;
  146. }
  147. strncpy(directory + len, FindFileData.cFileName, 499 - len);
  148. ret->gl_pathv[ret->gl_pathc] = strdup(directory);
  149. if (ret->gl_pathv[ret->gl_pathc] == NULL)
  150. break;
  151. ret->gl_pathc++;
  152. }
  153. ret->gl_pathv[ret->gl_pathc] = NULL;
  154. done:
  155. FindClose(hFind);
  156. return(0);
  157. }
  158. static void globfree(glob_t *pglob) {
  159. unsigned int i;
  160. if (pglob == NULL)
  161. return;
  162. for (i = 0;i < pglob->gl_pathc;i++) {
  163. if (pglob->gl_pathv[i] != NULL)
  164. free(pglob->gl_pathv[i]);
  165. }
  166. }
  167. #define vsnprintf _vsnprintf
  168. #define snprintf _snprintf
  169. #else
  170. #include <glob.h>
  171. #endif
  172. /************************************************************************
  173. * *
  174. * Libxml2 specific routines *
  175. * *
  176. ************************************************************************/
  177. static int nb_tests = 0;
  178. static int nb_errors = 0;
  179. static int nb_leaks = 0;
  180. static int extraMemoryFromResolver = 0;
  181. static int
  182. fatalError(void) {
  183. fprintf(stderr, "Exitting tests on fatal error\n");
  184. exit(1);
  185. }
  186. /*
  187. * We need to trap calls to the resolver to not account memory for the catalog
  188. * which is shared to the current running test. We also don't want to have
  189. * network downloads modifying tests.
  190. */
  191. static xmlParserInputPtr
  192. testExternalEntityLoader(const char *URL, const char *ID,
  193. xmlParserCtxtPtr ctxt) {
  194. xmlParserInputPtr ret;
  195. if (checkTestFile(URL)) {
  196. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  197. } else {
  198. int memused = xmlMemUsed();
  199. ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
  200. extraMemoryFromResolver += xmlMemUsed() - memused;
  201. }
  202. return(ret);
  203. }
  204. /*
  205. * Trapping the error messages at the generic level to grab the equivalent of
  206. * stderr messages on CLI tools.
  207. */
  208. static char testErrors[32769];
  209. static int testErrorsSize = 0;
  210. static void XMLCDECL
  211. testErrorHandler(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
  212. va_list args;
  213. int res;
  214. if (testErrorsSize >= 32768)
  215. return;
  216. va_start(args, msg);
  217. res = vsnprintf(&testErrors[testErrorsSize],
  218. 32768 - testErrorsSize,
  219. msg, args);
  220. va_end(args);
  221. if (testErrorsSize + res >= 32768) {
  222. /* buffer is full */
  223. testErrorsSize = 32768;
  224. testErrors[testErrorsSize] = 0;
  225. } else {
  226. testErrorsSize += res;
  227. }
  228. testErrors[testErrorsSize] = 0;
  229. }
  230. static void XMLCDECL
  231. channel(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
  232. va_list args;
  233. int res;
  234. if (testErrorsSize >= 32768)
  235. return;
  236. va_start(args, msg);
  237. res = vsnprintf(&testErrors[testErrorsSize],
  238. 32768 - testErrorsSize,
  239. msg, args);
  240. va_end(args);
  241. if (testErrorsSize + res >= 32768) {
  242. /* buffer is full */
  243. testErrorsSize = 32768;
  244. testErrors[testErrorsSize] = 0;
  245. } else {
  246. testErrorsSize += res;
  247. }
  248. testErrors[testErrorsSize] = 0;
  249. }
  250. /**
  251. * xmlParserPrintFileContext:
  252. * @input: an xmlParserInputPtr input
  253. *
  254. * Displays current context within the input content for error tracking
  255. */
  256. static void
  257. xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
  258. xmlGenericErrorFunc chanl, void *data ) {
  259. const xmlChar *cur, *base;
  260. unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
  261. xmlChar content[81]; /* space for 80 chars + line terminator */
  262. xmlChar *ctnt;
  263. if (input == NULL) return;
  264. cur = input->cur;
  265. base = input->base;
  266. /* skip backwards over any end-of-lines */
  267. while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
  268. cur--;
  269. }
  270. n = 0;
  271. /* search backwards for beginning-of-line (to max buff size) */
  272. while ((n++ < (sizeof(content)-1)) && (cur > base) &&
  273. (*(cur) != '\n') && (*(cur) != '\r'))
  274. cur--;
  275. if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
  276. /* calculate the error position in terms of the current position */
  277. col = input->cur - cur;
  278. /* search forward for end-of-line (to max buff size) */
  279. n = 0;
  280. ctnt = content;
  281. /* copy selected text to our buffer */
  282. while ((*cur != 0) && (*(cur) != '\n') &&
  283. (*(cur) != '\r') && (n < sizeof(content)-1)) {
  284. *ctnt++ = *cur++;
  285. n++;
  286. }
  287. *ctnt = 0;
  288. /* print out the selected text */
  289. chanl(data ,"%s\n", content);
  290. /* create blank line with problem pointer */
  291. n = 0;
  292. ctnt = content;
  293. /* (leave buffer space for pointer + line terminator) */
  294. while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
  295. if (*(ctnt) != '\t')
  296. *(ctnt) = ' ';
  297. ctnt++;
  298. }
  299. *ctnt++ = '^';
  300. *ctnt = 0;
  301. chanl(data ,"%s\n", content);
  302. }
  303. static void
  304. testStructuredErrorHandler(void *ctx ATTRIBUTE_UNUSED, xmlErrorPtr err) {
  305. char *file = NULL;
  306. int line = 0;
  307. int code = -1;
  308. int domain;
  309. void *data = NULL;
  310. const char *str;
  311. const xmlChar *name = NULL;
  312. xmlNodePtr node;
  313. xmlErrorLevel level;
  314. xmlParserInputPtr input = NULL;
  315. xmlParserInputPtr cur = NULL;
  316. xmlParserCtxtPtr ctxt = NULL;
  317. if (err == NULL)
  318. return;
  319. file = err->file;
  320. line = err->line;
  321. code = err->code;
  322. domain = err->domain;
  323. level = err->level;
  324. node = err->node;
  325. if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
  326. (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
  327. (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
  328. ctxt = err->ctxt;
  329. }
  330. str = err->message;
  331. if (code == XML_ERR_OK)
  332. return;
  333. if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
  334. name = node->name;
  335. /*
  336. * Maintain the compatibility with the legacy error handling
  337. */
  338. if (ctxt != NULL) {
  339. input = ctxt->input;
  340. if ((input != NULL) && (input->filename == NULL) &&
  341. (ctxt->inputNr > 1)) {
  342. cur = input;
  343. input = ctxt->inputTab[ctxt->inputNr - 2];
  344. }
  345. if (input != NULL) {
  346. if (input->filename)
  347. channel(data, "%s:%d: ", input->filename, input->line);
  348. else if ((line != 0) && (domain == XML_FROM_PARSER))
  349. channel(data, "Entity: line %d: ", input->line);
  350. }
  351. } else {
  352. if (file != NULL)
  353. channel(data, "%s:%d: ", file, line);
  354. else if ((line != 0) && (domain == XML_FROM_PARSER))
  355. channel(data, "Entity: line %d: ", line);
  356. }
  357. if (name != NULL) {
  358. channel(data, "element %s: ", name);
  359. }
  360. if (code == XML_ERR_OK)
  361. return;
  362. switch (domain) {
  363. case XML_FROM_PARSER:
  364. channel(data, "parser ");
  365. break;
  366. case XML_FROM_NAMESPACE:
  367. channel(data, "namespace ");
  368. break;
  369. case XML_FROM_DTD:
  370. case XML_FROM_VALID:
  371. channel(data, "validity ");
  372. break;
  373. case XML_FROM_HTML:
  374. channel(data, "HTML parser ");
  375. break;
  376. case XML_FROM_MEMORY:
  377. channel(data, "memory ");
  378. break;
  379. case XML_FROM_OUTPUT:
  380. channel(data, "output ");
  381. break;
  382. case XML_FROM_IO:
  383. channel(data, "I/O ");
  384. break;
  385. case XML_FROM_XINCLUDE:
  386. channel(data, "XInclude ");
  387. break;
  388. case XML_FROM_XPATH:
  389. channel(data, "XPath ");
  390. break;
  391. case XML_FROM_XPOINTER:
  392. channel(data, "parser ");
  393. break;
  394. case XML_FROM_REGEXP:
  395. channel(data, "regexp ");
  396. break;
  397. case XML_FROM_MODULE:
  398. channel(data, "module ");
  399. break;
  400. case XML_FROM_SCHEMASV:
  401. channel(data, "Schemas validity ");
  402. break;
  403. case XML_FROM_SCHEMASP:
  404. channel(data, "Schemas parser ");
  405. break;
  406. case XML_FROM_RELAXNGP:
  407. channel(data, "Relax-NG parser ");
  408. break;
  409. case XML_FROM_RELAXNGV:
  410. channel(data, "Relax-NG validity ");
  411. break;
  412. case XML_FROM_CATALOG:
  413. channel(data, "Catalog ");
  414. break;
  415. case XML_FROM_C14N:
  416. channel(data, "C14N ");
  417. break;
  418. case XML_FROM_XSLT:
  419. channel(data, "XSLT ");
  420. break;
  421. default:
  422. break;
  423. }
  424. if (code == XML_ERR_OK)
  425. return;
  426. switch (level) {
  427. case XML_ERR_NONE:
  428. channel(data, ": ");
  429. break;
  430. case XML_ERR_WARNING:
  431. channel(data, "warning : ");
  432. break;
  433. case XML_ERR_ERROR:
  434. channel(data, "error : ");
  435. break;
  436. case XML_ERR_FATAL:
  437. channel(data, "error : ");
  438. break;
  439. }
  440. if (code == XML_ERR_OK)
  441. return;
  442. if (str != NULL) {
  443. int len;
  444. len = xmlStrlen((const xmlChar *)str);
  445. if ((len > 0) && (str[len - 1] != '\n'))
  446. channel(data, "%s\n", str);
  447. else
  448. channel(data, "%s", str);
  449. } else {
  450. channel(data, "%s\n", "out of memory error");
  451. }
  452. if (code == XML_ERR_OK)
  453. return;
  454. if (ctxt != NULL) {
  455. xmlParserPrintFileContextInternal(input, channel, data);
  456. if (cur != NULL) {
  457. if (cur->filename)
  458. channel(data, "%s:%d: \n", cur->filename, cur->line);
  459. else if ((line != 0) && (domain == XML_FROM_PARSER))
  460. channel(data, "Entity: line %d: \n", cur->line);
  461. xmlParserPrintFileContextInternal(cur, channel, data);
  462. }
  463. }
  464. if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
  465. (err->int1 < 100) &&
  466. (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
  467. xmlChar buf[150];
  468. int i;
  469. channel(data, "%s\n", err->str1);
  470. for (i=0;i < err->int1;i++)
  471. buf[i] = ' ';
  472. buf[i++] = '^';
  473. buf[i] = 0;
  474. channel(data, "%s\n", buf);
  475. }
  476. }
  477. static void
  478. initializeLibxml2(void) {
  479. xmlGetWarningsDefaultValue = 0;
  480. xmlPedanticParserDefault(0);
  481. xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
  482. xmlInitParser();
  483. xmlSetExternalEntityLoader(testExternalEntityLoader);
  484. xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
  485. #ifdef LIBXML_SCHEMAS_ENABLED
  486. xmlSchemaInitTypes();
  487. xmlRelaxNGInitTypes();
  488. #endif
  489. }
  490. /************************************************************************
  491. * *
  492. * File name and path utilities *
  493. * *
  494. ************************************************************************/
  495. static const char *baseFilename(const char *filename) {
  496. const char *cur;
  497. if (filename == NULL)
  498. return(NULL);
  499. cur = &filename[strlen(filename)];
  500. while ((cur > filename) && (*cur != '/'))
  501. cur--;
  502. if (*cur == '/')
  503. return(cur + 1);
  504. return(cur);
  505. }
  506. static char *resultFilename(const char *filename, const char *out,
  507. const char *suffix) {
  508. const char *base;
  509. char res[500];
  510. char suffixbuff[500];
  511. /*************
  512. if ((filename[0] == 't') && (filename[1] == 'e') &&
  513. (filename[2] == 's') && (filename[3] == 't') &&
  514. (filename[4] == '/'))
  515. filename = &filename[5];
  516. *************/
  517. base = baseFilename(filename);
  518. if (suffix == NULL)
  519. suffix = ".tmp";
  520. if (out == NULL)
  521. out = "";
  522. strncpy(suffixbuff,suffix,499);
  523. #ifdef VMS
  524. if(strstr(base,".") && suffixbuff[0]=='.')
  525. suffixbuff[0]='_';
  526. #endif
  527. snprintf(res, 499, "%s%s%s", out, base, suffixbuff);
  528. res[499] = 0;
  529. return(strdup(res));
  530. }
  531. static int checkTestFile(const char *filename) {
  532. struct stat buf;
  533. if (stat(filename, &buf) == -1)
  534. return(0);
  535. #if defined(_WIN32) && !defined(__CYGWIN__)
  536. if (!(buf.st_mode & _S_IFREG))
  537. return(0);
  538. #else
  539. if (!S_ISREG(buf.st_mode))
  540. return(0);
  541. #endif
  542. return(1);
  543. }
  544. static int compareFiles(const char *r1, const char *r2) {
  545. int res1, res2;
  546. int fd1, fd2;
  547. char bytes1[4096];
  548. char bytes2[4096];
  549. fd1 = open(r1, RD_FLAGS);
  550. if (fd1 < 0)
  551. return(-1);
  552. fd2 = open(r2, RD_FLAGS);
  553. if (fd2 < 0) {
  554. close(fd1);
  555. return(-1);
  556. }
  557. while (1) {
  558. res1 = read(fd1, bytes1, 4096);
  559. res2 = read(fd2, bytes2, 4096);
  560. if ((res1 != res2) || (res1 < 0)) {
  561. close(fd1);
  562. close(fd2);
  563. return(1);
  564. }
  565. if (res1 == 0)
  566. break;
  567. if (memcmp(bytes1, bytes2, res1) != 0) {
  568. close(fd1);
  569. close(fd2);
  570. return(1);
  571. }
  572. }
  573. close(fd1);
  574. close(fd2);
  575. return(0);
  576. }
  577. static int compareFileMem(const char *filename, const char *mem, int size) {
  578. int res;
  579. int fd;
  580. char bytes[4096];
  581. int idx = 0;
  582. struct stat info;
  583. if (stat(filename, &info) < 0)
  584. return(-1);
  585. if (info.st_size != size)
  586. return(-1);
  587. fd = open(filename, RD_FLAGS);
  588. if (fd < 0)
  589. return(-1);
  590. while (idx < size) {
  591. res = read(fd, bytes, 4096);
  592. if (res <= 0)
  593. break;
  594. if (res + idx > size)
  595. break;
  596. if (memcmp(bytes, &mem[idx], res) != 0) {
  597. int ix;
  598. for (ix=0; ix<res; ix++)
  599. if (bytes[ix] != mem[idx+ix])
  600. break;
  601. fprintf(stderr,"Compare error at position %d\n", idx+ix);
  602. close(fd);
  603. return(1);
  604. }
  605. idx += res;
  606. }
  607. close(fd);
  608. return(idx != size);
  609. }
  610. static int loadMem(const char *filename, const char **mem, int *size) {
  611. int fd, res;
  612. struct stat info;
  613. char *base;
  614. int siz = 0;
  615. if (stat(filename, &info) < 0)
  616. return(-1);
  617. base = malloc(info.st_size + 1);
  618. if (base == NULL)
  619. return(-1);
  620. if ((fd = open(filename, RD_FLAGS)) < 0) {
  621. free(base);
  622. return(-1);
  623. }
  624. while ((res = read(fd, &base[siz], info.st_size - siz)) > 0) {
  625. siz += res;
  626. }
  627. close(fd);
  628. #if !defined(_WIN32)
  629. if (siz != info.st_size) {
  630. free(base);
  631. return(-1);
  632. }
  633. #endif
  634. base[siz] = 0;
  635. *mem = base;
  636. *size = siz;
  637. return(0);
  638. }
  639. static int unloadMem(const char *mem) {
  640. free((char *)mem);
  641. return(0);
  642. }
  643. /************************************************************************
  644. * *
  645. * Tests implementations *
  646. * *
  647. ************************************************************************/
  648. /************************************************************************
  649. * *
  650. * Parse to SAX based tests *
  651. * *
  652. ************************************************************************/
  653. static FILE *SAXdebug = NULL;
  654. /*
  655. * empty SAX block
  656. */
  657. static xmlSAXHandler emptySAXHandlerStruct = {
  658. NULL, /* internalSubset */
  659. NULL, /* isStandalone */
  660. NULL, /* hasInternalSubset */
  661. NULL, /* hasExternalSubset */
  662. NULL, /* resolveEntity */
  663. NULL, /* getEntity */
  664. NULL, /* entityDecl */
  665. NULL, /* notationDecl */
  666. NULL, /* attributeDecl */
  667. NULL, /* elementDecl */
  668. NULL, /* unparsedEntityDecl */
  669. NULL, /* setDocumentLocator */
  670. NULL, /* startDocument */
  671. NULL, /* endDocument */
  672. NULL, /* startElement */
  673. NULL, /* endElement */
  674. NULL, /* reference */
  675. NULL, /* characters */
  676. NULL, /* ignorableWhitespace */
  677. NULL, /* processingInstruction */
  678. NULL, /* comment */
  679. NULL, /* xmlParserWarning */
  680. NULL, /* xmlParserError */
  681. NULL, /* xmlParserError */
  682. NULL, /* getParameterEntity */
  683. NULL, /* cdataBlock; */
  684. NULL, /* externalSubset; */
  685. 1,
  686. NULL,
  687. NULL, /* startElementNs */
  688. NULL, /* endElementNs */
  689. NULL /* xmlStructuredErrorFunc */
  690. };
  691. static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
  692. static int callbacks = 0;
  693. static int quiet = 0;
  694. /**
  695. * isStandaloneDebug:
  696. * @ctxt: An XML parser context
  697. *
  698. * Is this document tagged standalone ?
  699. *
  700. * Returns 1 if true
  701. */
  702. static int
  703. isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
  704. {
  705. callbacks++;
  706. if (quiet)
  707. return(0);
  708. fprintf(SAXdebug, "SAX.isStandalone()\n");
  709. return(0);
  710. }
  711. /**
  712. * hasInternalSubsetDebug:
  713. * @ctxt: An XML parser context
  714. *
  715. * Does this document has an internal subset
  716. *
  717. * Returns 1 if true
  718. */
  719. static int
  720. hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
  721. {
  722. callbacks++;
  723. if (quiet)
  724. return(0);
  725. fprintf(SAXdebug, "SAX.hasInternalSubset()\n");
  726. return(0);
  727. }
  728. /**
  729. * hasExternalSubsetDebug:
  730. * @ctxt: An XML parser context
  731. *
  732. * Does this document has an external subset
  733. *
  734. * Returns 1 if true
  735. */
  736. static int
  737. hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
  738. {
  739. callbacks++;
  740. if (quiet)
  741. return(0);
  742. fprintf(SAXdebug, "SAX.hasExternalSubset()\n");
  743. return(0);
  744. }
  745. /**
  746. * internalSubsetDebug:
  747. * @ctxt: An XML parser context
  748. *
  749. * Does this document has an internal subset
  750. */
  751. static void
  752. internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  753. const xmlChar *ExternalID, const xmlChar *SystemID)
  754. {
  755. callbacks++;
  756. if (quiet)
  757. return;
  758. fprintf(SAXdebug, "SAX.internalSubset(%s,", name);
  759. if (ExternalID == NULL)
  760. fprintf(SAXdebug, " ,");
  761. else
  762. fprintf(SAXdebug, " %s,", ExternalID);
  763. if (SystemID == NULL)
  764. fprintf(SAXdebug, " )\n");
  765. else
  766. fprintf(SAXdebug, " %s)\n", SystemID);
  767. }
  768. /**
  769. * externalSubsetDebug:
  770. * @ctxt: An XML parser context
  771. *
  772. * Does this document has an external subset
  773. */
  774. static void
  775. externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  776. const xmlChar *ExternalID, const xmlChar *SystemID)
  777. {
  778. callbacks++;
  779. if (quiet)
  780. return;
  781. fprintf(SAXdebug, "SAX.externalSubset(%s,", name);
  782. if (ExternalID == NULL)
  783. fprintf(SAXdebug, " ,");
  784. else
  785. fprintf(SAXdebug, " %s,", ExternalID);
  786. if (SystemID == NULL)
  787. fprintf(SAXdebug, " )\n");
  788. else
  789. fprintf(SAXdebug, " %s)\n", SystemID);
  790. }
  791. /**
  792. * resolveEntityDebug:
  793. * @ctxt: An XML parser context
  794. * @publicId: The public ID of the entity
  795. * @systemId: The system ID of the entity
  796. *
  797. * Special entity resolver, better left to the parser, it has
  798. * more context than the application layer.
  799. * The default behaviour is to NOT resolve the entities, in that case
  800. * the ENTITY_REF nodes are built in the structure (and the parameter
  801. * values).
  802. *
  803. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  804. */
  805. static xmlParserInputPtr
  806. resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
  807. {
  808. callbacks++;
  809. if (quiet)
  810. return(NULL);
  811. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  812. fprintf(SAXdebug, "SAX.resolveEntity(");
  813. if (publicId != NULL)
  814. fprintf(SAXdebug, "%s", (char *)publicId);
  815. else
  816. fprintf(SAXdebug, " ");
  817. if (systemId != NULL)
  818. fprintf(SAXdebug, ", %s)\n", (char *)systemId);
  819. else
  820. fprintf(SAXdebug, ", )\n");
  821. /*********
  822. if (systemId != NULL) {
  823. return(xmlNewInputFromFile(ctxt, (char *) systemId));
  824. }
  825. *********/
  826. return(NULL);
  827. }
  828. /**
  829. * getEntityDebug:
  830. * @ctxt: An XML parser context
  831. * @name: The entity name
  832. *
  833. * Get an entity by name
  834. *
  835. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  836. */
  837. static xmlEntityPtr
  838. getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  839. {
  840. callbacks++;
  841. if (quiet)
  842. return(NULL);
  843. fprintf(SAXdebug, "SAX.getEntity(%s)\n", name);
  844. return(NULL);
  845. }
  846. /**
  847. * getParameterEntityDebug:
  848. * @ctxt: An XML parser context
  849. * @name: The entity name
  850. *
  851. * Get a parameter entity by name
  852. *
  853. * Returns the xmlParserInputPtr
  854. */
  855. static xmlEntityPtr
  856. getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  857. {
  858. callbacks++;
  859. if (quiet)
  860. return(NULL);
  861. fprintf(SAXdebug, "SAX.getParameterEntity(%s)\n", name);
  862. return(NULL);
  863. }
  864. /**
  865. * entityDeclDebug:
  866. * @ctxt: An XML parser context
  867. * @name: the entity name
  868. * @type: the entity type
  869. * @publicId: The public ID of the entity
  870. * @systemId: The system ID of the entity
  871. * @content: the entity value (without processing).
  872. *
  873. * An entity definition has been parsed
  874. */
  875. static void
  876. entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
  877. const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
  878. {
  879. const xmlChar *nullstr = BAD_CAST "(null)";
  880. /* not all libraries handle printing null pointers nicely */
  881. if (publicId == NULL)
  882. publicId = nullstr;
  883. if (systemId == NULL)
  884. systemId = nullstr;
  885. if (content == NULL)
  886. content = (xmlChar *)nullstr;
  887. callbacks++;
  888. if (quiet)
  889. return;
  890. fprintf(SAXdebug, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
  891. name, type, publicId, systemId, content);
  892. }
  893. /**
  894. * attributeDeclDebug:
  895. * @ctxt: An XML parser context
  896. * @name: the attribute name
  897. * @type: the attribute type
  898. *
  899. * An attribute definition has been parsed
  900. */
  901. static void
  902. attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
  903. const xmlChar * name, int type, int def,
  904. const xmlChar * defaultValue, xmlEnumerationPtr tree)
  905. {
  906. callbacks++;
  907. if (quiet)
  908. return;
  909. if (defaultValue == NULL)
  910. fprintf(SAXdebug, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
  911. elem, name, type, def);
  912. else
  913. fprintf(SAXdebug, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
  914. elem, name, type, def, defaultValue);
  915. xmlFreeEnumeration(tree);
  916. }
  917. /**
  918. * elementDeclDebug:
  919. * @ctxt: An XML parser context
  920. * @name: the element name
  921. * @type: the element type
  922. * @content: the element value (without processing).
  923. *
  924. * An element definition has been parsed
  925. */
  926. static void
  927. elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
  928. xmlElementContentPtr content ATTRIBUTE_UNUSED)
  929. {
  930. callbacks++;
  931. if (quiet)
  932. return;
  933. fprintf(SAXdebug, "SAX.elementDecl(%s, %d, ...)\n",
  934. name, type);
  935. }
  936. /**
  937. * notationDeclDebug:
  938. * @ctxt: An XML parser context
  939. * @name: The name of the notation
  940. * @publicId: The public ID of the entity
  941. * @systemId: The system ID of the entity
  942. *
  943. * What to do when a notation declaration has been parsed.
  944. */
  945. static void
  946. notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  947. const xmlChar *publicId, const xmlChar *systemId)
  948. {
  949. callbacks++;
  950. if (quiet)
  951. return;
  952. fprintf(SAXdebug, "SAX.notationDecl(%s, %s, %s)\n",
  953. (char *) name, (char *) publicId, (char *) systemId);
  954. }
  955. /**
  956. * unparsedEntityDeclDebug:
  957. * @ctxt: An XML parser context
  958. * @name: The name of the entity
  959. * @publicId: The public ID of the entity
  960. * @systemId: The system ID of the entity
  961. * @notationName: the name of the notation
  962. *
  963. * What to do when an unparsed entity declaration is parsed
  964. */
  965. static void
  966. unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  967. const xmlChar *publicId, const xmlChar *systemId,
  968. const xmlChar *notationName)
  969. {
  970. const xmlChar *nullstr = BAD_CAST "(null)";
  971. if (publicId == NULL)
  972. publicId = nullstr;
  973. if (systemId == NULL)
  974. systemId = nullstr;
  975. if (notationName == NULL)
  976. notationName = nullstr;
  977. callbacks++;
  978. if (quiet)
  979. return;
  980. fprintf(SAXdebug, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
  981. (char *) name, (char *) publicId, (char *) systemId,
  982. (char *) notationName);
  983. }
  984. /**
  985. * setDocumentLocatorDebug:
  986. * @ctxt: An XML parser context
  987. * @loc: A SAX Locator
  988. *
  989. * Receive the document locator at startup, actually xmlDefaultSAXLocator
  990. * Everything is available on the context, so this is useless in our case.
  991. */
  992. static void
  993. setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
  994. {
  995. callbacks++;
  996. if (quiet)
  997. return;
  998. fprintf(SAXdebug, "SAX.setDocumentLocator()\n");
  999. }
  1000. /**
  1001. * startDocumentDebug:
  1002. * @ctxt: An XML parser context
  1003. *
  1004. * called when the document start being processed.
  1005. */
  1006. static void
  1007. startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
  1008. {
  1009. callbacks++;
  1010. if (quiet)
  1011. return;
  1012. fprintf(SAXdebug, "SAX.startDocument()\n");
  1013. }
  1014. /**
  1015. * endDocumentDebug:
  1016. * @ctxt: An XML parser context
  1017. *
  1018. * called when the document end has been detected.
  1019. */
  1020. static void
  1021. endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
  1022. {
  1023. callbacks++;
  1024. if (quiet)
  1025. return;
  1026. fprintf(SAXdebug, "SAX.endDocument()\n");
  1027. }
  1028. /**
  1029. * startElementDebug:
  1030. * @ctxt: An XML parser context
  1031. * @name: The element name
  1032. *
  1033. * called when an opening tag has been processed.
  1034. */
  1035. static void
  1036. startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
  1037. {
  1038. int i;
  1039. callbacks++;
  1040. if (quiet)
  1041. return;
  1042. fprintf(SAXdebug, "SAX.startElement(%s", (char *) name);
  1043. if (atts != NULL) {
  1044. for (i = 0;(atts[i] != NULL);i++) {
  1045. fprintf(SAXdebug, ", %s='", atts[i++]);
  1046. if (atts[i] != NULL)
  1047. fprintf(SAXdebug, "%s'", atts[i]);
  1048. }
  1049. }
  1050. fprintf(SAXdebug, ")\n");
  1051. }
  1052. /**
  1053. * endElementDebug:
  1054. * @ctxt: An XML parser context
  1055. * @name: The element name
  1056. *
  1057. * called when the end of an element has been detected.
  1058. */
  1059. static void
  1060. endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  1061. {
  1062. callbacks++;
  1063. if (quiet)
  1064. return;
  1065. fprintf(SAXdebug, "SAX.endElement(%s)\n", (char *) name);
  1066. }
  1067. /**
  1068. * charactersDebug:
  1069. * @ctxt: An XML parser context
  1070. * @ch: a xmlChar string
  1071. * @len: the number of xmlChar
  1072. *
  1073. * receiving some chars from the parser.
  1074. * Question: how much at a time ???
  1075. */
  1076. static void
  1077. charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
  1078. {
  1079. char output[40];
  1080. int i;
  1081. callbacks++;
  1082. if (quiet)
  1083. return;
  1084. for (i = 0;(i<len) && (i < 30);i++)
  1085. output[i] = ch[i];
  1086. output[i] = 0;
  1087. fprintf(SAXdebug, "SAX.characters(%s, %d)\n", output, len);
  1088. }
  1089. /**
  1090. * referenceDebug:
  1091. * @ctxt: An XML parser context
  1092. * @name: The entity name
  1093. *
  1094. * called when an entity reference is detected.
  1095. */
  1096. static void
  1097. referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  1098. {
  1099. callbacks++;
  1100. if (quiet)
  1101. return;
  1102. fprintf(SAXdebug, "SAX.reference(%s)\n", name);
  1103. }
  1104. /**
  1105. * ignorableWhitespaceDebug:
  1106. * @ctxt: An XML parser context
  1107. * @ch: a xmlChar string
  1108. * @start: the first char in the string
  1109. * @len: the number of xmlChar
  1110. *
  1111. * receiving some ignorable whitespaces from the parser.
  1112. * Question: how much at a time ???
  1113. */
  1114. static void
  1115. ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
  1116. {
  1117. char output[40];
  1118. int i;
  1119. callbacks++;
  1120. if (quiet)
  1121. return;
  1122. for (i = 0;(i<len) && (i < 30);i++)
  1123. output[i] = ch[i];
  1124. output[i] = 0;
  1125. fprintf(SAXdebug, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
  1126. }
  1127. /**
  1128. * processingInstructionDebug:
  1129. * @ctxt: An XML parser context
  1130. * @target: the target name
  1131. * @data: the PI data's
  1132. * @len: the number of xmlChar
  1133. *
  1134. * A processing instruction has been parsed.
  1135. */
  1136. static void
  1137. processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
  1138. const xmlChar *data)
  1139. {
  1140. callbacks++;
  1141. if (quiet)
  1142. return;
  1143. if (data != NULL)
  1144. fprintf(SAXdebug, "SAX.processingInstruction(%s, %s)\n",
  1145. (char *) target, (char *) data);
  1146. else
  1147. fprintf(SAXdebug, "SAX.processingInstruction(%s, NULL)\n",
  1148. (char *) target);
  1149. }
  1150. /**
  1151. * cdataBlockDebug:
  1152. * @ctx: the user data (XML parser context)
  1153. * @value: The pcdata content
  1154. * @len: the block length
  1155. *
  1156. * called when a pcdata block has been parsed
  1157. */
  1158. static void
  1159. cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
  1160. {
  1161. callbacks++;
  1162. if (quiet)
  1163. return;
  1164. fprintf(SAXdebug, "SAX.pcdata(%.20s, %d)\n",
  1165. (char *) value, len);
  1166. }
  1167. /**
  1168. * commentDebug:
  1169. * @ctxt: An XML parser context
  1170. * @value: the comment content
  1171. *
  1172. * A comment has been parsed.
  1173. */
  1174. static void
  1175. commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
  1176. {
  1177. callbacks++;
  1178. if (quiet)
  1179. return;
  1180. fprintf(SAXdebug, "SAX.comment(%s)\n", value);
  1181. }
  1182. /**
  1183. * warningDebug:
  1184. * @ctxt: An XML parser context
  1185. * @msg: the message to display/transmit
  1186. * @...: extra parameters for the message display
  1187. *
  1188. * Display and format a warning messages, gives file, line, position and
  1189. * extra parameters.
  1190. */
  1191. static void XMLCDECL
  1192. warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  1193. {
  1194. va_list args;
  1195. callbacks++;
  1196. if (quiet)
  1197. return;
  1198. va_start(args, msg);
  1199. fprintf(SAXdebug, "SAX.warning: ");
  1200. vfprintf(SAXdebug, msg, args);
  1201. va_end(args);
  1202. }
  1203. /**
  1204. * errorDebug:
  1205. * @ctxt: An XML parser context
  1206. * @msg: the message to display/transmit
  1207. * @...: extra parameters for the message display
  1208. *
  1209. * Display and format a error messages, gives file, line, position and
  1210. * extra parameters.
  1211. */
  1212. static void XMLCDECL
  1213. errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  1214. {
  1215. va_list args;
  1216. callbacks++;
  1217. if (quiet)
  1218. return;
  1219. va_start(args, msg);
  1220. fprintf(SAXdebug, "SAX.error: ");
  1221. vfprintf(SAXdebug, msg, args);
  1222. va_end(args);
  1223. }
  1224. /**
  1225. * fatalErrorDebug:
  1226. * @ctxt: An XML parser context
  1227. * @msg: the message to display/transmit
  1228. * @...: extra parameters for the message display
  1229. *
  1230. * Display and format a fatalError messages, gives file, line, position and
  1231. * extra parameters.
  1232. */
  1233. static void XMLCDECL
  1234. fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  1235. {
  1236. va_list args;
  1237. callbacks++;
  1238. if (quiet)
  1239. return;
  1240. va_start(args, msg);
  1241. fprintf(SAXdebug, "SAX.fatalError: ");
  1242. vfprintf(SAXdebug, msg, args);
  1243. va_end(args);
  1244. }
  1245. static xmlSAXHandler debugSAXHandlerStruct = {
  1246. internalSubsetDebug,
  1247. isStandaloneDebug,
  1248. hasInternalSubsetDebug,
  1249. hasExternalSubsetDebug,
  1250. resolveEntityDebug,
  1251. getEntityDebug,
  1252. entityDeclDebug,
  1253. notationDeclDebug,
  1254. attributeDeclDebug,
  1255. elementDeclDebug,
  1256. unparsedEntityDeclDebug,
  1257. setDocumentLocatorDebug,
  1258. startDocumentDebug,
  1259. endDocumentDebug,
  1260. startElementDebug,
  1261. endElementDebug,
  1262. referenceDebug,
  1263. charactersDebug,
  1264. ignorableWhitespaceDebug,
  1265. processingInstructionDebug,
  1266. commentDebug,
  1267. warningDebug,
  1268. errorDebug,
  1269. fatalErrorDebug,
  1270. getParameterEntityDebug,
  1271. cdataBlockDebug,
  1272. externalSubsetDebug,
  1273. 1,
  1274. NULL,
  1275. NULL,
  1276. NULL,
  1277. NULL
  1278. };
  1279. static xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
  1280. /*
  1281. * SAX2 specific callbacks
  1282. */
  1283. /**
  1284. * startElementNsDebug:
  1285. * @ctxt: An XML parser context
  1286. * @name: The element name
  1287. *
  1288. * called when an opening tag has been processed.
  1289. */
  1290. static void
  1291. startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
  1292. const xmlChar *localname,
  1293. const xmlChar *prefix,
  1294. const xmlChar *URI,
  1295. int nb_namespaces,
  1296. const xmlChar **namespaces,
  1297. int nb_attributes,
  1298. int nb_defaulted,
  1299. const xmlChar **attributes)
  1300. {
  1301. int i;
  1302. callbacks++;
  1303. if (quiet)
  1304. return;
  1305. fprintf(SAXdebug, "SAX.startElementNs(%s", (char *) localname);
  1306. if (prefix == NULL)
  1307. fprintf(SAXdebug, ", NULL");
  1308. else
  1309. fprintf(SAXdebug, ", %s", (char *) prefix);
  1310. if (URI == NULL)
  1311. fprintf(SAXdebug, ", NULL");
  1312. else
  1313. fprintf(SAXdebug, ", '%s'", (char *) URI);
  1314. fprintf(SAXdebug, ", %d", nb_namespaces);
  1315. if (namespaces != NULL) {
  1316. for (i = 0;i < nb_namespaces * 2;i++) {
  1317. fprintf(SAXdebug, ", xmlns");
  1318. if (namespaces[i] != NULL)
  1319. fprintf(SAXdebug, ":%s", namespaces[i]);
  1320. i++;
  1321. fprintf(SAXdebug, "='%s'", namespaces[i]);
  1322. }
  1323. }
  1324. fprintf(SAXdebug, ", %d, %d", nb_attributes, nb_defaulted);
  1325. if (attributes != NULL) {
  1326. for (i = 0;i < nb_attributes * 5;i += 5) {
  1327. if (attributes[i + 1] != NULL)
  1328. fprintf(SAXdebug, ", %s:%s='", attributes[i + 1], attributes[i]);
  1329. else
  1330. fprintf(SAXdebug, ", %s='", attributes[i]);
  1331. fprintf(SAXdebug, "%.4s...', %d", attributes[i + 3],
  1332. (int)(attributes[i + 4] - attributes[i + 3]));
  1333. }
  1334. }
  1335. fprintf(SAXdebug, ")\n");
  1336. }
  1337. /**
  1338. * endElementDebug:
  1339. * @ctxt: An XML parser context
  1340. * @name: The element name
  1341. *
  1342. * called when the end of an element has been detected.
  1343. */
  1344. static void
  1345. endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
  1346. const xmlChar *localname,
  1347. const xmlChar *prefix,
  1348. const xmlChar *URI)
  1349. {
  1350. callbacks++;
  1351. if (quiet)
  1352. return;
  1353. fprintf(SAXdebug, "SAX.endElementNs(%s", (char *) localname);
  1354. if (prefix == NULL)
  1355. fprintf(SAXdebug, ", NULL");
  1356. else
  1357. fprintf(SAXdebug, ", %s", (char *) prefix);
  1358. if (URI == NULL)
  1359. fprintf(SAXdebug, ", NULL)\n");
  1360. else
  1361. fprintf(SAXdebug, ", '%s')\n", (char *) URI);
  1362. }
  1363. static xmlSAXHandler debugSAX2HandlerStruct = {
  1364. internalSubsetDebug,
  1365. isStandaloneDebug,
  1366. hasInternalSubsetDebug,
  1367. hasExternalSubsetDebug,
  1368. resolveEntityDebug,
  1369. getEntityDebug,
  1370. entityDeclDebug,
  1371. notationDeclDebug,
  1372. attributeDeclDebug,
  1373. elementDeclDebug,
  1374. unparsedEntityDeclDebug,
  1375. setDocumentLocatorDebug,
  1376. startDocumentDebug,
  1377. endDocumentDebug,
  1378. NULL,
  1379. NULL,
  1380. referenceDebug,
  1381. charactersDebug,
  1382. ignorableWhitespaceDebug,
  1383. processingInstructionDebug,
  1384. commentDebug,
  1385. warningDebug,
  1386. errorDebug,
  1387. fatalErrorDebug,
  1388. getParameterEntityDebug,
  1389. cdataBlockDebug,
  1390. externalSubsetDebug,
  1391. XML_SAX2_MAGIC,
  1392. NULL,
  1393. startElementNsDebug,
  1394. endElementNsDebug,
  1395. NULL
  1396. };
  1397. static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
  1398. #ifdef LIBXML_HTML_ENABLED
  1399. /**
  1400. * htmlstartElementDebug:
  1401. * @ctxt: An XML parser context
  1402. * @name: The element name
  1403. *
  1404. * called when an opening tag has been processed.
  1405. */
  1406. static void
  1407. htmlstartElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
  1408. {
  1409. int i;
  1410. fprintf(SAXdebug, "SAX.startElement(%s", (char *) name);
  1411. if (atts != NULL) {
  1412. for (i = 0;(atts[i] != NULL);i++) {
  1413. fprintf(SAXdebug, ", %s", atts[i++]);
  1414. if (atts[i] != NULL) {
  1415. unsigned char output[40];
  1416. const unsigned char *att = atts[i];
  1417. int outlen, attlen;
  1418. fprintf(SAXdebug, "='");
  1419. while ((attlen = strlen((char*)att)) > 0) {
  1420. outlen = sizeof output - 1;
  1421. htmlEncodeEntities(output, &outlen, att, &attlen, '\'');
  1422. output[outlen] = 0;
  1423. fprintf(SAXdebug, "%s", (char *) output);
  1424. att += attlen;
  1425. }
  1426. fprintf(SAXdebug, "'");
  1427. }
  1428. }
  1429. }
  1430. fprintf(SAXdebug, ")\n");
  1431. }
  1432. /**
  1433. * htmlcharactersDebug:
  1434. * @ctxt: An XML parser context
  1435. * @ch: a xmlChar string
  1436. * @len: the number of xmlChar
  1437. *
  1438. * receiving some chars from the parser.
  1439. * Question: how much at a time ???
  1440. */
  1441. static void
  1442. htmlcharactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
  1443. {
  1444. unsigned char output[40];
  1445. int inlen = len, outlen = 30;
  1446. htmlEncodeEntities(output, &outlen, ch, &inlen, 0);
  1447. output[outlen] = 0;
  1448. fprintf(SAXdebug, "SAX.characters(%s, %d)\n", output, len);
  1449. }
  1450. /**
  1451. * htmlcdataDebug:
  1452. * @ctxt: An XML parser context
  1453. * @ch: a xmlChar string
  1454. * @len: the number of xmlChar
  1455. *
  1456. * receiving some cdata chars from the parser.
  1457. * Question: how much at a time ???
  1458. */
  1459. static void
  1460. htmlcdataDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
  1461. {
  1462. unsigned char output[40];
  1463. int inlen = len, outlen = 30;
  1464. htmlEncodeEntities(output, &outlen, ch, &inlen, 0);
  1465. output[outlen] = 0;
  1466. fprintf(SAXdebug, "SAX.cdata(%s, %d)\n", output, len);
  1467. }
  1468. static xmlSAXHandler debugHTMLSAXHandlerStruct = {
  1469. internalSubsetDebug,
  1470. isStandaloneDebug,
  1471. hasInternalSubsetDebug,
  1472. hasExternalSubsetDebug,
  1473. resolveEntityDebug,
  1474. getEntityDebug,
  1475. entityDeclDebug,
  1476. notationDeclDebug,
  1477. attributeDeclDebug,
  1478. elementDeclDebug,
  1479. unparsedEntityDeclDebug,
  1480. setDocumentLocatorDebug,
  1481. startDocumentDebug,
  1482. endDocumentDebug,
  1483. htmlstartElementDebug,
  1484. endElementDebug,
  1485. referenceDebug,
  1486. htmlcharactersDebug,
  1487. ignorableWhitespaceDebug,
  1488. processingInstructionDebug,
  1489. commentDebug,
  1490. warningDebug,
  1491. errorDebug,
  1492. fatalErrorDebug,
  1493. getParameterEntityDebug,
  1494. htmlcdataDebug,
  1495. externalSubsetDebug,
  1496. 1,
  1497. NULL,
  1498. NULL,
  1499. NULL,
  1500. NULL
  1501. };
  1502. static xmlSAXHandlerPtr debugHTMLSAXHandler = &debugHTMLSAXHandlerStruct;
  1503. #endif /* LIBXML_HTML_ENABLED */
  1504. #ifdef LIBXML_SAX1_ENABLED
  1505. /**
  1506. * saxParseTest:
  1507. * @filename: the file to parse
  1508. * @result: the file with expected result
  1509. * @err: the file with error messages
  1510. *
  1511. * Parse a file using the SAX API and check for errors.
  1512. *
  1513. * Returns 0 in case of success, an error code otherwise
  1514. */
  1515. static int
  1516. saxParseTest(const char *filename, const char *result,
  1517. const char *err ATTRIBUTE_UNUSED,
  1518. int options) {
  1519. int ret;
  1520. char *temp;
  1521. nb_tests++;
  1522. temp = resultFilename(filename, "", ".res");
  1523. if (temp == NULL) {
  1524. fprintf(stderr, "out of memory\n");
  1525. fatalError();
  1526. }
  1527. SAXdebug = fopen(temp, "wb");
  1528. if (SAXdebug == NULL) {
  1529. fprintf(stderr, "Failed to write to %s\n", temp);
  1530. free(temp);
  1531. return(-1);
  1532. }
  1533. /* for SAX we really want the callbacks though the context handlers */
  1534. xmlSetStructuredErrorFunc(NULL, NULL);
  1535. xmlSetGenericErrorFunc(NULL, testErrorHandler);
  1536. #ifdef LIBXML_HTML_ENABLED
  1537. if (options & XML_PARSE_HTML) {
  1538. htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);
  1539. ret = 0;
  1540. } else
  1541. #endif
  1542. ret = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
  1543. if (ret == XML_WAR_UNDECLARED_ENTITY) {
  1544. fprintf(SAXdebug, "xmlSAXUserParseFile returned error %d\n", ret);
  1545. ret = 0;
  1546. }
  1547. if (ret != 0) {
  1548. fprintf(stderr, "Failed to parse %s\n", filename);
  1549. return(1);
  1550. }
  1551. #ifdef LIBXML_HTML_ENABLED
  1552. if (options & XML_PARSE_HTML) {
  1553. htmlSAXParseFile(filename, NULL, debugHTMLSAXHandler, NULL);
  1554. ret = 0;
  1555. } else
  1556. #endif
  1557. if (options & XML_PARSE_SAX1) {
  1558. ret = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
  1559. } else {
  1560. ret = xmlSAXUserParseFile(debugSAX2Handler, NULL, filename);
  1561. }
  1562. if (ret == XML_WAR_UNDECLARED_ENTITY) {
  1563. fprintf(SAXdebug, "xmlSAXUserParseFile returned error %d\n", ret);
  1564. ret = 0;
  1565. }
  1566. fclose(SAXdebug);
  1567. if (compareFiles(temp, result)) {
  1568. fprintf(stderr, "Got a difference for %s\n", filename);
  1569. ret = 1;
  1570. }
  1571. if (temp != NULL) {
  1572. unlink(temp);
  1573. free(temp);
  1574. }
  1575. /* switch back to structured error handling */
  1576. xmlSetGenericErrorFunc(NULL, NULL);
  1577. xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
  1578. return(ret);
  1579. }
  1580. #endif
  1581. /************************************************************************
  1582. * *
  1583. * Parse to tree based tests *
  1584. * *
  1585. ************************************************************************/
  1586. /**
  1587. * oldParseTest:
  1588. * @filename: the file to parse
  1589. * @result: the file with expected result
  1590. * @err: the file with error messages: unused
  1591. *
  1592. * Parse a file using the old xmlParseFile API, then serialize back
  1593. * reparse the result and serialize again, then check for deviation
  1594. * in serialization.
  1595. *
  1596. * Returns 0 in case of success, an error code otherwise
  1597. */
  1598. static int
  1599. oldParseTest(const char *filename, const char *result,
  1600. const char *err ATTRIBUTE_UNUSED,
  1601. int options ATTRIBUTE_UNUSED) {
  1602. xmlDocPtr doc;
  1603. char *temp;
  1604. int res = 0;
  1605. nb_tests++;
  1606. /*
  1607. * base of the test, parse with the old API
  1608. */
  1609. #ifdef LIBXML_SAX1_ENABLED
  1610. doc = xmlParseFile(filename);
  1611. #else
  1612. doc = xmlReadFile(filename, NULL, 0);
  1613. #endif
  1614. if (doc == NULL)
  1615. return(1);
  1616. temp = resultFilename(filename, "", ".res");
  1617. if (temp == NULL) {
  1618. fprintf(stderr, "out of memory\n");
  1619. fatalError();
  1620. }
  1621. xmlSaveFile(temp, doc);
  1622. if (compareFiles(temp, result)) {
  1623. res = 1;
  1624. }
  1625. xmlFreeDoc(doc);
  1626. /*
  1627. * Parse the saved result to make sure the round trip is okay
  1628. */
  1629. #ifdef LIBXML_SAX1_ENABLED
  1630. doc = xmlParseFile(temp);
  1631. #else
  1632. doc = xmlReadFile(temp, NULL, 0);
  1633. #endif
  1634. if (doc == NULL)
  1635. return(1);
  1636. xmlSaveFile(temp, doc);
  1637. if (compareFiles(temp, result)) {
  1638. res = 1;
  1639. }
  1640. xmlFreeDoc(doc);
  1641. if (temp != NULL) {
  1642. unlink(temp);
  1643. free(temp);
  1644. }
  1645. return(res);
  1646. }
  1647. #ifdef LIBXML_PUSH_ENABLED
  1648. /**
  1649. * pushParseTest:
  1650. * @filename: the file to parse
  1651. * @result: the file with expected result
  1652. * @err: the file with error messages: unused
  1653. *
  1654. * Parse a file using the Push API, then serialize back
  1655. * to check for content.
  1656. *
  1657. * Returns 0 in case of success, an error code otherwise
  1658. */
  1659. static int
  1660. pushParseTest(const char *filename, const char *result,
  1661. const char *err ATTRIBUTE_UNUSED,
  1662. int options) {
  1663. xmlParserCtxtPtr ctxt;
  1664. xmlDocPtr doc;
  1665. const char *base;
  1666. int size, res;
  1667. int cur = 0;
  1668. nb_tests++;
  1669. /*
  1670. * load the document in memory and work from there.
  1671. */
  1672. if (loadMem(filename, &base, &size) != 0) {
  1673. fprintf(stderr, "Failed to load %s\n", filename);
  1674. return(-1);
  1675. }
  1676. #ifdef LIBXML_HTML_ENABLED
  1677. if (options & XML_PARSE_HTML)
  1678. ctxt = htmlCreatePushParserCtxt(NULL, NULL, base + cur, 4, filename,
  1679. XML_CHAR_ENCODING_NONE);
  1680. else
  1681. #endif
  1682. ctxt = xmlCreatePushParserCtxt(NULL, NULL, base + cur, 4, filename);
  1683. xmlCtxtUseOptions(ctxt, options);
  1684. cur += 4;
  1685. while (cur < size) {
  1686. if (cur + 1024 >= size) {
  1687. #ifdef LIBXML_HTML_ENABLED
  1688. if (options & XML_PARSE_HTML)
  1689. htmlParseChunk(ctxt, base + cur, size - cur, 1);
  1690. else
  1691. #endif
  1692. xmlParseChunk(ctxt, base + cur, size - cur, 1);
  1693. break;
  1694. } else {
  1695. #ifdef LIBXML_HTML_ENABLED
  1696. if (options & XML_PARSE_HTML)
  1697. htmlParseChunk(ctxt, base + cur, 1024, 0);
  1698. else
  1699. #endif
  1700. xmlParseChunk(ctxt, base + cur, 1024, 0);
  1701. cur += 1024;
  1702. }
  1703. }
  1704. doc = ctxt->myDoc;
  1705. #ifdef LIBXML_HTML_ENABLED
  1706. if (options & XML_PARSE_HTML)
  1707. res = 1;
  1708. else
  1709. #endif
  1710. res = ctxt->wellFormed;
  1711. xmlFreeParserCtxt(ctxt);
  1712. free((char *)base);
  1713. if (!res) {
  1714. xmlFreeDoc(doc);
  1715. fprintf(stderr, "Failed to parse %s\n", filename);
  1716. return(-1);
  1717. }
  1718. #ifdef LIBXML_HTML_ENABLED
  1719. if (options & XML_PARSE_HTML)
  1720. htmlDocDumpMemory(doc, (xmlChar **) &base, &size);
  1721. else
  1722. #endif
  1723. xmlDocDumpMemory(doc, (xmlChar **) &base, &size);
  1724. xmlFreeDoc(doc);
  1725. res = compareFileMem(result, base, size);
  1726. if ((base == NULL) || (res != 0)) {
  1727. if (base != NULL)
  1728. xmlFree((char *)base);
  1729. fprintf(stderr, "Result for %s failed\n", filename);
  1730. return(-1);
  1731. }
  1732. xmlFree((char *)base);
  1733. if (err != NULL) {
  1734. res = compareFileMem(err, testErrors, testErrorsSize);
  1735. if (res != 0) {
  1736. fprintf(stderr, "Error for %s failed\n", filename);
  1737. return(-1);
  1738. }
  1739. }
  1740. return(0);
  1741. }
  1742. #endif
  1743. /**
  1744. * memParseTest:
  1745. * @filename: the file to parse
  1746. * @result: the file with expected result
  1747. * @err: the file with error messages: unused
  1748. *
  1749. * Parse a file using the old xmlReadMemory API, then serialize back
  1750. * reparse the result and serialize again, then check for deviation
  1751. * in serialization.
  1752. *
  1753. * Returns 0 in case of success, an error code otherwise
  1754. */
  1755. static int
  1756. memParseTest(const char *filename, const char *result,
  1757. const char *err ATTRIBUTE_UNUSED,
  1758. int options ATTRIBUTE_UNUSED) {
  1759. xmlDocPtr doc;
  1760. const char *base;
  1761. int size, res;
  1762. nb_tests++;
  1763. /*
  1764. * load and parse the memory
  1765. */
  1766. if (loadMem(filename, &base, &size) != 0) {
  1767. fprintf(stderr, "Failed to load %s\n", filename);
  1768. return(-1);
  1769. }
  1770. doc = xmlReadMemory(base, size, filename, NULL, 0);
  1771. unloadMem(base);
  1772. if (doc == NULL) {
  1773. return(1);
  1774. }
  1775. xmlDocDumpMemory(doc, (xmlChar **) &base, &size);
  1776. xmlFreeDoc(doc);
  1777. res = compareFileMem(result, base, size);
  1778. if ((base == NULL) || (res != 0)) {
  1779. if (base != NULL)
  1780. xmlFree((char *)base);
  1781. fprintf(stderr, "Result for %s failed\n", filename);
  1782. return(-1);
  1783. }
  1784. xmlFree((char *)base);
  1785. return(0);
  1786. }
  1787. /**
  1788. * noentParseTest:
  1789. * @filename: the file to parse
  1790. * @result: the file with expected result
  1791. * @err: the file with error messages: unused
  1792. *
  1793. * Parse a file with entity resolution, then serialize back
  1794. * reparse the result and serialize again, then check for deviation
  1795. * in serialization.
  1796. *
  1797. * Returns 0 in case of success, an error code otherwise
  1798. */
  1799. static int
  1800. noentParseTest(const char *filename, const char *result,
  1801. const char *err ATTRIBUTE_UNUSED,
  1802. int options) {
  1803. xmlDocPtr doc;
  1804. char *temp;
  1805. int res = 0;
  1806. nb_tests++;
  1807. /*
  1808. * base of the test, parse with the old API
  1809. */
  1810. doc = xmlReadFile(filename, NULL, options);
  1811. if (doc == NULL)
  1812. return(1);
  1813. temp = resultFilename(filename, "", ".res");
  1814. if (temp == NULL) {
  1815. fprintf(stderr, "Out of memory\n");
  1816. fatalError();
  1817. }
  1818. xmlSaveFile(temp, doc);
  1819. if (compareFiles(temp, result)) {
  1820. res = 1;
  1821. }
  1822. xmlFreeDoc(doc);
  1823. /*
  1824. * Parse the saved result to make sure the round trip is okay
  1825. */
  1826. doc = xmlReadFile(filename, NULL, options);
  1827. if (doc == NULL)
  1828. return(1);
  1829. xmlSaveFile(temp, doc);
  1830. if (compareFiles(temp, result)) {
  1831. res = 1;
  1832. }
  1833. xmlFreeDoc(doc);
  1834. if (temp != NULL) {
  1835. unlink(temp);
  1836. free(temp);
  1837. }
  1838. return(res);
  1839. }
  1840. /**
  1841. * errParseTest:
  1842. * @filename: the file to parse
  1843. * @result: the file with expected result
  1844. * @err: the file with error messages
  1845. *
  1846. * Parse a file using the xmlReadFile API and check for errors.
  1847. *
  1848. * Returns 0 in case of success, an error code otherwise
  1849. */
  1850. static int
  1851. errParseTest(const char *filename, const char *result, const char *err,
  1852. int options) {
  1853. xmlDocPtr doc;
  1854. const char *base = NULL;
  1855. int size, res = 0;
  1856. nb_tests++;
  1857. #ifdef LIBXML_HTML_ENABLED
  1858. if (options & XML_PARSE_HTML) {
  1859. doc = htmlReadFile(filename, NULL, options);
  1860. } else
  1861. #endif
  1862. #ifdef LIBXML_XINCLUDE_ENABLED
  1863. if (options & XML_PARSE_XINCLUDE) {
  1864. doc = xmlReadFile(filename, NULL, options);
  1865. xmlXIncludeProcessFlags(doc, options);
  1866. } else
  1867. #endif
  1868. {
  1869. xmlGetWarningsDefaultValue = 1;
  1870. doc = xmlReadFile(filename, NULL, options);
  1871. }
  1872. xmlGetWarningsDefaultValue = 0;
  1873. if (result) {
  1874. if (doc == NULL) {
  1875. base = "";
  1876. size = 0;
  1877. } else {
  1878. #ifdef LIBXML_HTML_ENABLED
  1879. if (options & XML_PARSE_HTML) {
  1880. htmlDocDumpMemory(doc, (xmlChar **) &base, &size);
  1881. } else
  1882. #endif
  1883. xmlDocDumpMemory(doc, (xmlChar **) &base, &size);
  1884. }
  1885. res = compareFileMem(result, base, size);
  1886. }
  1887. if (doc != NULL) {
  1888. if (base != NULL)
  1889. xmlFree((char *)base);
  1890. xmlFreeDoc(doc);
  1891. }
  1892. if (res != 0) {
  1893. fprintf(stderr, "Result for %s failed\n", filename);
  1894. return(-1);
  1895. }
  1896. if (err != NULL) {
  1897. res = compareFileMem(err, testErrors, testErrorsSize);
  1898. if (res != 0) {
  1899. fprintf(stderr, "Error for %s failed\n", filename);
  1900. return(-1);
  1901. }
  1902. } else if (options & XML_PARSE_DTDVALID) {
  1903. if (testErrorsSize != 0)
  1904. fprintf(stderr, "Validation for %s failed\n", filename);
  1905. }
  1906. return(0);
  1907. }
  1908. #ifdef LIBXML_READER_ENABLED
  1909. /************************************************************************
  1910. * *
  1911. * Reader based tests *
  1912. * *
  1913. ************************************************************************/
  1914. static void processNode(FILE *out, xmlTextReaderPtr reader) {
  1915. const xmlChar *name, *value;
  1916. int type, empty;
  1917. type = xmlTextReaderNodeType(reader);
  1918. empty = xmlTextReaderIsEmptyElement(reader);
  1919. name = xmlTextReaderConstName(reader);
  1920. if (name == NULL)
  1921. name = BAD_CAST "--";
  1922. value = xmlTextReaderConstValue(reader);
  1923. fprintf(out, "%d %d %s %d %d",
  1924. xmlTextReaderDepth(reader),
  1925. type,
  1926. name,
  1927. empty,
  1928. xmlTextReaderHasValue(reader));
  1929. if (value == NULL)
  1930. fprintf(out, "\n");
  1931. else {
  1932. fprintf(out, " %s\n", value);
  1933. }
  1934. }
  1935. static int
  1936. streamProcessTest(const char *filename, const char *result, const char *err,
  1937. xmlTextReaderPtr reader, const char *rng) {
  1938. int ret;
  1939. char *temp = NULL;
  1940. FILE *t = NULL;
  1941. if (reader == NULL)
  1942. return(-1);
  1943. nb_tests++;
  1944. if (result != NULL) {
  1945. temp = resultFilename(filename, "", ".res");
  1946. if (temp == NULL) {
  1947. fprintf(stderr, "Out of memory\n");
  1948. fatalError();
  1949. }
  1950. t = fopen(temp, "wb");
  1951. if (t == NULL) {
  1952. fprintf(stderr, "Can't open temp file %s\n", temp);
  1953. free(temp);
  1954. return(-1);
  1955. }
  1956. }
  1957. #ifdef LIBXML_SCHEMAS_ENABLED
  1958. if (rng != NULL) {
  1959. ret = xmlTextReaderRelaxNGValidate(reader, rng);
  1960. if (ret < 0) {
  1961. testErrorHandler(NULL, "Relax-NG schema %s failed to compile\n",
  1962. rng);
  1963. fclose(t);
  1964. if (temp != NULL) {
  1965. unlink(temp);
  1966. free(temp);
  1967. }
  1968. return(0);
  1969. }
  1970. }
  1971. #endif
  1972. xmlGetWarningsDefaultValue = 1;
  1973. ret = xmlTextReaderRead(reader);
  1974. while (ret == 1) {
  1975. if ((t != NULL) && (rng == NULL))
  1976. processNode(t, reader);
  1977. ret = xmlTextReaderRead(reader);
  1978. }
  1979. if (ret != 0) {
  1980. testErrorHandler(NULL, "%s : failed to parse\n", filename);
  1981. }
  1982. if (rng != NULL) {
  1983. if (xmlTextReaderIsValid(reader) != 1) {
  1984. testErrorHandler(NULL, "%s fails to validate\n", filename);
  1985. } else {
  1986. testErrorHandler(NULL, "%s validates\n", filename);
  1987. }
  1988. }
  1989. xmlGetWarningsDefaultValue = 0;
  1990. if (t != NULL) {
  1991. fclose(t);
  1992. ret = compareFiles(temp, result);
  1993. if (temp != NULL) {
  1994. unlink(temp);
  1995. free(temp);
  1996. }
  1997. if (ret) {
  1998. fprintf(stderr, "Result for %s failed\n", filename);
  1999. return(-1);
  2000. }
  2001. }
  2002. if (err != NULL) {
  2003. ret = compareFileMem(err, testErrors, testErrorsSize);
  2004. if (ret != 0) {
  2005. fprintf(stderr, "Error for %s failed\n", filename);
  2006. printf("%s", testErrors);
  2007. return(-1);
  2008. }
  2009. }
  2010. return(0);
  2011. }
  2012. /**
  2013. * streamParseTest:
  2014. * @filename: the file to parse
  2015. * @result: the file with expected result
  2016. * @err: the file with error messages
  2017. *
  2018. * Parse a file using the reader API and check for errors.
  2019. *
  2020. * Returns 0 in case of success, an error code otherwise
  2021. */
  2022. static int
  2023. streamParseTest(const char *filename, const char *result, const char *err,
  2024. int options) {
  2025. xmlTextReaderPtr reader;
  2026. int ret;
  2027. reader = xmlReaderForFile(filename, NULL, options);
  2028. ret = streamProcessTest(filename, result, err, reader, NULL);
  2029. xmlFreeTextReader(reader);
  2030. return(ret);
  2031. }
  2032. /**
  2033. * walkerParseTest:
  2034. * @filename: the file to parse
  2035. * @result: the file with expected result
  2036. * @err: the file with error messages
  2037. *
  2038. * Parse a file using the walker, i.e. a reader built from a atree.
  2039. *
  2040. * Returns 0 in case of success, an error code otherwise
  2041. */
  2042. static int
  2043. walkerParseTest(const char *filename, const char *result, const char *err,
  2044. int options) {
  2045. xmlDocPtr doc;
  2046. xmlTextReaderPtr reader;
  2047. int ret;
  2048. doc = xmlReadFile(filename, NULL, options);
  2049. if (doc == NULL) {
  2050. fprintf(stderr, "Failed to parse %s\n", filename);
  2051. return(-1);
  2052. }
  2053. reader = xmlReaderWalker(doc);
  2054. ret = streamProcessTest(filename, result, err, reader, NULL);
  2055. xmlFreeTextReader(reader);
  2056. xmlFreeDoc(doc);
  2057. return(ret);
  2058. }
  2059. /**
  2060. * streamMemParseTest:
  2061. * @filename: the file to parse
  2062. * @result: the file with expected result
  2063. * @err: the file with error messages
  2064. *
  2065. * Parse a file using the reader API from memory and check for errors.
  2066. *
  2067. * Returns 0 in case of success, an error code otherwise
  2068. */
  2069. static int
  2070. streamMemParseTest(const char *filename, const char *result, const char *err,
  2071. int options) {
  2072. xmlTextReaderPtr reader;
  2073. int ret;
  2074. const char *base;
  2075. int size;
  2076. /*
  2077. * load and parse the memory
  2078. */
  2079. if (loadMem(filename, &base, &size) != 0) {
  2080. fprintf(stderr, "Failed to load %s\n", filename);
  2081. return(-1);
  2082. }
  2083. reader = xmlReaderForMemory(base, size, filename, NULL, options);
  2084. ret = streamProcessTest(filename, result, err, reader, NULL);
  2085. free((char *)base);
  2086. xmlFreeTextReader(reader);
  2087. return(ret);
  2088. }
  2089. #endif
  2090. #ifdef LIBXML_XPATH_ENABLED
  2091. #ifdef LIBXML_DEBUG_ENABLED
  2092. /************************************************************************
  2093. * *
  2094. * XPath and XPointer based tests *
  2095. * *
  2096. ************************************************************************/
  2097. static FILE *xpathOutput;
  2098. static xmlDocPtr xpathDocument;
  2099. static void
  2100. testXPath(const char *str, int xptr, int expr) {
  2101. xmlXPathObjectPtr res;
  2102. xmlXPathContextPtr ctxt;
  2103. nb_tests++;
  2104. #if defined(LIBXML_XPTR_ENABLED)
  2105. if (xptr) {
  2106. ctxt = xmlXPtrNewContext(xpathDocument, NULL, NULL);
  2107. res = xmlXPtrEval(BAD_CAST str, ctxt);
  2108. } else {
  2109. #endif
  2110. ctxt = xmlXPathNewContext(xpathDocument);
  2111. ctxt->node = xmlDocGetRootElement(xpathDocument);
  2112. if (expr)
  2113. res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
  2114. else {
  2115. /* res = xmlXPathEval(BAD_CAST str, ctxt); */
  2116. xmlXPathCompExprPtr comp;
  2117. comp = xmlXPathCompile(BAD_CAST str);
  2118. if (comp != NULL) {
  2119. res = xmlXPathCompiledEval(comp, ctxt);
  2120. xmlXPathFreeCompExpr(comp);
  2121. } else
  2122. res = NULL;
  2123. }
  2124. #if defined(LIBXML_XPTR_ENABLED)
  2125. }
  2126. #endif
  2127. xmlXPathDebugDumpObject(xpathOutput, res, 0);
  2128. xmlXPathFreeObject(res);
  2129. xmlXPathFreeContext(ctxt);
  2130. }
  2131. /**
  2132. * xpathExprTest:
  2133. * @filename: the file to parse
  2134. * @result: the file with expected result
  2135. * @err: the file with error messages
  2136. *
  2137. * Parse a file containing XPath standalone expressions and evaluate them
  2138. *
  2139. * Returns 0 in case of success, an error code otherwise
  2140. */
  2141. static int
  2142. xpathCommonTest(const char *filename, const char *result,
  2143. int xptr, int expr) {
  2144. FILE *input;
  2145. char expression[5000];
  2146. int len, ret = 0;
  2147. char *temp;
  2148. temp = resultFilename(filename, "", ".res");
  2149. if (temp == NULL) {
  2150. fprintf(stderr, "Out of memory\n");
  2151. fatalError();
  2152. }
  2153. xpathOutput = fopen(temp, "wb");
  2154. if (xpathOutput == NULL) {
  2155. fprintf(stderr, "failed to open output file %s\n", temp);
  2156. free(temp);
  2157. return(-1);
  2158. }
  2159. input = fopen(filename, "rb");
  2160. if (input == NULL) {
  2161. xmlGenericError(xmlGenericErrorContext,
  2162. "Cannot open %s for reading\n", filename);
  2163. free(temp);
  2164. return(-1);
  2165. }
  2166. while (fgets(expression, 4500, input) != NULL) {
  2167. len = strlen(expression);
  2168. len--;
  2169. while ((len >= 0) &&
  2170. ((expression[len] == '\n') || (expression[len] == '\t') ||
  2171. (expression[len] == '\r') || (expression[len] == ' '))) len--;
  2172. expression[len + 1] = 0;
  2173. if (len >= 0) {
  2174. fprintf(xpathOutput,
  2175. "\n========================\nExpression: %s\n",
  2176. expression) ;
  2177. testXPath(expression, xptr, expr);
  2178. }
  2179. }
  2180. fclose(input);
  2181. fclose(xpathOutput);
  2182. if (result != NULL) {
  2183. ret = compareFiles(temp, result);
  2184. if (ret) {
  2185. fprintf(stderr, "Result for %s failed\n", filename);
  2186. }
  2187. }
  2188. if (temp != NULL) {
  2189. unlink(temp);
  2190. free(temp);
  2191. }
  2192. return(ret);
  2193. }
  2194. /**
  2195. * xpathExprTest:
  2196. * @filename: the file to parse
  2197. * @result: the file with expected result
  2198. * @err: the file with error messages
  2199. *
  2200. * Parse a file containing XPath standalone expressions and evaluate them
  2201. *
  2202. * Returns 0 in case of success, an error code otherwise
  2203. */
  2204. static int
  2205. xpathExprTest(const char *filename, const char *result,
  2206. const char *err ATTRIBUTE_UNUSED,
  2207. int options ATTRIBUTE_UNUSED) {
  2208. return(xpathCommonTest(filename, result, 0, 1));
  2209. }
  2210. /**
  2211. * xpathDocTest:
  2212. * @filename: the file to parse
  2213. * @result: the file with expected result
  2214. * @err: the file with error messages
  2215. *
  2216. * Parse a file containing XPath expressions and evaluate them against
  2217. * a set of corresponding documents.
  2218. *
  2219. * Returns 0 in case of success, an error code otherwise
  2220. */
  2221. static int
  2222. xpathDocTest(const char *filename,
  2223. const char *resul ATTRIBUTE_UNUSED,
  2224. const char *err ATTRIBUTE_UNUSED,
  2225. int options) {
  2226. char pattern[500];
  2227. char result[500];
  2228. glob_t globbuf;
  2229. size_t i;
  2230. int ret = 0, res;
  2231. xpathDocument = xmlReadFile(filename, NULL,
  2232. options | XML_PARSE_DTDATTR | XML_PARSE_NOENT);
  2233. if (xpathDocument == NULL) {
  2234. fprintf(stderr, "Failed to load %s\n", filename);
  2235. return(-1);
  2236. }
  2237. snprintf(pattern, 499, "./test/XPath/tests/%s*", baseFilename(filename));
  2238. pattern[499] = 0;
  2239. globbuf.gl_offs = 0;
  2240. glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
  2241. for (i = 0;i < globbuf.gl_pathc;i++) {
  2242. snprintf(result, 499, "result/XPath/tests/%s",
  2243. baseFilename(globbuf.gl_pathv[i]));
  2244. res = xpathCommonTest(globbuf.gl_pathv[i], &result[0], 0, 0);
  2245. if (res != 0)
  2246. ret = res;
  2247. }
  2248. globfree(&globbuf);
  2249. xmlFreeDoc(xpathDocument);
  2250. return(ret);
  2251. }
  2252. #ifdef LIBXML_XPTR_ENABLED
  2253. /**
  2254. * xptrDocTest:
  2255. * @filename: the file to parse
  2256. * @result: the file with expected result
  2257. * @err: the file with error messages
  2258. *
  2259. * Parse a file containing XPath expressions and evaluate them against
  2260. * a set of corresponding documents.
  2261. *
  2262. * Returns 0 in case of success, an error code otherwise
  2263. */
  2264. static int
  2265. xptrDocTest(const char *filename,
  2266. const char *resul ATTRIBUTE_UNUSED,
  2267. const char *err ATTRIBUTE_UNUSED,
  2268. int options) {
  2269. char pattern[500];
  2270. char result[500];
  2271. glob_t globbuf;
  2272. size_t i;
  2273. int ret = 0, res;
  2274. xpathDocument = xmlReadFile(filename, NULL,
  2275. options | XML_PARSE_DTDATTR | XML_PARSE_NOENT);
  2276. if (xpathDocument == NULL) {
  2277. fprintf(stderr, "Failed to load %s\n", filename);
  2278. return(-1);
  2279. }
  2280. snprintf(pattern, 499, "./test/XPath/xptr/%s*", baseFilename(filename));
  2281. pattern[499] = 0;
  2282. globbuf.gl_offs = 0;
  2283. glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
  2284. for (i = 0;i < globbuf.gl_pathc;i++) {
  2285. snprintf(result, 499, "result/XPath/xptr/%s",
  2286. baseFilename(globbuf.gl_pathv[i]));
  2287. res = xpathCommonTest(globbuf.gl_pathv[i], &result[0], 1, 0);
  2288. if (res != 0)
  2289. ret = res;
  2290. }
  2291. globfree(&globbuf);
  2292. xmlFreeDoc(xpathDocument);
  2293. return(ret);
  2294. }
  2295. #endif /* LIBXML_XPTR_ENABLED */
  2296. /**
  2297. * xmlidDocTest:
  2298. * @filename: the file to parse
  2299. * @result: the file with expected result
  2300. * @err: the file with error messages
  2301. *
  2302. * Parse a file containing xml:id and check for errors and verify
  2303. * that XPath queries will work on them as expected.
  2304. *
  2305. * Returns 0 in case of success, an error code otherwise
  2306. */
  2307. static int
  2308. xmlidDocTest(const char *filename,
  2309. const char *result,
  2310. const char *err,
  2311. int options) {
  2312. int res = 0;
  2313. int ret = 0;
  2314. char *temp;
  2315. xpathDocument = xmlReadFile(filename, NULL,
  2316. options | XML_PARSE_DTDATTR | XML_PARSE_NOENT);
  2317. if (xpathDocument == NULL) {
  2318. fprintf(stderr, "Failed to load %s\n", filename);
  2319. return(-1);
  2320. }
  2321. temp = resultFilename(filename, "", ".res");
  2322. if (temp == NULL) {
  2323. fprintf(stderr, "Out of memory\n");
  2324. fatalError();
  2325. }
  2326. xpathOutput = fopen(temp, "wb");
  2327. if (xpathOutput == NULL) {
  2328. fprintf(stderr, "failed to open output file %s\n", temp);
  2329. xmlFreeDoc(xpathDocument);
  2330. free(temp);
  2331. return(-1);
  2332. }
  2333. testXPath("id('bar')", 0, 0);
  2334. fclose(xpathOutput);
  2335. if (result != NULL) {
  2336. ret = compareFiles(temp, result);
  2337. if (ret) {
  2338. fprintf(stderr, "Result for %s failed\n", filename);
  2339. res = 1;
  2340. }
  2341. }
  2342. if (temp != NULL) {
  2343. unlink(temp);
  2344. free(temp);
  2345. }
  2346. xmlFreeDoc(xpathDocument);
  2347. if (err != NULL) {
  2348. ret = compareFileMem(err, testErrors, testErrorsSize);
  2349. if (ret != 0) {
  2350. fprintf(stderr, "Error for %s failed\n", filename);
  2351. res = 1;
  2352. }
  2353. }
  2354. return(res);
  2355. }
  2356. #endif /* LIBXML_DEBUG_ENABLED */
  2357. #endif /* XPATH */
  2358. /************************************************************************
  2359. * *
  2360. * URI based tests *
  2361. * *
  2362. ************************************************************************/
  2363. static void
  2364. handleURI(const char *str, const char *base, FILE *o) {
  2365. int ret;
  2366. xmlURIPtr uri;
  2367. xmlChar *res = NULL;
  2368. uri = xmlCreateURI();
  2369. if (base == NULL) {
  2370. ret = xmlParseURIReference(uri, str);
  2371. if (ret != 0)
  2372. fprintf(o, "%s : error %d\n", str, ret);
  2373. else {
  2374. xmlNormalizeURIPath(uri->path);
  2375. xmlPrintURI(o, uri);
  2376. fprintf(o, "\n");
  2377. }
  2378. } else {
  2379. res = xmlBuildURI((xmlChar *)str, (xmlChar *) base);
  2380. if (res != NULL) {
  2381. fprintf(o, "%s\n", (char *) res);
  2382. }
  2383. else
  2384. fprintf(o, "::ERROR::\n");
  2385. }
  2386. if (res != NULL)
  2387. xmlFree(res);
  2388. xmlFreeURI(uri);
  2389. }
  2390. /**
  2391. * uriCommonTest:
  2392. * @filename: the file to parse
  2393. * @result: the file with expected result
  2394. * @err: the file with error messages
  2395. *
  2396. * Parse a file containing URI and check for errors
  2397. *
  2398. * Returns 0 in case of success, an error code otherwise
  2399. */
  2400. static int
  2401. uriCommonTest(const char *filename,
  2402. const char *result,
  2403. const char *err,
  2404. const char *base) {
  2405. char *temp;
  2406. FILE *o, *f;
  2407. char str[1024];
  2408. int res = 0, i, ret;
  2409. temp = resultFilename(filename, "", ".res");
  2410. if (temp == NULL) {
  2411. fprintf(stderr, "Out of memory\n");
  2412. fatalError();
  2413. }
  2414. o = fopen(temp, "wb");
  2415. if (o == NULL) {
  2416. fprintf(stderr, "failed to open output file %s\n", temp);
  2417. free(temp);
  2418. return(-1);
  2419. }
  2420. f = fopen(filename, "rb");
  2421. if (f == NULL) {
  2422. fprintf(stderr, "failed to open input file %s\n", filename);
  2423. fclose(o);
  2424. if (temp != NULL) {
  2425. unlink(temp);
  2426. free(temp);
  2427. }
  2428. return(-1);
  2429. }
  2430. while (1) {
  2431. /*
  2432. * read one line in string buffer.
  2433. */
  2434. if (fgets (&str[0], sizeof (str) - 1, f) == NULL)
  2435. break;
  2436. /*
  2437. * remove the ending spaces
  2438. */
  2439. i = strlen(str);
  2440. while ((i > 0) &&
  2441. ((str[i - 1] == '\n') || (str[i - 1] == '\r') ||
  2442. (str[i - 1] == ' ') || (str[i - 1] == '\t'))) {
  2443. i--;
  2444. str[i] = 0;
  2445. }
  2446. nb_tests++;
  2447. handleURI(str, base, o);
  2448. }
  2449. fclose(f);
  2450. fclose(o);
  2451. if (result != NULL) {
  2452. ret = compareFiles(temp, result);
  2453. if (ret) {
  2454. fprintf(stderr, "Result for %s failed\n", filename);
  2455. res = 1;
  2456. }
  2457. }
  2458. if (err != NULL) {
  2459. ret = compareFileMem(err, testErrors, testErrorsSize);
  2460. if (ret != 0) {
  2461. fprintf(stderr, "Error for %s failed\n", filename);
  2462. res = 1;
  2463. }
  2464. }
  2465. if (temp != NULL) {
  2466. unlink(temp);
  2467. free(temp);
  2468. }
  2469. return(res);
  2470. }
  2471. /**
  2472. * uriParseTest:
  2473. * @filename: the file to parse
  2474. * @result: the file with expected result
  2475. * @err: the file with error messages
  2476. *
  2477. * Parse a file containing URI and check for errors
  2478. *
  2479. * Returns 0 in case of success, an error code otherwise
  2480. */
  2481. static int
  2482. uriParseTest(const char *filename,
  2483. const char *result,
  2484. const char *err,
  2485. int options ATTRIBUTE_UNUSED) {
  2486. return(uriCommonTest(filename, result, err, NULL));
  2487. }
  2488. /**
  2489. * uriBaseTest:
  2490. * @filename: the file to parse
  2491. * @result: the file with expected result
  2492. * @err: the file with error messages
  2493. *
  2494. * Parse a file containing URI, compose them against a fixed base and
  2495. * check for errors
  2496. *
  2497. * Returns 0 in case of success, an error code otherwise
  2498. */
  2499. static int
  2500. uriBaseTest(const char *filename,
  2501. const char *result,
  2502. const char *err,
  2503. int options ATTRIBUTE_UNUSED) {
  2504. return(uriCommonTest(filename, result, err,
  2505. "http://foo.com/path/to/index.html?orig#help"));
  2506. }
  2507. static int urip_success = 1;
  2508. static int urip_current = 0;
  2509. static const char *urip_testURLs[] = {
  2510. "urip://example.com/a b.html",
  2511. "urip://example.com/a%20b.html",
  2512. "file:///path/to/a b.html",
  2513. "file:///path/to/a%20b.html",
  2514. "/path/to/a b.html",
  2515. "/path/to/a%20b.html",
  2516. "urip://example.com/résumé.html",
  2517. "urip://example.com/test?a=1&b=2%263&c=4#foo",
  2518. NULL
  2519. };
  2520. static const char *urip_rcvsURLs[] = {
  2521. /* it is an URI the strings must be escaped */
  2522. "urip://example.com/a%20b.html",
  2523. /* check that % escaping is not broken */
  2524. "urip://example.com/a%20b.html",
  2525. /* it's an URI path the strings must be escaped */
  2526. "file:///path/to/a%20b.html",
  2527. /* check that % escaping is not broken */
  2528. "file:///path/to/a%20b.html",
  2529. /* this is not an URI, this is a path, so this should not be escaped */
  2530. "/path/to/a b.html",
  2531. /* check that paths with % are not broken */
  2532. "/path/to/a%20b.html",
  2533. /* out of context the encoding can't be guessed byte by byte conversion */
  2534. "urip://example.com/r%E9sum%E9.html",
  2535. /* verify we don't destroy URIs especially the query part */
  2536. "urip://example.com/test?a=1&b=2%263&c=4#foo",
  2537. NULL
  2538. };
  2539. static const char *urip_res = "<list/>";
  2540. static const char *urip_cur = NULL;
  2541. static int urip_rlen;
  2542. /**
  2543. * uripMatch:
  2544. * @URI: an URI to test
  2545. *
  2546. * Check for an urip: query
  2547. *
  2548. * Returns 1 if yes and 0 if another Input module should be used
  2549. */
  2550. static int
  2551. uripMatch(const char * URI) {
  2552. if ((URI == NULL) || (!strcmp(URI, "file:///etc/xml/catalog")))
  2553. return(0);
  2554. /* Verify we received the escaped URL */
  2555. if (strcmp(urip_rcvsURLs[urip_current], URI))
  2556. urip_success = 0;
  2557. return(1);
  2558. }
  2559. /**
  2560. * uripOpen:
  2561. * @URI: an URI to test
  2562. *
  2563. * Return a pointer to the urip: query handler, in this example simply
  2564. * the urip_current pointer...
  2565. *
  2566. * Returns an Input context or NULL in case or error
  2567. */
  2568. static void *
  2569. uripOpen(const char * URI) {
  2570. if ((URI == NULL) || (!strcmp(URI, "file:///etc/xml/catalog")))
  2571. return(NULL);
  2572. /* Verify we received the escaped URL */
  2573. if (strcmp(urip_rcvsURLs[urip_current], URI))
  2574. urip_success = 0;
  2575. urip_cur = urip_res;
  2576. urip_rlen = strlen(urip_res);
  2577. return((void *) urip_cur);
  2578. }
  2579. /**
  2580. * uripClose:
  2581. * @context: the read context
  2582. *
  2583. * Close the urip: query handler
  2584. *
  2585. * Returns 0 or -1 in case of error
  2586. */
  2587. static int
  2588. uripClose(void * context) {
  2589. if (context == NULL) return(-1);
  2590. urip_cur = NULL;
  2591. urip_rlen = 0;
  2592. return(0);
  2593. }
  2594. /**
  2595. * uripRead:
  2596. * @context: the read context
  2597. * @buffer: where to store data
  2598. * @len: number of bytes to read
  2599. *
  2600. * Implement an urip: query read.
  2601. *
  2602. * Returns the number of bytes read or -1 in case of error
  2603. */
  2604. static int
  2605. uripRead(void * context, char * buffer, int len) {
  2606. const char *ptr = (const char *) context;
  2607. if ((context == NULL) || (buffer == NULL) || (len < 0))
  2608. return(-1);
  2609. if (len > urip_rlen) len = urip_rlen;
  2610. memcpy(buffer, ptr, len);
  2611. urip_rlen -= len;
  2612. return(len);
  2613. }
  2614. static int
  2615. urip_checkURL(const char *URL) {
  2616. xmlDocPtr doc;
  2617. doc = xmlReadFile(URL, NULL, 0);
  2618. if (doc == NULL)
  2619. return(-1);
  2620. xmlFreeDoc(doc);
  2621. return(1);
  2622. }
  2623. /**
  2624. * uriPathTest:
  2625. * @filename: ignored
  2626. * @result: ignored
  2627. * @err: ignored
  2628. *
  2629. * Run a set of tests to check how Path and URI are handled before
  2630. * being passed to the I/O layer
  2631. *
  2632. * Returns 0 in case of success, an error code otherwise
  2633. */
  2634. static int
  2635. uriPathTest(const char *filename ATTRIBUTE_UNUSED,
  2636. const char *result ATTRIBUTE_UNUSED,
  2637. const char *err ATTRIBUTE_UNUSED,
  2638. int options ATTRIBUTE_UNUSED) {
  2639. int parsed;
  2640. int failures = 0;
  2641. /*
  2642. * register the new I/O handlers
  2643. */
  2644. if (xmlRegisterInputCallbacks(uripMatch, uripOpen, uripRead, uripClose) < 0)
  2645. {
  2646. fprintf(stderr, "failed to register HTTP handler\n");
  2647. return(-1);
  2648. }
  2649. for (urip_current = 0;urip_testURLs[urip_current] != NULL;urip_current++) {
  2650. urip_success = 1;
  2651. parsed = urip_checkURL(urip_testURLs[urip_current]);
  2652. if (urip_success != 1) {
  2653. fprintf(stderr, "failed the URL passing test for %s",
  2654. urip_testURLs[urip_current]);
  2655. failures++;
  2656. } else if (parsed != 1) {
  2657. fprintf(stderr, "failed the parsing test for %s",
  2658. urip_testURLs[urip_current]);
  2659. failures++;
  2660. }
  2661. nb_tests++;
  2662. }
  2663. xmlPopInputCallbacks();
  2664. return(failures);
  2665. }
  2666. #ifdef LIBXML_SCHEMAS_ENABLED
  2667. /************************************************************************
  2668. * *
  2669. * Schemas tests *
  2670. * *
  2671. ************************************************************************/
  2672. static int
  2673. schemasOneTest(const char *sch,
  2674. const char *filename,
  2675. const char *result,
  2676. const char *err,
  2677. int options,
  2678. xmlSchemaPtr schemas) {
  2679. xmlDocPtr doc;
  2680. xmlSchemaValidCtxtPtr ctxt;
  2681. int ret = 0;
  2682. int validResult = 0;
  2683. char *temp;
  2684. FILE *schemasOutput;
  2685. doc = xmlReadFile(filename, NULL, options);
  2686. if (doc == NULL) {
  2687. fprintf(stderr, "failed to parse instance %s for %s\n", filename, sch);
  2688. return(-1);
  2689. }
  2690. temp = resultFilename(result, "", ".res");
  2691. if (temp == NULL) {
  2692. fprintf(stderr, "Out of memory\n");
  2693. fatalError();
  2694. }
  2695. schemasOutput = fopen(temp, "wb");
  2696. if (schemasOutput == NULL) {
  2697. fprintf(stderr, "failed to open output file %s\n", temp);
  2698. xmlFreeDoc(doc);
  2699. free(temp);
  2700. return(-1);
  2701. }
  2702. ctxt = xmlSchemaNewValidCtxt(schemas);
  2703. xmlSchemaSetValidErrors(ctxt,
  2704. (xmlSchemaValidityErrorFunc) testErrorHandler,
  2705. (xmlSchemaValidityWarningFunc) testErrorHandler,
  2706. ctxt);
  2707. validResult = xmlSchemaValidateDoc(ctxt, doc);
  2708. if (validResult == 0) {
  2709. fprintf(schemasOutput, "%s validates\n", filename);
  2710. } else if (validResult > 0) {
  2711. fprintf(schemasOutput, "%s fails to validate\n", filename);
  2712. } else {
  2713. fprintf(schemasOutput, "%s validation generated an internal error\n",
  2714. filename);
  2715. }
  2716. fclose(schemasOutput);
  2717. if (result) {
  2718. if (compareFiles(temp, result)) {
  2719. fprintf(stderr, "Result for %s on %s failed\n", filename, sch);
  2720. ret = 1;
  2721. }
  2722. }
  2723. if (temp != NULL) {
  2724. unlink(temp);
  2725. free(temp);
  2726. }
  2727. if ((validResult != 0) && (err != NULL)) {
  2728. if (compareFileMem(err, testErrors, testErrorsSize)) {
  2729. fprintf(stderr, "Error for %s on %s failed\n", filename, sch);
  2730. ret = 1;
  2731. }
  2732. }
  2733. xmlSchemaFreeValidCtxt(ctxt);
  2734. xmlFreeDoc(doc);
  2735. return(ret);
  2736. }
  2737. /**
  2738. * schemasTest:
  2739. * @filename: the schemas file
  2740. * @result: the file with expected result
  2741. * @err: the file with error messages
  2742. *
  2743. * Parse a file containing URI, compose them against a fixed base and
  2744. * check for errors
  2745. *
  2746. * Returns 0 in case of success, an error code otherwise
  2747. */
  2748. static int
  2749. schemasTest(const char *filename,
  2750. const char *resul ATTRIBUTE_UNUSED,
  2751. const char *errr ATTRIBUTE_UNUSED,
  2752. int options) {
  2753. const char *base = baseFilename(filename);
  2754. const char *base2;
  2755. const char *instance;
  2756. xmlSchemaParserCtxtPtr ctxt;
  2757. xmlSchemaPtr schemas;
  2758. int res = 0, len, ret;
  2759. char pattern[500];
  2760. char prefix[500];
  2761. char result[500];
  2762. char err[500];
  2763. glob_t globbuf;
  2764. size_t i;
  2765. char count = 0;
  2766. /* first compile the schemas if possible */
  2767. ctxt = xmlSchemaNewParserCtxt(filename);
  2768. xmlSchemaSetParserErrors(ctxt,
  2769. (xmlSchemaValidityErrorFunc) testErrorHandler,
  2770. (xmlSchemaValidityWarningFunc) testErrorHandler,
  2771. ctxt);
  2772. schemas = xmlSchemaParse(ctxt);
  2773. xmlSchemaFreeParserCtxt(ctxt);
  2774. /*
  2775. * most of the mess is about the output filenames generated by the Makefile
  2776. */
  2777. len = strlen(base);
  2778. if ((len > 499) || (len < 5)) {
  2779. xmlSchemaFree(schemas);
  2780. return(-1);
  2781. }
  2782. len -= 4; /* remove trailing .xsd */
  2783. if (base[len - 2] == '_') {
  2784. len -= 2; /* remove subtest number */
  2785. }
  2786. if (base[len - 2] == '_') {
  2787. len -= 2; /* remove subtest number */
  2788. }
  2789. memcpy(prefix, base, len);
  2790. prefix[len] = 0;
  2791. snprintf(pattern, 499, "./test/schemas/%s_?.xml", prefix);
  2792. pattern[499] = 0;
  2793. if (base[len] == '_') {
  2794. len += 2;
  2795. memcpy(prefix, base, len);
  2796. prefix[len] = 0;
  2797. }
  2798. globbuf.gl_offs = 0;
  2799. glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
  2800. for (i = 0;i < globbuf.gl_pathc;i++) {
  2801. testErrorsSize = 0;
  2802. testErrors[0] = 0;
  2803. instance = globbuf.gl_pathv[i];
  2804. base2 = baseFilename(instance);
  2805. len = strlen(base2);
  2806. if ((len > 6) && (base2[len - 6] == '_')) {
  2807. count = base2[len - 5];
  2808. snprintf(result, 499, "result/schemas/%s_%c",
  2809. prefix, count);
  2810. result[499] = 0;
  2811. snprintf(err, 499, "result/schemas/%s_%c.err",
  2812. prefix, count);
  2813. err[499] = 0;
  2814. } else {
  2815. fprintf(stderr, "don't know how to process %s\n", instance);
  2816. continue;
  2817. }
  2818. if (schemas == NULL) {
  2819. } else {
  2820. nb_tests++;
  2821. ret = schemasOneTest(filename, instance, result, err,
  2822. options, schemas);
  2823. if (ret != 0)
  2824. res = ret;
  2825. }
  2826. }
  2827. globfree(&globbuf);
  2828. xmlSchemaFree(schemas);
  2829. return(res);
  2830. }
  2831. /************************************************************************
  2832. * *
  2833. * Schemas tests *
  2834. * *
  2835. ************************************************************************/
  2836. static int
  2837. rngOneTest(const char *sch,
  2838. const char *filename,
  2839. const char *result,
  2840. const char *err,
  2841. int options,
  2842. xmlRelaxNGPtr schemas) {
  2843. xmlDocPtr doc;
  2844. xmlRelaxNGValidCtxtPtr ctxt;
  2845. int ret = 0;
  2846. char *temp;
  2847. FILE *schemasOutput;
  2848. doc = xmlReadFile(filename, NULL, options);
  2849. if (doc == NULL) {
  2850. fprintf(stderr, "failed to parse instance %s for %s\n", filename, sch);
  2851. return(-1);
  2852. }
  2853. temp = resultFilename(result, "", ".res");
  2854. if (temp == NULL) {
  2855. fprintf(stderr, "Out of memory\n");
  2856. fatalError();
  2857. }
  2858. schemasOutput = fopen(temp, "wb");
  2859. if (schemasOutput == NULL) {
  2860. fprintf(stderr, "failed to open output file %s\n", temp);
  2861. xmlFreeDoc(doc);
  2862. free(temp);
  2863. return(-1);
  2864. }
  2865. ctxt = xmlRelaxNGNewValidCtxt(schemas);
  2866. xmlRelaxNGSetValidErrors(ctxt,
  2867. (xmlRelaxNGValidityErrorFunc) testErrorHandler,
  2868. (xmlRelaxNGValidityWarningFunc) testErrorHandler,
  2869. ctxt);
  2870. ret = xmlRelaxNGValidateDoc(ctxt, doc);
  2871. if (ret == 0) {
  2872. testErrorHandler(NULL, "%s validates\n", filename);
  2873. } else if (ret > 0) {
  2874. testErrorHandler(NULL, "%s fails to validate\n", filename);
  2875. } else {
  2876. testErrorHandler(NULL, "%s validation generated an internal error\n",
  2877. filename);
  2878. }
  2879. fclose(schemasOutput);
  2880. ret = 0;
  2881. if (result) {
  2882. if (compareFiles(temp, result)) {
  2883. fprintf(stderr, "Result for %s on %s failed\n", filename, sch);
  2884. ret = 1;
  2885. }
  2886. }
  2887. if (temp != NULL) {
  2888. unlink(temp);
  2889. free(temp);
  2890. }
  2891. if (err != NULL) {
  2892. if (compareFileMem(err, testErrors, testErrorsSize)) {
  2893. fprintf(stderr, "Error for %s on %s failed\n", filename, sch);
  2894. ret = 1;
  2895. printf("%s", testErrors);
  2896. }
  2897. }
  2898. xmlRelaxNGFreeValidCtxt(ctxt);
  2899. xmlFreeDoc(doc);
  2900. return(ret);
  2901. }
  2902. /**
  2903. * rngTest:
  2904. * @filename: the schemas file
  2905. * @result: the file with expected result
  2906. * @err: the file with error messages
  2907. *
  2908. * Parse an RNG schemas and then apply it to the related .xml
  2909. *
  2910. * Returns 0 in case of success, an error code otherwise
  2911. */
  2912. static int
  2913. rngTest(const char *filename,
  2914. const char *resul ATTRIBUTE_UNUSED,
  2915. const char *errr ATTRIBUTE_UNUSED,
  2916. int options) {
  2917. const char *base = baseFilename(filename);
  2918. const char *base2;
  2919. const char *instance;
  2920. xmlRelaxNGParserCtxtPtr ctxt;
  2921. xmlRelaxNGPtr schemas;
  2922. int res = 0, len, ret = 0;
  2923. char pattern[500];
  2924. char prefix[500];
  2925. char result[500];
  2926. char err[500];
  2927. glob_t globbuf;
  2928. size_t i;
  2929. char count = 0;
  2930. /* first compile the schemas if possible */
  2931. ctxt = xmlRelaxNGNewParserCtxt(filename);
  2932. xmlRelaxNGSetParserErrors(ctxt,
  2933. (xmlRelaxNGValidityErrorFunc) testErrorHandler,
  2934. (xmlRelaxNGValidityWarningFunc) testErrorHandler,
  2935. ctxt);
  2936. schemas = xmlRelaxNGParse(ctxt);
  2937. xmlRelaxNGFreeParserCtxt(ctxt);
  2938. /*
  2939. * most of the mess is about the output filenames generated by the Makefile
  2940. */
  2941. len = strlen(base);
  2942. if ((len > 499) || (len < 5)) {
  2943. xmlRelaxNGFree(schemas);
  2944. return(-1);
  2945. }
  2946. len -= 4; /* remove trailing .rng */
  2947. memcpy(prefix, base, len);
  2948. prefix[len] = 0;
  2949. snprintf(pattern, 499, "./test/relaxng/%s_?.xml", prefix);
  2950. pattern[499] = 0;
  2951. globbuf.gl_offs = 0;
  2952. glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
  2953. for (i = 0;i < globbuf.gl_pathc;i++) {
  2954. testErrorsSize = 0;
  2955. testErrors[0] = 0;
  2956. instance = globbuf.gl_pathv[i];
  2957. base2 = baseFilename(instance);
  2958. len = strlen(base2);
  2959. if ((len > 6) && (base2[len - 6] == '_')) {
  2960. count = base2[len - 5];
  2961. snprintf(result, 499, "result/relaxng/%s_%c",
  2962. prefix, count);
  2963. result[499] = 0;
  2964. snprintf(err, 499, "result/relaxng/%s_%c.err",
  2965. prefix, count);
  2966. err[499] = 0;
  2967. } else {
  2968. fprintf(stderr, "don't know how to process %s\n", instance);
  2969. continue;
  2970. }
  2971. if (schemas == NULL) {
  2972. } else {
  2973. nb_tests++;
  2974. ret = rngOneTest(filename, instance, result, err,
  2975. options, schemas);
  2976. if (res != 0)
  2977. ret = res;
  2978. }
  2979. }
  2980. globfree(&globbuf);
  2981. xmlRelaxNGFree(schemas);
  2982. return(ret);
  2983. }
  2984. #ifdef LIBXML_READER_ENABLED
  2985. /**
  2986. * rngStreamTest:
  2987. * @filename: the schemas file
  2988. * @result: the file with expected result
  2989. * @err: the file with error messages
  2990. *
  2991. * Parse a set of files with streaming, applying an RNG schemas
  2992. *
  2993. * Returns 0 in case of success, an error code otherwise
  2994. */
  2995. static int
  2996. rngStreamTest(const char *filename,
  2997. const char *resul ATTRIBUTE_UNUSED,
  2998. const char *errr ATTRIBUTE_UNUSED,
  2999. int options) {
  3000. const char *base = baseFilename(filename);
  3001. const char *base2;
  3002. const char *instance;
  3003. int res = 0, len, ret;
  3004. char pattern[500];
  3005. char prefix[500];
  3006. char result[500];
  3007. char err[500];
  3008. glob_t globbuf;
  3009. size_t i;
  3010. char count = 0;
  3011. xmlTextReaderPtr reader;
  3012. int disable_err = 0;
  3013. /*
  3014. * most of the mess is about the output filenames generated by the Makefile
  3015. */
  3016. len = strlen(base);
  3017. if ((len > 499) || (len < 5)) {
  3018. fprintf(stderr, "len(base) == %d !\n", len);
  3019. return(-1);
  3020. }
  3021. len -= 4; /* remove trailing .rng */
  3022. memcpy(prefix, base, len);
  3023. prefix[len] = 0;
  3024. /*
  3025. * strictly unifying the error messages is nearly impossible this
  3026. * hack is also done in the Makefile
  3027. */
  3028. if ((!strcmp(prefix, "tutor10_1")) || (!strcmp(prefix, "tutor10_2")) ||
  3029. (!strcmp(prefix, "tutor3_2")) || (!strcmp(prefix, "307377")) ||
  3030. (!strcmp(prefix, "tutor8_2")))
  3031. disable_err = 1;
  3032. snprintf(pattern, 499, "./test/relaxng/%s_?.xml", prefix);
  3033. pattern[499] = 0;
  3034. globbuf.gl_offs = 0;
  3035. glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
  3036. for (i = 0;i < globbuf.gl_pathc;i++) {
  3037. testErrorsSize = 0;
  3038. testErrors[0] = 0;
  3039. instance = globbuf.gl_pathv[i];
  3040. base2 = baseFilename(instance);
  3041. len = strlen(base2);
  3042. if ((len > 6) && (base2[len - 6] == '_')) {
  3043. count = base2[len - 5];
  3044. snprintf(result, 499, "result/relaxng/%s_%c",
  3045. prefix, count);
  3046. result[499] = 0;
  3047. snprintf(err, 499, "result/relaxng/%s_%c.err",
  3048. prefix, count);
  3049. err[499] = 0;
  3050. } else {
  3051. fprintf(stderr, "don't know how to process %s\n", instance);
  3052. continue;
  3053. }
  3054. reader = xmlReaderForFile(instance, NULL, options);
  3055. if (reader == NULL) {
  3056. fprintf(stderr, "Failed to build reder for %s\n", instance);
  3057. }
  3058. if (disable_err == 1)
  3059. ret = streamProcessTest(instance, result, NULL, reader, filename);
  3060. else
  3061. ret = streamProcessTest(instance, result, err, reader, filename);
  3062. xmlFreeTextReader(reader);
  3063. if (ret != 0) {
  3064. fprintf(stderr, "instance %s failed\n", instance);
  3065. res = ret;
  3066. }
  3067. }
  3068. globfree(&globbuf);
  3069. return(res);
  3070. }
  3071. #endif /* READER */
  3072. #endif
  3073. #ifdef LIBXML_PATTERN_ENABLED
  3074. #ifdef LIBXML_READER_ENABLED
  3075. /************************************************************************
  3076. * *
  3077. * Patterns tests *
  3078. * *
  3079. ************************************************************************/
  3080. static void patternNode(FILE *out, xmlTextReaderPtr reader,
  3081. const char *pattern, xmlPatternPtr patternc,
  3082. xmlStreamCtxtPtr patstream) {
  3083. xmlChar *path = NULL;
  3084. int match = -1;
  3085. int type, empty;
  3086. type = xmlTextReaderNodeType(reader);
  3087. empty = xmlTextReaderIsEmptyElement(reader);
  3088. if (type == XML_READER_TYPE_ELEMENT) {
  3089. /* do the check only on element start */
  3090. match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
  3091. if (match) {
  3092. path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
  3093. fprintf(out, "Node %s matches pattern %s\n", path, pattern);
  3094. }
  3095. }
  3096. if (patstream != NULL) {
  3097. int ret;
  3098. if (type == XML_READER_TYPE_ELEMENT) {
  3099. ret = xmlStreamPush(patstream,
  3100. xmlTextReaderConstLocalName(reader),
  3101. xmlTextReaderConstNamespaceUri(reader));
  3102. if (ret < 0) {
  3103. fprintf(out, "xmlStreamPush() failure\n");
  3104. xmlFreeStreamCtxt(patstream);
  3105. patstream = NULL;
  3106. } else if (ret != match) {
  3107. if (path == NULL) {
  3108. path = xmlGetNodePath(
  3109. xmlTextReaderCurrentNode(reader));
  3110. }
  3111. fprintf(out,
  3112. "xmlPatternMatch and xmlStreamPush disagree\n");
  3113. fprintf(out,
  3114. " pattern %s node %s\n",
  3115. pattern, path);
  3116. }
  3117. }
  3118. if ((type == XML_READER_TYPE_END_ELEMENT) ||
  3119. ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
  3120. ret = xmlStreamPop(patstream);
  3121. if (ret < 0) {
  3122. fprintf(out, "xmlStreamPop() failure\n");
  3123. xmlFreeStreamCtxt(patstream);
  3124. patstream = NULL;
  3125. }
  3126. }
  3127. }
  3128. if (path != NULL)
  3129. xmlFree(path);
  3130. }
  3131. /**
  3132. * patternTest:
  3133. * @filename: the schemas file
  3134. * @result: the file with expected result
  3135. * @err: the file with error messages
  3136. *
  3137. * Parse a set of files with streaming, applying an RNG schemas
  3138. *
  3139. * Returns 0 in case of success, an error code otherwise
  3140. */
  3141. static int
  3142. patternTest(const char *filename,
  3143. const char *resul ATTRIBUTE_UNUSED,
  3144. const char *err ATTRIBUTE_UNUSED,
  3145. int options) {
  3146. xmlPatternPtr patternc = NULL;
  3147. xmlStreamCtxtPtr patstream = NULL;
  3148. FILE *o, *f;
  3149. char str[1024];
  3150. char xml[500];
  3151. char result[500];
  3152. int len, i;
  3153. int ret = 0, res;
  3154. char *temp;
  3155. xmlTextReaderPtr reader;
  3156. xmlDocPtr doc;
  3157. len = strlen(filename);
  3158. len -= 4;
  3159. memcpy(xml, filename, len);
  3160. xml[len] = 0;
  3161. snprintf(result, 499, "result/pattern/%s", baseFilename(xml));
  3162. result[499] = 0;
  3163. memcpy(xml + len, ".xml", 5);
  3164. if (!checkTestFile(xml)) {
  3165. fprintf(stderr, "Missing xml file %s\n", xml);
  3166. return(-1);
  3167. }
  3168. if (!checkTestFile(result)) {
  3169. fprintf(stderr, "Missing result file %s\n", result);
  3170. return(-1);
  3171. }
  3172. f = fopen(filename, "rb");
  3173. if (f == NULL) {
  3174. fprintf(stderr, "Failed to open %s\n", filename);
  3175. return(-1);
  3176. }
  3177. temp = resultFilename(filename, "", ".res");
  3178. if (temp == NULL) {
  3179. fprintf(stderr, "Out of memory\n");
  3180. fatalError();
  3181. }
  3182. o = fopen(temp, "wb");
  3183. if (o == NULL) {
  3184. fprintf(stderr, "failed to open output file %s\n", temp);
  3185. fclose(f);
  3186. free(temp);
  3187. return(-1);
  3188. }
  3189. while (1) {
  3190. /*
  3191. * read one line in string buffer.
  3192. */
  3193. if (fgets (&str[0], sizeof (str) - 1, f) == NULL)
  3194. break;
  3195. /*
  3196. * remove the ending spaces
  3197. */
  3198. i = strlen(str);
  3199. while ((i > 0) &&
  3200. ((str[i - 1] == '\n') || (str[i - 1] == '\r') ||
  3201. (str[i - 1] == ' ') || (str[i - 1] == '\t'))) {
  3202. i--;
  3203. str[i] = 0;
  3204. }
  3205. doc = xmlReadFile(xml, NULL, options);
  3206. if (doc == NULL) {
  3207. fprintf(stderr, "Failed to parse %s\n", xml);
  3208. ret = 1;
  3209. } else {
  3210. xmlNodePtr root;
  3211. const xmlChar *namespaces[22];
  3212. int j;
  3213. xmlNsPtr ns;
  3214. root = xmlDocGetRootElement(doc);
  3215. for (ns = root->nsDef, j = 0;ns != NULL && j < 20;ns=ns->next) {
  3216. namespaces[j++] = ns->href;
  3217. namespaces[j++] = ns->prefix;
  3218. }
  3219. namespaces[j++] = NULL;
  3220. namespaces[j] = NULL;
  3221. patternc = xmlPatterncompile((const xmlChar *) str, doc->dict,
  3222. 0, &namespaces[0]);
  3223. if (patternc == NULL) {
  3224. testErrorHandler(NULL,
  3225. "Pattern %s failed to compile\n", str);
  3226. xmlFreeDoc(doc);
  3227. ret = 1;
  3228. continue;
  3229. }
  3230. patstream = xmlPatternGetStreamCtxt(patternc);
  3231. if (patstream != NULL) {
  3232. ret = xmlStreamPush(patstream, NULL, NULL);
  3233. if (ret < 0) {
  3234. fprintf(stderr, "xmlStreamPush() failure\n");
  3235. xmlFreeStreamCtxt(patstream);
  3236. patstream = NULL;
  3237. }
  3238. }
  3239. nb_tests++;
  3240. reader = xmlReaderWalker(doc);
  3241. res = xmlTextReaderRead(reader);
  3242. while (res == 1) {
  3243. patternNode(o, reader, str, patternc, patstream);
  3244. res = xmlTextReaderRead(reader);
  3245. }
  3246. if (res != 0) {
  3247. fprintf(o, "%s : failed to parse\n", filename);
  3248. }
  3249. xmlFreeTextReader(reader);
  3250. xmlFreeDoc(doc);
  3251. xmlFreeStreamCtxt(patstream);
  3252. patstream = NULL;
  3253. xmlFreePattern(patternc);
  3254. }
  3255. }
  3256. fclose(f);
  3257. fclose(o);
  3258. ret = compareFiles(temp, result);
  3259. if (ret) {
  3260. fprintf(stderr, "Result for %s failed\n", filename);
  3261. ret = 1;
  3262. }
  3263. if (temp != NULL) {
  3264. unlink(temp);
  3265. free(temp);
  3266. }
  3267. return(ret);
  3268. }
  3269. #endif /* READER */
  3270. #endif /* PATTERN */
  3271. #ifdef LIBXML_C14N_ENABLED
  3272. /************************************************************************
  3273. * *
  3274. * Canonicalization tests *
  3275. * *
  3276. ************************************************************************/
  3277. static xmlXPathObjectPtr
  3278. load_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
  3279. xmlXPathObjectPtr xpath;
  3280. xmlDocPtr doc;
  3281. xmlChar *expr;
  3282. xmlXPathContextPtr ctx;
  3283. xmlNodePtr node;
  3284. xmlNsPtr ns;
  3285. /*
  3286. * load XPath expr as a file
  3287. */
  3288. xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
  3289. xmlSubstituteEntitiesDefault(1);
  3290. doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
  3291. if (doc == NULL) {
  3292. fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
  3293. return(NULL);
  3294. }
  3295. /*
  3296. * Check the document is of the right kind
  3297. */
  3298. if(xmlDocGetRootElement(doc) == NULL) {
  3299. fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
  3300. xmlFreeDoc(doc);
  3301. return(NULL);
  3302. }
  3303. node = doc->children;
  3304. while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
  3305. node = node->next;
  3306. }
  3307. if(node == NULL) {
  3308. fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
  3309. xmlFreeDoc(doc);
  3310. return(NULL);
  3311. }
  3312. expr = xmlNodeGetContent(node);
  3313. if(expr == NULL) {
  3314. fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
  3315. xmlFreeDoc(doc);
  3316. return(NULL);
  3317. }
  3318. ctx = xmlXPathNewContext(parent_doc);
  3319. if(ctx == NULL) {
  3320. fprintf(stderr,"Error: unable to create new context\n");
  3321. xmlFree(expr);
  3322. xmlFreeDoc(doc);
  3323. return(NULL);
  3324. }
  3325. /*
  3326. * Register namespaces
  3327. */
  3328. ns = node->nsDef;
  3329. while(ns != NULL) {
  3330. if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
  3331. fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
  3332. xmlFree(expr);
  3333. xmlXPathFreeContext(ctx);
  3334. xmlFreeDoc(doc);
  3335. return(NULL);
  3336. }
  3337. ns = ns->next;
  3338. }
  3339. /*
  3340. * Evaluate xpath
  3341. */
  3342. xpath = xmlXPathEvalExpression(expr, ctx);
  3343. if(xpath == NULL) {
  3344. fprintf(stderr,"Error: unable to evaluate xpath expression\n");
  3345. xmlFree(expr);
  3346. xmlXPathFreeContext(ctx);
  3347. xmlFreeDoc(doc);
  3348. return(NULL);
  3349. }
  3350. /* print_xpath_nodes(xpath->nodesetval); */
  3351. xmlFree(expr);
  3352. xmlXPathFreeContext(ctx);
  3353. xmlFreeDoc(doc);
  3354. return(xpath);
  3355. }
  3356. /*
  3357. * Macro used to grow the current buffer.
  3358. */
  3359. #define xxx_growBufferReentrant() { \
  3360. buffer_size *= 2; \
  3361. buffer = (xmlChar **) \
  3362. xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
  3363. if (buffer == NULL) { \
  3364. perror("realloc failed"); \
  3365. return(NULL); \
  3366. } \
  3367. }
  3368. static xmlChar **
  3369. parse_list(xmlChar *str) {
  3370. xmlChar **buffer;
  3371. xmlChar **out = NULL;
  3372. int buffer_size = 0;
  3373. int len;
  3374. if(str == NULL) {
  3375. return(NULL);
  3376. }
  3377. len = xmlStrlen(str);
  3378. if((str[0] == '\'') && (str[len - 1] == '\'')) {
  3379. str[len - 1] = '\0';
  3380. str++;
  3381. }
  3382. /*
  3383. * allocate an translation buffer.
  3384. */
  3385. buffer_size = 1000;
  3386. buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
  3387. if (buffer == NULL) {
  3388. perror("malloc failed");
  3389. return(NULL);
  3390. }
  3391. out = buffer;
  3392. while(*str != '\0') {
  3393. if (out - buffer > buffer_size - 10) {
  3394. int indx = out - buffer;
  3395. xxx_growBufferReentrant();
  3396. out = &buffer[indx];
  3397. }
  3398. (*out++) = str;
  3399. while(*str != ',' && *str != '\0') ++str;
  3400. if(*str == ',') *(str++) = '\0';
  3401. }
  3402. (*out) = NULL;
  3403. return buffer;
  3404. }
  3405. static int
  3406. c14nRunTest(const char* xml_filename, int with_comments, int mode,
  3407. const char* xpath_filename, const char *ns_filename,
  3408. const char* result_file) {
  3409. xmlDocPtr doc;
  3410. xmlXPathObjectPtr xpath = NULL;
  3411. xmlChar *result = NULL;
  3412. int ret;
  3413. xmlChar **inclusive_namespaces = NULL;
  3414. const char *nslist = NULL;
  3415. int nssize;
  3416. /*
  3417. * build an XML tree from a the file; we need to add default
  3418. * attributes and resolve all character and entities references
  3419. */
  3420. xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
  3421. xmlSubstituteEntitiesDefault(1);
  3422. doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
  3423. if (doc == NULL) {
  3424. fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
  3425. return(-1);
  3426. }
  3427. /*
  3428. * Check the document is of the right kind
  3429. */
  3430. if(xmlDocGetRootElement(doc) == NULL) {
  3431. fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
  3432. xmlFreeDoc(doc);
  3433. return(-1);
  3434. }
  3435. /*
  3436. * load xpath file if specified
  3437. */
  3438. if(xpath_filename) {
  3439. xpath = load_xpath_expr(doc, xpath_filename);
  3440. if(xpath == NULL) {
  3441. fprintf(stderr,"Error: unable to evaluate xpath expression\n");
  3442. xmlFreeDoc(doc);
  3443. return(-1);
  3444. }
  3445. }
  3446. if (ns_filename != NULL) {
  3447. if (loadMem(ns_filename, &nslist, &nssize)) {
  3448. fprintf(stderr,"Error: unable to evaluate xpath expression\n");
  3449. if(xpath != NULL) xmlXPathFreeObject(xpath);
  3450. xmlFreeDoc(doc);
  3451. return(-1);
  3452. }
  3453. inclusive_namespaces = parse_list((xmlChar *) nslist);
  3454. }
  3455. /*
  3456. * Canonical form
  3457. */
  3458. /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
  3459. ret = xmlC14NDocDumpMemory(doc,
  3460. (xpath) ? xpath->nodesetval : NULL,
  3461. mode, inclusive_namespaces,
  3462. with_comments, &result);
  3463. if (ret >= 0) {
  3464. if(result != NULL) {
  3465. if (compareFileMem(result_file, (const char *) result, ret)) {
  3466. fprintf(stderr, "Result mismatch for %s\n", xml_filename);
  3467. fprintf(stderr, "RESULT:\n%s\n", (const char*)result);
  3468. ret = -1;
  3469. }
  3470. }
  3471. } else {
  3472. fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
  3473. ret = -1;
  3474. }
  3475. /*
  3476. * Cleanup
  3477. */
  3478. if (result != NULL) xmlFree(result);
  3479. if(xpath != NULL) xmlXPathFreeObject(xpath);
  3480. if (inclusive_namespaces != NULL) xmlFree(inclusive_namespaces);
  3481. if (nslist != NULL) free((char *) nslist);
  3482. xmlFreeDoc(doc);
  3483. return(ret);
  3484. }
  3485. static int
  3486. c14nCommonTest(const char *filename, int with_comments, int mode,
  3487. const char *subdir) {
  3488. char buf[500];
  3489. char prefix[500];
  3490. const char *base;
  3491. int len;
  3492. char *result = NULL;
  3493. char *xpath = NULL;
  3494. char *ns = NULL;
  3495. int ret = 0;
  3496. base = baseFilename(filename);
  3497. len = strlen(base);
  3498. len -= 4;
  3499. memcpy(prefix, base, len);
  3500. prefix[len] = 0;
  3501. snprintf(buf, 499, "result/c14n/%s/%s", subdir,prefix);
  3502. if (!checkTestFile(buf)) {
  3503. fprintf(stderr, "Missing result file %s", buf);
  3504. return(-1);
  3505. }
  3506. result = strdup(buf);
  3507. snprintf(buf, 499, "test/c14n/%s/%s.xpath", subdir,prefix);
  3508. if (checkTestFile(buf)) {
  3509. xpath = strdup(buf);
  3510. }
  3511. snprintf(buf, 499, "test/c14n/%s/%s.ns", subdir,prefix);
  3512. if (checkTestFile(buf)) {
  3513. ns = strdup(buf);
  3514. }
  3515. nb_tests++;
  3516. if (c14nRunTest(filename, with_comments, mode,
  3517. xpath, ns, result) < 0)
  3518. ret = 1;
  3519. if (result != NULL) free(result);
  3520. if (xpath != NULL) free(xpath);
  3521. if (ns != NULL) free(ns);
  3522. return(ret);
  3523. }
  3524. static int
  3525. c14nWithCommentTest(const char *filename,
  3526. const char *resul ATTRIBUTE_UNUSED,
  3527. const char *err ATTRIBUTE_UNUSED,
  3528. int options ATTRIBUTE_UNUSED) {
  3529. return(c14nCommonTest(filename, 1, XML_C14N_1_0, "with-comments"));
  3530. }
  3531. static int
  3532. c14nWithoutCommentTest(const char *filename,
  3533. const char *resul ATTRIBUTE_UNUSED,
  3534. const char *err ATTRIBUTE_UNUSED,
  3535. int options ATTRIBUTE_UNUSED) {
  3536. return(c14nCommonTest(filename, 0, XML_C14N_1_0, "without-comments"));
  3537. }
  3538. static int
  3539. c14nExcWithoutCommentTest(const char *filename,
  3540. const char *resul ATTRIBUTE_UNUSED,
  3541. const char *err ATTRIBUTE_UNUSED,
  3542. int options ATTRIBUTE_UNUSED) {
  3543. return(c14nCommonTest(filename, 0, XML_C14N_EXCLUSIVE_1_0, "exc-without-comments"));
  3544. }
  3545. static int
  3546. c14n11WithoutCommentTest(const char *filename,
  3547. const char *resul ATTRIBUTE_UNUSED,
  3548. const char *err ATTRIBUTE_UNUSED,
  3549. int options ATTRIBUTE_UNUSED) {
  3550. return(c14nCommonTest(filename, 0, XML_C14N_1_1, "1-1-without-comments"));
  3551. }
  3552. #endif
  3553. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED) && defined (LIBXML_SAX1_ENABLED)
  3554. /************************************************************************
  3555. * *
  3556. * Catalog and threads test *
  3557. * *
  3558. ************************************************************************/
  3559. /*
  3560. * mostly a cut and paste from testThreads.c
  3561. */
  3562. #define MAX_ARGC 20
  3563. static const char *catalog = "test/threads/complex.xml";
  3564. static const char *testfiles[] = {
  3565. "test/threads/abc.xml",
  3566. "test/threads/acb.xml",
  3567. "test/threads/bac.xml",
  3568. "test/threads/bca.xml",
  3569. "test/threads/cab.xml",
  3570. "test/threads/cba.xml",
  3571. "test/threads/invalid.xml",
  3572. };
  3573. static const char *Okay = "OK";
  3574. static const char *Failed = "Failed";
  3575. #ifndef xmlDoValidityCheckingDefaultValue
  3576. #error xmlDoValidityCheckingDefaultValue is not a macro
  3577. #endif
  3578. #ifndef xmlGenericErrorContext
  3579. #error xmlGenericErrorContext is not a macro
  3580. #endif
  3581. static void *
  3582. thread_specific_data(void *private_data)
  3583. {
  3584. xmlDocPtr myDoc;
  3585. const char *filename = (const char *) private_data;
  3586. int okay = 1;
  3587. if (!strcmp(filename, "test/threads/invalid.xml")) {
  3588. xmlDoValidityCheckingDefaultValue = 0;
  3589. xmlGenericErrorContext = stdout;
  3590. } else {
  3591. xmlDoValidityCheckingDefaultValue = 1;
  3592. xmlGenericErrorContext = stderr;
  3593. }
  3594. myDoc = xmlParseFile(filename);
  3595. if (myDoc) {
  3596. xmlFreeDoc(myDoc);
  3597. } else {
  3598. printf("parse failed\n");
  3599. okay = 0;
  3600. }
  3601. if (!strcmp(filename, "test/threads/invalid.xml")) {
  3602. if (xmlDoValidityCheckingDefaultValue != 0) {
  3603. printf("ValidityCheckingDefaultValue override failed\n");
  3604. okay = 0;
  3605. }
  3606. if (xmlGenericErrorContext != stdout) {
  3607. printf("xmlGenericErrorContext override failed\n");
  3608. okay = 0;
  3609. }
  3610. } else {
  3611. if (xmlDoValidityCheckingDefaultValue != 1) {
  3612. printf("ValidityCheckingDefaultValue override failed\n");
  3613. okay = 0;
  3614. }
  3615. if (xmlGenericErrorContext != stderr) {
  3616. printf("xmlGenericErrorContext override failed\n");
  3617. okay = 0;
  3618. }
  3619. }
  3620. if (okay == 0)
  3621. return ((void *) Failed);
  3622. return ((void *) Okay);
  3623. }
  3624. #if defined(linux) || defined(__sun) || defined(__APPLE_CC__)
  3625. #include <pthread.h>
  3626. static pthread_t tid[MAX_ARGC];
  3627. static int
  3628. testThread(void)
  3629. {
  3630. unsigned int i, repeat;
  3631. unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
  3632. void *results[MAX_ARGC];
  3633. int ret;
  3634. int res = 0;
  3635. xmlInitParser();
  3636. for (repeat = 0; repeat < 500; repeat++) {
  3637. xmlLoadCatalog(catalog);
  3638. nb_tests++;
  3639. for (i = 0; i < num_threads; i++) {
  3640. results[i] = NULL;
  3641. tid[i] = (pthread_t) - 1;
  3642. }
  3643. for (i = 0; i < num_threads; i++) {
  3644. ret = pthread_create(&tid[i], 0, thread_specific_data,
  3645. (void *) testfiles[i]);
  3646. if (ret != 0) {
  3647. fprintf(stderr, "pthread_create failed\n");
  3648. return (1);
  3649. }
  3650. }
  3651. for (i = 0; i < num_threads; i++) {
  3652. ret = pthread_join(tid[i], &results[i]);
  3653. if (ret != 0) {
  3654. fprintf(stderr, "pthread_join failed\n");
  3655. return (1);
  3656. }
  3657. }
  3658. xmlCatalogCleanup();
  3659. for (i = 0; i < num_threads; i++)
  3660. if (results[i] != (void *) Okay) {
  3661. fprintf(stderr, "Thread %d handling %s failed\n",
  3662. i, testfiles[i]);
  3663. res = 1;
  3664. }
  3665. }
  3666. return (res);
  3667. }
  3668. #elif defined WIN32
  3669. #include <windows.h>
  3670. #include <string.h>
  3671. #define TEST_REPEAT_COUNT 500
  3672. static HANDLE tid[MAX_ARGC];
  3673. static DWORD WINAPI
  3674. win32_thread_specific_data(void *private_data)
  3675. {
  3676. return((DWORD) thread_specific_data(private_data));
  3677. }
  3678. static int
  3679. testThread(void)
  3680. {
  3681. unsigned int i, repeat;
  3682. unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
  3683. DWORD results[MAX_ARGC];
  3684. BOOL ret;
  3685. int res = 0;
  3686. xmlInitParser();
  3687. for (repeat = 0; repeat < TEST_REPEAT_COUNT; repeat++) {
  3688. xmlLoadCatalog(catalog);
  3689. nb_tests++;
  3690. for (i = 0; i < num_threads; i++) {
  3691. results[i] = 0;
  3692. tid[i] = (HANDLE) - 1;
  3693. }
  3694. for (i = 0; i < num_threads; i++) {
  3695. DWORD useless;
  3696. tid[i] = CreateThread(NULL, 0,
  3697. win32_thread_specific_data,
  3698. (void *) testfiles[i], 0,
  3699. &useless);
  3700. if (tid[i] == NULL) {
  3701. fprintf(stderr, "CreateThread failed\n");
  3702. return(1);
  3703. }
  3704. }
  3705. if (WaitForMultipleObjects(num_threads, tid, TRUE, INFINITE) ==
  3706. WAIT_FAILED) {
  3707. fprintf(stderr, "WaitForMultipleObjects failed\n");
  3708. return(1);
  3709. }
  3710. for (i = 0; i < num_threads; i++) {
  3711. ret = GetExitCodeThread(tid[i], &results[i]);
  3712. if (ret == 0) {
  3713. fprintf(stderr, "GetExitCodeThread failed\n");
  3714. return(1);
  3715. }
  3716. CloseHandle(tid[i]);
  3717. }
  3718. xmlCatalogCleanup();
  3719. for (i = 0; i < num_threads; i++) {
  3720. if (results[i] != (DWORD) Okay) {
  3721. fprintf(stderr, "Thread %d handling %s failed\n",
  3722. i, testfiles[i]);
  3723. res = 1;
  3724. }
  3725. }
  3726. }
  3727. return (res);
  3728. }
  3729. #elif defined __BEOS__
  3730. #include <OS.h>
  3731. static thread_id tid[MAX_ARGC];
  3732. static int
  3733. testThread(void)
  3734. {
  3735. unsigned int i, repeat;
  3736. unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
  3737. void *results[MAX_ARGC];
  3738. status_t ret;
  3739. int res = 0;
  3740. xmlInitParser();
  3741. for (repeat = 0; repeat < 500; repeat++) {
  3742. xmlLoadCatalog(catalog);
  3743. for (i = 0; i < num_threads; i++) {
  3744. results[i] = NULL;
  3745. tid[i] = (thread_id) - 1;
  3746. }
  3747. for (i = 0; i < num_threads; i++) {
  3748. tid[i] =
  3749. spawn_thread(thread_specific_data, "xmlTestThread",
  3750. B_NORMAL_PRIORITY, (void *) testfiles[i]);
  3751. if (tid[i] < B_OK) {
  3752. fprintf(stderr, "beos_thread_create failed\n");
  3753. return (1);
  3754. }
  3755. printf("beos_thread_create %d -> %d\n", i, tid[i]);
  3756. }
  3757. for (i = 0; i < num_threads; i++) {
  3758. ret = wait_for_thread(tid[i], &results[i]);
  3759. printf("beos_thread_wait %d -> %d\n", i, ret);
  3760. if (ret != B_OK) {
  3761. fprintf(stderr, "beos_thread_wait failed\n");
  3762. return (1);
  3763. }
  3764. }
  3765. xmlCatalogCleanup();
  3766. ret = B_OK;
  3767. for (i = 0; i < num_threads; i++)
  3768. if (results[i] != (void *) Okay) {
  3769. printf("Thread %d handling %s failed\n", i, testfiles[i]);
  3770. ret = B_ERROR;
  3771. }
  3772. }
  3773. if (ret != B_OK)
  3774. return(1);
  3775. return (0);
  3776. }
  3777. #else
  3778. static int
  3779. testThread(void)
  3780. {
  3781. fprintf(stderr,
  3782. "Specific platform thread support not detected\n");
  3783. return (-1);
  3784. }
  3785. #endif
  3786. static int
  3787. threadsTest(const char *filename ATTRIBUTE_UNUSED,
  3788. const char *resul ATTRIBUTE_UNUSED,
  3789. const char *err ATTRIBUTE_UNUSED,
  3790. int options ATTRIBUTE_UNUSED) {
  3791. return(testThread());
  3792. }
  3793. #endif
  3794. /************************************************************************
  3795. * *
  3796. * Tests Descriptions *
  3797. * *
  3798. ************************************************************************/
  3799. static
  3800. testDesc testDescriptions[] = {
  3801. { "XML regression tests" ,
  3802. oldParseTest, "./test/*", "result/", "", NULL,
  3803. 0 },
  3804. { "XML regression tests on memory" ,
  3805. memParseTest, "./test/*", "result/", "", NULL,
  3806. 0 },
  3807. { "XML entity subst regression tests" ,
  3808. noentParseTest, "./test/*", "result/noent/", "", NULL,
  3809. XML_PARSE_NOENT },
  3810. { "XML Namespaces regression tests",
  3811. errParseTest, "./test/namespaces/*", "result/namespaces/", "", ".err",
  3812. 0 },
  3813. { "Error cases regression tests",
  3814. errParseTest, "./test/errors/*.xml", "result/errors/", "", ".err",
  3815. 0 },
  3816. #ifdef LIBXML_READER_ENABLED
  3817. { "Error cases stream regression tests",
  3818. streamParseTest, "./test/errors/*.xml", "result/errors/", NULL, ".str",
  3819. 0 },
  3820. { "Reader regression tests",
  3821. streamParseTest, "./test/*", "result/", ".rdr", NULL,
  3822. 0 },
  3823. { "Reader entities substitution regression tests",
  3824. streamParseTest, "./test/*", "result/", ".rde", NULL,
  3825. XML_PARSE_NOENT },
  3826. { "Reader on memory regression tests",
  3827. streamMemParseTest, "./test/*", "result/", ".rdr", NULL,
  3828. 0 },
  3829. { "Walker regression tests",
  3830. walkerParseTest, "./test/*", "result/", ".rdr", NULL,
  3831. 0 },
  3832. #endif
  3833. #ifdef LIBXML_SAX1_ENABLED
  3834. { "SAX1 callbacks regression tests" ,
  3835. saxParseTest, "./test/*", "result/", ".sax", NULL,
  3836. XML_PARSE_SAX1 },
  3837. { "SAX2 callbacks regression tests" ,
  3838. saxParseTest, "./test/*", "result/", ".sax2", NULL,
  3839. 0 },
  3840. #endif
  3841. #ifdef LIBXML_PUSH_ENABLED
  3842. { "XML push regression tests" ,
  3843. pushParseTest, "./test/*", "result/", "", NULL,
  3844. 0 },
  3845. #endif
  3846. #ifdef LIBXML_HTML_ENABLED
  3847. { "HTML regression tests" ,
  3848. errParseTest, "./test/HTML/*", "result/HTML/", "", ".err",
  3849. XML_PARSE_HTML },
  3850. #ifdef LIBXML_PUSH_ENABLED
  3851. { "Push HTML regression tests" ,
  3852. pushParseTest, "./test/HTML/*", "result/HTML/", "", ".err",
  3853. XML_PARSE_HTML },
  3854. #endif
  3855. #ifdef LIBXML_SAX1_ENABLED
  3856. { "HTML SAX regression tests" ,
  3857. saxParseTest, "./test/HTML/*", "result/HTML/", ".sax", NULL,
  3858. XML_PARSE_HTML },
  3859. #endif
  3860. #endif
  3861. #ifdef LIBXML_VALID_ENABLED
  3862. { "Valid documents regression tests" ,
  3863. errParseTest, "./test/VCM/*", NULL, NULL, NULL,
  3864. XML_PARSE_DTDVALID },
  3865. { "Validity checking regression tests" ,
  3866. errParseTest, "./test/VC/*", "result/VC/", NULL, "",
  3867. XML_PARSE_DTDVALID },
  3868. { "General documents valid regression tests" ,
  3869. errParseTest, "./test/valid/*", "result/valid/", "", ".err",
  3870. XML_PARSE_DTDVALID },
  3871. #endif
  3872. #ifdef LIBXML_XINCLUDE_ENABLED
  3873. { "XInclude regression tests" ,
  3874. errParseTest, "./test/XInclude/docs/*", "result/XInclude/", "", NULL,
  3875. /* Ignore errors at this point ".err", */
  3876. XML_PARSE_XINCLUDE },
  3877. #ifdef LIBXML_READER_ENABLED
  3878. { "XInclude xmlReader regression tests",
  3879. streamParseTest, "./test/XInclude/docs/*", "result/XInclude/", ".rdr",
  3880. /* Ignore errors at this point ".err", */
  3881. NULL, XML_PARSE_XINCLUDE },
  3882. #endif
  3883. { "XInclude regression tests stripping include nodes" ,
  3884. errParseTest, "./test/XInclude/docs/*", "result/XInclude/", "", NULL,
  3885. /* Ignore errors at this point ".err", */
  3886. XML_PARSE_XINCLUDE | XML_PARSE_NOXINCNODE },
  3887. #ifdef LIBXML_READER_ENABLED
  3888. { "XInclude xmlReader regression tests stripping include nodes",
  3889. streamParseTest, "./test/XInclude/docs/*", "result/XInclude/", ".rdr",
  3890. /* Ignore errors at this point ".err", */
  3891. NULL, XML_PARSE_XINCLUDE | XML_PARSE_NOXINCNODE },
  3892. #endif
  3893. #endif
  3894. #ifdef LIBXML_XPATH_ENABLED
  3895. #ifdef LIBXML_DEBUG_ENABLED
  3896. { "XPath expressions regression tests" ,
  3897. xpathExprTest, "./test/XPath/expr/*", "result/XPath/expr/", "", NULL,
  3898. 0 },
  3899. { "XPath document queries regression tests" ,
  3900. xpathDocTest, "./test/XPath/docs/*", NULL, NULL, NULL,
  3901. 0 },
  3902. #ifdef LIBXML_XPTR_ENABLED
  3903. { "XPointer document queries regression tests" ,
  3904. xptrDocTest, "./test/XPath/docs/*", NULL, NULL, NULL,
  3905. 0 },
  3906. #endif
  3907. { "xml:id regression tests" ,
  3908. xmlidDocTest, "./test/xmlid/*", "result/xmlid/", "", ".err",
  3909. 0 },
  3910. #endif
  3911. #endif
  3912. { "URI parsing tests" ,
  3913. uriParseTest, "./test/URI/*.uri", "result/URI/", "", NULL,
  3914. 0 },
  3915. { "URI base composition tests" ,
  3916. uriBaseTest, "./test/URI/*.data", "result/URI/", "", NULL,
  3917. 0 },
  3918. { "Path URI conversion tests" ,
  3919. uriPathTest, NULL, NULL, NULL, NULL,
  3920. 0 },
  3921. #ifdef LIBXML_SCHEMAS_ENABLED
  3922. { "Schemas regression tests" ,
  3923. schemasTest, "./test/schemas/*_*.xsd", NULL, NULL, NULL,
  3924. 0 },
  3925. { "Relax-NG regression tests" ,
  3926. rngTest, "./test/relaxng/*.rng", NULL, NULL, NULL,
  3927. XML_PARSE_DTDATTR | XML_PARSE_NOENT },
  3928. #ifdef LIBXML_READER_ENABLED
  3929. { "Relax-NG streaming regression tests" ,
  3930. rngStreamTest, "./test/relaxng/*.rng", NULL, NULL, NULL,
  3931. XML_PARSE_DTDATTR | XML_PARSE_NOENT },
  3932. #endif
  3933. #endif
  3934. #ifdef LIBXML_PATTERN_ENABLED
  3935. #ifdef LIBXML_READER_ENABLED
  3936. { "Pattern regression tests" ,
  3937. patternTest, "./test/pattern/*.pat", "result/pattern/", NULL, NULL,
  3938. 0 },
  3939. #endif
  3940. #endif
  3941. #ifdef LIBXML_C14N_ENABLED
  3942. { "C14N with comments regression tests" ,
  3943. c14nWithCommentTest, "./test/c14n/with-comments/*.xml", NULL, NULL, NULL,
  3944. 0 },
  3945. { "C14N without comments regression tests" ,
  3946. c14nWithoutCommentTest, "./test/c14n/without-comments/*.xml", NULL, NULL, NULL,
  3947. 0 },
  3948. { "C14N exclusive without comments regression tests" ,
  3949. c14nExcWithoutCommentTest, "./test/c14n/exc-without-comments/*.xml", NULL, NULL, NULL,
  3950. 0 },
  3951. { "C14N 1.1 without comments regression tests" ,
  3952. c14n11WithoutCommentTest, "./test/c14n/1-1-without-comments/*.xml", NULL, NULL, NULL,
  3953. 0 },
  3954. #endif
  3955. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_SAX1_ENABLED)
  3956. { "Catalog and Threads regression tests" ,
  3957. threadsTest, NULL, NULL, NULL, NULL,
  3958. 0 },
  3959. #endif
  3960. {NULL, NULL, NULL, NULL, NULL, NULL, 0}
  3961. };
  3962. /************************************************************************
  3963. * *
  3964. * The main code driving the tests *
  3965. * *
  3966. ************************************************************************/
  3967. static int
  3968. launchTests(testDescPtr tst) {
  3969. int res = 0, err = 0;
  3970. size_t i;
  3971. char *result;
  3972. char *error;
  3973. int mem;
  3974. if (tst == NULL) return(-1);
  3975. if (tst->in != NULL) {
  3976. glob_t globbuf;
  3977. globbuf.gl_offs = 0;
  3978. glob(tst->in, GLOB_DOOFFS, NULL, &globbuf);
  3979. for (i = 0;i < globbuf.gl_pathc;i++) {
  3980. if (!checkTestFile(globbuf.gl_pathv[i]))
  3981. continue;
  3982. if (tst->suffix != NULL) {
  3983. result = resultFilename(globbuf.gl_pathv[i], tst->out,
  3984. tst->suffix);
  3985. if (result == NULL) {
  3986. fprintf(stderr, "Out of memory !\n");
  3987. fatalError();
  3988. }
  3989. } else {
  3990. result = NULL;
  3991. }
  3992. if (tst->err != NULL) {
  3993. error = resultFilename(globbuf.gl_pathv[i], tst->out,
  3994. tst->err);
  3995. if (error == NULL) {
  3996. fprintf(stderr, "Out of memory !\n");
  3997. fatalError();
  3998. }
  3999. } else {
  4000. error = NULL;
  4001. }
  4002. if ((result) &&(!checkTestFile(result))) {
  4003. fprintf(stderr, "Missing result file %s\n", result);
  4004. } else if ((error) &&(!checkTestFile(error))) {
  4005. fprintf(stderr, "Missing error file %s\n", error);
  4006. } else {
  4007. mem = xmlMemUsed();
  4008. extraMemoryFromResolver = 0;
  4009. testErrorsSize = 0;
  4010. testErrors[0] = 0;
  4011. res = tst->func(globbuf.gl_pathv[i], result, error,
  4012. tst->options | XML_PARSE_COMPACT);
  4013. xmlResetLastError();
  4014. if (res != 0) {
  4015. fprintf(stderr, "File %s generated an error\n",
  4016. globbuf.gl_pathv[i]);
  4017. nb_errors++;
  4018. err++;
  4019. }
  4020. else if (xmlMemUsed() != mem) {
  4021. if ((xmlMemUsed() != mem) &&
  4022. (extraMemoryFromResolver == 0)) {
  4023. fprintf(stderr, "File %s leaked %d bytes\n",
  4024. globbuf.gl_pathv[i], xmlMemUsed() - mem);
  4025. nb_leaks++;
  4026. err++;
  4027. }
  4028. }
  4029. testErrorsSize = 0;
  4030. }
  4031. if (result)
  4032. free(result);
  4033. if (error)
  4034. free(error);
  4035. }
  4036. globfree(&globbuf);
  4037. } else {
  4038. testErrorsSize = 0;
  4039. testErrors[0] = 0;
  4040. extraMemoryFromResolver = 0;
  4041. res = tst->func(NULL, NULL, NULL, tst->options);
  4042. if (res != 0) {
  4043. nb_errors++;
  4044. err++;
  4045. }
  4046. }
  4047. return(err);
  4048. }
  4049. static int verbose = 0;
  4050. static int tests_quiet = 0;
  4051. static int
  4052. runtest(int i) {
  4053. int ret = 0, res;
  4054. int old_errors, old_tests, old_leaks;
  4055. old_errors = nb_errors;
  4056. old_tests = nb_tests;
  4057. old_leaks = nb_leaks;
  4058. if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL))
  4059. printf("## %s\n", testDescriptions[i].desc);
  4060. res = launchTests(&testDescriptions[i]);
  4061. if (res != 0)
  4062. ret++;
  4063. if (verbose) {
  4064. if ((nb_errors == old_errors) && (nb_leaks == old_leaks))
  4065. printf("Ran %d tests, no errors\n", nb_tests - old_tests);
  4066. else
  4067. printf("Ran %d tests, %d errors, %d leaks\n",
  4068. nb_tests - old_tests,
  4069. nb_errors - old_errors,
  4070. nb_leaks - old_leaks);
  4071. }
  4072. return(ret);
  4073. }
  4074. int
  4075. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  4076. int i, a, ret = 0;
  4077. int subset = 0;
  4078. initializeLibxml2();
  4079. for (a = 1; a < argc;a++) {
  4080. if (!strcmp(argv[a], "-v"))
  4081. verbose = 1;
  4082. else if (!strcmp(argv[a], "-quiet"))
  4083. tests_quiet = 1;
  4084. else {
  4085. for (i = 0; testDescriptions[i].func != NULL; i++) {
  4086. if (strstr(testDescriptions[i].desc, argv[a])) {
  4087. ret += runtest(i);
  4088. subset++;
  4089. }
  4090. }
  4091. }
  4092. }
  4093. if (subset == 0) {
  4094. for (i = 0; testDescriptions[i].func != NULL; i++) {
  4095. ret += runtest(i);
  4096. }
  4097. }
  4098. if ((nb_errors == 0) && (nb_leaks == 0)) {
  4099. ret = 0;
  4100. printf("Total %d tests, no errors\n",
  4101. nb_tests);
  4102. } else {
  4103. ret = 1;
  4104. printf("Total %d tests, %d errors, %d leaks\n",
  4105. nb_tests, nb_errors, nb_leaks);
  4106. }
  4107. xmlCleanupParser();
  4108. xmlMemoryDump();
  4109. return(ret);
  4110. }
  4111. #else /* ! LIBXML_OUTPUT_ENABLED */
  4112. int
  4113. main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  4114. fprintf(stderr, "runtest requires output to be enabled in libxml2\n");
  4115. return(1);
  4116. }
  4117. #endif