1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#!/usr/local/bin/perl -Tw
use strict;
use lib '..','../blib/lib','.','./blib/lib';
sub test;
my (@mods,@pads,@in,$pad,$test_data,$mod,$tnum,$c,$i,$p);
@mods = qw/Rijndael
Blowfish
Blowfish_PP
IDEA
DES
/;
@pads = qw/standard oneandzeroes space null/;
for $mod (@mods) {
eval "use Crypt::$mod(); 1" && push @in,$mod;
}
unless ($#in > -1) {
print "1..0 # Skipped: no cryptographic modules found\n";
exit;
}
# ($#in + 1): number of installed modules
# ($#pads + 1): number of padding methods
# 32: number of per-module, per-pad tests
# 1: the first test -- loading Crypt::CBC module
print '1..', ($#in + 1) * ($#pads + 1) * 32 + 1, "\n";
sub test {
local($^W) = 0;
my($num, $true,$msg) = @_;
$$num++;
print($true ? "ok $$num\n" : "not ok $$num $msg\n");
}
$tnum = 0;
eval "use Crypt::CBC";
test(\$tnum,!$@,"Couldn't load module");
for $mod (@in) {
for $pad (@pads) {
$test_data = <<END;
Mary had a little lamb,
Its fleece was black as coal,
And everywere that Mary went,
That lamb would dig a hole.
END
;
test(\$tnum,$i = Crypt::CBC->new(-key => 'secret',
-cipher => $mod,
-padding => $pad,
),
"Couldn't create new object");
test(\$tnum,$c = $i->encrypt($test_data),"Couldn't encrypt");
test(\$tnum,$p = $i->decrypt($c),"Couldn't decrypt");
test(\$tnum,$p eq $test_data,"Decrypted ciphertext doesn't match plaintext");
# now try various truncations of the whole string.
# iteration 3 ends in ' ' so 'space should fail
for ($c=1;$c<=7;$c++) {
substr($test_data,-$c) = '';
if ($c == 3 && $pad eq 'space') {
test(\$tnum,$i->decrypt($i->encrypt($test_data)) ne $test_data);
} else {
test(\$tnum,$i->decrypt($i->encrypt($test_data)) eq $test_data);
}
}
# try various short strings
for ($c=0;$c<=18;$c++) {
$test_data = 'i' x $c;
test(\$tnum,$i->decrypt($i->encrypt($test_data)) eq $test_data);
}
# 'space' should fail. others should succeed.
$test_data = "This string ends in some spaces ";
if ($pad eq 'space') {
test(\$tnum,$i->decrypt($i->encrypt($test_data)) ne $test_data);
} else {
test(\$tnum,$i->decrypt($i->encrypt($test_data)) eq $test_data);
}
# 'null' should fail. others should succeed.
$test_data = "This string ends in a null\0";
if ($pad eq 'null') {
test(\$tnum,$i->decrypt($i->encrypt($test_data)) ne $test_data);
} else {
test(\$tnum,$i->decrypt($i->encrypt($test_data)) eq $test_data);
}
}
}
|