Setting nTSecurityDescriptor via LDAP fails
[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
262 class DnTests(unittest.TestCase):
263     def setUp(self):
264         self.ldb = ldb.Ldb(filename())
265
266     def test_eq(self):
267         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
268         y = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
269         self.assertEquals(x, y)
270
271     def test_str(self):
272         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
273         self.assertEquals(x.__str__(), "dc=foo,bar=bloe")
274
275     def test_repr(self):
276         x = ldb.Dn(self.ldb, "dc=foo,bla=blie")
277         self.assertEquals(x.__repr__(), "Dn('dc=foo,bla=blie')")
278
279     def test_get_casefold(self):
280         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
281         self.assertEquals(x.get_casefold(), "DC=FOO,BAR=bloe")
282
283     def test_validate(self):
284         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
285         self.assertTrue(x.validate())
286
287     def test_parent(self):
288         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
289         self.assertEquals("bar=bloe", x.parent().__str__())
290
291     def test_compare(self):
292         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
293         y = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
294         self.assertEquals(x, y)
295         z = ldb.Dn(self.ldb, "dc=foo,bar=blie")
296         self.assertNotEquals(z, y)
297
298     def test_is_valid(self):
299         x = ldb.Dn(self.ldb, "dc=foo,dc=bloe")
300         self.assertTrue(x.is_valid())
301         x = ldb.Dn(self.ldb, "")
302         # is_valid()'s return values appears to be a side effect of 
303         # some other ldb functions. yuck.
304         # self.assertFalse(x.is_valid())
305
306     def test_is_special(self):
307         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
308         self.assertFalse(x.is_special())
309         x = ldb.Dn(self.ldb, "@FOOBAR")
310         self.assertTrue(x.is_special())
311
312     def test_check_special(self):
313         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
314         self.assertFalse(x.check_special("FOOBAR"))
315         x = ldb.Dn(self.ldb, "@FOOBAR")
316         self.assertTrue(x.check_special("@FOOBAR"))
317
318     def test_len(self):
319         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
320         self.assertEquals(2, len(x))
321         x = ldb.Dn(self.ldb, "dc=foo")
322         self.assertEquals(1, len(x))
323
324     def test_add_child(self):
325         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
326         self.assertTrue(x.add_child(ldb.Dn(self.ldb, "bla=bloe")))
327         self.assertEquals("bla=bloe,dc=foo,bar=bloe", x.__str__())
328
329     def test_add_base(self):
330         x = ldb.Dn(self.ldb, "dc=foo,bar=bloe")
331         base = ldb.Dn(self.ldb, "bla=bloe")
332         self.assertTrue(x.add_base(base))
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(filename()), "@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.assertEquals(None, 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(2, len(x))
445         self.assertEquals("MessageElement(['foo','bla'])", repr(x))
446
447     def test_get_item(self):
448         x = ldb.MessageElement(["foo", "bar"])
449         self.assertEquals("foo", x[0])
450         self.assertEquals("bar", x[1])
451         self.assertEquals("bar", x[-1])
452         self.assertRaises(IndexError, lambda: x[45])
453
454     def test_len(self):
455         x = ldb.MessageElement(["foo", "bar"])
456         self.assertEquals(2, len(x))
457
458     def test_eq(self):
459         x = ldb.MessageElement(["foo", "bar"])
460         y = ldb.MessageElement(["foo", "bar"])
461         self.assertEquals(y, x)
462         x = ldb.MessageElement(["foo"])
463         self.assertNotEquals(y, x)
464         y = ldb.MessageElement(["foo"])
465         self.assertEquals(y, x)
466
467
468 class ModuleTests(unittest.TestCase):
469     def test_register_module(self):
470         class ExampleModule:
471             name = "example"
472         ldb.register_module(ExampleModule)
473
474     def test_use_module(self):
475         ops = []
476         class ExampleModule:
477             name = "bla"
478
479             def __init__(self, ldb, next):
480                 ops.append("init")
481                 self.next = next
482
483             def search(self, *args, **kwargs):
484                 return self.next.search(*args, **kwargs)
485
486         ldb.register_module(ExampleModule)
487         if os.path.exists("usemodule.ldb"):
488             os.unlink("usemodule.ldb")
489         l = ldb.Ldb("usemodule.ldb")
490         l.add({"dn": "@MODULES", "@LIST": "bla"})
491         self.assertEquals([], ops)
492         l = ldb.Ldb("usemodule.ldb")
493         self.assertEquals(["init"], ops)
494
495 if __name__ == '__main__':
496     import unittest
497     unittest.TestProgram()