feat: validate user input for entity names

This commit is contained in:
IliaDenisov
2026-02-06 19:31:35 +03:00
parent 203d4e21c4
commit 3dd0440832
11 changed files with 304 additions and 28 deletions
+210
View File
@@ -0,0 +1,210 @@
package util_test
import (
"testing"
"unicode/utf8"
"github.com/iliadenisov/galaxy/internal/util"
"github.com/stretchr/testify/assert"
)
func TestValidateString(t *testing.T) {
tests := []struct {
name string
input string
expected string
ok bool
}{
// Basic cases
{
name: "Valid string with Latin characters and digits",
input: "Hello_World-123",
expected: "Hello_World-123",
ok: true,
},
{
name: "Valid string with Cyrillic characters",
input: "Привет_мир-42",
expected: "Привет_мир-42",
ok: true,
},
{
name: "Valid Greek alphabet string",
input: "Αλφα_Βητα-2024",
expected: "Αλφα_Βητα-2024",
ok: true,
},
{
name: "Valid Arabic alphabet string",
input: "مرحبا_العالم-7",
expected: "مرحبا_العالم-7",
ok: true,
},
{
name: "Valid Japanese Katakana string",
input: "テスト_ケース-1",
expected: "テスト_ケース-1",
ok: true,
},
{
name: "Valid Chinese characters",
input: "你好_世界-123", // "Hello World" in Chinese
expected: "你好_世界-123",
ok: true,
},
{
name: "Valid Hindi characters",
input: "नमस्ते_दुनिया-456", // "Hello World" in Hindi
expected: "नमस्ते_दुनिया-456",
ok: true,
},
{
name: "Valid Thai characters",
input: "สวัสดี_โลก-789", // "Hello World" in Thai
expected: "สวัสดี_โลก-789",
ok: true,
},
{
name: "Valid Korean characters",
input: "안녕하세요_세계-101", // "Hello World" in Korean
expected: "안녕하세요_세계-101",
ok: true,
},
{
name: "Valid Hebrew characters",
input: "שלום_עולם-202", // "Hello World" in Hebrew
expected: "שלום_עולם-202",
ok: true,
},
// Special characters test cases
{
name: "Valid special character @",
input: "Test@Name",
expected: "Test@Name",
ok: true,
},
{
name: "Valid special character ^",
input: "Test^Name",
expected: "Test^Name",
ok: true,
},
{
name: "Valid special character ~",
input: "Test~Name",
expected: "Test~Name",
ok: true,
},
// Edge cases
{
name: "Spaces are trimmed from both ends",
input: " Test123_Name ",
expected: "Test123_Name",
ok: true,
},
{
name: "Spaces in the middle are not allowed",
input: "Test 123",
expected: "",
ok: false,
},
{
name: "Tab character in the middle is not allowed",
input: "Test\tName",
expected: "",
ok: false,
},
{
name: "Newline character is not allowed",
input: "Test\nName",
expected: "",
ok: false,
},
{
name: "Dash at the beginning after TrimSpace is not allowed",
input: " -Test123",
expected: "",
ok: false,
},
{
name: "Dash at the end after TrimSpace is not allowed",
input: "Test123- ",
expected: "",
ok: false,
},
{
name: "Emoji is not allowed",
input: "Test🙂Name",
expected: "",
ok: false,
},
{
name: "String containing only spaces",
input: " ",
expected: "",
ok: false,
},
{
name: "Empty string",
input: "",
expected: "",
ok: false,
},
{
name: "Too long string",
input: "ValidatedStringHasTooManyCharacters",
expected: "",
ok: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, ok := util.ValidateTypeName(tt.input)
assert.Equal(t, tt.ok, ok)
assert.Equal(t, tt.expected, result)
})
}
}
// Fuzz test for ValidateString function
func FuzzValidateString(f *testing.F) {
// Adding a few basic strings to start the fuzz test
f.Add("Hello_World-123")
f.Add("Test@Name")
f.Add("Привет_мир-42")
f.Add("αβγ@~")
f.Add("مرحبا_العالم-7")
// Fuzz function
f.Fuzz(func(t *testing.T, input string) {
// Call the function and check if the result matches expectations
result, ok := util.ValidateTypeName(input)
// Check if the string is non-empty and valid UTF-8
if len(input) > 0 {
if !utf8.ValidString(input) {
t.Errorf("Error: string is not a valid UTF-8 string: %s", input)
}
}
// If the string is empty, ok should be false
if len(result) == 0 {
if ok {
t.Errorf("Expected false for invalid string, but got true: %s", input)
}
} else {
// If the result is not empty, ok should be true
if !ok {
t.Errorf("Expected true for valid string, but got false: %s", input)
}
}
// Additional check: if input has spaces at the beginning or end, it should fail
if input[0] == ' ' || input[len(input)-1] == ' ' {
if ok {
t.Errorf("Error: string contains spaces at the beginning or end: %s", input)
}
}
})
}