package service import ( "testing" ) func TestNormalizeAttachmentExts(t *testing.T) { got, err := NormalizeAttachmentExts([]string{".PDF", "msi", "PDF", " zip "}) if err != nil { t.Fatal(err) } want := []string{"pdf", "msi", "zip"} if len(got) != len(want) { t.Fatalf("len=%d want %d: %v", len(got), len(want), got) } for i := range want { if got[i] != want[i] { t.Fatalf("got[%d]=%s want %s", i, got[i], want[i]) } } } func TestNormalizeAttachmentExtsInvalid(t *testing.T) { _, err := NormalizeAttachmentExts([]string{"bad_ext"}) if err != ErrInvalidSiteSetting { t.Fatalf("want ErrInvalidSiteSetting, got %v", err) } } func TestResolveAttachmentMIMEActive(t *testing.T) { m := ResolveAttachmentMIME(".html", []byte("")) if m != "application/octet-stream" { t.Fatalf("html mime=%s", m) } } func TestResolveAttachmentMIMEDisguisedPNG(t *testing.T) { m := ResolveAttachmentMIME(".png", []byte("")) if m != "application/octet-stream" { t.Fatalf("disguised png mime=%s", m) } } func TestIsActiveContentExt(t *testing.T) { if !IsActiveContentExt(".JS") { t.Fatal("js should be active") } if IsActiveContentExt(".pdf") { t.Fatal("pdf should not be active") } } func TestSanitizeFilenameRTL(t *testing.T) { name := sanitizeFilename("evil\u202epdf.exe") if name != "evil_pdf.exe" && name != "evil_.exe" { // Map replaces U+202E with _ if !containsRune(name, '_') { t.Fatalf("expected RTL stripped: %q", name) } } } func containsRune(s string, r rune) bool { for _, c := range s { if c == r { return true } } return false }