s4-python: python is not always in /usr/bin
[samba.git] / source4 / scripting / python / samba / tests / samba3.py
1 #!/usr/bin/env 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__),
28                        "../../../../../testdata/samba3")
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         super(RegistryTestCase, self).tearDown()
39
40     def test_length(self):
41         self.assertEquals(28, len(self.registry))
42
43     def test_keys(self):
44         self.assertTrue("HKLM" in self.registry.keys())
45
46     def test_subkeys(self):
47         self.assertEquals(["SOFTWARE", "SYSTEM"], self.registry.subkeys("HKLM"))
48
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"))
53
54
55 class PolicyTestCase(TestCase):
56
57     def setUp(self):
58         super(PolicyTestCase, self).setUp()
59         self.policy = PolicyDatabase(os.path.join(DATADIR, "account_policy.tdb"))
60
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)
72
73
74 class GroupsTestCase(TestCase):
75
76     def setUp(self):
77         super(GroupsTestCase, self).setUp()
78         self.groupdb = GroupMappingDatabase(os.path.join(DATADIR, "group_mapping.tdb"))
79
80     def tearDown(self):
81         self.groupdb.close()
82         super(GroupsTestCase, self).tearDown()
83
84     def test_group_length(self):
85         self.assertEquals(13, len(list(self.groupdb.groupsids())))
86
87     def test_get_group(self):
88         self.assertEquals((-1, 5L, 'Administrators', ''), self.groupdb.get_group("S-1-5-32-544"))
89
90     def test_groupsids(self):
91         sids = list(self.groupdb.groupsids())
92         self.assertTrue("S-1-5-32-544" in sids)
93
94     def test_alias_length(self):
95         self.assertEquals(0, len(list(self.groupdb.aliases())))
96
97
98 class SecretsDbTestCase(TestCase):
99
100     def setUp(self):
101         super(SecretsDbTestCase, self).setUp()
102         self.secretsdb = SecretsDatabase(os.path.join(DATADIR, "secrets.tdb"))
103
104     def tearDown(self):
105         self.secretsdb.close()
106         super(SecretsDbTestCase, self).tearDown()
107
108     def test_get_sid(self):
109         self.assertTrue(self.secretsdb.get_sid("BEDWYR") is not None)
110
111
112 class TdbSamTestCase(TestCase):
113
114     def setUp(self):
115         super(TdbSamTestCase, self).setUp()
116         self.samdb = TdbSam(os.path.join(DATADIR, "passdb.tdb"))
117
118     def tearDown(self):
119         self.samdb.close()
120         super(TdbSamTestCase, self).tearDown()
121
122     def test_usernames(self):
123         self.assertEquals(3, len(list(self.samdb.usernames())))
124
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"
131         user.uid = None
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'
134         user.acct_ctrl = 16
135         user.pass_last_set_time = 1125418267
136         user.fullname = "root"
137         user.nt_username = ""
138         user.logoff_time = 2147483647
139         user.acct_desc = ""
140         user.group_rid = 1001
141         user.logon_count = 0
142         user.bad_password_count = 0
143         user.domain = "BEDWYR"
144         user.munged_dial = ""
145         user.workstations = ""
146         user.user_rid = 1000
147         user.kickoff_time = 2147483647
148         user.logoff_time = 2147483647
149         user.unknown_6 = 1260L
150         user.logon_divs = 0
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)
157
158
159 class WinsDatabaseTestCase(TestCase):
160
161     def setUp(self):
162         super(WinsDatabaseTestCase, self).setUp()
163         self.winsdb = WinsDatabase(os.path.join(DATADIR, "wins.dat"))
164
165     def test_length(self):
166         self.assertEquals(22, len(self.winsdb))
167
168     def test_first_entry(self):
169         self.assertEqual((1124185120, ["192.168.1.5"], 0x64), self.winsdb["ADMINISTRATOR#03"])
170
171     def tearDown(self):
172         self.winsdb.close()
173         super(WinsDatabaseTestCase, self).tearDown()
174
175
176 class SmbpasswdTestCase(TestCase):
177
178     def setUp(self):
179         super(SmbpasswdTestCase, self).setUp()
180         self.samdb = SmbpasswdFile(os.path.join(DATADIR, "smbpasswd"))
181
182     def test_length(self):
183         self.assertEquals(3, len(self.samdb))
184
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)
191         user.uid = 0
192         self.assertEquals(user, self.samdb["rootpw"])
193
194     def tearDown(self):
195         self.samdb.close()
196         super(SmbpasswdTestCase, self).tearDown()
197
198
199 class IdmapDbTestCase(TestCase):
200
201     def setUp(self):
202         super(IdmapDbTestCase, self).setUp()
203         self.idmapdb = IdmapDatabase(os.path.join(DATADIR,
204             "winbindd_idmap.tdb"))
205
206     def test_user_hwm(self):
207         self.assertEquals(10000, self.idmapdb.get_user_hwm())
208
209     def test_group_hwm(self):
210         self.assertEquals(10002, self.idmapdb.get_group_hwm())
211
212     def test_uids(self):
213         self.assertEquals(1, len(list(self.idmapdb.uids())))
214
215     def test_gids(self):
216         self.assertEquals(3, len(list(self.idmapdb.gids())))
217
218     def test_get_user_sid(self):
219         self.assertEquals("S-1-5-21-58189338-3053988021-627566699-501", self.idmapdb.get_user_sid(65534))
220
221     def test_get_group_sid(self):
222         self.assertEquals("S-1-5-21-2447931902-1787058256-3961074038-3007", self.idmapdb.get_group_sid(10001))
223
224     def tearDown(self):
225         self.idmapdb.close()
226         super(IdmapDbTestCase, self).tearDown()
227
228
229 class ParamTestCase(TestCase):
230
231     def test_init(self):
232         file = ParamFile()
233         self.assertTrue(file is not None)
234
235     def test_add_section(self):
236         file = ParamFile()
237         file.add_section("global")
238         self.assertTrue(file["global"] is not None)
239
240     def test_set_param_string(self):
241         file = ParamFile()
242         file.add_section("global")
243         file.set_string("data", "bar")
244         self.assertEquals("bar", file.get_string("data"))
245
246     def test_get_section(self):
247         file = ParamFile()
248         self.assertEquals(None, file.get_section("unknown"))
249         self.assertRaises(KeyError, lambda: file["unknown"])