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