0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech Petr Čech <pcech@redhat.com>
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech Copyright (C) 2017 Red Hat
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech This program is free software; you can redistribute it and/or modify
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech it under the terms of the GNU General Public License as published by
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech the Free Software Foundation; either version 3 of the License, or
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech (at your option) any later version.
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech This program is distributed in the hope that it will be useful,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech but WITHOUT ANY WARRANTY; without even the implied warranty of
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech GNU General Public License for more details.
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech You should have received a copy of the GNU General Public License
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech along with this program. If not, see <http://www.gnu.org/licenses/>.
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech#include "tests/cmocka/common_mock_sysdb_objects.h"
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech/* Declarations from providers/ldap/sdap_async_initgroups.c */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic int sdap_search_initgr_user_in_batch(struct sdap_get_initgr_state *state,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech#define OBJECT_BASE_DN1 "dc=domain,dc=test,dc=com,cn=sysdb"
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech#define OBJECT_BASE_DN2 "dc=subdom1,dc=domain,dc=test,dc=com,cn=sysdb"
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech#define OBJECT_BASE_DN3 "dc=another_domain,dc=test,dc=com,cn=sysdb"
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech/* ====================== Utilities =============================== */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech passwds = talloc_array(ctx, struct passwd *, user_count);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech for (int i = 0; i < user_count; i++) {
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech homedir = talloc_strdup_append(homedir, "/home/");
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech homedir = talloc_strdup_append(homedir, test_users[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech passwds[i]->pw_name = discard_const(test_users[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech passwds[i]->pw_dir = talloc_strdup(passwds[i], homedir);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech passwds[i]->pw_gecos = discard_const(test_users[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic struct sss_test_conf_param **get_params(TALLOC_CTX *ctx)
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech params = talloc_array(ctx, struct sss_test_conf_param *, base_count + 1);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech for (int i = 0; i < base_count; i++) {
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech params[i] = talloc(params, struct sss_test_conf_param);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech user_base_dn = talloc_strdup_append(user_base_dn, "cn=users,");
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech user_base_dn = talloc_strdup_append(user_base_dn, object_bases[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech group_base_dn = talloc_strdup_append(group_base_dn, "cn=groups,");
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech group_base_dn = talloc_strdup_append(group_base_dn, object_bases[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech params[i] = talloc_array(params[i], struct sss_test_conf_param, 5);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech params[i][1].value = talloc_strdup(params[i], object_bases[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech params[i][2].value = talloc_strdup(params[i], user_base_dn);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech params[i][3].value = talloc_strdup(params[i], group_base_dn);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstruct sss_domain_info *get_domain_info(struct sss_domain_info *domain,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstruct sdap_get_initgr_state *prepare_state(struct test_sdap_initgr_ctx *ctx,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech state = talloc_zero(ctx->tctx, struct sdap_get_initgr_state);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech dom_info = get_domain_info(ctx->tctx->dom, domain_names[i]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech if (i == 0) {
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech state->opts = mock_sdap_options_ldap(state, state->dom,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech/* TODO: This function is copied from test_nss_srv.c
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech * It could be fine move both to one place,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech /* Prime the cache with a valid user */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech/* ====================== Setup =============================== */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic int test_sdap_initgr_setup_one_domain(void **state)
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_zero(global_talloc_context, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic int test_sdap_initgr_setup_multi_domains(void **state)
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_zero(global_talloc_context, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx->tctx = create_multidom_test_ctx(test_ctx, TESTS_PATH,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic int test_sdap_initgr_setup_other_multi_domains(void **state)
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech const char *domains_vith_other[] = { TEST_DOM1_NAME,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_zero(global_talloc_context, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx->tctx = create_multidom_test_ctx(test_ctx, TESTS_PATH,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_get_type(*state, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech/* ====================== The tests =============================== */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_get_type(*state, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech dom1_info = get_domain_info(test_ctx->tctx->dom, domains[0]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech dom2_info = get_domain_info(test_ctx->tctx->dom, domains[1]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech initgr_state = prepare_state(test_ctx, domains_set);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = store_user(test_ctx, dom1_info, passwd_users[0], NULL, 0);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = store_user(test_ctx, dom2_info, passwd_users[1], NULL, 0);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users = talloc_array(test_ctx, struct sysdb_attrs *, 2);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users[0] = mock_sysdb_user(users, object_bases[0],
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users[1] = mock_sysdb_user(users, object_bases[1],
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sdap_search_initgr_user_in_batch(initgr_state, users, 2);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sysdb_attrs_get_string(initgr_state->orig_user, "name", &user_name);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech assert_string_equal(user_name, passwd_users[0]->pw_name);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic void test_user_is_from_subdomain(void **state)
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_get_type(*state, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech dom_info = get_domain_info(test_ctx->tctx->dom, domains[0]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech initgr_state = prepare_state(test_ctx, domains_set);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = store_user(test_ctx, dom_info, passwd_users[0], NULL, 0);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users = talloc_array(test_ctx, struct sysdb_attrs *, 1);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users[0] = mock_sysdb_user(users, object_bases[1],
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sysdb_attrs_get_string(users[0], SYSDB_ORIG_DN, &original_dn);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sdap_search_initgr_user_in_batch(initgr_state, users, 1);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sysdb_attrs_get_string(initgr_state->orig_user, "name", &user_name);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech assert_string_equal(user_name, passwd_users[1]->pw_name);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čechstatic void test_user_is_from_another_domain(void **state)
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech const char *domains_set[] = { domains[0], domains[2], NULL };
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_ctx = talloc_get_type(*state, struct test_sdap_initgr_ctx);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech dom_info = get_domain_info(test_ctx->tctx->dom, domains[0]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech initgr_state = prepare_state(test_ctx, domains_set);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech other_dom_info = get_domain_info(test_ctx->tctx->dom, domains[2]);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sdap_domain_add(initgr_state->opts, other_dom_info, &other_sdom);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech other_sdom->search_bases = talloc_array(other_sdom,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sdap_create_search_base(other_sdom, object_bases[2],
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = store_user(test_ctx, dom_info, passwd_users[0], NULL, 0);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users = talloc_array(test_ctx, struct sysdb_attrs *, 1);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech users[0] = mock_sysdb_user(users, object_bases[2],
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech ret = sdap_search_initgr_user_in_batch(initgr_state, users, 1);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech cmocka_unit_test_setup_teardown(test_user_is_on_batch,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech cmocka_unit_test_setup_teardown(test_user_is_from_subdomain,
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech cmocka_unit_test_setup_teardown(test_user_is_from_another_domain,
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech pc = poptGetContext(argv[0], argc, argv, long_options, 0);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech /* 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 */
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech test_multidom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, domains);
0b7ded15e53b3f31f1570c366f04bc41e5761929Petr Čech if (rv == 0) {