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
36 class SchemaAttributesTestCase(samba.tests.TestCase):
39 super(SchemaAttributesTestCase, self).setUp()
41 self.lp = samba.tests.env_loadparm()
42 self.samdb = samba.tests.connect_samdb(self.lp.samdb_url())
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])
52 super(SchemaAttributesTestCase, self).tearDown()
54 def _ldap_schemaUpdateNow(self):
61 self.samdb.modify_ldif(ldif)
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)
69 def _make_attr_ldif(self, attr_name, attr_dn, sub_oid, extra=None):
71 dn: """ + attr_dn + """
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
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)
96 # add the new attribute
97 self.samdb.add_ldif(ldif)
98 self._ldap_schemaUpdateNow()
102 attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
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")
110 idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
112 self.assertIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
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)
120 # add the new attribute
121 self.samdb.add_ldif(ldif)
122 self._ldap_schemaUpdateNow()
126 attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
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")
134 idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
136 self.assertNotIn(attr_ldap_name, [str(x) for x in idx_res[0]["@IDXATTR"]])
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)
145 # add the new attribute
146 self.samdb.add_ldif(ldif)
147 self._ldap_schemaUpdateNow()
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)
154 # add the new attribute
155 self.samdb.add_ldif(ldif)
156 self._ldap_schemaUpdateNow()
160 attr_res = self.samdb.search(base="@ATTRIBUTES", scope=ldb.SCOPE_BASE)
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")
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")
172 idx_res = self.samdb.search(base="@INDEXLIST", scope=ldb.SCOPE_BASE)
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"]])
177 def test_modify_at_attributes(self):
178 m = {"dn": "@ATTRIBUTES",
179 "@TEST_EXTRA": ["HIDDEN"]
182 msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)
183 self.samdb.modify(msg)
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")
194 samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())
196 # We now only update the @ATTRIBUTES when a transaction happens
197 # rather than making a read of the DB do writes.
199 # This avoids locking issues and is more expected
201 samdb2.transaction_start()
202 samdb2.transaction_commit()
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])
212 def test_modify_at_indexlist(self):
213 m = {"dn": "@INDEXLIST",
217 msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_ADD)
218 self.samdb.modify(msg)
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")
229 samdb2 = samba.tests.connect_samdb(self.lp.samdb_url())
231 # We now only update the @INDEXLIST when a transaction happens
232 # rather than making a read of the DB do writes.
234 # This avoids locking issues and is more expected
236 samdb2.transaction_start()
237 samdb2.transaction_commit()
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])
246 def test_modify_fail_of_at_indexlist(self):
247 m = {"dn": "@INDEXLIST",
248 "@TEST_NOT_EXTRA": ["1"]
251 msg = ldb.Message.from_dict(self.samdb, m, ldb.FLAG_MOD_DELETE)
253 self.samdb.modify(msg)
254 self.fail("modify of @INDEXLIST with a failed constraint should fail")
255 except LdbError as err:
257 self.assertEquals(enum, ldb.ERR_NO_SUCH_ATTRIBUTE)