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