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