pyldb: Add dom_sid.split in favor of less powerful dom_sid_to_rid().
[Samba/kamenim.git] / source4 / lib / ldb / tests / python / ldap.py
blobc2920c009e1f3f32832c0dae27613fbbd1ad02e0
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # This is a port of the original in testprogs/ejs/ldap.js
5 import getopt
6 import optparse
7 import sys
8 import time
9 import random
10 import base64
11 import os
13 sys.path.append("bin/python")
14 sys.path.append("../lib/subunit/python")
16 import samba.getopt as options
18 from samba.auth import system_session
19 from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL, SCOPE_BASE, LdbError
20 from ldb import ERR_NO_SUCH_OBJECT, ERR_ATTRIBUTE_OR_VALUE_EXISTS
21 from ldb import ERR_ENTRY_ALREADY_EXISTS, ERR_UNWILLING_TO_PERFORM
22 from ldb import ERR_NOT_ALLOWED_ON_NON_LEAF, ERR_OTHER, ERR_INVALID_DN_SYNTAX
23 from ldb import ERR_NO_SUCH_ATTRIBUTE, ERR_INSUFFICIENT_ACCESS_RIGHTS
24 from ldb import ERR_OBJECT_CLASS_VIOLATION, ERR_NOT_ALLOWED_ON_RDN
25 from ldb import ERR_NAMING_VIOLATION, ERR_CONSTRAINT_VIOLATION
26 from ldb import ERR_UNDEFINED_ATTRIBUTE_TYPE
27 from ldb import Message, MessageElement, Dn
28 from ldb import FLAG_MOD_ADD, FLAG_MOD_REPLACE, FLAG_MOD_DELETE
29 from samba import Ldb, param
30 from samba import UF_NORMAL_ACCOUNT, UF_TEMP_DUPLICATE_ACCOUNT
31 from samba import UF_SERVER_TRUST_ACCOUNT, UF_WORKSTATION_TRUST_ACCOUNT
32 from samba import UF_INTERDOMAIN_TRUST_ACCOUNT
33 from samba import UF_PASSWD_NOTREQD, UF_ACCOUNTDISABLE
34 from samba import GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
35 from samba import GTYPE_SECURITY_GLOBAL_GROUP, GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
36 from samba import GTYPE_SECURITY_UNIVERSAL_GROUP
37 from samba import GTYPE_DISTRIBUTION_GLOBAL_GROUP
38 from samba import GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
39 from samba import GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
40 from samba import ATYPE_NORMAL_ACCOUNT, ATYPE_WORKSTATION_TRUST
41 from samba import ATYPE_SECURITY_GLOBAL_GROUP, ATYPE_SECURITY_LOCAL_GROUP
42 from samba import ATYPE_SECURITY_UNIVERSAL_GROUP
43 from samba import ATYPE_DISTRIBUTION_GLOBAL_GROUP
44 from samba import ATYPE_DISTRIBUTION_LOCAL_GROUP
45 from samba import ATYPE_DISTRIBUTION_UNIVERSAL_GROUP
47 from subunit import SubunitTestRunner
48 import unittest
50 from samba.ndr import ndr_pack, ndr_unpack
51 from samba.dcerpc import security
53 parser = optparse.OptionParser("ldap [options] <host>")
54 sambaopts = options.SambaOptions(parser)
55 parser.add_option_group(sambaopts)
56 parser.add_option_group(options.VersionOptions(parser))
57 # use command line creds if available
58 credopts = options.CredentialsOptions(parser)
59 parser.add_option_group(credopts)
60 opts, args = parser.parse_args()
62 if len(args) < 1:
63 parser.print_usage()
64 sys.exit(1)
66 host = args[0]
68 lp = sambaopts.get_loadparm()
69 creds = credopts.get_credentials(lp)
71 class BasicTests(unittest.TestCase):
72 def delete_force(self, ldb, dn):
73 try:
74 ldb.delete(dn)
75 except LdbError, (num, _):
76 self.assertEquals(num, ERR_NO_SUCH_OBJECT)
78 def find_basedn(self, ldb):
79 res = ldb.search(base="", expression="", scope=SCOPE_BASE,
80 attrs=["defaultNamingContext"])
81 self.assertEquals(len(res), 1)
82 return res[0]["defaultNamingContext"][0]
84 def find_configurationdn(self, ldb):
85 res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["configurationNamingContext"])
86 self.assertEquals(len(res), 1)
87 return res[0]["configurationNamingContext"][0]
89 def find_schemadn(self, ldb):
90 res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["schemaNamingContext"])
91 self.assertEquals(len(res), 1)
92 return res[0]["schemaNamingContext"][0]
94 def find_domain_sid(self):
95 res = self.ldb.search(base=self.base_dn, expression="(objectClass=*)", scope=SCOPE_BASE)
96 return ndr_unpack( security.dom_sid,res[0]["objectSid"][0])
98 def setUp(self):
99 self.ldb = ldb
100 self.gc_ldb = gc_ldb
101 self.base_dn = self.find_basedn(ldb)
102 self.configuration_dn = self.find_configurationdn(ldb)
103 self.schema_dn = self.find_schemadn(ldb)
104 self.domain_sid = self.find_domain_sid()
106 print "baseDN: %s\n" % self.base_dn
108 self.delete_force(self.ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
109 self.delete_force(self.ldb, "cn=ldaptestuser2,cn=users," + self.base_dn)
110 self.delete_force(self.ldb, "cn=ldaptestuser3,cn=users," + self.base_dn)
111 self.delete_force(self.ldb, "cn=ldaptestuser4,cn=ldaptestcontainer," + self.base_dn)
112 self.delete_force(self.ldb, "cn=ldaptestuser4,cn=ldaptestcontainer2," + self.base_dn)
113 self.delete_force(self.ldb, "cn=ldaptestuser5,cn=users," + self.base_dn)
114 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
115 self.delete_force(self.ldb, "cn=ldaptestgroup2,cn=users," + self.base_dn)
116 self.delete_force(self.ldb, "cn=ldaptestcomputer,cn=computers," + self.base_dn)
117 self.delete_force(self.ldb, "cn=ldaptest2computer,cn=computers," + self.base_dn)
118 self.delete_force(self.ldb, "cn=ldaptestcomputer3,cn=computers," + self.base_dn)
119 self.delete_force(self.ldb, "cn=ldaptestutf8user èùéìòà,cn=users," + self.base_dn)
120 self.delete_force(self.ldb, "cn=ldaptestutf8user2 èùéìòà,cn=users," + self.base_dn)
121 self.delete_force(self.ldb, "cn=ldaptestcontainer," + self.base_dn)
122 self.delete_force(self.ldb, "cn=ldaptestcontainer2," + self.base_dn)
123 self.delete_force(self.ldb, "cn=parentguidtest,cn=users," + self.base_dn)
124 self.delete_force(self.ldb, "cn=parentguidtest,cn=testotherusers," + self.base_dn)
125 self.delete_force(self.ldb, "cn=testotherusers," + self.base_dn)
126 self.delete_force(self.ldb, "cn=ldaptestobject," + self.base_dn)
127 self.delete_force(self.ldb, "description=xyz,cn=users," + self.base_dn)
128 self.delete_force(self.ldb, "ou=testou,cn=users," + self.base_dn)
130 def test_system_only(self):
131 """Test systemOnly objects"""
132 print "Test systemOnly objects"""
134 try:
135 self.ldb.add({
136 "dn": "cn=ldaptestobject," + self.base_dn,
137 "objectclass": "configuration"})
138 self.fail()
139 except LdbError, (num, _):
140 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
142 self.delete_force(self.ldb, "cn=ldaptestobject," + self.base_dn)
144 def test_invalid_parent(self):
145 """Test adding an object with invalid parent"""
146 print "Test adding an object with invalid parent"""
148 try:
149 self.ldb.add({
150 "dn": "cn=ldaptestgroup,cn=thisdoesnotexist123,"
151 + self.base_dn,
152 "objectclass": "group"})
153 self.fail()
154 except LdbError, (num, _):
155 self.assertEquals(num, ERR_NO_SUCH_OBJECT)
157 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=thisdoesnotexist123,"
158 + self.base_dn)
160 try:
161 self.ldb.add({
162 "dn": "ou=testou,cn=users," + self.base_dn,
163 "objectclass": "organizationalUnit"})
164 self.fail()
165 except LdbError, (num, _):
166 self.assertEquals(num, ERR_NAMING_VIOLATION)
168 self.delete_force(self.ldb, "ou=testou,cn=users," + self.base_dn)
170 def test_invalid_attribute(self):
171 """Test adding invalid attributes (not in schema)"""
172 print "Test adding invalid attributes (not in schema)"""
174 try:
175 self.ldb.add({
176 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
177 "objectclass": "group",
178 "thisdoesnotexist": "x"})
179 self.fail()
180 except LdbError, (num, _):
181 self.assertEquals(num, ERR_NO_SUCH_ATTRIBUTE)
183 self.ldb.add({
184 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
185 "objectclass": "group"})
187 m = Message()
188 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
189 m["thisdoesnotexist"] = MessageElement("x", FLAG_MOD_REPLACE,
190 "thisdoesnotexist")
191 try:
192 ldb.modify(m)
193 self.fail()
194 except LdbError, (num, _):
195 self.assertEquals(num, ERR_NO_SUCH_ATTRIBUTE)
197 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
199 def test_single_valued_attributes(self):
200 """Test single-valued attributes"""
201 print "Test single-valued attributes"""
203 try:
204 self.ldb.add({
205 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
206 "objectclass": "group",
207 "sAMAccountName": ["nam1", "nam2"]})
208 self.fail()
209 except LdbError, (num, _):
210 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
212 self.ldb.add({
213 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
214 "objectclass": "group"})
216 m = Message()
217 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
218 m["sAMAccountName"] = MessageElement(["nam1","nam2"], FLAG_MOD_REPLACE,
219 "sAMAccountName")
220 try:
221 ldb.modify(m)
222 self.fail()
223 except LdbError, (num, _):
224 self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
226 m = Message()
227 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
228 m["sAMAccountName"] = MessageElement("testgroupXX", FLAG_MOD_REPLACE,
229 "sAMAccountName")
230 ldb.modify(m)
232 m = Message()
233 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
234 m["sAMAccountName"] = MessageElement("testgroupXX2", FLAG_MOD_ADD,
235 "sAMAccountName")
236 try:
237 ldb.modify(m)
238 self.fail()
239 except LdbError, (num, _):
240 self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
242 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
244 def test_multi_valued_attributes(self):
245 """Test multi-valued attributes"""
246 print "Test multi-valued attributes"""
248 # TODO: In this test I added some special tests where I got very unusual
249 # results back from a real AD. s4 doesn't match them and I've no idea how to
250 # implement those error cases (maybe there exists a special trigger for
251 # "description" attributes which handle them)
253 self.ldb.add({
254 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
255 "description": "desc2",
256 "objectclass": "group",
257 "description": "desc1"})
259 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
261 self.ldb.add({
262 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
263 "objectclass": "group",
264 "description": ["desc1", "desc2"]})
266 # m = Message()
267 # m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
268 # m["description"] = MessageElement(["desc1","desc2"], FLAG_MOD_REPLACE,
269 # "description")
270 # try:
271 # ldb.modify(m)
272 # self.fail()
273 # except LdbError, (num, _):
274 # self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
276 m = Message()
277 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
278 m["description"] = MessageElement("desc1", FLAG_MOD_REPLACE,
279 "description")
280 ldb.modify(m)
282 # m = Message()
283 # m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
284 # m["description"] = MessageElement("desc3", FLAG_MOD_ADD,
285 # "description")
286 # try:
287 # ldb.modify(m)
288 # self.fail()
289 # except LdbError, (num, _):
290 # self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
292 m = Message()
293 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
294 m["description"] = MessageElement(["desc1","desc2"], FLAG_MOD_DELETE,
295 "description")
296 try:
297 ldb.modify(m)
298 self.fail()
299 except LdbError, (num, _):
300 self.assertEquals(num, ERR_NO_SUCH_ATTRIBUTE)
302 m = Message()
303 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
304 m["description"] = MessageElement("desc1", FLAG_MOD_DELETE,
305 "description")
306 ldb.modify(m)
308 # m = Message()
309 # m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
310 # m["description"] = MessageElement(["desc1","desc2"], FLAG_MOD_REPLACE,
311 # "description")
312 # try:
313 # ldb.modify(m)
314 # self.fail()
315 # except LdbError, (num, _):
316 # self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
318 # m = Message()
319 # m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
320 # m["description"] = MessageElement(["desc3", "desc4"], FLAG_MOD_ADD,
321 # "description")
322 # try:
323 # ldb.modify(m)
324 # self.fail()
325 # except LdbError, (num, _):
326 # self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
328 m = Message()
329 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
330 m["description"] = MessageElement("desc3", FLAG_MOD_ADD,
331 "description")
332 ldb.modify(m)
334 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
336 def test_empty_messages(self):
337 """Test empty messages"""
338 print "Test empty messages"""
340 m = Message()
341 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
343 try:
344 ldb.add(m)
345 self.fail()
346 except LdbError, (num, _):
347 self.assertEquals(num, ERR_OBJECT_CLASS_VIOLATION)
349 try:
350 ldb.modify(m)
351 self.fail()
352 except LdbError, (num, _):
353 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
355 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
357 def test_empty_attributes(self):
358 """Test empty attributes"""
359 print "Test empty attributes"""
361 m = Message()
362 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
363 m["objectClass"] = MessageElement("group", FLAG_MOD_ADD, "objectClass")
364 m["description"] = MessageElement([], FLAG_MOD_ADD, "description")
366 try:
367 ldb.add(m)
368 self.fail()
369 except LdbError, (num, _):
370 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
372 self.ldb.add({
373 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
374 "objectclass": "group"})
376 m = Message()
377 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
378 m["description"] = MessageElement([], FLAG_MOD_ADD, "description")
380 try:
381 ldb.modify(m)
382 self.fail()
383 except LdbError, (num, _):
384 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
386 m = Message()
387 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
388 m["description"] = MessageElement([], FLAG_MOD_REPLACE, "description")
389 ldb.modify(m)
391 m = Message()
392 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
393 m["description"] = MessageElement([], FLAG_MOD_DELETE, "description")
394 try:
395 ldb.modify(m)
396 self.fail()
397 except LdbError, (num, _):
398 self.assertEquals(num, ERR_NO_SUCH_ATTRIBUTE)
400 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
402 def test_distinguished_name(self):
403 """Tests the 'distinguishedName' attribute"""
404 print "Tests the 'distinguishedName' attribute"""
406 self.ldb.add({
407 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
408 "objectclass": "group"})
410 m = Message()
411 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
412 m["distinguishedName"] = MessageElement(
413 "cn=ldaptestuser,cn=users," + self.base_dn, FLAG_MOD_ADD,
414 "distinguishedName")
416 try:
417 ldb.modify(m)
418 self.fail()
419 except LdbError, (num, _):
420 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
422 m = Message()
423 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
424 m["distinguishedName"] = MessageElement(
425 "cn=ldaptestuser,cn=users," + self.base_dn, FLAG_MOD_REPLACE,
426 "distinguishedName")
428 try:
429 ldb.modify(m)
430 self.fail()
431 except LdbError, (num, _):
432 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
434 m = Message()
435 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
436 m["distinguishedName"] = MessageElement(
437 "cn=ldaptestuser,cn=users," + self.base_dn, FLAG_MOD_DELETE,
438 "distinguishedName")
440 try:
441 ldb.modify(m)
442 self.fail()
443 except LdbError, (num, _):
444 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
446 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
448 def test_rdn_name(self):
449 """Tests the RDN"""
450 print "Tests the RDN"""
452 try:
453 self.ldb.add({
454 "dn": "description=xyz,cn=users," + self.base_dn,
455 "objectclass": "group"})
456 self.fail()
457 except LdbError, (num, _):
458 self.assertEquals(num, ERR_NAMING_VIOLATION)
460 self.delete_force(self.ldb, "description=xyz,cn=users," + self.base_dn)
462 self.ldb.add({
463 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
464 "objectclass": "group"})
466 m = Message()
467 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
468 m["name"] = MessageElement("cn=ldaptestuser", FLAG_MOD_REPLACE,
469 "name")
471 try:
472 ldb.modify(m)
473 self.fail()
474 except LdbError, (num, _):
475 self.assertEquals(num, ERR_NOT_ALLOWED_ON_RDN)
477 m = Message()
478 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
479 m["cn"] = MessageElement("ldaptestuser",
480 FLAG_MOD_REPLACE, "cn")
482 try:
483 ldb.modify(m)
484 self.fail()
485 except LdbError, (num, _):
486 self.assertEquals(num, ERR_NOT_ALLOWED_ON_RDN)
488 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
491 # this test needs to be disabled until we really understand
492 # what the rDN length constraints are
493 def DISABLED_test_largeRDN(self):
494 """Testing large rDN (limit 64 characters)"""
495 rdn = "CN=a012345678901234567890123456789012345678901234567890123456789012";
496 self.delete_force(self.ldb, "%s,%s" % (rdn, self.base_dn))
497 ldif = """
498 dn: %s,%s""" % (rdn,self.base_dn) + """
499 objectClass: container
501 self.ldb.add_ldif(ldif)
502 self.delete_force(self.ldb, "%s,%s" % (rdn, self.base_dn))
504 rdn = "CN=a0123456789012345678901234567890123456789012345678901234567890120";
505 self.delete_force(self.ldb, "%s,%s" % (rdn, self.base_dn))
506 try:
507 ldif = """
508 dn: %s,%s""" % (rdn,self.base_dn) + """
509 objectClass: container
511 self.ldb.add_ldif(ldif)
512 self.fail()
513 except LdbError, (num, _):
514 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
515 self.delete_force(self.ldb, "%s,%s" % (rdn, self.base_dn))
517 def test_rename(self):
518 """Tests the rename operation"""
519 print "Tests the rename operations"""
521 self.ldb.add({
522 "dn": "cn=ldaptestuser2,cn=users," + self.base_dn,
523 "objectclass": ["user", "person"] })
525 ldb.rename("cn=ldaptestuser2,cn=users," + self.base_dn, "cn=ldaptestuser2,cn=users," + self.base_dn)
526 ldb.rename("cn=ldaptestuser2,cn=users," + self.base_dn, "cn=ldaptestuser3,cn=users," + self.base_dn)
527 ldb.rename("cn=ldaptestuser3,cn=users," + self.base_dn, "cn=ldaptestUSER3,cn=users," + self.base_dn)
528 try:
529 ldb.rename("cn=ldaptestuser3,cn=users," + self.base_dn, ",cn=users," + self.base_dn)
530 self.fail()
531 except LdbError, (num, _):
532 self.assertEquals(num, ERR_INVALID_DN_SYNTAX)
534 self.delete_force(self.ldb, "cn=ldaptestuser3,cn=users," + self.base_dn)
536 def test_rename_twice(self):
537 """Tests the rename operation twice - this corresponds to a past bug"""
538 print "Tests the rename twice operation"""
540 self.ldb.add({
541 "dn": "cn=ldaptestuser5,cn=users," + self.base_dn,
542 "objectclass": ["user", "person"] })
544 ldb.rename("cn=ldaptestuser5,cn=users," + self.base_dn, "cn=ldaptestUSER5,cn=users," + self.base_dn)
545 self.delete_force(self.ldb, "cn=ldaptestuser5,cn=users," + self.base_dn)
546 self.ldb.add({
547 "dn": "cn=ldaptestuser5,cn=users," + self.base_dn,
548 "objectclass": ["user", "person"] })
549 ldb.rename("cn=ldaptestuser5,cn=Users," + self.base_dn, "cn=ldaptestUSER5,cn=users," + self.base_dn)
550 res = ldb.search(expression="cn=ldaptestuser5")
551 print "Found %u records" % len(res)
552 self.assertEquals(len(res), 1, "Wrong number of hits for cn=ldaptestuser5")
553 res = ldb.search(expression="(&(cn=ldaptestuser5)(objectclass=user))")
554 print "Found %u records" % len(res)
555 self.assertEquals(len(res), 1, "Wrong number of hits for (&(cn=ldaptestuser5)(objectclass=user))")
556 self.delete_force(self.ldb, "cn=ldaptestuser5,cn=users," + self.base_dn)
558 def test_parentGUID(self):
559 """Test parentGUID behaviour"""
560 print "Testing parentGUID behaviour\n"
562 # TODO: This seems to fail on Windows Server. Hidden attribute?
564 self.ldb.add({
565 "dn": "cn=parentguidtest,cn=users," + self.base_dn,
566 "objectclass":"user",
567 "samaccountname":"parentguidtest"});
568 res1 = ldb.search(base="cn=parentguidtest,cn=users," + self.base_dn, scope=SCOPE_BASE,
569 attrs=["parentGUID", "samaccountname"]);
570 res2 = ldb.search(base="cn=users," + self.base_dn,scope=SCOPE_BASE,
571 attrs=["objectGUID"]);
572 res3 = ldb.search(base=self.base_dn, scope=SCOPE_BASE,
573 attrs=["parentGUID"]);
575 """Check if the parentGUID is valid """
576 self.assertEquals(res1[0]["parentGUID"], res2[0]["objectGUID"]);
578 """Check if it returns nothing when there is no parent object"""
579 has_parentGUID = False
580 for key in res3[0].keys():
581 if key == "parentGUID":
582 has_parentGUID = True
583 break
584 self.assertFalse(has_parentGUID);
586 """Ensures that if you look for another object attribute after the constructed
587 parentGUID, it will return correctly"""
588 has_another_attribute = False
589 for key in res1[0].keys():
590 if key == "sAMAccountName":
591 has_another_attribute = True
592 break
593 self.assertTrue(has_another_attribute)
594 self.assertTrue(len(res1[0]["samaccountname"]) == 1)
595 self.assertEquals(res1[0]["samaccountname"][0], "parentguidtest");
597 print "Testing parentGUID behaviour on rename\n"
599 self.ldb.add({
600 "dn": "cn=testotherusers," + self.base_dn,
601 "objectclass":"container"});
602 res1 = ldb.search(base="cn=testotherusers," + self.base_dn,scope=SCOPE_BASE,
603 attrs=["objectGUID"]);
604 ldb.rename("cn=parentguidtest,cn=users," + self.base_dn,
605 "cn=parentguidtest,cn=testotherusers," + self.base_dn);
606 res2 = ldb.search(base="cn=parentguidtest,cn=testotherusers," + self.base_dn,
607 scope=SCOPE_BASE,
608 attrs=["parentGUID"]);
609 self.assertEquals(res1[0]["objectGUID"], res2[0]["parentGUID"]);
611 self.delete_force(self.ldb, "cn=parentguidtest,cn=testotherusers," + self.base_dn)
612 self.delete_force(self.ldb, "cn=testotherusers," + self.base_dn)
614 def test_groupType_int32(self):
615 """Test groupType (int32) behaviour (should appear to be casted to a 32 bit signed integer before comparsion)"""
616 print "Testing groupType (int32) behaviour\n"
618 res1 = ldb.search(base=self.base_dn, scope=SCOPE_SUBTREE,
619 attrs=["groupType"], expression="groupType=2147483653");
621 res2 = ldb.search(base=self.base_dn, scope=SCOPE_SUBTREE,
622 attrs=["groupType"], expression="groupType=-2147483643");
624 self.assertEquals(len(res1), len(res2))
626 self.assertTrue(res1.count > 0)
628 self.assertEquals(res1[0]["groupType"][0], "-2147483643")
630 def test_groups(self):
631 """This tests the group behaviour (setting, changing) of a user account"""
632 print "Testing group behaviour\n"
634 ldb.add({
635 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
636 "objectclass": "group"})
638 ldb.add({
639 "dn": "cn=ldaptestgroup2,cn=users," + self.base_dn,
640 "objectclass": "group"})
642 res1 = ldb.search("cn=ldaptestgroup,cn=users," + self.base_dn,
643 scope=SCOPE_BASE, attrs=["objectSID"])
644 self.assertTrue(len(res1) == 1)
645 group_rid_1 = security.dom_sid(ldb.schema_format_value("objectSID",
646 res1[0]["objectSID"][0])).split()[1]
648 res1 = ldb.search("cn=ldaptestgroup2,cn=users," + self.base_dn,
649 scope=SCOPE_BASE, attrs=["objectSID"])
650 self.assertTrue(len(res1) == 1)
651 group_rid_2 = security.dom_sid(ldb.schema_format_value("objectSID",
652 res1[0]["objectSID"][0])).split()[1]
654 # Try to create a user with an invalid primary group
655 try:
656 ldb.add({
657 "dn": "cn=ldaptestuser,cn=users," + self.base_dn,
658 "objectclass": ["user", "person"],
659 "primaryGroupID": "0"})
660 self.fail()
661 except LdbError, (num, _):
662 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
663 self.delete_force(self.ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
665 # Try to Create a user with a valid primary group
666 # TODO Some more investigation needed here
667 # try:
668 # ldb.add({
669 # "dn": "cn=ldaptestuser,cn=users," + self.base_dn,
670 # "objectclass": ["user", "person"],
671 # "primaryGroupID": str(group_rid_1)})
672 # self.fail()
673 # except LdbError, (num, _):
674 # self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
675 # self.delete_force(self.ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
677 ldb.add({
678 "dn": "cn=ldaptestuser,cn=users," + self.base_dn,
679 "objectclass": ["user", "person"]})
681 # Try to add invalid primary group
682 m = Message()
683 m.dn = Dn(ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
684 m["primaryGroupID"] = MessageElement("0", FLAG_MOD_REPLACE,
685 "primaryGroupID")
686 try:
687 ldb.modify(m)
688 self.fail()
689 except LdbError, (num, _):
690 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
692 # Try to make group 1 primary - should be denied since it is not yet
693 # secondary
694 m = Message()
695 m.dn = Dn(ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
696 m["primaryGroupID"] = MessageElement(str(group_rid_1),
697 FLAG_MOD_REPLACE, "primaryGroupID")
698 try:
699 ldb.modify(m)
700 self.fail()
701 except LdbError, (num, _):
702 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
704 # Make group 1 secondary
705 m = Message()
706 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
707 m["member"] = "cn=ldaptestuser,cn=users," + self.base_dn
708 ldb.modify(m)
710 # Make group 1 primary
711 m = Message()
712 m.dn = Dn(ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
713 m["primaryGroupID"] = MessageElement(str(group_rid_1),
714 FLAG_MOD_REPLACE, "primaryGroupID")
715 ldb.modify(m)
717 # Try to delete group 1 - should be denied
718 try:
719 ldb.delete("cn=ldaptestgroup,cn=users," + self.base_dn)
720 self.fail()
721 except LdbError, (num, _):
722 self.assertEquals(num, ERR_ENTRY_ALREADY_EXISTS)
724 # Try to add group 1 also as secondary - should be denied
725 m = Message()
726 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
727 m["member"] = "cn=ldaptestuser,cn=users," + self.base_dn
728 try:
729 ldb.modify(m)
730 self.fail()
731 except LdbError, (num, _):
732 self.assertEquals(num, ERR_ENTRY_ALREADY_EXISTS)
734 # Try to add invalid member to group 1 - should be denied
735 m = Message()
736 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
737 m["member"] = MessageElement(
738 "cn=ldaptestuser3,cn=users," + self.base_dn,
739 FLAG_MOD_ADD, "member")
740 try:
741 ldb.modify(m)
742 self.fail()
743 except LdbError, (num, _):
744 self.assertEquals(num, ERR_NO_SUCH_OBJECT)
746 # Make group 2 secondary
747 m = Message()
748 m.dn = Dn(ldb, "cn=ldaptestgroup2,cn=users," + self.base_dn)
749 m["member"] = "cn=ldaptestuser,cn=users," + self.base_dn
750 ldb.modify(m)
752 # Swap the groups
753 m = Message()
754 m.dn = Dn(ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
755 m["primaryGroupID"] = MessageElement(str(group_rid_2),
756 FLAG_MOD_REPLACE, "primaryGroupID")
757 ldb.modify(m)
759 # Old primary group should contain a "member" attribute for the user,
760 # the new shouldn't contain anymore one
761 res1 = ldb.search("cn=ldaptestgroup, cn=users," + self.base_dn,
762 scope=SCOPE_BASE, attrs=["member"])
763 self.assertTrue(len(res1) == 1)
764 self.assertTrue(len(res1[0]["member"]) == 1)
765 self.assertEquals(res1[0]["member"][0].lower(),
766 ("cn=ldaptestuser,cn=users," + self.base_dn).lower())
768 res1 = ldb.search("cn=ldaptestgroup2, cn=users," + self.base_dn,
769 scope=SCOPE_BASE, attrs=["member"])
770 self.assertTrue(len(res1) == 1)
771 self.assertFalse("member" in res1[0])
773 # Also this should be denied
774 try:
775 ldb.add({
776 "dn": "cn=ldaptestuser1,cn=users," + self.base_dn,
777 "objectclass": ["user", "person"],
778 "primaryGroupID": "0"})
779 self.fail()
780 except LdbError, (num, _):
781 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
783 self.delete_force(self.ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
784 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
785 self.delete_force(self.ldb, "cn=ldaptestgroup2,cn=users," + self.base_dn)
787 def test_primary_group_token(self):
788 """Test the primary group token behaviour (hidden-generated-readonly attribute on groups)"""
789 print "Testing primary group token behaviour\n"
791 try:
792 ldb.add({
793 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
794 "objectclass": "group",
795 "primaryGroupToken": "100"})
796 self.fail()
797 except LdbError, (num, _):
798 self.assertEquals(num, ERR_UNDEFINED_ATTRIBUTE_TYPE)
799 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
801 ldb.add({
802 "dn": "cn=ldaptestuser,cn=users," + self.base_dn,
803 "objectclass": ["user", "person"]})
805 ldb.add({
806 "dn": "cn=ldaptestgroup,cn=users," + self.base_dn,
807 "objectclass": "group"})
809 # Testing for one invalid, and one valid operational attribute, but also the things they are built from
810 res1 = ldb.search(self.base_dn,
811 scope=SCOPE_BASE, attrs=["primaryGroupToken", "canonicalName", "objectClass", "objectSid"])
812 self.assertTrue(len(res1) == 1)
813 self.assertFalse("primaryGroupToken" in res1[0])
814 self.assertTrue("canonicalName" in res1[0])
815 self.assertTrue("objectClass" in res1[0])
816 self.assertTrue("objectSid" in res1[0])
818 res1 = ldb.search(self.base_dn,
819 scope=SCOPE_BASE, attrs=["primaryGroupToken", "canonicalName"])
820 self.assertTrue(len(res1) == 1)
821 self.assertFalse("primaryGroupToken" in res1[0])
822 self.assertFalse("objectSid" in res1[0])
823 self.assertFalse("objectClass" in res1[0])
824 self.assertTrue("canonicalName" in res1[0])
826 res1 = ldb.search("cn=users,"+self.base_dn,
827 scope=SCOPE_BASE, attrs=["primaryGroupToken"])
828 self.assertTrue(len(res1) == 1)
829 self.assertFalse("primaryGroupToken" in res1[0])
831 res1 = ldb.search("cn=ldaptestuser, cn=users," + self.base_dn,
832 scope=SCOPE_BASE, attrs=["primaryGroupToken"])
833 self.assertTrue(len(res1) == 1)
834 self.assertFalse("primaryGroupToken" in res1[0])
836 res1 = ldb.search("cn=ldaptestgroup,cn=users," + self.base_dn,
837 scope=SCOPE_BASE)
838 self.assertTrue(len(res1) == 1)
839 self.assertFalse("primaryGroupToken" in res1[0])
841 res1 = ldb.search("cn=ldaptestgroup,cn=users," + self.base_dn,
842 scope=SCOPE_BASE, attrs=["primaryGroupToken", "objectSID"])
843 self.assertTrue(len(res1) == 1)
844 primary_group_token = int(res1[0]["primaryGroupToken"][0])
846 rid = security.dom_sid(ldb.schema_format_value("objectSID", res1[0]["objectSID"][0])).split()[1]
847 self.assertEquals(primary_group_token, rid)
849 m = Message()
850 m.dn = Dn(ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
851 m["primaryGroupToken"] = "100"
852 try:
853 ldb.modify(m)
854 self.fail()
855 except LdbError, (num, _):
856 self.assertEquals(num, ERR_CONSTRAINT_VIOLATION)
858 self.delete_force(self.ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
859 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
861 def test_wkguid(self):
862 """Test Well known GUID behaviours (including DN+Binary)"""
863 print "Test Well known GUID behaviours (including DN+Binary)"""
865 res = self.ldb.search(base=("<WKGUID=ab1d30f3768811d1aded00c04fd8d5cd,%s>" % self.base_dn), scope=SCOPE_BASE, attrs=[])
866 self.assertEquals(len(res), 1)
868 res2 = self.ldb.search(scope=SCOPE_BASE, attrs=["wellKnownObjects"], expression=("wellKnownObjects=B:32:ab1d30f3768811d1aded00c04fd8d5cd:%s" % res[0].dn))
869 self.assertEquals(len(res2), 1)
871 # Prove that the matching rule is over the whole DN+Binary
872 res2 = self.ldb.search(scope=SCOPE_BASE, attrs=["wellKnownObjects"], expression=("wellKnownObjects=B:32:ab1d30f3768811d1aded00c04fd8d5cd"))
873 self.assertEquals(len(res2), 0)
874 # Prove that the matching rule is over the whole DN+Binary
875 res2 = self.ldb.search(scope=SCOPE_BASE, attrs=["wellKnownObjects"], expression=("wellKnownObjects=%s") % res[0].dn)
876 self.assertEquals(len(res2), 0)
878 def test_subschemasubentry(self):
879 """Test subSchemaSubEntry appears when requested, but not when not requested"""
880 print "Test subSchemaSubEntry"""
882 res = self.ldb.search(base=self.base_dn, scope=SCOPE_BASE, attrs=["subSchemaSubEntry"])
883 self.assertEquals(len(res), 1)
884 self.assertEquals(res[0]["subSchemaSubEntry"][0], "CN=Aggregate,"+self.schema_dn)
886 res = self.ldb.search(base=self.base_dn, scope=SCOPE_BASE, attrs=["*"])
887 self.assertEquals(len(res), 1)
888 self.assertTrue("subScheamSubEntry" not in res[0])
890 def test_all(self):
891 """Basic tests"""
893 print "Testing user add"
895 ldb.add({
896 "dn": "cn=ldaptestuser,cn=uSers," + self.base_dn,
897 "objectclass": ["user", "person"],
898 "cN": "LDAPtestUSER",
899 "givenname": "ldap",
900 "sn": "testy"})
902 ldb.add({
903 "dn": "cn=ldaptestgroup,cn=uSers," + self.base_dn,
904 "objectclass": "group",
905 "member": "cn=ldaptestuser,cn=useRs," + self.base_dn})
907 ldb.add({
908 "dn": "cn=ldaptestcomputer,cn=computers," + self.base_dn,
909 "objectclass": "computer",
910 "cN": "LDAPtestCOMPUTER"})
912 ldb.add({"dn": "cn=ldaptest2computer,cn=computers," + self.base_dn,
913 "objectClass": "computer",
914 "cn": "LDAPtest2COMPUTER",
915 "userAccountControl": str(UF_WORKSTATION_TRUST_ACCOUNT),
916 "displayname": "ldap testy"})
918 try:
919 ldb.add({"dn": "cn=ldaptestcomputer3,cn=computers," + self.base_dn,
920 "objectClass": "computer",
921 "cn": "LDAPtest2COMPUTER"
923 self.fail()
924 except LdbError, (num, _):
925 self.assertEquals(num, ERR_INVALID_DN_SYNTAX)
927 try:
928 ldb.add({"dn": "cn=ldaptestcomputer3,cn=computers," + self.base_dn,
929 "objectClass": "computer",
930 "cn": "ldaptestcomputer3",
931 "sAMAccountType": str(ATYPE_NORMAL_ACCOUNT)
933 self.fail()
934 except LdbError, (num, _):
935 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
937 ldb.add({"dn": "cn=ldaptestcomputer3,cn=computers," + self.base_dn,
938 "objectClass": "computer",
939 "cn": "LDAPtestCOMPUTER3"
942 print "Testing ldb.search for (&(cn=ldaptestcomputer3)(objectClass=user))";
943 res = ldb.search(self.base_dn, expression="(&(cn=ldaptestcomputer3)(objectClass=user))");
944 self.assertEquals(len(res), 1, "Found only %d for (&(cn=ldaptestcomputer3)(objectClass=user))" % len(res))
946 self.assertEquals(str(res[0].dn), ("CN=ldaptestcomputer3,CN=Computers," + self.base_dn));
947 self.assertEquals(res[0]["cn"][0], "ldaptestcomputer3");
948 self.assertEquals(res[0]["name"][0], "ldaptestcomputer3");
949 self.assertEquals(res[0]["objectClass"][0], "top");
950 self.assertEquals(res[0]["objectClass"][1], "person");
951 self.assertEquals(res[0]["objectClass"][2], "organizationalPerson");
952 self.assertEquals(res[0]["objectClass"][3], "user");
953 self.assertEquals(res[0]["objectClass"][4], "computer");
954 self.assertTrue("objectGUID" in res[0])
955 self.assertTrue("whenCreated" in res[0])
956 self.assertEquals(res[0]["objectCategory"][0], ("CN=Computer,CN=Schema,CN=Configuration," + self.base_dn));
957 self.assertEquals(int(res[0]["primaryGroupID"][0]), 513);
958 self.assertEquals(int(res[0]["sAMAccountType"][0]), ATYPE_NORMAL_ACCOUNT);
959 self.assertEquals(int(res[0]["userAccountControl"][0]), UF_NORMAL_ACCOUNT | UF_PASSWD_NOTREQD | UF_ACCOUNTDISABLE);
961 self.delete_force(self.ldb, "cn=ldaptestcomputer3,cn=computers," + self.base_dn)
963 print "Testing attribute or value exists behaviour"
964 try:
965 ldb.modify_ldif("""
966 dn: cn=ldaptest2computer,cn=computers,""" + self.base_dn + """
967 changetype: modify
968 replace: servicePrincipalName
969 servicePrincipalName: host/ldaptest2computer
970 servicePrincipalName: host/ldaptest2computer
971 servicePrincipalName: cifs/ldaptest2computer
972 """)
973 self.fail()
974 except LdbError, (num, msg):
975 self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
977 ldb.modify_ldif("""
978 dn: cn=ldaptest2computer,cn=computers,""" + self.base_dn + """
979 changetype: modify
980 replace: servicePrincipalName
981 servicePrincipalName: host/ldaptest2computer
982 servicePrincipalName: cifs/ldaptest2computer
983 """)
984 try:
985 ldb.modify_ldif("""
986 dn: cn=ldaptest2computer,cn=computers,""" + self.base_dn + """
987 changetype: modify
988 add: servicePrincipalName
989 servicePrincipalName: host/ldaptest2computer
990 """)
991 self.fail()
992 except LdbError, (num, msg):
993 self.assertEquals(num, ERR_ATTRIBUTE_OR_VALUE_EXISTS)
995 print "Testing ranged results"
996 ldb.modify_ldif("""
997 dn: cn=ldaptest2computer,cn=computers,""" + self.base_dn + """
998 changetype: modify
999 replace: servicePrincipalName
1000 """)
1002 ldb.modify_ldif("""
1003 dn: cn=ldaptest2computer,cn=computers,""" + self.base_dn + """
1004 changetype: modify
1005 add: servicePrincipalName
1006 servicePrincipalName: host/ldaptest2computer0
1007 servicePrincipalName: host/ldaptest2computer1
1008 servicePrincipalName: host/ldaptest2computer2
1009 servicePrincipalName: host/ldaptest2computer3
1010 servicePrincipalName: host/ldaptest2computer4
1011 servicePrincipalName: host/ldaptest2computer5
1012 servicePrincipalName: host/ldaptest2computer6
1013 servicePrincipalName: host/ldaptest2computer7
1014 servicePrincipalName: host/ldaptest2computer8
1015 servicePrincipalName: host/ldaptest2computer9
1016 servicePrincipalName: host/ldaptest2computer10
1017 servicePrincipalName: host/ldaptest2computer11
1018 servicePrincipalName: host/ldaptest2computer12
1019 servicePrincipalName: host/ldaptest2computer13
1020 servicePrincipalName: host/ldaptest2computer14
1021 servicePrincipalName: host/ldaptest2computer15
1022 servicePrincipalName: host/ldaptest2computer16
1023 servicePrincipalName: host/ldaptest2computer17
1024 servicePrincipalName: host/ldaptest2computer18
1025 servicePrincipalName: host/ldaptest2computer19
1026 servicePrincipalName: host/ldaptest2computer20
1027 servicePrincipalName: host/ldaptest2computer21
1028 servicePrincipalName: host/ldaptest2computer22
1029 servicePrincipalName: host/ldaptest2computer23
1030 servicePrincipalName: host/ldaptest2computer24
1031 servicePrincipalName: host/ldaptest2computer25
1032 servicePrincipalName: host/ldaptest2computer26
1033 servicePrincipalName: host/ldaptest2computer27
1034 servicePrincipalName: host/ldaptest2computer28
1035 servicePrincipalName: host/ldaptest2computer29
1036 """)
1038 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE,
1039 attrs=["servicePrincipalName;range=0-*"])
1040 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1041 #print len(res[0]["servicePrincipalName;range=0-*"])
1042 self.assertEquals(len(res[0]["servicePrincipalName;range=0-*"]), 30)
1044 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=0-19"])
1045 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1046 # print res[0]["servicePrincipalName;range=0-19"].length
1047 self.assertEquals(len(res[0]["servicePrincipalName;range=0-19"]), 20)
1050 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=0-30"])
1051 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1052 self.assertEquals(len(res[0]["servicePrincipalName;range=0-*"]), 30)
1054 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=0-40"])
1055 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1056 self.assertEquals(len(res[0]["servicePrincipalName;range=0-*"]), 30)
1058 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=30-40"])
1059 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1060 self.assertEquals(len(res[0]["servicePrincipalName;range=30-*"]), 0)
1063 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=10-40"])
1064 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1065 self.assertEquals(len(res[0]["servicePrincipalName;range=10-*"]), 20)
1066 # pos_11 = res[0]["servicePrincipalName;range=10-*"][18]
1068 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=11-40"])
1069 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1070 self.assertEquals(len(res[0]["servicePrincipalName;range=11-*"]), 19)
1071 # print res[0]["servicePrincipalName;range=11-*"][18]
1072 # print pos_11
1073 # self.assertEquals((res[0]["servicePrincipalName;range=11-*"][18]), pos_11)
1075 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName;range=11-15"])
1076 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1077 self.assertEquals(len(res[0]["servicePrincipalName;range=11-15"]), 5)
1078 # self.assertEquals(res[0]["servicePrincipalName;range=11-15"][4], pos_11)
1080 res = ldb.search(self.base_dn, expression="(cn=ldaptest2computer))", scope=SCOPE_SUBTREE, attrs=["servicePrincipalName"])
1081 self.assertEquals(len(res), 1, "Could not find (cn=ldaptest2computer)")
1082 # print res[0]["servicePrincipalName"][18]
1083 # print pos_11
1084 self.assertEquals(len(res[0]["servicePrincipalName"]), 30)
1085 # self.assertEquals(res[0]["servicePrincipalName"][18], pos_11)
1087 self.delete_force(self.ldb, "cn=ldaptestuser2,cn=users," + self.base_dn)
1088 ldb.add({
1089 "dn": "cn=ldaptestuser2,cn=useRs," + self.base_dn,
1090 "objectClass": ["person", "user"],
1091 "cn": "LDAPtestUSER2",
1092 "givenname": "testy",
1093 "sn": "ldap user2"})
1095 print "Testing Ambigious Name Resolution"
1096 # Testing ldb.search for (&(anr=ldap testy)(objectClass=user))
1097 res = ldb.search(expression="(&(anr=ldap testy)(objectClass=user))")
1098 self.assertEquals(len(res), 3, "Found only %d of 3 for (&(anr=ldap testy)(objectClass=user))" % len(res))
1100 # Testing ldb.search for (&(anr=testy ldap)(objectClass=user))
1101 res = ldb.search(expression="(&(anr=testy ldap)(objectClass=user))")
1102 self.assertEquals(len(res), 2, "Found only %d of 2 for (&(anr=testy ldap)(objectClass=user))" % len(res))
1104 # Testing ldb.search for (&(anr=ldap)(objectClass=user))
1105 res = ldb.search(expression="(&(anr=ldap)(objectClass=user))")
1106 self.assertEquals(len(res), 4, "Found only %d of 4 for (&(anr=ldap)(objectClass=user))" % len(res))
1108 # Testing ldb.search for (&(anr==ldap)(objectClass=user))
1109 res = ldb.search(expression="(&(anr==ldap)(objectClass=user))")
1110 self.assertEquals(len(res), 1, "Could not find (&(anr==ldap)(objectClass=user)). Found only %d for (&(anr=ldap)(objectClass=user))" % len(res))
1112 self.assertEquals(str(res[0].dn), ("CN=ldaptestuser,CN=Users," + self.base_dn))
1113 self.assertEquals(res[0]["cn"][0], "ldaptestuser")
1114 self.assertEquals(str(res[0]["name"]), "ldaptestuser")
1116 # Testing ldb.search for (&(anr=testy)(objectClass=user))
1117 res = ldb.search(expression="(&(anr=testy)(objectClass=user))")
1118 self.assertEquals(len(res), 2, "Found only %d for (&(anr=testy)(objectClass=user))" % len(res))
1120 # Testing ldb.search for (&(anr=testy ldap)(objectClass=user))
1121 res = ldb.search(expression="(&(anr=testy ldap)(objectClass=user))")
1122 self.assertEquals(len(res), 2, "Found only %d for (&(anr=testy ldap)(objectClass=user))" % len(res))
1124 # Testing ldb.search for (&(anr==testy ldap)(objectClass=user))
1125 # this test disabled for the moment, as anr with == tests are not understood
1126 # res = ldb.search(expression="(&(anr==testy ldap)(objectClass=user))")
1127 # self.assertEquals(len(res), 1, "Found only %d for (&(anr==testy ldap)(objectClass=user))" % len(res))
1129 # self.assertEquals(str(res[0].dn), ("CN=ldaptestuser,CN=Users," + self.base_dn))
1130 # self.assertEquals(res[0]["cn"][0], "ldaptestuser")
1131 # self.assertEquals(res[0]["name"][0], "ldaptestuser")
1133 # Testing ldb.search for (&(anr==testy ldap)(objectClass=user))
1134 # res = ldb.search(expression="(&(anr==testy ldap)(objectClass=user))")
1135 # self.assertEquals(len(res), 1, "Could not find (&(anr==testy ldap)(objectClass=user))")
1137 # self.assertEquals(str(res[0].dn), ("CN=ldaptestuser,CN=Users," + self.base_dn))
1138 # self.assertEquals(res[0]["cn"][0], "ldaptestuser")
1139 # self.assertEquals(res[0]["name"][0], "ldaptestuser")
1141 # Testing ldb.search for (&(anr=testy ldap user)(objectClass=user))
1142 res = ldb.search(expression="(&(anr=testy ldap user)(objectClass=user))")
1143 self.assertEquals(len(res), 1, "Could not find (&(anr=testy ldap user)(objectClass=user))")
1145 self.assertEquals(str(res[0].dn), ("CN=ldaptestuser2,CN=Users," + self.base_dn))
1146 self.assertEquals(str(res[0]["cn"]), "ldaptestuser2")
1147 self.assertEquals(str(res[0]["name"]), "ldaptestuser2")
1149 # Testing ldb.search for (&(anr==testy ldap user2)(objectClass=user))
1150 # res = ldb.search(expression="(&(anr==testy ldap user2)(objectClass=user))")
1151 # self.assertEquals(len(res), 1, "Could not find (&(anr==testy ldap user2)(objectClass=user))")
1153 self.assertEquals(str(res[0].dn), ("CN=ldaptestuser2,CN=Users," + self.base_dn))
1154 self.assertEquals(str(res[0]["cn"]), "ldaptestuser2")
1155 self.assertEquals(str(res[0]["name"]), "ldaptestuser2")
1157 # Testing ldb.search for (&(anr==ldap user2)(objectClass=user))
1158 # res = ldb.search(expression="(&(anr==ldap user2)(objectClass=user))")
1159 # self.assertEquals(len(res), 1, "Could not find (&(anr==ldap user2)(objectClass=user))")
1161 self.assertEquals(str(res[0].dn), ("CN=ldaptestuser2,CN=Users," + self.base_dn))
1162 self.assertEquals(str(res[0]["cn"]), "ldaptestuser2")
1163 self.assertEquals(str(res[0]["name"]), "ldaptestuser2")
1165 # Testing ldb.search for (&(anr==not ldap user2)(objectClass=user))
1166 # res = ldb.search(expression="(&(anr==not ldap user2)(objectClass=user))")
1167 # self.assertEquals(len(res), 0, "Must not find (&(anr==not ldap user2)(objectClass=user))")
1169 # Testing ldb.search for (&(anr=not ldap user2)(objectClass=user))
1170 res = ldb.search(expression="(&(anr=not ldap user2)(objectClass=user))")
1171 self.assertEquals(len(res), 0, "Must not find (&(anr=not ldap user2)(objectClass=user))")
1173 # Testing ldb.search for (&(anr="testy ldap")(objectClass=user)) (ie, with quotes)
1174 # res = ldb.search(expression="(&(anr==\"testy ldap\")(objectClass=user))")
1175 # self.assertEquals(len(res), 0, "Found (&(anr==\"testy ldap\")(objectClass=user))")
1177 print "Testing Renames"
1179 attrs = ["objectGUID", "objectSid"]
1180 print "Testing ldb.search for (&(cn=ldaptestUSer2)(objectClass=user))"
1181 res_user = ldb.search(self.base_dn, expression="(&(cn=ldaptestUSer2)(objectClass=user))", scope=SCOPE_SUBTREE, attrs=attrs)
1182 self.assertEquals(len(res_user), 1, "Could not find (&(cn=ldaptestUSer2)(objectClass=user))")
1184 # Check rename works with extended/alternate DN forms
1185 ldb.rename("<SID=" + ldb.schema_format_value("objectSID", res_user[0]["objectSID"][0]) + ">" , "cn=ldaptestUSER3,cn=users," + self.base_dn)
1187 print "Testing ldb.search for (&(cn=ldaptestuser3)(objectClass=user))"
1188 res = ldb.search(expression="(&(cn=ldaptestuser3)(objectClass=user))")
1189 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestuser3)(objectClass=user))")
1191 self.assertEquals(str(res[0].dn), ("CN=ldaptestUSER3,CN=Users," + self.base_dn))
1192 self.assertEquals(str(res[0]["cn"]), "ldaptestUSER3")
1193 self.assertEquals(str(res[0]["name"]), "ldaptestUSER3")
1195 #"Testing ldb.search for (&(&(cn=ldaptestuser3)(userAccountControl=*))(objectClass=user))"
1196 res = ldb.search(expression="(&(&(cn=ldaptestuser3)(userAccountControl=*))(objectClass=user))")
1197 self.assertEquals(len(res), 1, "(&(&(cn=ldaptestuser3)(userAccountControl=*))(objectClass=user))")
1199 self.assertEquals(str(res[0].dn), ("CN=ldaptestUSER3,CN=Users," + self.base_dn))
1200 self.assertEquals(str(res[0]["cn"]), "ldaptestUSER3")
1201 self.assertEquals(str(res[0]["name"]), "ldaptestUSER3")
1203 #"Testing ldb.search for (&(&(cn=ldaptestuser3)(userAccountControl=546))(objectClass=user))"
1204 res = ldb.search(expression="(&(&(cn=ldaptestuser3)(userAccountControl=546))(objectClass=user))")
1205 self.assertEquals(len(res), 1, "(&(&(cn=ldaptestuser3)(userAccountControl=546))(objectClass=user))")
1207 self.assertEquals(str(res[0].dn), ("CN=ldaptestUSER3,CN=Users," + self.base_dn))
1208 self.assertEquals(str(res[0]["cn"]), "ldaptestUSER3")
1209 self.assertEquals(str(res[0]["name"]), "ldaptestUSER3")
1211 #"Testing ldb.search for (&(&(cn=ldaptestuser3)(userAccountControl=547))(objectClass=user))"
1212 res = ldb.search(expression="(&(&(cn=ldaptestuser3)(userAccountControl=547))(objectClass=user))")
1213 self.assertEquals(len(res), 0, "(&(&(cn=ldaptestuser3)(userAccountControl=547))(objectClass=user))")
1215 # This is a Samba special, and does not exist in real AD
1216 # print "Testing ldb.search for (dn=CN=ldaptestUSER3,CN=Users," + self.base_dn + ")"
1217 # res = ldb.search("(dn=CN=ldaptestUSER3,CN=Users," + self.base_dn + ")")
1218 # if (res.error != 0 || len(res) != 1) {
1219 # print "Could not find (dn=CN=ldaptestUSER3,CN=Users," + self.base_dn + ")"
1220 # self.assertEquals(len(res), 1)
1222 # self.assertEquals(res[0].dn, ("CN=ldaptestUSER3,CN=Users," + self.base_dn))
1223 # self.assertEquals(res[0].cn, "ldaptestUSER3")
1224 # self.assertEquals(res[0].name, "ldaptestUSER3")
1226 print "Testing ldb.search for (distinguishedName=CN=ldaptestUSER3,CN=Users," + self.base_dn + ")"
1227 res = ldb.search(expression="(distinguishedName=CN=ldaptestUSER3,CN=Users," + self.base_dn + ")")
1228 self.assertEquals(len(res), 1, "Could not find (dn=CN=ldaptestUSER3,CN=Users," + self.base_dn + ")")
1229 self.assertEquals(str(res[0].dn), ("CN=ldaptestUSER3,CN=Users," + self.base_dn))
1230 self.assertEquals(str(res[0]["cn"]), "ldaptestUSER3")
1231 self.assertEquals(str(res[0]["name"]), "ldaptestUSER3")
1233 # ensure we cannot add it again
1234 try:
1235 ldb.add({"dn": "cn=ldaptestuser3,cn=userS," + self.base_dn,
1236 "objectClass": ["person", "user"],
1237 "cn": "LDAPtestUSER3"})
1238 self.fail()
1239 except LdbError, (num, _):
1240 self.assertEquals(num, ERR_ENTRY_ALREADY_EXISTS)
1242 # rename back
1243 ldb.rename("cn=ldaptestuser3,cn=users," + self.base_dn, "cn=ldaptestuser2,cn=users," + self.base_dn)
1245 # ensure we cannot rename it twice
1246 try:
1247 ldb.rename("cn=ldaptestuser3,cn=users," + self.base_dn,
1248 "cn=ldaptestuser2,cn=users," + self.base_dn)
1249 self.fail()
1250 except LdbError, (num, _):
1251 self.assertEquals(num, ERR_NO_SUCH_OBJECT)
1253 # ensure can now use that name
1254 ldb.add({"dn": "cn=ldaptestuser3,cn=users," + self.base_dn,
1255 "objectClass": ["person", "user"],
1256 "cn": "LDAPtestUSER3"})
1258 # ensure we now cannot rename
1259 try:
1260 ldb.rename("cn=ldaptestuser2,cn=users," + self.base_dn, "cn=ldaptestuser3,cn=users," + self.base_dn)
1261 self.fail()
1262 except LdbError, (num, _):
1263 self.assertEquals(num, ERR_ENTRY_ALREADY_EXISTS)
1264 try:
1265 ldb.rename("cn=ldaptestuser3,cn=users," + self.base_dn, "cn=ldaptestuser3,cn=configuration," + self.base_dn)
1266 self.fail()
1267 except LdbError, (num, _):
1268 self.assertTrue(num in (71, 64))
1270 ldb.rename("cn=ldaptestuser3,cn=users," + self.base_dn, "cn=ldaptestuser5,cn=users," + self.base_dn)
1272 ldb.delete("cn=ldaptestuser5,cn=users," + self.base_dn)
1274 self.delete_force(ldb, "cn=ldaptestgroup2,cn=users," + self.base_dn)
1276 ldb.rename("cn=ldaptestgroup,cn=users," + self.base_dn, "cn=ldaptestgroup2,cn=users," + self.base_dn)
1278 print "Testing subtree renames"
1280 ldb.add({"dn": "cn=ldaptestcontainer," + self.base_dn,
1281 "objectClass": "container"})
1283 ldb.add({"dn": "CN=ldaptestuser4,CN=ldaptestcontainer," + self.base_dn,
1284 "objectClass": ["person", "user"],
1285 "cn": "LDAPtestUSER4"})
1287 ldb.modify_ldif("""
1288 dn: cn=ldaptestgroup2,cn=users,""" + self.base_dn + """
1289 changetype: modify
1290 add: member
1291 member: cn=ldaptestuser4,cn=ldaptestcontainer,""" + self.base_dn + """
1292 member: cn=ldaptestcomputer,cn=computers,""" + self.base_dn + """
1293 member: cn=ldaptestuser2,cn=users,""" + self.base_dn + """
1294 """)
1296 print "Testing ldb.rename of cn=ldaptestcontainer," + self.base_dn + " to cn=ldaptestcontainer2," + self.base_dn
1297 ldb.rename("CN=ldaptestcontainer," + self.base_dn, "CN=ldaptestcontainer2," + self.base_dn)
1299 print "Testing ldb.search for (&(cn=ldaptestuser4)(objectClass=user))"
1300 res = ldb.search(expression="(&(cn=ldaptestuser4)(objectClass=user))")
1301 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestuser4)(objectClass=user))")
1303 print "Testing subtree ldb.search for (&(cn=ldaptestuser4)(objectClass=user)) in (just renamed from) cn=ldaptestcontainer," + self.base_dn
1304 try:
1305 res = ldb.search("cn=ldaptestcontainer," + self.base_dn,
1306 expression="(&(cn=ldaptestuser4)(objectClass=user))",
1307 scope=SCOPE_SUBTREE)
1308 self.fail(res)
1309 except LdbError, (num, _):
1310 self.assertEquals(num, ERR_NO_SUCH_OBJECT)
1312 print "Testing one-level ldb.search for (&(cn=ldaptestuser4)(objectClass=user)) in (just renamed from) cn=ldaptestcontainer," + self.base_dn
1313 try:
1314 res = ldb.search("cn=ldaptestcontainer," + self.base_dn,
1315 expression="(&(cn=ldaptestuser4)(objectClass=user))", scope=SCOPE_ONELEVEL)
1316 self.fail()
1317 except LdbError, (num, _):
1318 self.assertEquals(num, ERR_NO_SUCH_OBJECT)
1320 print "Testing ldb.search for (&(cn=ldaptestuser4)(objectClass=user)) in renamed container"
1321 res = ldb.search("cn=ldaptestcontainer2," + self.base_dn, expression="(&(cn=ldaptestuser4)(objectClass=user))", scope=SCOPE_SUBTREE)
1322 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestuser4)(objectClass=user)) under cn=ldaptestcontainer2," + self.base_dn)
1324 self.assertEquals(str(res[0].dn), ("CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn))
1325 self.assertEquals(res[0]["memberOf"][0].upper(), ("CN=ldaptestgroup2,CN=Users," + self.base_dn).upper())
1327 time.sleep(4)
1329 print "Testing ldb.search for (&(member=CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn + ")(objectclass=group)) to check subtree renames and linked attributes"
1330 res = ldb.search(self.base_dn, expression="(&(member=CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn + ")(objectclass=group))", scope=SCOPE_SUBTREE)
1331 self.assertEquals(len(res), 1, "Could not find (&(member=CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn + ")(objectclass=group)), perhaps linked attributes are not consistant with subtree renames?")
1333 print "Testing ldb.rename (into itself) of cn=ldaptestcontainer2," + self.base_dn + " to cn=ldaptestcontainer,cn=ldaptestcontainer2," + self.base_dn
1334 try:
1335 ldb.rename("cn=ldaptestcontainer2," + self.base_dn, "cn=ldaptestcontainer,cn=ldaptestcontainer2," + self.base_dn)
1336 self.fail()
1337 except LdbError, (num, _):
1338 self.assertEquals(num, ERR_UNWILLING_TO_PERFORM)
1340 print "Testing ldb.rename (into non-existent container) of cn=ldaptestcontainer2," + self.base_dn + " to cn=ldaptestcontainer,cn=ldaptestcontainer3," + self.base_dn
1341 try:
1342 ldb.rename("cn=ldaptestcontainer2," + self.base_dn, "cn=ldaptestcontainer,cn=ldaptestcontainer3," + self.base_dn)
1343 self.fail()
1344 except LdbError, (num, _):
1345 self.assertTrue(num in (ERR_UNWILLING_TO_PERFORM, ERR_OTHER))
1347 print "Testing delete (should fail, not a leaf node) of renamed cn=ldaptestcontainer2," + self.base_dn
1348 try:
1349 ldb.delete("cn=ldaptestcontainer2," + self.base_dn)
1350 self.fail()
1351 except LdbError, (num, _):
1352 self.assertEquals(num, ERR_NOT_ALLOWED_ON_NON_LEAF)
1354 print "Testing base ldb.search for CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn
1355 res = ldb.search(expression="(objectclass=*)", base=("CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn), scope=SCOPE_BASE)
1356 self.assertEquals(len(res), 1)
1357 res = ldb.search(expression="(cn=ldaptestuser40)", base=("CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn), scope=SCOPE_BASE)
1358 self.assertEquals(len(res), 0)
1360 print "Testing one-level ldb.search for (&(cn=ldaptestuser4)(objectClass=user)) in cn=ldaptestcontainer2," + self.base_dn
1361 res = ldb.search(expression="(&(cn=ldaptestuser4)(objectClass=user))", base=("cn=ldaptestcontainer2," + self.base_dn), scope=SCOPE_ONELEVEL)
1362 # FIXME: self.assertEquals(len(res), 0)
1364 print "Testing one-level ldb.search for (&(cn=ldaptestuser4)(objectClass=user)) in cn=ldaptestcontainer2," + self.base_dn
1365 res = ldb.search(expression="(&(cn=ldaptestuser4)(objectClass=user))", base=("cn=ldaptestcontainer2," + self.base_dn), scope=SCOPE_SUBTREE)
1366 # FIXME: self.assertEquals(len(res), 0)
1368 print "Testing delete of subtree renamed "+("CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn)
1369 ldb.delete(("CN=ldaptestuser4,CN=ldaptestcontainer2," + self.base_dn))
1370 print "Testing delete of renamed cn=ldaptestcontainer2," + self.base_dn
1371 ldb.delete("cn=ldaptestcontainer2," + self.base_dn)
1373 ldb.add({"dn": "cn=ldaptestutf8user èùéìòà,cn=users," + self.base_dn, "objectClass": "user"})
1375 ldb.add({"dn": "cn=ldaptestutf8user2 èùéìòà,cn=users," + self.base_dn, "objectClass": "user"})
1377 print "Testing ldb.search for (&(cn=ldaptestuser)(objectClass=user))"
1378 res = ldb.search(expression="(&(cn=ldaptestuser)(objectClass=user))")
1379 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestuser)(objectClass=user))")
1381 self.assertEquals(str(res[0].dn), ("CN=ldaptestuser,CN=Users," + self.base_dn))
1382 self.assertEquals(str(res[0]["cn"]), "ldaptestuser")
1383 self.assertEquals(str(res[0]["name"]), "ldaptestuser")
1384 self.assertEquals(set(res[0]["objectClass"]), set(["top", "person", "organizationalPerson", "user"]))
1385 self.assertTrue("objectGUID" in res[0])
1386 self.assertTrue("whenCreated" in res[0])
1387 self.assertEquals(str(res[0]["objectCategory"]), ("CN=Person,CN=Schema,CN=Configuration," + self.base_dn))
1388 self.assertEquals(int(res[0]["sAMAccountType"][0]), ATYPE_NORMAL_ACCOUNT)
1389 self.assertEquals(int(res[0]["userAccountControl"][0]), UF_NORMAL_ACCOUNT | UF_PASSWD_NOTREQD | UF_ACCOUNTDISABLE)
1390 self.assertEquals(res[0]["memberOf"][0].upper(), ("CN=ldaptestgroup2,CN=Users," + self.base_dn).upper())
1391 self.assertEquals(len(res[0]["memberOf"]), 1)
1393 print "Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=cn=person,cn=schema,cn=configuration," + self.base_dn + "))"
1394 res2 = ldb.search(expression="(&(cn=ldaptestuser)(objectCategory=cn=person,cn=schema,cn=configuration," + self.base_dn + "))")
1395 self.assertEquals(len(res2), 1, "Could not find (&(cn=ldaptestuser)(objectCategory=cn=person,cn=schema,cn=configuration," + self.base_dn + "))")
1397 self.assertEquals(res[0].dn, res2[0].dn)
1399 print "Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=PerSon))"
1400 res3 = ldb.search(expression="(&(cn=ldaptestuser)(objectCategory=PerSon))")
1401 self.assertEquals(len(res3), 1, "Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)): matched %d" % len(res3))
1403 self.assertEquals(res[0].dn, res3[0].dn)
1405 if gc_ldb is not None:
1406 print "Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=PerSon)) in Global Catalog"
1407 res3gc = gc_ldb.search(expression="(&(cn=ldaptestuser)(objectCategory=PerSon))")
1408 self.assertEquals(len(res3gc), 1)
1410 self.assertEquals(res[0].dn, res3gc[0].dn)
1412 print "Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=PerSon)) in with 'phantom root' control"
1414 if gc_ldb is not None:
1415 res3control = gc_ldb.search(self.base_dn, expression="(&(cn=ldaptestuser)(objectCategory=PerSon))", scope=SCOPE_SUBTREE, attrs=["cn"], controls=["search_options:1:2"])
1416 self.assertEquals(len(res3control), 1, "Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)) in Global Catalog")
1418 self.assertEquals(res[0].dn, res3control[0].dn)
1420 ldb.delete(res[0].dn)
1422 print "Testing ldb.search for (&(cn=ldaptestcomputer)(objectClass=user))"
1423 res = ldb.search(expression="(&(cn=ldaptestcomputer)(objectClass=user))")
1424 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestuser)(objectClass=user))")
1426 self.assertEquals(str(res[0].dn), ("CN=ldaptestcomputer,CN=Computers," + self.base_dn))
1427 self.assertEquals(str(res[0]["cn"]), "ldaptestcomputer")
1428 self.assertEquals(str(res[0]["name"]), "ldaptestcomputer")
1429 self.assertEquals(set(res[0]["objectClass"]), set(["top", "person", "organizationalPerson", "user", "computer"]))
1430 self.assertTrue("objectGUID" in res[0])
1431 self.assertTrue("whenCreated" in res[0])
1432 self.assertEquals(str(res[0]["objectCategory"]), ("CN=Computer,CN=Schema,CN=Configuration," + self.base_dn))
1433 self.assertEquals(int(res[0]["primaryGroupID"][0]), 513)
1434 self.assertEquals(int(res[0]["sAMAccountType"][0]), ATYPE_NORMAL_ACCOUNT)
1435 self.assertEquals(int(res[0]["userAccountControl"][0]), UF_NORMAL_ACCOUNT | UF_PASSWD_NOTREQD | UF_ACCOUNTDISABLE)
1436 self.assertEquals(res[0]["memberOf"][0].upper(), ("CN=ldaptestgroup2,CN=Users," + self.base_dn).upper())
1437 self.assertEquals(len(res[0]["memberOf"]), 1)
1439 print "Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + self.base_dn + "))"
1440 res2 = ldb.search(expression="(&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + self.base_dn + "))")
1441 self.assertEquals(len(res2), 1, "Could not find (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + self.base_dn + "))")
1443 self.assertEquals(res[0].dn, res2[0].dn)
1445 if gc_ldb is not None:
1446 print "Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + self.base_dn + ")) in Global Catlog"
1447 res2gc = gc_ldb.search(expression="(&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + self.base_dn + "))")
1448 self.assertEquals(len(res2gc), 1, "Could not find (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + self.base_dn + ")) in Global Catlog")
1450 self.assertEquals(res[0].dn, res2gc[0].dn)
1452 print "Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=compuTER))"
1453 res3 = ldb.search(expression="(&(cn=ldaptestcomputer)(objectCategory=compuTER))")
1454 self.assertEquals(len(res3), 1, "Could not find (&(cn=ldaptestcomputer)(objectCategory=compuTER))")
1456 self.assertEquals(res[0].dn, res3[0].dn)
1458 if gc_ldb is not None:
1459 print "Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=compuTER)) in Global Catalog"
1460 res3gc = gc_ldb.search(expression="(&(cn=ldaptestcomputer)(objectCategory=compuTER))")
1461 self.assertEquals(len(res3gc), 1, "Could not find (&(cn=ldaptestcomputer)(objectCategory=compuTER)) in Global Catalog")
1463 self.assertEquals(res[0].dn, res3gc[0].dn)
1465 print "Testing ldb.search for (&(cn=ldaptestcomp*r)(objectCategory=compuTER))"
1466 res4 = ldb.search(expression="(&(cn=ldaptestcomp*r)(objectCategory=compuTER))")
1467 self.assertEquals(len(res4), 1, "Could not find (&(cn=ldaptestcomp*r)(objectCategory=compuTER))")
1469 self.assertEquals(res[0].dn, res4[0].dn)
1471 print "Testing ldb.search for (&(cn=ldaptestcomput*)(objectCategory=compuTER))"
1472 res5 = ldb.search(expression="(&(cn=ldaptestcomput*)(objectCategory=compuTER))")
1473 self.assertEquals(len(res5), 1, "Could not find (&(cn=ldaptestcomput*)(objectCategory=compuTER))")
1475 self.assertEquals(res[0].dn, res5[0].dn)
1477 print "Testing ldb.search for (&(cn=*daptestcomputer)(objectCategory=compuTER))"
1478 res6 = ldb.search(expression="(&(cn=*daptestcomputer)(objectCategory=compuTER))")
1479 self.assertEquals(len(res6), 1, "Could not find (&(cn=*daptestcomputer)(objectCategory=compuTER))")
1481 self.assertEquals(res[0].dn, res6[0].dn)
1483 ldb.delete("<GUID=" + ldb.schema_format_value("objectGUID", res[0]["objectGUID"][0]) + ">")
1485 print "Testing ldb.search for (&(cn=ldaptest2computer)(objectClass=user))"
1486 res = ldb.search(expression="(&(cn=ldaptest2computer)(objectClass=user))")
1487 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptest2computer)(objectClass=user))")
1489 self.assertEquals(str(res[0].dn), "CN=ldaptest2computer,CN=Computers," + self.base_dn)
1490 self.assertEquals(str(res[0]["cn"]), "ldaptest2computer")
1491 self.assertEquals(str(res[0]["name"]), "ldaptest2computer")
1492 self.assertEquals(list(res[0]["objectClass"]), ["top", "person", "organizationalPerson", "user", "computer"])
1493 self.assertTrue("objectGUID" in res[0])
1494 self.assertTrue("whenCreated" in res[0])
1495 self.assertEquals(res[0]["objectCategory"][0], "CN=Computer,CN=Schema,CN=Configuration," + self.base_dn)
1496 self.assertEquals(int(res[0]["sAMAccountType"][0]), ATYPE_WORKSTATION_TRUST)
1497 self.assertEquals(int(res[0]["userAccountControl"][0]), UF_WORKSTATION_TRUST_ACCOUNT)
1499 ldb.delete("<SID=" + ldb.schema_format_value("objectSID", res[0]["objectSID"][0]) + ">")
1501 attrs = ["cn", "name", "objectClass", "objectGUID", "objectSID", "whenCreated", "nTSecurityDescriptor", "memberOf", "allowedAttributes", "allowedAttributesEffective"]
1502 print "Testing ldb.search for (&(cn=ldaptestUSer2)(objectClass=user))"
1503 res_user = ldb.search(self.base_dn, expression="(&(cn=ldaptestUSer2)(objectClass=user))", scope=SCOPE_SUBTREE, attrs=attrs)
1504 self.assertEquals(len(res_user), 1, "Could not find (&(cn=ldaptestUSer2)(objectClass=user))")
1506 self.assertEquals(str(res_user[0].dn), ("CN=ldaptestuser2,CN=Users," + self.base_dn))
1507 self.assertEquals(str(res_user[0]["cn"]), "ldaptestuser2")
1508 self.assertEquals(str(res_user[0]["name"]), "ldaptestuser2")
1509 self.assertEquals(list(res_user[0]["objectClass"]), ["top", "person", "organizationalPerson", "user"])
1510 self.assertTrue("objectSid" in res_user[0])
1511 self.assertTrue("objectGUID" in res_user[0])
1512 self.assertTrue("whenCreated" in res_user[0])
1513 self.assertTrue("nTSecurityDescriptor" in res_user[0])
1514 self.assertTrue("allowedAttributes" in res_user[0])
1515 self.assertTrue("allowedAttributesEffective" in res_user[0])
1516 self.assertEquals(res_user[0]["memberOf"][0].upper(), ("CN=ldaptestgroup2,CN=Users," + self.base_dn).upper())
1518 ldaptestuser2_sid = res_user[0]["objectSid"][0]
1519 ldaptestuser2_guid = res_user[0]["objectGUID"][0]
1521 attrs = ["cn", "name", "objectClass", "objectGUID", "objectSID", "whenCreated", "nTSecurityDescriptor", "member", "allowedAttributes", "allowedAttributesEffective"]
1522 print "Testing ldb.search for (&(cn=ldaptestgroup2)(objectClass=group))"
1523 res = ldb.search(self.base_dn, expression="(&(cn=ldaptestgroup2)(objectClass=group))", scope=SCOPE_SUBTREE, attrs=attrs)
1524 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestgroup2)(objectClass=group))")
1526 self.assertEquals(str(res[0].dn), ("CN=ldaptestgroup2,CN=Users," + self.base_dn))
1527 self.assertEquals(str(res[0]["cn"]), "ldaptestgroup2")
1528 self.assertEquals(str(res[0]["name"]), "ldaptestgroup2")
1529 self.assertEquals(list(res[0]["objectClass"]), ["top", "group"])
1530 self.assertTrue("objectGUID" in res[0])
1531 self.assertTrue("objectSid" in res[0])
1532 self.assertTrue("whenCreated" in res[0])
1533 self.assertTrue("nTSecurityDescriptor" in res[0])
1534 self.assertTrue("allowedAttributes" in res[0])
1535 self.assertTrue("allowedAttributesEffective" in res[0])
1536 memberUP = []
1537 for m in res[0]["member"]:
1538 memberUP.append(m.upper())
1539 self.assertTrue(("CN=ldaptestuser2,CN=Users," + self.base_dn).upper() in memberUP)
1541 res = ldb.search(self.base_dn, expression="(&(cn=ldaptestgroup2)(objectClass=group))", scope=SCOPE_SUBTREE, attrs=attrs, controls=["extended_dn:1:1"])
1542 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestgroup2)(objectClass=group))")
1544 print res[0]["member"]
1545 memberUP = []
1546 for m in res[0]["member"]:
1547 memberUP.append(m.upper())
1548 print ("<GUID=" + ldb.schema_format_value("objectGUID", ldaptestuser2_guid) + ">;<SID=" + ldb.schema_format_value("objectSid", ldaptestuser2_sid) + ">;CN=ldaptestuser2,CN=Users," + self.base_dn).upper()
1550 self.assertTrue(("<GUID=" + ldb.schema_format_value("objectGUID", ldaptestuser2_guid) + ">;<SID=" + ldb.schema_format_value("objectSid", ldaptestuser2_sid) + ">;CN=ldaptestuser2,CN=Users," + self.base_dn).upper() in memberUP)
1552 print "Testing Linked attribute behaviours"
1553 ldb.modify_ldif("""
1554 dn: cn=ldaptestgroup2,cn=users,""" + self.base_dn + """
1555 changetype: modify
1556 replace: member
1557 member: CN=ldaptestuser2,CN=Users,""" + self.base_dn + """
1558 member: CN=ldaptestutf8user èùéìòà,CN=Users,""" + self.base_dn + """
1559 """)
1561 ldb.modify_ldif("""
1562 dn: <GUID=""" + ldb.schema_format_value("objectGUID", res[0]["objectGUID"][0]) + """>
1563 changetype: modify
1564 replace: member
1565 member: CN=ldaptestutf8user èùéìòà,CN=Users,""" + self.base_dn + """
1566 """)
1568 ldb.modify_ldif("""
1569 dn: <SID=""" + ldb.schema_format_value("objectSid", res[0]["objectSid"][0]) + """>
1570 changetype: modify
1571 delete: member
1572 """)
1574 ldb.modify_ldif("""
1575 dn: cn=ldaptestgroup2,cn=users,""" + self.base_dn + """
1576 changetype: modify
1577 add: member
1578 member: <GUID=""" + ldb.schema_format_value("objectGUID", res[0]["objectGUID"][0]) + """>
1579 member: CN=ldaptestutf8user èùéìòà,CN=Users,""" + self.base_dn + """
1580 """)
1582 ldb.modify_ldif("""
1583 dn: cn=ldaptestgroup2,cn=users,""" + self.base_dn + """
1584 changetype: modify
1585 replace: member
1586 """)
1588 ldb.modify_ldif("""
1589 dn: cn=ldaptestgroup2,cn=users,""" + self.base_dn + """
1590 changetype: modify
1591 add: member
1592 member: <SID=""" + ldb.schema_format_value("objectSid", res_user[0]["objectSid"][0]) + """>
1593 member: CN=ldaptestutf8user èùéìòà,CN=Users,""" + self.base_dn + """
1594 """)
1596 ldb.modify_ldif("""
1597 dn: cn=ldaptestgroup2,cn=users,""" + self.base_dn + """
1598 changetype: modify
1599 delete: member
1600 member: CN=ldaptestutf8user èùéìòà,CN=Users,""" + self.base_dn + """
1601 """)
1603 res = ldb.search(self.base_dn, expression="(&(cn=ldaptestgroup2)(objectClass=group))", scope=SCOPE_SUBTREE, attrs=attrs)
1604 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestgroup2)(objectClass=group))")
1606 self.assertEquals(str(res[0].dn), ("CN=ldaptestgroup2,CN=Users," + self.base_dn))
1607 self.assertEquals(res[0]["member"][0], ("CN=ldaptestuser2,CN=Users," + self.base_dn))
1608 self.assertEquals(len(res[0]["member"]), 1)
1610 ldb.delete(("CN=ldaptestuser2,CN=Users," + self.base_dn))
1612 time.sleep(4)
1614 attrs = ["cn", "name", "objectClass", "objectGUID", "whenCreated", "nTSecurityDescriptor", "member"]
1615 print "Testing ldb.search for (&(cn=ldaptestgroup2)(objectClass=group)) to check linked delete"
1616 res = ldb.search(self.base_dn, expression="(&(cn=ldaptestgroup2)(objectClass=group))", scope=SCOPE_SUBTREE, attrs=attrs)
1617 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestgroup2)(objectClass=group)) to check linked delete")
1619 self.assertEquals(str(res[0].dn), ("CN=ldaptestgroup2,CN=Users," + self.base_dn))
1620 self.assertTrue("member" not in res[0])
1622 print "Testing ldb.search for (&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))"
1623 # TODO UTF8 users don't seem to work fully anymore
1624 # res = ldb.search(expression="(&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))")
1625 res = ldb.search(expression="(&(cn=ldaptestutf8user èùéìòà)(objectclass=user))")
1626 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))")
1628 self.assertEquals(str(res[0].dn), ("CN=ldaptestutf8user èùéìòà,CN=Users," + self.base_dn))
1629 self.assertEquals(str(res[0]["cn"]), "ldaptestutf8user èùéìòà")
1630 self.assertEquals(str(res[0]["name"]), "ldaptestutf8user èùéìòà")
1631 self.assertEquals(list(res[0]["objectClass"]), ["top", "person", "organizationalPerson", "user"])
1632 self.assertTrue("objectGUID" in res[0])
1633 self.assertTrue("whenCreated" in res[0])
1635 ldb.delete(res[0].dn)
1637 print "Testing ldb.search for (&(cn=ldaptestutf8user2*)(objectClass=user))"
1638 res = ldb.search(expression="(&(cn=ldaptestutf8user2*)(objectClass=user))")
1639 self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestutf8user2*)(objectClass=user))")
1641 ldb.delete(res[0].dn)
1643 ldb.delete(("CN=ldaptestgroup2,CN=Users," + self.base_dn))
1645 print "Testing ldb.search for (&(cn=ldaptestutf8user2 ÈÙÉÌÒÀ)(objectClass=user))"
1646 # TODO UTF8 users don't seem to work fully anymore
1647 # res = ldb.search(expression="(&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))")
1648 # self.assertEquals(len(res), 1, "Could not find (&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))")
1650 print "Testing that we can't get at the configuration DN from the main search base"
1651 res = ldb.search(self.base_dn, expression="objectClass=crossRef", scope=SCOPE_SUBTREE, attrs=["cn"])
1652 self.assertEquals(len(res), 0)
1654 print "Testing that we can get at the configuration DN from the main search base on the LDAP port with the 'phantom root' search_options control"
1655 res = ldb.search(self.base_dn, expression="objectClass=crossRef", scope=SCOPE_SUBTREE, attrs=["cn"], controls=["search_options:1:2"])
1656 self.assertTrue(len(res) > 0)
1658 if gc_ldb is not None:
1659 print "Testing that we can get at the configuration DN from the main search base on the GC port with the search_options control == 0"
1661 res = gc_ldb.search(self.base_dn, expression="objectClass=crossRef", scope=SCOPE_SUBTREE, attrs=["cn"], controls=["search_options:1:0"])
1662 self.assertTrue(len(res) > 0)
1664 print "Testing that we do find configuration elements in the global catlog"
1665 res = gc_ldb.search(self.base_dn, expression="objectClass=crossRef", scope=SCOPE_SUBTREE, attrs=["cn"])
1666 self.assertTrue(len(res) > 0)
1668 print "Testing that we do find configuration elements and user elements at the same time"
1669 res = gc_ldb.search(self.base_dn, expression="(|(objectClass=crossRef)(objectClass=person))", scope=SCOPE_SUBTREE, attrs=["cn"])
1670 self.assertTrue(len(res) > 0)
1672 print "Testing that we do find configuration elements in the global catlog, with the configuration basedn"
1673 res = gc_ldb.search(self.configuration_dn, expression="objectClass=crossRef", scope=SCOPE_SUBTREE, attrs=["cn"])
1674 self.assertTrue(len(res) > 0)
1676 print "Testing that we can get at the configuration DN on the main LDAP port"
1677 res = ldb.search(self.configuration_dn, expression="objectClass=crossRef", scope=SCOPE_SUBTREE, attrs=["cn"])
1678 self.assertTrue(len(res) > 0)
1680 print "Testing objectCategory canonacolisation"
1681 res = ldb.search(self.configuration_dn, expression="objectCategory=ntDsDSA", scope=SCOPE_SUBTREE, attrs=["cn"])
1682 self.assertTrue(len(res) > 0, "Didn't find any records with objectCategory=ntDsDSA")
1683 self.assertTrue(len(res) != 0)
1685 res = ldb.search(self.configuration_dn, expression="objectCategory=CN=ntDs-DSA," + self.schema_dn, scope=SCOPE_SUBTREE, attrs=["cn"])
1686 self.assertTrue(len(res) > 0, "Didn't find any records with objectCategory=CN=ntDs-DSA," + self.schema_dn)
1687 self.assertTrue(len(res) != 0)
1689 print "Testing objectClass attribute order on "+ self.base_dn
1690 res = ldb.search(expression="objectClass=domain", base=self.base_dn,
1691 scope=SCOPE_BASE, attrs=["objectClass"])
1692 self.assertEquals(len(res), 1)
1694 self.assertEquals(list(res[0]["objectClass"]), ["top", "domain", "domainDNS"])
1696 # check enumeration
1698 print "Testing ldb.search for objectCategory=person"
1699 res = ldb.search(self.base_dn, expression="objectCategory=person", scope=SCOPE_SUBTREE, attrs=["cn"])
1700 self.assertTrue(len(res) > 0)
1702 print "Testing ldb.search for objectCategory=person with domain scope control"
1703 res = ldb.search(self.base_dn, expression="objectCategory=person", scope=SCOPE_SUBTREE, attrs=["cn"], controls=["domain_scope:1"])
1704 self.assertTrue(len(res) > 0)
1706 print "Testing ldb.search for objectCategory=user"
1707 res = ldb.search(self.base_dn, expression="objectCategory=user", scope=SCOPE_SUBTREE, attrs=["cn"])
1708 self.assertTrue(len(res) > 0)
1710 print "Testing ldb.search for objectCategory=user with domain scope control"
1711 res = ldb.search(self.base_dn, expression="objectCategory=user", scope=SCOPE_SUBTREE, attrs=["cn"], controls=["domain_scope:1"])
1712 self.assertTrue(len(res) > 0)
1714 print "Testing ldb.search for objectCategory=group"
1715 res = ldb.search(self.base_dn, expression="objectCategory=group", scope=SCOPE_SUBTREE, attrs=["cn"])
1716 self.assertTrue(len(res) > 0)
1718 print "Testing ldb.search for objectCategory=group with domain scope control"
1719 res = ldb.search(self.base_dn, expression="objectCategory=group", scope=SCOPE_SUBTREE, attrs=["cn"], controls=["domain_scope:1"])
1720 self.assertTrue(len(res) > 0)
1722 print "Testing creating a user with the posixAccount objectClass"
1723 self.ldb.add_ldif("""dn: cn=posixuser,CN=Users,%s
1724 objectClass: top
1725 objectClass: person
1726 objectClass: posixAccount
1727 objectClass: user
1728 objectClass: organizationalPerson
1729 cn: posixuser
1730 uid: posixuser
1731 sn: posixuser
1732 uidNumber: 10126
1733 gidNumber: 10126
1734 homeDirectory: /home/posixuser
1735 loginShell: /bin/bash
1736 gecos: Posix User;;;
1737 description: A POSIX user"""% (self.base_dn))
1739 print "Testing removing the posixAccount objectClass from an existing user"
1740 self.ldb.modify_ldif("""dn: cn=posixuser,CN=Users,%s
1741 changetype: modify
1742 delete: objectClass
1743 objectClass: posixAccount"""% (self.base_dn))
1745 print "Testing adding the posixAccount objectClass to an existing user"
1746 self.ldb.modify_ldif("""dn: cn=posixuser,CN=Users,%s
1747 changetype: modify
1748 add: objectClass
1749 objectClass: posixAccount"""% (self.base_dn))
1751 self.delete_force(self.ldb, "cn=posixuser,cn=users," + self.base_dn)
1752 self.delete_force(self.ldb, "cn=ldaptestuser,cn=users," + self.base_dn)
1753 self.delete_force(self.ldb, "cn=ldaptestuser2,cn=users," + self.base_dn)
1754 self.delete_force(self.ldb, "cn=ldaptestuser3,cn=users," + self.base_dn)
1755 self.delete_force(self.ldb, "cn=ldaptestuser4,cn=ldaptestcontainer," + self.base_dn)
1756 self.delete_force(self.ldb, "cn=ldaptestuser4,cn=ldaptestcontainer2," + self.base_dn)
1757 self.delete_force(self.ldb, "cn=ldaptestuser5,cn=users," + self.base_dn)
1758 self.delete_force(self.ldb, "cn=ldaptestgroup,cn=users," + self.base_dn)
1759 self.delete_force(self.ldb, "cn=ldaptestgroup2,cn=users," + self.base_dn)
1760 self.delete_force(self.ldb, "cn=ldaptestcomputer,cn=computers," + self.base_dn)
1761 self.delete_force(self.ldb, "cn=ldaptest2computer,cn=computers," + self.base_dn)
1762 self.delete_force(self.ldb, "cn=ldaptestcomputer3,cn=computers," + self.base_dn)
1763 self.delete_force(self.ldb, "cn=ldaptestutf8user èùéìòà,cn=users," + self.base_dn)
1764 self.delete_force(self.ldb, "cn=ldaptestutf8user2 èùéìòà,cn=users," + self.base_dn)
1765 self.delete_force(self.ldb, "cn=ldaptestcontainer," + self.base_dn)
1766 self.delete_force(self.ldb, "cn=ldaptestcontainer2," + self.base_dn)
1768 def test_security_descriptor_add(self):
1769 """ Testing ldb.add_ldif() for nTSecurityDescriptor """
1770 user_name = "testdescriptoruser1"
1771 user_dn = "CN=%s,CN=Users,%s" % (user_name, self.base_dn)
1773 # Test add_ldif() with SDDL security descriptor input
1775 self.delete_force(self.ldb, user_dn)
1776 try:
1777 sddl = "O:DUG:DUD:PAI(A;;RPWP;;;AU)S:PAI"
1778 self.ldb.add_ldif("""
1779 dn: """ + user_dn + """
1780 objectclass: user
1781 sAMAccountName: """ + user_name + """
1782 nTSecurityDescriptor: """ + sddl)
1783 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1784 desc = res[0]["nTSecurityDescriptor"][0]
1785 desc = ndr_unpack( security.descriptor, desc )
1786 desc_sddl = desc.as_sddl( self.domain_sid )
1787 self.assertEqual(desc_sddl, sddl)
1788 finally:
1789 self.delete_force(self.ldb, user_dn)
1791 # Test add_ldif() with BASE64 security descriptor
1793 try:
1794 sddl = "O:DUG:DUD:PAI(A;;RPWP;;;AU)S:PAI"
1795 desc = security.descriptor.from_sddl(sddl, self.domain_sid)
1796 desc_binary = ndr_pack(desc)
1797 desc_base64 = base64.b64encode(desc_binary)
1798 self.ldb.add_ldif("""
1799 dn: """ + user_dn + """
1800 objectclass: user
1801 sAMAccountName: """ + user_name + """
1802 nTSecurityDescriptor:: """ + desc_base64)
1803 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1804 desc = res[0]["nTSecurityDescriptor"][0]
1805 desc = ndr_unpack(security.descriptor, desc)
1806 desc_sddl = desc.as_sddl(self.domain_sid)
1807 self.assertEqual(desc_sddl, sddl)
1808 finally:
1809 self.delete_force(self.ldb, user_dn)
1811 def test_security_descriptor_add_neg(self):
1812 """Test add_ldif() with BASE64 security descriptor input using WRONG domain SID
1813 Negative test
1815 user_name = "testdescriptoruser1"
1816 user_dn = "CN=%s,CN=Users,%s" % (user_name, self.base_dn)
1817 self.delete_force(self.ldb, user_dn)
1818 try:
1819 sddl = "O:DUG:DUD:PAI(A;;RPWP;;;AU)S:PAI"
1820 desc = security.descriptor.from_sddl(sddl, security.dom_sid('S-1-5-21'))
1821 desc_base64 = base64.b64encode( ndr_pack(desc) )
1822 self.ldb.add_ldif("""
1823 dn: """ + user_dn + """
1824 objectclass: user
1825 sAMAccountName: """ + user_name + """
1826 nTSecurityDescriptor:: """ + desc_base64)
1827 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1828 print res
1829 self.assertRaises(KeyError, lambda: res[0]["nTSecurityDescriptor"])
1830 finally:
1831 self.delete_force(self.ldb, user_dn)
1833 def test_security_descriptor_modify(self):
1834 """ Testing ldb.modify_ldif() for nTSecurityDescriptor """
1835 user_name = "testdescriptoruser2"
1836 user_dn = "CN=%s,CN=Users,%s" % (user_name, self.base_dn)
1838 # Delete user object and test modify_ldif() with SDDL security descriptor input
1839 # Add ACE to the original descriptor test
1841 try:
1842 self.delete_force(self.ldb, user_dn)
1843 self.ldb.add_ldif("""
1844 dn: """ + user_dn + """
1845 objectclass: user
1846 sAMAccountName: """ + user_name)
1847 # Modify descriptor
1848 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1849 desc = res[0]["nTSecurityDescriptor"][0]
1850 desc = ndr_unpack(security.descriptor, desc)
1851 desc_sddl = desc.as_sddl(self.domain_sid)
1852 sddl = desc_sddl[:desc_sddl.find("(")] + "(A;;RPWP;;;AU)" + desc_sddl[desc_sddl.find("("):]
1853 mod = """
1854 dn: """ + user_dn + """
1855 changetype: modify
1856 replace: nTSecurityDescriptor
1857 nTSecurityDescriptor: """ + sddl
1858 self.ldb.modify_ldif(mod)
1859 # Read modified descriptor
1860 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1861 desc = res[0]["nTSecurityDescriptor"][0]
1862 desc = ndr_unpack(security.descriptor, desc)
1863 desc_sddl = desc.as_sddl(self.domain_sid)
1864 self.assertEqual(desc_sddl, sddl)
1865 finally:
1866 self.delete_force(self.ldb, user_dn)
1868 # Test modify_ldif() with SDDL security descriptor input
1869 # New desctiptor test
1871 try:
1872 self.ldb.add_ldif("""
1873 dn: """ + user_dn + """
1874 objectclass: user
1875 sAMAccountName: """ + user_name)
1876 # Modify descriptor
1877 sddl = "O:DUG:DUD:PAI(A;;RPWP;;;AU)S:PAI"
1878 mod = """
1879 dn: """ + user_dn + """
1880 changetype: modify
1881 replace: nTSecurityDescriptor
1882 nTSecurityDescriptor: """ + sddl
1883 self.ldb.modify_ldif(mod)
1884 # Read modified descriptor
1885 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1886 desc = res[0]["nTSecurityDescriptor"][0]
1887 desc = ndr_unpack(security.descriptor, desc)
1888 desc_sddl = desc.as_sddl(self.domain_sid)
1889 self.assertEqual(desc_sddl, sddl)
1890 finally:
1891 self.delete_force(self.ldb, user_dn)
1893 # Test modify_ldif() with BASE64 security descriptor input
1894 # Add ACE to the original descriptor test
1896 try:
1897 self.ldb.add_ldif("""
1898 dn: """ + user_dn + """
1899 objectclass: user
1900 sAMAccountName: """ + user_name)
1901 # Modify descriptor
1902 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1903 desc = res[0]["nTSecurityDescriptor"][0]
1904 desc = ndr_unpack(security.descriptor, desc)
1905 desc_sddl = desc.as_sddl(self.domain_sid)
1906 sddl = desc_sddl[:desc_sddl.find("(")] + "(A;;RPWP;;;AU)" + desc_sddl[desc_sddl.find("("):]
1907 desc = security.descriptor.from_sddl(sddl, self.domain_sid)
1908 desc_base64 = base64.b64encode(ndr_pack(desc))
1909 mod = """
1910 dn: """ + user_dn + """
1911 changetype: modify
1912 replace: nTSecurityDescriptor
1913 nTSecurityDescriptor:: """ + desc_base64
1914 self.ldb.modify_ldif(mod)
1915 # Read modified descriptor
1916 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1917 desc = res[0]["nTSecurityDescriptor"][0]
1918 desc = ndr_unpack(security.descriptor, desc)
1919 desc_sddl = desc.as_sddl(self.domain_sid)
1920 self.assertEqual(desc_sddl, sddl)
1921 finally:
1922 self.delete_force(self.ldb, user_dn)
1924 # Test modify_ldif() with BASE64 security descriptor input
1925 # New descriptor test
1927 try:
1928 self.delete_force(self.ldb, user_dn)
1929 self.ldb.add_ldif("""
1930 dn: """ + user_dn + """
1931 objectclass: user
1932 sAMAccountName: """ + user_name)
1933 # Modify descriptor
1934 sddl = "O:DUG:DUD:PAI(A;;RPWP;;;AU)S:PAI"
1935 desc = security.descriptor.from_sddl(sddl, self.domain_sid)
1936 desc_base64 = base64.b64encode(ndr_pack(desc))
1937 mod = """
1938 dn: """ + user_dn + """
1939 changetype: modify
1940 replace: nTSecurityDescriptor
1941 nTSecurityDescriptor:: """ + desc_base64
1942 self.ldb.modify_ldif(mod)
1943 # Read modified descriptor
1944 res = self.ldb.search(base=user_dn, attrs=["nTSecurityDescriptor"])
1945 desc = res[0]["nTSecurityDescriptor"][0]
1946 desc = ndr_unpack(security.descriptor, desc)
1947 desc_sddl = desc.as_sddl(self.domain_sid)
1948 self.assertEqual(desc_sddl, sddl)
1949 finally:
1950 self.delete_force(self.ldb, user_dn)
1952 class BaseDnTests(unittest.TestCase):
1953 def setUp(self):
1954 self.ldb = ldb
1956 def test_rootdse_attrs(self):
1957 """Testing for all rootDSE attributes"""
1958 res = self.ldb.search(scope=SCOPE_BASE, attrs=[])
1959 self.assertEquals(len(res), 1)
1961 def test_highestcommittedusn(self):
1962 """Testing for highestCommittedUSN"""
1963 res = self.ldb.search("", scope=SCOPE_BASE, attrs=["highestCommittedUSN"])
1964 self.assertEquals(len(res), 1)
1965 self.assertTrue(int(res[0]["highestCommittedUSN"][0]) != 0)
1967 def test_netlogon(self):
1968 """Testing for netlogon via LDAP"""
1969 res = self.ldb.search("", scope=SCOPE_BASE, attrs=["netlogon"])
1970 self.assertEquals(len(res), 0)
1972 def test_netlogon_highestcommitted_usn(self):
1973 """Testing for netlogon and highestCommittedUSN via LDAP"""
1974 res = self.ldb.search("", scope=SCOPE_BASE,
1975 attrs=["netlogon", "highestCommittedUSN"])
1976 self.assertEquals(len(res), 0)
1978 def test_namingContexts(self):
1979 """Testing for namingContexts in rootDSE"""
1980 res = self.ldb.search("", scope=SCOPE_BASE,
1981 attrs=["namingContexts", "defaultNamingContext", "schemaNamingContext", "configurationNamingContext"])
1982 self.assertEquals(len(res), 1)
1984 ncs = set([])
1985 for nc in res[0]["namingContexts"]:
1986 self.assertTrue(nc not in ncs)
1987 ncs.add(nc)
1989 self.assertTrue(res[0]["defaultNamingContext"][0] in ncs)
1990 self.assertTrue(res[0]["configurationNamingContext"][0] in ncs)
1991 self.assertTrue(res[0]["schemaNamingContext"][0] in ncs)
1994 if not "://" in host:
1995 if os.path.isfile(host):
1996 host = "tdb://%s" % host
1997 else:
1998 host = "ldap://%s" % host
2000 ldb = Ldb(host, credentials=creds, session_info=system_session(), lp=lp)
2001 if not "tdb://" in host:
2002 gc_ldb = Ldb("%s:3268" % host, credentials=creds,
2003 session_info=system_session(), lp=lp)
2004 else:
2005 gc_ldb = None
2007 runner = SubunitTestRunner()
2008 rc = 0
2009 if not runner.run(unittest.makeSuite(BaseDnTests)).wasSuccessful():
2010 rc = 1
2011 if not runner.run(unittest.makeSuite(BasicTests)).wasSuccessful():
2012 rc = 1
2013 sys.exit(rc)