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