Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <kdbglobbing.h>
10 :
11 : #include "tests.h"
12 :
13 : #define BASE_KEY "user/tests/globbing"
14 :
15 : #define should_match(k, p) succeed_if (check_key (k, p) == 0, p " does not match " k)
16 : #define should_not_match(KEY_NAME, PATTERN) \
17 : succeed_if (check_key (KEY_NAME, PATTERN) == ELEKTRA_GLOB_NOMATCH, PATTERN " should not match " KEY_NAME)
18 :
19 142 : static int check_key (const char * keyname, const char * pattern)
20 : {
21 142 : Key * k = keyNew (keyname, KEY_END);
22 142 : int rc = elektraKeyGlob (k, pattern);
23 142 : keyDel (k);
24 142 : return rc;
25 : }
26 :
27 2 : static void test_star (void)
28 : {
29 2 : printf ("star\n");
30 :
31 2 : should_match (BASE_KEY "/key", BASE_KEY "/*");
32 2 : should_match (BASE_KEY "/longkey123__31", BASE_KEY "/*");
33 2 : should_match (BASE_KEY "/1231412", BASE_KEY "/*");
34 2 : should_match (BASE_KEY "/#1231231", BASE_KEY "/*");
35 2 : should_match (BASE_KEY "/#___1234", BASE_KEY "/*");
36 2 : should_match (BASE_KEY "/????aased12355", BASE_KEY "/*");
37 2 : should_match (BASE_KEY "/***", BASE_KEY "/*");
38 2 : should_match (BASE_KEY "/*", BASE_KEY "/*");
39 2 : should_match (BASE_KEY "/abc*", BASE_KEY "/*");
40 2 : should_match (BASE_KEY "/keyabc", BASE_KEY "/*abc");
41 2 : should_match (BASE_KEY "/abckey", BASE_KEY "/*");
42 2 : should_match (BASE_KEY "/abckeyabc", BASE_KEY "/*");
43 2 : should_match (BASE_KEY "/abcabc", BASE_KEY "/*");
44 2 : should_match (BASE_KEY "/abcdabc", BASE_KEY "/*");
45 2 : should_match (BASE_KEY "/abc/d/abc", BASE_KEY "/abc/*/abc");
46 2 : should_match (BASE_KEY "/d/abc", BASE_KEY "/*/abc");
47 :
48 2 : should_not_match (BASE_KEY "/", BASE_KEY "/*");
49 2 : should_not_match (BASE_KEY "/abc/def", BASE_KEY "/*");
50 2 : }
51 :
52 2 : static void test_question_mark (void)
53 : {
54 2 : printf ("question mark\n");
55 :
56 2 : should_match (BASE_KEY "/k", BASE_KEY "/?");
57 2 : should_match (BASE_KEY "/?", BASE_KEY "/?");
58 2 : should_match (BASE_KEY "/key", BASE_KEY "/k?y");
59 2 : should_match (BASE_KEY "/key", BASE_KEY "/???");
60 :
61 2 : should_not_match (BASE_KEY "//", BASE_KEY "/?");
62 2 : should_not_match (BASE_KEY "/key", BASE_KEY "/?");
63 2 : }
64 :
65 2 : static void test_underscore (void)
66 : {
67 2 : printf ("underscore\n");
68 :
69 2 : should_match (BASE_KEY "/key", BASE_KEY "/_");
70 2 : should_match (BASE_KEY "/longkey123__31", BASE_KEY "/_");
71 2 : should_match (BASE_KEY "/1231412", BASE_KEY "/_");
72 2 : should_match (BASE_KEY "/#1231231", BASE_KEY "/_");
73 2 : should_match (BASE_KEY "/#__1234", BASE_KEY "/_");
74 2 : should_match (BASE_KEY "/????aased12355", BASE_KEY "/_");
75 2 : should_match (BASE_KEY "/***", BASE_KEY "/_");
76 2 : should_match (BASE_KEY "/_", BASE_KEY "/_");
77 2 : should_match (BASE_KEY "/abc_", BASE_KEY "/_");
78 2 : should_match (BASE_KEY "/abckey", BASE_KEY "/_");
79 2 : should_match (BASE_KEY "/abckeyabc", BASE_KEY "/_");
80 2 : should_match (BASE_KEY "/abcabc", BASE_KEY "/_");
81 2 : should_match (BASE_KEY "/abcdabc", BASE_KEY "/_");
82 2 : should_match (BASE_KEY "/abc/d/abc", BASE_KEY "/abc/_/abc");
83 2 : should_match (BASE_KEY "/d/abc", BASE_KEY "/_/abc");
84 :
85 2 : should_not_match (BASE_KEY "/", BASE_KEY "/_");
86 2 : should_not_match (BASE_KEY "/abc/def", BASE_KEY "/_");
87 2 : should_not_match (BASE_KEY "/keyabc", BASE_KEY "/_abc");
88 2 : should_not_match (BASE_KEY "/#___1234", BASE_KEY "/_");
89 2 : }
90 :
91 2 : static void test_hash (void)
92 : {
93 2 : printf ("hash\n");
94 :
95 2 : should_match (BASE_KEY "/#___1234", BASE_KEY "/#");
96 2 : should_match (BASE_KEY "/#___1234/adef", BASE_KEY "/#/adef");
97 :
98 2 : should_not_match (BASE_KEY "/abc/def", BASE_KEY "/#");
99 2 : should_not_match (BASE_KEY "/", BASE_KEY "/#");
100 2 : should_not_match (BASE_KEY "/key", BASE_KEY "/#");
101 2 : should_not_match (BASE_KEY "/longkey123__31", BASE_KEY "/#");
102 2 : should_not_match (BASE_KEY "/1231412", BASE_KEY "/#");
103 2 : should_not_match (BASE_KEY "/#1231231", BASE_KEY "/#");
104 2 : should_not_match (BASE_KEY "/#__1234", BASE_KEY "/#");
105 2 : should_not_match (BASE_KEY "/????aased12355", BASE_KEY "/#");
106 2 : should_not_match (BASE_KEY "/***", BASE_KEY "/#");
107 2 : should_not_match (BASE_KEY "/#", BASE_KEY "/#");
108 2 : should_not_match (BASE_KEY "/abc_", BASE_KEY "/#");
109 2 : should_not_match (BASE_KEY "/abckey", BASE_KEY "/#");
110 2 : should_not_match (BASE_KEY "/abckeyabc", BASE_KEY "/#");
111 2 : should_not_match (BASE_KEY "/abcabc", BASE_KEY "/#");
112 2 : should_not_match (BASE_KEY "/abcdabc", BASE_KEY "/#");
113 2 : should_not_match (BASE_KEY "/abc/d/abc", BASE_KEY "/abc/#/abc");
114 2 : should_not_match (BASE_KEY "/d/abc", BASE_KEY "/#/abc");
115 2 : should_not_match (BASE_KEY "/keyabc", BASE_KEY "/#abc");
116 2 : }
117 :
118 2 : static void test_prefix (void)
119 : {
120 2 : printf ("prefix\n");
121 :
122 2 : should_match (BASE_KEY "", BASE_KEY "/__");
123 2 : should_match (BASE_KEY "/key", BASE_KEY "/__");
124 2 : should_match (BASE_KEY "/key/subkey", BASE_KEY "/__");
125 2 : should_match (BASE_KEY "/key/sub/subkey", BASE_KEY "/__");
126 2 : should_match (BASE_KEY "/key/door", BASE_KEY "/key/__");
127 2 : should_match (BASE_KEY "/__/key", BASE_KEY "/__/key"); // should treat __ as literal
128 :
129 2 : should_not_match (BASE_KEY "/room/door/key", BASE_KEY "/__/key");
130 2 : should_not_match (BASE_KEY "/door/key", BASE_KEY "/key/__");
131 2 : }
132 :
133 2 : static void test_keyset (void)
134 : {
135 2 : printf ("keyset");
136 :
137 2 : KeySet * test = ksNew (4, keyNew (BASE_KEY "/yes/a", KEY_END), keyNew (BASE_KEY "/yes/b", KEY_END),
138 : keyNew (BASE_KEY "/no/a", KEY_END), keyNew (BASE_KEY "/no/b", KEY_END), KS_END);
139 :
140 2 : KeySet * expected = ksNew (2, keyNew (BASE_KEY "/yes/a", KEY_END), keyNew (BASE_KEY "/yes/b", KEY_END), KS_END);
141 :
142 2 : KeySet * actual = ksNew (0, KS_END);
143 2 : succeed_if (elektraKsGlob (actual, test, BASE_KEY "/yes/*") == ksGetSize (expected), "wrong number of matching keys");
144 :
145 2 : ksRewind (expected);
146 2 : ksRewind (actual);
147 :
148 2 : Key * curA = ksNext (actual);
149 2 : Key * curE = ksNext (expected);
150 8 : while (curA != NULL && curE != NULL)
151 : {
152 4 : succeed_if (keyCmp (curA, curE) == 0, keyName (curE));
153 4 : curA = ksNext (actual);
154 4 : curE = ksNext (expected);
155 : }
156 :
157 2 : succeed_if (curA == NULL && curE == NULL, "not same number of keys");
158 :
159 2 : ksDel (test);
160 2 : ksDel (expected);
161 2 : ksDel (actual);
162 2 : }
163 :
164 2 : int main (int argc, char ** argv)
165 : {
166 2 : printf (" GLOBBING TESTS\n");
167 2 : printf ("==================\n\n");
168 :
169 2 : init (argc, argv);
170 :
171 2 : test_star ();
172 2 : test_question_mark ();
173 2 : test_hash ();
174 2 : test_underscore ();
175 2 : test_prefix ();
176 2 : test_keyset ();
177 :
178 2 : print_result ("test_globbing");
179 :
180 2 : return nbError;
181 : }
|