forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestabi_key.c
2686 lines (2119 loc) · 104 KB
/
testabi_key.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
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <tests.h>
#define NUMBER_OF_NAMESPACES 6
char * namespaces[] = { "/", "spec:/", "proc:/", "dir:/", "user:/", "system:/", 0 };
struct test
{
char * testName;
char * keyName;
char * expectedKeyName;
char * expectedBaseName;
char * expectedFRootName;
};
struct test tstKeyName[] = { { "Normal key", "system:/foo/bar", "system:/foo/bar", "bar", "system" },
{
"Key containing redundant & trailing separator", "system://foo//bar//",
"system:/foo/bar", /* keyName */
"bar", /* keyBaseName */
"system", /* keyGetFullRootName */
},
{
"Normal user key", "user:/key", "user:/key", /* keyName */
"key", /* keyBaseName */
"user", /* keyGetFullRootName */
},
{
"Key with empty part", "user:///%", "user:/%", /* keyName */
"", /* keyBaseName */
"", /* keyGetFullRootName */
},
{
"Key with escaped %", "user:///\\%", "user:/\\%", /* keyName */
"%", /* keyBaseName */
"", /* keyGetFullRootName */
},
{
"Key with multi escaped %", "user:///\\\\%", "user:/\\\\%", /* keyName */
"\\%", /* keyBaseName */
"", /* keyGetFullRootName */
},
{
NULL, NULL, NULL, /* keyName */
NULL, /* keyBaseName */
NULL, /* keyGetFullRootName */
} };
static void test_keyNewSpecial (void)
{
printf ("Test special key creation\n");
Key * k = keyNew ("/", KEY_END);
succeed_if_same_string (keyName (k), "/");
keyDel (k);
k = keyNew (0, KEY_END);
succeed_if (k == NULL, "should be invalid");
keyDel (k);
k = keyNew ("", KEY_END);
succeed_if (k == NULL, "should be invalid");
keyDel (k);
k = keyNew ("invalid", KEY_END);
succeed_if (k == NULL, "should be invalid");
keyDel (k);
k = keyNew ("other invalid", KEY_END);
succeed_if (k == NULL, "should be invalid");
keyDel (k);
k = keyNew ("system spaces", KEY_END);
succeed_if (k == NULL, "should be invalid");
keyDel (k);
k = keyNew ("system:/bin", KEY_BINARY, KEY_VALUE, "a 2d\0b", KEY_END);
succeed_if_same_string (keyValue (k), "a 2d");
succeed_if (keyGetValueSize (k) == sizeof ("a 2d"), "no KEY_SIZE given, so bin is truncated");
succeed_if (keyIsBinary (k), "not a binary key");
keyDel (k);
}
static void test_keyNewWrongName (void)
{
printf ("Test key creation with wrong names\n");
Key * k = keyNew ("systemx:/hello", KEY_END);
succeed_if (k == NULL, "key with wrong name could be created");
k = keyNew ("usx:/hello", KEY_END);
succeed_if (k == NULL, "key with wrong name could be created");
k = keyNew ("usxx:/hello", KEY_END);
succeed_if (k == NULL, "key with wrong name could be created");
k = keyNew ("abc:/hello", KEY_END);
succeed_if (k == NULL, "key with wrong name could be created");
k = keyNew ("use:/hello", KEY_END);
succeed_if (k == NULL, "key with wrong name could be created");
k = keyNew ("spexc:/hello", KEY_END);
succeed_if (k == NULL, "key with wrong name could be created");
}
static void test_keyNewSystem (void)
{
Key * key;
char array[] = "here is some data stored";
Key * k1;
Key * k2;
Key * k3;
char * getBack;
printf ("Test system key creation\n");
// Empty key
key = keyNew ("/", KEY_END);
succeed_if (key != NULL, "keyNew: Unable to create a new empty key");
succeed_if (keyDel (key) == 0, "keyDel: Unable to delete empty key");
// Key with name
key = keyNew ("system:/sw/test", KEY_END);
succeed_if (key != NULL, "keyNew: Unable to create a key with name");
succeed_if_same_string (keyName (key), "system:/sw/test");
keyCopy (key, 0, KEY_CP_NAME);
succeed_if_same_string (keyName (key), "/");
succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name");
// Key with name
key = keyNew ("system:/sw/test", KEY_END);
succeed_if (key != NULL, "keyNew: Unable to create a key with name");
succeed_if_same_string (keyName (key), "system:/sw/test");
succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name");
// Key with name + value
key = keyNew ("system:/sw/test", KEY_VALUE, "test", KEY_END);
succeed_if (key != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if_same_string (keyValue (key), "test");
succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name + value");
key = keyNew ("system:/valid/there", KEY_BINARY, KEY_SIZE, sizeof (array), KEY_VALUE, array, KEY_END);
succeed_if (key != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsBinary (key), "Could not set type to binary");
succeed_if (keyGetValueSize (key) == sizeof (array), "Value size not correct");
succeed_if (memcmp ((char *) keyValue (key), array, sizeof (array)) == 0, "could not get correct binary value");
getBack = elektraMalloc (keyGetValueSize (key));
keyGetBinary (key, getBack, keyGetValueSize (key));
succeed_if (memcmp (getBack, array, sizeof (array)) == 0, "could not get correct value with keyGetBinary");
elektraFree (getBack);
succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name + owner");
key = keyNew ("system:/", KEY_END);
succeed_if_same_string (keyName (key), "system:/");
succeed_if (keyGetNameSize (key) == 9, "empty name size");
succeed_if (keyValue (keyGetMeta (key, "owner")) == 0, "owner not null");
keyDel (key);
// testing multiple values at once
k1 = keyNew ("system:/1", KEY_VALUE, "singlevalue", KEY_END);
k2 = keyNew ("system:/2", KEY_VALUE, "myvalue", KEY_END);
k3 = keyNew ("system:/3", KEY_VALUE, "syskey", KEY_END);
succeed_if (k1 != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsString (k1), "keyNew: Default key value isn't set to string");
succeed_if_same_string (keyValue (k1), "singlevalue");
succeed_if (k2 != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsString (k2), "keyNew: Default key value isn't set to string");
succeed_if_same_string (keyValue (k2), "myvalue");
succeed_if (k3 != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsString (k3), "keyNew: Default key value isn't set to string");
succeed_if_same_string (keyValue (k3), "syskey");
succeed_if (keyDel (k1) == 0, "keyDel: Unable to delete key with name + value");
succeed_if (keyDel (k2) == 0, "keyDel: Unable to delete key with name + value");
succeed_if (keyDel (k3) == 0, "keyDel: Unable to delete key with name + value");
}
static void test_keyNewUser (void)
{
Key * k1;
Key * k2;
Key * k3;
printf ("Test user key creation\n");
// testing multiple values at once
k1 = keyNew ("user:/1", KEY_VALUE, "singlevalue", KEY_END);
k2 = keyNew ("user:/2", KEY_VALUE, "myvalue", KEY_END);
k3 = keyNew ("user:/3", KEY_VALUE, "syskey", KEY_END);
succeed_if (k1 != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsString (k1), "keyNew: Default key value isn't set to string");
succeed_if_same_string (keyValue (k1), "singlevalue");
succeed_if (k2 != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsString (k2), "keyNew: Default key value isn't set to string");
succeed_if_same_string (keyValue (k2), "myvalue");
succeed_if (k3 != NULL, "keyNew: Unable to create a key with name + value of default type");
succeed_if (keyIsString (k3), "keyNew: Default key value isn't set to string");
succeed_if_same_string (keyValue (k3), "syskey");
succeed_if (keyDel (k1) == 0, "keyDel: Unable to delete key with name + value");
succeed_if (keyDel (k2) == 0, "keyDel: Unable to delete key with name + value");
succeed_if (keyDel (k3) == 0, "keyDel: Unable to delete key with name + value");
k1 = keyNew ("invalid", KEY_END);
succeed_if (k1 == 0, "should not construct key on invalid names");
}
static void test_keyReference (void)
{
printf ("Test key reference\n");
Key * key = keyNew ("user:/key", KEY_END);
Key * c = keyNew ("user:/c", KEY_END);
Key * d;
KeySet *ks1, *ks2;
succeed_if (keyGetRef (0) == UINT16_MAX, "No error on getting refcount of NULL Key");
succeed_if (keyDecRef (0) == UINT16_MAX, "No error on decrementing NULL Key");
succeed_if (keyIncRef (0) == UINT16_MAX, "No error on incrementing NULL Key");
succeed_if (keyGetRef (key) == 0, "New created key reference");
succeed_if (keyIncRef (key) == 1, "keyIncRef return value");
succeed_if (keyGetRef (key) == 1, "After keyIncRef key reference");
succeed_if (keyIncRef (key) == 2, "keyIncRef return value");
succeed_if (keyGetRef (key) == 2, "After keyIncRef key reference");
succeed_if (keyIncRef (key) == 3, "keyIncRef return value");
succeed_if (keyGetRef (key) == 3, "After keyIncRef key reference");
succeed_if (keyIncRef (key) == 4, "keyIncRef return value");
succeed_if (keyGetRef (key) == 4, "After keyIncRef key reference");
d = keyDup (key, KEY_CP_ALL);
succeed_if (keyGetRef (d) == 0, "After keyDup key reference");
succeed_if (keyIncRef (d) == 1, "keyIncRef return value");
succeed_if (keyGetRef (key) == 4, "Reference should not change");
succeed_if (keyDecRef (d) == 0, "decrement key");
succeed_if (keyDel (d) == 0, "last keyDel d, key exist");
keyCopy (c, key, KEY_CP_ALL);
succeed_if (keyGetRef (c) == 0, "After keyCopy key reference");
succeed_if (keyIncRef (c) == 1, "keyIncRef return value");
succeed_if (keyGetRef (key) == 4, "Reference should not change");
keyCopy (c, key, KEY_CP_ALL);
succeed_if (keyGetRef (c) == 1, "After keyCopy key reference");
succeed_if (keyDecRef (c) == 0, "keyDecRef return value");
succeed_if (keyGetRef (key) == 4, "Reference should not change");
succeed_if (keyIncRef (c) == 1, "keyIncRef return value");
succeed_if (keyIncRef (c) == 2, "keyIncRef return value");
keyCopy (c, key, KEY_CP_ALL);
succeed_if (keyGetRef (c) == 2, "After keyCopy key reference");
succeed_if (keyDecRef (c) == 1, "keyDecRef return value");
succeed_if (keyDecRef (c) == 0, "keyDecRef return value");
succeed_if (keyDel (c) == 0, "could not delete copy");
succeed_if (keyGetRef (key) == 4, "After keyIncRef key reference");
succeed_if (keyDecRef (key) == 3, "keyDel return value");
succeed_if (keyDel (key) == 3, "should not do anything");
succeed_if (keyGetRef (key) == 3, "After keyIncRef key reference");
succeed_if (keyDecRef (key) == 2, "keyDel return value");
succeed_if (keyDel (key) == 2, "should not do anything");
succeed_if (keyGetRef (key) == 2, "After keyIncRef key reference");
succeed_if (keyDecRef (key) == 1, "keyDel return value");
succeed_if (keyDel (key) == 1, "should not do anything");
succeed_if (keyGetRef (key) == 1, "Should have no more reference");
succeed_if (keyDecRef (key) == 0, "last keyDel key, key exist");
succeed_if (keyDel (key) == 0, "last keyDel key, key exist");
/* From examples in ksNew () */
key = keyNew ("/", KEY_END); // ref counter 0
succeed_if (keyGetRef (key) == 0, "reference counter");
keyIncRef (key); // ref counter of key 1
succeed_if (keyGetRef (key) == 1, "reference counter");
keyDel (key); // has no effect
succeed_if (keyGetRef (key) == 1, "reference counter");
keyDecRef (key); // ref counter back to 0
succeed_if (keyGetRef (key) == 0, "reference counter");
keyDel (key); // key is now deleted
ks1 = ksNew (0, KS_END);
ks2 = ksNew (0, KS_END);
key = keyNew ("user:/key", KEY_END); // ref counter 0
succeed_if (keyGetRef (key) == 0, "reference counter");
ksAppendKey (ks1, key); // ref counter of key 1
succeed_if (keyGetRef (key) == 1, "reference counter");
ksAppendKey (ks2, key); // ref counter of key 2
succeed_if (keyGetRef (key) == 2, "reference counter");
ksDel (ks1); // ref counter of key 1
succeed_if (keyGetRef (key) == 1, "reference counter");
ksDel (ks2); // key is now deleted
key = keyNew ("/", KEY_END); // ref counter 0
succeed_if (keyGetRef (key) == 0, "reference counter");
keyIncRef (key); // ref counter of key 1
succeed_if (keyGetRef (key) == 1, "reference counter");
keyDel (key); // has no effect
succeed_if (keyGetRef (key) == 1, "reference counter");
keyIncRef (key); // ref counter of key 2
succeed_if (keyGetRef (key) == 2, "reference counter");
keyDel (key); // has no effect
succeed_if (keyGetRef (key) == 2, "reference counter");
keyDecRef (key); // ref counter of key 1
succeed_if (keyGetRef (key) == 1, "reference counter");
keyDel (key); // has no effect
succeed_if (keyGetRef (key) == 1, "reference counter");
keyDecRef (key); // ref counter is now 0
succeed_if (keyGetRef (key) == 0, "reference counter");
keyDel (key); // key is now deleted
Key * k = keyNew ("system:/proper_name", KEY_END); // ref counter = 0
succeed_if (keyGetRef (k) == 0, "ref should be zero");
KeySet * ks = ksNew (1, k, KS_END);
succeed_if (keyGetRef (k) == 1, "ref should be one");
succeed_if (keyDel (k) == 1, "key will not be deleted, because its in the keyset");
succeed_if (keyGetRef (k) == 1, "ref should be one");
succeed_if (ksDel (ks) == 0, "could not del"); // now the key will be deleted
key = keyNew ("/", KEY_END); // ref counter 0
while (keyGetRef (key) < UINT16_MAX - 1)
keyIncRef (key);
succeed_if (keyGetRef (key) == UINT16_MAX - 1, "reference counter");
succeed_if (keyIncRef (key) == UINT16_MAX, "should report error");
succeed_if (keyGetRef (key) == UINT16_MAX - 1, "reference counter");
succeed_if (keyIncRef (key) == UINT16_MAX, "should report error");
while (keyGetRef (key) > 0)
keyDecRef (key);
keyDel (key);
}
static void test_keyName (void)
{
Key * key;
char ret[1000];
size_t i;
char testName[] = "user:/name";
char testBaseName[] = "name";
#ifdef HAVE_CLEARENV
clearenv ();
#else
unsetenv ("USER");
#endif
printf ("Test Key Name\n");
key = keyNew (testName, KEY_END);
succeed_if (keyGetName (0, ret, 100) == -1, "null pointer");
succeed_if (keyGetName (key, 0, 100) == -1, "string null pointer");
succeed_if (keyGetName (key, ret, 0) == -1, "length checking");
for (i = 1; i < sizeof (testName); i++)
{
succeed_if (keyGetName (key, ret, i) == -1, "length checking too short");
}
for (i = sizeof (testName); i < sizeof (testName) * 2; i++)
{
succeed_if (keyGetName (key, ret, i) == sizeof (testName), "length checking longer");
}
succeed_if (keyGetName (key, ret, (size_t) -1) == -1, "maxSize exceeded");
keyDel (key);
succeed_if (keyName (0) == 0, "null pointer");
succeed_if (keySetName (0, ret) == -1, "Null pointer");
printf ("Test Key Base Name\n");
key = keyNew (testName, KEY_END);
succeed_if (keyGetBaseName (0, ret, 100) == -1, "null pointer");
succeed_if (keyGetBaseName (key, 0, 100) == -1, "string null pointer");
succeed_if (keyGetBaseName (key, ret, 0) == -1, "length checking");
succeed_if (keyGetBaseNameSize (0) == -1, "no error on passing NULL pointer");
for (i = 1; i < sizeof (testBaseName); i++)
{
succeed_if (keyGetBaseName (key, ret, i) == -1, "length checking too short");
}
for (i = sizeof (testBaseName); i < sizeof (testBaseName) * 2; i++)
{
succeed_if (keyGetBaseName (key, ret, i) == sizeof (testBaseName), "length checking longer");
}
succeed_if (keyGetBaseName (key, ret, (size_t) -1) == -1, "maxSize exceeded");
keyDel (key);
succeed_if (keyBaseName (0) == 0, "null pointer");
key = keyNew ("/", KEY_END);
succeed_if_same_string (keyBaseName (key), "");
succeed_if (keyGetBaseName (key, ret, 1000) == 1, "get empty name");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 0) == -1, "get empty name");
keyDel (key);
succeed_if (keySetName (0, ret) == -1, "Null pointer");
key = keyNew ("user:/", KEY_END);
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("user:/", KEY_END);
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("user://", KEY_END);
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("system:/", KEY_END);
succeed_if_same_string (keyName (key), "system:/");
succeed_if (keyGetNameSize (key) == 9, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("system:/", KEY_END);
succeed_if_same_string (keyName (key), "system:/");
succeed_if (keyGetNameSize (key) == 9, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("dir:/", KEY_END);
succeed_if_same_string (keyName (key), "dir:/");
succeed_if (keyGetNameSize (key) == 6, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("dir:/", KEY_END);
succeed_if_same_string (keyName (key), "dir:/");
succeed_if (keyGetNameSize (key) == 6, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("proc:/", KEY_END);
succeed_if_same_string (keyName (key), "proc:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("proc:/", KEY_END);
succeed_if_same_string (keyName (key), "proc:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("spec:/", KEY_END);
succeed_if_same_string (keyName (key), "spec:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("spec:/", KEY_END);
succeed_if_same_string (keyName (key), "spec:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("meta:/", KEY_END);
succeed_if_same_string (keyName (key), "meta:/");
succeed_if (keyGetNameSize (key) == 7, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("default:/", KEY_END);
succeed_if_same_string (keyName (key), "default:/");
succeed_if (keyGetNameSize (key) == 10, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("/", KEY_END);
succeed_if_same_string (keyName (key), "/");
succeed_if (keyGetNameSize (key) == 2, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
key = keyNew ("//", KEY_END);
succeed_if_same_string (keyName (key), "/");
succeed_if (keyGetNameSize (key) == 2, "name length checking");
succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
succeed_if_same_string (ret, "");
keyDel (key);
}
static void test_keyNameSlashes (void)
{
printf ("Test Slashes in Key Name\n");
char * buf;
char * getBack;
Key * key = 0;
int i;
key = keyNew ("/", KEY_END);
succeed_if (keyGetNameSize (key) == 2, "empty name size");
keyDel (key);
key = keyNew ("", KEY_END);
succeed_if (key == 0, "key should be null!");
key = keyNew ("/", KEY_END);
keySetName (key, "user:/");
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keyGetNameSize (key) == 7, "empty name size");
keySetName (key, "system:/");
succeed_if_same_string (keyName (key), "system:/");
succeed_if (keyGetNameSize (key) == 9, "empty name size");
keyDel (key);
key = keyNew ("/", KEY_END);
keySetName (key, "system:/");
succeed_if_same_string (keyName (key), "system:/");
succeed_if (keyGetNameSize (key) == 9, "empty name size");
keySetName (key, "user:/");
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keyGetNameSize (key) == 7, "empty name size");
keyDel (key);
key = keyNew ("/", KEY_END);
succeed_if (keySetName (key, "user:/") == 7, "setting user:/ generates error");
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keyGetNameSize (key) == 7, "empty name size");
keyDel (key);
key = keyNew ("/", KEY_END);
succeed_if (keySetName (key, "no") == -1, "no error code setting invalid name");
succeed_if_same_string (keyName (key), "/");
succeed_if (keyGetNameSize (key) == 2, "empty name size");
keyDel (key);
key = keyNew ("user:/noname", KEY_END);
succeed_if (keyGetNameSize (key) == 13, "size not correct after keyNew");
getBack = elektraMalloc (13);
succeed_if (keyGetName (key, getBack, 13), "could not get name");
succeed_if_same_string (getBack, "user:/noname");
elektraFree (getBack);
keySetName (key, "user:/noname");
succeed_if (keyGetNameSize (key) == 13, "size not correct after keySetName");
getBack = elektraMalloc (13);
succeed_if (keyGetName (key, getBack, 13), "could not get name");
succeed_if_same_string (getBack, "user:/noname");
elektraFree (getBack);
keySetName (key, "no");
succeed_if (keyGetNameSize (key) == 13, "size not correct after keySetName");
getBack = elektraMalloc (13);
succeed_if (keyGetName (key, getBack, 13), "could not get name");
succeed_if_same_string (getBack, "user:/noname");
elektraFree (getBack);
keyDel (key);
key = keyNew ("user:/noname", KEY_END);
keySetName (key, "user://hidden");
succeed_if_same_string (keyName (key), "user:/hidden");
succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
keySetName (key, "user:///hidden");
succeed_if_same_string (keyName (key), "user:/hidden");
succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
keySetName (key, "user:////////////////////////////////////hidden");
succeed_if_same_string (keyName (key), "user:/hidden");
succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
printf ("Test trailing Slashes in Key Name\n");
keySetName (key, "user://hidden/");
succeed_if_same_string (keyName (key), "user:/hidden");
succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
keySetName (key, "user://hidden//");
succeed_if_same_string (keyName (key), "user:/hidden");
succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
keySetName (key, "user://hidden///////");
succeed_if_same_string (keyName (key), "user:/hidden");
succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
keySetName (key, "user:/");
succeed_if_same_string (keyName (key), "user:/");
keySetName (key, "user:/");
succeed_if_same_string (keyName (key), "user:/");
keySetName (key, "user:/a");
succeed_if_same_string (keyName (key), "user:/a");
keySetName (key, "user://");
succeed_if_same_string (keyName (key), "user:/");
keySetName (key, "user://///////");
succeed_if_same_string (keyName (key), "user:/");
printf ("Test Dots in Key Name\n");
keySetName (key, "user:/hidden/.");
succeed_if_same_string (keyName (key), "user:/hidden");
keySetName (key, "user:/.");
succeed_if_same_string (keyName (key), "user:/");
keySetName (key, "user:/./hidden");
succeed_if_same_string (keyName (key), "user:/hidden");
keySetName (key, "user:/.valid/.");
succeed_if_same_string (keyName (key), "user:/.valid");
keySetName (key, "user:/./.valid");
succeed_if_same_string (keyName (key), "user:/.valid");
keySetName (key, "user:/./.valid/.");
succeed_if_same_string (keyName (key), "user:/.valid");
keySetName (key, "user:/././././.valid/././././.");
succeed_if_same_string (keyName (key), "user:/.valid");
printf ("Test Double Dots in Key Name\n");
keySetName (key, "user:/hidden/parent/..");
succeed_if_same_string (keyName (key), "user:/hidden");
keySetName (key, "user:/hidden/..");
succeed_if_same_string (keyName (key), "user:/");
keySetName (key, "user:/..");
succeed_if_same_string (keyName (key), "user:/");
keySetName (key, "user:/hidden/../..");
// printf ("Name: %s\n", keyName(key));
succeed_if_same_string (keyName (key), "user:/");
succeed_if (keySetName (key, "user:///sw/../sw//././MyApp") == sizeof ("user:/sw/MyApp"), "could not set keySet example");
// printf("%s %d\n", keyName(key), keyGetNameSize(key));
succeed_if_same_string (keyName (key), "user:/sw/MyApp");
succeed_if (keyGetNameSize (key) == sizeof ("user:/sw/MyApp"), "incorrect length for keySet example");
printf ("Test Mixed Dots and Slashes in Key Name\n");
keySetName (key, "user:/hidden/../.");
keyDel (key);
printf ("Test failure key creation\n");
key = keyNew ("invalid", KEY_END);
succeed_if (key == 0, "should be null");
succeed_if (keyDel (key) == -1, "keyDel: should fail");
key = keyNew ("nonhere/valid/there", KEY_END);
succeed_if (key == 0, "should be null");
succeed_if (keyDel (key) == -1, "keyDel: should fail");
key = keyNew ("nonhere:y/valid/there", KEY_END);
succeed_if (key == 0, "should be null");
succeed_if (keyDel (key) == -1, "keyDel: should fail");
key = keyNew ("user:/validname", KEY_END);
succeed_if (key != NULL, "keyNew: Unable to create a key with name");
succeed_if_same_string (keyName (key), "user:/validname");
keySetName (key, "user:/validname");
succeed_if_same_string (keyName (key), "user:/validname");
keySetName (key, "user:/validname\\/t");
succeed_if_same_string (keyName (key), "user:/validname\\/t");
#ifdef COMPAT
keySetName (key, "user:/validname\\");
succeed_if_same_string (keyName (key), "user:/validname\\");
#endif
keySetName (key, "user:/validname\\/");
succeed_if_same_string (keyName (key), "user:/validname\\/");
succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name");
printf ("Test key's name manipulation\n");
Key * copy = keyNew ("/", KEY_END);
for (i = 0; tstKeyName[i].testName != NULL; i++)
{
key = keyNew (tstKeyName[i].keyName, KEY_END);
succeed_if (keyGetRef (copy) == 0, "reference of copy not correct");
keyCopy (copy, key, KEY_CP_ALL);
succeed_if (keyGetRef (copy) == 0, "reference of copy not correct");
Key * dup = keyDup (key, KEY_CP_ALL);
succeed_if (keyGetRef (dup) == 0, "reference of dup not correct");
compare_key (copy, key);
compare_key (copy, dup);
compare_key (dup, key);
/* keyName */
succeed_if_same_string (keyName (key), tstKeyName[i].expectedKeyName);
succeed_if_same_string (keyName (copy), tstKeyName[i].expectedKeyName);
succeed_if_same_string (keyName (dup), tstKeyName[i].expectedKeyName);
/* keyBaseName */
succeed_if_same_string (keyBaseName (key), tstKeyName[i].expectedBaseName);
succeed_if_same_string (keyBaseName (copy), tstKeyName[i].expectedBaseName);
succeed_if_same_string (keyBaseName (dup), tstKeyName[i].expectedBaseName);
/* keyGetBaseNameSize */
size_t size = keyGetBaseNameSize (key);
succeed_if ((size == strlen (tstKeyName[i].expectedBaseName) + 1), "keyGetBaseNameSize");
/* keyGetBaseName */
size = keyGetBaseNameSize (key) + 1;
buf = elektraMalloc (size * sizeof (char));
keyGetBaseName (key, buf, size);
succeed_if_same_string (buf, tstKeyName[i].expectedBaseName);
elektraFree (buf);
/* keyGetNameSize */
size = keyGetNameSize (key);
succeed_if ((size == strlen (tstKeyName[i].expectedKeyName) + 1), "keyGetKeyNameSize");
/* keyGetName */
size = keyGetNameSize (key);
buf = elektraMalloc (size * sizeof (char));
keyGetName (key, buf, size);
succeed_if_same_string (buf, tstKeyName[i].expectedKeyName);
elektraFree (buf);
keyDel (key);
keyDel (dup);
}
keyDel (copy);
}
static void test_keyValue (void)
{
Key * key;
char ret[1000];
size_t i;
char testString[] = "teststring";
char testBinary[] = "\0tes\1tbinary";
testBinary[sizeof (testBinary) - 1] = 'T';
printf ("Test value of keys\n");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if (keyGetValueSize (key) == 1, "empty value size");
succeed_if (keySetString (key, "perfectvalue") == 13, "could not set string");
succeed_if (keyGetValueSize (key) == 13, "value size not correct");
succeed_if_same_string (keyValue (key), "perfectvalue");
succeed_if (keySetString (key, "perfectvalue") == 13, "could not re-set same string");
succeed_if_same_string (keyValue (key), "perfectvalue");
succeed_if (keySetString (key, "nearperfectvalue") == 17, "could not re-set other string");
succeed_if (keyGetValueSize (key) == 17, "value size not correct");
succeed_if_same_string (keyValue (key), "nearperfectvalue");
succeed_if (keyGetString (key, ret, keyGetValueSize (key) >= 999 ? 999 : keyGetValueSize (key)) == 17, "could not get string");
succeed_if_same_string (ret, "nearperfectvalue");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if_same_string (keyValue (key), "");
succeed_if (keyGetValueSize (key) == 1, "Empty value size problem");
succeed_if (keySetString (key, "") == 1, "could not set empty string");
succeed_if_same_string (keyValue (key), "");
succeed_if (keyGetValueSize (key) == 1, "Empty value size problem");
succeed_if (keyGetString (key, ret, 0) == -1, "Could not get empty value");
succeed_if (keyGetString (key, ret, 1) == 1, "Could not get empty value");
succeed_if (ret[0] == 0, "keyGetValue did not return empty value");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if (keySetString (key, "a long long string") == 19, "could not set string");
succeed_if (keyGetString (key, ret, 6) == -1, "string not truncated");
succeed_if (keyGetBinary (key, ret, 999) == -1, "binary not mismatch");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if (keySetBinary (key, "a", 1) == 1, "could not set binary");
succeed_if (keyIsString (key) == 0, "is not a string");
succeed_if (keyIsBinary (key) == 1, "is not a string");
succeed_if (keyGetBinary (key, ret, 1) == 1, "binary not truncated");
succeed_if (!strncmp (ret, "a", 1), "binary value wrong");
succeed_if (keyGetString (key, ret, 999) == -1, "string not mismatch");
succeed_if (keySetString (key, 0) == 1, "wrong error code for SetString");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if (keySetBinary (key, NULL, 0) == 0, "could not set null binary");
succeed_if (keyIsString (key) == 0, "is not a string");
succeed_if (keyIsBinary (key) == 1, "is not a string");
succeed_if (keyGetValueSize (key) == 0, "Empty value size problem");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if (keySetString (key, "") == 1, "could not set empty string");
succeed_if (keyIsString (key) == 1, "is not a string");
succeed_if (keyIsBinary (key) == 0, "is a binary");
succeed_if (keyGetValueSize (key) == 1, "Empty value size problem");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
succeed_if (keySetBinary (key, "a long long binary", 19) == 19, "could not set string");
succeed_if (keyIsString (key) == 0, "is not a string");
succeed_if (keyIsBinary (key) == 1, "is not a string");
succeed_if (keyGetBinary (key, ret, 6) == -1, "binary not truncated");
succeed_if (keyGetBinary (key, ret, 19) == 19, "could not get binary");
succeed_if (!strncmp (ret, "a long long binary", 19), "binary value wrong");
succeed_if (keyGetString (key, ret, 999) == -1, "string not mismatch");
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
for (i = 1; i < 255; i++)
{
ret[0] = i;
ret[1] = i;
ret[2] = 0;
// output_key (key);
succeed_if (keySetString (key, ret) == 3, "could not set string");
succeed_if_same_string (keyValue (key), ret);
}
succeed_if (keyDel (key) == 0, "could not delete key");
succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
for (i = 0; i < 255; i++)
{
ret[0] = i;
ret[1] = 255 - i;
ret[2] = i;
// output_key (key);
succeed_if (keySetBinary (key, ret, 3) == 3, "could not set string");
succeed_if (memcmp (keyValue (key), ret, 3) == 0, "String not same as set");
}
succeed_if (keyDel (key) == 0, "could not delete key");
printf ("Test string of key\n");
succeed_if (keyIsString (0) == -1, "no error on NULL key");
succeed_if (keyValue (0) == 0, "null pointer");
succeed_if (keyGetValueSize (0) == -1, "null pointer");
succeed_if (keySetString (0, "") == -1, "null pointer");
succeed_if_same_string (keyString (0), "(null)");
key = keyNew ("/", KEY_END);
succeed_if (keyGetValueSize (key) == 1, "empty value size");
succeed_if_same_string (keyString (key), "");
keySetString (key, testString);
succeed_if (keyGetString (0, ret, 100) == -1, "null pointer");
succeed_if (keyGetString (key, 0, 100) == -1, "string null pointer");
succeed_if (keyGetString (key, ret, 0) == -1, "length checking");
succeed_if_same_string (keyString (key), testString);
for (i = 1; i < sizeof (testString); i++)
{
succeed_if (keyGetString (key, ret, i) == -1, "length checking too short");
}
for (i = sizeof (testString); i < sizeof (testString) * 2; i++)
{
succeed_if (keyGetString (key, ret, i) == sizeof (testString), "length checking longer");
}
succeed_if (keyGetString (key, ret, (size_t) -1) == -1, "maxSize exceeded");
succeed_if (keySetString (key, 0) == 1, "delete string");
succeed_if (keyGetString (key, ret, i) == 1, "length checking deleting");
succeed_if_same_string (ret, "");
succeed_if (keySetString (key, testString) == sizeof (testString), "set string");
succeed_if (keyGetString (key, ret, i) == sizeof (testString), "length checking working");
succeed_if_same_string (ret, testString);
succeed_if (keySetString (key, "") == 1, "delete string");
succeed_if (keyGetString (key, ret, i) == 1, "length checking deleting");
succeed_if_same_string (ret, "");
succeed_if (keySetString (key, testString) == sizeof (testString), "set string");
succeed_if (keyGetString (key, ret, i) == sizeof (testString), "length checking working");
succeed_if_same_string (ret, testString);
succeed_if (keyGetValueSize (key) == sizeof (testString), "testString value size");
succeed_if (strncmp (keyValue (key), testString, sizeof (testString)) == 0, "testString not same");
keyDel (key);
printf ("Test binary of key\n");
succeed_if (keyValue (0) == 0, "null pointer");
succeed_if (keyGetValueSize (0) == -1, "null pointer");
succeed_if (keySetBinary (0, "", 1) == -1, "null pointer");
key = keyNew ("/", KEY_END);
succeed_if (keySetBinary (key, "", 0) == -1, "null size");
succeed_if (keySetBinary (key, "b", 0) == -1, "null size");
succeed_if (keySetBinary (key, "", SIZE_MAX) == -1, "max size");
succeed_if (keySetBinary (key, "b", SIZE_MAX) == -1, "max size");
succeed_if (keyGetValueSize (key) == 1, "empty value size");
keySetBinary (key, testBinary, sizeof (testBinary));
succeed_if (keyGetBinary (0, ret, 100) == -1, "null pointer");
succeed_if (keyGetBinary (key, 0, 100) == -1, "binary null pointer");
succeed_if (keyGetBinary (key, ret, 0) == -1, "length checking");
for (i = 1; i < sizeof (testBinary); i++)
{
succeed_if (keyGetBinary (key, ret, i) == -1, "length checking too short");
}
for (i = sizeof (testBinary); i < sizeof (testBinary) * 2; i++)
{
succeed_if (keyGetBinary (key, ret, i) == sizeof (testBinary), "length checking longer");
}
succeed_if (keyGetBinary (key, ret, (size_t) -1) == -1, "maxSize exceeded");
succeed_if (keySetBinary (key, 0, 0) == 0, "delete binary");