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