]> Sergey Matveev's repositories - public-inbox.git/blob - t/eml.t
descend into message/(rfc822|news|global) parts
[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 {
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');
20 }
21
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');
27
28         # headers
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');
37
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');
55         }
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');
60         }
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');
64 } # for @classes
65
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");
70 }
71
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');
79 }
80
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
88 and really wrong
89 From: linux-mips archive injection
90 Object-Id: 8c56b7abdd551b1264e6522ededbbed9890cccd0
91 EOF
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');
101 } # for @classes
102
103 {
104         my $eml = eml_load 't/msg_iter-order.eml';
105         my @parts;
106         my $orig = $eml->as_string;
107         $eml->each_part(sub {
108                 my ($part, $level, @ex) = @{$_[0]};
109                 my $s = $part->body_str;
110                 $s =~ s/\s+//sg;
111                 push @parts, [ $s, $level, @ex ];
112         });
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');
118 }
119
120 if ('descend into message/rfc822') {
121         my $eml = eml_load 't/data/message_embed.eml';
122         my @parts;
123         $eml->each_part(sub {
124                 my ($part, $level, @ex) = @{$_[0]};
125                 push @parts, [ $part, $level, @ex ];
126         });
127         is(scalar(@parts), 6, 'got all parts');
128         like($parts[0]->[0]->body, qr/^testing embedded message harder\n/sm,
129                 'first part found');
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');
146 }
147
148 # body-less, boundary-less
149 for my $cls (@classes) {
150         my $call = 0;
151         $cls->new(<<'EOF')->each_part(sub { $call++ }, 0, 1);
152 Content-Type: multipart/mixed; boundary="body-less"
153
154 EOF
155         is($call, 1, 'called on bodyless multipart');
156
157         my @tmp;
158         $cls->new(<<'EOF')->each_part(sub { push @tmp, \@_; }, 0, 1);
159 Content-Type: multipart/mixed; boundary="boundary-less"
160
161 hello world
162 EOF
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');
167 }
168
169 # I guess the following only worked in PI::M because of a happy accident
170 # involving inheritance:
171 for my $cls (@classes) {
172         my @tmp;
173         my $header_less = <<'EOF';
174 Archived-At: <85k5su9k59.fsf_-_@lola.goethe.zz>
175 Content-Type: multipart/mixed; boundary="header-less"
176
177 --header-less
178
179 this is the body
180
181 --header-less
182 i-haz: header
183
184 something else
185
186 --header-less--
187 EOF
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'";
195         }
196         is($tmp[1]->[0]->[0]->body, "something else\n");
197         is(scalar(@tmp), 2, 'two parts');
198 }
199
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"');
205         is($eml->body, "");
206 }
207
208 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
209         my $s = <<EOF;
210 Content-Type: multipart/mixed; boundary="b"
211
212 --b
213 header: only
214 --b--
215 EOF
216         my $eml = $cls->new(\$s);
217         my $nr = 0;
218         my @v;
219         $eml->each_part(sub {
220                 @v = $_[0]->[0]->header_raw('Header');
221                 $nr++;
222         });
223         is($nr, 1, 'only one part');
224         is_deeply(\@v, [], "nothing w/o body $cls");
225 }
226
227 for my $cls (@classes) {
228         my $s = <<EOF; # double epilogue, double the fun
229 Content-Type: multipart/mixed; boundary="b"
230
231 --b
232 should: appear
233
234 yes
235
236 --b--
237
238 --b
239 should: not appear
240
241 nope
242 --b--
243 EOF
244         my $eml = $cls->new(\$s);
245         my $nr = 0;
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');
251                 $nr++;
252         });
253         is($nr, 1, 'only one part');
254 }
255
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)
261
262 \xc4\x80
263 EOF
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");
267 }
268
269 if ('we differ from Email::MIME with final "\n" on missing epilogue') {
270         my $s = <<EOF;
271 Content-Type: multipart/mixed; boundary="b"
272
273 --b
274 header: but
275
276 no epilogue
277 EOF
278         my $eml = PublicInbox::Eml->new(\$s);
279         is(($eml->subparts)[-1]->body, "no epilogue\n",
280                 'final "\n" preserved on missing epilogue');
281 }
282
283 if ('header_size_limit stolen from postfix') {
284         local $PublicInbox::Eml::header_size_limit = 4;
285         my @w;
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');
292
293         $eml = PublicInbox::Eml->new("a:b\na:d\n");
294         is_deeply([$eml->header('a')], ['b'], 'no overrun header w/o body');
295
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');
300
301         @w = ();
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');
306 }
307
308 if ('maxparts is a feature unique to us') {
309         my $eml = eml_load 't/psgi_attach.eml';
310         my @orig;
311         $eml->each_part(sub { push @orig, $_[0]->[0] });
312
313         local $PublicInbox::Eml::mime_parts_limit = scalar(@orig);
314         my $i = 0;
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");
319         });
320         is($i, scalar(@orig), 'maxparts honored');
321         $PublicInbox::Eml::mime_parts_limit--;
322         my @ltd;
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,
326                         "part[$i] matches");
327         }
328         is(scalar(@ltd), scalar(@orig) - 1, 'maxparts honored');
329 }
330
331 SKIP: {
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");
341         }
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');
350                 $s = $msg->body_str;
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');
357         }
358         $eml = eml_load 't/iso-2202-jp.eml';
359         $mime = mime_load 't/iso-2202-jp.eml';
360         $s = $eml->body_str;
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');
364
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;
375                 $msg->parts_set([]);
376                 is_deeply([$msg->subparts], [], 'parts_set can clear');
377                 $msg->parts_set([$old[-1]]);
378                 is(scalar $msg->subparts, 1, 'only last remains');
379         }
380         is($eml->as_string, $mime->as_string,
381                 'as_string matches after parts_set');
382 }
383
384 for my $cls (@classes) {
385         my $s = <<'EOF';
386 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
387 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
388
389 EOF
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');
396
397         $s = <<'EOF';
398 Content-Type: multipart/foo; boundary=b
399
400 --b
401 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
402
403 a
404 --b
405 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
406
407 b
408 --b--
409 EOF
410         my @tmp;
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');
414 }
415
416 done_testing;