test_ad_common.c revision 266110fa0f6eb086f8f88787bb167cea416fe108
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco/*
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco Authors:
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco Jakub Hrozek <jhrozek@redhat.com>
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco Copyright (C) 2013 Red Hat
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco SSSD tests: AD access control filter tests
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco This program is free software; you can redistribute it and/or modify
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco it under the terms of the GNU General Public License as published by
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco the Free Software Foundation; either version 3 of the License, or
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco (at your option) any later version.
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco This program is distributed in the hope that it will be useful,
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco but WITHOUT ANY WARRANTY; without even the implied warranty of
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco GNU General Public License for more details.
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco You should have received a copy of the GNU General Public License
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco along with this program. If not, see <http://www.gnu.org/licenses/>.
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen*/
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen#include <talloc.h>
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco#include <tevent.h>
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco#include <errno.h>
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen#include <popt.h>
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen#include <unistd.h>
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen#include <sys/types.h>
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes#include <ifaddrs.h>
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco#include <arpa/inet.h>
6d7c6f82e644c205bc679ee5b1fa2929ec949963Lubos Kosco
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco/* In order to access opaque types */
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco#include "providers/ad/ad_common.c"
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco#include "tests/cmocka/common_mock.h"
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco#define DOMNAME "domname"
6d7c6f82e644c205bc679ee5b1fa2929ec949963Lubos Kosco#define SUBDOMNAME "sub."DOMNAME
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco#define REALMNAME DOMNAME
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco#define HOST_NAME "ad."REALMNAME
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Koscostruct ad_common_test_ctx {
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco struct ad_id_ctx *ad_ctx;
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco struct ad_id_ctx *subdom_ad_ctx;
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco struct sss_domain_info *dom;
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco struct sss_domain_info *subdom;
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco};
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopesstatic void
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Koscoad_common_test_setup(void **state)
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen{
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen struct ad_common_test_ctx *test_ctx;
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen errno_t ret;
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes struct sdap_domain *sdom;
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes struct ad_id_ctx *ad_ctx;
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes struct ad_id_ctx *subdom_ad_ctx;
8dc938f5c6296cffbadd8e06925dc0762dd79c5aLubos Kosco struct sdap_id_conn_ctx *subdom_ldap_ctx;
8dc938f5c6296cffbadd8e06925dc0762dd79c5aLubos Kosco
8dc938f5c6296cffbadd8e06925dc0762dd79c5aLubos Kosco assert_true(leak_check_setup());
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen check_leaks_push(global_talloc_context);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen test_ctx = talloc_zero(global_talloc_context, struct ad_common_test_ctx);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen assert_non_null(test_ctx);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen test_ctx->dom = talloc_zero(test_ctx, struct sss_domain_info);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen assert_non_null(test_ctx->dom);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen test_ctx->dom->name = discard_const(DOMNAME);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen test_ctx->subdom = talloc_zero(test_ctx, struct sss_domain_info);
4750e1be4c1b2ba11705d5b73b86dd1b9dd4e1acKnut Anders Hatlen assert_non_null(test_ctx->subdom);
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen test_ctx->subdom->name = discard_const(SUBDOMNAME);
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen test_ctx->subdom->parent = test_ctx->dom;
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen ad_ctx = talloc_zero(test_ctx, struct ad_id_ctx);
bfc104f8240fac5b59d4347db4c27be705f6dfc2Knut Anders Hatlen assert_non_null(ad_ctx);
6d7c6f82e644c205bc679ee5b1fa2929ec949963Lubos Kosco
ff5eba819da0cf7964d884630fb13262ef12c505Trond Norbye ad_ctx->ad_options = ad_create_default_options(ad_ctx,
50203181d8c071cfd69cb197b5da0eda5c4d6372Jens Elkner REALMNAME, HOST_NAME);
c6e0f8b39af7343c04ec7558a085c965159f4ea0Lubos Kosco assert_non_null(ad_ctx->ad_options);
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco ad_ctx->gc_ctx = talloc_zero(ad_ctx, struct sdap_id_conn_ctx);
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco assert_non_null(ad_ctx->gc_ctx);
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
ff5eba819da0cf7964d884630fb13262ef12c505Trond Norbye ad_ctx->ldap_ctx = talloc_zero(ad_ctx, struct sdap_id_conn_ctx);
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco assert_non_null(ad_ctx->ldap_ctx);
b310c0aeaec52a0246021104b8f52cbb31b68480Lubos Kosco
ff5eba819da0cf7964d884630fb13262ef12c505Trond Norbye ad_ctx->sdap_id_ctx = talloc_zero(ad_ctx, struct sdap_id_ctx);
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco assert_non_null(ad_ctx->sdap_id_ctx);
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco
817883e9f0d419428e8236a09b77cdeeaa034df7Knut Anders Hatlen ad_ctx->sdap_id_ctx->opts = talloc_zero(ad_ctx->sdap_id_ctx,
be5cdf850da5383468637c6937c016f26bd339cfLubos Kosco struct sdap_options);
6d7c6f82e644c205bc679ee5b1fa2929ec949963Lubos Kosco assert_non_null(ad_ctx->sdap_id_ctx->opts);
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco
6d7c6f82e644c205bc679ee5b1fa2929ec949963Lubos Kosco ret = sdap_domain_add(ad_ctx->sdap_id_ctx->opts, test_ctx->dom, &sdom);
6d7c6f82e644c205bc679ee5b1fa2929ec949963Lubos Kosco assert_int_equal(ret, EOK);
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco
b645988bdc1cf4f2f82b8c00ed041ddddd822c24Lubos Kosco subdom_ad_ctx = talloc_zero(test_ctx, struct ad_id_ctx);
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes assert_non_null(subdom_ad_ctx);
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes subdom_ldap_ctx = talloc_zero(subdom_ad_ctx, struct sdap_id_conn_ctx);
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes assert_non_null(subdom_ldap_ctx);
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes subdom_ad_ctx->ldap_ctx = subdom_ldap_ctx;
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes ret = sdap_domain_add(ad_ctx->sdap_id_ctx->opts, test_ctx->subdom, &sdom);
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes assert_int_equal(ret, EOK);
b6ada49236374a1f4ae462c384d69bab7e6f0a1dGustavo Lopes sdom->pvt = subdom_ad_ctx;
eb32a77fdb57f20c042b7b79b28a4fb4060cb949Lubos Kosco
test_ctx->ad_ctx = ad_ctx;
test_ctx->subdom_ad_ctx = subdom_ad_ctx;
check_leaks_push(test_ctx);
*state = test_ctx;
}
static void
ad_common_test_teardown(void **state)
{
struct ad_common_test_ctx *test_ctx = talloc_get_type(*state,
struct ad_common_test_ctx);
assert_non_null(test_ctx);
assert_true(check_leaks_pop(test_ctx) == true);
talloc_free(test_ctx);
assert_true(check_leaks_pop(global_talloc_context) == true);
assert_true(leak_check_teardown());
}
errno_t
__wrap_sdap_set_sasl_options(struct sdap_options *id_opts,
char *default_primary,
char *default_realm,
const char *keytab_path)
{
/* Pretend SASL is fine */
return EOK;
}
void test_ldap_conn_list(void **state)
{
struct sdap_id_conn_ctx *conn;
struct ad_common_test_ctx *test_ctx = talloc_get_type(*state,
struct ad_common_test_ctx);
assert_non_null(test_ctx);
conn = ad_get_dom_ldap_conn(test_ctx->ad_ctx, test_ctx->dom);
assert_true(conn == test_ctx->ad_ctx->ldap_ctx);
conn = ad_get_dom_ldap_conn(test_ctx->ad_ctx, test_ctx->subdom);
assert_true(conn == test_ctx->subdom_ad_ctx->ldap_ctx);
}
void test_conn_list(void **state)
{
struct sdap_id_conn_ctx **conn_list;
struct ad_common_test_ctx *test_ctx = talloc_get_type(*state,
struct ad_common_test_ctx);
assert_non_null(test_ctx);
assert_true(dp_opt_get_bool(test_ctx->ad_ctx->ad_options->basic,
AD_ENABLE_GC));
conn_list = ad_gc_conn_list(test_ctx, test_ctx->ad_ctx, test_ctx->dom);
assert_non_null(conn_list);
assert_true(conn_list[0] == test_ctx->ad_ctx->gc_ctx);
/* If there is a fallback, we should ignore the offline mode */
assert_true(conn_list[0]->ignore_mark_offline);
assert_true(conn_list[1] == test_ctx->ad_ctx->ldap_ctx);
assert_false(conn_list[1]->ignore_mark_offline);
assert_null(conn_list[2]);
talloc_free(conn_list);
conn_list = ad_gc_conn_list(test_ctx, test_ctx->ad_ctx, test_ctx->subdom);
assert_non_null(conn_list);
assert_true(conn_list[0] == test_ctx->ad_ctx->gc_ctx);
assert_true(conn_list[0]->ignore_mark_offline);
assert_true(conn_list[1] == test_ctx->subdom_ad_ctx->ldap_ctx);
assert_false(conn_list[1]->ignore_mark_offline);
talloc_free(conn_list);
dp_opt_set_bool(test_ctx->ad_ctx->ad_options->basic, AD_ENABLE_GC, false);
assert_false(dp_opt_get_bool(test_ctx->ad_ctx->ad_options->basic,
AD_ENABLE_GC));
conn_list = ad_gc_conn_list(test_ctx, test_ctx->ad_ctx, test_ctx->dom);
assert_non_null(conn_list);
assert_true(conn_list[0] == test_ctx->ad_ctx->ldap_ctx);
assert_false(conn_list[0]->ignore_mark_offline);
assert_null(conn_list[1]);
talloc_free(conn_list);
conn_list = ad_gc_conn_list(test_ctx, test_ctx->ad_ctx, test_ctx->subdom);
assert_non_null(conn_list);
assert_true(conn_list[0] == test_ctx->subdom_ad_ctx->ldap_ctx);
talloc_free(conn_list);
}
int main(int argc, const char *argv[])
{
poptContext pc;
int opt;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_DEBUG_OPTS
POPT_TABLEEND
};
const UnitTest tests[] = {
unit_test_setup_teardown(test_ldap_conn_list,
ad_common_test_setup,
ad_common_test_teardown),
unit_test_setup_teardown(test_conn_list,
ad_common_test_setup,
ad_common_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_INIT(debug_level);
tests_set_cwd();
return run_tests(tests);
}