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>
7 use File::Temp qw/tempdir/;
9 use PublicInbox::MID qw(mid2path);
11 eval { require IPC::Run };
12 plan skip_all => "missing IPC::Run for t/mda.t" if $@;
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/";
29 my $git = PublicInbox::Git->new($maindir);
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)");
42 "$cfgpfx.address" => $addr,
43 "$cfgpfx.inboxdir" => $maindir,
45 while (my ($k,$v) = each %cfg) {
46 is(0, system(qw(git config --file), $pi_config, $k, $v),
51 local $ENV{GIT_COMMITTER_NAME} = eval {
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> };
59 my $msg = Email::MIME->new($str);
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');
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');
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>
85 Message-Id: <blah\@example.com>
87 Date: Thu, 01 Jan 1970 00:00:00 +0000
90 my $in = $simple->as_string;
92 # ensure successful message delivery
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");
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");
107 # ensure failures work, fail with bad spamc
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");
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>
124 Date: Thu, 01 Jan 1970 00:00:00 +0000
126 my $fail = fail_bad_header($good_rev, "duplicate Message-ID", <<"");
127 From: Me <me\@example.com>
128 To: You <you\@example.com>
130 Message-ID: <blah\@example.com>
132 Date: Thu, 01 Jan 1970 00:00:00 +0000
134 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
136 fail_bad_header($good_rev, "missing From:", <<"");
138 Message-ID: <missing-from\@example.com>
140 Date: Thu, 01 Jan 1970 00:00:00 +0000
142 fail_bad_header($good_rev, "short subject:", <<"");
144 From: cat\@example.com
145 Message-ID: <short-subject\@example.com>
147 Date: Thu, 01 Jan 1970 00:00:00 +0000
149 fail_bad_header($good_rev, "no date", <<"");
152 Message-ID: <no-date\@example.com>
155 fail_bad_header($good_rev, "bad date", <<"");
158 Message-ID: <bad-date\@example.com>
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>
176 Subject: this message will be trained as spam
177 Date: Thu, 01 Jan 1970 00:00:00 +0000
180 my $in = $simple->as_string;
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");
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");
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>
212 Subject: this message will be trained as spam
213 Date: Thu, 01 Jan 1970 00:00:00 +0000
216 my $in = $simple->as_string;
219 # these should be overridden
220 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
221 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
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 ");
231 # ensure trained email is filtered, too
232 my $html_body = "<html><body>hi</body></html>";
236 content_type => 'text/html; charset=UTF-8',
237 encoding => 'base64',
243 content_type => 'text/plain',
244 encoding => 'quoted-printable',
246 body => 'hi = "bye"',
249 $mid = 'multipart-html-sucks@11';
250 $mime = Email::MIME->create(
252 From => 'a@example.com',
255 'Message-ID' => "<$mid>",
256 'Content-Type' => 'multipart/alternative',
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');
272 # List-ID based delivery
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>
287 Subject: this message will be trained as spam
288 Date: Thu, 01 Jan 1970 00:00:00 +0000
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');
300 # try a message w/o precheck
301 $simple = Email::Simple->new(<<EOF);
302 To: You <you\@example.com>
305 this message would not be accepted without --no-precheck
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');
315 # try a message with multiple List-ID headers
319 Message-ID: <2lids\@example>
320 Subject: two List-IDs
321 From: user <user\@example.com>
323 Date: Fri, 02 Oct 1993 00:00:00 +0000
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');
337 sub fail_bad_header {
338 my ($good_rev, $msg, $in) = @_;
339 my @f = glob("$faildir/*/*");
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));
346 is($rev, $good_rev, "bad revision not commited ($msg)");
347 @f = glob("$faildir/*/*");
348 is(scalar @f, 1, "faildir written to");