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