test_utils.c revision f64296c40e07614668c4ac4c978bc8980fb6a7db
af84459fbf938e508fd10b01cb8d699c79083813takashi/*
af84459fbf938e508fd10b01cb8d699c79083813takashi Authors:
af84459fbf938e508fd10b01cb8d699c79083813takashi Sumit Bose <sbose@redhat.com>
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi Copyright (C) 2013 Red Hat
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi SSSD tests: Tests for utility functions
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi This program is free software; you can redistribute it and/or modify
af84459fbf938e508fd10b01cb8d699c79083813takashi it under the terms of the GNU General Public License as published by
af84459fbf938e508fd10b01cb8d699c79083813takashi the Free Software Foundation; either version 3 of the License, or
af84459fbf938e508fd10b01cb8d699c79083813takashi (at your option) any later version.
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi This program is distributed in the hope that it will be useful,
af84459fbf938e508fd10b01cb8d699c79083813takashi but WITHOUT ANY WARRANTY; without even the implied warranty of
af84459fbf938e508fd10b01cb8d699c79083813takashi MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
af84459fbf938e508fd10b01cb8d699c79083813takashi GNU General Public License for more details.
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi You should have received a copy of the GNU General Public License
af84459fbf938e508fd10b01cb8d699c79083813takashi along with this program. If not, see <http://www.gnu.org/licenses/>.
af84459fbf938e508fd10b01cb8d699c79083813takashi*/
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#include <popt.h>
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "tests/cmocka/common_mock.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#define DOM_COUNT 10
3c13a815670b54d1c17bf02954f7d2b066cde95cnd#define DOMNAME_TMPL "name_%zu.dom"
3c13a815670b54d1c17bf02954f7d2b066cde95cnd#define FLATNAME_TMPL "name_%zu"
2d39a41e98476f5235b7c37ce745a4aa0904b1cbrbowen#define SID_TMPL "S-1-5-21-1-2-%zu"
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowestruct dom_list_test_ctx {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe size_t dom_count;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct sss_domain_info *dom_list;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe};
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowevoid setup_dom_list(void **state)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx *test_ctx;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct sss_domain_info *dom = NULL;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t c;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi assert_true(leak_check_setup());
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi test_ctx = talloc_zero(global_talloc_context, struct dom_list_test_ctx);
af84459fbf938e508fd10b01cb8d699c79083813takashi assert_non_null(test_ctx);
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi test_ctx->dom_count = DOM_COUNT;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi for (c = 0; c < test_ctx->dom_count; c++) {
af84459fbf938e508fd10b01cb8d699c79083813takashi dom = talloc_zero(test_ctx, struct sss_domain_info);
af84459fbf938e508fd10b01cb8d699c79083813takashi assert_non_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
af84459fbf938e508fd10b01cb8d699c79083813takashi dom->name = talloc_asprintf(dom, DOMNAME_TMPL, c);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd assert_non_null(dom->name);
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi dom->flat_name = talloc_asprintf(dom, FLATNAME_TMPL, c);
af84459fbf938e508fd10b01cb8d699c79083813takashi assert_non_null(dom->flat_name);
af84459fbf938e508fd10b01cb8d699c79083813takashi
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom->domain_id = talloc_asprintf(dom, SID_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(dom->domain_id);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe DLIST_ADD(test_ctx->dom_list, dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe check_leaks_push(test_ctx);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe *state = test_ctx;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowevoid teardown_dom_list(void **state)
78f97ce162b66a0dbfd7af4dcd9984f162569b04minfrin{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe if (test_ctx == NULL) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe DEBUG(SSSDBG_CRIT_FAILURE, ("Type mismatch\n"));
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_true(check_leaks_pop(test_ctx) == true);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe talloc_free(test_ctx);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_true(leak_check_teardown());
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowevoid test_find_subdomain_by_sid_null(void **state)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx);
af84459fbf938e508fd10b01cb8d699c79083813takashi struct sss_domain_info *dom;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi dom = find_subdomain_by_sid(NULL, NULL);
af84459fbf938e508fd10b01cb8d699c79083813takashi assert_null(dom);
af84459fbf938e508fd10b01cb8d699c79083813takashi
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom = find_subdomain_by_sid(test_ctx->dom_list, NULL);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom = find_subdomain_by_sid(NULL, "S-1-5-21-1-2-3");
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowevoid test_find_subdomain_by_sid(void **state)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct sss_domain_info *dom;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe size_t c;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe char *name;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe char *flat_name;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe char *sid;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe for (c = 0; c < test_ctx->dom_count; c++) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(flat_name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(sid);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom = find_subdomain_by_sid(test_ctx->dom_list, sid);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_string_equal(name, dom->name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_string_equal(flat_name, dom->flat_name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_string_equal(sid, dom->domain_id);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe talloc_free(name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe talloc_free(flat_name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe talloc_free(sid);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowevoid test_find_subdomain_by_sid_missing_sid(void **state)
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct dom_list_test_ctx);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe struct sss_domain_info *dom;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe size_t c;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe char *name;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe char *flat_name;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe char *sid;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe size_t mis;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe mis = test_ctx->dom_count/2;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_true((mis >= 1 && mis < test_ctx->dom_count));
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom = test_ctx->dom_list;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe for (c = 0; c < mis; c++) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom = dom->next;
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe dom->domain_id = NULL;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe for (c = 0; c < test_ctx->dom_count; c++) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(name);
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(flat_name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(sid);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
af84459fbf938e508fd10b01cb8d699c79083813takashi dom = find_subdomain_by_sid(test_ctx->dom_list, sid);
af84459fbf938e508fd10b01cb8d699c79083813takashi if (c == mis - 1) {
2d39a41e98476f5235b7c37ce745a4aa0904b1cbrbowen assert_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe } else {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_non_null(dom);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_string_equal(name, dom->name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_string_equal(flat_name, dom->flat_name);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe assert_string_equal(sid, dom->domain_id);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
c8c717fafa0a09ed13469a603a178921b851dd22igalic talloc_free(name);
c8c717fafa0a09ed13469a603a178921b851dd22igalic talloc_free(flat_name);
c8c717fafa0a09ed13469a603a178921b851dd22igalic talloc_free(sid);
7c7e501f542451bf7225b23cb299ee4228bfe15dgryzor }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wroweint main(int argc, const char *argv[])
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe{
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe poptContext pc;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe int opt;
c8c717fafa0a09ed13469a603a178921b851dd22igalic struct poptOption long_options[] = {
7c7e501f542451bf7225b23cb299ee4228bfe15dgryzor POPT_AUTOHELP
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe SSSD_DEBUG_OPTS
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe POPT_TABLEEND
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe };
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe const UnitTest tests[] = {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe unit_test_setup_teardown(test_find_subdomain_by_sid_null,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe setup_dom_list, teardown_dom_list),
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe unit_test_setup_teardown(test_find_subdomain_by_sid,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe setup_dom_list, teardown_dom_list),
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe unit_test_setup_teardown(test_find_subdomain_by_sid_missing_sid,
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe setup_dom_list, teardown_dom_list),
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe };
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe /* Set debug level to invalid value so we can deside if -d 0 was used. */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe debug_level = SSSDBG_INVALID;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe pc = poptGetContext(argv[0], argc, argv, long_options, 0);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe while((opt = poptGetNextOpt(pc)) != -1) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe switch(opt) {
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe default:
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe fprintf(stderr, "\nInvalid option %s: %s\n\n",
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe poptBadOption(pc, 0), poptStrerror(opt));
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe poptPrintUsage(pc, stderr, 0);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return 1;
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe }
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe poptFreeContext(pc);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe DEBUG_INIT(debug_level);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe /* Even though normally the tests should clean up after themselves
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe * they might not after a failed run. Remove the old db to be sure */
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe tests_set_cwd();
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe return run_tests(tests);
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe}
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe