1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
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.
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.
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/>.
18 """Tests for samba.upgradeprovision."""
21 from samba
.upgradehelpers
import (usn_in_range
, dn_sort
,
23 construct_existor_expr
)
24 from samba
.descriptor
import get_diff_sds
25 from samba
.tests
.provision
import create_dummy_secretsdb
26 from samba
.tests
import TestCaseInTempDir
28 from ldb
import SCOPE_BASE
30 from samba
.dcerpc
import security
33 def dummymessage(a
=None, b
=None):
37 class UpgradeProvisionTestCase(TestCaseInTempDir
):
38 """Some simple tests for individual functions in the provisioning code.
40 def test_usn_in_range(self
):
41 range = [5, 25, 35, 55]
46 self
.assertFalse(usn_in_range(v
, range))
48 vals
= [5, 20, 25, 35, 36]
51 self
.assertTrue(usn_in_range(v
, range))
53 def test_dn_sort(self
):
54 # higher level comes after lower even if lexicographicaly closer
55 # ie dc=tata,dc=toto (2 levels), comes after dc=toto
56 # even if dc=toto is lexicographicaly after dc=tata, dc=toto
57 self
.assertEqual(dn_sort("dc=tata,dc=toto", "dc=toto"), 1)
58 self
.assertEqual(dn_sort("dc=zata", "dc=tata"), 1)
59 self
.assertEqual(dn_sort("dc=toto,dc=tata",
60 "cn=foo,dc=toto,dc=tata"), -1)
61 self
.assertEqual(dn_sort("cn=bar, dc=toto,dc=tata",
62 "cn=foo, dc=toto,dc=tata"), -1)
64 def test_get_diff_sds(self
):
65 domsid
= security
.dom_sid('S-1-5-21')
67 sddl
= "O:SAG:DUD:AI(A;CI;CCLCSWRPWPLOCRRCWDWO;;;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ddl(sddl
, domsid
),
85 security
.descriptor
.from_sddl(sddl1
, domsid
),
87 txt
= get_diff_sds(security
.descriptor
.from_sddl(sddl
, domsid
),
88 security
.descriptor
.from_sddl(sddl2
, domsid
),
90 self
.assertEqual(txt
, "\tOwner mismatch: SA (in ref) BA(in current)\n")
91 txt
= get_diff_sds(security
.descriptor
.from_sddl(sddl
, domsid
),
92 security
.descriptor
.from_sddl(sddl3
, domsid
),
94 self
.assertEqual(txt
, "\tGroup mismatch: DU (in ref) BA(in current)\n")
95 txt
= get_diff_sds(security
.descriptor
.from_sddl(sddl
, domsid
),
96 security
.descriptor
.from_sddl(sddl4
, domsid
),
98 txtmsg
= "\tPart dacl is different between reference and current here\
99 is the detail:\n\t\t(A;CI;CCLCSWRPWPLOCRRCWDWO;;;BA) ACE is not present in\
100 the reference\n\t\t(A;CI;CCLCSWRPWPLOCRRCWDWO;;;SA) ACE is not present in\
102 self
.assertEqual(txt
, txtmsg
)
104 txt
= get_diff_sds(security
.descriptor
.from_sddl(sddl
, domsid
),
105 security
.descriptor
.from_sddl(sddl5
, domsid
),
107 self
.assertEqual(txt
, "\tCurrent ACL hasn't a sacl part\n")
108 self
.assertEqual(get_diff_sds(security
.descriptor
.from_sddl(sddl
, domsid
),
109 security
.descriptor
.from_sddl(sddl6
, domsid
),
112 def test_construct_existor_expr(self
):
113 res
= construct_existor_expr([])
114 self
.assertEqual(res
, "")
116 res
= construct_existor_expr(["foo"])
117 self
.assertEqual(res
, "(|(foo=*))")
119 res
= construct_existor_expr(["foo", "bar"])
120 self
.assertEqual(res
, "(|(foo=*)(bar=*))")
123 class UpdateSecretsTests(samba
.tests
.TestCaseInTempDir
):
127 self
.referencedb
= create_dummy_secretsdb(
128 os
.path
.join(self
.tempdir
, "ref.ldb"))
130 def _getEmptyDb(self
):
131 return Ldb(os
.path
.join(self
.tempdir
, "secrets.ldb"))
133 def _getCurrentFormatDb(self
):
134 return create_dummy_secretsdb(
135 os
.path
.join(self
.tempdir
, "secrets.ldb"))
137 def test_trivial(self
):
138 # Test that updating an already up-to-date secretsdb works fine
139 self
.secretsdb
= self
._getCurrentFormatDb
()
140 self
.assertEqual(None,
141 update_secrets(self
.referencedb
, self
.secretsdb
, dummymessage
))
143 def test_update_modules(self
):
144 empty_db
= self
._getEmptyDb
()
145 update_secrets(self
.referencedb
, empty_db
, dummymessage
)
146 newmodules
= empty_db
.search(base
="@MODULES", scope
=SCOPE_BASE
)
147 refmodules
= self
.referencedb
.search(base
="@MODULES", scope
=SCOPE_BASE
)
148 self
.assertEqual(newmodules
.msgs
, refmodules
.msgs
)
151 for name
in ["ref.ldb", "secrets.ldb", "secrets.tdb", "secrets.tdb.bak", "secrets.ntdb"]:
152 path
= os
.path
.join(self
.tempdir
, name
)
153 if os
.path
.exists(path
):