| 1 |
#!perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Test::Most; |
| 5 |
use lib 't/lib'; |
| 6 |
use Test::zxcvbn qw(match_for_testing cmp_match cmp_sequence); |
| 7 |
use Data::Password::zxcvbn::MatchList; |
| 8 |
|
| 9 |
sub test_scoring { |
| 10 |
my ($input,$expected,$message) = @_; |
| 11 |
|
| 12 |
my $password = '0123456789'; |
| 13 |
my $result = Data::Password::zxcvbn::MatchList->new({ |
| 14 |
password => $password, |
| 15 |
matches => $input, |
| 16 |
})->most_guessable_match_list(1); |
| 17 |
|
| 18 |
cmp_sequence( |
| 19 |
$result, |
| 20 |
$expected, |
| 21 |
$message, |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
subtest 'empty match sequence' => sub { |
| 26 |
test_scoring( |
| 27 |
[], |
| 28 |
[ cmp_match(0,9,'BruteForce') ], |
| 29 |
'returns one bruteforce match', |
| 30 |
); |
| 31 |
}; |
| 32 |
|
| 33 |
subtest 'match covers prefix of password' => sub { |
| 34 |
my $match = match_for_testing(0,5,1); |
| 35 |
test_scoring( |
| 36 |
[ $match ], |
| 37 |
[ $match, cmp_match(6,9,'BruteForce') ], |
| 38 |
'returns match + bruteforce', |
| 39 |
); |
| 40 |
}; |
| 41 |
|
| 42 |
subtest 'match covers suffix of password' => sub { |
| 43 |
my $match = match_for_testing(3,9,1); |
| 44 |
test_scoring( |
| 45 |
[$match], |
| 46 |
[ cmp_match(0,2,'BruteForce'), $match ], |
| 47 |
'returns bruteforce + match', |
| 48 |
); |
| 49 |
}; |
| 50 |
|
| 51 |
subtest 'match covers infix of password' => sub { |
| 52 |
my $match = match_for_testing(1,8,1); |
| 53 |
test_scoring( |
| 54 |
[$match], |
| 55 |
[ |
| 56 |
cmp_match(0,0,'BruteForce'), |
| 57 |
$match, |
| 58 |
cmp_match(9,9,'BruteForce'), |
| 59 |
], |
| 60 |
'returns bruteforce + match + bruteforce', |
| 61 |
); |
| 62 |
}; |
| 63 |
|
| 64 |
subtest 'given two matches of the same span' => sub { |
| 65 |
my $matches = [ match_for_testing(0,9,1), match_for_testing(0,9,2) ]; |
| 66 |
test_scoring( |
| 67 |
$matches, |
| 68 |
[ $matches->[0] ], |
| 69 |
'chooses lower-guesses match', |
| 70 |
); |
| 71 |
|
| 72 |
$matches = [ match_for_testing(0,9,3), match_for_testing(0,9,2) ]; |
| 73 |
test_scoring( |
| 74 |
$matches, |
| 75 |
[ $matches->[1] ], |
| 76 |
'and the order in which matches are given does not matter', |
| 77 |
); |
| 78 |
}; |
| 79 |
|
| 80 |
subtest 'when m0 covers m1 and m2' => sub { |
| 81 |
my $matches = [ |
| 82 |
match_for_testing(0,9,3), |
| 83 |
match_for_testing(0,3,2), |
| 84 |
match_for_testing(4,9,1), |
| 85 |
]; |
| 86 |
test_scoring( |
| 87 |
$matches, |
| 88 |
{ guesses => 3, matches => [ $matches->[0] ] }, |
| 89 |
'choose [m0] when m0 < m1 * m2 * fact(2)', |
| 90 |
); |
| 91 |
|
| 92 |
$matches = [ |
| 93 |
match_for_testing(0,9,5), |
| 94 |
match_for_testing(0,3,2), |
| 95 |
match_for_testing(4,9,1), |
| 96 |
]; |
| 97 |
test_scoring( |
| 98 |
$matches, |
| 99 |
{ guesses => 4, matches => [ $matches->[1], $matches->[2] ] }, |
| 100 |
'choose [m1,m2] when m0 > m1 * m2 * fact(2)', |
| 101 |
); |
| 102 |
}; |
| 103 |
|
| 104 |
done_testing; |