xref: /aosp_15_r20/external/libxml2/python/tests/validate.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2import sys
3import setup_test
4import libxml2
5
6# Memory debug specific
7libxml2.debugMemory(1)
8
9ctxt = libxml2.createFileParserCtxt("valid.xml")
10ctxt.validate(1)
11ctxt.parseDocument()
12doc = ctxt.doc()
13valid = ctxt.isValid()
14
15if doc.name != "valid.xml":
16    print("doc.name failed")
17    sys.exit(1)
18root = doc.children
19if root.name != "doc":
20    print("root.name failed")
21    sys.exit(1)
22if valid != 1:
23    print("validity check failed")
24    sys.exit(1)
25doc.freeDoc()
26
27i = 1000
28while 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
41def noerr(ctx, str):
42    pass
43
44libxml2.registerErrorHandler(noerr, None)
45
46ctxt = libxml2.createFileParserCtxt("invalid.xml")
47ctxt.validate(1)
48ctxt.parseDocument()
49doc = ctxt.doc()
50valid = ctxt.isValid()
51if doc.name != "invalid.xml":
52    print("doc.name failed")
53    sys.exit(1)
54root = doc.children
55if root.name != "doc":
56    print("root.name failed")
57    sys.exit(1)
58if valid != 0:
59    print("validity check failed")
60    sys.exit(1)
61doc.freeDoc()
62
63i = 1000
64while 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
75del ctxt
76
77# Memory debug specific
78libxml2.cleanupParser()
79if libxml2.debugMemory(1) == 0:
80    print("OK")
81else:
82    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
83