Blob Blame History Raw
From 092c9d35a4cc85c9910669bb3a8169f000ebc69c Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Wed, 6 May 2015 08:06:53 +0200
Subject: [PATCH 204/207] DP: Add a function to inherit DP options, if set

Related to:
    https://fedorahosted.org/sssd/ticket/2644

Adds a utility function that checks if a DP option is present in
the subdomain_inherit list. If it is, then the option is set from source
to destination dp_option array.

Reviewed-by: Pavel Reichl <preichl@redhat.com>
(cherry picked from commit b3d110fbc424a03674a6e50e489a7cbab9702f0b)

Conflicts:
	src/tests/cmocka/test_dp_opts.c
---
 src/providers/data_provider.h      |   5 ++
 src/providers/data_provider_opts.c |  57 +++++++++++++++++
 src/tests/cmocka/test_dp_opts.c    | 127 ++++++++++++++++++++++++++++++++++---
 3 files changed, 181 insertions(+), 8 deletions(-)

diff --git a/src/providers/data_provider.h b/src/providers/data_provider.h
index 5df493e..657d2b7 100644
--- a/src/providers/data_provider.h
+++ b/src/providers/data_provider.h
@@ -277,6 +277,11 @@ struct dp_option {
 
 #define DP_OPTION_TERMINATOR { NULL, 0, NULL_STRING, NULL_STRING }
 
+void dp_option_inherit(char **inherit_opt_list,
+                       int option,
+                       struct dp_option *parent_opts,
+                       struct dp_option *subdom_opts);
+
 int dp_get_options(TALLOC_CTX *memctx,
                    struct confdb_ctx *cdb,
                    const char *conf_path,
diff --git a/src/providers/data_provider_opts.c b/src/providers/data_provider_opts.c
index 8ad8456..9db43fc 100644
--- a/src/providers/data_provider_opts.c
+++ b/src/providers/data_provider_opts.c
@@ -21,6 +21,63 @@
 
 #include "data_provider.h"
 
+/* =Copy-Option-From-Subdomain-If-Allowed================================= */
+void dp_option_inherit(char **inherit_opt_list,
+                       int option,
+                       struct dp_option *parent_opts,
+                       struct dp_option *subdom_opts)
+{
+    errno_t ret;
+    bool inherit_option;
+
+    inherit_option = string_in_list(parent_opts[option].opt_name,
+                                    inherit_opt_list, false);
+    if (inherit_option == false) {
+        DEBUG(SSSDBG_CONF_SETTINGS,
+              "Option %s is not set up to be inherited\n",
+              parent_opts[option].opt_name);
+        return;
+    }
+
+    DEBUG(SSSDBG_CONF_SETTINGS,
+          "Will inherit option %s\n", parent_opts[option].opt_name);
+    switch (parent_opts[option].type) {
+    case DP_OPT_NUMBER:
+        ret = dp_opt_set_int(subdom_opts,
+                             option,
+                             dp_opt_get_int(parent_opts,
+                                            option));
+        break;
+    case DP_OPT_STRING:
+        ret = dp_opt_set_string(subdom_opts,
+                                option,
+                                dp_opt_get_string(parent_opts,
+                                                  option));
+        break;
+    case DP_OPT_BLOB:
+        ret = dp_opt_set_blob(subdom_opts,
+                              option,
+                              dp_opt_get_blob(parent_opts,
+                                              option));
+        break;
+    case DP_OPT_BOOL:
+        ret = dp_opt_set_bool(subdom_opts,
+                              option,
+                              dp_opt_get_bool(parent_opts,
+                                              option));
+        break;
+    default:
+        ret = EINVAL;
+        break;
+    }
+
+    if (ret != EOK) {
+        DEBUG(SSSDBG_MINOR_FAILURE,
+              "Failed to inherit option %s\n", parent_opts[option].opt_name);
+        /* Not fatal */
+    }
+}
+
 /* =Retrieve-Options====================================================== */
 
 int dp_get_options(TALLOC_CTX *memctx,
diff --git a/src/tests/cmocka/test_dp_opts.c b/src/tests/cmocka/test_dp_opts.c
index 0f3052a..60267ab 100644
--- a/src/tests/cmocka/test_dp_opts.c
+++ b/src/tests/cmocka/test_dp_opts.c
@@ -284,37 +284,63 @@ void opt_test_getset_teardown(void **state)
     talloc_free(opts);
 }
 
-void opt_test_getset_string(void **state)
+static void assert_nondefault_string_empty(struct dp_option *opts)
 {
-    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
-    int ret;
     char *s;
 
     s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
     assert_null(s);
+}
+
+static void set_nondefault_string(struct dp_option *opts)
+{
+    int ret;
 
     ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
     assert_int_equal(ret, EOK);
+}
+
+static void check_nondefault_string(struct dp_option *opts)
+{
+    char *s;
 
     s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
     assert_non_null(s);
     assert_string_equal(s, "str1");
 }
 
-void opt_test_getset_blob(void **state)
+void opt_test_getset_string(void **state)
 {
     struct dp_option *opts = talloc_get_type(*state, struct dp_option);
-    int ret;
+
+    assert_nondefault_string_empty(opts);
+    set_nondefault_string(opts);
+    check_nondefault_string(opts);
+}
+
+static void assert_nondefault_blob_empty(struct dp_option *opts)
+{
     struct dp_opt_blob b;
 
     b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
     assert_null(b.data);
     assert_int_equal(b.length, 0);
+}
+
+static void set_nondefault_blob(struct dp_option *opts)
+{
+    struct dp_opt_blob b;
+    int ret;
 
     b.data = discard_const_p(uint8_t, "blob2");
     b.length = strlen("blob2");
     ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
     assert_int_equal(ret, EOK);
+}
+
+static void check_nondefault_blob(struct dp_option *opts)
+{
+    struct dp_opt_blob b;
 
     b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
     assert_non_null(b.data);
@@ -322,22 +348,45 @@ void opt_test_getset_blob(void **state)
     assert_memory_equal(b.data, "blob2", strlen("blob2"));
 }
 
-void opt_test_getset_int(void **state)
+void opt_test_getset_blob(void **state)
 {
     struct dp_option *opts = talloc_get_type(*state, struct dp_option);
-    int ret;
-    int i;
 
+    assert_nondefault_blob_empty(opts);
+    set_nondefault_blob(opts);
+    check_nondefault_blob(opts);
+}
+
+static void assert_nondefault_int_notset(struct dp_option *opts)
+{
+    int i;
     i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
     assert_int_equal(i, 0);
+}
 
+static void set_nondefault_int(struct dp_option *opts)
+{
+    int ret;
     ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
     assert_int_equal(ret, EOK);
+}
 
+static void assert_nondefault_int_set(struct dp_option *opts)
+{
+    int i;
     i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
     assert_int_equal(i, 456);
 }
 
+void opt_test_getset_int(void **state)
+{
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
+
+    assert_nondefault_int_notset(opts);
+    set_nondefault_int(opts);
+    assert_nondefault_int_set(opts);
+}
+
 void opt_test_getset_bool(void **state)
 {
     struct dp_option *opts = talloc_get_type(*state, struct dp_option);
@@ -354,6 +403,65 @@ void opt_test_getset_bool(void **state)
     assert_false(b == true);
 }
 
+void opt_test_inherit(void **state)
+{
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
+    int ret;
+    struct dp_option *opts_copy;
+    const char *s;
+    const char *sd_inherit_match[] = { "string_nodefault",
+                                       "blob_nodefault",
+                                       "int_nodefault",
+                                       "bool_true",
+                                       NULL };
+
+    ret = dp_copy_defaults(opts, test_def_opts,
+                           OPT_NUM_OPTS, &opts_copy);
+    assert_int_equal(ret, EOK);
+    assert_defaults(opts);
+
+    dp_option_inherit(NULL, OPT_STRING_NODEFAULT,
+                      opts, opts_copy);
+    s = dp_opt_get_string(opts_copy, OPT_STRING_NODEFAULT);
+    assert_null(s);
+
+    /* string */
+    assert_nondefault_string_empty(opts_copy);
+    set_nondefault_string(opts);
+    dp_option_inherit(discard_const(sd_inherit_match),
+                      OPT_STRING_NODEFAULT,
+                      opts, opts_copy);
+    check_nondefault_string(opts_copy);
+
+    /* blob */
+    assert_nondefault_blob_empty(opts_copy);
+    set_nondefault_blob(opts);
+    dp_option_inherit(discard_const(sd_inherit_match),
+                      OPT_BLOB_NODEFAULT,
+                      opts, opts_copy);
+    check_nondefault_blob(opts_copy);
+
+    /* number */
+    assert_nondefault_int_notset(opts_copy);
+    set_nondefault_int(opts);
+    dp_option_inherit(discard_const(sd_inherit_match),
+                      OPT_INT_NODEFAULT,
+                      opts, opts_copy);
+    assert_nondefault_int_set(opts_copy);
+
+    /* bool */
+    assert_true(dp_opt_get_bool(opts_copy, OPT_BOOL_TRUE));
+
+    ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
+    assert_int_equal(ret, EOK);
+
+    dp_option_inherit(discard_const(sd_inherit_match),
+                      OPT_BOOL_TRUE,
+                      opts, opts_copy);
+
+    assert_false(dp_opt_get_bool(opts_copy, OPT_BOOL_TRUE));
+}
+
 int main(int argc, const char *argv[])
 {
     int no_cleanup = 0;
@@ -380,6 +488,9 @@ int main(int argc, const char *argv[])
         unit_test_setup_teardown(opt_test_getset_blob,
                                  opt_test_getset_setup,
                                  opt_test_getset_teardown),
+        unit_test_setup_teardown(opt_test_inherit,
+                                 opt_test_getset_setup,
+                                 opt_test_getset_teardown),
         unit_test(opt_test_copy_default),
         unit_test(opt_test_copy_options),
         unit_test(opt_test_get)
-- 
2.1.0