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