#!perl

use strict;
use warnings;

use blib;
use lib './lib';

use Net::mbedTLS;

use Socket;

use Benchmark;

use Net::SSLeay;
$Net::SSLeay::trace = 3;

Net::SSLeay::initialize();

use File::Temp;
my ($fh, $pem_path) = File::Temp::tempfile( CLEANUP => 1 );
print {$fh} _PEM();
close $fh;

my $mbedtls = Net::mbedTLS->new();

# pre-load trust store:
$mbedtls->create_client(\*STDOUT);

printf Net::SSLeay::SSLeay_version() . $/;
printf "mbedTLS %s$/", Net::mbedTLS::mbedtls_version_get_string();

my $cln_ctx = Net::SSLeay::CTX_new() or die "ERROR: CTX_new failed";
Net::SSLeay::CTX_load_verify_locations($cln_ctx, Mozilla::CA::SSL_ca_file(), q<>);

Benchmark::cmpthese(
    1,
    {
        openssl => sub {
            my ($cln, $srv);

            my $srv_ctx = Net::SSLeay::CTX_new() or die "ERROR: CTX_new failed";
            Net::SSLeay::set_cert_and_key($srv_ctx, $pem_path, $pem_path);


            socketpair $cln, $srv, AF_UNIX, SOCK_STREAM, 0;

            $_->blocking(0) for ($cln, $srv);

            my $cln_ssl = Net::SSLeay::new($cln_ctx) or die "ERROR: new failed";
            Net::SSLeay::set_fd($cln_ssl, fileno $cln);

            my $srv_ssl = Net::SSLeay::new($srv_ctx) or die "ERROR: new failed";
            Net::SSLeay::set_fd($srv_ssl, fileno $srv);

            my ($c_ok, $s_ok, $err);

            for (1 .. 10) {
                return if Net::SSLeay::connect($cln_ssl);
                return if Net::SSLeay::accept($srv_ssl);
#                Net::SSLeay::connect($cln_ssl) and do {
#                    my $ses = Net::SSLeay::get_session($cln_ssl);
#                    Net::SSLeay::SESSION_print_fp(\*STDOUT,$ses);
#                };
#                Net::SSLeay::accept($srv_ssl) and do {
#                    my $ses = Net::SSLeay::get_session($srv_ssl);
#                    Net::SSLeay::SESSION_print_fp(\*STDOUT,$ses);
#                };
            }

            warn "OpenSSL failed handshake";
        },

        mbedtls => sub {
return;
            my ($cln_skt, $srv_skt);

            socketpair $cln_skt, $srv_skt, AF_UNIX, SOCK_STREAM, 0;

            $_->blocking(0) for ($cln_skt, $srv_skt);

Net::mbedTLS::set_debug_threshold(2);
            my $cln = $mbedtls->create_client(
                $cln_skt,
                authmode => Net::mbedTLS::SSL_VERIFY_NONE,
            );
Net::mbedTLS::set_debug_threshold(0);
            my $srv = $mbedtls->create_server(
                $srv_skt,
                key_and_certs => [_PEM()],
            );
Net::mbedTLS::set_debug_threshold(2);

            for (1 .. 10) {
                return if $cln->shake_hands();
                return if $srv->shake_hands();
print "====== end of iteration\n";
#                $cln->shake_hands() and do {
#                    print $cln->ciphersuite() . $/;
#                    return;
#                };
#                $srv->shake_hands() and do {
#                    print $srv->ciphersuite() . $/;
#                    return;
#                };
            }

            warn "mbedTLS failed handshake";
        },
    },
);

#----------------------------------------------------------------------

sub _PEM {
return <<END;
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAyICViCXD7URjeFzt5ts7/ik8tr9WYzGFXWQ7Xc8o3huYHFpC
KUPbfdeW4nxLuocTOiPAVeauS5rqkzGJFY+ijfZQj7c/iOJkCFZbpVX9Mk9aB5He
xIVk5FdnY1QyMO3n5ENl6pcz8swolEv7gXcHHcGd98h4sblJsLocdq06dzlY8Ls2
mB5OHYz2RUeZ4zL4kytQUw9xlQvwxJDp/Q+x1zrrZ7fmY8YfehO+oDYAtL6/mdOB
hFxpegiczlxP1QuO71wLxDXPrV4wJcopKgOVaKWRnNSZTiBbqlGNUzzQ5mYOj235
5eHS5/mF/On0+m1PuJlW9sA7fVKXdr0EKBql1wIDAQABAoIBACoZncxNqbsrTfua
/7UmuY0fmYkB2iDP6CH5BuImun0QrDrf1N6XSgI9f4gk8z3CWQ4vLZab7mMfrzui
/hbR5x8J5laW8rdKWhjKEUpBKP4kXVITlgQLwmiT1bismDFf8v4iDMdaYmUL60Vg
QvonRQ5Bdmrt5DHlJwz9tzZQH2Oi/tfantEfWSU+zeIK0Kx2B7MOBJuHT4XHTQ2R
AQDXbCFluA9yBxv7hCW3WboIa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