| 1 | package gotp |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | var totp = NewDefaultTOTP("4S62BZNFXXSZLCRO") |
| 8 | |
| 9 | func TestTOTP_At(t *testing.T) { |
| 10 | if totp.Now() != totp.At(currentTimestamp()) { |
| 11 | t.Error("TOTP generate otp error!") |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | func TestTOTP_NowWithExpiration(t *testing.T) { |
| 16 | otp, exp := totp.NowWithExpiration() |
| 17 | cts := currentTimestamp() |
| 18 | if otp != totp.Now() { |
| 19 | t.Error("TOTP generate otp error!") |
| 20 | } |
| 21 | if totp.At(cts+30) != totp.At(int(exp)) { |
| 22 | t.Error("TOTP expiration otp error!") |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestTOTP_Verify(t *testing.T) { |
| 27 | if !totp.Verify("179394", 1524485781) { |
| 28 | t.Error("verify faild") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestTOTP_ProvisioningUri(t *testing.T) { |
| 33 | expect := "otpauth://totp/github:xlzd?secret=4S62BZNFXXSZLCRO&issuer=github" |
| 34 | uri := totp.ProvisioningUri("xlzd", "github") |
| 35 | if expect != uri { |
| 36 | t.Error("ProvisioningUri error") |
| 37 | } |
| 38 | } |