s4/dsdb-test: fix usage comment
[samba.git] / source4 / lib / ldb / tests / python / dsdb_schema_info.py
1 #!/usr/bin/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 import os
32
33 sys.path.append("bin/python")
34
35 from samba.auth import system_session
36 from ldb import SCOPE_BASE, LdbError
37 from samba.samdb import SamDB
38
39 import samba.tests
40 import samba.dcerpc.drsuapi
41 from samba.dcerpc.drsblobs import schemaInfoBlob
42 from samba.ndr import ndr_unpack
43 from samba.dcerpc.misc import GUID
44
45
46 class SchemaInfoTestCase(samba.tests.TestCase):
47
48     def setUp(self):
49         super(SchemaInfoTestCase, self).setUp()
50
51         # fetch rootDSE
52         self.ldb = ldb
53         res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
54         self.assertEquals(len(res), 1)
55         self.schema_dn = res[0]["schemaNamingContext"][0]
56         self.base_dn = res[0]["defaultNamingContext"][0]
57         self.forest_level = int(res[0]["forestFunctionality"][0])
58
59         # get DC invocation_id
60         self.invocation_id = GUID(ldb.get_invocation_id())
61
62     def tearDown(self):
63         super(SchemaInfoTestCase, self).tearDown()
64
65     def _getSchemaInfo(self):
66         try:
67             schema_info_data = ldb.searchone(attribute="schemaInfo",
68                                              basedn=self.schema_dn,
69                                              expression="(objectClass=*)",
70                                              scope=SCOPE_BASE)
71             self.assertEqual(len(schema_info_data), 21)
72             schema_info = ndr_unpack(schemaInfoBlob, schema_info_data)
73             self.assertEqual(schema_info.marker, 0xFF)
74         except KeyError:
75             # create default schemaInfo if
76             # attribute value is not created yet
77             schema_info = schemaInfoBlob()
78             schema_info.revision = 0
79             schema_info.invocation_id = self.invocation_id
80         return schema_info
81
82     def _checkSchemaInfo(self, schi_before, schi_after):
83         self.assertEqual(schi_before.revision + 1, schi_after.revision)
84         self.assertEqual(schi_before.invocation_id, schi_after.invocation_id)
85         self.assertEqual(schi_after.invocation_id, self.invocation_id)
86
87     def _ldap_schemaUpdateNow(self):
88         ldif = """
89 dn:
90 changetype: modify
91 add: schemaUpdateNow
92 schemaUpdateNow: 1
93 """
94         self.ldb.modify_ldif(ldif)
95
96     def _make_obj_names(self, prefix):
97         obj_name = prefix + time.strftime("%s", time.gmtime())
98         obj_ldap_name = obj_name.replace("-", "")
99         obj_dn = "CN=%s,%s" % (obj_name, self.schema_dn)
100         return (obj_name, obj_ldap_name, obj_dn)
101
102     def _make_attr_ldif(self, attr_name, attr_dn):
103         ldif = """
104 dn: """ + attr_dn + """
105 objectClass: top
106 objectClass: attributeSchema
107 adminDescription: """ + attr_name + """
108 adminDisplayName: """ + attr_name + """
109 cn: """ + attr_name + """
110 attributeId: 1.2.840.""" + str(random.randint(1,100000)) + """.1.5.9940
111 attributeSyntax: 2.5.5.12
112 omSyntax: 64
113 instanceType: 4
114 isSingleValued: TRUE
115 systemOnly: FALSE
116 """
117         return ldif
118
119     def test_AddModifyAttribute(self):
120         # get initial schemaInfo
121         schi_before = self._getSchemaInfo()
122
123         # create names for an attribute to add
124         (attr_name, attr_ldap_name, attr_dn) = self._make_obj_names("schemaInfo-Attr-")
125         ldif = self._make_attr_ldif(attr_name, attr_dn)
126
127         # add the new attribute
128         self.ldb.add_ldif(ldif)
129         self._ldap_schemaUpdateNow()
130         # compare resulting schemaInfo
131         schi_after = self._getSchemaInfo()
132         self._checkSchemaInfo(schi_before, schi_after)
133
134         # rename the Attribute
135         attr_dn_new = attr_dn.replace(attr_name, attr_name + "-NEW")
136         try:
137             self.ldb.rename(attr_dn, attr_dn_new)
138         except LdbError, (num, _):
139             self.fail("failed to change lDAPDisplayName for %s: %s" % (attr_name, _))
140
141         # compare resulting schemaInfo
142         schi_after = self._getSchemaInfo()
143         self._checkSchemaInfo(schi_before, schi_after)
144         pass
145
146
147     def _make_class_ldif(self, class_name, class_dn):
148         ldif = """
149 dn: """ + class_dn + """
150 objectClass: top
151 objectClass: classSchema
152 adminDescription: """ + class_name + """
153 adminDisplayName: """ + class_name + """
154 cn: """ + class_name + """
155 governsId: 1.2.840.""" + str(random.randint(1,100000)) + """.1.5.9939
156 instanceType: 4
157 objectClassCategory: 1
158 subClassOf: organizationalPerson
159 rDNAttID: cn
160 systemMustContain: cn
161 systemOnly: FALSE
162 """
163         return ldif
164
165     def test_AddModifyClass(self):
166         # get initial schemaInfo
167         schi_before = self._getSchemaInfo()
168
169         # create names for a Class to add
170         (class_name, class_ldap_name, class_dn) = self._make_obj_names("schemaInfo-Class-")
171         ldif = self._make_class_ldif(class_name, class_dn)
172
173         # add the new Class
174         self.ldb.add_ldif(ldif)
175         self._ldap_schemaUpdateNow()
176         # compare resulting schemaInfo
177         schi_after = self._getSchemaInfo()
178         self._checkSchemaInfo(schi_before, schi_after)
179
180         # rename the Class
181         class_dn_new = class_dn.replace(class_name, class_name + "-NEW")
182         try:
183             self.ldb.rename(class_dn, class_dn_new)
184         except LdbError, (num, _):
185             self.fail("failed to change lDAPDisplayName for %s: %s" % (class_name, _))
186
187         # compare resulting schemaInfo
188         schi_after = self._getSchemaInfo()
189         self._checkSchemaInfo(schi_before, schi_after)
190
191
192 ########################################################################################
193 if not "DC_SERVER" in os.environ.keys():
194     raise AssertionError("Please supply TARGET_DC in environment")
195 ldb_url = os.environ["DC_SERVER"]
196
197 ldb_options = []
198 if not "://" in ldb_url:
199     if os.path.isfile(ldb_url):
200         ldb_url = "tdb://%s" % ldb_url
201     else:
202         ldb_url = "ldap://%s" % ldb_url
203         # user 'paged_search' module when connecting remotely
204         ldb_options = ["modules:paged_searches"]
205
206 ldb = SamDB(url=ldb_url,
207           lp=samba.tests.env_loadparm(),
208           session_info=system_session(),
209           credentials=samba.tests.cmdline_credentials,
210           options=ldb_options)