Crypt-MatrixSSL3

 view release on metacpan or  search on metacpan

t/leak.t  view on Meta::CPAN

use warnings;
use strict;
use Carp;
use Test::More tests => 26;
use Test::Exception;

use Scalar::Util qw( weaken );

use Crypt::MatrixSSL3 qw( :DEFAULT :Validate :Alert get_ssl_alert get_ssl_error );

Crypt::MatrixSSL3::Open();

my $trustedCAbundle     = $Crypt::MatrixSSL3::CA_CERTIFICATES;
my $trustedCAcertFiles  = 't/cert/testCA.crt';
my $certFile            = 't/cert/server.crt';
my $privFile            = 't/cert/server.key';
my $privPass            = undef;

my $trustedCAcert; if(open(IN,'<',"$trustedCAcertFiles.der")) {local $/; $trustedCAcert=<IN>; close(IN); }
my $cert; if(open(IN,'<',"$certFile.der")) {local $/; $cert=<IN>; close(IN); }
my $priv; if(open(IN,'<',"$privFile.der")) {local $/; $priv=<IN>; close(IN); }

our ($Server_Keys, $Client_Keys, $SessionID);

our @VALIDATE = (
    0, SSL_ALLOW_ANON_CONNECTION, undef, -1, 
    SSL_ALERT_BAD_CERTIFICATE, SSL_ALERT_UNKNOWN_CA,
    SSL_ALERT_UNSUPPORTED_CERTIFICATE, SSL_ALERT_CERTIFICATE_REVOKED,
    SSL_ALERT_CERTIFICATE_EXPIRED, SSL_ALERT_CERTIFICATE_UNKNOWN,
    SSL_ALERT_ACCESS_DENIED,
);

sub assert { croak 'assertion failed' if $_[0] }

my ($ref, $keys, $sessionId, $ssl);

{ my $x = 'x'; weaken($ref = \$x); $keys = \$x }
ok $ref;
$keys = Crypt::MatrixSSL3::Keys->new();
ok !$ref, 'Keys->new: old $keys freed';
undef $keys;
#ok(1, 'matrixSslClose'); # make sure matrixSslClose() doesn't crash

{ my $x = 'x'; weaken($ref = \$x); $sessionId = \$x; }
ok $ref;
$sessionId = Crypt::MatrixSSL3::SessID->new();
ok !$ref, 'SessID->new: old $sessionId freed';

{ my $x = 'x'; weaken($ref = \$x); $ssl = \$x; }
ok $ref;
$keys = Crypt::MatrixSSL3::Keys->new();
assert $keys->load_rsa(undef, undef, undef, $trustedCAcertFiles);
$ssl = Crypt::MatrixSSL3::Client->new($keys, $sessionId, undef, undef, undef, undef, undef);
ok !$ref, 'Client->new: old $ssl freed';
undef $ssl;

{ my $x = 'x'; weaken($ref = \$x); $ssl = \$x; }
ok $ref;
$keys = Crypt::MatrixSSL3::Keys->new();
assert $keys->load_rsa($certFile, $privFile, $privPass, undef);
$ssl = Crypt::MatrixSSL3::Server->new($keys, undef);
ok !$ref, 'Server->new: old $ssl freed';
undef $ssl;

leaktest('newkeys_deletekeys', test=>100000);
leaktest('loadrsakeys_client', test=>500);
leaktest('loadrsakeys_server');
leaktest('loadrsakeysmem_server');
leaktest('newsessionid_deletesessionid', test=>100000);

$Server_Keys = Crypt::MatrixSSL3::Keys->new();
$Client_Keys = Crypt::MatrixSSL3::Keys->new();
assert $Server_Keys->load_rsa($certFile, $privFile, $privPass, undef);
assert $Client_Keys->load_rsa(undef, undef, undef, $trustedCAcertFiles);

# allocate large variable outside the test subs or else the reported
# memory consumption will be wrong because of the garbage collector
my $newsession_cb = do { my $a = [1 .. 1000]; sub { return $a } };
my $client_server_s = "Hello MatrixSSL!\n";
my $client_server_s16k = $client_server_s . ("\0" x 16000);

leaktest('newsession', test=>10000);
leaktest('handshake');
leaktest('client_server', test=>500);
$SessionID = Crypt::MatrixSSL3::SessID->new();
leaktest('client_server', test=>500); # now with same sessionId

undef $Server_Keys;
undef $Client_Keys;
#ok(1, 'matrixSslClose'); # make sure matrixSslClose() doesn't crash

sub newkeys_deletekeys {
    $keys = Crypt::MatrixSSL3::Keys->new();
    undef $keys;
}

sub loadrsakeys_client {
    my $keys = Crypt::MatrixSSL3::Keys->new();
    assert $keys->load_rsa(undef, undef, undef, $trustedCAcertFiles);
}

sub loadrsakeys_server {
    my $keys = Crypt::MatrixSSL3::Keys->new();
    assert $keys->load_rsa($certFile, $privFile, $privPass, $trustedCAcertFiles);
}

sub loadrsakeysmem_server {
    my $keys = Crypt::MatrixSSL3::Keys->new();
    assert $keys->load_rsa_mem($cert, $priv, $trustedCAcert);
}

sub newsessionid_deletesessionid {
    my $sessionId = Crypt::MatrixSSL3::SessID->new();
    undef $sessionId;
}

sub newsession {



( run in 3.407 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )