forked from mtusi2026/mtusi2026lab5
test(worker): add retry and persistence checks and adapt attachment tests for async processing
This commit is contained in:
@@ -1,122 +0,0 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestThumbnail(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
width, height, wantWidth, wantHeight int
|
||||
}{
|
||||
{"landscape", 320, 200, 256, 160}, {"portrait", 201, 321, 160, 256},
|
||||
{"small", 12, 7, 12, 7}, {"thin", 1, 1000, 1, 256},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var source bytes.Buffer
|
||||
if err := png.Encode(&source, image.NewNRGBA(image.Rect(0, 0, tc.width, tc.height))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, width, height, err := thumbnail(bytes.NewReader(source.Bytes()), 40000000)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if width != tc.wantWidth || height != tc.wantHeight {
|
||||
t.Fatalf("got %dx%d", width, height)
|
||||
}
|
||||
|
||||
decoded, err := jpeg.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if decoded.Bounds().Dx() != width || decoded.Bounds().Dy() != height {
|
||||
t.Fatal("JPEG dimensions disagree")
|
||||
}
|
||||
|
||||
r, g, b, _ := decoded.At(0, 0).RGBA()
|
||||
if r < 65000 || g < 65000 || b < 65000 {
|
||||
t.Fatal("transparent source was not composited on white")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestThumbnailEXIF(t *testing.T) {
|
||||
src := image.NewRGBA(image.Rect(0, 0, 40, 20))
|
||||
|
||||
for y := 0; y < 20; y++ {
|
||||
for x := 0; x < 40; x++ {
|
||||
c := color.RGBA{255, 0, 0, 255}
|
||||
if x >= 20 {
|
||||
c = color.RGBA{0, 0, 255, 255}
|
||||
}
|
||||
|
||||
src.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
|
||||
var original bytes.Buffer
|
||||
if err := jpeg.Encode(&original, src, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Little-endian TIFF, orientation=6 (90 degrees clockwise).
|
||||
exif := []byte{
|
||||
'E', 'x', 'i', 'f', 0, 0, 'I', 'I',
|
||||
42, 0, 8, 0, 0, 0, 1, 0,
|
||||
0x12, 1, 3, 0, 1, 0, 0, 0,
|
||||
6, 0, 0, 0, 0, 0, 0, 0,
|
||||
}
|
||||
segment := []byte{0xff, 0xe1, 0, 0}
|
||||
binary.BigEndian.PutUint16(segment[2:], uint16(len(exif)+2))
|
||||
data := append([]byte{}, original.Bytes()[:2]...)
|
||||
data = append(data, segment...)
|
||||
data = append(data, exif...)
|
||||
data = append(data, original.Bytes()[2:]...)
|
||||
result, width, height, err := thumbnail(bytes.NewReader(data), 40000000)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if width != 20 || height != 40 {
|
||||
t.Fatalf("orientation ignored: %dx%d", width, height)
|
||||
}
|
||||
|
||||
decoded, err := jpeg.Decode(bytes.NewReader(result))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _, b, _ := decoded.At(10, 5).RGBA()
|
||||
if r <= b {
|
||||
t.Fatal("incorrect EXIF rotation")
|
||||
}
|
||||
|
||||
if bytes.Contains(result, []byte("Exif")) {
|
||||
t.Fatal("EXIF retained")
|
||||
}
|
||||
}
|
||||
|
||||
func TestThumbnailRejectsInvalidSource(t *testing.T) {
|
||||
var source bytes.Buffer
|
||||
if err := png.Encode(&source, image.NewNRGBA(image.Rect(0, 0, 100, 100))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, _, _, err := thumbnail(bytes.NewReader(source.Bytes()), 9999); err == nil {
|
||||
t.Fatal("pixel limit ignored")
|
||||
}
|
||||
|
||||
if _, _, _, err := thumbnail(bytes.NewReader(source.Bytes()[:20]), 40000000); err == nil {
|
||||
t.Fatal("truncated image accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user