-
Notifications
You must be signed in to change notification settings - Fork 4
/
password_policies_test.go
62 lines (54 loc) · 1.75 KB
/
password_policies_test.go
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
package passhash_test
import (
"errors"
"testing"
)
import (
"github.com/dhui/passhash"
)
func TestPasswordPoliciesNotMetDuplicateErrors(t *testing.T) {
err := passhash.PasswordPoliciesNotMet{UnMetPasswordPolicies: []passhash.PasswordPolicyError{
{passhash.AtLeastNRunes{}, errors.New("Dummy error 1")},
{passhash.AtLeastNRunes{}, errors.New("Dummy error 2")},
}}
expectedError := "Password policies not met due to: Dummy error 1, Dummy error 2"
if err.Error() != expectedError {
t.Errorf("PasswordPoliciesNotMet.Error() did not properly handle duplicate PasswordPolicyErrors. %s != %s", err.Error(), expectedError)
}
}
func TestAtLeastNRunesTooShort(t *testing.T) {
pp := passhash.AtLeastNRunes{N: 5}
if err := pp.PasswordAcceptable("1234"); err == nil {
t.Errorf("Password that's too short was accepted")
}
}
func TestAtLeastNRunesExact(t *testing.T) {
pp := passhash.AtLeastNRunes{N: 5}
if err := pp.PasswordAcceptable("12345"); err != nil {
t.Errorf("Password that's exactly long enough was rejected")
}
}
func TestAtLeastNRunesEnough(t *testing.T) {
pp := passhash.AtLeastNRunes{N: 5}
if err := pp.PasswordAcceptable("123456"); err != nil {
t.Errorf("Password that's long enough was rejected")
}
}
func TestNotCommonPasswordNaiveAccepted(t *testing.T) {
pp := passhash.NotCommonPasswordNaive{CommonPasswords: map[string]bool{
"foo": true,
"bar": true,
}}
if err := pp.PasswordAcceptable("rarepassword"); err != nil {
t.Errorf("Password that's not common was rejected")
}
}
func TestNotCommonPasswordNaiveRejected(t *testing.T) {
pp := passhash.NotCommonPasswordNaive{CommonPasswords: map[string]bool{
"foo": true,
"bar": true,
}}
if err := pp.PasswordAcceptable("foo"); err == nil {
t.Errorf("Password that's common was accepted")
}
}