forked from mtusi2026/mtusi2026lab4
158 lines
5.2 KiB
Go
158 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"hash/crc32"
|
|
"image"
|
|
"image/png"
|
|
"strings"
|
|
"testing"
|
|
"uuid"
|
|
|
|
"messenger/internal/domain"
|
|
)
|
|
|
|
func TestCheckFilename(t *testing.T) {
|
|
valid := []string{"photo.png", "фото 1.jpg", strings.Repeat("a", 255)}
|
|
for _, name := range valid {
|
|
if err := checkFilename(name); err != nil {
|
|
t.Errorf("%q: unexpected error %v", name, err)
|
|
}
|
|
}
|
|
invalid := []string{"", strings.Repeat("a", 256), "dir/photo.png", `dir\photo.png`, "photo\x00.png", "photo\n.png", "photo\x7f.png"}
|
|
for _, name := range invalid {
|
|
if err := checkFilename(name); !errors.Is(err, domain.ErrInvalidInput) {
|
|
t.Errorf("%q: got %v, want ErrInvalidInput", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDetectImageType(t *testing.T) {
|
|
if got := detectImageType(encodePNG(t, 4, 4)); got != domain.ContentPNG {
|
|
t.Errorf("png: got %q", got)
|
|
}
|
|
if got := detectImageType([]byte("not an image at all")); got != "" {
|
|
t.Errorf("garbage: got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestCheckPixels(t *testing.T) {
|
|
if err := checkPixels(encodePNG(t, 320, 200), 40000000); err != nil {
|
|
t.Errorf("small image: unexpected error %v", err)
|
|
}
|
|
if err := checkPixels(pngHeader(8000, 5000), 40000000); err != nil {
|
|
t.Errorf("exact pixel limit: unexpected error %v", err)
|
|
}
|
|
if err := checkPixels(pngHeader(20000, 2001), 40000000); !errors.Is(err, domain.ErrTooLarge) {
|
|
t.Errorf("above pixel limit: got %v, want ErrTooLarge", err)
|
|
}
|
|
if err := checkPixels(pngHeader(65536, 65536), 40000000); !errors.Is(err, domain.ErrTooLarge) {
|
|
t.Errorf("huge dimensions: got %v, want ErrTooLarge", err)
|
|
}
|
|
if err := checkPixels([]byte("garbage"), 40000000); !errors.Is(err, domain.ErrUnsupportedMedia) {
|
|
t.Errorf("garbage: got %v, want ErrUnsupportedMedia", err)
|
|
}
|
|
}
|
|
|
|
func TestCheckIntegrity(t *testing.T) {
|
|
if err := checkIntegrity(encodePNG(t, 8, 8)); err != nil {
|
|
t.Errorf("valid png: unexpected error %v", err)
|
|
}
|
|
corrupted := encodePNG(t, 8, 8)
|
|
corrupted = corrupted[:len(corrupted)-20]
|
|
if err := checkIntegrity(corrupted); !errors.Is(err, domain.ErrUnsupportedMedia) {
|
|
t.Errorf("truncated png: got %v, want ErrUnsupportedMedia", err)
|
|
}
|
|
}
|
|
|
|
func TestCheckMessageText(t *testing.T) {
|
|
if err := checkMessageText("", false); !errors.Is(err, domain.ErrInvalidInput) {
|
|
t.Errorf("empty text without attachments: got %v", err)
|
|
}
|
|
if err := checkMessageText(" ", false); !errors.Is(err, domain.ErrInvalidInput) {
|
|
t.Errorf("blank text without attachments: got %v", err)
|
|
}
|
|
if err := checkMessageText("", true); err != nil {
|
|
t.Errorf("empty text with attachments: unexpected error %v", err)
|
|
}
|
|
if err := checkMessageText(strings.Repeat("я", 4000), true); err != nil {
|
|
t.Errorf("4000 characters: unexpected error %v", err)
|
|
}
|
|
if err := checkMessageText(strings.Repeat("я", 4001), true); !errors.Is(err, domain.ErrInvalidInput) {
|
|
t.Errorf("4001 characters: got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCheckAttachmentIDs(t *testing.T) {
|
|
ids := make([]uuid.UUID, 10)
|
|
for i := range ids {
|
|
ids[i] = uuid.New()
|
|
}
|
|
if err := checkAttachmentIDs(ids); err != nil {
|
|
t.Errorf("ten unique ids: unexpected error %v", err)
|
|
}
|
|
if err := checkAttachmentIDs(append(ids, uuid.New())); !errors.Is(err, domain.ErrInvalidInput) {
|
|
t.Errorf("eleven ids: got %v", err)
|
|
}
|
|
if err := checkAttachmentIDs([]uuid.UUID{ids[0], ids[0]}); !errors.Is(err, domain.ErrInvalidInput) {
|
|
t.Errorf("duplicate ids: got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMessageHash(t *testing.T) {
|
|
first, second := uuid.New(), uuid.New()
|
|
if messageHash("", nil) != messageHash("", []uuid.UUID{}) {
|
|
t.Error("absent attachment_ids must hash like an empty list")
|
|
}
|
|
if messageHash("hi", []uuid.UUID{first, second}) != messageHash("hi", []uuid.UUID{second, first}) {
|
|
t.Error("attachment order must not change the hash")
|
|
}
|
|
if messageHash("hi", nil) == messageHash("hi!", nil) {
|
|
t.Error("different text must change the hash")
|
|
}
|
|
if messageHash("hi", []uuid.UUID{first}) == messageHash("hi", []uuid.UUID{second}) {
|
|
t.Error("different attachments must change the hash")
|
|
}
|
|
}
|
|
|
|
func TestReadLimited(t *testing.T) {
|
|
raw, err := readLimited(strings.NewReader("abcd"), 4)
|
|
if err != nil || string(raw) != "abcd" {
|
|
t.Fatalf("exact limit: got %q, %v", raw, err)
|
|
}
|
|
if _, err := readLimited(strings.NewReader("abcde"), 4); !errors.Is(err, domain.ErrTooLarge) {
|
|
t.Errorf("over limit: got %v, want ErrTooLarge", err)
|
|
}
|
|
if _, err := readLimited(strings.NewReader(""), 4); !errors.Is(err, domain.ErrUnsupportedMedia) {
|
|
t.Errorf("empty body: got %v, want ErrUnsupportedMedia", err)
|
|
}
|
|
}
|
|
|
|
func encodePNG(t *testing.T, width, height int) []byte {
|
|
t.Helper()
|
|
var buffer bytes.Buffer
|
|
if err := png.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return buffer.Bytes()
|
|
}
|
|
|
|
func pngHeader(width, height uint32) []byte {
|
|
var buffer bytes.Buffer
|
|
buffer.WriteString("\x89PNG\r\n\x1a\n")
|
|
header := make([]byte, 13)
|
|
binary.BigEndian.PutUint32(header[0:4], width)
|
|
binary.BigEndian.PutUint32(header[4:8], height)
|
|
header[8], header[9] = 8, 2
|
|
buffer.Write(binary.BigEndian.AppendUint32(nil, uint32(len(header))))
|
|
buffer.WriteString("IHDR")
|
|
buffer.Write(header)
|
|
checksum := crc32.NewIEEE()
|
|
checksum.Write([]byte("IHDR"))
|
|
checksum.Write(header)
|
|
buffer.Write(binary.BigEndian.AppendUint32(nil, checksum.Sum32()))
|
|
return buffer.Bytes()
|
|
}
|