-
Notifications
You must be signed in to change notification settings - Fork 10
/
hotp_test.go
292 lines (231 loc) · 6.45 KB
/
hotp_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
package otpgo
import (
"encoding/json"
"testing"
"github.com/jltorresm/otpgo/config"
)
func TestHOTP_Generate(t *testing.T) {
t.Run("Normal Generation", testHOTPNormalGeneration)
t.Run("Bad Key", testHOTPBadKey)
t.Run("Default Params", testHOTPDefaultParams)
t.Run("Autogenerated Key", testHOTPAutogeneratedKey)
t.Run("Lower Case Key", testHOTPLowerCaseKey)
}
func testHOTPNormalGeneration(t *testing.T) {
t.Parallel()
h := &HOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUP",
Counter: 363,
Algorithm: config.HmacSHA256,
Length: config.Length6,
}
expectedOtp := "363033"
otp, err := h.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if otp != expectedOtp {
t.Errorf("wrong hotp\nexpected: %s\n actual: %s", expectedOtp, otp)
}
}
func testHOTPBadKey(t *testing.T) {
t.Parallel()
h := &HOTP{
Key: "invalid-base-32",
Counter: 363,
Algorithm: config.HmacSHA256,
Length: config.Length6,
}
_, err := h.Generate()
expectedErr := ErrorInvalidKey{msg: "illegal base32 data at input byte 7"}
if err != expectedErr {
t.Errorf("unexpected error: %s", err)
}
}
func testHOTPDefaultParams(t *testing.T) {
t.Parallel()
h := &HOTP{Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ"}
expectedLeeway := HOTPDefaultLeeway
expectedAlg := config.HmacSHA1
expectedLength := config.Length6
expectedOtp := "769784"
otp, err := h.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if h.Leeway != expectedLeeway {
t.Errorf("unexpected hash algorithm\nexpected: %d (SHA1)\n actual: %d", expectedLeeway, h.Leeway)
}
if h.Algorithm != expectedAlg {
t.Errorf("unexpected hash algorithm\nexpected: %d (SHA1)\n actual: %d", expectedAlg, h.Algorithm)
}
if h.Length != expectedLength {
t.Errorf("unexpected length\nexpected: %d\n actual: %d", expectedLength, h.Length)
}
if otp != expectedOtp {
t.Errorf("unexpected hotp\nexpected: %s\n actual: %s", expectedOtp, otp)
}
}
func testHOTPAutogeneratedKey(t *testing.T) {
t.Parallel()
h := &HOTP{}
_, err := h.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if h.Key == "" {
t.Error("expected Key to be populated")
}
}
func testHOTPLowerCaseKey(t *testing.T) {
t.Parallel()
h := &HOTP{Key: "73qk7d3a3piz6nuqqbf4bnfyqbrvuhuq"}
_, err := h.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if h.Key == "" {
t.Error("expected Key to be populated")
}
}
func TestHOTP_Validate(t *testing.T) {
t.Run("Success", testHOTPValidateSuccess)
t.Run("Failure", testHOTPValidateFailure)
t.Run("Look Ahead Validation", testHOTPValidateLeeway)
t.Run("Missing Key", testHOTPValidateMissingKey)
}
func testHOTPValidateSuccess(t *testing.T) {
t.Parallel()
h := &HOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Counter: 363,
Algorithm: config.HmacSHA256,
Length: config.Length6,
}
// Corresponds to h.Counter = 363. Successful validation will increase the
// internal h.Counter, so that the next generated code corresponds to
// h.Counter = 364.
expectedOTP := "561655"
isValid, err := h.Validate(expectedOTP)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !isValid {
t.Errorf("invalid token\nexpected %s to be valid", expectedOTP)
}
otp, err := h.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if otp == expectedOTP {
t.Error("invalid token\nexpected generated token to be different after successful validation")
}
isValid, err = h.Validate(otp)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !isValid {
t.Errorf("invalid token\nexpected %s to be valid", otp)
}
}
func testHOTPValidateFailure(t *testing.T) {
t.Parallel()
invalidOTP := "1111111"
h := &HOTP{Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ", Length: config.Length8}
isValid, err := h.Validate(invalidOTP)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if isValid {
t.Errorf("unexpected valid token\nexpected %s to be invalid", invalidOTP)
}
}
func testHOTPValidateLeeway(t *testing.T) {
t.Parallel()
h := &HOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUT",
Counter: 362,
Leeway: 2,
Algorithm: config.HmacSHA512,
Length: config.Length7,
}
cases := []struct {
label string
modifier int
shouldBeValid bool
}{
{"Correct Step", 0, true},
{"One Step Behind", -1, true},
{"One Step Ahead", 1, true},
{"Two Step Behind", -2, true},
{"Two Step Ahead", +2, true},
{"Three Step Behind", -3, false},
{"Three Step Ahead", 3, false},
}
for _, c := range cases {
t.Run(c.label, func(t *testing.T) {
h.Counter = uint64(int(h.Counter) + c.modifier)
otp, err := h.Generate()
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
// Return to original counter before validation
h.Counter = uint64(int(h.Counter) - c.modifier)
isValid, err := h.Validate(otp)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if isValid != c.shouldBeValid {
t.Errorf("unexpected result from Validate()\nexpected %s to be %v", c.label, c.shouldBeValid)
}
})
}
}
func testHOTPValidateMissingKey(t *testing.T) {
h := &HOTP{}
isValid, err := h.Validate("irrelevant")
if err == nil {
t.Error("expected error")
t.FailNow()
}
if err.Error() != "missing secret key for validation" {
t.Errorf("unexpected error: %s", err)
}
if isValid {
t.Errorf("token should be invalid")
}
}
func TestHOTP_KeyUri(t *testing.T) {
h := HOTP{
Key: "JOC773H4BTUR5U6M422M2AT7S4MTQ7BLR75Y252JK3A",
Counter: 759,
Algorithm: config.HmacSHA256,
}
_, _ = h.Generate()
accountName := "jòhn.doe@example.com"
issuer := "Acme Inc"
expectedUri := "otpauth://hotp/Acme%20Inc:j%C3%B2hn.doe@example.com?algorithm=SHA256&counter=759&digits=6&issuer=Acme+Inc&secret=JOC773H4BTUR5U6M422M2AT7S4MTQ7BLR75Y252JK3A"
uri := h.KeyUri(accountName, issuer)
if expectedUri != uri.String() {
t.Errorf("unexpected key URI\nexpected: %s\n actual: %s", expectedUri, uri.String())
}
}
func TestHOTPJson(t *testing.T) {
h := HOTP{
Key: "73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ",
Counter: 9338,
Leeway: 1,
Algorithm: config.HmacSHA256,
Length: config.Length7,
}
expectedJson := `{"key":"73QK7D3A3PIZ6NUQQBF4BNFYQBRVUHUQ","counter":9338,"leeway":1,"algorithm":"SHA256","length":7}`
j, err := json.Marshal(h)
if err != nil {
t.Errorf("unexpected error: %s", err)
t.FailNow()
}
if expectedJson != string(j) {
t.Errorf("unexpected json:\nexpected: %s\n actual: %s", expectedJson, j)
}
}