1 | #!/usr/bin/env python3
|
---|
2 | import setup_test
|
---|
3 | import libxml2
|
---|
4 | import sys
|
---|
5 |
|
---|
6 | ARG = 'test string'
|
---|
7 |
|
---|
8 | class ErrorHandler:
|
---|
9 |
|
---|
10 | def __init__(self):
|
---|
11 | self.errors = []
|
---|
12 |
|
---|
13 | def handler(self, msg, data):
|
---|
14 | if data != ARG:
|
---|
15 | raise Exception("Error handler did not receive correct argument")
|
---|
16 | self.errors.append(msg)
|
---|
17 |
|
---|
18 |
|
---|
19 | # Memory debug specific
|
---|
20 | libxml2.debugMemory(1)
|
---|
21 |
|
---|
22 | dtd="""<!ELEMENT foo EMPTY>"""
|
---|
23 | valid="""<?xml version="1.0"?>
|
---|
24 | <foo></foo>"""
|
---|
25 |
|
---|
26 | invalid="""<?xml version="1.0"?>
|
---|
27 | <foo><bar/></foo>"""
|
---|
28 |
|
---|
29 | dtd = libxml2.parseDTD(None, 'test.dtd')
|
---|
30 | ctxt = libxml2.newValidCtxt()
|
---|
31 | e = ErrorHandler()
|
---|
32 | ctxt.setValidityErrorHandler(e.handler, e.handler, ARG)
|
---|
33 |
|
---|
34 | # Test valid document
|
---|
35 | doc = libxml2.parseDoc(valid)
|
---|
36 | ret = doc.validateDtd(ctxt, dtd)
|
---|
37 | if ret != 1 or e.errors:
|
---|
38 | print("error doing DTD validation")
|
---|
39 | sys.exit(1)
|
---|
40 | doc.freeDoc()
|
---|
41 |
|
---|
42 | # Test invalid document
|
---|
43 | doc = libxml2.parseDoc(invalid)
|
---|
44 | ret = doc.validateDtd(ctxt, dtd)
|
---|
45 | if ret != 0 or not e.errors:
|
---|
46 | print("Error: document supposed to be invalid")
|
---|
47 | doc.freeDoc()
|
---|
48 |
|
---|
49 | dtd.freeDtd()
|
---|
50 | del dtd
|
---|
51 | del ctxt
|
---|
52 |
|
---|
53 | # Memory debug specific
|
---|
54 | libxml2.cleanupParser()
|
---|
55 | if libxml2.debugMemory(1) == 0:
|
---|
56 | print("OK")
|
---|
57 | else:
|
---|
58 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
59 |
|
---|