2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 use PublicInbox::Spawn qw(which);
10 require_mods(qw(DBD::SQLite Mail::IMAPClient Linux::Inotify2));
11 my $level = '-Lbasic';
13 require_mods('Search::Xapian', 1);
18 push(@V, 2) if require_git('2.6', 1);
20 my ($tmpdir, $for_destroy) = tmpdir();
21 my $home = "$tmpdir/home";
22 local $ENV{HOME} = $home;
25 my $addr = "i$V\@example.com";
27 my $url = "http://example.com/i$V";
28 my $inboxdir = "$tmpdir/$name";
29 my $folder = "inbox.i$V";
30 my $cmd = ['-init', "-V$V", $level, $name, $inboxdir, $url, $addr];
31 run_script($cmd) or BAIL_OUT("init $name");
32 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
33 "publicinbox.$name.newsgroup", $folder) == 0 or
34 BAIL_OUT("setting newsgroup $V");
36 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
37 'publicinboxmda.spamcheck', 'none') == 0 or
38 BAIL_OUT("config: $?");
40 open(my $fh, '<', 't/utf8.eml') or BAIL_OUT("open t/utf8.eml: $!");
41 my $env = { ORIGINAL_RECIPIENT => $addr };
42 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
43 BAIL_OUT('-mda delivery');
45 run_script(['-index', $inboxdir]) or BAIL_OUT("index $?");
48 my $sock = tcp_server();
49 my $err = "$tmpdir/stderr.log";
50 my $out = "$tmpdir/stdout.log";
51 my $cmd = [ '-imapd', '-W0', "--stdout=$out", "--stderr=$err" ];
52 my $td = start_script($cmd, undef, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
54 Server => $sock->sockhost,
55 Port => $sock->sockport,
58 my $mic = Mail::IMAPClient->new(%mic_opt);
59 my $pre_login_capa = $mic->capability;
60 is(grep(/\AAUTH=ANONYMOUS\z/, @$pre_login_capa), 1,
61 'AUTH=ANONYMOUS advertised pre-login');
63 $mic->User('lorelei');
64 $mic->Password('Hunter2');
65 ok($mic->login && $mic->IsAuthenticated, 'LOGIN works');
66 my $post_login_capa = $mic->capability;
67 ok(join("\n", @$pre_login_capa) ne join("\n", @$post_login_capa),
68 'got different capabilities post-login');
70 $mic_opt{Authmechanism} = 'ANONYMOUS';
71 $mic_opt{Authcallback} = sub { '' };
72 $mic = Mail::IMAPClient->new(%mic_opt);
73 ok($mic && $mic->login && $mic->IsAuthenticated, 'AUTHENTICATE ANONYMOUS');
74 my $post_auth_anon_capa = $mic->capability;
75 is_deeply($post_auth_anon_capa, $post_login_capa,
76 'auth anon has same capabilities');
78 ok(!$mic->examine('foo') && ($e = $@), 'EXAMINE non-existent');
79 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
80 ok(!$mic->select('foo') && ($e = $@), 'EXAMINE non-existent');
81 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
82 ok($mic->select('inbox.i1'), 'SELECT succeeds');
83 ok($mic->examine('inbox.i1'), 'EXAMINE succeeds');
84 my @raw = $mic->status('inbox.i1', qw(Messages uidnext uidvalidity));
85 is(scalar(@raw), 2, 'got status response');
86 like($raw[0], qr/\A\*\x20STATUS\x20inbox\.i1\x20
87 \(MESSAGES\x20\d+\x20UIDNEXT\x20\d+\x20UIDVALIDITY\x20\d+\)\r\n/sx);
88 like($raw[1], qr/\A\S+ OK /, 'finished status response');
91 like($raw[0], qr/^\* LIST \(.*?\) "\." inbox/,
93 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
94 is(scalar(@raw), scalar(@V) + 2, 'default LIST response');
95 @raw = $mic->list('', 'inbox.i1');
96 is(scalar(@raw), 2, 'limited LIST response');
97 like($raw[0], qr/^\* LIST \(.*?\) "\." inbox/,
99 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
101 { # make sure we get '%' globbing right
102 my @n = map { { newsgroup => $_ } } (qw(x.y.z x.z.y));
103 my $self = { imapd => { grouplist => \@n } };
104 PublicInbox::IMAPD::refresh_inboxlist($self->{imapd});
105 my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
106 is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
107 like($$res, qr/ x\r\ntag OK/, 'saw expected');
108 $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
109 is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
110 is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
112 $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
113 like($$res, qr/\At OK /, 'refname does not match attempted RCE');
114 $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
115 like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
118 if ($ENV{TEST_BENCHMARK}) {
119 use Benchmark qw(:all);
120 my @n = map { { newsgroup => "inbox.comp.foo.bar.$_" } } (0..50000);
121 push @n, map { { newsgroup => "xobni.womp.foo.bar.$_" } } (0..50000);
122 my $self = { imapd => { grouplist => \@n } };
123 PublicInbox::IMAPD::refresh_inboxlist($self->{imapd});
126 open my $null, '>', '/dev/null' or die;
127 my $ds = { sock => $null };
129 diag "starting benchmark...";
130 my $t = timeit(1, sub {
132 my $res = PublicInbox::IMAP::cmd_list($self, 'tag',
134 PublicInbox::DS::write($ds, $res);
137 diag timestr($t). "list all for $n inboxes $nr times";
141 my $res = PublicInbox::IMAP::cmd_list($self, 'tag',
143 PublicInbox::DS::write($ds, $res);
146 diag timestr($t). "list partial for $n inboxes $nr times";
149 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
150 is_deeply($ret, [ 1 ], 'search all works');
151 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
152 is_deeply($ret, [ 1 ], 'search UID 1 works');
153 $ret = $mic->search('uid 1:1') or BAIL_OUT "SEARCH FAIL $@";
154 is_deeply($ret, [ 1 ], 'search UID 1:1 works');
155 $ret = $mic->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
156 is_deeply($ret, [ 1 ], 'search UID 1:* works');
158 is_deeply(scalar $mic->flags('1'), [], '->flags works');
160 for my $r ('1:*', '1') {
161 $ret = $mic->fetch_hash($r, 'RFC822') or BAIL_OUT "FETCH $@";
162 is_deeply([keys %$ret], [1]);
163 like($ret->{1}->{RFC822}, qr/\r\n\r\nThis is a test/, 'read full');
165 # ensure Mail::IMAPClient behaves
166 my $str = $mic->message_string($r) or BAIL_OUT "->message_string: $@";
167 is($str, $ret->{1}->{RFC822}, '->message_string works as expected');
169 my $sz = $mic->fetch_hash($r, 'RFC822.size') or BAIL_OUT "FETCH $@";
170 is($sz->{1}->{'RFC822.SIZE'}, length($ret->{1}->{RFC822}),
173 $ret = $mic->fetch_hash($r, 'RFC822.HEADER') or BAIL_OUT "FETCH $@";
174 is_deeply([keys %$ret], [1]);
175 like($ret->{1}->{'RFC822.HEADER'},
176 qr/^Message-ID: <testmessage\@example\.com>/ms, 'read header');
178 $ret = $mic->fetch_hash($r, 'INTERNALDATE') or BAIL_OUT "FETCH $@";
179 is($ret->{1}->{'INTERNALDATE'}, '01-Jan-1970 00:00:00 +0000',
180 'internaldate matches');
181 ok(!$mic->fetch_hash($r, 'INFERNALDATE'), 'bogus attribute fails');
183 my $envelope = $mic->get_envelope($r) or BAIL_OUT("get_envelope: $@");
184 is($envelope->{bcc}, 'NIL', 'empty bcc');
185 is($envelope->{messageid}, '<testmessage@example.com>', 'messageid');
186 is(scalar @{$envelope->{to}}, 1, 'one {to} header');
187 # *sigh* too much to verify...
188 #use Data::Dumper; diag Dumper($envelope);
190 $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
191 is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
194 # Mail::IMAPClient ->compress creates cyclic reference:
195 # https://rt.cpan.org/Ticket/Display.html?id=132654
196 my $compress_logout = sub {
198 ok($c->logout, 'logout ok after ->compress');
199 # all documented in Mail::IMAPClient manpage:
200 for (qw(Readmoremethod Readmethod Prewritemethod)) {
205 is_deeply([$mic->has_capability('COMPRESS')], ['DEFLATE'], 'deflate cap');
206 ok($mic->compress, 'compress enabled');
207 $compress_logout->($mic);
209 my $have_inotify = eval { require Linux::Inotify2; 1 };
211 my $pi_config = PublicInbox::Config->new;
212 $pi_config->each_inbox(sub {
214 my $env = { ORIGINAL_RECIPIENT => $ibx->{-primary_address} };
215 my $name = $ibx->{name};
216 my $ng = $ibx->{newsgroup};
217 my $mic = Mail::IMAPClient->new(%mic_opt);
218 ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
219 is_deeply([$mic->has_capability('IDLE')], ['IDLE'], "IDLE capa $name");
220 ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
221 ok($mic->examine($ng), "EXAMINE $ng succeeds");
222 ok($mic->idle, "IDLE succeeds on $ng");
224 open(my $fh, '<', 't/data/message_embed.eml') or BAIL_OUT("open: $!");
225 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
226 BAIL_OUT('-mda delivery');
227 my $t0 = Time::HiRes::time();
228 ok(my @res = $mic->idle_data(11), "IDLE succeeds on $ng");
229 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
230 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
232 my (@ino_info, $ino_fdinfo);
234 skip 'no inotify support', 1 unless $have_inotify;
235 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$td->{pid}/fd";
237 readlink($_) =~ /\binotify\b/
238 } glob("/proc/$td->{pid}/fd/*");
239 is(scalar(@ino), 1, 'only one inotify FD');
240 my $ino_fd = (split('/', $ino[0]))[-1];
241 $ino_fdinfo = "/proc/$td->{pid}/fdinfo/$ino_fd";
242 if (open my $fh, '<', $ino_fdinfo) {
244 @ino_info = grep(/^inotify wd:/, <$fh>);
245 ok(scalar(@ino_info), 'inotify has watches');
247 skip "$ino_fdinfo missing: $!", 1;
251 # ensure IDLE persists across HUP, w/o extra watches or FDs
252 $td->kill('HUP') or BAIL_OUT "failed to kill -imapd: $!";
254 skip 'no inotify fdinfo (or support)', 2 if !@ino_info;
258 until (time > $end) {
259 select undef, undef, undef, 0.01;
260 open my $fh, '<', $ino_fdinfo or
261 BAIL_OUT "$ino_fdinfo: $!";
262 %prev = map { $_ => 1 } @ino_info;
263 @tmp = grep(/^inotify wd:/, <$fh>);
264 if (scalar(@tmp) == scalar(@ino_info)) {
266 last if scalar(keys(%prev)) == @ino_info;
269 is(scalar @tmp, scalar @ino_info,
270 'old inotify watches replaced');
271 is(scalar keys %prev, scalar @ino_info,
272 'no previous watches overlap');
275 open($fh, '<', 't/data/0001.patch') or BAIL_OUT("open: $!");
276 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
277 BAIL_OUT('-mda delivery');
278 $t0 = Time::HiRes::time();
279 ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
280 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
281 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
286 is($?, 0, 'no error in exited process');
287 open my $fh, '<', $err or BAIL_OUT("open $err failed: $!");
288 my $eout = do { local $/; <$fh> };
289 unlike($eout, qr/wide/i, 'no Wide character warnings');