133bd180c17b1275315f182f62e8ccb2380da6b2
[ira/wip.git] / source4 / lib / ldb / tests / python / api.py
1 #!/usr/bin/python
2 # Simple tests for the ldb python bindings.
3 # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
4
5 import os, sys
6 import unittest
7
8 # Required for the standalone LDB build
9 sys.path.append("build/lib.linux-i686-2.4")
10
11 import ldb
12
13 def filename():
14     return os.tempnam()
15
16 class NoContextTests(unittest.TestCase):
17
18     def test_valid_attr_name(self):
19         self.assertTrue(ldb.valid_attr_name("foo"))
20         self.assertFalse(ldb.valid_attr_name("24foo"))
21
22     def test_timestring(self):
23         self.assertEquals("19700101000000.0Z", ldb.timestring(0))
24         self.assertEquals("20071119191012.0Z", ldb.timestring(1195499412))
25
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"))
29
30
31 class SimpleLdb(unittest.TestCase):
32
33     def test_connect(self):
34         ldb.Ldb(filename())
35
36     def test_connect_none(self):
37         ldb.Ldb()
38
39     def test_connect_later(self):
40         x = ldb.Ldb()
41         x.connect(filename())
42
43     def test_repr(self):
44         x = ldb.Ldb()
45         self.assertTrue(repr(x).startswith("<ldb connection"))
46
47     def test_set_create_perms(self):
48         x = ldb.Ldb()
49         x.set_create_perms(0600)
50
51     def test_set_modules_dir(self):
52         x = ldb.Ldb()
53         x.set_modules_dir("/tmp")
54
55     def test_modules_none(self):
56         x = ldb.Ldb()
57         self.assertEquals([], x.modules())
58
59     def test_modules_tdb(self):
60         x = ldb.Ldb("bar.ldb")
61         self.assertEquals("[<ldb module 'tdb'>]", repr(x.modules()))
62
63     def test_search(self):
64         l = ldb.Ldb(filename())
65         self.assertEquals(len(l.search()), 1)
66
67     def test_search_controls(self):
68         l = ldb.Ldb(filename())
69         self.assertEquals(len(l.search(controls=["paged_results:0:5"])), 1)
70
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)
74
75     def test_search_string_dn(self):
76         l = ldb.Ldb(filename())
77         self.assertEquals(len(l.search("", ldb.SCOPE_SUBTREE, "(dc=*)", ["dc"])), 0)
78
79     def test_search_attr_string(self):
80         l = ldb.Ldb("foo.tdb")
81         self.assertRaises(TypeError, l.search, attrs="dc")
82
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"))
88
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)
93
94     def test_delete(self):
95         l = ldb.Ldb(filename())
96         self.assertRaises(ldb.LdbError, lambda: l.delete(ldb.Dn(l, "dc=foo2")))
97
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())
102         m = ldb.Message()
103         m.dn = ldb.Dn(l, "dc=foo3")
104         m["b"] = ["a"]
105         l.add(m)
106         try:
107             self.assertTrue(ldb.Dn(l, "dc=foo3") in l)
108         finally:
109             l.delete(m.dn)
110
111     def test_get_config_basedn(self):
112         l = ldb.Ldb(filename())
113         self.assertEquals(None, l.get_config_basedn())
114
115     def test_get_root_basedn(self):
116         l = ldb.Ldb(filename())
117         self.assertEquals(None, l.get_root_basedn())
118
119     def test_get_schema_basedn(self):
120         l = ldb.Ldb(filename())
121         self.assertEquals(None, l.get_schema_basedn())
122
123     def test_get_default_basedn(self):
124         l = ldb.Ldb(filename())
125         self.assertEquals(None, l.get_default_basedn())
126
127     def test_add(self):
128         l = ldb.Ldb(filename())
129         m = ldb.Message()
130         m.dn = ldb.Dn(l, "dc=foo4")
131         m["bla"] = "bla"
132         self.assertEquals(len(l.search()), 1)
133         l.add(m)
134         try:
135             self.assertEquals(len(l.search()), 2)
136         finally:
137             l.delete(ldb.Dn(l, "dc=foo4"))
138
139     def test_add_dict(self):
140         l = ldb.Ldb(filename())
141         m = {"dn": ldb.Dn(l, "dc=foo5"),
142              "bla": "bla"}
143         self.assertEquals(len(l.search()), 1)
144         l.add(m)
145         try:
146             self.assertEquals(len(l.search()), 2)
147         finally:
148             l.delete(ldb.Dn(l, "dc=foo5"))
149
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)
154         l.add(m)
155         try:
156             self.assertEquals(len(l.search()), 2)
157         finally:
158             l.delete(ldb.Dn(l, "dc=foo6"))
159
160     def test_rename(self):
161         l = ldb.Ldb(filename())
162         m = ldb.Message()
163         m.dn = ldb.Dn(l, "dc=foo7")
164         m["bla"] = "bla"
165         self.assertEquals(len(l.search()), 1)
166         l.add(m)
167         try:
168             l.rename(ldb.Dn(l, "dc=foo7"), ldb.Dn(l, "dc=bar"))
169             self.assertEquals(len(l.search()), 2)
170         finally:
171             l.delete(ldb.Dn(l, "dc=bar"))
172
173     def test_rename_string_dns(self):
174         l = ldb.Ldb(filename())
175         m = ldb.Message()
176         m.dn = ldb.Dn(l, "dc=foo8")
177         m["bla"] = "bla"
178         self.assertEquals(len(l.search()), 1)
179         l.add(m)
180         self.assertEquals(len(l.search()), 2)
181         try:
182             l.rename("dc=foo8", "dc=bar")
183             self.assertEquals(len(l.search()), 2)
184         finally:
185             l.delete(ldb.Dn(l, "dc=bar"))
186
187     def test_modify_delete(self):
188         l = ldb.Ldb(filename())
189         m = ldb.Message()
190         m.dn = ldb.Dn(l, "dc=modifydelete")
191         m["bla"] = ["1234"]
192         l.add(m)
193         rm = l.search(m.dn)[0]
194         self.assertEquals(["1234"], list(rm["bla"]))
195         try:
196             m = ldb.Message()
197             m.dn = ldb.Dn(l, "dc=modifydelete")
198             m["bla"] = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, "bla")
199             self.assertEquals(ldb.FLAG_MOD_DELETE, m["bla"].flags())
200             l.modify(m)
201             rm = l.search(m.dn)[0]
202             self.assertEquals(1, len(rm))
203             rm = l.search(m.dn, attrs=["bla"])[0]
204             self.assertEquals(0, len(rm))
205         finally:
206             l.delete(ldb.Dn(l, "dc=modifydelete"))
207
208     def test_modify_add(self):
209         l = ldb.Ldb(filename())
210         m = ldb.Message()
211         m.dn = ldb.Dn(l, "dc=add")
212         m["bla"] = ["1234"]
213         l.add(m)
214         try:
215             m = ldb.Message()
216             m.dn = ldb.Dn(l, "dc=add")
217             m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
218             self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
219             l.modify(m)
220             rm = l.search(m.dn)[0]
221             self.assertEquals(2, len(rm))
222             self.assertEquals(["1234", "456"], list(rm["bla"]))
223         finally:
224             l.delete(ldb.Dn(l, "dc=add"))
225
226     def test_modify_replace(self):
227         l = ldb.Ldb(filename())
228         m = ldb.Message()
229         m.dn = ldb.Dn(l, "dc=modify2")
230         m["bla"] = ["1234", "456"]
231         l.add(m)
232         try:
233             m = ldb.Message()
234             m.dn = ldb.Dn(l, "dc=modify2")
235             m["bla"] = ldb.MessageElement(["789"], ldb.FLAG_MOD_REPLACE, "bla")
236             self.assertEquals(ldb.FLAG_MOD_REPLACE, m["bla"].flags())
237             l.modify(m)
238             rm = l.search(m.dn)[0]
239             self.assertEquals(2, len(rm))
240             self.assertEquals(["789"], list(rm["bla"]))
241             rm = l.search(m.dn, attrs=["bla"])[0]
242             self.assertEquals(1, len(rm))
243         finally:
244             l.delete(ldb.Dn(l, "dc=modify2"))
245
246     def test_modify_flags_change(self):
247         l = ldb.Ldb(filename())
248         m = ldb.Message()
249         m.dn = ldb.Dn(l, "dc=add")
250         m["bla"] = ["1234"]
251         l.add(m)
252         try:
253             m = ldb.Message()
254             m.dn = ldb.Dn(l, "dc=add")
255             m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
256             self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
257             l.modify(m)
258             rm = l.search(m.dn)[0]
259             self.assertEquals(2, len(rm))
260             self.assertEquals(["1234", "456"], list(rm["bla"]))
261             
262             #Now create another modify, but switch the flags before we do it
263             m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
264             m["bla"].set_flags(ldb.FLAG_MOD_DELETE)
265             l.modify(m)
266             rm = l.search(m.dn, attrs=["bla"])[0]
267             self.assertEquals(1, len(rm))
268             self.assertEquals(["1234"], list(rm["bla"]))
269             
270         finally:
271             l.delete(ldb.Dn(l, "dc=add"))
272
273     def test_transaction_commit(self):
274         l = ldb.Ldb(filename())
275         l.transaction_start()
276         m = ldb.Message(ldb.Dn(l, "dc=foo9"))
277         m["foo"] = ["bar"]
278         l.add(m)
279         l.transaction_commit()
280         l.delete(m.dn)
281
282     def test_transaction_cancel(self):
283         l = ldb.Ldb(filename())
284         l.transaction_start()
285         m = ldb.Message(ldb.Dn(l, "dc=foo10"))
286         m["foo"] = ["bar"]
287         l.add(m)
288         l.transaction_cancel()
289         self.assertEquals(0, len(l.search(ldb.Dn(l, "dc=foo10"))))
290
291     def test_set_debug(self):
292         def my_report_fn(level, text):
293             pass
294         l = ldb.Ldb(filename())
295         l.set_debug(my_report_fn)
296
297     def test_zero_byte_string(self):
298         """Testing we do not get trapped in the \0 byte in a property string."""
299         l = ldb.Ldb(filename())
300         l.add({
301             "dn" : "dc=somedn",
302             "objectclass" : "user",
303             "cN" : "LDAPtestUSER",
304             "givenname" : "ldap",
305             "displayname" : "foo\0bar",
306         })
307         res = l.search(expression="(dn=dc=somedn)")
308         self.assertEquals("foo\0bar", res[0]["displayname"][0])
309
310
311 class DnTests(unittest.TestCase):
312
313     def setUp(self):
314         self.ldb = ldb.Ldb(filename())
315
316     def test_set_dn_invalid(self):
317         x = ldb.Message()
318         def assign():
319             x.dn = "astring"
320         self.assertRaises(TypeError, assign)
321
322     def test_eq(self):
323         x = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
324         y = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
325         self.assertEquals(x, y)
326
327     def test_str(self):
328         x = ldb.Dn(self.ldb, "dc=foo12,bar=bloe")
329         self.assertEquals(x.__str__(), "dc=foo12,bar=bloe")
330
331     def test_repr(self):
332         x = ldb.Dn(self.ldb, "dc=foo13,bla=blie")
333         self.assertEquals(x.__repr__(), "Dn('dc=foo13,bla=blie')")
334
335     def test_get_casefold(self):
336         x = ldb.Dn(self.ldb, "dc=foo14,bar=bloe")
337         self.assertEquals(x.get_casefold(), "DC=FOO14,BAR=bloe")
338
339     def test_validate(self):
340         x = ldb.Dn(self.ldb, "dc=foo15,bar=bloe")
341         self.assertTrue(x.validate())
342
343     def test_parent(self):
344         x = ldb.Dn(self.ldb, "dc=foo16,bar=bloe")
345         self.assertEquals("bar=bloe", x.parent().__str__())
346
347     def test_parent_nonexistant(self):
348         x = ldb.Dn(self.ldb, "@BLA")
349         self.assertEquals(None, x.parent())
350
351     def test_compare(self):
352         x = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
353         y = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
354         self.assertEquals(x, y)
355         z = ldb.Dn(self.ldb, "dc=foo17,bar=blie")
356         self.assertNotEquals(z, y)
357
358     def test_is_valid(self):
359         x = ldb.Dn(self.ldb, "dc=foo18,dc=bloe")
360         self.assertTrue(x.is_valid())
361         x = ldb.Dn(self.ldb, "")
362         # is_valid()'s return values appears to be a side effect of 
363         # some other ldb functions. yuck.
364         # self.assertFalse(x.is_valid())
365
366     def test_is_special(self):
367         x = ldb.Dn(self.ldb, "dc=foo19,bar=bloe")
368         self.assertFalse(x.is_special())
369         x = ldb.Dn(self.ldb, "@FOOBAR")
370         self.assertTrue(x.is_special())
371
372     def test_check_special(self):
373         x = ldb.Dn(self.ldb, "dc=foo20,bar=bloe")
374         self.assertFalse(x.check_special("FOOBAR"))
375         x = ldb.Dn(self.ldb, "@FOOBAR")
376         self.assertTrue(x.check_special("@FOOBAR"))
377
378     def test_len(self):
379         x = ldb.Dn(self.ldb, "dc=foo21,bar=bloe")
380         self.assertEquals(2, len(x))
381         x = ldb.Dn(self.ldb, "dc=foo21")
382         self.assertEquals(1, len(x))
383
384     def test_add_child(self):
385         x = ldb.Dn(self.ldb, "dc=foo22,bar=bloe")
386         self.assertTrue(x.add_child(ldb.Dn(self.ldb, "bla=bloe")))
387         self.assertEquals("bla=bloe,dc=foo22,bar=bloe", x.__str__())
388
389     def test_add_base(self):
390         x = ldb.Dn(self.ldb, "dc=foo23,bar=bloe")
391         base = ldb.Dn(self.ldb, "bla=bloe")
392         self.assertTrue(x.add_base(base))
393         self.assertEquals("dc=foo23,bar=bloe,bla=bloe", x.__str__())
394
395     def test_add(self):
396         x = ldb.Dn(self.ldb, "dc=foo24")
397         y = ldb.Dn(self.ldb, "bar=bla")
398         self.assertEquals("dc=foo24,bar=bla", str(y + x))
399
400     def test_parse_ldif(self):
401         msgs = self.ldb.parse_ldif("dn: foo=bar\n")
402         msg = msgs.next()
403         self.assertEquals("foo=bar", str(msg[1].dn))
404         self.assertTrue(isinstance(msg[1], ldb.Message))
405         ldif = self.ldb.write_ldif(msg[1], ldb.CHANGETYPE_NONE)
406         self.assertEquals("dn: foo=bar\n\n", ldif)
407         
408     def test_parse_ldif_more(self):
409         msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
410         msg = msgs.next()
411         self.assertEquals("foo=bar", str(msg[1].dn))
412         msg = msgs.next()
413         self.assertEquals("bar=bar", str(msg[1].dn))
414
415     def test_canonical_string(self):
416         x = ldb.Dn(self.ldb, "dc=foo25,bar=bloe")
417         self.assertEquals("/bloe/foo25", x.canonical_str())
418
419     def test_canonical_ex_string(self):
420         x = ldb.Dn(self.ldb, "dc=foo26,bar=bloe")
421         self.assertEquals("/bloe\nfoo26", x.canonical_ex_str())
422
423
424 class LdbMsgTests(unittest.TestCase):
425
426     def setUp(self):
427         self.msg = ldb.Message()
428
429     def test_init_dn(self):
430         self.msg = ldb.Message(ldb.Dn(ldb.Ldb(), "dc=foo27"))
431         self.assertEquals("dc=foo27", str(self.msg.dn))
432
433     def test_iter_items(self):
434         self.assertEquals(0, len(self.msg.items()))
435         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo28")
436         self.assertEquals(1, len(self.msg.items()))
437
438     def test_repr(self):
439         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo29")
440         self.msg["dc"] = "foo"
441         self.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self.msg))
442
443     def test_len(self):
444         self.assertEquals(0, len(self.msg))
445
446     def test_notpresent(self):
447         self.assertRaises(KeyError, lambda: self.msg["foo"])
448
449     def test_del(self):
450         del self.msg["foo"]
451
452     def test_add_value(self):
453         self.assertEquals(0, len(self.msg))
454         self.msg["foo"] = ["foo"]
455         self.assertEquals(1, len(self.msg))
456
457     def test_add_value_multiple(self):
458         self.assertEquals(0, len(self.msg))
459         self.msg["foo"] = ["foo", "bla"]
460         self.assertEquals(1, len(self.msg))
461         self.assertEquals(["foo", "bla"], list(self.msg["foo"]))
462
463     def test_set_value(self):
464         self.msg["foo"] = ["fool"]
465         self.assertEquals(["fool"], list(self.msg["foo"]))
466         self.msg["foo"] = ["bar"]
467         self.assertEquals(["bar"], list(self.msg["foo"]))
468
469     def test_keys(self):
470         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
471         self.msg["foo"] = ["bla"]
472         self.msg["bar"] = ["bla"]
473         self.assertEquals(["dn", "foo", "bar"], self.msg.keys())
474
475     def test_dn(self):
476         self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO")
477         self.assertEquals("@BASEINFO", self.msg.dn.__str__())
478
479     def test_get_dn(self):
480         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
481         self.assertEquals("@BASEINFO", self.msg.get("dn").__str__())
482
483     def test_get_invalid(self):
484         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
485         self.assertRaises(TypeError, self.msg.get, 42)
486
487     def test_get_other(self):
488         self.msg["foo"] = ["bar"]
489         self.assertEquals("bar", self.msg.get("foo")[0])
490
491     def test_get_unknown(self):
492         self.assertEquals(None, self.msg.get("lalalala"))
493
494     def test_msg_diff(self):
495         l = ldb.Ldb()
496         msgs = l.parse_ldif("dn: foo=bar\nfoo: bar\nbaz: do\n\ndn: foo=bar\nfoo: bar\nbaz: dont\n")
497         msg1 = msgs.next()[1]
498         msg2 = msgs.next()[1]
499         msgdiff = l.msg_diff(msg1, msg2)
500         self.assertEquals("foo=bar", msgdiff.get("dn").__str__())
501         self.assertRaises(KeyError, lambda: msgdiff["foo"])
502         self.assertEquals(1, len(msgdiff))
503
504
505
506 class MessageElementTests(unittest.TestCase):
507
508     def test_cmp_element(self):
509         x = ldb.MessageElement(["foo"])
510         y = ldb.MessageElement(["foo"])
511         z = ldb.MessageElement(["bzr"])
512         self.assertEquals(x, y)
513         self.assertNotEquals(x, z)
514
515     def test_create_iterable(self):
516         x = ldb.MessageElement(["foo"])
517         self.assertEquals(["foo"], list(x))
518
519     def test_repr(self):
520         x = ldb.MessageElement(["foo"])
521         self.assertEquals("MessageElement(['foo'])", repr(x))
522         x = ldb.MessageElement(["foo", "bla"])
523         self.assertEquals(2, len(x))
524         self.assertEquals("MessageElement(['foo','bla'])", repr(x))
525
526     def test_get_item(self):
527         x = ldb.MessageElement(["foo", "bar"])
528         self.assertEquals("foo", x[0])
529         self.assertEquals("bar", x[1])
530         self.assertEquals("bar", x[-1])
531         self.assertRaises(IndexError, lambda: x[45])
532
533     def test_len(self):
534         x = ldb.MessageElement(["foo", "bar"])
535         self.assertEquals(2, len(x))
536
537     def test_eq(self):
538         x = ldb.MessageElement(["foo", "bar"])
539         y = ldb.MessageElement(["foo", "bar"])
540         self.assertEquals(y, x)
541         x = ldb.MessageElement(["foo"])
542         self.assertNotEquals(y, x)
543         y = ldb.MessageElement(["foo"])
544         self.assertEquals(y, x)
545
546
547 class ModuleTests(unittest.TestCase):
548
549     def test_register_module(self):
550         class ExampleModule:
551             name = "example"
552         ldb.register_module(ExampleModule)
553
554     def test_use_module(self):
555         ops = []
556         class ExampleModule:
557             name = "bla"
558
559             def __init__(self, ldb, next):
560                 ops.append("init")
561                 self.next = next
562
563             def search(self, *args, **kwargs):
564                 return self.next.search(*args, **kwargs)
565
566         ldb.register_module(ExampleModule)
567         if os.path.exists("usemodule.ldb"):
568             os.unlink("usemodule.ldb")
569         l = ldb.Ldb("usemodule.ldb")
570         l.add({"dn": "@MODULES", "@LIST": "bla"})
571         self.assertEquals([], ops)
572         l = ldb.Ldb("usemodule.ldb")
573         self.assertEquals(["init"], ops)
574
575
576 if __name__ == '__main__':
577     import unittest
578     unittest.TestProgram()