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 # body-less, boundary-less
121 for my $cls (@classes) {
123 $cls->new(<<'EOF')->each_part(sub { $call++ }, 0, 1);
124 Content-Type: multipart/mixed; boundary="body-less"
127 is($call, 1, 'called on bodyless multipart');
130 $cls->new(<<'EOF')->each_part(sub { push @tmp, \@_; }, 0, 1);
131 Content-Type: multipart/mixed; boundary="boundary-less"
135 is(scalar(@tmp), 1, 'got one part even w/o boundary');
136 is($tmp[0]->[0]->[0]->body, "hello world\n", 'body preserved');
137 is($tmp[0]->[0]->[1], 0, '$depth is zero');
138 is($tmp[0]->[0]->[2], 0, '@idx is zero');
141 # I guess the following only worked in PI::M because of a happy accident
142 # involving inheritance:
143 for my $cls (@classes) {
145 my $header_less = <<'EOF';
146 Archived-At: <85k5su9k59.fsf_-_@lola.goethe.zz>
147 Content-Type: multipart/mixed; boundary="header-less"
160 my $expect = "this is the body\n";
161 $cls->new($header_less)->each_part(sub { push @tmp, \@_ }, 0, 1);
162 my $body = $tmp[0]->[0]->[0]->body;
163 if ($cls eq 'PublicInbox::Eml') {
164 is($body, $expect, 'body-only subpart in '.$cls);
165 } elsif ($body ne $expect) {
166 diag "W: $cls `$body' != `$expect'";
168 is($tmp[1]->[0]->[0]->body, "something else\n");
169 is(scalar(@tmp), 2, 'two parts');
172 if ('one newline before headers') {
173 my $eml = PublicInbox::Eml->new("\nNewline: no Header \n");
174 my @v = $eml->header_raw('Newline');
175 is_deeply(\@v, ['no Header'], 'no header');
176 is($eml->crlf, "\n", 'got CRLF as "\n"');
180 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
182 Content-Type: multipart/mixed; boundary="b"
188 my $eml = $cls->new(\$s);
191 $eml->each_part(sub {
192 @v = $_[0]->[0]->header_raw('Header');
195 is($nr, 1, 'only one part');
196 is_deeply(\@v, [], "nothing w/o body $cls");
199 for my $cls (@classes) {
200 my $s = <<EOF; # double epilogue, double the fun
201 Content-Type: multipart/mixed; boundary="b"
216 my $eml = $cls->new(\$s);
218 $eml->each_part(sub {
219 my $part = $_[0]->[0];
220 is_deeply([$part->header_raw('should')], ['appear'],
221 'only got one header');
222 is($part->body, "yes\n", 'got expected body');
225 is($nr, 1, 'only one part');
228 for my $cls (@classes) {
229 my $s = <<EOF; # buggy git-send-email versions, again?
230 Content-Type: text/plain; =?ISO-8859-1?Q?=20charset=3D=1BOF?=
231 Content-Transfer-Encoding: 8bit
232 Object-Id: ab0440d8cd6d843bee9a27709a459ce3b2bdb94d (lore/kvm)
236 my $eml = $cls->new(\$s);
237 my ($str, $err) = msg_part_text($eml, $eml->content_type);
238 is($str, "\x{100}\n", "got wide character by assuming utf-8");
241 if ('we differ from Email::MIME with final "\n" on missing epilogue') {
243 Content-Type: multipart/mixed; boundary="b"
250 my $eml = PublicInbox::Eml->new(\$s);
251 is(($eml->subparts)[-1]->body, "no epilogue\n",
252 'final "\n" preserved on missing epilogue');
255 if ('maxparts is a feature unique to us') {
256 my $eml = eml_load 't/psgi_attach.eml';
258 $eml->each_part(sub { push @orig, $_[0]->[0] });
260 local $PublicInbox::Eml::MAXPARTS = scalar(@orig);
262 $eml->each_part(sub {
263 my $cur = $_[0]->[0];
264 my $prv = $orig[$i++];
265 is($cur->body_raw, $prv->body_raw, "part #$i matches");
267 is($i, scalar(@orig), 'maxparts honored');
268 $PublicInbox::Eml::MAXPARTS--;
270 $eml->each_part(sub { push @ltd, $_[0]->[0] });
271 for ($i = 0; $i <= $#ltd; $i++) {
272 is($ltd[$i]->body_raw, $orig[$i]->body_raw,
275 is(scalar(@ltd), scalar(@orig) - 1, 'maxparts honored');
279 require_mods('PublicInbox::MIME', 1);
280 my $eml = eml_load 't/utf8.eml';
281 my $mime = mime_load 't/utf8.eml';
282 for my $h (qw(Subject From To)) {
283 my $v = $eml->header($h);
284 my $m = $mime->header($h);
285 is($v, $m, "decoded -8 $h matches Email::MIME");
286 ok(utf8::is_utf8($v), "$h is UTF-8");
287 ok(utf8::valid($v), "UTF-8 valid $h");
289 my $s = $eml->body_str;
290 ok(utf8::is_utf8($s), 'body_str is UTF-8');
291 ok(utf8::valid($s), 'UTF-8 valid body_str');
292 my $ref = \(my $x = 'ref');
293 for my $msg ($eml, $mime) {
294 $msg->body_str_set($s .= "\nHI\n");
295 ok(!utf8::is_utf8($msg->body_raw),
296 'raw octets after body_str_set');
298 ok(utf8::is_utf8($s), 'body_str is UTF-8 after set');
299 ok(utf8::valid($s), 'UTF-8 valid body_str after set');
300 $msg->body_set($ref);
301 is($msg->body_raw, $$ref, 'body_set worked on scalar ref');
302 $msg->body_set($$ref);
303 is($msg->body_raw, $$ref, 'body_set worked on scalar');
305 $eml = eml_load 't/iso-2202-jp.eml';
306 $mime = mime_load 't/iso-2202-jp.eml';
308 is($s, $mime->body_str, 'ISO-2202-JP body_str');
309 ok(utf8::is_utf8($s), 'ISO-2202-JP => UTF-8 body_str');
310 ok(utf8::valid($s), 'UTF-8 valid body_str');
312 $eml = eml_load 't/psgi_attach.eml';
313 $mime = mime_load 't/psgi_attach.eml';
314 is_deeply([ map { $_->body_raw } $eml->subparts ],
315 [ map { $_->body_raw } $mime->subparts ],
316 'raw ->subparts match deeply');
317 is_deeply([ map { $_->body } $eml->subparts ],
318 [ map { $_->body } $mime->subparts ],
319 '->subparts match deeply');
320 for my $msg ($eml, $mime) {
321 my @old = $msg->subparts;
323 is_deeply([$msg->subparts], [], 'parts_set can clear');
324 $msg->parts_set([$old[-1]]);
325 is(scalar $msg->subparts, 1, 'only last remains');
327 is($eml->as_string, $mime->as_string,
328 'as_string matches after parts_set');
331 for my $cls (@classes) {
333 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
334 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
337 is($cls->new($s)->filename, 'vtpm-makefile.patch', 'filename decoded');
338 $s =~ s/^Content-Disposition:.*$//sm;
339 is($cls->new($s)->filename, 'vtpm-fakefile.patch', 'filename fallback');
340 is($cls->new($s)->content_type,
341 'text/x-patch; name="vtpm-fakefile.patch"',
342 'matches Email::MIME output, "correct" or not');
345 Content-Type: multipart/foo; boundary=b
348 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
352 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
358 $cls->new($s)->each_part(sub { push @tmp, $_[0]->[0]->filename });
359 is_deeply(['vtpm-makefile.patch', 'vtpm-fakefile.patch'], \@tmp,
360 'got filename for both attachments');