dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0146-TESTS-Add-unit-tests-for-cfg-validation.patch

ecf709
From 897216b87352e9f80181be6f1a036163c599ba46 Mon Sep 17 00:00:00 2001
ecf709
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
ecf709
Date: Fri, 26 May 2017 19:58:48 +0200
ecf709
Subject: [PATCH 146/152] TESTS: Add unit tests for cfg validation
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
Add infrastructure for unit tests for validators.
ecf709
ecf709
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
ecf709
---
ecf709
 Makefile.am                          |  16 +++
ecf709
 src/tests/cmocka/test_config_check.c | 268 +++++++++++++++++++++++++++++++++++
ecf709
 2 files changed, 284 insertions(+)
ecf709
 create mode 100644 src/tests/cmocka/test_config_check.c
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index a6279133b56dcd5bcbd1306ae8f2ce18d90c2c12..503c8cfd795b503f566431c08a56a56147180322 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -252,6 +252,7 @@ if HAVE_CMOCKA
ecf709
         dp_opt_tests \
ecf709
         responder-get-domains-tests \
ecf709
         sbus-internal-tests \
ecf709
+        config_check-tests \
ecf709
         sss_sifp-tests \
ecf709
         test_search_bases \
ecf709
         test_ldap_auth \
ecf709
@@ -2429,6 +2430,21 @@ sbus_internal_tests_LDADD = \
ecf709
     libsss_debug.la \
ecf709
     libsss_test_common.la
ecf709
 
ecf709
+config_check_tests_SOURCES = \
ecf709
+    src/tests/cmocka/test_config_check.c \
ecf709
+    $(NULL)
ecf709
+config_check_tests_CFLAGS = \
ecf709
+    $(AM_CFLAGS) \
ecf709
+    $(NULL)
ecf709
+config_check_tests_LDADD = \
ecf709
+    $(CMOCKA_LIBS) \
ecf709
+    $(POPT_LIBS) \
ecf709
+    $(INI_CONFIG_LIBS) \
ecf709
+    $(TALLOC_LIBS) \
ecf709
+    $(SSSD_INTERNAL_LTLIBS) \
ecf709
+    libsss_test_common.la \
ecf709
+    $(NULL)
ecf709
+
ecf709
 test_find_uid_SOURCES = \
ecf709
     src/tests/cmocka/test_find_uid.c \
ecf709
     src/util/find_uid.c \
ecf709
diff --git a/src/tests/cmocka/test_config_check.c b/src/tests/cmocka/test_config_check.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..8fc0b01f3ef3fe03152efd979a3e96c21ba567cc
ecf709
--- /dev/null
ecf709
+++ b/src/tests/cmocka/test_config_check.c
ecf709
@@ -0,0 +1,268 @@
ecf709
+/*
ecf709
+    Authors:
ecf709
+        Michal Zidek <mzidek@redhat.com>
ecf709
+
ecf709
+    Copyright (C) 2017 Red Hat
ecf709
+
ecf709
+    Config file validators test
ecf709
+
ecf709
+    This program is free software; you can redistribute it and/or modify
ecf709
+    it under the terms of the GNU General Public License as published by
ecf709
+    the Free Software Foundation; either version 3 of the License, or
ecf709
+    (at your option) any later version.
ecf709
+
ecf709
+    This program is distributed in the hope that it will be useful,
ecf709
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecf709
+    GNU General Public License for more details.
ecf709
+
ecf709
+    You should have received a copy of the GNU General Public License
ecf709
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+*/
ecf709
+
ecf709
+#include <popt.h>
ecf709
+#include <talloc.h>
ecf709
+#include <ini_configobj.h>
ecf709
+
ecf709
+#include "util/sss_ini.h"
ecf709
+#include "tests/cmocka/common_mock.h"
ecf709
+
ecf709
+#ifdef HAVE_LIBINI_CONFIG_V1_3
ecf709
+
ecf709
+#define RULES_PATH ABS_SRC_DIR"/src/config/cfg_rules.ini"
ecf709
+
ecf709
+struct sss_ini_initdata {
ecf709
+    char **error_list;
ecf709
+    struct ref_array *ra_success_list;
ecf709
+    struct ref_array *ra_error_list;
ecf709
+    struct ini_cfgobj *sssd_config;
ecf709
+    struct value_obj *obj;
ecf709
+    const struct stat *cstat;
ecf709
+    struct ini_cfgfile *file;
ecf709
+};
ecf709
+
ecf709
+void config_check_test_common(const char *cfg_string,
ecf709
+                              size_t num_errors_expected,
ecf709
+                              const char **errors_expected)
ecf709
+{
ecf709
+    struct sss_ini_initdata *init_data;
ecf709
+    size_t num_errors;
ecf709
+    char **strs;
ecf709
+    int ret;
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    assert_non_null(tmp_ctx);
ecf709
+
ecf709
+    init_data = sss_ini_initdata_init(tmp_ctx);
ecf709
+
ecf709
+    ret = ini_config_file_from_mem(discard_const(cfg_string),
ecf709
+                                   strlen(cfg_string),
ecf709
+                                   &init_data->file);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = ini_config_create(&(init_data->sssd_config));
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = ini_config_parse(init_data->file,
ecf709
+                           INI_STOP_ON_ANY,
ecf709
+                           INI_MV1S_OVERWRITE,
ecf709
+                           INI_PARSE_NOWRAP,
ecf709
+                           init_data->sssd_config);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sss_ini_call_validators_strs(tmp_ctx, init_data,
ecf709
+                                       RULES_PATH,
ecf709
+                                       &strs, &num_errors);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    /* Output from validators */
ecf709
+    for (int i = 0; i < num_errors; i++) {
ecf709
+        /* Keep this printf loop for faster debugging */
ecf709
+        printf("%s\n", strs[i]);
ecf709
+    }
ecf709
+
ecf709
+    for (int i = 0; i < num_errors && i <= num_errors_expected; i++) {
ecf709
+        assert_string_equal(strs[i], errors_expected[i]);
ecf709
+    }
ecf709
+
ecf709
+    /* Check if the number of errors is the same */
ecf709
+    assert_int_equal(num_errors_expected, num_errors);
ecf709
+
ecf709
+    sss_ini_close_file(init_data);
ecf709
+    sss_ini_config_destroy(init_data);
ecf709
+    talloc_free(tmp_ctx);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_section_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[sssssssssssssd]";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_sections]: Section [sssssssssssssd] is not allowed. "
ecf709
+        "Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_sssd_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[sssd]\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_sssd_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'sssd'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_pam_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[pam]\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_pam_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'pam'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_nss_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[nss]\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_nss_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'nss'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_pac_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[pac]\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_pac_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'pac'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_ifp_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[ifp]\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_ifp_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'ifp'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_domain_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[domain/A.test\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_subdomain_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'domain/A.test'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_appdomain_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[application/myapp\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_subdomain_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'application/myapp'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_bad_subdom_option_name(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[domain/A.test/B.A.test]\n"
ecf709
+                     "debug_leTYPOvel = 10\n";
ecf709
+    const char *expected_errors[] = {
ecf709
+        "[rule/allowed_sssd_options]: Attribute 'debug_leTYPOvel' is not "
ecf709
+        "allowed in section 'domain/A.test/B.A.test'. Check for typos.",
ecf709
+    };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 1, expected_errors);
ecf709
+}
ecf709
+
ecf709
+void config_check_test_good_sections(void **state)
ecf709
+{
ecf709
+    char cfg_str[] = "[sssd]\n"
ecf709
+                     "[pam]\n"
ecf709
+                     "[nss]\n"
ecf709
+                     "[domain/testdom.test]\n"
ecf709
+                     "[domain/testdom.test/testsubdom.testdom.test]\n"
ecf709
+                     "[application/myapp]\n"
ecf709
+                     "[secrets]\n"
ecf709
+                     "[ifp]\n"
ecf709
+                     "[pac]\n";
ecf709
+    const char *expected_errors[] = { NULL };
ecf709
+
ecf709
+    config_check_test_common(cfg_str, 0, expected_errors);
ecf709
+}
ecf709
+
ecf709
+int main(int argc, const char *argv[])
ecf709
+{
ecf709
+    poptContext pc;
ecf709
+    int opt;
ecf709
+    struct poptOption long_options[] = {
ecf709
+        POPT_AUTOHELP
ecf709
+        SSSD_DEBUG_OPTS
ecf709
+        POPT_TABLEEND
ecf709
+    };
ecf709
+
ecf709
+    const struct CMUnitTest tests[] = {
ecf709
+        cmocka_unit_test(config_check_test_bad_section_name),
ecf709
+        cmocka_unit_test(config_check_test_bad_sssd_option_name),
ecf709
+        cmocka_unit_test(config_check_test_bad_pam_option_name),
ecf709
+        cmocka_unit_test(config_check_test_bad_nss_option_name),
ecf709
+        cmocka_unit_test(config_check_test_bad_pac_option_name),
ecf709
+        cmocka_unit_test(config_check_test_bad_ifp_option_name),
ecf709
+        cmocka_unit_test(config_check_test_good_sections),
ecf709
+    };
ecf709
+
ecf709
+    /* Set debug level to invalid value so we can decide if -d 0 was used. */
ecf709
+    debug_level = SSSDBG_INVALID;
ecf709
+
ecf709
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
ecf709
+    while ((opt = poptGetNextOpt(pc)) != -1) {
ecf709
+        switch (opt) {
ecf709
+        default:
ecf709
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
ecf709
+                    poptBadOption(pc, 0), poptStrerror(opt));
ecf709
+            poptPrintUsage(pc, stderr, 0);
ecf709
+            return 1;
ecf709
+        }
ecf709
+    }
ecf709
+    poptFreeContext(pc);
ecf709
+
ecf709
+    DEBUG_CLI_INIT(debug_level);
ecf709
+    tests_set_cwd();
ecf709
+    return cmocka_run_group_tests(tests, NULL, NULL);
ecf709
+}
ecf709
+
ecf709
+#else /* !HAVE_LIBINI_CONFIG_V1_3 */
ecf709
+
ecf709
+int main(int argc, const char *argv[])
ecf709
+{
ecf709
+    fprintf(stderr, "%s requires newer version of libini\n", argv[0]);
ecf709
+    return 0;
ecf709
+}
ecf709
+
ecf709
+#endif /* HAVE_LIBINI_CONFIG_V1_3 */
ecf709
-- 
ecf709
2.9.4
ecf709