1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
8 use File::Temp qw/tempdir/;
11 use PublicInbox::MID qw(mid2path);
13 my $mda = "blib/script/public-inbox-mda";
14 my $learn = "blib/script/public-inbox-learn";
15 my $tmpdir = tempdir('pi-mda-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 my $home = "$tmpdir/pi-home";
17 my $pi_home = "$home/.public-inbox";
18 my $pi_config = "$pi_home/config";
19 my $maindir = "$tmpdir/main.git";
20 my $main_bin = getcwd()."/t/main-bin";
21 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
22 my $fail_bin = getcwd()."/t/fail-bin";
23 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
24 my $addr = 'test-public@example.com';
25 my $cfgpfx = "publicinbox.test";
26 my $failbox = "$home/fail.mbox";
30 ok(-x "$main_bin/spamc",
31 "spamc ham mock found (run in top of source tree");
32 ok(-x "$fail_bin/spamc",
33 "spamc mock found (run in top of source tree");
34 ok(-x $mda, "$mda is executable");
35 is(1, mkdir($home, 0755), "setup ~/ for testing");
36 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
37 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
40 "$cfgpfx.address" => $addr,
41 "$cfgpfx.mainrepo" => $maindir,
43 while (my ($k,$v) = each %cfg) {
44 is(0, system(qw(git config --file), $pi_config, $k, $v),
49 local $ENV{GIT_COMMITTER_NAME} = eval {
51 use Encode qw/encode/;
52 my $mbox = 't/utf8.mbox';
53 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
54 my $str = eval { local $/; <$fh> };
56 my $msg = Email::Filter->new(data => $str);
57 $msg = Email::MIME->new($msg->simple->as_string);
59 my $from = $msg->header('From');
60 my @from = Email::Address->parse($from);
61 my $author = $from[0]->name;
62 my $email = $from[0]->address;
63 my $date = $msg ->header('Date');
66 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
67 'HTML conversion is correct');
68 is($email, 'e@example.com', 'email parsed correctly');
69 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
70 'message date parsed correctly');
77 local $ENV{PI_EMERGENCY} = $failbox;
78 local $ENV{HOME} = $home;
79 local $ENV{ORIGINAL_RECIPIENT} = $addr;
80 my $simple = Email::Simple->new(<<EOF);
81 From: Me <me\@example.com>
82 To: You <you\@example.com>
84 Message-Id: <blah\@example.com>
86 Date: Thu, 01 Jan 1970 00:00:00 +0000
89 my $in = $simple->as_string;
91 # ensure successful message delivery
93 local $ENV{PATH} = $main_path;
95 local $ENV{GIT_DIR} = $maindir;
96 my $rev = `git rev-list HEAD`;
97 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
99 my $cmt = `git cat-file commit $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 ok(!-e $failbox, "nothing in PI_EMERGENCY before");
110 local $ENV{PATH} = $fail_path;
112 local $ENV{GIT_DIR} = $maindir;
113 my @revs = `git rev-list HEAD`;
114 is(scalar @revs, 1, "bad revision not committed");
115 ok(-s $failbox > 0, "PI_EMERGENCY is written to");
118 fail_bad_header($good_rev, "bad recipient", <<"");
119 From: Me <me\@example.com>
120 To: You <you\@example.com>
121 Message-Id: <bad-recipient\@example.com>
123 Date: Thu, 01 Jan 1970 00:00:00 +0000
125 my $fail = fail_bad_header($good_rev, "duplicate Message-ID", <<"");
126 From: Me <me\@example.com>
127 To: You <you\@example.com>
129 Message-ID: <blah\@example.com>
131 Date: Thu, 01 Jan 1970 00:00:00 +0000
133 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
135 fail_bad_header($good_rev, "missing From:", <<"");
137 Message-ID: <missing-from\@example.com>
139 Date: Thu, 01 Jan 1970 00:00:00 +0000
141 fail_bad_header($good_rev, "short subject:", <<"");
143 From: cat\@example.com
144 Message-ID: <short-subject\@example.com>
146 Date: Thu, 01 Jan 1970 00:00:00 +0000
148 fail_bad_header($good_rev, "no date", <<"");
151 Message-ID: <no-date\@example.com>
154 fail_bad_header($good_rev, "bad date", <<"");
157 Message-ID: <bad-date\@example.com>
165 local $ENV{PI_EMERGENCY} = $failbox;
166 local $ENV{HOME} = $home;
167 local $ENV{ORIGINAL_RECIPIENT} = $addr;
168 local $ENV{PATH} = $main_path;
169 my $mid = 'spam-train@example.com';
170 my $simple = Email::Simple->new(<<EOF);
171 From: Spammer <spammer\@example.com>
172 To: You <you\@example.com>
175 Subject: this message will be trained as spam
176 Date: Thu, 01 Jan 1970 00:00:00 +0000
179 my $in = $simple->as_string;
182 # deliver the spam message, first
184 my $path = mid2path($mid);
185 my $msg = `git --git-dir=$maindir cat-file blob HEAD:$path`;
186 like($msg, qr/\Q$mid\E/, "message delivered");
189 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
190 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
191 run([$learn, "spam"], \$msg);
192 is($?, 0, "no failure from learning spam");
193 run([$learn, "spam"], \$msg);
194 is($?, 0, "no failure from learning spam idempotently");
200 local $ENV{PI_EMERGENCY} = $failbox;
201 local $ENV{HOME} = $home;
202 local $ENV{ORIGINAL_RECIPIENT} = $addr;
203 local $ENV{PATH} = $main_path;
204 my $mid = 'ham-train@example.com';
205 my $simple = Email::Simple->new(<<EOF);
206 From: False-positive <hammer\@example.com>
207 To: You <you\@example.com>
210 Subject: this message will be trained as spam
211 Date: Thu, 01 Jan 1970 00:00:00 +0000
214 my $in = $simple->as_string;
217 # these should be overridden
218 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
219 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
221 run([$learn, "ham"], \$in);
222 is($?, 0, "learned ham without failure");
223 my $path = mid2path($mid);
224 my $msg = `git --git-dir=$maindir cat-file blob HEAD:$path`;
225 like($msg, qr/\Q$mid\E/, "ham message delivered");
226 run([$learn, "ham"], \$in);
227 is($?, 0, "learned ham idempotently ");
229 # ensure trained email is filtered, too
230 my $html_body = "<html><body>hi</body></html>";
234 content_type => 'text/html; charset=UTF-8',
235 encoding => 'base64',
241 content_type => 'text/plain',
242 encoding => 'quoted-printable',
244 body => 'hi = "bye"',
247 $mid = 'multipart-html-sucks@11';
248 $mime = Email::MIME->create(
250 From => 'a@example.com',
253 'Message-ID' => "<$mid>",
254 'Content-Type' => 'multipart/alternative',
260 $in = $mime->as_string;
261 run([$learn, "ham"], \$in);
262 is($?, 0, "learned ham without failure");
263 my $path = mid2path($mid);
264 $msg = `git --git-dir=$maindir cat-file blob HEAD:$path`;
265 like($msg, qr/<\Q$mid\E>/, "ham message delivered");
266 unlike($msg, qr/<html>/i, '<html> filtered');
270 # faildir - emergency destination is maildir
272 my $faildir= "$home/faildir/";
273 local $ENV{PI_EMERGENCY} = $faildir;
274 local $ENV{HOME} = $home;
275 local $ENV{ORIGINAL_RECIPIENT} = $addr;
276 local $ENV{PATH} = $fail_path;
278 From: Faildir <faildir\@example.com>
279 To: You <you\@example.com>
281 Message-ID: <faildir\@example.com>
282 Subject: faildir subject
283 Date: Thu, 01 Jan 1970 00:00:00 +0000
287 ok(-d $faildir, "emergency exists");
288 my @new = glob("$faildir/new/*");
289 is(scalar(@new), 1, "message delivered");
290 is(unlink(@new), 1, "removed emergency message");
292 local $ENV{PATH} = $main_path;
294 From: Faildir <faildir\@example.com>
296 Content-Type: text/html
297 Message-ID: <faildir\@example.com>
298 Subject: faildir subject
299 Date: Thu, 01 Jan 1970 00:00:00 +0000
301 <html><body>bad</body></html>
305 run([$mda], \$in, \$out, \$err);
306 isnt($?, 0, "mda exited with failure");
307 is(length $out, 0, 'nothing in stdout');
308 isnt(length $err, 0, 'error message in stderr');
310 @new = glob("$faildir/new/*");
311 is(scalar(@new), 0, "new message did not show up");
313 # reject multipart again
314 $in = $mime->as_string;
316 run([$mda], \$in, \$out, \$err);
317 isnt($?, 0, "mda exited with failure");
318 is(length $out, 0, 'nothing in stdout');
319 isnt(length $err, 0, 'error message in stderr');
320 @new = glob("$faildir/new/*");
321 is(scalar(@new), 0, "new message did not show up");
326 sub fail_bad_header {
327 my ($good_rev, $msg, $in) = @_;
328 open my $fh, '>', $failbox or die "failed to open $failbox: $!\n";
329 close $fh or die "failed to close $failbox: $!\n";
330 my ($out, $err) = ("", "");
331 local $ENV{PATH} = $main_path;
332 run([$mda], \$in, \$out, \$err);
333 local $ENV{GIT_DIR} = $maindir;
334 my $rev = `git rev-list HEAD`;
336 is($rev, $good_rev, "bad revision not commited ($msg)");
337 ok(-s $failbox > 0, "PI_EMERGENCY is written to ($msg)");