ac37a8146c41e8ae864d84f94cd06b4c9671e312
[vlendec/samba-autobuild/.git] / source4 / dsdb / tests / python / dsdb_schema_info.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Unix SMB/CIFS implementation.
5 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
6 #
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.
11 #
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.
16 #
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/>.
19 #
20
21 #
22 # Usage:
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"
26 #
27
28 import sys
29 import time
30 import random
31
32 sys.path.insert(0, "bin/python")
33 import samba.tests
34
35 from ldb import SCOPE_BASE, LdbError
36
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
41
42
43 class SchemaInfoTestCase(samba.tests.TestCase):
44
45     # static SamDB connection
46     sam_db = None
47
48     def setUp(self):
49         super(SchemaInfoTestCase, self).setUp()
50
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)
55
56         # fetch rootDSE
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])
62
63         # get DC invocation_id
64         self.invocation_id = GUID(self.sam_db.get_invocation_id())
65
66     def tearDown(self):
67         super(SchemaInfoTestCase, self).tearDown()
68
69     def _getSchemaInfo(self):
70         try:
71             schema_info_data = self.sam_db.searchone(attribute="schemaInfo",
72                                                      basedn=self.schema_dn,
73                                                      expression="(objectClass=*)",
74                                                      scope=SCOPE_BASE)
75             self.assertEqual(len(schema_info_data), 21)
76             schema_info = ndr_unpack(schemaInfoBlob, schema_info_data)
77             self.assertEqual(schema_info.marker, 0xFF)
78         except KeyError:
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
84         return schema_info
85
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)
90
91     def _ldap_schemaUpdateNow(self):
92         ldif = """
93 dn:
94 changetype: modify
95 add: schemaUpdateNow
96 schemaUpdateNow: 1
97 """
98         self.sam_db.modify_ldif(ldif)
99
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)
105
106     def _make_attr_ldif(self, attr_name, attr_dn, sub_oid):
107         ldif = """
108 dn: """ + attr_dn + """
109 objectClass: top
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
116 omSyntax: 64
117 instanceType: 4
118 isSingleValued: TRUE
119 systemOnly: FALSE
120 """
121         return ldif
122
123     def test_AddModifyAttribute(self):
124         # get initial schemaInfo
125         schi_before = self._getSchemaInfo()
126
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)
130
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)
137
138         # rename the Attribute
139         attr_dn_new = attr_dn.replace(attr_name, attr_name + "-NEW")
140         try:
141             self.sam_db.rename(attr_dn, attr_dn_new)
142         except LdbError as e:
143             (num, _) = e.args
144             self.fail("failed to change CN for %s: %s" % (attr_name, _))
145
146         # compare resulting schemaInfo
147         schi_after = self._getSchemaInfo()
148         self._checkSchemaInfo(schi_before, schi_after)
149         pass
150
151     def _make_class_ldif(self, class_name, class_dn, sub_oid):
152         ldif = """
153 dn: """ + class_dn + """
154 objectClass: top
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)) + """
160 instanceType: 4
161 objectClassCategory: 1
162 subClassOf: organizationalPerson
163 rDNAttID: cn
164 systemMustContain: cn
165 systemOnly: FALSE
166 """
167         return ldif
168
169     def test_AddModifyClass(self):
170         # get initial schemaInfo
171         schi_before = self._getSchemaInfo()
172
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)
176
177         # add the new Class
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)
183
184         # rename the Class
185         class_dn_new = class_dn.replace(class_name, class_name + "-NEW")
186         try:
187             self.sam_db.rename(class_dn, class_dn_new)
188         except LdbError as e1:
189             (num, _) = e1.args
190             self.fail("failed to change CN for %s: %s" % (class_name, _))
191
192         # compare resulting schemaInfo
193         schi_after = self._getSchemaInfo()
194         self._checkSchemaInfo(schi_before, schi_after)