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