check-relaxng-test-suite2.py 10 KB

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