]> Sergey Matveev's repositories - public-inbox.git/blob - t/mda.t
tests: remove Email::MIME->create use entirely
[public-inbox.git] / t / mda.t
1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Cwd qw(getcwd);
7 use PublicInbox::MID qw(mid2path);
8 use PublicInbox::Git;
9 use PublicInbox::InboxWritable;
10 use PublicInbox::TestCommon;
11 use PublicInbox::Import;
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $main_bin = getcwd()."/t/main-bin";
18 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
19 my $fail_bin = getcwd()."/t/fail-bin";
20 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
23 my $faildir = "$home/faildir/";
24 my $git = PublicInbox::Git->new($maindir);
25
26 my $fail_bad_header = sub ($$$) {
27         my ($good_rev, $msg, $in) = @_;
28         my @f = glob("$faildir/*/*");
29         unlink @f if @f;
30         my ($out, $err) = ("", "");
31         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
32         local $ENV{PATH} = $main_path;
33         ok(run_script(['-mda'], undef, $opt),
34                 "no error on undeliverable ($msg)");
35         my $rev = $git->qx(qw(rev-list HEAD));
36         chomp $rev;
37         is($rev, $good_rev, "bad revision not committed ($msg)");
38         @f = glob("$faildir/*/*");
39         is(scalar @f, 1, "faildir written to");
40         [ $in, $out, $err ];
41 };
42
43 {
44         ok(-x "$main_bin/spamc",
45                 "spamc ham mock found (run in top of source tree");
46         ok(-x "$fail_bin/spamc",
47                 "spamc mock found (run in top of source tree");
48         is(1, mkdir($home, 0755), "setup ~/ for testing");
49         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
50         PublicInbox::Import::init_bare($maindir);
51
52         open my $fh, '>>', $pi_config or die;
53         print $fh <<EOF or die;
54 [publicinbox "test"]
55         address = $addr
56         inboxdir = $maindir
57 EOF
58         close $fh or die;
59 }
60
61 local $ENV{GIT_COMMITTER_NAME} = eval {
62         use PublicInbox::MDA;
63         use PublicInbox::Address;
64         use Encode qw/encode/;
65         my $eml = 't/utf8.eml';
66         my $msg = PublicInbox::InboxWritable::mime_from_path($eml) or
67                 die "failed to open $eml: $!";
68         my $from = $msg->header('From');
69         my ($author) = PublicInbox::Address::names($from);
70         my ($email) = PublicInbox::Address::emails($from);
71         my $date = $msg->header('Date');
72
73         is('El&#233;anor',
74                 encode('us-ascii', my $tmp = $author, Encode::HTMLCREF),
75                 'HTML conversion is correct');
76         is($email, 'e@example.com', 'email parsed correctly');
77         is($date, 'Thu, 01 Jan 1970 00:00:00 +0000',
78                 'message date parsed correctly');
79         $author;
80 };
81 die $@ if $@;
82
83 {
84         my $good_rev;
85         local $ENV{PI_EMERGENCY} = $faildir;
86         local $ENV{HOME} = $home;
87         local $ENV{ORIGINAL_RECIPIENT} = $addr;
88         my $in = <<EOF;
89 From: Me <me\@example.com>
90 To: You <you\@example.com>
91 Cc: $addr
92 Message-Id: <blah\@example.com>
93 Subject: hihi
94 Date: Thu, 01 Jan 1970 00:00:00 +0000
95
96 EOF
97         # ensure successful message delivery
98         {
99                 local $ENV{PATH} = $main_path;
100                 ok(run_script(['-mda'], undef, { 0 => \$in }));
101                 my $rev = $git->qx(qw(rev-list HEAD));
102                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
103                 chomp $rev;
104                 my $cmt = $git->cat_file($rev);
105                 like($$cmt, qr/^author Me <me\@example\.com> 0 \+0000\n/m,
106                         "author info set correctly");
107                 like($$cmt, qr/^committer test <test-public\@example\.com>/m,
108                         "committer info set correctly");
109                 $good_rev = $rev;
110         }
111
112         # ensure failures work, fail with bad spamc
113         {
114                 my @prev = <$faildir/new/*>;
115                 is(scalar @prev, 0 , "nothing in PI_EMERGENCY before");
116                 local $ENV{PATH} = $fail_path;
117                 ok(run_script(['-mda'], undef, { 0 => \$in }));
118                 my @revs = $git->qx(qw(rev-list HEAD));
119                 is(scalar @revs, 1, "bad revision not committed");
120                 my @new = <$faildir/new/*>;
121                 is(scalar @new, 1, "PI_EMERGENCY is written to");
122         }
123
124         $fail_bad_header->($good_rev, "bad recipient", <<"");
125 From: Me <me\@example.com>
126 To: You <you\@example.com>
127 Message-Id: <bad-recipient\@example.com>
128 Subject: hihi
129 Date: Thu, 01 Jan 1970 00:00:00 +0000
130
131         my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
132 From: Me <me\@example.com>
133 To: You <you\@example.com>
134 Cc: $addr
135 Message-ID: <blah\@example.com>
136 Subject: hihi
137 Date: Thu, 01 Jan 1970 00:00:00 +0000
138
139         like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
140
141         $fail_bad_header->($good_rev, "missing From:", <<"");
142 To: $addr
143 Message-ID: <missing-from\@example.com>
144 Subject: hihi
145 Date: Thu, 01 Jan 1970 00:00:00 +0000
146
147         $fail_bad_header->($good_rev, "short subject:", <<"");
148 To: $addr
149 From: cat\@example.com
150 Message-ID: <short-subject\@example.com>
151 Subject: a
152 Date: Thu, 01 Jan 1970 00:00:00 +0000
153
154         $fail_bad_header->($good_rev, "no date", <<"");
155 To: $addr
156 From: u\@example.com
157 Message-ID: <no-date\@example.com>
158 Subject: hihi
159
160         $fail_bad_header->($good_rev, "bad date", <<"");
161 To: $addr
162 From: u\@example.com
163 Message-ID: <bad-date\@example.com>
164 Subject: hihi
165 Date: deadbeef
166
167 }
168
169 # spam training
170 {
171         local $ENV{PI_EMERGENCY} = $faildir;
172         local $ENV{HOME} = $home;
173         local $ENV{ORIGINAL_RECIPIENT} = $addr;
174         local $ENV{PATH} = $main_path;
175         my $mid = 'spam-train@example.com';
176         my $in = <<EOF;
177 From: Spammer <spammer\@example.com>
178 To: You <you\@example.com>
179 Cc: $addr
180 Message-ID: <$mid>
181 Subject: this message will be trained as spam
182 Date: Thu, 01 Jan 1970 00:00:00 +0000
183
184 EOF
185         {
186                 # deliver the spam message, first
187                 ok(run_script(['-mda'], undef, { 0 => \$in }));
188                 my $path = mid2path($mid);
189                 my $msg = $git->cat_file("HEAD:$path");
190                 like($$msg, qr/\Q$mid\E/, "message delivered");
191
192                 # now train it
193                 local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
194                 local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
195                 local $ENV{GIT_COMMITTER_NAME};
196                 delete $ENV{GIT_COMMITTER_NAME};
197                 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
198                         "no failure from learning spam");
199                 ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
200                         "no failure from learning spam idempotently");
201         }
202 }
203
204 # train ham message
205 {
206         local $ENV{PI_EMERGENCY} = $faildir;
207         local $ENV{HOME} = $home;
208         local $ENV{ORIGINAL_RECIPIENT} = $addr;
209         local $ENV{PATH} = $main_path;
210         my $mid = 'ham-train@example.com';
211         my $in = <<EOF;
212 From: False-positive <hammer\@example.com>
213 To: You <you\@example.com>
214 Cc: $addr
215 Message-ID: <$mid>
216 Subject: this message will be trained as spam
217 Date: Thu, 01 Jan 1970 00:00:00 +0000
218
219 EOF
220         # now train it
221         # these should be overridden
222         local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
223         local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
224
225         ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
226                 "learned ham without failure");
227         my $path = mid2path($mid);
228         my $msg = $git->cat_file("HEAD:$path");
229         like($$msg, qr/\Q$mid\E/, "ham message delivered");
230         ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
231                 "learned ham idempotently ");
232
233         # ensure trained email is filtered, too
234         my $mime = mime_load 't/mda-mime.eml';
235         ($mid) = ($mime->header_raw('message-id') =~ /<([^>]+)>/);
236         {
237                 $in = $mime->as_string;
238                 ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
239                         "learned ham without failure");
240                 my $path = mid2path($mid);
241                 $msg = $git->cat_file("HEAD:$path");
242                 like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
243                 unlike($$msg, qr/<html>/i, '<html> filtered');
244         }
245 }
246
247 # List-ID based delivery
248 {
249         local $ENV{PI_EMERGENCY} = $faildir;
250         local $ENV{HOME} = $home;
251         local $ENV{ORIGINAL_RECIPIENT} = undef;
252         delete $ENV{ORIGINAL_RECIPIENT};
253         local $ENV{PATH} = $main_path;
254         my $list_id = 'foo.example.com';
255         my $mid = 'list-id-delivery@example.com';
256         my $in = <<EOF;
257 From: user <user\@example.com>
258 To: You <you\@example.com>
259 Cc: $addr
260 Message-ID: <$mid>
261 List-Id: <$list_id>
262 Subject: this message will be trained as spam
263 Date: Thu, 01 Jan 1970 00:00:00 +0000
264
265 EOF
266         xsys(qw(git config --file), $pi_config, "$cfgpfx.listid", $list_id);
267         $? == 0 or die "failed to set listid $?";
268         ok(run_script(['-mda'], undef, { 0 => \$in }),
269                 'mda OK with List-Id match');
270         my $path = mid2path($mid);
271         my $msg = $git->cat_file("HEAD:$path");
272         like($$msg, qr/\Q$list_id\E/, 'delivered message w/ List-ID matches');
273
274         # try a message w/o precheck
275         $in = <<EOF;
276 To: You <you\@example.com>
277 List-Id: <$list_id>
278
279 this message would not be accepted without --no-precheck
280 EOF
281         my ($out, $err) = ('', '');
282         my $rdr = { 0 => \$in, 1 => \$out, 2 => \$err };
283         ok(run_script(['-mda', '--no-precheck'], undef, $rdr),
284                 'mda OK with List-Id match and --no-precheck');
285         my $cur = $git->qx(qw(diff HEAD~1..HEAD));
286         like($cur, qr/this message would not be accepted without --no-precheck/,
287                 '--no-precheck delivered message anyways');
288
289         # try a message with multiple List-ID headers
290         $in = <<EOF;
291 List-ID: <foo.bar>
292 List-ID: <$list_id>
293 Message-ID: <2lids\@example>
294 Subject: two List-IDs
295 From: user <user\@example.com>
296 To: $addr
297 Date: Fri, 02 Oct 1993 00:00:00 +0000
298
299 EOF
300         ($out, $err) = ('', '');
301         ok(run_script(['-mda'], undef, $rdr),
302                 'mda OK with multiple List-Id matches');
303         $cur = $git->qx(qw(diff HEAD~1..HEAD));
304         like($cur, qr/Message-ID: <2lids\@example>/,
305                 'multi List-ID match delivered');
306         like($err, qr/multiple List-ID/, 'warned about multiple List-ID');
307 }
308
309 done_testing();