smbd: improve reinit_after_fork error handling
[Samba.git] / python / samba / tests / dcerpc / string_tests.py
bloba3426bb61a8b33ffeb70ddd72ad6e51c9e9abed7
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2016
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.
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.
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/>.
18 """Tests for string and unicode handling in PIDL generated bindings
19 samba.dcerpc.*"""
21 from samba.dcerpc import drsblobs
22 import samba.tests
23 from samba.ndr import ndr_unpack, ndr_pack
24 import talloc
25 import gc
28 class TestException(Exception):
29 pass
32 class StringTests(samba.tests.TestCase):
34 def setUp(self):
35 super().setUp()
36 talloc.enable_null_tracking()
37 self.startup_blocks = talloc.total_blocks()
39 def tearDown(self):
40 super().tearDown()
41 gc.collect()
42 if talloc.total_blocks() != self.startup_blocks:
43 talloc.report_full()
44 self.fail("it appears we are leaking memory")
46 def test_string_from_python(self):
47 info = drsblobs.repsFromTo2OtherInfo()
48 info.dns_name1 = "hello.example.com"
49 info.dns_name2 = "goodbye.example.com"
50 gc.collect()
51 self.assertIsNotNone(info)
52 self.assertEqual(info.dns_name1, "hello.example.com")
53 self.assertEqual(info.dns_name2, "goodbye.example.com")
55 info.dns_name1 = ""
56 info.dns_name2 = "goodbye.example.com"
58 self.assertEqual(info.dns_name1, "")
59 self.assertEqual(info.dns_name2, "goodbye.example.com")
61 info.dns_name2 = None
63 self.assertEqual(info.dns_name1, "")
64 self.assertIsNone(info.dns_name2)
66 def test_string_with_exception(self):
67 try:
68 self.test_string_from_python()
69 raise TestException()
70 except TestException:
71 pass
73 def test_string_from_python_function(self):
74 def get_info():
75 info = drsblobs.repsFromTo2OtherInfo()
76 info.dns_name1 = "1.example.com"
77 info.dns_name2 = "2.example.com"
78 return info
80 info = get_info()
81 gc.collect()
82 self.assertIsNotNone(info)
83 self.assertEqual(info.dns_name1, "1.example.com")
84 self.assertEqual(info.dns_name2, "2.example.com")
86 def test_string_modify_in_place(self):
87 info = drsblobs.repsFromTo2OtherInfo()
88 info.dns_name1 = "1.example.com"
89 info.dns_name2 = "%s.example.com"
90 gc.collect()
91 self.assertIsNotNone(info)
92 self.assertEqual(info.dns_name1, "1.example.com")
93 self.assertEqual(info.dns_name2, "%s.example.com")
94 info.dns_name1 += ".co.nz"
95 info.dns_name2 %= 2
96 self.assertEqual(info.dns_name1, "1.example.com.co.nz")
97 self.assertEqual(info.dns_name2, "2.example.com")
98 del info
100 def test_string_delete(self):
101 gc.collect()
102 info = drsblobs.repsFromTo2OtherInfo()
103 info.dns_name1 = "1.example.com"
104 info.dns_name2 = "2.example.com"
105 info.dns_name1 = None
106 try:
107 del info.dns_name2
108 except AttributeError:
109 pass
111 self.assertIsNotNone(info)
112 self.assertIsNone(info.dns_name1)
113 self.assertIsNotNone(info.dns_name2)
116 class StringTestsWithoutLeakCheck(samba.tests.TestCase):
117 """We know that the ndr unpacking test leaves stuff in the
118 autofree_context, and we don't want to worry about that. So for
119 this test we don't make memory leak assertions."""
121 def test_string_from_ndr(self):
122 info = drsblobs.repsFromTo2OtherInfo()
123 info.dns_name1 = "1.example.com"
124 info.dns_name2 = "2.example.com"
125 packed = ndr_pack(info)
126 gc.collect()
128 info_unpacked = ndr_unpack(drsblobs.repsFromTo2OtherInfo, packed)
130 self.assertIsNotNone(info_unpacked)
131 self.assertEqual(info_unpacked.dns_name1, "1.example.com")
132 self.assertEqual(info_unpacked.dns_name2, "2.example.com")