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
12 Email::Address::XS||Mail::Address Parse::RecDescent));
13 my $imap_client = 'Mail::IMAPClient';
14 my $can_compress = $imap_client->can('compress');
15 if ($can_compress) { # hope this gets fixed upstream, soon
16 require PublicInbox::IMAPClient;
17 $imap_client = 'PublicInbox::IMAPClient';
20 require_ok 'PublicInbox::IMAP';
21 my $first_range = '0';
25 require_mods('Search::Xapian', 1);
30 push(@V, 2) if require_git('2.6', 1);
32 my ($tmpdir, $for_destroy) = tmpdir();
33 my $home = "$tmpdir/home";
34 local $ENV{HOME} = $home;
37 my $addr = "i$V\@example.com";
39 my $url = "http://example.com/i$V";
40 my $inboxdir = "$tmpdir/$name";
41 my $folder = "inbox.i$V";
42 my $cmd = ['-init', "-V$V", "-L$level", "--ng=$folder",
43 $name, $inboxdir, $url, $addr];
44 run_script($cmd) or BAIL_OUT("init $name");
46 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
47 'publicinboxmda.spamcheck', 'none') == 0 or
48 BAIL_OUT("config: $?");
50 open(my $fh, '<', 't/utf8.eml') or BAIL_OUT("open t/utf8.eml: $!");
51 my $env = { ORIGINAL_RECIPIENT => $addr };
52 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
53 BAIL_OUT('-mda delivery');
55 run_script(['-index', $inboxdir]) or BAIL_OUT("index $?");
58 my $sock = tcp_server();
59 my $err = "$tmpdir/stderr.log";
60 my $out = "$tmpdir/stdout.log";
61 my $cmd = [ '-imapd', '-W0', "--stdout=$out", "--stderr=$err" ];
62 my $td = start_script($cmd, undef, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
64 Server => $sock->sockhost,
65 Port => $sock->sockport,
68 my $mic = $imap_client->new(%mic_opt);
69 my $pre_login_capa = $mic->capability;
70 is(grep(/\AAUTH=ANONYMOUS\z/, @$pre_login_capa), 1,
71 'AUTH=ANONYMOUS advertised pre-login');
73 $mic->User('lorelei');
74 $mic->Password('Hunter2');
75 ok($mic->login && $mic->IsAuthenticated, 'LOGIN works');
76 my $post_login_capa = $mic->capability;
77 ok(join("\n", @$pre_login_capa) ne join("\n", @$post_login_capa),
78 'got different capabilities post-login');
80 $mic_opt{Authmechanism} = 'ANONYMOUS';
81 $mic_opt{Authcallback} = sub { '' };
82 $mic = $imap_client->new(%mic_opt);
83 ok($mic && $mic->login && $mic->IsAuthenticated, 'AUTHENTICATE ANONYMOUS');
84 my $post_auth_anon_capa = $mic->capability;
85 is_deeply($post_auth_anon_capa, $post_login_capa,
86 'auth anon has same capabilities');
88 ok($mic->noop, 'NOOP');
89 ok($mic->noop, 'NOOP (again)'); # for warnings
90 ok(!$mic->examine('foo') && ($e = $@), 'EXAMINE non-existent');
91 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
92 ok(!$mic->select('foo') && ($e = $@), 'EXAMINE non-existent');
93 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
94 my $mailbox1 = "inbox.i1.$first_range";
95 ok($mic->select('inbox.i1'), 'SELECT on parent succeeds');
96 ok($mic->noop, 'NOOP while selected');
97 ok($mic->noop, 'NOOP again while selected'); # check warnings later
98 ok($mic->select($mailbox1), 'SELECT succeeds');
99 ok($mic->examine($mailbox1), 'EXAMINE succeeds');
100 my @raw = $mic->status($mailbox1, qw(Messages uidnext uidvalidity));
101 is(scalar(@raw), 2, 'got status response');
102 like($raw[0], qr/\A\*\x20STATUS\x20inbox\.i1\.$first_range\x20
103 \(MESSAGES\x20\d+\x20UIDNEXT\x20\d+\x20UIDVALIDITY\x20\d+\)\r\n/sx);
104 like($raw[1], qr/\A\S+ OK /, 'finished status response');
106 my @orig_list = @raw = $mic->list;
107 like($raw[0], qr/^\* LIST \(.*?\) "\." INBOX/,
109 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
110 is(scalar(@raw), scalar(@V) * 2 + 2, 'default LIST response');
111 @raw = $mic->list('', 'inbox.i1');
112 is(scalar(@raw), 2, 'limited LIST response');
113 like($raw[0], qr/^\* LIST \(.*?\) "\." INBOX/,
115 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
117 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
118 is_deeply($ret, [ 1 ], 'search all works');
119 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
120 is_deeply($ret, [ 1 ], 'search UID 1 works');
121 $ret = $mic->search('uid 1:1') or BAIL_OUT "SEARCH FAIL $@";
122 is_deeply($ret, [ 1 ], 'search UID 1:1 works');
123 $ret = $mic->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
124 is_deeply($ret, [ 1 ], 'search UID 1:* works');
127 skip 'Xapian missing', 7 if $level eq 'basic';
128 my $x = $mic->search(qw(smaller 99999));
129 is_deeply($x, [1], 'SMALLER works with Xapian (hit)');
130 $x = $mic->search(qw(smaller 9));
131 is_deeply($x, [], 'SMALLER works with Xapian (miss)');
133 $x = $mic->search(qw(larger 99999));
134 is_deeply($x, [], 'LARGER works with Xapian (miss)');
135 $x = $mic->search(qw(larger 9));
136 is_deeply($x, [1], 'LARGER works with Xapian (hit)');
138 $x = $mic->search(qw(HEADER Message-ID testmessage@example.com));
139 is_deeply($x, [1], 'HEADER Message-ID works');
140 $x = $mic->search(qw(HEADER Message-ID miss));
141 is_deeply($x, [], 'HEADER Message-ID can miss');
143 my @q = qw[OR HEADER Message-ID testmessage@example.com
144 (OR FROM Ryan (OR TO Joe CC Scott))];
145 $x = $mic->search(join(' ', @q));
146 is_deeply($x, [1], 'nested query works');
149 is_deeply(scalar $mic->flags('1'), [], '->flags works');
151 # RFC 3501 section 6.4.8 states:
152 # Also note that a UID range of 559:* always includes the
153 # UID of the last message in the mailbox, even if 559 is
154 # higher than any assigned UID value.
155 my $exp = $mic->fetch_hash(1, 'UID');
156 $ret = $mic->fetch_hash('559:*', 'UID');
157 is_deeply($ret, $exp, 'beginning range too big');
159 my @w; # Mail::IMAPClient hits a warning via overload
160 local $SIG{__WARN__} = sub { push @w, @_ };
161 $ret = $mic->fetch_hash(my $r = '559:558', 'UID');
162 is_deeply($ret, {}, "out-of-range UID FETCH $r");
163 @w = grep(!/\boverload\.pm\b/, @w);
164 is_deeply(\@w, [], 'no unexpected warning');
166 $ret = $mic->fetch_hash(my $r = '558:559', 'UID');
167 is_deeply($ret, {}, "out-of-range UID FETCH $r");
170 for my $r ('1:*', '1') {
171 $ret = $mic->fetch_hash($r, 'RFC822') or BAIL_OUT "FETCH $@";
172 is_deeply([keys %$ret], [1]);
173 like($ret->{1}->{RFC822}, qr/\r\n\r\nThis is a test/, 'read full');
175 # ensure Mail::IMAPClient behaves
176 my $str = $mic->message_string($r) or BAIL_OUT "->message_string: $@";
177 is($str, $ret->{1}->{RFC822}, '->message_string works as expected');
179 my $sz = $mic->fetch_hash($r, 'RFC822.size') or BAIL_OUT "FETCH $@";
180 is($sz->{1}->{'RFC822.SIZE'}, length($ret->{1}->{RFC822}),
183 $ret = $mic->fetch_hash($r, 'RFC822.HEADER') or BAIL_OUT "FETCH $@";
184 is_deeply([keys %$ret], [1]);
185 like($ret->{1}->{'RFC822.HEADER'},
186 qr/^Message-ID: <testmessage\@example\.com>/ms, 'read header');
188 $ret = $mic->fetch_hash($r, 'INTERNALDATE') or BAIL_OUT "FETCH $@";
189 is($ret->{1}->{'INTERNALDATE'}, '01-Jan-1970 00:00:00 +0000',
190 'internaldate matches');
191 ok(!$mic->fetch_hash($r, 'INFERNALDATE'), 'bogus attribute fails');
193 my $envelope = $mic->get_envelope($r) or BAIL_OUT("get_envelope: $@");
194 is($envelope->{bcc}, 'NIL', 'empty bcc');
195 is($envelope->{messageid}, '<testmessage@example.com>', 'messageid');
196 is(scalar @{$envelope->{to}}, 1, 'one {to} header');
197 # *sigh* too much to verify...
198 #use Data::Dumper; diag Dumper($envelope);
200 $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
201 is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
203 $ret = $mic->fetch_hash($r, 'BODY[1]') or BAIL_OUT "FETCH $@";
204 like($ret->{1}->{'BODY[1]'}, qr/\AThis is a test message/, 'BODY[1]');
206 $ret = $mic->fetch_hash($r, 'BODY[1]<1>') or BAIL_OUT "FETCH $@";
207 like($ret->{1}->{'BODY[1]<1>'}, qr/\Ahis is a test message/,
210 $ret = $mic->fetch_hash($r, 'BODY[1]<2.3>') or BAIL_OUT "FETCH $@";
211 is($ret->{1}->{'BODY[1]<2>'}, "is ", 'BODY[1]<2.3>');
212 $ret = $mic->bodypart_string($r, 1, 3, 2) or
213 BAIL_OUT "bodypart_string $@";
214 is($ret, "is ", 'bodypart string');
216 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS.NOT (Message-ID)]')
217 or BAIL_OUT "FETCH $@";
218 $ret = $ret->{1}->{'BODY[HEADER.FIELDS.NOT (MESSAGE-ID)]'};
219 unlike($ret, qr/message-id/i, 'Message-ID excluded');
220 like($ret, qr/\r\n\r\n\z/s, 'got header end');
222 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS (Message-ID)]')
223 or BAIL_OUT "FETCH $@";
224 is($ret->{1}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
225 'Message-ID: <testmessage@example.com>'."\r\n\r\n",
226 'got only Message-ID');
228 my $bs = $mic->get_bodystructure($r) or BAIL_OUT("bodystructure: $@");
229 ok($bs, 'got a bodystructure');
230 is(lc($bs->bodytype), 'text', '->bodytype');
231 is(lc($bs->bodyenc), '8bit', '->bodyenc');
233 ok($mic->has_capability('COMPRESS') ||
234 $mic->has_capability('COMPRESS=DEFLATE'), 'deflate cap');
236 skip 'Mail::IMAPClient too old for ->compress', 2 if !$can_compress;
237 my $c = $imap_client->new(%mic_opt);
238 ok($c && $c->compress, 'compress enabled');
239 ok($c->examine($mailbox1), 'EXAMINE succeeds after COMPRESS');
240 $ret = $c->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
241 is_deeply($ret, [ 1 ], 'search UID 1:* works after compression');
244 ok($mic->logout, 'logout works');
246 my $have_inotify = eval { require Linux::Inotify2; 1 };
248 my $pi_config = PublicInbox::Config->new;
249 $pi_config->each_inbox(sub {
251 my $env = { ORIGINAL_RECIPIENT => $ibx->{-primary_address} };
252 my $name = $ibx->{name};
253 my $ng = $ibx->{newsgroup};
254 my $mic = $imap_client->new(%mic_opt);
255 ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
256 my $mb = "$ng.$first_range";
257 my $uidnext = $mic->uidnext($mb); # we'll fetch BODYSTRUCTURE on this
258 ok($uidnext, 'got uidnext for later fetch');
259 ok($mic->has_capability('IDLE'), "IDLE capa $name");
260 ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
261 ok($mic->examine($mb), "EXAMINE $ng succeeds");
262 ok(my $idle_tag = $mic->idle, "IDLE succeeds on $ng");
264 open(my $fh, '<', 't/data/message_embed.eml') or BAIL_OUT("open: $!");
265 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
266 BAIL_OUT('-mda delivery');
267 my $t0 = Time::HiRes::time();
268 ok(my @res = $mic->idle_data(11), "IDLE succeeds on $ng");
269 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
270 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
272 my (@ino_info, $ino_fdinfo);
274 skip 'no inotify support', 1 unless $have_inotify;
275 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$td->{pid}/fd";
277 readlink($_) =~ /\binotify\b/
278 } glob("/proc/$td->{pid}/fd/*");
279 is(scalar(@ino), 1, 'only one inotify FD');
280 my $ino_fd = (split('/', $ino[0]))[-1];
281 $ino_fdinfo = "/proc/$td->{pid}/fdinfo/$ino_fd";
282 if (open my $fh, '<', $ino_fdinfo) {
284 @ino_info = grep(/^inotify wd:/, <$fh>);
285 ok(scalar(@ino_info), 'inotify has watches');
287 skip "$ino_fdinfo missing: $!", 1;
291 # ensure IDLE persists across HUP, w/o extra watches or FDs
292 $td->kill('HUP') or BAIL_OUT "failed to kill -imapd: $!";
294 skip 'no inotify fdinfo (or support)', 2 if !@ino_info;
298 until (time > $end) {
299 select undef, undef, undef, 0.01;
300 open my $fh, '<', $ino_fdinfo or
301 BAIL_OUT "$ino_fdinfo: $!";
302 %prev = map { $_ => 1 } @ino_info;
303 @tmp = grep(/^inotify wd:/, <$fh>);
304 if (scalar(@tmp) == scalar(@ino_info)) {
306 last if scalar(keys(%prev)) == @ino_info;
309 is(scalar @tmp, scalar @ino_info,
310 'old inotify watches replaced');
311 is(scalar keys %prev, scalar @ino_info,
312 'no previous watches overlap');
315 open($fh, '<', 't/data/0001.patch') or BAIL_OUT("open: $!");
316 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
317 BAIL_OUT('-mda delivery');
318 $t0 = Time::HiRes::time();
319 ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
320 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
321 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
322 ok($mic->done($idle_tag), 'IDLE DONE');
323 my $bs = $mic->get_bodystructure($uidnext);
324 ok($bs, 'BODYSTRUCTURE ok for deeply nested');
325 $ret = $mic->fetch_hash($uidnext, 'BODY') or BAIL_OUT "FETCH $@";
326 ok($ret->{$uidnext}->{BODY}, 'got something in BODY');
328 # this matches dovecot behavior
329 $ret = $mic->fetch_hash($uidnext, 'BODY[1]') or BAIL_OUT "FETCH $@";
330 is($ret->{$uidnext}->{'BODY[1]'},
331 "testing embedded message harder\r\n", 'BODY[1]');
332 $ret = $mic->fetch_hash($uidnext, 'BODY[2]') or BAIL_OUT "FETCH $@";
333 like($ret->{$uidnext}->{'BODY[2]'},
334 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /, 'BODY[2]');
336 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.1]') or BAIL_OUT "FETCH $@";
337 is($ret->{$uidnext}->{'BODY[2.1.1]'},
338 "testing embedded message\r\n", 'BODY[2.1.1]');
340 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.2]') or BAIL_OUT "FETCH $@";
341 like($ret->{$uidnext}->{'BODY[2.1.2]'}, qr/\AFrom: /,
342 'BODY[2.1.2] tip matched');
343 like($ret->{$uidnext}->{'BODY[2.1.2]'},
344 # trailing CRLF may vary depending on MIME parser
345 qr/done_testing;(?:\r\n){1,2}\z/,
346 'BODY[2.1.2] tail matched');
348 $ret = $mic->fetch_hash("1:$uidnext", 'BODY[2.HEADER]') or
349 BAIL_OUT "2.HEADER $@";
350 like($ret->{$uidnext}->{'BODY[2.HEADER]'},
351 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /,
352 '2.HEADER of message/rfc822');
354 $ret = $mic->fetch_hash($uidnext, 'BODY[2.MIME]') or
355 BAIL_OUT "2.MIME $@";
356 is($ret->{$uidnext}->{'BODY[2.MIME]'}, <<EOF, 'BODY[2.MIME]');
357 Content-Type: message/rfc822\r
358 Content-Disposition: attachment; filename="embed2x\.eml"\r
362 my @hits = $mic->search('SENTON' => '18-Apr-2020');
363 is_deeply(\@hits, [ $uidnext ], 'search with date condition works');
364 ok($mic->examine($ng), 'EXAMINE on dummy');
365 @hits = $mic->search('SENTSINCE' => '18-Apr-2020');
366 is_deeply(\@hits, [], 'search on dummy with condition works');
367 ok(!$mic->search('SENTSINCE' => '18-Abr-2020'), 'bad month fails');
370 # message sequence numbers :<
371 is($mic->Uid(0), 0, 'disable UID on '.ref($mic));
372 ok($mic->reconnect, 'reconnected');
373 $ret = $mic->fetch_hash('1,2:3', 'RFC822') or BAIL_OUT "FETCH $@";
374 is(scalar keys %$ret, 3, 'got all 3 messages with comma-separated sequence');
375 $ret = $mic->fetch_hash('1:*', 'RFC822') or BAIL_OUT "FETCH $@";
376 is(scalar keys %$ret, 3, 'got all 3 messages');
379 # do any clients use non-UID IMAP SEARCH?
380 skip 'Xapian missing', 2 if $level eq 'basic';
381 my $x = $mic->search('all');
382 is_deeply($x, [1, 2, 3], 'MSN SEARCH works before rm');
383 $x = $mic->search(qw(header subject embedded));
384 is_deeply($x, [2], 'MSN SEARCH on Subject works before rm');
388 my $rdr = { 0 => \($ret->{1}->{RFC822}) };
389 my $env = { HOME => $ENV{HOME} };
390 my @cmd = qw(-learn rm --all);
391 run_script(\@cmd, $env, $rdr) or BAIL_OUT('-learn rm');
395 # do any clients use non-UID IMAP SEARCH? We only ensure
396 # MSN "SEARCH" can return a result which can be retrieved
398 skip 'Xapian missing', 3 if $level eq 'basic';
399 my $x = $mic->search(qw(header subject embedded));
400 is(scalar(@$x), 1, 'MSN SEARCH on Subject works after rm');
401 $x = $mic->message_string($x->[0]);
402 is($x, $ret->{2}->{RFC822}, 'message 2 unchanged');
403 $x = $mic->search(qw(text embedded));
404 is(scalar(@$x), 1, 'MSN SEARCH on TEXT works after rm');
407 # FIXME? no EXPUNGE response, yet
408 my $r2 = $mic->fetch_hash('1:*', 'BODY.PEEK[]') or BAIL_OUT "FETCH $@";
409 is(scalar keys %$r2, 2, 'did not get all 3 messages');
410 is($r2->{2}->{'BODY[]'}, $ret->{2}->{RFC822}, 'message 2 unchanged');
411 is($r2->{3}->{'BODY[]'}, $ret->{3}->{RFC822}, 'message 3 unchanged');
412 $r2 = $mic->fetch_hash(2, 'BODY.PEEK[HEADER.FIELDS (message-id)]')
413 or BAIL_OUT "FETCH $@";
414 is($r2->{2}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
415 'Message-ID: <20200418222508.GA13918@dcvr>'."\r\n\r\n",
416 'BODY.PEEK[HEADER.FIELDS ...] drops .PEEK');
419 my @new_list = $mic->list;
420 # tag differs in [-1]
421 like($orig_list[-1], qr/\A\S+ OK List done\r\n/, 'orig LIST');
422 like($new_list[-1], qr/\A\S+ OK List done\r\n/, 'new LIST');
425 # TODO: not sure if sort order matters, imapd_refresh_finalize
426 # doesn't sort, hopefully clients don't care...
427 @new_list = sort @new_list;
428 @orig_list = sort @orig_list;
429 is_deeply(\@new_list, \@orig_list, 'LIST identical');
431 ok($mic->close, 'CLOSE works');
432 ok(!$mic->close, 'CLOSE not idempotent');
433 ok($mic->logout, 'logged out');
436 my $c = tcp_connect($sock);
438 like(<$c>, qr/\* OK/, 'got a greeting');
440 like(<$c>, qr/\A\* BAD Error in IMAP command/, 'empty line');
441 print $c "tagonly\r\n";
442 like(<$c>, qr/\Atagonly BAD Error in IMAP command/, 'tag-only line');
446 use_ok 'PublicInbox::WatchMaildir';
447 use_ok 'PublicInbox::InboxIdle';
448 require_git('1.8.5', 1) or
449 skip('git 1.8.5+ needed for --urlmatch', 4);
450 my $old_env = { HOME => $ENV{HOME} };
451 my $home = "$tmpdir/watch_home";
452 mkdir $home or BAIL_OUT $!;
453 mkdir "$home/.public-inbox" or BAIL_OUT $!;
454 local $ENV{HOME} = $home;
455 my $name = 'watchimap';
456 my $addr = "i1\@example.com";
457 my $url = "http://example.com/i1";
458 my $inboxdir = "$tmpdir/watchimap";
459 my $cmd = ['-init', '-V2', '-Lbasic', $name, $inboxdir, $url, $addr];
460 my ($ihost, $iport) = ($sock->sockhost, $sock->sockport);
461 my $imapurl = "imap://$ihost:$iport/inbox.i1.0";
462 run_script($cmd) or BAIL_OUT("init $name");
463 xsys(qw(git config), "--file=$home/.public-inbox/config",
464 "publicinbox.$name.watch",
465 $imapurl) == 0 or BAIL_OUT "git config $?";
466 my $cfg = PublicInbox::Config->new;
467 PublicInbox::DS->Reset;
468 my $ii = PublicInbox::InboxIdle->new($cfg);
469 my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
470 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
471 $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
472 my $watcherr = "$tmpdir/watcherr";
473 open my $err_wr, '>>', $watcherr or BAIL_OUT $!;
474 open my $err, '<', $watcherr or BAIL_OUT $!;
475 my $w = start_script(['-watch'], undef, { 2 => $err_wr });
477 diag 'waiting for initial fetch...';
478 PublicInbox::DS->EventLoop;
479 diag 'inbox unlocked on initial fetch, waiting for IDLE';
481 tick until (grep(/I: \S+ idling/, <$err>));
482 open my $fh, '<', 't/iso-2202-jp.eml' or BAIL_OUT $!;
483 $old_env->{ORIGINAL_RECIPIENT} = $addr;
484 ok(run_script([qw(-mda --no-precheck)], $old_env, { 0 => $fh }),
485 'delivered a message for IDLE to kick -watch');
486 diag 'waiting for IMAP IDLE wakeup';
487 PublicInbox::DS->SetPostLoopCallback(undef);
488 PublicInbox::DS->EventLoop;
489 diag 'inbox unlocked on IDLE wakeup';
491 # try again with polling
492 xsys(qw(git config), "--file=$home/.public-inbox/config",
493 'imap.PollInterval', 0.11) == 0
494 or BAIL_OUT "git config $?";
496 diag 'waiting for -watch reload + initial fetch';
497 tick until (grep(/I: will check/, <$err>));
499 open $fh, '<', 't/psgi_attach.eml' or BAIL_OUT $!;
500 ok(run_script([qw(-mda --no-precheck)], $old_env, { 0 => $fh }),
501 'delivered a message for -watch PollInterval');
503 diag 'waiting for PollInterval wakeup';
504 PublicInbox::DS->SetPostLoopCallback(undef);
505 PublicInbox::DS->EventLoop;
506 diag 'inbox unlocked (poll)';
509 is($?, 0, 'no error in exited -watch process');
511 $cfg->each_inbox(sub { shift->unsubscribe_unlock('ident') });
513 PublicInbox::DS->Reset;
515 my @err = grep(!/^I:/, <$err>);
516 is(@err, 0, 'no warnings/errors from -watch'.join(' ', @err));
518 if ($ENV{TEST_KILL_IMAPD}) { # not sure how reliable this test can be
519 xsys(qw(git config), "--file=$home/.public-inbox/config",
520 qw(--unset imap.PollInterval)) == 0
521 or BAIL_OUT "git config $?";
522 truncate($err_wr, 0) or BAIL_OUT $!;
524 $w = start_script(['-watch'], undef, { 2 => $err_wr });
526 tick until (grep(/I: \S+ idling/, <$err>));
527 diag 'killing imapd, waiting for CPU spins';
533 is($?, 0, 'no error in exited -watch process');
535 my $c = $t1[2] + $t1[3] - $t0[2] - $t0[3];
536 my $thresh = (0.9 * $delay);
537 diag "c=$c, threshold=$thresh";
538 ok($c < $thresh, 'did not burn much CPU');
539 is_deeply([grep(/ line \d+$/m, <$err>)], [],
540 'no backtraces from errors');
546 is($?, 0, 'no error in exited process') if !$ENV{TEST_KILL_IMAPD};
547 open my $fh, '<', $err or BAIL_OUT("open $err failed: $!");
548 my $eout = do { local $/; <$fh> };
549 unlike($eout, qr/wide/i, 'no Wide character warnings');
550 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');