Set license using %license
[platform/upstream/libical.git] / src / test / testmime.c
1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: 
4   CREATOR: eric 25 June 2000
5   
6   $Id: testmime.c,v 1.6 2008-02-03 16:10:48 dothebart Exp $
7   $Locker:  $
8     
9  The contents of this file are subject to the Mozilla Public License
10  Version 1.0 (the "License"); you may not use this file except in
11  compliance with the License. You may obtain a copy of the License at
12  http://www.mozilla.org/MPL/
13  
14  Software distributed under the License is distributed on an "AS IS"
15  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
16  the License for the specific language governing rights and
17  limitations under the License.
18  
19
20  This program is free software; you can redistribute it and/or modify
21  it under the terms of either: 
22
23     The LGPL as published by the Free Software Foundation, version
24     2.1, available at: http://www.fsf.org/copyleft/lesser.html
25
26   Or:
27
28     The Mozilla Public License Version 1.0. You may obtain a copy of
29     the License at http://www.mozilla.org/MPL/
30
31   The Initial Developer of the Original Code is Eric Busboom
32
33  (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
34  ======================================================================*/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <libical/ical.h>
41
42 #include <stdlib.h> /* For rand */
43 #include <string.h> /* for strrchr, strdup*/
44 #if defined(HAVE_UNISTD_H)
45 #include <unistd.h>   /* for getopt */
46 #endif
47
48 /*int sspm_parse_mime(struct sspm_part *parts, 
49                size_t max_parts,
50                struct sspm_action_map *actions,
51                char* (*get_string)(char *s, size_t size, void* data),
52                void *get_string_data,
53                struct sspm_header *first_header
54     );
55 */
56
57
58
59 char* major_type_string[] = {
60     "TEXT",
61     "IMAGE",
62     "AUDIO",
63     "VIDEO",
64     "APPLICATION",
65     "MULTIPART",
66     "MESSAGE",
67     "UNKNOWN",
68     "NO"
69 };
70
71 char* minor_type_string[] = {
72     "ANY",
73     "PLAIN",
74     "RFC822",
75     "DIGEST",
76     "CALENDAR",
77     "MIXED",
78     "RELATED",
79     "ALTERNATIVE",
80     "PARALLEL",
81     "UNKNOWN",
82     "NO"
83 };
84
85
86 char* read_stream(char *s, size_t size, void *d)
87 {
88     char *c = fgets(s,size, (FILE*)d);
89
90     return c;
91
92 }
93
94
95 int main(int argc, char* argv[]) {
96
97     FILE *f;
98     int c;
99 #if !defined(HAVE_UNISTD_H)
100     extern char *optarg;
101     extern int optind, optopt;
102 #endif
103     int errflg=0;
104     char* program_name;
105
106     struct options{
107             int normal;
108             int stress;
109             int base64;
110             int qp;
111             int sleep;
112             int count;
113             char* input_file;
114     } opt;
115
116     memset(&opt,0,sizeof(opt));
117     
118     program_name = (char*)strrchr((char*)argv[0],'/');
119     program_name++;
120
121     while ((c = getopt(argc, argv, "nsbqi:S:c:")) != -1) {
122         switch (c) {
123             case 'i': { /* Input comes from named file */
124                 opt.input_file = strdup(optarg);
125                 break;
126             }
127             case 'n':{ /* Normal */
128
129                 if(opt.stress+opt.base64+opt.qp != 0){
130                     fprintf(stderr,
131                             "%s: Use only one of  n,s,b and q\n",
132                             program_name);
133                 }
134                 opt.normal = 1;
135                 break;
136             }
137             case 's':{ /* Stress-test*/
138                 if(opt.base64+opt.normal+opt.qp != 0){
139                     fprintf(stderr,
140                             "%s: Use only one of  n,s,b and q\n",
141                             program_name);
142                 }
143                 opt.stress = 1;
144                 break;
145             }
146             case 'b':{ /* test base64 decoding*/
147                 if(opt.stress+opt.normal+opt.qp != 0){
148                     fprintf(stderr,
149                             "%s: Use only one of  n,s,b and q\n",
150                             program_name);
151                 }
152                 opt.base64 = 1;
153                 break;
154             }
155             case 'q':{ /* test quoted-printable decoding*/
156                 if(opt.stress+opt.base64+opt.normal != 0){
157                     fprintf(stderr,
158                             "%s: Use only one of  n,s,b and q\n",
159                             program_name);
160                 }
161                 opt.qp = 1;
162                 break;
163             }
164             case 'S':{ /* sleep at end of run */
165                 opt.sleep = atoi(optarg);
166                 break;
167             }
168
169             case 'c':{ /* number of iterations of stress test */
170                 opt.count = atoi(optarg);
171                 break;
172             }
173             
174             case ':': {/* Option given without an operand */
175                 fprintf(stderr,
176                         "%s: Option -%c requires an operand\n", 
177                         program_name,optopt);
178                 errflg++;
179                 break;
180             }
181             case '?': {
182                 errflg++;
183             }
184         }
185     }
186     
187     if (errflg >0){
188         fprintf(stderr,"Usage: %s [-n|-s|-b|-q] [-i input_file]\n",
189                 program_name);
190         exit(1);
191     }
192     
193     if(opt.stress+opt.base64+opt.normal+opt.qp == 0){
194         fprintf(stderr,
195                 "%s: Must have one of n,s,b or q\n",
196                 program_name);
197     }
198     
199     if(opt.input_file){
200         f = fopen(opt.input_file,"r");
201         if (f == 0){
202             fprintf(stderr,"Could not open input file \"%s\"\n",
203                     opt.input_file);
204             exit(1);
205         }
206     } else {
207         f = stdin;
208     }
209         
210         
211
212     if(opt.normal == 1){
213         icalcomponent *c;
214
215         c = icalmime_parse(read_stream,f);
216         
217         printf("%s\n",icalcomponent_as_ical_string(c));
218
219         icalcomponent_free(c);
220
221     }  else if (opt.stress==1 ){
222         /* Read file in by lines, then randomize the lines into a
223            string buffer */
224
225         char *array[1024];
226         char temp[1024];
227         char *buf;
228         int i,last;
229         int size = 0;
230         int non_rand;
231         int rand_lines;
232         int r;
233         int j;
234         icalcomponent *c;
235         struct slg_data {
236                 char* pos;
237                 char* str;
238         } d;
239     
240         for(i=0; !feof(f); i++){
241             fgets(temp,1024,f);
242             array[i] = strdup(temp);
243             size += strlen(temp);
244         }
245         last = i;
246  
247         buf = malloc(size*2);
248         assert(buf != 0);
249         
250
251         for(j=0; j<opt.count; j++){
252
253             srand(j);
254             memset(buf,0,size*2);
255             /* First insert some non-randomized lines */
256             non_rand = ((float)rand()/(float)RAND_MAX) * last;
257             for(i=0;i<non_rand;i++){
258                 strcat(buf,array[i]);
259             }
260             
261             /* Then, insert some lines at random */
262             
263             rand_lines = last - non_rand;
264             
265             for(i=0;i<rand_lines;i++){
266                 srand(i);
267                 r = ((float)rand()/(float)RAND_MAX) * rand_lines;
268                 strcat(buf,array[r+non_rand]);
269                 
270             }
271
272             d.pos = 0;
273             d.str = buf;
274
275             c = icalmime_parse(icalparser_string_line_generator,&d);
276
277             printf("%s\n",icalcomponent_as_ical_string(c));
278
279             icalcomponent_free(c);
280             
281         }
282
283         free(buf);
284
285         for(i=0; i<last; i++){
286             free(array[i]);
287         }
288
289     } else if(opt.qp == 1){     
290         char str[4096];
291         char conv[4096];
292         
293         memset(str,0,4096);
294         
295         while(!feof(f) && fgets(str,4096,f)!=0){
296             size_t size;
297             
298             size = strlen(str);
299             memset(conv,0,4096);
300             decode_quoted_printable(conv,str,&size);
301             
302             conv[size] = '\0';
303             printf("%s",conv);
304             memset(str,0,4096);
305             
306         }
307     } else if (opt.base64 == 1) {
308         char str[4096];
309         char conv[4096];
310         
311         memset(str,0,4096);
312         
313         while(!feof(f) && fgets(str,4096,f)!=0){
314             size_t size;
315             
316             size = strlen(str);
317             memset(conv,0,4096);
318             decode_base64(conv,str,&size);
319             
320             conv[size] = '\0';
321             printf("%s",conv);
322             memset(str,0,4096);
323             
324         }
325     }
326
327     if (opt.sleep != 0){
328 #ifdef WIN32
329         _sleep(opt.sleep*1000);
330 #else
331         sleep(opt.sleep);
332 #endif
333     }
334
335     if( opt.input_file != 0){
336         free(opt.input_file);
337     }
338
339     icalmemory_free_ring();
340
341     return 0;
342
343 }
344
345
346
347
348
349         
350         
351