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