Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for the ini plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #ifdef HAVE_KDBCONFIG_H
11 : #include "kdbconfig.h"
12 : #endif
13 :
14 : #include <stdio.h>
15 : #ifdef HAVE_STDLIB_H
16 : #include <stdlib.h>
17 : #endif
18 : #ifdef HAVE_STRING_H
19 : #include <string.h>
20 : #endif
21 :
22 : #include <tests_plugin.h>
23 :
24 :
25 4 : static void test_plainIniRead (char * fileName)
26 : {
27 4 : Key * parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
28 :
29 4 : KeySet * conf = ksNew (0, KS_END);
30 4 : PLUGIN_OPEN ("ini");
31 :
32 4 : KeySet * ks = ksNew (0, KS_END);
33 :
34 4 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
35 4 : succeed_if (output_error (parentKey), "error in kdbGet");
36 4 : succeed_if (output_warnings (parentKey), "warnings in kdbGet");
37 :
38 4 : Key * key = ksLookupByName (ks, "user/tests/ini-read/nosectionkey", KDB_O_NONE);
39 4 : exit_if_fail (key, "nosectionkey not found");
40 4 : succeed_if (!strcmp ("nosectionvalue", keyString (key)), "nosectionkey contained invalid data");
41 :
42 4 : key = ksLookupByName (ks, "user/tests/ini-read/section1", KDB_O_NONE);
43 4 : exit_if_fail (key, "section1 not found");
44 :
45 4 : key = ksLookupByName (ks, "user/tests/ini-read/section1/key1", KDB_O_NONE);
46 4 : exit_if_fail (key, "key1 not found");
47 4 : succeed_if (!strcmp ("value1", keyString (key)), "key1 contained invalid data");
48 :
49 4 : key = ksLookupByName (ks, "user/tests/ini-read/section2/emptykey", KDB_O_NONE);
50 4 : exit_if_fail (key, "emptykey not found");
51 4 : succeed_if (!strcmp ("", keyString (key)), "emptykey contained invalid data");
52 :
53 4 : ksDel (ks);
54 4 : keyDel (parentKey);
55 :
56 4 : PLUGIN_CLOSE ();
57 4 : }
58 :
59 2 : static void test_plainIniWrite (char * fileName)
60 : {
61 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
62 2 : KeySet * conf = ksNew (0, KS_END);
63 2 : PLUGIN_OPEN ("ini");
64 :
65 2 : KeySet * ks = ksNew (30, keyNew ("user/tests/ini-write/nosectionkey", KEY_VALUE, "nosectionvalue", KEY_END),
66 : keyNew ("user/tests/ini-write/section1", KEY_META, "internal/ini/section", "", KEY_END),
67 : keyNew ("user/tests/ini-write/section1/key1", KEY_VALUE, "value1", KEY_END),
68 : keyNew ("user/tests/ini-write/section1/key2", KEY_VALUE, "value2", KEY_END),
69 : keyNew ("user/tests/ini-write/section2", KEY_META, "internal/ini/section", "", KEY_END),
70 : keyNew ("user/tests/ini-write/section2/key3", KEY_VALUE, "value3", KEY_END),
71 : keyNew ("user/tests/ini-write/section2/emptykey", KEY_END), KS_END);
72 :
73 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
74 2 : succeed_if (output_error (parentKey), "error in kdbSet");
75 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
76 :
77 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
78 :
79 2 : keyDel (parentKey);
80 2 : ksDel (ks);
81 :
82 2 : PLUGIN_CLOSE ();
83 2 : }
84 :
85 2 : static void test_plainIniEmptyWrite (char * fileName)
86 : {
87 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
88 2 : KeySet * conf = ksNew (0, KS_END);
89 2 : PLUGIN_OPEN ("ini");
90 :
91 2 : KeySet * ks = ksNew (30, keyNew ("user/tests/ini-write/nosectionkey", KEY_VALUE, "nosectionvalue", KEY_END),
92 : keyNew ("user/tests/ini-write/section1", KEY_META, "internal/ini/section", "",
93 :
94 : KEY_END),
95 : keyNew ("user/tests/ini-write/section1/key1", KEY_VALUE, "value1", KEY_END),
96 : keyNew ("user/tests/ini-write/section1/key2", KEY_VALUE, "value2", KEY_END),
97 : keyNew ("user/tests/ini-write/section2", KEY_META, "internal/ini/section", "", KEY_END),
98 : keyNew ("user/tests/ini-write/section2/key3", KEY_VALUE, "value3", KEY_END),
99 : keyNew ("user/tests/ini-write/section2/emptykey", KEY_META, "internal/ini/empty", "", KEY_END), KS_END);
100 :
101 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
102 2 : succeed_if (output_error (parentKey), "error in kdbSet");
103 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
104 :
105 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
106 :
107 2 : KeySet * readKS = ksNew (0, KS_END);
108 2 : succeed_if (plugin->kdbGet (plugin, readKS, parentKey) >= 0, "kdbGet failed");
109 : const Key * meta;
110 2 : Key * searchKey = keyNew ("user/tests/ini-write/section2/emptykey", KEY_META, "internal/ini/empty", "", KEY_END);
111 2 : Key * key = ksLookup (readKS, searchKey, KDB_O_NONE);
112 2 : meta = keyGetMeta (key, "internal/ini/empty");
113 2 : succeed_if (meta != NULL, "reading empty key again failed");
114 2 : ksDel (readKS);
115 2 : keyDel (parentKey);
116 2 : keyDel (searchKey);
117 2 : ksDel (ks);
118 2 : PLUGIN_CLOSE ();
119 2 : }
120 2 : static void test_commentIniRead (char * fileName)
121 : {
122 2 : Key * parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
123 :
124 2 : KeySet * conf = ksNew (0, KS_END);
125 2 : PLUGIN_OPEN ("ini");
126 :
127 2 : KeySet * ks = ksNew (0, KS_END);
128 :
129 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
130 2 : succeed_if (output_error (parentKey), "error in kdbGet");
131 2 : succeed_if (output_warnings (parentKey), "warnings in kdbGet");
132 :
133 2 : Key * key = ksLookupByName (ks, "user/tests/ini-read/nosectionkey", KDB_O_NONE);
134 2 : exit_if_fail (key, "nosectionkey not found");
135 2 : const Key * noSectionComment = keyGetMeta (key, "comments/#0");
136 2 : exit_if_fail (noSectionComment, "nosectionkey contained no comment");
137 2 : succeed_if (!strcmp (";nosection comment1", keyString (noSectionComment)), "nosectionkey contained an invalid comment");
138 2 : noSectionComment = keyGetMeta (key, "comments/#1");
139 2 : exit_if_fail (noSectionComment, "nosectionkey contained no comment");
140 2 : succeed_if (!strcmp (";nosection comment2", keyString (noSectionComment)), "nosectionkey contained an invalid comment");
141 :
142 :
143 2 : key = ksLookupByName (ks, "user/tests/ini-read/section1", KDB_O_NONE);
144 2 : exit_if_fail (key, "section1 not found");
145 2 : const Key * sectionComment = keyGetMeta (key, "comments/#0");
146 2 : exit_if_fail (sectionComment, "sectionkey contained no comment");
147 2 : succeed_if (!strcmp (";section comment1", keyString (sectionComment)), "sectionkey contained an invalid comment");
148 2 : sectionComment = keyGetMeta (key, "comments/#1");
149 2 : exit_if_fail (sectionComment, "sectionkey contained no comment");
150 2 : succeed_if (!strcmp (";section comment2", keyString (sectionComment)), "sectionkey contained an invalid comment");
151 :
152 :
153 2 : key = ksLookupByName (ks, "user/tests/ini-read/section1/key1", KDB_O_NONE);
154 2 : exit_if_fail (key, "key1 not found");
155 2 : const Key * keyComment_ = keyGetMeta (key, "comments/#0");
156 2 : exit_if_fail (keyComment_, "key1 contained no comment");
157 2 : succeed_if (!strcmp (";key comment1", keyString (keyComment_)), "key1 contained an invalid comment");
158 2 : keyComment_ = keyGetMeta (key, "comments/#1");
159 2 : exit_if_fail (keyComment_, "key1 contained no comment");
160 2 : succeed_if (!strcmp (";key comment2", keyString (keyComment_)), "key1 contained an invalid comment");
161 :
162 :
163 2 : ksDel (ks);
164 2 : keyDel (parentKey);
165 :
166 2 : PLUGIN_CLOSE ();
167 2 : }
168 :
169 2 : static void test_commentIniWrite (char * fileName)
170 : {
171 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
172 2 : KeySet * conf = ksNew (0, KS_END);
173 2 : PLUGIN_OPEN ("ini");
174 :
175 2 : KeySet * ks = ksNew (30,
176 : keyNew ("user/tests/ini-write/nosectionkey", KEY_VALUE, "nosectionvalue", KEY_META, "comments", "#1", KEY_META,
177 : "comments/#0", ";nosection comment1", KEY_META, "comments/#1", ";nosection comment2", KEY_END),
178 : keyNew ("user/tests/ini-write/section1", KEY_META, "internal/ini/section", "", KEY_META, "comments", "#1",
179 : KEY_META, "comments/#0", ";section comment1", KEY_META, "comments/#1", ";section comment2", KEY_END),
180 : keyNew ("user/tests/ini-write/section1/key1", KEY_VALUE, "value1", KEY_META, "comments", "#1", KEY_META,
181 : "comments/#0", ";key comment1", KEY_META, "comments/#1", ";key comment2", KEY_END),
182 : KS_END);
183 :
184 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
185 2 : succeed_if (output_error (parentKey), "error in kdbSet");
186 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
187 :
188 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
189 :
190 2 : ksDel (ks);
191 2 : keyDel (parentKey);
192 :
193 2 : PLUGIN_CLOSE ();
194 2 : }
195 :
196 :
197 2 : static void test_multilineIniRead (char * fileName)
198 : {
199 2 : Key * parentKey = keyNew ("user/tests/ini-multiline-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
200 :
201 2 : KeySet * conf = ksNew (30, keyNew ("system/multiline", KEY_VALUE, "1", KEY_END),
202 : keyNew ("system/linecont", KEY_VALUE, "\t", KEY_END), KS_END);
203 2 : PLUGIN_OPEN ("ini");
204 :
205 2 : KeySet * ks = ksNew (0, KS_END);
206 :
207 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
208 2 : succeed_if (output_error (parentKey), "error in kdbGet");
209 2 : succeed_if (output_warnings (parentKey), "warnings in kdbGet");
210 :
211 2 : Key * key = ksLookupByName (ks, "user/tests/ini-multiline-read/multilinesection/key1", KDB_O_NONE);
212 2 : exit_if_fail (key, "key1 not found");
213 2 : succeed_if (!strcmp ("value1\nwith continuation\nlines", keyString (key)), "key1 contained invalid data");
214 :
215 2 : key = ksLookupByName (ks, "user/tests/ini-multiline-read/singlelinesection/key2", KDB_O_NONE);
216 2 : exit_if_fail (key, "key2 not found");
217 2 : succeed_if (!strcmp ("", keyString (key)), "key2 contained invalid data");
218 :
219 2 : key = ksLookupByName (ks, "user/tests/ini-multiline-read/singlelinesection/key3", KDB_O_NONE);
220 2 : exit_if_fail (key, "key3 not found");
221 2 : succeed_if (!strcmp ("value3", keyString (key)), "key3 contained invalid data");
222 :
223 2 : ksDel (ks);
224 2 : keyDel (parentKey);
225 :
226 2 : PLUGIN_CLOSE ();
227 2 : }
228 :
229 2 : static void test_multilineIniWrite (char * fileName)
230 : {
231 2 : Key * parentKey = keyNew ("user/tests/ini-multiline-write", KEY_VALUE, elektraFilename (), KEY_END);
232 2 : KeySet * conf = ksNew (30, keyNew ("system/multiline", KEY_VALUE, "1", KEY_END),
233 : keyNew ("system/linecont", KEY_VALUE, "\t", KEY_END), KS_END);
234 2 : PLUGIN_OPEN ("ini");
235 :
236 2 : KeySet * ks = ksNew (
237 : 30, keyNew ("user/tests/ini-multiline-write/multilinesection", KEY_META, "internal/ini/section", "", KEY_END),
238 : keyNew ("user/tests/ini-multiline-write/multilinesection/key1", KEY_VALUE, "value1\nwith continuation\nlines", KEY_END),
239 : keyNew ("user/tests/ini-multiline-write/singlelinesection", KEY_META, "internal/ini/section", "", KEY_END),
240 : keyNew ("user/tests/ini-multiline-write/singlelinesection/key2", KEY_VALUE, "", KEY_END),
241 : keyNew ("user/tests/ini-multiline-write/singlelinesection/key3", KEY_VALUE, "value3", KEY_END), KS_END);
242 :
243 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
244 2 : succeed_if (output_error (parentKey), "error in kdbSet");
245 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
246 :
247 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
248 :
249 2 : ksDel (ks);
250 2 : keyDel (parentKey);
251 :
252 2 : PLUGIN_CLOSE ();
253 2 : }
254 2 : static void test_multilineIniInvalidConfigWrite (void)
255 : {
256 2 : Key * parentKey = keyNew ("user/tests/ini-multiline-write", KEY_VALUE, elektraFilename (), KEY_END);
257 2 : KeySet * conf = ksNew (30, keyNew ("system/multiline", KEY_VALUE, "0", KEY_END),
258 : keyNew ("system/linecont", KEY_VALUE, "\t", KEY_END), KS_END);
259 2 : PLUGIN_OPEN ("ini");
260 :
261 2 : KeySet * ks = ksNew (
262 : 30, keyNew ("user/tests/ini-multiline-write/multilinesection", KEY_META, "internal/ini/section", "", KEY_END),
263 : keyNew ("user/tests/ini-multiline-write/multilinesection/key1", KEY_VALUE, "value1\nwith continuation\nlines", KEY_END),
264 : KS_END);
265 :
266 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) < 0, "call to kdbSet was successful, but should fail");
267 :
268 2 : const Key * metaError = keyGetMeta (parentKey, "error");
269 2 : exit_if_fail (metaError, "No error was produced on the parentKey");
270 :
271 2 : succeed_if (!strcmp (keyString (keyGetMeta (parentKey, "error/number")), ELEKTRA_ERROR_INSTALLATION),
272 : "The plugin threw the wrong error");
273 :
274 2 : ksDel (ks);
275 2 : keyDel (parentKey);
276 :
277 2 : PLUGIN_CLOSE ();
278 2 : }
279 :
280 2 : static void test_sectionRead (char * fileName)
281 : {
282 2 : Key * parentKey = keyNew ("user/tests/ini-section-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
283 2 : KeySet * conf = ksNew (0, KS_END);
284 2 : PLUGIN_OPEN ("ini");
285 :
286 2 : KeySet * ks = ksNew (0, KS_END);
287 :
288 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
289 2 : succeed_if (output_error (parentKey), "error in kdbGet");
290 2 : succeed_if (output_warnings (parentKey), "warnings in kdbGet");
291 :
292 2 : Key * key = ksLookupByName (ks, "user/tests/ini-section-read/akey/looking/like/sections", KDB_O_NONE);
293 2 : exit_if_fail (key, "section like key not found not found");
294 2 : succeed_if (!strcmp ("value", keyString (key)), "section like key contained invalid data");
295 :
296 2 : key = ksLookupByName (ks, "user/tests/ini-section-read/emptysection", KDB_O_NONE);
297 2 : exit_if_fail (key, "empty section key not found");
298 2 : succeed_if (keyGetMeta (key, "internal/ini/section"), "empty section key is not a section key");
299 :
300 2 : key = ksLookupByName (ks, "user/tests/ini-section-read/section1", KDB_O_NONE);
301 2 : exit_if_fail (key, "section1 key not found");
302 2 : succeed_if (keyGetMeta (key, "internal/ini/section"), "section1 key is not a section key");
303 :
304 2 : key = ksLookupByName (ks, "user/tests/ini-section-read/section1/key1", KDB_O_NONE);
305 2 : exit_if_fail (key, "key1 not found not found");
306 2 : succeed_if (!strcmp ("value1", keyString (key)), "key1 contained invalid data");
307 :
308 2 : key = ksLookupByName (ks, "user/tests/ini-section-read/section1/key/with/subkey", KDB_O_NONE);
309 2 : exit_if_fail (key, "key with subkey not found not found");
310 2 : succeed_if (!strcmp ("value2", keyString (key)), "key with subkey contained invalid data");
311 :
312 2 : key = ksLookupByName (ks, "user/tests/ini-section-read/section2/with/subkey", KDB_O_NONE);
313 2 : exit_if_fail (key, "section2 key not found");
314 2 : succeed_if (keyGetMeta (key, "internal/ini/section"), "section2 key is not a section key");
315 :
316 2 : key = ksLookupByName (ks, "user/tests/ini-section-read/section2/with/subkey/key2", KDB_O_NONE);
317 2 : exit_if_fail (key, "key2 not found not found");
318 2 : succeed_if (!strcmp ("value2", keyString (key)), "key2 contained invalid data");
319 :
320 2 : ksDel (ks);
321 2 : keyDel (parentKey);
322 :
323 2 : PLUGIN_CLOSE ();
324 2 : }
325 :
326 2 : static void test_sectionWrite (char * fileName)
327 : {
328 2 : Key * parentKey = keyNew ("user/tests/ini-section-write", KEY_VALUE, elektraFilename (), KEY_END);
329 2 : KeySet * conf = ksNew (10, keyNew ("system/section", KEY_VALUE, "NULL", KEY_END), KS_END);
330 2 : PLUGIN_OPEN ("ini");
331 :
332 2 : KeySet * ks = ksNew (30, keyNew ("user/tests/ini-section-write/akey/looking/like/sections", KEY_VALUE, "value", KEY_END),
333 : keyNew ("user/tests/ini-section-write/emptysection", KEY_META, "internal/ini/section", "", KEY_END),
334 : keyNew ("user/tests/ini-section-write/section1", KEY_META, "internal/ini/section", "", KEY_END),
335 : keyNew ("user/tests/ini-section-write/section1/key1", KEY_VALUE, "value1", KEY_END),
336 : keyNew ("user/tests/ini-section-write/section1/key/with/subkey", KEY_VALUE, "value2", KEY_END),
337 : keyNew ("user/tests/ini-section-write/section2/with/subkey", KEY_META, "internal/ini/section", "", KEY_END),
338 : keyNew ("user/tests/ini-section-write/section2/with/subkey/key2", KEY_VALUE, "value2", KEY_END), KS_END);
339 :
340 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
341 2 : succeed_if (output_error (parentKey), "error in kdbSet");
342 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
343 :
344 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
345 :
346 2 : ksDel (ks);
347 2 : keyDel (parentKey);
348 :
349 2 : PLUGIN_CLOSE ();
350 2 : }
351 :
352 2 : static void test_emptySectionBug (char * fileName)
353 : {
354 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
355 2 : KeySet * conf = ksNew (0, KS_END);
356 2 : KeySet * ks = ksNew (30, keyNew ("user/tests/ini-write/MyApp/mykey", KEY_VALUE, "new_value", KEY_END),
357 : keyNew ("user/tests/ini-write/binarytest", KEY_META, "internal/ini/section", "", KEY_END),
358 : keyNew ("user/tests/ini-write/debienna/test", KEY_VALUE, "value", KEY_END), KS_END);
359 :
360 2 : PLUGIN_OPEN ("ini");
361 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
362 2 : succeed_if (output_error (parentKey), "error in kdbSet");
363 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
364 :
365 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
366 :
367 2 : ksDel (ks);
368 2 : keyDel (parentKey);
369 2 : PLUGIN_CLOSE ();
370 2 : }
371 :
372 2 : static void test_sectionMerge (char * inFile, char * cmpFile)
373 : {
374 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, srcdir_file (inFile), KEY_END);
375 2 : Key * writeParentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
376 2 : KeySet * conf = ksNew (10, keyNew ("system/mergesections", KEY_VALUE, "1", KEY_END), KS_END);
377 2 : KeySet * ks = ksNew (30, KS_END);
378 2 : PLUGIN_OPEN ("ini");
379 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
380 2 : keyDel (ksLookup (ks, parentKey, KDB_O_POP));
381 2 : keyDel (parentKey);
382 2 : succeed_if (plugin->kdbSet (plugin, ks, writeParentKey) >= 1, "call to kdbSet was not successful");
383 2 : succeed_if (compare_line_files (srcdir_file (cmpFile), keyString (writeParentKey)), "files do not match as expected");
384 2 : keyDel (ksLookup (ks, writeParentKey, KDB_O_POP));
385 2 : keyDel (writeParentKey);
386 2 : ksDel (ks);
387 2 : PLUGIN_CLOSE ();
388 2 : }
389 :
390 2 : static void test_array (char * fileName)
391 : {
392 2 : Key * parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
393 2 : KeySet * conf = ksNew (10, keyNew ("system/array", KEY_VALUE, "1", KEY_END), KS_END);
394 2 : KeySet * ks = ksNew (30, KS_END);
395 2 : PLUGIN_OPEN ("ini");
396 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
397 : Key * lookupKey;
398 2 : lookupKey = ksLookupByName (ks, "user/tests/ini-read/sec/a/#0", KDB_O_NONE);
399 2 : succeed_if (!strcmp (keyString (lookupKey), "1"), "key sec/a/#0 has the wrong value");
400 2 : lookupKey = ksLookupByName (ks, "user/tests/ini-read/sec/a/#3", KDB_O_NONE);
401 2 : succeed_if (!strcmp (keyString (lookupKey), "4"), "key sec/a/#3 has the wrong value");
402 2 : ksDel (ks);
403 2 : keyDel (parentKey);
404 2 : PLUGIN_CLOSE ();
405 2 : }
406 :
407 2 : static void test_preserveEmptyLines (char * fileName)
408 : {
409 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, srcdir_file (fileName), KEY_END);
410 2 : Key * writeParentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
411 2 : KeySet * conf = ksNew (0, KS_END);
412 2 : KeySet * ks = ksNew (30, KS_END);
413 2 : PLUGIN_OPEN ("ini");
414 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
415 2 : keyDel (ksLookup (ks, parentKey, KDB_O_POP));
416 2 : keyDel (parentKey);
417 2 : succeed_if (plugin->kdbSet (plugin, ks, writeParentKey) >= 1, "call to kdbSet was not successful");
418 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (writeParentKey)), "files do not match as expected");
419 2 : keyDel (ksLookup (ks, writeParentKey, KDB_O_POP));
420 2 : keyDel (writeParentKey);
421 2 : ksDel (ks);
422 2 : PLUGIN_CLOSE ();
423 2 : }
424 :
425 2 : static void test_insertOrder (char * source, char * compare)
426 : {
427 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, srcdir_file (source), KEY_END);
428 2 : Key * writeParentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
429 2 : KeySet * conf = ksNew (0, KS_END);
430 2 : KeySet * ks = ksNew (30, KS_END);
431 2 : KeySet * appendKS = ksNew (10, keyNew ("user/tests/ini-write/1", KEY_META, "internal/ini/section", "", KEY_END),
432 : keyNew ("user/tests/ini-write/1/testkey1_0", KEY_VALUE, "testval1_0", KEY_END),
433 : keyNew ("user/tests/ini-write/1/testkey1_1", KEY_VALUE, "testval1_1", KEY_END), KS_END);
434 :
435 2 : PLUGIN_OPEN ("ini");
436 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
437 2 : keyDel (ksLookup (ks, parentKey, KDB_O_POP));
438 2 : keyDel (parentKey);
439 2 : ksAppend (ks, appendKS);
440 2 : succeed_if (plugin->kdbSet (plugin, ks, writeParentKey) >= 1, "call to kdbSet was not successful");
441 2 : succeed_if (compare_line_files (srcdir_file (compare), keyString (writeParentKey)), "files do not match as expected");
442 2 : keyDel (ksLookup (ks, writeParentKey, KDB_O_POP));
443 2 : keyDel (writeParentKey);
444 2 : ksDel (appendKS);
445 2 : ksDel (ks);
446 2 : PLUGIN_CLOSE ();
447 2 : }
448 :
449 2 : static void test_complexInsert (char * source, char * compare)
450 : {
451 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, srcdir_file (source), KEY_END);
452 2 : Key * writeParentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
453 2 : KeySet * conf = ksNew (0, KS_END);
454 2 : KeySet * ks = ksNew (30, KS_END);
455 2 : KeySet * appendKS = ksNew (10, keyNew ("user/tests/ini-write/section/subsection", KEY_META, "internal/ini/section", "", KEY_END),
456 : keyNew ("user/tests/ini-write/section/subsection/subkey", KEY_VALUE, "subval", KEY_END),
457 : keyNew ("user/tests/ini-write/section/zkey3", KEY_VALUE, "3", KEY_END), KS_END);
458 :
459 2 : PLUGIN_OPEN ("ini");
460 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
461 2 : keyDel (ksLookup (ks, parentKey, KDB_O_POP));
462 2 : keyDel (parentKey);
463 2 : ksAppend (ks, appendKS);
464 2 : succeed_if (plugin->kdbSet (plugin, ks, writeParentKey) >= 1, "call to kdbSet was not successful");
465 2 : succeed_if (compare_line_files (srcdir_file (compare), keyString (writeParentKey)), "files do not match as expected");
466 2 : keyDel (ksLookup (ks, writeParentKey, KDB_O_POP));
467 2 : keyDel (writeParentKey);
468 2 : ksDel (appendKS);
469 2 : ksDel (ks);
470 2 : PLUGIN_CLOSE ();
471 2 : }
472 2 : static void test_arrayInsert (char * source, char * compare)
473 : {
474 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, srcdir_file (source), KEY_END);
475 2 : Key * writeParentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
476 2 : KeySet * conf = ksNew (10, keyNew ("system/array", KEY_VALUE, "1", KEY_END), KS_END);
477 2 : KeySet * ks = ksNew (30, KS_END);
478 2 : KeySet * appendKS = ksNew (10, keyNew ("user/tests/ini-write/a/section/array/#0", KEY_VALUE, "0", KEY_END),
479 : keyNew ("user/tests/ini-write/a/section/array/#1", KEY_VALUE, "1", KEY_END),
480 : keyNew ("user/tests/ini-write/a/section/array/#2", KEY_VALUE, "2", KEY_END),
481 : keyNew ("user/tests/ini-write/a/section/array/#3", KEY_VALUE, "3", KEY_END), KS_END);
482 :
483 2 : PLUGIN_OPEN ("ini");
484 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
485 2 : keyDel (ksLookup (ks, parentKey, KDB_O_POP));
486 2 : keyDel (parentKey);
487 2 : ksAppend (ks, appendKS);
488 2 : succeed_if (plugin->kdbSet (plugin, ks, writeParentKey) >= 1, "call to kdbSet was not successful");
489 2 : succeed_if (compare_line_files (srcdir_file (compare), keyString (writeParentKey)), "files do not match as expected");
490 2 : keyDel (ksLookup (ks, writeParentKey, KDB_O_POP));
491 2 : keyDel (writeParentKey);
492 2 : ksDel (appendKS);
493 2 : ksDel (ks);
494 2 : PLUGIN_CLOSE ();
495 2 : }
496 2 : static void test_readCommentMeta (char * fileName)
497 : {
498 2 : Key * parentKey = keyNew ("user/tests/ini-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
499 2 : KeySet * conf = ksNew (0, KS_END);
500 2 : KeySet * ks = ksNew (30, KS_END);
501 2 : PLUGIN_OPEN ("ini");
502 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 0, "call to kdbGet was not successful");
503 : Key * lookupKey;
504 2 : lookupKey = ksLookupByName (ks, "user/tests/ini-read/section", KDB_O_NONE);
505 2 : succeed_if (!strcmp (keyString (keyGetMeta (lookupKey, "sectionmeta")), "sectionvalue"), "failed reading section meta comment");
506 2 : lookupKey = ksLookupByName (ks, "user/tests/ini-read/section/key2", KDB_O_NONE);
507 2 : succeed_if (!strcmp (keyString (keyGetMeta (lookupKey, "key2meta")), "keyvalue"), "failed reading section meta comment");
508 2 : ksDel (ks);
509 2 : keyDel (parentKey);
510 2 : PLUGIN_CLOSE ();
511 2 : }
512 2 : static void test_writeMeta (char * fileName)
513 : {
514 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
515 2 : KeySet * conf = ksNew (0, KS_END);
516 2 : PLUGIN_OPEN ("ini");
517 :
518 2 : KeySet * ks = ksNew (30, keyNew ("user/tests/ini-write/key", KEY_VALUE, "value", KEY_META, "METAKEY", "METAVALUE", KEY_END),
519 : keyNew ("user/tests/ini-write/section/key2", KEY_VALUE, "value2", KEY_META, "METAKEY2", "METAVALUE2", KEY_END),
520 :
521 : KS_END);
522 :
523 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
524 2 : succeed_if (output_error (parentKey), "error in kdbSet");
525 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
526 :
527 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
528 :
529 2 : keyDel (parentKey);
530 2 : ksDel (ks);
531 :
532 2 : PLUGIN_CLOSE ();
533 2 : }
534 :
535 :
536 2 : static void test_commentDefaultChar (char * fileName)
537 : {
538 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
539 2 : KeySet * conf = ksNew (10, keyNew ("system/comment", KEY_VALUE, ";", KEY_END), KS_END);
540 2 : PLUGIN_OPEN ("ini");
541 :
542 2 : KeySet * ks = ksNew (30,
543 : keyNew ("user/tests/ini-write/nosectionkey", KEY_VALUE, "nosectionvalue", KEY_META, "comments", "#1", KEY_META,
544 : "comments/#0", "nosection comment1", KEY_META, "comments/#1", "nosection comment2", KEY_END),
545 : keyNew ("user/tests/ini-write/section1", KEY_BINARY, KEY_META, "comments", "#1", KEY_META, "comments/#0",
546 : "section comment1", KEY_META, "comments/#1", "section comment2", KEY_END),
547 : keyNew ("user/tests/ini-write/section1/key1", KEY_VALUE, "value1", KEY_META, "comments", "#1", KEY_META,
548 : "comments/#0", "key comment1", KEY_META, "comments/#1", "key comment2", KEY_END),
549 : KS_END);
550 :
551 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
552 2 : succeed_if (output_error (parentKey), "error in kdbSet");
553 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
554 :
555 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
556 :
557 2 : ksDel (ks);
558 2 : keyDel (parentKey);
559 :
560 2 : PLUGIN_CLOSE ();
561 2 : }
562 :
563 2 : static void test_dontquotebracketvalues (char * fileName)
564 : {
565 2 : Key * parentKey = keyNew ("user/tests/ini-write", KEY_VALUE, elektraFilename (), KEY_END);
566 2 : KeySet * conf = ksNew (0, KS_END);
567 2 : PLUGIN_OPEN ("ini");
568 :
569 2 : KeySet * ks = ksNew (30, keyNew ("user/tests/ini-write/key", KEY_VALUE, "[1, 2, 3, 4, 5, 6]", KEY_END), KS_END);
570 :
571 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
572 2 : succeed_if (output_error (parentKey), "error in kdbSet");
573 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
574 :
575 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
576 :
577 2 : keyDel (parentKey);
578 2 : ksDel (ks);
579 :
580 2 : PLUGIN_CLOSE ();
581 2 : }
582 :
583 2 : int main (int argc, char ** argv)
584 : {
585 2 : printf ("INI TESTS\n");
586 2 : printf ("==================\n\n");
587 :
588 2 : init (argc, argv);
589 :
590 2 : test_plainIniRead ("ini/plainini");
591 2 : test_plainIniWrite ("ini/plainini");
592 2 : test_plainIniRead ("ini/emptyval");
593 2 : test_plainIniEmptyWrite ("ini/emptyval");
594 2 : test_commentIniRead ("ini/commentini");
595 2 : test_commentIniWrite ("ini/commentini");
596 2 : test_multilineIniRead ("ini/multilineini");
597 2 : test_multilineIniWrite ("ini/multilineini");
598 2 : test_multilineIniInvalidConfigWrite ();
599 2 : test_sectionRead ("ini/sectionini");
600 2 : test_sectionWrite ("ini/sectionini");
601 2 : test_emptySectionBug ("ini/emptySectionBugTest");
602 2 : test_sectionMerge ("ini/sectionmerge.input", "ini/sectionmerge.output");
603 2 : test_array ("ini/array.ini");
604 2 : test_preserveEmptyLines ("ini/emptyLines");
605 2 : test_insertOrder ("ini/insertTest.input.ini", "ini/insertTest.output.ini");
606 2 : test_complexInsert ("ini/complexIn.ini", "ini/complexOut.ini");
607 2 : test_arrayInsert ("ini/arrayInsertIn.ini", "ini/arrayInsertOut.ini");
608 2 : test_dontquotebracketvalues ("ini/bracketQuoteOut.ini");
609 2 : test_commentDefaultChar ("ini/commentini");
610 2 : test_readCommentMeta ("ini/testCommentMeta.ini");
611 2 : test_writeMeta ("ini/writeCommentMeta.ini");
612 2 : print_result ("test_ini");
613 :
614 2 : return nbError;
615 : }
|