forked from mtusi2026/mtusi2026lab4
S3-вложения и сверка Postgresql с MinIO
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package imagecheck
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func tinyPNG(t *testing.T, w, h int) []byte {
|
||||
t.Helper()
|
||||
img := image.NewNRGBA(image.Rect(0, 0, w, h))
|
||||
img.Set(0, 0, color.NRGBA{R: 10, G: 20, B: 30, A: 255})
|
||||
var buf bytes.Buffer
|
||||
if err := png.Encode(&buf, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func tinyJPEG(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
img := image.NewRGBA(image.Rect(0, 0, 8, 4))
|
||||
var buf bytes.Buffer
|
||||
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 80}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestInspectAcceptsEncodedImages(t *testing.T) {
|
||||
pngBytes := tinyPNG(t, 3, 2)
|
||||
w, h, err := Inspect("image/png", pngBytes, 40_000_000)
|
||||
if err != nil || w != 3 || h != 2 {
|
||||
t.Fatalf("png: %d %d %v", w, h, err)
|
||||
}
|
||||
jpg := tinyJPEG(t)
|
||||
w, h, err = Inspect("image/jpeg", jpg, 40_000_000)
|
||||
if err != nil || w != 8 || h != 4 {
|
||||
t.Fatalf("jpeg: %d %d %v", w, h, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectRejectsMIMESwapAndDamage(t *testing.T) {
|
||||
pngBytes := tinyPNG(t, 2, 2)
|
||||
if _, _, err := Inspect("image/jpeg", pngBytes, 40_000_000); err != ErrFormat {
|
||||
t.Fatalf("png declared as jpeg: %v", err)
|
||||
}
|
||||
if _, _, err := Inspect("image/png", tinyJPEG(t), 40_000_000); err != ErrFormat {
|
||||
t.Fatalf("jpeg declared as png: %v", err)
|
||||
}
|
||||
if _, _, err := Inspect("image/png", []byte("not an image"), 40_000_000); err != ErrFormat {
|
||||
t.Fatalf("text: %v", err)
|
||||
}
|
||||
cut := pngBytes[:len(pngBytes)/2]
|
||||
if _, _, err := Inspect("image/png", cut, 40_000_000); err != ErrFormat {
|
||||
t.Fatalf("truncated: %v", err)
|
||||
}
|
||||
flipped := append([]byte(nil), pngBytes...)
|
||||
flipped[len(flipped)-20] ^= 0xff
|
||||
if _, _, err := Inspect("image/png", flipped, 40_000_000); err != ErrFormat {
|
||||
t.Fatalf("flipped byte: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectPixelLimitBeforeDecode(t *testing.T) {
|
||||
// 20000×2001 = 40_020_000 > 40_000_000. Файл маленький: лимит смотрит IHDR, не растр.
|
||||
huge := pngWithSize(t, 20000, 2001)
|
||||
if _, _, err := Inspect("image/png", huge, 40_000_000); err != ErrPixels {
|
||||
t.Fatalf("pixels: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func pngWithSize(t *testing.T, w, h int) []byte {
|
||||
t.Helper()
|
||||
// Валидный минимальный PNG с подменёнными размерами и пересчитанным CRC IHDR
|
||||
// не обязателен: достаточно корректного CRC, Decode для огромного размера не вызывается.
|
||||
base := tinyPNG(t, 1, 1)
|
||||
// IHDR width/height лежат сразу после сигнатуры и длины/типа: смещение 16 и 20.
|
||||
out := append([]byte(nil), base...)
|
||||
out[16] = byte(w >> 24)
|
||||
out[17] = byte(w >> 16)
|
||||
out[18] = byte(w >> 8)
|
||||
out[19] = byte(w)
|
||||
out[20] = byte(h >> 24)
|
||||
out[21] = byte(h >> 16)
|
||||
out[22] = byte(h >> 8)
|
||||
out[23] = byte(h)
|
||||
// CRC IHDR: байты 12..28 type+data, CRC по смещению 29.
|
||||
sum := crc32.ChecksumIEEE(out[12:29])
|
||||
out[29] = byte(sum >> 24)
|
||||
out[30] = byte(sum >> 16)
|
||||
out[31] = byte(sum >> 8)
|
||||
out[32] = byte(sum)
|
||||
return out
|
||||
}
|
||||
|
||||
func TestFixtureSample(t *testing.T) {
|
||||
b, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w, h, err := Inspect("image/png", b, 40_000_000)
|
||||
if err != nil || w != 320 || h != 200 {
|
||||
t.Fatalf("fixture: %d %d %v", w, h, err)
|
||||
}
|
||||
if _, _, err := Inspect("image/jpeg", b, 40_000_000); err != ErrFormat {
|
||||
t.Fatalf("fixture mime: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user