forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_glob.c
313 lines (256 loc) · 10 KB
/
testmod_glob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#include <stdio.h>
#include "glob.h"
#include <fnmatch.h>
#include <tests.h>
#include <tests_plugin.h>
#define NR_KEYS 1
void test_match (void)
{
succeed_if (fnmatch ("user:/*/to/key", "user:/path/to/key", FNM_PATHNAME) == 0, "could not do simple fnmatch");
}
void testKeys (KeySet * ks)
{
Key * key = ksLookupByName (ks, "user:/tests/glob/test1", 0);
exit_if_fail (key, "key user:/tests/glob/test1 not found");
const Key * metaKey = keyGetMeta (key, "testmetakey1");
exit_if_fail (metaKey, "testmetakey1 not found");
succeed_if (strcmp ("testvalue1", keyValue (metaKey)) == 0, "value of metakey testmetakey1 not correct");
metaKey = keyGetMeta (key, "testmetakey2");
exit_if_fail (metaKey, "testmetakey2 not found");
succeed_if (strcmp ("testvalue2", keyValue (metaKey)) == 0, "value of metakey testmetakey2 not correct");
key = ksLookupByName (ks, "user:/tests/glob/test2/subtest1", 0);
exit_if_fail (key, "key user:/test1/subtest1 not found");
succeed_if (!keyGetMeta (key, "testmetakey1"), "testmetakey1 copied to wrong key");
succeed_if (!keyGetMeta (key, "testmetakey2"), "testmetakey2 copied to wrong key");
key = ksLookupByName (ks, "user:/tests/glob/test3", 0);
exit_if_fail (key, "key user:/tests/glob/test3 not found");
metaKey = keyGetMeta (key, "testmetakey1");
exit_if_fail (metaKey, "testmetakey1 not found");
succeed_if (strcmp ("testvalue1", keyValue (metaKey)) == 0, "value of metakey testmetakey1 not correct");
metaKey = keyGetMeta (key, "testmetakey2");
exit_if_fail (metaKey, "testmetakey2 not found");
succeed_if (strcmp ("testvalue2", keyValue (metaKey)) == 0, "value of metakey testmetakey2 not correct");
}
KeySet * createKeys (void)
{
KeySet * ks = ksNew (30, keyNew ("user:/tests/glob/test1", KEY_END), keyNew ("user:/tests/glob/test2/subtest1", KEY_END),
keyNew ("user:/tests/glob/test3", KEY_END), KS_END);
return ks;
}
void test_zeroMatchFlags (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
KeySet * conf = ksNew (20, keyNew ("user:/glob/#1", KEY_VALUE, "*test1", KEY_META, "testmetakey1", "testvalue1", KEY_END),
/* disable default pathname globbing behaviour */
keyNew ("user:/glob/#1/flags", KEY_VALUE, "", KEY_END), KS_END);
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
Key * key = ksLookupByName (ks, "user:/tests/glob/test1", 0);
exit_if_fail (key, "key user:/tests/glob/test1 not found");
const Key * metaKey1 = keyGetMeta (key, "testmetakey1");
exit_if_fail (metaKey1, "testmetakey1 not found");
succeed_if (strcmp ("testvalue1", keyValue (metaKey1)) == 0, "value of metakey testmetakey1 not correct");
key = ksLookupByName (ks, "user:/tests/glob/test3", 0);
exit_if_fail (key, "user:/tests/glob/test3 not found");
succeed_if (!keyGetMeta (key, "testmetakey1"), "testmetakey1 copied to wrong key");
key = ksLookupByName (ks, "user:/tests/glob/test2/subtest1", 0);
exit_if_fail (key, "user:/tests/glob/test2/subtest1 not found");
const Key * metaKey2 = keyGetMeta (key, "testmetakey1");
exit_if_fail (metaKey2, "testmetakey1 not found");
succeed_if (strcmp ("testvalue1", keyValue (metaKey2)) == 0, "value of metakey testmetakey1 not correct");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_setGlobalMatch (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
// clang-format off
KeySet *conf = ksNew (20,
keyNew ("user:/glob/#1", KEY_VALUE, "/*",
KEY_META, "testmetakey1", "testvalue1",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
KS_END);
// clang-format on
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
testKeys (ks);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_getGlobalMatch (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
// clang-format off
KeySet *conf = ksNew (20,
keyNew ("user:/glob/#1", KEY_VALUE, "/*",
KEY_META, "testmetakey1", "testvalue1",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
KS_END);
// clang-format on
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
succeed_if (output_error (parentKey), "error in kdbGet");
succeed_if (output_warnings (parentKey), "warnings in kdbGet");
testKeys (ks);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_getDirectionMatch (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
// clang-format off
KeySet *conf = ksNew (20,
keyNew ("user:/glob/get/#1", KEY_VALUE, "/*",
KEY_META, "testmetakey1", "testvalue1",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
keyNew ("user:/glob/set/#1", KEY_VALUE, "/*/*",
KEY_META, "testmetakey1", "testvalue1",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
KS_END);
// clang-format on
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
succeed_if (output_error (parentKey), "error in kdbGet");
succeed_if (output_warnings (parentKey), "warnings in kdbGet");
testKeys (ks);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_setDirectionMatch (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
// clang-format off
KeySet *conf = ksNew (20,
keyNew ("user:/glob/set/#1", KEY_VALUE, "/*",
KEY_META, "testmetakey1", "testvalue1",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
keyNew ("user:/glob/get/#1", KEY_VALUE, "/*/*",
KEY_META, "testmetakey1", "testvalue1",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
KS_END);
// clang-format on
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
testKeys (ks);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_namedMatchFlags (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
KeySet * conf =
ksNew (20, keyNew ("user:/glob/#1", KEY_VALUE, "user:/tests/glob/*", KEY_META, "testmetakey1", "testvalue1", KEY_END),
/* explicitly request pathname matching */
keyNew ("user:/glob/#1/flags", KEY_VALUE, "pathname", KEY_END), KS_END);
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
Key * key = ksLookupByName (ks, "user:/tests/glob/test1", 0);
exit_if_fail (key, "key user:/tests/glob/test1 not found");
const Key * metaKey1 = keyGetMeta (key, "testmetakey1");
exit_if_fail (metaKey1, "testmetakey1 not found");
key = ksLookupByName (ks, "user:/tests/glob/test3", 0);
exit_if_fail (key, "user:/tests/glob/test3 not found");
const Key * metaKey2 = keyGetMeta (key, "testmetakey1");
exit_if_fail (metaKey2, "testmetakey1 not found");
key = ksLookupByName (ks, "user:/tests/glob/test2/subtest1", 0);
exit_if_fail (key, "user:/tests/glob/test2/subtest1 not found");
const Key * metaKey3 = keyGetMeta (key, "testmetakey1");
exit_if_fail (!metaKey3, "testmetakey1 was copied to subtest1, but subtest1 should not be matched with pathname flag");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_onlyFirstMatchIsApplied (void)
{
Key * parentKey = keyNew ("user:/tests/glob", KEY_END);
// clang-format off
KeySet * conf = ksNew (20,
keyNew ("user:/glob/#1",
KEY_VALUE, "user:/tests/glob/test1*",
KEY_META, "testmetakey1", "testvalue1",
KEY_END),
keyNew ("user:/glob/#2",
KEY_VALUE, "user:/tests/glob/*",
KEY_META, "testmetakey2", "testvalue2",
KEY_END),
/* disable all flags */
keyNew ("user:/glob/#1/flags",
KEY_VALUE, "",
KEY_END),
keyNew ("user:/glob/#2/flags",
KEY_VALUE, "",
KEY_END),
KS_END);
// clang-format on
PLUGIN_OPEN ("glob");
KeySet * ks = createKeys ();
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
Key * key = ksLookupByName (ks, "user:/tests/glob/test1", 0);
exit_if_fail (key, "key user:/tests/glob/test1 not found");
const Key * firstMatchKey = keyGetMeta (key, "testmetakey1");
exit_if_fail (firstMatchKey, "testmetakey1 not found");
const Key * secondMatchKey = keyGetMeta (key, "testmetakey2");
exit_if_fail (!secondMatchKey, "testmetakey2 was applied to testmetakey1 although another match was already applied")
key = ksLookupByName (ks, "user:/tests/glob/test2/subtest1", 0);
exit_if_fail (key, "user:/tests/glob/test2/subtest1 not found");
exit_if_fail (keyGetMeta (key, "testmetakey2"), "testmetakey2 not found");
key = ksLookupByName (ks, "user:/tests/glob/test3", 0);
exit_if_fail (key, "user:/tests/glob/test3 not found");
exit_if_fail (keyGetMeta (key, "testmetakey2"), "testmetakey2 not found");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("GLOB TESTS\n");
printf ("====================\n\n");
init (argc, argv);
test_match ();
test_zeroMatchFlags ();
test_setGlobalMatch ();
test_setDirectionMatch ();
test_getGlobalMatch ();
test_getDirectionMatch ();
test_namedMatchFlags ();
test_onlyFirstMatchIsApplied ();
print_result ("testmod_glob");
return nbError;
}