1#!/usr/bin/env python3 2# -*- coding: ISO-8859-1 -*- 3import sys 4import setup_test 5import libxml2 6 7# Memory debug specific 8libxml2.debugMemory(1) 9 10ctxt = None 11 12class callback: 13 def __init__(self, startd, starte, ende, delta, endd): 14 self.startd = startd 15 self.starte = starte 16 self.ende = ende 17 self.endd = endd 18 self.delta = delta 19 self.count = 0 20 21 def startDocument(self): 22 global ctxt 23 if ctxt.byteConsumed() != self.startd: 24 print("document start at wrong index: %d expecting %d\n" % ( 25 ctxt.byteConsumed(), self.startd)) 26 sys.exit(1) 27 28 def endDocument(self): 29 global ctxt 30 expect = self.ende + self.delta * (self.count - 1) + self.endd 31 if ctxt.byteConsumed() != expect: 32 print("document end at wrong index: %d expecting %d\n" % ( 33 ctxt.byteConsumed(), expect)) 34 sys.exit(1) 35 36 def startElement(self, tag, attrs): 37 global ctxt 38 if tag == "bar1": 39 expect = self.starte + self.delta * self.count 40 if ctxt.byteConsumed() != expect: 41 print("element start at wrong index: %d expecting %d\n" % ( 42 ctxt.byteConsumed(), expect)) 43 sys.exit(1) 44 45 46 def endElement(self, tag): 47 global ctxt 48 if tag == "bar1": 49 expect = self.ende + self.delta * self.count 50 if ctxt.byteConsumed() != expect: 51 print("element end at wrong index: %d expecting %d\n" % ( 52 ctxt.byteConsumed(), expect)) 53 sys.exit(1) 54 self.count = self.count + 1 55 56 def characters(self, data): 57 pass 58 59# 60# First run a pure UTF-8 test 61# 62handler = callback(0, 13, 27, 198, 183) 63ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml") 64chunk = """ <bar1>chars1</bar1> 65 <bar2>chars2</bar2> 66 <bar3>chars3</bar3> 67 <bar4>chars4</bar4> 68 <bar5>chars5</bar5> 69 <bar6><s6</bar6> 70 <bar7>chars7</bar7> 71 <bar8>&8</bar8> 72 <bar9>chars9</bar9> 73""" 74i = 0 75while i < 10000: 76 ctxt.parseChunk(chunk, len(chunk), 0) 77 i = i + 1 78chunk = "</foo>" 79ctxt.parseChunk(chunk, len(chunk), 1) 80ctxt=None 81 82# 83# Then run a test relying on ISO-Latin-1 84# 85handler = callback(43, 57, 71, 198, 183) 86chunk="""<?xml version="1.0" encoding="ISO-8859-1"?> 87<foo> 88""" 89ctxt = libxml2.createPushParser(handler, chunk, len(chunk), "test.xml") 90chunk = """ <bar1>chars1</bar1> 91 <bar2>chars2</bar2> 92 <bar3>chars3</bar3> 93 <bar4>ch�rs4</bar4> 94 <bar5>chars5</bar5> 95 <bar6><s6</bar6> 96 <bar7>chars7</bar7> 97 <bar8>&8</bar8> 98 <bar9>tr�s 9</bar9> 99""" 100i = 0 101while i < 10000: 102 ctxt.parseChunk(chunk, len(chunk), 0) 103 i = i + 1 104chunk = "</foo>" 105ctxt.parseChunk(chunk, len(chunk), 1) 106ctxt=None 107 108# Memory debug specific 109libxml2.cleanupParser() 110if libxml2.debugMemory(1) == 0: 111 print("OK") 112else: 113 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 114