-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig_test.go
174 lines (149 loc) · 5.55 KB
/
config_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package passhash_test
import (
"errors"
"testing"
)
import (
"github.com/dhui/passhash"
)
type TestingWorkFactor struct {
Error bool
Len int
}
func (wf TestingWorkFactor) Marshal() ([]int, error) {
if wf.Error {
return []int{}, errors.New("Test marshaling error")
}
return make([]int, wf.Len), nil
}
func (wf TestingWorkFactor) Unmarshal([]int) error {
return nil
}
func TestWorkFactorsEqualDiffTypes(t *testing.T) {
a := &passhash.Pbkdf2WorkFactor{}
b := &passhash.BcryptWorkFactor{}
if passhash.WorkFactorsEqual(a, b) {
t.Errorf("WorkFactors with different types are considered equal. %T == %T", a, b)
}
}
func TestWorkFactorsEqualMarshalError(t *testing.T) {
a := TestingWorkFactor{Error: true, Len: 10}
b := TestingWorkFactor{Error: false, Len: 10}
if passhash.WorkFactorsEqual(a, b) {
t.Errorf("WorkFactors with marshaling error are considered equal. %T == %T", a, b)
}
if passhash.WorkFactorsEqual(b, a) {
t.Errorf("WorkFactors with marshaling error are considered equal. %T == %T", b, a)
}
}
func TestWorkFactorsEqualDiffLen(t *testing.T) {
a := TestingWorkFactor{Error: false, Len: 10}
b := TestingWorkFactor{Error: false, Len: 5}
if passhash.WorkFactorsEqual(a, b) {
t.Errorf("WorkFactors with different \"lengths\" are considered equal. %T == %T", a, b)
}
}
func TestWorkFactorsEqual(t *testing.T) {
a := &passhash.ScryptWorkFactor{R: 1, P: 2, N: 3}
b := &passhash.ScryptWorkFactor{R: 1, P: 2, N: 3}
if !passhash.WorkFactorsEqual(a, b) {
t.Errorf("The same WorkFactors are not considered equal. %v != %v", a, b)
}
}
func TestWorkFactorsEqualDifferent(t *testing.T) {
a := &passhash.ScryptWorkFactor{R: 1, P: 2, N: 3}
b := &passhash.ScryptWorkFactor{R: 1, P: 2, N: 4}
if passhash.WorkFactorsEqual(a, b) {
t.Errorf("Different WorkFactors are considered equal. %v == %v", a, b)
}
}
func TestConfigNewCredentialFailsPasswordPolicies(t *testing.T) {
userID := passhash.UserID(0)
password := "tooshort"
_, err := passhash.DefaultConfig.NewCredential(userID, password)
if err == nil {
t.Errorf("Password that didn't meet password policies created new credential. %v", err)
}
_, ok := err.(passhash.PasswordPoliciesNotMet)
if !ok {
t.Errorf("error should be of type PasswordPoliciesNotMet instead of %T", err)
}
}
type readerError struct{}
func (re readerError) Read(p []byte) (n int, err error) {
return 0, errors.New("Error reading")
}
func TestNewCredentialRandError(t *testing.T) {
origRandReader := passhash.GetRandReader()
defer func() {
// Very important to reset the global randReader. Otherwise other tests will fail
passhash.SetRandReader(origRandReader)
}()
passhash.SetRandReader(readerError{})
userID := passhash.UserID(0)
password := testPassword
if _, err := passhash.DefaultConfig.NewCredential(userID, password); err == nil {
t.Error("No error hit when calling random numbers")
}
}
func testWorkFactorMarshal(t *testing.T, workFactor passhash.WorkFactor, expected []int) {
marshaled, err := workFactor.Marshal()
if err != nil {
t.Fatalf("Error marshaling workFactor: %T. %v", workFactor, err)
}
if len(marshaled) != len(expected) {
t.Fatalf("Marshaled length (%d) != expected length (%d) for workFactor %T", len(marshaled), len(expected), workFactor)
}
for i, v := range marshaled {
if v != expected[i] {
t.Errorf("marshaled value (%d) != expected value (%d) at index %d for workFactor %T", v, expected[i], i, workFactor)
}
}
}
func testWorkFactorUnmarshal(t *testing.T, data []int, tester passhash.WorkFactor, expected passhash.WorkFactor) {
if err := tester.Unmarshal(data); err != nil {
t.Fatalf("Error unmarshaling workFactor: %T. %v", tester, err)
}
expectedMarshaled, err := expected.Marshal()
if err != nil {
t.Fatalf("Unable to marshal expected WorkFactor %v", expected)
}
testWorkFactorMarshal(t, tester, expectedMarshaled)
}
func testWorkFactorUnmarshalError(t *testing.T, data []int, tester passhash.WorkFactor) {
if err := tester.Unmarshal(data); err == nil {
t.Errorf("Successfully unmarshaling workFactor: %T with data: %v", tester, data)
}
}
func TestPdkdf2WorkFactorMarshal(t *testing.T) {
testWorkFactorMarshal(t, &passhash.Pbkdf2WorkFactor{Iter: 1}, []int{1})
}
func TestPdkdf2WorkFactorUnmarshal(t *testing.T) {
testWorkFactorUnmarshal(t, []int{1}, &passhash.Pbkdf2WorkFactor{}, &passhash.Pbkdf2WorkFactor{Iter: 1})
}
func TestPdkdf2WorkFactorUnmarshalError(t *testing.T) {
testWorkFactorUnmarshalError(t, []int{}, &passhash.Pbkdf2WorkFactor{})
testWorkFactorUnmarshalError(t, []int{1, 2}, &passhash.Pbkdf2WorkFactor{})
}
func TestBcryptWorkFactorMarshal(t *testing.T) {
testWorkFactorMarshal(t, &passhash.BcryptWorkFactor{Cost: 1}, []int{1})
}
func TestBcryptWorkFactorUnmarshal(t *testing.T) {
testWorkFactorUnmarshal(t, []int{1}, &passhash.BcryptWorkFactor{}, &passhash.BcryptWorkFactor{Cost: 1})
}
func TestBcryptWorkFactorUnmarshalError(t *testing.T) {
testWorkFactorUnmarshalError(t, []int{}, &passhash.BcryptWorkFactor{})
testWorkFactorUnmarshalError(t, []int{1, 2}, &passhash.BcryptWorkFactor{})
}
func TestScryptWorkFactorMarshal(t *testing.T) {
testWorkFactorMarshal(t, &passhash.ScryptWorkFactor{R: 1, P: 2, N: 3}, []int{1, 2, 3})
}
func TestScryptWorkFactorUnmarshal(t *testing.T) {
testWorkFactorUnmarshal(t, []int{1, 2, 3}, &passhash.ScryptWorkFactor{},
&passhash.ScryptWorkFactor{R: 1, P: 2, N: 3})
}
func TestScryptWorkFactorUnmarshalError(t *testing.T) {
testWorkFactorUnmarshalError(t, []int{}, &passhash.ScryptWorkFactor{})
testWorkFactorUnmarshalError(t, []int{1, 2}, &passhash.BcryptWorkFactor{})
testWorkFactorUnmarshalError(t, []int{1, 2, 3, 4}, &passhash.BcryptWorkFactor{})
}