package auth import ( "testing" "time" ) func TestVerifyTOTPRFC6238SHA1Vectors(t *testing.T) { secret := "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" cases := []struct { unix int64 code string }{ {unix: 59, code: "287082"}, {unix: 1111111109, code: "081804"}, {unix: 1111111111, code: "050471"}, {unix: 1234567890, code: "005924"}, {unix: 2000000000, code: "279037"}, } for _, testCase := range cases { step, valid := VerifyTOTP(secret, testCase.code, time.Unix(testCase.unix, 0)) if !valid { t.Errorf("VerifyTOTP(%d, %q) = false", testCase.unix, testCase.code) } else if want := testCase.unix / int64(TOTPPeriod/time.Second); step != want { t.Errorf("VerifyTOTP(%d, %q) matched step %d, want %d", testCase.unix, testCase.code, step, want) } } } func TestTOTPProvisioningURI(t *testing.T) { uri := TOTPProvisioningURI("Example App", "admin@example.com", "abc123") if uri == "" || len(uri) < len("otpauth://totp/") || uri[:15] != "otpauth://totp/" { t.Fatalf("unexpected provisioning URI: %q", uri) } }