]> Sergey Matveev's repositories - public-inbox.git/blob - t/mda.t
inboxwritable: mime_from_path: reuse in more places
[public-inbox.git] / t / mda.t
1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use Cwd qw(getcwd);
8 use PublicInbox::MID qw(mid2path);
9 use PublicInbox::Git;
10 use PublicInbox::InboxWritable;
11 use PublicInbox::TestCommon;
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $main_bin = getcwd()."/t/main-bin";
18 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
19 my $fail_bin = getcwd()."/t/fail-bin";
20 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
23 my $faildir = "$home/faildir/";
24 my $mime;
25 my $git = PublicInbox::Git->new($maindir);
26
27 my $fail_bad_header = sub ($$$) {
28         my ($good_rev, $msg, $in) = @_;
29         my @f = glob("$faildir/*/*");
30         unlink @f if @f;
31         my ($out, $err) = ("", "");
32         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
33         local $ENV{PATH} = $main_path;
34         ok(run_script(['-mda'], undef, $opt),
35                 "no error on undeliverable ($msg)");
36         my $rev = $git->qx(qw(rev-list HEAD));
37         chomp $rev;
38         is($rev, $good_rev, "bad revision not committed ($msg)");
39         @f = glob("$faildir/*/*");
40         is(scalar @f, 1, "faildir written to");
41         [ $in, $out, $err ];
42 };
43
44 {
45         ok(-x "$main_bin/spamc",
46                 "spamc ham mock found (run in top of source tree");
47         ok(-x "$fail_bin/spamc",
48                 "spamc mock found (run in top of source tree");
49         is(1, mkdir($home, 0755), "setup ~/ for testing");
50         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
51         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
52
53         open my $fh, '>>', $pi_config or die;
54         print $fh <<EOF or die;
55 [publicinbox "test"]
56         address = $addr
57         inboxdir = $maindir
58 EOF
59         close $fh or die;
60 }
61
62 local $ENV{GIT_COMMITTER_NAME} = eval {
63         use PublicInbox::MDA;
64         use PublicInbox::Address;
65         use Encode qw/encode/;
66         my $eml = 't/utf8.eml';
67         my $msg = PublicInbox::InboxWritable::mime_from_path($eml) or
68                 die "failed to open $eml: $!";
69         my $from = $msg->header('From');
70         my ($author) = PublicInbox::Address::names($from);
71         my ($email) = PublicInbox::Address::emails($from);
72         my $date = $msg->header('Date');
73
74         is('El&#233;anor',
75                 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
76                 'HTML conversion is correct');
77         is($email, 'e@example.com', 'email parsed correctly');
78         is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
79                 'message date parsed correctly');
80         $author;
81 };
82 die $@ if $@;
83
84 {
85         my $good_rev;
86         local $ENV{PI_EMERGENCY} = $faildir;
87         local $ENV{HOME} = $home;
88         local $ENV{ORIGINAL_RECIPIENT} = $addr;
89         my $simple = Email::Simple->new(<<EOF);
90 From: Me <me\@example.com>
91 To: You <you\@example.com>
92 Cc: $addr
93 Message-Id: <blah\@example.com>
94 Subject: hihi
95 Date: Thu, 01 Jan 1970 00:00:00 +0000
96
97 EOF
98         my $in = $simple->as_string;
99
100         # ensure successful message delivery
101         {
102                 local $ENV{PATH} = $main_path;
103                 ok(run_script(['-mda'], undef, { 0 => \$in }));
104                 my $rev = $git->qx(qw(rev-list HEAD));
105                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
106                 chomp $rev;
107                 my $cmt = $git->cat_file($rev);
108                 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
109                         "author info set correctly");
110                 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
111                         "committer info set correctly");
112                 $good_rev = $rev;
113         }
114
115         # ensure failures work, fail with bad spamc
116         {
117                 my @prev = <$faildir/new/*>;
118                 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
119                 local $ENV{PATH} = $fail_path;
120                 ok(run_script(['-mda'], undef, { 0 => \$in }));
121                 my @revs = $git->qx(qw(rev-list HEAD));
122                 is(scalar @revs, 1, "bad revision not committed");
123                 my @new = <$faildir/new/*>;
124                 is(scalar @new, 1, "PI_EMERGENCY is written to");
125         }
126
127         $fail_bad_header->($good_rev, "bad recipient", <<"");
128 From: Me <me\@example.com>
129 To: You <you\@example.com>
130 Message-Id: <bad-recipient\@example.com>
131 Subject: hihi
132 Date: Thu, 01 Jan 1970 00:00:00 +0000
133
134         my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
135 From: Me <me\@example.com>
136 To: You <you\@example.com>
137 Cc: $addr
138 Message-ID: <blah\@example.com>
139 Subject: hihi
140 Date: Thu, 01 Jan 1970 00:00:00 +0000
141
142         like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
143
144         $fail_bad_header->($good_rev, "missing From:", <<"");
145 To: $addr
146 Message-ID: <missing-from\@example.com>
147 Subject: hihi
148 Date: Thu, 01 Jan 1970 00:00:00 +0000
149
150         $fail_bad_header->($good_rev, "short subject:", <<"");
151 To: $addr
152 From: cat\@example.com
153 Message-ID: <short-subject\@example.com>
154 Subject: a
155 Date: Thu, 01 Jan 1970 00:00:00 +0000
156
157         $fail_bad_header->($good_rev, "no date", <<"");
158 To: $addr
159 From: u\@example.com
160 Message-ID: <no-date\@example.com>
161 Subject: hihi
162
163         $fail_bad_header->($good_rev, "bad date", <<"");
164 To: $addr
165 From: u\@example.com
166 Message-ID: <bad-date\@example.com>
167 Subject: hihi
168 Date: deadbeef
169
170 }
171
172 # spam training
173 {
174         local $ENV{PI_EMERGENCY} = $faildir;
175         local $ENV{HOME} = $home;
176         local $ENV{ORIGINAL_RECIPIENT} = $addr;
177         local $ENV{PATH} = $main_path;
178         my $mid = 'spam-train@example.com';
179         my $simple = Email::Simple->new(<<EOF);
180 From: Spammer <spammer\@example.com>
181 To: You <you\@example.com>
182 Cc: $addr
183 Message-ID: <$mid>
184 Subject: this message will be trained as spam
185 Date: Thu, 01 Jan 1970 00:00:00 +0000
186
187 EOF
188         my $in = $simple->as_string;
189
190         {
191                 # deliver the spam message, first
192                 ok(run_script(['-mda'], undef, { 0 => \$in }));
193                 my $path = mid2path($mid);
194                 my $msg = $git->cat_file("HEAD:$path");
195                 like($$msg, qr/\Q$mid\E/, "message delivered");
196
197                 # now train it
198                 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
199                 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
200                 local $ENV{GIT_COMMITTER_NAME};
201                 delete $ENV{GIT_COMMITTER_NAME};
202                 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
203                         "no failure from learning spam");
204                 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
205                         "no failure from learning spam idempotently");
206         }
207 }
208
209 # train ham message
210 {
211         local $ENV{PI_EMERGENCY} = $faildir;
212         local $ENV{HOME} = $home;
213         local $ENV{ORIGINAL_RECIPIENT} = $addr;
214         local $ENV{PATH} = $main_path;
215         my $mid = 'ham-train@example.com';
216         my $simple = Email::Simple->new(<<EOF);
217 From: False-positive <hammer\@example.com>
218 To: You <you\@example.com>
219 Cc: $addr
220 Message-ID: <$mid>
221 Subject: this message will be trained as spam
222 Date: Thu, 01 Jan 1970 00:00:00 +0000
223
224 EOF
225         my $in = $simple->as_string;
226
227         # now train it
228         # these should be overridden
229         local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
230         local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
231
232         ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
233                 "learned ham without failure");
234         my $path = mid2path($mid);
235         my $msg = $git->cat_file("HEAD:$path");
236         like($$msg, qr/\Q$mid\E/, "ham message delivered");
237         ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
238                 "learned ham idempotently ");
239
240         # ensure trained email is filtered, too
241         my $html_body = "<html><body>hi</body></html>";
242         my $parts = [
243                 Email::MIME->create(
244                         attributes => {
245                                 content_type => 'text/html; charset=UTF-8',
246                                 encoding => 'base64',
247                         },
248                         body => $html_body,
249                 ),
250                 Email::MIME->create(
251                         attributes => {
252                                 content_type => 'text/plain',
253                                 encoding => 'quoted-printable',
254                         },
255                         body => 'hi = "bye"',
256                 )
257         ];
258         $mid = 'multipart-html-sucks@11';
259         $mime = Email::MIME->create(
260                 header_str => [
261                   From => 'a@example.com',
262                   Subject => 'blah',
263                   Cc => $addr,
264                   'Message-ID' => "<$mid>",
265                   'Content-Type' => 'multipart/alternative',
266                 ],
267                 parts => $parts,
268         );
269
270         {
271                 $in = $mime->as_string;
272                 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
273                         "learned ham without failure");
274                 my $path = mid2path($mid);
275                 $msg = $git->cat_file("HEAD:$path");
276                 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
277                 unlike($$msg, qr/<html>/i, '<html> filtered');
278         }
279 }
280
281 # List-ID based delivery
282 {
283         local $ENV{PI_EMERGENCY} = $faildir;
284         local $ENV{HOME} = $home;
285         local $ENV{ORIGINAL_RECIPIENT} = undef;
286         delete $ENV{ORIGINAL_RECIPIENT};
287         local $ENV{PATH} = $main_path;
288         my $list_id = 'foo.example.com';
289         my $mid = 'list-id-delivery@example.com';
290         my $simple = Email::Simple->new(<<EOF);
291 From: user <user\@example.com>
292 To: You <you\@example.com>
293 Cc: $addr
294 Message-ID: <$mid>
295 List-Id: <$list_id>
296 Subject: this message will be trained as spam
297 Date: Thu, 01 Jan 1970 00:00:00 +0000
298
299 EOF
300         system(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
301         $? == 0 or die "failed to set listid $?";
302         my $in = $simple->as_string;
303         ok(run_script(['-mda'], undef, { 0 => \$in }),
304                 'mda OK with List-Id match');
305         my $path = mid2path($mid);
306         my $msg = $git->cat_file("HEAD:$path");
307         like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
308
309         # try a message w/o precheck
310         $simple = Email::Simple->new(<<EOF);
311 To: You <you\@example.com>
312 List-Id: <$list_id>
313
314 this message would not be accepted without --no-precheck
315 EOF
316         $in = $simple->as_string;
317         my ($out, $err) = ('', '');
318         my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
319         ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
320                 'mda OK with List-Id match and --no-precheck');
321         my $cur = $git->qx(qw(diff HEAD~1..HEAD));
322         like($cur, qr/this message would not be accepted without --no-precheck/,
323                 '--no-precheck delivered message anyways');
324
325         # try a message with multiple List-ID headers
326         $in = <<EOF;
327 List-ID: <foo.bar>
328 List-ID: <$list_id>
329 Message-ID: <2lids\@example>
330 Subject: two List-IDs
331 From: user <user\@example.com>
332 To: $addr
333 Date: Fri, 02 Oct 1993 00:00:00 +0000
334
335 EOF
336         ($out, $err) = ('', '');
337         ok(run_script(['-mda'], undef, $rdr),
338                 'mda OK with multiple List-Id matches');
339         $cur = $git->qx(qw(diff HEAD~1..HEAD));
340         like($cur, qr/Message-ID: <2lids\@example>/,
341                 'multi List-ID match delivered');
342         like($err, qr/multiple List-ID/, 'warned about multiple List-ID');
343 }
344
345 done_testing();