2 # Simple tests for the ldb python bindings.
3 # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
8 # Required for the standalone LDB build
9 sys
.path
.append("build/lib.linux-i686-2.4")
16 class NoContextTests(unittest
.TestCase
):
18 def test_valid_attr_name(self
):
19 self
.assertTrue(ldb
.valid_attr_name("foo"))
20 self
.assertFalse(ldb
.valid_attr_name("24foo"))
22 def test_timestring(self
):
23 self
.assertEquals("19700101000000.0Z", ldb
.timestring(0))
24 self
.assertEquals("20071119191012.0Z", ldb
.timestring(1195499412))
26 def test_string_to_time(self
):
27 self
.assertEquals(0, ldb
.string_to_time("19700101000000.0Z"))
28 self
.assertEquals(1195499412, ldb
.string_to_time("20071119191012.0Z"))
31 class SimpleLdb(unittest
.TestCase
):
33 def test_connect(self
):
36 def test_connect_none(self
):
39 def test_connect_later(self
):
45 self
.assertTrue(repr(x
).startswith("<ldb connection"))
47 def test_set_create_perms(self
):
49 x
.set_create_perms(0600)
51 def test_set_modules_dir(self
):
53 x
.set_modules_dir("/tmp")
55 def test_modules_none(self
):
57 self
.assertEquals([], x
.modules())
59 def test_modules_tdb(self
):
60 x
= ldb
.Ldb("bar.ldb")
61 self
.assertEquals("[<ldb module 'tdb'>]", repr(x
.modules()))
63 def test_search(self
):
64 l
= ldb
.Ldb(filename())
65 self
.assertEquals(len(l
.search()), 1)
67 def test_search_controls(self
):
68 l
= ldb
.Ldb(filename())
69 self
.assertEquals(len(l
.search(controls
=["paged_results:0:5"])), 1)
71 def test_search_attrs(self
):
72 l
= ldb
.Ldb(filename())
73 self
.assertEquals(len(l
.search(ldb
.Dn(l
, ""), ldb
.SCOPE_SUBTREE
, "(dc=*)", ["dc"])), 0)
75 def test_search_string_dn(self
):
76 l
= ldb
.Ldb(filename())
77 self
.assertEquals(len(l
.search("", ldb
.SCOPE_SUBTREE
, "(dc=*)", ["dc"])), 0)
79 def test_search_attr_string(self
):
80 l
= ldb
.Ldb("foo.tdb")
81 self
.assertRaises(TypeError, l
.search
, attrs
="dc")
83 def test_opaque(self
):
84 l
= ldb
.Ldb(filename())
85 l
.set_opaque("my_opaque", l
)
86 self
.assertTrue(l
.get_opaque("my_opaque") is not None)
87 self
.assertEquals(None, l
.get_opaque("unknown"))
89 def test_search_scope_base(self
):
90 l
= ldb
.Ldb(filename())
91 self
.assertEquals(len(l
.search(ldb
.Dn(l
, "dc=foo1"),
92 ldb
.SCOPE_ONELEVEL
)), 0)
94 def test_delete(self
):
95 l
= ldb
.Ldb(filename())
96 self
.assertRaises(ldb
.LdbError
, lambda: l
.delete(ldb
.Dn(l
, "dc=foo2")))
98 def test_contains(self
):
99 l
= ldb
.Ldb(filename())
100 self
.assertFalse(ldb
.Dn(l
, "dc=foo3") in l
)
101 l
= ldb
.Ldb(filename())
103 m
.dn
= ldb
.Dn(l
, "dc=foo3")
107 self
.assertTrue(ldb
.Dn(l
, "dc=foo3") in l
)
111 def test_get_config_basedn(self
):
112 l
= ldb
.Ldb(filename())
113 self
.assertEquals(None, l
.get_config_basedn())
115 def test_get_root_basedn(self
):
116 l
= ldb
.Ldb(filename())
117 self
.assertEquals(None, l
.get_root_basedn())
119 def test_get_schema_basedn(self
):
120 l
= ldb
.Ldb(filename())
121 self
.assertEquals(None, l
.get_schema_basedn())
123 def test_get_default_basedn(self
):
124 l
= ldb
.Ldb(filename())
125 self
.assertEquals(None, l
.get_default_basedn())
128 l
= ldb
.Ldb(filename())
130 m
.dn
= ldb
.Dn(l
, "dc=foo4")
132 self
.assertEquals(len(l
.search()), 1)
135 self
.assertEquals(len(l
.search()), 2)
137 l
.delete(ldb
.Dn(l
, "dc=foo4"))
139 def test_add_dict(self
):
140 l
= ldb
.Ldb(filename())
141 m
= {"dn": ldb
.Dn(l
, "dc=foo5"),
143 self
.assertEquals(len(l
.search()), 1)
146 self
.assertEquals(len(l
.search()), 2)
148 l
.delete(ldb
.Dn(l
, "dc=foo5"))
150 def test_add_dict_string_dn(self
):
151 l
= ldb
.Ldb(filename())
152 m
= {"dn": "dc=foo6", "bla": "bla"}
153 self
.assertEquals(len(l
.search()), 1)
156 self
.assertEquals(len(l
.search()), 2)
158 l
.delete(ldb
.Dn(l
, "dc=foo6"))
160 def test_rename(self
):
161 l
= ldb
.Ldb(filename())
163 m
.dn
= ldb
.Dn(l
, "dc=foo7")
165 self
.assertEquals(len(l
.search()), 1)
168 l
.rename(ldb
.Dn(l
, "dc=foo7"), ldb
.Dn(l
, "dc=bar"))
169 self
.assertEquals(len(l
.search()), 2)
171 l
.delete(ldb
.Dn(l
, "dc=bar"))
173 def test_rename_string_dns(self
):
174 l
= ldb
.Ldb(filename())
176 m
.dn
= ldb
.Dn(l
, "dc=foo8")
178 self
.assertEquals(len(l
.search()), 1)
180 self
.assertEquals(len(l
.search()), 2)
182 l
.rename("dc=foo8", "dc=bar")
183 self
.assertEquals(len(l
.search()), 2)
185 l
.delete(ldb
.Dn(l
, "dc=bar"))
187 def test_modify_delete(self
):
188 l
= ldb
.Ldb(filename())
190 m
.dn
= ldb
.Dn(l
, "dc=modifydelete")
193 rm
= l
.search(m
.dn
)[0]
194 self
.assertEquals(["1234"], list(rm
["bla"]))
197 m
.dn
= ldb
.Dn(l
, "dc=modifydelete")
198 m
["bla"] = ldb
.MessageElement([], ldb
.CHANGETYPE_DELETE
, "bla")
200 rm
= l
.search(m
.dn
)[0]
201 self
.assertEquals(1, len(rm
))
203 l
.delete(ldb
.Dn(l
, "dc=modifydelete"))
205 def test_modify_add(self
):
206 l
= ldb
.Ldb(filename())
208 m
.dn
= ldb
.Dn(l
, "dc=add")
213 m
.dn
= ldb
.Dn(l
, "dc=add")
214 m
["bla"] = ldb
.MessageElement(["456"], ldb
.CHANGETYPE_ADD
, "bla")
216 rm
= l
.search(m
.dn
)[0]
217 self
.assertEquals(2, len(rm
))
218 self
.assertEquals(["1234", "456"], list(rm
["bla"]))
220 l
.delete(ldb
.Dn(l
, "dc=add"))
222 def test_modify_modify(self
):
223 l
= ldb
.Ldb(filename())
225 m
.dn
= ldb
.Dn(l
, "dc=modify2")
226 m
["bla"] = ["1234", "456"]
230 m
.dn
= ldb
.Dn(l
, "dc=modify2")
231 m
["bla"] = ldb
.MessageElement(["456"], ldb
.CHANGETYPE_MODIFY
, "bla")
233 rm
= l
.search(m
.dn
)[0]
234 self
.assertEquals(2, len(rm
))
235 self
.assertEquals(["1234"], list(rm
["bla"]))
237 l
.delete(ldb
.Dn(l
, "dc=modify2"))
239 def test_transaction_commit(self
):
240 l
= ldb
.Ldb(filename())
241 l
.transaction_start()
242 m
= ldb
.Message(ldb
.Dn(l
, "dc=foo9"))
245 l
.transaction_commit()
248 def test_transaction_cancel(self
):
249 l
= ldb
.Ldb(filename())
250 l
.transaction_start()
251 m
= ldb
.Message(ldb
.Dn(l
, "dc=foo10"))
254 l
.transaction_cancel()
255 self
.assertEquals(0, len(l
.search(ldb
.Dn(l
, "dc=foo10"))))
257 def test_set_debug(self
):
258 def my_report_fn(level
, text
):
260 l
= ldb
.Ldb(filename())
261 l
.set_debug(my_report_fn
)
263 def test_zero_byte_string(self
):
264 """Testing we do not get trapped in the \0 byte in a property string."""
265 l
= ldb
.Ldb(filename())
268 "objectclass" : "user",
269 "cN" : "LDAPtestUSER",
270 "givenname" : "ldap",
271 "displayname" : "foo\0bar",
273 res
= l
.search(expression
="(dn=dc=somedn)")
274 self
.assertEquals("foo\0bar", res
[0]["displayname"][0])
277 class DnTests(unittest
.TestCase
):
280 self
.ldb
= ldb
.Ldb(filename())
283 x
= ldb
.Dn(self
.ldb
, "dc=foo11,bar=bloe")
284 y
= ldb
.Dn(self
.ldb
, "dc=foo11,bar=bloe")
285 self
.assertEquals(x
, y
)
288 x
= ldb
.Dn(self
.ldb
, "dc=foo12,bar=bloe")
289 self
.assertEquals(x
.__str
__(), "dc=foo12,bar=bloe")
292 x
= ldb
.Dn(self
.ldb
, "dc=foo13,bla=blie")
293 self
.assertEquals(x
.__repr
__(), "Dn('dc=foo13,bla=blie')")
295 def test_get_casefold(self
):
296 x
= ldb
.Dn(self
.ldb
, "dc=foo14,bar=bloe")
297 self
.assertEquals(x
.get_casefold(), "DC=FOO14,BAR=bloe")
299 def test_validate(self
):
300 x
= ldb
.Dn(self
.ldb
, "dc=foo15,bar=bloe")
301 self
.assertTrue(x
.validate())
303 def test_parent(self
):
304 x
= ldb
.Dn(self
.ldb
, "dc=foo16,bar=bloe")
305 self
.assertEquals("bar=bloe", x
.parent().__str
__())
307 def test_parent_nonexistant(self
):
308 x
= ldb
.Dn(self
.ldb
, "@BLA")
309 self
.assertEquals(None, x
.parent())
311 def test_compare(self
):
312 x
= ldb
.Dn(self
.ldb
, "dc=foo17,bar=bloe")
313 y
= ldb
.Dn(self
.ldb
, "dc=foo17,bar=bloe")
314 self
.assertEquals(x
, y
)
315 z
= ldb
.Dn(self
.ldb
, "dc=foo17,bar=blie")
316 self
.assertNotEquals(z
, y
)
318 def test_is_valid(self
):
319 x
= ldb
.Dn(self
.ldb
, "dc=foo18,dc=bloe")
320 self
.assertTrue(x
.is_valid())
321 x
= ldb
.Dn(self
.ldb
, "")
322 # is_valid()'s return values appears to be a side effect of
323 # some other ldb functions. yuck.
324 # self.assertFalse(x.is_valid())
326 def test_is_special(self
):
327 x
= ldb
.Dn(self
.ldb
, "dc=foo19,bar=bloe")
328 self
.assertFalse(x
.is_special())
329 x
= ldb
.Dn(self
.ldb
, "@FOOBAR")
330 self
.assertTrue(x
.is_special())
332 def test_check_special(self
):
333 x
= ldb
.Dn(self
.ldb
, "dc=foo20,bar=bloe")
334 self
.assertFalse(x
.check_special("FOOBAR"))
335 x
= ldb
.Dn(self
.ldb
, "@FOOBAR")
336 self
.assertTrue(x
.check_special("@FOOBAR"))
339 x
= ldb
.Dn(self
.ldb
, "dc=foo21,bar=bloe")
340 self
.assertEquals(2, len(x
))
341 x
= ldb
.Dn(self
.ldb
, "dc=foo21")
342 self
.assertEquals(1, len(x
))
344 def test_add_child(self
):
345 x
= ldb
.Dn(self
.ldb
, "dc=foo22,bar=bloe")
346 self
.assertTrue(x
.add_child(ldb
.Dn(self
.ldb
, "bla=bloe")))
347 self
.assertEquals("bla=bloe,dc=foo22,bar=bloe", x
.__str
__())
349 def test_add_base(self
):
350 x
= ldb
.Dn(self
.ldb
, "dc=foo23,bar=bloe")
351 base
= ldb
.Dn(self
.ldb
, "bla=bloe")
352 self
.assertTrue(x
.add_base(base
))
353 self
.assertEquals("dc=foo23,bar=bloe,bla=bloe", x
.__str
__())
356 x
= ldb
.Dn(self
.ldb
, "dc=foo24")
357 y
= ldb
.Dn(self
.ldb
, "bar=bla")
358 self
.assertEquals("dc=foo24,bar=bla", str(y
+ x
))
360 def test_parse_ldif(self
):
361 msgs
= self
.ldb
.parse_ldif("dn: foo=bar\n")
363 self
.assertEquals("foo=bar", str(msg
[1].dn
))
364 self
.assertTrue(isinstance(msg
[1], ldb
.Message
))
366 def test_parse_ldif_more(self
):
367 msgs
= self
.ldb
.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
369 self
.assertEquals("foo=bar", str(msg
[1].dn
))
371 self
.assertEquals("bar=bar", str(msg
[1].dn
))
373 def test_canonical_string(self
):
374 x
= ldb
.Dn(self
.ldb
, "dc=foo25,bar=bloe")
375 self
.assertEquals("/bloe/foo25", x
.canonical_str())
377 def test_canonical_ex_string(self
):
378 x
= ldb
.Dn(self
.ldb
, "dc=foo26,bar=bloe")
379 self
.assertEquals("/bloe\nfoo26", x
.canonical_ex_str())
382 class LdbMsgTests(unittest
.TestCase
):
385 self
.msg
= ldb
.Message()
387 def test_init_dn(self
):
388 self
.msg
= ldb
.Message(ldb
.Dn(ldb
.Ldb(), "dc=foo27"))
389 self
.assertEquals("dc=foo27", str(self
.msg
.dn
))
391 def test_iter_items(self
):
392 self
.assertEquals(0, len(self
.msg
.items()))
393 self
.msg
.dn
= ldb
.Dn(ldb
.Ldb("foo.tdb"), "dc=foo28")
394 self
.assertEquals(1, len(self
.msg
.items()))
397 self
.msg
.dn
= ldb
.Dn(ldb
.Ldb("foo.tdb"), "dc=foo29")
398 self
.msg
["dc"] = "foo"
399 self
.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self
.msg
))
402 self
.assertEquals(0, len(self
.msg
))
404 def test_notpresent(self
):
405 self
.assertRaises(KeyError, lambda: self
.msg
["foo"])
410 def test_add_value(self
):
411 self
.assertEquals(0, len(self
.msg
))
412 self
.msg
["foo"] = ["foo"]
413 self
.assertEquals(1, len(self
.msg
))
415 def test_add_value_multiple(self
):
416 self
.assertEquals(0, len(self
.msg
))
417 self
.msg
["foo"] = ["foo", "bla"]
418 self
.assertEquals(1, len(self
.msg
))
419 self
.assertEquals(["foo", "bla"], list(self
.msg
["foo"]))
421 def test_set_value(self
):
422 self
.msg
["foo"] = ["fool"]
423 self
.assertEquals(["fool"], list(self
.msg
["foo"]))
424 self
.msg
["foo"] = ["bar"]
425 self
.assertEquals(["bar"], list(self
.msg
["foo"]))
428 self
.msg
.dn
= ldb
.Dn(ldb
.Ldb("foo.tdb"), "@BASEINFO")
429 self
.msg
["foo"] = ["bla"]
430 self
.msg
["bar"] = ["bla"]
431 self
.assertEquals(["dn", "foo", "bar"], self
.msg
.keys())
434 self
.msg
.dn
= ldb
.Dn(ldb
.Ldb(filename()), "@BASEINFO")
435 self
.assertEquals("@BASEINFO", self
.msg
.dn
.__str
__())
437 def test_get_dn(self
):
438 self
.msg
.dn
= ldb
.Dn(ldb
.Ldb("foo.tdb"), "@BASEINFO")
439 self
.assertEquals("@BASEINFO", self
.msg
.get("dn").__str
__())
441 def test_get_other(self
):
442 self
.msg
["foo"] = ["bar"]
443 self
.assertEquals("bar", self
.msg
.get("foo")[0])
445 def test_get_unknown(self
):
446 self
.assertEquals(None, self
.msg
.get("lalalala"))
449 class MessageElementTests(unittest
.TestCase
):
451 def test_cmp_element(self
):
452 x
= ldb
.MessageElement(["foo"])
453 y
= ldb
.MessageElement(["foo"])
454 z
= ldb
.MessageElement(["bzr"])
455 self
.assertEquals(x
, y
)
456 self
.assertNotEquals(x
, z
)
458 def test_create_iterable(self
):
459 x
= ldb
.MessageElement(["foo"])
460 self
.assertEquals(["foo"], list(x
))
463 x
= ldb
.MessageElement(["foo"])
464 self
.assertEquals("MessageElement(['foo'])", repr(x
))
465 x
= ldb
.MessageElement(["foo", "bla"])
466 self
.assertEquals(2, len(x
))
467 self
.assertEquals("MessageElement(['foo','bla'])", repr(x
))
469 def test_get_item(self
):
470 x
= ldb
.MessageElement(["foo", "bar"])
471 self
.assertEquals("foo", x
[0])
472 self
.assertEquals("bar", x
[1])
473 self
.assertEquals("bar", x
[-1])
474 self
.assertRaises(IndexError, lambda: x
[45])
477 x
= ldb
.MessageElement(["foo", "bar"])
478 self
.assertEquals(2, len(x
))
481 x
= ldb
.MessageElement(["foo", "bar"])
482 y
= ldb
.MessageElement(["foo", "bar"])
483 self
.assertEquals(y
, x
)
484 x
= ldb
.MessageElement(["foo"])
485 self
.assertNotEquals(y
, x
)
486 y
= ldb
.MessageElement(["foo"])
487 self
.assertEquals(y
, x
)
490 class ModuleTests(unittest
.TestCase
):
492 def test_register_module(self
):
495 ldb
.register_module(ExampleModule
)
497 def test_use_module(self
):
502 def __init__(self
, ldb
, next
):
506 def search(self
, *args
, **kwargs
):
507 return self
.next
.search(*args
, **kwargs
)
509 ldb
.register_module(ExampleModule
)
510 if os
.path
.exists("usemodule.ldb"):
511 os
.unlink("usemodule.ldb")
512 l
= ldb
.Ldb("usemodule.ldb")
513 l
.add({"dn": "@MODULES", "@LIST": "bla"})
514 self
.assertEquals([], ops
)
515 l
= ldb
.Ldb("usemodule.ldb")
516 self
.assertEquals(["init"], ops
)
519 if __name__
== '__main__':
521 unittest
.TestProgram()