Blame SOURCES/IO-Socket-SSL-2.066-Test-client-performs-Post-Handshake-Authentication.patch

1415bb
From 6b05dc28e94e90ab4852c9977d7fbe66fec6cd48 Mon Sep 17 00:00:00 2001
1415bb
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
1415bb
Date: Fri, 8 Feb 2019 14:50:32 +0100
1415bb
Subject: [PATCH] Test client performs Post-Handshake-Authentication
1415bb
MIME-Version: 1.0
1415bb
Content-Type: text/plain; charset=UTF-8
1415bb
Content-Transfer-Encoding: 8bit
1415bb
1415bb
This test uses openssl tool because PHA is not yet supported by
1415bb
IO::Socket::SSL's server implementation. The openssl tool uses a fixed
1415bb
port. So the test can fail.
1415bb
1415bb
Signed-off-by: Petr Písař <ppisar@redhat.com>
1415bb
---
1415bb
 MANIFEST       |  1 +
1415bb
 t/pha_client.t | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++
1415bb
 2 files changed, 91 insertions(+)
1415bb
 create mode 100755 t/pha_client.t
1415bb
1415bb
diff --git a/MANIFEST b/MANIFEST
1415bb
index 20cddb6..2b8328d 100644
1415bb
--- a/MANIFEST
1415bb
+++ b/MANIFEST
1415bb
@@ -57,6 +57,7 @@ t/mitm.t
1415bb
 t/multiple-cert-rsa-ecc.t
1415bb
 t/nonblock.t
1415bb
 t/npn.t
1415bb
+t/pha_client.t
1415bb
 t/plain_upgrade_downgrade.t
1415bb
 t/protocol_version.t
1415bb
 t/public_suffix_lib_encode_idn.t
1415bb
diff --git a/t/pha_client.t b/t/pha_client.t
1415bb
new file mode 100755
1415bb
index 0000000..2413588
1415bb
--- /dev/null
1415bb
+++ b/t/pha_client.t
1415bb
@@ -0,0 +1,90 @@
1415bb
+#!/usr/bin/perl
1415bb
+use strict;
1415bb
+use warnings;
1415bb
+use Test::More;
1415bb
+use IPC::Run ();
1415bb
+use IO::Socket::SSL ();
1415bb
+use Net::SSLeay ();
1415bb
+use IO::Select ();
1415bb
+
1415bb
+if (system('openssl', 'version')) {
1415bb
+    plan skip_all => 'openssl tool is not available';
1415bb
+} elsif (!defined &Net::SSLeay::CTX_set_post_handshake_auth) {
1415bb
+    plan skip_all => 'Net::SSLeay does not expose PHA';
1415bb
+} else {
1415bb
+    plan tests => 5;
1415bb
+}
1415bb
+
1415bb
+my $port = 2000;
1415bb
+my $ca_cert = 'certs/test-ca.pem';
1415bb
+
1415bb
+diag 'Starting a server';
1415bb
+my ($server, $input, $stdout, $stderr);
1415bb
+eval {
1415bb
+    $server = IPC::Run::start(['openssl', 's_server', '-port', $port,
1415bb
+            '-Verify', '1',
1415bb
+            '-cert', 'certs/server-wildcard.pem',
1415bb
+            '-key', 'certs/server-wildcard.pem', '-CAfile', $ca_cert],
1415bb
+        \$input, \$stdout, \$stderr);
1415bb
+    # subsequent \undef does not work
1415bb
+    # <https://github.com/toddr/IPC-Run/issues/124>
1415bb
+};
1415bb
+if (!$server or $@) {
1415bb
+    BAIL_OUT("Could not start a server: $@");
1415bb
+}
1415bb
+# openssl s_server does not return a non-zero exit code in case of bind(2) failure.
1415bb
+while ($server->pumpable && $stdout !~ /\nACCEPT\n/) { $server->pump; }
1415bb
+if ($stderr =~ /unable to bind socket/) {
1415bb
+    $server->kill_kill;
1415bb
+    BAIL_OUT("Could not start a server: $stderr");
1415bb
+}
1415bb
+ok($server, 'Server started');
1415bb
+
1415bb
+my $client = IO::Socket::SSL->new(
1415bb
+    PeerHost => 'localhost',
1415bb
+    PeerPort => $port,
1415bb
+    SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER,
1415bb
+    SSL_verifycn_scheme => 'www',
1415bb
+    SSL_verifycn_name => 'www.server.local',
1415bb
+    SSL_ca_file => $ca_cert,
1415bb
+    SSL_key_file => 'certs/client-key.pem',
1415bb
+    SSL_cert_file => 'certs/client-cert.pem'
1415bb
+);
1415bb
+ok($client, 'Client connected');
1415bb
+
1415bb
+SKIP: {
1415bb
+    skip "Connection failed: errno=$!, SSL errror=$IO::Socket::SSL::SSL_ERROR", 2
1415bb
+        unless $client;
1415bb
+    $client->blocking(0);
1415bb
+
1415bb
+    SKIP: {
1415bb
+        # Ask openssl s_server for PHA request and wait for the result.
1415bb
+        $input .= "c\n";
1415bb
+        while ($server->pumpable &&
1415bb
+            $stderr !~ /SSL_verify_client_post_handshake/ &&
1415bb
+            $stdout !~ /SSL_do_handshake -> 1/
1415bb
+        ) {
1415bb
+            # Push the PHA command to the server and read outputs.
1415bb
+            $server->pump;
1415bb
+
1415bb
+            # Client also must perform I/O to process the PHA request.
1415bb
+            my $select = IO::Select->new($client);
1415bb
+            while ($select->can_read(1)) {  # 1 second time-out because of
1415bb
+                                            # blocking IPC::Run
1415bb
+                my $retval = $client->read(my $buf, 1);
1415bb
+                if (defined $buf and $buf eq 'c') {
1415bb
+                    skip 'openssl tool does not support PHA command', 1;
1415bb
+                }
1415bb
+            }
1415bb
+        }
1415bb
+        ok($stdout =~ /SSL_do_handshake -> 1/, 'Client performed PHA');
1415bb
+    }
1415bb
+
1415bb
+    ok($client->close, 'Client disconnected');
1415bb
+}
1415bb
+
1415bb
+eval {
1415bb
+    $server->kill_kill;
1415bb
+};
1415bb
+ok(!$@, 'Server terminated');
1415bb
+
1415bb
-- 
1415bb
2.20.1
1415bb