File: digest.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 (112 lines) | stat: -rw-r--r-- 2,166 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
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
109
110
111
112
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Exception;

use Crypt::Util;

my ( $c, $fallback_digest, $fallback_mac );

BEGIN {
	$c = Crypt::Util->new;

	$fallback_digest = eval { $c->fallback_digest };
	plan skip_all => "Couldn't load any digest" if $@;

	$fallback_mac = eval { $c->fallback_mac };
	plan skip_all => "Couldn't load any mac" if $@;

	plan 'no_plan';
}

my $string = "magic moose";

my $hash = $c->digest_string( string => $string );

ok(
	eval {
		$c->verify_hash(
			hash   => $hash,
			string => $string,
		);
	},
	"verify digest",
);

ok( !$@, "no error" ) || diag $@;

ok(
	eval {
		!$c->verify_hash(
			hash   => $hash,
			string => "some other string",
		);
	},
	"verify bad digest",
);

ok( !$@, "no error" ) || diag $@;

throws_ok {
	$c->verify_hash(
		hash   => $hash,
		string => "some other string",
		fatal  => 1,
	),
} qr/verification failed/, "verify_hash with fatal => 1";

{
	my $mac_1 = $c->mac_digest_string( string => "foo", key => "moose" );
	my $mac_2 = $c->mac_digest_string( string => "foo", key => "elk" );

	cmp_ok( $mac_1, "ne", $mac_2, "mac hashes are ne with different keys" );
}

{
	my $mac_1 = $c->mac_digest_string( string => "foo", key => "moose" );
	my $mac_2 = $c->mac_digest_string( string => "bar", key => "moose" );

	cmp_ok( $mac_1, "ne", $mac_2, "mac hashes are ne with different messages" );
}

{
	my $mac_1 = $c->mac_digest_string( string => "foo", key => "moose" );
	my $mac_2 = $c->mac_digest_string( string => "foo", key => "moose" );

	is( $mac_1, $mac_2, "mac hashes are eq when the same" );
}


SKIP: {
	eval { require Digest::MD5 };
	skip "Digest::MD5 couldn't be loaded", 3 if $@;
	skip "Digest::MD5 is the only fallback", 3 if $fallback_digest eq "SHAMD5";

	my $md5_hash = $c->digest_string(
		digest => "MD5",
		string => $string,
	);

	cmp_ok( $md5_hash, "ne", $hash, "$fallback_digest hash ne MD5 hash" );

	ok(
		!$c->verify_hash(
			hash   => $md5_hash,
			string => $string,
		),
		"verification fails without same digest",
	);

	ok(
		$c->verify_hash(
			hash   => $md5_hash,
			string => $string,
			digest => "MD5",
		),
		"verification succeeds when MD5",
	);
}