Remove ntdb from scripts
[samba.git] / python / samba / tests / upgradeprovisionneeddc.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
3 #
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.
8 #
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.
13 #
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/>.
16 #
17
18 """Tests for samba.upgradeprovision that need a DC."""
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,find_provision_key_parameters
28 from samba.upgradehelpers import (get_paths, get_ldbs,
29                                  identic_rename,
30                                  updateOEMInfo, getOEMInfo, update_gpo,
31                                  delta_update_basesamdb,
32                                  update_dns_account_password,
33                                  search_constructed_attrs_stored,
34                                  increment_calculated_keyversion_number)
35 from samba.tests import env_loadparm, TestCaseInTempDir
36 from samba.tests.provision import create_dummy_secretsdb
37 import ldb
38
39
40 def dummymessage(a=None, b=None):
41     pass
42
43 smb_conf_path = "%s/%s/%s" % (os.environ["SELFTEST_PREFIX"], "ad_dc_ntvfs", "etc/smb.conf")
44
45 class UpgradeProvisionBasicLdbHelpersTestCase(TestCaseInTempDir):
46     """Some simple tests for individual functions in the provisioning code.
47     """
48
49     def test_get_ldbs(self):
50         paths = get_paths(param, None, smb_conf_path)
51         creds = Credentials()
52         lp = env_loadparm()
53         creds.guess(lp)
54         get_ldbs(paths, creds, system_session(), lp)
55
56     def test_find_key_param(self):
57         paths = get_paths(param, None, smb_conf_path)
58         creds = Credentials()
59         lp = env_loadparm()
60         creds.guess(lp)
61         rootdn = "dc=samba,dc=example,dc=com"
62         ldbs = get_ldbs(paths, creds, system_session(), lp)
63         names = find_provision_key_parameters(ldbs.sam, ldbs.secrets, ldbs.idmap,
64                                                 paths, smb_conf_path, lp)
65         self.assertEquals(names.realm, "SAMBA.EXAMPLE.COM")
66         self.assertEquals(str(names.rootdn).lower(), rootdn.lower())
67         self.assertNotEquals(names.policyid_dc, None)
68         self.assertNotEquals(names.ntdsguid, "")
69
70
71 class UpgradeProvisionWithLdbTestCase(TestCaseInTempDir):
72
73     def _getEmptyDbName(self):
74         return os.path.join(self.tempdir, "sam.ldb")
75
76     def setUp(self):
77         super(UpgradeProvisionWithLdbTestCase, self).setUp()
78         paths = get_paths(param, None, smb_conf_path)
79         self.creds = Credentials()
80         self.lp = env_loadparm()
81         self.creds.guess(self.lp)
82         self.paths = paths
83         self.ldbs = get_ldbs(paths, self.creds, system_session(), self.lp)
84         self.names = find_provision_key_parameters(self.ldbs.sam,
85                 self.ldbs.secrets, self.ldbs.idmap, paths, smb_conf_path,
86                 self.lp)
87         self.referencedb = create_dummy_secretsdb(
88             os.path.join(self.tempdir, "ref.ldb"))
89
90     def test_search_constructed_attrs_stored(self):
91         hashAtt = search_constructed_attrs_stored(self.ldbs.sam,
92                                                   self.names.rootdn,
93                                                   ["msds-KeyVersionNumber"])
94         self.assertFalse(hashAtt.has_key("msds-KeyVersionNumber"))
95
96     def test_increment_calculated_keyversion_number(self):
97         dn = "CN=Administrator,CN=Users,%s" % self.names.rootdn
98         # We conctruct a simple hash for the user administrator
99         hash = {}
100         # And we want the version to be 140
101         hash[dn.lower()] = 140
102
103         increment_calculated_keyversion_number(self.ldbs.sam,
104                                                self.names.rootdn,
105                                                hash)
106         self.assertEqual(self.ldbs.sam.get_attribute_replmetadata_version(dn,
107                             "unicodePwd"),
108                             140)
109         # This function should not decrement the version
110         hash[dn.lower()] = 130
111
112         increment_calculated_keyversion_number(self.ldbs.sam,
113                                                self.names.rootdn,
114                                                hash)
115         self.assertEqual(self.ldbs.sam.get_attribute_replmetadata_version(dn,
116                             "unicodePwd"),
117                             140)
118
119     def test_identic_rename(self):
120         rootdn = "DC=samba,DC=example,DC=com"
121
122         guestDN = ldb.Dn(self.ldbs.sam, "CN=Guest,CN=Users,%s" % rootdn)
123         identic_rename(self.ldbs.sam, guestDN)
124         res = self.ldbs.sam.search(expression="(name=Guest)", base=rootdn,
125                                 scope=ldb.SCOPE_SUBTREE, attrs=["dn"])
126         self.assertEquals(len(res), 1)
127         self.assertEquals(str(res[0]["dn"]), "CN=Guest,CN=Users,%s" % rootdn)
128
129     def test_delta_update_basesamdb(self):
130         dummysampath = self._getEmptyDbName()
131         delta_update_basesamdb(self.paths.samdb, dummysampath,
132                                 self.creds, system_session(), self.lp,
133                                 dummymessage)
134
135     def test_update_gpo_simple(self):
136         dir = getpolicypath(self.paths.sysvol, self.names.dnsdomain,
137                 self.names.policyid)
138         shutil.rmtree(dir)
139         self.assertFalse(os.path.isdir(dir))
140         update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
141         self.assertTrue(os.path.isdir(dir))
142
143     def test_update_gpo_acl(self):
144         path = os.path.join(self.tempdir, "testupdategpo")
145         save = self.paths.sysvol
146         self.paths.sysvol = path
147         os.mkdir(path)
148         os.mkdir(os.path.join(path, self.names.dnsdomain))
149         os.mkdir(os.path.join(os.path.join(path, self.names.dnsdomain),
150             "Policies"))
151         update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
152         shutil.rmtree(path)
153         self.paths.sysvol = save
154
155     def test_getOEMInfo(self):
156         realm = self.lp.get("realm")
157         basedn = "DC=%s" % realm.replace(".", ", DC=")
158         oem = getOEMInfo(self.ldbs.sam, basedn)
159         self.assertNotEquals(oem, "")
160
161     def test_update_dns_account(self):
162         update_dns_account_password(self.ldbs.sam, self.ldbs.secrets,
163             self.names)
164
165     def test_updateOEMInfo(self):
166         realm = self.lp.get("realm")
167         basedn = "DC=%s" % realm.replace(".", ", DC=")
168         oem = getOEMInfo(self.ldbs.sam, basedn)
169         updateOEMInfo(self.ldbs.sam, basedn)
170         oem2 = getOEMInfo(self.ldbs.sam, basedn)
171         self.assertNotEquals(str(oem), str(oem2))
172         self.assertTrue(re.match(".*upgrade to.*", str(oem2)))
173
174     def tearDown(self):
175         for name in ["ref.ldb", "secrets.ldb", "secrets.tdb", "secrets.tdb.bak", "sam.ldb"]:
176             path = os.path.join(self.tempdir, name)
177             if os.path.exists(path):
178                 os.unlink(path)
179         super(UpgradeProvisionWithLdbTestCase, self).tearDown()