Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for specload plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include "specload.h"
11 :
12 : #include <config.c>
13 :
14 : #include <stdlib.h>
15 : #include <string.h>
16 :
17 : #include <kdbconfig.h>
18 : #include <tests_plugin.h>
19 :
20 : #include "testdata.h"
21 :
22 : #include <unistd.h>
23 :
24 48 : static FILE * backupFile (const char * filename)
25 : {
26 48 : FILE * in = fopen (filename, "rb");
27 48 : FILE * out = tmpfile ();
28 :
29 : char c;
30 2132 : while ((c = fgetc (in)) != EOF)
31 : {
32 2036 : fputc (c, out);
33 : }
34 :
35 48 : fclose (in);
36 :
37 48 : return out;
38 : }
39 :
40 48 : static void restoreBackup (FILE * backup, const char * filename)
41 : {
42 48 : rewind (backup);
43 48 : FILE * out = fopen (filename, "wb");
44 :
45 : char c;
46 2132 : while ((c = fgetc (backup)) != EOF)
47 : {
48 2036 : fputc (c, out);
49 : }
50 :
51 48 : fclose (out);
52 48 : fclose (backup);
53 48 : }
54 :
55 4 : static void test_basics (bool directFile)
56 : {
57 4 : printf ("test basics %s\n", directFile ? "directFile" : "");
58 :
59 4 : Key * parentKey = keyNew (PARENT_KEY, KEY_VALUE, srcdir_file ("specload/basics.quickdump"), KEY_END);
60 : KeySet * conf;
61 4 : if (directFile)
62 : {
63 2 : conf = ksNew (2, keyNew ("/file", KEY_VALUE, srcdir_file ("specload/spec.quickdump"), KEY_END), KS_END);
64 : }
65 : else
66 : {
67 2 : conf = ksNew (2, keyNew ("/app", KEY_VALUE, TESTAPP_PATH, KEY_END), KS_END);
68 : }
69 :
70 4 : succeed_if (elektraSpecloadCheckConfig (parentKey, conf) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE,
71 : "call to checkConfig was not successful");
72 :
73 4 : PLUGIN_OPEN ("specload");
74 :
75 4 : KeySet * ks = ksNew (0, KS_END);
76 4 : KeySet * defaultSpec = DEFAULT_SPEC;
77 :
78 4 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
79 4 : compare_keyset (defaultSpec, ks);
80 :
81 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
82 4 : compare_keyset (defaultSpec, ks);
83 :
84 4 : ksDel (defaultSpec);
85 :
86 4 : keyDel (parentKey);
87 4 : ksDel (ks);
88 4 : PLUGIN_CLOSE ();
89 4 : }
90 :
91 4 : static void test_newfile (bool directFile)
92 : {
93 4 : printf ("test newfile %s\n", directFile ? "directFile" : "");
94 :
95 4 : exit_if_fail (access (srcdir_file ("specload/new.quickdump"), F_OK) == -1, "srcdir_file specload/new.quickdump shouldn't exist");
96 :
97 4 : Key * parentKey = keyNew (PARENT_KEY, KEY_VALUE, srcdir_file ("specload/new.quickdump"), KEY_END);
98 : KeySet * conf;
99 4 : if (directFile)
100 : {
101 2 : conf = ksNew (2, keyNew ("/file", KEY_VALUE, srcdir_file ("specload/spec.quickdump"), KEY_END), KS_END);
102 : }
103 : else
104 : {
105 2 : conf = ksNew (2, keyNew ("/app", KEY_VALUE, TESTAPP_PATH, KEY_END), KS_END);
106 : }
107 :
108 4 : succeed_if (elektraSpecloadCheckConfig (parentKey, conf) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE,
109 : "call to checkConfig was not successful");
110 :
111 4 : PLUGIN_OPEN ("specload");
112 :
113 4 : KeySet * ks = ksNew (0, KS_END);
114 4 : KeySet * defaultSpec = DEFAULT_SPEC;
115 :
116 4 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
117 4 : compare_keyset (defaultSpec, ks);
118 :
119 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
120 4 : compare_keyset (defaultSpec, ks);
121 :
122 4 : ksDel (defaultSpec);
123 :
124 4 : keyDel (parentKey);
125 4 : ksDel (ks);
126 4 : PLUGIN_CLOSE ();
127 :
128 4 : remove (srcdir_file ("specload/new.quickdump"));
129 4 : }
130 :
131 4 : static void test_add (bool directFile)
132 : {
133 4 : printf ("test add %s\n", directFile ? "directFile" : "");
134 :
135 4 : Key * parentKey = keyNew (PARENT_KEY, KEY_VALUE, srcdir_file ("specload/add.quickdump"), KEY_END);
136 : KeySet * conf;
137 4 : if (directFile)
138 : {
139 2 : conf = ksNew (2, keyNew ("/file", KEY_VALUE, srcdir_file ("specload/spec.quickdump"), KEY_END), KS_END);
140 : }
141 : else
142 : {
143 2 : conf = ksNew (2, keyNew ("/app", KEY_VALUE, TESTAPP_PATH, KEY_END), KS_END);
144 : }
145 :
146 4 : succeed_if (elektraSpecloadCheckConfig (parentKey, conf) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE,
147 : "call to checkConfig was not successful");
148 :
149 :
150 4 : PLUGIN_OPEN ("specload");
151 :
152 4 : FILE * backup = backupFile (srcdir_file ("specload/add.quickdump"));
153 4 : KeySet * ks = ksNew (0, KS_END);
154 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/newkey", KEY_VALUE, "0", KEY_END));
155 4 : KeySet * orig = ksDup (ks);
156 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "adding values should fail");
157 4 : compare_keyset (orig, ks);
158 4 : ksDel (ks);
159 4 : ksDel (orig);
160 4 : restoreBackup (backup, srcdir_file ("specload/add.quickdump"));
161 :
162 4 : backup = backupFile (srcdir_file ("specload/add.quickdump"));
163 4 : ks = ksNew (0, KS_END);
164 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/newkey", KEY_META, "default", "0", KEY_END));
165 4 : orig = ksDup (ks);
166 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "adding default should work");
167 4 : compare_keyset (orig, ks);
168 4 : ksDel (ks);
169 4 : ksDel (orig);
170 4 : restoreBackup (backup, srcdir_file ("specload/add.quickdump"));
171 :
172 4 : backup = backupFile (srcdir_file ("specload/add.quickdump"));
173 4 : ks = ksNew (0, KS_END);
174 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/newkey", KEY_META, "type", "string", KEY_END));
175 4 : orig = ksDup (ks);
176 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "adding type should work");
177 4 : compare_keyset (orig, ks);
178 4 : ksDel (ks);
179 4 : ksDel (orig);
180 4 : restoreBackup (backup, srcdir_file ("specload/add.quickdump"));
181 :
182 4 : backup = backupFile (srcdir_file ("specload/add.quickdump"));
183 4 : ks = ksNew (0, KS_END);
184 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/newkey", KEY_META, "description", "Lorem ipsum", KEY_END));
185 4 : orig = ksDup (ks);
186 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "adding description should work");
187 4 : compare_keyset (orig, ks);
188 4 : ksDel (ks);
189 4 : ksDel (orig);
190 4 : restoreBackup (backup, srcdir_file ("specload/add.quickdump"));
191 :
192 4 : backup = backupFile (srcdir_file ("specload/add.quickdump"));
193 4 : ks = ksNew (0, KS_END);
194 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/newkey", KEY_META, "opt/help", "Lorem ipsum opt", KEY_END));
195 4 : orig = ksDup (ks);
196 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "adding opt/help should work");
197 4 : compare_keyset (orig, ks);
198 4 : ksDel (ks);
199 4 : ksDel (orig);
200 4 : restoreBackup (backup, srcdir_file ("specload/add.quickdump"));
201 :
202 4 : keyDel (parentKey);
203 4 : PLUGIN_CLOSE ();
204 4 : }
205 :
206 4 : static void test_edit (bool directFile)
207 : {
208 4 : printf ("test edit %s\n", directFile ? "directFile" : "");
209 :
210 4 : Key * parentKey = keyNew (PARENT_KEY, KEY_VALUE, srcdir_file ("specload/edit.quickdump"), KEY_END);
211 : KeySet * conf;
212 4 : if (directFile)
213 : {
214 2 : conf = ksNew (2, keyNew ("/file", KEY_VALUE, srcdir_file ("specload/spec.quickdump"), KEY_END), KS_END);
215 : }
216 : else
217 : {
218 2 : conf = ksNew (2, keyNew ("/app", KEY_VALUE, TESTAPP_PATH, KEY_END), KS_END);
219 : }
220 :
221 4 : succeed_if (elektraSpecloadCheckConfig (parentKey, conf) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE,
222 : "call to checkConfig was not successful");
223 :
224 :
225 4 : PLUGIN_OPEN ("specload");
226 :
227 4 : FILE * backup = backupFile (srcdir_file ("specload/edit.quickdump"));
228 4 : KeySet * ks = ksNew (0, KS_END);
229 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/key", KEY_VALUE, "1", KEY_META, "default", "0", KEY_META, "description", "Lorem ipsum",
230 : KEY_META, "opt/help", "Lorem ipsum opt", KEY_END));
231 4 : KeySet * orig = ksDup (ks);
232 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "changing values should fail");
233 4 : compare_keyset (orig, ks);
234 4 : ksDel (ks);
235 4 : ksDel (orig);
236 4 : restoreBackup (backup, srcdir_file ("specload/edit.quickdump"));
237 :
238 4 : backup = backupFile (srcdir_file ("specload/edit.quickdump"));
239 4 : ks = ksNew (0, KS_END);
240 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/key", KEY_VALUE, "0", KEY_META, "default", "1", KEY_META, "description", "Lorem ipsum",
241 : KEY_META, "opt/help", "Lorem ipsum opt", KEY_END));
242 4 : orig = ksDup (ks);
243 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "changing default should work");
244 4 : compare_keyset (orig, ks);
245 4 : ksDel (ks);
246 4 : ksDel (orig);
247 4 : restoreBackup (backup, srcdir_file ("specload/edit.quickdump"));
248 :
249 4 : backup = backupFile (srcdir_file ("specload/edit.quickdump"));
250 4 : ks = ksNew (0, KS_END);
251 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/key", KEY_VALUE, "0", KEY_META, "default", "0", KEY_META, "description", "Lorem ipsum edit",
252 : KEY_META, "opt/help", "Lorem ipsum opt", KEY_END));
253 4 : orig = ksDup (ks);
254 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "changing description should work");
255 4 : compare_keyset (orig, ks);
256 4 : ksDel (ks);
257 4 : ksDel (orig);
258 4 : restoreBackup (backup, srcdir_file ("specload/edit.quickdump"));
259 :
260 4 : backup = backupFile (srcdir_file ("specload/edit.quickdump"));
261 4 : ks = ksNew (0, KS_END);
262 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/key", KEY_VALUE, "0", KEY_META, "default", "0", KEY_META, "description", "Lorem ipsum",
263 : KEY_META, "opt/help", "Lorem ipsum opt edit", KEY_END));
264 4 : orig = ksDup (ks);
265 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "changing opt/help should work");
266 4 : compare_keyset (orig, ks);
267 4 : ksDel (ks);
268 4 : ksDel (orig);
269 4 : restoreBackup (backup, srcdir_file ("specload/edit.quickdump"));
270 :
271 4 : keyDel (parentKey);
272 4 : PLUGIN_CLOSE ();
273 4 : }
274 :
275 4 : static void test_remove (bool directFile)
276 : {
277 4 : printf ("test remove %s\n", directFile ? "directFile" : "");
278 :
279 4 : Key * parentKey = keyNew (PARENT_KEY, KEY_VALUE, srcdir_file ("specload/remove.quickdump"), KEY_END);
280 : KeySet * conf;
281 4 : if (directFile)
282 : {
283 2 : conf = ksNew (2, keyNew ("/file", KEY_VALUE, srcdir_file ("specload/spec.quickdump"), KEY_END), KS_END);
284 : }
285 : else
286 : {
287 2 : conf = ksNew (2, keyNew ("/app", KEY_VALUE, TESTAPP_PATH, KEY_END), KS_END);
288 : }
289 :
290 4 : succeed_if (elektraSpecloadCheckConfig (parentKey, conf) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE,
291 : "call to checkConfig was not successful");
292 :
293 :
294 4 : PLUGIN_OPEN ("specload");
295 :
296 4 : FILE * backup = backupFile (srcdir_file ("specload/remove.quickdump"));
297 4 : KeySet * ks = ksNew (0, KS_END);
298 4 : ksAppendKey (ks,
299 : keyNew (PARENT_KEY "/key", KEY_META, "description", "Lorem ipsum", KEY_META, "opt/help", "Lorem ipsum opt", KEY_END));
300 4 : KeySet * orig = ksDup (ks);
301 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "removing values should fail");
302 4 : compare_keyset (orig, ks);
303 4 : ksDel (ks);
304 4 : ksDel (orig);
305 4 : restoreBackup (backup, srcdir_file ("specload/remove.quickdump"));
306 :
307 4 : backup = backupFile (srcdir_file ("specload/remove.quickdump"));
308 4 : ks = ksNew (0, KS_END);
309 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/key", KEY_VALUE, "0", KEY_META, "opt/help", "Lorem ipsum opt", KEY_END));
310 4 : orig = ksDup (ks);
311 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "removing description should work");
312 4 : compare_keyset (orig, ks);
313 4 : ksDel (ks);
314 4 : ksDel (orig);
315 4 : restoreBackup (backup, srcdir_file ("specload/remove.quickdump"));
316 :
317 4 : backup = backupFile (srcdir_file ("specload/remove.quickdump"));
318 4 : ks = ksNew (0, KS_END);
319 4 : ksAppendKey (ks, keyNew (PARENT_KEY "/key", KEY_VALUE, "0", KEY_META, "description", "Lorem ipsum", KEY_END));
320 4 : orig = ksDup (ks);
321 4 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "removing opt/help should work");
322 4 : compare_keyset (orig, ks);
323 4 : ksDel (ks);
324 4 : ksDel (orig);
325 4 : restoreBackup (backup, srcdir_file ("specload/remove.quickdump"));
326 :
327 4 : keyDel (parentKey);
328 4 : PLUGIN_CLOSE ();
329 4 : }
330 :
331 :
332 2 : int main (int argc, char ** argv)
333 : {
334 2 : printf ("SPECLOAD TESTS\n");
335 2 : printf ("==================\n\n");
336 :
337 2 : init (argc, argv);
338 :
339 2 : test_basics (true);
340 2 : test_basics (false);
341 :
342 2 : test_add (true);
343 2 : test_add (false);
344 :
345 2 : test_edit (true);
346 2 : test_edit (false);
347 :
348 2 : test_remove (true);
349 2 : test_remove (false);
350 :
351 2 : test_newfile (true);
352 2 : test_newfile (false);
353 :
354 2 : print_result ("testmod_specload");
355 :
356 2 : return nbError;
357 : }
|