62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl/*
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl Copyright (C) 2015 Red Hat
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl This program is free software; you can redistribute it and/or modify
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl it under the terms of the GNU General Public License as published by
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl the Free Software Foundation; either version 3 of the License, or
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl (at your option) any later version.
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl This program is distributed in the hope that it will be useful,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl but WITHOUT ANY WARRANTY; without even the implied warranty of
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl GNU General Public License for more details.
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl You should have received a copy of the GNU General Public License
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl along with this program. If not, see <http://www.gnu.org/licenses/>.
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl*/
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include <talloc.h>
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include <tevent.h>
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include <errno.h>
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include <popt.h>
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include <security/pam_modules.h>
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "providers/backend.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "providers/data_provider/dp_private.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "providers/data_provider/dp_builtin.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "providers/data_provider/dp.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "tests/cmocka/common_mock.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "tests/common.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "tests/cmocka/common_mock_be.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#include "tests/cmocka/data_provider/mock_dp.h"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#define TESTS_PATH "tp_" BASE_FILE_STEM
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#define TEST_CONF_DB "test_dp_request.ldb"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#define TEST_DOM_NAME "dp_request_test"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl#define TEST_ID_PROVIDER "ldap"
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichlstruct test_ctx {
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct sss_test_ctx *tctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct be_ctx *be_ctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct dp_req_params *params;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl};
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichlstatic int test_setup(void **state)
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl{
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct test_ctx *test_ctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_true(leak_check_setup());
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx = talloc_zero(global_talloc_context, struct test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH, TEST_CONF_DB,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl TEST_DOM_NAME, TEST_ID_PROVIDER, NULL);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(test_ctx->tctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx->be_ctx = mock_be_ctx(test_ctx, test_ctx->tctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(test_ctx->be_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx->params = mock_dp_req_params(test_ctx, test_ctx->be_ctx->ev,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx->be_ctx, NULL,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl DPT_ID, DPM_ACCOUNT_HANDLER);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(test_ctx->params);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl check_leaks_push(test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl *state = test_ctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl return 0;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl}
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichlstatic int test_teardown(void **state)
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl{
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct test_ctx *test_ctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx = talloc_get_type_abort(*state, struct test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_true(check_leaks_pop(test_ctx));
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl talloc_zfree(test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_true(leak_check_teardown());
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl return 0;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl}
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichlstatic void test_deny_handler(void **state)
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl{
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl errno_t ret;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct test_ctx *test_ctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct tevent_req *req;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct pam_data *pd;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct pam_data *out_pd;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx = talloc_get_type(*state, struct test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl pd = talloc_zero(test_ctx, struct pam_data);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl req = dp_access_deny_handler_send(test_ctx, NULL, pd, test_ctx->params);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(req);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl tevent_loop_wait(test_ctx->tctx->ev);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl ret = dp_access_deny_handler_recv(test_ctx, req, &out_pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_int_equal(ret, EOK);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_ptr_equal(pd, out_pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_int_equal(pd->pam_status, PAM_PERM_DENIED);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl talloc_free(req);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl talloc_free(pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl}
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichlstatic void test_permit_handler(void **state)
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl{
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl errno_t ret;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct test_ctx *test_ctx;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct tevent_req *req;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct pam_data *pd;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct pam_data *out_pd;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_ctx = talloc_get_type(*state, struct test_ctx);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl pd = talloc_zero(test_ctx, struct pam_data);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl req = dp_access_permit_handler_send(test_ctx, NULL, pd, test_ctx->params);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_non_null(req);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl tevent_loop_wait(test_ctx->tctx->ev);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl ret = dp_access_permit_handler_recv(test_ctx, req, &out_pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_int_equal(ret, EOK);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_ptr_equal(pd, out_pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl assert_int_equal(pd->pam_status, PAM_SUCCESS);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl talloc_free(req);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl talloc_free(pd);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl}
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichlint main(int argc, const char *argv[])
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl{
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl poptContext pc;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl int opt;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl int rv;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl int no_cleanup = 0;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl struct poptOption long_options[] = {
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl POPT_AUTOHELP
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl SSSD_DEBUG_OPTS
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl _("Do not delete the test database after a test run"), NULL },
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl POPT_TABLEEND
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl };
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl const struct CMUnitTest tests[] = {
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl cmocka_unit_test_setup_teardown(test_permit_handler,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_setup,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_teardown),
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl cmocka_unit_test_setup_teardown(test_deny_handler,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_setup,
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_teardown),
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl };
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl debug_level = SSSDBG_INVALID;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl pc = poptGetContext(argv[0], argc, argv, long_options, 0);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl while((opt = poptGetNextOpt(pc)) != -1) {
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl switch(opt) {
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl default:
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl fprintf(stderr, "\nInvalid option %s: %s\n\n",
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl poptBadOption(pc, 0), poptStrerror(opt));
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl poptPrintUsage(pc, stderr, 0);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl return 1;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl }
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl }
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl poptFreeContext(pc);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl DEBUG_CLI_INIT(debug_level);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl /* Even though normally the tests should clean up after themselves
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz * they might not after a failed run. Remove the old DB to be sure */
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl tests_set_cwd();
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_dom_suite_setup(TESTS_PATH);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl rv = cmocka_run_group_tests(tests, NULL, NULL);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl if (rv == 0 && !no_cleanup) {
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl }
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl return rv;
62370340092503baeaf6587d7ffe4fe25bd9582dPavel Reichl}