chiark / gitweb /
Start on 'settings' window. Currently disabled as it's not very
[disorder] / lib / t-mime.c
CommitLineData
b90f122b
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20#include "test.h"
21
22static int test_multipart_callback(const char *s, void *u) {
23 struct vector *parts = u;
24
25 vector_append(parts, (char *)s);
26 return 0;
27}
28
121f51ac
RK
29static int header_callback(const char *name, const char *value,
30 void *u) {
31 hash *const h = u;
32
33 hash_add(h, name, &value, HASH_INSERT);
34 return 0;
35}
36
b90f122b
RK
37void test_mime(void) {
38 char *t, *n, *v;
39 struct vector parts[1];
40 struct kvp *k;
a1bedb6d 41 const char *s, *cs, *enc;
121f51ac 42 hash *h;
b90f122b
RK
43
44 fprintf(stderr, "test_mime\n");
45
46 t = 0;
47 k = 0;
48 insist(!mime_content_type("text/plain", &t, &k));
49 check_string(t, "text/plain");
50 insist(k == 0);
51
52 insist(mime_content_type("TEXT ((broken) comment", &t, &k) < 0);
53 insist(mime_content_type("TEXT ((broken) comment\\", &t, &k) < 0);
54
55 t = 0;
56 k = 0;
57 insist(!mime_content_type("TEXT ((nested)\\ comment) /plain", &t, &k));
58 check_string(t, "text/plain");
59 insist(k == 0);
60
61 t = 0;
62 k = 0;
63 insist(!mime_content_type(" text/plain ; Charset=\"utf-\\8\"", &t, &k));
64 check_string(t, "text/plain");
65 insist(k != 0);
66 insist(k->next == 0);
67 check_string(k->name, "charset");
68 check_string(k->value, "utf-8");
69
70 t = 0;
71 k = 0;
72 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &k));
73 insist(k != 0);
74 insist(k->next == 0);
75 check_string(t, "text/plain");
76 check_string(k->name, "charset");
77 check_string(k->value, "ISO-8859-1");
78
79 t = n = v = 0;
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");
84
85 insist(!mime_rfc2388_content_disposition("inline", &t, &n, &v));
86 check_string(t, "inline");
87 insist(n == 0);
88 insist(v == 0);
89
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. */
93
94 insist(!mime_rfc2388_content_disposition(
95 "attachment; filename=genome.jpeg;\n"
96 "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
97 &t, &n, &v));
98 check_string(t, "attachment");
99 check_string(n, "filename");
100 check_string(v, "genome.jpeg");
101
102 vector_init(parts);
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"
107 "\r\n"
108 "Some text goes here\r\n"
109 "\r\n"
110 "--outer\r\n"
111 "Content-Type: multipart/mixed; boundary=inner\r\n"
112 "Content-Disposition: attachment\r\n"
113 "Content-Description: multipart-2\r\n"
114 "\r\n"
115 "--inner\r\n"
116 "Content-Type: text/plain\r\n"
117 "Content-Disposition: inline\r\n"
118 "Content-Description: text-part-2\r\n"
119 "\r\n"
120 "Some more text here.\r\n"
121 "\r\n"
122 "--inner\r\n"
123 "Content-Type: image/jpeg\r\n"
124 "Content-Disposition: attachment\r\n"
125 "Content-Description: jpeg-1\r\n"
126 "\r\n"
127 "<jpeg data>\r\n"
128 "--inner--\r\n"
129 "--outer--\r\n",
130 test_multipart_callback,
131 "outer",
132 parts) == 0);
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"
138 "\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"
144 "\r\n"
145 "--inner\r\n"
146 "Content-Type: text/plain\r\n"
147 "Content-Disposition: inline\r\n"
148 "Content-Description: text-part-2\r\n"
149 "\r\n"
150 "Some more text here.\r\n"
151 "\r\n"
152 "--inner\r\n"
153 "Content-Type: image/jpeg\r\n"
154 "Content-Disposition: attachment\r\n"
155 "Content-Description: jpeg-1\r\n"
156 "\r\n"
157 "<jpeg data>\r\n"
158 "--inner--");
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. */
163 vector_init(parts);
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"
168 "\r\n"
169 "Some more text here.\r\n"
170 "\r\n"
171 "--inner\r\n"
172 "Content-Type: image/jpeg\r\n"
173 "Content-Disposition: attachment\r\n"
174 "Content-Description: jpeg-1\r\n"
175 "\r\n"
176 "<jpeg data>\r\n"
177 "--inner--",
178 test_multipart_callback,
179 "inner",
180 parts) == 0);
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"
186 "\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"
192 "\r\n"
193 "<jpeg data>");
194
195 /* XXX mime_parse */
196
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");
205
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");
215
216 /* from RFC2045 */
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.");
221
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), \
226 encoded); \
227 } while(0)
228
229
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");
240
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),
255 "\x04\x10\x41");
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),
262 "\x04\x10\x41");
121f51ac
RK
263
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"
268 "\r\n"
269 "body\r\n",
270 header_callback, h);
271 insist(s != 0);
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");
276
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"
282 "\r\n"
283 "body\r\n",
284 header_callback, h);
285 insist(s != 0);
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");
291
292 h = hash_new(sizeof (char *));
293 s = mime_parse("From: sender@example.com\r\n"
294 "To: \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"
300 "\r\n"
301 "d2liYmxlDQo=\r\n",
302 header_callback, h);
303 insist(s != 0);
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");
a1bedb6d
RK
311
312#define CHECK_QUOTE(INPUT, EXPECT) do { \
313 s = quote822(INPUT, 0); \
314 insist(s != 0); \
315 check_string(s, EXPECT); \
316 s = mime_parse_word(s, &t, mime_http_separator); \
317 check_string(t, INPUT); \
318} while(0)
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)\"");
324
325 s = mime_encode_text("wibble\n", &cs, &enc);
326 insist(s != 0);
327 check_string(s, "wibble\n");
328 check_string(cs, "us-ascii");
329 check_string(enc, "7bit");
330
331 s = mime_encode_text("wibble\xC3\xB7\n", &cs, &enc);
332 insist(s != 0);
333 check_string(s, "wibble=C3=B7\n");
334 check_string(cs, "utf-8");
335 check_string(enc, "quoted-printable");
b90f122b
RK
336}
337
338/*
339Local Variables:
340c-basic-offset:2
341comment-column:40
342fill-column:79
343indent-tabs-mode:nil
344End:
345*/