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
33 def create_dummy_secretsdb(path, lp=None):
34 """Create a dummy secrets database for use in tests.
36 :param path: Path to store the secrets db
37 :param lp: Optional loadparm context. A simple one will
38 be generated if not specified.
42 paths = ProvisionPaths()
44 paths.private_dir = os.path.dirname(path)
45 paths.keytab = "no.keytab"
46 paths.dns_keytab = "no.dns.keytab"
47 secrets_ldb = setup_secretsdb(paths, None, None, lp=lp)
48 secrets_ldb.transaction_commit()
52 class ProvisionTestCase(samba.tests.TestCaseInTempDir):
53 """Some simple tests for individual functions in the provisioning code.
56 def test_setup_secretsdb(self):
57 path = os.path.join(self.tempdir, "secrets.ldb")
58 paths = ProvisionPaths()
59 secrets_tdb_path = os.path.join(self.tempdir, "secrets.tdb")
61 paths.private_dir = os.path.dirname(path)
62 paths.keytab = "no.keytab"
63 paths.dns_keytab = "no.dns.keytab"
64 ldb = setup_secretsdb(paths, None, None, lp=env_loadparm())
66 self.assertEquals("LSA Secrets",
67 ldb.searchone(basedn="CN=LSA Secrets", attribute="CN"))
71 if os.path.exists(secrets_tdb_path):
72 os.unlink(secrets_tdb_path)
74 class FindNssTests(TestCase):
75 """Test findnss() function."""
77 def test_nothing(self):
80 self.assertRaises(KeyError, findnss, x, [])
83 self.assertEquals("bla", findnss(lambda x: "bla", ["bla"]))
85 def test_skip_first(self):
90 self.assertEquals("ha", findnss(x, ["bloe", "bla"]))
93 class Disabled(object):
95 def test_setup_templatesdb(self):
96 raise NotImplementedError(self.test_setup_templatesdb)
98 def test_setup_registry(self):
99 raise NotImplementedError(self.test_setup_registry)
101 def test_setup_samdb_rootdse(self):
102 raise NotImplementedError(self.test_setup_samdb_rootdse)
104 def test_setup_samdb_partitions(self):
105 raise NotImplementedError(self.test_setup_samdb_partitions)
107 def test_provision_dns(self):
108 raise NotImplementedError(self.test_provision_dns)
110 def test_provision_ldapbase(self):
111 raise NotImplementedError(self.test_provision_ldapbase)
113 def test_provision_guess(self):
114 raise NotImplementedError(self.test_provision_guess)
116 def test_join_domain(self):
117 raise NotImplementedError(self.test_join_domain)
120 class SanitizeServerRoleTests(TestCase):
123 self.assertEquals("standalone server",
124 sanitize_server_role("standalone server"))
125 self.assertEquals("member server",
126 sanitize_server_role("member server"))
128 def test_invalid(self):
129 self.assertRaises(ValueError, sanitize_server_role, "foo")
131 def test_valid(self):
134 sanitize_server_role("ROLE_STANDALONE"))
137 sanitize_server_role("standalone"))
139 "active directory domain controller",
140 sanitize_server_role("domain controller"))
143 class DummyLogger(object):
148 def info(self, text, *args):
149 self.entries.append(("INFO", text % args))
152 class ProvisionResultTests(TestCase):
154 def report_logger(self, result):
155 logger = DummyLogger()
156 result.report_logger(logger)
157 return logger.entries
159 def base_result(self):
160 result = ProvisionResult()
161 result.server_role = "domain controller"
162 result.names = ProvisionNames()
163 result.names.hostname = "hostnaam"
164 result.names.domain = "DOMEIN"
165 result.names.dnsdomain = "dnsdomein"
166 result.domainsid = "S1-1-1"
167 result.paths = ProvisionPaths()
170 def test_basic_report_logger(self):
171 result = self.base_result()
172 entries = self.report_logger(result)
173 self.assertEquals(entries, [
174 ('INFO', 'Once the above files are installed, your Samba4 server '
175 'will be ready to use'),
176 ('INFO', 'Server Role: domain controller'),
177 ('INFO', 'Hostname: hostnaam'),
178 ('INFO', 'NetBIOS Domain: DOMEIN'),
179 ('INFO', 'DNS Domain: dnsdomein'),
180 ('INFO', 'DOMAIN SID: S1-1-1')])
182 def test_report_logger_adminpass(self):
183 result = self.base_result()
184 result.adminpass_generated = True
185 result.adminpass = "geheim"
186 entries = self.report_logger(result)
187 self.assertEquals(entries[1],
188 ("INFO", 'Admin password: geheim'))
191 class DetermineNetbiosNameTests(TestCase):
193 def test_limits_to_15(self):
194 self.assertEquals("A" * 15, determine_netbios_name("a" * 30))
196 def test_strips_invalid(self):
197 self.assertEquals("BLABLA", determine_netbios_name("bla/bla"))