-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_test.go
303 lines (252 loc) · 8.1 KB
/
helper_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
296
297
298
299
300
301
302
303
package passit
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"errors"
"io"
"math/bits"
"regexp"
"strings"
"testing"
"testing/iotest"
"unicode"
"unicode/utf8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestRand returns a deterministic CSPRNG for testing use only.
func newTestRand() io.Reader {
var key [16]byte
var iv [aes.BlockSize]byte
block, _ := aes.NewCipher(key[:])
ctr := cipher.NewCTR(block, iv[:])
sr := cipher.StreamReader{S: ctr, R: zeroReader{}}
return bufio.NewReader(sr)
}
type zeroReader struct{}
func (zeroReader) Read(p []byte) (int, error) {
clear(p)
return len(p), nil
}
func errTestReader() io.Reader {
return iotest.ErrReader(errors.New("should not call Read"))
}
func TestJoin(t *testing.T) {
{
pattern := regexp.MustCompile(`^([a-z]+ ){5}[A-Z][0-9][~!@#$%^&*()] \+abc-[de]$`)
gen := Join("",
Repeat(EFFLargeWordlist, " ", 5),
Space,
LatinUpper,
Digit,
FromCharset("~!@#$%^&*()"),
Space,
String("+abc"),
Hyphen,
FromCharset("de"),
)
tr := newTestRand()
pass, err := gen.Password(tr)
require.NoError(t, err)
assert.Equal(t, "reprint wool pantry unworried mummify L2* +abc-e", pass)
assert.Truef(t, pattern.MatchString(pass),
"regexp.MustCompile(%q).MatchString(%q)", pattern, pass)
assert.Truef(t, utf8.ValidString(pass),
"utf8.ValidString(%q)", pass)
allRunesAllowed(t, rangeTableASCII, pass)
}
{
gen := Join("@$", LatinUpper, LatinLower, LatinMixed)
tr := newTestRand()
pass, err := gen.Password(tr)
require.NoError(t, err)
assert.Equal(t, "Y@$z@$x", pass)
assert.Truef(t, utf8.ValidString(pass),
"utf8.ValidString(%q)", pass)
allRunesAllowed(t, rangeTableASCII, pass)
}
}
func TestRepeat(t *testing.T) {
assert.PanicsWithValue(t, "passit: count must be positive", func() {
Repeat(Hyphen, " ", -1)
})
assert.Equal(t, Empty, Repeat(Hyphen, " ", 0),
"Repeat with count zero should return Empty")
assert.Equal(t, Hyphen, Repeat(Hyphen, " ", 1),
"Repeat with count one should return Generator")
for _, tc := range []struct {
count int
sep string
expect string
}{
{2, " ", "reprint wool"},
{4, "", "reprintwoolpantryunworried"},
{12, "-", "reprint-wool-pantry-unworried-mummify-veneering-securely-munchkin-juiciness-steep-cresting-dastardly"},
} {
tr := newTestRand()
pass, err := Repeat(EFFLargeWordlist, tc.sep, tc.count).Password(tr)
if !assert.NoErrorf(t, err, "valid range should not error when generating: %v", tc) {
continue
}
assert.Equal(t, tc.expect, pass, "valid range expected password: %v", tc)
}
}
func TestRepeatGen(t *testing.T) {
assert.PanicsWithValue(t, "passit: count must be positive", func() {
RepeatGen(Hyphen, Space, -1)
})
assert.Equal(t, Empty, RepeatGen(Hyphen, Space, 0),
"Repeat with count zero should return Empty")
assert.Equal(t, Hyphen, RepeatGen(Hyphen, Space, 1),
"Repeat with count one should return Generator")
for _, tc := range []struct {
count int
sep Generator
expect string
}{
{2, Space, "reprint wool"},
{2, Digit, "reprint5ultimatum"},
{4, Empty, "reprintwoolpantryunworried"},
{4, LatinUpper, "reprintXultimatumIunworriedGrecoil"},
{4, FromRangeTable(unicode.P), "reprint」pantry𛲟mummify❩securely"},
{12, String("-"), "reprint-wool-pantry-unworried-mummify-veneering-securely-munchkin-juiciness-steep-cresting-dastardly"},
{12, FromCharset("-~_"), "reprint-ultimatum-unworried~recoil~munchkin~phrase~cubical_haunt-voice-cycle_acetone~grunt"},
{12, ASCIINoLettersNumbers, "reprint,ultimatum+unworried)recoil?munchkin]phrase\"cubical|haunt(voice$cycle/acetone$grunt"},
{12, ASCIINoLetters, "reprint\\ultimatum-unworried+recoil+munchkin%phrase.cubical>haunt6voice$cycle{acetone`grunt"},
} {
tr := newTestRand()
pass, err := RepeatGen(EFFLargeWordlist, tc.sep, tc.count).Password(tr)
if !assert.NoErrorf(t, err, "valid range should not error when generating: %v", tc) {
continue
}
assert.Equal(t, tc.expect, pass, "valid range expected password: %v", tc)
}
}
func TestRandomRepeat(t *testing.T) {
const maxInt = 1<<(bits.UintSize-1) - 1
assert.PanicsWithValue(t, "passit: min argument cannot be greater than max argument", func() {
RandomRepeat(Hyphen, " ", 10, 7)
}, "min greater than max")
assert.PanicsWithValue(t, "passit: min argument must be positive", func() {
RandomRepeat(Hyphen, " ", -5, 7)
}, "min negative")
assert.PanicsWithValue(t, "passit: min argument cannot be greater than max argument", func() {
RandomRepeat(Hyphen, " ", 5, -7)
}, "min greater than max; max negative")
assert.PanicsWithValue(t, "passit: [min,max] range too large", func() {
RandomRepeat(Hyphen, " ", 0, maxInt)
}, "out of range: 0, max int")
gen := RandomRepeat(Hyphen, " ", 0, 0)
assert.Equal(t, Empty, gen, "min and max equal zero should return Empty")
gen = RandomRepeat(Hyphen, " ", 1, 1)
assert.Equal(t, Hyphen, gen, "min and max equal one should return Generator")
for _, tc := range []int{
70,
1<<16 - 1,
1<<31 - 1,
maxInt,
} {
gen := RandomRepeat(Hyphen, " ", tc, tc)
assert.IsTypef(t, (*repeatGenerator)(nil), gen,
"equal min and max should return Repeat(...): %v", tc)
}
for _, tc := range []struct {
min, max int
sep string
expect string
}{
{1, 2, " ", "mascot"},
{2, 5, "", "mascotultimatumlanternlushly"},
{4, 7, "-", "mascot-ultimatum-lantern-lushly-recoil-humvee"},
{10, 20, " ", "mascot ultimatum lantern lushly recoil humvee uncolored phrase spearmint vividness haunt esquire cargo"},
} {
tr := newTestRand()
pass, err := RandomRepeat(EFFLargeWordlist, tc.sep, tc.min, tc.max).Password(tr)
if !assert.NoErrorf(t, err, "valid range should not error when generating: %v", tc) {
continue
}
assert.Equal(t, tc.expect, pass, "valid range expected password: %v", tc)
}
}
func TestAlternate(t *testing.T) {
assert.Equal(t, Empty, Alternate(),
"Alternate with no Generators should return Empty")
assert.Equal(t, Hyphen, Alternate(Hyphen),
"Alternate with single Generator should return Generator")
for _, tc := range []struct {
gens []Generator
expect string
}{
{[]Generator{}, ""},
{[]Generator{LatinLower}, "y"},
{[]Generator{LatinLower, LatinUpper, Digit}, "z"},
{[]Generator{EFFShortWordlist1, EFFShortWordlist2, EFFLargeWordlist}, "yummy"},
{[]Generator{
Repeat(LatinLower, "!", 5),
Repeat(LatinUpper, "@", 3),
Repeat(Digit, "#", 7),
}, "z!x!e!i!s"},
} {
tr := newTestRand()
pass, err := Alternate(tc.gens...).Password(tr)
if !assert.NoErrorf(t, err, "should not error when generating: %#v", tc) {
continue
}
assert.Equal(t, tc.expect, pass, "expected password: %#v", tc)
}
}
func TestRejectionSample(t *testing.T) {
rs := RejectionSample(Repeat(LatinMixedDigit, "", 20), func(s string) bool {
return strings.Contains(s, "A") && strings.Contains(s, "0")
})
tr := newTestRand()
for _, expect := range []string{
"OVnA1oS7moBq0RUAOucW", // 1
"N0A7kS2RKGEgim1C0pr7", // 23
"RNrej47HNFYrK1z23A0D", // 25
"Y2Za255TBLAQ6Dwb0bXy", // 12
"57A0CPUu01ig0NI4BqvP", // 20
"ThD321Av11GE0kvEQhLc", // 11
"74gna0HQ5jjKALLPxdrF", // 15
"awrGYutFKLcE3O6W0AE6", // 7
"mqjrX7JnahA7gSuks0qx", // 1
"TVz0ods6Ai0otGpdbbIn", // 15
} {
pass, err := rs.Password(tr)
if !assert.NoError(t, err) {
continue
}
assert.Equal(t, expect, pass)
}
}
func TestFromSlice(t *testing.T) {
for _, tc := range []struct {
expect string
gen Generator
}{
{" ", FromSlice()},
{"to to to to to to to to", FromSlice("to")},
{"and or or and or and and or", FromSlice("and", "or")},
{"ευτυχία υγεία αιώνια ελπίδα αιώνια ευτυχία ελπίδα αιώνια", FromSlice("ελπίδα", "υγεία", "ευτυχία", "αιώνια")},
} {
tr := newTestRand()
pass, err := Repeat(tc.gen, " ", 8).Password(tr)
if !assert.NoError(t, err) {
continue
}
assert.Equal(t, tc.expect, pass)
assert.Truef(t, utf8.ValidString(pass),
"utf8.ValidString(%q)", pass)
}
}
func benchmarkGeneratorPassword(b *testing.B, gen Generator) {
b.Helper()
tr := newTestRand()
for range b.N {
_, err := gen.Password(tr)
if err != nil {
require.NoError(b, err)
}
}
}