2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
6 use Socket qw(IPPROTO_TCP SOL_SOCKET);
7 my $cert = 'certs/server-cert.pem';
8 my $key = 'certs/server-key.pem';
9 unless (-r $key && -r $cert) {
11 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
14 # Net::POP3 is part of the standard library, but distros may split it off...
15 require_mods(qw(DBD::SQLite Net::POP3 IO::Socket::SSL));
16 require_git('2.6'); # for v2
17 require_mods(qw(File::FcntlLock)) if $^O !~ /\A(?:linux|freebsd)\z/;
18 use_ok 'IO::Socket::SSL';
19 use_ok 'PublicInbox::TLS';
20 my ($tmpdir, $for_destroy) = tmpdir();
21 mkdir("$tmpdir/p3state") or xbail "mkdir: $!";
22 my $err = "$tmpdir/stderr.log";
23 my $out = "$tmpdir/stdout.log";
24 my $olderr = "$tmpdir/plain.err";
25 my $group = 'test-pop3';
26 my $addr = $group . '@example.com';
27 my $stls = tcp_server();
28 my $plain = tcp_server();
29 my $pop3s = tcp_server();
30 my $patch = eml_load('t/data/0001.patch');
31 my $ibx = create_inbox 'pop3d', version => 2, -primary_address => $addr,
32 indexlevel => 'basic', sub {
34 $im->add(eml_load('t/plack-qp.eml')) or BAIL_OUT '->add';
35 $im->add($patch) or BAIL_OUT '->add';
37 my $pi_config = "$tmpdir/pi_config";
38 open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
39 print $fh <<EOF or BAIL_OUT "print: $!";
41 pop3state = $tmpdir/p3state
43 inboxdir = $ibx->{inboxdir}
48 close $fh or BAIL_OUT "close: $!\n";
50 my $pop3s_addr = tcp_host_port($pop3s);
51 my $stls_addr = tcp_host_port($stls);
52 my $plain_addr = tcp_host_port($plain);
53 my $env = { PI_CONFIG => $pi_config };
54 my $old = start_script(['-pop3d', '-W0',
55 "--stdout=$tmpdir/plain.out", "--stderr=$olderr" ],
56 $env, { 3 => $plain });
57 my @old_args = ($plain->sockhost, Port => $plain->sockport);
58 my $oldc = Net::POP3->new(@old_args);
59 my $locked_mb = ('e'x32)."\@$group";
60 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP to old');
62 my $dbh = DBI->connect("dbi:SQLite:dbname=$tmpdir/p3state/db.sqlite3",'','', {
66 sqlite_use_immediate_transaction => 1,
67 sqlite_see_if_its_a_number => 1,
70 { # locking within the same process
71 my $x = Net::POP3->new(@old_args);
72 ok(!$x->apop("$locked_mb.0", 'anonymous'), 'APOP lock failure');
73 like($x->message, qr/unable to lock/, 'diagnostic message');
75 $x = Net::POP3->new(@old_args);
76 ok($x->apop($locked_mb, 'anonymous'), 'APOP lock acquire');
78 my $y = Net::POP3->new(@old_args);
79 ok(!$y->apop($locked_mb, 'anonymous'), 'APOP lock fails once');
82 $y = Net::POP3->new(@old_args);
83 ok($y->apop($locked_mb, 'anonymous'), 'APOP lock works after release');
87 [ "--cert=$cert", "--key=$key",
88 "-lpop3s://$pop3s_addr",
89 "-lpop3://$stls_addr" ],
91 for ($out, $err) { open my $fh, '>', $_ or BAIL_OUT "truncate: $!" }
92 my $cmd = [ '-netd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
93 my $td = start_script($cmd, $env, { 3 => $stls, 4 => $pop3s });
96 SSL_hostname => 'server.local',
97 SSL_verifycn_name => 'server.local',
98 SSL_verify_mode => SSL_VERIFY_PEER(),
99 SSL_ca_file => 'certs/test-ca.pem',
101 # start negotiating a slow TLS connection
102 my $slow = tcp_connect($pop3s, Blocking => 0);
103 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
104 my $slow_done = $slow->connect_SSL;
107 diag('W: connect_SSL early OK, slow client test invalid');
108 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
109 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
111 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
114 my @p3s_args = ($pop3s->sockhost,
115 Port => $pop3s->sockport, SSL => 1, %o);
116 my $p3s = Net::POP3->new(@p3s_args);
117 my $capa = $p3s->capa;
118 ok(!exists $capa->{STLS}, 'no STLS CAPA for POP3S');
119 ok($p3s->quit, 'QUIT works w/POP3S');
121 $p3s = Net::POP3->new(@p3s_args);
122 ok(!$p3s->apop("$locked_mb.0", 'anonymous'),
123 'APOP lock failure w/ another daemon');
124 like($p3s->message, qr/unable to lock/, 'diagnostic message');
127 # slow TLS connection did not block the other fast clients while
128 # connecting, finish it off:
130 IO::Poll::_poll(-1, @poll);
131 $slow_done = $slow->connect_SSL and last;
132 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
135 ok(sysread($slow, my $greet, 4096) > 0, 'slow got a greeting');
136 my @np3_args = ($stls->sockhost, Port => $stls->sockport);
137 my $np3 = Net::POP3->new(@np3_args);
138 ok($np3->quit, 'plain QUIT works');
139 $np3 = Net::POP3->new(@np3_args, %o);
141 ok(exists $capa->{STLS}, 'STLS CAPA advertised before STLS');
142 ok($np3->starttls, 'STLS works');
144 ok(!exists $capa->{STLS}, 'STLS CAPA not advertised after STLS');
145 ok($np3->quit, 'QUIT works after STLS');
147 for my $mailbox (('x'x32)."\@$group", $group, ('a'x32)."\@z.$group") {
148 $np3 = Net::POP3->new(@np3_args);
149 ok(!$np3->user($mailbox), "USER $mailbox reject");
150 ok($np3->quit, 'QUIT after USER fail');
152 $np3 = Net::POP3->new(@np3_args);
153 ok(!$np3->apop($mailbox, 'anonymous'), "APOP $mailbox reject");
154 ok($np3->quit, "QUIT after APOP fail $mailbox");
157 # we do connect+QUIT bumps to try ensuring non-QUIT disconnects
158 # get processed below:
159 for my $mailbox ($group, "$group.0") {
160 my $u = ('f'x32)."\@$mailbox";
162 ok(Net::POP3->new(@np3_args)->quit, 'connect+QUIT bump');
163 $np3 = Net::POP3->new(@np3_args);
164 my $n0 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
165 my $u0 = $dbh->selectrow_array('SELECT COUNT(*) FROM users');
166 ok($np3->user($u), "UUID\@$mailbox accept");
167 ok($np3->pass('anonymous'), 'pass works');
168 my $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
169 is($n1 - $n0, 1, 'deletes bumped while connected');
170 ok($np3->quit, 'client QUIT');
172 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
173 is($n1, $n0, 'deletes row gone on no-op after QUIT');
174 my $u1 = $dbh->selectrow_array('SELECT COUNT(*) FROM users');
175 is($u1, $u0, 'users row gone on no-op after QUIT');
177 $np3 = Net::POP3->new(@np3_args);
178 ok($np3->user($u), "UUID\@$mailbox accept");
179 ok($np3->pass('anonymous'), 'pass works');
181 my $list = $np3->list;
182 my $uidl = $np3->uidl;
183 is_deeply([sort keys %$list], [sort keys %$uidl],
184 'LIST and UIDL keys match');
185 ok($_ > 0, 'bytes in LIST result') for values %$list;
186 like($_, qr/\A[a-z0-9]{40,}\z/,
187 'blob IDs in UIDL result') for values %$uidl;
188 ok($np3->quit, 'QUIT after LIST+UIDL');
189 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
190 is($n1, $n0, 'deletes row gone on no-op after LIST+UIDL');
193 $np3 = Net::POP3->new(@np3_args);
194 ok($np3->user($u), "UUID\@$mailbox accept");
195 ok($np3->pass('anonymous'), 'pass works');
196 undef $np3; # QUIT-less disconnect
197 ok(Net::POP3->new(@np3_args)->quit, 'connect+QUIT bump');
199 $u1 = $dbh->selectrow_array('SELECT COUNT(*) FROM users');
200 is($u1, $u0, 'users row gone on QUIT-less disconnect');
201 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
202 is($n1, $n0, 'deletes row gone on QUIT-less disconnect');
205 $np3 = Net::POP3->new(@np3_args);
206 ok(!$np3->apop($u, 'anonumuss'), 'APOP wrong pass reject');
207 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
208 is($n1, $n0, 'deletes row not bumped w/ wrong pass');
209 undef $np3; # QUIT-less disconnect
210 ok(Net::POP3->new(@np3_args)->quit, 'connect+QUIT bump');
212 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
213 is($n1, $n0, 'deletes row not bumped w/ wrong pass');
215 $np3 = Net::POP3->new(@np3_args);
216 ok($np3->apop($u, 'anonymous'), "APOP UUID\@$mailbox");
217 my @res = $np3->popstat;
218 is($res[0], 2, 'STAT knows about 2 messages');
220 my $msg = $np3->get(2);
221 $msg = join('', @$msg);
223 is_deeply(PublicInbox::Eml->new($msg), $patch,
224 't/data/0001.patch round-tripped');
226 ok(!$np3->get(22), 'missing message');
228 $msg = $np3->top(2, 0);
229 $msg = join('', @$msg);
231 is($msg, $patch->header_obj->as_string . "\n",
234 ok(!$np3->top(2, -1), 'negative TOP numlines');
236 $msg = $np3->top(2, 1);
237 $msg = join('', @$msg);
239 is($msg, $patch->header_obj->as_string . <<EOF,
241 Filenames within a project tend to be reasonably stable within a
245 $msg = $np3->top(2, 10000);
246 $msg = join('', @$msg);
248 is_deeply(PublicInbox::Eml->new($msg), $patch,
249 'TOP numlines=10000 (excess)');
251 $np3 = Net::POP3->new(@np3_args, %o);
252 ok($np3->starttls, 'STLS works before APOP');
253 ok($np3->apop($u, 'anonymous'), "APOP UUID\@$mailbox w/ STLS");
256 ok($np3->_NOOP, 'NOOP works') if $np3->can('_NOOP');
260 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
261 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
262 my $x = getsockopt($pop3s, IPPROTO_TCP, $var) //
263 xbail "IPPROTO_TCP: $!";
264 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on POP3S');
265 $x = getsockopt($stls, IPPROTO_TCP, $var) //
266 xbail "IPPROTO_TCP: $!";
267 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain POP3');
270 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
271 system('kldstat -m accf_data >/dev/null') and
272 skip 'accf_data not loaded? kldload accf_data', 2;
273 require PublicInbox::Daemon;
274 my $x = getsockopt($pop3s, SOL_SOCKET,
275 $PublicInbox::Daemon::SO_ACCEPTFILTER);
276 like($x, qr/\Adataready\0+\z/, 'got dataready accf for pop3s');
277 $x = getsockopt($stls, IPPROTO_TCP,
278 $PublicInbox::Daemon::SO_ACCEPTFILTER);
279 is($x, undef, 'no BSD accept filter for plain IMAP');
284 is($?, 0, 'no error in exited -netd');
285 open my $fh, '<', $err or BAIL_OUT "open $err failed: $!";
286 my $eout = do { local $/; <$fh> };
287 unlike($eout, qr/wide/i, 'no Wide character warnings in -netd');
291 my $capa = $oldc->capa;
292 ok(defined($capa->{PIPELINING}), 'pipelining supported by CAPA');
293 is($capa->{EXPIRE}, 0, 'EXPIRE 0 set');
294 ok(!exists $capa->{STLS}, 'STLS unset w/o daemon certs');
296 # ensure TOP doesn't trigger "EXPIRE 0" like RETR does (cf. RFC2449)
297 my $list = $oldc->list;
298 ok(scalar keys %$list, 'got a listing of messages');
299 ok($oldc->top($_, 1), "TOP $_ 1") for keys %$list;
300 ok($oldc->quit, 'QUIT after TOP');
302 # clients which see "EXPIRE 0" can elide DELE requests
303 $oldc = Net::POP3->new(@old_args);
304 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP for RETR');
305 is_deeply($oldc->capa, $capa, 'CAPA unchanged');
306 is_deeply($oldc->list, $list, 'LIST unchanged by previous TOP');
307 ok($oldc->get($_), "RETR $_") for keys %$list;
308 ok($oldc->quit, 'QUIT after RETR');
310 $oldc = Net::POP3->new(@old_args);
311 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP reconnect');
312 my $cont = $oldc->list;
313 is_deeply($cont, {}, 'no messages after implicit DELE from EXPIRE 0');
314 ok($oldc->quit, 'QUIT on noop');
316 # test w/o checking CAPA to trigger EXPIRE 0
317 $oldc = Net::POP3->new(@old_args);
318 ok($oldc->apop($locked_mb, 'anonymous'), 'APOP on latest slice');
319 my $l2 = $oldc->list;
320 is_deeply($l2, $list, 'different mailbox, different deletes');
321 ok($oldc->get($_), "RETR $_") for keys %$list;
322 ok($oldc->quit, 'QUIT w/o EXPIRE nor DELE');
324 $oldc = Net::POP3->new(@old_args);
325 ok($oldc->apop($locked_mb, 'anonymous'), 'APOP again on latest');
327 is_deeply($l2, $list, 'no DELE nor EXPIRE preserves messages');
328 ok($oldc->delete(2), 'explicit DELE on latest');
329 ok($oldc->quit, 'QUIT w/ highest DELE');
331 # this is non-standard behavior, but necessary if we expect hundreds
332 # of thousands of users on cheap HW
333 $oldc = Net::POP3->new(@old_args);
334 ok($oldc->apop($locked_mb, 'anonymous'), 'APOP yet again on latest');
335 is_deeply($oldc->list, {}, 'highest DELE deletes older messages, too');
338 # TODO: more tests, but mpop was really helpful in helping me
339 # figure out bugs with larger newsgroups (>50K messages) which
340 # probably isn't suited for this test suite.
344 is($?, 0, 'no error in exited -pop3d');
345 open $fh, '<', $olderr or BAIL_OUT "open $olderr failed: $!";
346 my $eout = do { local $/; <$fh> };
347 unlike($eout, qr/wide/i, 'no Wide character warnings in -pop3d');