>From 99c0793df036f8bc1226687d648ee6953447b30b Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Wed, 6 May 2015 08:06:53 +0200 Subject: [PATCH 2/5] 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. --- src/providers/data_provider.h | 5 ++ src/providers/data_provider_opts.c | 57 +++++++++++++ src/tests/cmocka/test_dp_opts.c | 163 +++++++++++++++++++++++++++++++------ 3 files changed, 201 insertions(+), 24 deletions(-) diff --git a/src/providers/data_provider.h b/src/providers/data_provider.h index 5df493e9d1ae21ada6f5fd6198a6d9c36680d044..4e741547c06cf8bb9da83871eaba963280c7484d 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(const struct sss_domain_info *parent_dom, + 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 8ad84560b7329fb1602c675affa4bd0bdf9ecc2e..7e503acacf739ccea01355283f9b82453f306bec 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(const struct sss_domain_info *parent_dom, + 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, + parent_dom->sd_inherit, 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 9192eaaca605526547a1f7be1830c9940cf9ccb4..b443e93e8bed338390bd0abf5f9a48bded84c73b 100644 --- a/src/tests/cmocka/test_dp_opts.c +++ b/src/tests/cmocka/test_dp_opts.c @@ -285,37 +285,63 @@ static int opt_test_getset_teardown(void **state) return 0; } +static void assert_nondefault_string_empty(struct dp_option *opts) +{ + 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_string(void **state) { 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); - - ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1"); - assert_int_equal(ret, EOK); - - s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT); - assert_non_null(s); - assert_string_equal(s, "str1"); + assert_nondefault_string_empty(opts); + set_nondefault_string(opts); + check_nondefault_string(opts); } -void opt_test_getset_blob(void **state) +static void assert_nondefault_blob_empty(struct dp_option *opts) { - struct dp_option *opts = talloc_get_type(*state, struct dp_option); - int ret; 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); @@ -323,20 +349,43 @@ void opt_test_getset_blob(void **state) assert_memory_equal(b.data, "blob2", strlen("blob2")); } +void opt_test_getset_blob(void **state) +{ + struct dp_option *opts = talloc_get_type(*state, struct dp_option); + + 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); - int ret; - int i; - i = dp_opt_get_int(opts, OPT_INT_NODEFAULT); - assert_int_equal(i, 0); - - ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456); - assert_int_equal(ret, EOK); - - i = dp_opt_get_int(opts, OPT_INT_NODEFAULT); - assert_int_equal(i, 456); + assert_nondefault_int_notset(opts); + set_nondefault_int(opts); + assert_nondefault_int_set(opts); } void opt_test_getset_bool(void **state) @@ -355,6 +404,69 @@ 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 sss_domain_info *parent_dom; + struct dp_option *opts_copy; + const char *s; + const char *sd_inherit_match[] = { "string_nodefault", + "blob_nodefault", + "int_nodefault", + "bool_true", + NULL }; + + parent_dom = talloc(opts, struct sss_domain_info); + assert_non_null(parent_dom); + + ret = dp_copy_defaults(parent_dom, test_def_opts, + OPT_NUM_OPTS, &opts_copy); + assert_int_equal(ret, EOK); + assert_defaults(opts); + + parent_dom->sd_inherit = NULL; + dp_option_inherit(parent_dom, OPT_STRING_NODEFAULT, + opts, opts_copy); + s = dp_opt_get_string(opts_copy, OPT_STRING_NODEFAULT); + assert_null(s); + + /* test inheritance */ + parent_dom->sd_inherit = discard_const(sd_inherit_match); + + /* string */ + assert_nondefault_string_empty(opts_copy); + set_nondefault_string(opts); + dp_option_inherit(parent_dom, 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(parent_dom, 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(parent_dom, 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(parent_dom, 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; @@ -381,6 +493,9 @@ int main(int argc, const char *argv[]) cmocka_unit_test_setup_teardown(opt_test_getset_blob, opt_test_getset_setup, opt_test_getset_teardown), + cmocka_unit_test_setup_teardown(opt_test_inherit, + opt_test_getset_setup, + opt_test_getset_teardown), cmocka_unit_test(opt_test_copy_default), cmocka_unit_test(opt_test_copy_options), cmocka_unit_test(opt_test_get) -- 2.1.0