summaryrefslogtreecommitdiff
path: root/t/20-app-raps2-password.t
blob: ae4f267a6b5682add3ed6210aca2a59b0b114603 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

use Test::More tests => 19;
use Test::Fatal;

my $pw;
my $salt = 'abcdefghijklmnop';
my $pass = 'something';

use_ok('App::Raps2::Password');

like(
	exception {
		App::Raps2::Password->new();
	},
	qr{incorrect salt length},
	'new() missing salt and passphrase'
);

like(
	exception {
		App::Raps2::Password->new(salt => $salt);
	},
	qr{no passphrase given},
	'new() missing passphrase'
);

like(
	exception {
		App::Raps2::Password->new(
			salt => $salt,
			passphrase => q{}
		);
	},
	qr{no passphrase given},
	'new() missing passphrase'
);

like(
	exception {
		App::Raps2::Password->new(passphrase => $pass);
	},
	qr{incorrect salt length},
	'new() missing salt'
);

like(
	exception {
		App::Raps2::Password->new(
			passphrase => $pass,
			salt => 'abcdefghijklmno',
		);
	},
	qr{incorrect salt length},
	'new() salt one too short'
);

like(
	exception {
		App::Raps2::Password->new(
			passphrase => $pass,
			salt => $salt . 'z',
		);
	},
	qr{incorrect salt length},
	'new() salt one too long'
);

$pw = App::Raps2::Password->new(
	passphrase => $pass,
	salt => $salt,
);
isa_ok($pw, 'App::Raps2::Password');

$pw = App::Raps2::Password->new(
	cost => 8,
	salt => $salt,
	passphrase => $pass,
);

isa_ok($pw, 'App::Raps2::Password');

is($pw->decrypt('53616c7465645f5f80d8c367e15980d43ec9a6eabc5390b4'), 'quux',
	'decrypt okay');

is($pw->decrypt($pw->encrypt('foo')), 'foo', 'encrypt->decrypt okay');

ok($pw->verify('3lJRlaRuOGWv/z3g1DAOlcH.u9vS8Wm'), 'verify: verifies correct hash');

like(
	exception {
		$pw->verify('3lJRlaRuOGWv/z3g1DAOlcH.u9vS8WM');
	},
	qr{Passwords did not match},
	'verify: does not verify invalid hash'
);

ok($pw->verify($pw->crypt('truth')), 'crypt->verify okay');

like(
	exception {
		$pw->salt();
	},
	qr{incorrect salt length},
	'salt: No argument',
);

like(
	exception {
		$pw->salt('');
	},
	qr{incorrect salt length},
	'salt: Empty argument',
);

like(
	exception {
		$pw->salt('abcdefghijklmno');
	},
	qr{incorrect salt length},
	'salt: One too short',
);

like(
	exception {
		$pw->salt($salt . 'z');
	},
	qr{incorrect salt length},
	'salt: One too long',
);

is(
	exception {
		$pw->salt($salt);
	},
	undef,
	'salt: Correct length',
);