pythonbindings: allow add() to have an array of controls as second parameter
[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_w_unhandled_ctrl(self):
140         l = ldb.Ldb(filename())
141         m = ldb.Message()
142         m.dn = ldb.Dn(l, "dc=foo4")
143         m["bla"] = "bla"
144         self.assertEquals(len(l.search()), 1)
145         self.assertRaises(ldb.LdbError, lambda: l.add(m,["search_options:1:2"]))
146
147     def test_add_dict(self):
148         l = ldb.Ldb(filename())
149         m = {"dn": ldb.Dn(l, "dc=foo5"),
150              "bla": "bla"}
151         self.assertEquals(len(l.search()), 1)
152         l.add(m)
153         try:
154             self.assertEquals(len(l.search()), 2)
155         finally:
156             l.delete(ldb.Dn(l, "dc=foo5"))
157
158     def test_add_dict_string_dn(self):
159         l = ldb.Ldb(filename())
160         m = {"dn": "dc=foo6", "bla": "bla"}
161         self.assertEquals(len(l.search()), 1)
162         l.add(m)
163         try:
164             self.assertEquals(len(l.search()), 2)
165         finally:
166             l.delete(ldb.Dn(l, "dc=foo6"))
167
168     def test_rename(self):
169         l = ldb.Ldb(filename())
170         m = ldb.Message()
171         m.dn = ldb.Dn(l, "dc=foo7")
172         m["bla"] = "bla"
173         self.assertEquals(len(l.search()), 1)
174         l.add(m)
175         try:
176             l.rename(ldb.Dn(l, "dc=foo7"), ldb.Dn(l, "dc=bar"))
177             self.assertEquals(len(l.search()), 2)
178         finally:
179             l.delete(ldb.Dn(l, "dc=bar"))
180
181     def test_rename_string_dns(self):
182         l = ldb.Ldb(filename())
183         m = ldb.Message()
184         m.dn = ldb.Dn(l, "dc=foo8")
185         m["bla"] = "bla"
186         self.assertEquals(len(l.search()), 1)
187         l.add(m)
188         self.assertEquals(len(l.search()), 2)
189         try:
190             l.rename("dc=foo8", "dc=bar")
191             self.assertEquals(len(l.search()), 2)
192         finally:
193             l.delete(ldb.Dn(l, "dc=bar"))
194
195     def test_modify_delete(self):
196         l = ldb.Ldb(filename())
197         m = ldb.Message()
198         m.dn = ldb.Dn(l, "dc=modifydelete")
199         m["bla"] = ["1234"]
200         l.add(m)
201         rm = l.search(m.dn)[0]
202         self.assertEquals(["1234"], list(rm["bla"]))
203         try:
204             m = ldb.Message()
205             m.dn = ldb.Dn(l, "dc=modifydelete")
206             m["bla"] = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, "bla")
207             self.assertEquals(ldb.FLAG_MOD_DELETE, m["bla"].flags())
208             l.modify(m)
209             rm = l.search(m.dn)[0]
210             self.assertEquals(1, len(rm))
211             rm = l.search(m.dn, attrs=["bla"])[0]
212             self.assertEquals(0, len(rm))
213         finally:
214             l.delete(ldb.Dn(l, "dc=modifydelete"))
215
216     def test_modify_add(self):
217         l = ldb.Ldb(filename())
218         m = ldb.Message()
219         m.dn = ldb.Dn(l, "dc=add")
220         m["bla"] = ["1234"]
221         l.add(m)
222         try:
223             m = ldb.Message()
224             m.dn = ldb.Dn(l, "dc=add")
225             m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
226             self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
227             l.modify(m)
228             rm = l.search(m.dn)[0]
229             self.assertEquals(2, len(rm))
230             self.assertEquals(["1234", "456"], list(rm["bla"]))
231         finally:
232             l.delete(ldb.Dn(l, "dc=add"))
233
234     def test_modify_replace(self):
235         l = ldb.Ldb(filename())
236         m = ldb.Message()
237         m.dn = ldb.Dn(l, "dc=modify2")
238         m["bla"] = ["1234", "456"]
239         l.add(m)
240         try:
241             m = ldb.Message()
242             m.dn = ldb.Dn(l, "dc=modify2")
243             m["bla"] = ldb.MessageElement(["789"], ldb.FLAG_MOD_REPLACE, "bla")
244             self.assertEquals(ldb.FLAG_MOD_REPLACE, m["bla"].flags())
245             l.modify(m)
246             rm = l.search(m.dn)[0]
247             self.assertEquals(2, len(rm))
248             self.assertEquals(["789"], list(rm["bla"]))
249             rm = l.search(m.dn, attrs=["bla"])[0]
250             self.assertEquals(1, len(rm))
251         finally:
252             l.delete(ldb.Dn(l, "dc=modify2"))
253
254     def test_modify_flags_change(self):
255         l = ldb.Ldb(filename())
256         m = ldb.Message()
257         m.dn = ldb.Dn(l, "dc=add")
258         m["bla"] = ["1234"]
259         l.add(m)
260         try:
261             m = ldb.Message()
262             m.dn = ldb.Dn(l, "dc=add")
263             m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
264             self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
265             l.modify(m)
266             rm = l.search(m.dn)[0]
267             self.assertEquals(2, len(rm))
268             self.assertEquals(["1234", "456"], list(rm["bla"]))
269             
270             #Now create another modify, but switch the flags before we do it
271             m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
272             m["bla"].set_flags(ldb.FLAG_MOD_DELETE)
273             l.modify(m)
274             rm = l.search(m.dn, attrs=["bla"])[0]
275             self.assertEquals(1, len(rm))
276             self.assertEquals(["1234"], list(rm["bla"]))
277             
278         finally:
279             l.delete(ldb.Dn(l, "dc=add"))
280
281     def test_transaction_commit(self):
282         l = ldb.Ldb(filename())
283         l.transaction_start()
284         m = ldb.Message(ldb.Dn(l, "dc=foo9"))
285         m["foo"] = ["bar"]
286         l.add(m)
287         l.transaction_commit()
288         l.delete(m.dn)
289
290     def test_transaction_cancel(self):
291         l = ldb.Ldb(filename())
292         l.transaction_start()
293         m = ldb.Message(ldb.Dn(l, "dc=foo10"))
294         m["foo"] = ["bar"]
295         l.add(m)
296         l.transaction_cancel()
297         self.assertEquals(0, len(l.search(ldb.Dn(l, "dc=foo10"))))
298
299     def test_set_debug(self):
300         def my_report_fn(level, text):
301             pass
302         l = ldb.Ldb(filename())
303         l.set_debug(my_report_fn)
304
305     def test_zero_byte_string(self):
306         """Testing we do not get trapped in the \0 byte in a property string."""
307         l = ldb.Ldb(filename())
308         l.add({
309             "dn" : "dc=somedn",
310             "objectclass" : "user",
311             "cN" : "LDAPtestUSER",
312             "givenname" : "ldap",
313             "displayname" : "foo\0bar",
314         })
315         res = l.search(expression="(dn=dc=somedn)")
316         self.assertEquals("foo\0bar", res[0]["displayname"][0])
317
318
319 class DnTests(unittest.TestCase):
320
321     def setUp(self):
322         self.ldb = ldb.Ldb(filename())
323
324     def test_set_dn_invalid(self):
325         x = ldb.Message()
326         def assign():
327             x.dn = "astring"
328         self.assertRaises(TypeError, assign)
329
330     def test_eq(self):
331         x = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
332         y = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
333         self.assertEquals(x, y)
334
335     def test_str(self):
336         x = ldb.Dn(self.ldb, "dc=foo12,bar=bloe")
337         self.assertEquals(x.__str__(), "dc=foo12,bar=bloe")
338
339     def test_repr(self):
340         x = ldb.Dn(self.ldb, "dc=foo13,bla=blie")
341         self.assertEquals(x.__repr__(), "Dn('dc=foo13,bla=blie')")
342
343     def test_get_casefold(self):
344         x = ldb.Dn(self.ldb, "dc=foo14,bar=bloe")
345         self.assertEquals(x.get_casefold(), "DC=FOO14,BAR=bloe")
346
347     def test_validate(self):
348         x = ldb.Dn(self.ldb, "dc=foo15,bar=bloe")
349         self.assertTrue(x.validate())
350
351     def test_parent(self):
352         x = ldb.Dn(self.ldb, "dc=foo16,bar=bloe")
353         self.assertEquals("bar=bloe", x.parent().__str__())
354
355     def test_parent_nonexistant(self):
356         x = ldb.Dn(self.ldb, "@BLA")
357         self.assertEquals(None, x.parent())
358
359     def test_compare(self):
360         x = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
361         y = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
362         self.assertEquals(x, y)
363         z = ldb.Dn(self.ldb, "dc=foo17,bar=blie")
364         self.assertNotEquals(z, y)
365
366     def test_is_valid(self):
367         x = ldb.Dn(self.ldb, "dc=foo18,dc=bloe")
368         self.assertTrue(x.is_valid())
369         x = ldb.Dn(self.ldb, "")
370         # is_valid()'s return values appears to be a side effect of 
371         # some other ldb functions. yuck.
372         # self.assertFalse(x.is_valid())
373
374     def test_is_special(self):
375         x = ldb.Dn(self.ldb, "dc=foo19,bar=bloe")
376         self.assertFalse(x.is_special())
377         x = ldb.Dn(self.ldb, "@FOOBAR")
378         self.assertTrue(x.is_special())
379
380     def test_check_special(self):
381         x = ldb.Dn(self.ldb, "dc=foo20,bar=bloe")
382         self.assertFalse(x.check_special("FOOBAR"))
383         x = ldb.Dn(self.ldb, "@FOOBAR")
384         self.assertTrue(x.check_special("@FOOBAR"))
385
386     def test_len(self):
387         x = ldb.Dn(self.ldb, "dc=foo21,bar=bloe")
388         self.assertEquals(2, len(x))
389         x = ldb.Dn(self.ldb, "dc=foo21")
390         self.assertEquals(1, len(x))
391
392     def test_add_child(self):
393         x = ldb.Dn(self.ldb, "dc=foo22,bar=bloe")
394         self.assertTrue(x.add_child(ldb.Dn(self.ldb, "bla=bloe")))
395         self.assertEquals("bla=bloe,dc=foo22,bar=bloe", x.__str__())
396
397     def test_add_base(self):
398         x = ldb.Dn(self.ldb, "dc=foo23,bar=bloe")
399         base = ldb.Dn(self.ldb, "bla=bloe")
400         self.assertTrue(x.add_base(base))
401         self.assertEquals("dc=foo23,bar=bloe,bla=bloe", x.__str__())
402
403     def test_add(self):
404         x = ldb.Dn(self.ldb, "dc=foo24")
405         y = ldb.Dn(self.ldb, "bar=bla")
406         self.assertEquals("dc=foo24,bar=bla", str(y + x))
407
408     def test_parse_ldif(self):
409         msgs = self.ldb.parse_ldif("dn: foo=bar\n")
410         msg = msgs.next()
411         self.assertEquals("foo=bar", str(msg[1].dn))
412         self.assertTrue(isinstance(msg[1], ldb.Message))
413         ldif = self.ldb.write_ldif(msg[1], ldb.CHANGETYPE_NONE)
414         self.assertEquals("dn: foo=bar\n\n", ldif)
415         
416     def test_parse_ldif_more(self):
417         msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
418         msg = msgs.next()
419         self.assertEquals("foo=bar", str(msg[1].dn))
420         msg = msgs.next()
421         self.assertEquals("bar=bar", str(msg[1].dn))
422
423     def test_canonical_string(self):
424         x = ldb.Dn(self.ldb, "dc=foo25,bar=bloe")
425         self.assertEquals("/bloe/foo25", x.canonical_str())
426
427     def test_canonical_ex_string(self):
428         x = ldb.Dn(self.ldb, "dc=foo26,bar=bloe")
429         self.assertEquals("/bloe\nfoo26", x.canonical_ex_str())
430
431
432 class LdbMsgTests(unittest.TestCase):
433
434     def setUp(self):
435         self.msg = ldb.Message()
436
437     def test_init_dn(self):
438         self.msg = ldb.Message(ldb.Dn(ldb.Ldb(), "dc=foo27"))
439         self.assertEquals("dc=foo27", str(self.msg.dn))
440
441     def test_iter_items(self):
442         self.assertEquals(0, len(self.msg.items()))
443         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo28")
444         self.assertEquals(1, len(self.msg.items()))
445
446     def test_repr(self):
447         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo29")
448         self.msg["dc"] = "foo"
449         self.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self.msg))
450
451     def test_len(self):
452         self.assertEquals(0, len(self.msg))
453
454     def test_notpresent(self):
455         self.assertRaises(KeyError, lambda: self.msg["foo"])
456
457     def test_del(self):
458         del self.msg["foo"]
459
460     def test_add_value(self):
461         self.assertEquals(0, len(self.msg))
462         self.msg["foo"] = ["foo"]
463         self.assertEquals(1, len(self.msg))
464
465     def test_add_value_multiple(self):
466         self.assertEquals(0, len(self.msg))
467         self.msg["foo"] = ["foo", "bla"]
468         self.assertEquals(1, len(self.msg))
469         self.assertEquals(["foo", "bla"], list(self.msg["foo"]))
470
471     def test_set_value(self):
472         self.msg["foo"] = ["fool"]
473         self.assertEquals(["fool"], list(self.msg["foo"]))
474         self.msg["foo"] = ["bar"]
475         self.assertEquals(["bar"], list(self.msg["foo"]))
476
477     def test_keys(self):
478         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
479         self.msg["foo"] = ["bla"]
480         self.msg["bar"] = ["bla"]
481         self.assertEquals(["dn", "foo", "bar"], self.msg.keys())
482
483     def test_dn(self):
484         self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO")
485         self.assertEquals("@BASEINFO", self.msg.dn.__str__())
486
487     def test_get_dn(self):
488         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
489         self.assertEquals("@BASEINFO", self.msg.get("dn").__str__())
490
491     def test_get_invalid(self):
492         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
493         self.assertRaises(TypeError, self.msg.get, 42)
494
495     def test_get_other(self):
496         self.msg["foo"] = ["bar"]
497         self.assertEquals("bar", self.msg.get("foo")[0])
498
499     def test_get_unknown(self):
500         self.assertEquals(None, self.msg.get("lalalala"))
501
502     def test_msg_diff(self):
503         l = ldb.Ldb()
504         msgs = l.parse_ldif("dn: foo=bar\nfoo: bar\nbaz: do\n\ndn: foo=bar\nfoo: bar\nbaz: dont\n")
505         msg1 = msgs.next()[1]
506         msg2 = msgs.next()[1]
507         msgdiff = l.msg_diff(msg1, msg2)
508         self.assertEquals("foo=bar", msgdiff.get("dn").__str__())
509         self.assertRaises(KeyError, lambda: msgdiff["foo"])
510         self.assertEquals(1, len(msgdiff))
511
512
513
514 class MessageElementTests(unittest.TestCase):
515
516     def test_cmp_element(self):
517         x = ldb.MessageElement(["foo"])
518         y = ldb.MessageElement(["foo"])
519         z = ldb.MessageElement(["bzr"])
520         self.assertEquals(x, y)
521         self.assertNotEquals(x, z)
522
523     def test_create_iterable(self):
524         x = ldb.MessageElement(["foo"])
525         self.assertEquals(["foo"], list(x))
526
527     def test_repr(self):
528         x = ldb.MessageElement(["foo"])
529         self.assertEquals("MessageElement(['foo'])", repr(x))
530         x = ldb.MessageElement(["foo", "bla"])
531         self.assertEquals(2, len(x))
532         self.assertEquals("MessageElement(['foo','bla'])", repr(x))
533
534     def test_get_item(self):
535         x = ldb.MessageElement(["foo", "bar"])
536         self.assertEquals("foo", x[0])
537         self.assertEquals("bar", x[1])
538         self.assertEquals("bar", x[-1])
539         self.assertRaises(IndexError, lambda: x[45])
540
541     def test_len(self):
542         x = ldb.MessageElement(["foo", "bar"])
543         self.assertEquals(2, len(x))
544
545     def test_eq(self):
546         x = ldb.MessageElement(["foo", "bar"])
547         y = ldb.MessageElement(["foo", "bar"])
548         self.assertEquals(y, x)
549         x = ldb.MessageElement(["foo"])
550         self.assertNotEquals(y, x)
551         y = ldb.MessageElement(["foo"])
552         self.assertEquals(y, x)
553
554
555 class ModuleTests(unittest.TestCase):
556
557     def test_register_module(self):
558         class ExampleModule:
559             name = "example"
560         ldb.register_module(ExampleModule)
561
562     def test_use_module(self):
563         ops = []
564         class ExampleModule:
565             name = "bla"
566
567             def __init__(self, ldb, next):
568                 ops.append("init")
569                 self.next = next
570
571             def search(self, *args, **kwargs):
572                 return self.next.search(*args, **kwargs)
573
574         ldb.register_module(ExampleModule)
575         if os.path.exists("usemodule.ldb"):
576             os.unlink("usemodule.ldb")
577         l = ldb.Ldb("usemodule.ldb")
578         l.add({"dn": "@MODULES", "@LIST": "bla"})
579         self.assertEquals([], ops)
580         l = ldb.Ldb("usemodule.ldb")
581         self.assertEquals(["init"], ops)
582
583
584 if __name__ == '__main__':
585     import unittest
586     unittest.TestProgram()