s4:ldb Add python binding and test for ldb_msg_diff()
[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.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(filename())
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(filename())
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(filename())
241         l.transaction_start()
242         m = ldb.Message(ldb.Dn(l, "dc=foo9"))
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(filename())
250         l.transaction_start()
251         m = ldb.Message(ldb.Dn(l, "dc=foo10"))
252         m["foo"] = ["bar"]
253         l.add(m)
254         l.transaction_cancel()
255         self.assertEquals(0, len(l.search(ldb.Dn(l, "dc=foo10"))))
256
257     def test_set_debug(self):
258         def my_report_fn(level, text):
259             pass
260         l = ldb.Ldb(filename())
261         l.set_debug(my_report_fn)
262
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())
266         l.add({
267             "dn" : "dc=somedn",
268             "objectclass" : "user",
269             "cN" : "LDAPtestUSER",
270             "givenname" : "ldap",
271             "displayname" : "foo\0bar",
272         })
273         res = l.search(expression="(dn=dc=somedn)")
274         self.assertEquals("foo\0bar", res[0]["displayname"][0])
275
276
277 class DnTests(unittest.TestCase):
278
279     def setUp(self):
280         self.ldb = ldb.Ldb(filename())
281
282     def test_set_dn_invalid(self):
283         x = ldb.Message()
284         def assign():
285             x.dn = "astring"
286         self.assertRaises(TypeError, assign)
287
288     def test_eq(self):
289         x = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
290         y = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
291         self.assertEquals(x, y)
292
293     def test_str(self):
294         x = ldb.Dn(self.ldb, "dc=foo12,bar=bloe")
295         self.assertEquals(x.__str__(), "dc=foo12,bar=bloe")
296
297     def test_repr(self):
298         x = ldb.Dn(self.ldb, "dc=foo13,bla=blie")
299         self.assertEquals(x.__repr__(), "Dn('dc=foo13,bla=blie')")
300
301     def test_get_casefold(self):
302         x = ldb.Dn(self.ldb, "dc=foo14,bar=bloe")
303         self.assertEquals(x.get_casefold(), "DC=FOO14,BAR=bloe")
304
305     def test_validate(self):
306         x = ldb.Dn(self.ldb, "dc=foo15,bar=bloe")
307         self.assertTrue(x.validate())
308
309     def test_parent(self):
310         x = ldb.Dn(self.ldb, "dc=foo16,bar=bloe")
311         self.assertEquals("bar=bloe", x.parent().__str__())
312
313     def test_parent_nonexistant(self):
314         x = ldb.Dn(self.ldb, "@BLA")
315         self.assertEquals(None, x.parent())
316
317     def test_compare(self):
318         x = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
319         y = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
320         self.assertEquals(x, y)
321         z = ldb.Dn(self.ldb, "dc=foo17,bar=blie")
322         self.assertNotEquals(z, y)
323
324     def test_is_valid(self):
325         x = ldb.Dn(self.ldb, "dc=foo18,dc=bloe")
326         self.assertTrue(x.is_valid())
327         x = ldb.Dn(self.ldb, "")
328         # is_valid()'s return values appears to be a side effect of 
329         # some other ldb functions. yuck.
330         # self.assertFalse(x.is_valid())
331
332     def test_is_special(self):
333         x = ldb.Dn(self.ldb, "dc=foo19,bar=bloe")
334         self.assertFalse(x.is_special())
335         x = ldb.Dn(self.ldb, "@FOOBAR")
336         self.assertTrue(x.is_special())
337
338     def test_check_special(self):
339         x = ldb.Dn(self.ldb, "dc=foo20,bar=bloe")
340         self.assertFalse(x.check_special("FOOBAR"))
341         x = ldb.Dn(self.ldb, "@FOOBAR")
342         self.assertTrue(x.check_special("@FOOBAR"))
343
344     def test_len(self):
345         x = ldb.Dn(self.ldb, "dc=foo21,bar=bloe")
346         self.assertEquals(2, len(x))
347         x = ldb.Dn(self.ldb, "dc=foo21")
348         self.assertEquals(1, len(x))
349
350     def test_add_child(self):
351         x = ldb.Dn(self.ldb, "dc=foo22,bar=bloe")
352         self.assertTrue(x.add_child(ldb.Dn(self.ldb, "bla=bloe")))
353         self.assertEquals("bla=bloe,dc=foo22,bar=bloe", x.__str__())
354
355     def test_add_base(self):
356         x = ldb.Dn(self.ldb, "dc=foo23,bar=bloe")
357         base = ldb.Dn(self.ldb, "bla=bloe")
358         self.assertTrue(x.add_base(base))
359         self.assertEquals("dc=foo23,bar=bloe,bla=bloe", x.__str__())
360
361     def test_add(self):
362         x = ldb.Dn(self.ldb, "dc=foo24")
363         y = ldb.Dn(self.ldb, "bar=bla")
364         self.assertEquals("dc=foo24,bar=bla", str(y + x))
365
366     def test_parse_ldif(self):
367         msgs = self.ldb.parse_ldif("dn: foo=bar\n")
368         msg = msgs.next()
369         self.assertEquals("foo=bar", str(msg[1].dn))
370         self.assertTrue(isinstance(msg[1], ldb.Message))
371
372     def test_parse_ldif_more(self):
373         msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
374         msg = msgs.next()
375         self.assertEquals("foo=bar", str(msg[1].dn))
376         msg = msgs.next()
377         self.assertEquals("bar=bar", str(msg[1].dn))
378
379     def test_canonical_string(self):
380         x = ldb.Dn(self.ldb, "dc=foo25,bar=bloe")
381         self.assertEquals("/bloe/foo25", x.canonical_str())
382
383     def test_canonical_ex_string(self):
384         x = ldb.Dn(self.ldb, "dc=foo26,bar=bloe")
385         self.assertEquals("/bloe\nfoo26", x.canonical_ex_str())
386
387
388 class LdbMsgTests(unittest.TestCase):
389
390     def setUp(self):
391         self.msg = ldb.Message()
392
393     def test_init_dn(self):
394         self.msg = ldb.Message(ldb.Dn(ldb.Ldb(), "dc=foo27"))
395         self.assertEquals("dc=foo27", str(self.msg.dn))
396
397     def test_iter_items(self):
398         self.assertEquals(0, len(self.msg.items()))
399         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo28")
400         self.assertEquals(1, len(self.msg.items()))
401
402     def test_repr(self):
403         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo29")
404         self.msg["dc"] = "foo"
405         self.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self.msg))
406
407     def test_len(self):
408         self.assertEquals(0, len(self.msg))
409
410     def test_notpresent(self):
411         self.assertRaises(KeyError, lambda: self.msg["foo"])
412
413     def test_del(self):
414         del self.msg["foo"]
415
416     def test_add_value(self):
417         self.assertEquals(0, len(self.msg))
418         self.msg["foo"] = ["foo"]
419         self.assertEquals(1, len(self.msg))
420
421     def test_add_value_multiple(self):
422         self.assertEquals(0, len(self.msg))
423         self.msg["foo"] = ["foo", "bla"]
424         self.assertEquals(1, len(self.msg))
425         self.assertEquals(["foo", "bla"], list(self.msg["foo"]))
426
427     def test_set_value(self):
428         self.msg["foo"] = ["fool"]
429         self.assertEquals(["fool"], list(self.msg["foo"]))
430         self.msg["foo"] = ["bar"]
431         self.assertEquals(["bar"], list(self.msg["foo"]))
432
433     def test_keys(self):
434         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
435         self.msg["foo"] = ["bla"]
436         self.msg["bar"] = ["bla"]
437         self.assertEquals(["dn", "foo", "bar"], self.msg.keys())
438
439     def test_dn(self):
440         self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO")
441         self.assertEquals("@BASEINFO", self.msg.dn.__str__())
442
443     def test_get_dn(self):
444         self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
445         self.assertEquals("@BASEINFO", self.msg.get("dn").__str__())
446
447     def test_get_other(self):
448         self.msg["foo"] = ["bar"]
449         self.assertEquals("bar", self.msg.get("foo")[0])
450
451     def test_get_unknown(self):
452         self.assertEquals(None, self.msg.get("lalalala"))
453
454     def test_msg_diff(self):
455         l = ldb.Ldb()
456         msgs = l.parse_ldif("dn: foo=bar\nfoo: bar\nbaz: do\n\ndn: foo=bar\nfoo: bar\nbaz: dont\n")
457         msg1 = msgs.next()[1]
458         msg2 = msgs.next()[1]
459         msgdiff = l.msg_diff(msg1, msg2)
460         self.assertEquals("foo=bar", msgdiff.get("dn").__str__())
461         self.assertRaises(KeyError, lambda: msgdiff["foo"])
462         self.assertEquals(1, len(msgdiff))
463
464
465
466 class MessageElementTests(unittest.TestCase):
467
468     def test_cmp_element(self):
469         x = ldb.MessageElement(["foo"])
470         y = ldb.MessageElement(["foo"])
471         z = ldb.MessageElement(["bzr"])
472         self.assertEquals(x, y)
473         self.assertNotEquals(x, z)
474
475     def test_create_iterable(self):
476         x = ldb.MessageElement(["foo"])
477         self.assertEquals(["foo"], list(x))
478
479     def test_repr(self):
480         x = ldb.MessageElement(["foo"])
481         self.assertEquals("MessageElement(['foo'])", repr(x))
482         x = ldb.MessageElement(["foo", "bla"])
483         self.assertEquals(2, len(x))
484         self.assertEquals("MessageElement(['foo','bla'])", repr(x))
485
486     def test_get_item(self):
487         x = ldb.MessageElement(["foo", "bar"])
488         self.assertEquals("foo", x[0])
489         self.assertEquals("bar", x[1])
490         self.assertEquals("bar", x[-1])
491         self.assertRaises(IndexError, lambda: x[45])
492
493     def test_len(self):
494         x = ldb.MessageElement(["foo", "bar"])
495         self.assertEquals(2, len(x))
496
497     def test_eq(self):
498         x = ldb.MessageElement(["foo", "bar"])
499         y = ldb.MessageElement(["foo", "bar"])
500         self.assertEquals(y, x)
501         x = ldb.MessageElement(["foo"])
502         self.assertNotEquals(y, x)
503         y = ldb.MessageElement(["foo"])
504         self.assertEquals(y, x)
505
506
507 class ModuleTests(unittest.TestCase):
508
509     def test_register_module(self):
510         class ExampleModule:
511             name = "example"
512         ldb.register_module(ExampleModule)
513
514     def test_use_module(self):
515         ops = []
516         class ExampleModule:
517             name = "bla"
518
519             def __init__(self, ldb, next):
520                 ops.append("init")
521                 self.next = next
522
523             def search(self, *args, **kwargs):
524                 return self.next.search(*args, **kwargs)
525
526         ldb.register_module(ExampleModule)
527         if os.path.exists("usemodule.ldb"):
528             os.unlink("usemodule.ldb")
529         l = ldb.Ldb("usemodule.ldb")
530         l.add({"dn": "@MODULES", "@LIST": "bla"})
531         self.assertEquals([], ops)
532         l = ldb.Ldb("usemodule.ldb")
533         self.assertEquals(["init"], ops)
534
535
536 if __name__ == '__main__':
537     import unittest
538     unittest.TestProgram()