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