1 #!/usr/bin/python -u 2 # 3 # this tests the DTD validation with the XmlTextReader interface 4 # 5 import sys 6 import glob 7 import string 8 import libxml2 9 try: 10 import StringIO 11 str_io = StringIO.StringIO 12 except: 13 import io 14 str_io = io.StringIO 15 16 # Memory debug specific 17 libxml2.debugMemory(1) 18 19 err="" 20 expect="""../../test/valid/rss.xml:177: element rss: validity error : Element rss does not carry attribute version 21 </rss> 22 ^ 23 ../../test/valid/xlink.xml:450: element termdef: validity error : ID dt-arc already defined 24 <p><termdef id="dt-arc" term="Arc">An <ter 25 ^ 26 ../../test/valid/xlink.xml:530: validity error : attribute def line 199 references an unknown ID "dt-xlg" 27 28 ^ 29 """ 30 def callback(ctx, str): 31 global err 32 err = err + "%s" % (str) 33 libxml2.registerErrorHandler(callback, "") 34 35 valid_files = glob.glob("../../test/valid/*.x*") 36 valid_files.sort() 37 for file in valid_files: 38 if file.find("t8") != -1: 39 continue 40 if file == "../../test/valid/rss.xml": 41 continue 42 if file == "../../test/valid/xlink.xml": 43 continue 44 reader = libxml2.newTextReaderFilename(file) 45 #print "%s:" % (file) 46 reader.SetParserProp(libxml2.PARSER_VALIDATE, 1) 47 ret = reader.Read() 48 while ret == 1: 49 ret = reader.Read() 50 if ret != 0: 51 print("Error parsing and validating %s" % (file)) 52 #sys.exit(1) 53 54 if err != expect: 55 print(err) 56 57 # 58 # another separate test based on Stephane Bidoul one 59 # 60 s = """ 61 <!DOCTYPE test [ 62 <!ELEMENT test (x,b)> 63 <!ELEMENT x (c)> 64 <!ELEMENT b (#PCDATA)> 65 <!ELEMENT c (#PCDATA)> 66 <!ENTITY x "<x><c>xxx</c></x>"> 67 ]> 68 <test> 69 &x; 70 <b>bbb</b> 71 </test> 72 """ 73 expect="""10,test 74 1,test 75 14,#text 76 1,x 77 1,c 78 3,#text 79 15,c 80 15,x 81 14,#text 82 1,b 83 3,#text 84 15,b 85 14,#text 86 15,test 87 """ 88 res="" 89 err="" 90 91 input = libxml2.inputBuffer(str_io(s)) 92 reader = input.newTextReader("test2") 93 reader.SetParserProp(libxml2.PARSER_LOADDTD,1) 94 reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1) 95 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1) 96 reader.SetParserProp(libxml2.PARSER_VALIDATE,1) 97 while reader.Read() == 1: 98 res = res + "%s,%s\n" % (reader.NodeType(),reader.Name()) 99 100 if res != expect: 101 print("test2 failed: unexpected output") 102 print(res) 103 sys.exit(1) 104 if err != "": 105 print("test2 failed: validation error found") 106 print(err) 107 sys.exit(1) 108 109 # 110 # Another test for external entity parsing and validation 111 # 112 113 s = """<!DOCTYPE test [ 114 <!ELEMENT test (x)> 115 <!ELEMENT x (#PCDATA)> 116 <!ENTITY e SYSTEM "tst.ent"> 117 ]> 118 <test> 119 &e; 120 </test> 121 """ 122 tst_ent = """<x>hello</x>""" 123 expect="""10 test 124 1 test 125 14 #text 126 1 x 127 3 #text 128 15 x 129 14 #text 130 15 test 131 """ 132 res="" 133 134 def myResolver(URL, ID, ctxt): 135 if URL == "tst.ent": 136 return(str_io(tst_ent)) 137 return None 138 139 libxml2.setEntityLoader(myResolver) 140 141 input = libxml2.inputBuffer(str_io(s)) 142 reader = input.newTextReader("test3") 143 reader.SetParserProp(libxml2.PARSER_LOADDTD,1) 144 reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1) 145 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1) 146 reader.SetParserProp(libxml2.PARSER_VALIDATE,1) 147 while reader.Read() == 1: 148 res = res + "%s %s\n" % (reader.NodeType(),reader.Name()) 149 150 if res != expect: 151 print("test3 failed: unexpected output") 152 print(res) 153 sys.exit(1) 154 if err != "": 155 print("test3 failed: validation error found") 156 print(err) 157 sys.exit(1) 158 159 # 160 # Another test for recursive entity parsing, validation, and replacement of 161 # entities, making sure the entity ref node doesn't show up in that case 162 # 163 164 s = """<!DOCTYPE test [ 165 <!ELEMENT test (x, x)> 166 <!ELEMENT x (y)> 167 <!ELEMENT y (#PCDATA)> 168 <!ENTITY x "<x>&y;</x>"> 169 <!ENTITY y "<y>yyy</y>"> 170 ]> 171 <test> 172 &x; 173 &x; 174 </test>""" 175 expect="""10 test 0 176 1 test 0 177 14 #text 1 178 1 x 1 179 1 y 2 180 3 #text 3 181 15 y 2 182 15 x 1 183 14 #text 1 184 1 x 1 185 1 y 2 186 3 #text 3 187 15 y 2 188 15 x 1 189 14 #text 1 190 15 test 0 191 """ 192 res="" 193 err="" 194 195 input = libxml2.inputBuffer(str_io(s)) 196 reader = input.newTextReader("test4") 197 reader.SetParserProp(libxml2.PARSER_LOADDTD,1) 198 reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1) 199 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1) 200 reader.SetParserProp(libxml2.PARSER_VALIDATE,1) 201 while reader.Read() == 1: 202 res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth()) 203 204 if res != expect: 205 print("test4 failed: unexpected output") 206 print(res) 207 sys.exit(1) 208 if err != "": 209 print("test4 failed: validation error found") 210 print(err) 211 sys.exit(1) 212 213 # 214 # The same test but without entity substitution this time 215 # 216 217 s = """<!DOCTYPE test [ 218 <!ELEMENT test (x, x)> 219 <!ELEMENT x (y)> 220 <!ELEMENT y (#PCDATA)> 221 <!ENTITY x "<x>&y;</x>"> 222 <!ENTITY y "<y>yyy</y>"> 223 ]> 224 <test> 225 &x; 226 &x; 227 </test>""" 228 expect="""10 test 0 229 1 test 0 230 14 #text 1 231 5 x 1 232 14 #text 1 233 5 x 1 234 14 #text 1 235 15 test 0 236 """ 237 res="" 238 err="" 239 240 input = libxml2.inputBuffer(str_io(s)) 241 reader = input.newTextReader("test5") 242 reader.SetParserProp(libxml2.PARSER_VALIDATE,1) 243 while reader.Read() == 1: 244 res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth()) 245 246 if res != expect: 247 print("test5 failed: unexpected output") 248 print(res) 249 if err != "": 250 print("test5 failed: validation error found") 251 print(err) 252 253 # 254 # cleanup 255 # 256 del input 257 del reader 258 259 # Memory debug specific 260 libxml2.cleanupParser() 261 if libxml2.debugMemory(1) == 0: 262 print("OK") 263 else: 264 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 265 libxml2.dumpMemory() 266