e400e17168ab6c5ad6ee3ecebeefc8c51c061ca8
[nivanova/samba-autobuild/.git] / source4 / scripting / python / samba / tests / upgradeprovisionneeddc.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 import os
21 import re
22 import shutil
23
24 from samba import param
25 from samba.credentials import Credentials
26 from samba.auth import system_session
27 from samba.provision import getpolicypath
28 from samba.upgradehelpers import (get_paths, get_ldbs,
29                                  find_provision_key_parameters, identic_rename,
30                                  updateOEMInfo, getOEMInfo, update_gpo,
31                                  delta_update_basesamdb,
32                                  search_constructed_attrs_stored)
33 from samba.tests import env_loadparm, TestCaseInTempDir
34 from samba.tests.provision import create_dummy_secretsdb
35 import ldb
36
37
38 def dummymessage(a=None, b=None):
39     pass
40
41 smb_conf_path = "%s/%s/%s" % (os.environ["SELFTEST_PREFIX"], "dc", "etc/smb.conf")
42
43 class UpgradeProvisionBasicLdbHelpersTestCase(TestCaseInTempDir):
44     """Some simple tests for individual functions in the provisioning code.
45     """
46
47     def test_get_ldbs(self):
48         paths = get_paths(param, None, smb_conf_path)
49         creds = Credentials()
50         lp = env_loadparm()
51         creds.guess(lp)
52         get_ldbs(paths, creds, system_session(), lp)
53
54     def test_find_key_param(self):
55         paths = get_paths(param, None, smb_conf_path)
56         creds = Credentials()
57         lp = env_loadparm()
58         creds.guess(lp)
59         rootdn = "dc=samba,dc=example,dc=com"
60         ldbs = get_ldbs(paths, creds, system_session(), lp)
61         names = find_provision_key_parameters(ldbs.sam, ldbs.secrets, ldbs.idmap,
62                                                 paths, smb_conf_path, lp)
63         self.assertEquals(names.realm, "SAMBA.EXAMPLE.COM")
64         self.assertEquals(str(names.rootdn).lower(), rootdn.lower())
65         self.assertNotEquals(names.policyid_dc, None)
66         self.assertNotEquals(names.ntdsguid, "")
67
68
69 class UpgradeProvisionWithLdbTestCase(TestCaseInTempDir):
70
71     def _getEmptyDbName(self):
72         return os.path.join(self.tempdir, "sam.ldb")
73
74     def setUp(self):
75         super(UpgradeProvisionWithLdbTestCase, self).setUp()
76         paths = get_paths(param, None, smb_conf_path)
77         self.creds = Credentials()
78         self.lp = env_loadparm()
79         self.creds.guess(self.lp)
80         self.paths = paths
81         self.ldbs = get_ldbs(paths, self.creds, system_session(), self.lp)
82         self.names = find_provision_key_parameters(self.ldbs.sam,
83                 self.ldbs.secrets, self.ldbs.idmap, paths, smb_conf_path,
84                 self.lp)
85         self.referencedb = create_dummy_secretsdb(
86             os.path.join(self.tempdir, "ref.ldb"))
87
88     def test_search_constructed_attrs_stored(self):
89         hashAtt = search_constructed_attrs_stored(self.ldbs.sam,
90                                                   self.names.rootdn,
91                                                   ["msds-KeyVersionNumber"])
92         self.assertFalse(hashAtt.has_key("msds-KeyVersionNumber"))
93
94     def test_identic_rename(self):
95         rootdn = "DC=samba,DC=example,DC=com"
96
97         guestDN = ldb.Dn(self.ldbs.sam, "CN=Guest,CN=Users,%s" % rootdn)
98         identic_rename(self.ldbs.sam, guestDN)
99         res = self.ldbs.sam.search(expression="(name=Guest)", base=rootdn,
100                                 scope=ldb.SCOPE_SUBTREE, attrs=["dn"])
101         self.assertEquals(len(res), 1)
102         self.assertEquals(str(res[0]["dn"]), "CN=Guest,CN=Users,%s" % rootdn)
103
104     def test_delta_update_basesamdb(self):
105         dummysampath = self._getEmptyDbName()
106         delta_update_basesamdb(self.paths.samdb, dummysampath,
107                                 self.creds, system_session(), self.lp,
108                                 dummymessage)
109
110     def test_update_gpo_simple(self):
111         dir = getpolicypath(self.paths.sysvol, self.names.dnsdomain,
112                 self.names.policyid)
113         shutil.rmtree(dir)
114         self.assertFalse(os.path.isdir(dir))
115         update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
116         self.assertTrue(os.path.isdir(dir))
117
118     def test_update_gpo_acl(self):
119         path = os.path.join(self.tempdir, "testupdategpo")
120         save = self.paths.sysvol
121         self.paths.sysvol = path
122         os.mkdir(path)
123         os.mkdir(os.path.join(path, self.names.dnsdomain))
124         os.mkdir(os.path.join(os.path.join(path, self.names.dnsdomain),
125             "Policies"))
126         update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
127         shutil.rmtree(path)
128         self.paths.sysvol = save
129
130     def test_getOEMInfo(self):
131         realm = self.lp.get("realm")
132         basedn = "DC=%s" % realm.replace(".", ", DC=")
133         oem = getOEMInfo(self.ldbs.sam, basedn)
134         self.assertNotEquals(oem, "")
135
136     def test_updateOEMInfo(self):
137         realm = self.lp.get("realm")
138         basedn = "DC=%s" % realm.replace(".", ", DC=")
139         oem = getOEMInfo(self.ldbs.sam, basedn)
140         updateOEMInfo(self.ldbs.sam, basedn)
141         oem2 = getOEMInfo(self.ldbs.sam, basedn)
142         self.assertNotEquals(str(oem), str(oem2))
143         self.assertTrue(re.match(".*upgrade to.*", str(oem2)))
144
145     def tearDown(self):
146         for name in ["ref.ldb", "secrets.ldb", "sam.ldb"]:
147             path = os.path.join(self.tempdir, name)
148             if os.path.exists(path):
149                 os.unlink(path)
150         super(UpgradeProvisionWithLdbTestCase, self).tearDown()