15
15
16
16
#include "vcard.h"
17
17
18
+ #include <assert.h>
18
19
#include <stdio.h>
19
20
#include <string.h>
20
21
#include <sys/types.h>
26
27
#include <unistd.h>
27
28
#endif
28
29
30
+ #define assert_str_equals (want , have ) \
31
+ { \
32
+ const char *_w = (want); \
33
+ const char *_h = (have); \
34
+ int _v = strcmp(_w, _h); \
35
+ if (_v) { fprintf(stderr, "line %d: string mismatch\n want=%s\n have=%s\n", __LINE__, _w, _h); assert(0); } \
36
+ }
37
+
29
38
void strip_errors (vcardcomponent * comp )
30
39
{
31
40
vcardproperty * prop , * next ;
@@ -38,49 +47,90 @@ void strip_errors(vcardcomponent *comp)
38
47
}
39
48
}
40
49
41
- int main ( int argc , const char * * argv )
50
+ static void test_parse_file ( const char * fname )
42
51
{
43
- int fd ;
44
- const char * fname ;
52
+ int fd , r ;
45
53
struct stat sbuf ;
46
54
size_t filesize ;
47
55
void * data = NULL ;
56
+ vcardcomponent * card ;
57
+ const char * want =
58
+ "BEGIN:VCARD\r\n"
59
+ "VERSION:4.0\r\n"
60
+ "FN:Simon Perreault\r\n"
61
+ "N:Perreault;Simon;;;ing. jr,M.Sc.\r\n"
62
+ "BDAY;VALUE=DATE:--0203\r\n"
63
+ "BDAY;VALUE=DATE:--0203\r\n"
64
+ "ANNIVERSARY;VALUE=TIMESTAMP:20090808T143000-0500\r\n"
65
+ "GENDER:M;manly\r\n"
66
+ "ADR;TYPE=WORK:;Suite D2-630;2875 Laurier;Quebec;QC;G1V 2M2;Canada\r\n"
67
+ "TEL;VALUE=URI;TYPE=WORK,TEXT,VOICE,CELL,VIDEO,bar,foo:tel:\r\n"
68
+ " +1-418-262-6501\r\n"
69
+ "TEL;VALUE=URI:tel:+1-418-656-9254;ext=102\r\n"
70
+ "EMAIL;TYPE=WORK:[email protected] \r\n"
71
+ "LANG;PREF=2:en\r\n"
72
+ "LANG;PREF=1:fr\r\n"
73
+ "TZ;VALUE=TEXT:-0500\r\n"
74
+ "GEO;TYPE=WORK:geo:46.772673,-71.282945\r\n"
75
+ "ORG;TYPE=WORK:Viagenie;Foo\r\n"
76
+ "CATEGORIES:bar,foo\r\n"
77
+ "NOTE;LANGUAGE=en;PID=1.0,3:Test vCard\r\n"
78
+ "URL;TYPE=HOME:http://nomis80.org\r\n"
79
+ "KEY;VALUE=URI;TYPE=WORK:http://www.viagenie.ca/simon.perreault/simon.asc\r\n"
80
+ "X-LIC-ERROR;X-LIC-ERRORTYPE=RESTRICTION-CHECK:Failed restrictions for \r\n"
81
+ " BDAY property. Expected zero or one instances of the property and got 2\r\n"
82
+ "END:VCARD\r\n"
83
+ "BEGIN:VCARD\r\n"
84
+ "FN:Mickey Mouse\r\n"
85
+ "X-LIC-ERROR;X-LIC-ERRORTYPE=RESTRICTION-CHECK:Failed restrictions for N \r\n"
86
+ " property. Expected 1 instances of the property and got 0\r\n"
87
+ "X-LIC-ERROR;X-LIC-ERRORTYPE=RESTRICTION-CHECK:Failed restrictions for \r\n"
88
+ " VERSION property. Expected 1 instances of the property and got 0\r\n"
89
+ "END:VCARD\r\n" ;
48
90
49
- if (argc != 2 ) {
50
- fprintf (stderr , "Usage: %s fname\n" , argv [0 ]);
51
- exit (1 );
52
- }
53
-
54
- fname = argv [1 ];
55
91
fd = open (fname , O_RDONLY );
56
92
if (fd < 0 ) {
57
93
fprintf (stderr , "Error: unable to open %s\n" , fname );
58
- exit ( 1 );
94
+ assert ( 0 );
59
95
}
60
96
fstat (fd , & sbuf );
61
97
filesize = sbuf .st_size ; //to make fortify compile happy
62
98
data = malloc (filesize + 1 );
63
99
memset (data , 0 , filesize + 1 );
64
- if (read (fd , data , filesize ) < 0 ) {
100
+
101
+ r = read (fd , data , filesize );
102
+ close (fd );
103
+
104
+ if (r < 0 ) {
65
105
fprintf (stderr , "Failed to read vCard\n" );
66
106
free (data );
67
- close (fd );
68
- return -1 ;
107
+ assert (0 );
69
108
}
70
109
71
- vcardcomponent * card = vcardparser_parse_string (data );
110
+ card = vcardparser_parse_string (data );
72
111
free (data );
73
112
74
113
if (card == NULL ) {
75
114
fprintf (stderr , "Failed to parse vCard\n" );
76
- close (fd );
77
- return -1 ;
115
+ assert (0 );
78
116
}
79
117
80
118
vcardrestriction_check (card );
81
119
vcardcomponent_normalize (card );
82
- printf ( "%s\n" , vcardcomponent_as_vcard_string (card ));
120
+ assert_str_equals ( want , vcardcomponent_as_vcard_string (card ));
83
121
vcardcomponent_free (card );
122
+ }
123
+
124
+ static vcardcomponent * test_comp_vanew (void )
125
+ {
126
+ vcardcomponent * card ;
127
+ const char * want =
128
+ "BEGIN:VCARD\r\n"
129
+ "VERSION:4.0\r\n"
130
+ "X-LIC-ERROR;X-LIC-ERRORTYPE=RESTRICTION-CHECK:Failed restrictions for FN \r\n"
131
+ " property. Expected one or more instances of the property and got 0\r\n"
132
+ "END:VCARD\r\n" ;
133
+
84
134
85
135
card = vcardcomponent_vanew (VCARD_VCARD_COMPONENT ,
86
136
vcardproperty_new_version (VCARD_VERSION_40 ),
@@ -89,14 +139,31 @@ int main(int argc, const char **argv)
89
139
90
140
if (card == NULL ) {
91
141
fprintf (stderr , "Failed to create vCard\n" );
92
- close (fd );
93
- return -1 ;
142
+ assert (0 );
94
143
}
95
144
96
145
vcardrestriction_check (card );
97
- printf ("\n%s\n" , vcardcomponent_as_vcard_string (card ));
146
+ vcardcomponent_normalize (card );
147
+ assert_str_equals (want , vcardcomponent_as_vcard_string (card ));
98
148
strip_errors (card );
99
149
150
+ return card ;
151
+ }
152
+
153
+ static void test_add_props (vcardcomponent * card )
154
+ {
155
+ const char * want =
156
+ "BEGIN:VCARD\r\n"
157
+ "VERSION:4.0\r\n"
158
+ "FN:Mickey Mouse\r\n"
159
+ "N:Mouse;Mickey;;;;;\r\n"
160
+ "BDAY;VALUE=DATE:19281118\r\n"
161
+ "ADR:;;123 Main Street,Disney World;Orlando;FL;32836;USA;;;;;;;;;;;\r\n"
162
+ "CATEGORIES:aaa,zzz\r\n"
163
+ "group1.NOTE;LANGUAGE=en;PID=1,3;SORT-AS=bar,foo;TYPE=WORK:Test vCard\r\n"
164
+ // "REV:20240424T143248Z\r\n"
165
+ "END:VCARD\r\n" ;
166
+
100
167
/* Create and add NOTE property */
101
168
vcardstrarray * sa = vcardstrarray_new (10 );
102
169
vcardstrarray_append (sa , "1" );
@@ -173,25 +240,60 @@ int main(int argc, const char **argv)
173
240
t .day = 18 ;
174
241
prop = vcardproperty_new_bday (t );
175
242
vcardcomponent_add_property (card , prop );
176
-
243
+ #if 0 // Can't easily compare
177
244
/* Create and add REV property */
178
245
t = vcardtime_current_utc_time ();
179
246
prop = vcardproperty_new_rev (t );
180
247
vcardcomponent_add_property (card , prop );
181
-
248
+ #endif
182
249
vcardrestriction_check (card );
183
250
vcardcomponent_normalize (card );
184
- printf ("\n%s\n" , vcardcomponent_as_vcard_string (card ));
251
+ assert_str_equals (want , vcardcomponent_as_vcard_string (card ));
252
+ }
253
+
254
+ static void test_n_restriction (vcardcomponent * card )
255
+ {
256
+ vcardproperty * prop ;
257
+ const char * want =
258
+ "BEGIN:VCARD\r\n"
259
+ "VERSION:3.0\r\n"
260
+ "FN:Mickey Mouse\r\n"
261
+ "BDAY;VALUE=DATE:19281118\r\n"
262
+ "ADR:;;123 Main Street,Disney World;Orlando;FL;32836;USA;;;;;;;;;;;\r\n"
263
+ "CATEGORIES:aaa,zzz\r\n"
264
+ "group1.NOTE;LANGUAGE=en;PID=1,3;SORT-AS=bar,foo;TYPE=WORK:Test vCard\r\n"
265
+ "X-LIC-ERROR;X-LIC-ERRORTYPE=RESTRICTION-CHECK:Failed restrictions for N \r\n"
266
+ " property. Expected 1 instances of the property and got 0\r\n"
267
+ "END:VCARD\r\n" ;
185
268
186
269
/* Change VERSION from 4.0 to 3.0 */
187
270
prop = vcardcomponent_get_first_property (card , VCARD_VERSION_PROPERTY );
188
271
vcardproperty_set_version (prop , VCARD_VERSION_30 );
189
272
273
+ /* Remove N property */
274
+ prop = vcardcomponent_get_first_property (card , VCARD_N_PROPERTY );
275
+ vcardcomponent_remove_property (card , prop );
276
+ vcardproperty_free (prop );
277
+
190
278
vcardrestriction_check (card );
191
- printf ("\n%s\n" , vcardcomponent_as_vcard_string (card ));
279
+ assert_str_equals (want , vcardcomponent_as_vcard_string (card ));
280
+ }
281
+
282
+ int main (int argc , const char * * argv )
283
+ {
284
+ vcardcomponent * card ;
285
+
286
+ if (argc != 2 ) {
287
+ fprintf (stderr , "Usage: %s fname\n" , argv [0 ]);
288
+ exit (1 );
289
+ }
290
+
291
+ test_parse_file (argv [1 ]);
292
+ card = test_comp_vanew ();
293
+ test_add_props (card );
294
+ test_n_restriction (card );
192
295
193
296
vcardcomponent_free (card );
194
297
195
- close (fd );
196
298
return 0 ;
197
299
}
0 commit comments