2 # Copyright (C) 2020-2021 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 require_mods(qw(-imapd Mail::IMAPClient));
11 my $imap_client = 'Mail::IMAPClient';
12 my $can_compress = $imap_client->can('compress');
13 if ($can_compress) { # hope this gets fixed upstream, soon
14 require PublicInbox::IMAPClient;
15 $imap_client = 'PublicInbox::IMAPClient';
18 require_ok 'PublicInbox::IMAP';
19 my $first_range = '0';
23 require_mods('Search::Xapian', 1);
28 push(@V, 2) if require_git('2.6', 1);
30 my ($tmpdir, $for_destroy) = tmpdir();
31 my $home = "$tmpdir/home";
32 BAIL_OUT "mkdir: $!" unless (mkdir($home) and mkdir("$home/.public-inbox"));
34 open my $cfgfh, '>', "$home/.public-inbox/config" or BAIL_OUT;
35 print $cfgfh <<EOM or BAIL_OUT;
41 my $ibx = create_inbox("i$V-$level",
42 tmpdir => "$tmpdir/i$V", version => $V,
43 indexlevel => $level, sub {
45 $im->add($eml //= eml_load('t/utf8.eml')) or BAIL_OUT;
48 $ibx->{newsgroup} = "inbox.i$V";
49 print $cfgfh <<EOF or BAIL_OUT;
51 inboxdir = $ibx->{inboxdir}
52 address = $ibx->{-primary_address};
54 url = http://example.com/i$V
58 close $cfgfh or BAIL_OUT;
59 local $ENV{HOME} = $home;
60 my $sock = tcp_server();
61 my $err = "$tmpdir/stderr.log";
62 my $out = "$tmpdir/stdout.log";
63 my $cmd = [ '-imapd', '-W0', "--stdout=$out", "--stderr=$err" ];
64 my $td = start_script($cmd, undef, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
65 my ($ihost, $iport) = tcp_host_port($sock);
66 my %mic_opt = ( Server => $ihost, Port => $iport, Uid => 1 );
67 my $mic = $imap_client->new(%mic_opt);
68 my $pre_login_capa = $mic->capability;
69 is(grep(/\AAUTH=ANONYMOUS\z/, @$pre_login_capa), 1,
70 'AUTH=ANONYMOUS advertised pre-login');
72 $mic->User('lorelei');
73 $mic->Password('Hunter2');
74 ok($mic->login && $mic->IsAuthenticated, 'LOGIN works');
75 my $post_login_capa = $mic->capability;
76 ok(join("\n", @$pre_login_capa) ne join("\n", @$post_login_capa),
77 'got different capabilities post-login');
79 $mic_opt{Authmechanism} = 'ANONYMOUS';
80 $mic_opt{Authcallback} = sub { '' };
81 $mic = $imap_client->new(%mic_opt);
82 ok($mic && $mic->login && $mic->IsAuthenticated, 'AUTHENTICATE ANONYMOUS');
83 my $post_auth_anon_capa = $mic->capability;
84 is_deeply($post_auth_anon_capa, $post_login_capa,
85 'auth anon has same capabilities');
87 ok($mic->noop, 'NOOP');
88 ok($mic->noop, 'NOOP (again)'); # for warnings
89 ok(!$mic->examine('foo') && ($e = $@), 'EXAMINE non-existent');
90 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
91 ok(!$mic->select('foo') && ($e = $@), 'EXAMINE non-existent');
92 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
93 my $mailbox1 = "inbox.i1.$first_range";
94 ok($mic->select('inbox.i1'), 'SELECT on parent succeeds');
95 ok($mic->noop, 'NOOP while selected');
96 ok($mic->noop, 'NOOP again while selected'); # check warnings later
97 ok($mic->select($mailbox1), 'SELECT succeeds');
98 ok($mic->examine($mailbox1), 'EXAMINE succeeds');
99 my @raw = $mic->status($mailbox1, qw(Messages uidnext uidvalidity));
100 is(scalar(@raw), 2, 'got status response');
101 like($raw[0], qr/\A\*\x20STATUS\x20inbox\.i1\.$first_range\x20
102 \(MESSAGES\x20\d+\x20UIDNEXT\x20\d+\x20UIDVALIDITY\x20\d+\)\r\n/sx);
103 like($raw[1], qr/\A\S+ OK /, 'finished status response');
105 my @orig_list = @raw = $mic->list;
106 like($raw[0], qr/^\* LIST \(.*?\) "\." INBOX/,
108 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
109 is(scalar(@raw), scalar(@V) * 2 + 2, 'default LIST response');
110 @raw = $mic->list('', 'inbox.i1');
111 is(scalar(@raw), 2, 'limited LIST response');
112 like($raw[0], qr/^\* LIST \(.*?\) "\." INBOX/,
114 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
116 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
117 is_deeply($ret, [ 1 ], 'search all works');
118 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
119 is_deeply($ret, [ 1 ], 'search UID 1 works');
120 $ret = $mic->search('uid 1:1') or BAIL_OUT "SEARCH FAIL $@";
121 is_deeply($ret, [ 1 ], 'search UID 1:1 works');
122 $ret = $mic->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
123 is_deeply($ret, [ 1 ], 'search UID 1:* works');
124 $ret = $mic->search('DELETED') or BAIL_OUT "SEARCH FAIL $@";
125 is_deeply($ret, [], 'searching for DELETED returns nothing');
128 skip 'Xapian missing', 8 if $level eq 'basic';
129 my $x = $mic->search(qw(smaller 99999));
130 is_deeply($x, [1], 'SMALLER works with Xapian (hit)');
131 $x = $mic->search(qw(smaller 9));
132 is_deeply($x, [], 'SMALLER works with Xapian (miss)');
134 $x = $mic->search(qw(larger 99999));
135 is_deeply($x, [], 'LARGER works with Xapian (miss)');
136 $x = $mic->search(qw(larger 9));
137 is_deeply($x, [1], 'LARGER works with Xapian (hit)');
139 $x = $mic->search(qw(HEADER Message-ID testmessage@example.com));
140 is_deeply($x, [1], 'HEADER Message-ID works');
142 $x = $mic->search(qw(DRAFT HEADER Message-ID testmessage@example.com));
143 is_deeply($x, [], 'impossible (DRAFT) key filters out match');
145 $x = $mic->search(qw(HEADER Message-ID miss));
146 is_deeply($x, [], 'HEADER Message-ID can miss');
148 my @q = qw[OR HEADER Message-ID testmessage@example.com
149 (OR FROM Ryan (OR TO Joe CC Scott))];
150 $x = $mic->search(join(' ', @q));
151 is_deeply($x, [1], 'nested query works');
154 is_deeply(scalar $mic->flags('1'), [], '->flags works');
156 # RFC 3501 section 6.4.8 states:
157 # Also note that a UID range of 559:* always includes the
158 # UID of the last message in the mailbox, even if 559 is
159 # higher than any assigned UID value.
160 my $exp = $mic->fetch_hash(1, 'UID');
161 $ret = $mic->fetch_hash('559:*', 'UID');
162 is_deeply($ret, $exp, 'beginning range too big');
164 my @w; # Mail::IMAPClient hits a warning via overload
165 local $SIG{__WARN__} = sub { push @w, @_ };
166 $ret = $mic->fetch_hash(my $r = '559:558', 'UID');
167 is_deeply($ret, {}, "out-of-range UID FETCH $r");
168 @w = grep(!/\boverload\.pm\b/, @w);
169 is_deeply(\@w, [], 'no unexpected warning');
171 $ret = $mic->fetch_hash(my $r = '558:559', 'UID');
172 is_deeply($ret, {}, "out-of-range UID FETCH $r");
175 for my $r ('1:*', '1') {
176 $ret = $mic->fetch_hash($r, 'RFC822') or BAIL_OUT "FETCH $@";
177 is_deeply([keys %$ret], [1]);
178 like($ret->{1}->{RFC822}, qr/\r\n\r\nThis is a test/, 'read full');
180 # ensure Mail::IMAPClient behaves
181 my $str = $mic->message_string($r) or BAIL_OUT "->message_string: $@";
182 is($str, $ret->{1}->{RFC822}, '->message_string works as expected');
184 my $sz = $mic->fetch_hash($r, 'RFC822.size') or BAIL_OUT "FETCH $@";
185 is($sz->{1}->{'RFC822.SIZE'}, length($ret->{1}->{RFC822}),
188 $ret = $mic->fetch_hash($r, 'RFC822.HEADER') or BAIL_OUT "FETCH $@";
189 is_deeply([keys %$ret], [1]);
190 like($ret->{1}->{'RFC822.HEADER'},
191 qr/^Message-ID: <testmessage\@example\.com>/ms, 'read header');
193 $ret = $mic->fetch_hash($r, 'INTERNALDATE') or BAIL_OUT "FETCH $@";
194 is($ret->{1}->{'INTERNALDATE'}, '01-Jan-1970 00:00:00 +0000',
195 'internaldate matches');
196 ok(!$mic->fetch_hash($r, 'INFERNALDATE'), 'bogus attribute fails');
198 my $envelope = $mic->get_envelope($r) or BAIL_OUT("get_envelope: $@");
199 is($envelope->{bcc}, 'NIL', 'empty bcc');
200 is($envelope->{messageid}, '<testmessage@example.com>', 'messageid');
201 is(scalar @{$envelope->{to}}, 1, 'one {to} header');
202 # *sigh* too much to verify...
203 #use Data::Dumper; diag Dumper($envelope);
205 $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
206 is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
208 $ret = $mic->fetch_hash($r, 'BODY[1]') or BAIL_OUT "FETCH $@";
209 like($ret->{1}->{'BODY[1]'}, qr/\AThis is a test message/, 'BODY[1]');
211 $ret = $mic->fetch_hash($r, 'BODY[1]<1>') or BAIL_OUT "FETCH $@";
212 like($ret->{1}->{'BODY[1]<1>'}, qr/\Ahis is a test message/,
215 $ret = $mic->fetch_hash($r, 'BODY[1]<2.3>') or BAIL_OUT "FETCH $@";
216 is($ret->{1}->{'BODY[1]<2>'}, "is ", 'BODY[1]<2.3>');
217 $ret = $mic->bodypart_string($r, 1, 3, 2) or
218 BAIL_OUT "bodypart_string $@";
219 is($ret, "is ", 'bodypart string');
221 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS.NOT (Message-ID)]')
222 or BAIL_OUT "FETCH $@";
223 $ret = $ret->{1}->{'BODY[HEADER.FIELDS.NOT (MESSAGE-ID)]'};
224 unlike($ret, qr/message-id/i, 'Message-ID excluded');
225 like($ret, qr/\r\n\r\n\z/s, 'got header end');
227 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS (Message-ID)]')
228 or BAIL_OUT "FETCH $@";
229 is($ret->{1}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
230 'Message-ID: <testmessage@example.com>'."\r\n\r\n",
231 'got only Message-ID');
233 my $bs = $mic->get_bodystructure($r) or BAIL_OUT("bodystructure: $@");
234 ok($bs, 'got a bodystructure');
235 is(lc($bs->bodytype), 'text', '->bodytype');
236 is(lc($bs->bodyenc), '8bit', '->bodyenc');
238 ok($mic->has_capability('COMPRESS') ||
239 $mic->has_capability('COMPRESS=DEFLATE'), 'deflate cap');
241 skip 'Mail::IMAPClient too old for ->compress', 2 if !$can_compress;
242 my $c = $imap_client->new(%mic_opt);
243 ok($c && $c->compress, 'compress enabled');
244 ok($c->examine($mailbox1), 'EXAMINE succeeds after COMPRESS');
245 $ret = $c->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
246 is_deeply($ret, [ 1 ], 'search UID 1:* works after compression');
249 ok($mic->logout, 'logout works');
251 my $have_inotify = eval { require Linux::Inotify2; 1 };
254 my $name = $ibx->{name};
255 my $ng = $ibx->{newsgroup};
256 my $mic = $imap_client->new(%mic_opt);
257 ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
258 my $mb = "$ng.$first_range";
259 my $uidnext = $mic->uidnext($mb); # we'll fetch BODYSTRUCTURE on this
260 ok($uidnext, 'got uidnext for later fetch');
261 ok($mic->has_capability('IDLE'), "IDLE capa $name");
262 ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
263 ok($mic->examine($mb), "EXAMINE $ng succeeds");
264 ok(my $idle_tag = $mic->idle, "IDLE succeeds on $ng");
265 my $im = $ibx->importer(0);
266 $im->add(eml_load 't/data/message_embed.eml') or BAIL_OUT;
268 my $t0 = Time::HiRes::time();
269 ok(my @res = $mic->idle_data(11), "IDLE succeeds on $ng");
270 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
271 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
273 my (@ino_info, $ino_fdinfo);
275 skip 'no inotify support', 1 unless $have_inotify;
276 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$td->{pid}/fd";
278 readlink($_) =~ /\binotify\b/
279 } glob("/proc/$td->{pid}/fd/*");
280 is(scalar(@ino), 1, 'only one inotify FD');
281 my $ino_fd = (split('/', $ino[0]))[-1];
282 $ino_fdinfo = "/proc/$td->{pid}/fdinfo/$ino_fd";
283 if (open my $fh, '<', $ino_fdinfo) {
285 @ino_info = grep(/^inotify wd:/, <$fh>);
286 ok(scalar(@ino_info), 'inotify has watches');
288 skip "$ino_fdinfo missing: $!", 1;
292 # ensure IDLE persists across HUP, w/o extra watches or FDs
293 $td->kill('HUP') or BAIL_OUT "failed to kill -imapd: $!";
294 for my $n (1..2) { # kick the event loop so we know HUP is done
295 my $m = $imap_client->new(%mic_opt);
296 ok($m->login && $m->IsAuthenticated && $m->logout,
297 "connection $n works after HUP");
300 $im->add(eml_load 't/data/0001.patch') or BAIL_OUT;
302 $t0 = Time::HiRes::time();
303 ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
304 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
305 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
306 ok($mic->done($idle_tag), 'IDLE DONE');
307 my $bs = $mic->get_bodystructure($uidnext);
308 ok($bs, 'BODYSTRUCTURE ok for deeply nested');
309 $ret = $mic->fetch_hash($uidnext, 'BODY') or BAIL_OUT "FETCH $@";
310 ok($ret->{$uidnext}->{BODY}, 'got something in BODY');
312 # this matches dovecot behavior
313 $ret = $mic->fetch_hash($uidnext, 'BODY[1]') or BAIL_OUT "FETCH $@";
314 is($ret->{$uidnext}->{'BODY[1]'},
315 "testing embedded message harder\r\n", 'BODY[1]');
316 $ret = $mic->fetch_hash($uidnext, 'BODY[2]') or BAIL_OUT "FETCH $@";
317 like($ret->{$uidnext}->{'BODY[2]'},
318 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /, 'BODY[2]');
320 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.1]') or BAIL_OUT "FETCH $@";
321 is($ret->{$uidnext}->{'BODY[2.1.1]'},
322 "testing embedded message\r\n", 'BODY[2.1.1]');
324 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.2]') or BAIL_OUT "FETCH $@";
325 like($ret->{$uidnext}->{'BODY[2.1.2]'}, qr/\AFrom: /,
326 'BODY[2.1.2] tip matched');
327 like($ret->{$uidnext}->{'BODY[2.1.2]'},
328 # trailing CRLF may vary depending on MIME parser
329 qr/done_testing;(?:\r\n){1,2}\z/,
330 'BODY[2.1.2] tail matched');
332 $ret = $mic->fetch_hash("1:$uidnext", 'BODY[2.HEADER]') or
333 BAIL_OUT "2.HEADER $@";
334 like($ret->{$uidnext}->{'BODY[2.HEADER]'},
335 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /,
336 '2.HEADER of message/rfc822');
338 $ret = $mic->fetch_hash($uidnext, 'BODY[2.MIME]') or
339 BAIL_OUT "2.MIME $@";
340 is($ret->{$uidnext}->{'BODY[2.MIME]'}, <<EOF, 'BODY[2.MIME]');
341 Content-Type: message/rfc822\r
342 Content-Disposition: attachment; filename="embed2x\.eml"\r
346 my @hits = $mic->search('SENTON' => '18-Apr-2020');
347 is_deeply(\@hits, [ $uidnext ], 'search with date condition works');
348 ok($mic->examine($ng), 'EXAMINE on dummy');
349 @hits = $mic->search('SENTSINCE' => '18-Apr-2020');
350 is_deeply(\@hits, [], 'search on dummy with condition works');
351 ok(!$mic->search('SENTSINCE' => '18-Abr-2020'), 'bad month fails');
353 my $ret = $mic->fetch_hash(2, 'RFC822');
355 'MSN FETCH on empty dummy will not trigger warnings, later');
358 # message sequence numbers :<
359 is($mic->Uid(0), 0, 'disable UID on '.ref($mic));
360 ok($mic->reconnect, 'reconnected');
361 $ret = $mic->fetch_hash('1,2:3', 'RFC822') or BAIL_OUT "FETCH $@";
362 is(scalar keys %$ret, 3, 'got all 3 messages with comma-separated sequence');
363 $ret = $mic->fetch_hash('1:*', 'RFC822') or BAIL_OUT "FETCH $@";
364 is(scalar keys %$ret, 3, 'got all 3 messages');
367 # do any clients use non-UID IMAP SEARCH?
368 skip 'Xapian missing', 3 if $level eq 'basic';
369 my $x = $mic->search('all');
370 is_deeply($x, [1, 2, 3], 'MSN SEARCH works before rm');
371 $x = $mic->search(qw(header subject embedded));
372 is_deeply($x, [2], 'MSN SEARCH on Subject works before rm');
373 $x = $mic->search('FROM scraper@example.com');
374 is_deeply($x, [], "MSN SEARCH miss won't trigger warnings");
378 my $rdr = { 0 => \($ret->{1}->{RFC822}) };
379 my $env = { HOME => $ENV{HOME} };
380 my @cmd = qw(-learn rm --all);
381 run_script(\@cmd, $env, $rdr) or BAIL_OUT('-learn rm');
385 # do any clients use non-UID IMAP SEARCH? We only ensure
386 # MSN "SEARCH" can return a result which can be retrieved
388 skip 'Xapian missing', 3 if $level eq 'basic';
389 my $x = $mic->search(qw(header subject embedded));
390 is(scalar(@$x), 1, 'MSN SEARCH on Subject works after rm');
392 skip 'previous test failed', 1 if !scalar(@$x);
393 $x = $mic->message_string($x->[0]);
394 is($x, $ret->{2}->{RFC822}, 'message 2 unchanged');
396 $x = $mic->search(qw(text embedded));
397 is(scalar(@$x), 1, 'MSN SEARCH on TEXT works after rm');
400 # FIXME? no EXPUNGE response, yet
401 my $r2 = $mic->fetch_hash('1:*', 'BODY.PEEK[]') or BAIL_OUT "FETCH $@";
402 is(scalar keys %$r2, 2, 'did not get all 3 messages');
403 is($r2->{2}->{'BODY[]'}, $ret->{2}->{RFC822}, 'message 2 unchanged');
404 is($r2->{3}->{'BODY[]'}, $ret->{3}->{RFC822}, 'message 3 unchanged');
405 $r2 = $mic->fetch_hash(2, 'BODY.PEEK[HEADER.FIELDS (message-id)]')
406 or BAIL_OUT "FETCH $@";
407 is($r2->{2}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
408 'Message-ID: <20200418222508.GA13918@dcvr>'."\r\n\r\n",
409 'BODY.PEEK[HEADER.FIELDS ...] drops .PEEK');
412 my @new_list = $mic->list;
413 # tag differs in [-1]
414 like($orig_list[-1], qr/\A\S+ OK List done\r\n/, 'orig LIST');
415 like($new_list[-1], qr/\A\S+ OK List done\r\n/, 'new LIST');
418 # TODO: not sure if sort order matters, imapd_refresh_finalize
419 # doesn't sort, hopefully clients don't care...
420 @new_list = sort @new_list;
421 @orig_list = sort @orig_list;
422 is_deeply(\@new_list, \@orig_list, 'LIST identical');
424 ok($mic->close, 'CLOSE works');
425 ok(!$mic->close, 'CLOSE not idempotent');
426 ok($mic->logout, 'logged out');
429 my $c = tcp_connect($sock);
431 like(<$c>, qr/\* OK/, 'got a greeting');
433 like(<$c>, qr/\A\* BAD Error in IMAP command/, 'empty line');
434 print $c "tagonly\r\n";
435 like(<$c>, qr/\Atagonly BAD Error in IMAP command/, 'tag-only line');
439 use_ok 'PublicInbox::InboxIdle';
440 require_git('1.8.5', 1) or
441 skip('git 1.8.5+ needed for --urlmatch', 4);
442 my $old_env = { HOME => $ENV{HOME} };
443 my $home = "$tmpdir/watch_home";
444 mkdir $home or BAIL_OUT $!;
445 mkdir "$home/.public-inbox" or BAIL_OUT $!;
446 local $ENV{HOME} = $home;
447 my $name = 'watchimap';
448 my $addr = "i1-$level\@example.com";
449 my $url = "http://example.com/i1";
450 my $inboxdir = "$tmpdir/watchimap";
451 my $cmd = ['-init', '-V2', '-Lbasic', $name, $inboxdir, $url, $addr];
452 my $imapurl = "imap://$ihost:$iport/inbox.i1.0";
453 run_script($cmd) or BAIL_OUT("init $name");
454 xsys(qw(git config), "--file=$home/.public-inbox/config",
455 "publicinbox.$name.watch",
456 $imapurl) == 0 or BAIL_OUT "git config $?";
457 my $cfg = PublicInbox::Config->new;
458 PublicInbox::DS->Reset;
459 my $ii = PublicInbox::InboxIdle->new($cfg);
460 my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
461 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
462 $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
463 my $watcherr = "$tmpdir/watcherr";
464 open my $err_wr, '>>', $watcherr or BAIL_OUT $!;
465 open my $err, '<', $watcherr or BAIL_OUT $!;
466 my $w = start_script(['-watch'], undef, { 2 => $err_wr });
468 diag 'waiting for initial fetch...';
469 PublicInbox::DS->EventLoop;
470 diag 'inbox unlocked on initial fetch, waiting for IDLE';
472 tick until (grep(/I: \S+ idling/, <$err>));
473 open my $fh, '<', 't/iso-2202-jp.eml' or BAIL_OUT $!;
474 $old_env->{ORIGINAL_RECIPIENT} = $addr;
475 ok(run_script([qw(-mda --no-precheck)], $old_env, { 0 => $fh }),
476 'delivered a message for IDLE to kick -watch') or
477 diag "mda error \$?=$?";
478 diag 'waiting for IMAP IDLE wakeup';
479 PublicInbox::DS->SetPostLoopCallback(undef);
480 PublicInbox::DS->EventLoop;
481 diag 'inbox unlocked on IDLE wakeup';
483 # try again with polling
484 xsys(qw(git config), "--file=$home/.public-inbox/config",
485 'imap.PollInterval', 0.11) == 0
486 or BAIL_OUT "git config $?";
488 diag 'waiting for -watch reload + initial fetch';
489 tick until (grep(/I: will check/, <$err>));
491 open $fh, '<', 't/psgi_attach.eml' or BAIL_OUT $!;
492 ok(run_script([qw(-mda --no-precheck)], $old_env, { 0 => $fh }),
493 'delivered a message for -watch PollInterval');
495 diag 'waiting for PollInterval wakeup';
496 PublicInbox::DS->SetPostLoopCallback(undef);
497 PublicInbox::DS->EventLoop;
498 diag 'inbox unlocked (poll)';
501 is($?, 0, 'no error in exited -watch process');
503 $cfg->each_inbox(sub { shift->unsubscribe_unlock('ident') });
505 PublicInbox::DS->Reset;
507 my @err = grep(!/^(?:I:|#)/, <$err>);
508 is(@err, 0, 'no warnings/errors from -watch'.join(' ', @err));
510 if ($ENV{TEST_KILL_IMAPD}) { # not sure how reliable this test can be
511 xsys(qw(git config), "--file=$home/.public-inbox/config",
512 qw(--unset imap.PollInterval)) == 0
513 or BAIL_OUT "git config $?";
514 truncate($err_wr, 0) or BAIL_OUT $!;
516 $w = start_script(['-watch'], undef, { 2 => $err_wr });
518 tick until (grep(/I: \S+ idling/, <$err>));
519 diag 'killing imapd, waiting for CPU spins';
525 is($?, 0, 'no error in exited -watch process');
527 my $c = $t1[2] + $t1[3] - $t0[2] - $t0[3];
528 my $thresh = (0.9 * $delay);
529 diag "c=$c, threshold=$thresh";
530 ok($c < $thresh, 'did not burn much CPU');
531 is_deeply([grep(/ line \d+$/m, <$err>)], [],
532 'no backtraces from errors');
538 is($?, 0, 'no error in exited process') if !$ENV{TEST_KILL_IMAPD};
539 open my $fh, '<', $err or BAIL_OUT("open $err failed: $!");
540 my $eout = do { local $/; <$fh> };
541 unlike($eout, qr/wide/i, 'no Wide character warnings');
542 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');