Line data Source code
1 : /*
2 : SSSD
3 :
4 : sysdb_utils - Tests for various sysdb calls
5 :
6 : Authors:
7 : Sumit Bose <sbose@redhat.com>
8 :
9 : Copyright (C) 2015 Red Hat
10 :
11 : This program is free software; you can redistribute it and/or modify
12 : it under the terms of the GNU General Public License as published by
13 : the Free Software Foundation; either version 3 of the License, or
14 : (at your option) any later version.
15 :
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 :
21 : You should have received a copy of the GNU General Public License
22 : along with this program. If not, see <http://www.gnu.org/licenses/>.
23 : */
24 :
25 : #include <stdarg.h>
26 : #include <stddef.h>
27 : #include <setjmp.h>
28 : #include <cmocka.h>
29 : #include <popt.h>
30 :
31 : #include "tests/cmocka/common_mock.h"
32 :
33 : #define IPA_UUID "bcae7c40-97eb-11e4-88ca-525400e96a6b"
34 :
35 : #define AD_GUID_BIN {0x8d, 0x0d, 0xa8, 0xfe, 0xd5, 0xdb, 0x84, 0x4f, \
36 : 0x85, 0x74, 0x7d, 0xb0, 0x47, 0x7f, 0x96, 0x2e};
37 : #define AD_GUID "fea80d8d-dbd5-4f84-8574-7db0477f962e"
38 1 : static void test_sysdb_handle_original_uuid(void **state)
39 : {
40 : int ret;
41 : struct sysdb_attrs *src_attrs;
42 : struct sysdb_attrs *dest_attrs;
43 : const char *guid;
44 1 : uint8_t bin_guid[] = AD_GUID_BIN;
45 1 : struct ldb_val guid_val = {bin_guid, 16};
46 :
47 1 : ret = sysdb_handle_original_uuid(NULL, NULL, NULL, NULL, NULL);
48 1 : assert_int_equal(ret, ENOENT);
49 :
50 1 : src_attrs = sysdb_new_attrs(NULL);
51 1 : assert_non_null(src_attrs);
52 :
53 1 : dest_attrs = sysdb_new_attrs(NULL);
54 1 : assert_non_null(dest_attrs);
55 :
56 1 : ret = sysdb_handle_original_uuid("xyz", src_attrs, "abc", dest_attrs,
57 : "def");
58 1 : assert_int_equal(ret, ENOENT);
59 :
60 1 : ret = sysdb_attrs_add_val(src_attrs, "GUID", &guid_val);
61 1 : assert_int_equal(ret, EOK);
62 :
63 1 : ret = sysdb_attrs_add_string(src_attrs, "UUID", IPA_UUID);
64 1 : assert_int_equal(ret, EOK);
65 :
66 1 : ret = sysdb_handle_original_uuid(NULL, src_attrs, "GUID",
67 : dest_attrs, "def");
68 1 : assert_int_equal(ret, ENOENT);
69 :
70 1 : ret = sysdb_handle_original_uuid("objectGUID", NULL, "GUID",
71 : dest_attrs, "def");
72 1 : assert_int_equal(ret, EINVAL);
73 :
74 1 : ret = sysdb_handle_original_uuid("objectGUID", src_attrs, "GUID",
75 : dest_attrs, "def");
76 1 : assert_int_equal(ret, EOK);
77 1 : ret = sysdb_attrs_get_string(dest_attrs, "def", &guid);
78 1 : assert_int_equal(ret, EOK);
79 1 : assert_string_equal(guid, AD_GUID);
80 :
81 1 : ret = sysdb_handle_original_uuid("ipaUniqueID", src_attrs, "UUID",
82 : dest_attrs, "ghi");
83 1 : assert_int_equal(ret, EOK);
84 1 : ret = sysdb_attrs_get_string(dest_attrs, "ghi", &guid);
85 1 : assert_int_equal(ret, EOK);
86 1 : assert_string_equal(guid, IPA_UUID);
87 :
88 1 : talloc_free(src_attrs);
89 1 : src_attrs = sysdb_new_attrs(NULL);
90 1 : assert_non_null(src_attrs);
91 :
92 : /* check objectGUID with length other than 16 */
93 1 : ret = sysdb_attrs_add_string(src_attrs, "GUID", IPA_UUID);
94 1 : assert_int_equal(ret, EOK);
95 1 : ret = sysdb_handle_original_uuid("objectGUID", src_attrs, "GUID",
96 : dest_attrs, "jkl");
97 1 : assert_int_equal(ret, EOK);
98 1 : ret = sysdb_attrs_get_string(dest_attrs, "jkl", &guid);
99 1 : assert_int_equal(ret, EOK);
100 1 : assert_string_equal(guid, IPA_UUID);
101 :
102 1 : talloc_free(src_attrs);
103 1 : talloc_free(dest_attrs);
104 1 : }
105 :
106 1 : int main(int argc, const char *argv[])
107 : {
108 : int rv;
109 : poptContext pc;
110 : int opt;
111 6 : struct poptOption long_options[] = {
112 : POPT_AUTOHELP
113 5 : SSSD_DEBUG_OPTS
114 : POPT_TABLEEND
115 : };
116 :
117 1 : const struct CMUnitTest tests[] = {
118 : cmocka_unit_test(test_sysdb_handle_original_uuid),
119 : };
120 :
121 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
122 1 : debug_level = SSSDBG_INVALID;
123 :
124 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
125 1 : while((opt = poptGetNextOpt(pc)) != -1) {
126 : switch(opt) {
127 : default:
128 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
129 : poptBadOption(pc, 0), poptStrerror(opt));
130 0 : poptPrintUsage(pc, stderr, 0);
131 0 : return 1;
132 : }
133 : }
134 1 : poptFreeContext(pc);
135 :
136 1 : DEBUG_CLI_INIT(debug_level);
137 :
138 1 : tests_set_cwd();
139 1 : rv = cmocka_run_group_tests(tests, NULL, NULL);
140 :
141 1 : return rv;
142 : }
|