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 Socket qw(SOCK_STREAM IPPROTO_TCP SOL_SOCKET);
7 # IO::Poll is part of the standard library, but distros may split them off...
8 foreach my $mod (qw(IO::Socket::SSL IO::Poll)) {
10 plan skip_all => "$mod missing for $0" if $@;
12 my $cert = 'certs/server-cert.pem';
13 my $key = 'certs/server-key.pem';
14 unless (-r $key && -r $cert) {
16 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
18 use_ok 'PublicInbox::TLS';
19 use_ok 'IO::Socket::SSL';
20 require './t/common.perl';
21 my $psgi = "./t/httpd-corner.psgi";
22 my ($tmpdir, $for_destroy) = tmpdir();
23 my $err = "$tmpdir/stderr.log";
24 my $out = "$tmpdir/stdout.log";
25 my $https = tcp_server();
27 my $https_addr = $https->sockhost . ':' . $https->sockport;
30 [ "-lhttps://$https_addr/?key=$key,cert=$cert" ],
33 open my $fh, '>', $_ or die "truncate: $!";
35 my $cmd = [ '-httpd', '-W0', @$args,
36 "--stdout=$out", "--stderr=$err", $psgi ];
37 $td = start_script($cmd, undef, { 3 => $https });
39 SSL_hostname => 'server.local',
40 SSL_verifycn_name => 'server.local',
41 SSL_verify_mode => SSL_VERIFY_PEER(),
42 SSL_ca_file => 'certs/test-ca.pem',
44 # start negotiating a slow TLS connection
45 my $slow = tcp_connect($https, Blocking => 0);
46 $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o);
47 my @poll = (fileno($slow));
48 my $slow_done = $slow->connect_SSL;
50 diag('W: connect_SSL early OK, slow client test invalid');
51 push @poll, PublicInbox::Syscall::EPOLLOUT();
53 push @poll, PublicInbox::TLS::epollbit();
57 my $c = tcp_connect($https);
58 IO::Socket::SSL->start_SSL($c, %o);
59 ok($c->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
60 'wrote HTTP request');
62 sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/;
63 like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response');
65 # HTTPS with bad hostname
66 $c = tcp_connect($https);
67 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.fail';
68 $c = IO::Socket::SSL->start_SSL($c, %o);
69 is($c, undef, 'HTTPS fails with bad hostname');
71 $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local';
72 $c = tcp_connect($https);
73 IO::Socket::SSL->start_SSL($c, %o);
74 ok($c, 'HTTPS succeeds again with valid hostname');
76 # slow TLS connection did not block the other fast clients while
77 # connecting, finish it off:
79 IO::Poll::_poll(-1, @poll);
80 $slow_done = $slow->connect_SSL and last;
81 @poll = (fileno($slow), PublicInbox::TLS::epollbit());
84 ok($slow->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"),
85 'wrote HTTP request from slow');
87 sysread($slow, $buf, 666, length($buf)) until $buf =~ /\r\n\r\n/;
88 like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response from slow');
92 skip 'TCP_DEFER_ACCEPT is Linux-only', 2 if $^O ne 'linux';
93 my $var = Socket::TCP_DEFER_ACCEPT();
94 defined(my $x = getsockopt($https, IPPROTO_TCP, $var)) or die;
95 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set on https');
98 skip 'SO_ACCEPTFILTER is FreeBSD-only', 2 if $^O ne 'freebsd';
99 if (system('kldstat -m accf_data >/dev/null')) {
100 skip 'accf_data not loaded? kldload accf_data', 2;
102 require PublicInbox::Daemon;
103 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
104 my $x = getsockopt($https, SOL_SOCKET, $var);
105 like($x, qr/\Adataready\0+\z/, 'got dataready accf for https');
111 is($?, 0, 'no error in exited process');