talloc: Add python talloc module, move convenience functions to it.
[samba.git] / source4 / scripting / python / samba / tests / dcerpc / rpc_talloc.py
1 #!/usr/bin/env python
2 # test generated python code from pidl
3 # Andrew Tridgell August 2010
4 #
5 # to run this test, use one of these:
6 #
7 #    python -m testtools.run samba.tests.dcerpc.rpc_talloc
8 #
9 # or if you have trial installed (from twisted), use
10 #
11 #    trial samba.tests.dcerpc.rpc_talloc
12
13 import sys
14
15 sys.path.insert(0, "bin/python")
16
17 import samba
18 import samba.tests
19 from samba.dcerpc import drsuapi
20 import talloc
21
22 talloc.enable_null_tracking()
23
24 class TallocTests(samba.tests.TestCase):
25     '''test talloc behaviour of pidl generated python code'''
26
27     def check_blocks(self, object, num_expected):
28         '''check that the number of allocated blocks is correct'''
29         nblocks = talloc.total_blocks(object)
30         if object is None:
31             nblocks -= self.initial_blocks
32         self.assertEquals(nblocks, num_expected)
33
34     def get_rodc_partial_attribute_set(self):
35         '''get a list of attributes for RODC replication'''
36         partial_attribute_set = drsuapi.DsPartialAttributeSet()
37
38         # we expect one block for the object, and one for the structure
39         self.check_blocks(partial_attribute_set, 2)
40
41         attids = [ 1, 2, 3]
42         partial_attribute_set.version = 1
43         partial_attribute_set.attids     = attids
44         partial_attribute_set.num_attids = len(attids)
45
46         # we expect one block object, a structure, an ARRAY, and a
47         # reference to the array
48         self.check_blocks(partial_attribute_set, 3)
49
50         return partial_attribute_set
51
52     def pas_test(self):
53         pas = self.get_rodc_partial_attribute_set()
54         self.check_blocks(pas, 3)
55         req8 = drsuapi.DsGetNCChangesRequest8()
56         self.check_blocks(req8, 2)
57         self.check_blocks(None, 5)
58         req8.partial_attribute_set = pas
59         if req8.partial_attribute_set.attids[1] != 2:
60             raise Exception("Wrong value in attids[2]")
61         # we now get an additional reference
62         self.check_blocks(None, 6)
63
64     def test_run(self):
65         self.initial_blocks = talloc.total_blocks(None)
66         self.check_blocks(None, 0)
67         self.pas_test()
68         self.check_blocks(None, 0)