-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotp_test.go
More file actions
60 lines (52 loc) · 2.1 KB
/
totp_test.go
File metadata and controls
60 lines (52 loc) · 2.1 KB
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
package main
import (
"fmt"
"testing"
)
func TestTOTP(t *testing.T) {
lengths := [2]int{ 6, 8 }
var tests = []struct {
secret string
time int64
algorithm Algorithm
code string
}{
{"12345678901234567890", 59, SHA1, "94287082"},
{"12345678901234567890123456789012", 59, SHA256, "46119246"},
{"1234567890123456789012345678901234567890123456789012345678901234", 59, SHA512, "90693936"},
{"12345678901234567890", 1111111109, SHA1, "07081804"},
{"12345678901234567890123456789012", 1111111109, SHA256, "68084774"},
{"1234567890123456789012345678901234567890123456789012345678901234", 1111111109, SHA512, "25091201"},
{"12345678901234567890", 1111111111, SHA1, "14050471"},
{"12345678901234567890123456789012", 1111111111, SHA256, "67062674"},
{"1234567890123456789012345678901234567890123456789012345678901234", 1111111111, SHA512, "99943326"},
{"12345678901234567890", 1234567890, SHA1, "89005924"},
{"12345678901234567890123456789012", 1234567890, SHA256, "91819424"},
{"1234567890123456789012345678901234567890123456789012345678901234", 1234567890, SHA512, "93441116"},
{"12345678901234567890", 2000000000, SHA1, "69279037"},
{"12345678901234567890123456789012", 2000000000, SHA256, "90698825"},
{"1234567890123456789012345678901234567890123456789012345678901234", 2000000000, SHA512, "38618901"},
{"12345678901234567890", 20000000000, SHA1, "65353130"},
{"12345678901234567890123456789012", 20000000000, SHA256, "77737706"},
{"1234567890123456789012345678901234567890123456789012345678901234", 20000000000, SHA512, "47863826"},
}
for _, tt := range tests {
for _, digits := range lengths {
testname := fmt.Sprintf("time:%d,digits:%d,algorithm:%s", tt.time, digits, tt.algorithm)
t.Run(testname, func(t *testing.T) {
totp := &totp{
algorithm: tt.algorithm,
digits: Digits(digits),
period: 30,
secret: []byte(tt.secret),
}
code := totp.GenerateOTP(tt.time)
// Get last [digits] digits from tt.totp
expectedTOTP := tt.code[len(tt.code)-digits:]
if code != expectedTOTP {
t.Errorf("got %s, want %s", code, expectedTOTP)
}
})
}
}
}