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