Blame SOURCES/0204-DP-Add-a-function-to-inherit-DP-options-if-set.patch

e543c9
From 092c9d35a4cc85c9910669bb3a8169f000ebc69c Mon Sep 17 00:00:00 2001
e543c9
From: Jakub Hrozek <jhrozek@redhat.com>
e543c9
Date: Wed, 6 May 2015 08:06:53 +0200
e543c9
Subject: [PATCH 204/207] DP: Add a function to inherit DP options, if set
e543c9
e543c9
Related to:
e543c9
    https://fedorahosted.org/sssd/ticket/2644
e543c9
e543c9
Adds a utility function that checks if a DP option is present in
e543c9
the subdomain_inherit list. If it is, then the option is set from source
e543c9
to destination dp_option array.
e543c9
e543c9
Reviewed-by: Pavel Reichl <preichl@redhat.com>
e543c9
(cherry picked from commit b3d110fbc424a03674a6e50e489a7cbab9702f0b)
e543c9
e543c9
Conflicts:
e543c9
	src/tests/cmocka/test_dp_opts.c
e543c9
---
e543c9
 src/providers/data_provider.h      |   5 ++
e543c9
 src/providers/data_provider_opts.c |  57 +++++++++++++++++
e543c9
 src/tests/cmocka/test_dp_opts.c    | 127 ++++++++++++++++++++++++++++++++++---
e543c9
 3 files changed, 181 insertions(+), 8 deletions(-)
e543c9
e543c9
diff --git a/src/providers/data_provider.h b/src/providers/data_provider.h
e543c9
index 5df493e..657d2b7 100644
e543c9
--- a/src/providers/data_provider.h
e543c9
+++ b/src/providers/data_provider.h
e543c9
@@ -277,6 +277,11 @@ struct dp_option {
e543c9
 
e543c9
 #define DP_OPTION_TERMINATOR { NULL, 0, NULL_STRING, NULL_STRING }
e543c9
 
e543c9
+void dp_option_inherit(char **inherit_opt_list,
e543c9
+                       int option,
e543c9
+                       struct dp_option *parent_opts,
e543c9
+                       struct dp_option *subdom_opts);
e543c9
+
e543c9
 int dp_get_options(TALLOC_CTX *memctx,
e543c9
                    struct confdb_ctx *cdb,
e543c9
                    const char *conf_path,
e543c9
diff --git a/src/providers/data_provider_opts.c b/src/providers/data_provider_opts.c
e543c9
index 8ad8456..9db43fc 100644
e543c9
--- a/src/providers/data_provider_opts.c
e543c9
+++ b/src/providers/data_provider_opts.c
e543c9
@@ -21,6 +21,63 @@
e543c9
 
e543c9
 #include "data_provider.h"
e543c9
 
e543c9
+/* =Copy-Option-From-Subdomain-If-Allowed================================= */
e543c9
+void dp_option_inherit(char **inherit_opt_list,
e543c9
+                       int option,
e543c9
+                       struct dp_option *parent_opts,
e543c9
+                       struct dp_option *subdom_opts)
e543c9
+{
e543c9
+    errno_t ret;
e543c9
+    bool inherit_option;
e543c9
+
e543c9
+    inherit_option = string_in_list(parent_opts[option].opt_name,
e543c9
+                                    inherit_opt_list, false);
e543c9
+    if (inherit_option == false) {
e543c9
+        DEBUG(SSSDBG_CONF_SETTINGS,
e543c9
+              "Option %s is not set up to be inherited\n",
e543c9
+              parent_opts[option].opt_name);
e543c9
+        return;
e543c9
+    }
e543c9
+
e543c9
+    DEBUG(SSSDBG_CONF_SETTINGS,
e543c9
+          "Will inherit option %s\n", parent_opts[option].opt_name);
e543c9
+    switch (parent_opts[option].type) {
e543c9
+    case DP_OPT_NUMBER:
e543c9
+        ret = dp_opt_set_int(subdom_opts,
e543c9
+                             option,
e543c9
+                             dp_opt_get_int(parent_opts,
e543c9
+                                            option));
e543c9
+        break;
e543c9
+    case DP_OPT_STRING:
e543c9
+        ret = dp_opt_set_string(subdom_opts,
e543c9
+                                option,
e543c9
+                                dp_opt_get_string(parent_opts,
e543c9
+                                                  option));
e543c9
+        break;
e543c9
+    case DP_OPT_BLOB:
e543c9
+        ret = dp_opt_set_blob(subdom_opts,
e543c9
+                              option,
e543c9
+                              dp_opt_get_blob(parent_opts,
e543c9
+                                              option));
e543c9
+        break;
e543c9
+    case DP_OPT_BOOL:
e543c9
+        ret = dp_opt_set_bool(subdom_opts,
e543c9
+                              option,
e543c9
+                              dp_opt_get_bool(parent_opts,
e543c9
+                                              option));
e543c9
+        break;
e543c9
+    default:
e543c9
+        ret = EINVAL;
e543c9
+        break;
e543c9
+    }
e543c9
+
e543c9
+    if (ret != EOK) {
e543c9
+        DEBUG(SSSDBG_MINOR_FAILURE,
e543c9
+              "Failed to inherit option %s\n", parent_opts[option].opt_name);
e543c9
+        /* Not fatal */
e543c9
+    }
e543c9
+}
e543c9
+
e543c9
 /* =Retrieve-Options====================================================== */
e543c9
 
e543c9
 int dp_get_options(TALLOC_CTX *memctx,
e543c9
diff --git a/src/tests/cmocka/test_dp_opts.c b/src/tests/cmocka/test_dp_opts.c
e543c9
index 0f3052a..60267ab 100644
e543c9
--- a/src/tests/cmocka/test_dp_opts.c
e543c9
+++ b/src/tests/cmocka/test_dp_opts.c
e543c9
@@ -284,37 +284,63 @@ void opt_test_getset_teardown(void **state)
e543c9
     talloc_free(opts);
e543c9
 }
e543c9
 
e543c9
-void opt_test_getset_string(void **state)
e543c9
+static void assert_nondefault_string_empty(struct dp_option *opts)
e543c9
 {
e543c9
-    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
e543c9
-    int ret;
e543c9
     char *s;
e543c9
 
e543c9
     s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
e543c9
     assert_null(s);
e543c9
+}
e543c9
+
e543c9
+static void set_nondefault_string(struct dp_option *opts)
e543c9
+{
e543c9
+    int ret;
e543c9
 
e543c9
     ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
e543c9
     assert_int_equal(ret, EOK);
e543c9
+}
e543c9
+
e543c9
+static void check_nondefault_string(struct dp_option *opts)
e543c9
+{
e543c9
+    char *s;
e543c9
 
e543c9
     s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
e543c9
     assert_non_null(s);
e543c9
     assert_string_equal(s, "str1");
e543c9
 }
e543c9
 
e543c9
-void opt_test_getset_blob(void **state)
e543c9
+void opt_test_getset_string(void **state)
e543c9
 {
e543c9
     struct dp_option *opts = talloc_get_type(*state, struct dp_option);
e543c9
-    int ret;
e543c9
+
e543c9
+    assert_nondefault_string_empty(opts);
e543c9
+    set_nondefault_string(opts);
e543c9
+    check_nondefault_string(opts);
e543c9
+}
e543c9
+
e543c9
+static void assert_nondefault_blob_empty(struct dp_option *opts)
e543c9
+{
e543c9
     struct dp_opt_blob b;
e543c9
 
e543c9
     b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
e543c9
     assert_null(b.data);
e543c9
     assert_int_equal(b.length, 0);
e543c9
+}
e543c9
+
e543c9
+static void set_nondefault_blob(struct dp_option *opts)
e543c9
+{
e543c9
+    struct dp_opt_blob b;
e543c9
+    int ret;
e543c9
 
e543c9
     b.data = discard_const_p(uint8_t, "blob2");
e543c9
     b.length = strlen("blob2");
e543c9
     ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
e543c9
     assert_int_equal(ret, EOK);
e543c9
+}
e543c9
+
e543c9
+static void check_nondefault_blob(struct dp_option *opts)
e543c9
+{
e543c9
+    struct dp_opt_blob b;
e543c9
 
e543c9
     b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
e543c9
     assert_non_null(b.data);
e543c9
@@ -322,22 +348,45 @@ void opt_test_getset_blob(void **state)
e543c9
     assert_memory_equal(b.data, "blob2", strlen("blob2"));
e543c9
 }
e543c9
 
e543c9
-void opt_test_getset_int(void **state)
e543c9
+void opt_test_getset_blob(void **state)
e543c9
 {
e543c9
     struct dp_option *opts = talloc_get_type(*state, struct dp_option);
e543c9
-    int ret;
e543c9
-    int i;
e543c9
 
e543c9
+    assert_nondefault_blob_empty(opts);
e543c9
+    set_nondefault_blob(opts);
e543c9
+    check_nondefault_blob(opts);
e543c9
+}
e543c9
+
e543c9
+static void assert_nondefault_int_notset(struct dp_option *opts)
e543c9
+{
e543c9
+    int i;
e543c9
     i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
e543c9
     assert_int_equal(i, 0);
e543c9
+}
e543c9
 
e543c9
+static void set_nondefault_int(struct dp_option *opts)
e543c9
+{
e543c9
+    int ret;
e543c9
     ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
e543c9
     assert_int_equal(ret, EOK);
e543c9
+}
e543c9
 
e543c9
+static void assert_nondefault_int_set(struct dp_option *opts)
e543c9
+{
e543c9
+    int i;
e543c9
     i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
e543c9
     assert_int_equal(i, 456);
e543c9
 }
e543c9
 
e543c9
+void opt_test_getset_int(void **state)
e543c9
+{
e543c9
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
e543c9
+
e543c9
+    assert_nondefault_int_notset(opts);
e543c9
+    set_nondefault_int(opts);
e543c9
+    assert_nondefault_int_set(opts);
e543c9
+}
e543c9
+
e543c9
 void opt_test_getset_bool(void **state)
e543c9
 {
e543c9
     struct dp_option *opts = talloc_get_type(*state, struct dp_option);
e543c9
@@ -354,6 +403,65 @@ void opt_test_getset_bool(void **state)
e543c9
     assert_false(b == true);
e543c9
 }
e543c9
 
e543c9
+void opt_test_inherit(void **state)
e543c9
+{
e543c9
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
e543c9
+    int ret;
e543c9
+    struct dp_option *opts_copy;
e543c9
+    const char *s;
e543c9
+    const char *sd_inherit_match[] = { "string_nodefault",
e543c9
+                                       "blob_nodefault",
e543c9
+                                       "int_nodefault",
e543c9
+                                       "bool_true",
e543c9
+                                       NULL };
e543c9
+
e543c9
+    ret = dp_copy_defaults(opts, test_def_opts,
e543c9
+                           OPT_NUM_OPTS, &opts_copy);
e543c9
+    assert_int_equal(ret, EOK);
e543c9
+    assert_defaults(opts);
e543c9
+
e543c9
+    dp_option_inherit(NULL, OPT_STRING_NODEFAULT,
e543c9
+                      opts, opts_copy);
e543c9
+    s = dp_opt_get_string(opts_copy, OPT_STRING_NODEFAULT);
e543c9
+    assert_null(s);
e543c9
+
e543c9
+    /* string */
e543c9
+    assert_nondefault_string_empty(opts_copy);
e543c9
+    set_nondefault_string(opts);
e543c9
+    dp_option_inherit(discard_const(sd_inherit_match),
e543c9
+                      OPT_STRING_NODEFAULT,
e543c9
+                      opts, opts_copy);
e543c9
+    check_nondefault_string(opts_copy);
e543c9
+
e543c9
+    /* blob */
e543c9
+    assert_nondefault_blob_empty(opts_copy);
e543c9
+    set_nondefault_blob(opts);
e543c9
+    dp_option_inherit(discard_const(sd_inherit_match),
e543c9
+                      OPT_BLOB_NODEFAULT,
e543c9
+                      opts, opts_copy);
e543c9
+    check_nondefault_blob(opts_copy);
e543c9
+
e543c9
+    /* number */
e543c9
+    assert_nondefault_int_notset(opts_copy);
e543c9
+    set_nondefault_int(opts);
e543c9
+    dp_option_inherit(discard_const(sd_inherit_match),
e543c9
+                      OPT_INT_NODEFAULT,
e543c9
+                      opts, opts_copy);
e543c9
+    assert_nondefault_int_set(opts_copy);
e543c9
+
e543c9
+    /* bool */
e543c9
+    assert_true(dp_opt_get_bool(opts_copy, OPT_BOOL_TRUE));
e543c9
+
e543c9
+    ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
e543c9
+    assert_int_equal(ret, EOK);
e543c9
+
e543c9
+    dp_option_inherit(discard_const(sd_inherit_match),
e543c9
+                      OPT_BOOL_TRUE,
e543c9
+                      opts, opts_copy);
e543c9
+
e543c9
+    assert_false(dp_opt_get_bool(opts_copy, OPT_BOOL_TRUE));
e543c9
+}
e543c9
+
e543c9
 int main(int argc, const char *argv[])
e543c9
 {
e543c9
     int no_cleanup = 0;
e543c9
@@ -380,6 +488,9 @@ int main(int argc, const char *argv[])
e543c9
         unit_test_setup_teardown(opt_test_getset_blob,
e543c9
                                  opt_test_getset_setup,
e543c9
                                  opt_test_getset_teardown),
e543c9
+        unit_test_setup_teardown(opt_test_inherit,
e543c9
+                                 opt_test_getset_setup,
e543c9
+                                 opt_test_getset_teardown),
e543c9
         unit_test(opt_test_copy_default),
e543c9
         unit_test(opt_test_copy_options),
e543c9
         unit_test(opt_test_get)
e543c9
-- 
e543c9
2.1.0
e543c9