1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2012
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.
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.
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/>.
18 """Tests for samba.provision."""
21 from samba
.provision
import (
25 determine_netbios_name
,
31 from samba
.tests
import env_loadparm
, TestCase
34 def create_dummy_secretsdb(path
, lp
=None):
35 """Create a dummy secrets database for use in tests.
37 :param path: Path to store the secrets db
38 :param lp: Optional loadparm context. A simple one will
39 be generated if not specified.
43 paths
= ProvisionPaths()
45 paths
.private_dir
= os
.path
.dirname(path
)
46 paths
.binddns_dir
= os
.path
.dirname(path
)
47 paths
.keytab
= "no.keytab"
48 paths
.dns_keytab
= "no.dns.keytab"
49 secrets_ldb
= setup_secretsdb(paths
, None, lp
=lp
)
50 secrets_ldb
.transaction_commit()
54 class ProvisionTestCase(samba
.tests
.TestCaseInTempDir
):
55 """Some simple tests for individual functions in the provisioning code.
58 def test_setup_secretsdb(self
):
59 path
= os
.path
.join(self
.tempdir
, "secrets.ldb")
60 paths
= ProvisionPaths()
61 secrets_tdb_path
= os
.path
.join(self
.tempdir
, "secrets.tdb")
63 paths
.private_dir
= os
.path
.dirname(path
)
64 paths
.binddns_dir
= os
.path
.dirname(path
)
65 paths
.keytab
= "no.keytab"
66 paths
.dns_keytab
= "no.dns.keytab"
67 ldb
= setup_secretsdb(paths
, None, lp
=env_loadparm())
69 self
.assertEqual("LSA Secrets",
70 ldb
.searchone(basedn
="CN=LSA Secrets", attribute
="CN").decode('utf8'))
74 if os
.path
.exists(secrets_tdb_path
):
75 os
.unlink(secrets_tdb_path
)
78 class FindNssTests(TestCase
):
79 """Test findnss() function."""
81 def test_nothing(self
):
84 self
.assertRaises(KeyError, findnss
, x
, [])
87 self
.assertEqual("bla", findnss(lambda x
: "bla", ["bla"]))
89 def test_skip_first(self
):
94 self
.assertEqual("ha", findnss(x
, ["bloe", "bla"]))
97 class Disabled(object):
99 def test_setup_templatesdb(self
):
100 raise NotImplementedError(self
.test_setup_templatesdb
)
102 def test_setup_registry(self
):
103 raise NotImplementedError(self
.test_setup_registry
)
105 def test_setup_samdb_rootdse(self
):
106 raise NotImplementedError(self
.test_setup_samdb_rootdse
)
108 def test_setup_samdb_partitions(self
):
109 raise NotImplementedError(self
.test_setup_samdb_partitions
)
111 def test_provision_dns(self
):
112 raise NotImplementedError(self
.test_provision_dns
)
114 def test_provision_ldapbase(self
):
115 raise NotImplementedError(self
.test_provision_ldapbase
)
117 def test_provision_guess(self
):
118 raise NotImplementedError(self
.test_provision_guess
)
120 def test_join_domain(self
):
121 raise NotImplementedError(self
.test_join_domain
)
124 class SanitizeServerRoleTests(TestCase
):
127 self
.assertEqual("standalone server",
128 sanitize_server_role("standalone server"))
129 self
.assertEqual("member server",
130 sanitize_server_role("member server"))
132 def test_invalid(self
):
133 self
.assertRaises(ValueError, sanitize_server_role
, "foo")
135 def test_valid(self
):
138 sanitize_server_role("ROLE_STANDALONE"))
141 sanitize_server_role("standalone"))
143 "active directory domain controller",
144 sanitize_server_role("domain controller"))
147 class DummyLogger(object):
152 def info(self
, text
, *args
):
153 self
.entries
.append(("INFO", text
% args
))
156 class ProvisionResultTests(TestCase
):
158 def report_logger(self
, result
):
159 logger
= DummyLogger()
160 result
.report_logger(logger
)
161 return logger
.entries
163 def base_result(self
):
164 result
= ProvisionResult()
165 result
.server_role
= "domain controller"
166 result
.names
= ProvisionNames()
167 result
.names
.hostname
= "hostnaam"
168 result
.names
.domain
= "DOMEIN"
169 result
.names
.dnsdomain
= "dnsdomein"
170 result
.domainsid
= "S1-1-1"
171 result
.paths
= ProvisionPaths()
174 def test_basic_report_logger(self
):
175 result
= self
.base_result()
176 entries
= self
.report_logger(result
)
177 self
.assertEqual(entries
, [
178 ('INFO', 'Once the above files are installed, your Samba AD server '
179 'will be ready to use'),
180 ('INFO', 'Server Role: domain controller'),
181 ('INFO', 'Hostname: hostnaam'),
182 ('INFO', 'NetBIOS Domain: DOMEIN'),
183 ('INFO', 'DNS Domain: dnsdomein'),
184 ('INFO', 'DOMAIN SID: S1-1-1')])
186 def test_report_logger_adminpass(self
):
187 result
= self
.base_result()
188 result
.adminpass_generated
= True
189 result
.adminpass
= "geheim"
190 entries
= self
.report_logger(result
)
191 self
.assertEqual(entries
[1],
192 ("INFO", 'Admin password: geheim'))
195 class DetermineNetbiosNameTests(TestCase
):
197 def test_limits_to_15(self
):
198 self
.assertEqual("A" * 15, determine_netbios_name("a" * 30))
200 def test_strips_invalid(self
):
201 self
.assertEqual("BLABLA", determine_netbios_name("bla/bla"))