2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # end-to-end IMAP tests, see unit tests in t/imap.t, too
8 use PublicInbox::TestCommon;
9 use PublicInbox::Config;
10 use PublicInbox::Spawn qw(which);
11 require_mods(qw(DBD::SQLite Mail::IMAPClient Mail::IMAPClient::BodyStructure));
13 my $level = '-Lbasic';
15 require_mods('Search::Xapian', 1);
20 push(@V, 2) if require_git('2.6', 1);
22 my ($tmpdir, $for_destroy) = tmpdir();
23 my $home = "$tmpdir/home";
24 local $ENV{HOME} = $home;
27 my $addr = "i$V\@example.com";
29 my $url = "http://example.com/i$V";
30 my $inboxdir = "$tmpdir/$name";
31 my $folder = "inbox.i$V";
32 my $cmd = ['-init', "-V$V", $level, $name, $inboxdir, $url, $addr];
33 run_script($cmd) or BAIL_OUT("init $name");
34 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
35 "publicinbox.$name.newsgroup", $folder) == 0 or
36 BAIL_OUT("setting newsgroup $V");
38 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
39 'publicinboxmda.spamcheck', 'none') == 0 or
40 BAIL_OUT("config: $?");
42 open(my $fh, '<', 't/utf8.eml') or BAIL_OUT("open t/utf8.eml: $!");
43 my $env = { ORIGINAL_RECIPIENT => $addr };
44 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
45 BAIL_OUT('-mda delivery');
47 run_script(['-index', $inboxdir]) or BAIL_OUT("index $?");
50 my $sock = tcp_server();
51 my $err = "$tmpdir/stderr.log";
52 my $out = "$tmpdir/stdout.log";
53 my $cmd = [ '-imapd', '-W0', "--stdout=$out", "--stderr=$err" ];
54 my $td = start_script($cmd, undef, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
56 Server => $sock->sockhost,
57 Port => $sock->sockport,
60 my $mic = Mail::IMAPClient->new(%mic_opt);
61 my $pre_login_capa = $mic->capability;
62 is(grep(/\AAUTH=ANONYMOUS\z/, @$pre_login_capa), 1,
63 'AUTH=ANONYMOUS advertised pre-login');
65 $mic->User('lorelei');
66 $mic->Password('Hunter2');
67 ok($mic->login && $mic->IsAuthenticated, 'LOGIN works');
68 my $post_login_capa = $mic->capability;
69 ok(join("\n", @$pre_login_capa) ne join("\n", @$post_login_capa),
70 'got different capabilities post-login');
72 $mic_opt{Authmechanism} = 'ANONYMOUS';
73 $mic_opt{Authcallback} = sub { '' };
74 $mic = Mail::IMAPClient->new(%mic_opt);
75 ok($mic && $mic->login && $mic->IsAuthenticated, 'AUTHENTICATE ANONYMOUS');
76 my $post_auth_anon_capa = $mic->capability;
77 is_deeply($post_auth_anon_capa, $post_login_capa,
78 'auth anon has same capabilities');
80 ok(!$mic->examine('foo') && ($e = $@), 'EXAMINE non-existent');
81 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
82 ok(!$mic->select('foo') && ($e = $@), 'EXAMINE non-existent');
83 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
84 ok($mic->select('inbox.i1'), 'SELECT succeeds');
85 ok($mic->examine('inbox.i1'), 'EXAMINE succeeds');
86 my @raw = $mic->status('inbox.i1', qw(Messages uidnext uidvalidity));
87 is(scalar(@raw), 2, 'got status response');
88 like($raw[0], qr/\A\*\x20STATUS\x20inbox\.i1\x20
89 \(MESSAGES\x20\d+\x20UIDNEXT\x20\d+\x20UIDVALIDITY\x20\d+\)\r\n/sx);
90 like($raw[1], qr/\A\S+ OK /, 'finished status response');
93 like($raw[0], qr/^\* LIST \(.*?\) "\." inbox/,
95 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
96 is(scalar(@raw), scalar(@V) + 2, 'default LIST response');
97 @raw = $mic->list('', 'inbox.i1');
98 is(scalar(@raw), 2, 'limited LIST response');
99 like($raw[0], qr/^\* LIST \(.*?\) "\." inbox/,
101 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
103 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
104 is_deeply($ret, [ 1 ], 'search all works');
105 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
106 is_deeply($ret, [ 1 ], 'search UID 1 works');
107 $ret = $mic->search('uid 1:1') or BAIL_OUT "SEARCH FAIL $@";
108 is_deeply($ret, [ 1 ], 'search UID 1:1 works');
109 $ret = $mic->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
110 is_deeply($ret, [ 1 ], 'search UID 1:* works');
112 is_deeply(scalar $mic->flags('1'), [], '->flags works');
114 for my $r ('1:*', '1') {
115 $ret = $mic->fetch_hash($r, 'RFC822') or BAIL_OUT "FETCH $@";
116 is_deeply([keys %$ret], [1]);
117 like($ret->{1}->{RFC822}, qr/\r\n\r\nThis is a test/, 'read full');
119 # ensure Mail::IMAPClient behaves
120 my $str = $mic->message_string($r) or BAIL_OUT "->message_string: $@";
121 is($str, $ret->{1}->{RFC822}, '->message_string works as expected');
123 my $sz = $mic->fetch_hash($r, 'RFC822.size') or BAIL_OUT "FETCH $@";
124 is($sz->{1}->{'RFC822.SIZE'}, length($ret->{1}->{RFC822}),
127 $ret = $mic->fetch_hash($r, 'RFC822.HEADER') or BAIL_OUT "FETCH $@";
128 is_deeply([keys %$ret], [1]);
129 like($ret->{1}->{'RFC822.HEADER'},
130 qr/^Message-ID: <testmessage\@example\.com>/ms, 'read header');
132 $ret = $mic->fetch_hash($r, 'INTERNALDATE') or BAIL_OUT "FETCH $@";
133 is($ret->{1}->{'INTERNALDATE'}, '01-Jan-1970 00:00:00 +0000',
134 'internaldate matches');
135 ok(!$mic->fetch_hash($r, 'INFERNALDATE'), 'bogus attribute fails');
137 my $envelope = $mic->get_envelope($r) or BAIL_OUT("get_envelope: $@");
138 is($envelope->{bcc}, 'NIL', 'empty bcc');
139 is($envelope->{messageid}, '<testmessage@example.com>', 'messageid');
140 is(scalar @{$envelope->{to}}, 1, 'one {to} header');
141 # *sigh* too much to verify...
142 #use Data::Dumper; diag Dumper($envelope);
144 $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
145 is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
147 $ret = $mic->fetch_hash($r, 'BODY[1]') or BAIL_OUT "FETCH $@";
148 like($ret->{1}->{'BODY[1]'}, qr/\AThis is a test message/, 'BODY[1]');
150 $ret = $mic->fetch_hash($r, 'BODY[1]<1>') or BAIL_OUT "FETCH $@";
151 like($ret->{1}->{'BODY[1]<1>'}, qr/\Ahis is a test message/,
154 $ret = $mic->fetch_hash($r, 'BODY[1]<2.3>') or BAIL_OUT "FETCH $@";
155 is($ret->{1}->{'BODY[1]<2>'}, "is ", 'BODY[1]<2.3>');
156 $ret = $mic->bodypart_string($r, 1, 3, 2) or
157 BAIL_OUT "bodypart_string $@";
158 is($ret, "is ", 'bodypart string');
160 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS.NOT (Message-ID)]')
161 or BAIL_OUT "FETCH $@";
162 $ret = $ret->{1}->{'BODY[HEADER.FIELDS.NOT (MESSAGE-ID)]'};
163 unlike($ret, qr/message-id/i, 'Message-ID excluded');
164 like($ret, qr/\r\n\r\n\z/s, 'got header end');
166 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS (Message-ID)]')
167 or BAIL_OUT "FETCH $@";
168 is($ret->{1}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
169 'Message-ID: <testmessage@example.com>'."\r\n\r\n",
170 'got only Message-ID');
172 my $bs = $mic->get_bodystructure($r) or BAIL_OUT("bodystructure: $@");
173 ok($bs, 'got a bodystructure');
174 is(lc($bs->bodytype), 'text', '->bodytype');
175 is(lc($bs->bodyenc), '8bit', '->bodyenc');
178 # Mail::IMAPClient ->compress creates cyclic reference:
179 # https://rt.cpan.org/Ticket/Display.html?id=132654
180 my $compress_logout = sub {
182 ok($c->logout, 'logout ok after ->compress');
183 # all documented in Mail::IMAPClient manpage:
184 for (qw(Readmoremethod Readmethod Prewritemethod)) {
189 is_deeply([$mic->has_capability('COMPRESS')], ['DEFLATE'], 'deflate cap');
190 ok($mic->compress, 'compress enabled');
191 $compress_logout->($mic);
193 my $have_inotify = eval { require Linux::Inotify2; 1 };
195 my $pi_config = PublicInbox::Config->new;
196 $pi_config->each_inbox(sub {
198 my $env = { ORIGINAL_RECIPIENT => $ibx->{-primary_address} };
199 my $name = $ibx->{name};
200 my $ng = $ibx->{newsgroup};
201 my $mic = Mail::IMAPClient->new(%mic_opt);
202 ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
203 my $uidnext = $mic->uidnext($ng); # we'll fetch BODYSTRUCTURE on this
204 ok($uidnext, 'got uidnext for later fetch');
205 is_deeply([$mic->has_capability('IDLE')], ['IDLE'], "IDLE capa $name");
206 ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
207 ok($mic->examine($ng), "EXAMINE $ng succeeds");
208 ok(my $idle_tag = $mic->idle, "IDLE succeeds on $ng");
210 open(my $fh, '<', 't/data/message_embed.eml') or BAIL_OUT("open: $!");
211 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
212 BAIL_OUT('-mda delivery');
213 my $t0 = Time::HiRes::time();
214 ok(my @res = $mic->idle_data(11), "IDLE succeeds on $ng");
215 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
216 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
218 my (@ino_info, $ino_fdinfo);
220 skip 'no inotify support', 1 unless $have_inotify;
221 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$td->{pid}/fd";
223 readlink($_) =~ /\binotify\b/
224 } glob("/proc/$td->{pid}/fd/*");
225 is(scalar(@ino), 1, 'only one inotify FD');
226 my $ino_fd = (split('/', $ino[0]))[-1];
227 $ino_fdinfo = "/proc/$td->{pid}/fdinfo/$ino_fd";
228 if (open my $fh, '<', $ino_fdinfo) {
230 @ino_info = grep(/^inotify wd:/, <$fh>);
231 ok(scalar(@ino_info), 'inotify has watches');
233 skip "$ino_fdinfo missing: $!", 1;
237 # ensure IDLE persists across HUP, w/o extra watches or FDs
238 $td->kill('HUP') or BAIL_OUT "failed to kill -imapd: $!";
240 skip 'no inotify fdinfo (or support)', 2 if !@ino_info;
244 until (time > $end) {
245 select undef, undef, undef, 0.01;
246 open my $fh, '<', $ino_fdinfo or
247 BAIL_OUT "$ino_fdinfo: $!";
248 %prev = map { $_ => 1 } @ino_info;
249 @tmp = grep(/^inotify wd:/, <$fh>);
250 if (scalar(@tmp) == scalar(@ino_info)) {
252 last if scalar(keys(%prev)) == @ino_info;
255 is(scalar @tmp, scalar @ino_info,
256 'old inotify watches replaced');
257 is(scalar keys %prev, scalar @ino_info,
258 'no previous watches overlap');
261 open($fh, '<', 't/data/0001.patch') or BAIL_OUT("open: $!");
262 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
263 BAIL_OUT('-mda delivery');
264 $t0 = Time::HiRes::time();
265 ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
266 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
267 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
268 ok($mic->done($idle_tag), 'IDLE DONE');
269 my $bs = $mic->get_bodystructure($uidnext);
270 ok($bs, 'BODYSTRUCTURE ok for deeply nested');
271 $ret = $mic->fetch_hash($uidnext, 'BODY') or BAIL_OUT "FETCH $@";
272 ok($ret->{$uidnext}->{BODY}, 'got something in BODY');
274 # this matches dovecot behavior
275 $ret = $mic->fetch_hash($uidnext, 'BODY[1]') or BAIL_OUT "FETCH $@";
276 is($ret->{$uidnext}->{'BODY[1]'},
277 "testing embedded message harder\r\n", 'BODY[1]');
278 $ret = $mic->fetch_hash($uidnext, 'BODY[2]') or BAIL_OUT "FETCH $@";
279 like($ret->{$uidnext}->{'BODY[2]'},
280 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /, 'BODY[2]');
282 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.1]') or BAIL_OUT "FETCH $@";
283 is($ret->{$uidnext}->{'BODY[2.1.1]'},
284 "testing embedded message\r\n", 'BODY[2.1.1]');
286 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.2]') or BAIL_OUT "FETCH $@";
287 like($ret->{$uidnext}->{'BODY[2.1.2]'}, qr/\AFrom: /,
288 'BODY[2.1.2] tip matched');
289 like($ret->{$uidnext}->{'BODY[2.1.2]'},
290 # trailing CRLF may vary depending on MIME parser
291 qr/done_testing;(?:\r\n){1,2}\z/,
292 'BODY[2.1.2] tail matched');
294 $ret = $mic->fetch_hash("1:$uidnext", 'BODY[2.HEADER]') or
295 BAIL_OUT "2.HEADER $@";
296 like($ret->{$uidnext}->{'BODY[2.HEADER]'},
297 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /,
298 '2.HEADER of message/rfc822');
300 $ret = $mic->fetch_hash($uidnext, 'BODY[2.MIME]') or
301 BAIL_OUT "2.MIME $@";
302 is($ret->{$uidnext}->{'BODY[2.MIME]'}, <<EOF, 'BODY[2.MIME]');
303 Content-Type: message/rfc822\r
304 Content-Disposition: attachment; filename="embed2x\.eml"\r
309 # message sequence numbers :<
310 is($mic->Uid(0), 0, 'disable UID on '.ref($mic));
311 ok($mic->reconnect, 'reconnected');
312 $ret = $mic->fetch_hash('1:*', 'RFC822') or BAIL_OUT "FETCH $@";
313 is(scalar keys %$ret, 3, 'got all 3 messages');
315 my $rdr = { 0 => \($ret->{1}->{RFC822}) };
316 my $env = { HOME => $ENV{HOME} };
317 my @cmd = qw(-learn rm --all);
318 run_script(\@cmd, $env, $rdr) or BAIL_OUT('-learn rm');
320 my $r2 = $mic->fetch_hash('1:*', 'RFC822') or BAIL_OUT "FETCH $@";
321 is(scalar keys %$r2, 3, 'still got all 3 messages');
322 like($r2->{1}->{RFC822}, qr/dummy message #1/, 'got dummy message 1');
323 is($r2->{2}->{RFC822}, $ret->{2}->{RFC822}, 'message 2 unchanged');
324 is($r2->{3}->{RFC822}, $ret->{3}->{RFC822}, 'message 3 unchanged');
325 ok($mic->logout, 'logged out');
329 is($?, 0, 'no error in exited process');
330 open my $fh, '<', $err or BAIL_OUT("open $err failed: $!");
331 my $eout = do { local $/; <$fh> };
332 unlike($eout, qr/wide/i, 'no Wide character warnings');