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');
85 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
86 is_deeply($ret, [ 1 ], 'search all works');
87 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
88 is_deeply($ret, [ 1 ], 'search UID 1 works');
89 $ret = $mic->search('uid 1:1') or BAIL_OUT "SEARCH FAIL $@";
90 is_deeply($ret, [ 1 ], 'search UID 1:1 works');
91 $ret = $mic->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
92 is_deeply($ret, [ 1 ], 'search UID 1:* works');
94 is_deeply(scalar $mic->flags('1'), [], '->flags works');
96 for my $r ('1:*', '1') {
97 $ret = $mic->fetch_hash($r, 'RFC822') or BAIL_OUT "FETCH $@";
98 is_deeply([keys %$ret], [1]);
99 like($ret->{1}->{RFC822}, qr/\r\n\r\nThis is a test/, 'read full');
101 # ensure Mail::IMAPClient behaves
102 my $str = $mic->message_string($r) or BAIL_OUT "->message_string: $@";
103 is($str, $ret->{1}->{RFC822}, '->message_string works as expected');
105 my $sz = $mic->fetch_hash($r, 'RFC822.size') or BAIL_OUT "FETCH $@";
106 is($sz->{1}->{'RFC822.SIZE'}, length($ret->{1}->{RFC822}),
109 $ret = $mic->fetch_hash($r, 'RFC822.HEADER') or BAIL_OUT "FETCH $@";
110 is_deeply([keys %$ret], [1]);
111 like($ret->{1}->{'RFC822.HEADER'},
112 qr/^Message-ID: <testmessage\@example\.com>/ms, 'read header');
114 $ret = $mic->fetch_hash($r, 'INTERNALDATE') or BAIL_OUT "FETCH $@";
115 is($ret->{1}->{'INTERNALDATE'}, '01-Jan-1970 00:00:00 +0000',
116 'internaldate matches');
117 ok(!$mic->fetch_hash($r, 'INFERNALDATE'), 'bogus attribute fails');
119 my $envelope = $mic->get_envelope($r) or BAIL_OUT("get_envelope: $@");
120 is($envelope->{bcc}, 'NIL', 'empty bcc');
121 is($envelope->{messageid}, '<testmessage@example.com>', 'messageid');
122 is(scalar @{$envelope->{to}}, 1, 'one {to} header');
123 # *sigh* too much to verify...
124 #use Data::Dumper; diag Dumper($envelope);
126 $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
127 is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
130 # Mail::IMAPClient ->compress creates cyclic reference:
131 # https://rt.cpan.org/Ticket/Display.html?id=132654
132 my $compress_logout = sub {
134 ok($c->logout, 'logout ok after ->compress');
135 # all documented in Mail::IMAPClient manpage:
136 for (qw(Readmoremethod Readmethod Prewritemethod)) {
141 is_deeply([$mic->has_capability('COMPRESS')], ['DEFLATE'], 'deflate cap');
142 ok($mic->compress, 'compress enabled');
143 $compress_logout->($mic);
145 my $have_inotify = eval { require Linux::Inotify2; 1 };
147 my $pi_config = PublicInbox::Config->new;
148 $pi_config->each_inbox(sub {
150 my $env = { ORIGINAL_RECIPIENT => $ibx->{-primary_address} };
151 my $name = $ibx->{name};
152 my $ng = $ibx->{newsgroup};
153 my $mic = Mail::IMAPClient->new(%mic_opt);
154 ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
155 is_deeply([$mic->has_capability('IDLE')], ['IDLE'], "IDLE capa $name");
156 ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
157 ok($mic->examine($ng), "EXAMINE $ng succeeds");
158 ok($mic->idle, "IDLE succeeds on $ng");
160 open(my $fh, '<', 't/data/message_embed.eml') or BAIL_OUT("open: $!");
161 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
162 BAIL_OUT('-mda delivery');
163 my $t0 = Time::HiRes::time();
164 ok(my @res = $mic->idle_data(11), "IDLE succeeds on $ng");
165 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
166 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
168 my (@ino_info, $ino_fdinfo);
170 skip 'no inotify support', 1 unless $have_inotify;
171 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$td->{pid}/fd";
173 readlink($_) =~ /\binotify\b/
174 } glob("/proc/$td->{pid}/fd/*");
175 is(scalar(@ino), 1, 'only one inotify FD');
176 my $ino_fd = (split('/', $ino[0]))[-1];
177 $ino_fdinfo = "/proc/$td->{pid}/fdinfo/$ino_fd";
178 if (open my $fh, '<', $ino_fdinfo) {
180 @ino_info = grep(/^inotify wd:/, <$fh>);
181 ok(scalar(@ino_info), 'inotify has watches');
183 skip "$ino_fdinfo missing: $!", 1;
187 # ensure IDLE persists across HUP, w/o extra watches or FDs
188 $td->kill('HUP') or BAIL_OUT "failed to kill -imapd: $!";
190 skip 'no inotify fdinfo (or support)', 2 if !@ino_info;
194 until (time > $end) {
195 select undef, undef, undef, 0.01;
196 open my $fh, '<', $ino_fdinfo or
197 BAIL_OUT "$ino_fdinfo: $!";
198 %prev = map { $_ => 1 } @ino_info;
199 @tmp = grep(/^inotify wd:/, <$fh>);
200 if (scalar(@tmp) == scalar(@ino_info)) {
202 last if scalar(keys(%prev)) == @ino_info;
205 is(scalar @tmp, scalar @ino_info,
206 'old inotify watches replaced');
207 is(scalar keys %prev, scalar @ino_info,
208 'no previous watches overlap');
211 open($fh, '<', 't/data/0001.patch') or BAIL_OUT("open: $!");
212 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
213 BAIL_OUT('-mda delivery');
214 $t0 = Time::HiRes::time();
215 ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
216 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
217 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
222 is($?, 0, 'no error in exited process');
223 open my $fh, '<', $err or BAIL_OUT("open $err failed: $!");
224 my $eout = do { local $/; <$fh> };
225 unlike($eout, qr/wide/i, 'no Wide character warnings');