runtest.c 138 KB

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