-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotp_test.go
211 lines (198 loc) · 4.84 KB
/
otp_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
package otp
import (
"encoding/base32"
"encoding/hex"
"net/url"
"strings"
"testing"
)
func TestRandomSecret(t *testing.T) {
tests := []struct {
name string
algo Algorithm
wantBytes int
wantErr bool
}{
{"SHA1", SHA1, 20, false},
{"SHA256", SHA256, 32, false},
{"SHA512", SHA512, 64, false},
{"InvalidAlgo", Algorithm(99), 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
secret, err := RandomSecret(tt.algo)
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error state: got error = %v, wantErr = %v", err, tt.wantErr)
}
if err == nil {
decoded, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(secret)
if err != nil {
t.Fatalf("failed to decode base32: %v", err)
}
if len(decoded) != tt.wantBytes {
t.Errorf("unexpected decoded length: got = %d, want = %d", len(decoded), tt.wantBytes)
}
}
t.Log(secret)
})
}
}
func TestParseOTPAuthURL(t *testing.T) {
tests := []struct {
name string
rawURL string
want *URLParam
wantErr bool
}{
{
name: "Valid TOTP URL with all fields",
rawURL: "otpauth://totp/MyApp:[email protected]?secret=ABCDEF123456&issuer=MyApp&algorithm=SHA256&digits=8&period=45",
want: &URLParam{
Issuer: "MyApp",
AccountName: "[email protected]",
Secret: "ABCDEF123456",
Digits: 8,
Algorithm: SHA256,
Period: 45,
},
},
{
name: "Valid HOTP URL with minimal fields",
rawURL: "otpauth://hotp/Example:[email protected]?secret=XYZ987",
want: &URLParam{
Issuer: "Example",
AccountName: "[email protected]",
Secret: "XYZ987",
Digits: 6,
Algorithm: SHA1,
Period: 30,
},
},
{
name: "Invalid scheme",
rawURL: "http://totp/Example:[email protected]?secret=ABC",
wantErr: true,
},
{
name: "Invalid host",
rawURL: "otpauth://foo/Example:[email protected]?secret=ABC",
wantErr: true,
},
{
name: "Missing label",
rawURL: "otpauth://totp/?secret=ABC",
wantErr: true,
},
{
name: "Invalid digits",
rawURL: "otpauth://totp/Issuer:user?secret=ABC&digits=abc",
wantErr: true,
},
{
name: "Unsupported algorithm",
rawURL: "otpauth://totp/Issuer:user?secret=ABC&algorithm=MD5",
wantErr: true,
},
{
name: "Invalid period",
rawURL: "otpauth://totp/Issuer:user?secret=ABC&period=abc",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := url.Parse(tt.rawURL)
if err != nil {
t.Fatalf("failed to parse input URL: %v", err)
}
got, err := ParseOTPAuthURL(u)
if (err != nil) != tt.wantErr {
t.Fatalf("error mismatch: got %v, wantErr %v", err, tt.wantErr)
}
if err == nil && tt.want != nil {
if got.Issuer != tt.want.Issuer ||
got.AccountName != tt.want.AccountName ||
got.Secret != tt.want.Secret ||
got.Digits != tt.want.Digits ||
got.Algorithm != tt.want.Algorithm ||
got.Period != tt.want.Period {
t.Errorf("parsed output mismatch:\n got %+v\n want %+v", got, tt.want)
}
}
})
}
}
func TestHexInputToOCRA(t *testing.T) {
validHex := hex.EncodeToString([]byte("test"))
tests := []struct {
name string
counter string
challenge string
password string
sessionInfo string
timestamp string
wantErr bool
errContains string
}{
{
name: "all valid",
counter: validHex,
challenge: validHex,
password: validHex,
sessionInfo: validHex,
timestamp: validHex,
wantErr: false,
},
{
name: "invalid counter",
counter: "zzzz",
wantErr: true,
errContains: "counter",
},
{
name: "invalid challenge",
challenge: "bad!",
wantErr: true,
errContains: "challenge",
},
{
name: "invalid password",
password: "123g", // invalid hex
wantErr: true,
errContains: "password",
},
{
name: "invalid sessionInfo",
sessionInfo: "nothex",
wantErr: true,
errContains: "session info",
},
{
name: "invalid timestamp",
timestamp: "xxxx",
wantErr: true,
errContains: "timestamp",
},
{
name: "all empty",
wantErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := HexInputToOCRA(tc.counter, tc.challenge, tc.password, tc.sessionInfo, tc.timestamp)
if tc.wantErr {
if err == nil {
t.Errorf("expected error, got nil")
} else if tc.errContains != "" && !containsIgnoreCase(err.Error(), tc.errContains) {
t.Errorf("error = %q, expected to contain %q", err.Error(), tc.errContains)
}
} else if err != nil {
t.Errorf("expected no error, got: %v", err)
}
})
}
}
func containsIgnoreCase(s, substr string) bool {
return strings.Contains(strings.ToLower(s), strings.ToLower(substr))
}