#!/usr/bin/perl

# Copyright (c) F5, Inc.
#
# This source code is licensed under the Apache License, Version 2.0 license
# found in the LICENSE file in the root directory of this source tree.

# Tests for ACME client: IPv6 identifier support.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;
use Test::Nginx::ACME;
use Test::Nginx::DNS;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http http_ssl socket_ssl/)
	->has_daemon('openssl');

eval { require IO::Socket::INET6; };
plan(skip_all => 'IO::Socket::INET6 is not installed') if $@;

$t->write_file_expand('nginx.conf', <<'EOF');

%%TEST_GLOBALS%%

daemon off;

events {
}

http {
    %%TEST_GLOBALS_HTTP%%

    resolver 127.0.0.1:%%PORT_8980_UDP%%;

    acme_issuer default {
        uri https://acme.test:%%PORT_9000%%/dir;
        challenge http-01;
        ssl_trusted_certificate acme.test.crt;
        state_path %%TESTDIR%%/acme_default;
        accept_terms_of_service;
    }

    acme_issuer tls-alpn {
        uri https://acme.test:%%PORT_9000%%/dir;
        challenge tls-alpn-01;
        ssl_trusted_certificate acme.test.crt;
        state_path %%TESTDIR%%/acme_tls-alpn;
        accept_terms_of_service;
    }

    server {
        listen       [::1]:%%PORT_8080%%;
    }

    server {
        listen       [::1]:%%PORT_8443%% ssl;
        server_name  0000:0000:0000:0000:0000:0000:0:1;

        acme_certificate default;

        ssl_certificate $acme_certificate;
        ssl_certificate_key $acme_certificate_key;
    }

    server {
        listen       [::1]:%%PORT_8444%% ssl;
        server_name  ::1;

        acme_certificate tls-alpn;

        ssl_certificate $acme_certificate;
        ssl_certificate_key $acme_certificate_key;
    }
}

EOF

$t->write_file('openssl.conf', <<EOF);
[ req ]
default_bits = 2048
encrypt_key = no
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
EOF

my $d = $t->testdir();

foreach my $name ('acme.test') {
	system('openssl req -x509 -new '
		. "-config $d/openssl.conf -subj /CN=$name/ "
		. "-out $d/$name.crt -keyout $d/$name.key "
		. ">>$d/openssl.out 2>&1") == 0
		or die "Can't create certificate for $name: $!\n";
}

my $dp = port(8980, udp => 1);
my @dc = (
	{ name => 'acme.test', A => '127.0.0.1' },
);

my $acme = Test::Nginx::ACME->new($t, port(9000), port(9001),
	$t->testdir . '/acme.test.crt',
	$t->testdir . '/acme.test.key',
	http_port => port(8080),
	tls_port => port(8444),
	dns_port => $dp,
	nosleep => 1,
);

$t->run_daemon(\&Test::Nginx::DNS::dns_test_daemon, $t, 8980, \@dc, tcp => 1);
$t->waitforfile($t->testdir . '/' . $dp);
port(8980, socket => 1)->close();

$t->run_daemon(\&Test::Nginx::ACME::acme_test_daemon, $t, $acme);
$t->waitforsocket('127.0.0.1:' . $acme->port());

$t->write_file('index.html', 'SUCCESS');
$t->try_run('no inet6 support')->plan(2);

###############################################################################

$acme->wait_certificate('acme_default/::1') or die "no certificate";
$acme->wait_certificate('acme_tls-alpn/::1') or die "no certificate";

like(get(8443), qr/SUCCESS/, 'ipv6 cert via http-01');
like(get(8444), qr/SUCCESS/, 'ipv6 cert via tls-alpn-01');

###############################################################################

sub get {
	my ($port) = @_;

	my $s = getconn('::1', $port) || return;

	http_get('/',
		socket => $s,
		SSL => 1,
		SSL_ca_file => $acme->trusted_ca(),
		SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
		SSL_verifycn_name => '::1',
	);
}

sub getconn() {
	my ($addr, $port) = @_;
	my $s;

	eval {
		local $SIG{ALRM} = sub { die "timeout\n" };
		local $SIG{PIPE} = sub { die "sigpipe\n" };
		alarm(8);

		$s = IO::Socket::INET6->new(
			PeerAddr => $addr,
			PeerPort => port($port),
		)
			or die "Can't connect to nginx: $!\n";

		alarm(0);
	};
	alarm(0);

	if ($@) {
		log_in("died: $@");
		return undef;
	}

	return $s;
}

###############################################################################
