summaryrefslogtreecommitdiffstats
path: root/ini/ini_valueobj_ut.c
blob: 05f649b4455d76f295259889eedd535f074a0907 (plain)
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
/*
    INI LIBRARY

    Unit test for the value object.

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2010

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE
#include <errno.h>  /* for errors */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>

#include "ini_valueobj.h"
#include "ini_defines.h"
#include "config.h"
#define TRACE_HOME
#include "trace.h"

int verbose = 0;

#define VOOUT(foo) \
    do { \
        if (verbose) foo; \
    } while(0)


typedef int (*test_fn)(void);


extern void ref_array_debug(struct ref_array *ra);

static int create_comment(int i, struct ini_comment **ic)
{
    int error = EOK;
    const char *template = ";Line 0 of the value %d";
    char comment[80];
    struct ini_comment *new_ic = NULL;

    TRACE_FLOW_ENTRY();

    sprintf(comment, template, i);


    if ((error = ini_comment_create(&new_ic)) ||
        (error = ini_comment_build(new_ic, comment)) ||
        (error = ini_comment_build(new_ic, NULL)) ||
        (error = ini_comment_build(new_ic, "#This is the second line")) ||
        (error = ini_comment_build(new_ic, ";This is the third line")) ||
        (error = ini_comment_build(new_ic, ""))) {
        printf("Failed to create comment object\n");
        ini_comment_destroy(new_ic);
        return -1;
    }

    *ic = new_ic;

    TRACE_FLOW_EXIT();
    return EOK;
}

/* Save value to the file */
/* NOTE: might be moved into the API in future */
int save_value(FILE *ff, const char *key, struct value_obj *vo)
{

    int error = EOK;
    struct simplebuffer *sbobj = NULL;
    uint32_t left = 0;

    TRACE_FLOW_ENTRY();

    error = simplebuffer_alloc(&sbobj);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
        return error;
    }

    /* Serialize */
    error = value_serialize(vo, key, sbobj);
    if (error) {
        printf("Failed to serialize a value object %d.\n", error);
        simplebuffer_free(sbobj);
        return error;
    }

    /* Add CR */
    error = simplebuffer_add_cr(sbobj);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to add CR", error);
        simplebuffer_free(sbobj);
        return error;
    }

    /* Save */
    left = simplebuffer_get_len(sbobj);
    while (left > 0) {
        error = simplebuffer_write(fileno(ff), sbobj, &left);
        if (error) {
            printf("Failed to write value object %d.\n", error);
            simplebuffer_free(sbobj);
            return error;
        }
    }

    simplebuffer_free(sbobj);

    TRACE_FLOW_EXIT();
    return EOK;
}

/* Test to create value object using arrays */
int other_create_test(FILE *ff, struct value_obj **vo)
{
    int error = EOK;
    struct value_obj *new_vo = NULL;
    struct ref_array *raw_lines;
    struct ref_array *raw_lengths;
    struct ini_comment *ic = NULL;
    struct ini_comment *ic2 = NULL;
    char *val;
    const char *vallines[] = { "Domain1,",
                               "  Domain2 ,",
                               "  Domain3" };
    const char *fullstr;
    const char *expected = "Domain1,  Domain2 ,  Domain3";
    int i;
    uint32_t origin = 0;
    uint32_t line = 0;
    uint32_t len = 0;
    uint32_t expected_len = 0;


    TRACE_FLOW_ENTRY();

    /* Create a pair of arrays */
    error = value_create_arrays(&raw_lines,
                                &raw_lengths);
    if (error) {
        printf("Failed to create arrays %d.\n", error);
        return error;
    }

    for (i=0; i< 3; i++) {
        errno = 0;
        val = strdup(vallines[i]);
        if (val == NULL) {
            error = errno;
            printf("Failed to dup memory %d.\n", error);
            value_destroy_arrays(raw_lines,
                                raw_lengths);
            return error;
        }

        /* Add line to the arrays */
        error = value_add_to_arrays(val,
                                    strlen(val),
                                    raw_lines,
                                    raw_lengths);
        if (error) {
            printf("Failed to add to arrays %d.\n", error);
            value_destroy_arrays(raw_lines,
                                raw_lengths);
            return error;
        }

    }

    /* Create a comment */
    error = create_comment(1000, &ic);
    if (error) {
        printf("Failed to create comment %d.\n", error);
        value_destroy_arrays(raw_lines,
                            raw_lengths);
        return error;
    }

    /* Create object */
    error = value_create_from_refarray(raw_lines,
                                       raw_lengths,
                                       1,
                                       INI_VALUE_READ,
                                       3,
                                       70,
                                       ic,
                                       &new_vo);

    if (error) {
        printf("Failed to create comment %d.\n", error);
        value_destroy_arrays(raw_lines,
                            raw_lengths);
        ini_comment_destroy(ic);
        return error;
    }

    /* Save value to the file */
    error = save_value(ff, "baz", new_vo);
    if (error) {
        printf("Failed to save value to file %d.\n", error);
        value_destroy(new_vo);
        return error;
    }

    /* Now do assertions and modifications to the object */

    /* NOTE: Below this line do not need to free arrays or comment
     * they became internal parts of the value object
     * and will be freed as a part of it.
     */

    /* Get concatenated value */
    error = value_get_concatenated(new_vo,
                                   &fullstr);

    if (error) {
        printf("Failed to get the string %d.\n", error);
        value_destroy(new_vo);
        return error;
    }

    /* Get length of the concatenated value */
    value_get_concatenated_len(new_vo, &len);
    expected_len = strlen(expected);

    if ((len != expected_len) ||
        (strncmp(fullstr, expected, expected_len + 1) != 0)) {
        printf("The expected value is different.\n%s\n", fullstr);
        value_destroy(new_vo);
        return EINVAL;
    }

    /* Get value's origin */
    error = value_get_origin(new_vo, &origin);
    if (error) {
        printf("Failed to get origin %d.\n", error);
        value_destroy(new_vo);
        return error;
    }

    if (origin != INI_VALUE_READ) {
        printf("The expected origin is different.\n%d\n", origin);
        value_destroy(new_vo);
        return EINVAL;
    }

    /* Get value's line */
    error = value_get_line(new_vo, &line);
    if (error) {
        printf("Failed to get origin %d.\n", error);
        value_destroy(new_vo);
        return error;
    }

    if (line != 1) {
        printf("The expected line is different.\n%d\n", origin);
        value_destroy(new_vo);
        return EINVAL;
    }

    /* Get comment from the value */
    ic = NULL;
    error = value_extract_comment(new_vo, &ic);
    if (error) {
        printf("Failed to extract comment %d.\n", error);
        value_destroy(new_vo);
        return error;
    }

    if (ic == NULL) {
        printf("The expected comment to be there.\n");
        value_destroy(new_vo);
        return error;
    }

    VOOUT(ini_comment_print(ic, stdout));

    /* Get comment again */
    ic2 = NULL;
    error = value_extract_comment(new_vo, &ic2);
    if (error) {
        printf("Failed to extract comment %d.\n", error);
        value_destroy(new_vo);
        ini_comment_destroy(ic);
        return error;
    }

    if (ic2 != NULL) {
        printf("The expected NO comment to be there.\n");
        value_destroy(new_vo);
        ini_comment_destroy(ic);
        /* No free for ic2 since it is the same object */

        /* But this should not happen anyways -
         * it will be coding error.
         */
        return EINVAL;
    }

    /* Put comment back */
    error = value_put_comment(new_vo, ic);
    if (error) {
        printf("Failed to put comment back %d.\n", error);
        value_destroy(new_vo);
        ini_comment_destroy(ic);
        return error;
    }

    /* Save value to the file */
    error = save_value(ff, "bar", new_vo);
    if (error) {
        printf("Failed to save value to file %d.\n", error);
        value_destroy(new_vo);
        return error;
    }

    *vo = new_vo;

    TRACE_FLOW_EXIT();
    return EOK;
}

/* Modify the value object */
int modify_test(FILE *ff, struct value_obj *vo)
{
    int error = EOK;
    const char *strval = "Domain100, Domain200, Domain300";

    TRACE_FLOW_ENTRY();


    /* Update key length */
    error = value_set_keylen(vo, strlen("foobar"));
    if (error) {
        printf("Failed to change key length %d.\n", error);
        return error;
    }

    /* Update value */
    error = value_update(vo,
                         strval,
                         strlen(strval),
                         INI_VALUE_CREATED,
                         10);
    if (error) {
        printf("Failed to update value %d.\n", error);
        return error;
    }


    /* Save value to the file */
    error = save_value(ff, "foobar", vo);
    if (error) {
        printf("Failed to save value to file %d.\n", error);
        return error;
    }

    TRACE_FLOW_EXIT();
    return EOK;
}


int vo_basic_test(void)
{
    int error = EOK;
    const char *strvalue = "Test multi_word_value_that_will_"
                           "be_split_between_several_lines_!";

    /* Other testing can be done with the following string:
     * const char *strvalue = "Test multi word value that "
     *                        "will be split between several lines";
     */

    struct value_obj *vo = NULL;
    uint32_t wrap = 0;
    struct ini_comment *ic = NULL;
    FILE *ff = NULL;

    TRACE_FLOW_ENTRY();

    errno = 0;
    ff = fopen("test.ini","wt");
    if (ff == NULL) {
        error = errno;
        printf("Failed to open file. Error %d.\n", error);
        return error;
    }


    for (wrap = 0; wrap < 80; wrap++) {

        ic = NULL;
        error = create_comment(wrap, &ic);
        if (error) {
            printf("Failed to create a new comment object %d.\n", error);
            fclose(ff);
            return error;
        }

        vo = NULL;
        error = value_create_new(strvalue,
                                 strlen(strvalue),
                                 INI_VALUE_CREATED,
                                 3,
                                 wrap,
                                 ic,
                                 &vo);
        if (error) {
            printf("Failed to create a new value object %d.\n", error);
            ini_comment_destroy(ic);
            fclose(ff);
            return error;
        }

        error = save_value(ff, "key", vo);
        if (error) {
            printf("Failed to save value to file %d.\n", error);
            value_destroy(vo);
            fclose(ff);
            return error;
        }

        value_destroy(vo);
    }

    vo = NULL;

    /* Run other create test here */
    error = other_create_test(ff, &vo);
    if (error) {
        printf("Create test failed %d.\n", error);
        fclose(ff);
        return error;
    }

    /* Run modify test here */
    error = modify_test(ff, vo);
    if (error) {
        printf("Modify test failed %d.\n", error);
        fclose(ff);
        value_destroy(vo);
        return error;
    }

    value_destroy(vo);


    ic = NULL;
    error = create_comment(100, &ic);
    if (error) {
        printf("Failed to create a new comment object %d.\n", error);
        fclose(ff);
        return error;
    }

    ini_comment_print(ic, ff);

    ini_comment_destroy(ic);

    fclose(ff);

    TRACE_FLOW_EXIT();
    return EOK;
}

int vo_copy_test(void)
{
    int error = EOK;
    const char *strvalue = "Test multi word value that "
                           "will be split between several lines";

    struct value_obj *vo = NULL;
    struct value_obj *vo_copy = NULL;
    uint32_t wrap = 0;
    struct ini_comment *ic = NULL;
    FILE *ff = NULL;
    char comment[100];

    TRACE_FLOW_ENTRY();

    VOOUT(printf("Copy test\n"));

    errno = 0;
    ff = fopen("test.ini","a");
    if (ff == NULL) {
        error = errno;
        printf("Failed to open file. Error %d.\n", error);
        return error;
    }

    error = ini_comment_create(&ic);
    if (error) {
        printf("Failed to create comment object\n");
        fclose(ff);
        return -1;
    }

    error = ini_comment_append(ic, "#This is a copy test!");
    if (error) {
        printf("Failed to add a line to the comment %d.\n", error);
        ini_comment_destroy(ic);
        fclose(ff);
        return error;
    }

    error = ini_comment_append(ic, "#Replacable comment line");
    if (error) {
        printf("Failed to add a line to the comment %d.\n", error);
        ini_comment_destroy(ic);
        fclose(ff);
        return error;
    }

    error = value_create_new(strvalue,
                             strlen(strvalue),
                             INI_VALUE_CREATED,
                             3,
                             20,
                             ic,
                             &vo);
    if (error) {
        printf("Failed to create a new value object %d.\n", error);
        ini_comment_destroy(ic);
        fclose(ff);
        return error;
    }

    error = save_value(ff, "key", vo);
    if (error) {
        printf("Failed to save value to file %d.\n", error);
        value_destroy(vo);
        fclose(ff);
        return error;
    }

    for (wrap = 0; wrap < 80; wrap++) {

        TRACE_INFO_NUMBER("Iteration:", wrap);

        vo_copy = NULL;
        error = value_copy(vo, &vo_copy);
        if (error) {
            printf("Failed to create a new value object %d.\n", error);
            value_destroy(vo);
            fclose(ff);
            return error;
        }

        error = value_set_boundary(vo_copy, wrap);
        if (error) {
            printf("Failed to set boundary %d.\n", error);
            value_destroy(vo);
            value_destroy(vo_copy);
            fclose(ff);
            return error;
        }

        /* Get comment from the value */
        ic = NULL;
        error = value_extract_comment(vo_copy, &ic);
        if (error) {
            printf("Failed to extract comment %d.\n", error);
            value_destroy(vo);
            value_destroy(vo_copy);
            fclose(ff);
            return error;
        }

        /* Replace comment in the value */
        sprintf(comment, ";This is value with boundary %d", wrap);
        VOOUT(printf("Comment: %s\n", comment));
        error = ini_comment_replace(ic, 1, comment);
        if (error) {
            printf("Failed to replace comment %d.\n", error);
            value_destroy(vo);
            value_destroy(vo_copy);
            fclose(ff);
            return error;
        }

        /* Set comment into the value */
        error = value_put_comment(vo_copy, ic);
        if (error) {
            printf("Failed to set comment %d.\n", error);
            value_destroy(vo);
            value_destroy(vo_copy);
            fclose(ff);
            return error;
        }

        error = save_value(ff, "key", vo_copy);
        if (error) {
            printf("Failed to save value to file %d.\n", error);
            value_destroy(vo);
            value_destroy(vo_copy);
            fclose(ff);
            return error;
        }

        value_destroy(vo_copy);
    }

    value_destroy(vo);
    fclose(ff);
    TRACE_FLOW_EXIT();
    return EOK;
}

int vo_show_test(void)
{
    VOOUT(system("cat test.ini"));
    return EOK;
}

int vo_mc_test(void)
{
    int error = EOK;
    struct value_obj *vo1 = NULL;
    struct value_obj *vo2 = NULL;
    struct ini_comment *ic = NULL;

    TRACE_FLOW_ENTRY();

    VOOUT(printf("<=== Merge Comment Test ===>\n"));

    error = create_comment(1, &ic);
    if (error) {
        printf("Failed to create a new comment object %d.\n", error);
        return error;
    }

    error = value_create_new("test1",
                             5,
                             INI_VALUE_CREATED,
                             3,
                             80,
                             ic,
                             &vo1);
    if (error) {
        printf("Failed to create the first value object %d.\n", error);
        ini_comment_destroy(ic);
        return error;
    }

    error = create_comment(2, &ic);
    if (error) {
        printf("Failed to create a new comment object %d.\n", error);
        value_destroy(vo1);
        return error;
    }

    error = value_create_new("test2",
                             5,
                             INI_VALUE_CREATED,
                             3,
                             80,
                             ic,
                             &vo2);
    if (error) {
        printf("Failed to create the second value object %d.\n", error);
        ini_comment_destroy(ic);
        value_destroy(vo1);
        return error;
    }

    /* Merge comment from one value into another */
    error = value_merge_comment(vo2, vo1);
    if (error) {
        printf("Failed to merge comments %d.\n", error);
        value_destroy(vo1);
        value_destroy(vo2);
        return error;
    }

    value_destroy(vo2);

    VOOUT(printf("<=== Key ===>\n"));
    VOOUT(value_print("key", vo1));

    error = value_extract_comment(vo1, &ic);
    if (error) {
        printf("Failed to extract comment %d.\n", error);
        value_destroy(vo1);
        return error;
    }

    value_destroy(vo1);

    VOOUT(printf("<=== Comment ===>\n"));
    VOOUT(ini_comment_print(ic, stdout));
    ini_comment_destroy(ic);

    TRACE_FLOW_EXIT();
    return EOK;
}


/* Main function of the unit test */
int main(int argc, char *argv[])
{
    int error = 0;
    test_fn tests[] = { vo_basic_test,
                        vo_copy_test,
                        vo_show_test,
                        vo_mc_test,
                        NULL };
    test_fn t;
    int i = 0;
    char *var;

    if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
    else {
        var = getenv("COMMON_TEST_VERBOSE");
        if (var) verbose = 1;
    }

    VOOUT(printf("Start\n"));

    while ((t = tests[i++])) {
        error = t();
        if (error) {
            VOOUT(printf("Failed with error %d!\n", error));
            return error;
        }
    }

    VOOUT(printf("Success!\n"));
    return 0;
}