1 /***************************************************************************
2 * test_setget.c - setting and getting test suite
4 * begin : Wed Aug 26 2006
5 * copyright : (C) 2006 by Patrick Sabin
6 * email : patricksabin@gmx.at
7 ****************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the BSD License (revised). *
14 ***************************************************************************/
29 printf("testing subdir\n");
32 exit_if_fail(kdb,"kdbOpen() failed");
35 exit_if_fail(ks,"ksNew(0) failed");
37 /* Test creating two keys */
38 key=keyNew(KEY_ROOT "/test_d", KEY_DIR, KEY_VALUE, "dirkey", KEY_COMMENT, "dircomment", KEY_END);
39 succeed_if(key!=NULL, "keyNew: Unable to create a new key: user/test");
40 succeed_if(keySetString(key,"test value")!=0, "keySetString: Cannot set value");
41 succeed_if(keySetComment(key,"empty")!=0, "keySetComment: Cannot set the key Comment");
44 key=keyNew(KEY_ROOT "/test_d/test", KEY_END);
45 succeed_if(key!=NULL, "keyNew: Unable to create a new key: user/test/test");
46 succeed_if(keySetString(key,"test value2")!=0, "keySetString: Cannot set value");
47 succeed_if(keySetComment(key,"empty")!=0, "keySetComment: Cannot set the key Comment");
50 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "kdbSet failed");
52 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
53 exit_if_fail((kdb=kdbOpen())!=NULL,"Could not open elektra");
55 // read the directory key
57 succeed_if(ksDel(ks)==0,"Could not close keySet");
61 parent=keyNew(KEY_ROOT "/",KEY_END);
62 succeed_if (kdbGet(kdb,returned,parent,0)!=-1, "kdbGet failed");
65 for (key=ksNext(returned);key;key=ksNext(returned)) {
67 if (!strcmp(name, KEY_ROOT "/test_d")) {
72 succeed_if(!strcmp(keyValue(key),"test value"),"value of directory key not correct");
73 succeed_if(!strcmp(keyComment(key),"empty"),"comment of directory key not correct");
76 succeed_if(key1_found, KEY_ROOT "/test1 not found");
80 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
81 exit_if_fail((kdb=kdbOpen())!=NULL,"Could not open elektra");
83 // user/test and user/test/test keys are written. Now overwrite them
85 key1=keyNew(KEY_ROOT "/test_d", KEY_DIR, KEY_END);
86 succeed_if(key1!=NULL, "keyNew: Unable to create a new key: user/test");
87 succeed_if(keySetString(key1,"test value")!=0, "keySetString: Cannot set value");
88 succeed_if(keySetComment(key1,"empty")!=0, "keySetComment: Cannot set the key Comment");
91 key2=keyNew(KEY_ROOT "/test_d/test", KEY_END);
92 succeed_if(key2!=NULL, "keyNew: Unable to create a new key: user/test/test");
93 succeed_if(keySetString(key2,"test value2")!=0, "keySetString: Cannot set value");
94 succeed_if(keySetComment(key2,"empty")!=0, "keySetComment: Cannot set the key Comment");
97 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "kdbSet failed");
100 succeed_if(kdbSetKey(kdb,key2)==0,"Cannot remove key2");
102 succeed_if(kdbSetKey(kdb,key1)==0,"Cannot remove key1");
103 succeed_if(ksDel(ks)==0,"Could not close keySet");
104 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
107 void test_writeread()
110 Key *parent,*key,*key1,*key2;
114 int key1_found,key2_found;
117 printf("testing write/read\n");
119 exit_if_fail(kdb,"kdbOpen() failed");
122 exit_if_fail(ks,"ksNew(0) failed");
124 /* Test creating two keys */
125 key1=keyNew(KEY_ROOT "/test1", KEY_VALUE,"value1", KEY_END);
126 succeed_if(key1!=NULL, "keyNew: Unable to create a new key: user/test");
127 ksAppendKey(ks,key1);
129 key2=keyNew(KEY_ROOT "/test2", KEY_VALUE,"value2",KEY_COMMENT,"comment2",KEY_END);
130 succeed_if(key2!=NULL, "keyNew: Unable to create a new key: user/test2");
131 ksAppendKey(ks,key2);
133 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "kdbSet failed");
134 succeed_if(ksDel(ks)==0,"Could not close keySet");
136 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
139 /* Test reading the written keys */
142 parent=keyNew(KEY_ROOT "/",KEY_END);
144 succeed_if (kdbGet(kdb,returned,parent,0)!=-1, "could not get keys");
146 key1_found=key2_found=0;
147 for (key=ksNext(returned);key;key=ksNext(returned)) {
149 if (!strcmp(name, KEY_ROOT "/test1")) {
154 succeed_if(!strcmp(keyValue(key),"value1"),"value of first written key not correct");
155 succeed_if(!strcmp(keyComment(key),""),"comment of first written key not correct");
157 if (!strcmp(name, KEY_ROOT "/test2")) {
162 succeed_if(!strcmp(keyValue(key),"value2"),"value of second written key not correct");
163 succeed_if(!strcmp(keyComment(key),"comment2"),"value of second written key not correct");
166 succeed_if(key1_found, KEY_ROOT "/test1 not found");
167 succeed_if(key2_found, KEY_ROOT "/test2 not found");
171 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
172 exit_if_fail((kdb=kdbOpen())!=NULL,"Could not open elektra");
174 /* Test overwriting the keys */
177 exit_if_fail(ks,"ksNew(0) failed");
179 key1=keyNew(KEY_ROOT "/test1", KEY_VALUE,"value_new1",KEY_COMMENT,"comment1",KEY_END);
180 succeed_if(key1!=NULL, "keyNew: Unable to create a new key: user/test");
181 ksAppendKey(ks,key1);
183 key2=keyNew(KEY_ROOT "/test2", KEY_VALUE,"value_new2",KEY_END);
184 succeed_if(key2!=NULL, "keyNew: Unable to create a new key: user/test/test");
185 ksAppendKey(ks,key2);
187 succeed_if(kdbSet(kdb,ks,0,0)!=-1, "kdbSet failed");
189 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
190 exit_if_fail((kdb=kdbOpen())!=NULL,"Could not open elektra");
192 /* Test reading the overwritten keys */
195 parent=keyNew(KEY_ROOT "/",KEY_END);
197 succeed_if (kdbGet(kdb,returned,parent,0)!=-1, "could not kdbGet keys");
198 succeed_if (keyDel (parent) == 0, "Could not delete parent key");
200 key1_found=key2_found=0;
202 while ((key=ksNext(returned))!=0)
205 if (!strcmp(name, KEY_ROOT "/test1")) {
207 succeed_if(!strcmp(keyValue(key),"value_new1"),"value of first overwritten key not correct");
208 succeed_if(!strcmp(keyComment(key),"comment1"),"comment of first overwritten key not correct");
210 if (!strcmp(name, KEY_ROOT "/test2")) {
212 succeed_if(!strcmp(keyValue(key),"value_new2"),"value of second overwritten key not correct");
213 succeed_if(!strcmp(keyComment(key),""),"comment of second overwritten key not correct");
216 succeed_if(key1_found, KEY_ROOT "/test1 not found");
217 succeed_if(key2_found, KEY_ROOT "/test2 not found");
221 succeed_if(kdbSetKey(kdb,key2)==0,"Cannot remove key2");
223 succeed_if(kdbSetKey(kdb,key1)==0,"Cannot remove key1");
224 succeed_if(ksDel(ks)==0,"Could not close keySet");
225 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
231 Key *key,*key1,*key2,*parent;
232 KeySet *returned,*ks;
237 printf("testing mode\n");
239 exit_if_fail(kdb,"kdbOpen() failed");
242 for (mode = 6; mode < 8; mode++)
244 setMode = mode | mode << 3 | mode << 6;
245 printf ("Testing mode: %o\n", setMode);
247 exit_if_fail(ks=ksNew(0),"ksNew(0) failed");
248 /* Create a key with permission setMode */
249 key=keyNew(KEY_ROOT "/mode", KEY_MODE, setMode, KEY_VALUE,"value1",
250 KEY_COMMENT,"comment1",KEY_END);
251 succeed_if(key!=NULL, "keyNew: Unable to create a new key: user/test");
252 succeed_if(keyGetMode(key) == setMode, "Could not set mode");
254 /*errno = KDB_ERR_OK;*/
255 ret = kdbSet(kdb,ks,0,0);
256 if (mode & MODE_WRITE)
258 // succeed_if (ret == 0, "could not write key");
260 succeed_if (ret == -1, "wrote key without write mode?");
261 /*succeed_if (errno == KDB_ERR_NOCRED, "Errno not set correctly");*/
262 // TODO: exit_if_fail (ksCurrent(ks), "no current key, where did it fail?");
263 // succeed_if (strcmp (keyName(ksCurrent(ks)), KEY_ROOT "/mode") == 0, "did stop at wrong key");
264 // succeed_if (strcmp (keyValue(ksCurrent(ks)), "value1") == 0, "wrong value");
265 // succeed_if (strcmp (keyComment(ksCurrent(ks)), "comment1") == 0, "wrong comment");
270 parent=keyNew(KEY_ROOT, KEY_END);
271 ret = kdbGet(kdb,returned,parent,0);
277 while ((key=ksNext(returned))!=0)
279 if (!strcmp(keyName(key), KEY_ROOT "/mode")) {
281 // succeed_if(keyGetMode(key) == setMode, "Mode not correct after get");
282 if ((mode & MODE_READ) && (mode & MODE_WRITE))
284 succeed_if(strcmp (keyValue (key), "value1") == 0,
285 "Could not read value, but should be allowed to");
286 succeed_if(strcmp (keyComment (key), "comment1") == 0,
287 "Could not read comment, but should be allowed to");
289 else if (mode & MODE_WRITE)
291 succeed_if(!strcmp (keyValue (key), "value1") == 0,
292 "Could read value, but should not be allowed to");
293 succeed_if(strcmp (keyValue (key), "") == 0,
294 "Value not empty, but write should have failed");
295 succeed_if(!strcmp (keyComment (key), "comment1") == 0,
296 "Could read comment, but should not be allowed to");
297 succeed_if(strcmp (keyComment (key), "") == 0,
298 "Comment not empty, but write should have failed");
300 else if (mode & MODE_READ)
302 succeed_if(!strcmp (keyValue (key), "value1") == 0,
303 "Could read value, but should not be allowed to");
304 succeed_if(strcmp (keyValue (key), "") == 0,
305 "Value not empty after not allowed read");
306 succeed_if(!strcmp (keyComment (key), "comment1") == 0,
307 "Could read comment, but should not be allowed to");
308 succeed_if(strcmp (keyComment (key), "") == 0,
309 "Comment not empty after not allowed read");
314 succeed_if(key1_found, KEY_ROOT "/test1 key not found");
317 /* Overwrite the key*/
318 // TODO: copy code from above
321 /* Create a sub key for /mode */
322 exit_if_fail(ks=ksNew(0), "ksNew(0) failed");
323 key2=keyNew(KEY_ROOT "/mode/key", KEY_VALUE,"value_sub",KEY_COMMENT,"comment_sub",KEY_END);
324 succeed_if(key2!=NULL, "keyNew: Unable to create a new subkey");
325 ksAppendKey(ks,key2);
327 if (kdbSet(kdb,ks,0,0)==-1) {
328 if (!(mode & 1)) // not a directory, can't write subkey
330 succeed_if(0,"kdbSet failed");
335 /* Overwrite the directory key if there is already a subkey */
337 key1=keyNew(KEY_ROOT "/mode/key", KEY_VALUE,"value_new",KEY_COMMENT,"comment_new",KEY_END);
338 key2=keyNew(KEY_ROOT "/mode", KEY_VALUE,"value_new",KEY_COMMENT,"comment_new",KEY_END);
339 ksAppendKey(ks,key1);
340 ksAppendKey(ks,key2);
342 if (kdbSet(kdb,ks,0,0)==-1)
344 // succeed_if(errno==KDB_ERR_NOTEMPTY, "kdbSet delete nonempty key KDB_ERR_NOTEMPTY expected");
345 // TODO exit_if_fail (ksCurrent(ks) != 0, "key is Null but should be set to incorrect key");
346 // printf("trapped: %s\n",keyName(ksCurrent(ks)));
347 // succeed_if (!strcmp(KEY_ROOT "/mode",keyName(ksCurrent(ks))), "kdbSet messed up setting ksCurrent()");
350 if (!(mode & 1)) // is not directory key
352 succeed_if(0,"Could overwrite a directory with subkeys");
358 succeed_if (kdbRemove (kdb, KEY_ROOT "/mode/key") == 0, "Could not remove mode/key");
359 succeed_if (kdbRemove (kdb, KEY_ROOT "/mode") == 0, "Could not remove mode");
360 succeed_if(ksDel(ks)==0,"Could not close keySet");
362 exit_if_fail(kdbClose(kdb)==0,"Could not close libelektra");
368 /* This tests the lowest-level way to set a key and get it afterwards */
369 void test_simple_getset()
376 printf("testing simple write/read\n");
381 key1=keyNew(KEY_ROOT "/test1", KEY_VALUE,"value", KEY_COMMENT,"comment", KEY_END);
382 ksAppendKey(ks,key1);
384 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
390 key=keyNew(KEY_ROOT, KEY_END);
392 if (kdbGet(kdb,returned,key,0)==-1) {
393 succeed_if(0,"kdbGet failed");
396 key1 = ksLookupByName (returned, KEY_ROOT "/test1", 0);
397 exit_if_fail (key1 != 0, "could not lookup key");
398 succeed_if (strcmp (keyValue (key1), "value") == 0, "Value not correct");
399 succeed_if (strcmp (keyComment(key1), "comment") == 0, "Comment not correct");
406 key1=keyNew(KEY_ROOT "/test1", KEY_REMOVE, KEY_END);
407 ksAppendKey(ks,key1);
408 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
415 /* This tests the highlevel-level way to set a key and get it afterwards */
416 void test_highlevel_getset()
421 printf("testing simple write/read\n");
425 key1=keyNew(KEY_ROOT "/test3", KEY_VALUE,"value", KEY_COMMENT,"comment", KEY_END);
426 succeed_if (kdbSetKey(kdb,key1)!=-1, "could not kdbSet keys");
432 key1=keyNew(KEY_ROOT "/test3", KEY_END);
433 if (kdbGetKey(kdb,key1)==-1) {
434 succeed_if(0,"kdbGetKey failed");
436 succeed_if (strcmp (keyValue (key1), "value") == 0, "Value not correct");
437 succeed_if (strcmp (keyComment(key1), "comment") == 0, "Comment not correct");
443 key1=keyNew(KEY_ROOT "/test3", KEY_REMOVE, KEY_END);
444 succeed_if (kdbSetKey (kdb,key1)==0, "could not get a key");
451 /* This tests do recursive set and get keys */
452 void test_simple_recursive_getset()
459 printf("testing recursive write/read\n");
464 key1=keyNew(KEY_ROOT "/dir", KEY_DIR, KEY_VALUE,"dirvalue", KEY_COMMENT,"dircomment", KEY_END);
465 ksAppendKey(ks,key1);
466 key1=keyNew(KEY_ROOT "/dir/key", KEY_VALUE,"value", KEY_COMMENT,"comment", KEY_END);
467 ksAppendKey(ks,key1);
469 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
475 key=keyNew(KEY_ROOT, KEY_END);
477 if (kdbGet(kdb,returned,key,0)==-1) {
478 succeed_if(0,"kdbGet failed");
481 key1 = ksLookupByName (returned, KEY_ROOT "/dir/key", 0);
482 exit_if_fail (key1 != 0, "could not lookup key");
483 succeed_if (strcmp (keyValue (key1), "value") == 0, "Value not correct");
484 succeed_if (strcmp (keyComment(key1), "comment") == 0, "Comment not correct");
486 key1 = ksLookupByName (returned, KEY_ROOT "/dir", 0);
487 exit_if_fail (key1 != 0, "could not lookup key");
488 succeed_if (strcmp (keyValue (key1), "dirvalue") == 0, "Value not correct");
489 succeed_if (strcmp (keyComment(key1), "dircomment") == 0, "Comment not correct");
496 key1=keyNew(KEY_ROOT "/dir/key", KEY_REMOVE, KEY_END);
497 ksAppendKey(ks,key1);
498 key1=keyNew(KEY_ROOT "/dir", KEY_DIR, KEY_REMOVE, KEY_END);
499 ksAppendKey(ks,key1);
500 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
507 /* This tests do recursive set and get keys */
508 void test_recursive_getset()
517 for (i=0; i<5; i++) found[i] = 0;
519 printf("testing recursive write/read\n");
524 key1=keyNew(KEY_ROOT "/key1", KEY_VALUE,"value1", KEY_COMMENT,"comment", KEY_END);
525 ksAppendKey(ks,key1);
526 key1=keyNew(KEY_ROOT "/dir2", KEY_DIR, KEY_VALUE,"dirvalue", KEY_COMMENT,"dircomment", KEY_END);
527 ksAppendKey(ks,key1);
528 key1=keyNew(KEY_ROOT "/dir1", KEY_DIR, KEY_VALUE,"dirvalue", KEY_COMMENT,"dircomment", KEY_END);
529 ksAppendKey(ks,key1);
530 key1=keyNew(KEY_ROOT "/dir1/key2", KEY_VALUE,"value2", KEY_COMMENT,"comment", KEY_END);
531 ksAppendKey(ks,key1);
532 key1=keyNew(KEY_ROOT "/dir1/key1", KEY_VALUE,"value1", KEY_COMMENT,"comment", KEY_END);
533 ksAppendKey(ks,key1);
535 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
541 key=keyNew(KEY_ROOT, KEY_END);
543 if (kdbGet(kdb,returned,key,0)==-1) {
544 succeed_if(0,"kdbGet failed");
548 // check if all keys are in set and exactly once
549 // output_keyset(returned, 0);
551 while ((key=ksNext(returned))!=0)
553 if (strcmp (keyName(key), KEY_ROOT "/key1") == 0) found [0]++;
554 else if (strcmp (keyName(key), KEY_ROOT "/dir2") == 0) found [1]++;
555 else if (strcmp (keyName(key), KEY_ROOT "/dir1") == 0) found [2]++;
556 else if (strcmp (keyName(key), KEY_ROOT "/dir1/key2") == 0) found [3]++;
557 else if (strcmp (keyName(key), KEY_ROOT "/dir1/key1") == 0) found [4]++;
562 // printf("%d", found[i]);
563 succeed_if (found[i] == 1, "key did not appear exactly once");
567 key1 = ksLookupByName (returned, KEY_ROOT "/dir1/key1", 0);
568 exit_if_fail (key1 != 0, "could not lookup key");
569 succeed_if (strcmp (keyValue (key1), "value1") == 0, "Value not correct");
570 succeed_if (strcmp (keyComment(key1), "comment") == 0, "Comment not correct");
572 key1 = ksLookupByName (returned, KEY_ROOT "/dir1", 0);
573 exit_if_fail (key1 != 0, "could not lookup key");
574 succeed_if (strcmp (keyValue (key1), "dirvalue") == 0, "Value not correct");
575 succeed_if (strcmp (keyComment(key1), "dircomment") == 0, "Comment not correct");
582 key1=keyNew(KEY_ROOT "/dir1/key1", KEY_REMOVE, KEY_END);
583 ksAppendKey(ks,key1);
584 key1=keyNew(KEY_ROOT "/dir1/key2", KEY_REMOVE, KEY_END);
585 ksAppendKey(ks,key1);
586 key1=keyNew(KEY_ROOT "/dir1", KEY_REMOVE, KEY_END);
587 ksAppendKey(ks,key1);
588 key1=keyNew(KEY_ROOT "/dir2", KEY_REMOVE, KEY_END);
589 ksAppendKey(ks,key1);
590 key1=keyNew(KEY_ROOT "/key1", KEY_REMOVE, KEY_END);
591 ksAppendKey(ks,key1);
592 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
599 /* This tests the lowest-level way to set a directory key and get it afterwards */
600 void test_directory_getset()
607 printf("testing simple write/read\n");
612 key1=keyNew(KEY_ROOT "/dir4", KEY_DIR, KEY_VALUE,"value4", KEY_COMMENT,"comment4", KEY_END);
613 ksAppendKey(ks,key1);
615 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
621 key=keyNew(KEY_ROOT, KEY_END);
623 if (kdbGet(kdb,returned,key,0)==-1) {
624 succeed_if(0,"kdbGet failed");
627 key1 = ksLookupByName (returned, KEY_ROOT "/dir4", 0);
628 succeed_if (strcmp (keyValue (key1), "value4") == 0, "Value not correct");
629 succeed_if (strcmp (keyComment(key1), "comment4") == 0, "Comment not correct");
630 succeed_if (keyIsDir(key1) , "not a directory");
637 key1=keyNew(KEY_ROOT "/dir4", KEY_REMOVE, KEY_END);
638 ksAppendKey(ks,key1);
639 succeed_if (kdbSet(kdb,ks,0,0)!=-1, "could not kdbSet keys");
646 /* This tests the highlevel-level way to set a directory and get it afterwards */
647 void test_directory_highlevel_getset()
652 printf("testing simple write/read\n");
656 key1=keyNew(KEY_ROOT "/dir5", KEY_DIR, KEY_VALUE,"value5", KEY_COMMENT,"comment5", KEY_END);
657 succeed_if (kdbSetKey(kdb,key1)!=-1, "could not kdbSet a key");
663 key1=keyNew(KEY_ROOT "/dir5", KEY_END);
664 if (kdbGetKey(kdb,key1)==-1) {
665 succeed_if(0,"kdbGetKey failed");
667 succeed_if (strcmp (keyValue (key1), "value5") == 0, "Value not correct");
668 succeed_if (strcmp (keyComment(key1), "comment5") == 0, "Comment not correct");
669 succeed_if (keyIsDir(key1) , "not a directory");
675 key1=keyNew(KEY_ROOT "/dir5", KEY_REMOVE, KEY_END);
676 succeed_if (kdbSetKey (kdb,key1)!=-1, "could not set a key");
686 printf("ELEKTRA SET/GET TEST SUITE\n");
687 printf("========================================\n\n");
689 exit_if_fail (loadToolsLib()==0, "Unable to load elektratools");
693 test_simple_getset();
697 test_highlevel_getset();
698 test_simple_recursive_getset();
699 test_recursive_getset();
700 test_directory_getset();
701 test_directory_highlevel_getset();
703 printf("\ntest_getset RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);