2f3a6a0aefd2f7bd51d09635ea8a20e8b2d0ad36
[mat/samba.git] / source4 / scripting / python / samba / tests / dcerpc / testrpc.py
1 #!/usr/bin/env python
2 #
3 # test generated python code from pidl
4 # Andrew Tridgell August 2010
5 #
6 import sys
7
8 sys.path.insert(0, "bin/python")
9
10 import samba
11 import samba.tests
12 from samba.dcerpc import drsuapi
13 import talloc
14
15 talloc.enable_null_tracking()
16
17 class RpcTests(object):
18     '''test type behaviour of pidl generated python RPC code'''
19
20     def check_blocks(self, object, num_expected):
21         '''check that the number of allocated blocks is correct'''
22         nblocks = talloc.total_blocks(object)
23         if object is None:
24             nblocks -= self.initial_blocks
25         leaked_blocks = (nblocks - num_expected)
26         if leaked_blocks != 0:
27             print "Leaked %d blocks" % leaked_blocks
28
29     def check_type(self, interface, typename, type):
30         print "Checking type %s" % typename
31         v = type()
32         for n in dir(v):
33             if n[0] == '_':
34                 continue
35             try:
36                 value = getattr(v, n)
37             except TypeError, errstr:
38                 if str(errstr) == "unknown union level":
39                     print "ERROR: Unknown union level in %s.%s" % (typename, n)
40                     self.errcount += 1
41                     continue
42                 print str(errstr)[1:21]
43                 if str(errstr)[0:21] == "Can not convert C Type":
44                     print "ERROR: Unknown C type for %s.%s" % (typename, n)
45                     self.errcount += 1
46                     continue
47                 else:
48                     print "ERROR: Failed to instantiate %s.%s" % (typename, n)
49                     self.errcount += 1
50                     continue
51             except:
52                 print "ERROR: Failed to instantiate %s.%s" % (typename, n)
53                 self.errcount += 1
54                 continue
55
56             # now try setting the value back
57             try:
58                 print "Setting %s.%s" % (typename, n)
59                 setattr(v, n, value)
60             except Exception, e:
61                 if isinstance(e, AttributeError) and str(e).endswith("is read-only"):
62                     # readonly, ignore
63                     continue
64                 else:
65                     print "ERROR: Failed to set %s.%s: %r: %s" % (typename, n, e.__class__, e)
66                     self.errcount += 1
67                     continue
68
69             # and try a comparison
70             try:
71                 if value != getattr(v, n):
72                     print "ERROR: Comparison failed for %s.%s: %r != %r" % (typename, n, value, getattr(v, n))
73                     continue
74             except Exception, e:
75                 print "ERROR: compare exception for %s.%s: %r: %s" % (typename, n, e.__class__, e)
76                 continue
77
78     def check_interface(self, interface, iname):
79         errcount = self.errcount
80         for n in dir(interface):
81             if n[0] == '_' or n == iname:
82                 # skip the special ones
83                 continue
84             value = getattr(interface, n)
85             if isinstance(value, str):
86                 #print "%s=\"%s\"" % (n, value)
87                 pass
88             elif isinstance(value, int) or isinstance(value, long):
89                 #print "%s=%d" % (n, value)
90                 pass
91             elif isinstance(value, type):
92                 try:
93                     initial_blocks = talloc.total_blocks(None)
94                     self.check_type(interface, n, value)
95                     self.check_blocks(None, initial_blocks)
96                 except Exception, e:
97                     print "ERROR: Failed to check_type %s.%s: %r: %s" % (iname, n, e.__class__, e)
98                     self.errcount += 1
99             elif callable(value):
100                 pass # Method
101             else:
102                 print "UNKNOWN: %s=%s" % (n, value)
103         if self.errcount - errcount != 0:
104             print "Found %d errors in %s" % (self.errcount - errcount, iname)
105
106     def check_all_interfaces(self):
107         for iname in dir(samba.dcerpc):
108             if iname[0] == '_':
109                 continue
110             if iname == 'ClientConnection' or iname == 'base':
111                 continue
112             print "Checking interface %s" % iname
113             iface = getattr(samba.dcerpc, iname)
114             initial_blocks = talloc.total_blocks(None)
115             self.check_interface(iface, iname)
116             self.check_blocks(None, initial_blocks)
117
118     def run(self):
119         self.initial_blocks = talloc.total_blocks(None)
120         self.errcount = 0
121         self.check_all_interfaces()
122         return self.errcount
123
124 tests = RpcTests()
125 errcount = tests.run()
126 if errcount == 0:
127     sys.exit(0)
128 else:
129     print "%d failures" % errcount
130     sys.exit(1)