2 # -*- coding: utf-8 -*-
4 # Unix SMB/CIFS implementation.
5 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # export DC_SERVER=target_dc_or_local_samdb_url
24 # export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
25 # PYTHONPATH="$PYTHONPATH:$samba4srcdir/lib/ldb/tests/python" $SUBUNITRUN dsdb_schema_info -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
32 sys.path.insert(0, "bin/python")
35 from ldb import SCOPE_BASE, LdbError
37 import samba.dcerpc.drsuapi
38 from samba.dcerpc.drsblobs import schemaInfoBlob
39 from samba.ndr import ndr_unpack
40 from samba.dcerpc.misc import GUID
43 class SchemaInfoTestCase(samba.tests.TestCase):
45 # static SamDB connection
49 super(SchemaInfoTestCase, self).setUp()
51 # connect SamDB if we haven't yet
52 if self.sam_db is None:
53 ldb_url = "ldap://%s" % samba.tests.env_get_var_value("DC_SERVER")
54 SchemaInfoTestCase.sam_db = samba.tests.connect_samdb(ldb_url)
57 res = self.sam_db.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
58 self.assertEquals(len(res), 1)
59 self.schema_dn = res[0]["schemaNamingContext"][0]
60 self.base_dn = res[0]["defaultNamingContext"][0]
61 self.forest_level = int(res[0]["forestFunctionality"][0])
63 # get DC invocation_id
64 self.invocation_id = GUID(self.sam_db.get_invocation_id())
67 super(SchemaInfoTestCase, self).tearDown()
69 def _getSchemaInfo(self):
71 schema_info_data = self.sam_db.searchone(attribute="schemaInfo",
72 basedn=self.schema_dn,
73 expression="(objectClass=*)",
75 self.assertEqual(len(schema_info_data), 21)
76 schema_info = ndr_unpack(schemaInfoBlob, schema_info_data)
77 self.assertEqual(schema_info.marker, 0xFF)
79 # create default schemaInfo if
80 # attribute value is not created yet
81 schema_info = schemaInfoBlob()
82 schema_info.revision = 0
83 schema_info.invocation_id = self.invocation_id
86 def _checkSchemaInfo(self, schi_before, schi_after):
87 self.assertEqual(schi_before.revision + 1, schi_after.revision)
88 self.assertEqual(schi_before.invocation_id, schi_after.invocation_id)
89 self.assertEqual(schi_after.invocation_id, self.invocation_id)
91 def _ldap_schemaUpdateNow(self):
98 self.sam_db.modify_ldif(ldif)
100 def _make_obj_names(self, prefix):
101 obj_name = prefix + time.strftime("%s", time.gmtime())
102 obj_ldap_name = obj_name.replace("-", "")
103 obj_dn = "CN=%s,%s" % (obj_name, self.schema_dn)
104 return (obj_name, obj_ldap_name, obj_dn)
106 def _make_attr_ldif(self, attr_name, attr_dn, sub_oid):
108 dn: """ + attr_dn + """
110 objectClass: attributeSchema
111 adminDescription: """ + attr_name + """
112 adminDisplayName: """ + attr_name + """
113 cn: """ + attr_name + """
114 attributeId: 1.3.6.1.4.1.7165.4.6.1.7.%d.""" % sub_oid + str(random.randint(1, 100000)) + """
115 attributeSyntax: 2.5.5.12
123 def test_AddModifyAttribute(self):
124 # get initial schemaInfo
125 schi_before = self._getSchemaInfo()
127 # create names for an attribute to add
128 (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaInfo-Attr-")
129 ldif = self._make_attr_ldif(attr_name, attr_dn, 1)
131 # add the new attribute
132 self.sam_db.add_ldif(ldif)
133 self._ldap_schemaUpdateNow()
134 # compare resulting schemaInfo
135 schi_after = self._getSchemaInfo()
136 self._checkSchemaInfo(schi_before, schi_after)
138 # rename the Attribute
139 attr_dn_new = attr_dn.replace(attr_name, attr_name + "-NEW")
141 self.sam_db.rename(attr_dn, attr_dn_new)
142 except LdbError as e:
144 self.fail("failed to change CN for %s: %s" % (attr_name, _))
146 # compare resulting schemaInfo
147 schi_after = self._getSchemaInfo()
148 self._checkSchemaInfo(schi_before, schi_after)
151 def _make_class_ldif(self, class_name, class_dn, sub_oid):
153 dn: """ + class_dn + """
155 objectClass: classSchema
156 adminDescription: """ + class_name + """
157 adminDisplayName: """ + class_name + """
158 cn: """ + class_name + """
159 governsId: 1.3.6.1.4.1.7165.4.6.2.7.%d.""" % sub_oid + str(random.randint(1, 100000)) + """
161 objectClassCategory: 1
162 subClassOf: organizationalPerson
164 systemMustContain: cn
169 def test_AddModifyClass(self):
170 # get initial schemaInfo
171 schi_before = self._getSchemaInfo()
173 # create names for a Class to add
174 (class_name, class_ldap_name, class_dn) = self._make_obj_names("schemaInfo-Class-")
175 ldif = self._make_class_ldif(class_name, class_dn, 1)
178 self.sam_db.add_ldif(ldif)
179 self._ldap_schemaUpdateNow()
180 # compare resulting schemaInfo
181 schi_after = self._getSchemaInfo()
182 self._checkSchemaInfo(schi_before, schi_after)
185 class_dn_new = class_dn.replace(class_name, class_name + "-NEW")
187 self.sam_db.rename(class_dn, class_dn_new)
188 except LdbError as e1:
190 self.fail("failed to change CN for %s: %s" % (class_name, _))
192 # compare resulting schemaInfo
193 schi_after = self._getSchemaInfo()
194 self._checkSchemaInfo(schi_before, schi_after)