-
Notifications
You must be signed in to change notification settings - Fork 1
/
otp_test.go
147 lines (140 loc) · 3.55 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
package otp
import (
"reflect"
"testing"
)
var (
secretSha1 = b32("12345678901234567890")
secretSha256 = b32("12345678901234567890123456789012")
secretSha512 = b32("1234567890123456789012345678901234567890123456789012345678901234")
)
func TestKey(t *testing.T) {
testCases := []struct {
url string
typ string
issuer string
account string
secret string
period int
digits uint
counter uint64
algorithm Algorithm
}{
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&issuer=Example",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 30,
digits: 0,
counter: 0,
algorithm: AlgorithmUnknown,
},
{
url: "otpauth://hotp/alice@bob.com?secret=JBSWY3DPEHPK3PXP",
typ: "hotp",
issuer: "",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 30,
digits: 0,
counter: 0,
algorithm: AlgorithmUnknown,
},
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&period=42",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 42,
digits: 0,
counter: 0,
algorithm: AlgorithmUnknown,
},
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&period=42&digits=8",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 42,
digits: 8,
counter: 0,
algorithm: AlgorithmUnknown,
},
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&period=42&counter=42",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 42,
digits: 0,
counter: 42,
algorithm: AlgorithmUnknown,
},
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&period=42&algorithm=SHA1",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 42,
digits: 0,
counter: 0,
algorithm: AlgorithmSHA1,
},
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&period=42&algorithm=SHA256",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 42,
digits: 0,
counter: 0,
algorithm: AlgorithmSHA256,
},
{
url: "otpauth://totp/Example:alice@bob.com?secret=JBSWY3DPEHPK3PXP&period=42&algorithm=SHA512",
typ: "totp",
issuer: "Example",
account: "alice@bob.com",
secret: "JBSWY3DPEHPK3PXP",
period: 42,
digits: 0,
counter: 0,
algorithm: AlgorithmSHA512,
},
}
for _, tc := range testCases {
key, err := ParseKeyFromURL(tc.url)
mustOk(t, err)
mustEqual(t, key.String(), tc.url)
mustEqual(t, key.Type(), tc.typ)
mustEqual(t, key.Issuer(), tc.issuer)
mustEqual(t, key.Account(), tc.account)
mustEqual(t, key.Secret(), tc.secret)
mustEqual(t, key.Period(), uint64(tc.period))
mustEqual(t, key.Digits(), tc.digits)
mustEqual(t, key.Counter(), tc.counter)
mustEqual(t, key.Algorithm(), tc.algorithm)
}
}
func b32(s string) string {
return b32Enc([]byte(s))
}
func mustOk(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Fatal(err)
}
}
func mustEqual(tb testing.TB, have, want interface{}) {
tb.Helper()
if !reflect.DeepEqual(have, want) {
tb.Fatalf("\nhave: %+v\nwant: %+v\n", have, want)
}
}