validate.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4. # Memory debug specific
  5. libxml2.debugMemory(1)
  6. ctxt = libxml2.createFileParserCtxt("valid.xml")
  7. ctxt.validate(1)
  8. ctxt.parseDocument()
  9. doc = ctxt.doc()
  10. valid = ctxt.isValid()
  11. if doc.name != "valid.xml":
  12. print "doc.name failed"
  13. sys.exit(1)
  14. root = doc.children
  15. if root.name != "doc":
  16. print "root.name failed"
  17. sys.exit(1)
  18. if valid != 1:
  19. print "validity chec failed"
  20. sys.exit(1)
  21. doc.freeDoc()
  22. i = 1000
  23. while i > 0:
  24. ctxt = libxml2.createFileParserCtxt("valid.xml")
  25. ctxt.validate(1)
  26. ctxt.parseDocument()
  27. doc = ctxt.doc()
  28. valid = ctxt.isValid()
  29. doc.freeDoc()
  30. if valid != 1:
  31. print "validity check failed"
  32. sys.exit(1)
  33. i = i - 1
  34. #desactivate error messages from the validation
  35. def noerr(ctx, str):
  36. pass
  37. libxml2.registerErrorHandler(noerr, None)
  38. ctxt = libxml2.createFileParserCtxt("invalid.xml")
  39. ctxt.validate(1)
  40. ctxt.parseDocument()
  41. doc = ctxt.doc()
  42. valid = ctxt.isValid()
  43. if doc.name != "invalid.xml":
  44. print "doc.name failed"
  45. sys.exit(1)
  46. root = doc.children
  47. if root.name != "doc":
  48. print "root.name failed"
  49. sys.exit(1)
  50. if valid != 0:
  51. print "validity chec failed"
  52. sys.exit(1)
  53. doc.freeDoc()
  54. i = 1000
  55. while i > 0:
  56. ctxt = libxml2.createFileParserCtxt("invalid.xml")
  57. ctxt.validate(1)
  58. ctxt.parseDocument()
  59. doc = ctxt.doc()
  60. valid = ctxt.isValid()
  61. doc.freeDoc()
  62. if valid != 0:
  63. print "validity check failed"
  64. sys.exit(1)
  65. i = i - 1
  66. del ctxt
  67. # Memory debug specific
  68. libxml2.cleanupParser()
  69. if libxml2.debugMemory(1) == 0:
  70. print "OK"
  71. else:
  72. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  73. libxml2.dumpMemory()