-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotp_test.go
219 lines (199 loc) · 4.84 KB
/
totp_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
package totp
import (
"errors"
"fmt"
"testing"
"time"
)
const (
VALID_SECRET = "KRHVIUC7KRCVGVBB"
)
func TestGenerate(t *testing.T) {
tests := []struct {
secret string
algo Algorithm
digits uint
period uint
}{
{VALID_SECRET, SHA1, 10, 30},
{VALID_SECRET, SHA1, 6, 30},
{VALID_SECRET, SHA256, 6, 30},
{VALID_SECRET, SHA512, 6, 30},
{VALID_SECRET, SHA1, 8, 30},
{VALID_SECRET, SHA1, 6, 60},
}
for _, test := range tests {
name := fmt.Sprintf("%s-%d-%d", test.algo, test.digits, test.period)
t.Run(name, func(t *testing.T) {
totp, err := New(test.secret, &Options{
Algorithm: test.algo,
Digits: test.digits,
Period: test.period,
})
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
code := totp.Generate()
if len(code) != int(test.digits) {
t.Errorf("Expected code length of %d, got %d", test.digits, len(code))
}
})
}
}
func TestGenerateInvalidSecret(t *testing.T) {
secret := "1234567890"
_, err := New(secret, nil)
if err != ErrDecodingBase32Secret {
t.Errorf("Expected ErrDecodingBase32Secret, got %v", err)
}
}
func TestGenerateInvalidDigits(t *testing.T) {
secret := VALID_SECRET
_, err := New(secret, &Options{Digits: 11})
if !errors.Is(err, ErrTooManyDigits) {
t.Errorf("Expected ErrTooManyDigits, got %v", err)
}
}
func TestGenerateInvalidAlgorithm(t *testing.T) {
secret := VALID_SECRET
_, err := New(secret, &Options{Algorithm: "FOO"})
if !errors.Is(err, ErrUnknownHashAlgorithm) {
t.Errorf("Expected ErrUnknownHashAlgorithm, got %v", err)
}
}
func TestGenerateAt(t *testing.T) {
totp, err := New(VALID_SECRET, nil)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
tests := []struct {
ts int64
code string
}{
{1703882207, "980239"},
{1703882237, "552606"},
{1703882267, "743299"},
{1703882297, "334300"},
{1703882327, "993457"},
}
for _, test := range tests {
name := fmt.Sprintf("%d", test.ts)
t.Run(name, func(t *testing.T) {
code := totp.GenerateAt(time.Unix(test.ts, 0))
if code != test.code {
t.Errorf("Expected %s, got %s", test.code, code)
}
if len(code) != 6 {
t.Errorf("Expected code length of 6, got %d", len(code))
}
})
}
}
func TestVerify(t *testing.T) {
tests := []struct {
secret string
algo Algorithm
digits uint
period uint
}{
{VALID_SECRET, SHA1, 6, 30},
{VALID_SECRET, SHA256, 6, 30},
{VALID_SECRET, SHA512, 6, 30},
{VALID_SECRET, SHA1, 8, 30},
{VALID_SECRET, SHA1, 6, 60},
}
for _, test := range tests {
name := fmt.Sprintf("%s-%d-%d", test.algo, test.digits, test.period)
t.Run(name, func(t *testing.T) {
totp, err := New(test.secret, &Options{
Algorithm: test.algo,
Digits: test.digits,
Period: test.period,
})
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
code := totp.Generate()
if !totp.Verify(code) {
t.Errorf("Expected verification to pass")
}
})
}
}
func TestVerifyAt(t *testing.T) {
totp, err := New(VALID_SECRET, nil)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
tests := []struct {
code string
ts int64
result bool
}{
{"980239", 1703882207, true},
{"552606", 1703882237, true},
{"743299", 1703882267, true},
{"334300", 1703882297, true},
{"993457", 1703882327, true},
{"123456", 1703882206, false},
}
for _, test := range tests {
name := fmt.Sprintf("%s-%d", test.code, test.ts)
t.Run(name, func(t *testing.T) {
if !totp.VerifyAt(test.code, time.Unix(test.ts, 0)) == test.result {
t.Errorf("Expected verification to be %t", test.result)
}
})
}
}
func TestPadCode(t *testing.T) {
tests := []struct {
code uint64
expected string
digits uint
}{
{0, "000000", 6},
{1, "000001", 6},
{12, "000012", 6},
{123, "000123", 6},
{1234, "001234", 6},
{12345, "012345", 6},
{123456, "123456", 6},
{12345678, "12345678", 8},
{12345678, "0012345678", 10},
{1234567890, "1234567890", 10},
}
for _, test := range tests {
name := fmt.Sprintf("%d-%d", test.code, test.digits)
t.Run(name, func(t *testing.T) {
actual := padCode(test.code, test.digits)
if actual != test.expected {
t.Errorf("Expected %s, got %s", test.expected, actual)
}
})
}
}
func TestDefaultOptions(t *testing.T) {
totp, err := New(VALID_SECRET, nil)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if totp.Digits != 6 {
t.Errorf("Expected default digits to be 6, got %d", totp.Digits)
}
if totp.Period != 30 {
t.Errorf("Expected default period to be 30, got %d", totp.Period)
}
if totp.ALgorithm != SHA1 {
t.Errorf("Expected default algorithm to be SHA1, got %s", totp.ALgorithm)
}
const (
refTS = 1703882207
refCode = "980239"
)
// check if default algorithm is SHA1 using reference value
code := totp.GenerateAt(time.Unix(refTS, 0))
if code != refCode {
t.Errorf("The default algorithm should be SHA1")
}
}