3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
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.
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.
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/>.
23 from samba.credentials import Credentials
24 from samba.auth import system_session
25 from samba.provision import getpolicypath
26 from samba.upgradehelpers import (get_paths, get_ldbs,
27 find_provision_key_parameters, identic_rename,
28 updateOEMInfo, getOEMInfo, update_gpo,
29 delta_update_basesamdb)
31 from samba.tests.provision import create_dummy_secretsdb
32 from samba import param
33 from samba.tests import env_loadparm, TestCaseInTempDir
37 def dummymessage(a=None, b=None):
42 smbConfPath = "%s/%s/%s" % (os.environ["SELFTEST_PREFIX"], "dc", "etc/smb.conf")
44 class UpgradeProvisionBasicLdbHelpersTestCase(TestCaseInTempDir):
45 """Some simple tests for individual functions in the provisioning code.
48 def test_get_ldbs(self):
49 paths = get_paths(param, None, smbConfPath)
52 get_ldbs(paths, creds, system_session(), lp)
54 def test_find_key_param(self):
55 paths = get_paths(param, None, smbConfPath)
58 rootdn = "dc=samba,dc=example,dc=com"
59 ldbs = get_ldbs(paths, creds, system_session(), lp)
60 names = find_provision_key_parameters(ldbs.sam, ldbs.secrets, ldbs.idmap,
61 paths, smbConfPath, lp)
62 self.assertEquals(names.realm, "SAMBA.EXAMPLE.COM")
63 self.assertTrue(str(names.rootdn).lower() == rootdn.lower())
64 self.assertTrue(names.policyid_dc != None)
65 self.assertTrue(names.ntdsguid != "")
68 class UpgradeProvisionWithLdbTestCase(TestCaseInTempDir):
69 def _getEmptyDbName(self):
70 return os.path.join(self.tempdir, "sam.ldb")
73 super(UpgradeProvisionWithLdbTestCase, self).setUp()
74 paths = get_paths(param, None, smbConfPath)
75 self.creds = Credentials()
78 self.ldbs = get_ldbs(paths, self.creds, system_session(), lp)
80 self.names = find_provision_key_parameters(self.ldbs.sam, self.ldbs.secrets,
81 self.ldbs.idmap, paths, smbConfPath, lp)
82 self.referencedb = create_dummy_secretsdb(
83 os.path.join(self.tempdir, "ref.ldb"))
85 def test_identic_rename(self):
86 rootdn = "DC=samba,DC=example,DC=com"
88 guestDN = ldb.Dn(self.ldbs.sam, "CN=Guest,CN=Users,%s" % rootdn)
89 identic_rename(self.ldbs.sam, guestDN)
90 res = self.ldbs.sam.search(expression="(name=Guest)", base=rootdn,
91 scope=ldb.SCOPE_SUBTREE, attrs=["dn"])
92 self.assertEquals(len(res), 1)
93 self.assertEquals(str(res[0]["dn"]), "CN=Guest,CN=Users,%s" % rootdn)
95 def test_delta_update_basesamdb(self):
96 dummysampath = self._getEmptyDbName()
97 delta_update_basesamdb(self.paths.samdb, dummysampath,
98 self.creds, system_session(), self.lp, dummymessage)
100 def test_update_gpo_simple(self):
101 dir = getpolicypath(self.paths.sysvol, self.names.dnsdomain, self.names.policyid)
103 self.assertFalse(os.path.isdir(dir))
104 update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
105 self.assertTrue(os.path.isdir(dir))
107 def test_update_gpo_acl(self):
108 path = os.path.join(self.tempdir, "testupdategpo")
109 save = self.paths.sysvol
110 self.paths.sysvol = path
112 os.mkdir(os.path.join(path, self.names.dnsdomain))
113 os.mkdir(os.path.join(os.path.join(path, self.names.dnsdomain), "Policies"))
114 update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
116 self.paths.sysvol = save
118 def test_getOEMInfo(self):
119 realm = self.lp.get("realm")
120 basedn = "DC=%s" % realm.replace(".", ", DC=")
121 oem = getOEMInfo(self.ldbs.sam, basedn)
122 self.assertTrue(oem != "")
124 def test_updateOEMInfo(self):
125 realm = self.lp.get("realm")
126 basedn = "DC=%s" % realm.replace(".", ", DC=")
127 oem = getOEMInfo(self.ldbs.sam, basedn)
128 updateOEMInfo(self.ldbs.sam, basedn)
129 oem2 = getOEMInfo(self.ldbs.sam, basedn)
130 self.assertTrue(str(oem) != str(oem2))
131 self.assertTrue(re.match(".*upgrade to.*", str(oem2)))
134 for name in ["ref.ldb", "secrets.ldb", "sam.ldb"]:
135 path = os.path.join(self.tempdir, name)
136 if os.path.exists(path):
138 super(UpgradeProvisionWithLdbTestCase, self).tearDown()