summaryrefslogtreecommitdiffstats
path: root/test/vformat.c
blob: 40c299e26fff3034549dd193bc7b5e23be89454d (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
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
/******************************************************************************

    (C) Nick Marley, 2001 -

    This software is distributed under the GNU Lesser General Public Licence.
    Please read and understand the comments at the top of vf_iface.h before use!

FILE
    $Workfile$
    $Revision: 1.18 $
    $Author: tilda $
         
ORIGINAL AUTHOR
    Nick Marley

DESCRIPTION
    Platform independant test harness for the vformat library functions.  This
    code runs various tests on the library, results are written to stdout.

REFERENCES
    (none)    

MODIFICATION HISTORY
 *  $Log: vformat.c,v $
 *  Revision 1.18  2002/11/16 13:33:18  tilda
 *  IID639288 - Implement method for adding subobjects.
 *
 *  Revision 1.17  2002/11/15 09:14:59  tilda
 *  IID638823 - Various portability issues.
 *
 *  Revision 1.16  2002/11/14 20:14:25  tilda
 *  Minot tidy ups to WIN32 versions.
 *
 *  Revision 1.15  2002/11/11 17:23:08  monos
 *  run testing harness on linux systems
 *
 *  Revision 1.14  2002/11/03 18:56:45  tilda
 *  Add test functions for VCAL encoding functions.
 *
 *  Revision 1.13  2002/11/02 18:29:26  tilda
 *  IID485157 - UI does character conversion based on CHARSET property.
 *
 *  Revision 1.12  2002/11/02 09:03:45  tilda
 *  Start of internationalisation changes.
 *
 *  Revision 1.11  2001/11/18 22:05:57  tilda
 *  Correct args for vf_get_property().
 *
 *  Revision 1.10  2001/10/24 20:20:27  tilda
 *  uit printing when we're done.
 *
 *  Revision 1.9  2001/10/24 18:40:40  tilda
 *  BASE64 bugfixes.  Split reader/writer code. Start create/modify work.
 *
 *  Revision 1.8  2001/10/24 05:33:59  tilda
 *  Test harness / file list mods
 *
 *  Revision 1.7  2001/10/16 05:50:53  tilda
 *  Debug support for lists of vobjects from single file (ie. a phonebook).
 *  
 *  Revision 1.6  2001/10/14 20:42:37  tilda
 *  Addition of group searching.
 *  
 *  Revision 1.5  2001/10/14 19:53:36  tilda
 *  Group handling.  NO group searching functions.
 *  
 *  Revision 1.4  2001/10/13 14:58:56  tilda
 *  Tidy up version headers, add vf_strings.h where needed.
 *  
 *  Revision 1.3  2001/10/12 19:02:15  tilda
 *  Moved generic test functions to new file
 * 
 ******************************************************************************/

#ifndef NORCSID
static const char vformat_c_vss_id[] = "$Header: /cvsroot/vformat/build/vformat/vformat.c,v 1.18 2002/11/16 13:33:18 tilda Exp $";
#endif

/*============================================================================*
 ANSI C & System-wide Header Files
 *============================================================================*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*============================================================================*
 Interface Header Files
 *============================================================================*/

#include "vformat/vf_iface.h"

/*============================================================================*
 Local Header File
 *============================================================================*/

#include "testsuppt.h"

/*============================================================================*
 Private Defines
 *============================================================================*/

#define LINELENGTH      (76)

#define MAXBASE64ENC    (1000)

/*============================================================================*
 Private Data Types
 *============================================================================*/
/* None */

/*============================================================================*
 Private Function Prototypes
 *============================================================================*/

static void read_cards_from_dir(const char *dirname, const char *p_pattern);
static void test_read_vcard(const char *filename);
static bool_t read_show_status(VF_OBJECT_T **pp_object, const char *p_name);
static bool_t write_show_status(const char *p_name, VF_OBJECT_T *p_object);
static void update_results(bool_t pass, bool_t warning);
static void do_title_line(const char *filename);
static void print_search_results(VF_PROP_T *p_tmp);
static void check_extract_fields(const char *filename, const char *p_array[3]);
static void check_extract_groups(const char *filename, const char *p_group);
static void check_base64_enc(bool_t rdata);
static bool_t encode_decode_check(const char *p_data, uint32_t length);
static void check_vcal_functions(void);
static void get_random_date(VF_ISO8601_PERIOD_T *p_period);
static void get_out_file(char *, const char *, const char *);

/*============================================================================*
 Private Data
 *============================================================================*/

static int tot_tests = 0;
static int tot_errors = 0;
static int tot_warnings = 0;

static const char *pp_tel_fields[3] =
{
    VFP_TELEPHONE,
    VFP_WORK,
    VFP_PREFERRED
};


/*============================================================================*
 Public Function Implementations
 *============================================================================*/


/*----------------------------------------------------------------------------*
 * NAME
 *      main()
 * 
 * DESCRIPTION
 *      Entry point.
 *
 * RETURNS
 *      Hopefully.
 *----------------------------------------------------------------------------*/

int main(int argc, char **argv)
{
    /*
     * simple reading / writing tests.
     */
    read_cards_from_dir("tests//spec", "*.vcf");
    read_cards_from_dir("tests//adhoc", "*.vcf");  
    read_cards_from_dir("tests//utf-8", "*.vcf");

    /*
     * list access functions.
     */
    check_extract_fields("tests//access//access_1.vcf", pp_tel_fields);
    check_extract_groups("tests//adhoc//group_7.vcf", "ALPHA");
  
    /*
     * data format tests.
     */
    check_base64_enc(TRUE);
  
    /*
     * VCAL access functions
     */
    check_vcal_functions(); 

    /* split_phonebook("C:\\Windows\\Desktop\\Whole Phonebook.vcf", "C:\\Temp");  */


    /* for automatic testing */
    if (tot_errors) exit (1);
    exit (0);
}



/*============================================================================*
 Private Function Implementations
 *============================================================================*/


/*----------------------------------------------------------------------------*
 * NAME
 *      main()
 * 
 * DESCRIPTION
 *      Entry point.
 *
 * RETURNS
 *      Hopefully.
 *----------------------------------------------------------------------------*/

void read_cards_from_dir(
    const char *p_dirname,
    const char *p_pattern
    )
{
    file_enum_t f;
    char filename[_MAX_PATH];

    if (find_first_file(filename, &f, p_dirname, p_pattern))
    {
        do
        {
            test_read_vcard(filename);
        }
        while (find_next_file(filename, &f))
            ;

        close_file_find(&f);
    }
}






/*----------------------------------------------------------------------------*
 * NAME
 *      test_read_vcard()
 * 
 * DESCRIPTION
 *      Attempt to read the indicated card.  If the read succeeds, then write
 *      the card out to a new file.  Compare the original file with the new
 *      files - they will be the same if the file was in exactly the format
 *      produced by this library (line lengths & ends may differ of course as
 *      well as choice of characters escaped in quoted printable format).
 *
 *      We then read in "our" version of the test file and write it out. This
 *      time, the output should be the same as the input.  Well - for the
 *      purposes of this test I consider it an error if they differ!
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void test_read_vcard(
    const char *filename
    )
{
    VF_OBJECT_T *p_object = NULL;
    bool_t warning = FALSE, pass = FALSE;

    do_title_line(filename);

    if (read_show_status(&p_object, filename))
    {
        VF_OBJECT_T *p_object_1 = NULL;
        char outfile_1[_MAX_PATH];
        char outfile_2[_MAX_PATH];

        get_out_file(outfile_1, ".out-1", filename);
        get_out_file(outfile_2, ".out-2", filename);

        write_show_status("vobject.vcf", p_object);

        write_show_status(outfile_1, p_object);

        if (0 == filecomp(outfile_1, filename))
        {
            printf("    OK - no differences after 1st read/write\n");
        }
        else
        {
            printf("    Warning - differences after 1st read/write\n");

            warning = TRUE;
        }

        if (read_show_status(&p_object_1, outfile_1))
        {
            write_show_status(outfile_2, p_object_1);
        }

        if (0 == filecomp(outfile_2, outfile_1))
        {
            printf("    OK - no differences after 2nd read/write\n");

            pass = TRUE;
        }
        else
        {
            printf("    Error - differences after 2nd read/write\n");
        }

        vf_delete_object(p_object, TRUE);
        vf_delete_object(p_object_1, TRUE);
    }

    update_results(pass, warning);
}





/*----------------------------------------------------------------------------*
 * NAME
 *      do_title_line()
 * 
 * DESCRIPTION
 *      Print a title line.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void do_title_line(const char *filename)
{
    char title[LINELENGTH];

    memset(title, '-', sizeof(title));
    title[sizeof(title) - 1] = '\0';
    memcpy(title + 2, filename, strlen(filename));
    printf("\n%s\n", title);
}







/*----------------------------------------------------------------------------*
 * NAME
 *      read_show_status()
 * 
 * DESCRIPTION
 *      Wrapper on vf_read_file() which displays status after the read.
 *
 * RETURNS
 *      Same as wrapped vf_read_file().
 *----------------------------------------------------------------------------*/

static bool_t read_show_status(
    VF_OBJECT_T **pp_object,
    const char *p_name
    )
{
    bool_t ret;

    ret = vf_read_file(pp_object, p_name);

    if (ret)
    {
        printf("Successfully read file '%s'\n", p_name);
    }
    else
    {
        printf("Failed to read file '%s'\n", p_name);
    }

    return ret;
}




/*----------------------------------------------------------------------------*
 * NAME
 *      write_show_status()
 * 
 * DESCRIPTION
 *      Wrapper on vf_write_file() which displays status after the read.
 *
 * RETURNS
 *      Same as wrapped vf_read_file().
 *----------------------------------------------------------------------------*/

static bool_t write_show_status(
    const char *p_name,
    VF_OBJECT_T *p_object
    )
{
    bool_t ret;

    ret = vf_write_file(p_name, p_object, TRUE);

    if (ret)
    {
        printf("Successfully wrote file '%s'\n", p_name);
    }
    else
    {
        printf("Failed to wrote file '%s'\n", p_name);
    }

    return ret;
}



/*----------------------------------------------------------------------------*
 * NAME
 *      write_show_status()
 * 
 * DESCRIPTION
 *      Wrapper on vf_write_file() which displays status after the read.
 *
 * RETURNS
 *      Same as wrapped vf_read_file().
 *----------------------------------------------------------------------------*/

static void update_results(
    bool_t pass,
    bool_t warning
    )
{
    char buffer[80];

    tot_tests += 1;

    if (pass)
    {
        /* OK */
    }
    else
    {
        tot_errors += 1;
    }

    if (warning)
    {
        tot_warnings += 1;
    }

    sprintf(buffer, "[%d tests, %d errors, %d warnings]",
        tot_tests, tot_errors, tot_warnings);

    printf("%*s\n", LINELENGTH-1, buffer);
}






/*----------------------------------------------------------------------------*
 * NAME
 *      check_extract_fields()
 * 
 * DESCRIPTION
 *      Exract fields.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void check_extract_fields(
    const char *filename,
    const char *p_array[3]
    )
{
    VF_OBJECT_T *p_object = NULL;
    bool_t warning = FALSE, pass = FALSE;

    do_title_line(filename);

    if (read_show_status(&p_object, filename))
    {
        VF_PROP_T *p_tmp;

        printf(" Looking for {%s}...\n", p_array[0]);
        if (vf_get_property(&p_tmp, p_object, VFGP_FIND, NULL, p_array[0], NULL))
        {
            print_search_results(p_tmp);
        }

        printf(" Looking for {%s, %s}...\n", p_array[0], p_array[1]);
        if (vf_get_property(&p_tmp, p_object, VFGP_FIND, NULL, p_array[0], p_array[1], (void *) NULL))
        {
            print_search_results(p_tmp);
        }

        printf(" Looking for {%s, %s}...\n", p_array[0], p_array[2]);
        if (vf_get_property(&p_tmp, p_object, VFGP_FIND, NULL, p_array[0], p_array[2], (void *) NULL))
        {
            print_search_results(p_tmp);
        }

        printf(" Looking for {%s, %s, %s}...\n", p_array[0], p_array[1], p_array[2]);
        if (vf_get_property(&p_tmp, p_object, VFGP_FIND, NULL, p_array[0], p_array[1], p_array[2], (void *) NULL))
        {
            print_search_results(p_tmp);
        }

        vf_delete_object(p_object, TRUE);

        pass = TRUE;
    }
    
    update_results(pass, warning);
}







/*----------------------------------------------------------------------------*
 * NAME
 *      extract_groups()
 * 
 * DESCRIPTION
 *      
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void check_extract_groups(
    const char *filename,
    const char *p_group
    )
{
    VF_OBJECT_T *p_object = NULL;
    bool_t warning = FALSE, pass = FALSE;

    do_title_line(filename);

    if (read_show_status(&p_object, filename))
    {
        VF_PROP_T *p_tmp;

        if (vf_get_property(&p_tmp, p_object, VFGP_FIND, p_group, "LOGO", NULL))
        {
            print_search_results(p_tmp);
        }

        vf_delete_object(p_object, TRUE);

        pass = TRUE;
    }
    
    update_results(pass, warning);
}








/*----------------------------------------------------------------------------*
 * NAME
 *      print_search_results()
 * 
 * DESCRIPTION
 *      List the values in each of the current search results.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void print_search_results(
    VF_PROP_T *p_tmp        /* Pointer to current property */
    )
{
    do
    {
        void *ptr;
        uint32_t size;
        vf_encoding_t enc;

        if (vf_get_prop_value(p_tmp, &ptr, &size, &enc))
        {
            switch (enc)
            {
            case VF_ENC_BASE64:
                {
                    printf("  prop = \"%s\"\n", vf_get_prop_name_string(p_tmp, 0));
                }
                break;

            case VF_ENC_7BIT:
            case VF_ENC_QUOTEDPRINTABLE:
                {
                    uint16_t i;

                    for (i = 0;;i++)
                    {
                        const char *p_value = vf_get_prop_value_string(p_tmp, i);

                        if (p_value)
                        {
                            printf("  field[%d] = \"%s\"\n", i, p_value);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                break;
            }
        }
    }
    while (vf_get_next_property(&p_tmp))
        ;
}






/*----------------------------------------------------------------------------*
 * NAME
 *      check_base64_enc()
 * 
 * DESCRIPTION
 *      Check BASE64 encoding.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void check_base64_enc(bool_t rdata)
{
    char buffer[MAXBASE64ENC+1];
    int i, s;
    bool_t pass;

    do_title_line("BASE64 encoding");

    for (s = 1, pass = TRUE;pass && (s < sizeof(buffer));s++)
    {
        for (i = 0;i < s;i++)
        {
            buffer[i] = (rdata ? rand() : (i + 1)) % 256;
        }

        pass &= encode_decode_check(buffer, s);

        if (pass)
        {
            printf("encode_decode_check() : OK @ %d bytes\r", s);
        }
    }

    printf("\n");

    update_results(pass, 0);
}



/*----------------------------------------------------------------------------*
 * NAME
 *      encode_decode_check()
 * 
 * DESCRIPTION
 *      Encode & decode chunk of data.  Check data consistency.
 *
 * RETURNS
 *      TRUE iff encoded / decoded successfully.
 *----------------------------------------------------------------------------*/

static bool_t encode_decode_check(
    const char *p_data,         /* Pointer to data */
    uint32_t length             /* Length */
    )
{
    VF_OBJECT_T *p_object;
    const char *testfile = "bindata.vcf";
    const char *testprop = "X-BINARY-DATA";

    bool_t pass = FALSE;

    p_object = vf_create_object("TEST", NULL);

    if (p_object)
    {
        VF_PROP_T *p_prop;

        if (vf_get_property(&p_prop, p_object, VFGP_GET, NULL, testprop, NULL))
        {
            vf_set_prop_value_base64(p_prop, p_data, length, TRUE);
        }

        vf_write_file(testfile, p_object, TRUE);

        vf_delete_object(p_object, TRUE);
    }

    if (vf_read_file(&p_object, testfile))
    {
        VF_PROP_T *p_prop;

        if (vf_get_property(&p_prop, p_object, VFGP_FIND, NULL, testprop, NULL))
        {
            const char *p_new_data;
            uint32_t new_length;

            p_new_data = vf_get_prop_value_base64(p_prop, &new_length);

            if (p_new_data)
            {
                if (length == new_length)
                {
                    if (0 == memcmp(p_data, p_new_data, length))
                    {
                        pass = TRUE;
                    }
                    else
                    {
                        printf("encode_decode_check() : data NOT OK\n");
                    }
                }
                else
                {
                    printf("encode_decode_check() : Length error\n");
                }
            }
            else
            {
                printf("encode_decode_check() : No data\n");
            }
        }

        vf_delete_object(p_object, TRUE);
    }

    return pass;
}





/*----------------------------------------------------------------------------*
 * NAME
 *      check_vcal_functions()
 * 
 * DESCRIPTION
 *      Check the date encoding functions.  This is a non systematic random
 *      test ehich generates random values, encodes them, decodes them and
 *      compares the result with the initial value.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

static void check_vcal_functions(void)
{
    VF_ISO8601_PERIOD_T test_in, test_out;
    int i;
    bool_t pass;

    do_title_line("VCAL encoding functions");

    for (i = 0, pass = TRUE;pass && (i < 1000);i++)
    {
        char sz[256];

        get_random_date(&test_in);

        vf_period_time_to_string(sz, &test_in);
        vf_period_string_to_time(&test_out, sz);

        if (memcmp(&test_in, &test_out, sizeof(VF_ISO8601_PERIOD_T)))
        {
            pass = FALSE;
        }
        else
        {
            printf("check_vcal_functions() : %s        \r", sz);
        }
    }

    printf("\n");

    update_results(pass, 0);
}




/*----------------------------------------------------------------------------*
 * NAME
 *      get_random_date()
 * 
 * DESCRIPTION
 *      Initialise a random VF_ISO8601_PERIOD_T value.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

void get_random_date(VF_ISO8601_PERIOD_T *p_period)
{
    p_period->days = rand() % 100;
    p_period->hours = rand() % 24;
    p_period->minutes = rand() % 60;
    p_period->months = rand() % 12;
    p_period->seconds = rand() % 60;
    p_period->weeks = rand() % 9;
    p_period->years = rand() % 20;
}



/*----------------------------------------------------------------------------*
 * NAME
 *      get_out_file()
 * 
 * DESCRIPTION
 *      Obtain the name of a test output file from a teset input file.  The
 *      outout filename is the input name with the subscript replaced.
 *
 * RETURNS
 *      (none)
 *----------------------------------------------------------------------------*/

void get_out_file(
    char *p_outfile,           /* Buffer to receive filename */
    const char *p_subscript,   /* New subscript */
    const char *p_infile       /* Name of source file we're mimicing */
    )
{
    char *p_found_subscript;

    strcpy(p_outfile, p_infile);

    if (NULL == (p_found_subscript = strstr(p_outfile, ".")))
    {
        printf("get_out_file() : invalid name (%s) in test files\n", p_outfile);
    }
    else
    {
        strcpy(p_found_subscript, p_subscript);
    }
}