]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd-tls.t
ds|nntp: use CORE::close on socket
[public-inbox.git] / t / nntpd-tls.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use File::Temp qw(tempdir);
7 use Socket qw(SOCK_STREAM);
8 foreach my $mod (qw(DBD::SQLite IO::Socket::SSL Net::NNTP)) {
9         eval "require $mod";
10         plan skip_all => "$mod missing for $0" if $@;
11 }
12 my $cert = 'certs/server-cert.pem';
13 my $key = 'certs/server-key.pem';
14 unless (-r $key && -r $cert) {
15         plan skip_all =>
16                 "certs/ missing for $0, run ./create-certs.perl in certs/";
17 }
18
19 use_ok 'PublicInbox::TLS';
20 use_ok 'IO::Socket::SSL';
21 require './t/common.perl';
22 require PublicInbox::InboxWritable;
23 require PublicInbox::MIME;
24 require PublicInbox::SearchIdx;
25 my $version = 2; # v2 needs newer git
26 require_git('2.6') if $version >= 2;
27 my $tmpdir = tempdir('pi-nntpd-tls-XXXXXX', TMPDIR => 1, CLEANUP => 1);
28 my $err = "$tmpdir/stderr.log";
29 my $out = "$tmpdir/stdout.log";
30 my $mainrepo = "$tmpdir";
31 my $pi_config = "$tmpdir/pi_config";
32 my $group = 'test-nntpd-tls';
33 my $addr = $group . '@example.com';
34 my $nntpd = 'blib/script/public-inbox-nntpd';
35 my %opts = (
36         LocalAddr => '127.0.0.1',
37         ReuseAddr => 1,
38         Proto => 'tcp',
39         Type => SOCK_STREAM,
40         Listen => 1024,
41 );
42 my $starttls = IO::Socket::INET->new(%opts);
43 my $nntps = IO::Socket::INET->new(%opts);
44 my ($pid, $tail_pid);
45 END {
46         foreach ($pid, $tail_pid) {
47                 kill 'TERM', $_ if defined $_;
48         }
49 };
50
51 my $ibx = PublicInbox::Inbox->new({
52         mainrepo => $mainrepo,
53         name => 'nntpd-tls',
54         version => $version,
55         -primary_address => $addr,
56         indexlevel => 'basic',
57 });
58 $ibx = PublicInbox::InboxWritable->new($ibx, {nproc=>1});
59 $ibx->init_inbox(0);
60 {
61         open my $fh, '>', $pi_config or die "open: $!\n";
62         print $fh <<EOF
63 [publicinbox "nntpd-tls"]
64         mainrepo = $mainrepo
65         address = $addr
66         indexlevel = basic
67         newsgroup = $group
68 EOF
69         ;
70         close $fh or die "close: $!\n";
71 }
72
73 {
74         my $im = $ibx->importer(0);
75         my $mime = PublicInbox::MIME->new(do {
76                 open my $fh, '<', 't/data/0001.patch' or die;
77                 local $/;
78                 <$fh>
79         });
80         ok($im->add($mime), 'message added');
81         $im->done;
82         if ($version == 1) {
83                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
84                 $s->index_sync;
85         }
86 }
87
88 my $nntps_addr = $nntps->sockhost . ':' . $nntps->sockport;
89 my $starttls_addr = $starttls->sockhost . ':' . $starttls->sockport;
90 my $env = { PI_CONFIG => $pi_config };
91
92 for my $args (
93         [ "--cert=$cert", "--key=$key",
94                 "-lnntps://$nntps_addr",
95                 "-lnntp://$starttls_addr" ],
96 ) {
97         for ($out, $err) {
98                 open my $fh, '>', $_ or die "truncate: $!";
99         }
100         if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
101                 $tail_pid = fork;
102                 if (defined $tail_pid && $tail_pid == 0) {
103                         exec(split(' ', $tail_cmd), $out, $err);
104                 }
105         }
106         my $cmd = [ $nntpd, '-W0', @$args, "--stdout=$out", "--stderr=$err" ];
107         $pid = spawn_listener($env, $cmd, [ $starttls, $nntps ]);
108         my %o = (
109                 SSL_hostname => 'server.local',
110                 SSL_verifycn_name => 'server.local',
111                 SSL => 1,
112                 SSL_verify_mode => SSL_VERIFY_PEER(),
113                 SSL_ca_file => 'certs/test-ca.pem',
114         );
115         my $expect = { $group => [qw(1 1 n)] };
116
117         # NNTPS
118         my $c = Net::NNTP->new($nntps_addr, %o);
119         my $list = $c->list;
120         is_deeply($list, $expect, 'NNTPS LIST works');
121
122         # STARTTLS
123         delete $o{SSL};
124         $c = Net::NNTP->new($starttls_addr, %o);
125         $list = $c->list;
126         is_deeply($list, $expect, 'plain LIST works');
127         ok($c->starttls, 'STARTTLS succeeds');
128         is($c->code, 382, 'got 382 for STARTTLS');
129         $list = $c->list;
130         is_deeply($list, $expect, 'LIST works after STARTTLS');
131
132         # Net::NNTP won't let us do dumb things, but we need to test
133         # dumb things, so use Net::Cmd directly:
134         my $n = $c->command('STARTTLS')->response();
135         is($n, Net::Cmd::CMD_ERROR(), 'error attempting STARTTLS again');
136         is($c->code, 502, '502 according to RFC 4642 sec#2.2.1');
137
138         # STARTTLS with bad hostname
139         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
140         $c = Net::NNTP->new($starttls_addr, %o);
141         $list = $c->list;
142         is_deeply($list, $expect, 'plain LIST works again');
143         ok(!$c->starttls, 'STARTTLS fails with bad hostname');
144         $c = Net::NNTP->new($starttls_addr, %o);
145         $list = $c->list;
146         is_deeply($list, $expect, 'not broken after bad negotiation');
147
148         # NNTPS with bad hostname
149         $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
150         is($c, undef, 'NNTPS fails with bad hostname');
151         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
152         $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
153         ok($c, 'NNTPS succeeds again with valid hostname');
154
155         $c = undef;
156         kill('TERM', $pid);
157         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
158         is($?, 0, 'no error in exited process');
159         $pid = undef;
160         my $eout = eval {
161                 open my $fh, '<', $err or die "open $err failed: $!";
162                 local $/;
163                 <$fh>;
164         };
165         unlike($eout, qr/wide/i, 'no Wide character warnings');
166         if (defined $tail_pid) {
167                 kill 'TERM', $tail_pid;
168                 waitpid($tail_pid, 0);
169                 $tail_pid = undef;
170         }
171 }
172 done_testing();
173 1;