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