-
Notifications
You must be signed in to change notification settings - Fork 5
/
totp_test.go
155 lines (134 loc) · 3.99 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
package otp
import (
"testing"
"time"
)
const (
secret256 string = "12345678901234567890123456789012"
secret512 string = "1234567890123456789012345678901234567890123456789012345678901234"
)
type hashConfig struct {
secret string
otp string
crypto string
}
func getTimeTestMap() map[time.Time][]hashConfig {
return map[time.Time][]hashConfig{
time.Date(1970, 1, 1, 0, 0, 59, 0, time.UTC): []hashConfig{
hashConfig{defaultSecret, `94287082`, "sha1"},
hashConfig{secret256, `46119246`, "sha256"},
hashConfig{secret512, `90693936`, "sha512"},
},
time.Date(2005, 3, 18, 1, 58, 29, 0, time.UTC): []hashConfig{
hashConfig{defaultSecret, `07081804`, "sha1"},
hashConfig{secret256, `68084774`, "sha256"},
hashConfig{secret512, `25091201`, "sha512"},
},
time.Date(2005, 3, 18, 1, 58, 31, 0, time.UTC): []hashConfig{
hashConfig{defaultSecret, `14050471`, "sha1"},
hashConfig{secret256, `67062674`, "sha256"},
hashConfig{secret512, `99943326`, "sha512"},
},
time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC): []hashConfig{
hashConfig{defaultSecret, `89005924`, "sha1"},
hashConfig{secret256, `91819424`, "sha256"},
hashConfig{secret512, `93441116`, "sha512"},
},
time.Date(2033, 5, 18, 3, 33, 20, 0, time.UTC): []hashConfig{
hashConfig{defaultSecret, `69279037`, "sha1"},
hashConfig{secret256, `90698825`, "sha256"},
hashConfig{secret512, `38618901`, "sha512"},
},
time.Date(2603, 10, 11, 11, 33, 20, 0, time.UTC): []hashConfig{
hashConfig{defaultSecret, `65353130`, "sha1"},
hashConfig{secret256, `77737706`, "sha256"},
hashConfig{secret512, `47863826`, "sha512"},
},
}
}
func TestGenerateTotp(t *testing.T) {
t.Parallel()
timeMap := getTimeTestMap()
for tm, otpHashMap := range timeMap {
for _, hc := range otpHashMap {
c := TotpConfig{Secret: hc.secret, Time: tm, Crypto: hc.crypto}
otp := NewTOTP(&c)
rOtp := otp.Generate()
if rOtp != hc.otp {
t.Errorf("Expected %v but got %v", hc.otp, rOtp)
}
}
}
}
func TestCheckTotp_validProgression(t *testing.T) {
t.Parallel()
timeMap := getTimeTestMap()
for tm, otpHashMap := range timeMap {
for _, hc := range otpHashMap {
c := TotpConfig{Secret: hc.secret, Time: tm, Crypto: hc.crypto}
otp := NewTOTP(&c)
isValid := otp.Check(hc.otp)
if !isValid {
t.Errorf("Expected %v to be valid but was %v", hc.otp, isValid)
}
// Base32 tests
otp32 := NewTOTP(&c)
isValid32 := otp32.Check(hc.otp)
if !isValid32 {
t.Errorf("Expected base32 encoded %v to be valid but was %v", hc.otp, isValid32)
}
}
}
}
func TestCheckTotp_invalid(t *testing.T) {
t.Parallel()
c := TotpConfig{
Secret: defaultSecret,
Time: time.Date(1970, 1, 1, 0, 0, 59, 0, time.UTC),
}
otp := NewTOTP(&c)
isValid := otp.Check("12345678")
if isValid {
t.Errorf("Expected %v to be false but was 94287082", isValid)
}
}
func TestNewTOTP_customConfig(t *testing.T) {
t.Parallel()
testTime := time.Date(1970, 1, 1, 0, 0, 59, 0, time.UTC)
cConfig := TotpConfig{
Secret: "secret",
Time: testTime,
Length: 10,
Window: 45,
WindowSize: 3,
UseBase32: true,
}
cToken := NewTOTP(&cConfig)
if cToken.Secret != "secret" ||
cToken.TimeBox != testTime ||
cToken.Length != 10 ||
cToken.Window != 45 ||
cToken.WindowSize != 3 ||
!cToken.IsBase32 {
t.Errorf("NewTOTP (custom) returned an object with unexpected properties %+v", cToken)
}
}
func TestNewTOTP_defaultConfig(t *testing.T) {
t.Parallel()
testTime := time.Now()
dConfig := TotpConfig{}
dToken := NewTOTP(&dConfig)
isTimeSimilar := testTime.Minute() == dToken.TimeBox.Minute() &&
testTime.Hour() == dToken.TimeBox.Hour() &&
testTime.Day() == dToken.TimeBox.Day() &&
testTime.Month() == dToken.TimeBox.Month() &&
testTime.Year() == dToken.TimeBox.Year()
if len(dToken.Secret) != 20 ||
!isTimeSimilar ||
dToken.Length != 8 ||
dToken.Window != 30 ||
dToken.WindowSize != 2 ||
dToken.IsBase32 {
t.Errorf("NewTOTP (default) returned an object with unexpected properties %+v", dToken)
}
}