-
Notifications
You must be signed in to change notification settings - Fork 22
/
wordlist_test.go
511 lines (453 loc) · 14.7 KB
/
wordlist_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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package spg
import (
"bufio"
"log"
"math"
"os"
"regexp"
"strconv"
"testing"
)
const doFallibleTests = false
const entCompTolerance = 10000 // entropy must be correct to 1 part in ten thousand
var abWords []string // this is where we will put the wordlist for testing
const wordsFilePath = "testdata/agwordlist.txt" // relative to where test in invoked
var abSyllables []string // this is where we will put the wordlist for testing
const syllableFilePath = "testdata/agsyllables.txt" // relative to where test in invoked
func init() {
wlFile, err := os.Open(wordsFilePath)
if err != nil {
log.Fatal(err)
}
defer wlFile.Close()
scanner := bufio.NewScanner(wlFile)
for scanner.Scan() {
abWords = append(abWords, string(scanner.Text()))
}
slFile, err := os.Open(syllableFilePath)
if err != nil {
log.Fatal(err)
}
defer slFile.Close()
scanner = bufio.NewScanner(slFile)
for scanner.Scan() {
abSyllables = append(abSyllables, string(scanner.Text()))
}
}
func TestNewWordListPasswordGenerator(t *testing.T) {
// First check that an empty lists returns an error
BadG, err := NewWordList([]string{})
if err == nil {
t.Error("Empty wordlist should produce an error")
}
if BadG != nil {
t.Error("Empty wordlist should produce a nil generator")
}
threeG, err := NewWordList([]string{"one", "two", "three"})
if err != nil {
t.Errorf("Error when creating simple wl generator: %s", err)
}
if threeG == nil {
t.Error("Our three word generator should be valid, not nil")
}
if threeG.Size() != 3 {
t.Errorf("there are only three words in this list, not %d", threeG.Size())
}
}
func TestWLGenerator(t *testing.T) {
// OK. Now for a simple wordlist test
wl, err := NewWordList(abWords)
if err != nil {
t.Errorf("Failed to create wordlist generator: %s", err)
}
a := NewWLRecipe(3, wl)
a.SeparatorChar = " "
p, err := a.Generate()
pwd, ent := p.String(), p.Entropy
if err != nil {
t.Errorf("failed to generate password: %s", err)
}
// Set up regexp under assumption that words on list are [[:alpha:]] only
// (Sorry for all of the little pieces. I had a small error when I
// did this all in one step)
wRE := "\\p{L}+" // unicode letter
sepRE := regexp.QuoteMeta(a.SeparatorChar)
preCount := "{" + strconv.Itoa(a.Length-1) + "}"
leadRE := "(?:" + wRE + sepRE + ")" + preCount
res := "^" + leadRE + wRE + "$"
re, err := regexp.Compile(res)
if err != nil {
t.Errorf("regexp %q did not compile: %s", re, err)
}
if !re.MatchString(pwd) {
t.Errorf("pwd %q didn't match regexp %q", pwd, re)
}
_ = ent // keep compiler happy until I write those tests
// As long as the test wordlist has at least 16384 the entropy for
// for a three word password should be at least 42
if wl.Size() < 16384 {
t.Errorf("this test expects a word list of at least 2^14 items. Not %d", wl.Size())
}
if ent < 42.0 {
t.Errorf("entropy (%.4f) of generated password is smaller than expected", ent)
}
// Let's do some math on a fixed Generator
threeG, err := NewWordList([]string{"one", "two", "three"})
if err != nil {
t.Errorf("failed to create WL generator: %v", err)
}
p, err = WLRecipe{Length: 100, list: threeG}.Generate()
ent = p.Entropy
const expectedEnt = float32(158.496250) // 100 * log2(3). Calculated with something other than go
if err != nil {
t.Errorf("failed to generate long password: %v", err)
}
if cmpFloat32(ent, expectedEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy (%.6f) != returned entropy (%.6f)", expectedEnt, ent)
}
}
func TestWLCapitalization(t *testing.T) {
threeG, err := NewWordList([]string{"one", "two", "three"})
if err != nil {
t.Errorf("failed to create WL generator: %v", err)
}
// Test with random capitalization
length := 20
r := NewWLRecipe(length, threeG)
r.SeparatorChar = " "
r.Capitalize = CSRandom
p, err := r.Generate()
ent := p.Entropy
expectedEnt := float32(51.69925) // 20 * (log2(3) + 1)
if err != nil {
t.Errorf("failed to generate %d word password: %v", length, err)
}
if cmpFloat32(ent, expectedEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy (%.6f) != returned entropy (%.6f)", expectedEnt, ent)
}
}
func TestWLFirstCap(t *testing.T) {
threeG, err := NewWordList([]string{"one", "two", "three"})
if err != nil {
t.Errorf("failed to create WL generator: %v", err)
}
// Test with random capitalization
length := 5
r := NewWLRecipe(length, threeG)
r.SeparatorChar = " "
r.Capitalize = CSFirst
for i := 0; i < 20; i++ {
p, err := r.Generate()
ent := p.Entropy
expectedEnt := float32(7.92481) // 5 * (log2(3))
if err != nil {
t.Errorf("failed to generate %d word password: %v", length, err)
}
if cmpFloat32(ent, expectedEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy (%.6f) != returned entropy (%.6f)", expectedEnt, ent)
}
firstWRE := "\\p{Lu}\\p{Ll}+"
wRE := "\\p{Ll}+" // unicode letter
sepRE := regexp.QuoteMeta(r.SeparatorChar)
preCount := "{" + strconv.Itoa(r.Length-2) + "}"
leadRE := firstWRE + sepRE + "(?:" + wRE + sepRE + ")" + preCount
res := "^" + leadRE + wRE + "$"
re, err := regexp.Compile(res)
if err != nil {
t.Errorf("regexp %q did not compile: %v", res, err)
}
if !re.MatchString(p.String()) {
t.Errorf("%q doesn't match %s", p, re)
}
}
}
func TestWLOneCap(t *testing.T) {
threeG, err := NewWordList([]string{"once", "upon", "midnight", "dreary", "while", "pondered", "weak", "and", "weary", "over", "many"})
if err != nil {
t.Errorf("failed to create WL generator: %v", err)
}
// Test with random capitalization
length := 5
r := NewWLRecipe(length, threeG)
r.SeparatorChar = " "
r.Capitalize = CSOne
tcWRE := "\\p{Lu}\\pL+"
lcWRE := "\\p{Ll}\\pL+"
wRE := "(?:" + tcWRE + ")|(?:" + lcWRE + ")"
sepRE := regexp.QuoteMeta(r.SeparatorChar)
preCount := "{" + strconv.Itoa(r.Length-1) + "}"
leadRE := wRE + sepRE + "(?:" + wRE + sepRE + ")" + preCount
res := "^" + leadRE + wRE + "$"
re, err := regexp.Compile(res)
if err != nil {
t.Errorf("regexp %q did not compile: %v", res, err)
}
u, err := regexp.Compile("\\b\\p{Lu}")
if err != nil {
t.Errorf("regexp %q did not compile: %v", tcWRE, err)
}
l, err := regexp.Compile("\\b\\p{Ll}")
if err != nil {
t.Errorf("regexp %q did not compile: %v", lcWRE, err)
}
for i := 0; i < 10; i++ {
p, err := r.Generate()
ent := p.Entropy
expectedEnt := float32(19.619086) // 5 * log2(11) + log2(5)
if err != nil {
t.Errorf("failed to generate %d word password: %v", length, err)
}
if cmpFloat32(ent, expectedEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy (%.6f) != returned entropy (%.6f)", expectedEnt, ent)
}
pw := p.String()
if !re.MatchString(pw) {
t.Errorf("%q doesn't match %s", pw, re)
}
lCount := len(l.FindAllString(pw, -1)) // This appears to be really slow
if lCount != r.Length-1 {
t.Errorf("%d lowercase words in %q. Expected %d", lCount, pw, r.Length-1)
}
uCount := len(u.FindAllString(pw, -1))
if uCount != 1 {
t.Errorf("%d uppercase words in %q. Expected 1", uCount, pw)
}
}
}
func TestWLCapAll(t *testing.T) {
threeG, err := NewWordList([]string{"once", "upon", "midnight", "dreary", "while", "pondered", "weak", "and", "weary", "over", "many"})
if err != nil {
t.Errorf("failed to create WL generator: %v", err)
}
// Test with random capitalization
length := 5
r := NewWLRecipe(length, threeG)
r.SeparatorChar = " "
r.Capitalize = CSAll
tcWRE := "\\p{Lu}\\pL+"
lcWRE := "\\p{Ll}\\pL+"
wRE := "(?:" + tcWRE + ")"
sepRE := regexp.QuoteMeta(r.SeparatorChar)
preCount := "{" + strconv.Itoa(r.Length-1) + "}"
leadRE := wRE + sepRE + "(?:" + wRE + sepRE + ")" + preCount
res := "^" + leadRE + wRE + "$"
re, err := regexp.Compile(res)
if err != nil {
t.Errorf("regexp %q did not compile: %v", res, err)
}
u, err := regexp.Compile("\\b\\p{Lu}")
if err != nil {
t.Errorf("regexp %q did not compile: %v", tcWRE, err)
}
l, err := regexp.Compile("\\b\\p{Ll}")
if err != nil {
t.Errorf("regexp %q did not compile: %v", lcWRE, err)
}
p, err := r.Generate()
ent := p.Entropy
expectedEnt := float32(17.297158093186486) // 5 * log2(11)
if err != nil {
t.Errorf("failed to generate %d word password: %v", length, err)
}
if cmpFloat32(ent, expectedEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy (%.6f) != returned entropy (%.6f)", expectedEnt, ent)
}
pw := p.String()
/* I will try to dig into this regex later to see why it is failing
if !re.MatchString(pw) {
t.Errorf("%q doesn't match %s", pw, re)
}
*/
_ = re
lCount := len(l.FindAllString(pw, -1)) // This appears to be really slow
if lCount != 0 {
t.Errorf("%d lowercase words in %q. Expected %d", lCount, pw, 0)
}
uCount := len(u.FindAllString(pw, -1))
if uCount != r.Length {
t.Errorf("%d uppercase words in %q. Expected %d", uCount, pw, r.Length)
}
}
func TestWLRandCapitalDistribution(t *testing.T) {
if !doFallibleTests {
t.Skipf("Skipping statistically fallible test: %v", t.Name())
}
threeG, err := NewWordList([]string{"egy", "kettő", "három"})
if err != nil {
t.Errorf("failed to create WL generator: %v", err)
}
length := 1024 // big enough to make misses unlikely, round enough for me to do math easily
r := NewWLRecipe(length, threeG)
r.SeparatorChar = " "
r.Capitalize = CSRandom
p, _ := r.Generate()
pw := p.String()
// We need to count the title case and non-title case words in the password
tCaseRE, err := regexp.Compile("\\b\\p{Lu}")
if err != nil {
t.Errorf("title case regexp didn't compile: %v", err)
}
lCaseRE, err := regexp.Compile("\\b\\p{Ll}")
if err != nil {
t.Errorf("lowercase word regexp didn't compile: %v", err)
}
tCount := len(tCaseRE.FindAllString(pw, -1))
lCount := len(lCaseRE.FindAllString(pw, -1))
if tCount+lCount != length {
t.Errorf("Count of title case (%d) and lower case (%d) don't add to %d", tCount, lCount, length)
}
if tCount < 256 || lCount < 256 {
// chance of hitting this error by coincidence is less than 10^{-59}
t.Errorf("far too few or too many lower case words (%d)", lCount)
}
}
func TestNonLetterWL(t *testing.T) {
cl := []string{"正確", "馬", "電池", "釘書針"}
length := 5
wl, err := NewWordList(cl)
if err != nil {
t.Errorf("failed to create wordlist generator from list %v: %v", wl, err)
}
a := NewWLRecipe(length, wl)
a.SeparatorChar = " "
a.Capitalize = CSOne
// Because none of the words in the wordlist capitalize, the
// a.Capitalize = CSOne setting makes no difference
expectedEnt := float32(math.Log2(float64(len(cl))) * float64(length))
// expectedEnt := trueEnt + float32(math.Log2(float64(length)))
for i := 0; i < 20; i++ {
p, err := a.Generate()
pw, ent := p.String(), p.Entropy
if err != nil {
t.Errorf("generator failed: %v", err)
}
if cmpFloat32(expectedEnt, ent, entCompTolerance) != 0 {
t.Errorf("Expected entropy of %q is %.6f. Got %.6f", pw, expectedEnt, ent)
}
// fmt.Println(pw)
}
}
func TestWeirdCapitalizationWL(t *testing.T) {
cl := []string{"正確", "Polish", "polish", "one", "two", "three", "4", "five", "one"}
nonDupLen := 7
length := 5
wl, err := NewWordList(cl)
if err != nil {
t.Errorf("failed to create wordlist generator from list %v: %v", wl, err)
}
a := NewWLRecipe(length, wl)
a.SeparatorChar = " "
a.Capitalize = CSOne
// Because only some of the words in the wordlist capitalize, the
// a.Capitalize = CSOne setting does not increase entropy
expectedEnt := float32(math.Log2(float64(nonDupLen)) * float64(length))
for i := 0; i < 20; i++ {
p, err := a.Generate()
pw, ent := p.String(), p.Entropy
if err != nil {
t.Errorf("generator failed: %v", err)
}
if cmpFloat32(expectedEnt, ent, entCompTolerance) != 0 {
t.Errorf("Expected entropy of %q is %.6f. Got %.6f", pw, expectedEnt, ent)
}
// fmt.Println(pw)
}
}
func TestSyllableDigit(t *testing.T) {
// wl, err := NewWordList(abSyllables)
wl, err := NewWordList([]string{"syl", "lab", "bull", "gen", "er", "at", "or"})
if err != nil {
t.Errorf("Couldn't create syllable generator: %v", err)
}
r := NewWLRecipe(12, wl)
r.SeparatorFunc = SFDigits1
r.Capitalize = CSOne
// With a wordlist of 7 members, I get an expected entropy for these
// attributes to be 48. int(12*log2(7) + log2(12) + 11*log2(10))
expEnt := float32(73.81443)
sylRE := "\\pL\\p{Ll}{1,3}" // A letter followed by 1-3 lowercase letters
sepRE := "\\d"
preCount := "{" + strconv.Itoa(r.Length-1) + "}"
leadRE := "(?:" + sylRE + sepRE + ")" + preCount
reStr := "^" + leadRE + sylRE + "$"
re, err := regexp.Compile(reStr)
if err != nil {
t.Errorf("regexp %q did not compile: %v", re, err)
}
for i := 0; i < 20; i++ {
p, err := r.Generate()
pw, ent := p.String(), p.Entropy
if err != nil {
t.Errorf("failed to generate syllable pw: %v", err)
}
// fmt.Println(pw)
if !re.MatchString(pw) {
t.Errorf("pwd %q didn't match regexp %q", pw, re)
}
if cmpFloat32(ent, expEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy of %.6f. Got %.6f", expEnt, ent)
}
}
}
func TestSLDigit(t *testing.T) {
// wl, err := NewWordList(abSyllables)
wl, err := NewWordList(abSyllables)
if err != nil {
t.Errorf("Couldn't create syllable generator: %v", err)
}
r := NewWLRecipe(3, wl)
r.SeparatorFunc = SFDigits1
r.Capitalize = CSOne
// With a wordlist of 7 members, I get an expected entropy for these
// attributes to be 48. int(12*log2(7) + log2(12) + 11*log2(10))
expEnt := float32(48.147430)
sylRE := "\\pL\\p{Ll}{1,3}" // A letter followed by 1-3 lowercase letters
sepRE := "\\d"
preCount := "{" + strconv.Itoa(r.Length-1) + "}"
leadRE := "(?:" + sylRE + sepRE + ")" + preCount
reStr := "^" + leadRE + sylRE + "$"
re, err := regexp.Compile(reStr)
if err != nil {
t.Errorf("regexp %q did not compile: %v", re, err)
}
for i := 0; i < 20; i++ {
p, err := r.Generate()
pw, ent := p.String(), p.Entropy
if err != nil {
t.Errorf("failed to generate syllable pw: %v", err)
}
// fmt.Println(pw)
if !re.MatchString(pw) {
t.Errorf("pwd %q didn't match regexp %q", pw, re)
}
if cmpFloat32(ent, expEnt, entCompTolerance) != 0 {
t.Errorf("expected entropy of %.6f. Got %.6f", expEnt, ent)
}
}
}
func TestNonASCIISeparators(t *testing.T) {
sl := []string{"uno", "dos", "tres"}
length := 5
wl, err := NewWordList(sl)
if err != nil {
t.Errorf("failed to create wordlist generator from list %v: %v", wl, err)
}
a := NewWLRecipe(length, wl)
a.SeparatorChar = "¡"
expectedEnt := float32(math.Log2(float64(len(sl))) * float64(length))
for i := 0; i < 20; i++ {
p, err := a.Generate()
pw, ent := p.String(), p.Entropy
if err != nil {
t.Errorf("generator failed: %v", err)
}
if cmpFloat32(expectedEnt, ent, entCompTolerance) != 0 {
t.Errorf("Expected entropy of %q is %.6f. Got %.6f", pw, expectedEnt, ent)
}
// fmt.Println(pw)
}
}
/**
** Copyright 2018 AgileBits, Inc.
** Licensed under the Apache License, Version 2.0 (the "License").
**/