-
Notifications
You must be signed in to change notification settings - Fork 12
/
otpgen_test.go
90 lines (81 loc) · 5.62 KB
/
otpgen_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
package otpgen
import (
"encoding/base32"
"testing"
)
type TOTPTest struct {
TOTP
Output string
}
type HOTPTest struct {
HOTP
Output string
}
func TestTOTP(t *testing.T) {
testcases := []TOTPTest{
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 59, Secret: "12345678901234567890", Digits: 8}, Output: "94287082"},
{TOTP: TOTP{Algorithm: "SHA256", Period: 30, UnixTime: 59, Secret: "12345678901234567890123456789012", Digits: 8}, Output: "46119246"},
{TOTP: TOTP{Algorithm: "SHA512", Period: 30, UnixTime: 59, Secret: "1234567890123456789012345678901234567890123456789012345678901234", Digits: 8}, Output: "90693936"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 1111111109, Secret: "12345678901234567890", Digits: 8}, Output: "07081804"},
{TOTP: TOTP{Algorithm: "SHA256", Period: 30, UnixTime: 1111111109, Secret: "12345678901234567890123456789012", Digits: 8}, Output: "68084774"},
{TOTP: TOTP{Algorithm: "SHA512", Period: 30, UnixTime: 1111111109, Secret: "1234567890123456789012345678901234567890123456789012345678901234", Digits: 8}, Output: "25091201"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 1111111111, Secret: "12345678901234567890", Digits: 8}, Output: "14050471"},
{TOTP: TOTP{Algorithm: "SHA256", Period: 30, UnixTime: 1111111111, Secret: "12345678901234567890123456789012", Digits: 8}, Output: "67062674"},
{TOTP: TOTP{Algorithm: "SHA512", Period: 30, UnixTime: 1111111111, Secret: "1234567890123456789012345678901234567890123456789012345678901234", Digits: 8}, Output: "99943326"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 1234567890, Secret: "12345678901234567890", Digits: 8}, Output: "89005924"},
{TOTP: TOTP{Algorithm: "SHA256", Period: 30, UnixTime: 1234567890, Secret: "12345678901234567890123456789012", Digits: 8}, Output: "91819424"},
{TOTP: TOTP{Algorithm: "SHA512", Period: 30, UnixTime: 1234567890, Secret: "1234567890123456789012345678901234567890123456789012345678901234", Digits: 8}, Output: "93441116"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 2000000000, Secret: "12345678901234567890", Digits: 8}, Output: "69279037"},
{TOTP: TOTP{Algorithm: "SHA256", Period: 30, UnixTime: 2000000000, Secret: "12345678901234567890123456789012", Digits: 8}, Output: "90698825"},
{TOTP: TOTP{Algorithm: "SHA512", Period: 30, UnixTime: 2000000000, Secret: "1234567890123456789012345678901234567890123456789012345678901234", Digits: 8}, Output: "38618901"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 20000000000, Secret: "12345678901234567890", Digits: 8}, Output: "65353130"},
{TOTP: TOTP{Algorithm: "SHA256", Period: 30, UnixTime: 20000000000, Secret: "12345678901234567890123456789012", Digits: 8}, Output: "77737706"},
{TOTP: TOTP{Algorithm: "SHA512", Period: 30, UnixTime: 20000000000, Secret: "1234567890123456789012345678901234567890123456789012345678901234", Digits: 8}, Output: "47863826"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 0, UnixTime: 20000000000, Secret: "12345678901234567890", Digits: 8}, Output: "65353130"},
{TOTP: TOTP{Algorithm: "", Period: 30, UnixTime: 20000000000, Secret: "12345678901234567890", Digits: 8}, Output: "65353130"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 20000000000, Secret: "12345678901234567890", Digits: 0}, Output: "353130"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 20000000000, Secret: "", Digits: 8}, Output: "no secret key provided"},
{TOTP: TOTP{Algorithm: "SHA1", Period: 30, UnixTime: 20000000000, Secret: "INVALID", Digits: 8}, Output: "bad secret key"},
{TOTP: TOTP{Algorithm: "SHA1024", Period: 0, UnixTime: 20000000000, Secret: "12345678901234567890", Digits: 8}, Output: "invalid algorithm. Please use any one of SHA1/SHA256/SHA512"},
}
for _, test := range testcases {
if test.Secret != "INVALID" {
test.Secret = base32.StdEncoding.EncodeToString([]byte(test.Secret)) // Convert secret to base32
}
otp, err := test.Generate()
if err != nil {
if err.Error() != test.Output {
t.Errorf("Expected error: %v, Received error: %v\n", test.Output, err.Error())
}
} else if otp != test.Output {
t.Errorf("Expected OTP: %v, Received OTP: %v\n", test.Output, otp)
}
}
}
func TestHOTP(t *testing.T) {
testcases := []HOTPTest{
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 0}, Output: "755224"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 1}, Output: "287082"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 2}, Output: "359152"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 3}, Output: "969429"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 4}, Output: "338314"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 5}, Output: "254676"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 6}, Output: "287922"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 7}, Output: "162583"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 8}, Output: "399871"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 6, Counter: 9}, Output: "520489"},
{HOTP: HOTP{Secret: "", Digits: 6, Counter: 9}, Output: "no secret key provided"},
{HOTP: HOTP{Secret: "12345678901234567890", Digits: 0, Counter: 9}, Output: "520489"},
}
for _, test := range testcases {
test.Secret = base32.StdEncoding.EncodeToString([]byte(test.Secret)) // Convert secret to base32
otp, err := test.Generate()
if err != nil {
if err.Error() != test.Output {
t.Errorf("Expected error: %v, Received error: %v\n", test.Output, err.Error())
}
} else if otp != test.Output {
t.Errorf("Expected OTP: %v, Received OTP: %v\n", test.Output, otp)
}
}
}