python/tests: Add tests for integer overflow handling
[samba.git] / python / samba / tests / dcerpc / integer.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2015
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
18 """Tests for integer handling in PIDL generated bindings samba.dcerpc.*"""
19
20 from samba.dcerpc import server_id, misc, srvsvc
21 import samba.tests
22
23 class IntegerTests(samba.tests.TestCase):
24
25     def test_uint32_into_hyper(self):
26         s = server_id.server_id()
27         s.unique_id = server_id.NONCLUSTER_VNN
28         self.assertEquals(s.unique_id, 0xFFFFFFFFL)
29
30     def test_int_into_hyper(self):
31         s = server_id.server_id()
32         s.unique_id = 1
33
34     def test_negative_int_into_hyper(self):
35         s = server_id.server_id()
36         def assign():
37             s.unique_id = -1
38         self.assertRaises(OverflowError, assign)
39
40     def test_hyper_into_uint32(self):
41         s = server_id.server_id()
42         def assign():
43             s.vnn = server_id.SERVERID_UNIQUE_ID_NOT_TO_VERIFY
44         self.assertRaises(OverflowError, assign)
45
46     def test_hyper_into_int32(self):
47         s = srvsvc.NetRemoteTODInfo()
48         def assign():
49             s.timezone = server_id.SERVERID_UNIQUE_ID_NOT_TO_VERIFY
50         self.assertRaises(OverflowError, assign)
51
52     def test_int_into_int32(self):
53         s = srvsvc.NetRemoteTODInfo()
54         s.timezone = 5
55
56     def test_uint32_into_int32(self):
57         s = srvsvc.NetRemoteTODInfo()
58         def assign():
59             s.timezone = server_id.NONCLUSTER_VNN
60         self.assertRaises(OverflowError, assign)
61
62     def test_long_into_int32(self):
63         s = srvsvc.NetRemoteTODInfo()
64         s.timezone = 5L
65
66     def test_larger_long_int_into_int32(self):
67         s = srvsvc.NetRemoteTODInfo()
68         def assign():
69             s.timezone = 2147483648
70         self.assertRaises(OverflowError, assign)
71
72     def test_larger_int_into_int32(self):
73         s = srvsvc.NetRemoteTODInfo()
74         s.timezone = 2147483647
75
76     def test_float_into_int32(self):
77         s = srvsvc.NetRemoteTODInfo()
78         def assign():
79             s.timezone = 2.5
80         self.assertRaises(TypeError, assign)
81
82     def test_int_float_into_int32(self):
83         s = srvsvc.NetRemoteTODInfo()
84         def assign():
85             s.timezone = 2.0
86         self.assertRaises(TypeError, assign)
87
88     def test_negative_int_into_int32(self):
89         s = srvsvc.NetRemoteTODInfo()
90         s.timezone = -2147483648
91
92     def test_negative_into_uint32(self):
93         s = server_id.server_id()
94         def assign():
95             s.vnn = -1
96         self.assertRaises(OverflowError, assign)
97
98     def test_hyper_into_uint16(self):
99         g = misc.GUID()
100         def assign():
101             g.time_mid = server_id.SERVERID_UNIQUE_ID_NOT_TO_VERIFY
102         self.assertRaises(OverflowError, assign)
103
104     def test_int_into_uint16(self):
105         g = misc.GUID()
106         def assign():
107             g.time_mid = 200000
108         self.assertRaises(OverflowError, assign)
109
110     def test_negative_int_into_uint16(self):
111         g = misc.GUID()
112         def assign():
113             g.time_mid = -2
114         self.assertRaises(OverflowError, assign)
115
116     def test_int_into_uint16(self):
117         g = misc.GUID()
118         def assign():
119             g.time_mid = 200000
120         self.assertRaises(OverflowError, assign)
121
122     def test_negative_int_into_uint16(self):
123         g = misc.GUID()
124         def assign():
125             g.time_mid = -2
126         self.assertRaises(OverflowError, assign)
127
128     def test_enum_into_uint16(self):
129         g = misc.GUID()
130         g.time_mid = misc.SEC_CHAN_DOMAIN
131
132     def test_bitmap_into_uint16(self):
133         g = misc.GUID()
134         g.time_mid = misc.SV_TYPE_WFW
135         self.assertEqual(g.time_mid, misc.SV_TYPE_WFW)
136
137     def test_overflow_bitmap_into_uint16(self):
138         g = misc.GUID()
139         def assign():
140             g.time_mid = misc.SV_TYPE_LOCAL_LIST_ONLY
141         self.assertRaises(OverflowError, assign)
142
143     def test_overflow_bitmap_into_uint16_2(self):
144         g = misc.GUID()
145         def assign():
146             g.time_mid = misc.SV_TYPE_DOMAIN_ENUM
147         self.assertRaises(OverflowError, assign)
148
149     def test_int_list_over_list(self):
150         g = misc.GUID()
151         g.node = [5, 0, 5, 0, 7, 4]
152         self.assertEqual(g.node[0], 5)
153
154     def test_long_int_list_over_uint8_list(self):
155         g = misc.GUID()
156         g.node = [5L, 0, 5, 0, 7, 4]
157         self.assertEqual(g.node[0], 5)
158
159     def test_negative_list_over_uint8_list(self):
160         g = misc.GUID()
161         def assign():
162             g.node = [-1, 0, 5, 0, 7, 4]
163         self.assertRaises(OverflowError, assign)
164
165     def test_overflow_list_over_uint8_list(self):
166         g = misc.GUID()
167         def assign():
168             g.node = [256, 0, 5, 0, 7, 4]
169         self.assertRaises(OverflowError, assign)
170
171     def test_short_list_over_uint8_list(self):
172         g = misc.GUID()
173         def assign():
174             g.node = [5, 0, 5]
175         self.assertRaises(TypeError, assign)
176
177     def test_long_list_over_uint8_list(self):
178         g = misc.GUID()
179         def assign():
180             g.node = [5, 0, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
181         self.assertRaises(TypeError, assign)
182
183     # Due to our PIDL bindings generating a python List, modifications
184     # to a list of non-objects are not reflected in the C list
185     # (modifications objects in lists of objects work because the
186     # objects are modified), so changes essentially vanish and are not
187     # type checked either.
188     def test_assign_into_uint8_list(self):
189         g = misc.GUID()
190         g.node[1] = 5
191         self.assertEqual(g.node[1], 5)
192
193     def test_negative_into_uint8_list(self):
194         g = misc.GUID()
195         def assign():
196             g.node[1] = -1
197         self.assertRaises(OverflowError, assign)
198
199     def test_overflow_into_uint8_list(self):
200         g = misc.GUID()
201         def assign():
202             g.node[1] = 256
203         self.assertRaises(OverflowError, assign)