-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_test.go
119 lines (107 loc) · 5.2 KB
/
key_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
package otp_test
import (
"crypto/rand"
"encoding/base32"
"errors"
"testing"
"github.com/agiledragon/gomonkey"
"github.com/smartystreets/goconvey/convey"
"github.com/v8fg/kit4go/otp"
)
var b32NoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)
func TestRandomSecret(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestRandomSecret", t, func() {
convey.Convey("TestRandomSecret-Failed", func() {
outputs := []gomonkey.OutputCell{
{Values: gomonkey.Params{0, nil}, Times: 1},
{Values: gomonkey.Params{0, errors.New("error")}, Times: 1},
}
af := gomonkey.ApplyFuncSeq(rand.Read, outputs)
defer af.Reset()
convey.So(otp.RandomSecret(6), convey.ShouldEqual, "")
convey.So(otp.RandomSecret(6), convey.ShouldEqual, "")
})
convey.Convey("TestRandomSecret-Success", func() {
code := otp.RandomSecret(4)
decodeString, _ := b32NoPadding.DecodeString(code)
convey.So(decodeString, convey.ShouldHaveLength, 4)
code = otp.RandomSecret(6)
decodeString, _ = b32NoPadding.DecodeString(code)
convey.So(decodeString, convey.ShouldHaveLength, 6)
})
})
}
func TestVerifySecret(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestVerifySecret", t, func() {
convey.So(otp.VerifySecret("7ZDW4TVCYM"), convey.ShouldBeTrue)
convey.So(otp.VerifySecret("JBSWY3DPEHPK3PXP"), convey.ShouldBeTrue)
})
}
func TestGenerateURLHOTP(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestGenerateURLHOTP", t, func() {
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: ""}), convey.ShouldBeEmpty)
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: "xwi88"}), convey.ShouldBeEmpty)
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com"}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", Algorithm: otp.AlgorithmSHA512}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", Secret: []byte("7ZDW4TVCYM")}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", SecretSize: uint(0)}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLHOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", Secret: []byte("7ZDW4TVCYM"), Digits: 8}), convey.ShouldNotBeEmpty)
})
}
func TestGenerateURLTOTP(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestGenerateURLTOTP", t, func() {
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: ""}), convey.ShouldBeEmpty)
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: "xwi88"}), convey.ShouldBeEmpty)
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com"}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", Algorithm: otp.AlgorithmSHA512}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", Secret: []byte("7ZDW4TVCYM")}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", SecretSize: uint(0)}), convey.ShouldNotBeEmpty)
convey.So(otp.GenerateURLTOTP(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com", Secret: []byte("7ZDW4TVCYM"), Digits: 8}), convey.ShouldNotBeEmpty)
})
}
func TestKeyFromURL(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestKeyFromURL", t, func() {
key, err := otp.KeyFromURL("")
convey.So(key, convey.ShouldBeNil)
convey.So(err, convey.ShouldBeError)
key, err = otp.KeyFromURL("otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example")
convey.So(key, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeNil)
key, err = otp.KeyFromURL("otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30")
convey.So(key, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeNil)
})
}
func TestKeyFromHOTPOpts(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestKeyFromHOTPOpts", t, func() {
key, err := otp.KeyFromHOTPOpts(otp.KeyOpts{Issuer: ""})
convey.So(key, convey.ShouldBeNil)
convey.So(err, convey.ShouldBeError)
key, err = otp.KeyFromHOTPOpts(otp.KeyOpts{Issuer: "xwi88"})
convey.So(key, convey.ShouldBeNil)
convey.So(err, convey.ShouldBeError)
key, err = otp.KeyFromHOTPOpts(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com"})
convey.So(key, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeNil)
})
}
func TestKeyFromTOTPOpts(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestKeyFromTOTPOpts", t, func() {
key, err := otp.KeyFromTOTPOpts(otp.KeyOpts{Issuer: ""})
convey.So(key, convey.ShouldBeNil)
convey.So(err, convey.ShouldBeError)
key, err = otp.KeyFromTOTPOpts(otp.KeyOpts{Issuer: "xwi88"})
convey.So(key, convey.ShouldBeNil)
convey.So(err, convey.ShouldBeError)
key, err = otp.KeyFromTOTPOpts(otp.KeyOpts{Issuer: "xwi88", AccountName: "xwi88.com"})
convey.So(key, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeNil)
})
}