blob: 6a2b6eb96d75f780aabfdf708c3ef31ea575997b [file] [log] [blame]
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001#!/usr/bin/python
2import sys
3import time
4import os
5import string
6import StringIO
7sys.path.append("python")
8import libxml2
9
10# Memory debug specific
11libxml2.debugMemory(1)
12debug = 0
13
14#
15# the testsuite description
16#
17CONF="test/relaxng/testsuite.xml"
18LOG="check-relaxng-test-suite2.log"
19
20log = open(LOG, "w")
21nb_schemas_tests = 0
22nb_schemas_success = 0
23nb_schemas_failed = 0
24nb_instances_tests = 0
25nb_instances_success = 0
26nb_instances_failed = 0
27
28libxml2.lineNumbersDefault(1)
29#
30# Resolver callback
31#
32resources = {}
33def resolver(URL, ID, ctxt):
34 global resources
35
36 if resources.has_key(URL):
37 return(StringIO.StringIO(resources[URL]))
38 log.write("Resolver failure: asked %s\n" % (URL))
39 log.write("resources: %s\n" % (resources))
40 return None
41
42#
43# Load the previous results
44#
45#results = {}
46#previous = {}
47#
48#try:
49# res = libxml2.parseFile(RES)
50#except:
51# log.write("Could not parse %s" % (RES))
52
53#
54# handle a valid instance
55#
56def handle_valid(node, schema):
57 global log
58 global nb_instances_success
59 global nb_instances_failed
60
61 instance = node.prop("dtd")
62 if instance == None:
63 instance = ""
64 child = node.children
65 while child != None:
66 if child.type != 'text':
67 instance = instance + child.serialize()
68 child = child.next
69
70 mem = libxml2.debugMemory(1);
71 try:
72 doc = libxml2.parseDoc(instance)
73 except:
74 doc = None
75
76 if doc == None:
77 log.write("\nFailed to parse correct instance:\n-----\n")
78 log.write(instance)
79 log.write("\n-----\n")
80 nb_instances_failed = nb_instances_failed + 1
81 return
82
83 if debug:
84 print "instance line %d" % (node.lineNo())
85
86 try:
87 ctxt = schema.relaxNGNewValidCtxt()
88 ret = doc.relaxNGValidateDoc(ctxt)
89 del ctxt
90 except:
91 ret = -1
92
93 doc.freeDoc()
94 if mem != libxml2.debugMemory(1):
95 print "validating instance %d line %d leaks" % (
96 nb_instances_tests, node.lineNo())
97
98 if ret != 0:
99 log.write("\nFailed to validate correct instance:\n-----\n")
100 log.write(instance)
101 log.write("\n-----\n")
102 nb_instances_failed = nb_instances_failed + 1
103 else:
104 nb_instances_success = nb_instances_success + 1
105
106#
107# handle an invalid instance
108#
109def handle_invalid(node, schema):
110 global log
111 global nb_instances_success
112 global nb_instances_failed
113
114 instance = node.prop("dtd")
115 if instance == None:
116 instance = ""
117 child = node.children
118 while child != None:
119 if child.type != 'text':
120 instance = instance + child.serialize()
121 child = child.next
122
123 mem = libxml2.debugMemory(1);
124
125 try:
126 doc = libxml2.parseDoc(instance)
127 except:
128 doc = None
129
130 if doc == None:
131 log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
132 log.write(instance)
133 log.write("\n-----\n")
134 return
135
136 if debug:
137 print "instance line %d" % (node.lineNo())
138
139 try:
140 ctxt = schema.relaxNGNewValidCtxt()
141 ret = doc.relaxNGValidateDoc(ctxt)
142 del ctxt
143
144 except:
145 ret = -1
146
147 doc.freeDoc()
148 if mem != libxml2.debugMemory(1):
149 print "validating instance %d line %d leaks" % (
150 nb_instances_tests, node.lineNo())
151
152 if ret == 0:
153 log.write("\nFailed to detect validation problem in instance:\n-----\n")
154 log.write(instance)
155 log.write("\n-----\n")
156 nb_instances_failed = nb_instances_failed + 1
157 else:
158 nb_instances_success = nb_instances_success + 1
159
160#
161# handle an incorrect test
162#
163def handle_correct(node):
164 global log
165 global nb_schemas_success
166 global nb_schemas_failed
167
168 schema = ""
169 child = node.children
170 while child != None:
171 if child.type != 'text':
172 schema = schema + child.serialize()
173 child = child.next
174
175 try:
176 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
177 rngs = rngp.relaxNGParse()
178 except:
179 rngs = None
180 if rngs == None:
181 log.write("\nFailed to compile correct schema:\n-----\n")
182 log.write(schema)
183 log.write("\n-----\n")
184 nb_schemas_failed = nb_schemas_failed + 1
185 else:
186 nb_schemas_success = nb_schemas_success + 1
187 return rngs
188
189def handle_incorrect(node):
190 global log
191 global nb_schemas_success
192 global nb_schemas_failed
193
194 schema = ""
195 child = node.children
196 while child != None:
197 if child.type != 'text':
198 schema = schema + child.serialize()
199 child = child.next
200
201 try:
202 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
203 rngs = rngp.relaxNGParse()
204 except:
205 rngs = None
206 if rngs != None:
207 log.write("\nFailed to detect schema error in:\n-----\n")
208 log.write(schema)
209 log.write("\n-----\n")
210 nb_schemas_failed = nb_schemas_failed + 1
211 else:
212# log.write("\nSuccess detecting schema error in:\n-----\n")
213# log.write(schema)
214# log.write("\n-----\n")
215 nb_schemas_success = nb_schemas_success + 1
216 return None
217
218#
219# resource handling: keep a dictionary of URL->string mappings
220#
221def handle_resource(node, dir):
222 global resources
223
224 try:
225 name = node.prop('name')
226 except:
227 name = None
228
229 if name == None or name == '':
230 log.write("resource has no name")
231 return;
232
233 if dir != None:
234# name = libxml2.buildURI(name, dir)
235 name = dir + '/' + name
236
237 res = ""
238 child = node.children
239 while child != None:
240 if child.type != 'text':
241 res = res + child.serialize()
242 child = child.next
243 resources[name] = res
244
245#
246# dir handling: pseudo directory resources
247#
248def handle_dir(node, dir):
249 try:
250 name = node.prop('name')
251 except:
252 name = None
253
254 if name == None or name == '':
255 log.write("resource has no name")
256 return;
257
258 if dir != None:
259# name = libxml2.buildURI(name, dir)
260 name = dir + '/' + name
261
262 dirs = node.xpathEval('dir')
263 for dir in dirs:
264 handle_dir(dir, name)
265 res = node.xpathEval('resource')
266 for r in res:
267 handle_resource(r, name)
268
269#
270# handle a testCase element
271#
272def handle_testCase(node):
273 global nb_schemas_tests
274 global nb_instances_tests
275 global resources
276
277 sections = node.xpathEval('string(section)')
278 log.write("\n ======== test %d line %d section %s ==========\n" % (
279
280 nb_schemas_tests, node.lineNo(), sections))
281 resources = {}
282 if debug:
283 print "test %d line %d" % (nb_schemas_tests, node.lineNo())
284
285 dirs = node.xpathEval('dir')
286 for dir in dirs:
287 handle_dir(dir, None)
288 res = node.xpathEval('resource')
289 for r in res:
290 handle_resource(r, None)
291
292 tsts = node.xpathEval('incorrect')
293 if tsts != []:
294 if len(tsts) != 1:
295 print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
296 schema = handle_incorrect(tsts[0])
297 else:
298 tsts = node.xpathEval('correct')
299 if tsts != []:
300 if len(tsts) != 1:
301 print "warning test line %d has more than one <correct> example"% (node.lineNo())
302 schema = handle_correct(tsts[0])
303 else:
304 print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
305
306 nb_schemas_tests = nb_schemas_tests + 1;
307
308 valids = node.xpathEval('valid')
309 invalids = node.xpathEval('invalid')
310 nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
311 if schema != None:
312 for valid in valids:
313 handle_valid(valid, schema)
314 for invalid in invalids:
315 handle_invalid(invalid, schema)
316
317
318#
319# handle a testSuite element
320#
321def handle_testSuite(node, level = 0):
322 global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
323 global nb_instances_tests, nb_instances_success, nb_instances_failed
324 if level >= 1:
325 old_schemas_tests = nb_schemas_tests
326 old_schemas_success = nb_schemas_success
327 old_schemas_failed = nb_schemas_failed
328 old_instances_tests = nb_instances_tests
329 old_instances_success = nb_instances_success
330 old_instances_failed = nb_instances_failed
331
332 docs = node.xpathEval('documentation')
333 authors = node.xpathEval('author')
334 if docs != []:
335 msg = ""
336 for doc in docs:
337 msg = msg + doc.content + " "
338 if authors != []:
339 msg = msg + "written by "
340 for author in authors:
341 msg = msg + author.content + " "
342 print msg
343 sections = node.xpathEval('section')
344 if sections != [] and level <= 0:
345 msg = ""
346 for section in sections:
347 msg = msg + section.content + " "
348 print "Tests for section %s" % (msg)
349 for test in node.xpathEval('testCase'):
350 handle_testCase(test)
351 for test in node.xpathEval('testSuite'):
352 handle_testSuite(test, level + 1)
353
354
355 if level >= 1 and sections != []:
356 msg = ""
357 for section in sections:
358 msg = msg + section.content + " "
359 print "Result of tests for section %s" % (msg)
360 if nb_schemas_tests != old_schemas_tests:
361 print "found %d test schemas: %d success %d failures" % (
362 nb_schemas_tests - old_schemas_tests,
363 nb_schemas_success - old_schemas_success,
364 nb_schemas_failed - old_schemas_failed)
365 if nb_instances_tests != old_instances_tests:
366 print "found %d test instances: %d success %d failures" % (
367 nb_instances_tests - old_instances_tests,
368 nb_instances_success - old_instances_success,
369 nb_instances_failed - old_instances_failed)
370#
371# Parse the conf file
372#
373libxml2.substituteEntitiesDefault(1);
374testsuite = libxml2.parseFile(CONF)
375
376#
377# Error and warnng callbacks
378#
379def callback(ctx, str):
380 global log
381 log.write("%s%s" % (ctx, str))
382
383libxml2.registerErrorHandler(callback, "")
384
385libxml2.setEntityLoader(resolver)
386root = testsuite.getRootElement()
387if root.name != 'testSuite':
388 print "%s doesn't start with a testSuite element, aborting" % (CONF)
389 sys.exit(1)
390print "Running Relax NG testsuite"
391handle_testSuite(root)
392
393print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
394 nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
395print "found %d test instances: %d success %d failures" % (
396 nb_instances_tests, nb_instances_success, nb_instances_failed)
397
398testsuite.freeDoc()
399
400# Memory debug specific
401libxml2.relaxNGCleanupTypes()
402libxml2.cleanupParser()
403if libxml2.debugMemory(1) == 0:
404 print "OK"
405else:
406 print "Memory leak %d bytes" % (libxml2.debugMemory(1))
407 libxml2.dumpMemory()