Debian Codegraph @

lemonldap-ng-common/t/35-Common-Crypto.t

raw
1 use warnings;
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl Lemonldap-NG-Common.t'
5
6 #########################
7
8 # change 'tests => 1' to 'tests => last_test_to_print';
9
10 use Test::More tests => 21;
11 use Digest::MD5 qw(md5 md5_hex md5_base64);
12 use strict;
13
14 BEGIN {
15 use_ok('Lemonldap::NG::Common::Crypto');
16 }
17
18 #########################
19
20 # Insert your test code below, the Test::More module is used here so read
21 # its man page ( perldoc Test::More ) for help writing this test script.
22
23 my $c;
24
25 ok(
26 $c = Lemonldap::NG::Common::Crypto->new(
27 'lemonldap-ng-key', Crypt::Rijndael::MODE_CBC()
28 ),
29 'New object'
30 );
31 foreach my $i ( 1 .. 17 ) {
32 my $s = '';
33 $s = join( '', map { chr( int( rand(94) ) + 33 ) } ( 1 .. $i ) );
34 ok( $c->decrypt( $c->encrypt($s) ) eq $s,
35 "Test of base64 encrypting with $i characters string" )
36 or diagErr( $s, $c->decrypt( $c->encrypt($s) ), $c->encrypt($s) );
37 }
38
39 my $data = md5_hex(rand);
40 my $secondKey = md5(rand);
41 ok(
42 $c->decryptHex( $c->encryptHex( $data, $secondKey ), $secondKey ) eq $data,
43 "Test of hexadecimal encrypting"
44 )
45 or diagErr(
46 $data,
47 $c->decryptHex( $c->encryptHex( $data, $secondKey ), $secondKey ),
48 $c->encryptHex( $data, $secondKey )
49 );
50
51 # Test a long value, and replace carriage return by %0A
52 my $long = Lemonldap::NG::Common::Crypto::srandom()->randpattern( "s" x 1000 );
53 ok( $c->decrypt( $c->encrypt($long) ) eq $long,
54 "Test of long value encrypting" )
55 or diagErr(
56 $long,
57 $c->decryptHex( $c->encryptHex($long) ),
58 $c->encryptHex($long)
59 );
60
61 sub diagErr {
62 diag "Expect: $_[0]\nGet : $_[1]\nCipher: $_[2]";
63 }