| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use Test::More 'no_plan'; |
| 7 | |
| 8 | use ok "Crypt::Util"; |
| 9 | |
| 10 | can_ok "Crypt::Util" => qw/ |
| 11 | cipher_object |
| 12 | digest_object |
| 13 | /; |
| 14 | |
| 15 | my $c = Crypt::Util->new; |
| 16 | |
| 17 | isa_ok( $c, "Crypt::Util" ); |
| 18 | |
| 19 | ok( !$c->has_default_cipher, "no default cipher" ); |
| 20 | |
| 21 | SKIP: { |
| 22 | my $fallback_cipher = eval { $c->fallback_cipher }; |
| 23 | |
| 24 | skip "Couldn't load any cipher", 8 unless $fallback_cipher; |
| 25 | skip "Couldn't load any mode", 8 unless eval { $c->fallback_mode }; |
| 26 | |
| 27 | ok( defined($fallback_cipher), "fallback defined" ); |
| 28 | |
| 29 | my $cipher = $c->cipher_object( key => "foo" ); |
| 30 | |
| 31 | can_ok( $cipher, qw/encrypt decrypt/ ); |
| 32 | my $ciphertext = $cipher->encrypt("foo"); |
| 33 | $cipher->reset if $cipher->can("reset"); |
| 34 | is( $cipher->decrypt($ciphertext), "foo", "round trip encryption" ); |
| 35 | |
| 36 | $c->default_key("moose"); |
| 37 | |
| 38 | my ( $binary, $encoded ) = map { $c->encrypt_string( |
| 39 | string => "The quick brown fox had a crush on the lazy moose. One day she wrote the moose a love letter but since he was lazy he never replied. The end.", |
| 40 | encode => $_, |
| 41 | ) } 0, 1; |
| 42 | |
| 43 | like( |
| 44 | $encoded, |
| 45 | qr{^[\w\+\*\-/=]+$}, |
| 46 | "no funny chars", |
| 47 | ); |
| 48 | |
| 49 | cmp_ok( $binary, "ne", $encoded, "encoded != binary" ); |
| 50 | |
| 51 | cmp_ok( length($binary), "<", length($encoded), "encoded is longer" ); |
| 52 | |
| 53 | is( $c->decrypt_string( string => $encoded, decode => 1 ), $c->decrypt_string( string => $binary ), "decoded == binary" ); |
| 54 | } |
| 55 | |
| 56 | ok( !$c->has_default_digest, "no default digest" ); |
| 57 | |
| 58 | my $fallback_digest = eval { $c->fallback_digest }; |
| 59 | |
| 60 | SKIP: { |
| 61 | skip "Couldn't load any digest", 4 if $@ =~ /^Couldn't load any digest/; |
| 62 | |
| 63 | ok( !$@, "no unexpected error" ); |
| 64 | ok( defined($fallback_digest), "fallback defined" ); |
| 65 | |
| 66 | my $digest = $c->digest_object; |
| 67 | |
| 68 | can_ok( $digest, qw/add digest/ ); |
| 69 | |
| 70 | $digest->add("foo"); |
| 71 | |
| 72 | my $foo_digest = $digest->digest; |
| 73 | |
| 74 | $digest->add("bar"); |
| 75 | |
| 76 | my $bar_digest = $digest->digest; |
| 77 | |
| 78 | cmp_ok( $foo_digest, "ne", $bar_digest, "digests differ" ); |
| 79 | |
| 80 | } |
| 81 | |
| 82 |