From 7809dee889672eb335a480668a014f2601e0f4e1 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Thu, 1 Oct 2015 20:09:36 +0200 Subject: WIP: Test for sdap_async.c --- Makefile.am | 31 ++++++ src/tests/cmocka/common_mock_sdap.c | 96 +++++----------- src/tests/cmocka/common_mock_sdap.h | 5 + src/tests/cmocka/common_mock_sdap_async.c | 96 ++++++++++++++++ src/tests/cmocka/test_sdap_async.c | 178 ++++++++++++++++++++++++++++++ 5 files changed, 336 insertions(+), 70 deletions(-) create mode 100644 src/tests/cmocka/common_mock_sdap_async.c create mode 100644 src/tests/cmocka/test_sdap_async.c diff --git a/Makefile.am b/Makefile.am index aae123c18..61ea9d20f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -238,6 +238,7 @@ if HAVE_CMOCKA test_cert_utils \ test_ldap_id_cleanup \ test_data_provider_be \ + sdap-async-tests \ $(NULL) if HAVE_LIBRESOLV @@ -1864,6 +1865,7 @@ TEST_MOCK_PROVIDER_OBJ = \ src/providers/ldap/sdap_utils.c \ src/providers/ldap/sdap_range.c \ src/tests/cmocka/common_mock_sdap.c \ + src/tests/cmocka/common_mock_sdap_async.c \ src/tests/cmocka/common_mock_sysdb_objects.c EXTRA_nss_srv_tests_DEPENDENCIES = \ @@ -2266,6 +2268,34 @@ sdap_tests_LDADD = \ libsss_test_common.la \ $(NULL) +sdap_async_tests_SOURCES = \ + src/providers/data_provider_opts.c \ + src/providers/ldap/sdap_domain.c \ + src/providers/ldap/sdap.c \ + src/providers/ldap/sdap_range.c \ + src/providers/ldap/sdap_async.c \ + src/providers/ldap/sdap_fd_events.c \ + src/util/sss_ldap.c \ + src/tests/cmocka/common_mock_sdap.c \ + src/tests/cmocka/test_sdap_async.c \ + $(NULL) +sdap_async_tests_CFLAGS = \ + $(AM_CFLAGS) \ + $(SSS_CRYPT_CFLAGS) \ + $(NULL) +sdap_async_tests_LDFLAGS = \ + $(NULL) +sdap_async_tests_LDADD = \ + $(CMOCKA_LIBS) \ + $(TALLOC_LIBS) \ + $(LDB_LIBS) \ + $(POPT_LIBS) \ + $(SSSD_INTERNAL_LTLIBS) \ + $(SSS_CRYPT_LIBS) \ + $(OPENLDAP_LIBS) \ + libsss_test_common.la \ + $(NULL) + if BUILD_IFP ifp_tests_SOURCES = \ $(TEST_MOCK_RESP_OBJ) \ @@ -2525,6 +2555,7 @@ test_ipa_subdom_util_LDADD = \ test_ipa_subdom_server_SOURCES = \ $(libsss_krb5_common_la_SOURCES) \ src/tests/cmocka/common_mock_sdap.c \ + src/tests/cmocka/common_mock_sdap_async.c \ src/tests/cmocka/common_mock_be.c \ src/tests/cmocka/common_mock_krb5.c \ src/tests/cmocka/test_ipa_subdomains_server.c \ diff --git a/src/tests/cmocka/common_mock_sdap.c b/src/tests/cmocka/common_mock_sdap.c index cef321613..4b32d4e6f 100644 --- a/src/tests/cmocka/common_mock_sdap.c +++ b/src/tests/cmocka/common_mock_sdap.c @@ -40,6 +40,32 @@ struct sdap_id_ctx *mock_sdap_id_ctx(TALLOC_CTX *mem_ctx, return sdap_id_ctx; } +struct sdap_options *mock_sdap_options(TALLOC_CTX *mem_ctx, + struct sdap_attr_map *src_user_map, + struct sdap_attr_map *src_group_map, + struct dp_option *src_basic_opts) +{ + struct sdap_options *opts; + errno_t ret; + + opts = talloc_zero(mem_ctx, struct sdap_options); + assert_non_null(opts); + + ret = sdap_copy_map(opts, src_user_map, + SDAP_OPTS_USER, &opts->user_map); + assert_int_equal(ret, ERR_OK); + + ret = sdap_copy_map(opts, src_group_map, + SDAP_OPTS_GROUP, &opts->group_map); + assert_int_equal(ret, ERR_OK); + + ret = dp_copy_defaults(opts, src_basic_opts, + SDAP_OPTS_BASIC, &opts->basic); + assert_int_equal(ret, ERR_OK); + + return opts; +} + struct sdap_options *mock_sdap_options_ldap(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, struct confdb_ctx *confdb_ctx, @@ -67,73 +93,3 @@ struct sdap_handle *mock_sdap_handle(TALLOC_CTX *mem_ctx) return handle; } -/* - * Mock sdap_async.c - * - * Every function that is placed in sdap_async.c module has to be mocked, - * to avoid any attempt to communicate with remote servers. Therefore no test - * can be compiled with sdap_async.c. If any of these functions is needed, - * their mock equivalent shall be used. - */ - -bool sdap_has_deref_support(struct sdap_handle *sh, struct sdap_options *opts) -{ - return sss_mock_type(bool); -} - -struct tevent_req *sdap_get_generic_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sdap_options *opts, - struct sdap_handle *sh, - const char *search_base, - int scope, - const char *filter, - const char **attrs, - struct sdap_attr_map *map, - int map_num_attrs, - int timeout, - bool allow_paging) -{ - return test_req_succeed_send(mem_ctx, ev); -} - -int sdap_get_generic_recv(struct tevent_req *req, - TALLOC_CTX *mem_ctx, - size_t *reply_count, - struct sysdb_attrs ***reply) -{ - TEVENT_REQ_RETURN_ON_ERROR(req); - - *reply_count = sss_mock_type(size_t); - *reply = sss_mock_ptr_type(struct sysdb_attrs **); - - return sss_mock_type(int); -} - -struct tevent_req * sdap_deref_search_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct sdap_options *opts, - struct sdap_handle *sh, - const char *base_dn, - const char *deref_attr, - const char **attrs, - int num_maps, - struct sdap_attr_map_info *maps, - int timeout) -{ - return test_req_succeed_send(mem_ctx, ev); -} - -int sdap_deref_search_recv(struct tevent_req *req, - TALLOC_CTX *mem_ctx, - size_t *reply_count, - struct sdap_deref_attrs ***reply) -{ - TEVENT_REQ_RETURN_ON_ERROR(req); - - *reply_count = sss_mock_type(size_t); - *reply = talloc_steal(mem_ctx, - sss_mock_ptr_type(struct sdap_deref_attrs **)); - - return EOK; -} diff --git a/src/tests/cmocka/common_mock_sdap.h b/src/tests/cmocka/common_mock_sdap.h index 747287d1f..b1102e782 100644 --- a/src/tests/cmocka/common_mock_sdap.h +++ b/src/tests/cmocka/common_mock_sdap.h @@ -26,6 +26,11 @@ #include "util/util.h" #include "providers/ldap/sdap.h" +struct sdap_options *mock_sdap_options(TALLOC_CTX *mem_ctx, + struct sdap_attr_map *src_user_map, + struct sdap_attr_map *src_group_map, + struct dp_option *src_basic_opts); + struct sdap_options *mock_sdap_options_ldap(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, struct confdb_ctx *confdb_ctx, diff --git a/src/tests/cmocka/common_mock_sdap_async.c b/src/tests/cmocka/common_mock_sdap_async.c new file mode 100644 index 000000000..0ec6ab252 --- /dev/null +++ b/src/tests/cmocka/common_mock_sdap_async.c @@ -0,0 +1,96 @@ +/* + Copyright (C) 2015 Red Hat + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +#include + +#include "util/util.h" +#include "providers/ldap/ldap_common.h" +#include "providers/ldap/sdap.h" +#include "tests/cmocka/common_mock.h" + +/* + * Mock sdap_async.c + * + * Every function that is placed in sdap_async.c module has to be mocked, + * to avoid any attempt to communicate with remote servers. Therefore no test + * can be compiled with sdap_async.c. If any of these functions is needed, + * their mock equivalent shall be used. + */ + +bool sdap_has_deref_support(struct sdap_handle *sh, struct sdap_options *opts) +{ + return sss_mock_type(bool); +} + +struct tevent_req *sdap_get_generic_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sdap_options *opts, + struct sdap_handle *sh, + const char *search_base, + int scope, + const char *filter, + const char **attrs, + struct sdap_attr_map *map, + int map_num_attrs, + int timeout, + bool allow_paging) +{ + return test_req_succeed_send(mem_ctx, ev); +} + +int sdap_get_generic_recv(struct tevent_req *req, + TALLOC_CTX *mem_ctx, + size_t *reply_count, + struct sysdb_attrs ***reply) +{ + TEVENT_REQ_RETURN_ON_ERROR(req); + + *reply_count = sss_mock_type(size_t); + *reply = sss_mock_ptr_type(struct sysdb_attrs **); + + return sss_mock_type(int); +} + +struct tevent_req * sdap_deref_search_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct sdap_options *opts, + struct sdap_handle *sh, + const char *base_dn, + const char *deref_attr, + const char **attrs, + int num_maps, + struct sdap_attr_map_info *maps, + int timeout) +{ + return test_req_succeed_send(mem_ctx, ev); +} + +int sdap_deref_search_recv(struct tevent_req *req, + TALLOC_CTX *mem_ctx, + size_t *reply_count, + struct sdap_deref_attrs ***reply) +{ + TEVENT_REQ_RETURN_ON_ERROR(req); + + *reply_count = sss_mock_type(size_t); + *reply = talloc_steal(mem_ctx, + sss_mock_ptr_type(struct sdap_deref_attrs **)); + + return EOK; +} + diff --git a/src/tests/cmocka/test_sdap_async.c b/src/tests/cmocka/test_sdap_async.c new file mode 100644 index 000000000..c232e938f --- /dev/null +++ b/src/tests/cmocka/test_sdap_async.c @@ -0,0 +1,178 @@ +/* + Copyright (C) 2015 Red Hat + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + +#include "tests/cmocka/common_mock.h" +#include "tests/cmocka/common_mock_sdap.h" +#include "providers/ldap/sdap_async.h" +#include "providers/ldap/ldap_opts.h" +#include "providers/ad/ad_opts.h" +#include "util/crypto/sss_crypto.h" + +/* Mock parsing search base without overlinking the test */ +errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx, + struct dp_option *opts, int class, + struct sdap_search_base ***_search_bases) +{ + return EOK; +} + +int ldap_get_options(TALLOC_CTX *memctx, + struct sss_domain_info *dom, + struct confdb_ctx *cdb, + const char *conf_path, + struct sdap_options **_opts) +{ + return 0; +} + +struct posix_test_state { + struct sss_test_ctx *tctx; + + struct sdap_options *opts; + struct sdap_handle *sh; + struct sdap_search_base **search_bases; +}; + +static int posix_test_setup(void **state) +{ + + struct posix_test_state *test_state; + errno_t ret; + + assert_true(leak_check_setup()); + + test_state = talloc_zero(global_talloc_context, + struct posix_test_state); + assert_non_null(test_state); + + test_state->tctx = create_ev_test_ctx(test_state); + assert_non_null(test_state->tctx); + + test_state->opts = mock_sdap_options(test_state, + ad_2008r2_user_map, + ad_2008r2_group_map, + default_basic_opts); + assert_non_null(test_state->opts); + + test_state->search_bases = talloc_array(test_state, + struct sdap_search_base *, 2); + assert_non_null(test_state->search_bases); + test_state->search_bases[1] = NULL; + + ret = sdap_create_search_base(test_state, + "cn=example,cn=com", + LDAP_SCOPE_SUBTREE, + NULL, + &test_state->search_bases[0]); + assert_int_equal(ret, EOK); + test_state->sh = mock_sdap_handle(test_state); + assert_non_null(test_state->sh); + + *state = test_state; + return 0; +} + +static int posix_test_teardown(void **state) +{ + struct posix_test_state *test_state = talloc_get_type_abort(*state, + struct posix_test_state); + assert_true(check_leaks_pop(test_state) == true); + talloc_free(test_state); + assert_true(leak_check_teardown()); + return 0; +} + +static void sdap_posix_check_done(struct tevent_req *req); + +static void test_posix_attrs(void **state) +{ + struct posix_test_state *test_ctx = + talloc_get_type(*state, struct posix_test_state); + struct tevent_req *req; + errno_t ret; + + req = sdap_posix_check_send(test_ctx, + test_ctx->tctx->ev, + test_ctx->opts, + test_ctx->sh, + test_ctx->search_bases, + 5); + assert_non_null(req); + + tevent_req_set_callback(req, sdap_posix_check_done, test_ctx); + + ret = test_ev_loop(test_ctx->tctx); + assert_int_equal(ret, ERR_OK); +} + +static void sdap_posix_check_done(struct tevent_req *req) +{ + struct posix_test_state *test_ctx = \ + tevent_req_callback_data(req, struct posix_test_state); + errno_t ret; + + ret = sdap_posix_check_recv(req, NULL); + talloc_zfree(req); + assert_int_equal(ret, ERR_OK); + + test_ctx->tctx->done = true; +} + +int main(int argc, const char *argv[]) +{ + poptContext pc; + int opt; + struct poptOption long_options[] = { + POPT_AUTOHELP + SSSD_DEBUG_OPTS + POPT_TABLEEND + }; + + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(test_posix_attrs, + posix_test_setup, + posix_test_teardown), + }; + + /* Set debug level to invalid value so we can deside if -d 0 was used. */ + debug_level = SSSDBG_INVALID; + + pc = poptGetContext(argv[0], argc, argv, long_options, 0); + while((opt = poptGetNextOpt(pc)) != -1) { + switch(opt) { + default: + fprintf(stderr, "\nInvalid option %s: %s\n\n", + poptBadOption(pc, 0), poptStrerror(opt)); + poptPrintUsage(pc, stderr, 0); + return 1; + } + } + poptFreeContext(pc); + + DEBUG_CLI_INIT(debug_level); + + /* Even though normally the tests should clean up after themselves + * they might not after a failed run. Remove the old db to be sure */ + tests_set_cwd(); + + return cmocka_run_group_tests(tests, NULL, NULL); +} -- cgit