Files
mtusi2026lab6/internal/messenger/transform_test.go
T

119 lines
3.3 KiB
Go

package messenger
import (
"bytes"
"encoding/binary"
"hash/crc32"
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestFitFloorNoUpscale(t *testing.T) {
for _, v := range [][5]int{{320, 200, 256, 256, 160}, {3, 2, 256, 3, 2}, {301, 200, 256, 256, 170}, {1, 40000, 256, 1, 256}} {
w, h := fitDimensions(v[0], v[1], v[2])
if w != v[3] || h != v[4] {
t.Fatalf("fit %v got %dx%d", v, w, h)
}
}
}
func TestTransformAlphaAndEXIF(t *testing.T) {
if _, e := exec.LookPath("cwebp"); e != nil {
t.Skip("cwebp provided by Docker test target")
}
dir := t.TempDir()
input := filepath.Join(dir, "input.png")
f, _ := os.Create(input)
png.Encode(f, image.NewNRGBA(image.Rect(0, 0, 320, 200)))
f.Close()
if e := Transform(input, dir); e != nil {
t.Fatal(e)
}
b, _ := os.ReadFile(filepath.Join(dir, "thumbnail"))
img, e := jpeg.Decode(bytes.NewReader(b))
if e != nil {
t.Fatal(e)
}
r, g, bl, _ := img.At(20, 20).RGBA()
if r < 65000 || g < 65000 || bl < 65000 {
t.Fatal("alpha not composited white")
}
if img.Bounds().Dx() != 256 || img.Bounds().Dy() != 160 {
t.Fatal(img.Bounds())
}
source := image.NewNRGBA(image.Rect(0, 0, 320, 200))
source.Set(0, 0, color.White)
var jpg bytes.Buffer
jpeg.Encode(&jpg, source, nil)
// APP1 Exif, 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}
var oriented bytes.Buffer
oriented.Write(jpg.Bytes()[:2])
oriented.Write([]byte{255, 225})
binary.Write(&oriented, binary.BigEndian, uint16(len(exif)+2))
oriented.Write(exif)
oriented.Write(jpg.Bytes()[2:])
os.WriteFile(input, oriented.Bytes(), 0600)
if e = Transform(input, dir); e != nil {
t.Fatal(e)
}
b, _ = os.ReadFile(filepath.Join(dir, "thumbnail"))
img, e = jpeg.Decode(bytes.NewReader(b))
if e != nil {
t.Fatal(e)
}
if img.Bounds().Dx() != 160 || img.Bounds().Dy() != 256 {
t.Fatalf("EXIF ignored: %v", img.Bounds())
}
}
func TestPNGEXIFAndMalformedIFD(t *testing.T) {
tiff := []byte{'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}
if tiffOrientation(tiff) != 6 {
t.Fatal("TIFF orientation")
}
for i := 0; i < len(tiff); i++ {
_ = tiffOrientation(tiff[:i])
}
bad := append([]byte{}, tiff...)
for i := 4; i < 8; i++ {
bad[i] = 255
}
if tiffOrientation(bad) != 1 {
t.Fatal("bad IFD offset")
}
var raw bytes.Buffer
png.Encode(&raw, image.NewNRGBA(image.Rect(0, 0, 320, 200)))
var chunk bytes.Buffer
binary.Write(&chunk, binary.BigEndian, uint32(len(tiff)))
chunk.WriteString("eXIf")
chunk.Write(tiff)
binary.Write(&chunk, binary.BigEndian, crc32.ChecksumIEEE(chunk.Bytes()[4:]))
data := append(append(append([]byte{}, raw.Bytes()[:33]...), chunk.Bytes()...), raw.Bytes()[33:]...)
dir := t.TempDir()
input := filepath.Join(dir, "input.png")
os.WriteFile(input, data, 0600)
if pngOrientation(input) != 6 {
t.Fatal("PNG eXIf orientation")
}
if _, e := exec.LookPath("cwebp"); e != nil {
return
}
if e := Transform(input, dir); e != nil {
t.Fatal(e)
}
b, _ := os.ReadFile(filepath.Join(dir, "thumbnail"))
img, e := jpeg.Decode(bytes.NewReader(b))
if e != nil {
t.Fatal(e)
}
if img.Bounds().Dx() != 160 || img.Bounds().Dy() != 256 {
t.Fatal("PNG orientation not applied", img.Bounds())
}
}