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 :fcntl_lock));
16 require_git(v2.6); # for v2
17 use_ok 'IO::Socket::SSL';
18 use_ok 'PublicInbox::TLS';
19 my ($tmpdir, $for_destroy) = tmpdir();
20 mkdir("$tmpdir/p3state") or xbail "mkdir: $!";
21 my $err = "$tmpdir/stderr.log";
22 my $out = "$tmpdir/stdout.log";
23 my $olderr = "$tmpdir/plain.err";
24 my $group = 'test-pop3';
25 my $addr = $group . '@example.com';
26 my $stls = tcp_server();
27 my $plain = tcp_server();
28 my $pop3s = tcp_server();
29 my $patch = eml_load('t/data/0001.patch');
30 my $ibx = create_inbox 'pop3d', version => 2, -primary_address => $addr,
31 indexlevel => 'basic', sub {
33 $im->add(eml_load('t/plack-qp.eml')) or BAIL_OUT '->add';
34 $im->add($patch) or BAIL_OUT '->add';
36 my $pi_config = "$tmpdir/pi_config";
37 open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
38 print $fh <<EOF or BAIL_OUT "print: $!";
40 pop3state = $tmpdir/p3state
42 inboxdir = $ibx->{inboxdir}
47 close $fh or BAIL_OUT "close: $!\n";
49 my $pop3s_addr = tcp_host_port($pop3s);
50 my $stls_addr = tcp_host_port($stls);
51 my $plain_addr = tcp_host_port($plain);
52 my $env = { PI_CONFIG => $pi_config };
53 my $old = start_script(['-pop3d', '-W0',
54 "--stdout=$tmpdir/plain.out", "--stderr=$olderr" ],
55 $env, { 3 => $plain });
56 my @old_args = ($plain->sockhost, Port => $plain->sockport);
57 my $oldc = Net::POP3->new(@old_args);
58 my $locked_mb = ('e'x32)."\@$group";
59 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP to old');
61 my $dbh = DBI->connect("dbi:SQLite:dbname=$tmpdir/p3state/db.sqlite3",'','', {
65 sqlite_use_immediate_transaction => 1,
66 sqlite_see_if_its_a_number => 1,
69 { # locking within the same process
70 my $x = Net::POP3->new(@old_args);
71 ok(!$x->apop("$locked_mb.0", 'anonymous'), 'APOP lock failure');
72 like($x->message, qr/unable to lock/, 'diagnostic message');
74 $x = Net::POP3->new(@old_args);
75 ok($x->apop($locked_mb, 'anonymous'), 'APOP lock acquire');
77 my $y = Net::POP3->new(@old_args);
78 ok(!$y->apop($locked_mb, 'anonymous'), 'APOP lock fails once');
81 $y = Net::POP3->new(@old_args);
82 ok($y->apop($locked_mb, 'anonymous'), 'APOP lock works after release');
86 [ "--cert=$cert", "--key=$key",
87 "-lpop3s://$pop3s_addr",
88 "-lpop3://$stls_addr" ],
90 for ($out, $err) { open my $fh, '>', $_ or BAIL_OUT "truncate: $!" }
91 my $cmd = [ '-netd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
92 my $td = start_script($cmd, $env, { 3 => $stls, 4 => $pop3s });
95 SSL_hostname => 'server.local',
96 SSL_verifycn_name => 'server.local',
97 SSL_verify_mode => SSL_VERIFY_PEER(),
98 SSL_ca_file => 'certs/test-ca.pem',
100 # start negotiating a slow TLS connection
101 my $slow = tcp_connect($pop3s, Blocking => 0);
102 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
103 my $slow_done = $slow->connect_SSL;
106 diag('W: connect_SSL early OK, slow client test invalid');
107 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
108 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
110 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
113 my @p3s_args = ($pop3s->sockhost,
114 Port => $pop3s->sockport, SSL => 1, %o);
115 my $p3s = Net::POP3->new(@p3s_args);
116 my $capa = $p3s->capa;
117 ok(!exists $capa->{STLS}, 'no STLS CAPA for POP3S');
118 ok($p3s->quit, 'QUIT works w/POP3S');
120 $p3s = Net::POP3->new(@p3s_args);
121 ok(!$p3s->apop("$locked_mb.0", 'anonymous'),
122 'APOP lock failure w/ another daemon');
123 like($p3s->message, qr/unable to lock/, 'diagnostic message');
126 # slow TLS connection did not block the other fast clients while
127 # connecting, finish it off:
129 IO::Poll::_poll(-1, @poll);
130 $slow_done = $slow->connect_SSL and last;
131 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
134 ok(sysread($slow, my $greet, 4096) > 0, 'slow got a greeting');
135 my @np3_args = ($stls->sockhost, Port => $stls->sockport);
136 my $np3 = Net::POP3->new(@np3_args);
137 ok($np3->quit, 'plain QUIT works');
138 $np3 = Net::POP3->new(@np3_args, %o);
140 ok(exists $capa->{STLS}, 'STLS CAPA advertised before STLS');
141 ok($np3->starttls, 'STLS works');
143 ok(!exists $capa->{STLS}, 'STLS CAPA not advertised after STLS');
144 ok($np3->quit, 'QUIT works after STLS');
146 for my $mailbox (('x'x32)."\@$group", $group, ('a'x32)."\@z.$group") {
147 $np3 = Net::POP3->new(@np3_args);
148 ok(!$np3->user($mailbox), "USER $mailbox reject");
149 ok($np3->quit, 'QUIT after USER fail');
151 $np3 = Net::POP3->new(@np3_args);
152 ok(!$np3->apop($mailbox, 'anonymous'), "APOP $mailbox reject");
153 ok($np3->quit, "QUIT after APOP fail $mailbox");
156 # we do connect+QUIT bumps to try ensuring non-QUIT disconnects
157 # get processed below:
158 for my $mailbox ($group, "$group.0") {
159 my $u = ('f'x32)."\@$mailbox";
161 ok(Net::POP3->new(@np3_args)->quit, 'connect+QUIT bump');
162 $np3 = Net::POP3->new(@np3_args);
163 my $n0 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
164 my $u0 = $dbh->selectrow_array('SELECT COUNT(*) FROM users');
165 ok($np3->user($u), "UUID\@$mailbox accept");
166 ok($np3->pass('anonymous'), 'pass works');
167 my $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
168 is($n1 - $n0, 1, 'deletes bumped while connected');
169 ok($np3->quit, 'client QUIT');
171 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
172 is($n1, $n0, 'deletes row gone on no-op after QUIT');
173 my $u1 = $dbh->selectrow_array('SELECT COUNT(*) FROM users');
174 is($u1, $u0, 'users row gone on no-op after QUIT');
176 $np3 = Net::POP3->new(@np3_args);
177 ok($np3->user($u), "UUID\@$mailbox accept");
178 ok($np3->pass('anonymous'), 'pass works');
180 my $list = $np3->list;
181 my $uidl = $np3->uidl;
182 is_deeply([sort keys %$list], [sort keys %$uidl],
183 'LIST and UIDL keys match');
184 ok($_ > 0, 'bytes in LIST result') for values %$list;
185 like($_, qr/\A[a-z0-9]{40,}\z/,
186 'blob IDs in UIDL result') for values %$uidl;
187 ok($np3->quit, 'QUIT after LIST+UIDL');
188 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
189 is($n1, $n0, 'deletes row gone on no-op after LIST+UIDL');
192 $np3 = Net::POP3->new(@np3_args);
193 ok($np3->user($u), "UUID\@$mailbox accept");
194 ok($np3->pass('anonymous'), 'pass works');
195 undef $np3; # QUIT-less disconnect
196 ok(Net::POP3->new(@np3_args)->quit, 'connect+QUIT bump');
198 $u1 = $dbh->selectrow_array('SELECT COUNT(*) FROM users');
199 is($u1, $u0, 'users row gone on QUIT-less disconnect');
200 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
201 is($n1, $n0, 'deletes row gone on QUIT-less disconnect');
204 $np3 = Net::POP3->new(@np3_args);
205 ok(!$np3->apop($u, 'anonumuss'), 'APOP wrong pass reject');
206 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
207 is($n1, $n0, 'deletes row not bumped w/ wrong pass');
208 undef $np3; # QUIT-less disconnect
209 ok(Net::POP3->new(@np3_args)->quit, 'connect+QUIT bump');
211 $n1 = $dbh->selectrow_array('SELECT COUNT(*) FROM deletes');
212 is($n1, $n0, 'deletes row not bumped w/ wrong pass');
214 $np3 = Net::POP3->new(@np3_args);
215 ok($np3->apop($u, 'anonymous'), "APOP UUID\@$mailbox");
216 my @res = $np3->popstat;
217 is($res[0], 2, 'STAT knows about 2 messages');
219 my $msg = $np3->get(2);
220 $msg = join('', @$msg);
222 is_deeply(PublicInbox::Eml->new($msg), $patch,
223 't/data/0001.patch round-tripped');
225 ok(!$np3->get(22), 'missing message');
227 $msg = $np3->top(2, 0);
228 $msg = join('', @$msg);
230 is($msg, $patch->header_obj->as_string . "\n",
233 ok(!$np3->top(2, -1), 'negative TOP numlines');
235 $msg = $np3->top(2, 1);
236 $msg = join('', @$msg);
238 is($msg, $patch->header_obj->as_string . <<EOF,
240 Filenames within a project tend to be reasonably stable within a
244 $msg = $np3->top(2, 10000);
245 $msg = join('', @$msg);
247 is_deeply(PublicInbox::Eml->new($msg), $patch,
248 'TOP numlines=10000 (excess)');
250 $np3 = Net::POP3->new(@np3_args, %o);
251 ok($np3->starttls, 'STLS works before APOP');
252 ok($np3->apop($u, 'anonymous'), "APOP UUID\@$mailbox w/ STLS");
255 ok($np3->_NOOP, 'NOOP works') if $np3->can('_NOOP');
259 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
260 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
261 my $x = getsockopt($pop3s, IPPROTO_TCP, $var) //
262 xbail "IPPROTO_TCP: $!";
263 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on POP3S');
264 $x = getsockopt($stls, IPPROTO_TCP, $var) //
265 xbail "IPPROTO_TCP: $!";
266 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain POP3');
269 require_mods '+accf_data';
270 require PublicInbox::Daemon;
271 my $x = getsockopt($pop3s, SOL_SOCKET,
272 $PublicInbox::Daemon::SO_ACCEPTFILTER);
273 like($x, qr/\Adataready\0+\z/, 'got dataready accf for pop3s');
274 $x = getsockopt($stls, IPPROTO_TCP,
275 $PublicInbox::Daemon::SO_ACCEPTFILTER);
276 is($x, undef, 'no BSD accept filter for plain POP3');
281 is($?, 0, 'no error in exited -netd');
282 open my $fh, '<', $err or BAIL_OUT "open $err failed: $!";
283 my $eout = do { local $/; <$fh> };
284 unlike($eout, qr/wide/i, 'no Wide character warnings in -netd');
288 my $capa = $oldc->capa;
289 ok(defined($capa->{PIPELINING}), 'pipelining supported by CAPA');
290 is($capa->{EXPIRE}, 0, 'EXPIRE 0 set');
291 ok(!exists $capa->{STLS}, 'STLS unset w/o daemon certs');
293 # ensure TOP doesn't trigger "EXPIRE 0" like RETR does (cf. RFC2449)
294 my $list = $oldc->list;
295 ok(scalar keys %$list, 'got a listing of messages');
296 ok($oldc->top($_, 1), "TOP $_ 1") for keys %$list;
297 ok($oldc->quit, 'QUIT after TOP');
299 # clients which see "EXPIRE 0" can elide DELE requests
300 $oldc = Net::POP3->new(@old_args);
301 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP for RETR');
302 is_deeply($oldc->capa, $capa, 'CAPA unchanged');
303 is_deeply($oldc->list, $list, 'LIST unchanged by previous TOP');
304 ok($oldc->get($_), "RETR $_") for keys %$list;
305 ok($oldc->quit, 'QUIT after RETR');
307 $oldc = Net::POP3->new(@old_args);
308 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP reconnect');
309 my $cont = $oldc->list;
310 is_deeply($cont, {}, 'no messages after implicit DELE from EXPIRE 0');
311 ok($oldc->quit, 'QUIT on noop');
313 # test w/o checking CAPA to trigger EXPIRE 0
314 $oldc = Net::POP3->new(@old_args);
315 ok($oldc->apop($locked_mb, 'anonymous'), 'APOP on latest slice');
316 my $l2 = $oldc->list;
317 is_deeply($l2, $list, 'different mailbox, different deletes');
318 ok($oldc->get($_), "RETR $_") for keys %$list;
319 ok($oldc->quit, 'QUIT w/o EXPIRE nor DELE');
321 $oldc = Net::POP3->new(@old_args);
322 ok($oldc->apop($locked_mb, 'anonymous'), 'APOP again on latest');
324 is_deeply($l2, $list, 'no DELE nor EXPIRE preserves messages');
325 ok($oldc->delete(2), 'explicit DELE on latest');
326 ok($oldc->quit, 'QUIT w/ highest DELE');
328 # this is non-standard behavior, but necessary if we expect hundreds
329 # of thousands of users on cheap HW
330 $oldc = Net::POP3->new(@old_args);
331 ok($oldc->apop($locked_mb, 'anonymous'), 'APOP yet again on latest');
332 is_deeply($oldc->list, {}, 'highest DELE deletes older messages, too');
335 # TODO: more tests, but mpop was really helpful in helping me
336 # figure out bugs with larger newsgroups (>50K messages) which
337 # probably isn't suited for this test suite.
341 is($?, 0, 'no error in exited -pop3d');
342 open $fh, '<', $olderr or BAIL_OUT "open $olderr failed: $!";
343 my $eout = do { local $/; <$fh> };
344 unlike($eout, qr/wide/i, 'no Wide character warnings in -pop3d');