]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-https.t
daemon: provide TCP_DEFER_ACCEPT for Perl <5.14
[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 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 = $https->sockhost . ':' . $https->sockport;
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         ok($c->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
57                 'wrote HTTP request');
58         my $buf = '';
59         sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/;
60         like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response');
61
62         # HTTPS with bad hostname
63         $c = tcp_connect($https);
64         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.fail';
65         $c = IO::Socket::SSL->start_SSL($c, %o);
66         is($c, undef, 'HTTPS fails with bad hostname');
67
68         $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
69         $c = tcp_connect($https);
70         IO::Socket::SSL->start_SSL($c, %o);
71         ok($c, 'HTTPS succeeds again with valid hostname');
72
73         # slow TLS connection did not block the other fast clients while
74         # connecting, finish it off:
75         until ($slow_done) {
76                 IO::Poll::_poll(-1, @poll);
77                 $slow_done = $slow->connect_SSL and last;
78                 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
79         }
80         $slow->blocking(1);
81         ok($slow->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
82                 'wrote HTTP request from slow');
83         $buf = '';
84         sysread($slow, $buf, 666, length($buf)) until $buf =~ /\r\n\r\n/;
85         like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response from slow');
86         $slow = undef;
87
88         SKIP: {
89                 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
90                 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
91                 defined(my $x = getsockopt($https, IPPROTO_TCP, $var)) or die;
92                 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on https');
93         };
94         SKIP: {
95                 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
96                 if (system('kldstat -m accf_data >/dev/null')) {
97                         skip 'accf_data not loaded? kldload accf_data', 2;
98                 }
99                 require PublicInbox::Daemon;
100                 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
101                 my $x = getsockopt($https, SOL_SOCKET, $var);
102                 like($x, qr/\Adataready\0+\z/, 'got dataready accf for https');
103         };
104
105         $c = undef;
106         $td->kill;
107         $td->join;
108         is($?, 0, 'no error in exited process');
109 }
110 done_testing();
111 1;