This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexity_test.go
90 lines (82 loc) · 2.11 KB
/
complexity_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
package extrand
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestComplexity_IsComplexEnough(t *testing.T) {
testlist := []struct {
complexity *Complexity
truevalues []string
falsevalues []string
}{
{
NewComplexity(),
[]string{"1", "-", "a", "A", "ñ", "日本語"},
[]string{},
},
{
NewComplexity(WithLower(), WithMeet()),
[]string{"abc", "abc!"},
[]string{"ABC", "123", "=!$", ""},
},
{
NewComplexity(WithUpper(), WithMeet()),
[]string{"ABC"},
[]string{"abc", "123", "=!$", "abc!", ""},
},
{
NewComplexity(WithDigit(), WithMeet()),
[]string{"123"},
[]string{"abc", "ABC", "=!$", "abc!", ""},
},
{
NewComplexity(WithSpec(), WithMeet()),
[]string{"=!$", "abc!"},
[]string{"abc", "ABC", "123", ""},
},
{
NewComplexity(WithLower(), WithSpec(), WithMeet()),
[]string{"abc!"},
[]string{"abc", "ABC", "123", "=!$", "abcABC123", ""},
},
{
NewComplexity(WithLowerUpperDigit(), WithMeet()),
[]string{"abcABC123"},
[]string{"abc", "ABC", "123", "=!$", "abc!", ""},
},
{
NewComplexity(WithAll()),
[]string{"abcABC123!"},
[]string{"abc", "ABC", "123", "=!$", "abc!", ""},
},
}
for i, test := range testlist {
for ii, val := range test.truevalues {
assert.Truef(t, test.complexity.IsComplexEnough(val), "true, index: %d idx: %d", i, ii)
}
for ii, val := range test.falsevalues {
assert.Falsef(t, test.complexity.IsComplexEnough(val), "false, index: %d idx: %d", i, ii)
}
}
}
func TestComplexity_Generate(t *testing.T) {
const maxCount = 50
const pwdLen = 50
test := func(t *testing.T, opt ...Option) {
c := NewComplexity(opt...)
for i := 0; i < maxCount; i++ {
pwd := c.Generate(pwdLen)
assert.Equal(t, pwdLen, len(pwd))
assert.True(t, c.IsComplexEnough(pwd), "Failed complexities for generated: %s", pwd)
}
}
test(t, WithLower(), WithMeet())
test(t, WithUpper(), WithMeet())
test(t, WithLowerUpper(), WithSpec(), WithMeet())
test(t, WithMeet())
}
func TestDefaultComplexity(t *testing.T) {
s := Generate(10)
require.True(t, IsComplexEnough(s))
}