From e1a86771137f67e0e8d24c4bbb014bfc21d85538 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Wed, 23 Oct 2013 14:39:55 +0200 Subject: [PATCH 1/4] find_subdomain_by_sid: skip domains with missing domain_id --- Makefile.am | 12 ++- src/tests/cmocka/test_utils.c | 221 ++++++++++++++++++++++++++++++++++++++++++ src/util/domain_info_utils.c | 30 +++--- 3 files changed, 251 insertions(+), 12 deletions(-) create mode 100644 src/tests/cmocka/test_utils.c diff --git a/Makefile.am b/Makefile.am index cb24df8..d2a7384 100644 --- a/Makefile.am +++ b/Makefile.am @@ -153,7 +153,8 @@ if HAVE_CMOCKA sss_nss_idmap-tests \ dyndns-tests \ fqnames-tests \ - test_sss_idmap + test_sss_idmap \ + test_utils endif check_PROGRAMS = \ @@ -1397,6 +1398,15 @@ test_sss_idmap_LDADD = \ $(SSSD_INTERNAL_LTLIBS) \ libsss_test_common.la +test_utils_SOURCES = \ + $(TEST_MOCK_OBJ) \ + src/tests/cmocka/test_utils.c +test_utils_CFLAGS = \ + $(AM_CFLAGS) +test_utils_LDADD = \ + $(CMOCKA_LIBS) \ + $(SSSD_INTERNAL_LTLIBS) \ + libsss_test_common.la endif noinst_PROGRAMS = pam_test_client diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c new file mode 100644 index 0000000..9152dcf --- /dev/null +++ b/src/tests/cmocka/test_utils.c @@ -0,0 +1,221 @@ +/* + Authors: + Sumit Bose + + Copyright (C) 2013 Red Hat + + SSSD tests: Tests for utility functions + + 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 "tests/cmocka/common_mock.h" + +#define DOM_COUNT 10 +#define DOMNAME_TMPL "name_%u.dom" +#define FLATNAME_TMPL "name_%u" +#define SID_TMPL "S-1-5-21-1-2-%u" + +struct dom_list_test_ctx { + size_t dom_count; + struct sss_domain_info *dom_list; +}; + +void setup_dom_list(void **state) +{ + struct dom_list_test_ctx *test_ctx; + struct sss_domain_info *dom = NULL; + size_t c; + + assert_true(leak_check_setup()); + + test_ctx = talloc_zero(global_talloc_context, struct dom_list_test_ctx); + assert_non_null(test_ctx); + + test_ctx->dom_count = DOM_COUNT; + + for (c = 0; c < test_ctx->dom_count; c++) { + dom = talloc_zero(test_ctx, struct sss_domain_info); + assert_non_null(dom); + + dom->name = talloc_asprintf(dom, DOMNAME_TMPL, c); + assert_non_null(dom->name); + + dom->flat_name = talloc_asprintf(dom, FLATNAME_TMPL, c); + assert_non_null(dom->flat_name); + + dom->domain_id = talloc_asprintf(dom, SID_TMPL, c); + assert_non_null(dom->domain_id); + + DLIST_ADD(test_ctx->dom_list, dom); + } + + check_leaks_push(test_ctx); + *state = test_ctx; +} + +void teardown_dom_list(void **state) +{ + struct dom_list_test_ctx *test_ctx = talloc_get_type(*state, + struct dom_list_test_ctx); + if (test_ctx == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, ("Type mismatch\n")); + return; + } + + assert_true(check_leaks_pop(test_ctx) == true); + talloc_free(test_ctx); + assert_true(leak_check_teardown()); +} + +void test_find_subdomain_by_sid_null(void **state) +{ + struct dom_list_test_ctx *test_ctx = talloc_get_type(*state, + struct dom_list_test_ctx); + struct sss_domain_info *dom; + + dom = find_subdomain_by_sid(NULL, NULL); + assert_null(dom); + + dom = find_subdomain_by_sid(test_ctx->dom_list, NULL); + assert_null(dom); + + dom = find_subdomain_by_sid(NULL, "S-1-5-21-1-2-3"); + assert_null(dom); +} + +void test_find_subdomain_by_sid(void **state) +{ + struct dom_list_test_ctx *test_ctx = talloc_get_type(*state, + struct dom_list_test_ctx); + struct sss_domain_info *dom; + size_t c; + char *name; + char *flat_name; + char *sid; + + for (c = 0; c < test_ctx->dom_count; c++) { + name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c); + assert_non_null(name); + + flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c); + assert_non_null(flat_name); + + sid = talloc_asprintf(global_talloc_context, SID_TMPL, c); + assert_non_null(sid); + + dom = find_subdomain_by_sid(test_ctx->dom_list, sid); + assert_non_null(dom); + assert_string_equal(name, dom->name); + assert_string_equal(flat_name, dom->flat_name); + assert_string_equal(sid, dom->domain_id); + + talloc_free(name); + talloc_free(flat_name); + talloc_free(sid); + } +} + +void test_find_subdomain_by_sid_missing_sid(void **state) +{ + struct dom_list_test_ctx *test_ctx = talloc_get_type(*state, + struct dom_list_test_ctx); + struct sss_domain_info *dom; + size_t c; + char *name; + char *flat_name; + char *sid; + size_t mis; + + mis = test_ctx->dom_count/2; + assert_true((mis >= 1 && mis < test_ctx->dom_count)); + + dom = test_ctx->dom_list; + for (c = 0; c < mis; c++) { + assert_non_null(dom); + dom = dom->next; + } + assert_non_null(dom); + dom->domain_id = NULL; + + for (c = 0; c < test_ctx->dom_count; c++) { + name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c); + assert_non_null(name); + + flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c); + assert_non_null(flat_name); + + sid = talloc_asprintf(global_talloc_context, SID_TMPL, c); + assert_non_null(sid); + + dom = find_subdomain_by_sid(test_ctx->dom_list, sid); + if (c == mis - 1) { + assert_null(dom); + } else { + assert_non_null(dom); + assert_string_equal(name, dom->name); + assert_string_equal(flat_name, dom->flat_name); + assert_string_equal(sid, dom->domain_id); + } + + talloc_free(name); + talloc_free(flat_name); + talloc_free(sid); + } +} + +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_find_subdomain_by_sid_null, + setup_dom_list, teardown_dom_list), + unit_test_setup_teardown(test_find_subdomain_by_sid, + setup_dom_list, teardown_dom_list), + unit_test_setup_teardown(test_find_subdomain_by_sid_missing_sid, + setup_dom_list, teardown_dom_list), + }; + + /* 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); + + /* 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 run_tests(tests); +} diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c index 9d7bb5f..9f2f154 100644 --- a/src/util/domain_info_utils.c +++ b/src/util/domain_info_utils.c @@ -112,26 +112,34 @@ struct sss_domain_info *find_subdomain_by_sid(struct sss_domain_info *domain, const char *sid) { struct sss_domain_info *dom = domain; - size_t sid_len = strlen(sid); + size_t sid_len; size_t dom_sid_len; + if (sid == NULL) { + return NULL; + } + + sid_len = strlen(sid); + while (dom && dom->disabled) { dom = get_next_domain(dom, true); } while (dom) { - dom_sid_len = strlen(dom->domain_id); + if (dom->domain_id != NULL) { + dom_sid_len = strlen(dom->domain_id); - if (strncasecmp(dom->domain_id, sid, dom_sid_len) == 0) { - if (dom_sid_len == sid_len) { - /* sid is domain sid */ - return dom; - } + if (strncasecmp(dom->domain_id, sid, dom_sid_len) == 0) { + if (dom_sid_len == sid_len) { + /* sid is domain sid */ + return dom; + } - /* sid is object sid, check if domain sid is align with - * sid first subauthority component */ - if (sid[dom_sid_len] == '-') { - return dom; + /* sid is object sid, check if domain sid is align with + * sid first subauthority component */ + if (sid[dom_sid_len] == '-') { + return dom; + } } } -- 1.8.3.1