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