Files
mtusi2026lab5/internal/imagecheck/imagecheck_test.go
T
Vladislav Kunetskiy b965350cc9 lab5: исправления второго ревью
Reprocess of a queued/processing/failed upload now sources from verified staging instead of the not-yet-written final original, which used to supersede v1 and strand the attachment in queued. PNG uploads now inflate the concatenated IDAT stream (bounded by IHDR dimensions) before acceptance, so a structurally valid PNG whose IDAT is not a zlib stream is rejected with 415 instead of 202.
2026-10-31 00:41:38 +03:00

144 lines
4.4 KiB
Go

package imagecheck_test
import (
"bytes"
"encoding/binary"
"hash/crc32"
"image"
"image/color"
"image/jpeg"
"image/png"
"testing"
"github.com/stretchr/testify/require"
"mtusi-messenger/internal/imagecheck"
)
func encodePNG(t *testing.T, width, height int) []byte {
t.Helper()
source := image.NewRGBA(image.Rect(0, 0, width, height))
for x := range width {
for y := range height {
source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255})
}
}
var buffer bytes.Buffer
require.NoError(t, png.Encode(&buffer, source))
return buffer.Bytes()
}
func encodeJPEG(t *testing.T, width, height int) []byte {
t.Helper()
source := image.NewRGBA(image.Rect(0, 0, width, height))
for x := range width {
for y := range height {
source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 64, A: 255})
}
}
var buffer bytes.Buffer
require.NoError(t, jpeg.Encode(&buffer, source, &jpeg.Options{Quality: 90}))
return buffer.Bytes()
}
func TestValidatePNG(t *testing.T) {
info, err := imagecheck.Validate(bytes.NewReader(encodePNG(t, 32, 20)), 40_000_000)
require.NoError(t, err)
require.Equal(t, "image/png", info.ContentType)
require.Equal(t, int64(32), info.Width)
require.Equal(t, int64(20), info.Height)
}
func TestValidateJPEG(t *testing.T) {
info, err := imagecheck.Validate(bytes.NewReader(encodeJPEG(t, 24, 16)), 40_000_000)
require.NoError(t, err)
require.Equal(t, "image/jpeg", info.ContentType)
require.Equal(t, int64(24), info.Width)
require.Equal(t, int64(16), info.Height)
}
func TestValidateRejectsUnsupported(t *testing.T) {
_, err := imagecheck.Validate(bytes.NewReader([]byte("not an image at all")), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrUnsupported)
_, err = imagecheck.Validate(bytes.NewReader(nil), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrUnsupported)
}
func TestValidateRejectsTruncatedPNG(t *testing.T) {
data := encodePNG(t, 64, 64)
_, err := imagecheck.Validate(bytes.NewReader(data[:len(data)/2]), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
}
func TestValidateRejectsPNGCRCMismatch(t *testing.T) {
data := encodePNG(t, 64, 64)
index := bytes.Index(data, []byte("IDAT"))
require.Positive(t, index)
data[index+8] ^= 0xFF // first IDAT payload byte; CRC no longer matches
_, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
}
// Builds a structurally valid PNG (signature, well-formed IHDR/IDAT/IEND,
// correct CRCs) whose IDAT payload is not a zlib stream at all. Merely a
// stream-like structure must not make the upload pass: it has to inflate.
func TestValidateRejectsPNGIDATNotAZlibStream(t *testing.T) {
chunk := func(ctype string, payload []byte) []byte {
out := make([]byte, 12+len(payload))
binary.BigEndian.PutUint32(out[0:4], uint32(len(payload)))
copy(out[4:8], ctype)
copy(out[8:], payload)
checksum := crc32.NewIEEE()
_, _ = checksum.Write(out[4 : 8+len(payload)])
binary.BigEndian.PutUint32(out[8+len(payload):], checksum.Sum32())
return out
}
ihdr := make([]byte, 13)
binary.BigEndian.PutUint32(ihdr[0:4], 64) // width
binary.BigEndian.PutUint32(ihdr[4:8], 32) // height
ihdr[8] = 8 // bit depth
ihdr[9] = 6 // color type RGBA
data := append([]byte("\x89PNG\r\n\x1a\n"), chunk("IHDR", ihdr)...)
data = append(data, chunk("IDAT", []byte("this is not a zlib stream, but the CRC is right"))...)
data = append(data, chunk("IEND", nil)...)
_, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
}
func TestValidateRejectsTruncatedJPEG(t *testing.T) {
data := encodeJPEG(t, 64, 64)
_, err := imagecheck.Validate(bytes.NewReader(data[:len(data)-32]), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
}
func TestValidateRejectsTooManyPixels(t *testing.T) {
_, err := imagecheck.Validate(bytes.NewReader(encodePNG(t, 100, 100)), 9_999)
require.ErrorIs(t, err, imagecheck.ErrTooLarge)
_, err = imagecheck.Validate(bytes.NewReader(encodeJPEG(t, 100, 100)), 9_999)
require.ErrorIs(t, err, imagecheck.ErrTooLarge)
}
func TestValidateAllowsTrailingBytes(t *testing.T) {
pngData := append(encodePNG(t, 8, 8), []byte("trailing")...)
_, err := imagecheck.Validate(bytes.NewReader(pngData), 40_000_000)
require.NoError(t, err)
jpegData := append(encodeJPEG(t, 8, 8), []byte("trailing")...)
_, err = imagecheck.Validate(bytes.NewReader(jpegData), 40_000_000)
require.NoError(t, err)
}