s4-unittests: replace assertEquals(res, []) by assertEquals(len(res), 0)
[metze/samba/wip.git] / source4 / scripting / python / samba / tests / upgradeprovision.py
1 #!/usr/bin/env 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 """Tests for samba.upgradeprovision."""
21
22 import os
23 from samba.upgradehelpers import  (usn_in_range, dn_sort,
24                                   get_diff_sddls, update_secrets,
25                                   construct_existor_expr)
26
27 from samba.tests.provision import create_dummy_secretsdb
28 from samba.tests import TestCaseInTempDir
29 from samba import Ldb
30 from ldb import SCOPE_SUBTREE
31 import samba.tests
32
33 def dummymessage(a=None, b=None):
34     pass
35
36
37 class UpgradeProvisionTestCase(TestCaseInTempDir):
38     """Some simple tests for individual functions in the provisioning code.
39     """
40     def test_usn_in_range(self):
41         range = [5, 25, 35, 55]
42
43         vals = [3, 26, 56]
44
45         for v in vals:
46             self.assertFalse(usn_in_range(v, range))
47
48         vals = [5, 20, 25, 35, 36]
49
50         for v in vals:
51             self.assertTrue(usn_in_range(v, range))
52
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.assertEquals(dn_sort("dc=tata,dc=toto", "dc=toto"), 1)
58         self.assertEquals(dn_sort("dc=zata", "dc=tata"), 1)
59         self.assertEquals(dn_sort("dc=toto,dc=tata",
60                                     "cn=foo,dc=toto,dc=tata"), -1)
61         self.assertEquals(dn_sort("cn=bar, dc=toto,dc=tata",
62                                     "cn=foo, dc=toto,dc=tata"), -1)
63
64     def test_get_diff_sddl(self):
65         sddl = "O:SAG:DUD:AI(A;CIID;RPWPCRCCLCLORCWOWDSW;;;SA)\
66 (A;CIID;RP LCLORC;;;AU)(A;CIID;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)S:AI(AU;CIIDSA;WP;;;WD)"
67         sddl1 = "O:SAG:DUD:AI(A;CIID;RPWPCRCCLCLORCWOWDSW;;;SA)\
68 (A;CIID;RP LCLORC;;;AU)(A;CIID;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)S:AI(AU;CIIDSA;WP;;;WD)"
69         sddl2 = "O:BAG:DUD:AI(A;CIID;RPWPCRCCLCLORCWOWDSW;;;SA)\
70 (A;CIID;RP LCLORC;;;AU)(A;CIID;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)S:AI(AU;CIIDSA;WP;;;WD)"
71         sddl3 = "O:SAG:BAD:AI(A;CIID;RPWPCRCCLCLORCWOWDSW;;;SA)\
72 (A;CIID;RP LCLORC;;;AU)(A;CIID;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)S:AI(AU;CIIDSA;WP;;;WD)"
73         sddl4 = "O:SAG:DUD:AI(A;CIID;RPWPCRCCLCLORCWOWDSW;;;BA)\
74 (A;CIID;RP LCLORC;;;AU)(A;CIID;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)S:AI(AU;CIIDSA;WP;;;WD)"
75         sddl5 = "O:SAG:DUD:AI(A;CIID;RPWPCRCCLCLORCWOWDSW;;;SA)\
76 (A;CIID;RP LCLORC;;;AU)(A;CIID;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
77
78         self.assertEquals(get_diff_sddls(sddl, sddl1), "")
79         txt = get_diff_sddls(sddl, sddl2)
80         self.assertEquals(txt, "\tOwner mismatch: SA (in ref) BA(in current)\n")
81         txt = get_diff_sddls(sddl, sddl3)
82         self.assertEquals(txt, "\tGroup mismatch: DU (in ref) BA(in current)\n")
83         txt = get_diff_sddls(sddl, sddl4)
84         txtmsg = "\tPart dacl is different between reference and current here\
85  is the detail:\n\t\t(A;CIID;RPWPCRCCLCLORCWOWDSW;;;BA) ACE is not present in\
86  the reference\n\t\t(A;CIID;RPWPCRCCLCLORCWOWDSW;;;SA) ACE is not present in\
87  the current\n"
88         self.assertEquals(txt, txtmsg)
89         txt = get_diff_sddls(sddl, sddl5)
90         self.assertEquals(txt, "\tCurrent ACL hasn't a sacl part\n")
91
92     def test_construct_existor_expr(self):
93         res = construct_existor_expr([])
94         self.assertEquals(res, "")
95
96         res = construct_existor_expr(["foo"])
97         self.assertEquals(res, "(|(foo=*))")
98
99         res = construct_existor_expr(["foo", "bar"])
100         self.assertEquals(res, "(|(foo=*)(bar=*))")
101
102
103 class UpdateSecretsTests(samba.tests.TestCaseInTempDir):
104
105     def setUp(self):
106         super(UpdateSecretsTests, self).setUp()
107         self.referencedb = create_dummy_secretsdb(
108             os.path.join(self.tempdir, "ref.ldb"))
109
110     def _getEmptyDb(self):
111         return Ldb(os.path.join(self.tempdir, "secrets.ldb"))
112
113     def _getCurrentFormatDb(self):
114         return create_dummy_secretsdb(
115             os.path.join(self.tempdir, "secrets.ldb"))
116
117     def test_trivial(self):
118         # Test that updating an already up-to-date secretsdb works fine
119         self.secretsdb = self._getCurrentFormatDb()
120         self.assertEquals(None,
121             update_secrets(self.referencedb, self.secretsdb, dummymessage))
122
123     def test_update_modules(self):
124         empty_db = self._getEmptyDb()
125         update_secrets(self.referencedb, empty_db, dummymessage)
126         newmodules = empty_db.search(
127             expression="dn=@MODULES", base="", scope=SCOPE_SUBTREE)
128         refmodules = self.referencedb.search(
129             expression="dn=@MODULES", base="", scope=SCOPE_SUBTREE)
130         self.assertEquals(newmodules.msgs, refmodules.msgs)
131
132     def tearDown(self):
133         for name in ["ref.ldb", "secrets.ldb"]:
134             path = os.path.join(self.tempdir, name)
135             if os.path.exists(path):
136                 os.unlink(path)
137         super(UpdateSecretsTests, self).tearDown()
138
139