1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose/*
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose SSSD
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose sysdb_utils - Tests for various sysdb calls
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose Authors:
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose Sumit Bose <sbose@redhat.com>
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose Copyright (C) 2015 Red Hat
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose This program is free software; you can redistribute it and/or modify
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose it under the terms of the GNU General Public License as published by
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose the Free Software Foundation; either version 3 of the License, or
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose (at your option) any later version.
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose This program is distributed in the hope that it will be useful,
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose GNU General Public License for more details.
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose You should have received a copy of the GNU General Public License
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose*/
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#include <stdarg.h>
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#include <stddef.h>
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#include <setjmp.h>
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#include <cmocka.h>
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#include <popt.h>
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#include "tests/cmocka/common_mock.h"
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#define IPA_UUID "bcae7c40-97eb-11e4-88ca-525400e96a6b"
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#define AD_GUID_BIN {0x8d, 0x0d, 0xa8, 0xfe, 0xd5, 0xdb, 0x84, 0x4f, \
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose 0x85, 0x74, 0x7d, 0xb0, 0x47, 0x7f, 0x96, 0x2e};
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose#define AD_GUID "fea80d8d-dbd5-4f84-8574-7db0477f962e"
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bosestatic void test_sysdb_handle_original_uuid(void **state)
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose{
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose int ret;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose struct sysdb_attrs *src_attrs;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose struct sysdb_attrs *dest_attrs;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose const char *guid;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose uint8_t bin_guid[] = AD_GUID_BIN;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose struct ldb_val guid_val = {bin_guid, 16};
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_handle_original_uuid(NULL, NULL, NULL, NULL, NULL);
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek assert_int_equal(ret, ENOENT);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose src_attrs = sysdb_new_attrs(NULL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_non_null(src_attrs);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose dest_attrs = sysdb_new_attrs(NULL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_non_null(dest_attrs);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_handle_original_uuid("xyz", src_attrs, "abc", dest_attrs,
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose "def");
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, ENOENT);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_attrs_add_val(src_attrs, "GUID", &guid_val);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_attrs_add_string(src_attrs, "UUID", IPA_UUID);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek ret = sysdb_handle_original_uuid(NULL, src_attrs, "GUID",
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek dest_attrs, "def");
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek assert_int_equal(ret, ENOENT);
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek ret = sysdb_handle_original_uuid("objectGUID", NULL, "GUID",
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek dest_attrs, "def");
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek assert_int_equal(ret, EINVAL);
e7e61c777e13e0bb07d29b5d1b53e21ca199bf0fJakub Hrozek
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_handle_original_uuid("objectGUID", src_attrs, "GUID",
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose dest_attrs, "def");
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_attrs_get_string(dest_attrs, "def", &guid);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_string_equal(guid, AD_GUID);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_handle_original_uuid("ipaUniqueID", src_attrs, "UUID",
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose dest_attrs, "ghi");
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_attrs_get_string(dest_attrs, "ghi", &guid);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_string_equal(guid, IPA_UUID);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose talloc_free(src_attrs);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose src_attrs = sysdb_new_attrs(NULL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_non_null(src_attrs);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose /* check objectGUID with length other than 16 */
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_attrs_add_string(src_attrs, "GUID", IPA_UUID);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_handle_original_uuid("objectGUID", src_attrs, "GUID",
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose dest_attrs, "jkl");
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose ret = sysdb_attrs_get_string(dest_attrs, "jkl", &guid);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_int_equal(ret, EOK);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose assert_string_equal(guid, IPA_UUID);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose talloc_free(src_attrs);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose talloc_free(dest_attrs);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose}
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose#define TEST_BASE64_ABC "YWJj"
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose#define TEST_BASE64_123 "AQID"
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bosestatic void test_sysdb_attrs_add_base64_blob(void **state)
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose{
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose struct sysdb_attrs *attrs;
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose struct ldb_message_element *el;
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose char zero[] = { '\1', '\2', '\3' };
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose int ret;
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose attrs = sysdb_new_attrs(NULL);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_non_null(attrs);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose ret = sysdb_attrs_add_base64_blob(attrs, "testAttrABC", TEST_BASE64_ABC);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(ret, EOK);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose ret = sysdb_attrs_add_base64_blob(attrs, "testAttr000", TEST_BASE64_123);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(ret, EOK);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose ret = sysdb_attrs_get_el(attrs, "testAttrABC", &el);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(ret, EOK);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(el->num_values, 1);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_non_null(el->values);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_non_null(el->values[0].data);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(el->values[0].length, 3);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_memory_equal(el->values[0].data, "abc", 3);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose ret = sysdb_attrs_get_el(attrs, "testAttr000", &el);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(ret, EOK);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(el->num_values, 1);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_non_null(el->values);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_non_null(el->values[0].data);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_int_equal(el->values[0].length, 3);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose assert_memory_equal(el->values[0].data, zero, 3);
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose}
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Boseint main(int argc, const char *argv[])
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose{
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose int rv;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose poptContext pc;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose int opt;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose struct poptOption long_options[] = {
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose POPT_AUTOHELP
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose SSSD_DEBUG_OPTS
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose POPT_TABLEEND
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose };
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
e2405de14e2fb3d58af4108ecc2726818e6c7c71Jakub Hrozek const struct CMUnitTest tests[] = {
e2405de14e2fb3d58af4108ecc2726818e6c7c71Jakub Hrozek cmocka_unit_test(test_sysdb_handle_original_uuid),
6cdeb0923c16e3fafe21aaadca6dac1d71474c31Sumit Bose cmocka_unit_test(test_sysdb_attrs_add_base64_blob),
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose };
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
57c5ea8825c7179fd93382dbcbb07e828e5aec19René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose debug_level = SSSDBG_INVALID;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose pc = poptGetContext(argv[0], argc, argv, long_options, 0);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose while((opt = poptGetNextOpt(pc)) != -1) {
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose switch(opt) {
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose default:
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose fprintf(stderr, "\nInvalid option %s: %s\n\n",
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose poptBadOption(pc, 0), poptStrerror(opt));
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose poptPrintUsage(pc, stderr, 0);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose return 1;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose }
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose }
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose poptFreeContext(pc);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose DEBUG_CLI_INIT(debug_level);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose tests_set_cwd();
e2405de14e2fb3d58af4108ecc2726818e6c7c71Jakub Hrozek rv = cmocka_run_group_tests(tests, NULL, NULL);
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose return rv;
1d93029624d708119bbf803e6647a2cbb271f001Sumit Bose}