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 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
209 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
210 run([$learn, "ham"], \$in);
211 is($?, 0, "learned ham without failure");
212 my $msg = `ssoma cat $mid $maindir`;
213 like($msg, qr/\Q$mid\E/, "ham message delivered");
214 run([$learn, "ham"], \$in);
215 is($?, 0, "learned ham idempotently ");
218 # faildir - emergency destination is maildir
220 my $faildir= "$home/faildir/";
221 local $ENV{PI_EMERGENCY} = $faildir;
222 local $ENV{HOME} = $home;
223 local $ENV{ORIGINAL_RECIPIENT} = $addr;
224 local $ENV{PATH} = $fail_path;
226 From: Faildir <faildir\@example.com>
227 To: You <you\@example.com>
229 Message-ID: <faildir\@example.com>
230 Subject: faildir subject
231 Date: Thu, 01 Jan 1970 00:00:00 +0000
235 ok(-d $faildir, "emergency exists");
236 my @new = glob("$faildir/new/*");
237 is(scalar(@new), 1, "message delivered");
242 sub fail_bad_header {
243 my ($good_rev, $msg, $in) = @_;
244 open my $fh, '>', $failbox or die "failed to open $failbox: $!\n";
245 close $fh or die "failed to close $failbox: $!\n";
246 my ($out, $err) = ("", "");
247 local $ENV{PATH} = $main_path;
248 run([$mda], \$in, \$out, \$err);
249 local $ENV{GIT_DIR} = $maindir;
250 my $rev = `git rev-list HEAD`;
252 is($rev, $good_rev, "bad revision not commited ($msg)");
253 ok(-s $failbox > 0, "PI_EMERGENCY is written to ($msg)");