pyldb: Fix three more (minor) memory leaks.
[Samba/gebeck_regimport.git] / source4 / lib / ldb / tests / python / api.py
blob05e3da782edc42b1e8d53dabb71f58d317e54cd5
1 #!/usr/bin/python
2 # Simple tests for the ldb python bindings.
3 # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
5 import os, sys
6 import unittest
8 # Required for the standalone LDB build
9 sys.path.append("build/lib.linux-i686-2.4")
11 import ldb
13 def filename():
14 return os.tempnam()
16 class NoContextTests(unittest.TestCase):
18 def test_valid_attr_name(self):
19 self.assertTrue(ldb.valid_attr_name("foo"))
20 self.assertFalse(ldb.valid_attr_name("24foo"))
22 def test_timestring(self):
23 self.assertEquals("19700101000000.0Z", ldb.timestring(0))
24 self.assertEquals("20071119191012.0Z", ldb.timestring(1195499412))
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"))
31 class SimpleLdb(unittest.TestCase):
33 def test_connect(self):
34 ldb.Ldb(filename())
36 def test_connect_none(self):
37 ldb.Ldb()
39 def test_connect_later(self):
40 x = ldb.Ldb()
41 x.connect(filename())
43 def test_repr(self):
44 x = ldb.Ldb()
45 self.assertTrue(repr(x).startswith("<ldb connection"))
47 def test_set_create_perms(self):
48 x = ldb.Ldb()
49 x.set_create_perms(0600)
51 def test_set_modules_dir(self):
52 x = ldb.Ldb()
53 x.set_modules_dir("/tmp")
55 def test_modules_none(self):
56 x = ldb.Ldb()
57 self.assertEquals([], x.modules())
59 def test_modules_tdb(self):
60 x = ldb.Ldb("bar.ldb")
61 self.assertEquals("[<ldb module 'tdb'>]", repr(x.modules()))
63 def test_search(self):
64 l = ldb.Ldb(filename())
65 self.assertEquals(len(l.search()), 1)
67 def test_search_controls(self):
68 l = ldb.Ldb(filename())
69 self.assertEquals(len(l.search(controls=["paged_results:0:5"])), 1)
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)
75 def test_search_string_dn(self):
76 l = ldb.Ldb(filename())
77 self.assertEquals(len(l.search("", ldb.SCOPE_SUBTREE, "(dc=*)", ["dc"])), 0)
79 def test_search_attr_string(self):
80 l = ldb.Ldb("foo.tdb")
81 self.assertRaises(TypeError, l.search, attrs="dc")
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"))
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)
94 def test_delete(self):
95 l = ldb.Ldb(filename())
96 self.assertRaises(ldb.LdbError, lambda: l.delete(ldb.Dn(l, "dc=foo2")))
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)
111 def test_get_config_basedn(self):
112 l = ldb.Ldb(filename())
113 self.assertEquals(None, l.get_config_basedn())
115 def test_get_root_basedn(self):
116 l = ldb.Ldb(filename())
117 self.assertEquals(None, l.get_root_basedn())
119 def test_get_schema_basedn(self):
120 l = ldb.Ldb(filename())
121 self.assertEquals(None, l.get_schema_basedn())
123 def test_get_default_basedn(self):
124 l = ldb.Ldb(filename())
125 self.assertEquals(None, l.get_default_basedn())
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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)
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"))))
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)
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",
273 res = l.search(expression="(dn=dc=somedn)")
274 self.assertEquals("foo\0bar", res[0]["displayname"][0])
277 class DnTests(unittest.TestCase):
279 def setUp(self):
280 self.ldb = ldb.Ldb(filename())
282 def test_eq(self):
283 x = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
284 y = ldb.Dn(self.ldb, "dc=foo11,bar=bloe")
285 self.assertEquals(x, y)
287 def test_str(self):
288 x = ldb.Dn(self.ldb, "dc=foo12,bar=bloe")
289 self.assertEquals(x.__str__(), "dc=foo12,bar=bloe")
291 def test_repr(self):
292 x = ldb.Dn(self.ldb, "dc=foo13,bla=blie")
293 self.assertEquals(x.__repr__(), "Dn('dc=foo13,bla=blie')")
295 def test_get_casefold(self):
296 x = ldb.Dn(self.ldb, "dc=foo14,bar=bloe")
297 self.assertEquals(x.get_casefold(), "DC=FOO14,BAR=bloe")
299 def test_validate(self):
300 x = ldb.Dn(self.ldb, "dc=foo15,bar=bloe")
301 self.assertTrue(x.validate())
303 def test_parent(self):
304 x = ldb.Dn(self.ldb, "dc=foo16,bar=bloe")
305 self.assertEquals("bar=bloe", x.parent().__str__())
307 def test_parent_nonexistant(self):
308 x = ldb.Dn(self.ldb, "@BLA")
309 self.assertEquals(None, x.parent())
311 def test_compare(self):
312 x = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
313 y = ldb.Dn(self.ldb, "dc=foo17,bar=bloe")
314 self.assertEquals(x, y)
315 z = ldb.Dn(self.ldb, "dc=foo17,bar=blie")
316 self.assertNotEquals(z, y)
318 def test_is_valid(self):
319 x = ldb.Dn(self.ldb, "dc=foo18,dc=bloe")
320 self.assertTrue(x.is_valid())
321 x = ldb.Dn(self.ldb, "")
322 # is_valid()'s return values appears to be a side effect of
323 # some other ldb functions. yuck.
324 # self.assertFalse(x.is_valid())
326 def test_is_special(self):
327 x = ldb.Dn(self.ldb, "dc=foo19,bar=bloe")
328 self.assertFalse(x.is_special())
329 x = ldb.Dn(self.ldb, "@FOOBAR")
330 self.assertTrue(x.is_special())
332 def test_check_special(self):
333 x = ldb.Dn(self.ldb, "dc=foo20,bar=bloe")
334 self.assertFalse(x.check_special("FOOBAR"))
335 x = ldb.Dn(self.ldb, "@FOOBAR")
336 self.assertTrue(x.check_special("@FOOBAR"))
338 def test_len(self):
339 x = ldb.Dn(self.ldb, "dc=foo21,bar=bloe")
340 self.assertEquals(2, len(x))
341 x = ldb.Dn(self.ldb, "dc=foo21")
342 self.assertEquals(1, len(x))
344 def test_add_child(self):
345 x = ldb.Dn(self.ldb, "dc=foo22,bar=bloe")
346 self.assertTrue(x.add_child(ldb.Dn(self.ldb, "bla=bloe")))
347 self.assertEquals("bla=bloe,dc=foo22,bar=bloe", x.__str__())
349 def test_add_base(self):
350 x = ldb.Dn(self.ldb, "dc=foo23,bar=bloe")
351 base = ldb.Dn(self.ldb, "bla=bloe")
352 self.assertTrue(x.add_base(base))
353 self.assertEquals("dc=foo23,bar=bloe,bla=bloe", x.__str__())
355 def test_add(self):
356 x = ldb.Dn(self.ldb, "dc=foo24")
357 y = ldb.Dn(self.ldb, "bar=bla")
358 self.assertEquals("dc=foo24,bar=bla", str(y + x))
360 def test_parse_ldif(self):
361 msgs = self.ldb.parse_ldif("dn: foo=bar\n")
362 msg = msgs.next()
363 self.assertEquals("foo=bar", str(msg[1].dn))
364 self.assertTrue(isinstance(msg[1], ldb.Message))
366 def test_parse_ldif_more(self):
367 msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
368 msg = msgs.next()
369 self.assertEquals("foo=bar", str(msg[1].dn))
370 msg = msgs.next()
371 self.assertEquals("bar=bar", str(msg[1].dn))
373 def test_canonical_string(self):
374 x = ldb.Dn(self.ldb, "dc=foo25,bar=bloe")
375 self.assertEquals("/bloe/foo25", x.canonical_str())
377 def test_canonical_ex_string(self):
378 x = ldb.Dn(self.ldb, "dc=foo26,bar=bloe")
379 self.assertEquals("/bloe\nfoo26", x.canonical_ex_str())
382 class LdbMsgTests(unittest.TestCase):
384 def setUp(self):
385 self.msg = ldb.Message()
387 def test_init_dn(self):
388 self.msg = ldb.Message(ldb.Dn(ldb.Ldb(), "dc=foo27"))
389 self.assertEquals("dc=foo27", str(self.msg.dn))
391 def test_iter_items(self):
392 self.assertEquals(0, len(self.msg.items()))
393 self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo28")
394 self.assertEquals(1, len(self.msg.items()))
396 def test_repr(self):
397 self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "dc=foo29")
398 self.msg["dc"] = "foo"
399 self.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self.msg))
401 def test_len(self):
402 self.assertEquals(0, len(self.msg))
404 def test_notpresent(self):
405 self.assertRaises(KeyError, lambda: self.msg["foo"])
407 def test_del(self):
408 del self.msg["foo"]
410 def test_add_value(self):
411 self.assertEquals(0, len(self.msg))
412 self.msg["foo"] = ["foo"]
413 self.assertEquals(1, len(self.msg))
415 def test_add_value_multiple(self):
416 self.assertEquals(0, len(self.msg))
417 self.msg["foo"] = ["foo", "bla"]
418 self.assertEquals(1, len(self.msg))
419 self.assertEquals(["foo", "bla"], list(self.msg["foo"]))
421 def test_set_value(self):
422 self.msg["foo"] = ["fool"]
423 self.assertEquals(["fool"], list(self.msg["foo"]))
424 self.msg["foo"] = ["bar"]
425 self.assertEquals(["bar"], list(self.msg["foo"]))
427 def test_keys(self):
428 self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
429 self.msg["foo"] = ["bla"]
430 self.msg["bar"] = ["bla"]
431 self.assertEquals(["dn", "foo", "bar"], self.msg.keys())
433 def test_dn(self):
434 self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO")
435 self.assertEquals("@BASEINFO", self.msg.dn.__str__())
437 def test_get_dn(self):
438 self.msg.dn = ldb.Dn(ldb.Ldb("foo.tdb"), "@BASEINFO")
439 self.assertEquals("@BASEINFO", self.msg.get("dn").__str__())
441 def test_get_other(self):
442 self.msg["foo"] = ["bar"]
443 self.assertEquals("bar", self.msg.get("foo")[0])
445 def test_get_unknown(self):
446 self.assertEquals(None, self.msg.get("lalalala"))
449 class MessageElementTests(unittest.TestCase):
451 def test_cmp_element(self):
452 x = ldb.MessageElement(["foo"])
453 y = ldb.MessageElement(["foo"])
454 z = ldb.MessageElement(["bzr"])
455 self.assertEquals(x, y)
456 self.assertNotEquals(x, z)
458 def test_create_iterable(self):
459 x = ldb.MessageElement(["foo"])
460 self.assertEquals(["foo"], list(x))
462 def test_repr(self):
463 x = ldb.MessageElement(["foo"])
464 self.assertEquals("MessageElement(['foo'])", repr(x))
465 x = ldb.MessageElement(["foo", "bla"])
466 self.assertEquals(2, len(x))
467 self.assertEquals("MessageElement(['foo','bla'])", repr(x))
469 def test_get_item(self):
470 x = ldb.MessageElement(["foo", "bar"])
471 self.assertEquals("foo", x[0])
472 self.assertEquals("bar", x[1])
473 self.assertEquals("bar", x[-1])
474 self.assertRaises(IndexError, lambda: x[45])
476 def test_len(self):
477 x = ldb.MessageElement(["foo", "bar"])
478 self.assertEquals(2, len(x))
480 def test_eq(self):
481 x = ldb.MessageElement(["foo", "bar"])
482 y = ldb.MessageElement(["foo", "bar"])
483 self.assertEquals(y, x)
484 x = ldb.MessageElement(["foo"])
485 self.assertNotEquals(y, x)
486 y = ldb.MessageElement(["foo"])
487 self.assertEquals(y, x)
490 class ModuleTests(unittest.TestCase):
492 def test_register_module(self):
493 class ExampleModule:
494 name = "example"
495 ldb.register_module(ExampleModule)
497 def test_use_module(self):
498 ops = []
499 class ExampleModule:
500 name = "bla"
502 def __init__(self, ldb, next):
503 ops.append("init")
504 self.next = next
506 def search(self, *args, **kwargs):
507 return self.next.search(*args, **kwargs)
509 ldb.register_module(ExampleModule)
510 if os.path.exists("usemodule.ldb"):
511 os.unlink("usemodule.ldb")
512 l = ldb.Ldb("usemodule.ldb")
513 l.add({"dn": "@MODULES", "@LIST": "bla"})
514 self.assertEquals([], ops)
515 l = ldb.Ldb("usemodule.ldb")
516 self.assertEquals(["init"], ops)
519 if __name__ == '__main__':
520 import unittest
521 unittest.TestProgram()