Imported Upstream version 2.9.1
[platform/upstream/libxml2.git] / python / tests / xpathleak.py
1 #!/usr/bin/python
2 import sys, libxml2
3
4 libxml2.debugMemory(True)
5
6 expect="""--> Invalid expression
7 --> xmlXPathEval: evaluation failed
8 --> Invalid expression
9 --> xmlXPathEval: evaluation failed
10 --> Invalid expression
11 --> xmlXPathEval: evaluation failed
12 --> Invalid expression
13 --> xmlXPathEval: evaluation failed
14 --> Invalid expression
15 --> xmlXPathEval: evaluation failed
16 --> Invalid expression
17 --> xmlXPathEval: evaluation failed
18 --> Invalid expression
19 --> xmlXPathEval: evaluation failed
20 --> Invalid expression
21 --> xmlXPathEval: evaluation failed
22 --> Invalid expression
23 --> xmlXPathEval: evaluation failed
24 --> Invalid expression
25 --> xmlXPathEval: evaluation failed
26 """
27 err=""
28 def callback(ctx, str):
29      global err
30
31      err = err + "%s %s" % (ctx, str)
32
33 libxml2.registerErrorHandler(callback, "-->")
34
35 doc = libxml2.parseDoc("<fish/>")
36 ctxt = doc.xpathNewContext()
37 ctxt.setContextNode(doc)
38 badexprs = (
39         ":false()", "bad:()", "bad(:)", ":bad(:)", "bad:(:)", "bad:bad(:)",
40         "a:/b", "/c:/d", "//e:/f", "g://h"
41         )
42 for expr in badexprs:
43         try:
44                 ctxt.xpathEval(expr)
45         except libxml2.xpathError:
46                 pass
47         else:
48                 print("Unexpectedly legal expression:", expr)
49 ctxt.xpathFreeContext()
50 doc.freeDoc()
51
52 if err != expect:
53     print("error")
54     print("received %s" %(err))
55     print("expected %s" %(expect))
56     sys.exit(1)
57
58 libxml2.cleanupParser()
59 leakedbytes = libxml2.debugMemory(True)
60 if leakedbytes == 0:
61         print("OK")
62 else:
63         print("Memory leak", leakedbytes, "bytes")
64         # drop file to .memdump file in cwd, but won't work if not compiled in
65         libxml2.dumpMemory()