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