]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-https.t
Merge remote-tracking branch 'origin/email-simple-mem' into master
[public-inbox.git] / t / httpd-https.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 IPPROTO_TCP SOL_SOCKET);
8 # IO::Poll is part of the standard library, but distros may split them off...
9 foreach my $mod (qw(IO::Socket::SSL IO::Poll)) {
10         eval "require $mod";
11         plan skip_all => "$mod missing for $0" if $@;
12 }
13 my $cert = 'certs/server-cert.pem';
14 my $key = 'certs/server-key.pem';
15 unless (-r $key && -r $cert) {
16         plan skip_all =>
17                 "certs/ missing for $0, run ./create-certs.perl in certs/";
18 }
19 use_ok 'PublicInbox::TLS';
20 use_ok 'IO::Socket::SSL';
21 require './t/common.perl';
22 my $psgi = "./t/httpd-corner.psgi";
23 my $tmpdir = tempdir('pi-httpd-https-XXXXXX', TMPDIR => 1, CLEANUP => 1);
24 my $err = "$tmpdir/stderr.log";
25 my $out = "$tmpdir/stdout.log";
26 my $httpd = 'blib/script/public-inbox-httpd';
27 my %opts = (
28         LocalAddr => '127.0.0.1',
29         ReuseAddr => 1,
30         Proto => 'tcp',
31         Type => SOCK_STREAM,
32         Listen => 1024,
33 );
34 my $https = IO::Socket::INET->new(%opts);
35 my ($pid, $tail_pid);
36 END {
37         foreach ($pid, $tail_pid) {
38                 kill 'TERM', $_ if defined $_;
39         }
40 };
41 my $https_addr = $https->sockhost . ':' . $https->sockport;
42 my %opt = ( Proto => 'tcp', PeerAddr => $https_addr, Type => SOCK_STREAM );
43
44 for my $args (
45         [ "-lhttps://$https_addr/?key=$key,cert=$cert" ],
46 ) {
47         for ($out, $err) {
48                 open my $fh, '>', $_ or die "truncate: $!";
49         }
50         if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
51                 $tail_pid = fork;
52                 if (defined $tail_pid && $tail_pid == 0) {
53                         exec(split(' ', $tail_cmd), $out, $err);
54                 }
55         }
56         my $cmd = [ $httpd, '-W0', @$args,
57                         "--stdout=$out", "--stderr=$err", $psgi ];
58         $pid = spawn_listener(undef, $cmd, [ $https ]);
59         my %o = (
60                 SSL_hostname => 'server.local',
61                 SSL_verifycn_name => 'server.local',
62                 SSL_verify_mode => SSL_VERIFY_PEER(),
63                 SSL_ca_file => 'certs/test-ca.pem',
64         );
65         # start negotiating a slow TLS connection
66         my $slow = IO::Socket::INET->new(%opt, Blocking => 0);
67         $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
68         my @poll = (fileno($slow));
69         my $slow_done = $slow->connect_SSL;
70         if ($slow_done) {
71                 diag('W: connect_SSL early OK, slow client test invalid');
72                 push @poll, PublicInbox::Syscall::EPOLLOUT();
73         } else {
74                 push @poll, PublicInbox::TLS::epollbit();
75         }
76
77         # normal HTTPS
78         my $c = IO::Socket::INET->new(%opt);
79         IO::Socket::SSL->start_SSL($c, %o);
80         ok($c->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
81                 'wrote HTTP request');
82         my $buf = '';
83         sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/;
84         like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response');
85
86         # HTTPS with bad hostname
87         $c = IO::Socket::INET->new(%opt);
88         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.fail';
89         $c = IO::Socket::SSL->start_SSL($c, %o);
90         is($c, undef, 'HTTPS fails with bad hostname');
91
92         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
93         $c = IO::Socket::INET->new(%opt);
94         IO::Socket::SSL->start_SSL($c, %o);
95         ok($c, 'HTTPS succeeds again with valid hostname');
96
97         # slow TLS connection did not block the other fast clients while
98         # connecting, finish it off:
99         until ($slow_done) {
100                 IO::Poll::_poll(-1, @poll);
101                 $slow_done = $slow->connect_SSL and last;
102                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
103         }
104         $slow->blocking(1);
105         ok($slow->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
106                 'wrote HTTP request from slow');
107         $buf = '';
108         sysread($slow, $buf, 666, length($buf)) until $buf =~ /\r\n\r\n/;
109         like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response from slow');
110         $slow = undef;
111
112         SKIP: {
113                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
114                 my $var = Socket::TCP_DEFER_ACCEPT();
115                 defined(my $x = getsockopt($https, IPPROTO_TCP, $var)) or die;
116                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on https');
117         };
118         SKIP: {
119                 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
120                 if (system('kldstat -m accf_data >/dev/null')) {
121                         skip 'accf_data not loaded? kldload accf_data', 2;
122                 }
123                 require PublicInbox::Daemon;
124                 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
125                 my $x = getsockopt($https, SOL_SOCKET, $var);
126                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for https');
127         };
128
129         $c = undef;
130         kill('TERM', $pid);
131         is($pid, waitpid($pid, 0), 'httpd exited successfully');
132         is($?, 0, 'no error in exited process');
133         $pid = undef;
134         if (defined $tail_pid) {
135                 kill 'TERM', $tail_pid;
136                 waitpid($tail_pid, 0);
137                 $tail_pid = undef;
138         }
139 }
140 done_testing();
141 1;