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