]> Sergey Matveev's repositories - public-inbox.git/blob - t/pop3d.t
No ext_urls
[public-inbox.git] / t / pop3d.t
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 use Socket qw(IPPROTO_TCP SOL_SOCKET);
7 # Net::POP3 is part of the standard library, but distros may split it off...
8 require_mods(qw(DBD::SQLite Net::POP3 IO::Socket::SSL));
9 require_git('2.6'); # for v2
10 require_mods(qw(File::FcntlLock)) if $^O !~ /\A(?:linux|freebsd)\z/;
11 use_ok 'IO::Socket::SSL';
12 use_ok 'PublicInbox::TLS';
13 my ($tmpdir, $for_destroy) = tmpdir();
14 mkdir("$tmpdir/p3state") or xbail "mkdir: $!";
15 my $err = "$tmpdir/stderr.log";
16 my $out = "$tmpdir/stdout.log";
17 my $olderr = "$tmpdir/plain.err";
18 my $group = 'test-pop3';
19 my $addr = $group . '@example.com';
20 my $stls = tcp_server();
21 my $plain = tcp_server();
22 my $pop3s = tcp_server();
23 my $patch = eml_load('t/data/0001.patch');
24 my $ibx = create_inbox 'pop3d', version => 2, -primary_address => $addr,
25                         indexlevel => 'basic', sub {
26         my ($im, $ibx) = @_;
27         $im->add(eml_load('t/plack-qp.eml')) or BAIL_OUT '->add';
28         $im->add($patch) or BAIL_OUT '->add';
29 };
30 my $pi_config = "$tmpdir/pi_config";
31 open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
32 print $fh <<EOF or BAIL_OUT "print: $!";
33 [publicinbox]
34         pop3state = $tmpdir/p3state
35 [publicinbox "pop3"]
36         inboxdir = $ibx->{inboxdir}
37         address = $addr
38         indexlevel = basic
39         newsgroup = $group
40 EOF
41 close $fh or BAIL_OUT "close: $!\n";
42
43 my $pop3s_addr = tcp_host_port($pop3s);
44 my $stls_addr = tcp_host_port($stls);
45 my $plain_addr = tcp_host_port($plain);
46 my $env = { PI_CONFIG => $pi_config };
47 my $cert = 'certs/server-cert.pem';
48 my $key = 'certs/server-key.pem';
49
50 unless (-r $key && -r $cert) {
51         plan skip_all =>
52                 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
53 }
54
55 my $old = start_script(['-pop3d', '-W0',
56         "--stdout=$tmpdir/plain.out", "--stderr=$olderr" ],
57         $env, { 3 => $plain });
58 my @old_args = ($plain->sockhost, Port => $plain->sockport);
59 my $oldc = Net::POP3->new(@old_args);
60 my $locked_mb = ('e'x32)."\@$group";
61 ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP to old');
62
63 { # locking within the same process
64         my $x = Net::POP3->new(@old_args);
65         ok(!$x->apop("$locked_mb.0", 'anonymous'), 'APOP lock failure');
66         like($x->message, qr/unable to lock/, 'diagnostic message');
67
68         $x = Net::POP3->new(@old_args);
69         ok($x->apop($locked_mb, 'anonymous'), 'APOP lock acquire');
70
71         my $y = Net::POP3->new(@old_args);
72         ok(!$y->apop($locked_mb, 'anonymous'), 'APOP lock fails once');
73
74         undef $x;
75         $y = Net::POP3->new(@old_args);
76         ok($y->apop($locked_mb, 'anonymous'), 'APOP lock works after release');
77 }
78
79 for my $args (
80         [ "--cert=$cert", "--key=$key",
81                 "-lpop3s://$pop3s_addr",
82                 "-lpop3://$stls_addr" ],
83 ) {
84         for ($out, $err) { open my $fh, '>', $_ or BAIL_OUT "truncate: $!" }
85         my $cmd = [ '-netd', '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
86         my $td = start_script($cmd, $env, { 3 => $stls, 4 => $pop3s });
87
88         my %o = (
89                 SSL_hostname => 'server.local',
90                 SSL_verifycn_name => 'server.local',
91                 SSL_verify_mode => SSL_VERIFY_PEER(),
92                 SSL_ca_file => 'certs/test-ca.pem',
93         );
94         # start negotiating a slow TLS connection
95         my $slow = tcp_connect($pop3s, Blocking => 0);
96         $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
97         my $slow_done = $slow->connect_SSL;
98         my @poll;
99         if ($slow_done) {
100                 diag('W: connect_SSL early OK, slow client test invalid');
101                 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT);
102                 @poll = (fileno($slow), EPOLLIN | EPOLLOUT);
103         } else {
104                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
105         }
106
107         my @p3s_args = ($pop3s->sockhost,
108                         Port => $pop3s->sockport, SSL => 1, %o);
109         my $p3s = Net::POP3->new(@p3s_args);
110         my $capa = $p3s->capa;
111         ok(!exists $capa->{STLS}, 'no STLS CAPA for POP3S');
112         ok($p3s->quit, 'QUIT works w/POP3S');
113         {
114                 $p3s = Net::POP3->new(@p3s_args);
115                 ok(!$p3s->apop("$locked_mb.0", 'anonymous'),
116                         'APOP lock failure w/ another daemon');
117                 like($p3s->message, qr/unable to lock/, 'diagnostic message');
118         }
119
120         # slow TLS connection did not block the other fast clients while
121         # connecting, finish it off:
122         until ($slow_done) {
123                 IO::Poll::_poll(-1, @poll);
124                 $slow_done = $slow->connect_SSL and last;
125                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
126         }
127         $slow->blocking(1);
128         ok(sysread($slow, my $greet, 4096) > 0, 'slow got a greeting');
129         my @np3_args = ($stls->sockhost, Port => $stls->sockport);
130         my $np3 = Net::POP3->new(@np3_args);
131         ok($np3->quit, 'plain QUIT works');
132         $np3 = Net::POP3->new(@np3_args, %o);
133         $capa = $np3->capa;
134         ok(exists $capa->{STLS}, 'STLS CAPA advertised before STLS');
135         ok($np3->starttls, 'STLS works');
136         $capa = $np3->capa;
137         ok(!exists $capa->{STLS}, 'STLS CAPA not advertised after STLS');
138         ok($np3->quit, 'QUIT works after STLS');
139
140         for my $mailbox (('x'x32)."\@$group", $group, ('a'x32)."\@z.$group") {
141                 $np3 = Net::POP3->new(@np3_args);
142                 ok(!$np3->user($mailbox), "USER $mailbox reject");
143                 ok($np3->quit, 'QUIT after USER fail');
144
145                 $np3 = Net::POP3->new(@np3_args);
146                 ok(!$np3->apop($mailbox, 'anonymous'), "APOP $mailbox reject");
147                 ok($np3->quit, "QUIT after APOP fail $mailbox");
148         }
149         for my $mailbox ($group, "$group.0") {
150                 my $u = ('f'x32)."\@$mailbox";
151                 $np3 = Net::POP3->new(@np3_args);
152                 ok($np3->user($u), "UUID\@$mailbox accept");
153                 ok($np3->pass('anonymous'), 'pass works');
154
155                 $np3 = Net::POP3->new(@np3_args);
156                 ok($np3->user($u), "UUID\@$mailbox accept");
157                 ok($np3->pass('anonymous'), 'pass works');
158
159                 my $list = $np3->list;
160                 my $uidl = $np3->uidl;
161                 is_deeply([sort keys %$list], [sort keys %$uidl],
162                         'LIST and UIDL keys match');
163                 ok($_ > 0, 'bytes in LIST result') for values %$list;
164                 like($_, qr/\A[a-z0-9]{40,}\z/,
165                         'blob IDs in UIDL result') for values %$uidl;
166
167                 $np3 = Net::POP3->new(@np3_args);
168                 ok(!$np3->apop($u, 'anonumuss'), 'APOP wrong pass reject');
169
170                 $np3 = Net::POP3->new(@np3_args);
171                 ok($np3->apop($u, 'anonymous'), "APOP UUID\@$mailbox");
172                 my @res = $np3->popstat;
173                 is($res[0], 2, 'STAT knows about 2 messages');
174
175                 my $msg = $np3->get(2);
176                 $msg = join('', @$msg);
177                 $msg =~ s/\r\n/\n/g;
178                 is_deeply(PublicInbox::Eml->new($msg), $patch,
179                         't/data/0001.patch round-tripped');
180
181                 ok(!$np3->get(22), 'missing message');
182
183                 $msg = $np3->top(2, 0);
184                 $msg = join('', @$msg);
185                 $msg =~ s/\r\n/\n/g;
186                 is($msg, $patch->header_obj->as_string . "\n",
187                         'TOP numlines=0');
188
189                 ok(!$np3->top(2, -1), 'negative TOP numlines');
190
191                 $msg = $np3->top(2, 1);
192                 $msg = join('', @$msg);
193                 $msg =~ s/\r\n/\n/g;
194                 is($msg, $patch->header_obj->as_string . <<EOF,
195
196 Filenames within a project tend to be reasonably stable within a
197 EOF
198                         'TOP numlines=1');
199
200                 $msg = $np3->top(2, 10000);
201                 $msg = join('', @$msg);
202                 $msg =~ s/\r\n/\n/g;
203                 is_deeply(PublicInbox::Eml->new($msg), $patch,
204                         'TOP numlines=10000 (excess)');
205
206                 $np3 = Net::POP3->new(@np3_args, %o);
207                 ok($np3->starttls, 'STLS works before APOP');
208                 ok($np3->apop($u, 'anonymous'), "APOP UUID\@$mailbox w/ STLS");
209
210                 # undocumented:
211                 ok($np3->_NOOP, 'NOOP works') if $np3->can('_NOOP');
212         }
213
214         SKIP: {
215                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
216                 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
217                 my $x = getsockopt($pop3s, IPPROTO_TCP, $var) //
218                         xbail "IPPROTO_TCP: $!";
219                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on POP3S');
220                 $x = getsockopt($stls, IPPROTO_TCP, $var) //
221                         xbail "IPPROTO_TCP: $!";
222                 is(unpack('i', $x), 0, 'TCP_DEFER_ACCEPT is 0 on plain POP3');
223         };
224         SKIP: {
225                 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
226                 system('kldstat -m accf_data >/dev/null') and
227                         skip 'accf_data not loaded? kldload accf_data', 2;
228                 require PublicInbox::Daemon;
229                 my $x = getsockopt($pop3s, SOL_SOCKET,
230                                 $PublicInbox::Daemon::SO_ACCEPTFILTER);
231                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for pop3s');
232                 $x = getsockopt($stls, IPPROTO_TCP,
233                                 $PublicInbox::Daemon::SO_ACCEPTFILTER);
234                 is($x, undef, 'no BSD accept filter for plain IMAP');
235         };
236
237         $td->kill;
238         $td->join;
239         is($?, 0, 'no error in exited -netd');
240         open my $fh, '<', $err or BAIL_OUT "open $err failed: $!";
241         my $eout = do { local $/; <$fh> };
242         unlike($eout, qr/wide/i, 'no Wide character warnings in -netd');
243 }
244
245 {
246         my $capa = $oldc->capa;
247         ok(defined($capa->{PIPELINING}), 'pipelining supported by CAPA');
248         is($capa->{EXPIRE}, 0, 'EXPIRE 0 set');
249         ok(!exists $capa->{STLS}, 'STLS unset w/o daemon certs');
250
251         # ensure TOP doesn't trigger "EXPIRE 0" like RETR does (cf. RFC2449)
252         my $list = $oldc->list;
253         ok(scalar keys %$list, 'got a listing of messages');
254         ok($oldc->top($_, 1), "TOP $_ 1") for keys %$list;
255         ok($oldc->quit, 'QUIT after TOP');
256
257         # clients which see "EXPIRE 0" can elide DELE requests
258         $oldc = Net::POP3->new(@old_args);
259         ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP for RETR');
260         is_deeply($oldc->capa, $capa, 'CAPA unchanged');
261         is_deeply($oldc->list, $list, 'LIST unchanged by previous TOP');
262         ok($oldc->get($_), "RETR $_") for keys %$list;
263         ok($oldc->quit, 'QUIT after RETR');
264
265         $oldc = Net::POP3->new(@old_args);
266         ok($oldc->apop("$locked_mb.0", 'anonymous'), 'APOP reconnect');
267         my $cont = $oldc->list;
268         is_deeply($cont, {}, 'no messages after implicit DELE from EXPIRE 0');
269         ok($oldc->quit, 'QUIT on noop');
270
271         # test w/o checking CAPA to trigger EXPIRE 0
272         $oldc = Net::POP3->new(@old_args);
273         ok($oldc->apop($locked_mb, 'anonymous'), 'APOP on latest slice');
274         my $l2 = $oldc->list;
275         is_deeply($l2, $list, 'different mailbox, different deletes');
276         ok($oldc->get($_), "RETR $_") for keys %$list;
277         ok($oldc->quit, 'QUIT w/o EXPIRE nor DELE');
278
279         $oldc = Net::POP3->new(@old_args);
280         ok($oldc->apop($locked_mb, 'anonymous'), 'APOP again on latest');
281         $l2 = $oldc->list;
282         is_deeply($l2, $list, 'no DELE nor EXPIRE preserves messages');
283         ok($oldc->delete(2), 'explicit DELE on latest');
284         ok($oldc->quit, 'QUIT w/ highest DELE');
285
286         # this is non-standard behavior, but necessary if we expect hundreds
287         # of thousands of users on cheap HW
288         $oldc = Net::POP3->new(@old_args);
289         ok($oldc->apop($locked_mb, 'anonymous'), 'APOP yet again on latest');
290         is_deeply($oldc->list, {}, 'highest DELE deletes older messages, too');
291 }
292
293 # TODO: more tests, but mpop was really helpful in helping me
294 # figure out bugs with larger newsgroups (>50K messages) which
295 # probably isn't suited for this test suite.
296
297 $old->kill;
298 $old->join;
299 is($?, 0, 'no error in exited -pop3d');
300 open $fh, '<', $olderr or BAIL_OUT "open $olderr failed: $!";
301 my $eout = do { local $/; <$fh> };
302 unlike($eout, qr/wide/i, 'no Wide character warnings in -pop3d');
303
304 done_testing;