-
Notifications
You must be signed in to change notification settings - Fork 10
/
otp_test.go
68 lines (57 loc) · 1.45 KB
/
otp_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
package otpgo
import (
"testing"
"github.com/jltorresm/otpgo/config"
)
func TestGenerateOTP(t *testing.T) {
cases := []struct {
label string
key string
expectedOtp string
}{
{"Pad", "4LOWUKIZK2YA=======", "551709"},
{"No Pad", "4LOWUKIZK2YA", "551709"},
{"Pad 2", "NAZXS===", "967352"},
{"No Pad 2", "NAZXS", "967352"},
}
for _, c := range cases {
t.Run(c.label, func(t *testing.T) {
otp, err := generateOTP(c.key, 1, config.Length6, config.HmacSHA1)
if c.expectedOtp != otp {
t.Errorf("unexpected otp\nexpected: %s\n actual: %s", c.expectedOtp, otp)
}
if err != nil {
t.Errorf("unexpected error: %s", err)
}
})
}
}
func TestRandomKey(t *testing.T) {
cases := []struct {
label string
length uint
expectedLength int
expectedError error
}{
{"Zero", 0, 0, nil},
{"One", 1, 2, nil},
{"Three", 3, 5, nil},
{"Small", 10, 16, nil},
{"Normal", 64, 103, nil},
{"Big", 1024, 1639, nil},
}
for _, c := range cases {
t.Run(c.label, func(t *testing.T) {
key, err := randomKey(c.length)
if c.expectedLength != len(key) {
t.Errorf("unexpected key length\nexpected: %d\n actual: %d", c.expectedLength, len(key))
}
if c.expectedError != err {
t.Errorf("unexpected error\nexpected: %s\n actual: %s", c.expectedError, err)
}
if len(key) > 0 && key[len(key)-1:] == "=" {
t.Errorf("unexpected padding, expected the key to have no padding")
}
})
}
}