forked from GGP1/atoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_test.go
295 lines (261 loc) · 6.67 KB
/
password_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package atoll
import (
"bytes"
"strings"
"testing"
)
func TestPassword(t *testing.T) {
cases := []struct {
p *Password
desc string
}{
{
desc: "Test all",
p: &Password{
Length: 14,
Levels: []Level{Lower, Upper, Digit, Space, Special},
Include: "kure ",
Exclude: "ad",
Repeat: false,
},
},
{
desc: "Repeat",
p: &Password{
Length: 8,
Levels: []Level{Lower, Space},
Include: "bee",
Repeat: true,
},
},
{
desc: "Length < levels",
p: &Password{
Length: 2,
Levels: []Level{Lower, Digit, Space, Special},
Include: "!",
},
},
{
desc: "Verify levels",
p: &Password{
Length: 35,
Levels: []Level{Lower, Upper, Digit, Space, Special},
Exclude: "0aT&7896a!45awq-=",
Repeat: true,
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
password, err := tc.p.Generate()
if err != nil {
t.Fatalf("Generate() failed: %v", err)
}
if len(password) != int(tc.p.Length) {
t.Errorf("Expected password to be %d characters long, got %d", tc.p.Length, len(password))
}
for i, lvl := range tc.p.Levels {
if lvl == Space {
continue
}
if int(tc.p.Length) > len(tc.p.Levels) {
if !bytes.ContainsAny(password, string(lvl)) {
t.Errorf("Expected the password to contain at least one character of the level %d", i)
}
}
}
for _, inc := range tc.p.Include {
// Skip space as we cannot guarantee that it won't be at the start or end of the password
if !bytes.ContainsRune(password, inc) && inc != ' ' {
t.Errorf("Character %q is not included", inc)
}
}
for _, exc := range tc.p.Exclude {
if bytes.ContainsRune(password, exc) {
t.Errorf("Found undesired character: %q", exc)
}
}
if !tc.p.Repeat && tc.p.Include == "" {
uniques := make(map[byte]struct{}, tc.p.Length)
for _, char := range password {
if _, ok := uniques[char]; !ok {
uniques[char] = struct{}{}
}
}
if len(password) != len(uniques) {
t.Errorf("Did not expect duplicated characters, got %d duplicates", len(password)-len(uniques))
}
}
})
}
}
func TestInvalidPassword(t *testing.T) {
cases := map[string]*Password{
"invalid length": {Length: 0},
"invalid levels": {Length: 10},
"empty level": {Length: 3, Levels: []Level{Level("")}},
"not enough characters to meet the length required": {
Length: 30,
Levels: []Level{Lower},
Repeat: false,
},
"include characters also excluded": {
Length: 7,
Levels: []Level{Digit},
Include: "?",
Exclude: "?",
},
"include characters exceeds the length": {
Length: 3,
Levels: []Level{Digit},
Include: "abcd",
},
"invalid include character": {
Length: 5,
Levels: []Level{Digit},
Include: "éÄ",
},
"lowercase level chars are excluded": {
Length: 26,
Levels: []Level{Lower, Space},
Exclude: string(Lower),
},
"uppercase level chars are excluded": {
Length: 26,
Levels: []Level{Upper, Space},
Exclude: string(Upper),
},
"digit level chars are excluded": {
Length: 10,
Levels: []Level{Lower, Digit, Space},
Exclude: string(Digit) + "aB",
},
"space level chars are excluded": {
Length: 1,
Levels: []Level{Space},
Exclude: string(Space) + "/",
},
"special level chars are excluded": {
Length: 20,
Levels: []Level{Space, Special},
Exclude: string(Special),
},
"custom level chars are excluded": {
Length: 12,
Levels: []Level{Level("test")},
Exclude: "test",
},
}
for k, tc := range cases {
if _, err := tc.Generate(); err == nil {
t.Errorf("Expected %q error, got nil", k)
}
}
}
func TestNewPassword(t *testing.T) {
length := 15
password, err := NewPassword(uint64(length), []Level{Lower, Upper, Digit})
if err != nil {
t.Fatalf("NewPassword() failed: %v", err)
}
if len(password) != length {
t.Errorf("Expected length to be %d but got %d", length, len(password))
}
if bytes.ContainsAny(password, string(Space)+string(Special)) {
t.Error("Found undesired characters")
}
}
func TestInvalidNewPassword(t *testing.T) {
cases := map[string]struct {
levels []Level
length uint64
}{
"invalid length": {length: 0, levels: []Level{Lower}},
}
for k, tc := range cases {
if _, err := NewPassword(tc.length, tc.levels); err == nil {
t.Errorf("Expected %q error, got nil", k)
}
}
}
func TestGeneratePool(t *testing.T) {
cases := map[string]struct {
password *Password
pool string
fail bool
}{
"All levels": {
fail: false,
pool: string(Lower + Upper + Digit + Space + Special),
password: &Password{Levels: []Level{Lower, Upper, Digit, Space, Special}, Exclude: "aA"},
},
"Repeating levels": {
fail: false,
pool: string(Lower) + string(Digit),
password: &Password{Levels: []Level{Lower, Lower, Digit, Digit}},
},
"First three levels": {
fail: true,
pool: string(Lower) + string(Upper) + string(Digit),
password: &Password{Levels: []Level{Lower, Upper, Digit}, Exclude: "123"},
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
tc.password.generatePool()
for _, e := range tc.password.Exclude {
tc.pool = strings.ReplaceAll(tc.pool, string(e), "")
}
if !bytes.ContainsAny(tc.password.pool, tc.pool) && tc.pool != "" {
t.Error("Pool does not contain an expected character")
}
})
}
}
func TestRandInsert(t *testing.T) {
p := &Password{Length: 13, Repeat: false, pool: []byte("ab")}
char1 := 'a'
char2 := 'b'
password := []byte{}
password = p.randInsert(password, byte(char1))
password = p.randInsert(password, byte(char2))
pwd := string(password)
if pwd != "ab" && pwd != "ba" {
t.Errorf("Expected \"ab\"/\"ba\" and got %q", pwd)
}
if bytes.ContainsAny(p.pool, "ab") {
t.Errorf("Failed removing characters from the pool")
}
}
func TestSanitize(t *testing.T) {
cases := [][]byte{[]byte(" trimSpacesX "), []byte("admin123login")}
p := &Password{Length: 13}
p.pool = []byte(string(Lower) + string(Upper) + string(Digit))
for _, tc := range cases {
got := p.sanitize(tc)
if commonPatterns.Match(got) {
t.Errorf("%q still contains common patterns", got)
}
start := got[0]
end := got[len(got)-1]
if start == ' ' || end == ' ' {
t.Errorf("The password contains leading or traling spaces: %q", got)
}
if len(got) != int(p.Length) {
t.Error("Trimmed spaces were not replaced with new characters")
}
}
}
func TestPasswordEntropy(t *testing.T) {
p := &Password{
Length: 20,
Levels: []Level{Lower, Upper, Digit, Space, Special},
Exclude: "a1r/ö",
}
expected := 130.15589280397393
got := p.Entropy()
if got != expected {
t.Errorf("Expected %f, got %f", expected, got)
}
}