summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/dcerpc/testrpc.py
blob: 789ea9af9768f3e1399a50e3910a642888d6ffe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# test generated python code from pidl
# Copyright (C) Andrew Tridgell August 2010
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
import sys

sys.path.insert(0, "bin/python")

import samba
import samba.tests
from samba.dcerpc import drsuapi
import talloc

talloc.enable_null_tracking()


class RpcTests(object):
    """test type behaviour of pidl generated python RPC code"""

    def check_blocks(self, object, num_expected):
        """check that the number of allocated blocks is correct"""
        nblocks = talloc.total_blocks(object)
        if object is None:
            nblocks -= self.initial_blocks
        leaked_blocks = (nblocks - num_expected)
        if leaked_blocks != 0:
            print("Leaked %d blocks" % leaked_blocks)

    def check_type(self, interface, typename, type):
        print("Checking type %s" % typename)
        v = type()
        for n in dir(v):
            if n[0] == '_':
                continue
            try:
                value = getattr(v, n)
            except TypeError as errstr:
                if str(errstr) == "unknown union level":
                    print("ERROR: Unknown union level in %s.%s" % (typename, n))
                    self.errcount += 1
                    continue
                print(str(errstr)[1:21])
                if str(errstr)[0:21] == "Can not convert C Type":
                    print("ERROR: Unknown C type for %s.%s" % (typename, n))
                    self.errcount += 1
                    continue
                else:
                    print("ERROR: Failed to instantiate %s.%s" % (typename, n))
                    self.errcount += 1
                    continue
            except Exception:
                print("ERROR: Failed to instantiate %s.%s" % (typename, n))
                self.errcount += 1
                continue

            # now try setting the value back
            try:
                print("Setting %s.%s" % (typename, n))
                setattr(v, n, value)
            except Exception as e:
                if isinstance(e, AttributeError) and str(e).endswith("is read-only"):
                    # readonly, ignore
                    continue
                else:
                    print("ERROR: Failed to set %s.%s: %r: %s" % (typename, n, e.__class__, e))
                    self.errcount += 1
                    continue

            # and try a comparison
            try:
                if value != getattr(v, n):
                    print("ERROR: Comparison failed for %s.%s: %r != %r" % (typename, n, value, getattr(v, n)))
                    continue
            except Exception as e:
                print("ERROR: compare exception for %s.%s: %r: %s" % (typename, n, e.__class__, e))
                continue

    def check_interface(self, interface, iname):
        errcount = self.errcount
        for n in dir(interface):
            if n[0] == '_' or n == iname:
                # skip the special ones
                continue
            value = getattr(interface, n)
            if isinstance(value, str):
                # print "%s=\"%s\"" % (n, value)
                pass
            elif isinstance(value, int):
                # print "%s=%d" % (n, value)
                pass
            elif isinstance(value, type):
                try:
                    initial_blocks = talloc.total_blocks(None)
                    self.check_type(interface, n, value)
                    self.check_blocks(None, initial_blocks)
                except Exception as e:
                    print("ERROR: Failed to check_type %s.%s: %r: %s" % (iname, n, e.__class__, e))
                    self.errcount += 1
            elif callable(value):
                pass  # Method
            else:
                print("UNKNOWN: %s=%s" % (n, value))
        if self.errcount - errcount != 0:
            print("Found %d errors in %s" % (self.errcount - errcount, iname))

    def check_all_interfaces(self):
        for iname in dir(samba.dcerpc):
            if iname[0] == '_':
                continue
            if iname == 'ClientConnection' or iname == 'base':
                continue
            print("Checking interface %s" % iname)
            iface = getattr(samba.dcerpc, iname)
            initial_blocks = talloc.total_blocks(None)
            self.check_interface(iface, iname)
            self.check_blocks(None, initial_blocks)

    def run(self):
        self.initial_blocks = talloc.total_blocks(None)
        self.errcount = 0
        self.check_all_interfaces()
        return self.errcount


tests = RpcTests()
errcount = tests.run()
if errcount == 0:
    sys.exit(0)
else:
    print("%d failures" % errcount)
    sys.exit(1)