add even more tests
[elektra-initiative:libelektra.git] / src / plugins / yajl / testmod_yajl.c
1 /*************************************************************************** 
2  *           test_mount.c  - Test suite for testing backend mounting
3  *                  -------------------
4  *  begin                : Thu Nov 6 2007
5  *  copyright            : (C) 2007 by Patrick Sabin
6  *  email                : patricksabin@gmx.at
7  ****************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the BSD License (revised).                      *
13  *                                                                         *
14  ***************************************************************************/
15
16 #include "yajl.h"
17 #include "name.h"
18 #include "array.h"
19 #include "iterator.h"
20
21 #ifdef HAVE_KDBCONFIG_H
22 #include "kdbconfig.h"
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <tests_internal.h>
30
31 KeySet *getEmptyKeys()
32 {
33         return ksNew(1,
34                         keyNew("user/tests/yajl",
35                                KEY_END),
36                         KS_END
37                         );
38 }
39
40 KeySet *getNullKeys()
41 {
42         Key *k1, *k2;
43         KeySet *ks = ksNew(10,
44                         keyNew("user/tests/yajl",
45                                KEY_END),
46                         k1 = keyNew("user/tests/yajl/nullkey",
47                                KEY_VALUE, "will be removed",
48                                KEY_END),
49                         k2 = keyNew("user/tests/yajl/second_nullkey",
50                                KEY_VALUE, "will be removed too",
51                                KEY_END),
52                         KS_END
53                 );
54         keySetBinary(k1, NULL, 0);
55         keySetBinary(k2, NULL, 0);
56
57         ksRewind(ks); // shouldn't that be default?
58         return ks;
59 }
60
61 KeySet *getBelowKeys()
62 {
63         KeySet *ks = ksNew(10,
64                         keyNew("user/tests/yajl",
65                                KEY_END),
66                         keyNew("user/tests/yajl/fancy",
67                                KEY_END),
68                         keyNew("user/tests/yajl/fancy/path",
69                                KEY_END),
70                         keyNew("user/tests/yajl/fancy/path/below",
71                                KEY_END),
72                         keyNew("user/tests/yajl/fancy/path/below/x",
73                                KEY_END),
74                         keyNew("user/tests/yajl/fancy/path/below/x/y",
75                                KEY_END),
76                         keyNew("user/tests/yajl/fancy/path/below/x/y/z",
77                                KEY_VALUE, "val1",
78                                KEY_END),
79                         keyNew("user/tests/yajl/fancy/path/below/v",
80                                KEY_END),
81                         keyNew("user/tests/yajl/fancy/path/below/v/y",
82                                KEY_END),
83                         keyNew("user/tests/yajl/fancy/path/below/v/y/z",
84                                KEY_VALUE, "val2",
85                                KEY_END),
86                         KS_END
87                 );
88
89         ksRewind(ks); // shouldn't that be default?
90         return ks;
91 }
92
93 /*
94 KeySet *getBelowKeys()
95 {
96         KeySet *ks = ksNew(10,
97                         keyNew("user/tests/yajl",
98                                KEY_END),
99                         keyNew("user/tests/yajl/x",
100                                KEY_END),
101                         keyNew("user/tests/yajl/x/y",
102                                KEY_END),
103                         keyNew("user/tests/yajl/x/y/z",
104                                KEY_VALUE, "val1",
105                                KEY_END),
106                         keyNew("user/tests/yajl/v",
107                                KEY_END),
108                         keyNew("user/tests/yajl/v/y",
109                                KEY_END),
110                         keyNew("user/tests/yajl/v/y/z",
111                                KEY_VALUE, "val2",
112                                KEY_END),
113                         KS_END
114                 );
115
116         ksRewind(ks); // shouldn't that be default?
117         return ks;
118 }
119 */
120
121 KeySet *getBooleanKeys()
122 {
123         KeySet *ks = ksNew(10,
124                         keyNew("user/tests/yajl",
125                                KEY_END),
126                         keyNew("user/tests/yajl/boolean_key",
127                                KEY_VALUE, "true",
128                                KEY_META, "type", "boolean",
129                                KEY_END),
130                         keyNew("user/tests/yajl/second_boolean_key",
131                                KEY_VALUE, "false",
132                                KEY_META, "type", "boolean",
133                                KEY_END),
134                         KS_END
135                 );
136
137         ksRewind(ks);
138         return ks;
139 }
140
141 KeySet *getNumberKeys()
142 {
143         KeySet *ks = ksNew(10,
144                         keyNew("user/tests/yajl",
145                                KEY_END),
146                         keyNew("user/tests/yajl/number_key",
147                                KEY_VALUE, "25",
148                                KEY_META, "type", "number",
149                                KEY_END),
150                         keyNew("user/tests/yajl/second_number_key",
151                                KEY_VALUE, "23002390202",
152                                KEY_META, "type", "number",
153                                KEY_END),
154                         keyNew("user/tests/yajl/third_number_key",
155                                KEY_VALUE, "230020202.233",
156                                KEY_META, "type", "number",
157                                KEY_END),
158                         KS_END
159                 );
160
161         return ks;
162 }
163
164 KeySet *getStringKeys()
165 {
166         KeySet *ks = ksNew(10,
167                         keyNew("user/tests/yajl",
168                                KEY_END),
169                         keyNew("user/tests/yajl/string_key",
170                                KEY_VALUE, "25",
171                                KEY_END),
172                         keyNew("user/tests/yajl/second_string_key",
173                                KEY_VALUE, "some string",
174                                KEY_END),
175                         keyNew("user/tests/yajl/third_string_key",
176                                KEY_VALUE, "escape {}; \" \\ problem",
177                                KEY_END),
178                         KS_END
179                 );
180
181         return ks;
182 }
183
184 KeySet *getMapKeys ()
185 {
186         KeySet *ks = ksNew(10,
187                         keyNew("user/tests/yajl",
188                                KEY_END),
189                         keyNew("user/tests/yajl/map",
190                                KEY_END),
191                         keyNew("user/tests/yajl/map/string_key",
192                                KEY_VALUE, "25",
193                                KEY_END),
194                         keyNew("user/tests/yajl/map/second_string_key",
195                                KEY_VALUE, "some string",
196                                KEY_END),
197                         keyNew("user/tests/yajl/map/nested_map",
198                                KEY_END),
199                         keyNew("user/tests/yajl/map/nested_map/string_key",
200                                KEY_VALUE, "25",
201                                KEY_END),
202                         keyNew("user/tests/yajl/map/nested_map/second_string_key",
203                                KEY_VALUE, "some string",
204                                KEY_END),
205                         keyNew("user/tests/yajl/second_map",
206                                KEY_END),
207                         keyNew("user/tests/yajl/second_map/string_key",
208                                KEY_VALUE, "25",
209                                KEY_END),
210                         keyNew("user/tests/yajl/second_map/second_string_key",
211                                KEY_VALUE, "some string",
212                                KEY_END),
213                         keyNew("user/tests/yajl/string_key",
214                                KEY_VALUE, "25",
215                                KEY_END),
216                         keyNew("user/tests/yajl/second_string_key",
217                                KEY_VALUE, "some string",
218                                KEY_END),
219                         KS_END
220                 );
221
222         return ks;
223 }
224
225 KeySet *getArrayKeys()
226 {
227         KeySet *ks = ksNew(30,
228                         keyNew("user/tests/yajl",
229                                KEY_END),
230                         keyNew("user/tests/yajl/array",
231                                KEY_END),
232                         keyNew("user/tests/yajl/array/#0",
233                                KEY_VALUE, "true",
234                                KEY_META, "type", "boolean",
235                                KEY_END),
236                         keyNew("user/tests/yajl/array/#1",
237                                KEY_VALUE, "25",
238                                KEY_META, "type", "number",
239                                KEY_END),
240                         keyNew("user/tests/yajl/array/#2",
241                                KEY_VALUE, "some string",
242                                KEY_END),
243                         keyNew("user/tests/yajl/array/#3",
244                                KEY_VALUE, "0",
245                                KEY_META, "type", "number",
246                                KEY_END),
247                         keyNew("user/tests/yajl/array/#4",
248                                KEY_VALUE, "1",
249                                KEY_META, "type", "number",
250                                KEY_END),
251                         keyNew("user/tests/yajl/array/#5",
252                                KEY_VALUE, "2",
253                                KEY_META, "type", "number",
254                                KEY_END),
255                         keyNew("user/tests/yajl/array/#6",
256                                KEY_VALUE, "3",
257                                KEY_META, "type", "number",
258                                KEY_END),
259                         keyNew("user/tests/yajl/array/#7",
260                                KEY_VALUE, "more \\ a",
261                                KEY_END),
262                         keyNew("user/tests/yajl/array/#8",
263                                KEY_VALUE, "string \"",
264                                KEY_END),
265                         keyNew("user/tests/yajl/array/#9",
266                                KEY_VALUE, "string abc",
267                                KEY_END),
268                         keyNew("user/tests/yajl/array/#_10", // hack for keeping sort order
269                                KEY_VALUE, "def abc",
270                                KEY_END),
271                         keyNew("user/tests/yajl/array/#_11",
272                                KEY_VALUE, "false",
273                                KEY_META, "type", "boolean",
274                                KEY_END),
275                         keyNew("user/tests/yajl/array/#_12",
276                                KEY_VALUE, "42",
277                                KEY_META, "type", "number",
278                                KEY_END),
279                         /*
280                         keyNew("user/tests/yajl/array/#___333",
281                                KEY_END),
282                         keyNew("user/tests/yajl/array/#_#__#333",
283                                KEY_VALUE, "42",
284                                KEY_META, "type", "number",
285                                KEY_END),
286                         keyNew("user/tests/yajl/array/#______4444", // number of _ not intuitive
287                                KEY_END),
288                         keyNew("user/tests/yajl/array/#_#__#___#4444", // gets quite long... (but works!)
289                                KEY_END),
290                         */
291                         KS_END
292                 );
293
294         return ks;
295 }
296
297 KeySet *getOpenICCKeys()
298 {
299         KeySet *ks = ksNew(60,
300                         keyNew("user/tests/yajl",
301                                KEY_END),
302
303 keyNew("user/tests/yajl/org",
304                 KEY_END),
305 keyNew("user/tests/yajl/org/freedesktop",
306                 KEY_END),
307 keyNew("user/tests/yajl/org/freedesktop/openicc",
308                 KEY_END),
309 keyNew("user/tests/yajl/org/freedesktop/openicc/device",
310                 KEY_END),
311 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera",
312                 KEY_END),
313 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0",
314                 KEY_END),
315 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/prefix",
316                 KEY_VALUE, "EXIF_",
317                 KEY_END),
318 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/EXIF_model",
319                 KEY_VALUE, "ShinyGlass", KEY_END),
320 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/EXIF_serial",
321                 KEY_VALUE, "1200000", KEY_END),
322 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/EXIF_mnft",
323                 KEY_VALUE, "GLAS", KEY_END),
324 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/EXIF_manufacturer",
325                 KEY_VALUE, "Glasshuette", KEY_END),
326 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/icc_profile",
327                 KEY_VALUE, "profile_name.icc",
328                 KEY_END),
329 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/creation_date",
330                 KEY_VALUE, "05/08/11 11:59:50",
331                 KEY_END),
332 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/expire_date",
333                 KEY_VALUE, "08/08/11 11:59:50",
334                 KEY_END),
335 //Typo in example:
336 //keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/automatic_assignment",
337 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/automatic_assigment",
338                 KEY_VALUE, "1", KEY_END),
339 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#0/comment",
340                 KEY_VALUE, "nonsense example", KEY_END),
341 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1",
342                 KEY_END),
343 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/prefix",
344                 KEY_VALUE, "EXIF_", KEY_END),
345 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/EXIF_model",
346                 KEY_VALUE, "Knips", KEY_END),
347 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/EXIF_serial",
348                 KEY_VALUE, "3400000", KEY_END),
349 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/EXIF_mnft",
350                 KEY_VALUE, "CON", KEY_END),
351 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/EXIF_manufacturer",
352                 KEY_VALUE, "ConquerLight",
353                 KEY_END),
354 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/icc_profile",
355                 KEY_VALUE, "profile_name2.icc",
356                 KEY_END),
357 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/creation_date",
358                 KEY_VALUE, "05/08/11 11:59:50",
359                 KEY_END),
360 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/expire_date",
361                 KEY_VALUE, "08/08/11 11:59:50",
362                 KEY_END),
363 // keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/automatic_assignment",
364 keyNew("user/tests/yajl/org/freedesktop/openicc/device/camera/#1/automatic_assigment",
365                 KEY_VALUE, "1", KEY_END),
366 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor",
367                 KEY_END),
368 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0",
369                 KEY_END),
370 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/prefix",
371                 KEY_VALUE, "EDID_", KEY_END),
372 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_mnft_id",
373                 KEY_VALUE, "12", KEY_END),
374 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_model_id",
375                 KEY_VALUE, "123", KEY_END),
376 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_model",
377                 KEY_VALUE, "LCD1", KEY_END),
378 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_serial",
379                 KEY_VALUE, "ABCD", KEY_END),
380 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_red_x",
381                 KEY_VALUE, "0.599609", KEY_END),
382 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_red_y",
383                 KEY_VALUE, "0.34375", KEY_END),
384 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_green_x",
385                 KEY_VALUE, "0.320312", KEY_END),
386 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_green_y",
387                 KEY_VALUE, "0.554688", KEY_END),
388 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_blue_x",
389                 KEY_VALUE, "0.150391", KEY_END),
390 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_blue_y",
391                 KEY_VALUE, "0.120117", KEY_END),
392 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_white_x",
393                 KEY_VALUE, "0.313477", KEY_END),
394 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_white_y",
395                 KEY_VALUE, "0.329102", KEY_END),
396 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_gamma",
397                 KEY_VALUE, "2.2", KEY_END),
398 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_mnft",
399                 KEY_VALUE, "VEN", KEY_END),
400 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_manufacturer",
401                 KEY_VALUE, "Vendor1", KEY_END),
402 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#0/EDID_date",
403                 KEY_VALUE, "2007-T16", KEY_END),
404 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1",
405                 KEY_END),
406 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/prefix",
407                 KEY_VALUE, "EDID_", KEY_END),
408 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_mnft_id",
409                 KEY_VALUE, "34", KEY_END),
410 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_model_id",
411                 KEY_VALUE, "456", KEY_END),
412 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_model",
413                 KEY_VALUE, "other monitor", KEY_END),
414 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_serial",
415                 KEY_VALUE, "other serial", KEY_END),
416 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_red_x",
417                 KEY_VALUE, "0.599609", KEY_END),
418 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_red_y",
419                 KEY_VALUE, "0.34375", KEY_END),
420 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_green_x",
421                 KEY_VALUE, "0.320312", KEY_END),
422 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_green_y",
423                 KEY_VALUE, "0.554688", KEY_END),
424 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_blue_x",
425                 KEY_VALUE, "0.150391", KEY_END),
426 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_blue_y",
427                 KEY_VALUE, "0.120117", KEY_END),
428 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_white_x",
429                 KEY_VALUE, "0.313477", KEY_END),
430 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_white_y",
431                 KEY_VALUE, "0.329102", KEY_END),
432 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_gamma",
433                 KEY_VALUE, "2.2", KEY_END),
434 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_mnft",
435                 KEY_VALUE, "NEC", KEY_END),
436 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_manufacturer",
437                 KEY_VALUE, "NEC", KEY_END),
438 keyNew("user/tests/yajl/org/freedesktop/openicc/device/monitor/#1/EDID_date",
439                 KEY_VALUE, "2001-T12", KEY_END),
440
441
442                         KS_END
443                 );
444
445         return ks;
446 }
447
448 KeySet *getSomeBelowKeys()
449 {
450         return ksNew(10,
451                         keyNew("user/some/path/below",
452                                KEY_END),
453                         keyNew("user/some/path/below/tests",
454                                KEY_END),
455                         keyNew("user/some/path/below/tests/yajl",
456                                KEY_END),
457                         keyNew("user/some/path/below/tests/yajl/boolean_key",
458                                KEY_VALUE, "true",
459                                KEY_META, "type", "boolean",
460                                KEY_END),
461                         keyNew("user/some/path/below/tests/yajl/second_boolean_key",
462                                KEY_VALUE, "false",
463                                KEY_META, "type", "boolean",
464                                KEY_END),
465                         KS_END);
466 }
467
468
469 KeySet *modules;
470
471 void test_json(const char * fileName,
472                      KeySet * compareKeySet,
473                      KeySet * conf)
474 {
475         printf ("Test json with %s\n", fileName);
476
477         Plugin *plugin = elektraPluginOpen("yajl", modules, conf, 0);
478         exit_if_fail (plugin != 0, "could not open plugin");
479         // printf ("Test with %s\n", srcdir_file(fileName));
480
481         Key *parentKey = keyNew ("user/tests/yajl",
482                         KEY_VALUE, srcdir_file(fileName),
483                         KEY_END);
484         KeySet *keys = ksNew(0);
485         succeed_if(plugin->kdbGet(plugin, keys, parentKey) == 1, "kdbGet was not successful");
486         succeed_if(output_error(parentKey), "error in kdbGet");
487         succeed_if(output_warnings(parentKey), "warnings in kdbGet");
488
489         /*
490         output_keyset(keys);
491         output_keyset(compareKeySet);
492         */
493         compare_keyset(keys, compareKeySet);
494
495         char * fileNameCompare = malloc(strlen(fileName)+6);
496         strcpy(fileNameCompare, fileName);
497         strcat(fileNameCompare, ".comp");
498         keySetString(parentKey, srcdir_file(fileNameCompare));
499         // printf("File name is: %s\n", keyString(parentKey));
500
501         succeed_if(plugin->kdbSet(plugin, keys, parentKey) == 1, "kdbSet was not successful");
502         succeed_if(output_error(parentKey), "error in kdbSet");
503         succeed_if(output_warnings(parentKey), "warnings in kdbSet");
504         free(fileNameCompare);
505
506         succeed_if(compare_line_files(srcdir_file(fileName), keyString(parentKey)),
507                         "files do not match as expected");
508
509         /*
510         printf ("The keys we read out are:\n");
511         output_keyset(keys);
512         printf ("The keys we compared it with:\n");
513         output_keyset(compareKeySet);
514         */
515
516         keyDel (parentKey);
517         ksDel (keys);
518         ksDel (compareKeySet);
519
520         elektraPluginClose(plugin, 0);
521 }
522
523 void test_readWrite(const char * fileName,
524                      KeySet * conf)
525 {
526         printf ("Test read write with %s\n", fileName);
527
528         Plugin *plugin = elektraPluginOpen("yajl", modules, conf, 0);
529         exit_if_fail (plugin != 0, "could not open plugin");
530         // printf ("Test with %s\n", srcdir_file(fileName));
531
532         Key *parentKey = keyNew ("user/tests/yajl",
533                         KEY_VALUE, srcdir_file(fileName),
534                         KEY_END);
535         KeySet *keys = ksNew(0);
536         succeed_if(plugin->kdbGet(plugin, keys, parentKey) == 1, "kdbGet was not successful");
537         succeed_if(output_error(parentKey), "error in kdbGet");
538         succeed_if(output_warnings(parentKey), "warnings in kdbGet");
539
540         output_keyset(keys);
541
542         char * fileNameCompare = malloc(strlen(fileName)+6);
543         strcpy(fileNameCompare, fileName);
544         strcat(fileNameCompare, ".comp");
545         keySetString(parentKey, srcdir_file(fileNameCompare));
546         // keySetString(parentKey, "/proc/self/fd/1");
547         printf("File name is: %s\n", keyString(parentKey));
548
549         succeed_if(plugin->kdbSet(plugin, keys, parentKey) == 1, "kdbSet was not successful");
550         succeed_if(output_error(parentKey), "error in kdbSet");
551         succeed_if(output_warnings(parentKey), "warnings in kdbSet");
552         free(fileNameCompare);
553
554         succeed_if(compare_line_files(srcdir_file(fileName), keyString(parentKey)),
555                         "files do not match as expected");
556
557         keyDel (parentKey);
558         ksDel (keys);
559
560         elektraPluginClose(plugin, 0);
561 }
562
563 void test_array()
564 {
565         printf ("Test array\n");
566
567         Key *k = keyNew("user/array/#0", KEY_END);
568         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
569         succeed_if(!strcmp(keyName(k), "user/array/#1"), "array entry name not correct");
570         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
571         succeed_if(!strcmp(keyName(k), "user/array/#2"), "array entry name not correct");
572         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
573         succeed_if(!strcmp(keyName(k), "user/array/#3"), "array entry name not correct");
574         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
575         succeed_if(!strcmp(keyName(k), "user/array/#4"), "array entry name not correct");
576         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
577         succeed_if(!strcmp(keyName(k), "user/array/#5"), "array entry name not correct");
578         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
579         succeed_if(!strcmp(keyName(k), "user/array/#6"), "array entry name not correct");
580         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
581         succeed_if(!strcmp(keyName(k), "user/array/#7"), "array entry name not correct");
582         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
583         succeed_if(!strcmp(keyName(k), "user/array/#8"), "array entry name not correct");
584         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
585         succeed_if(!strcmp(keyName(k), "user/array/#9"), "array entry name not correct");
586         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
587         succeed_if(!strcmp(keyName(k), "user/array/#_10"), "array entry name not correct");
588         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
589         succeed_if(!strcmp(keyName(k), "user/array/#_11"), "array entry name not correct");
590         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
591         succeed_if(!strcmp(keyName(k), "user/array/#_12"), "array entry name not correct");
592         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
593         succeed_if(!strcmp(keyName(k), "user/array/#_13"), "array entry name not correct");
594         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
595         succeed_if(!strcmp(keyName(k), "user/array/#_14"), "array entry name not correct");
596         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
597         succeed_if(!strcmp(keyName(k), "user/array/#_15"), "array entry name not correct");
598         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
599         succeed_if(!strcmp(keyName(k), "user/array/#_16"), "array entry name not correct");
600         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
601         succeed_if(!strcmp(keyName(k), "user/array/#_17"), "array entry name not correct");
602         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
603         succeed_if(!strcmp(keyName(k), "user/array/#_18"), "array entry name not correct");
604         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
605         succeed_if(!strcmp(keyName(k), "user/array/#_19"), "array entry name not correct");
606         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
607         succeed_if(!strcmp(keyName(k), "user/array/#_20"), "array entry name not correct");
608         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
609         succeed_if(!strcmp(keyName(k), "user/array/#_21"), "array entry name not correct");
610         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
611         succeed_if(!strcmp(keyName(k), "user/array/#_22"), "array entry name not correct");
612         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
613         succeed_if(!strcmp(keyName(k), "user/array/#_23"), "array entry name not correct");
614         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
615         succeed_if(!strcmp(keyName(k), "user/array/#_24"), "array entry name not correct");
616         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
617         succeed_if(!strcmp(keyName(k), "user/array/#_25"), "array entry name not correct");
618         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
619         succeed_if(!strcmp(keyName(k), "user/array/#_26"), "array entry name not correct");
620         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
621         succeed_if(!strcmp(keyName(k), "user/array/#_27"), "array entry name not correct");
622         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
623         succeed_if(!strcmp(keyName(k), "user/array/#_28"), "array entry name not correct");
624         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
625         succeed_if(!strcmp(keyName(k), "user/array/#_29"), "array entry name not correct");
626         succeed_if(!elektraArrayIncName(k), "increment array entry name returned error");
627         keyDel(k);
628 }
629
630 // TODO: make nicer and put to test framework
631 #define succeed_if_equal(x,y) succeed_if(!strcmp(x,y), x)
632
633 void test_nextNotBelow()
634 {
635         printf ("Test next not below\n");
636
637         KeySet *ks = getNullKeys();
638         ksRewind(ks);
639         Key *k = elektraNextNotBelow(ks);
640         succeed_if_equal(keyName(k), "user/tests/yajl/nullkey");
641         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/nullkey");
642         k = elektraNextNotBelow(ks);
643         succeed_if_equal(keyName(k), "user/tests/yajl/second_nullkey");
644         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/second_nullkey");
645         k = elektraNextNotBelow(ks);
646         succeed_if(k == 0, "not at end of keyset");
647         succeed_if(ksCurrent(ks) == 0, "not at end of keyset");
648         ksDel (ks);
649
650         ks = getBooleanKeys();
651         ksRewind(ks);
652         k = elektraNextNotBelow(ks);
653         succeed_if_equal(keyName(k), "user/tests/yajl/boolean_key");
654         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/boolean_key");
655         k = elektraNextNotBelow(ks);
656         succeed_if_equal(keyName(k), "user/tests/yajl/second_boolean_key");
657         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/second_boolean_key");
658         k = elektraNextNotBelow(ks);
659         succeed_if(k == 0, "not at end of keyset");
660         succeed_if(ksCurrent(ks) == 0, "not at end of keyset");
661         ksDel (ks);
662
663         ks = getBelowKeys();
664         ksRewind(ks);
665         k = elektraNextNotBelow(ks);
666         succeed_if_equal(keyName(k), "user/tests/yajl/fancy/path/below/v/y/z");
667         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/fancy/path/below/v/y/z");
668         k = elektraNextNotBelow(ks);
669         succeed_if_equal(keyName(k), "user/tests/yajl/fancy/path/below/x/y/z");
670         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/fancy/path/below/x/y/z");
671         k = elektraNextNotBelow(ks);
672         succeed_if(k == 0, "not at end of keyset");
673         succeed_if(ksCurrent(ks) == 0, "not at end of keyset");
674         ksDel (ks);
675
676         ks = getMapKeys();
677         ksRewind(ks);
678         k = elektraNextNotBelow(ks);
679         succeed_if_equal(keyName(k), "user/tests/yajl/map/nested_map/second_string_key");
680         succeed_if_equal(keyName(ksCurrent(ks)), "user/tests/yajl/map/nested_map/second_string_key");
681         ksDel (ks);
682 }
683
684 void test_reverseLevel()
685 {
686         Key *k = keyNew("user/abc/defghi/jkl", KEY_END);
687         int level=0;
688         char buffer[20];
689
690         printf ("Test reverse level\n");
691
692         keyNameReverseIterator it =  elektraKeyNameGetReverseIterator(k);
693         while (elektraKeyNameReverseNext(&it))
694         {
695                 level++;
696
697                 strncpy(buffer,it.current,it.size);
698                 buffer[it.size]=0;
699
700                 // printf("Level %d name: \"%s\"\n",level,buffer);
701                 switch (level)
702                 {
703                         case 4: succeed_if (strcmp (buffer, "user") == 0, "keyNameGetOneLevel not correct"); break;
704                         case 3: succeed_if (strcmp (buffer, "abc") == 0, "keyNameGetOneLevel not correct"); break;
705                         case 2: succeed_if (strcmp (buffer, "defghi") == 0, "keyNameGetOneLevel not correct"); break;
706                         case 1: succeed_if (strcmp (buffer, "jkl") == 0, "keyNameGetOneLevel not correct"); break;
707                         default: succeed_if (0, "should not reach case statement");
708                 }
709         }
710
711         keySetName(k,"user////\\/abc/\\/def\\/ghi////jkl\\/\\/");
712
713         level = 0;
714         it =  elektraKeyNameGetReverseIterator(k);
715         while (elektraKeyNameReverseNext(&it))
716         {
717                 level++;
718
719                 strncpy(buffer,it.current,it.size);
720                 buffer[it.size]=0;
721
722                 // printf("Level %d name: \"%s\"\n",level,buffer);
723                 switch (level)
724                 {
725                         case 4: succeed_if (strcmp (buffer, "user") == 0, "keyNameGetOneLevel not correct"); break;
726                         case 3: succeed_if (strcmp (buffer, "\\/abc") == 0, "keyNameGetOneLevel not correct"); break;
727                         case 2: succeed_if (strcmp (buffer, "\\/def\\/ghi") == 0, "keyNameGetOneLevel not correct"); break;
728                         case 1: succeed_if (strcmp (buffer, "jkl\\/\\/") == 0, "keyNameGetOneLevel not correct"); break;
729                         default: succeed_if (0, "should not reach case statement");
730                 }
731         }
732
733         keyDel (k);
734 }
735
736 void test_countLevel()
737 {
738         Key *k = keyNew("user///", KEY_END);
739         succeed_if(elektraKeyCountLevel(k) == 1, "count level wrong");
740         keySetName(k, "user/x");
741         succeed_if(elektraKeyCountLevel(k) == 2, "count level wrong");
742         keySetName(k, "user/x/z/f");
743         succeed_if(elektraKeyCountLevel(k) == 4, "count level wrong");
744         keySetName(k, "user/x/z\\/f");
745         succeed_if(elektraKeyCountLevel(k) == 3, "count level wrong");
746
747         Key *k2 = keyNew("user/x/z", KEY_END);
748         succeed_if(elektraKeyCountEqualLevel(k, k2) == 2,
749                         "equal level wrong");
750
751         keySetName(k,  "user/x/z\\/f");
752         keySetName(k2, "user/x/z\\/f");
753         succeed_if(elektraKeyCountEqualLevel(k, k2) == 3,
754                         "equal level wrong");
755
756         keySetName(k,  "user/x/v/ffkkk");
757         keySetName(k2, "user/x/v/ff");
758         succeed_if(elektraKeyCountEqualLevel(k, k2) == 3,
759                         "equal level wrong");
760
761         keySetName(k,  "user/x/v/ff");
762         keySetName(k2, "user/x/v/ff");
763         succeed_if(elektraKeyCountEqualLevel(k, k2) == 4,
764                         "equal level wrong");
765
766         keySetName(k,  "user/x\\abc/v/ff");
767         keySetName(k2, "user/x\\abc/v/ff");
768         succeed_if(elektraKeyCountEqualLevel(k, k2) == 4,
769                         "equal level wrong");
770
771         keyDel(k);
772         keyDel(k2);
773 }
774
775 void test_writing()
776 {
777         KeySet *conf = ksNew(0);
778         Key *parentKey = keyNew("user/tests/yajl",
779                                 KEY_VALUE, "/proc/self/fd/1",
780                                 KEY_END);
781
782         Plugin *plugin = elektraPluginOpen("yajl", modules, conf, 0);
783         exit_if_fail (plugin != 0, "could not open plugin");
784
785         KeySet *ks = getNullKeys();
786         /*
787         output_keyset(ks);
788
789         succeed_if(plugin->kdbSet(plugin, ks, parentKey) == 1, "kdbSet was not successful");
790         succeed_if(plugin->kdbSet(plugin, getBooleanKeys(), parentKey) == 1, "kdbSet was not successful");
791         succeed_if(plugin->kdbSet(plugin, getNumberKeys(), parentKey) == 1, "kdbSet was not successful");
792         succeed_if(plugin->kdbSet(plugin, getStringKeys(), parentKey) == 1, "kdbSet was not successful");
793         succeed_if(plugin->kdbSet(plugin, getMapKeys(), parentKey) == 1, "kdbSet was not successful");
794         succeed_if(plugin->kdbSet(plugin, getArrayKeys(), parentKey) == 1, "kdbSet was not successful");
795         ksDel(ks); ks = getOpenICCKeys();
796         succeed_if(plugin->kdbSet(plugin, ks, parentKey) == 1, "kdbSet was not successful");
797         */
798
799         ksDel(ks);
800         keyDel(parentKey);
801
802         elektraPluginClose(plugin, 0);
803 }
804
805 int main(int argc, char** argv)
806 {
807         printf("YAJL       TESTS\n");
808         printf("==================\n\n");
809
810         modules = ksNew(0);
811         elektraModulesInit(modules, 0);
812
813         init (argc, argv);
814
815         test_array();
816         test_nextNotBelow();
817         test_reverseLevel();
818         test_countLevel();
819         test_writing();
820
821         test_json("examples/testdata_null.json", getNullKeys(), ksNew(0));
822         test_json("examples/testdata_boolean.json", getBooleanKeys(), ksNew(0));
823         test_json("examples/testdata_number.json", getNumberKeys(), ksNew(0));
824         test_json("examples/testdata_string.json", getStringKeys(), ksNew(0));
825         test_json("examples/testdata_maps.json", getMapKeys(), ksNew(0));
826         test_json("examples/testdata_array.json", getArrayKeys(), ksNew(0));
827         test_json("examples/testdata_below.json", getBelowKeys(), ksNew(0));
828         test_json("examples/OpenICC_device_config_DB.json", getOpenICCKeys(), ksNew(0));
829
830         // TODO currently do not have a KeySet, wait for C-plugin to make
831         // it easy to generate it..
832         test_readWrite("examples/empty_object.json", ksNew(0));
833         test_readWrite("examples/empty_array.json", ksNew(0));
834         test_readWrite("examples/rfc_object.json", ksNew(0));
835         test_readWrite("examples/rfc_array.json", ksNew(0));
836         test_readWrite("examples/testdata_array_mixed.json", ksNew(0));
837         test_readWrite("examples/testdata_array_in_array.json", ksNew(0));
838         test_readWrite("examples/testdata_array_in_array_anon_map.json", ksNew(0));
839         test_readWrite("examples/testdata_array_nested.json", ksNew(0));
840         test_readWrite("examples/testdata_array_broken.json", ksNew(0));
841         test_readWrite("examples/testdata_array_special_ending.json", ksNew(0));
842         test_readWrite("examples/testdata_array_outside.json", ksNew(0));
843         test_readWrite("examples/keyframes_complex.json", ksNew(0));
844         test_readWrite("examples/testdata_array_mixed2.json", ksNew(0));
845         test_readWrite("examples/testdata_array_special_start.json", ksNew(0));
846         test_readWrite("examples/testdata_array_mixed3.json", ksNew(0));
847         test_readWrite("examples/testdata_empty_in_array.json", ksNew(0));
848         test_readWrite("examples/testdata_empty_in_map.json", ksNew(0));
849         test_readWrite("examples/testdata_empty_in_array1.json", ksNew(0));
850         test_readWrite("examples/testdata_empty_in_map1.json", ksNew(0));
851
852         elektraModulesClose(modules, 0);
853         ksDel (modules);
854
855         printf("\ntest_yajl RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
856
857         return nbError;
858 }