-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtotp_test.go
117 lines (100 loc) · 2.94 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
package totp
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"hash"
"os"
"testing"
"time"
)
func TestPrint(t *testing.T) {
b, err := BarcodeImage("[email protected]", []byte("hello"), nil)
if err != nil {
t.Errorf("expecting no error, got %q", err)
}
if len(b) <= 0 {
t.Errorf("expecting b to be non-empty")
}
return
// This code is for manual testing of the library functionality
// Authenticates to test authentication
t.Logf("Authenticate=%v", Authenticate([]byte("hello"), "493478", nil))
// Creates a QR code
f, err := os.Create("foo.png")
if err != nil {
t.Errorf("Could not create file: %v", err)
return
}
_, err = f.Write(b)
if err != nil {
t.Errorf("Could not write barcode: %v", err)
return
}
}
func TestVarious(t *testing.T) {
s20 := "3132333435363738393031323334353637383930"
s32 := "3132333435363738393031323334353637383930" +
"313233343536373839303132"
s64 := "3132333435363738393031323334353637383930" +
"3132333435363738393031323334353637383930" +
"3132333435363738393031323334353637383930" +
"31323334"
var secrets [][]byte
for _, v := range []string{s20, s32, s64} {
sec, _ := hex.DecodeString(v)
secrets = append(secrets, []byte(sec))
}
tests := []struct {
time int64
totps []string
}{
{time: 59, totps: []string{"94287082", "46119246", "90693936"}},
{time: 1111111109, totps: []string{"07081804", "68084774", "25091201"}},
{time: 1111111111, totps: []string{"14050471", "67062674", "99943326"}},
{time: 1234567890, totps: []string{"89005924", "91819424", "93441116"}},
{time: 2000000000, totps: []string{"69279037", "90698825", "38618901"}},
{time: 20000000000, totps: []string{"65353130", "77737706", "47863826"}},
}
for _, c := range tests {
for i, h := range []func() hash.Hash{sha1.New, sha256.New, sha512.New} {
if i >= len(c.totps) {
break
}
opt := NewOptions()
opt.Time = func() time.Time {
return time.Unix(c.time, 0)
}
opt.Tries = []int64{0}
opt.TimeStep = 30 * time.Second
opt.Digits = 8
opt.Hash = h
// Test the simple case
auth := Authenticate(secrets[i], c.totps[i], opt)
if !auth {
t.Errorf("should have authenticated, but didn't. TOTP:%q Unix-Time:%v", c.totps[i], c.time)
continue
}
// Test that the tries array works as intended
newtime := c.time - int64(opt.TimeStep/time.Second)
opt.Tries = []int64{0, 1}
opt.Time = func() time.Time {
return time.Unix(newtime, 0)
}
auth = Authenticate(secrets[i], c.totps[i], opt)
if !auth {
t.Errorf("should have authenticated, but didn't. TOTP:%q Unix-Time:%v", c.totps[i], newtime)
continue
}
// Modify the TOTP, and make sure that it fails
failtotp := []byte(c.totps[i])
failtotp[0] = ((failtotp[0]-'0')+1)%('9'-'0') + '0'
auth = Authenticate(secrets[i], string(failtotp), opt)
if auth {
t.Errorf("should have failed to authenticate, but didnt. TOTP:%q Unix-Time:%v", failtotp, c.time)
continue
}
}
}
}