1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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) {
14 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
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();
24 my $https_addr = $https->sockhost . ':' . $https->sockport;
27 [ "-lhttps://$https_addr/?key=$key,cert=$cert" ],
30 open my $fh, '>', $_ or die "truncate: $!";
32 my $cmd = [ '-httpd', '-W0', @$args,
33 "--stdout=$out", "--stderr=$err", $psgi ];
34 $td = start_script($cmd, undef, { 3 => $https });
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',
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;
47 diag('W: connect_SSL early OK, slow client test invalid');
48 push @poll, PublicInbox::Syscall::EPOLLOUT();
50 push @poll, PublicInbox::TLS::epollbit();
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');
59 sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/;
60 like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response');
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');
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');
73 # slow TLS connection did not block the other fast clients while
74 # connecting, finish it off:
76 IO::Poll::_poll(-1, @poll);
77 $slow_done = $slow->connect_SSL and last;
78 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
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');
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');
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');
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;
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');
108 is($?, 0, 'no error in exited process');