Move python modules from source4/scripting/python/ to python/.
[amitay/samba.git] / python / samba / tests / samdb.py
1 # Unix SMB/CIFS implementation. Tests for SamDB
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 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.samdb."""
19
20 import logging
21 import os
22 import uuid
23
24 from samba.auth import system_session
25 from samba.provision import (setup_samdb, guess_names, make_smbconf,
26     provision_paths_from_lp)
27 from samba.provision import DEFAULT_POLICY_GUID, DEFAULT_DC_POLICY_GUID
28 from samba.provision.backend import ProvisionBackend
29 from samba.tests import TestCaseInTempDir
30 from samba.dcerpc import security
31 from samba.schema import Schema
32 from samba import param
33
34
35 class SamDBTestCase(TestCaseInTempDir):
36     """Base-class for tests with a Sam Database.
37
38     This is used by the Samba SamDB-tests, but e.g. also by the OpenChange
39     provisioning tests (which need a Sam).
40     """
41
42     def setUp(self):
43         super(SamDBTestCase, self).setUp()
44         invocationid = str(uuid.uuid4())
45         domaindn = "DC=COM,DC=EXAMPLE"
46         self.domaindn = domaindn
47         configdn = "CN=Configuration," + domaindn
48         schemadn = "CN=Schema," + configdn
49         domainguid = str(uuid.uuid4())
50         policyguid = DEFAULT_POLICY_GUID
51         domainsid = security.random_sid()
52         path = os.path.join(self.tempdir, "samdb.ldb")
53         session_info = system_session()
54
55         hostname="foo"
56         domain="EXAMPLE"
57         dnsdomain="example.com"
58         serverrole="domain controller"
59         policyguid_dc = DEFAULT_DC_POLICY_GUID
60
61         smbconf = os.path.join(self.tempdir, "smb.conf")
62         make_smbconf(smbconf, hostname, domain, dnsdomain,
63                      self.tempdir, serverrole=serverrole)
64
65         self.lp = param.LoadParm()
66         self.lp.load(smbconf)
67
68         names = guess_names(lp=self.lp, hostname=hostname,
69                             domain=domain, dnsdomain=dnsdomain,
70                             serverrole=serverrole,
71                             domaindn=self.domaindn, configdn=configdn,
72                             schemadn=schemadn)
73
74         paths = provision_paths_from_lp(self.lp, names.dnsdomain)
75
76         logger = logging.getLogger("provision")
77
78         provision_backend = ProvisionBackend("ldb", paths=paths,
79                 lp=self.lp, credentials=None,
80                 names=names, logger=logger)
81
82         schema = Schema(domainsid, invocationid=invocationid,
83                 schemadn=names.schemadn, serverdn=names.serverdn,
84                 am_rodc=False)
85
86         self.samdb = setup_samdb(path, session_info,
87                 provision_backend, self.lp, names, logger,
88                 domainsid, domainguid, policyguid, policyguid_dc, False,
89                 "secret", "secret", "secret", invocationid, "secret",
90                 None, "domain controller", schema=schema)
91
92     def tearDown(self):
93         for f in ['schema.ldb', 'configuration.ldb',
94                   'users.ldb', 'samdb.ldb', 'smb.conf']:
95             os.remove(os.path.join(self.tempdir, f))
96         super(SamDBTestCase, self).tearDown()