lab4: вложения в S3 (MinIO), reconciler PostgreSQL↔S3, тесты и CI

This commit is contained in:
2026-11-04 20:11:33 +03:00
parent ec540a1a72
commit 77b6e5c2c7
86 changed files with 6921 additions and 2 deletions
+168
View File
@@ -0,0 +1,168 @@
package imagecheck
import (
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"hash/crc32"
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
"testing"
)
const limit = 40_000_000
func testImage(w, h int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
img.Set(x, y, color.RGBA{uint8(x * 7), uint8(y * 3), uint8(x ^ y), 255})
}
}
return img
}
func encodePNG(t *testing.T, w, h int) []byte {
var b bytes.Buffer
if err := png.Encode(&b, testImage(w, h)); err != nil {
t.Fatal(err)
}
return b.Bytes()
}
func encodeJPEG(t *testing.T, w, h int, progressive bool) []byte {
var b bytes.Buffer
if err := jpeg.Encode(&b, testImage(w, h), &jpeg.Options{Quality: 80}); err != nil {
t.Fatal(err)
}
return b.Bytes()
}
func chunk(typ string, data []byte) []byte {
var b bytes.Buffer
binary.Write(&b, binary.BigEndian, uint32(len(data)))
b.WriteString(typ)
b.Write(data)
crc := crc32.NewIEEE()
crc.Write([]byte(typ))
crc.Write(data)
binary.Write(&b, binary.BigEndian, crc.Sum32())
return b.Bytes()
}
// craftPNG собирает PNG вручную: так можно задать любые IHDR и поток данных.
func craftPNG(w, h uint32, colorType, depth, interlace byte, raw []byte) []byte {
var b bytes.Buffer
b.Write(pngSignature)
ihdr := make([]byte, 13)
binary.BigEndian.PutUint32(ihdr[0:], w)
binary.BigEndian.PutUint32(ihdr[4:], h)
ihdr[8], ihdr[9], ihdr[12] = depth, colorType, interlace
b.Write(chunk("IHDR", ihdr))
var z bytes.Buffer
zw := zlib.NewWriter(&z)
zw.Write(raw)
zw.Close()
b.Write(chunk("IDAT", z.Bytes()))
b.Write(chunk("IEND", nil))
return b.Bytes()
}
func check(t *testing.T, name string, data []byte, format string, want error) {
t.Helper()
_, err := Validate(bytes.NewReader(data), format, limit)
if want == nil && err != nil {
t.Errorf("%s: unexpected error %v", name, err)
}
if want != nil && !errors.Is(err, want) {
t.Errorf("%s: got %v, want %v", name, err, want)
}
}
func TestValidPNGAndJPEG(t *testing.T) {
p := encodePNG(t, 320, 200)
res, err := Validate(bytes.NewReader(p), PNG, limit)
if err != nil || res.Width != 320 || res.Height != 200 {
t.Fatalf("png: %+v %v", res, err)
}
j := encodeJPEG(t, 321, 199, false)
res, err = Validate(bytes.NewReader(j), JPEG, limit)
if err != nil || res.Width != 321 || res.Height != 199 {
t.Fatalf("jpeg: %+v %v", res, err)
}
check(t, "png with trailing bytes", append(p, []byte("trailer")...), PNG, nil)
}
func TestCourseFixture(t *testing.T) {
data, err := os.ReadFile("../../tests/fixtures/sample.png")
if err != nil {
t.Skip("fixture not found")
}
res, err := Validate(bytes.NewReader(data), PNG, limit)
if err != nil || res.Width != 320 || res.Height != 200 {
t.Fatalf("fixture: %+v %v", res, err)
}
}
func TestInterlacedPNG(t *testing.T) {
// 10×7 RGB, Adam7: длина сырых данных по семи проходам.
w, h := int64(10), int64(7)
raw := make([]byte, expectedRaw(w, h, 24, true))
check(t, "interlaced", craftPNG(uint32(w), uint32(h), 2, 8, 1, raw), PNG, nil)
check(t, "interlaced short data", craftPNG(uint32(w), uint32(h), 2, 8, 1, raw[:len(raw)-1]), PNG, ErrInvalid)
}
func TestBrokenImages(t *testing.T) {
p := encodePNG(t, 64, 64)
j := encodeJPEG(t, 64, 64, false)
check(t, "png truncated", p[:len(p)/2], PNG, ErrInvalid)
check(t, "png without IEND", p[:len(p)-12], PNG, ErrInvalid)
bad := bytes.Clone(p)
bad[len(bad)/2] ^= 0xFF
check(t, "png corrupted byte", bad, PNG, ErrInvalid)
check(t, "jpeg truncated", j[:len(j)/2], JPEG, ErrInvalid)
check(t, "jpeg without EOI", j[:len(j)-2], JPEG, ErrInvalid)
check(t, "png declared as jpeg", p, JPEG, ErrInvalid)
check(t, "text as png", []byte("hello, not an image at all"), PNG, ErrInvalid)
check(t, "empty", nil, PNG, ErrInvalid)
check(t, "png short raw data", craftPNG(8, 8, 2, 8, 0, make([]byte, 8*(1+24)-1)), PNG, ErrInvalid)
check(t, "png bad bit depth", craftPNG(8, 8, 2, 4, 0, make([]byte, 8*(1+12))), PNG, ErrInvalid)
}
func TestPixelLimitBeforeData(t *testing.T) {
// «Бомба»: IHDR на 100000×100000, данных почти нет. Должно быть ErrTooLarge,
// а не попытка распаковать 30 ГБ.
bomb := craftPNG(100000, 100000, 2, 8, 0, []byte{0})
check(t, "png bomb", bomb, PNG, ErrTooLarge)
// Ровно на границе лимита — допустимо по пикселям (данных не хватает → invalid).
check(t, "png at limit", craftPNG(8000, 5000, 0, 1, 0, nil), PNG, ErrInvalid)
check(t, "png over limit", craftPNG(8000, 5001, 0, 1, 0, nil), PNG, ErrTooLarge)
// JPEG: подменяем размеры в SOF0 на 60000×60000.
j := encodeJPEG(t, 16, 16, false)
i := bytes.Index(j, []byte{0xFF, 0xC0})
if i < 0 {
t.Fatal("no SOF0")
}
big := bytes.Clone(j)
binary.BigEndian.PutUint16(big[i+5:], 60000)
binary.BigEndian.PutUint16(big[i+7:], 60000)
check(t, "jpeg bomb", big, JPEG, ErrTooLarge)
}
func TestSniff(t *testing.T) {
if f, ok := Sniff(encodePNG(t, 2, 2)); !ok || f != PNG {
t.Error("png sniff")
}
if f, ok := Sniff(encodeJPEG(t, 2, 2, false)); !ok || f != JPEG {
t.Error("jpeg sniff")
}
if _, ok := Sniff([]byte("GIF89a")); ok {
t.Error("gif must not be accepted")
}
}