r26616: Support parsing of user data in SAmba 3 tdbsam.
[ab/samba.git/.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 import unittest
21 from samba.samba3 import (GroupMappingDatabase, Registry, PolicyDatabase, SecretsDatabase, TdbSam,
22                           WinsDatabase, SmbpasswdFile, ACB_NORMAL, IdmapDatabase, SAMUser)
23 import os
24
25 DATADIR=os.path.join(os.path.dirname(__file__), "../../../../../testdata/samba3")
26 print "Samba 3 data dir: %s" % DATADIR
27
28 class RegistryTestCase(unittest.TestCase):
29     def setUp(self):
30         self.registry = Registry(os.path.join(DATADIR, "registry.tdb"))
31
32     def tearDown(self):
33         self.registry.close()
34
35     def test_length(self):
36         self.assertEquals(28, len(self.registry))
37
38     def test_keys(self):
39         self.assertTrue("HKLM" in self.registry.keys())
40
41     def test_subkeys(self):
42         self.assertEquals(["SOFTWARE", "SYSTEM"], self.registry.subkeys("HKLM"))
43
44     def test_values(self):
45         self.assertEquals({'DisplayName': (1L, 'E\x00v\x00e\x00n\x00t\x00 \x00L\x00o\x00g\x00\x00\x00'), 
46                            'ErrorControl': (4L, '\x01\x00\x00\x00')}, 
47                            self.registry.values("HKLM/SYSTEM/CURRENTCONTROLSET/SERVICES/EVENTLOG"))
48
49
50 class PolicyTestCase(unittest.TestCase):
51     def setUp(self):
52         self.policy = PolicyDatabase(os.path.join(DATADIR, "account_policy.tdb"))
53
54     def test_policy(self):
55         self.assertEquals(self.policy.min_password_length, 5)
56         self.assertEquals(self.policy.minimum_password_age, 0)
57         self.assertEquals(self.policy.maximum_password_age, 999999999)
58         self.assertEquals(self.policy.refuse_machine_password_change, 0)
59         self.assertEquals(self.policy.reset_count_minutes, 0)
60         self.assertEquals(self.policy.disconnect_time, -1)
61         self.assertEquals(self.policy.user_must_logon_to_change_password, None)
62         self.assertEquals(self.policy.password_history, 0)
63         self.assertEquals(self.policy.lockout_duration, 0)
64         self.assertEquals(self.policy.bad_lockout_minutes, None)
65
66
67 class GroupsTestCase(unittest.TestCase):
68     def setUp(self):
69         self.groupdb = GroupMappingDatabase(os.path.join(DATADIR, "group_mapping.tdb"))
70
71     def tearDown(self):
72         self.groupdb.close()
73
74     def test_group_length(self):
75         self.assertEquals(13, len(list(self.groupdb.groupsids())))
76
77     def test_get_group(self):
78         self.assertEquals((-1, 5L, 'Administrators', ''), self.groupdb.get_group("S-1-5-32-544"))
79
80     def test_groupsids(self):
81         sids = list(self.groupdb.groupsids())
82         self.assertTrue("S-1-5-32-544" in sids)
83
84     def test_alias_length(self):
85         self.assertEquals(0, len(list(self.groupdb.aliases())))
86
87
88 class SecretsDbTestCase(unittest.TestCase):
89     def setUp(self):
90         self.secretsdb = SecretsDatabase(os.path.join(DATADIR, "secrets.tdb"))
91
92     def tearDown(self):
93         self.secretsdb.close()
94
95     def test_get_sid(self):
96         self.assertTrue(self.secretsdb.get_sid("BEDWYR") is not None)
97
98
99 class TdbSamTestCase(unittest.TestCase):
100     def setUp(self):
101         self.samdb = TdbSam(os.path.join(DATADIR, "passdb.tdb"))
102
103     def tearDown(self):
104         self.samdb.close()
105
106     def test_usernames(self):
107         self.assertEquals(3, len(list(self.samdb.usernames())))
108
109     def test_getuser(self):
110         user = SAMUser("root")
111         user.logoff_time = 2147483647
112         user.kickoff_time = 2147483647
113         user.pass_can_change_time = 1125418267
114         user.username = "root"
115         user.uid = None
116         user.lm_password = 'U)\x02\x03\x1b\xed\xe9\xef\xaa\xd3\xb45\xb5\x14\x04\xee'
117         user.nt_password = '\x87\x8d\x80\x14`l\xda)gzD\xef\xa15?\xc7'
118         user.acct_ctrl = 16
119         user.pass_last_set_time = 1125418267
120         user.fullname = "root"
121         user.nt_username = ""
122         user.logoff_time = 2147483647
123         user.acct_desc = ""
124         user.group_rid = 1001
125         user.logon_count = 0
126         user.bad_password_count = 0
127         user.domain = "BEDWYR"
128         user.munged_dial = ""
129         user.workstations = ""
130         user.user_rid = 1000
131         user.kickoff_time = 2147483647
132         user.logoff_time = 2147483647
133         user.unknown_6 = 1260L
134         user.logon_divs = 0
135         user.hours = [True for i in range(168)]
136         other = self.samdb["root"]
137         for name in other.__dict__:
138             if other.__dict__[name] != user.__dict__[name]:
139                 print "%s: %r != %r" % (name, other.__dict__[name], user.__dict__[name])
140         self.assertEquals(user, other)
141
142
143 class WinsDatabaseTestCase(unittest.TestCase):
144     def setUp(self):
145         self.winsdb = WinsDatabase(os.path.join(DATADIR, "wins.dat"))
146
147     def test_length(self):
148         self.assertEquals(22, len(self.winsdb))
149
150     def test_first_entry(self):
151         self.assertEqual((1124185120, ["192.168.1.5"], 0x64), self.winsdb["ADMINISTRATOR#03"])
152
153     def tearDown(self):
154         self.winsdb.close()
155
156 class SmbpasswdTestCase(unittest.TestCase):
157     def setUp(self):
158         self.samdb = SmbpasswdFile(os.path.join(DATADIR, "smbpasswd"))
159
160     def test_length(self):
161         self.assertEquals(3, len(self.samdb))
162
163     def test_get_user(self):
164         user = SAMUser("rootpw")
165         user.lm_password = "552902031BEDE9EFAAD3B435B51404EE"
166         user.nt_password = "878D8014606CDA29677A44EFA1353FC7"
167         user.acct_ctrl = ACB_NORMAL
168         user.pass_last_set_time = int(1125418267)
169         user.uid = 0
170         self.assertEquals(user, self.samdb["rootpw"])
171
172     def tearDown(self):
173         self.samdb.close()
174
175
176 class IdmapDbTestCase(unittest.TestCase):
177     def setUp(self):
178         self.idmapdb = IdmapDatabase(os.path.join(DATADIR, "winbindd_idmap.tdb"))
179
180     def test_user_hwm(self):
181         self.assertEquals(10000, self.idmapdb.get_user_hwm())
182
183     def test_group_hwm(self):
184         self.assertEquals(10002, self.idmapdb.get_group_hwm())
185
186     def test_uids(self):
187         self.assertEquals(1, len(list(self.idmapdb.uids())))
188
189     def test_gids(self):
190         self.assertEquals(3, len(list(self.idmapdb.gids())))
191
192     def test_get_user_sid(self):
193         self.assertEquals("S-1-5-21-58189338-3053988021-627566699-501", self.idmapdb.get_user_sid(65534))
194
195     def test_get_group_sid(self):
196         self.assertEquals("S-1-5-21-2447931902-1787058256-3961074038-3007", self.idmapdb.get_group_sid(10001))
197
198     def tearDown(self):
199         self.idmapdb.close()
200
201
202 class ShareInfoTestCase(unittest.TestCase):
203     def setUp(self):
204         self.shareinfodb = ShareInfoDatabase(os.path.join(DATADIR, "share_info.tdb"))
205
206     # FIXME: needs proper data so it can be tested
207
208     def tearDown(self):
209         self.shareinfodb.close()