forked from mtusi2026/mtusi2026lab5
chore(migrate) moved codebase from lab4
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestValidateFilename(t *testing.T) {
|
||||
valid := []string{"photo.png", "фото 1.jpeg", "a", strings.Repeat("a", 255)}
|
||||
for _, name := range valid {
|
||||
if err := ValidateFilename(name); err != nil {
|
||||
t.Errorf("ValidateFilename(%q) = %v, want nil", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
invalid := []string{
|
||||
"", " ", ".", "..", "dir/photo.png", `dir\photo.png`,
|
||||
"nul\x00byte", "line\nbreak", strings.Repeat("a", 256),
|
||||
}
|
||||
for _, name := range invalid {
|
||||
if err := ValidateFilename(name); err == nil {
|
||||
t.Errorf("ValidateFilename(%q) = nil, want error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAttachmentIDs(t *testing.T) {
|
||||
ids := make([]uuid.UUID, 10)
|
||||
for index := range ids {
|
||||
ids[index] = uuid.New()
|
||||
}
|
||||
if err := ValidateAttachmentIDs(ids); err != nil {
|
||||
t.Fatalf("ten unique ids rejected: %v", err)
|
||||
}
|
||||
if err := ValidateAttachmentIDs(append(ids, uuid.New())); err == nil {
|
||||
t.Fatal("eleven ids accepted")
|
||||
}
|
||||
if err := ValidateAttachmentIDs([]uuid.UUID{ids[0], ids[0]}); err == nil {
|
||||
t.Fatal("duplicate ids accepted")
|
||||
}
|
||||
if err := ValidateAttachmentIDs(nil); err != nil {
|
||||
t.Fatalf("empty list rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMessageText(t *testing.T) {
|
||||
if err := ValidateMessageText("", 1); err != nil {
|
||||
t.Fatalf("empty text with attachment rejected: %v", err)
|
||||
}
|
||||
if err := ValidateMessageText(" ", 1); err != nil {
|
||||
t.Fatalf("blank text with attachment rejected: %v", err)
|
||||
}
|
||||
if err := ValidateMessageText("", 0); err == nil {
|
||||
t.Fatal("empty text without attachment accepted")
|
||||
}
|
||||
if err := ValidateMessageText(strings.Repeat("ж", 4000), 0); err != nil {
|
||||
t.Fatalf("4000 code points rejected: %v", err)
|
||||
}
|
||||
if err := ValidateMessageText(strings.Repeat("ж", 4001), 1); err == nil {
|
||||
t.Fatal("4001 code points accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user