cce6ea84d33bee3d4496616958b97866b1432014
[ira/wip.git] / source4 / scripting / python / samba / tests / samdb.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation. Tests for SamDB
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 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 from samba.auth import system_session
20 from samba.credentials import Credentials
21 import os
22 from samba.provision import setup_samdb, guess_names, setup_templatesdb, make_smbconf
23 from samba.samdb import SamDB
24 from samba.tests import cmdline_loadparm, TestCaseInTempDir
25 from samba.dcerpc import security
26 from unittest import TestCase
27 import uuid
28 from samba import param
29
30 class SamDBTestCase(TestCaseInTempDir):
31     def setUp(self):
32         super(SamDBTestCase, self).setUp()
33         invocationid = str(uuid.uuid4())
34         domaindn = "DC=COM,DC=EXAMPLE"
35         self.domaindn = domaindn
36         configdn = "CN=Configuration," + domaindn
37         schemadn = "CN=Schema," + configdn
38         domainguid = str(uuid.uuid4())
39         policyguid = str(uuid.uuid4())
40         setup_path = lambda x: os.path.join("setup", x)
41         creds = Credentials()
42         creds.set_anonymous()
43         domainsid = security.random_sid()
44         hostguid = str(uuid.uuid4())
45         path = os.path.join(self.tempdir, "samdb.ldb")
46         session_info = system_session()
47         
48         hostname="foo"
49         domain="EXAMPLE"
50         dnsdomain="example.com" 
51         serverrole="domain controller"
52
53         smbconf = os.path.join(self.tempdir, "smb.conf")
54         make_smbconf(smbconf, setup_path, hostname, domain, dnsdomain, serverrole, 
55                      self.tempdir)
56
57         lp = param.LoadParm()
58         lp.load(smbconf)
59
60         names = guess_names(lp=lp, hostname=hostname, 
61                             domain=domain, dnsdomain=dnsdomain, 
62                             serverrole=serverrole, 
63                             domaindn=self.domaindn, configdn=configdn, 
64                             schemadn=schemadn)
65         setup_templatesdb(os.path.join(self.tempdir, "templates.ldb"), 
66                           setup_path, session_info=session_info, 
67                           credentials=creds, lp=cmdline_loadparm)
68         self.samdb = setup_samdb(path, setup_path, session_info, creds, 
69                                  cmdline_loadparm, names, 
70                                  lambda x: None, domainsid, 
71                                  "# no aci", domainguid, 
72                                  policyguid, False, "secret", 
73                                  "secret", "secret", invocationid, 
74                                  "secret", "domain controller")
75
76     def tearDown(self):
77         for f in ['templates.ldb', 'schema.ldb', 'configuration.ldb', 
78                   'users.ldb', 'samdb.ldb', 'smb.conf']:
79             os.remove(os.path.join(self.tempdir, f))
80         super(SamDBTestCase, self).tearDown()
81
82     def test_add_foreign(self):
83         self.samdb.add_foreign(self.domaindn, "S-1-5-7", "Somedescription")
84