s4:gensec/gssapi: use gensec_gssapi_max_{input,wrapped}_size() for all backends
[Samba.git] / python / samba / tests / provision.py
blob67f8c18cf10e5ba6a52e7606fc09f2937f1ee5cf
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."""
20 import os
21 from samba.provision import (
22 ProvisionNames,
23 ProvisionPaths,
24 ProvisionResult,
25 determine_netbios_name,
26 sanitize_server_role,
27 setup_secretsdb,
28 findnss,
30 import samba.tests
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.
39 """
40 if lp is None:
41 lp = env_loadparm()
42 paths = ProvisionPaths()
43 paths.secrets = path
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()
49 return secrets_ldb
52 class ProvisionTestCase(samba.tests.TestCaseInTempDir):
53 """Some simple tests for individual functions in the provisioning code.
54 """
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")
60 paths.secrets = path
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())
65 try:
66 self.assertEquals("LSA Secrets",
67 ldb.searchone(basedn="CN=LSA Secrets", attribute="CN"))
68 finally:
69 del ldb
70 os.unlink(path)
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):
78 def x(y):
79 raise KeyError
80 self.assertRaises(KeyError, findnss, x, [])
82 def test_first(self):
83 self.assertEquals("bla", findnss(lambda x: "bla", ["bla"]))
85 def test_skip_first(self):
86 def x(y):
87 if y != "bla":
88 raise KeyError
89 return "ha"
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)
119 def test_vampire(self):
120 raise NotImplementedError(self.test_vampire)
123 class SanitizeServerRoleTests(TestCase):
125 def test_same(self):
126 self.assertEquals("standalone server",
127 sanitize_server_role("standalone server"))
128 self.assertEquals("member server",
129 sanitize_server_role("member server"))
131 def test_invalid(self):
132 self.assertRaises(ValueError, sanitize_server_role, "foo")
134 def test_valid(self):
135 self.assertEquals(
136 "standalone server",
137 sanitize_server_role("ROLE_STANDALONE"))
138 self.assertEquals(
139 "standalone server",
140 sanitize_server_role("standalone"))
141 self.assertEquals(
142 "active directory domain controller",
143 sanitize_server_role("domain controller"))
146 class DummyLogger(object):
148 def __init__(self):
149 self.entries = []
151 def info(self, text, *args):
152 self.entries.append(("INFO", text % args))
155 class ProvisionResultTests(TestCase):
157 def report_logger(self, result):
158 logger = DummyLogger()
159 result.report_logger(logger)
160 return logger.entries
162 def base_result(self):
163 result = ProvisionResult()
164 result.server_role = "domain controller"
165 result.names = ProvisionNames()
166 result.names.hostname = "hostnaam"
167 result.names.domain = "DOMEIN"
168 result.names.dnsdomain = "dnsdomein"
169 result.domainsid = "S1-1-1"
170 result.paths = ProvisionPaths()
171 return result
173 def test_basic_report_logger(self):
174 result = self.base_result()
175 entries = self.report_logger(result)
176 self.assertEquals(entries, [
177 ('INFO', 'Once the above files are installed, your Samba4 server '
178 'will be ready to use'),
179 ('INFO', 'Server Role: domain controller'),
180 ('INFO', 'Hostname: hostnaam'),
181 ('INFO', 'NetBIOS Domain: DOMEIN'),
182 ('INFO', 'DNS Domain: dnsdomein'),
183 ('INFO', 'DOMAIN SID: S1-1-1')])
185 def test_report_logger_adminpass(self):
186 result = self.base_result()
187 result.adminpass_generated = True
188 result.adminpass = "geheim"
189 entries = self.report_logger(result)
190 self.assertEquals(entries[1],
191 ("INFO", 'Admin password: geheim'))
194 class DetermineNetbiosNameTests(TestCase):
196 def test_limits_to_15(self):
197 self.assertEquals("A" * 15, determine_netbios_name("a" * 30))
199 def test_strips_invalid(self):
200 self.assertEquals("BLABLA", determine_netbios_name("bla/bla"))