]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-https.t
tests: recommend running create-certs.pl with $^X
[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 $^X ./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 $https = tcp_server();
28 my ($pid, $tail_pid);
29 END {
30         foreach ($pid, $tail_pid) {
31                 kill 'TERM', $_ if defined $_;
32         }
33 };
34 my $https_addr = $https->sockhost . ':' . $https->sockport;
35
36 for my $args (
37         [ "-lhttps://$https_addr/?key=$key,cert=$cert" ],
38 ) {
39         for ($out, $err) {
40                 open my $fh, '>', $_ or die "truncate: $!";
41         }
42         if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
43                 $tail_pid = fork;
44                 if (defined $tail_pid && $tail_pid == 0) {
45                         exec(split(' ', $tail_cmd), $out, $err);
46                 }
47         }
48         my $cmd = [ $httpd, '-W0', @$args,
49                         "--stdout=$out", "--stderr=$err", $psgi ];
50         $pid = spawn_listener(undef, $cmd, [ $https ]);
51         my %o = (
52                 SSL_hostname => 'server.local',
53                 SSL_verifycn_name => 'server.local',
54                 SSL_verify_mode => SSL_VERIFY_PEER(),
55                 SSL_ca_file => 'certs/test-ca.pem',
56         );
57         # start negotiating a slow TLS connection
58         my $slow = tcp_connect($https, Blocking => 0);
59         $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
60         my @poll = (fileno($slow));
61         my $slow_done = $slow->connect_SSL;
62         if ($slow_done) {
63                 diag('W: connect_SSL early OK, slow client test invalid');
64                 push @poll, PublicInbox::Syscall::EPOLLOUT();
65         } else {
66                 push @poll, PublicInbox::TLS::epollbit();
67         }
68
69         # normal HTTPS
70         my $c = tcp_connect($https);
71         IO::Socket::SSL->start_SSL($c, %o);
72         ok($c->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
73                 'wrote HTTP request');
74         my $buf = '';
75         sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/;
76         like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response');
77
78         # HTTPS with bad hostname
79         $c = tcp_connect($https);
80         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.fail';
81         $c = IO::Socket::SSL->start_SSL($c, %o);
82         is($c, undef, 'HTTPS fails with bad hostname');
83
84         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
85         $c = tcp_connect($https);
86         IO::Socket::SSL->start_SSL($c, %o);
87         ok($c, 'HTTPS succeeds again with valid hostname');
88
89         # slow TLS connection did not block the other fast clients while
90         # connecting, finish it off:
91         until ($slow_done) {
92                 IO::Poll::_poll(-1, @poll);
93                 $slow_done = $slow->connect_SSL and last;
94                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
95         }
96         $slow->blocking(1);
97         ok($slow->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
98                 'wrote HTTP request from slow');
99         $buf = '';
100         sysread($slow, $buf, 666, length($buf)) until $buf =~ /\r\n\r\n/;
101         like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response from slow');
102         $slow = undef;
103
104         SKIP: {
105                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
106                 my $var = Socket::TCP_DEFER_ACCEPT();
107                 defined(my $x = getsockopt($https, IPPROTO_TCP, $var)) or die;
108                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on https');
109         };
110         SKIP: {
111                 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
112                 if (system('kldstat -m accf_data >/dev/null')) {
113                         skip 'accf_data not loaded? kldload accf_data', 2;
114                 }
115                 require PublicInbox::Daemon;
116                 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
117                 my $x = getsockopt($https, SOL_SOCKET, $var);
118                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for https');
119         };
120
121         $c = undef;
122         kill('TERM', $pid);
123         is($pid, waitpid($pid, 0), 'httpd exited successfully');
124         is($?, 0, 'no error in exited process');
125         $pid = undef;
126         if (defined $tail_pid) {
127                 kill 'TERM', $tail_pid;
128                 waitpid($tail_pid, 0);
129                 $tail_pid = undef;
130         }
131 }
132 done_testing();
133 1;