misaki/server/internal_totp_test.go
q 119b6d73c4
Some checks failed
Build & push Docker image / build (push) Has been cancelled
totp 2fac
2026-11-07 11:43:40 -05:00

30 lines
869 B
Go

package server
import (
"fmt"
"testing"
)
// RFC 6238 appendix B, SHA1 rows (8 digits).
func TestTOTPVectors(t *testing.T) {
key := []byte("12345678901234567890")
for ts, want := range map[int64]string{
59: "94287082", 1111111109: "07081804", 1111111111: "14050471",
1234567890: "89005924", 2000000000: "69279037", 20000000000: "65353130",
} {
if got := fmt.Sprintf("%08d", hotp(key, uint64(ts/30))%100000000); got != want {
t.Errorf("T=%d: got %s, want %s", ts, got, want)
}
if got := totpCode(key, ts/30); got != want[2:] {
t.Errorf("T=%d 6-digit: got %s, want %s", ts, got, want[2:])
}
}
}
func TestNormCode(t *testing.T) {
for in, want := range map[string]string{" 123 456 ": "123456", "ABCD-efgh-2345-6789": "abcdefgh23456789"} {
if got := normCode(in); got != want {
t.Errorf("normCode(%q) = %q, want %q", in, got, want)
}
}
}