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