Skip to content

Commit

Permalink
lib/ldb: add unit test for ldb_ldap internal code
Browse files Browse the repository at this point in the history
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14413
Signed-off-by: Alexander Bokovoy <[email protected]>
Reviewed-by: Andreas Schneider <[email protected]>
  • Loading branch information
abbra authored and cryptomilk committed Jun 19, 2020
1 parent 9a447fb commit 36bd6ed
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
105 changes: 105 additions & 0 deletions lib/ldb/tests/lldb_ldap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* from cmocka.c:
* These headers or their equivalents should be included prior to
* including
* this header file.
*
* #include <stdarg.h>
* #include <stddef.h>
* #include <setjmp.h>
*
* This allows test applications to use custom definitions of C standard
* library functions and types.
*/
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include <errno.h>
#include <unistd.h>
#include <talloc.h>

#include <ldb.h>
#include <ldb_private.h>
#include <string.h>
#include <ctype.h>

int ldb_ldap_init(const char *version);

#include "ldb_ldap/ldb_ldap.c"

struct test_ctx {
struct tevent_context *ev;
struct ldb_context *ldb;
struct ldb_message *msg;
};

static int lldb_msg_setup(void **state)
{
struct test_ctx *test_ctx;

test_ctx = talloc_zero(NULL, struct test_ctx);
assert_non_null(test_ctx);

test_ctx->ev = tevent_context_init(test_ctx);
assert_non_null(test_ctx->ev);

test_ctx->ldb = ldb_init(test_ctx, test_ctx->ev);
assert_non_null(test_ctx->ldb);

test_ctx->msg = ldb_msg_new(test_ctx);
assert_non_null(test_ctx->msg);

*state = test_ctx;
return 0;
}

static int lldb_msg_teardown(void **state)
{
struct test_ctx *test_ctx = talloc_get_type_abort(*state,
struct test_ctx);

talloc_free(test_ctx);
return 0;
}

static void test_lldb_add_msg_attr(void **state)
{
struct test_ctx *test_ctx = talloc_get_type_abort(*state,
struct test_ctx);
struct ldb_message *msg = test_ctx->msg;
int ret;
unsigned int num_elements = 0;
struct berval **v = NULL;

v = talloc_zero_array(test_ctx, struct berval *, 2);
assert_non_null(v);

v[0] = talloc_zero(v, struct berval);
assert_non_null(v[0]);

v[0]->bv_val = talloc_strdup(msg, "dc=example,dc=test");
assert_non_null(v[0]->bv_val);

v[0]->bv_len = strlen(v[0]->bv_val);

num_elements = msg->num_elements;

ret = lldb_add_msg_attr(test_ctx->ldb, msg, "defaultNamingContext", v);
assert_int_equal(ret, LDB_SUCCESS);
assert_int_equal(msg->num_elements, num_elements + 1);
}


int main(int argc, const char **argv)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_lldb_add_msg_attr,
lldb_msg_setup,
lldb_msg_teardown),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
14 changes: 14 additions & 0 deletions lib/ldb/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ def build(bld):
deps='cmocka ldb ldb_tdb_err_map',
install=False)

# If both libldap and liblber are available, test ldb_ldap
# code for a regression of bz#14413 -- even if we don't build
# it ourselves and simply using the system version
if bld.env.LIB_LDAP and bld.env.LIB_LBER:
bld.SAMBA_BINARY('lldb_ldap_test',
source='tests/lldb_ldap.c',
deps='cmocka talloc lber ldap ldb',
install=False)

if bld.CONFIG_SET('HAVE_LMDB'):
bld.SAMBA_BINARY('ldb_mdb_mod_op_test',
source='tests/ldb_mod_op_test.c',
Expand Down Expand Up @@ -628,6 +637,11 @@ def test(ctx):
# 'ldb_key_value_sub_txn_tdb_test'
'ldb_parse_test']

# if LIB_LDAP and LIB_LBER defined, then we can test ldb_ldap backend
# behavior regression for bz#14413
if env.LIB_LDAP and env.LIB_LBER:
test_exes += ["lldb_ldap_test"]

if env.HAVE_LMDB:
test_exes += ['ldb_mdb_mod_op_test',
'ldb_lmdb_test',
Expand Down

0 comments on commit 36bd6ed

Please sign in to comment.