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