-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotp_test.go
117 lines (99 loc) · 2.68 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
package gotp
// spell-checker:disable
import (
"testing"
"github.com/stretchr/testify/assert"
)
var rfc4226TestSecret = []byte("12345678901234567890")
func getDefaultHOTP(t *testing.T) *HOTP {
secret, err := DecodeBase32("4S62BZNFXXSZLCRO")
assert.NoError(t, err)
hotp, err := NewHOTP(secret)
assert.NoError(t, err)
return hotp
}
func TestHOTP_WithError(t *testing.T) {
_, err := NewHOTP([]byte{}, WithLength(-1))
assert.Error(t, err)
}
func TestHOTP_At(t *testing.T) {
hotp := getDefaultHOTP(t)
otp, err := hotp.At(12345)
assert.NoError(t, err, "OTP generation failed")
assert.Equal(t, "194001", otp)
}
func TestHOTP_Verify(t *testing.T) {
hotp := getDefaultHOTP(t)
valid, err := hotp.Verify("194001", 12345)
assert.NoError(t, err, "OTP verify failed")
assert.True(t, valid)
}
func TestHOTP_Hex(t *testing.T) {
secret, err := DecodeBase32("KZOSZD7X6RG7HWZUQI2KBJULFU")
assert.NoError(t, err)
otpHex, err := NewHOTP(secret, WithLength(8), FormatHex())
assert.NoError(t, err)
otp, err := otpHex.At(0)
assert.NoError(t, err, "OTP generation failed")
assert.Equal(t, "07a45595", otp)
}
func TestHOTP_HexFive(t *testing.T) {
secret, err := DecodeBase32("KZOSZD7X6RG7HWZUQI2KBJULFU")
assert.NoError(t, err)
otpHex, err := NewHOTP(secret, WithLength(5), FormatHex())
assert.NoError(t, err)
otp, err := otpHex.At(0)
assert.NoError(t, err, "OTP generation failed")
assert.Equal(t, "07a45", otp)
}
func TestHOTP_RFCTestValues(t *testing.T) {
otpDec, err := NewHOTP(rfc4226TestSecret)
assert.NoError(t, err)
// Expected results from https://tools.ietf.org/html/rfc4226#page-32
expectedResults := []string{
"755224",
"287082",
"359152",
"969429",
"338314",
"254676",
"287922",
"162583",
"399871",
"520489",
}
for i, expectedResult := range expectedResults {
otp, err := otpDec.At(i)
assert.NoError(t, err, "OTP generation failed")
assert.Equal(t, expectedResult, otp)
}
}
func TestHOTP_HexRFCTestValues(t *testing.T) {
otpHex, err := NewHOTP(rfc4226TestSecret, WithLength(8), FormatHex())
assert.NoError(t, err)
// Expected results from https://tools.ietf.org/html/rfc4226#page-32
expectedResults := []string{
"4c93cf18",
"41397eea",
"082fef30",
"66ef7655",
"61c5938a",
"33c083d4",
"7256c032",
"04e5b397",
"2823443f",
"2679dc69",
}
for i, expectedResult := range expectedResults {
otp, err := otpHex.At(i)
assert.NoError(t, err, "OTP generation failed")
assert.Equal(t, expectedResult, otp)
}
}
func TestHOTP_VerifyError(t *testing.T) {
brokenHasher := &Hasher{HashName: "broken", Digest: NewHash}
otp, err := NewHOTP(defaultTestSecret, WithHasher(brokenHasher))
assert.NoError(t, err)
_, err = otp.Verify("", 0)
assert.Error(t, err)
}