forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestabi_meta.c
653 lines (498 loc) · 25 KB
/
testabi_meta.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
/**
* @file
*
* @brief Test suite for meta information.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <tests.h>
static void test_basic (void)
{
Key * key;
key = keyNew ("user:/key_with_meta", KEY_END);
exit_if_fail (key, "could not create new key");
succeed_if (keyGetMeta (key, "hello") == 0, "hello was not set up to now");
succeed_if (keyGetMeta (key, "error") == 0, "hello was not set up to now");
keySetMeta (key, "hello", "hello_world");
succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "hello_world");
keySetMeta (key, "mode", "0644");
succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "hello_world");
keySetMeta (key, "time", "1271234264");
succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "hello_world");
succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0644");
succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271234264");
keySetMeta (key, "hello", "between");
succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "between");
keySetMeta (key, "hello", 0);
succeed_if (keyValue (keyGetMeta (key, "hello")) == 0, "could not remove metadata");
keySetMeta (key, "hello", "goodbye");
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "goodbye"), "could not set meta information again (2x)");
keySetMeta (key, "empty", "");
succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
keySetMeta (key, "owner", "hugo");
succeed_if_same_string (keyValue (keyGetMeta (key, "owner")), "hugo");
keySetMeta (key, "mode", "775");
succeed_if_same_string (keyValue (keyGetMeta (key, "owner")), "hugo");
succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "775");
keySetMeta (key, "", "empty");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "empty");
keySetMeta (key, "", "");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "");
keySetMeta (key, "", 0);
succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
keyDel (key);
}
static void test_null_pointer (void)
{
Key * key;
key = keyNew ("user:/test1", KEY_END);
exit_if_fail (key, "could not create new key");
succeed_if (keyRewindMeta (0) == -1, "Could rewind NULL Key");
succeed_if (keyGetMeta (0, "test") == 0, "Could get meta of NULL Key");
succeed_if (keyGetMeta (key, 0) == 0, "Could get meta of NULL metaName");
succeed_if (keyMeta (0) == 0, "Could get metadata of NULL Key");
succeed_if (keyCurrentMeta (0) == 0, "Could get current meta Key of NULL key");
succeed_if (keyNextMeta (0) == 0, "Could get next meta Key of NULL key");
succeed_if (keyCopyMeta (0, key, "test") == -1, "Could copy metadata to NULL Key");
succeed_if (keyCopyMeta (key, 0, "test") == -1, "Could copy metadata from NULL Key");
succeed_if (keyCopyAllMeta (0, key) == -1, "Could copy all metadata to NULL Key");
succeed_if (keyCopyAllMeta (key, 0) == -1, "Could copy all metadata from NULL Key");
succeed_if (keySetMeta (0, "test", "test"), "Could set metadata to NULL Key");
succeed_if (keySetMeta (key, 0, "test"), "Could set metadata with NULL metaName");
keyDel (key);
}
static void test_iterate (void)
{
Key * key;
key = keyNew ("user:/test", KEY_END);
exit_if_fail (key, "could not create new key");
succeed_if (keyRewindMeta (key) == 0, "Could not rewind empty key");
succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty");
succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty");
keySetMeta (key, "meta1", "meta_value");
succeed_if (keyRewindMeta (key) == 0, "Could not rewind key");
succeed_if_same_string (keyName (keyNextMeta (key)), "meta:/meta1");
succeed_if_same_string (keyValue (keyCurrentMeta (key)), "meta_value");
succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty at 2. iteration");
succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty at 2. iteration");
succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty at 3. iteration");
succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty at 3. iteration");
succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty at 4. iteration");
succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty at 4. iteration");
keyDel (key);
}
static void test_size (void)
{
Key * key;
char * buffer;
key = keyNew ("user:/test", KEY_END);
exit_if_fail (key, "could not create new key");
succeed_if (keyValue (keyGetMeta (key, "hello")) == 0, "hello was not set up to now");
succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == -1, "got wrong size for empty metavalue");
keySetMeta (key, "hello", "hello_world");
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "hello_world"), "could not receive previously set meta information");
succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == sizeof ("hello_world"), "got wrong size");
keySetMeta (key, "mode", "0644");
keySetMeta (key, "time", "1271234264");
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "hello_world"), "meta info changed unexpectly");
succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0644");
succeed_if (keyGetValueSize (keyGetMeta (key, "mode")) == sizeof ("0644"), "got wrong size");
succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271234264");
succeed_if (keyGetValueSize (keyGetMeta (key, "time")) == sizeof ("1271234264"), "got wrong size");
keySetMeta (key, "hello", "between");
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "between"), "could not set meta information again");
succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == sizeof ("between"), "got wrong size");
buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "hello")));
succeed_if (keyGetString (keyGetMeta (key, "hello"), buffer, keyGetValueSize (keyGetMeta (key, "hello"))) ==
keyGetValueSize (keyGetMeta (key, "hello")),
"could not get meta");
succeed_if_same_string (buffer, "between");
elektraFree (buffer);
keySetMeta (key, "hello", 0);
succeed_if (keyValue (keyGetMeta (key, "hello")) == 0, "could not remove metadata");
succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == -1, "got wrong size");
keySetMeta (key, "hello", "goodbye");
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "goodbye"), "could not set meta information again (2x)");
succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == sizeof ("goodbye"), "got wrong size");
buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "hello")));
succeed_if (keyGetString (keyGetMeta (key, "hello"), buffer, keyGetValueSize (keyGetMeta (key, "hello"))) ==
keyGetValueSize (keyGetMeta (key, "hello")),
"could not get meta");
succeed_if_same_string (buffer, "goodbye");
elektraFree (buffer);
keySetMeta (key, "empty", "");
succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
succeed_if (keyGetValueSize (keyGetMeta (key, "empty")) == sizeof (""), "got wrong size");
buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "empty")));
succeed_if (keyGetString (keyGetMeta (key, "empty"), buffer, keyGetValueSize (keyGetMeta (key, "empty"))) ==
keyGetValueSize (keyGetMeta (key, "empty")),
"could not get meta");
succeed_if_same_string (buffer, "");
elektraFree (buffer);
keySetMeta (key, "", "empty");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "empty");
succeed_if (keyGetValueSize (keyGetMeta (key, "")) == sizeof ("empty"), "got wrong size");
buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "")));
succeed_if (keyGetString (keyGetMeta (key, ""), buffer, keyGetValueSize (keyGetMeta (key, ""))) ==
keyGetValueSize (keyGetMeta (key, "")),
"could not get meta");
succeed_if_same_string (buffer, "empty");
elektraFree (buffer);
keySetMeta (key, "", "");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "");
succeed_if (keyGetValueSize (keyGetMeta (key, "")) == sizeof (""), "got wrong size");
buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "")));
succeed_if (keyGetString (keyGetMeta (key, ""), buffer, keyGetValueSize (keyGetMeta (key, ""))) ==
keyGetValueSize (keyGetMeta (key, "")),
"could not get meta");
succeed_if_same_string (buffer, "");
elektraFree (buffer);
keySetMeta (key, "", 0);
succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
succeed_if (keyGetValueSize (keyGetMeta (key, "")) == -1, "got wrong size");
keyDel (key);
}
static void test_dup (void)
{
Key * key;
Key * dup;
key = keyNew ("user:/orig", KEY_END);
succeed_if (keySetMeta (key, "test", "some_meta_test") == sizeof ("some_meta_test"), "could not set meta");
succeed_if_same_string (keyValue (keyGetMeta (key, "test")), "some_meta_test");
dup = keyDup (key, KEY_CP_ALL);
succeed_if_same_string (keyValue (keyGetMeta (dup, "test")), "some_meta_test");
succeed_if (keySetMeta (dup, "test", "some_other_meta_test") == sizeof ("some_other_meta_test"), "sizeof meta test wrong");
succeed_if_same_string (keyValue (keyGetMeta (dup, "test")), "some_other_meta_test");
succeed_if_same_string (keyValue (keyGetMeta (key, "test")), "some_meta_test");
keyDel (dup);
keyDel (key);
}
Key * g_c;
static void j (Key * k)
{
size_t size = keyGetValueSize (k);
char * value = elektraMalloc (size);
int bstring = keyIsString (k);
// receive key g_c
memcpy (value, keyValue (k), size);
keyCopy (k, g_c, KEY_CP_ALL);
if (bstring)
keySetString (k, value);
else
keySetBinary (k, value, size);
elektraFree (value);
// the caller will see the changed key k
// with the metadata from g_c
}
static void l (Key * k)
{
// receive g_c
keyCopyMeta (k, g_c, "type");
// the caller will see the changed key k
// with the metadata "type" from g_c
}
static void test_examples (void)
{
Key * key;
key = keyNew ("/", KEY_END);
keySetMeta (key, "def", "abc");
keySetMeta (key, "nop", "cup");
g_c = keyNew ("/", KEY_END);
keySetMeta (g_c, "xef", "ybc");
keySetMeta (g_c, "xop", "yup");
j (key);
succeed_if_same_string (keyValue (keyGetMeta (key, "xef")), "ybc");
succeed_if_same_string (keyValue (keyGetMeta (key, "xop")), "yup");
succeed_if (keyValue (keyGetMeta (key, "def")) == 0, "old metadata remained");
succeed_if (keyValue (keyGetMeta (key, "nop")) == 0, "old metadata remained");
keyDel (key);
keyDel (g_c);
key = keyNew ("/", KEY_END);
keySetMeta (key, "def", "abc");
keySetMeta (key, "nop", "cup");
g_c = keyNew ("/", KEY_END);
keySetMeta (g_c, "type", "boolean");
keySetMeta (g_c, "xop", "yup");
l (key);
succeed_if_same_string (keyValue (keyGetMeta (key, "def")), "abc");
succeed_if_same_string (keyValue (keyGetMeta (key, "nop")), "cup");
succeed_if_same_string (keyValue (keyGetMeta (key, "type")), "boolean");
succeed_if (keyValue (keyGetMeta (key, "xop")) == 0, "this metadata was not requested to be copied");
keyDel (key);
keyDel (g_c);
}
static void test_copy (void)
{
printf ("Test key meta copy\n");
Key * key1;
Key * key2;
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keyCopyMeta (key2, key1, "nonexist") == 0, "could not do anything");
succeed_if (keyValue (keyGetMeta (key2, "nonexist")) == 0, "should not be there");
keyDel (key1);
keyDel (key2);
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if (keyCopyMeta (key2, key1, "mymeta") == 1, "could not copy metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
succeed_if (keyCopyMeta (key1, key2, "mymeta") == 1, "did nothing in the end");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
keyDel (key1);
keyDel (key2);
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if (keyCopyMeta (key2, key1, "mymeta") == 1, "could not copy metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue2") == sizeof ("a longer metavalue2"), "could not set metavalue2");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue2");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key (with another value)");
keyDel (key1);
keyDel (key2);
Key * k;
Key * c;
// clang-format off
k=keyNew ("user:/metakey",
KEY_META, "t", "test1",
KEY_META, "a", "another",
KEY_META, "cya", "see the metadata later",
KEY_META, "mode", "0775",
KEY_END);
// clang-format on
c = keyNew ("user:/metacopy", KEY_END);
succeed_if (keyGetMeta (k, "t") != 0, "could not get metakey");
succeed_if (keyGetMeta (k, "a") != 0, "could not get metakey");
succeed_if (keyGetMeta (c, "t") == 0, "could get metakey not there");
succeed_if (keyGetMeta (c, "a") == 0, "could get metakey not there");
succeed_if (keyCopyMeta (c, k, "t") == 1, "could not copy metadata");
succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
succeed_if (keyCopyMeta (c, k, "a") == 1, "could not copy metadata");
succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
keyDel (k);
keyDel (c);
// clang-format off
k=keyNew ("user:/metakey",
KEY_META, "t", "test1",
KEY_META, "a", "another",
KEY_META, "cya", "see the metadata later",
KEY_META, "mode", "0775",
KEY_END);
c=keyNew ("user:/metacopy",
KEY_META, "t", "test1",
KEY_META, "a", "wrong",
KEY_META, "old", "will stay",
KEY_END);
// clang-format on
succeed_if (keyGetMeta (k, "t") != 0, "could not get metakey");
succeed_if (keyGetMeta (k, "a") != 0, "could not get metakey");
succeed_if (keyCopyMeta (c, k, "t") == 1, "could not copy metadata");
succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
succeed_if_same_string (keyValue (keyGetMeta (k, "t")), "test1");
succeed_if_same_string (keyValue (keyGetMeta (c, "t")), "test1");
succeed_if (keyCopyMeta (c, k, "a") == 1, "could not copy metadata");
succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
succeed_if_same_string (keyValue (keyGetMeta (k, "a")), "another");
succeed_if_same_string (keyValue (keyGetMeta (c, "a")), "another");
succeed_if_same_string (keyValue (keyGetMeta (c, "old")), "will stay");
succeed_if (keyGetMeta (c, "cya") == 0, "metakey should not be there");
succeed_if (keyGetMeta (c, "mode") == 0, "metakey should not be there");
keyDel (k);
keyDel (c);
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keySetMeta (key2, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyCopyMeta (key2, key1, "mymeta") == 0, "could not copy metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == 0, "value of mymeta is not NULL");
succeed_if (keyGetMeta (key2, "mymeta") == 0, "value of mymeta has not been cleared");
keyDel (key1);
keyDel (key2);
}
static void test_new (void)
{
Key * key;
// clang-format off
key = keyNew ("user:/test",
KEY_META, "hello", "hello_world",
KEY_META, "mode", "0644",
KEY_META, "time", "1271234264",
KEY_META, "empty", "",
KEY_META, "", "empty",
KEY_END);
// clang-format on
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "hello_world"), "could not receive previously set meta information");
succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0644");
succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271234264");
succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "empty");
keySetMeta (key, "", "full");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "full");
keySetMeta (key, "", 0);
succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
keyDel (key);
// clang-format off
key = keyNew ("user:/test",
KEY_META, "hello", "goodbye",
KEY_META, "mode", "0775",
KEY_META, "time", "1271939923",
KEY_META, "empty", "",
KEY_META, "", "",
KEY_END);
// clang-format on
succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "goodbye"), "could not receive previously set meta information");
succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0775");
succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271939923");
succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "");
keySetMeta (key, "", "full");
succeed_if_same_string (keyValue (keyGetMeta (key, "")), "full");
keySetMeta (key, "", 0);
succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
keyDel (key);
}
static void test_copyall (void)
{
printf ("Test key meta copy all\n");
Key * key1;
Key * key2;
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keyCopyAllMeta (key2, key1) == 0, "could not do anything");
succeed_if (keyValue (keyGetMeta (key2, "nonexist")) == 0, "should not be there");
keyDel (key1);
keyDel (key2);
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if (keyCopyAllMeta (key2, key1) == 1, "could not copy metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
succeed_if (keyCopyAllMeta (key1, key2) == 1, "did nothing in the end");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
keyDel (key1);
keyDel (key2);
succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if (keyCopyAllMeta (key2, key1) == 1, "could not copy metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key");
succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue2") == sizeof ("a longer metavalue2"), "could not set metavalue2");
succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue2");
succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key (with another value)");
keyDel (key1);
keyDel (key2);
Key * k;
Key * c;
// clang-format off
k=keyNew ("user:/metakey",
KEY_META, "t", "test1",
KEY_META, "a", "another",
KEY_META, "cya", "see the metadata later",
KEY_META, "mode", "0775",
KEY_END);
// clang-format on
c = keyNew ("user:/metacopy", KEY_END);
succeed_if (keyGetMeta (k, "t") != 0, "could not get metakey");
succeed_if (keyGetMeta (k, "a") != 0, "could not get metakey");
succeed_if (keyGetMeta (c, "t") == 0, "could get metakey not there");
succeed_if (keyGetMeta (c, "a") == 0, "could get metakey not there");
succeed_if (keyCopyAllMeta (c, k) == 1, "could not copy metadata");
succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
succeed_if (keyGetMeta (k, "cya") == keyGetMeta (c, "cya"), "not the same metadata after copy");
succeed_if (keyGetMeta (k, "mode") == keyGetMeta (c, "mode"), "not the same metadata after copy");
succeed_if (keyValue (keyGetMeta (k, "nonexist")) == 0, "should not be there");
succeed_if (keyValue (keyGetMeta (c, "nonexist")) == 0, "should not be there");
succeed_if (keyCopyAllMeta (c, k) == 1, "could not copy metadata (again)");
succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
succeed_if (keyGetMeta (k, "cya") == keyGetMeta (c, "cya"), "not the same metadata after copy");
succeed_if (keyGetMeta (k, "mode") == keyGetMeta (c, "mode"), "not the same metadata after copy");
succeed_if (keyValue (keyGetMeta (k, "nonexist")) == 0, "should not be there");
succeed_if (keyValue (keyGetMeta (c, "nonexist")) == 0, "should not be there");
keyDel (k);
keyDel (c);
}
static void test_type (void)
{
Key * key;
succeed_if (key = keyNew ("/", KEY_END), "could not create a new key");
succeed_if (keyValue (keyGetMeta (key, "binary")) == 0, "wrong type after key creation");
succeed_if (keySetString (key, "mystring") == sizeof ("mystring"), "could not set string");
succeed_if (keyValue (keyGetMeta (key, "binary")) == 0, "wrong type after setting string");
succeed_if (keySetBinary (key, "mystring", sizeof ("mystring")) == sizeof ("mystring"), "could not set binary");
succeed_if (keyValue (keyGetMeta (key, "binary")) != 0, "wrong type after setting string");
keyDel (key);
}
static void test_keyMeta (void)
{
Key * key = keyNew ("/", KEY_END);
KeySet * meta = keyMeta (key);
succeed_if (meta, "meta keyset is null");
succeed_if (ksGetSize (meta) == 0, "meta keyset is not empty");
keyDel (key);
// clang-format off
key = keyNew ("user:/test",
KEY_META, "hello", "hello_world",
KEY_META, "mode", "0644",
KEY_META, "time", "1271234264",
KEY_META, "empty", "",
KEY_META, "", "empty",
KEY_END);
// clang-format on
meta = keyMeta (key);
const char * value = keyString (ksLookupByName (meta, "meta:/hello", 0));
succeed_if (!strcmp (value, "hello_world"), "unexpected value");
value = keyString (ksLookupByName (meta, "meta:/mode", 0));
succeed_if (!strcmp (value, "0644"), "unexpected value");
value = keyString (ksLookupByName (meta, "meta:/time", 0));
succeed_if (!strcmp (value, "1271234264"), "unexpected value");
value = keyString (ksLookupByName (meta, "meta:/empty", 0));
succeed_if (!strcmp (value, ""), "unexpected value");
value = keyString (ksLookupByName (meta, "meta:/", 0));
succeed_if (!strcmp (value, "empty"), "unexpected value");
succeed_if (ksGetSize (meta) == 5, "unexpected meta keyset size");
keyDel (key);
}
int main (int argc, char ** argv)
{
printf ("KEY META ABI TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_basic ();
test_null_pointer ();
test_iterate ();
test_size ();
test_dup ();
test_examples ();
test_type ();
test_copy ();
test_new ();
test_copyall ();
test_keyMeta ();
printf ("\ntestabi_meta RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
return nbError;
}