Skip to content

Commit

Permalink
pyldb: Improve test coverage
Browse files Browse the repository at this point in the history
Add tests for:
 - ldb.Dn.{get,set}_{extended_,}component_{name,value}
 - ldb.Dn.{get,set}_rdn_{name,value}
 - ldb.Dn.get_casefold
 - ldb.Dn.get_linearized
 - ldb.Dn.canonical_str

Add negative test for Dn.__contains__

Add a helper function to register a dummy DN extension for testing.

Signed-off-by: Petr Viktorin <[email protected]>
Reviewed-by: Andrew Bartlett <[email protected]>
Reviewed-by: Stefan Metzmacher <[email protected]>
  • Loading branch information
encukou authored and metze-samba committed Nov 5, 2015
1 parent dd7baa2 commit 0d36d6d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/ldb/pyldb.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <Python.h>
#include "ldb_private.h"
#include "ldb_handlers.h"
#include "pyldb.h"

void initldb(void);
Expand Down Expand Up @@ -1931,6 +1932,28 @@ static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)

return PyLong_FromLongLong(value);
}


static const struct ldb_dn_extended_syntax test_dn_syntax = {
.name = "TEST",
.read_fn = ldb_handler_copy,
.write_clear_fn = ldb_handler_copy,
.write_hex_fn = ldb_handler_copy,
};

static PyObject *py_ldb_register_test_extensions(PyLdbObject *self)
{
struct ldb_context *ldb = pyldb_Ldb_AsLdbContext(self);
int ret;

ret = ldb_dn_extended_add_syntax(ldb, LDB_ATTR_FLAG_FIXED, &test_dn_syntax);

PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);

Py_RETURN_NONE;
}


static PyMethodDef py_ldb_methods[] = {
{ "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
"S.set_debug(callback) -> None\n"
Expand Down Expand Up @@ -2020,6 +2043,9 @@ static PyMethodDef py_ldb_methods[] = {
{ "sequence_number", (PyCFunction)py_ldb_sequence_number, METH_VARARGS,
"S.sequence_number(type) -> value\n"
"Return the value of the sequence according to the requested type" },
{ "_register_test_extensions", (PyCFunction)py_ldb_register_test_extensions, METH_NOARGS,
"S._register_test_extensions() -> None\n"
"Register internal extensions used in testing" },
{ NULL },
};

Expand Down
82 changes: 82 additions & 0 deletions lib/ldb/tests/python/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def test_contains(self):
l.add(m)
try:
self.assertTrue(ldb.Dn(l, "dc=foo3") in l)
self.assertFalse(ldb.Dn(l, "dc=foo4") in l)
finally:
l.delete(m.dn)

Expand Down Expand Up @@ -620,6 +621,87 @@ def test_ldb_is_child_of_str(self):
self.assertFalse(dn3.is_child_of(dn2_str))
self.assertFalse(dn1.is_child_of(dn4_str))

def test_get_component_name(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertEqual(dn.get_component_name(0), 'cn')
self.assertEqual(dn.get_component_name(1), 'dc')
self.assertEqual(dn.get_component_name(2), None)
self.assertEqual(dn.get_component_name(-1), None)

def test_get_component_value(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertEqual(dn.get_component_value(0), 'foo')
self.assertEqual(dn.get_component_value(1), 'base')
self.assertEqual(dn.get_component_name(2), None)
self.assertEqual(dn.get_component_name(-1), None)

def test_set_component(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
dn.set_component(0, 'cn', 'bar')
self.assertEqual(str(dn), "cn=bar,dc=base")
dn.set_component(1, 'o', 'asep')
self.assertEqual(str(dn), "cn=bar,o=asep")
self.assertRaises(TypeError, dn.set_component, 2, 'dc', 'base')
self.assertEqual(str(dn), "cn=bar,o=asep")
dn.set_component(1, 'o', 'a,b+c')
self.assertEqual(str(dn), r"cn=bar,o=a\,b\+c")

def test_set_component_bytes(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
dn.set_component(0, 'cn', b'bar')
self.assertEqual(str(dn), "cn=bar,dc=base")
dn.set_component(1, 'o', b'asep')
self.assertEqual(str(dn), "cn=bar,o=asep")

def test_set_component_none(self):
dn = ldb.Dn(self.ldb, "cn=foo,cn=bar,dc=base")
self.assertRaises(TypeError, dn.set_component, 1, 'cn', None)

def test_get_extended_component_null(self):
dn = ldb.Dn(self.ldb, "cn=foo,cn=bar,dc=base")
self.assertEqual(dn.get_extended_component("TEST"), None)

def test_get_extended_component(self):
self.ldb._register_test_extensions()
dn = ldb.Dn(self.ldb, "<TEST=foo>;cn=bar,dc=base")
self.assertEqual(dn.get_extended_component("TEST"), b"foo")

def test_set_extended_component(self):
self.ldb._register_test_extensions()
dn = ldb.Dn(self.ldb, "dc=base")
dn.set_extended_component("TEST", "foo")
self.assertEqual(dn.get_extended_component("TEST"), b"foo")
dn.set_extended_component("TEST", b"bar")
self.assertEqual(dn.get_extended_component("TEST"), b"bar")

def test_extended_str(self):
self.ldb._register_test_extensions()
dn = ldb.Dn(self.ldb, "<TEST=foo>;cn=bar,dc=base")
self.assertEqual(dn.extended_str(), "<TEST=foo>;cn=bar,dc=base")

def test_get_rdn_name(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertEqual(dn.get_rdn_name(), 'cn')

def test_get_rdn_value(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertEqual(dn.get_rdn_value(), 'foo')

def test_get_casefold(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertEqual(dn.get_casefold(), 'CN=FOO,DC=BASE')

def test_get_linearized(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertEqual(dn.get_linearized(), 'cn=foo,dc=base')

def test_is_null(self):
dn = ldb.Dn(self.ldb, "cn=foo,dc=base")
self.assertFalse(dn.is_null())

dn = ldb.Dn(self.ldb, '')
self.assertTrue(dn.is_null())

class LdbMsgTests(TestCase):

def setUp(self):
Expand Down

0 comments on commit 0d36d6d

Please sign in to comment.