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/;
12 my $mda = "blib/script/public-inbox-mda";
13 my $learn = "blib/script/public-inbox-learn";
14 my $tmpdir = tempdir(CLEANUP => 1);
15 my $home = "$tmpdir/pi-home";
16 my $pi_home = "$home/.public-inbox";
17 my $pi_config = "$pi_home/config";
18 my $maindir = "$tmpdir/main.git";
19 my $main_bin = getcwd()."/t/main-bin";
20 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
21 my $fail_bin = getcwd()."/t/fail-bin";
22 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
23 my $addr = 'test-public@example.com';
24 my $cfgpfx = "publicinbox.test";
25 my $failbox = "$home/fail.mbox";
29 ok(-x "$main_bin/spamc",
30 "spamc ham mock found (run in top of source tree");
31 ok(-x "$fail_bin/spamc",
32 "spamc mock found (run in top of source tree");
33 ok(-x $mda, "$mda is executable");
34 is(1, mkdir($home, 0755), "setup ~/ for testing");
35 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
36 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
39 "$cfgpfx.address" => $addr,
40 "$cfgpfx.mainrepo" => $maindir,
42 while (my ($k,$v) = each %cfg) {
43 is(0, system(qw(git config --file), $pi_config, $k, $v),
48 local $ENV{GIT_COMMITTER_NAME} = eval {
50 use Encode qw/encode/;
51 my $mbox = 't/utf8.mbox';
52 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
53 my $str = eval { local $/; <$fh> };
55 my $msg = Email::Filter->new(data => $str);
56 $msg = Email::MIME->new($msg->simple->as_string);
57 my ($author, $email, $date) = PublicInbox::MDA->author_info($msg);
59 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
60 'HTML conversion is correct');
61 is($email, 'e@example.com', 'email parsed correctly');
62 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
63 'message date parsed correctly');
70 local $ENV{PI_EMERGENCY} = $failbox;
71 local $ENV{HOME} = $home;
72 local $ENV{ORIGINAL_RECIPIENT} = $addr;
73 my $simple = Email::Simple->new(<<EOF);
74 From: Me <me\@example.com>
75 To: You <you\@example.com>
77 Message-Id: <blah\@example.com>
79 Date: Thu, 01 Jan 1970 00:00:00 +0000
82 my $in = $simple->as_string;
84 # ensure successful message delivery
86 local $ENV{PATH} = $main_path;
88 local $ENV{GIT_DIR} = $maindir;
89 my $rev = `git rev-list HEAD`;
90 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
92 my $cmt = `git cat-file commit $rev`;
93 like($cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
94 "author info set correctly");
95 like($cmt, qr/^committer test <test-public\@example\.com>/m,
96 "committer info set correctly");
100 # ensure failures work, fail with bad spamc
102 ok(!-e $failbox, "nothing in PI_EMERGENCY before");
103 local $ENV{PATH} = $fail_path;
105 local $ENV{GIT_DIR} = $maindir;
106 my @revs = `git rev-list HEAD`;
107 is(scalar @revs, 1, "bad revision not committed");
108 ok(-s $failbox > 0, "PI_EMERGENCY is written to");
111 fail_bad_header($good_rev, "bad recipient", <<"");
112 From: Me <me\@example.com>
113 To: You <you\@example.com>
114 Message-Id: <bad-recipient\@example.com>
116 Date: Thu, 01 Jan 1970 00:00:00 +0000
118 my $fail = fail_bad_header($good_rev, "duplicate Message-ID", <<"");
119 From: Me <me\@example.com>
120 To: You <you\@example.com>
122 Message-ID: <blah\@example.com>
124 Date: Thu, 01 Jan 1970 00:00:00 +0000
126 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
128 fail_bad_header($good_rev, "missing From:", <<"");
130 Message-ID: <missing-from\@example.com>
132 Date: Thu, 01 Jan 1970 00:00:00 +0000
134 fail_bad_header($good_rev, "short subject:", <<"");
136 From: cat\@example.com
137 Message-ID: <short-subject\@example.com>
139 Date: Thu, 01 Jan 1970 00:00:00 +0000
141 fail_bad_header($good_rev, "no date", <<"");
144 Message-ID: <no-date\@example.com>
147 fail_bad_header($good_rev, "bad date", <<"");
150 Message-ID: <bad-date\@example.com>
158 local $ENV{PI_EMERGENCY} = $failbox;
159 local $ENV{HOME} = $home;
160 local $ENV{ORIGINAL_RECIPIENT} = $addr;
161 local $ENV{PATH} = $main_path;
162 my $mid = 'spam-train@example.com';
163 my $simple = Email::Simple->new(<<EOF);
164 From: Spammer <spammer\@example.com>
165 To: You <you\@example.com>
168 Subject: this message will be trained as spam
169 Date: Thu, 01 Jan 1970 00:00:00 +0000
172 my $in = $simple->as_string;
175 # deliver the spam message, first
177 my $msg = `ssoma cat $mid $maindir`;
178 like($msg, qr/\Q$mid\E/, "message delivered");
181 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
182 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
183 run([$learn, "spam"], \$msg);
184 is($?, 0, "no failure from learning spam");
185 run([$learn, "spam"], \$msg);
186 is($?, 0, "no failure from learning spam idempotently");
192 local $ENV{PI_EMERGENCY} = $failbox;
193 local $ENV{HOME} = $home;
194 local $ENV{ORIGINAL_RECIPIENT} = $addr;
195 local $ENV{PATH} = $main_path;
196 my $mid = 'ham-train@example.com';
197 my $simple = Email::Simple->new(<<EOF);
198 From: False-positive <hammer\@example.com>
199 To: You <you\@example.com>
202 Subject: this message will be trained as spam
203 Date: Thu, 01 Jan 1970 00:00:00 +0000
206 my $in = $simple->as_string;
209 # these should be overridden
210 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
211 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
213 run([$learn, "ham"], \$in);
214 is($?, 0, "learned ham without failure");
215 my $msg = `ssoma cat $mid $maindir`;
216 like($msg, qr/\Q$mid\E/, "ham message delivered");
217 run([$learn, "ham"], \$in);
218 is($?, 0, "learned ham idempotently ");
220 # ensure trained email is filtered, too
221 my $html_body = "<html><body>hi</body></html>";
225 content_type => 'text/html; charset=UTF-8',
226 encoding => 'base64',
232 content_type => 'text/plain',
233 encoding => 'quoted-printable',
235 body => 'hi = "bye"',
238 $mid = 'multipart-html-sucks@11';
239 $mime = Email::MIME->create(
241 From => 'a@example.com',
244 'Message-ID' => "<$mid>",
245 'Content-Type' => 'multipart/alternative',
251 $in = $mime->as_string;
252 run([$learn, "ham"], \$in);
253 is($?, 0, "learned ham without failure");
254 $msg = `ssoma cat $mid $maindir`;
255 like($msg, qr/<\Q$mid\E>/, "ham message delivered");
256 unlike($msg, qr/<html>/i, '<html> filtered');
260 # faildir - emergency destination is maildir
262 my $faildir= "$home/faildir/";
263 local $ENV{PI_EMERGENCY} = $faildir;
264 local $ENV{HOME} = $home;
265 local $ENV{ORIGINAL_RECIPIENT} = $addr;
266 local $ENV{PATH} = $fail_path;
268 From: Faildir <faildir\@example.com>
269 To: You <you\@example.com>
271 Message-ID: <faildir\@example.com>
272 Subject: faildir subject
273 Date: Thu, 01 Jan 1970 00:00:00 +0000
277 ok(-d $faildir, "emergency exists");
278 my @new = glob("$faildir/new/*");
279 is(scalar(@new), 1, "message delivered");
280 is(unlink(@new), 1, "removed emergency message");
282 local $ENV{PATH} = $main_path;
284 From: Faildir <faildir\@example.com>
286 Content-Type: text/html
287 Message-ID: <faildir\@example.com>
288 Subject: faildir subject
289 Date: Thu, 01 Jan 1970 00:00:00 +0000
291 <html><body>bad</body></html>
295 run([$mda], \$in, \$out, \$err);
296 isnt($?, 0, "mda exited with failure");
297 is(length $out, 0, 'nothing in stdout');
298 isnt(length $err, 0, 'error message in stderr');
300 @new = glob("$faildir/new/*");
301 is(scalar(@new), 0, "new message did not show up");
303 # reject multipart again
304 $in = $mime->as_string;
306 run([$mda], \$in, \$out, \$err);
307 isnt($?, 0, "mda exited with failure");
308 is(length $out, 0, 'nothing in stdout');
309 isnt(length $err, 0, 'error message in stderr');
310 @new = glob("$faildir/new/*");
311 is(scalar(@new), 0, "new message did not show up");
316 sub fail_bad_header {
317 my ($good_rev, $msg, $in) = @_;
318 open my $fh, '>', $failbox or die "failed to open $failbox: $!\n";
319 close $fh or die "failed to close $failbox: $!\n";
320 my ($out, $err) = ("", "");
321 local $ENV{PATH} = $main_path;
322 run([$mda], \$in, \$out, \$err);
323 local $ENV{GIT_DIR} = $maindir;
324 my $rev = `git rev-list HEAD`;
326 is($rev, $good_rev, "bad revision not commited ($msg)");
327 ok(-s $failbox > 0, "PI_EMERGENCY is written to ($msg)");