package apikey import "testing" func TestGenerateValid(t *testing.T) { plain, hash, keyPrefix, err := Generate() if err != nil { t.Fatalf("Generate: %v", err) } if !Valid(plain) { t.Fatalf("generated key invalid: %q", plain) } if len(plain) != len(prefix)+keyLen { t.Fatalf("unexpected key length: %d", len(plain)) } if keyPrefix != plain[:12] { t.Fatalf("prefix mismatch: %s vs %s", keyPrefix, plain[:12]) } if Hash(plain) != hash { t.Fatal("hash mismatch") } } func TestHashStable(t *testing.T) { if Hash("sk-ot-test") != Hash("sk-ot-test") { t.Fatal("hash not stable") } } func TestValidRejects(t *testing.T) { tooShort := prefix + "abc" tooLong := prefix + "1234567890123456789012345678901234567890123456789" wrongPrefix := "abc-ot123456789012345678901234567890123456789012345678" cases := []string{"", tooShort, wrongPrefix, tooLong} for _, c := range cases { if Valid(c) { t.Fatalf("expected invalid: %q", c) } } }