-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpassphrase_test.go
202 lines (179 loc) · 4.4 KB
/
passphrase_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
package atoll
import (
"bytes"
"math"
"testing"
)
func TestPassphrase(t *testing.T) {
cases := map[string]*Passphrase{
"No list": {
Length: 14,
Separator: "/",
List: NoList,
},
"Word list": {
Length: 4,
Separator: "",
Include: []string{"apple", "orange", "watermelon"},
List: WordList,
},
"Syllable list": {
Length: 6,
Separator: "==",
Include: []string{"test"},
List: SyllableList,
},
"Default values": {
Length: 10,
Include: []string{"background"},
Exclude: []string{"unit"},
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
passphrase, err := tc.Generate()
if err != nil {
t.Fatalf("Generate() failed: %v", err)
}
words := bytes.Split(passphrase, []byte(tc.Separator))
if len(words) != int(tc.Length) {
t.Errorf("Expected %d words, got %d", tc.Length, len(words))
}
if !bytes.Contains(passphrase, []byte(tc.Separator)) {
t.Errorf("The separator %q is not used", tc.Separator)
}
for _, inc := range tc.Include {
if !bytes.ContainsAny(passphrase, inc) {
t.Errorf("Expected %q to be included", inc)
}
}
for _, w := range words {
for _, exc := range tc.Exclude {
if exc == string(w) {
t.Errorf("Expected %q to be excluded", exc)
}
}
}
})
}
}
func TestInvalidPassphrase(t *testing.T) {
cases := map[string]*Passphrase{
"invalid length": {Length: 0},
"invalid separator": {Length: 5, Separator: "¿"},
"len(Include) > Length": {Length: 2, Include: []string{"must", "throw", "error"}},
"included words also excluded": {Length: 2, Include: []string{"Go"}, Exclude: []string{"Go"}},
"invalid included word": {Length: 7, Include: []string{"ínvalid"}},
}
for k, tc := range cases {
if _, err := tc.Generate(); err == nil {
t.Errorf("Expected %q error, got nil", k)
}
}
}
func TestNewPassphrase(t *testing.T) {
length := 5
passphrase, err := NewPassphrase(uint64(length), NoList)
if err != nil {
t.Errorf("NewPassphrase() failed: %v", err)
}
words := bytes.Split(passphrase, []byte(" "))
got := len(words)
if got != length {
t.Errorf("Expected %d words, got %d", length, got)
}
}
func TestInvalidNewPassphrase(t *testing.T) {
_, err := NewPassphrase(0, WordList)
if err == nil {
t.Error("Expected \"invalid length\" error, got nil")
}
}
func TestExcludeWords(t *testing.T) {
cases := map[string]*Passphrase{
"No list": {
words: [][]byte{[]byte("cow"), []byte("horse"), []byte("bee")},
Separator: " ",
Exclude: []string{"cow", "horse", "beer"},
List: NoList,
},
"Word list": {
words: [][]byte{[]byte("about"), []byte("abysmal"), []byte("accurate")},
Separator: " ",
Exclude: []string{"about"},
List: WordList,
},
"Syllable list": {
words: [][]byte{[]byte("alt"), []byte("bet"), []byte("bang flux")},
Separator: " ",
Exclude: []string{"alt", "flux"},
List: SyllableList,
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
tc.excludeWords()
for _, exc := range tc.Exclude {
for _, word := range tc.words {
if exc == string(word) {
t.Errorf("Found undesired word %q", exc)
}
}
}
})
}
}
func TestPassphraseEntropy(t *testing.T) {
cases := []struct {
list list
desc string
expected float64
}{
{
desc: "No list",
list: NoList,
},
{
desc: "Word list",
list: WordList,
expected: 56.64641721601138,
},
{
desc: "Syllable list",
list: SyllableList,
expected: 53.225386214754,
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
p := &Passphrase{
Length: 4,
List: tc.list,
Separator: "/",
Include: []string{"atoll"},
}
p.Generate()
// NoList entropy changes everytime as it generates random words
if getFuncName(tc.list) == "NoList" {
secretLength := len(bytes.Join(p.words, []byte(""))) - (len(p.Separator) * int(p.Length))
tc.expected = math.Log2(math.Pow(float64(26), float64(secretLength)))
}
got := p.Entropy()
if got != tc.expected {
t.Errorf("Expected %f, got %f", tc.expected, got)
}
})
}
}
func TestPassphraseEntropyNoSecret(t *testing.T) {
p := &Passphrase{
Length: 7,
List: NoList,
Separator: "/",
}
var expected float64
got := p.Entropy()
if got != expected {
t.Errorf("Expected %f, got %f", expected, got)
}
}