Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcmime.c
1 /* SilcMime tests */
2
3 #include "silc.h"
4 #include "silcmime.h"
5
6 int main(int argc, char **argv)
7 {
8   SilcBool success = FALSE;
9   SilcMime mime, part, part2;
10   SilcMimeAssembler ass;
11   int i;
12   char tmp[500];
13   unsigned char *enc;
14   SilcUInt32 enc_len;
15   SilcDList frag;
16   SilcBuffer buf;
17   const char *mtype;
18
19   if (argc > 1 && !strcmp(argv[1], "-d")) {
20     silc_log_debug(TRUE);
21     silc_log_debug_hexdump(TRUE);
22     silc_log_set_debug_string("*mime*,*errno*");
23   }
24
25   /* 
26    * Simple MIME test
27    */
28   SILC_LOG_DEBUG(("Allocating MIME message context"));
29   mime = silc_mime_alloc();
30   if (!mime)
31     goto err;
32   SILC_LOG_DEBUG(("Adding MIME fields"));
33   SILC_LOG_DEBUG(("Adding MIME-Version: 1.0"));
34   silc_mime_add_field(mime, "MIME-Version", "1.0");
35   SILC_LOG_DEBUG(("Adding Content-Type: foo/bar"));
36   silc_mime_add_field(mime, "Content-Type", "foo/bar");
37   SILC_LOG_DEBUG(("Adding Content-Transfer-Encoding: binary"));
38   silc_mime_add_field(mime, "Content-Transfer-Encoding", "binary");
39   SILC_LOG_DEBUG(("Adding FOO: BaR"));
40   silc_mime_add_field(mime, "FOO", "BaR");
41   SILC_LOG_DEBUG(("Adding MIME data, 100 A's + 1 B"));
42   for (i = 0; i < 100; i++)
43     tmp[i] = 'A';
44   tmp[100] = 'B';
45   silc_mime_add_data(mime, tmp, 101);
46   SILC_LOG_DEBUG(("Encoding MIME context"));
47   enc = silc_mime_encode(mime, &enc_len);
48   if (!enc)
49     goto err;
50   SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
51   silc_mime_free(mime);
52   SILC_LOG_DEBUG(("Decoding MIME message"));
53   mime = silc_mime_decode(NULL, enc, enc_len);
54   if (!mime)
55     goto err;
56   SILC_LOG_DEBUG(("Re-encoding MIME context"));
57   silc_free(enc);
58   enc = silc_mime_encode(mime, &enc_len);
59   if (!enc)
60     goto err;
61   SILC_LOG_DEBUG(("Re-encoded MIME message: \n%s", enc));
62   silc_free(enc);
63   silc_mime_free(mime);
64
65   /*
66    * Empty data area, only headers present
67    */
68   SILC_LOG_DEBUG(("Allocating MIME message context"));
69   mime = silc_mime_alloc();
70   if (!mime)
71     goto err;
72   SILC_LOG_DEBUG(("Adding Content-Transfer-Encoding: binary"));
73   silc_mime_add_field(mime, "Content-Transfer-Encoding", "binary");
74   SILC_LOG_DEBUG(("No data area, only header present"));
75   SILC_LOG_DEBUG(("Encoding MIME context"));
76   enc = silc_mime_encode(mime, &enc_len);
77   if (!enc)
78     goto err;
79   SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
80   silc_mime_free(mime);
81   SILC_LOG_DEBUG(("Decoding MIME message"));
82   mime = silc_mime_decode(NULL, enc, enc_len);
83   if (!mime)
84     goto err;
85   SILC_LOG_DEBUG(("Re-encoding MIME context"));
86   silc_free(enc);
87   enc = silc_mime_encode(mime, &enc_len);
88   if (!enc)
89     goto err;
90   SILC_LOG_HEXDUMP(("Re-encoded MIME message:"), enc, enc_len);
91   silc_free(enc);
92   silc_mime_free(mime);
93
94   /*
95    * Multipart test, with nesting
96    */
97   SILC_LOG_DEBUG(("Allocating MIME message context"));
98   mime = silc_mime_alloc();
99   if (!mime)
100     goto err;
101   SILC_LOG_DEBUG(("Adding MIME-Version: 1.0"));
102   silc_mime_add_field(mime, "MIME-Version", "1.0");
103   SILC_LOG_DEBUG(("Adding Content-Transfer-Encoding: binary"));
104   silc_mime_add_field(mime, "Content-Transfer-Encoding", "binary");
105   SILC_LOG_DEBUG(("Marking as multipart MIME message"));
106   silc_mime_set_multipart(mime, "mixed", "boundary");
107   SILC_LOG_DEBUG(("Adding FOO: BaR"));
108   silc_mime_add_field(mime, "FOO", "BaR");
109   SILC_LOG_DEBUG(("Allocating part"));
110   part = silc_mime_alloc();
111   if (!part)
112     goto err;
113   SILC_LOG_DEBUG(("Adding MIME fields"));
114   SILC_LOG_DEBUG(("Adding Content-Type: foo/bar"));
115   silc_mime_add_field(part, "Content-Type", "foo/bar");
116   SILC_LOG_DEBUG(("Adding MIME data, 100 A's + 1 B"));
117   for (i = 0; i < 100; i++)
118     tmp[i] = 'A';
119   tmp[100] = 'B';
120   silc_mime_add_data(part, tmp, 101);
121   SILC_LOG_DEBUG(("Adding part to MIME message"));
122   if (!silc_mime_add_multipart(mime, part))
123     goto err;
124   SILC_LOG_DEBUG(("Allocating part"));
125   part = silc_mime_alloc();
126   if (!part)
127     goto err;
128   SILC_LOG_DEBUG(("Adding Content-Type: image/foobar"));
129   silc_mime_add_field(part, "Content-Type", "image/foobar");
130   SILC_LOG_DEBUG(("Adding MIME data, 50 A's + 1 B"));
131   for (i = 0; i < 50; i++)
132     tmp[i] = 'A';
133   tmp[50] = 'B';
134   silc_mime_add_data(part, tmp, 51);
135   SILC_LOG_DEBUG(("Adding part to MIME message"));
136   if (!silc_mime_add_multipart(mime, part))
137     goto err;
138   SILC_LOG_DEBUG(("Allocating part"));
139   part = silc_mime_alloc();
140   if (!part)
141     goto err;
142   SILC_LOG_DEBUG(("Adding MIME data (NO HEADERS), 10 A's + 1 B"));
143   for (i = 0; i < 10; i++)
144     tmp[i] = 'A';
145   tmp[10] = 'B';
146   silc_mime_add_data(part, tmp, 11);
147   SILC_LOG_DEBUG(("Adding part to MIME message"));
148   if (!silc_mime_add_multipart(mime, part))
149     goto err;
150   SILC_LOG_DEBUG(("Allocating part"));
151   part = silc_mime_alloc();
152   if (!part)
153     goto err;
154   SILC_LOG_DEBUG(("Adding Content-Type: image/foobar"));
155   SILC_LOG_DEBUG(("No data area, only header"));
156   silc_mime_add_field(part, "Content-Type", "image/foobar");
157   SILC_LOG_DEBUG(("Adding part to MIME message"));
158   if (!silc_mime_add_multipart(mime, part))
159     goto err;
160   SILC_LOG_DEBUG(("Allocating part"));
161   part = silc_mime_alloc();
162   if (!part)
163     goto err;
164   SILC_LOG_DEBUG(("Adding part to MIME message"));
165   if (!silc_mime_add_multipart(mime, part))
166     goto err;
167   silc_mime_set_multipart(part, "mixed", "booooooooundary");
168   SILC_LOG_DEBUG(("Allocating part for nested multipart"));
169   part2 = silc_mime_alloc();
170   if (!part)
171     goto err;
172   SILC_LOG_DEBUG(("Adding Content-Type: foo/nested"));
173   silc_mime_add_field(part2, "Content-Type", "foo/nested");
174   SILC_LOG_DEBUG(("Adding MIME data, 150 A's + 1 B"));
175   for (i = 0; i < 150; i++)
176     tmp[i] = 'A';
177   tmp[150] = 'B';
178   silc_mime_add_data(part2, tmp, 151);
179   SILC_LOG_DEBUG(("Adding part to another part message"));
180   if (!silc_mime_add_multipart(part, part2))
181     goto err;
182   SILC_LOG_DEBUG(("Encoding MIME context"));
183   enc = silc_mime_encode(mime, &enc_len);
184   if (!enc)
185     goto err;
186   SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
187   silc_mime_free(mime);
188   SILC_LOG_DEBUG(("Decoding MIME message"));
189   mime = silc_mime_decode(NULL, enc, enc_len);
190   if (!mime)
191     goto err;
192   SILC_LOG_DEBUG(("Re-encoding MIME context"));
193   silc_free(enc);
194   enc = silc_mime_encode(mime, &enc_len);
195   if (!enc)
196     goto err;
197   SILC_LOG_DEBUG(("Re-encoded MIME message: \n%s", enc));
198   silc_free(enc);
199   SILC_LOG_DEBUG(("Get multiparts"));
200   frag = silc_mime_get_multiparts(mime, &mtype);
201   if (!frag)
202     goto err;
203   SILC_LOG_DEBUG(("Multipart type '%s'", mtype));
204   silc_dlist_start(frag);
205   while ((part = silc_dlist_get(frag)) != SILC_LIST_END) {
206     SILC_LOG_DEBUG(("Encoding MIME part"));
207     enc = silc_mime_encode(part, &enc_len);
208     if (!enc)
209          goto err;
210     if (silc_mime_is_multipart(part))
211          SILC_LOG_DEBUG(("Is multipart"));
212     SILC_LOG_DEBUG(("Encoded MIME part: \n%s", enc));
213     silc_free(enc);
214   }
215   silc_mime_free(mime);
216
217   /*
218    * Fragmentation test
219    */
220   SILC_LOG_DEBUG(("Allocating MIME assembler"));
221   ass = silc_mime_assembler_alloc();
222   if (!ass)
223     goto err;
224   SILC_LOG_DEBUG(("Allocating MIME message context"));
225   mime = silc_mime_alloc();
226   if (!mime)
227     goto err;
228   SILC_LOG_DEBUG(("Adding MIME fields"));
229   SILC_LOG_DEBUG(("Adding MIME-Version: 1.0"));
230   silc_mime_add_field(mime, "MIME-Version", "1.0");
231   SILC_LOG_DEBUG(("Adding Content-Type: foo/bar"));
232   silc_mime_add_field(mime, "Content-Type", "foo/bar");
233   SILC_LOG_DEBUG(("Adding Content-Transfer-Encoding: binary"));
234   silc_mime_add_field(mime, "Content-Transfer-Encoding", "binary");
235   SILC_LOG_DEBUG(("Adding FOO: BaR"));
236   silc_mime_add_field(mime, "FOO", "BaR");
237   SILC_LOG_DEBUG(("Adding MIME data, 300 A's + 1 B"));
238   for (i = 0; i < 300; i++)
239     tmp[i] = 'A';
240   tmp[300] = 'B';
241   silc_mime_add_data(mime, tmp, 301);
242   SILC_LOG_DEBUG(("Encoding MIME context"));
243   enc = silc_mime_encode(mime, &enc_len);
244   if (!enc)
245     goto err;
246   SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
247   silc_free(enc);
248   SILC_LOG_DEBUG(("Fragment MIME message in 100 byte chunks"));
249   frag = silc_mime_encode_partial(mime, 100);
250   if (!frag)
251     goto err;
252   silc_dlist_start(frag);
253   while ((buf = silc_dlist_get(frag)) != SILC_LIST_END)
254     SILC_LOG_DEBUG(("Fragment \n%s", buf->data, silc_buffer_len(buf)));
255   SILC_LOG_DEBUG(("Defragment"));
256   silc_dlist_start(frag);
257   while ((buf = silc_dlist_get(frag)) != SILC_LIST_END) {
258     part = silc_mime_decode(NULL, buf->data, silc_buffer_len(buf));
259     if (!silc_mime_is_partial(part))
260          goto err;
261     part = silc_mime_assemble(ass, part);
262     if (part) {
263       SILC_LOG_DEBUG(("Defragmentation completed"));
264       SILC_LOG_DEBUG(("Encoding MIME context"));
265       enc = silc_mime_encode(mime, &enc_len);
266       if (!enc)
267         SILC_LOG_DEBUG(("Error encoding"));
268       SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
269       silc_free(enc);
270       silc_mime_free(part);
271     }
272   }
273   silc_mime_partial_free(frag);
274   silc_mime_assembler_free(ass);
275
276   success = TRUE;
277
278  err:
279   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
280   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
281
282   return success;
283 }