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