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