1 # -*- coding: utf-8 -*-
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
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.
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.
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/>.
22 # export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
23 # PYTHONPATH="$PYTHONPATH:$samba4srcdir/dsdb/tests/python" $SUBUNITRUN dsdb_schema_attributes
32 from ldb import SCOPE_BASE, LdbError
37 class SchemaAttributesTestCase(samba.tests.TestCase):
40 super(SchemaAttributesTestCase, self).setUp()
42 self.lp = samba.tests.env_loadparm()
43 self.samdb = samba.tests.connect_samdb(self.lp.samdb_url())
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])
53 super(SchemaAttributesTestCase, self).tearDown()
55 def _ldap_schemaUpdateNow(self):
62 self.samdb.modify_ldif(ldif)
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)
70 def _make_attr_ldif(self, attr_name, attr_dn, sub_oid, extra=None):
72 dn: """ + attr_dn + """
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
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)
97 # add the new attribute
98 self.samdb.add_ldif(ldif)
99 self._ldap_schemaUpdateNow()
103 attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
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")
111 idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
113 self.assertIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
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)
121 # add the new attribute
122 self.samdb.add_ldif(ldif)
123 self._ldap_schemaUpdateNow()
127 attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
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")
135 idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
137 self.assertNotIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
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)
146 # add the new attribute
147 self.samdb.add_ldif(ldif)
148 self._ldap_schemaUpdateNow()
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)
155 # add the new attribute
156 self.samdb.add_ldif(ldif)
157 self._ldap_schemaUpdateNow()
161 attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
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")
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")
173 idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
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"]])
178 def test_modify_at_attributes(self):
179 m = {"dn": "@ATTRIBUTES",
180 "@TEST_EXTRA": ["HIDDEN"]
183 msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)
184 self.samdb.modify(msg)
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")
195 samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())
197 # We now only update the @ATTRIBUTES when a transaction happens
198 # rather than making a read of the DB do writes.
200 # This avoids locking issues and is more expected
202 samdb2.transaction_start()
203 samdb2.transaction_commit()
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])
213 def test_modify_at_indexlist(self):
214 m = {"dn": "@INDEXLIST",
218 msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)
219 self.samdb.modify(msg)
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")
230 samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())
232 # We now only update the @INDEXLIST when a transaction happens
233 # rather than making a read of the DB do writes.
235 # This avoids locking issues and is more expected
237 samdb2.transaction_start()
238 samdb2.transaction_commit()
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])
247 def test_modify_fail_of_at_indexlist(self):
248 m = {"dn": "@INDEXLIST",
249 "@TEST_NOT_EXTRA": ["1"]
252 msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_DELETE)
254 self.samdb.modify(msg)
255 self.fail("modify of @INDEXLIST with a failed constraint should fail")
256 except LdbError as err:
258 self.assertEquals(enum, ldb.ERR_NO_SUCH_ATTRIBUTE)