Add support for implementing LDB modules in Python.
[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 sys
6 import unittest
7
8 # Required for the standalone LDB build
9 sys.path.append("swig")
10 sys.path.append("build/lib.linux-i686-2.4")
11
12 import ldb
13
14 class NoContextTests(unittest.TestCase):
15     def test_valid_attr_name(self):
16         self.assertTrue(ldb.valid_attr_name("foo"))
17         self.assertFalse(ldb.valid_attr_name("24foo"))
18
19     def test_timestring(self):
20         self.assertEquals("19700101000000.0Z", ldb.timestring(0))
21         self.assertEquals("20071119191012.0Z", ldb.timestring(1195499412))
22
23     def test_string_to_time(self):
24         self.assertEquals(0, ldb.string_to_time("19700101000000.0Z"))
25         self.assertEquals(1195499412, ldb.string_to_time("20071119191012.0Z"))
26
27
28 class SimpleLdb(unittest.TestCase):
29     def test_connect(self):
30         ldb.Ldb("foo.ldb")
31
32     def test_connect_none(self):
33         ldb.Ldb()
34
35     def test_connect_later(self):
36         x = ldb.Ldb()
37         x.connect("foo.ldb")
38
39     def test_repr(self):
40         x = ldb.Ldb()
41         self.assertTrue(repr(x).startswith("<ldb connection"))
42
43     def test_set_create_perms(self):
44         x = ldb.Ldb()
45         x.set_create_perms(0600)
46
47     def test_set_modules_dir(self):
48         x = ldb.Ldb()
49         x.set_modules_dir("/tmp")
50
51     def test_modules_none(self):
52         x = ldb.Ldb()
53         self.assertEquals([], x.modules())
54
55     def test_modules_tdb(self):
56         x = ldb.Ldb("bar.ldb")
57         self.assertEquals("[<ldb module 'tdb'>]", repr(x.modules()))
58
59     def test_search(self):
60         l = ldb.Ldb("foo.ldb")
61         self.assertEquals(len(l.search()), 1)
62
63     def test_search_controls(self):
64         l = ldb.Ldb("foo.ldb")
65         self.assertEquals(len(l.search(controls=["paged_results:0:5"])), 1)
66
67     def test_search_attrs(self):
68         l = ldb.Ldb("foo.ldb")
69         self.assertEquals(len(l.search(ldb.Dn(l, ""), ldb.SCOPE_SUBTREE, "(dc=*)", ["dc"])), 0)
70
71     def test_search_string_dn(self):
72         l = ldb.Ldb("foo.ldb")
73         self.assertEquals(len(l.search("", ldb.SCOPE_SUBTREE, "(dc=*)", ["dc"])), 0)
74
75     def test_search_attr_string(self):
76         l = ldb.Ldb("foo.tdb")
77         self.assertRaises(TypeError, l.search, attrs="dc")
78
79     def test_opaque(self):
80         l = ldb.Ldb("foo.ldb")
81         l.set_opaque("my_opaque", l)
82         self.assertTrue(l.get_opaque("my_opaque") is not None)
83         self.assertEquals(None, l.get_opaque("unknown"))
84
85     def test_parse_control_strings(self):
86         l = ldb.Ldb("foo.ldb")
87         self.assertRaises(ldb.LdbError, l.parse_control_strings, ["foo", "bar"])
88         self.assertTrue(l.parse_control_strings(["paged_results:0:5"]) is not None)
89
90     def test_search_scope_base(self):
91         l = ldb.Ldb("foo.ldb")
92         self.assertEquals(len(l.search(ldb.Dn(l, "dc=foo"), 
93                           ldb.SCOPE_ONELEVEL)), 0)
94
95     def test_delete(self):
96         l = ldb.Ldb("foo.ldb")
97         self.assertRaises(ldb.LdbError, lambda: l.delete(ldb.Dn(l, "dc=foo")))
98
99     def test_contains(self):
100         l = ldb.Ldb("foo.ldb")
101         self.assertFalse(ldb.Dn(l, "dc=foo") in l)
102         l = ldb.Ldb("foo.ldb")
103         m = ldb.Message()
104         m.dn = ldb.Dn(l, "dc=foo")
105         m["b"] = ["a"]
106         l.add(m)
107         try:
108             self.assertTrue(ldb.Dn(l, "dc=foo") in l)
109         finally:
110             l.delete(m.dn)
111
112     def test_get_config_basedn(self):
113         l = ldb.Ldb("foo.ldb")
114         self.assertEquals(None, l.get_config_basedn())
115
116     def test_get_root_basedn(self):
117         l = ldb.Ldb("foo.ldb")
118         self.assertEquals(None, l.get_root_basedn())
119
120     def test_get_schema_basedn(self):
121         l = ldb.Ldb("foo.ldb")
122         self.assertEquals(None, l.get_schema_basedn())
123
124     def test_get_default_basedn(self):
125         l = ldb.Ldb("foo.ldb")
126         self.assertEquals(None, l.get_default_basedn())
127
128     def test_add(self):
129         l = ldb.Ldb("foo.ldb")
130         m = ldb.Message()
131         m.dn = ldb.Dn(l, "dc=foo")
132         m["bla"] = "bla"
133         self.assertEquals(len(l.search()), 1)
134         l.add(m)
135         try:
136             self.assertEquals(len(l.search()), 2)
137         finally:
138             l.delete(ldb.Dn(l, "dc=foo"))
139
140     def test_add_dict(self):
141         l = ldb.Ldb("foo.ldb")
142         m = {"dn": ldb.Dn(l, "dc=foo"),
143              "bla": "bla"}
144         self.assertEquals(len(l.search()), 1)
145         l.add(m)
146         try:
147             self.assertEquals(len(l.search()), 2)
148         finally:
149             l.delete(ldb.Dn(l, "dc=foo"))
150
151     def test_add_dict_string_dn(self):
152         l = ldb.Ldb("foo.ldb")
153         m = {"dn": "dc=foo", "bla": "bla"}
154         self.assertEquals(len(l.search()), 1)
155         l.add(m)
156         try:
157             self.assertEquals(len(l.search()), 2)
158         finally:
159             l.delete(ldb.Dn(l, "dc=foo"))
160
161     def test_rename(self):
162         l = ldb.Ldb("foo.ldb")
163         m = ldb.Message()
164         m.dn = ldb.Dn(l, "dc=foo")
165         m["bla"] = "bla"
166         self.assertEquals(len(l.search()), 1)
167         l.add(m)
168         try:
169             l.rename(ldb.Dn(l, "dc=foo"), ldb.Dn(l, "dc=bar"))
170             self.assertEquals(len(l.search()), 2)
171         finally:
172             l.delete(ldb.Dn(l, "dc=bar"))
173
174     def test_rename_string_dns(self):
175         l = ldb.Ldb("foo.ldb")
176         m = ldb.Message()
177         m.dn = ldb.Dn(l, "dc=foo")
178         m["bla"] = "bla"
179         self.assertEquals(len(l.search()), 1)
180         l.add(m)
181         try:
182             l.rename("dc=foo", "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("foo.ldb")
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.CHANGETYPE_DELETE, "bla")
199             l.modify(m)
200             rm = l.search(m.dn)[0]
201             self.assertEquals(1, len(rm))
202         finally:
203             l.delete(ldb.Dn(l, "dc=modifydelete"))
204
205     def test_modify_add(self):
206         l = ldb.Ldb("foo.ldb")
207         m = ldb.Message()
208         m.dn = ldb.Dn(l, "dc=add")
209         m["bla"] = ["1234"]
210         l.add(m)
211         try:
212             m = ldb.Message()
213             m.dn = ldb.Dn(l, "dc=add")
214             m["bla"] = ldb.MessageElement(["456"], ldb.CHANGETYPE_ADD, "bla")
215             l.modify(m)
216             rm = l.search(m.dn)[0]
217             self.assertEquals(2, len(rm))
218             self.assertEquals(["1234", "456"], list(rm["bla"]))
219         finally:
220             l.delete(ldb.Dn(l, "dc=add"))
221
222     def test_modify_modify(self):
223         l = ldb.Ldb("foo.ldb")
224         m = ldb.Message()
225         m.dn = ldb.Dn(l, "dc=modify2")
226         m["bla"] = ["1234", "456"]
227         l.add(m)
228         try:
229             m = ldb.Message()
230             m.dn = ldb.Dn(l, "dc=modify2")
231             m["bla"] = ldb.MessageElement(["456"], ldb.CHANGETYPE_MODIFY, "bla")
232             l.modify(m)
233             rm = l.search(m.dn)[0]
234             self.assertEquals(2, len(rm))
235             self.assertEquals(["1234"], list(rm["bla"]))
236         finally:
237             l.delete(ldb.Dn(l, "dc=modify2"))
238
239     def test_transaction_commit(self):
240         l = ldb.Ldb("foo.ldb")
241         l.transaction_start()
242         m = ldb.Message(ldb.Dn(l, "dc=foo"))
243         m["foo"] = ["bar"]
244         l.add(m)
245         l.transaction_commit()
246         l.delete(m.dn)
247
248     def test_transaction_cancel(self):
249         l = ldb.Ldb("foo.ldb")
250         l.transaction_start()
251         m = ldb.Message(ldb.Dn(l, "dc=foo"))
252         m["foo"] = ["bar"]
253         l.add(m)
254         l.transaction_cancel()
255         self.assertEquals(0, len(l.search(ldb.Dn(l, "dc=foo"))))
256
257     def test_set_debug(self):
258         def my_report_fn(level, text):
259             pass
260         l = ldb.Ldb("foo.ldb")
261         l.set_debug(my_report_fn)
262
263
264 class DnTests(unittest.TestCase):
265     def setUp(self):
266         self.ldb = ldb.Ldb("foo.ldb")
267
268     def test_eq_str(self):
269         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
270         self.assertEquals("dc=foo,bar=bloe", x)
271
272     def test_str(self):
273         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
274         self.assertEquals(x.__str__(), "dc=foo,bar=bloe")
275
276     def test_repr(self):
277         x = ldb.Dn(self.ldb, "dc=foo,bla=blie")
278         self.assertEquals(x.__repr__(), "Dn('dc=foo,bla=blie')")
279
280     def test_get_casefold(self):
281         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
282         self.assertEquals(x.get_casefold(), "DC=FOO,BAR=bloe")
283
284     def test_validate(self):
285         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
286         self.assertTrue(x.validate())
287
288     def test_parent(self):
289         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
290         self.assertEquals("bar=bloe", x.parent().__str__())
291
292     def test_compare(self):
293         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
294         y = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
295         self.assertEquals(x, y)
296         z = ldb.Dn(self.ldb, "dc=foo,bar=blie")
297         self.assertNotEquals(z, y)
298
299     def test_is_valid(self):
300         x = ldb.Dn(self.ldb, "dc=foo,dc=bloe")
301         self.assertTrue(x.is_valid())
302         x = ldb.Dn(self.ldb, "")
303         # is_valid()'s return values appears to be a side effect of 
304         # some other ldb functions. yuck.
305         # self.assertFalse(x.is_valid())
306
307     def test_is_special(self):
308         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
309         self.assertFalse(x.is_special())
310         x = ldb.Dn(self.ldb, "@FOOBAR")
311         self.assertTrue(x.is_special())
312
313     def test_check_special(self):
314         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
315         self.assertFalse(x.check_special("FOOBAR"))
316         x = ldb.Dn(self.ldb, "@FOOBAR")
317         self.assertTrue(x.check_special("@FOOBAR"))
318
319     def test_len(self):
320         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
321         self.assertEquals(2, len(x))
322         x = ldb.Dn(self.ldb, "dc=foo")
323         self.assertEquals(1, len(x))
324
325     def test_add_child(self):
326         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
327         self.assertTrue(x.add_child(ldb.Dn(self.ldb, "bla=bloe")))
328         self.assertEquals("bla=bloe,dc=foo,bar=bloe", x.__str__())
329
330     def test_add_base(self):
331         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
332         self.assertTrue(x.add_base(ldb.Dn(self.ldb, "bla=bloe")))
333         self.assertEquals("dc=foo,bar=bloe,bla=bloe", x.__str__())
334
335     def test_add(self):
336         x = ldb.Dn(self.ldb, "dc=foo")
337         y = ldb.Dn(self.ldb, "bar=bla")
338         self.assertEquals("dc=foo,bar=bla", str(y + x))
339
340     def test_parse_ldif(self):
341         msgs = self.ldb.parse_ldif("dn: foo=bar\n")
342         msg = msgs.next()
343         self.assertEquals("foo=bar", str(msg[1].dn))
344         self.assertTrue(isinstance(msg[1], ldb.Message))
345
346     def test_parse_ldif_more(self):
347         msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
348         msg = msgs.next()
349         self.assertEquals("foo=bar", str(msg[1].dn))
350         msg = msgs.next()
351         self.assertEquals("bar=bar", str(msg[1].dn))
352
353     def test_canonical_string(self):
354         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
355         self.assertEquals("/bloe/foo", x.canonical_str())
356
357     def test_canonical_ex_string(self):
358         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
359         self.assertEquals("/bloe\nfoo", x.canonical_ex_str())
360
361
362 class LdbMsgTests(unittest.TestCase):
363     def setUp(self):
364         self.msg = ldb.Message()
365
366     def test_init_dn(self):
367         self.msg = ldb.Message(ldb.Dn(ldb.Ldb(), "dc=foo"))
368         self.assertEquals("dc=foo", str(self.msg.dn))
369
370     def test_iter_items(self):
371         self.assertEquals(0, len(self.msg.items()))
372         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo")
373         self.assertEquals(1, len(self.msg.items()))
374
375     def test_repr(self):
376         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo")
377         self.msg["dc"] = "foo"
378         self.assertEquals("Message({'dn': Dn('dc=foo'), 'dc': MessageElement(['foo'])})", repr(self.msg))
379
380     def test_len(self):
381         self.assertEquals(0, len(self.msg))
382
383     def test_notpresent(self):
384         self.assertRaises(KeyError, lambda: self.msg["foo"])
385
386     def test_del(self):
387         del self.msg["foo"]
388
389     def test_add_value(self):
390         self.assertEquals(0, len(self.msg))
391         self.msg["foo"] = ["foo"]
392         self.assertEquals(1, len(self.msg))
393
394     def test_add_value_multiple(self):
395         self.assertEquals(0, len(self.msg))
396         self.msg["foo"] = ["foo", "bla"]
397         self.assertEquals(1, len(self.msg))
398         self.assertEquals(["foo", "bla"], list(self.msg["foo"]))
399
400     def test_set_value(self):
401         self.msg["foo"] = ["fool"]
402         self.assertEquals(["fool"], list(self.msg["foo"]))
403         self.msg["foo"] = ["bar"]
404         self.assertEquals(["bar"], list(self.msg["foo"]))
405
406     def test_keys(self):
407         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
408         self.msg["foo"] = ["bla"]
409         self.msg["bar"] = ["bla"]
410         self.assertEquals(["dn", "foo", "bar"], self.msg.keys())
411
412     def test_dn(self):
413         self.msg.dn = ldb.Dn(ldb.Ldb("foo.ldb"), "@BASEINFO")
414         self.assertEquals("@BASEINFO", self.msg.dn.__str__())
415
416     def test_get_dn(self):
417         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
418         self.assertEquals("@BASEINFO", self.msg.get("dn").__str__())
419
420     def test_get_other(self):
421         self.msg["foo"] = ["bar"]
422         self.assertEquals("bar", self.msg.get("foo")[0])
423
424     def test_get_unknown(self):
425         self.assertRaises(KeyError, self.msg.get, "lalalala")
426
427
428 class MessageElementTests(unittest.TestCase):
429     def test_cmp_element(self):
430         x = ldb.MessageElement(["foo"])
431         y = ldb.MessageElement(["foo"])
432         z = ldb.MessageElement(["bzr"])
433         self.assertEquals(x, y)
434         self.assertNotEquals(x, z)
435
436     def test_create_iterable(self):
437         x = ldb.MessageElement(["foo"])
438         self.assertEquals(["foo"], list(x))
439
440     def test_repr(self):
441         x = ldb.MessageElement(["foo"])
442         self.assertEquals("MessageElement(['foo'])", repr(x))
443         x = ldb.MessageElement(["foo", "bla"])
444         self.assertEquals("MessageElement(['foo','bla'])", repr(x))
445
446     def test_get_item(self):
447         x = ldb.MessageElement(["foo", "bar"])
448         self.assertEquals("foo", x[0])
449         self.assertEquals("bar", x[1])
450         self.assertRaises(KeyError, lambda: x[-1])
451
452     def test_len(self):
453         x = ldb.MessageElement(["foo", "bar"])
454         self.assertEquals(2, len(x))
455
456     def test_eq(self):
457         x = ldb.MessageElement(["foo", "bar"])
458         self.assertEquals(["foo", "bar"], x)
459         x = ldb.MessageElement(["foo"])
460         self.assertEquals("foo", x)
461
462 class ModuleTests(unittest.TestCase):
463     def test_register_module(self):
464         class ExampleModule:
465             name = "example"
466         ldb.register_module(ExampleModule)
467
468     def test_use_module(self):
469         ops = []
470         class ExampleModule:
471             name = "bla"
472
473             def __init__(self, ldb, next):
474                 ops.append("init")
475
476         ldb.register_module(ExampleModule)
477         l = ldb.Ldb("usemodule.ldb")
478         l.add({"dn": "@MODULES",
479                  "@LIST": "bla"})
480         self.assertEquals([], ops)
481         l = ldb.Ldb("usemodule.ldb")
482         self.assertEquals(["init"], ops)
483
484 if __name__ == '__main__':
485     import unittest
486     unittest.TestProgram()