runtest.c 122 KB

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