329614783ae7363deddbab007ba858c2613d5acb
[samba.git] / python / samba / tests / s3passdb.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
3 #
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.
8 #
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.
13 #
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/>.
16 #
17
18 """Tests for samba.s3passdb"""
19
20 from samba.samba3 import passdb
21 from samba.samba3 import param as s3param
22 from samba.tests import TestCase, TestCaseInTempDir
23 from samba.dcerpc.security import dom_sid
24 import os
25
26
27 for p in ["../../../../../testdata/samba3", "../../../../testdata/samba3"]:
28     DATADIR = os.path.join(os.path.dirname(__file__), p)
29     if os.path.exists(DATADIR):
30         break
31
32
33 class PassdbTestCase(TestCaseInTempDir):
34
35     def setUp(self):
36         super(PassdbTestCase, self).setUp()
37         os.system("cp -r %s %s" % (DATADIR, self.tempdir))
38         datadir = os.path.join(self.tempdir, "samba3")
39
40         self.lp = s3param.get_context()
41         self.lp.load(os.path.join(datadir, "smb.conf"))
42         self.lp.set("private dir", datadir)
43         self.lp.set("state directory", datadir)
44         self.lp.set("lock directory", datadir)
45         self.lp.set("cache directory", datadir)
46         passdb.set_secrets_dir(datadir)
47         self.pdb = passdb.PDB("tdbsam")
48
49     def tearDown(self):
50         self.lp = []
51         self.pdb = []
52         os.system("rm -rf %s" % os.path.join(self.tempdir, "samba3"))
53         super(PassdbTestCase, self).tearDown()
54
55     def test_policy(self):
56         policy = self.pdb.get_account_policy()
57         self.assertEquals(0, policy['bad lockout attempt'])
58         self.assertEquals(-1, policy['disconnect time'])
59         self.assertEquals(0, policy['lockout duration'])
60         self.assertEquals(999999999, policy['maximum password age'])
61         self.assertEquals(0, policy['minimum password age'])
62         self.assertEquals(5, policy['min password length'])
63         self.assertEquals(0, policy['password history'])
64         self.assertEquals(0, policy['refuse machine password change'])
65         self.assertEquals(0, policy['reset count minutes'])
66         self.assertEquals(0, policy['user must logon to change password'])
67
68     def test_get_sid(self):
69         domain_sid = passdb.get_global_sam_sid()
70         self.assertEquals(dom_sid("S-1-5-21-2470180966-3899876309-2637894779"), domain_sid)
71
72     def test_usernames(self):
73         userlist = self.pdb.search_users(0)
74         self.assertEquals(3, len(userlist))
75
76     def test_getuser(self):
77         user = self.pdb.getsampwnam("root")
78
79         self.assertEquals(16, user.acct_ctrl)
80         self.assertEquals("", user.acct_desc)
81         self.assertEquals(0, user.bad_password_count)
82         self.assertEquals(0, user.bad_password_time)
83         self.assertEquals(0, user.code_page)
84         self.assertEquals(0, user.country_code)
85         self.assertEquals("", user.dir_drive)
86         self.assertEquals("BEDWYR", user.domain)
87         self.assertEquals("root", user.full_name)
88         self.assertEquals(dom_sid('S-1-5-21-2470180966-3899876309-2637894779-513'), user.group_sid)
89         self.assertEquals("\\\\BEDWYR\\root", user.home_dir)
90         self.assertEquals([-1 for i in range(21)], user.hours)
91         self.assertEquals(21, user.hours_len)
92         self.assertEquals(9223372036854775807, user.kickoff_time)
93         self.assertEquals(None, user.lanman_passwd)
94         self.assertEquals(9223372036854775807, user.logoff_time)
95         self.assertEquals(0, user.logon_count)
96         self.assertEquals(168, user.logon_divs)
97         self.assertEquals("", user.logon_script)
98         self.assertEquals(0, user.logon_time)
99         self.assertEquals("", user.munged_dial)
100         self.assertEquals(b'\x87\x8d\x80\x14`l\xda)gzD\xef\xa15?\xc7', user.nt_passwd)
101         self.assertEquals("", user.nt_username)
102         self.assertEquals(1125418267, user.pass_can_change_time)
103         self.assertEquals(1125418267, user.pass_last_set_time)
104         self.assertEquals(2125418266, user.pass_must_change_time)
105         self.assertEquals(None, user.plaintext_passwd)
106         self.assertEquals("\\\\BEDWYR\\root\\profile", user.profile_path)
107         self.assertEquals(None, user.pw_history)
108         self.assertEquals(dom_sid("S-1-5-21-2470180966-3899876309-2637894779-1000"), user.user_sid)
109         self.assertEquals("root", user.username)
110         self.assertEquals("", user.workstations)
111
112     def test_group_length(self):
113         grouplist = self.pdb.enum_group_mapping()
114         self.assertEquals(13, len(grouplist))
115
116     def test_get_group(self):
117         group = self.pdb.getgrsid(dom_sid("S-1-5-32-544"))
118         self.assertEquals("Administrators", group.nt_name)
119         self.assertEquals(-1, group.gid)
120         self.assertEquals(5, group.sid_name_use)
121
122     def test_groupsids(self):
123         grouplist = self.pdb.enum_group_mapping()
124         sids = []
125         for g in grouplist:
126             sids.append(str(g.sid))
127         self.assertTrue("S-1-5-32-544" in sids)
128         self.assertTrue("S-1-5-32-545" in sids)
129         self.assertTrue("S-1-5-32-546" in sids)
130         self.assertTrue("S-1-5-32-548" in sids)
131         self.assertTrue("S-1-5-32-549" in sids)
132         self.assertTrue("S-1-5-32-550" in sids)
133         self.assertTrue("S-1-5-32-551" in sids)
134
135     def test_alias_length(self):
136         aliaslist = self.pdb.search_aliases()
137         self.assertEquals(1, len(aliaslist))
138         self.assertEquals("Jelmers NT Group", aliaslist[0]['account_name'])