90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek Copyright (C) 2014 Red Hat
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek SSSD tests: Data Provider Option Tests
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek This program is free software; you can redistribute it and/or modify
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek it under the terms of the GNU General Public License as published by
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek (at your option) any later version.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek This program is distributed in the hope that it will be useful,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek GNU General Public License for more details.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek You should have received a copy of the GNU General Public License
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "string_nodefault", DP_OPT_STRING, NULL_STRING, NULL_STRING },
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "string_default", DP_OPT_STRING, { STRING_DEFAULT }, NULL_STRING},
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "blob_nodefault", DP_OPT_BLOB, NULL_BLOB, NULL_BLOB },
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "int_nodefault", DP_OPT_NUMBER, NULL_NUMBER, NULL_NUMBER },
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "int_default", DP_OPT_NUMBER, { .number = INT_DEFAULT }, NULL_NUMBER },
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "bool_true", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE },
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek { "bool_false", DP_OPT_BOOL, BOOL_FALSE, BOOL_FALSE },
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozekstatic void assert_defaults(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_int_equal(b.length, strlen(BLOB_DEFAULT));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_memory_equal(b.data, BLOB_DEFAULT, strlen(BLOB_DEFAULT));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_copy_defaults(mem_ctx, test_def_opts, OPT_NUM_OPTS, &opts);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* Test that copy_defaults would still copy defaults even if we
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * change the values
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_string(opts, OPT_STRING_DEFAULT, "str2");
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_blob(opts, OPT_BLOB_DEFAULT, b);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_int(opts, OPT_INT_DEFAULT, 789);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_bool(opts, OPT_BOOL_FALSE, true);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_copy_defaults(mem_ctx, test_def_opts, OPT_NUM_OPTS, &opts);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_copy_options(mem_ctx, test_def_opts, OPT_NUM_OPTS, &opts);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* Test that options set to an explicit value retain
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * the value and even options with default value
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * do not return the default unless explicitly set
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_memory_equal(b.data, "blob1", strlen("blob1"));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek tctx = create_dom_test_ctx(global_talloc_context, TESTS_PATH, TEST_CONF_DB,
919cfb623e9b725eaa654c6e1b513d1305057314Jakub Hrozek ret = dp_get_options(global_talloc_context, tctx->confdb, tctx->conf_dom_path,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* Options that were not specified explicitly should only have the default
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * value, those that have been specified explicitly should carry that
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_DEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_int_equal(b.length, strlen("blobval2"));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_memory_equal(b.data, "blobval2", strlen("blobval2"));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_int_equal(b.length, strlen(BLOB_DEFAULT));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_memory_equal(b.data, BLOB_DEFAULT, strlen(BLOB_DEFAULT));
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozekstatic int opt_test_getset_teardown(void **state)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek struct dp_option *opts = talloc_get_type(*state, struct dp_option);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void assert_nondefault_string_empty(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void set_nondefault_string(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_string(opts, OPT_STRING_NODEFAULT, "str1");
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void check_nondefault_string(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek s = dp_opt_get_string(opts, OPT_STRING_NODEFAULT);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek struct dp_option *opts = talloc_get_type(*state, struct dp_option);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void assert_nondefault_blob_empty(struct dp_option *opts)
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void set_nondefault_blob(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_blob(opts, OPT_BLOB_NODEFAULT, b);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void check_nondefault_blob(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek assert_memory_equal(b.data, "blob2", strlen("blob2"));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek struct dp_option *opts = talloc_get_type(*state, struct dp_option);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void assert_nondefault_int_notset(struct dp_option *opts)
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void set_nondefault_int(struct dp_option *opts)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_int(opts, OPT_INT_NODEFAULT, 456);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekstatic void assert_nondefault_int_set(struct dp_option *opts)
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek struct dp_option *opts = talloc_get_type(*state, struct dp_option);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek struct dp_option *opts = talloc_get_type(*state, struct dp_option);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek struct dp_option *opts = talloc_get_type(*state, struct dp_option);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek const char *s;
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek const char *sd_inherit_match[] = { "string_nodefault",
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek "blob_nodefault",
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek "int_nodefault",
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek "bool_true",
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek s = dp_opt_get_string(opts_copy, OPT_STRING_NODEFAULT);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek /* string */
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek dp_option_inherit(discard_const(sd_inherit_match),
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek dp_option_inherit(discard_const(sd_inherit_match),
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek /* number */
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek dp_option_inherit(discard_const(sd_inherit_match),
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek assert_true(dp_opt_get_bool(opts_copy, OPT_BOOL_TRUE));
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek ret = dp_opt_set_bool(opts, OPT_BOOL_TRUE, false);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek dp_option_inherit(discard_const(sd_inherit_match),
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek assert_false(dp_opt_get_bool(opts_copy, OPT_BOOL_TRUE));
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek _("Do not delete the test database after a test run"), NULL },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(opt_test_getset_string,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(opt_test_getset_int,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(opt_test_getset_bool,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cmocka_unit_test_setup_teardown(opt_test_getset_blob,
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek cmocka_unit_test_setup_teardown(opt_test_inherit,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* Set debug level to invalid value so we can deside if -d 0 was used. */
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* Even though normally the tests should clean up after themselves
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * they might not after a failed run. Remove the old db to be sure */
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek ret = cmocka_run_group_tests(tests, NULL, NULL);