]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir.t
No ext_urls
[public-inbox.git] / t / watch_maildir.t
1 # Copyright (C) 2016-2021 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 Test::More;
5 use PublicInbox::Eml;
6 use Cwd;
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
10 my ($tmpdir, $for_destroy) = tmpdir();
11 my $git_dir = "$tmpdir/test.git";
12 my $maildir = "$tmpdir/md";
13 my $spamdir = "$tmpdir/spam";
14 use_ok 'PublicInbox::Watch';
15 use_ok 'PublicInbox::Emergency';
16 my $cfgpfx = "publicinbox.test";
17 my $addr = 'test-public@example.com';
18 my $default_branch = PublicInbox::Import::default_branch;
19 PublicInbox::Import::init_bare($git_dir);
20
21 my $msg = <<EOF;
22 From: user\@example.com
23 To: $addr
24 Subject: spam
25 Message-Id: <a\@b.com>
26 Date: Sat, 18 Jun 2016 00:00:00 +0000
27
28 something
29 EOF
30 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
31 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
32         'create FIFO to ensure we do not get stuck on it :P');
33 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
34
35 {
36         my @w;
37         local $SIG{__WARN__} = sub { push @w, @_ };
38         my $cfg = PublicInbox::Config->new(\<<EOF);
39 $cfgpfx.address=$addr
40 $cfgpfx.inboxdir=$git_dir
41 $cfgpfx.watch=maildir:$spamdir
42 publicinboxlearn.watchspam=maildir:$spamdir
43 EOF
44         my $wm = PublicInbox::Watch->new($cfg);
45         is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam');
46         is_deeply($wm->{mdmap}, { "$spamdir/cur" => 'watchspam' },
47                 'only got the spam folder to watch');
48 }
49
50 my $cfg_path = "$tmpdir/config";
51 {
52         open my $fh, '>', $cfg_path or BAIL_OUT $!;
53         print $fh <<EOF or BAIL_OUT $!;
54 [publicinbox "test"]
55         address = $addr
56         inboxdir = $git_dir
57         watch = maildir:$maildir
58         filter = PublicInbox::Filter::Vger
59 [publicinboxlearn]
60         watchspam = maildir:$spamdir
61 EOF
62         close $fh or BAIL_OUT $!;
63 }
64
65 my $cfg = PublicInbox::Config->new($cfg_path);
66 PublicInbox::Watch->new($cfg)->scan('full');
67 my $git = PublicInbox::Git->new($git_dir);
68 my @list = $git->qx('rev-list', $default_branch);
69 is(scalar @list, 1, 'one revision in rev-list');
70
71 my $write_spam = sub {
72         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
73         $sem->prepare(\$msg);
74         $sem->commit;
75         my @new = glob("$spamdir/new/*");
76         is(scalar @new, 1);
77         my @p = split(m!/+!, $new[0]);
78         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
79         is(unlink($new[0]), 1);
80 };
81 $write_spam->();
82 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
83 PublicInbox::Watch->new($cfg)->scan('full');
84 @list = $git->qx('rev-list', $default_branch);
85 is(scalar @list, 2, 'two revisions in rev-list');
86 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
87 is(scalar @list, 0, 'tree is empty');
88 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
89
90 # check with scrubbing
91 {
92         $msg .= qq(--
93 To unsubscribe from this list: send the line "unsubscribe git" in
94 the body of a message to majordomo\@vger.kernel.org
95 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
96         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
97         PublicInbox::Watch->new($cfg)->scan('full');
98         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
99         is(scalar @list, 1, 'tree has one file');
100         my $mref = $git->cat_file('HEAD:'.$list[0]);
101         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
102
103         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
104         $write_spam->();
105         PublicInbox::Watch->new($cfg)->scan('full');
106         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
107         is(scalar @list, 0, 'tree is empty');
108         @list = $git->qx('rev-list', $default_branch);
109         is(scalar @list, 4, 'four revisions in rev-list');
110         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
111 }
112
113 {
114         my $fail_bin = getcwd()."/t/fail-bin";
115         ok(-x "$fail_bin/spamc", "mock spamc exists");
116         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
117         local $ENV{PATH} = $fail_path;
118         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
119         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
120         {
121                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
122                 PublicInbox::Watch->new($cfg)->scan('full');
123         }
124         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
125         is(scalar @list, 0, 'tree has no files spamc checked');
126         is(unlink(glob("$maildir/new/*")), 1);
127 }
128
129 {
130         my $main_bin = getcwd()."/t/main-bin";
131         ok(-x "$main_bin/spamc", "mock spamc exists");
132         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
133         local $ENV{PATH} = $main_path;
134         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
135         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
136         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
137         PublicInbox::Watch->new($cfg)->scan('full');
138         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
139         is(scalar @list, 1, 'tree has one file after spamc checked');
140
141         # XXX: workaround some weird caching/memoization in cat-file,
142         # shouldn't be an issue in real-world use, though...
143         $git = PublicInbox::Git->new($git_dir);
144
145         my $mref = $git->cat_file($default_branch.':'.$list[0]);
146         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
147 }
148
149 # end-to-end test which actually uses inotify/kevent
150 {
151         my $env = { PI_CONFIG => $cfg_path };
152         $git->cleanup;
153
154         # n.b. --no-scan is only intended for testing atm
155         my $wm = start_script([qw(-watch --no-scan)], $env);
156         my $eml = eml_load('t/data/0001.patch');
157         $eml->header_set('Cc', $addr);
158         my $em = PublicInbox::Emergency->new($maildir);
159         $em->prepare(\($eml->as_string));
160
161         use_ok 'PublicInbox::InboxIdle';
162         use_ok 'PublicInbox::DS';
163         my $delivered = 0;
164         my $cb = sub {
165                 my ($ibx) = @_;
166                 diag "message delivered to `$ibx->{name}'";
167                 $delivered++;
168         };
169         PublicInbox::DS->Reset;
170         my $ii = PublicInbox::InboxIdle->new($cfg);
171         my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
172         $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
173         PublicInbox::DS->SetPostLoopCallback(sub { $delivered == 0 });
174
175         # wait for -watch to setup inotify watches
176         my $sleep = 1;
177         if (eval { require Linux::Inotify2 } && -d "/proc/$wm->{pid}/fd") {
178                 my $end = time + 2;
179                 my (@ino, @ino_info);
180                 do {
181                         @ino = grep {
182                                 (readlink($_)//'') =~ /\binotify\b/
183                         } glob("/proc/$wm->{pid}/fd/*");
184                 } until (@ino || time > $end || !tick);
185                 if (scalar(@ino) == 1) {
186                         my $ino_fd = (split('/', $ino[0]))[-1];
187                         my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
188                         while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
189                                 @ino_info = grep(/^inotify wd:/, <$fh>);
190                                 last if @ino_info >= 3;
191                                 tick;
192                         }
193                         $sleep = undef if @ino_info >= 3;
194                 }
195         }
196         if ($sleep) {
197                 diag "waiting ${sleep}s for -watch to start up";
198                 sleep $sleep;
199         }
200
201         $em->commit; # wake -watch up
202         diag 'waiting for -watch to import new message';
203         PublicInbox::DS::event_loop();
204         $wm->kill;
205         $wm->join;
206         $ii->close;
207         PublicInbox::DS->Reset;
208         my $head = $git->qx(qw(cat-file commit HEAD));
209         my $subj = $eml->header('Subject');
210         like($head, qr/^\Q$subj\E/sm, 'new commit made');
211 }
212
213 sub is_maildir {
214         my ($dir) = @_;
215         PublicInbox::Watch::is_maildir($dir);
216 }
217
218 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
219 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
220 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');
221
222 done_testing;