File: basic.t

package info (click to toggle)
libcrypt-util-perl 0.11-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: perl: 2,438; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,887 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More 'no_plan';

use ok "Crypt::Util";

can_ok "Crypt::Util" => qw/
	cipher_object
	digest_object
/;

my $c = Crypt::Util->new;

isa_ok( $c, "Crypt::Util" );

ok( !$c->has_default_cipher, "no default cipher" );

SKIP: {
	my $fallback_cipher = eval { $c->fallback_cipher };

	skip "Couldn't load any cipher", 8 unless $fallback_cipher;
	skip "Couldn't load any mode", 8 unless eval { $c->fallback_mode };

	ok( defined($fallback_cipher), "fallback defined" );

	my $cipher = $c->cipher_object( key => "foo" );

	can_ok( $cipher, qw/encrypt decrypt/ );
	my $ciphertext = $cipher->encrypt("foo");
	$cipher->reset if $cipher->can("reset");
	is( $cipher->decrypt($ciphertext), "foo", "round trip encryption" );

	$c->default_key("moose");

	my ( $binary, $encoded ) = map { $c->encrypt_string(
		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.",
		encode => $_,
	) } 0, 1;

	like(
		$encoded,
		qr{^[\w\+\*\-/=]+$},
		"no funny chars",
	);

	cmp_ok( $binary, "ne", $encoded, "encoded != binary" );

	cmp_ok( length($binary), "<", length($encoded), "encoded is longer" );

	is( $c->decrypt_string( string => $encoded, decode => 1 ), $c->decrypt_string( string => $binary ), "decoded == binary" );
}

ok( !$c->has_default_digest, "no default digest" );

my $fallback_digest = eval { $c->fallback_digest };

SKIP: {
	skip "Couldn't load any digest", 4 if $@ =~ /^Couldn't load any digest/;

	ok( !$@, "no unexpected error" );
	ok( defined($fallback_digest), "fallback defined" );

	my $digest = $c->digest_object;

	can_ok( $digest, qw/add digest/ );

	$digest->add("foo");

	my $foo_digest = $digest->digest;

	$digest->add("bar");

	my $bar_digest = $digest->digest;

	cmp_ok( $foo_digest, "ne", $bar_digest, "digests differ" );

}