-
Notifications
You must be signed in to change notification settings - Fork 65
/
totp_test.go
45 lines (38 loc) · 999 Bytes
/
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
package gotp
import (
"testing"
"time"
)
var totp = NewDefaultTOTP("4S62BZNFXXSZLCRO")
func TestTOTP_At(t *testing.T) {
if totp.Now() != totp.At(currentTimestamp()) {
t.Error("TOTP generate otp error!")
}
}
func TestTOTP_AtTime(t *testing.T) {
if totp.Now() != totp.AtTime(time.Now()) {
t.Error("TOTP at time generate otp error!")
}
}
func TestTOTP_NowWithExpiration(t *testing.T) {
otp, exp := totp.NowWithExpiration()
cts := currentTimestamp()
if otp != totp.Now() {
t.Error("TOTP generate otp error!")
}
if totp.At(cts+30) != totp.At(exp) {
t.Error("TOTP expiration otp error!")
}
}
func TestTOTP_Verify(t *testing.T) {
if !totp.Verify("179394", 1524485781) {
t.Error("verify faild")
}
}
func TestTOTP_ProvisioningUri(t *testing.T) {
expect := "otpauth://totp/github:xlzd?issuer=github&secret=4S62BZNFXXSZLCRO"
uri := totp.ProvisioningUri("xlzd", "github")
if expect != uri {
t.Errorf("ProvisioningUri error.\n\texpected: %s,\n\tactual: %s", expect, uri)
}
}