check-relaxng-test-suite.py 9.8 KB

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