2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 static int test_multipart_callback(const char *s
, void *u
) {
23 struct vector
*parts
= u
;
25 vector_append(parts
, (char *)s
);
29 static int header_callback(const char *name
, const char *value
,
33 hash_add(h
, name
, &value
, HASH_INSERT
);
37 void test_mime(void) {
39 struct vector parts
[1];
41 const char *s
, *cs
, *enc
;
44 fprintf(stderr
, "test_mime\n");
48 insist(!mime_content_type("text/plain", &t
, &k
));
49 check_string(t
, "text/plain");
52 insist(mime_content_type("TEXT ((broken) comment", &t
, &k
) < 0);
53 insist(mime_content_type("TEXT ((broken) comment\\", &t
, &k
) < 0);
57 insist(!mime_content_type("TEXT ((nested)\\ comment) /plain", &t
, &k
));
58 check_string(t
, "text/plain");
63 insist(!mime_content_type(" text/plain ; Charset=\"utf-\\8\"", &t
, &k
));
64 check_string(t
, "text/plain");
67 check_string(k
->name
, "charset");
68 check_string(k
->value
, "utf-8");
72 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t
, &k
));
75 check_string(t
, "text/plain");
76 check_string(k
->name
, "charset");
77 check_string(k
->value
, "ISO-8859-1");
80 insist(!mime_rfc2388_content_disposition("form-data; name=\"field1\"", &t
, &n
, &v
));
81 check_string(t
, "form-data");
82 check_string(n
, "name");
83 check_string(v
, "field1");
85 insist(!mime_rfc2388_content_disposition("inline", &t
, &n
, &v
));
86 check_string(t
, "inline");
90 /* Current versions of the code only understand a single arg to these
91 * headers. This is a bug at the level they work at but suffices for
92 * DisOrder's current purposes. */
94 insist(!mime_rfc2388_content_disposition(
95 "attachment; filename=genome.jpeg;\n"
96 "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
98 check_string(t
, "attachment");
99 check_string(n
, "filename");
100 check_string(v
, "genome.jpeg");
103 insist(mime_multipart("--outer\r\n"
104 "Content-Type: text/plain\r\n"
105 "Content-Disposition: inline\r\n"
106 "Content-Description: text-part-1\r\n"
108 "Some text goes here\r\n"
111 "Content-Type: multipart/mixed; boundary=inner\r\n"
112 "Content-Disposition: attachment\r\n"
113 "Content-Description: multipart-2\r\n"
116 "Content-Type: text/plain\r\n"
117 "Content-Disposition: inline\r\n"
118 "Content-Description: text-part-2\r\n"
120 "Some more text here.\r\n"
123 "Content-Type: image/jpeg\r\n"
124 "Content-Disposition: attachment\r\n"
125 "Content-Description: jpeg-1\r\n"
130 test_multipart_callback
,
133 check_integer(parts
->nvec
, 2);
134 check_string(parts
->vec
[0],
135 "Content-Type: text/plain\r\n"
136 "Content-Disposition: inline\r\n"
137 "Content-Description: text-part-1\r\n"
139 "Some text goes here\r\n");
140 check_string(parts
->vec
[1],
141 "Content-Type: multipart/mixed; boundary=inner\r\n"
142 "Content-Disposition: attachment\r\n"
143 "Content-Description: multipart-2\r\n"
146 "Content-Type: text/plain\r\n"
147 "Content-Disposition: inline\r\n"
148 "Content-Description: text-part-2\r\n"
150 "Some more text here.\r\n"
153 "Content-Type: image/jpeg\r\n"
154 "Content-Disposition: attachment\r\n"
155 "Content-Description: jpeg-1\r\n"
159 /* No trailing CRLF is _correct_ - see RFC2046 5.1.1 note regarding CRLF
160 * preceding the boundary delimiter line. An implication of this is that we
161 * must cope with partial lines at the end of the input when recursively
162 * decomposing a multipart message. */
164 insist(mime_multipart("--inner\r\n"
165 "Content-Type: text/plain\r\n"
166 "Content-Disposition: inline\r\n"
167 "Content-Description: text-part-2\r\n"
169 "Some more text here.\r\n"
172 "Content-Type: image/jpeg\r\n"
173 "Content-Disposition: attachment\r\n"
174 "Content-Description: jpeg-1\r\n"
178 test_multipart_callback
,
181 check_integer(parts
->nvec
, 2);
182 check_string(parts
->vec
[0],
183 "Content-Type: text/plain\r\n"
184 "Content-Disposition: inline\r\n"
185 "Content-Description: text-part-2\r\n"
187 "Some more text here.\r\n");
188 check_string(parts
->vec
[1],
189 "Content-Type: image/jpeg\r\n"
190 "Content-Disposition: attachment\r\n"
191 "Content-Description: jpeg-1\r\n"
197 check_string(mime_qp(""), "");
198 check_string(mime_qp("foobar"), "foobar");
199 check_string(mime_qp("foo=20bar"), "foo bar");
200 check_string(mime_qp("x \r\ny"), "x\r\ny");
201 check_string(mime_qp("x=\r\ny"), "xy");
202 check_string(mime_qp("x= \r\ny"), "xy");
203 check_string(mime_qp("x =\r\ny"), "x y");
204 check_string(mime_qp("x = \r\ny"), "x y");
206 check_string(mime_to_qp(""), "");
207 check_string(mime_to_qp("foobar\n"), "foobar\n");
208 check_string(mime_to_qp("foobar \n"), "foobar=20\n");
209 check_string(mime_to_qp("foobar\t\n"), "foobar=09\n");
210 check_string(mime_to_qp("foobar \t \n"), "foobar=20=09=20\n");
211 check_string(mime_to_qp(" foo=bar"), " foo=3Dbar\n");
212 check_string(mime_to_qp("copyright \xC2\xA9"), "copyright =C2=A9\n");
213 check_string(mime_to_qp("foo\nbar\nbaz\n"), "foo\nbar\nbaz\n");
214 check_string(mime_to_qp("wibble wobble wibble wobble wibble wobble wibble wobble wibble wobble wibble"), "wibble wobble wibble wobble wibble wobble wibble wobble wibble wobble wibb=\nle\n");
217 check_string(mime_qp("Now's the time =\r\n"
218 "for all folk to come=\r\n"
219 " to the aid of their country."),
220 "Now's the time for all folk to come to the aid of their country.");
222 #define check_base64(encoded, decoded) do { \
223 check_string(mime_base64(encoded, 0), decoded); \
224 check_string(mime_to_base64((const uint8_t *)decoded, \
225 (sizeof decoded) - 1), \
230 check_base64("", "");
231 check_base64("BBBB", "\x04\x10\x41");
232 check_base64("////", "\xFF\xFF\xFF");
233 check_base64("//BB", "\xFF\xF0\x41");
234 check_base64("BBBB//BB////",
235 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
236 check_base64("BBBBBA==",
237 "\x04\x10\x41" "\x04");
238 check_base64("BBBBBBA=",
239 "\x04\x10\x41" "\x04\x10");
241 /* Check that decoding handles various kinds of rubbish OK */
242 check_string(mime_base64("B B B B / / B B / / / /", 0),
243 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
244 check_string(mime_base64("B\r\nBBB.// B-B//~//", 0),
245 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
246 check_string(mime_base64("BBBB BB==", 0),
247 "\x04\x10\x41" "\x04");
248 check_string(mime_base64("BBBB BB = =", 0),
249 "\x04\x10\x41" "\x04");
250 check_string(mime_base64("BBBB BBB=", 0),
251 "\x04\x10\x41" "\x04\x10");
252 check_string(mime_base64("BBBB BBB = ", 0),
253 "\x04\x10\x41" "\x04\x10");
254 check_string(mime_base64("BBBB=", 0),
256 check_string(mime_base64("BBBBBB==", 0),
257 "\x04\x10\x41" "\x04");
258 check_string(mime_base64("BBBBBBB=", 0),
259 "\x04\x10\x41" "\x04\x10");
260 /* Not actually valid base64 */
261 check_string(mime_base64("BBBBx=", 0),
264 h
= hash_new(sizeof (char *));
265 s
= mime_parse("From: sender@example.com\r\n"
266 "To: rcpt@example.com\r\n"
267 "Subject: test #1\r\n"
272 check_string(*(char **)hash_find(h
, "from"), "sender@example.com");
273 check_string(*(char **)hash_find(h
, "to"), "rcpt@example.com");
274 check_string(*(char **)hash_find(h
, "subject"), "test #1");
275 check_string(s
, "body\r\n");
277 h
= hash_new(sizeof (char *));
278 s
= mime_parse("FROM: sender@example.com\r\n"
279 "TO: rcpt@example.com\r\n"
280 "SUBJECT: test #1\r\n"
281 "CONTENT-TRANSFER-ENCODING: 7bit\r\n"
286 check_string(*(char **)hash_find(h
, "from"), "sender@example.com");
287 check_string(*(char **)hash_find(h
, "to"), "rcpt@example.com");
288 check_string(*(char **)hash_find(h
, "subject"), "test #1");
289 check_string(*(char **)hash_find(h
, "content-transfer-encoding"), "7bit");
290 check_string(s
, "body\r\n");
292 h
= hash_new(sizeof (char *));
293 s
= mime_parse("From: sender@example.com\r\n"
295 " rcpt@example.com\r\n"
296 "Subject: test #1\r\n"
297 "MIME-Version: 1.0\r\n"
298 "Content-Type: text/plain\r\n"
299 "Content-Transfer-Encoding: BASE64\r\n"
304 check_string(*(char **)hash_find(h
, "from"), "sender@example.com");
305 check_string(*(char **)hash_find(h
, "to"), "rcpt@example.com");
306 check_string(*(char **)hash_find(h
, "subject"), "test #1");
307 check_string(*(char **)hash_find(h
, "mime-version"), "1.0");
308 check_string(*(char **)hash_find(h
, "content-type"), "text/plain");
309 check_string(*(char **)hash_find(h
, "content-transfer-encoding"), "BASE64");
310 check_string(s
, "wibble\r\n");
312 #define CHECK_QUOTE(INPUT, EXPECT) do { \
313 s = quote822(INPUT, 0); \
315 check_string(s, EXPECT); \
316 s = mime_parse_word(s, &t, mime_http_separator); \
317 check_string(t, INPUT); \
319 CHECK_QUOTE("wibble", "wibble");
320 CHECK_QUOTE("wibble spong", "\"wibble spong\"");
321 CHECK_QUOTE("wibble\\spong", "\"wibble\\\\spong\"");
322 CHECK_QUOTE("wibble\"spong", "\"wibble\\\"spong\"");
323 CHECK_QUOTE("(wibble)", "\"(wibble)\"");
325 s
= mime_encode_text("wibble\n", &cs
, &enc
);
327 check_string(s
, "wibble\n");
328 check_string(cs
, "us-ascii");
329 check_string(enc
, "7bit");
331 s
= mime_encode_text("wibble\xC3\xB7\n", &cs
, &enc
);
333 check_string(s
, "wibble=C3=B7\n");
334 check_string(cs
, "utf-8");
335 check_string(enc
, "quoted-printable");