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 <tests_internal.h>
10 :
11 2 : static void test_ksPopAtCursor (void)
12 : {
13 2 : KeySet * ks = ksNew (5, keyNew ("user/valid/key1", KEY_END), keyNew ("user/valid/key2", KEY_END),
14 : keyNew ("system/valid/key1", KEY_END), keyNew ("system/valid/key2", KEY_END), KS_END);
15 2 : KeySet * ks_c = ksNew (5, keyNew ("user/valid/key1", KEY_END), keyNew ("user/valid/key2", KEY_END),
16 : keyNew ("system/valid/key1", KEY_END), KS_END);
17 2 : ksRewind (ks);
18 2 : ksNext (ks);
19 2 : ksNext (ks);
20 2 : cursor_t c = ksGetCursor (ks);
21 2 : keyDel (ksPopAtCursor (ks, c));
22 2 : succeed_if (ksCurrent (ks) == 0, "cursor position wrong");
23 :
24 2 : compare_keyset (ks, ks_c);
25 2 : ksDel (ks);
26 2 : ksDel (ks_c);
27 2 : }
28 :
29 2 : static void test_ksToArray (void)
30 : {
31 2 : KeySet * ks = ksNew (5, keyNew ("user/test1", KEY_END), keyNew ("user/test2", KEY_END), keyNew ("user/test3", KEY_END), KS_END);
32 :
33 2 : Key ** keyArray = calloc (ksGetSize (ks), sizeof (Key *));
34 2 : elektraKsToMemArray (ks, keyArray);
35 :
36 2 : succeed_if_same_string ("user/test1", keyName (keyArray[0]));
37 2 : succeed_if_same_string ("user/test2", keyName (keyArray[1]));
38 2 : succeed_if_same_string ("user/test3", keyName (keyArray[2]));
39 :
40 : /* test if cursor is restored */
41 2 : ksNext (ks);
42 2 : cursor_t cursor = ksGetCursor (ks);
43 2 : elektraKsToMemArray (ks, keyArray);
44 :
45 2 : succeed_if (ksGetCursor (ks) == cursor, "cursor was not restored");
46 :
47 2 : succeed_if (elektraKsToMemArray (0, keyArray) < 0, "wrong result on null pointer");
48 2 : succeed_if (elektraKsToMemArray (ks, 0) < 0, "wrong result on null buffer");
49 2 : KeySet * empty = ksNew (0, KS_END);
50 2 : succeed_if (elektraKsToMemArray (empty, keyArray) == 0, "wrong result on empty keyset");
51 2 : ksDel (empty);
52 :
53 2 : elektraFree (keyArray);
54 2 : ksDel (ks);
55 2 : }
56 :
57 2 : static void test_keyAsCascading (void)
58 : {
59 2 : printf ("test keyAsCascading\n");
60 2 : Key * system = keyNew ("system", KEY_END);
61 2 : Key * user = keyNew ("user/", KEY_END);
62 2 : Key * sysKey = keyNew ("system/test", KEY_END);
63 2 : Key * cascadingKey = keyNew ("/test", KEY_END);
64 : Key * ret;
65 2 : ret = keyAsCascading (system);
66 2 : succeed_if (!strcmp (keyName (ret), "/"), "Failed turning \"system\" into a cascading Key");
67 2 : keyDel (ret);
68 2 : ret = keyAsCascading (user);
69 2 : succeed_if (!strcmp (keyName (ret), "/"), "Failed turning \"user/\" into a cascading Key");
70 2 : keyDel (ret);
71 2 : ret = keyAsCascading (sysKey);
72 2 : succeed_if (!strcmp (keyName (ret), "/test"), "Failed turning \"system/test\" into a cascading Key");
73 2 : keyDel (ret);
74 2 : ret = keyAsCascading (cascadingKey);
75 2 : succeed_if (!strcmp (keyName (ret), "/test"), "Failed turning \"/test\" into a cascading Key");
76 2 : keyDel (ret);
77 2 : keyDel (system);
78 2 : keyDel (user);
79 2 : keyDel (sysKey);
80 2 : keyDel (cascadingKey);
81 2 : }
82 :
83 2 : static void test_keyGetLevelsBelow (void)
84 : {
85 2 : printf ("test keyGetLevelsBelow\n");
86 2 : Key * grandparent = keyNew ("system/grandparent", KEY_END);
87 2 : Key * parent = keyNew ("system/grandparent/parent", KEY_END);
88 2 : Key * user = keyNew ("user/grandparent/parent", KEY_END);
89 2 : Key * oneLvl = keyNew ("system/grandparent/parent/child", KEY_END);
90 2 : Key * threeLvl = keyNew ("system/grandparent/parent/child1/child2/child3", KEY_END);
91 2 : succeed_if (keyGetLevelsBelow (parent, oneLvl) == 1, "getLevelsBelow returned wrong value");
92 2 : succeed_if (keyGetLevelsBelow (parent, threeLvl) == 3, "getLevelsBelow returned wrong value");
93 2 : succeed_if (keyGetLevelsBelow (parent, parent) == 0, "getLevelsBelow returned wrong value");
94 2 : succeed_if (keyGetLevelsBelow (parent, user) == 0, "getLevelsBelow returned wrong value");
95 2 : succeed_if (keyGetLevelsBelow (parent, grandparent) == 0, "getLevelsBelow returned wrong value");
96 2 : succeed_if (keyGetLevelsBelow (grandparent, parent) == 1, "getLevelsBelow returned wrong value");
97 2 : succeed_if (keyGetLevelsBelow (grandparent, oneLvl) == 2, "getLevelsBelow returned wrong value");
98 2 : succeed_if (keyGetLevelsBelow (grandparent, threeLvl) == 4, "getLevelsBelow returned wrong value");
99 2 : succeed_if (keyGetLevelsBelow (threeLvl, grandparent) == 0, "getLevelsBelow returned wrong value");
100 2 : keyDel (grandparent);
101 2 : keyDel (parent);
102 2 : keyDel (user);
103 2 : keyDel (oneLvl);
104 2 : keyDel (threeLvl);
105 2 : }
106 :
107 2 : static void test_keyRel2 (void)
108 : {
109 2 : printf ("test keyRel2\n");
110 :
111 2 : Key * systemParent = keyNew ("system/parent", KEY_END);
112 2 : Key * userParent = keyNew ("system/parent", KEY_END);
113 2 : Key * systemChild = keyNew ("system/parent/child", KEY_END);
114 2 : Key * systemGrandChild = keyNew ("system/parent/child/grandchild", KEY_END);
115 2 : Key * userChild = keyNew ("user/parent/child", KEY_END);
116 2 : Key * userGrandChild = keyNew ("user/parent/child/grandchild", KEY_END);
117 2 : Key * cascadingChild = keyNew ("/parent/child", KEY_CASCADING_NAME, KEY_END);
118 2 : Key * cascadingGrandChild = keyNew ("/parent/child/grandchild", KEY_CASCADING_NAME, KEY_END);
119 2 : Key * systemSilbling = keyNew ("system/silbling", KEY_END);
120 2 : Key * userSilbling = keyNew ("user/silbling", KEY_END);
121 2 : Key * cascadingSilbling = keyNew ("/silbling", KEY_END);
122 2 : Key * systemNephew = keyNew ("system/silbling/nephew", KEY_END);
123 2 : Key * userNephew = keyNew ("user/silbling/nephew", KEY_END);
124 2 : Key * cascadingNephew = keyNew ("/silbling/nephew", KEY_CASCADING_NAME, KEY_END);
125 2 : Key * systemGrandNephew = keyNew ("system/silbling/nephew/grandnephew", KEY_END);
126 2 : Key * userGrandNephew = keyNew ("user/silbling/nephew/grandnephew", KEY_END);
127 2 : Key * cascadingGrandNephew = keyNew ("/silbling/nephew/grandnephew", KEY_CASCADING_NAME, KEY_END);
128 2 : Key * metaParent = keyNew ("meta", KEY_META_NAME, KEY_END);
129 2 : Key * metaChild = keyNew ("meta/child", KEY_META_NAME, KEY_END);
130 2 : Key * metaUnrelated = keyNew ("unrelated", KEY_META_NAME, KEY_END);
131 :
132 2 : succeed_if (keyRel2 (systemParent, systemChild, ELEKTRA_REL_BELOW_SAME_NS) == 1, "ELEKTRA_REL_BELOW_SAME_NS keyRel2 failed\n");
133 2 : succeed_if (keyRel2 (systemParent, userChild, ELEKTRA_REL_BELOW_SAME_NS) == 0,
134 : "ELEKTRA_REL_BELOW_SAME_NS keyRel2 should have failed\n");
135 2 : succeed_if (keyRel2 (systemParent, userChild, ELEKTRA_REL_BELOW_IGNORE_NS) == 1, "ELEKTRA_REL_BELOW_IGNORE_NS keyRel2 failed\n");
136 2 : succeed_if (keyRel2 (systemParent, cascadingChild, ELEKTRA_REL_BELOW_SAME_NS) == 0,
137 : "ELEKTRA_REL_BELOW_SAME_NS keyRel2 with cascading child should have failed\n");
138 2 : succeed_if (keyRel2 (systemParent, cascadingChild, ELEKTRA_REL_BELOW_CASCADING_NS) == 1,
139 : "ELEKTRA_REL_BELOW_SAME_NS keyRel2 with cascading child failed\n");
140 2 : succeed_if (keyRel2 (systemParent, systemGrandChild, ELEKTRA_REL_BELOW_SAME_NS) == 2, "ELEKTRA_REL_BELOW_SAME_NS keyRel2 failed\n");
141 2 : succeed_if (keyRel2 (systemParent, userGrandChild, ELEKTRA_REL_BELOW_SAME_NS) == 0,
142 : "ELEKTRA_REL_BELOW_SAME_NS keyRel2 should have failed\n");
143 2 : succeed_if (keyRel2 (systemParent, userGrandChild, ELEKTRA_REL_BELOW_IGNORE_NS) == 2,
144 : "ELEKTRA_REL_BELOW_IGNORE_NS keyRel2 failed\n");
145 2 : succeed_if (keyRel2 (systemParent, cascadingGrandChild, ELEKTRA_REL_BELOW_SAME_NS) == 0,
146 : "ELEKTRA_REL_BELOW_SAME_NS keyRel2 with cascading child should have failed\n");
147 2 : succeed_if (keyRel2 (systemParent, cascadingGrandChild, ELEKTRA_REL_BELOW_CASCADING_NS) == 2,
148 : "ELEKTRA_REL_BELOW_SAME_NS keyRel2 with cascading child failed\n");
149 2 : succeed_if (keyRel2 (systemParent, userParent, ELEKTRA_REL_BELOW_IGNORE_NS) == 0,
150 : "ELEKTRA_REL_BELOW_IGNORE_NS keyRel2 with silblings should have returned 0\n");
151 2 : succeed_if (keyRel2 (systemParent, systemChild, ELEKTRA_REL_DIRECT_BELOW_SAME_NS) == 1,
152 : "ELEKTRA_REL_DIRECT_BELOW_SAME_NS keyRel2 failed\n");
153 2 : succeed_if (keyRel2 (systemParent, userChild, ELEKTRA_REL_DIRECT_BELOW_SAME_NS) == 0,
154 : "ELEKTRA_REL_DIRECT_BELOW_SAME_NS keyRel2 should have failed\n");
155 2 : succeed_if (keyRel2 (systemParent, userChild, ELEKTRA_REL_DIRECT_BELOW_IGNORE_NS) == 1,
156 : "ELEKTRA_REL_DIRECT_BELOW_IGNORE_NS keyRel2 failed\n");
157 2 : succeed_if (keyRel2 (systemParent, cascadingChild, ELEKTRA_REL_DIRECT_BELOW_SAME_NS) == 0,
158 : "ELEKTRA_REL_DIRECT_BELOW_SAME_NS keyRel2 with cascading child should have failed\n");
159 2 : succeed_if (keyRel2 (systemParent, cascadingChild, ELEKTRA_REL_DIRECT_BELOW_CASCADING_NS) == 1,
160 : "ELEKTRA_REL_DIRECT_BELOW_SAME_NS keyRel2 with cascading child failed\n");
161 :
162 2 : succeed_if (keyRel2 (systemParent, systemSilbling, ELEKTRA_REL_SILBLING_SAME_NS) == 1,
163 : "ELEKTRA_REL_SILBLINGSAME_NS keyRel2 failed\n");
164 2 : succeed_if (keyRel2 (systemParent, userSilbling, ELEKTRA_REL_SILBLING_SAME_NS) == 0,
165 : "ELEKTRA_REL_SILBLINGSAME_NS keyRel2 should have failed\n");
166 2 : succeed_if (keyRel2 (systemParent, userSilbling, ELEKTRA_REL_SILBLING_IGNORE_NS) == 1,
167 : "ELEKTRA_REL_SILBLINGIGNORE_NS keyRel2 failed\n");
168 2 : succeed_if (keyRel2 (systemParent, cascadingSilbling, ELEKTRA_REL_SILBLING_SAME_NS) == 0,
169 : "ELEKTRA_REL_SILBLINGSAME_NS keyRel2 with cascading child should have failed\n");
170 2 : succeed_if (keyRel2 (systemParent, cascadingSilbling, ELEKTRA_REL_SILBLING_CASCADING_NS) == 1,
171 : "ELEKTRA_REL_SILBLINGSAME_NS keyRel2 with cascading child failed\n");
172 :
173 2 : succeed_if (keyRel2 (systemParent, systemNephew, ELEKTRA_REL_NEPHEW_SAME_NS) == 1, "NephewSAME_NS keyRel2 failed\n");
174 2 : succeed_if (keyRel2 (systemParent, userNephew, ELEKTRA_REL_NEPHEW_SAME_NS) == 0, "NephewSAME_NS keyRel2 should have failed\n");
175 2 : succeed_if (keyRel2 (systemParent, userNephew, ELEKTRA_REL_NEPHEW_IGNORE_NS) == 1, "NephewIGNORE_NS keyRel2 failed\n");
176 2 : succeed_if (keyRel2 (systemParent, cascadingNephew, ELEKTRA_REL_NEPHEW_SAME_NS) == 0,
177 : "NephewSAME_NS keyRel2 with cascading child should have failed\n");
178 2 : succeed_if (keyRel2 (systemParent, cascadingNephew, ELEKTRA_REL_NEPHEW_CASCADING_NS) == 1,
179 : "NephewSAME_NS keyRel2 with cascading child failed\n");
180 :
181 2 : succeed_if (keyRel2 (systemParent, systemGrandNephew, ELEKTRA_REL_NEPHEW_SAME_NS) == 2, "NephewSAME_NS keyRel2 failed\n");
182 2 : succeed_if (keyRel2 (systemParent, userGrandNephew, ELEKTRA_REL_NEPHEW_SAME_NS) == 0, "NephewSAME_NS keyRel2 should have failed\n");
183 2 : succeed_if (keyRel2 (systemParent, userGrandNephew, ELEKTRA_REL_NEPHEW_IGNORE_NS) == 2, "NephewIGNORE_NS keyRel2 failed\n");
184 2 : succeed_if (keyRel2 (systemParent, cascadingGrandNephew, ELEKTRA_REL_NEPHEW_SAME_NS) == 0,
185 : "NephewSAME_NS keyRel2 with cascading child should have failed\n");
186 2 : succeed_if (keyRel2 (systemParent, cascadingGrandNephew, ELEKTRA_REL_NEPHEW_CASCADING_NS) == 2,
187 : "NephewSAME_NS keyRel2 with cascading child failed\n");
188 :
189 2 : succeed_if (keyRel2 (systemParent, systemGrandChild, ELEKTRA_REL_NEPHEW_SAME_NS) == 0,
190 : "NephewSAME_NS keyRel2 should have failed\n");
191 2 : succeed_if (keyRel2 (systemParent, systemChild, ELEKTRA_REL_SILBLING_SAME_NS) == 0,
192 : "ELEKTRA_REL_SILBLINGSAME_NS keyRel2 should have failed\n");
193 :
194 2 : succeed_if (keyRel2 (metaParent, metaChild, ELEKTRA_REL_BELOW_IGNORE_NS) == 1, "ELEKTRA_REL_BELOW_IGNORE_NS keyRel2 failed\n");
195 2 : succeed_if (keyRel2 (metaUnrelated, metaChild, ELEKTRA_REL_BELOW_IGNORE_NS) == 0,
196 : "ELEKTRA_REL_BELOW_IGNORE_NS keyRel2 should have failed\n");
197 2 : succeed_if (keyRel2 (metaParent, metaUnrelated, ELEKTRA_REL_SILBLING_IGNORE_NS) == 1,
198 : "ELEKTRA_REL_SILBLING_IGNORE_NS keyRel2 failed\n");
199 2 : succeed_if (keyRel2 (metaParent, metaUnrelated, ELEKTRA_REL_BELOW_IGNORE_NS) == 0,
200 : "ELEKTRA_REL_BELOW_IGNORE_NS keyRel2 should have failed\n");
201 :
202 2 : succeed_if (keyRel2 (metaParent, metaChild, ELEKTRA_REL_BELOW_CASCADING_NS) == 0,
203 : "ELEKTRA_REL_BELOW_CASCADING_NS keyRel2 should have failed\n");
204 2 : succeed_if (keyRel2 (metaUnrelated, metaChild, ELEKTRA_REL_BELOW_CASCADING_NS) == 0,
205 : "ELEKTRA_REL_BELOW_CASCADING_NS keyRel2 should have failed\n");
206 2 : succeed_if (keyRel2 (metaParent, metaUnrelated, ELEKTRA_REL_SILBLING_CASCADING_NS) == 0,
207 : "ELEKTRA_REL_SILBLING_CASCADING_NS keyRel2 should have failed\n");
208 2 : succeed_if (keyRel2 (metaParent, metaUnrelated, ELEKTRA_REL_BELOW_CASCADING_NS) == 0,
209 : "ELEKTRA_REL_BELOW_CASCADING_NS keyRel2 should have failed\n");
210 :
211 2 : keyDel (systemParent);
212 2 : keyDel (userParent);
213 2 : keyDel (systemChild);
214 2 : keyDel (systemGrandChild);
215 2 : keyDel (userChild);
216 2 : keyDel (userGrandChild);
217 2 : keyDel (cascadingChild);
218 2 : keyDel (cascadingGrandChild);
219 2 : keyDel (systemSilbling);
220 2 : keyDel (userSilbling);
221 2 : keyDel (cascadingSilbling);
222 2 : keyDel (systemNephew);
223 2 : keyDel (userNephew);
224 2 : keyDel (cascadingNephew);
225 2 : keyDel (systemGrandNephew);
226 2 : keyDel (userGrandNephew);
227 2 : keyDel (cascadingGrandNephew);
228 2 : keyDel (metaParent);
229 2 : keyDel (metaChild);
230 2 : keyDel (metaUnrelated);
231 2 : }
232 :
233 2 : int main (int argc, char ** argv)
234 : {
235 2 : printf ("KEY PROPOSAL TESTS\n");
236 2 : printf ("==================\n\n");
237 :
238 2 : init (argc, argv);
239 :
240 2 : test_ksPopAtCursor ();
241 2 : test_ksToArray ();
242 :
243 2 : test_keyAsCascading ();
244 2 : test_keyGetLevelsBelow ();
245 2 : test_keyRel2 ();
246 :
247 2 : printf ("\ntest_proposal RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
248 : }
|