X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Feml.t;h=2d8993a51075f317ce11f5cc61012d55211663d6;hb=10e0e2052b2c2a4bb405cfbb4c2c72464671152d;hp=43c735e76b9e466aa3d62f256492e36540608743;hpb=57af9c8d0bedafac3267b5b42f963bb8aa5c2ea1;p=public-inbox.git
diff --git a/t/eml.t b/t/eml.t
index 43c735e7..2d8993a5 100644
--- a/t/eml.t
+++ b/t/eml.t
@@ -1,5 +1,5 @@
#!perl -w
-# Copyright (C) 2020 all contributors
+# Copyright (C) 2020-2021 all contributors
# License: AGPL-3.0+
use strict;
use Test::More;
@@ -8,15 +8,26 @@ use PublicInbox::MsgIter qw(msg_part_text);
my @classes = qw(PublicInbox::Eml);
SKIP: {
require_mods('Email::MIME', 1);
+ # TODO: Email::MIME behavior is not consistent in newer versions
+ # we need to evaluate and possibly adjust our behavior to decide
+ # between DWIM-ness with historical mail...
push @classes, 'PublicInbox::MIME';
};
use_ok $_ for @classes;
+sub mime_load ($) {
+ my ($path) = @_;
+ open(my $fh, '<', $path) or die "open $path: $!";
+ PublicInbox::MIME->new(\(do { local $/; <$fh> }));
+}
+
{
my $eml = PublicInbox::Eml->new(\(my $str = "a: b\n\nhi\n"));
is($str, "hi\n", '->new modified body like Email::Simple');
is($eml->body, "hi\n", '->body works');
is($eml->as_string, "a: b\n\nhi\n", '->as_string');
+ my $empty = PublicInbox::Eml->new("\n\n");
+ is($empty->as_string, "\n\n", 'empty message');
}
for my $cls (@classes) {
@@ -74,7 +85,7 @@ for my $cls (@classes) {
$eml->header_str_set('Subject', "\x{100}");
like($eml->header_raw('Subject'), qr/utf-8\?B\?/i,
'MIME-B encoded UTF-8 Subject');
- is_deeply([$eml->header_str('Subject')], [ "\x{100}" ],
+ is_deeply([$eml->header('Subject')], [ "\x{100}" ],
'got wide character back');
}
@@ -117,6 +128,34 @@ EOF
'', 'each_part can clobber body');
}
+if ('descend into message/rfc822') {
+ my $eml = eml_load 't/data/message_embed.eml';
+ my @parts;
+ $eml->each_part(sub {
+ my ($part, $level, @ex) = @{$_[0]};
+ push @parts, [ $part, $level, @ex ];
+ });
+ is(scalar(@parts), 6, 'got all parts');
+ like($parts[0]->[0]->body, qr/^testing embedded message harder\n/sm,
+ 'first part found');
+ is_deeply([ @{$parts[0]}[1..2] ], [ 1, '1' ],
+ 'got expected depth and level for part #0');
+ is($parts[1]->[0]->filename, 'embed2x.eml',
+ 'attachment filename found');
+ is_deeply([ @{$parts[1]}[1..2] ], [ 1, '2' ],
+ 'got expected depth and level for part #1');
+ is_deeply([ @{$parts[2]}[1..2] ], [ 2, '2.1' ],
+ 'got expected depth and level for part #2');
+ is_deeply([ @{$parts[3]}[1..2] ], [ 3, '2.1.1' ],
+ 'got expected depth and level for part #3');
+ is_deeply([ @{$parts[4]}[1..2] ], [ 3, '2.1.2' ],
+ 'got expected depth and level for part #4');
+ is($parts[4]->[0]->filename, 'test.eml',
+ 'another attachment filename found');
+ is_deeply([ @{$parts[5]}[1..2] ], [ 4, '2.1.2.1' ],
+ 'got expected depth and level for part #5');
+}
+
# body-less, boundary-less
for my $cls (@classes) {
my $call = 0;
@@ -135,7 +174,7 @@ EOF
is(scalar(@tmp), 1, 'got one part even w/o boundary');
is($tmp[0]->[0]->[0]->body, "hello world\n", 'body preserved');
is($tmp[0]->[0]->[1], 0, '$depth is zero');
- is($tmp[0]->[0]->[2], 0, '@idx is zero');
+ is($tmp[0]->[0]->[2], 1, '@idx is one');
}
# I guess the following only worked in PI::M because of a happy accident
@@ -177,6 +216,17 @@ if ('one newline before headers') {
is($eml->body, "");
}
+if ('body only') {
+ my $str = <new($str);
+ is($eml->body, $str, 'body-only accepted');
+}
+
for my $cls (@classes) { # XXX: matching E::M, but not sure about this
my $s = <new(\$s);
my ($str, $err) = msg_part_text($eml, $eml->content_type);
- is($str, "\x{100}\n", "got wide character by assuming utf-8");
+ is($str, "\x{100}\n", "got wide character by assuming utf-8 ($cls)");
+} # SKIP
}
if ('we differ from Email::MIME with final "\n" on missing epilogue') {
@@ -252,12 +306,37 @@ EOF
'final "\n" preserved on missing epilogue');
}
+if ('header_size_limit stolen from postfix') {
+ local $PublicInbox::Eml::header_size_limit = 4;
+ my @w;
+ local $SIG{__WARN__} = sub { push @w, @_ };
+ my $eml = PublicInbox::Eml->new("a:b\na:d\n\nzz");
+ is_deeply([$eml->header('a')], ['b'], 'no overrun header');
+ is($eml->body_raw, 'zz', 'body not damaged');
+ is($eml->header_obj->as_string, "a:b\n", 'header truncated');
+ is(grep(/truncated/, @w), 1, 'truncation warned');
+
+ $eml = PublicInbox::Eml->new("a:b\na:d\n");
+ is_deeply([$eml->header('a')], ['b'], 'no overrun header w/o body');
+
+ local $PublicInbox::Eml::header_size_limit = 5;
+ $eml = PublicInbox::Eml->new("a:b\r\na:d\r\n\nzz");
+ is_deeply([$eml->header('a')], ['b'], 'no overrun header on CRLF');
+ is($eml->body_raw, 'zz', 'body not damaged');
+
+ @w = ();
+ $eml = PublicInbox::Eml->new("too:long\n");
+ $eml = PublicInbox::Eml->new("too:long\n\n");
+ $eml = PublicInbox::Eml->new("too:long\r\n\r\n");
+ is(grep(/ignored/, @w), 3, 'ignored header warned');
+}
+
if ('maxparts is a feature unique to us') {
my $eml = eml_load 't/psgi_attach.eml';
my @orig;
$eml->each_part(sub { push @orig, $_[0]->[0] });
- local $PublicInbox::Eml::MAXPARTS = scalar(@orig);
+ local $PublicInbox::Eml::mime_parts_limit = scalar(@orig);
my $i = 0;
$eml->each_part(sub {
my $cur = $_[0]->[0];
@@ -265,7 +344,7 @@ if ('maxparts is a feature unique to us') {
is($cur->body_raw, $prv->body_raw, "part #$i matches");
});
is($i, scalar(@orig), 'maxparts honored');
- $PublicInbox::Eml::MAXPARTS--;
+ $PublicInbox::Eml::mime_parts_limit--;
my @ltd;
$eml->each_part(sub { push @ltd, $_[0]->[0] });
for ($i = 0; $i <= $#ltd; $i++) {
@@ -324,8 +403,12 @@ SKIP: {
$msg->parts_set([$old[-1]]);
is(scalar $msg->subparts, 1, 'only last remains');
}
- is($eml->as_string, $mime->as_string,
- 'as_string matches after parts_set');
+
+ # some versions of Email::MIME or Email::MIME::* will drop
+ # unnecessary ", while PublicInbox::Eml will preserve the original
+ my $exp = $mime->as_string;
+ $exp =~ s/; boundary=b\b/; boundary="b"/;
+ is($eml->as_string, $exp, 'as_string matches after parts_set');
}
for my $cls (@classes) {
@@ -336,7 +419,8 @@ Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
EOF
is($cls->new($s)->filename, 'vtpm-makefile.patch', 'filename decoded');
$s =~ s/^Content-Disposition:.*$//sm;
- is($cls->new($s)->filename, 'vtpm-fakefile.patch', 'filename fallback');
+ is($cls->new($s)->filename, 'vtpm-fakefile.patch',
+ "filename fallback ($cls)") if $cls ne 'PublicInbox::MIME';
is($cls->new($s)->content_type,
'text/x-patch; name="vtpm-fakefile.patch"',
'matches Email::MIME output, "correct" or not');
@@ -354,10 +438,14 @@ Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
b
--b--
EOF
- my @tmp;
- $cls->new($s)->each_part(sub { push @tmp, $_[0]->[0]->filename });
- is_deeply(['vtpm-makefile.patch', 'vtpm-fakefile.patch'], \@tmp,
- 'got filename for both attachments');
+ SKIP: {
+ skip 'newer Email::MIME is inconsistent here', 1
+ if $cls eq 'PublicInbox::MIME';
+ my @x;
+ $cls->new($s)->each_part(sub { push @x, $_[0]->[0]->filename });
+ is_deeply(['vtpm-makefile.patch', 'vtpm-fakefile.patch'], \@x,
+ "got filename for both attachments ($cls)");
+ }
}
done_testing;