samba python tests: convert 'except X, e' to 'except X as e'
[samba.git] / python / samba / tests / dcerpc / array.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2016
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 array handling in PIDL generated bindings samba.dcerpc.*"""
19
20 from samba.dcerpc import drsblobs
21 import samba.tests
22 from samba.ndr import ndr_unpack, ndr_pack
23 import talloc
24 import gc
25
26 class ArrayTests(samba.tests.TestCase):
27
28     def setUp(self):
29         super(ArrayTests, self).setUp()
30         talloc.enable_null_tracking()
31         self.startup_blocks = talloc.total_blocks()
32
33     def tearDown(self):
34         super(ArrayTests, self).tearDown()
35         gc.collect()
36         if talloc.total_blocks() != self.startup_blocks:
37             talloc.report_full()
38             self.fail("it appears we are leaking memory")
39
40     def test_array_from_python(self):
41         rmd = drsblobs.replPropertyMetaDataBlob()
42
43         rmd.version = 1
44         rmd.ctr = drsblobs.replPropertyMetaDataCtr1()
45         rmd.ctr.count = 3
46
47         rmd1 = drsblobs.replPropertyMetaData1()
48         rmd1.attid = 1
49         rmd1.version = 2
50
51         rmd2 = drsblobs.replPropertyMetaData1()
52         rmd2.attid = 2
53         rmd2.version = 2
54
55         rmd3 = drsblobs.replPropertyMetaData1()
56         rmd3.attid = 3
57         rmd3.version = 2
58
59         rmd.ctr.array = [rmd1, rmd2, rmd3]
60         gc.collect()
61
62         self.assertIsNotNone(rmd)
63         self.assertEqual(rmd.version, 1)
64         self.assertIsNotNone(rmd.ctr)
65         self.assertEqual(rmd.ctr.count, 3)
66         self.assertEqual(len(rmd.ctr.array), rmd.ctr.count)
67         self.assertIsNotNone(rmd.ctr.array[0])
68         self.assertEqual(rmd.ctr.array[0].attid, 1)
69
70     def test_array_with_exception(self):
71         try:
72             rmd = drsblobs.replPropertyMetaDataBlob()
73
74             rmd.version = 1
75             rmd.ctr = drsblobs.replPropertyMetaDataCtr1()
76             rmd.ctr.count = 3
77
78             rmd1 = drsblobs.replPropertyMetaData1()
79             rmd1.attid = 1
80             rmd1.version = 2
81
82             rmd2 = drsblobs.replPropertyMetaData1()
83             rmd2.attid = 2
84             rmd2.version = 2
85
86             rmd3 = drsblobs.replPropertyMetaData1()
87             rmd3.attid = 3
88             rmd3.version = 2
89
90             rmd.ctr.array = [rmd1, rmd2, rmd3]
91
92             gc.collect()
93
94             self.assertIsNotNone(rmd)
95             self.assertEqual(rmd.version, 1)
96             self.assertIsNotNone(rmd.ctr)
97             self.assertEqual(rmd.ctr.count, 3)
98             self.assertEqual(len(rmd.ctr.array), rmd.ctr.count)
99             self.assertIsNotNone(rmd.ctr.array[0])
100             self.assertEqual(rmd.ctr.array[0].attid, 1)
101
102             raise Exception()
103         except:
104             pass
105
106     def test_array_from_python_function(self):
107         def get_rmd():
108             rmd = drsblobs.replPropertyMetaDataBlob()
109
110             rmd.version = 1
111             rmd.ctr = drsblobs.replPropertyMetaDataCtr1()
112             rmd.ctr.count = 3
113
114             rmd1 = drsblobs.replPropertyMetaData1()
115             rmd1.attid = 1
116             rmd1.version = 2
117
118             rmd2 = drsblobs.replPropertyMetaData1()
119             rmd2.attid = 2
120             rmd2.version = 2
121
122             rmd3 = drsblobs.replPropertyMetaData1()
123             rmd3.attid = 3
124             rmd3.version = 2
125
126             rmd.ctr.array = [rmd1, rmd2, rmd3]
127             return rmd
128
129         rmd = get_rmd()
130         gc.collect()
131         self.assertIsNotNone(rmd)
132         self.assertEqual(rmd.version, 1)
133         self.assertIsNotNone(rmd.ctr)
134         self.assertEqual(rmd.ctr.count, 3)
135         self.assertEqual(len(rmd.ctr.array), rmd.ctr.count)
136         self.assertIsNotNone(rmd.ctr.array[0])
137         self.assertEqual(rmd.ctr.array[0].attid, 1)
138
139     def test_array_from_ndr(self):
140         rmd = drsblobs.replPropertyMetaDataBlob()
141
142         rmd.version = 1
143         rmd.ctr = drsblobs.replPropertyMetaDataCtr1()
144         rmd.ctr.count = 3
145
146         rmd1 = drsblobs.replPropertyMetaData1()
147         rmd1.attid = 1
148         rmd1.version = 2
149
150         rmd2 = drsblobs.replPropertyMetaData1()
151         rmd2.attid = 2
152         rmd2.version = 2
153
154         rmd3 = drsblobs.replPropertyMetaData1()
155         rmd3.attid = 3
156         rmd3.version = 2
157
158         rmd.ctr.array = [rmd1, rmd2, rmd3]
159
160         packed = ndr_pack(rmd)
161         gc.collect()
162
163         rmd_unpacked = ndr_unpack(drsblobs.replPropertyMetaDataBlob, packed)
164         self.assertIsNotNone(rmd_unpacked)
165         self.assertEqual(rmd_unpacked.version, 1)
166         self.assertIsNotNone(rmd_unpacked.ctr)
167         self.assertEqual(rmd_unpacked.ctr.count, 3)
168         self.assertEqual(len(rmd_unpacked.ctr.array), rmd_unpacked.ctr.count)
169         self.assertIsNotNone(rmd_unpacked.ctr.array[0])
170         self.assertEqual(rmd_unpacked.ctr.array[0].attid, 1)
171
172         self.assertEqual(rmd.ctr.array[0].attid,
173                          rmd_unpacked.ctr.array[0].attid)
174
175     def test_array_delete(self):
176         rmd = drsblobs.replPropertyMetaDataBlob()
177
178         rmd.version = 1
179         rmd.ctr = drsblobs.replPropertyMetaDataCtr1()
180         rmd.ctr.count = 3
181
182         rmd1 = drsblobs.replPropertyMetaData1()
183         rmd1.attid = 1
184         rmd1.version = 2
185
186         rmd2 = drsblobs.replPropertyMetaData1()
187         rmd2.attid = 2
188         rmd2.version = 2
189
190         rmd3 = drsblobs.replPropertyMetaData1()
191         rmd3.attid = 3
192         rmd3.version = 2
193
194         rmd.ctr.array = [rmd1, rmd2, rmd3]
195         try:
196             del rmd1.version
197             self.fail("succeeded in deleting rmd1.version")
198         except AttributeError as e:
199             pass
200
201         try:
202             del rmd.ctr.array
203             self.fail("succeeded in deleting rmd.ctr.array")
204         except AttributeError as e:
205             pass