forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_storage.c
1005 lines (786 loc) · 34.5 KB
/
test_storage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* @brief Tests for storage plugins
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
/* -- Imports --------------------------------------------------------------------------------------------------------------------------- */
#include <tests_internal.h>
#include <tests_plugin.h>
#include <kdbconfig.h>
/* -- Macros & Test Configuration ------------------------------------------------------------------------------------------------------- */
#define TEST_ROOT_KEY "user:/tests/storage"
// below are the plugins suggested for testing, but only compiled plugins are tested
#define NUM_PLUGINS_SUGGESTED 4
static const char * pluginsSuggested[] = { "mmapstorage_crc", "mmapstorage", "dump",
"quickdump" }; // remember to adjust NUM_PLUGINS_SUGGESTED if you add/remove a storage plugin
// below is the list of available plugins truly tested.
static size_t numPlugins = 0;
static KeySet * modules[NUM_PLUGINS_SUGGESTED];
static Plugin * plugins[NUM_PLUGINS_SUGGESTED];
static const char * pluginNames[NUM_PLUGINS_SUGGESTED];
#define open_storage_plugin(storagePlugin) \
if (openStoragePlugin (storagePlugin) == -1) \
{ \
nbError++; \
printf ("Error opening storage plugin: %s. Skipping test.\n", pluginNames[storagePlugin]); \
return; \
}
/* -- KeySet test data ------------------------------------------------------------------------------------------------------------------ */
static KeySet * simpleTestKeySet (void)
{
return ksNew (10, keyNew ("user:/tests/storage/simpleKey", KEY_VALUE, "root key", KEY_END),
keyNew ("user:/tests/storage/simpleKey/a", KEY_VALUE, "a value", KEY_END),
keyNew ("user:/tests/storage/simpleKey/b", KEY_VALUE, "b value", KEY_END), KS_END);
}
static KeySet * metaTestKeySet (void)
{
return ksNew (10, keyNew ("user:/tests/storage", KEY_VALUE, "root key", KEY_META, "a", "some metadata for root key", KEY_END),
keyNew ("user:/tests/storage/a", KEY_VALUE, "a value", KEY_META, "ab", "other metadata for a key", KEY_END),
keyNew ("user:/tests/storage/b", KEY_VALUE, "b value", KEY_META, "longer val", "metadata for key b", KEY_END),
KS_END);
}
/* -- Test helpers ---------------------------------------------------------------------------------------------------------------------- */
static void initPlugins (void)
{
// check if plugin is compiled, and only test if it is
for (size_t plugin = 0; plugin < NUM_PLUGINS_SUGGESTED; ++plugin)
{
if (strstr (ELEKTRA_PLUGINS, pluginsSuggested[plugin]) != NULL)
{
pluginNames[numPlugins] = pluginsSuggested[plugin];
numPlugins++;
}
else
{
printf ("Warning: Plugin %s is not compiled. Excuding from storage tests.\n", pluginsSuggested[plugin]);
}
}
}
static int openStoragePlugin (const size_t storagePlugin)
{
modules[storagePlugin] = ksNew (0, KS_END);
elektraModulesInit (modules[storagePlugin], 0);
KeySet * conf = ksNew (0, KS_END);
Key * errorKey = keyNew ("/", KEY_END);
Plugin * plugin = elektraPluginOpen (pluginNames[storagePlugin], modules[storagePlugin], conf, errorKey);
const Key * metaWarnings = keyGetMeta (errorKey, "warnings");
if (metaWarnings) printf ("There are warnings for plugin: %s\n", pluginNames[storagePlugin]);
const Key * metaError = keyGetMeta (errorKey, "error");
if (metaError) printf ("There are errors for plugin: %s\n", pluginNames[storagePlugin]);
if (plugin == 0)
{
printf ("Could not open plugin: %s\n", pluginNames[storagePlugin]);
return -1;
}
plugins[storagePlugin] = plugin;
keyDel (errorKey);
return 0;
}
static int closeStoragePlugin (const size_t storagePlugin)
{
elektraPluginClose (plugins[storagePlugin], 0);
elektraModulesClose (modules[storagePlugin], 0);
ksDel (modules[storagePlugin]);
return 0;
}
/* -- KeySet API tests ------------------------------------------------------------------------------------------------------------------ */
static void test_ksDupFun (const size_t storagePlugin, const char * tmpFile, KeySet * copyFunction (const KeySet * source))
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
KeySet * dupKs = copyFunction (ks);
compare_keyset (dupKs, ks);
compare_keyset (ks, dupKs);
ksDel (dupKs);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksCopy (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
KeySet * copyKs = ksNew (0, KS_END);
if (ksCopy (copyKs, ks) == 1)
{
compare_keyset (copyKs, ks);
compare_keyset (ks, copyKs);
}
else
{
yield_error ("ksCopy failed");
}
ksDel (copyKs);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksGetSize (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
ssize_t origSize = ksGetSize (ks);
succeed_if (origSize > 0, "ks was empty before kdbSet");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
ksDel (ks);
ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
ssize_t returnedSize = ksGetSize (ks);
succeed_if (origSize == returnedSize, "ksGetSize before and after kdbSet, kdbGet differ");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_double_get (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
KeySet * first = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, first, parentKey) == 1, "kdbGet was not successful");
KeySet * second = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, second, parentKey) == 1, "kdbGet was not successful");
succeed_if (first->data->array != second->data->array, "ks->array points to same thing");
compare_keyset (first, ks);
compare_keyset (ks, first);
compare_keyset (second, ks);
compare_keyset (ks, second);
ksDel (ks);
ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
KeySet * simple = simpleTestKeySet ();
compare_keyset (first, simple);
compare_keyset (second, simple);
ksDel (first);
ksDel (second);
ksDel (simple);
ksDel (ks);
keyDel (parentKey);
closeStoragePlugin (storagePlugin);
}
static void test_ksAppendKey (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
ssize_t origSize = ksGetSize (ks);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
ssize_t appendSize = 0;
Key * toAppend = keyNew (TEST_ROOT_KEY "/my/new/key", KEY_END);
if ((appendSize = ksAppendKey (ks, toAppend)) == -1)
{
yield_error ("ksAppendKey failed");
}
succeed_if (appendSize == (origSize + 1), "ksAppendKey after append should be incremented");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
ksDel (ks);
ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
ssize_t returnedSize = ksGetSize (ks);
succeed_if (returnedSize == (origSize + 1), "ksGetSize after append should be incremented");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksAppend (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
KeySet * toAppend = ksNew (10, keyNew ("user:/tests/storage/zzzz", KEY_VALUE, "root key", KEY_END),
keyNew ("user:/tests/storage/simpleKey/c", KEY_VALUE, "c value", KEY_END),
keyNew ("user:/tests/storage/simpleKey/d", KEY_VALUE, "d value", KEY_END), KS_END);
if (ksAppend (ks, toAppend) == -1)
{
yield_error ("ksAppend failed");
}
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
ksDel (ks);
ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
ksDel (toAppend);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksCut (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
// create keyset with some folder 'other' that we will then cut
KeySet * ks = simpleTestKeySet ();
KeySet * other = ksNew (10, keyNew ("user:/tests/storage/other", KEY_VALUE, "other key", KEY_END),
keyNew ("user:/tests/storage/other/a", KEY_VALUE, "other a value", KEY_END),
keyNew ("user:/tests/storage/other/b", KEY_VALUE, "other b value", KEY_END), KS_END);
if (ksAppend (ks, other) == -1)
{
yield_error ("ksAppend failed");
}
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
// now cut the 'other' folder
Key * cutKey = keyNew ("user:/tests/storage/other", KEY_END);
KeySet * returned = ksCut (ks, cutKey);
succeed_if (returned, "keyset is empty (does not contain the cut keyset)");
KeySet * simple = simpleTestKeySet ();
compare_keyset (simple, ks);
compare_keyset (other, returned);
ksDel (other);
ksDel (returned);
ksDel (simple);
keyDel (cutKey);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksPop (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
KeySet * poppedKeys = ksNew (0, KS_END);
succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) != -1, "ksAppendKey failed");
succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) != -1, "ksAppendKey failed");
succeed_if (ksGetSize (ks) == 1, "ksGetSize after ksPop should be decremented");
succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) != -1, "ksAppendKey failed");
succeed_if (ksGetSize (poppedKeys) == 3, "expecting three keys to be in ks");
succeed_if (ksPop (ks) == 0, "ks should be empty");
succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) == -1, "ks should be empty, but is not");
KeySet * test = simpleTestKeySet ();
compare_keyset (poppedKeys, test);
ksDel (test);
ksDel (poppedKeys);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksLookup (const size_t storagePlugin, const char * tmpFile, elektraLookupFlags options)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * lookup = keyNew ("user:/tests/storage/simpleKey/a", KEY_END);
Key * found = ksLookup (ks, lookup, options);
succeed_if (found, "did not find key");
if (options == KDB_O_POP)
{
// make sure key is really popped
keyDel (found);
found = ksLookup (ks, lookup, 0);
succeed_if (!found, "found key that should not exist");
}
keyDel (lookup);
lookup = keyNew ("user:/tests/storage/simpleKey/foo", KEY_END);
found = ksLookup (ks, lookup, options);
succeed_if (!found, "found key that should not exist");
keyDel (lookup);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_ksLookupByName (const size_t storagePlugin, const char * tmpFile, elektraLookupFlags options)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = simpleTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
const char * name = "user:/tests/storage/simpleKey/a";
Key * found = ksLookupByName (ks, name, options);
succeed_if (found, "did not find key");
if (options == KDB_O_POP)
{
// make sure key is really popped
keyDel (found);
found = ksLookupByName (ks, name, 0);
succeed_if (!found, "found key that should not exist");
}
name = "user:/tests/storage/simpleKey/foo";
found = ksLookupByName (ks, name, options);
succeed_if (!found, "found key that should not exist");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
/* -- Key API tests --------------------------------------------------------------------------------------------------------------------- */
static void test_keyFlags (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = ksNew (10, keyNew ("user:/tests/storage/testKey", KEY_FLAGS, KEY_BINARY, KEY_VALUE, "test key", KEY_END), KS_END);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, "user:/tests/storage/testKey", 0);
succeed_if (found, "did not find key");
succeed_if (keyIsBinary (found) == 1, "Key is not binary.");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyDup (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, "user:/tests/storage/b", 0);
succeed_if (found, "did not find key");
Key * duplicate = keyDup (found, KEY_CP_ALL);
// check that keyDup has not changed KeySet
KeySet * expected = metaTestKeySet ();
compare_keyset (ks, expected);
// check that KeySet is intact after deleting duplicate Key
keyDel (duplicate);
compare_keyset (ks, expected);
ksDel (expected);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyCopy_newKey (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, "user:/tests/storage/b", 0);
succeed_if (found, "did not find key");
Key * copy = keyNew ("/", KEY_END);
succeed_if (keyCopy (copy, found, KEY_CP_ALL) != NULL, "keyCopy failed");
compare_key (found, copy);
// check that keyCopy has not changed KeySet
KeySet * expected = metaTestKeySet ();
compare_keyset (ks, expected);
// check that KeySet is intact after deleting Key copy
keyDel (copy);
compare_keyset (ks, expected);
ksDel (expected);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyCopy_clearOverwriteKey (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * toCopy = keyNew ("user:/tests/storage/newnewkey", KEY_VALUE, "new key", KEY_END);
Key * found = ksLookupByName (ks, "user:/tests/storage/b", KDB_O_POP);
succeed_if (found, "did not find key");
// currently, KDB_O_POP doest not clear the readonly name flag
found->hasReadOnlyName = false;
// overwrite Key
succeed_if (keyCopy (found, 0, KEY_CP_ALL) != NULL, "keyCopy: clear destination failed");
succeed_if (keyCopy (found, toCopy, KEY_CP_ALL) != NULL, "keyCopy failed");
compare_key (found, toCopy);
keyDel (toCopy);
// put key back into place
ksAppendKey (ks, found);
// write KeySet back to storage
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
found = ksLookupByName (ks, "user:/tests/storage/newnewkey", 0);
succeed_if (found, "did not find key");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyDel (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, "user:/tests/storage/b", 0);
succeed_if (found, "did not find key");
succeed_if (keyDel (found) > 0, "Key was NULL or free()'d unexpectedly");
// check that keyDel has not changed KeySet
KeySet * expected = metaTestKeySet ();
compare_keyset (ks, expected);
ksDel (expected);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyClear (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, "user:/tests/storage/a", 0);
succeed_if (found, "did not find key");
succeed_if (keyClear (found) == 0, "Key was NULL, keyClear failed");
keySetName (found, "user:/tests/storage/foo");
keySetString (found, "new key value");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
/* -- Key name API tests ---------------------------------------------------------------------------------------------------------------- */
static void test_keyName (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
const char * name = "user:/tests/storage/a";
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
ssize_t nameSize = keyGetNameSize (found);
succeed_if (elektraStrNCmp (name, keyName (found), nameSize) == 0, "wrong Key name");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keySetName (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, "user:/tests/storage/b", 0);
succeed_if (found, "did not find key");
Key * duplicate = keyDup (found, KEY_CP_ALL);
keySetName (duplicate, "user:/tests/storage/z");
keySetString (duplicate, "zzz");
KeySet * expected = metaTestKeySet ();
compare_keyset (ks, expected);
ksDel (expected);
keyDel (duplicate);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyGetBaseName (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
const char * name = "user:/tests/storage/a";
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
const char * constBaseName = "a";
size_t constBaseNameSize = elektraStrLen (constBaseName);
ssize_t baseNameSize = keyGetBaseNameSize (found);
char * baseName = elektraMalloc (baseNameSize);
ssize_t ret = keyGetBaseName (found, baseName, baseNameSize);
if (ret < 1)
{
yield_error ("Key base name NULL or size error");
}
else
{
succeed_if ((size_t) ret == elektraStrLen (constBaseName), "Key base name has wrong size");
}
succeed_if (elektraStrNCmp (constBaseName, baseName, constBaseNameSize) == 0, "Key base name is wrong");
elektraFree (baseName);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
/* -- Key value API tests --------------------------------------------------------------------------------------------------------------- */
static void test_keyValue (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user:/tests/storage/specialkey";
size_t valueSize = 42;
void * value = elektraMalloc (valueSize);
memset (value, 42, valueSize);
Key * key = keyNew (name, KEY_END);
keySetBinary (key, value, valueSize);
ksAppendKey (ks, keyDup (key, KEY_CP_ALL));
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
compare_key (key, found);
elektraFree (value);
keyDel (parentKey);
ksDel (ks);
keyDel (key);
closeStoragePlugin (storagePlugin);
}
static void test_keyString (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user:/tests/storage/specialkey";
const char * value = "special value";
size_t valueSize = elektraStrLen (value);
Key * key = keyNew (name, KEY_VALUE, value, KEY_END);
ksAppendKey (ks, key);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
succeed_if (elektraStrNCmp (value, keyString (found), valueSize) == 0, "Key string value is wrong");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyGetBinary (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user:/tests/storage/specialkey";
size_t realValueSize = 42;
void * value = elektraMalloc (realValueSize);
memset (value, 42, realValueSize);
Key * key = keyNew (name, KEY_END);
keySetBinary (key, value, realValueSize);
ksAppendKey (ks, key);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
ssize_t apiValueSize = keyGetValueSize (found);
char * apiValue = elektraMalloc (apiValueSize);
succeed_if (keyGetBinary (found, apiValue, apiValueSize) == (ssize_t) realValueSize, "Key binary has wrong size");
succeed_if (elektraStrNCmp (value, apiValue, realValueSize) == 0, "Key binary value is wrong");
elektraFree (apiValue);
elektraFree (value);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keyGetString (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user:/tests/storage/specialkey";
const char * value = "special value";
size_t realValueSize = elektraStrLen (value);
Key * key = keyNew (name, KEY_VALUE, value, KEY_END);
ksAppendKey (ks, key);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
ssize_t apiValueSize = keyGetValueSize (found);
char * apiString = elektraMalloc (apiValueSize);
succeed_if (keyGetString (found, apiString, apiValueSize) == (ssize_t) realValueSize, "Key string has wrong size");
succeed_if (elektraStrNCmp (value, apiString, realValueSize) == 0, "Key string value is wrong");
elektraFree (apiString);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keySetBinary (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user:/tests/storage/specialkey";
size_t realValueSize = 42;
void * value = elektraMalloc (realValueSize);
memset (value, 42, realValueSize);
Key * key = keyNew (name, KEY_END);
keySetBinary (key, value, realValueSize);
ksAppendKey (ks, key);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, KDB_O_POP);
succeed_if (found, "did not find key");
// now set a new key value to the Key _after_ kdbGet
size_t newValueSize = 4096;
void * newValue = elektraMalloc (newValueSize);
memset (newValue, 253, newValueSize);
succeed_if (keySetBinary (found, newValue, newValueSize) == (ssize_t) newValueSize, "Key binary could not be set");
ksAppendKey (ks, found);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
ssize_t apiValueSize = keyGetValueSize (found);
char * apiValue = elektraMalloc (apiValueSize);
succeed_if (keyGetBinary (found, apiValue, apiValueSize) == (ssize_t) newValueSize, "Key binary has wrong size");
succeed_if (elektraStrNCmp (value, apiValue, realValueSize) != 0, "Key binary value is wrong");
succeed_if (elektraStrNCmp (newValue, apiValue, newValueSize) == 0, "Key binary value is wrong");
elektraFree (newValue);
elektraFree (apiValue);
elektraFree (value);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void test_keySetString (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user:/tests/storage/specialkey";
const char * value = "special value";
size_t realValueSize = elektraStrLen (value);
Key * key = keyNew (name, KEY_VALUE, value, KEY_END);
ksAppendKey (ks, key);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, KDB_O_POP);
succeed_if (found, "did not find key");
// now set a new key string to the Key _after_ kdbGet
const char * newValue = "some new special value";
size_t newValueSize = elektraStrLen (newValue);
succeed_if (keySetString (found, newValue) == (ssize_t) newValueSize, "Key string could not be set");
ksAppendKey (ks, found);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
ssize_t apiValueSize = keyGetValueSize (found);
char * apiValue = elektraMalloc (apiValueSize);
succeed_if (keyGetString (found, apiValue, apiValueSize) == (ssize_t) newValueSize, "Key string has wrong size");
succeed_if (elektraStrNCmp (value, apiValue, realValueSize) != 0, "Key string value is wrong");
succeed_if (elektraStrNCmp (newValue, apiValue, newValueSize) == 0, "Key string value is wrong");
elektraFree (apiValue);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
static void clearStorage (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
/* -- Main ------------------------------------------------------------------------------------------------------------------------------ */
int main (int argc, char ** argv)
{
printf ("STORAGE TESTS\n");
printf ("==================\n\n");
init (argc, argv);
initPlugins ();
for (size_t plugin = 0; plugin < numPlugins; ++plugin)
{
const char * tmpFile = elektraFilename ();
printf ("Testing plugin %s\n", pluginNames[plugin]);
fprintf (stdout, "Tmp-file: %s\n", tmpFile);
// KeySet API tests
clearStorage (plugin, tmpFile);
test_ksDupFun (plugin, tmpFile, ksDup);
clearStorage (plugin, tmpFile);
test_ksDupFun (plugin, tmpFile, ksDeepDup);
clearStorage (plugin, tmpFile);
test_ksCopy (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_ksGetSize (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_double_get (plugin, tmpFile); // regression test
clearStorage (plugin, tmpFile);
test_ksAppendKey (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_ksAppend (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_ksCut (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_ksPop (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_ksLookup (plugin, tmpFile, 0);
clearStorage (plugin, tmpFile);
test_ksLookup (plugin, tmpFile, KDB_O_POP);
clearStorage (plugin, tmpFile);
test_ksLookupByName (plugin, tmpFile, 0);
clearStorage (plugin, tmpFile);
test_ksLookupByName (plugin, tmpFile, KDB_O_POP);
// Key API tests
clearStorage (plugin, tmpFile);
test_keyFlags (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyDup (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyCopy_newKey (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyCopy_clearOverwriteKey (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyDel (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyClear (plugin, tmpFile);
// Key Name API tests
clearStorage (plugin, tmpFile);
test_keyName (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keySetName (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyGetBaseName (plugin, tmpFile);
// Key Value API tests
clearStorage (plugin, tmpFile);
test_keyValue (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyString (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyGetBinary (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keyGetString (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keySetBinary (plugin, tmpFile);
clearStorage (plugin, tmpFile);
test_keySetString (plugin, tmpFile);
}