Line data Source code
1 : /*
2 : Authors:
3 : Pavel Březina <pbrezina@redhat.com>
4 :
5 : Copyright (C) 2013 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "util/util.h"
22 : #include "db/sysdb.h"
23 : #include "tests/cmocka/common_mock.h"
24 : #include "tests/cmocka/common_mock_sysdb_objects.h"
25 :
26 : enum sysdb_attr_type {
27 : SYSDB_ATTR_TYPE_BOOL,
28 : SYSDB_ATTR_TYPE_LONG,
29 : SYSDB_ATTR_TYPE_UINT32,
30 : SYSDB_ATTR_TYPE_TIME,
31 : SYSDB_ATTR_TYPE_STRING
32 : };
33 :
34 : static enum sysdb_attr_type
35 23 : get_attr_type(const char *attr)
36 : {
37 : /* Most attributes in sysdb are strings. Since this is only for the purpose
38 : * of unit tests, we can safe ourselves some time and handle all attributes
39 : * that are not listed amongst other types as string instead of invalid
40 : * or unknown.
41 : */
42 :
43 : static const char *table_bool[] = {
44 : SYSDB_POSIX,
45 : NULL
46 : };
47 :
48 : static const char *table_long[] = {
49 : NULL
50 : };
51 :
52 : static const char *table_uint32[] = {
53 : SYSDB_UIDNUM, SYSDB_GIDNUM,
54 : NULL
55 : };
56 :
57 : static const char *table_time[] = {
58 : SYSDB_CACHE_EXPIRE,
59 : NULL
60 : };
61 :
62 : static const char **tables[SYSDB_ATTR_TYPE_STRING] = {
63 : table_bool, table_long, table_uint32, table_time
64 : };
65 :
66 : enum sysdb_attr_type type;
67 : int i;
68 :
69 69 : for (type = 0; type < SYSDB_ATTR_TYPE_STRING; type++) {
70 107 : for (i = 0; tables[type][i] != NULL; i++) {
71 61 : if (strcmp(attr, tables[type][i]) == 0) {
72 23 : return type;
73 : }
74 : }
75 : }
76 :
77 : /* we didn't find the attribute, consider it as string */
78 0 : return SYSDB_ATTR_TYPE_STRING;
79 : }
80 :
81 : static errno_t
82 23 : fill_attrs(struct sysdb_attrs *attrs, va_list in_ap)
83 : {
84 : va_list ap;
85 23 : const char *attr = NULL;
86 : errno_t ret;
87 :
88 23 : va_copy(ap, in_ap);
89 69 : while ((attr = va_arg(ap, const char *)) != NULL) {
90 23 : switch (get_attr_type(attr)) {
91 : case SYSDB_ATTR_TYPE_STRING:
92 0 : ret = sysdb_attrs_add_string(attrs, attr, va_arg(ap, const char *));
93 0 : break;
94 : case SYSDB_ATTR_TYPE_BOOL:
95 : /* _Bool is implicitly promoted to int in variadic functions */
96 0 : ret = sysdb_attrs_add_bool(attrs, attr, va_arg(ap, int));
97 0 : break;
98 : case SYSDB_ATTR_TYPE_LONG:
99 0 : ret = sysdb_attrs_add_long(attrs, attr, va_arg(ap, long int));
100 0 : break;
101 : case SYSDB_ATTR_TYPE_UINT32:
102 23 : ret = sysdb_attrs_add_uint32(attrs, attr, va_arg(ap, uint32_t));
103 23 : break;
104 : case SYSDB_ATTR_TYPE_TIME:
105 0 : ret = sysdb_attrs_add_time_t(attrs, attr, va_arg(ap, time_t));
106 0 : break;
107 : }
108 :
109 23 : if (ret != EOK) {
110 0 : return ret;
111 : }
112 : }
113 23 : va_end(ap);
114 :
115 23 : return EOK;
116 : }
117 :
118 : struct sysdb_attrs *
119 23 : _mock_sysdb_object(TALLOC_CTX *mem_ctx,
120 : const char *base_dn,
121 : const char *name,
122 : ...)
123 : {
124 : va_list ap;
125 23 : struct sysdb_attrs *attrs = NULL;
126 23 : char *orig_dn = NULL;
127 : errno_t ret;
128 :
129 23 : attrs = sysdb_new_attrs(mem_ctx);
130 23 : if (attrs == NULL) {
131 0 : goto fail;
132 : }
133 :
134 23 : orig_dn = talloc_asprintf(attrs, "cn=%s,%s", name, base_dn);
135 23 : if (orig_dn == NULL) {
136 0 : goto fail;
137 : }
138 :
139 23 : ret = sysdb_attrs_add_string(attrs, SYSDB_ORIG_DN, orig_dn);
140 23 : if (ret != EOK) {
141 0 : goto fail;
142 : }
143 :
144 23 : ret = sysdb_attrs_add_string(attrs, SYSDB_NAME, name);
145 23 : if (ret != EOK) {
146 0 : goto fail;
147 : }
148 :
149 23 : va_start(ap, name);
150 23 : ret = fill_attrs(attrs, ap);
151 23 : va_end(ap);
152 :
153 23 : if (ret != EOK) {
154 0 : goto fail;
155 : }
156 :
157 23 : talloc_free(orig_dn);
158 23 : return attrs;
159 :
160 : fail:
161 0 : talloc_free(attrs);
162 0 : return NULL;
163 : }
164 :
165 : struct sysdb_attrs *
166 15 : mock_sysdb_group_rfc2307bis(TALLOC_CTX *mem_ctx,
167 : const char *base_dn,
168 : gid_t gid,
169 : const char *name,
170 : const char **members)
171 : {
172 15 : struct sysdb_attrs *attrs = NULL;
173 : errno_t ret;
174 : int i;
175 :
176 15 : attrs = mock_sysdb_object(mem_ctx, base_dn, name,
177 : SYSDB_GIDNUM, gid);
178 15 : if (attrs == NULL) {
179 0 : return NULL;
180 : }
181 :
182 15 : if (members != NULL) {
183 26 : for (i = 0; members[i] != NULL; i++) {
184 16 : ret = sysdb_attrs_add_string(attrs, SYSDB_MEMBER, members[i]);
185 16 : if (ret != EOK) {
186 0 : talloc_zfree(attrs);
187 0 : return NULL;
188 : }
189 : }
190 : }
191 :
192 15 : return attrs;
193 : }
194 :
195 : struct sysdb_attrs *
196 8 : mock_sysdb_user(TALLOC_CTX *mem_ctx,
197 : const char *base_dn,
198 : uid_t uid,
199 : const char *name)
200 : {
201 8 : return mock_sysdb_object(mem_ctx, base_dn, name,
202 : SYSDB_UIDNUM, uid);
203 : }
|