1 | #!/usr/bin/python -u
|
---|
2 | #
|
---|
3 | # this tests the entities substitutions with the XmlTextReader interface
|
---|
4 | #
|
---|
5 | import sys
|
---|
6 | import libxml2
|
---|
7 | try:
|
---|
8 | import StringIO
|
---|
9 | str_io = StringIO.StringIO
|
---|
10 | except:
|
---|
11 | import io
|
---|
12 | str_io = io.StringIO
|
---|
13 |
|
---|
14 | docstr="""<?xml version='1.0'?>
|
---|
15 | <!DOCTYPE doc [
|
---|
16 | <!ENTITY tst "<p>test</p>">
|
---|
17 | ]>
|
---|
18 | <doc>&tst;</doc>"""
|
---|
19 |
|
---|
20 | # Memory debug specific
|
---|
21 | libxml2.debugMemory(1)
|
---|
22 |
|
---|
23 | #
|
---|
24 | # First test, normal don't substitute entities.
|
---|
25 | #
|
---|
26 | f = str_io(docstr)
|
---|
27 | input = libxml2.inputBuffer(f)
|
---|
28 | reader = input.newTextReader("test_noent")
|
---|
29 | ret = reader.Read()
|
---|
30 | if ret != 1:
|
---|
31 | print("Error reading to root")
|
---|
32 | sys.exit(1)
|
---|
33 | if reader.Name() == "doc" or reader.NodeType() == 10:
|
---|
34 | ret = reader.Read()
|
---|
35 | if ret != 1:
|
---|
36 | print("Error reading to root")
|
---|
37 | sys.exit(1)
|
---|
38 | if reader.Name() != "doc" or reader.NodeType() != 1:
|
---|
39 | print("test_normal: Error reading the root element")
|
---|
40 | sys.exit(1)
|
---|
41 | ret = reader.Read()
|
---|
42 | if ret != 1:
|
---|
43 | print("test_normal: Error reading to the entity")
|
---|
44 | sys.exit(1)
|
---|
45 | if reader.Name() != "tst" or reader.NodeType() != 5:
|
---|
46 | print("test_normal: Error reading the entity")
|
---|
47 | sys.exit(1)
|
---|
48 | ret = reader.Read()
|
---|
49 | if ret != 1:
|
---|
50 | print("test_normal: Error reading to the end of root")
|
---|
51 | sys.exit(1)
|
---|
52 | if reader.Name() != "doc" or reader.NodeType() != 15:
|
---|
53 | print("test_normal: Error reading the end of the root element")
|
---|
54 | sys.exit(1)
|
---|
55 | ret = reader.Read()
|
---|
56 | if ret != 0:
|
---|
57 | print("test_normal: Error detecting the end")
|
---|
58 | sys.exit(1)
|
---|
59 |
|
---|
60 | #
|
---|
61 | # Second test, completely substitute the entities.
|
---|
62 | #
|
---|
63 | f = str_io(docstr)
|
---|
64 | input = libxml2.inputBuffer(f)
|
---|
65 | reader = input.newTextReader("test_noent")
|
---|
66 | reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
|
---|
67 | ret = reader.Read()
|
---|
68 | if ret != 1:
|
---|
69 | print("Error reading to root")
|
---|
70 | sys.exit(1)
|
---|
71 | if reader.Name() == "doc" or reader.NodeType() == 10:
|
---|
72 | ret = reader.Read()
|
---|
73 | if ret != 1:
|
---|
74 | print("Error reading to root")
|
---|
75 | sys.exit(1)
|
---|
76 | if reader.Name() != "doc" or reader.NodeType() != 1:
|
---|
77 | print("test_noent: Error reading the root element")
|
---|
78 | sys.exit(1)
|
---|
79 | ret = reader.Read()
|
---|
80 | if ret != 1:
|
---|
81 | print("test_noent: Error reading to the entity content")
|
---|
82 | sys.exit(1)
|
---|
83 | if reader.Name() != "p" or reader.NodeType() != 1:
|
---|
84 | print("test_noent: Error reading the p element from entity")
|
---|
85 | sys.exit(1)
|
---|
86 | ret = reader.Read()
|
---|
87 | if ret != 1:
|
---|
88 | print("test_noent: Error reading to the text node")
|
---|
89 | sys.exit(1)
|
---|
90 | if reader.NodeType() != 3 or reader.Value() != "test":
|
---|
91 | print("test_noent: Error reading the text node")
|
---|
92 | sys.exit(1)
|
---|
93 | ret = reader.Read()
|
---|
94 | if ret != 1:
|
---|
95 | print("test_noent: Error reading to the end of p element")
|
---|
96 | sys.exit(1)
|
---|
97 | if reader.Name() != "p" or reader.NodeType() != 15:
|
---|
98 | print("test_noent: Error reading the end of the p element")
|
---|
99 | sys.exit(1)
|
---|
100 | ret = reader.Read()
|
---|
101 | if ret != 1:
|
---|
102 | print("test_noent: Error reading to the end of root")
|
---|
103 | sys.exit(1)
|
---|
104 | if reader.Name() != "doc" or reader.NodeType() != 15:
|
---|
105 | print("test_noent: Error reading the end of the root element")
|
---|
106 | sys.exit(1)
|
---|
107 | ret = reader.Read()
|
---|
108 | if ret != 0:
|
---|
109 | print("test_noent: Error detecting the end")
|
---|
110 | sys.exit(1)
|
---|
111 |
|
---|
112 | #
|
---|
113 | # third test, crazy stuff about empty element in external parsed entities
|
---|
114 | #
|
---|
115 | s = """<!DOCTYPE struct [
|
---|
116 | <!ENTITY simplestruct2.ent SYSTEM "simplestruct2.ent">
|
---|
117 | ]>
|
---|
118 | <struct>&simplestruct2.ent;</struct>
|
---|
119 | """
|
---|
120 | expect="""10 struct 0 0
|
---|
121 | 1 struct 0 0
|
---|
122 | 1 descr 1 1
|
---|
123 | 15 struct 0 0
|
---|
124 | """
|
---|
125 | res=""
|
---|
126 | simplestruct2_ent="""<descr/>"""
|
---|
127 |
|
---|
128 | def myResolver(URL, ID, ctxt):
|
---|
129 | if URL == "simplestruct2.ent":
|
---|
130 | return(str_io(simplestruct2_ent))
|
---|
131 | return None
|
---|
132 |
|
---|
133 | libxml2.setEntityLoader(myResolver)
|
---|
134 |
|
---|
135 | input = libxml2.inputBuffer(str_io(s))
|
---|
136 | reader = input.newTextReader("test3")
|
---|
137 | reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
|
---|
138 | while reader.Read() == 1:
|
---|
139 | res = res + "%s %s %d %d\n" % (reader.NodeType(),reader.Name(),
|
---|
140 | reader.Depth(),reader.IsEmptyElement())
|
---|
141 |
|
---|
142 | if res != expect:
|
---|
143 | print("test3 failed: unexpected output")
|
---|
144 | print(res)
|
---|
145 | sys.exit(1)
|
---|
146 |
|
---|
147 | #
|
---|
148 | # cleanup
|
---|
149 | #
|
---|
150 | del f
|
---|
151 | del input
|
---|
152 | del reader
|
---|
153 |
|
---|
154 | # Memory debug specific
|
---|
155 | libxml2.cleanupParser()
|
---|
156 | if libxml2.debugMemory(1) == 0:
|
---|
157 | print("OK")
|
---|
158 | else:
|
---|
159 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
160 | libxml2.dumpMemory()
|
---|