2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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 push @classes, 'PublicInbox::MIME';
13 use_ok $_ for @classes;
16 my $eml = PublicInbox::Eml->new(\(my $str = "a: b\n\nhi\n"));
17 is($str, "hi\n", '->new modified body like Email::Simple');
18 is($eml->body, "hi\n", '->body works');
19 is($eml->as_string, "a: b\n\nhi\n", '->as_string');
22 for my $cls (@classes) {
23 my $mime = $cls->new(my $orig = "From: x\n\nb");
24 is($mime->as_string, $orig, '->as_string works');
25 is($mime->header_obj->as_string, "From: x\n",
26 'header ->as_string works');
29 is($mime->header_raw('From'), 'x', 'header_raw scalar context');
30 $mime = $cls->new("R:\n\tx\nR:\n 1\n");
31 is_deeply([$mime->header_raw('r')], [ 'x', '1' ], 'multi-value');
32 $mime = $cls->new("R:x\nR: 1\n");
33 is_deeply([$mime->header_raw('r')], [ 'x', '1' ], 'multi-value header');
34 $mime = $cls->new("R:x\n R: 1\nR:\n f\n");
35 is_deeply([$mime->header_raw('r')], [ 'x R: 1', 'f' ],
36 'multi-line, multi-value header');
38 $mime->header_set('r');
39 is_deeply([$mime->header_raw('r')], [], 'header_set clears');
40 $mime->header_set('r');
41 is_deeply([$mime->header_raw('r')], [], 'header_set clears idempotent');
42 $mime->header_set('r', 'h');
43 is_deeply([$mime->header_raw('r')], ['h'], 'header_set');
44 $mime->header_set('r', 'h', 'i');
45 is_deeply([$mime->header_raw('r')], ['h', 'i'], 'header_set ary');
46 $mime->header_set('rr', 'b');
47 is_deeply([$mime->header_raw('r')], ['h', 'i'],
48 "header_set `rr' did not clobber `r'");
49 is($mime->header_raw('rr'), 'b', 'got set scalar');
50 $mime->header_set('rr', 'b'x100);
51 is($mime->header_raw('rr'), 'b'x100, 'got long set scalar');
52 if ($cls eq 'PublicInbox::Eml') {
53 like($mime->as_string, qr/^rr: b{100}\n(?:\n|\z)/sm,
54 'single token not wrapped');
56 $mime->header_set('rr', ('b'x100) . ' wrap me');
57 if ($cls eq 'PublicInbox::Eml') {
58 like($mime->as_string, qr/^rr: b{100}\n\twrap me\n/sm,
59 'wrapped after long token');
61 my $exp = "pre\tformatted\n with\n breaks";
62 $mime->header_set('r', $exp);
63 like($mime->as_string, qr/^r: \Q$exp\E/sm, 'preformatted preserved');
66 for my $cls (@classes) { # make sure we don't add quotes if not needed
67 my $eml = $cls->new("From: John Smith <j\@example.com>\n\n");
68 is($eml->header('From'), 'John Smith <j@example.com>',
69 "name not unnecessarily quoted $cls");
72 for my $cls (@classes) {
73 my $eml = $cls->new("Subject: foo\n\n");
74 $eml->header_str_set('Subject', "\x{100}");
75 like($eml->header_raw('Subject'), qr/utf-8\?B\?/i,
76 'MIME-B encoded UTF-8 Subject');
77 is_deeply([$eml->header_str('Subject')], [ "\x{100}" ],
78 'got wide character back');
81 # linux-mips apparently got some messages injected w/o Message-ID
82 # and long Subject: lines w/o leading whitespace.
83 # What appears in the blobs was generated by V2Writable.
84 for my $cls (@classes) {
85 my $eml = $cls->new(<<'EOF');
86 Message-ID: <20101130193431@z>
87 Subject: something really long
89 From: linux-mips archive injection
90 Object-Id: 8c56b7abdd551b1264e6522ededbbed9890cccd0
92 is_deeply([ $eml->header('Subject') ],
93 [ 'something really long and really wrong' ],
94 'continued long line w/o leading spaces '.$cls);
95 is_deeply([ $eml->header('From') ],
96 [ 'linux-mips archive injection' ],
97 'subsequent line not corrupted');
98 is_deeply([ $eml->header('Message-ID') ],
99 ['<20101130193431@z>'],
100 'preceding line readable');
104 my $eml = eml_load 't/msg_iter-order.eml';
106 my $orig = $eml->as_string;
107 $eml->each_part(sub {
108 my ($part, $level, @ex) = @{$_[0]};
109 my $s = $part->body_str;
111 push @parts, [ $s, $level, @ex ];
113 is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine');
114 is($eml->as_string, $orig, 'unchanged by ->each_part');
115 $eml->each_part(sub {}, undef, 1);
116 is(defined($eml) ? $eml->body_raw : '', # old msg_iter clobbers $eml
117 '', 'each_part can clobber body');
120 if ('descend into message/rfc822') {
121 my $eml = eml_load 't/data/message_embed.eml';
123 $eml->each_part(sub {
124 my ($part, $level, @ex) = @{$_[0]};
125 push @parts, [ $part, $level, @ex ];
127 is(scalar(@parts), 6, 'got all parts');
128 like($parts[0]->[0]->body, qr/^testing embedded message harder\n/sm,
130 is_deeply([ @{$parts[0]}[1..2] ], [ 1, '1' ],
131 'got expected depth and level for part #0');
132 is($parts[1]->[0]->filename, 'embed2x.eml',
133 'attachment filename found');
134 is_deeply([ @{$parts[1]}[1..2] ], [ 1, '2' ],
135 'got expected depth and level for part #1');
136 is_deeply([ @{$parts[2]}[1..2] ], [ 2, '2.1' ],
137 'got expected depth and level for part #2');
138 is_deeply([ @{$parts[3]}[1..2] ], [ 3, '2.1.1' ],
139 'got expected depth and level for part #3');
140 is_deeply([ @{$parts[4]}[1..2] ], [ 3, '2.1.2' ],
141 'got expected depth and level for part #4');
142 is($parts[4]->[0]->filename, 'test.eml',
143 'another attachment filename found');
144 is_deeply([ @{$parts[5]}[1..2] ], [ 4, '2.1.2.1' ],
145 'got expected depth and level for part #5');
148 # body-less, boundary-less
149 for my $cls (@classes) {
151 $cls->new(<<'EOF')->each_part(sub { $call++ }, 0, 1);
152 Content-Type: multipart/mixed; boundary="body-less"
155 is($call, 1, 'called on bodyless multipart');
158 $cls->new(<<'EOF')->each_part(sub { push @tmp, \@_; }, 0, 1);
159 Content-Type: multipart/mixed; boundary="boundary-less"
163 is(scalar(@tmp), 1, 'got one part even w/o boundary');
164 is($tmp[0]->[0]->[0]->body, "hello world\n", 'body preserved');
165 is($tmp[0]->[0]->[1], 0, '$depth is zero');
166 is($tmp[0]->[0]->[2], 0, '@idx is zero');
169 # I guess the following only worked in PI::M because of a happy accident
170 # involving inheritance:
171 for my $cls (@classes) {
173 my $header_less = <<'EOF';
174 Archived-At: <85k5su9k59.fsf_-_@lola.goethe.zz>
175 Content-Type: multipart/mixed; boundary="header-less"
188 my $expect = "this is the body\n";
189 $cls->new($header_less)->each_part(sub { push @tmp, \@_ }, 0, 1);
190 my $body = $tmp[0]->[0]->[0]->body;
191 if ($cls eq 'PublicInbox::Eml') {
192 is($body, $expect, 'body-only subpart in '.$cls);
193 } elsif ($body ne $expect) {
194 diag "W: $cls `$body' != `$expect'";
196 is($tmp[1]->[0]->[0]->body, "something else\n");
197 is(scalar(@tmp), 2, 'two parts');
200 if ('one newline before headers') {
201 my $eml = PublicInbox::Eml->new("\nNewline: no Header \n");
202 my @v = $eml->header_raw('Newline');
203 is_deeply(\@v, ['no Header'], 'no header');
204 is($eml->crlf, "\n", 'got CRLF as "\n"');
208 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
210 Content-Type: multipart/mixed; boundary="b"
216 my $eml = $cls->new(\$s);
219 $eml->each_part(sub {
220 @v = $_[0]->[0]->header_raw('Header');
223 is($nr, 1, 'only one part');
224 is_deeply(\@v, [], "nothing w/o body $cls");
227 for my $cls (@classes) {
228 my $s = <<EOF; # double epilogue, double the fun
229 Content-Type: multipart/mixed; boundary="b"
244 my $eml = $cls->new(\$s);
246 $eml->each_part(sub {
247 my $part = $_[0]->[0];
248 is_deeply([$part->header_raw('should')], ['appear'],
249 'only got one header');
250 is($part->body, "yes\n", 'got expected body');
253 is($nr, 1, 'only one part');
256 for my $cls (@classes) {
257 my $s = <<EOF; # buggy git-send-email versions, again?
258 Content-Type: text/plain; =?ISO-8859-1?Q?=20charset=3D=1BOF?=
259 Content-Transfer-Encoding: 8bit
260 Object-Id: ab0440d8cd6d843bee9a27709a459ce3b2bdb94d (lore/kvm)
264 my $eml = $cls->new(\$s);
265 my ($str, $err) = msg_part_text($eml, $eml->content_type);
266 is($str, "\x{100}\n", "got wide character by assuming utf-8");
269 if ('we differ from Email::MIME with final "\n" on missing epilogue') {
271 Content-Type: multipart/mixed; boundary="b"
278 my $eml = PublicInbox::Eml->new(\$s);
279 is(($eml->subparts)[-1]->body, "no epilogue\n",
280 'final "\n" preserved on missing epilogue');
283 if ('header_size_limit stolen from postfix') {
284 local $PublicInbox::Eml::header_size_limit = 4;
286 local $SIG{__WARN__} = sub { push @w, @_ };
287 my $eml = PublicInbox::Eml->new("a:b\na:d\n\nzz");
288 is_deeply([$eml->header('a')], ['b'], 'no overrun header');
289 is($eml->body_raw, 'zz', 'body not damaged');
290 is($eml->header_obj->as_string, "a:b\n", 'header truncated');
291 is(grep(/truncated/, @w), 1, 'truncation warned');
293 $eml = PublicInbox::Eml->new("a:b\na:d\n");
294 is_deeply([$eml->header('a')], ['b'], 'no overrun header w/o body');
296 local $PublicInbox::Eml::header_size_limit = 5;
297 $eml = PublicInbox::Eml->new("a:b\r\na:d\r\n\nzz");
298 is_deeply([$eml->header('a')], ['b'], 'no overrun header on CRLF');
299 is($eml->body_raw, 'zz', 'body not damaged');
302 $eml = PublicInbox::Eml->new("too:long\n");
303 $eml = PublicInbox::Eml->new("too:long\n\n");
304 $eml = PublicInbox::Eml->new("too:long\r\n\r\n");
305 is(grep(/ignored/, @w), 3, 'ignored header warned');
308 if ('maxparts is a feature unique to us') {
309 my $eml = eml_load 't/psgi_attach.eml';
311 $eml->each_part(sub { push @orig, $_[0]->[0] });
313 local $PublicInbox::Eml::mime_parts_limit = scalar(@orig);
315 $eml->each_part(sub {
316 my $cur = $_[0]->[0];
317 my $prv = $orig[$i++];
318 is($cur->body_raw, $prv->body_raw, "part #$i matches");
320 is($i, scalar(@orig), 'maxparts honored');
321 $PublicInbox::Eml::mime_parts_limit--;
323 $eml->each_part(sub { push @ltd, $_[0]->[0] });
324 for ($i = 0; $i <= $#ltd; $i++) {
325 is($ltd[$i]->body_raw, $orig[$i]->body_raw,
328 is(scalar(@ltd), scalar(@orig) - 1, 'maxparts honored');
332 require_mods('PublicInbox::MIME', 1);
333 my $eml = eml_load 't/utf8.eml';
334 my $mime = mime_load 't/utf8.eml';
335 for my $h (qw(Subject From To)) {
336 my $v = $eml->header($h);
337 my $m = $mime->header($h);
338 is($v, $m, "decoded -8 $h matches Email::MIME");
339 ok(utf8::is_utf8($v), "$h is UTF-8");
340 ok(utf8::valid($v), "UTF-8 valid $h");
342 my $s = $eml->body_str;
343 ok(utf8::is_utf8($s), 'body_str is UTF-8');
344 ok(utf8::valid($s), 'UTF-8 valid body_str');
345 my $ref = \(my $x = 'ref');
346 for my $msg ($eml, $mime) {
347 $msg->body_str_set($s .= "\nHI\n");
348 ok(!utf8::is_utf8($msg->body_raw),
349 'raw octets after body_str_set');
351 ok(utf8::is_utf8($s), 'body_str is UTF-8 after set');
352 ok(utf8::valid($s), 'UTF-8 valid body_str after set');
353 $msg->body_set($ref);
354 is($msg->body_raw, $$ref, 'body_set worked on scalar ref');
355 $msg->body_set($$ref);
356 is($msg->body_raw, $$ref, 'body_set worked on scalar');
358 $eml = eml_load 't/iso-2202-jp.eml';
359 $mime = mime_load 't/iso-2202-jp.eml';
361 is($s, $mime->body_str, 'ISO-2202-JP body_str');
362 ok(utf8::is_utf8($s), 'ISO-2202-JP => UTF-8 body_str');
363 ok(utf8::valid($s), 'UTF-8 valid body_str');
365 $eml = eml_load 't/psgi_attach.eml';
366 $mime = mime_load 't/psgi_attach.eml';
367 is_deeply([ map { $_->body_raw } $eml->subparts ],
368 [ map { $_->body_raw } $mime->subparts ],
369 'raw ->subparts match deeply');
370 is_deeply([ map { $_->body } $eml->subparts ],
371 [ map { $_->body } $mime->subparts ],
372 '->subparts match deeply');
373 for my $msg ($eml, $mime) {
374 my @old = $msg->subparts;
376 is_deeply([$msg->subparts], [], 'parts_set can clear');
377 $msg->parts_set([$old[-1]]);
378 is(scalar $msg->subparts, 1, 'only last remains');
380 is($eml->as_string, $mime->as_string,
381 'as_string matches after parts_set');
384 for my $cls (@classes) {
386 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
387 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
390 is($cls->new($s)->filename, 'vtpm-makefile.patch', 'filename decoded');
391 $s =~ s/^Content-Disposition:.*$//sm;
392 is($cls->new($s)->filename, 'vtpm-fakefile.patch', 'filename fallback');
393 is($cls->new($s)->content_type,
394 'text/x-patch; name="vtpm-fakefile.patch"',
395 'matches Email::MIME output, "correct" or not');
398 Content-Type: multipart/foo; boundary=b
401 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
405 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
411 $cls->new($s)->each_part(sub { push @tmp, $_[0]->[0]->filename });
412 is_deeply(['vtpm-makefile.patch', 'vtpm-fakefile.patch'], \@tmp,
413 'got filename for both attachments');