1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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)) {
11 plan skip_all => "$mod missing for $0" if $@;
13 my $cert = 'certs/server-cert.pem';
14 my $key = 'certs/server-key.pem';
15 unless (-r $key && -r $cert) {
17 "certs/ missing for $0, run ./create-certs.perl in certs/";
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();
30 foreach ($pid, $tail_pid) {
31 kill 'TERM', $_ if defined $_;
34 my $https_addr = $https->sockhost . ':' . $https->sockport;
35 my %opt = ( Proto => 'tcp', PeerAddr => $https_addr, Type => SOCK_STREAM );
38 [ "-lhttps://$https_addr/?key=$key,cert=$cert" ],
41 open my $fh, '>', $_ or die "truncate: $!";
43 if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
45 if (defined $tail_pid && $tail_pid == 0) {
46 exec(split(' ', $tail_cmd), $out, $err);
49 my $cmd = [ $httpd, '-W0', @$args,
50 "--stdout=$out", "--stderr=$err", $psgi ];
51 $pid = spawn_listener(undef, $cmd, [ $https ]);
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',
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;
64 diag('W: connect_SSL early OK, slow client test invalid');
65 push @poll, PublicInbox::Syscall::EPOLLOUT();
67 push @poll, PublicInbox::TLS::epollbit();
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');
76 sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/;
77 like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response');
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');
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');
90 # slow TLS connection did not block the other fast clients while
91 # connecting, finish it off:
93 IO::Poll::_poll(-1, @poll);
94 $slow_done = $slow->connect_SSL and last;
95 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
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');
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');
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');
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;
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');
124 is($pid, waitpid($pid, 0), 'httpd exited successfully');
125 is($?, 0, 'no error in exited process');
127 if (defined $tail_pid) {
128 kill 'TERM', $tail_pid;
129 waitpid($tail_pid, 0);