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