-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder_test.go
48 lines (43 loc) · 1.27 KB
/
decoder_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
package otp
import (
"encoding/hex"
"testing"
)
func TestDecodeSecret(t *testing.T) {
tests := []struct {
name string
secret string
expected string
wantErr bool
}{
// ✅ From RFC 4648 §10
{"RFC 4648 - 0 chars", "", "", false},
{"RFC 4648 - 1 char", "MY======", "66", false}, // f
{"RFC 4648 - 2 chars", "MZXQ====", "666f", false}, // fo
{"RFC 4648 - 3 chars", "MZXW6===", "666f6f", false}, // foo
{"RFC 4648 - 4 chars", "MZXW6YQ=", "666f6f62", false}, // foob
{"RFC 4648 - 5 chars", "MZXW6YTB", "666f6f6261", false}, // fooba
{"RFC 4648 - 6 chars", "MZXW6YTBOI======", "666f6f626172", false}, // foobar
{"Malformed input", "123!@#", "", true},
// ❌ Error case
{"Unsupported encoding", "foobar", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := DecodeSecret(tt.secret)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error but got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedBytes, _ := hex.DecodeString(tt.expected)
if string(got) != string(expectedBytes) {
t.Errorf("decoded mismatch:\nexpected: %x\ngot: %x", expectedBytes, got)
}
})
}
}