auth4: Fix CID 1034877 Resource leak
[Samba.git] / lib / talloc / test_pytalloc.py
blob655b83f3d3147e3fbfc36bc150bb847bb1998da3
1 #!/usr/bin/env python3
2 # Simple tests for the talloc python bindings.
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
5 import unittest
6 import subprocess
7 import sys
8 import gc
10 import talloc
11 import _test_pytalloc
14 def dummy_func():
15 pass
18 class TallocTests(unittest.TestCase):
20 def test_report_full(self):
21 # report_full is hardcoded to print to stdout, so use a subprocess
22 process = subprocess.Popen([
23 sys.executable, '-c',
24 """if True:
25 import talloc, _test_pytalloc
26 obj = _test_pytalloc.new()
27 talloc.report_full(obj)
28 """
29 ], stdout=subprocess.PIPE)
30 output, stderr = process.communicate()
31 output = str(output)
32 self.assertTrue("full talloc report on 'talloc.Object" in output)
33 self.assertTrue("This is a test string" in output)
35 def test_totalblocks(self):
36 obj = _test_pytalloc.new()
37 # Two blocks: the string, and the name
38 self.assertEqual(talloc.total_blocks(obj), 2)
40 def test_repr(self):
41 obj = _test_pytalloc.new()
42 prefix = '<talloc.Object talloc object at'
43 self.assertTrue(repr(obj).startswith(prefix))
44 self.assertEqual(repr(obj), str(obj))
46 def test_base_repr(self):
47 obj = _test_pytalloc.base_new()
48 prefix = '<talloc.BaseObject talloc based object at'
49 self.assertTrue(repr(obj).startswith(prefix))
50 self.assertEqual(repr(obj), str(obj))
52 def test_destructor(self):
53 # Check correct lifetime of the talloc'd data
54 lst = []
55 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
56 self.assertEqual(lst, [])
57 del obj
58 gc.collect()
59 self.assertEqual(lst, ['dead'])
61 def test_base_destructor(self):
62 # Check correct lifetime of the talloc'd data
63 lst = []
64 obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
65 self.assertEqual(lst, [])
66 del obj
67 gc.collect()
68 self.assertEqual(lst, ['dead'])
71 class TallocComparisonTests(unittest.TestCase):
73 def test_compare_same(self):
74 obj1 = _test_pytalloc.new()
75 self.assertTrue(obj1 == obj1)
76 self.assertFalse(obj1 != obj1)
77 self.assertTrue(obj1 <= obj1)
78 self.assertFalse(obj1 < obj1)
79 self.assertTrue(obj1 >= obj1)
80 self.assertFalse(obj1 > obj1)
82 def test_compare_different(self):
83 # object comparison is consistent
84 obj1, obj2 = sorted([
85 _test_pytalloc.new(),
86 _test_pytalloc.new()])
87 self.assertFalse(obj1 == obj2)
88 self.assertTrue(obj1 != obj2)
89 self.assertTrue(obj1 <= obj2)
90 self.assertTrue(obj1 < obj2)
91 self.assertFalse(obj1 >= obj2)
92 self.assertFalse(obj1 > obj2)
95 class TallocBaseComparisonTests(unittest.TestCase):
97 def test_compare_same(self):
98 obj1 = _test_pytalloc.base_new()
99 self.assertTrue(obj1 == obj1)
100 self.assertFalse(obj1 != obj1)
101 self.assertTrue(obj1 <= obj1)
102 self.assertFalse(obj1 < obj1)
103 self.assertTrue(obj1 >= obj1)
104 self.assertFalse(obj1 > obj1)
106 def test_compare_different(self):
107 # object comparison is consistent
108 obj1, obj2 = sorted([
109 _test_pytalloc.base_new(),
110 _test_pytalloc.base_new()])
111 self.assertFalse(obj1 == obj2)
112 self.assertTrue(obj1 != obj2)
113 self.assertTrue(obj1 <= obj2)
114 self.assertTrue(obj1 < obj2)
115 self.assertFalse(obj1 >= obj2)
116 self.assertFalse(obj1 > obj2)
119 class TallocUtilTests(unittest.TestCase):
121 def test_get_type(self):
122 self.assertTrue(talloc.Object is _test_pytalloc.get_object_type())
124 def test_reference(self):
125 # Check correct lifetime of the talloc'd data with multiple references
126 lst = []
127 obj = _test_pytalloc.DObject(lambda: lst.append('dead'))
128 ref = _test_pytalloc.reference(obj)
129 del obj
130 gc.collect()
131 self.assertEqual(lst, [])
132 del ref
133 gc.collect()
134 self.assertEqual(lst, ['dead'])
136 def test_get_base_type(self):
137 self.assertTrue(talloc.BaseObject is _test_pytalloc.base_get_object_type())
139 def test_base_reference(self):
140 # Check correct lifetime of the talloc'd data with multiple references
141 lst = []
142 obj = _test_pytalloc.DBaseObject(lambda: lst.append('dead'))
143 ref = _test_pytalloc.base_reference(obj)
144 del obj
145 gc.collect()
146 self.assertEqual(lst, [])
147 del ref
148 gc.collect()
149 self.assertEqual(lst, ['dead'])
152 if __name__ == '__main__':
153 unittest.TestProgram()