package apikey import "testing" func TestGenerate(t *testing.T) { plain, hash, prefix, err := Generate() if err != nil { t.Fatal(err) } if !Valid(plain) { t.Fatalf("generated key invalid: %q", plain) } if len(plain) != 3+48 { t.Fatalf("key length = %d, want 51", len(plain)) } if Hash(plain) != hash { t.Fatal("hash mismatch") } if len(prefix) > len(plain) || prefix != plain[:len(prefix)] { t.Fatal("prefix must be prefix of plain key") } // 两次生成不重复 plain2, _, _, _ := Generate() if plain == plain2 { t.Fatal("keys should be unique") } } func TestValid(t *testing.T) { if Valid("") || Valid("sk-short") || Valid("xxx") { t.Fatal("invalid keys should be rejected") } }