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

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