samba.tests.samba3: Clean up imports, use new TestCase class.
[metze/samba/wip.git] / source4 / scripting / python / samba / tests / samba3.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 #   
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #   
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #   
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 from samba.samba3 import (GroupMappingDatabase, Registry, PolicyDatabase,
21         SecretsDatabase, TdbSam)
22 from samba.samba3 import (WinsDatabase, SmbpasswdFile, ACB_NORMAL,
23         IdmapDatabase, SAMUser, ParamFile)
24 from samba.tests import TestCase
25 import os
26
27 DATADIR=os.path.join(os.path.dirname(__file__), "../../../../../testdata/samba3")
28 print "Samba 3 data dir: %s" % DATADIR
29
30 class RegistryTestCase(TestCase):
31
32     def setUp(self):
33         super(RegistryTestCase, self).setUp()
34         self.registry = Registry(os.path.join(DATADIR, "registry.tdb"))
35
36     def tearDown(self):
37         self.registry.close()
38
39     def test_length(self):
40         self.assertEquals(28, len(self.registry))
41
42     def test_keys(self):
43         self.assertTrue("HKLM" in self.registry.keys())
44
45     def test_subkeys(self):
46         self.assertEquals(["SOFTWARE", "SYSTEM"], self.registry.subkeys("HKLM"))
47
48     def test_values(self):
49         self.assertEquals({'DisplayName': (1L, 'E\x00v\x00e\x00n\x00t\x00 \x00L\x00o\x00g\x00\x00\x00'), 
50                            'ErrorControl': (4L, '\x01\x00\x00\x00')}, 
51                            self.registry.values("HKLM/SYSTEM/CURRENTCONTROLSET/SERVICES/EVENTLOG"))
52
53
54 class PolicyTestCase(TestCase):
55
56     def setUp(self):
57         super(PolicyTestCase, self).setUp()
58         self.policy = PolicyDatabase(os.path.join(DATADIR, "account_policy.tdb"))
59
60     def test_policy(self):
61         self.assertEquals(self.policy.min_password_length, 5)
62         self.assertEquals(self.policy.minimum_password_age, 0)
63         self.assertEquals(self.policy.maximum_password_age, 999999999)
64         self.assertEquals(self.policy.refuse_machine_password_change, 0)
65         self.assertEquals(self.policy.reset_count_minutes, 0)
66         self.assertEquals(self.policy.disconnect_time, -1)
67         self.assertEquals(self.policy.user_must_logon_to_change_password, None)
68         self.assertEquals(self.policy.password_history, 0)
69         self.assertEquals(self.policy.lockout_duration, 0)
70         self.assertEquals(self.policy.bad_lockout_minutes, None)
71
72
73 class GroupsTestCase(TestCase):
74
75     def setUp(self):
76         super(GroupsTestCase, self).setUp()
77         self.groupdb = GroupMappingDatabase(os.path.join(DATADIR, "group_mapping.tdb"))
78
79     def tearDown(self):
80         self.groupdb.close()
81         super(GroupsTestCase, self).tearDown()
82
83     def test_group_length(self):
84         self.assertEquals(13, len(list(self.groupdb.groupsids())))
85
86     def test_get_group(self):
87         self.assertEquals((-1, 5L, 'Administrators', ''), self.groupdb.get_group("S-1-5-32-544"))
88
89     def test_groupsids(self):
90         sids = list(self.groupdb.groupsids())
91         self.assertTrue("S-1-5-32-544" in sids)
92
93     def test_alias_length(self):
94         self.assertEquals(0, len(list(self.groupdb.aliases())))
95
96
97 class SecretsDbTestCase(TestCase):
98
99     def setUp(self):
100         super(SecretsDbTestCase, self).setUp()
101         self.secretsdb = SecretsDatabase(os.path.join(DATADIR, "secrets.tdb"))
102
103     def tearDown(self):
104         self.secretsdb.close()
105         super(SecretsDbTestCase, self).tearDown()
106
107     def test_get_sid(self):
108         self.assertTrue(self.secretsdb.get_sid("BEDWYR") is not None)
109
110
111 class TdbSamTestCase(TestCase):
112
113     def setUp(self):
114         super(TdbSamTestCase, self).setUp()
115         self.samdb = TdbSam(os.path.join(DATADIR, "passdb.tdb"))
116
117     def tearDown(self):
118         self.samdb.close()
119         super(TdbSamTestCase, self).tearDown()
120
121     def test_usernames(self):
122         self.assertEquals(3, len(list(self.samdb.usernames())))
123
124     def test_getuser(self):
125         user = SAMUser("root")
126         user.logoff_time = 2147483647
127         user.kickoff_time = 2147483647
128         user.pass_can_change_time = 1125418267
129         user.username = "root"
130         user.uid = None
131         user.lm_password = 'U)\x02\x03\x1b\xed\xe9\xef\xaa\xd3\xb45\xb5\x14\x04\xee'
132         user.nt_password = '\x87\x8d\x80\x14`l\xda)gzD\xef\xa15?\xc7'
133         user.acct_ctrl = 16
134         user.pass_last_set_time = 1125418267
135         user.fullname = "root"
136         user.nt_username = ""
137         user.logoff_time = 2147483647
138         user.acct_desc = ""
139         user.group_rid = 1001
140         user.logon_count = 0
141         user.bad_password_count = 0
142         user.domain = "BEDWYR"
143         user.munged_dial = ""
144         user.workstations = ""
145         user.user_rid = 1000
146         user.kickoff_time = 2147483647
147         user.logoff_time = 2147483647
148         user.unknown_6 = 1260L
149         user.logon_divs = 0
150         user.hours = [True for i in range(168)]
151         other = self.samdb["root"]
152         for name in other.__dict__:
153             if other.__dict__[name] != user.__dict__[name]:
154                 print "%s: %r != %r" % (name, other.__dict__[name], user.__dict__[name])
155         self.assertEquals(user, other)
156
157
158 class WinsDatabaseTestCase(TestCase):
159
160     def setUp(self):
161         super(WinsDatabaseTestCase, self).setUp()
162         self.winsdb = WinsDatabase(os.path.join(DATADIR, "wins.dat"))
163
164     def test_length(self):
165         self.assertEquals(22, len(self.winsdb))
166
167     def test_first_entry(self):
168         self.assertEqual((1124185120, ["192.168.1.5"], 0x64), self.winsdb["ADMINISTRATOR#03"])
169
170     def tearDown(self):
171         self.winsdb.close()
172         super(WinsDatabaseTestCase, self).tearDown()
173
174
175 class SmbpasswdTestCase(TestCase):
176
177     def setUp(self):
178         super(SmbpasswdTestCase, self).setUp()
179         self.samdb = SmbpasswdFile(os.path.join(DATADIR, "smbpasswd"))
180
181     def test_length(self):
182         self.assertEquals(3, len(self.samdb))
183
184     def test_get_user(self):
185         user = SAMUser("rootpw")
186         user.lm_password = "552902031BEDE9EFAAD3B435B51404EE"
187         user.nt_password = "878D8014606CDA29677A44EFA1353FC7"
188         user.acct_ctrl = ACB_NORMAL
189         user.pass_last_set_time = int(1125418267)
190         user.uid = 0
191         self.assertEquals(user, self.samdb["rootpw"])
192
193     def tearDown(self):
194         self.samdb.close()
195         super(SmbpasswdTestCase, self).tearDown()
196
197
198 class IdmapDbTestCase(TestCase):
199
200     def setUp(self):
201         super(IdmapDbTestCase, self).setUp()
202         self.idmapdb = IdmapDatabase(os.path.join(DATADIR,
203             "winbindd_idmap.tdb"))
204
205     def test_user_hwm(self):
206         self.assertEquals(10000, self.idmapdb.get_user_hwm())
207
208     def test_group_hwm(self):
209         self.assertEquals(10002, self.idmapdb.get_group_hwm())
210
211     def test_uids(self):
212         self.assertEquals(1, len(list(self.idmapdb.uids())))
213
214     def test_gids(self):
215         self.assertEquals(3, len(list(self.idmapdb.gids())))
216
217     def test_get_user_sid(self):
218         self.assertEquals("S-1-5-21-58189338-3053988021-627566699-501", self.idmapdb.get_user_sid(65534))
219
220     def test_get_group_sid(self):
221         self.assertEquals("S-1-5-21-2447931902-1787058256-3961074038-3007", self.idmapdb.get_group_sid(10001))
222
223     def tearDown(self):
224         self.idmapdb.close()
225         super(IdmapDbTestCase, self).tearDown()
226
227
228 class ParamTestCase(TestCase):
229
230     def test_init(self):
231         file = ParamFile()
232         self.assertTrue(file is not None)
233
234     def test_add_section(self):
235         file = ParamFile()
236         file.add_section("global")
237         self.assertTrue(file["global"] is not None)
238
239     def test_set_param_string(self):
240         file = ParamFile()
241         file.add_section("global")
242         file.set_string("data", "bar")
243         self.assertEquals("bar", file.get_string("data"))
244
245     def test_get_section(self):
246         file = ParamFile()
247         self.assertEquals(None, file.get_section("unknown"))
248         self.assertRaises(KeyError, lambda: file["unknown"])