3 #include "silcruntime.h"
6 int main(int argc, char **argv)
8 SilcBool success = FALSE;
9 SilcMime mime, part, part2;
10 SilcMimeAssembler ass;
19 if (argc > 1 && !strcmp(argv[1], "-d")) {
21 silc_log_debug_hexdump(TRUE);
22 silc_log_set_debug_string("*mime*,*errno*");
28 SILC_LOG_DEBUG(("Allocating MIME message context"));
29 mime = silc_mime_alloc();
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++)
45 silc_mime_add_data(mime, tmp, 101);
46 SILC_LOG_DEBUG(("Encoding MIME context"));
47 enc = silc_mime_encode(mime, &enc_len);
50 SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
52 SILC_LOG_DEBUG(("Decoding MIME message"));
53 mime = silc_mime_decode(NULL, enc, enc_len);
56 SILC_LOG_DEBUG(("Re-encoding MIME context"));
58 enc = silc_mime_encode(mime, &enc_len);
61 SILC_LOG_DEBUG(("Re-encoded MIME message: \n%s", enc));
66 * Empty data area, only headers present
68 SILC_LOG_DEBUG(("Allocating MIME message context"));
69 mime = silc_mime_alloc();
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);
79 SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
81 SILC_LOG_DEBUG(("Decoding MIME message"));
82 mime = silc_mime_decode(NULL, enc, enc_len);
85 SILC_LOG_DEBUG(("Re-encoding MIME context"));
87 enc = silc_mime_encode(mime, &enc_len);
90 SILC_LOG_HEXDUMP(("Re-encoded MIME message:"), enc, enc_len);
95 * Multipart test, with nesting
97 SILC_LOG_DEBUG(("Allocating MIME message context"));
98 mime = silc_mime_alloc();
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();
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++)
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))
124 SILC_LOG_DEBUG(("Allocating part"));
125 part = silc_mime_alloc();
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++)
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))
138 SILC_LOG_DEBUG(("Allocating part"));
139 part = silc_mime_alloc();
142 SILC_LOG_DEBUG(("Adding MIME data (NO HEADERS), 10 A's + 1 B"));
143 for (i = 0; i < 10; i++)
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))
150 SILC_LOG_DEBUG(("Allocating part"));
151 part = silc_mime_alloc();
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))
160 SILC_LOG_DEBUG(("Allocating part"));
161 part = silc_mime_alloc();
164 SILC_LOG_DEBUG(("Adding part to MIME message"));
165 if (!silc_mime_add_multipart(mime, part))
167 silc_mime_set_multipart(part, "mixed", "booooooooundary");
168 SILC_LOG_DEBUG(("Allocating part for nested multipart"));
169 part2 = silc_mime_alloc();
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++)
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))
182 SILC_LOG_DEBUG(("Encoding MIME context"));
183 enc = silc_mime_encode(mime, &enc_len);
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);
192 SILC_LOG_DEBUG(("Re-encoding MIME context"));
194 enc = silc_mime_encode(mime, &enc_len);
197 SILC_LOG_DEBUG(("Re-encoded MIME message: \n%s", enc));
199 SILC_LOG_DEBUG(("Get multiparts"));
200 frag = silc_mime_get_multiparts(mime, &mtype);
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);
210 if (silc_mime_is_multipart(part))
211 SILC_LOG_DEBUG(("Is multipart"));
212 SILC_LOG_DEBUG(("Encoded MIME part: \n%s", enc));
215 silc_mime_free(mime);
220 SILC_LOG_DEBUG(("Allocating MIME assembler"));
221 ass = silc_mime_assembler_alloc();
224 SILC_LOG_DEBUG(("Allocating MIME message context"));
225 mime = silc_mime_alloc();
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++)
241 silc_mime_add_data(mime, tmp, 301);
242 SILC_LOG_DEBUG(("Encoding MIME context"));
243 enc = silc_mime_encode(mime, &enc_len);
246 SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
248 SILC_LOG_DEBUG(("Fragment MIME message in 100 byte chunks"));
249 frag = silc_mime_encode_partial(mime, 100);
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))
261 part = silc_mime_assemble(ass, part);
263 SILC_LOG_DEBUG(("Defragmentation completed"));
264 SILC_LOG_DEBUG(("Encoding MIME context"));
265 enc = silc_mime_encode(mime, &enc_len);
267 SILC_LOG_DEBUG(("Error encoding"));
268 SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
270 silc_mime_free(part);
273 silc_mime_partial_free(frag);
274 silc_mime_assembler_free(ass);
279 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
280 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");