2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.10.1; # TODO: check unicode_strings w/ 5.12
6 use PublicInbox::TestCommon;
7 use PublicInbox::MsgIter qw(msg_part_text);
8 my @classes = qw(PublicInbox::Eml);
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';
16 use_ok $_ for @classes;
20 open(my $fh, '<', $path) or die "open $path: $!";
21 PublicInbox::MIME->new(\(do { local $/; <$fh> }));
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');
32 # we use this pattern in -watch for writing an eml to multiple inboxes
33 my $in = do { # n.b. `my' must be used to assign a local var
34 local $eml->{hdr} = \(my $copy = ${$eml->{hdr}});
35 $eml->header_set(qw(Message-ID <a@example> <b@example>));
38 like $in, qr/<a\@example>.*?<b\@example>/s, 'Message-ID set in copy';
39 is $eml->header_raw('Message-ID'), undef, 'local $eml->{hdr} works';
42 for my $cls (@classes) {
43 my $mime = $cls->new(my $orig = "From: x\n\nb");
44 is($mime->as_string, $orig, '->as_string works');
45 is($mime->header_obj->as_string, "From: x\n",
46 'header ->as_string works');
49 is($mime->header_raw('From'), 'x', 'header_raw scalar context');
50 $mime = $cls->new("R:\n\tx\nR:\n 1\n");
51 is_deeply([$mime->header_raw('r')], [ 'x', '1' ], 'multi-value');
52 $mime = $cls->new("R:x\nR: 1\n");
53 is_deeply([$mime->header_raw('r')], [ 'x', '1' ], 'multi-value header');
54 $mime = $cls->new("R:x\n R: 1\nR:\n f\n");
55 is_deeply([$mime->header_raw('r')], [ 'x R: 1', 'f' ],
56 'multi-line, multi-value header');
58 $mime->header_set('r');
59 is_deeply([$mime->header_raw('r')], [], 'header_set clears');
60 $mime->header_set('r');
61 is_deeply([$mime->header_raw('r')], [], 'header_set clears idempotent');
62 $mime->header_set('r', 'h');
63 is_deeply([$mime->header_raw('r')], ['h'], 'header_set');
64 $mime->header_set('r', 'h', 'i');
65 is_deeply([$mime->header_raw('r')], ['h', 'i'], 'header_set ary');
66 $mime->header_set('rr', 'b');
67 is_deeply([$mime->header_raw('r')], ['h', 'i'],
68 "header_set `rr' did not clobber `r'");
69 is($mime->header_raw('rr'), 'b', 'got set scalar');
70 $mime->header_set('rr', 'b'x100);
71 is($mime->header_raw('rr'), 'b'x100, 'got long set scalar');
72 if ($cls eq 'PublicInbox::Eml') {
73 like($mime->as_string, qr/^rr: b{100}\n(?:\n|\z)/sm,
74 'single token not wrapped');
76 $mime->header_set('rr', ('b'x100) . ' wrap me');
77 if ($cls eq 'PublicInbox::Eml') {
78 like($mime->as_string, qr/^rr: b{100}\n\twrap me\n/sm,
79 'wrapped after long token');
81 my $exp = "pre\tformatted\n with\n breaks";
82 $mime->header_set('r', $exp);
83 like($mime->as_string, qr/^r: \Q$exp\E/sm, 'preformatted preserved');
86 for my $cls (@classes) { # make sure we don't add quotes if not needed
87 my $eml = $cls->new("From: John Smith <j\@example.com>\n\n");
88 is($eml->header('From'), 'John Smith <j@example.com>',
89 "name not unnecessarily quoted $cls");
92 for my $cls (@classes) {
93 my $eml = $cls->new("Subject: foo\n\n");
94 $eml->header_str_set('Subject', "\x{100}");
95 like($eml->header_raw('Subject'), qr/utf-8\?B\?/i,
96 'MIME-B encoded UTF-8 Subject');
97 is_deeply([$eml->header('Subject')], [ "\x{100}" ],
98 'got wide character back');
101 # linux-mips apparently got some messages injected w/o Message-ID
102 # and long Subject: lines w/o leading whitespace.
103 # What appears in the blobs was generated by V2Writable.
104 for my $cls (@classes) {
105 my $eml = $cls->new(<<'EOF');
106 Message-ID: <20101130193431@z>
107 Subject: something really long
109 From: linux-mips archive injection
110 Object-Id: 8c56b7abdd551b1264e6522ededbbed9890cccd0
112 is_deeply([ $eml->header('Subject') ],
113 [ 'something really long and really wrong' ],
114 'continued long line w/o leading spaces '.$cls);
115 is_deeply([ $eml->header('From') ],
116 [ 'linux-mips archive injection' ],
117 'subsequent line not corrupted');
118 is_deeply([ $eml->header('Message-ID') ],
119 ['<20101130193431@z>'],
120 'preceding line readable');
124 my $eml = eml_load 't/msg_iter-order.eml';
126 my $orig = $eml->as_string;
127 $eml->each_part(sub {
128 my ($part, $level, @ex) = @{$_[0]};
129 my $s = $part->body_str;
131 push @parts, [ $s, $level, @ex ];
133 is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine');
134 is($eml->as_string, $orig, 'unchanged by ->each_part');
135 $eml->each_part(sub {}, undef, 1);
136 is(defined($eml) ? $eml->body_raw : '', # old msg_iter clobbers $eml
137 '', 'each_part can clobber body');
140 if ('descend into message/rfc822') {
141 my $eml = eml_load 't/data/message_embed.eml';
143 $eml->each_part(sub {
144 my ($part, $level, @ex) = @{$_[0]};
145 push @parts, [ $part, $level, @ex ];
147 is(scalar(@parts), 6, 'got all parts');
148 like($parts[0]->[0]->body, qr/^testing embedded message harder\n/sm,
150 is_deeply([ @{$parts[0]}[1..2] ], [ 1, '1' ],
151 'got expected depth and level for part #0');
152 is($parts[1]->[0]->filename, 'embed2x.eml',
153 'attachment filename found');
154 is_deeply([ @{$parts[1]}[1..2] ], [ 1, '2' ],
155 'got expected depth and level for part #1');
156 is_deeply([ @{$parts[2]}[1..2] ], [ 2, '2.1' ],
157 'got expected depth and level for part #2');
158 is_deeply([ @{$parts[3]}[1..2] ], [ 3, '2.1.1' ],
159 'got expected depth and level for part #3');
160 is_deeply([ @{$parts[4]}[1..2] ], [ 3, '2.1.2' ],
161 'got expected depth and level for part #4');
162 is($parts[4]->[0]->filename, 'test.eml',
163 'another attachment filename found');
164 is_deeply([ @{$parts[5]}[1..2] ], [ 4, '2.1.2.1' ],
165 'got expected depth and level for part #5');
168 # body-less, boundary-less
169 for my $cls (@classes) {
171 $cls->new(<<'EOF')->each_part(sub { $call++ }, 0, 1);
172 Content-Type: multipart/mixed; boundary="body-less"
175 is($call, 1, 'called on bodyless multipart');
178 $cls->new(<<'EOF')->each_part(sub { push @tmp, \@_; }, 0, 1);
179 Content-Type: multipart/mixed; boundary="boundary-less"
183 is(scalar(@tmp), 1, 'got one part even w/o boundary');
184 is($tmp[0]->[0]->[0]->body, "hello world\n", 'body preserved');
185 is($tmp[0]->[0]->[1], 0, '$depth is zero');
186 is($tmp[0]->[0]->[2], 1, '@idx is one');
189 # I guess the following only worked in PI::M because of a happy accident
190 # involving inheritance:
191 for my $cls (@classes) {
193 my $header_less = <<'EOF';
194 Archived-At: <85k5su9k59.fsf_-_@lola.goethe.zz>
195 Content-Type: multipart/mixed; boundary="header-less"
208 my $expect = "this is the body\n";
209 $cls->new($header_less)->each_part(sub { push @tmp, \@_ }, 0, 1);
210 my $body = $tmp[0]->[0]->[0]->body;
211 if ($cls eq 'PublicInbox::Eml') {
212 is($body, $expect, 'body-only subpart in '.$cls);
213 } elsif ($body ne $expect) {
214 diag "W: $cls `$body' != `$expect'";
216 is($tmp[1]->[0]->[0]->body, "something else\n");
217 is(scalar(@tmp), 2, 'two parts');
220 if ('one newline before headers') {
221 my $eml = PublicInbox::Eml->new("\nNewline: no Header \n");
222 my @v = $eml->header_raw('Newline');
223 is_deeply(\@v, ['no Header'], 'no header');
224 is($eml->crlf, "\n", 'got CRLF as "\n"');
230 --- a/lib/PublicInbox/Eml.pm
231 +++ b/lib/PublicInbox/Eml.pm
232 @@ -122,9 +122,10 @@ sub new {
235 my $eml = PublicInbox::Eml->new($str);
236 is($eml->body, $str, 'body-only accepted');
239 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
241 Content-Type: multipart/mixed; boundary="b"
247 my $eml = $cls->new(\$s);
250 $eml->each_part(sub {
251 @v = $_[0]->[0]->header_raw('Header');
254 is($nr, 1, 'only one part');
255 is_deeply(\@v, [], "nothing w/o body $cls");
258 for my $cls (@classes) {
259 my $s = <<EOF; # double epilogue, double the fun
260 Content-Type: multipart/mixed; boundary="b"
275 my $eml = $cls->new(\$s);
277 $eml->each_part(sub {
278 my $part = $_[0]->[0];
279 is_deeply([$part->header_raw('should')], ['appear'],
280 'only got one header');
281 is($part->body, "yes\n", 'got expected body');
284 is($nr, 1, 'only one part');
287 for my $cls (@classes) {
289 skip 'newer Email::MIME behavior inconsistent', 1 if
290 $cls eq 'PublicInbox::MIME';
291 my $s = <<EOF; # buggy git-send-email versions, again?
292 Content-Type: text/plain; =?ISO-8859-1?Q?=20charset=3D=1BOF?=
293 Content-Transfer-Encoding: 8bit
294 Object-Id: ab0440d8cd6d843bee9a27709a459ce3b2bdb94d (lore/kvm)
298 my $eml = $cls->new(\$s);
299 my ($str, $err) = msg_part_text($eml, $eml->content_type);
300 is($str, "\x{100}\n", "got wide character by assuming utf-8 ($cls)");
304 if ('we differ from Email::MIME with final "\n" on missing epilogue') {
306 Content-Type: multipart/mixed; boundary="b"
313 my $eml = PublicInbox::Eml->new(\$s);
314 is(($eml->subparts)[-1]->body, "no epilogue\n",
315 'final "\n" preserved on missing epilogue');
318 if ('header_size_limit stolen from postfix') {
319 local $PublicInbox::Eml::header_size_limit = 4;
321 local $SIG{__WARN__} = sub { push @w, @_ };
322 my $eml = PublicInbox::Eml->new("a:b\na:d\n\nzz");
323 is_deeply([$eml->header('a')], ['b'], 'no overrun header');
324 is($eml->body_raw, 'zz', 'body not damaged');
325 is($eml->header_obj->as_string, "a:b\n", 'header truncated');
326 is(grep(/truncated/, @w), 1, 'truncation warned');
328 $eml = PublicInbox::Eml->new("a:b\na:d\n");
329 is_deeply([$eml->header('a')], ['b'], 'no overrun header w/o body');
331 local $PublicInbox::Eml::header_size_limit = 5;
332 $eml = PublicInbox::Eml->new("a:b\r\na:d\r\n\nzz");
333 is_deeply([$eml->header('a')], ['b'], 'no overrun header on CRLF');
334 is($eml->body_raw, 'zz', 'body not damaged');
337 $eml = PublicInbox::Eml->new("too:long\n");
338 $eml = PublicInbox::Eml->new("too:long\n\n");
339 $eml = PublicInbox::Eml->new("too:long\r\n\r\n");
340 is(grep(/ignored/, @w), 3, 'ignored header warned');
343 if ('maxparts is a feature unique to us') {
344 my $eml = eml_load 't/psgi_attach.eml';
346 $eml->each_part(sub { push @orig, $_[0]->[0] });
348 local $PublicInbox::Eml::mime_parts_limit = scalar(@orig);
350 $eml->each_part(sub {
351 my $cur = $_[0]->[0];
352 my $prv = $orig[$i++];
353 is($cur->body_raw, $prv->body_raw, "part #$i matches");
355 is($i, scalar(@orig), 'maxparts honored');
356 $PublicInbox::Eml::mime_parts_limit--;
358 $eml->each_part(sub { push @ltd, $_[0]->[0] });
359 for ($i = 0; $i <= $#ltd; $i++) {
360 is($ltd[$i]->body_raw, $orig[$i]->body_raw,
363 is(scalar(@ltd), scalar(@orig) - 1, 'maxparts honored');
367 require_mods('Email::MIME', 1);
368 my $eml = eml_load 't/utf8.eml';
369 my $mime = mime_load 't/utf8.eml';
370 for my $h (qw(Subject From To)) {
371 my $v = $eml->header($h);
372 my $m = $mime->header($h);
373 is($v, $m, "decoded -8 $h matches Email::MIME");
374 ok(utf8::is_utf8($v), "$h is UTF-8");
375 ok(utf8::valid($v), "UTF-8 valid $h");
377 my $s = $eml->body_str;
378 ok(utf8::is_utf8($s), 'body_str is UTF-8');
379 ok(utf8::valid($s), 'UTF-8 valid body_str');
380 my $ref = \(my $x = 'ref');
381 for my $msg ($eml, $mime) {
382 $msg->body_str_set($s .= "\nHI\n");
383 ok(!utf8::is_utf8($msg->body_raw),
384 'raw octets after body_str_set');
386 ok(utf8::is_utf8($s), 'body_str is UTF-8 after set');
387 ok(utf8::valid($s), 'UTF-8 valid body_str after set');
388 $msg->body_set($ref);
389 is($msg->body_raw, $$ref, 'body_set worked on scalar ref');
390 $msg->body_set($$ref);
391 is($msg->body_raw, $$ref, 'body_set worked on scalar');
393 $eml = eml_load 't/iso-2202-jp.eml';
394 $mime = mime_load 't/iso-2202-jp.eml';
396 is($s, $mime->body_str, 'ISO-2202-JP body_str');
397 ok(utf8::is_utf8($s), 'ISO-2202-JP => UTF-8 body_str');
398 ok(utf8::valid($s), 'UTF-8 valid body_str');
400 $eml = eml_load 't/psgi_attach.eml';
401 $mime = mime_load 't/psgi_attach.eml';
402 is_deeply([ map { $_->body_raw } $eml->subparts ],
403 [ map { $_->body_raw } $mime->subparts ],
404 'raw ->subparts match deeply');
405 is_deeply([ map { $_->body } $eml->subparts ],
406 [ map { $_->body } $mime->subparts ],
407 '->subparts match deeply');
408 for my $msg ($eml, $mime) {
409 my @old = $msg->subparts;
411 is_deeply([$msg->subparts], [], 'parts_set can clear');
412 $msg->parts_set([$old[-1]]);
413 is(scalar $msg->subparts, 1, 'only last remains');
416 # some versions of Email::MIME or Email::MIME::* will drop
417 # unnecessary ", while PublicInbox::Eml will preserve the original
418 my $exp = $mime->as_string;
419 $exp =~ s/; boundary=b\b/; boundary="b"/;
420 is($eml->as_string, $exp, 'as_string matches after parts_set');
423 for my $cls (@classes) {
425 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
426 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
429 is($cls->new($s)->filename, 'vtpm-makefile.patch',
430 "filename decoded ($cls)") if $cls ne 'PublicInbox::MIME';
431 $s =~ s/^Content-Disposition:.*$//sm;
432 is($cls->new($s)->filename, 'vtpm-fakefile.patch',
433 "filename fallback ($cls)") if $cls ne 'PublicInbox::MIME';
434 is($cls->new($s)->content_type,
435 'text/x-patch; name="vtpm-fakefile.patch"',
436 qq[matches Email::MIME output, "correct" or not ($cls)]);
439 Content-Type: multipart/foo; boundary=b
442 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
446 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
452 skip 'newer Email::MIME is inconsistent here', 1
453 if $cls eq 'PublicInbox::MIME';
455 $cls->new($s)->each_part(sub { push @x, $_[0]->[0]->filename });
456 is_deeply(['vtpm-makefile.patch', 'vtpm-fakefile.patch'], \@x,
457 "got filename for both attachments ($cls)");