>From c6df218fea569033dd69008f382a91cd4805e8b7 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Fri, 29 May 2015 15:42:46 +0200 Subject: [PATCH 16/29] TESTS: Add a common mock_be_ctx function Reduces code duplication between tests. --- Makefile.am | 4 ++++ src/tests/cmocka/common_mock_be.c | 39 +++++++++++++++++++++++++++++++++++ src/tests/cmocka/common_mock_be.h | 30 +++++++++++++++++++++++++++ src/tests/cmocka/test_be_ptask.c | 8 +++++-- src/tests/cmocka/test_dyndns.c | 6 ++---- src/tests/cmocka/test_nested_groups.c | 5 ++--- 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/tests/cmocka/common_mock_be.c create mode 100644 src/tests/cmocka/common_mock_be.h diff --git a/Makefile.am b/Makefile.am index 6912fa971fbe7a4bebcce0ad5de9a9008f7344a6..db5b23242d34abb3006ba5a2f2d3da16feae8664 100644 --- a/Makefile.am +++ b/Makefile.am @@ -620,6 +620,7 @@ dist_noinst_HEADERS = \ src/tests/cmocka/common_mock_sdap.h \ src/tests/cmocka/common_mock_sysdb_objects.h \ src/tests/cmocka/common_mock_krb5.h \ + src/tests/cmocka/common_mock_be.h \ src/tests/cmocka/test_expire_common.h \ src/sss_client/pam_message.h \ src/sss_client/ssh/sss_ssh_client.h \ @@ -1935,6 +1936,7 @@ EXTRA_dyndns_tests_DEPENDENCIES = \ $(ldblib_LTLIBRARIES) dyndns_tests_SOURCES = \ $(SSSD_RESOLV_OBJ) \ + src/tests/cmocka/common_mock_be.c \ src/tests/cmocka/test_dyndns.c \ src/providers/data_provider_opts.c dyndns_tests_CFLAGS = \ @@ -1966,6 +1968,7 @@ nestedgroups_tests_SOURCES = \ $(TEST_MOCK_PROVIDER_OBJ) \ src/providers/ldap/sdap_idmap.c \ src/tests/cmocka/test_nested_groups.c \ + src/tests/cmocka/common_mock_be.c \ src/providers/ldap/sdap_async_nested_groups.c \ src/providers/ldap/sdap_ad_groups.c \ $(NULL) @@ -2269,6 +2272,7 @@ test_sysdb_utils_LDADD = \ $(NULL) test_be_ptask_SOURCES = \ + src/tests/cmocka/common_mock_be.c \ src/tests/cmocka/test_be_ptask.c \ src/providers/dp_ptask.c \ $(NULL) diff --git a/src/tests/cmocka/common_mock_be.c b/src/tests/cmocka/common_mock_be.c new file mode 100644 index 0000000000000000000000000000000000000000..29821ba3429aab2fc063cd75f5cf16652d407674 --- /dev/null +++ b/src/tests/cmocka/common_mock_be.c @@ -0,0 +1,39 @@ +/* + Authors: + Jakub Hrozek + + Copyright (C) 2013 Red Hat + + SSSD tests: Fake back end + + 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 "util/util.h" +#include "tests/cmocka/common_mock_resp.h" + +struct be_ctx *mock_be_ctx(TALLOC_CTX *mem_ctx, struct sss_test_ctx *tctx) +{ + struct be_ctx *be_ctx; + + be_ctx = talloc_zero(mem_ctx, struct be_ctx); + assert_non_null(be_ctx); + + be_ctx->cdb = tctx->confdb; + be_ctx->ev = tctx->ev; + be_ctx->domain = tctx->dom; + be_ctx->conf_path = tctx->conf_dom_path; + + return be_ctx; +} diff --git a/src/tests/cmocka/common_mock_be.h b/src/tests/cmocka/common_mock_be.h new file mode 100644 index 0000000000000000000000000000000000000000..80694d7c7afe61eba7abe5029612d5dbfe8a4a1c --- /dev/null +++ b/src/tests/cmocka/common_mock_be.h @@ -0,0 +1,30 @@ +/* + Authors: + Sumit Bose + + Copyright (C) 2014 Red Hat + + SSSD tests: Tests keytab utilities + + 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 . +*/ + +#ifndef __COMMON_MOCK_BE_H_ +#define __COMMON_MOCK_BE_H_ + +#include "tests/cmocka/common_mock.h" + +struct be_ctx *mock_be_ctx(TALLOC_CTX *mem_ctx, struct sss_test_ctx *tctx); + +#endif /* __COMMON_MOCK_BE_H_ */ diff --git a/src/tests/cmocka/test_be_ptask.c b/src/tests/cmocka/test_be_ptask.c index f4a120c0c6201a6a306004445445024684f713d1..a0daaf967a6f1ea991ff30445488ffadff51f821 100644 --- a/src/tests/cmocka/test_be_ptask.c +++ b/src/tests/cmocka/test_be_ptask.c @@ -28,6 +28,7 @@ #include "providers/dp_ptask_private.h" #include "providers/dp_ptask.h" #include "tests/cmocka/common_mock.h" +#include "tests/cmocka/common_mock_be.h" #include "tests/common.h" #define DELAY 2 @@ -37,6 +38,7 @@ cmocka_unit_test_setup_teardown(test_ ## test, test_setup, test_teardown) struct test_ctx { + struct sss_test_ctx *tctx; struct be_ctx *be_ctx; time_t when; @@ -274,8 +276,10 @@ static int test_setup(void **state) test_ctx = talloc_zero(global_talloc_context, struct test_ctx); assert_non_null(test_ctx); - /* create be_ctx, only ev and offline field should be used */ - test_ctx->be_ctx = talloc_zero(test_ctx, struct be_ctx); + test_ctx->tctx = create_ev_test_ctx(test_ctx); + assert_non_null(test_ctx->tctx); + + test_ctx->be_ctx = mock_be_ctx(test_ctx, test_ctx->tctx); assert_non_null(test_ctx->be_ctx); test_ctx->be_ctx->ev = tevent_context_init(test_ctx->be_ctx); diff --git a/src/tests/cmocka/test_dyndns.c b/src/tests/cmocka/test_dyndns.c index 97fac9425a63e1306301c8256285b4b40f0f3e1e..689e333d4e68c4a2582894d06b8b7b20c76b9be8 100644 --- a/src/tests/cmocka/test_dyndns.c +++ b/src/tests/cmocka/test_dyndns.c @@ -33,6 +33,7 @@ #include "providers/dp_dyndns.c" #include "tests/cmocka/common_mock.h" +#include "tests/cmocka/common_mock_be.h" #include "src/providers/dp_dyndns.h" #define TESTS_PATH "tests_dyndns" @@ -412,12 +413,9 @@ static int dyndns_test_setup(void **state) TEST_ID_PROVIDER, params); assert_non_null(dyndns_test_ctx->tctx); - dyndns_test_ctx->be_ctx = talloc_zero(dyndns_test_ctx, struct be_ctx); + dyndns_test_ctx->be_ctx = mock_be_ctx(dyndns_test_ctx, dyndns_test_ctx->tctx); assert_non_null(dyndns_test_ctx->be_ctx); - dyndns_test_ctx->be_ctx->cdb = dyndns_test_ctx->tctx->confdb; - dyndns_test_ctx->be_ctx->ev = dyndns_test_ctx->tctx->ev; - dyndns_test_ctx->be_ctx->conf_path = dyndns_test_ctx->tctx->conf_dom_path; return 0; } diff --git a/src/tests/cmocka/test_nested_groups.c b/src/tests/cmocka/test_nested_groups.c index 4f748a170075898fca9583ec2da7a1d15a51e2d2..75fb3a19f27596fe89fdc779c73653cbbaf5e536 100644 --- a/src/tests/cmocka/test_nested_groups.c +++ b/src/tests/cmocka/test_nested_groups.c @@ -25,6 +25,7 @@ #include "tests/cmocka/common_mock.h" #include "tests/cmocka/common_mock_sdap.h" +#include "tests/cmocka/common_mock_be.h" #include "tests/cmocka/common_mock_sysdb_objects.h" #include "providers/ldap/ldap_common.h" #include "providers/ldap/sdap.h" @@ -599,12 +600,10 @@ static int nested_groups_test_setup(void **state) struct sdap_id_ctx); assert_non_null(test_ctx->sdap_id_ctx); - test_ctx->sdap_id_ctx->be = talloc_zero(test_ctx->sdap_id_ctx, - struct be_ctx); + test_ctx->sdap_id_ctx->be = mock_be_ctx(test_ctx, test_ctx->tctx); assert_non_null(test_ctx->sdap_id_ctx->be); test_ctx->sdap_id_ctx->opts = test_ctx->sdap_opts; - test_ctx->sdap_id_ctx->be->domain = test_ctx->tctx->dom; ret = sdap_idmap_init(test_ctx, test_ctx->sdap_id_ctx, &test_ctx->idmap_ctx); assert_int_equal(ret, EOK); -- 2.1.0