]> Sergey Matveev's repositories - public-inbox.git/blob - t/eml.t
eml: pure-Perl replacement for Email::MIME
[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 # body-less, boundary-less
121 for my $cls (@classes) {
122         my $call = 0;
123         $cls->new(<<'EOF')->each_part(sub { $call++ }, 0, 1);
124 Content-Type: multipart/mixed; boundary="body-less"
125
126 EOF
127         is($call, 1, 'called on bodyless multipart');
128
129         my @tmp;
130         $cls->new(<<'EOF')->each_part(sub { push @tmp, \@_; }, 0, 1);
131 Content-Type: multipart/mixed; boundary="boundary-less"
132
133 hello world
134 EOF
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');
139 }
140
141 # I guess the following only worked in PI::M because of a happy accident
142 # involving inheritance:
143 for my $cls (@classes) {
144         my @tmp;
145         my $header_less = <<'EOF';
146 Archived-At: <85k5su9k59.fsf_-_@lola.goethe.zz>
147 Content-Type: multipart/mixed; boundary="header-less"
148
149 --header-less
150
151 this is the body
152
153 --header-less
154 i-haz: header
155
156 something else
157
158 --header-less--
159 EOF
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'";
167         }
168         is($tmp[1]->[0]->[0]->body, "something else\n");
169         is(scalar(@tmp), 2, 'two parts');
170 }
171
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"');
177         is($eml->body, "");
178 }
179
180 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
181         my $s = <<EOF;
182 Content-Type: multipart/mixed; boundary="b"
183
184 --b
185 header: only
186 --b--
187 EOF
188         my $eml = $cls->new(\$s);
189         my $nr = 0;
190         my @v;
191         $eml->each_part(sub {
192                 @v = $_[0]->[0]->header_raw('Header');
193                 $nr++;
194         });
195         is($nr, 1, 'only one part');
196         is_deeply(\@v, [], "nothing w/o body $cls");
197 }
198
199 for my $cls (@classes) {
200         my $s = <<EOF; # double epilogue, double the fun
201 Content-Type: multipart/mixed; boundary="b"
202
203 --b
204 should: appear
205
206 yes
207
208 --b--
209
210 --b
211 should: not appear
212
213 nope
214 --b--
215 EOF
216         my $eml = $cls->new(\$s);
217         my $nr = 0;
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');
223                 $nr++;
224         });
225         is($nr, 1, 'only one part');
226 }
227
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)
233
234 \xc4\x80
235 EOF
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");
239 }
240
241 if ('we differ from Email::MIME with final "\n" on missing epilogue') {
242         my $s = <<EOF;
243 Content-Type: multipart/mixed; boundary="b"
244
245 --b
246 header: but
247
248 no epilogue
249 EOF
250         my $eml = PublicInbox::Eml->new(\$s);
251         is(($eml->subparts)[-1]->body, "no epilogue\n",
252                 'final "\n" preserved on missing epilogue');
253 }
254
255 if ('maxparts is a feature unique to us') {
256         my $eml = eml_load 't/psgi_attach.eml';
257         my @orig;
258         $eml->each_part(sub { push @orig, $_[0]->[0] });
259
260         local $PublicInbox::Eml::MAXPARTS = scalar(@orig);
261         my $i = 0;
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");
266         });
267         is($i, scalar(@orig), 'maxparts honored');
268         $PublicInbox::Eml::MAXPARTS--;
269         my @ltd;
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,
273                         "part[$i] matches");
274         }
275         is(scalar(@ltd), scalar(@orig) - 1, 'maxparts honored');
276 }
277
278 SKIP: {
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");
288         }
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');
297                 $s = $msg->body_str;
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');
304         }
305         $eml = eml_load 't/iso-2202-jp.eml';
306         $mime = mime_load 't/iso-2202-jp.eml';
307         $s = $eml->body_str;
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');
311
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;
322                 $msg->parts_set([]);
323                 is_deeply([$msg->subparts], [], 'parts_set can clear');
324                 $msg->parts_set([$old[-1]]);
325                 is(scalar $msg->subparts, 1, 'only last remains');
326         }
327         is($eml->as_string, $mime->as_string,
328                 'as_string matches after parts_set');
329 }
330
331 for my $cls (@classes) {
332         my $s = <<'EOF';
333 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
334 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
335
336 EOF
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');
343
344         $s = <<'EOF';
345 Content-Type: multipart/foo; boundary=b
346
347 --b
348 Content-Disposition: attachment; filename="=?utf-8?q?vtpm-makefile.patch?="
349
350 a
351 --b
352 Content-Type: text/x-patch; name="=?utf-8?q?vtpm-fakefile.patch?="
353
354 b
355 --b--
356 EOF
357         my @tmp;
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');
361 }
362
363 done_testing;