Blame SOURCES/0097-DP-Provide-separate-dp_copy_defaults-function.patch

2fc102
From cb5090d6da0e0b378b095b151af70fa21cd62e9e Mon Sep 17 00:00:00 2001
2fc102
From: Jakub Hrozek <jhrozek@redhat.com>
2fc102
Date: Mon, 24 Feb 2014 15:42:51 +0100
2fc102
Subject: [PATCH 97/97] DP: Provide separate dp_copy_defaults function
2fc102
MIME-Version: 1.0
2fc102
Content-Type: text/plain; charset=UTF-8
2fc102
Content-Transfer-Encoding: 8bit
2fc102
2fc102
https://fedorahosted.org/sssd/ticket/2257
2fc102
2fc102
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
2fc102
(cherry picked from commit 90afedb00608547ae1f32aa7aafd552c4b306909)
2fc102
---
2fc102
 Makefile.am                        |  12 ++
2fc102
 src/providers/ad/ad_common.c       |  16 +-
2fc102
 src/providers/data_provider.h      |   5 +
2fc102
 src/providers/data_provider_opts.c |  42 ++--
2fc102
 src/tests/cmocka/test_dp_opts.c    | 421 +++++++++++++++++++++++++++++++++++++
2fc102
 src/tests/ipa_ldap_opt-tests.c     |   2 +-
2fc102
 6 files changed, 476 insertions(+), 22 deletions(-)
2fc102
 create mode 100644 src/tests/cmocka/test_dp_opts.c
2fc102
2fc102
diff --git a/Makefile.am b/Makefile.am
2fc102
index 2e1a1e6bacfb79e4ef7068a22a64c21d23858cb9..9025ec6a5bfa16408278506fdd573666b4b6dbe5 100644
2fc102
--- a/Makefile.am
2fc102
+++ b/Makefile.am
2fc102
@@ -154,6 +154,7 @@ if HAVE_CMOCKA
2fc102
         test_utils \
2fc102
         ad_access_filter_tests \
2fc102
         ad_common_tests \
2fc102
+        dp_opt_tests \
2fc102
         test_search_bases
2fc102
 endif
2fc102
 
2fc102
@@ -1459,6 +1460,17 @@ ad_common_tests_LDADD = \
2fc102
     libsss_krb5_common.la \
2fc102
     libsss_test_common.la
2fc102
 
2fc102
+dp_opt_tests_SOURCES = \
2fc102
+    src/providers/data_provider_opts.c \
2fc102
+    src/tests/cmocka/test_dp_opts.c
2fc102
+dp_opt_tests_CFLAGS = \
2fc102
+    $(AM_CFLAGS)
2fc102
+dp_opt_tests_LDADD = \
2fc102
+    $(CMOCKA_LIBS) \
2fc102
+    $(TALLOC_LIBS) \
2fc102
+    $(SSSD_INTERNAL_LTLIBS) \
2fc102
+    libsss_test_common.la
2fc102
+
2fc102
 endif
2fc102
 
2fc102
 noinst_PROGRAMS = pam_test_client
2fc102
diff --git a/src/providers/ad/ad_common.c b/src/providers/ad/ad_common.c
2fc102
index 99fa4c07af2a79bb3ca195214ddb0dbd60c61620..605de49f7f7ae910cbc78e38f888600ba78a0c4f 100644
2fc102
--- a/src/providers/ad/ad_common.c
2fc102
+++ b/src/providers/ad/ad_common.c
2fc102
@@ -44,10 +44,10 @@ ad_create_default_sdap_options(TALLOC_CTX *mem_ctx)
2fc102
         return NULL;
2fc102
     }
2fc102
 
2fc102
-    ret = dp_copy_options(id_opts,
2fc102
-                          ad_def_ldap_opts,
2fc102
-                          SDAP_OPTS_BASIC,
2fc102
-                          &id_opts->basic);
2fc102
+    ret = dp_copy_defaults(id_opts,
2fc102
+                           ad_def_ldap_opts,
2fc102
+                           SDAP_OPTS_BASIC,
2fc102
+                           &id_opts->basic);
2fc102
     if (ret != EOK) {
2fc102
         goto fail;
2fc102
     }
2fc102
@@ -117,10 +117,10 @@ ad_create_default_options(TALLOC_CTX *mem_ctx,
2fc102
     ad_options = talloc_zero(mem_ctx, struct ad_options);
2fc102
     if (ad_options == NULL) return NULL;
2fc102
 
2fc102
-    ret = dp_copy_options(ad_options,
2fc102
-                          ad_basic_opts,
2fc102
-                          AD_OPTS_BASIC,
2fc102
-                          &ad_options->basic);
2fc102
+    ret = dp_copy_defaults(ad_options,
2fc102
+                           ad_basic_opts,
2fc102
+                           AD_OPTS_BASIC,
2fc102
+                           &ad_options->basic);
2fc102
     if (ret != EOK) {
2fc102
         talloc_free(ad_options);
2fc102
         return NULL;
2fc102
diff --git a/src/providers/data_provider.h b/src/providers/data_provider.h
2fc102
index d086d5d2f368578c6a44a2c3b33738c894feba95..d86ff58e65c7ae35f7269fdd10ed78f529ed8d8c 100644
2fc102
--- a/src/providers/data_provider.h
2fc102
+++ b/src/providers/data_provider.h
2fc102
@@ -295,6 +295,11 @@ int dp_copy_options(TALLOC_CTX *memctx,
2fc102
                     int num_opts,
2fc102
                     struct dp_option **_opts);
2fc102
 
2fc102
+int dp_copy_defaults(TALLOC_CTX *memctx,
2fc102
+                     struct dp_option *src_opts,
2fc102
+                     int num_opts,
2fc102
+                     struct dp_option **_opts);
2fc102
+
2fc102
 const char *_dp_opt_get_cstring(struct dp_option *opts,
2fc102
                                     int id, const char *location);
2fc102
 char *_dp_opt_get_string(struct dp_option *opts,
2fc102
diff --git a/src/providers/data_provider_opts.c b/src/providers/data_provider_opts.c
2fc102
index 5a2e3b74da7d4af4326a56a9cd47b7826e4b78e3..0cc48e46e8bbba487aea15b4ad1044e8ddc0482a 100644
2fc102
--- a/src/providers/data_provider_opts.c
2fc102
+++ b/src/providers/data_provider_opts.c
2fc102
@@ -131,11 +131,11 @@ done:
2fc102
 }
2fc102
 
2fc102
 /* =Basic-Option-Helpers================================================== */
2fc102
-
2fc102
-int dp_copy_options(TALLOC_CTX *memctx,
2fc102
-                    struct dp_option *src_opts,
2fc102
-                    int num_opts,
2fc102
-                    struct dp_option **_opts)
2fc102
+static int dp_copy_options_ex(TALLOC_CTX *memctx,
2fc102
+                              bool copy_values,
2fc102
+                              struct dp_option *src_opts,
2fc102
+                              int num_opts,
2fc102
+                              struct dp_option **_opts)
2fc102
 {
2fc102
     struct dp_option *opts;
2fc102
     int i, ret = EOK;
2fc102
@@ -151,9 +151,9 @@ int dp_copy_options(TALLOC_CTX *memctx,
2fc102
 
2fc102
         switch (src_opts[i].type) {
2fc102
         case DP_OPT_STRING:
2fc102
-            if (src_opts[i].val.string) {
2fc102
+            if (copy_values) {
2fc102
                 ret = dp_opt_set_string(opts, i, src_opts[i].val.string);
2fc102
-            } else if (src_opts[i].def_val.string) {
2fc102
+            } else {
2fc102
                 ret = dp_opt_set_string(opts, i, src_opts[i].def_val.string);
2fc102
             }
2fc102
             if (ret != EOK) {
2fc102
@@ -169,9 +169,9 @@ int dp_copy_options(TALLOC_CTX *memctx,
2fc102
             break;
2fc102
 
2fc102
         case DP_OPT_BLOB:
2fc102
-            if (src_opts[i].val.blob.data) {
2fc102
+            if (copy_values) {
2fc102
                 ret = dp_opt_set_blob(opts, i, src_opts[i].val.blob);
2fc102
-            } else if (src_opts[i].def_val.blob.data) {
2fc102
+            } else {
2fc102
                 ret = dp_opt_set_blob(opts, i, src_opts[i].def_val.blob);
2fc102
             }
2fc102
             if (ret != EOK) {
2fc102
@@ -185,9 +185,9 @@ int dp_copy_options(TALLOC_CTX *memctx,
2fc102
             break;
2fc102
 
2fc102
         case DP_OPT_NUMBER:
2fc102
-            if (src_opts[i].val.number) {
2fc102
+            if (copy_values) {
2fc102
                 ret = dp_opt_set_int(opts, i, src_opts[i].val.number);
2fc102
-            } else if (src_opts[i].def_val.number) {
2fc102
+            } else {
2fc102
                 ret = dp_opt_set_int(opts, i, src_opts[i].def_val.number);
2fc102
             }
2fc102
             if (ret != EOK) {
2fc102
@@ -201,9 +201,9 @@ int dp_copy_options(TALLOC_CTX *memctx,
2fc102
             break;
2fc102
 
2fc102
         case DP_OPT_BOOL:
2fc102
-            if (src_opts[i].val.boolean) {
2fc102
+            if (copy_values) {
2fc102
                 ret = dp_opt_set_bool(opts, i, src_opts[i].val.boolean);
2fc102
-            } else if (src_opts[i].def_val.boolean) {
2fc102
+            } else {
2fc102
                 ret = dp_opt_set_bool(opts, i, src_opts[i].def_val.boolean);
2fc102
             }
2fc102
             if (ret != EOK) {
2fc102
@@ -225,6 +225,22 @@ done:
2fc102
     return ret;
2fc102
 }
2fc102
 
2fc102
+int dp_copy_options(TALLOC_CTX *memctx,
2fc102
+                    struct dp_option *src_opts,
2fc102
+                    int num_opts,
2fc102
+                    struct dp_option **_opts)
2fc102
+{
2fc102
+    return dp_copy_options_ex(memctx, true, src_opts, num_opts, _opts);
2fc102
+}
2fc102
+
2fc102
+int dp_copy_defaults(TALLOC_CTX *memctx,
2fc102
+                     struct dp_option *src_opts,
2fc102
+                     int num_opts,
2fc102
+                     struct dp_option **_opts)
2fc102
+{
2fc102
+    return dp_copy_options_ex(memctx, false, src_opts, num_opts, _opts);
2fc102
+}
2fc102
+
2fc102
 static const char *dp_opt_type_to_string(enum dp_opt_type type)
2fc102
 {
2fc102
     switch (type) {
2fc102
diff --git a/src/tests/cmocka/test_dp_opts.c b/src/tests/cmocka/test_dp_opts.c
2fc102
new file mode 100644
2fc102
index 0000000000000000000000000000000000000000..07998b4034fb33195c99340e5544596925ecf145
2fc102
--- /dev/null
2fc102
+++ b/src/tests/cmocka/test_dp_opts.c
2fc102
@@ -0,0 +1,421 @@
2fc102
+/*
2fc102
+    Authors:
2fc102
+        Jakub Hrozek <jhrozek@redhat.com>
2fc102
+
2fc102
+    Copyright (C) 2014 Red Hat
2fc102
+
2fc102
+    SSSD tests: Data Provider Option Tests
2fc102
+
2fc102
+    This program is free software; you can redistribute it and/or modify
2fc102
+    it under the terms of the GNU General Public License as published by
2fc102
+    the Free Software Foundation; either version 3 of the License, or
2fc102
+    (at your option) any later version.
2fc102
+
2fc102
+    This program is distributed in the hope that it will be useful,
2fc102
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
2fc102
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2fc102
+    GNU General Public License for more details.
2fc102
+
2fc102
+    You should have received a copy of the GNU General Public License
2fc102
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
2fc102
+*/
2fc102
+
2fc102
+#include <popt.h>
2fc102
+
2fc102
+#include "providers/data_provider.h"
2fc102
+
2fc102
+#include "tests/cmocka/common_mock.h"
2fc102
+
2fc102
+#define STRING_DEFAULT  "stringval"
2fc102
+#define BLOB_DEFAULT    "blobval"
2fc102
+#define INT_DEFAULT     123
2fc102
+
2fc102
+#define TESTS_PATH "tests_opts"
2fc102
+#define TEST_CONF_DB "test_opt_conf.ldb"
2fc102
+#define TEST_SYSDB_FILE "cache_opt_test.ldb"
2fc102
+#define TEST_DOM_NAME "opt_test"
2fc102
+#define TEST_ID_PROVIDER "ldap"
2fc102
+
2fc102
+enum test_opts {
2fc102
+    OPT_STRING_NODEFAULT,
2fc102
+    OPT_STRING_DEFAULT,
2fc102
+    OPT_BLOB_NODEFAULT,
2fc102
+    OPT_BLOB_DEFAULT,
2fc102
+    OPT_INT_NODEFAULT,
2fc102
+    OPT_INT_DEFAULT,
2fc102
+    OPT_BOOL_TRUE,
2fc102
+    OPT_BOOL_FALSE,
2fc102
+
2fc102
+    OPT_NUM_OPTS
2fc102
+};
2fc102
+
2fc102
+struct dp_option test_def_opts[] = {
2fc102
+    { "string_nodefault", DP_OPT_STRING, NULL_STRING, NULL_STRING },
2fc102
+    { "string_default", DP_OPT_STRING, { STRING_DEFAULT }, NULL_STRING},
2fc102
+    { "blob_nodefault", DP_OPT_BLOB, NULL_BLOB, NULL_BLOB },
2fc102
+    { "blob_default", DP_OPT_BLOB,
2fc102
+                      { .blob = { discard_const(BLOB_DEFAULT),
2fc102
+                                  sizeof(BLOB_DEFAULT) - 1 } },
2fc102
+                      NULL_BLOB },
2fc102
+    { "int_nodefault", DP_OPT_NUMBER, NULL_NUMBER, NULL_NUMBER },
2fc102
+    { "int_default", DP_OPT_NUMBER, { .number = INT_DEFAULT }, NULL_NUMBER },
2fc102
+    { "bool_true", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE },
2fc102
+    { "bool_false", DP_OPT_BOOL, BOOL_FALSE, BOOL_FALSE },
2fc102
+    DP_OPTION_TERMINATOR
2fc102
+};
2fc102
+
2fc102
+static void assert_defaults(struct dp_option *opts)
2fc102
+{
2fc102
+    char *s;
2fc102
+    struct dp_opt_blob b;
2fc102
+    int i;
2fc102
+    bool bo;
2fc102
+
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
2fc102
+    assert_null(s);
2fc102
+
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
2fc102
+    assert_non_null(s);
2fc102
+    assert_string_equal(s, STRING_DEFAULT);
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
2fc102
+    assert_null(b.data);
2fc102
+    assert_int_equal(b.length, 0);
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_DEFAULT);
2fc102
+    assert_non_null(b.data);
2fc102
+    assert_int_equal(b.length, strlen(BLOB_DEFAULT));
2fc102
+    assert_memory_equal(b.data, BLOB_DEFAULT, strlen(BLOB_DEFAULT));
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
2fc102
+    assert_int_equal(i, 0);
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_DEFAULT);
2fc102
+    assert_int_equal(i, INT_DEFAULT);
2fc102
+
2fc102
+    bo = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
2fc102
+    assert_true(bo == true);
2fc102
+
2fc102
+    bo = dp_opt_get_bool(opts, OPT_BOOL_FALSE);
2fc102
+    assert_true(bo == false);
2fc102
+}
2fc102
+
2fc102
+void opt_test_copy_default(void **state)
2fc102
+{
2fc102
+    int ret;
2fc102
+    TALLOC_CTX *mem_ctx;
2fc102
+    struct dp_option *opts;
2fc102
+    struct dp_opt_blob b;
2fc102
+
2fc102
+    mem_ctx = talloc_new(global_talloc_context);
2fc102
+    assert_non_null(mem_ctx);
2fc102
+
2fc102
+    ret = dp_copy_defaults(mem_ctx, test_def_opts, OPT_NUM_OPTS, &opts);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    assert_defaults(opts);
2fc102
+
2fc102
+    /* Test that copy_defaults would still copy defaults even if we
2fc102
+     * change the values
2fc102
+     */
2fc102
+    ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    ret = dp_opt_set_string(opts, OPT_STRING_DEFAULT, "str2");
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    b.data = discard_const_p(uint8_t, "blob1");
2fc102
+    b.length = strlen("blob1");
2fc102
+    ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    ret = dp_opt_set_blob(opts, OPT_BLOB_DEFAULT, b);
2fc102
+    b.data = discard_const_p(uint8_t, "blob2");
2fc102
+    b.length = strlen("blob2");
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    ret = dp_opt_set_int(opts, OPT_INT_DEFAULT, 789);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    ret = dp_opt_set_bool(opts, OPT_BOOL_FALSE, true);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    talloc_free(opts);
2fc102
+    ret = dp_copy_defaults(mem_ctx, test_def_opts, OPT_NUM_OPTS, &opts);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    assert_defaults(opts);
2fc102
+}
2fc102
+
2fc102
+void opt_test_copy_options(void **state)
2fc102
+{
2fc102
+    int ret;
2fc102
+    TALLOC_CTX *mem_ctx;
2fc102
+    struct dp_option *opts;
2fc102
+    char *s;
2fc102
+    struct dp_opt_blob b;
2fc102
+    int i;
2fc102
+    bool bo;
2fc102
+
2fc102
+    mem_ctx = talloc_new(global_talloc_context);
2fc102
+    assert_non_null(mem_ctx);
2fc102
+
2fc102
+    ret = dp_copy_options(mem_ctx, test_def_opts, OPT_NUM_OPTS, &opts);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    b.data = discard_const_p(uint8_t, "blob1");
2fc102
+    b.length = strlen("blob1");
2fc102
+    ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    /* Test that options set to an explicit value retain
2fc102
+     * the value and even options with default value
2fc102
+     * do not return the default unless explicitly set
2fc102
+     */
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
2fc102
+    assert_string_equal(s, "str1");
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
2fc102
+    assert_null(s);
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
2fc102
+    assert_non_null(b.data);
2fc102
+    assert_int_equal(b.length, strlen("blob1"));
2fc102
+    assert_memory_equal(b.data, "blob1", strlen("blob1"));
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_DEFAULT);
2fc102
+    assert_null(b.data);
2fc102
+    assert_int_equal(b.length, 0);
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
2fc102
+    assert_int_equal(i, 456);
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_DEFAULT);
2fc102
+    assert_int_equal(i, 0);
2fc102
+
2fc102
+    bo = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
2fc102
+    assert_false(bo == true);
2fc102
+}
2fc102
+
2fc102
+void opt_test_get(void **state)
2fc102
+{
2fc102
+    int ret;
2fc102
+    struct sss_test_ctx *tctx;
2fc102
+    struct dp_option *opts;
2fc102
+    char *dompath;
2fc102
+    struct sss_test_conf_param params[] = {
2fc102
+        { "string_nodefault", "stringval2" },
2fc102
+        { "blob_nodefault", "blobval2" },
2fc102
+        { "int_nodefault", "456" },
2fc102
+        { "bool_true", "false" },
2fc102
+        { NULL, NULL },             /* Sentinel */
2fc102
+    };
2fc102
+    char *s;
2fc102
+    struct dp_opt_blob b;
2fc102
+    int i;
2fc102
+    bool bo;
2fc102
+
2fc102
+    tctx = create_dom_test_ctx(global_talloc_context, TESTS_PATH, TEST_CONF_DB,
2fc102
+                               TEST_SYSDB_FILE, TEST_DOM_NAME,
2fc102
+                               TEST_ID_PROVIDER, params);
2fc102
+    assert_non_null(tctx);
2fc102
+
2fc102
+    dompath = talloc_asprintf(tctx, "config/domain/%s", TEST_DOM_NAME);
2fc102
+    assert_non_null(dompath);
2fc102
+
2fc102
+    ret = dp_get_options(global_talloc_context, tctx->confdb, dompath,
2fc102
+                         test_def_opts, OPT_NUM_OPTS, &opts);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    /* Options that were not specified explicitly should only have the default
2fc102
+     * value, those that have been specified explicitly should carry that
2fc102
+     * value
2fc102
+     */
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
2fc102
+    assert_non_null(s);
2fc102
+    assert_string_equal(s, "stringval2");
2fc102
+
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
2fc102
+    assert_non_null(s);
2fc102
+    assert_string_equal(s, STRING_DEFAULT);
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
2fc102
+    assert_non_null(b.data);
2fc102
+    assert_int_equal(b.length, strlen("blobval2"));
2fc102
+    assert_memory_equal(b.data, "blobval2", strlen("blobval2"));
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_DEFAULT);
2fc102
+    assert_non_null(b.data);
2fc102
+    assert_int_equal(b.length, strlen(BLOB_DEFAULT));
2fc102
+    assert_memory_equal(b.data, BLOB_DEFAULT, strlen(BLOB_DEFAULT));
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
2fc102
+    assert_int_equal(i, 456);
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_DEFAULT);
2fc102
+    assert_int_equal(i, INT_DEFAULT);
2fc102
+
2fc102
+    bo = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
2fc102
+    assert_true(bo == false);
2fc102
+
2fc102
+    bo = dp_opt_get_bool(opts, OPT_BOOL_FALSE);
2fc102
+    assert_true(bo == false);
2fc102
+}
2fc102
+
2fc102
+void opt_test_getset_setup(void **state)
2fc102
+{
2fc102
+    int ret;
2fc102
+    struct dp_option *opts;
2fc102
+
2fc102
+    ret = dp_copy_defaults(global_talloc_context,
2fc102
+                           test_def_opts, OPT_NUM_OPTS, &opts);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+    assert_defaults(opts);
2fc102
+
2fc102
+    *state = opts;
2fc102
+}
2fc102
+
2fc102
+void opt_test_getset_teardown(void **state)
2fc102
+{
2fc102
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
2fc102
+    talloc_free(opts);
2fc102
+}
2fc102
+
2fc102
+void opt_test_getset_string(void **state)
2fc102
+{
2fc102
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
2fc102
+    int ret;
2fc102
+    char *s;
2fc102
+
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
2fc102
+    assert_null(s);
2fc102
+
2fc102
+    ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
2fc102
+    assert_non_null(s);
2fc102
+    assert_string_equal(s, "str1");
2fc102
+}
2fc102
+
2fc102
+void opt_test_getset_blob(void **state)
2fc102
+{
2fc102
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
2fc102
+    int ret;
2fc102
+    struct dp_opt_blob b;
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
2fc102
+    assert_null(b.data);
2fc102
+    assert_int_equal(b.length, 0);
2fc102
+
2fc102
+    b.data = discard_const_p(uint8_t, "blob2");
2fc102
+    b.length = strlen("blob2");
2fc102
+    ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    b = dp_opt_get_blob(opts, OPT_BLOB_NODEFAULT);
2fc102
+    assert_non_null(b.data);
2fc102
+    assert_int_equal(b.length, strlen("blob2"));
2fc102
+    assert_memory_equal(b.data, "blob2", strlen("blob2"));
2fc102
+}
2fc102
+
2fc102
+void opt_test_getset_int(void **state)
2fc102
+{
2fc102
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
2fc102
+    int ret;
2fc102
+    int i;
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
2fc102
+    assert_int_equal(i, 0);
2fc102
+
2fc102
+    ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    i = dp_opt_get_int(opts, OPT_INT_NODEFAULT);
2fc102
+    assert_int_equal(i, 456);
2fc102
+}
2fc102
+
2fc102
+void opt_test_getset_bool(void **state)
2fc102
+{
2fc102
+    struct dp_option *opts = talloc_get_type(*state, struct dp_option);
2fc102
+    int ret;
2fc102
+    bool b;
2fc102
+
2fc102
+    b = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
2fc102
+    assert_true(b == true);
2fc102
+
2fc102
+    ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    b = dp_opt_get_bool(opts, OPT_BOOL_TRUE);
2fc102
+    assert_false(b == true);
2fc102
+}
2fc102
+
2fc102
+int main(int argc, const char *argv[])
2fc102
+{
2fc102
+    int no_cleanup = 0;
2fc102
+    poptContext pc;
2fc102
+    int opt;
2fc102
+    int ret;
2fc102
+    struct poptOption long_options[] = {
2fc102
+        POPT_AUTOHELP
2fc102
+        SSSD_DEBUG_OPTS
2fc102
+        {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
2fc102
+         _("Do not delete the test database after a test run"), NULL },
2fc102
+        POPT_TABLEEND
2fc102
+    };
2fc102
+    const UnitTest tests[] = {
2fc102
+        unit_test_setup_teardown(opt_test_getset_string,
2fc102
+                                 opt_test_getset_setup,
2fc102
+                                 opt_test_getset_teardown),
2fc102
+        unit_test_setup_teardown(opt_test_getset_int,
2fc102
+                                 opt_test_getset_setup,
2fc102
+                                 opt_test_getset_teardown),
2fc102
+        unit_test_setup_teardown(opt_test_getset_bool,
2fc102
+                                 opt_test_getset_setup,
2fc102
+                                 opt_test_getset_teardown),
2fc102
+        unit_test_setup_teardown(opt_test_getset_blob,
2fc102
+                                 opt_test_getset_setup,
2fc102
+                                 opt_test_getset_teardown),
2fc102
+        unit_test(opt_test_copy_default),
2fc102
+        unit_test(opt_test_copy_options),
2fc102
+        unit_test(opt_test_get)
2fc102
+    };
2fc102
+
2fc102
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
2fc102
+    debug_level = SSSDBG_INVALID;
2fc102
+
2fc102
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
2fc102
+    while((opt = poptGetNextOpt(pc)) != -1) {
2fc102
+        switch(opt) {
2fc102
+        default:
2fc102
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
2fc102
+                    poptBadOption(pc, 0), poptStrerror(opt));
2fc102
+            poptPrintUsage(pc, stderr, 0);
2fc102
+            return 1;
2fc102
+        }
2fc102
+    }
2fc102
+    poptFreeContext(pc);
2fc102
+
2fc102
+    DEBUG_INIT(debug_level);
2fc102
+
2fc102
+    /* Even though normally the tests should clean up after themselves
2fc102
+     * they might not after a failed run. Remove the old db to be sure */
2fc102
+    tests_set_cwd();
2fc102
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
2fc102
+    test_dom_suite_setup(TESTS_PATH);
2fc102
+
2fc102
+    ret = run_tests(tests);
2fc102
+    if (ret == 0 && !no_cleanup) {
2fc102
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
2fc102
+    }
2fc102
+    return ret;
2fc102
+}
2fc102
diff --git a/src/tests/ipa_ldap_opt-tests.c b/src/tests/ipa_ldap_opt-tests.c
2fc102
index 40afa5cba87d89bc6fa7345302991fe766b43314..25a094082dc369092f10ad823d98909027dd9a7e 100644
2fc102
--- a/src/tests/ipa_ldap_opt-tests.c
2fc102
+++ b/src/tests/ipa_ldap_opt-tests.c
2fc102
@@ -170,7 +170,7 @@ START_TEST(test_copy_opts)
2fc102
     tmp_ctx = talloc_new(NULL);
2fc102
     fail_unless(tmp_ctx != NULL, "talloc_new failed");
2fc102
 
2fc102
-    ret = dp_copy_options(tmp_ctx, ad_def_ldap_opts, SDAP_OPTS_BASIC, &opts);
2fc102
+    ret = dp_copy_defaults(tmp_ctx, ad_def_ldap_opts, SDAP_OPTS_BASIC, &opts);
2fc102
     fail_unless(ret == EOK, "[%s]", strerror(ret));
2fc102
 
2fc102
     for (int i=0; i < SDAP_OPTS_BASIC; i++) {
2fc102
-- 
2fc102
1.8.5.3
2fc102