build: Change bin/default/python -> bin/python symlink to bin/default/python_modules
[Samba.git] / source4 / scripting / python / samba / tests / dcerpc / testrpc.py
blobe35d6b55446c7a0e2673350dd6956981ac393ce6
1 # test generated python code from pidl
2 # Copyright (C) Andrew Tridgell August 2010
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import sys
19 sys.path.insert(0, "bin/python")
21 import samba
22 import samba.tests
23 from samba.dcerpc import drsuapi
24 import talloc
26 talloc.enable_null_tracking()
28 class RpcTests(object):
29 '''test type behaviour of pidl generated python RPC code'''
31 def check_blocks(self, object, num_expected):
32 '''check that the number of allocated blocks is correct'''
33 nblocks = talloc.total_blocks(object)
34 if object is None:
35 nblocks -= self.initial_blocks
36 leaked_blocks = (nblocks - num_expected)
37 if leaked_blocks != 0:
38 print "Leaked %d blocks" % leaked_blocks
40 def check_type(self, interface, typename, type):
41 print "Checking type %s" % typename
42 v = type()
43 for n in dir(v):
44 if n[0] == '_':
45 continue
46 try:
47 value = getattr(v, n)
48 except TypeError, errstr:
49 if str(errstr) == "unknown union level":
50 print "ERROR: Unknown union level in %s.%s" % (typename, n)
51 self.errcount += 1
52 continue
53 print str(errstr)[1:21]
54 if str(errstr)[0:21] == "Can not convert C Type":
55 print "ERROR: Unknown C type for %s.%s" % (typename, n)
56 self.errcount += 1
57 continue
58 else:
59 print "ERROR: Failed to instantiate %s.%s" % (typename, n)
60 self.errcount += 1
61 continue
62 except Exception:
63 print "ERROR: Failed to instantiate %s.%s" % (typename, n)
64 self.errcount += 1
65 continue
67 # now try setting the value back
68 try:
69 print "Setting %s.%s" % (typename, n)
70 setattr(v, n, value)
71 except Exception, e:
72 if isinstance(e, AttributeError) and str(e).endswith("is read-only"):
73 # readonly, ignore
74 continue
75 else:
76 print "ERROR: Failed to set %s.%s: %r: %s" % (typename, n, e.__class__, e)
77 self.errcount += 1
78 continue
80 # and try a comparison
81 try:
82 if value != getattr(v, n):
83 print "ERROR: Comparison failed for %s.%s: %r != %r" % (typename, n, value, getattr(v, n))
84 continue
85 except Exception, e:
86 print "ERROR: compare exception for %s.%s: %r: %s" % (typename, n, e.__class__, e)
87 continue
89 def check_interface(self, interface, iname):
90 errcount = self.errcount
91 for n in dir(interface):
92 if n[0] == '_' or n == iname:
93 # skip the special ones
94 continue
95 value = getattr(interface, n)
96 if isinstance(value, str):
97 #print "%s=\"%s\"" % (n, value)
98 pass
99 elif isinstance(value, int) or isinstance(value, long):
100 #print "%s=%d" % (n, value)
101 pass
102 elif isinstance(value, type):
103 try:
104 initial_blocks = talloc.total_blocks(None)
105 self.check_type(interface, n, value)
106 self.check_blocks(None, initial_blocks)
107 except Exception, e:
108 print "ERROR: Failed to check_type %s.%s: %r: %s" % (iname, n, e.__class__, e)
109 self.errcount += 1
110 elif callable(value):
111 pass # Method
112 else:
113 print "UNKNOWN: %s=%s" % (n, value)
114 if self.errcount - errcount != 0:
115 print "Found %d errors in %s" % (self.errcount - errcount, iname)
117 def check_all_interfaces(self):
118 for iname in dir(samba.dcerpc):
119 if iname[0] == '_':
120 continue
121 if iname == 'ClientConnection' or iname == 'base':
122 continue
123 print "Checking interface %s" % iname
124 iface = getattr(samba.dcerpc, iname)
125 initial_blocks = talloc.total_blocks(None)
126 self.check_interface(iface, iname)
127 self.check_blocks(None, initial_blocks)
129 def run(self):
130 self.initial_blocks = talloc.total_blocks(None)
131 self.errcount = 0
132 self.check_all_interfaces()
133 return self.errcount
135 tests = RpcTests()
136 errcount = tests.run()
137 if errcount == 0:
138 sys.exit(0)
139 else:
140 print "%d failures" % errcount
141 sys.exit(1)