PEP8: fix E302: expected 2 blank lines, found 1
[bbaumbach/samba-autobuild/.git] / python / samba / tests / dsdb_schema_attributes.py
1 # -*- coding: utf-8 -*-
2 #
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
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 #
21 # Usage:
22 #  export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
23 #  PYTHONPATH="$PYTHONPATH:$samba4srcdir/dsdb/tests/python" $SUBUNITRUN dsdb_schema_attributes
24 #
25
26 import sys
27 import time
28 import random
29
30 import samba.tests
31 import ldb
32 from ldb import SCOPE_BASE, LdbError
33
34 import samba.tests
35
36
37 class SchemaAttributesTestCase(samba.tests.TestCase):
38
39     def setUp(self):
40         super(SchemaAttributesTestCase, self).setUp()
41
42         self.lp = samba.tests.env_loadparm()
43         self.samdb = samba.tests.connect_samdb(self.lp.samdb_url())
44
45         # fetch rootDSE
46         res = self.samdb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
47         self.assertEquals(len(res), 1)
48         self.schema_dn = res[0]["schemaNamingContext"][0]
49         self.base_dn = res[0]["defaultNamingContext"][0]
50         self.forest_level = int(res[0]["forestFunctionality"][0])
51
52     def tearDown(self):
53         super(SchemaAttributesTestCase, self).tearDown()
54
55     def _ldap_schemaUpdateNow(self):
56         ldif = """
57 dn:
58 changetype: modify
59 add: schemaUpdateNow
60 schemaUpdateNow: 1
61 """
62         self.samdb.modify_ldif(ldif)
63
64     def _make_obj_names(self, prefix):
65         obj_name = prefix + time.strftime("%s", time.gmtime())
66         obj_ldap_name = obj_name.replace("-", "")
67         obj_dn = "CN=%s,%s" % (obj_name, self.schema_dn)
68         return (obj_name, obj_ldap_name, obj_dn)
69
70     def _make_attr_ldif(self, attr_name, attr_dn, sub_oid, extra=None):
71         ldif = """
72 dn: """ + attr_dn + """
73 objectClass: top
74 objectClass: attributeSchema
75 adminDescription: """ + attr_name + """
76 adminDisplayName: """ + attr_name + """
77 cn: """ + attr_name + """
78 attributeId: 1.3.6.1.4.1.7165.4.6.1.8.%d.""" % sub_oid + str(random.randint(1, 100000)) + """
79 attributeSyntax: 2.5.5.12
80 omSyntax: 64
81 instanceType: 4
82 isSingleValued: TRUE
83 systemOnly: FALSE
84 """
85
86         if extra is not None:
87             ldif += extra + "\n"
88
89         return ldif
90
91     def test_AddIndexedAttribute(self):
92         # create names for an attribute to add
93         (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaAttributes-Attr-")
94         ldif = self._make_attr_ldif(attr_name, attr_dn, 1,
95                                     "searchFlags: %d" % samba.dsdb.SEARCH_FLAG_ATTINDEX)
96
97         # add the new attribute
98         self.samdb.add_ldif(ldif)
99         self._ldap_schemaUpdateNow()
100
101         # Check @ATTRIBUTES
102
103         attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
104
105         self.assertIn(attr_ldap_name, attr_res[0])
106         self.assertEquals(len(attr_res[0][attr_ldap_name]), 1)
107         self.assertEquals(attr_res[0][attr_ldap_name][0], "CASE_INSENSITIVE")
108
109         # Check @INDEXLIST
110
111         idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
112
113         self.assertIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
114
115
116     def test_AddUnIndexedAttribute(self):
117         # create names for an attribute to add
118         (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaAttributes-Attr-")
119         ldif = self._make_attr_ldif(attr_name, attr_dn, 2)
120
121         # add the new attribute
122         self.samdb.add_ldif(ldif)
123         self._ldap_schemaUpdateNow()
124
125         # Check @ATTRIBUTES
126
127         attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
128
129         self.assertIn(attr_ldap_name, attr_res[0])
130         self.assertEquals(len(attr_res[0][attr_ldap_name]), 1)
131         self.assertEquals(attr_res[0][attr_ldap_name][0], "CASE_INSENSITIVE")
132
133         # Check @INDEXLIST
134
135         idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
136
137         self.assertNotIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
138
139
140     def test_AddTwoIndexedAttributes(self):
141         # create names for an attribute to add
142         (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaAttributes-Attr-")
143         ldif = self._make_attr_ldif(attr_name, attr_dn, 3,
144                                     "searchFlags: %d" % samba.dsdb.SEARCH_FLAG_ATTINDEX)
145
146         # add the new attribute
147         self.samdb.add_ldif(ldif)
148         self._ldap_schemaUpdateNow()
149
150         # create names for an attribute to add
151         (attr_name2, attr_ldap_name2, attr_dn2) = self._make_obj_names("schemaAttributes-Attr-")
152         ldif = self._make_attr_ldif(attr_name2, attr_dn2, 4,
153                                     "searchFlags: %d" % samba.dsdb.SEARCH_FLAG_ATTINDEX)
154
155         # add the new attribute
156         self.samdb.add_ldif(ldif)
157         self._ldap_schemaUpdateNow()
158
159         # Check @ATTRIBUTES
160
161         attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
162
163         self.assertIn(attr_ldap_name, attr_res[0])
164         self.assertEquals(len(attr_res[0][attr_ldap_name]), 1)
165         self.assertEquals(attr_res[0][attr_ldap_name][0], "CASE_INSENSITIVE")
166
167         self.assertIn(attr_ldap_name2, attr_res[0])
168         self.assertEquals(len(attr_res[0][attr_ldap_name2]), 1)
169         self.assertEquals(attr_res[0][attr_ldap_name2][0], "CASE_INSENSITIVE")
170
171         # Check @INDEXLIST
172
173         idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
174
175         self.assertIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
176         self.assertIn(attr_ldap_name2, [str(x) for x in idx_res[0]["@IDXATTR"]])
177
178     def test_modify_at_attributes(self):
179         m = {"dn": "@ATTRIBUTES",
180              "@TEST_EXTRA": ["HIDDEN"]
181              }
182
183         msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)
184         self.samdb.modify(msg)
185
186         res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE,
187                                 attrs=["@TEST_EXTRA"])
188         self.assertEquals(len(res), 1)
189         self.assertEquals(str(res[0].dn), "@ATTRIBUTES")
190         self.assertEquals(len(res[0]), 1)
191         self.assertTrue("@TEST_EXTRA" in res[0])
192         self.assertEquals(len(res[0]["@TEST_EXTRA"]), 1)
193         self.assertEquals(res[0]["@TEST_EXTRA"][0], "HIDDEN")
194
195         samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())
196
197         # We now only update the @ATTRIBUTES when a transaction happens
198         # rather than making a read of the DB do writes.
199         #
200         # This avoids locking issues and is more expected
201
202         samdb2.transaction_start()
203         samdb2.transaction_commit()
204
205         res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE,
206                                 attrs=["@TEST_EXTRA"])
207         self.assertEquals(len(res), 1)
208         self.assertEquals(str(res[0].dn), "@ATTRIBUTES")
209         self.assertEquals(len(res[0]), 0)
210         self.assertFalse("@TEST_EXTRA" in res[0])
211
212
213     def test_modify_at_indexlist(self):
214         m = {"dn": "@INDEXLIST",
215              "@TEST_EXTRA": ["1"]
216              }
217
218         msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)
219         self.samdb.modify(msg)
220
221         res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE,
222                                 attrs=["@TEST_EXTRA"])
223         self.assertEquals(len(res), 1)
224         self.assertEquals(str(res[0].dn), "@INDEXLIST")
225         self.assertEquals(len(res[0]), 1)
226         self.assertTrue("@TEST_EXTRA" in res[0])
227         self.assertEquals(len(res[0]["@TEST_EXTRA"]), 1)
228         self.assertEquals(res[0]["@TEST_EXTRA"][0], "1")
229
230         samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())
231
232         # We now only update the @INDEXLIST when a transaction happens
233         # rather than making a read of the DB do writes.
234         #
235         # This avoids locking issues and is more expected
236
237         samdb2.transaction_start()
238         samdb2.transaction_commit()
239
240         res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE,
241                                 attrs=["@TEST_EXTRA"])
242         self.assertEquals(len(res), 1)
243         self.assertEquals(str(res[0].dn), "@INDEXLIST")
244         self.assertEquals(len(res[0]), 0)
245         self.assertFalse("@TEST_EXTRA" in res[0])
246
247     def test_modify_fail_of_at_indexlist(self):
248         m = {"dn": "@INDEXLIST",
249              "@TEST_NOT_EXTRA": ["1"]
250              }
251
252         msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_DELETE)
253         try:
254             self.samdb.modify(msg)
255             self.fail("modify of @INDEXLIST with a failed constraint should fail")
256         except LdbError as err:
257             enum = err.args[0]
258             self.assertEquals(enum, ldb.ERR_NO_SUCH_ATTRIBUTE)