3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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.
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.
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/>.
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
27 DATADIR = os.path.join(os.path.dirname(__file__),
28 "../../../../../testdata/samba3")
30 class RegistryTestCase(TestCase):
33 super(RegistryTestCase, self).setUp()
34 self.registry = Registry(os.path.join(DATADIR, "registry.tdb"))
38 super(RegistryTestCase, self).tearDown()
40 def test_length(self):
41 self.assertEquals(28, len(self.registry))
44 self.assertTrue("HKLM" in self.registry.keys())
46 def test_subkeys(self):
47 self.assertEquals(["SOFTWARE", "SYSTEM"], self.registry.subkeys("HKLM"))
49 def test_values(self):
50 self.assertEquals({'DisplayName': (1L, 'E\x00v\x00e\x00n\x00t\x00 \x00L\x00o\x00g\x00\x00\x00'),
51 'ErrorControl': (4L, '\x01\x00\x00\x00')},
52 self.registry.values("HKLM/SYSTEM/CURRENTCONTROLSET/SERVICES/EVENTLOG"))
55 class PolicyTestCase(TestCase):
58 super(PolicyTestCase, self).setUp()
59 self.policy = PolicyDatabase(os.path.join(DATADIR, "account_policy.tdb"))
61 def test_policy(self):
62 self.assertEquals(self.policy.min_password_length, 5)
63 self.assertEquals(self.policy.minimum_password_age, 0)
64 self.assertEquals(self.policy.maximum_password_age, 999999999)
65 self.assertEquals(self.policy.refuse_machine_password_change, 0)
66 self.assertEquals(self.policy.reset_count_minutes, 0)
67 self.assertEquals(self.policy.disconnect_time, -1)
68 self.assertEquals(self.policy.user_must_logon_to_change_password, None)
69 self.assertEquals(self.policy.password_history, 0)
70 self.assertEquals(self.policy.lockout_duration, 0)
71 self.assertEquals(self.policy.bad_lockout_minutes, None)
74 class GroupsTestCase(TestCase):
77 super(GroupsTestCase, self).setUp()
78 self.groupdb = GroupMappingDatabase(os.path.join(DATADIR, "group_mapping.tdb"))
82 super(GroupsTestCase, self).tearDown()
84 def test_group_length(self):
85 self.assertEquals(13, len(list(self.groupdb.groupsids())))
87 def test_get_group(self):
88 self.assertEquals((-1, 5L, 'Administrators', ''), self.groupdb.get_group("S-1-5-32-544"))
90 def test_groupsids(self):
91 sids = list(self.groupdb.groupsids())
92 self.assertTrue("S-1-5-32-544" in sids)
94 def test_alias_length(self):
95 self.assertEquals(0, len(list(self.groupdb.aliases())))
98 class SecretsDbTestCase(TestCase):
101 super(SecretsDbTestCase, self).setUp()
102 self.secretsdb = SecretsDatabase(os.path.join(DATADIR, "secrets.tdb"))
105 self.secretsdb.close()
106 super(SecretsDbTestCase, self).tearDown()
108 def test_get_sid(self):
109 self.assertTrue(self.secretsdb.get_sid("BEDWYR") is not None)
112 class TdbSamTestCase(TestCase):
115 super(TdbSamTestCase, self).setUp()
116 self.samdb = TdbSam(os.path.join(DATADIR, "passdb.tdb"))
120 super(TdbSamTestCase, self).tearDown()
122 def test_usernames(self):
123 self.assertEquals(3, len(list(self.samdb.usernames())))
125 def test_getuser(self):
126 user = SAMUser("root")
127 user.logoff_time = 2147483647
128 user.kickoff_time = 2147483647
129 user.pass_can_change_time = 1125418267
130 user.username = "root"
132 user.lm_password = 'U)\x02\x03\x1b\xed\xe9\xef\xaa\xd3\xb45\xb5\x14\x04\xee'
133 user.nt_password = '\x87\x8d\x80\x14`l\xda)gzD\xef\xa15?\xc7'
135 user.pass_last_set_time = 1125418267
136 user.fullname = "root"
137 user.nt_username = ""
138 user.logoff_time = 2147483647
140 user.group_rid = 1001
142 user.bad_password_count = 0
143 user.domain = "BEDWYR"
144 user.munged_dial = ""
145 user.workstations = ""
147 user.kickoff_time = 2147483647
148 user.logoff_time = 2147483647
149 user.unknown_6 = 1260L
151 user.hours = [True for i in range(168)]
152 other = self.samdb["root"]
153 for name in other.__dict__:
154 if other.__dict__[name] != user.__dict__[name]:
155 print "%s: %r != %r" % (name, other.__dict__[name], user.__dict__[name])
156 self.assertEquals(user, other)
159 class WinsDatabaseTestCase(TestCase):
162 super(WinsDatabaseTestCase, self).setUp()
163 self.winsdb = WinsDatabase(os.path.join(DATADIR, "wins.dat"))
165 def test_length(self):
166 self.assertEquals(22, len(self.winsdb))
168 def test_first_entry(self):
169 self.assertEqual((1124185120, ["192.168.1.5"], 0x64), self.winsdb["ADMINISTRATOR#03"])
173 super(WinsDatabaseTestCase, self).tearDown()
176 class SmbpasswdTestCase(TestCase):
179 super(SmbpasswdTestCase, self).setUp()
180 self.samdb = SmbpasswdFile(os.path.join(DATADIR, "smbpasswd"))
182 def test_length(self):
183 self.assertEquals(3, len(self.samdb))
185 def test_get_user(self):
186 user = SAMUser("rootpw")
187 user.lm_password = "552902031BEDE9EFAAD3B435B51404EE"
188 user.nt_password = "878D8014606CDA29677A44EFA1353FC7"
189 user.acct_ctrl = ACB_NORMAL
190 user.pass_last_set_time = int(1125418267)
192 self.assertEquals(user, self.samdb["rootpw"])
196 super(SmbpasswdTestCase, self).tearDown()
199 class IdmapDbTestCase(TestCase):
202 super(IdmapDbTestCase, self).setUp()
203 self.idmapdb = IdmapDatabase(os.path.join(DATADIR,
204 "winbindd_idmap.tdb"))
206 def test_user_hwm(self):
207 self.assertEquals(10000, self.idmapdb.get_user_hwm())
209 def test_group_hwm(self):
210 self.assertEquals(10002, self.idmapdb.get_group_hwm())
213 self.assertEquals(1, len(list(self.idmapdb.uids())))
216 self.assertEquals(3, len(list(self.idmapdb.gids())))
218 def test_get_user_sid(self):
219 self.assertEquals("S-1-5-21-58189338-3053988021-627566699-501", self.idmapdb.get_user_sid(65534))
221 def test_get_group_sid(self):
222 self.assertEquals("S-1-5-21-2447931902-1787058256-3961074038-3007", self.idmapdb.get_group_sid(10001))
226 super(IdmapDbTestCase, self).tearDown()
229 class ParamTestCase(TestCase):
233 self.assertTrue(file is not None)
235 def test_add_section(self):
237 file.add_section("global")
238 self.assertTrue(file["global"] is not None)
240 def test_set_param_string(self):
242 file.add_section("global")
243 file.set_string("data", "bar")
244 self.assertEquals("bar", file.get_string("data"))
246 def test_get_section(self):
248 self.assertEquals(None, file.get_section("unknown"))
249 self.assertRaises(KeyError, lambda: file["unknown"])