Skip to content

Commit

Permalink
Convert tls.t to use subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
spazm committed Nov 1, 2020
1 parent 5a11af3 commit da575ba
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions t/mojo/tls.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,50 @@ plan skip_all => 'IO::Socket::SSL 2.009+ required for this test!' unless Moj
use Mojo::IOLoop;
use Socket;

# Built-in certificate
socketpair(my $client_sock, my $server_sock, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "Couldn't create socket pair: $!";
$client_sock->blocking(0);
$server_sock->blocking(0);
my $delay = Mojo::IOLoop->delay;
my $server = Mojo::IOLoop::TLS->new($server_sock);
$server->once(upgrade => $delay->begin);
$server->once(error => sub { warn pop });
$server->negotiate({server => 1});
my $client = Mojo::IOLoop::TLS->new($client_sock);
$client->once(upgrade => $delay->begin);
$client->once(error => sub { warn pop });
$client->negotiate(tls_verify => 0x00);
my ($client_result, $server_result);
$delay->then(sub { ($server_result, $client_result) = @_ });
$delay->wait;
is ref $client_result, 'IO::Socket::SSL', 'right class';
is ref $server_result, 'IO::Socket::SSL', 'right class';

# Built-in certificate (custom event loop and cipher)
my $loop = Mojo::IOLoop->new;
socketpair(my $client_sock2, my $server_sock2, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die "Couldn't create socket pair: $!";
$client_sock2->blocking(0);
$server_sock2->blocking(0);
$delay = $loop->delay;
$server = Mojo::IOLoop::TLS->new($server_sock2)->reactor($loop->reactor);
$server->once(upgrade => $delay->begin);
$server->once(error => sub { warn pop });
$server->negotiate(server => 1, tls_ciphers => 'AES256-SHA:ALL');
$client = Mojo::IOLoop::TLS->new($client_sock2)->reactor($loop->reactor);
$client->once(upgrade => $delay->begin);
$client->once(error => sub { warn pop });
$client->negotiate(tls_verify => 0x00);
$client_result = $server_result = undef;
$delay->then(sub { ($server_result, $client_result) = @_ });
$delay->wait;
is ref $client_result, 'IO::Socket::SSL', 'right class';
is ref $server_result, 'IO::Socket::SSL', 'right class';
my $expect = $server_result->get_sslversion eq 'TLSv1_3' ? 'TLS_AES_256_GCM_SHA384' : 'AES256-SHA';
is $client_result->get_cipher, $expect, "$expect has been negotiatied";
is $server_result->get_cipher, $expect, "$expect has been negotiatied";
subtest("Built-in certificate") => sub {
socketpair(my $client_sock, my $server_sock, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "Couldn't create socket pair: $!";
$client_sock->blocking(0);
$server_sock->blocking(0);
my $delay = Mojo::IOLoop->delay;
my $server = Mojo::IOLoop::TLS->new($server_sock);
$server->once(upgrade => $delay->begin);
$server->once(error => sub { warn pop });
$server->negotiate({server => 1});
my $client = Mojo::IOLoop::TLS->new($client_sock);
$client->once(upgrade => $delay->begin);
$client->once(error => sub { warn pop });
$client->negotiate(tls_verify => 0x00);
my ($client_result, $server_result);
$delay->then(sub { ($server_result, $client_result) = @_ });
$delay->wait;
is ref $client_result, 'IO::Socket::SSL', 'right class';
is ref $server_result, 'IO::Socket::SSL', 'right class';
}

subtest("Built-in certificate (custom event loop and cipher)") => sub {
my $loop = Mojo::IOLoop->new;
socketpair(my $client_sock2, my $server_sock2, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die "Couldn't create socket pair: $!";
$client_sock2->blocking(0);
$server_sock2->blocking(0);
$delay = $loop->delay;
$server = Mojo::IOLoop::TLS->new($server_sock2)->reactor($loop->reactor);
$server->once(upgrade => $delay->begin);
$server->once(error => sub { warn pop });
$server->negotiate(server => 1, tls_ciphers => 'AES256-SHA:ALL');
$client = Mojo::IOLoop::TLS->new($client_sock2)->reactor($loop->reactor);
$client->once(upgrade => $delay->begin);
$client->once(error => sub { warn pop });
$client->negotiate(tls_verify => 0x00);
$client_result = $server_result = undef;
$delay->then(sub { ($server_result, $client_result) = @_ });
$delay->wait;
is ref $client_result, 'IO::Socket::SSL', 'right class';
is ref $server_result, 'IO::Socket::SSL', 'right class';

my $expect = $server_result->get_sslversion eq 'TLSv1_3' ? 'TLS_AES_256_GCM_SHA384' : 'AES256-SHA';
is $client_result->get_cipher, $expect, "$expect has been negotiatied";
is $server_result->get_cipher, $expect, "$expect has been negotiatied";
}

done_testing;

0 comments on commit da575ba

Please sign in to comment.