1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
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";
28 ok(-x "$main_bin/spamc",
29 "spamc ham mock found (run in top of source tree");
30 ok(-x "$fail_bin/spamc",
31 "spamc mock found (run in top of source tree");
32 ok(-x $mda, "$mda is executable");
33 is(1, mkdir($home, 0755), "setup ~/ for testing");
34 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
35 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
38 "$cfgpfx.address" => $addr,
39 "$cfgpfx.mainrepo" => $maindir,
41 while (my ($k,$v) = each %cfg) {
42 is(0, system(qw(git config --file), $pi_config, $k, $v),
47 local $ENV{GIT_COMMITTER_NAME} = eval {
49 use Encode qw/encode/;
50 my $mbox = 't/utf8.mbox';
51 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
52 my $str = eval { local $/; <$fh> };
54 my $msg = Email::Filter->new(data => $str);
55 $msg = Email::MIME->new($msg->simple->as_string);
56 my ($author, $email, $date) = PublicInbox::MDA->author_info($msg);
58 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
59 'HTML conversion is correct');
60 is($email, 'e@example.com', 'email parsed correctly');
61 is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
62 'message date parsed correctly');
69 local $ENV{PI_EMERGENCY} = $failbox;
70 local $ENV{HOME} = $home;
71 local $ENV{ORIGINAL_RECIPIENT} = $addr;
72 my $simple = Email::Simple->new(<<EOF);
73 From: Me <me\@example.com>
74 To: You <you\@example.com>
76 Message-Id: <blah\@example.com>
78 Date: Thu, 01 Jan 1970 00:00:00 +0000
81 my $in = $simple->as_string;
83 # ensure successful message delivery
85 local $ENV{PATH} = $main_path;
87 local $ENV{GIT_DIR} = $maindir;
88 my $rev = `git rev-list HEAD`;
89 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
91 my $cmt = `git cat-file commit $rev`;
92 like($cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
93 "author info set correctly");
94 like($cmt, qr/^committer test <test-public\@example\.com>/m,
95 "committer info set correctly");
99 # ensure failures work, fail with bad spamc
101 ok(!-e $failbox, "nothing in PI_EMERGENCY before");
102 local $ENV{PATH} = $fail_path;
104 local $ENV{GIT_DIR} = $maindir;
105 my @revs = `git rev-list HEAD`;
106 is(scalar @revs, 1, "bad revision not committed");
107 ok(-s $failbox > 0, "PI_EMERGENCY is written to");
110 fail_bad_header($good_rev, "bad recipient", <<"");
111 From: Me <me\@example.com>
112 To: You <you\@example.com>
113 Message-Id: <bad-recipient\@example.com>
115 Date: Thu, 01 Jan 1970 00:00:00 +0000
117 my $fail = fail_bad_header($good_rev, "duplicate Message-ID", <<"");
118 From: Me <me\@example.com>
119 To: You <you\@example.com>
121 Message-ID: <blah\@example.com>
123 Date: Thu, 01 Jan 1970 00:00:00 +0000
125 like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
127 fail_bad_header($good_rev, "missing From:", <<"");
129 Message-ID: <missing-from\@example.com>
131 Date: Thu, 01 Jan 1970 00:00:00 +0000
133 fail_bad_header($good_rev, "short subject:", <<"");
135 From: cat\@example.com
136 Message-ID: <short-subject\@example.com>
138 Date: Thu, 01 Jan 1970 00:00:00 +0000
140 fail_bad_header($good_rev, "no date", <<"");
143 Message-ID: <no-date\@example.com>
146 fail_bad_header($good_rev, "bad date", <<"");
149 Message-ID: <bad-date\@example.com>
157 local $ENV{PI_EMERGENCY} = $failbox;
158 local $ENV{HOME} = $home;
159 local $ENV{ORIGINAL_RECIPIENT} = $addr;
160 local $ENV{PATH} = $main_path;
161 my $mid = 'spam-train@example.com';
162 my $simple = Email::Simple->new(<<EOF);
163 From: Spammer <spammer\@example.com>
164 To: You <you\@example.com>
167 Subject: this message will be trained as spam
168 Date: Thu, 01 Jan 1970 00:00:00 +0000
171 my $in = $simple->as_string;
174 # deliver the spam message, first
176 my $msg = `ssoma cat $mid $maindir`;
177 like($msg, qr/\Q$mid\E/, "message delivered");
180 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
181 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
182 run([$learn, "spam"], \$msg);
183 is($?, 0, "no failure from learning spam");
184 run([$learn, "spam"], \$msg);
185 is($?, 0, "no failure from learning spam idempotently");
191 local $ENV{PI_EMERGENCY} = $failbox;
192 local $ENV{HOME} = $home;
193 local $ENV{ORIGINAL_RECIPIENT} = $addr;
194 local $ENV{PATH} = $main_path;
195 my $mid = 'ham-train@example.com';
196 my $simple = Email::Simple->new(<<EOF);
197 From: False-positive <hammer\@example.com>
198 To: You <you\@example.com>
201 Subject: this message will be trained as spam
202 Date: Thu, 01 Jan 1970 00:00:00 +0000
205 my $in = $simple->as_string;
208 # these should be overridden
209 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
210 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
212 run([$learn, "ham"], \$in);
213 is($?, 0, "learned ham without failure");
214 my $msg = `ssoma cat $mid $maindir`;
215 like($msg, qr/\Q$mid\E/, "ham message delivered");
216 run([$learn, "ham"], \$in);
217 is($?, 0, "learned ham idempotently ");
219 # ensure trained email is filtered, too
220 my $html_body = "<html><body>hi</body></html>";
224 content_type => 'text/html; charset=UTF-8',
225 encoding => 'base64',
231 content_type => 'text/plain',
232 encoding => 'quoted-printable',
234 body => 'hi = "bye"',
237 $mid = 'multipart-html-sucks@11';
238 my $mime = Email::MIME->create(
240 From => 'a@example.com',
243 'Message-ID' => "<$mid>",
244 'Content-Type' => 'multipart/alternative',
250 $in = $mime->as_string;
251 run([$learn, "ham"], \$in);
252 is($?, 0, "learned ham without failure");
253 $msg = `ssoma cat $mid $maindir`;
254 like($msg, qr/<\Q$mid\E>/, "ham message delivered");
255 unlike($msg, qr/<html>/i, '<html> filtered');
259 # faildir - emergency destination is maildir
261 my $faildir= "$home/faildir/";
262 local $ENV{PI_EMERGENCY} = $faildir;
263 local $ENV{HOME} = $home;
264 local $ENV{ORIGINAL_RECIPIENT} = $addr;
265 local $ENV{PATH} = $fail_path;
267 From: Faildir <faildir\@example.com>
268 To: You <you\@example.com>
270 Message-ID: <faildir\@example.com>
271 Subject: faildir subject
272 Date: Thu, 01 Jan 1970 00:00:00 +0000
276 ok(-d $faildir, "emergency exists");
277 my @new = glob("$faildir/new/*");
278 is(scalar(@new), 1, "message delivered");
283 sub fail_bad_header {
284 my ($good_rev, $msg, $in) = @_;
285 open my $fh, '>', $failbox or die "failed to open $failbox: $!\n";
286 close $fh or die "failed to close $failbox: $!\n";
287 my ($out, $err) = ("", "");
288 local $ENV{PATH} = $main_path;
289 run([$mda], \$in, \$out, \$err);
290 local $ENV{GIT_DIR} = $maindir;
291 my $rev = `git rev-list HEAD`;
293 is($rev, $good_rev, "bad revision not commited ($msg)");
294 ok(-s $failbox > 0, "PI_EMERGENCY is written to ($msg)");