]> Sergey Matveev's repositories - public-inbox.git/blob - t/eml.t
t/eml.t: workaround newer Email::MIME* behavior
[public-inbox.git] / t / eml.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::MsgIter qw(msg_part_text);
8 my @classes = qw(PublicInbox::Eml);
9 SKIP: {
10         require_mods('Email::MIME', 1);
11         # TODO: Email::MIME behavior is not consistent in newer versions
12         # we need to evaluate and possibly adjust our behavior to decide
13         # between DWIM-ness with historical mail...
14         push @classes, 'PublicInbox::MIME';
15 };
16 use_ok $_ for @classes;
17
18 sub mime_load ($) {
19         my ($path) = @_;
20         open(my $fh, '<', $path) or die "open $path: $!";
21         PublicInbox::MIME->new(\(do { local $/; <$fh> }));
22 }
23
24 {
25         my $eml = PublicInbox::Eml->new(\(my $str = "a: b\n\nhi\n"));
26         is($str, "hi\n", '->new modified body like Email::Simple');
27         is($eml->body, "hi\n", '->body works');
28         is($eml->as_string, "a: b\n\nhi\n", '->as_string');
29 }
30
31 for my $cls (@classes) {
32         my $mime = $cls->new(my $orig = "From: x\n\nb");
33         is($mime->as_string, $orig, '->as_string works');
34         is($mime->header_obj->as_string, "From: x\n",
35                         'header ->as_string works');
36
37         # headers
38         is($mime->header_raw('From'), 'x', 'header_raw scalar context');
39         $mime = $cls->new("R:\n\tx\nR:\n 1\n");
40         is_deeply([$mime->header_raw('r')], [ 'x', '1' ], 'multi-value');
41         $mime = $cls->new("R:x\nR: 1\n");
42         is_deeply([$mime->header_raw('r')], [ 'x', '1' ], 'multi-value header');
43         $mime = $cls->new("R:x\n R: 1\nR:\n f\n");
44         is_deeply([$mime->header_raw('r')], [ 'x R: 1', 'f' ],
45                 'multi-line, multi-value header');
46
47         $mime->header_set('r');
48         is_deeply([$mime->header_raw('r')], [], 'header_set clears');
49         $mime->header_set('r');
50         is_deeply([$mime->header_raw('r')], [], 'header_set clears idempotent');
51         $mime->header_set('r', 'h');
52         is_deeply([$mime->header_raw('r')], ['h'], 'header_set');
53         $mime->header_set('r', 'h', 'i');
54         is_deeply([$mime->header_raw('r')], ['h', 'i'], 'header_set ary');
55         $mime->header_set('rr', 'b');
56         is_deeply([$mime->header_raw('r')], ['h', 'i'],
57                                 "header_set `rr' did not clobber `r'");
58         is($mime->header_raw('rr'), 'b', 'got set scalar');
59         $mime->header_set('rr', 'b'x100);
60         is($mime->header_raw('rr'), 'b'x100, 'got long set scalar');
61         if ($cls eq 'PublicInbox::Eml') {
62                 like($mime->as_string, qr/^rr: b{100}\n(?:\n|\z)/sm,
63                         'single token not wrapped');
64         }
65         $mime->header_set('rr', ('b'x100) . ' wrap me');
66         if ($cls eq 'PublicInbox::Eml') {
67                 like($mime->as_string, qr/^rr: b{100}\n\twrap me\n/sm,
68                         'wrapped after long token');
69         }
70         my $exp = "pre\tformatted\n with\n breaks";
71         $mime->header_set('r', $exp);
72         like($mime->as_string, qr/^r: \Q$exp\E/sm, 'preformatted preserved');
73 } # for @classes
74
75 for my $cls (@classes) { # make sure we don't add quotes if not needed
76         my $eml = $cls->new("From: John Smith <j\@example.com>\n\n");
77         is($eml->header('From'), 'John Smith <j@example.com>',
78                 "name not unnecessarily quoted $cls");
79 }
80
81 for my $cls (@classes) {
82         my $eml = $cls->new("Subject: foo\n\n");
83         $eml->header_str_set('Subject', "\x{100}");
84         like($eml->header_raw('Subject'), qr/utf-8\?B\?/i,
85                 'MIME-B encoded UTF-8 Subject');
86         is_deeply([$eml->header('Subject')], [ "\x{100}" ],
87                 'got wide character back');
88 }
89
90 # linux-mips apparently got some messages injected w/o Message-ID
91 # and long Subject: lines w/o leading whitespace.
92 # What appears in the blobs was generated by V2Writable.
93 for my $cls (@classes) {
94         my $eml = $cls->new(<<'EOF');
95 Message-ID: <20101130193431@z>
96 Subject: something really long
97 and really wrong
98 From: linux-mips archive injection
99 Object-Id: 8c56b7abdd551b1264e6522ededbbed9890cccd0
100 EOF
101         is_deeply([ $eml->header('Subject') ],
102                 [ 'something really long and really wrong' ],
103                 'continued long line w/o leading spaces '.$cls);
104         is_deeply([ $eml->header('From') ],
105                 [ 'linux-mips archive injection' ],
106                 'subsequent line not corrupted');
107         is_deeply([ $eml->header('Message-ID') ],
108                 ['<20101130193431@z>'],
109                 'preceding line readable');
110 } # for @classes
111
112 {
113         my $eml = eml_load 't/msg_iter-order.eml';
114         my @parts;
115         my $orig = $eml->as_string;
116         $eml->each_part(sub {
117                 my ($part, $level, @ex) = @{$_[0]};
118                 my $s = $part->body_str;
119                 $s =~ s/\s+//sg;
120                 push @parts, [ $s, $level, @ex ];
121         });
122         is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine');
123         is($eml->as_string, $orig, 'unchanged by ->each_part');
124         $eml->each_part(sub {}, undef, 1);
125         is(defined($eml) ? $eml->body_raw : '', # old msg_iter clobbers $eml
126                 '', 'each_part can clobber body');
127 }
128
129 if ('descend into message/rfc822') {
130         my $eml = eml_load 't/data/message_embed.eml';
131         my @parts;
132         $eml->each_part(sub {
133                 my ($part, $level, @ex) = @{$_[0]};
134                 push @parts, [ $part, $level, @ex ];
135         });
136         is(scalar(@parts), 6, 'got all parts');
137         like($parts[0]->[0]->body, qr/^testing embedded message harder\n/sm,
138                 'first part found');
139         is_deeply([ @{$parts[0]}[1..2] ], [ 1, '1' ],
140                 'got expected depth and level for part #0');
141         is($parts[1]->[0]->filename, 'embed2x.eml',
142                 'attachment filename found');
143         is_deeply([ @{$parts[1]}[1..2] ], [ 1, '2' ],
144                 'got expected depth and level for part #1');
145         is_deeply([ @{$parts[2]}[1..2] ], [ 2, '2.1' ],
146                 'got expected depth and level for part #2');
147         is_deeply([ @{$parts[3]}[1..2] ], [ 3, '2.1.1' ],
148                 'got expected depth and level for part #3');
149         is_deeply([ @{$parts[4]}[1..2] ], [ 3, '2.1.2' ],
150                 'got expected depth and level for part #4');
151         is($parts[4]->[0]->filename, 'test.eml',
152                 'another attachment filename found');
153         is_deeply([ @{$parts[5]}[1..2] ], [ 4, '2.1.2.1' ],
154                 'got expected depth and level for part #5');
155 }
156
157 # body-less, boundary-less
158 for my $cls (@classes) {
159         my $call = 0;
160         $cls->new(<<'EOF')->each_part(sub { $call++ }, 0, 1);
161 Content-Type: multipart/mixed; boundary="body-less"
162
163 EOF
164         is($call, 1, 'called on bodyless multipart');
165
166         my @tmp;
167         $cls->new(<<'EOF')->each_part(sub { push @tmp, \@_; }, 0, 1);
168 Content-Type: multipart/mixed; boundary="boundary-less"
169
170 hello world
171 EOF
172         is(scalar(@tmp), 1, 'got one part even w/o boundary');
173         is($tmp[0]->[0]->[0]->body, "hello world\n", 'body preserved');
174         is($tmp[0]->[0]->[1], 0, '$depth is zero');
175         is($tmp[0]->[0]->[2], 1, '@idx is one');
176 }
177
178 # I guess the following only worked in PI::M because of a happy accident
179 # involving inheritance:
180 for my $cls (@classes) {
181         my @tmp;
182         my $header_less = <<'EOF';
183 Archived-At: <85k5su9k59.fsf_-_@lola.goethe.zz>
184 Content-Type: multipart/mixed; boundary="header-less"
185
186 --header-less
187
188 this is the body
189
190 --header-less
191 i-haz: header
192
193 something else
194
195 --header-less--
196 EOF
197         my $expect = "this is the body\n";
198         $cls->new($header_less)->each_part(sub { push @tmp, \@_  }, 0, 1);
199         my $body = $tmp[0]->[0]->[0]->body;
200         if ($cls eq 'PublicInbox::Eml') {
201                 is($body, $expect, 'body-only subpart in '.$cls);
202         } elsif ($body ne $expect) {
203                 diag "W: $cls `$body' != `$expect'";
204         }
205         is($tmp[1]->[0]->[0]->body, "something else\n");
206         is(scalar(@tmp), 2, 'two parts');
207 }
208
209 if ('one newline before headers') {
210         my $eml = PublicInbox::Eml->new("\nNewline: no Header \n");
211         my @v = $eml->header_raw('Newline');
212         is_deeply(\@v, ['no Header'], 'no header');
213         is($eml->crlf, "\n", 'got CRLF as "\n"');
214         is($eml->body, "");
215 }
216
217 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
218         my $s = <<EOF;
219 Content-Type: multipart/mixed; boundary="b"
220
221 --b
222 header: only
223 --b--
224 EOF
225         my $eml = $cls->new(\$s);
226         my $nr = 0;
227         my @v;
228         $eml->each_part(sub {
229                 @v = $_[0]->[0]->header_raw('Header');
230                 $nr++;
231         });
232         is($nr, 1, 'only one part');
233         is_deeply(\@v, [], "nothing w/o body $cls");
234 }
235
236 for my $cls (@classes) {
237         my $s = <<EOF; # double epilogue, double the fun
238 Content-Type: multipart/mixed; boundary="b"
239
240 --b
241 should: appear
242
243 yes
244
245 --b--
246
247 --b
248 should: not appear
249
250 nope
251 --b--
252 EOF
253         my $eml = $cls->new(\$s);
254         my $nr = 0;
255         $eml->each_part(sub {
256                 my $part = $_[0]->[0];
257                 is_deeply([$part->header_raw('should')], ['appear'],
258                         'only got one header');
259                 is($part->body, "yes\n", 'got expected body');
260                 $nr++;
261         });
262         is($nr, 1, 'only one part');
263 }
264
265 for my $cls (@classes) {
266 SKIP: {
267         skip 'newer Email::MIME behavior inconsistent', 1 if
268                 $cls eq 'PublicInbox::MIME';
269         my $s = <<EOF; # buggy git-send-email versions, again?
270 Content-Type: text/plain; =?ISO-8859-1?Q?=20charset=3D=1BOF?=
271 Content-Transfer-Encoding: 8bit
272 Object-Id: ab0440d8cd6d843bee9a27709a459ce3b2bdb94d (lore/kvm)
273
274 \xc4\x80
275 EOF
276         my $eml = $cls->new(\$s);
277         my ($str, $err) = msg_part_text($eml, $eml->content_type);
278         is($str, "\x{100}\n", "got wide character by assuming utf-8 ($cls)");
279 } # SKIP
280 }
281
282 if ('we differ from Email::MIME with final "\n" on missing epilogue') {
283         my $s = <<EOF;
284 Content-Type: multipart/mixed; boundary="b"
285
286 --b
287 header: but
288
289 no epilogue
290 EOF
291         my $eml = PublicInbox::Eml->new(\$s);
292         is(($eml->subparts)[-1]->body, "no epilogue\n",
293                 'final "\n" preserved on missing epilogue');
294 }
295
296 if ('header_size_limit stolen from postfix') {
297         local $PublicInbox::Eml::header_size_limit = 4;
298         my @w;
299         local $SIG{__WARN__} = sub { push @w, @_ };
300         my $eml = PublicInbox::Eml->new("a:b\na:d\n\nzz");
301         is_deeply([$eml->header('a')], ['b'], 'no overrun header');
302         is($eml->body_raw, 'zz', 'body not damaged');
303         is($eml->header_obj->as_string, "a:b\n", 'header truncated');
304         is(grep(/truncated/, @w), 1, 'truncation warned');
305
306         $eml = PublicInbox::Eml->new("a:b\na:d\n");
307         is_deeply([$eml->header('a')], ['b'], 'no overrun header w/o body');
308
309         local $PublicInbox::Eml::header_size_limit = 5;
310         $eml = PublicInbox::Eml->new("a:b\r\na:d\r\n\nzz");
311         is_deeply([$eml->header('a')], ['b'], 'no overrun header on CRLF');
312         is($eml->body_raw, 'zz', 'body not damaged');
313
314         @w = ();
315         $eml = PublicInbox::Eml->new("too:long\n");
316         $eml = PublicInbox::Eml->new("too:long\n\n");
317         $eml = PublicInbox::Eml->new("too:long\r\n\r\n");
318         is(grep(/ignored/, @w), 3, 'ignored header warned');
319 }
320
321 if ('maxparts is a feature unique to us') {
322         my $eml = eml_load 't/psgi_attach.eml';
323         my @orig;
324         $eml->each_part(sub { push @orig, $_[0]->[0] });
325
326         local $PublicInbox::Eml::mime_parts_limit = scalar(@orig);
327         my $i = 0;
328         $eml->each_part(sub {
329                 my $cur = $_[0]->[0];
330                 my $prv = $orig[$i++];
331                 is($cur->body_raw, $prv->body_raw, "part #$i matches");
332         });
333         is($i, scalar(@orig), 'maxparts honored');
334         $PublicInbox::Eml::mime_parts_limit--;
335         my @ltd;
336         $eml->each_part(sub { push @ltd, $_[0]->[0] });
337         for ($i = 0; $i <= $#ltd; $i++) {
338                 is($ltd[$i]->body_raw, $orig[$i]->body_raw,
339                         "part[$i] matches");
340         }
341         is(scalar(@ltd), scalar(@orig) - 1, 'maxparts honored');
342 }
343
344 SKIP: {
345         require_mods('PublicInbox::MIME', 1);
346         my $eml = eml_load 't/utf8.eml';
347         my $mime = mime_load 't/utf8.eml';
348         for my $h (qw(Subject From To)) {
349                 my $v = $eml->header($h);
350                 my $m = $mime->header($h);
351                 is($v, $m, "decoded -8 $h matches Email::MIME");
352                 ok(utf8::is_utf8($v), "$h is UTF-8");
353                 ok(utf8::valid($v), "UTF-8 valid $h");
354         }
355         my $s = $eml->body_str;
356         ok(utf8::is_utf8($s), 'body_str is UTF-8');
357         ok(utf8::valid($s), 'UTF-8 valid body_str');
358         my $ref = \(my $x = 'ref');
359         for my $msg ($eml, $mime) {
360                 $msg->body_str_set($s .= "\nHI\n");
361                 ok(!utf8::is_utf8($msg->body_raw),
362                                 'raw octets after body_str_set');
363                 $s = $msg->body_str;
364                 ok(utf8::is_utf8($s), 'body_str is UTF-8 after set');
365                 ok(utf8::valid($s), 'UTF-8 valid body_str after set');
366                 $msg->body_set($ref);
367                 is($msg->body_raw, $$ref, 'body_set worked on scalar ref');
368                 $msg->body_set($$ref);
369                 is($msg->body_raw, $$ref, 'body_set worked on scalar');
370         }
371         $eml = eml_load 't/iso-2202-jp.eml';
372         $mime = mime_load 't/iso-2202-jp.eml';
373         $s = $eml->body_str;
374         is($s, $mime->body_str, 'ISO-2202-JP body_str');
375         ok(utf8::is_utf8($s), 'ISO-2202-JP => UTF-8 body_str');
376         ok(utf8::valid($s), 'UTF-8 valid body_str');
377
378         $eml = eml_load 't/psgi_attach.eml';
379         $mime = mime_load 't/psgi_attach.eml';
380         is_deeply([ map { $_->body_raw } $eml->subparts ],
381                 [ map { $_->body_raw } $mime->subparts ],
382                 'raw ->subparts match deeply');
383         is_deeply([ map { $_->body } $eml->subparts ],
384                 [ map { $_->body } $mime->subparts ],
385                 '->subparts match deeply');
386         for my $msg ($eml, $mime) {
387                 my @old = $msg->subparts;
388                 $msg->parts_set([]);
389                 is_deeply([$msg->subparts], [], 'parts_set can clear');
390                 $msg->parts_set([$old[-1]]);
391                 is(scalar $msg->subparts, 1, 'only last remains');
392         }
393
394         # some versions of Email::MIME or Email::MIME::* will drop
395         # unnecessary ", while PublicInbox::Eml will preserve the original
396         my $exp = $mime->as_string;
397         $exp =~ s/; boundary=b\b/; boundary="b"/;
398         is($eml->as_string, $exp, 'as_string matches after parts_set');
399 }
400
401 for my $cls (@classes) {
402         my $s = <<'EOF';
403 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
404 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
405
406 EOF
407         is($cls->new($s)->filename, 'vtpm-makefile.patch', 'filename decoded');
408         $s =~ s/^Content-Disposition:.*$//sm;
409         is($cls->new($s)->filename, 'vtpm-fakefile.patch',
410                 "filename fallback ($cls)") if $cls ne 'PublicInbox::MIME';
411         is($cls->new($s)->content_type,
412                 'text/x-patch; name="vtpm-fakefile.patch"',
413                 'matches Email::MIME output, "correct" or not');
414
415         $s = <<'EOF';
416 Content-Type: multipart/foo; boundary=b
417
418 --b
419 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
420
421 a
422 --b
423 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
424
425 b
426 --b--
427 EOF
428         SKIP: {
429                 skip 'newer Email::MIME is inconsistent here', 1
430                         if $cls eq 'PublicInbox::MIME';
431                 my @x;
432                 $cls->new($s)->each_part(sub { push @x, $_[0]->[0]->filename });
433                 is_deeply(['vtpm-makefile.patch', 'vtpm-fakefile.patch'], \@x,
434                         "got filename for both attachments ($cls)");
435         }
436 }
437
438 done_testing;