forked from mtusi2026/mtusi2026lab5
107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package imaging
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"testing"
|
|
|
|
"golang.org/x/sync/semaphore"
|
|
)
|
|
|
|
func lim() Limits {
|
|
return Limits{MaxBytes: 10 << 20, MaxPixels: 40_000_000, Budget: semaphore.NewWeighted(256 << 20), BudgetSize: 256 << 20}
|
|
}
|
|
|
|
func TestFit(t *testing.T) {
|
|
cases := []struct{ w, h, box, ew, eh int }{
|
|
{320, 200, 256, 256, 160},
|
|
{200, 320, 256, 160, 256},
|
|
{100, 50, 256, 100, 50},
|
|
{5000, 1, 256, 256, 1},
|
|
{1, 5000, 1280, 1, 1280},
|
|
}
|
|
for _, c := range cases {
|
|
if w, h := Fit(c.w, c.h, c.box); w != c.ew || h != c.eh {
|
|
t.Errorf("Fit(%d,%d,%d)=%d,%d want %d,%d", c.w, c.h, c.box, w, h, c.ew, c.eh)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessPNGWithAlpha(t *testing.T) {
|
|
src := image.NewNRGBA(image.Rect(0, 0, 320, 200))
|
|
var buf bytes.Buffer
|
|
_ = png.Encode(&buf, src)
|
|
outs, err := Process(context.Background(), buf.Bytes(), "image/png", lim())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string][2]int{"thumbnail": {256, 160}, "crop": {128, 128}, "optimized": {320, 200}}
|
|
for _, o := range outs {
|
|
if d := want[o.Name]; d != [2]int{o.Width, o.Height} {
|
|
t.Errorf("%s: %dx%d want %v", o.Name, o.Width, o.Height, d)
|
|
}
|
|
if o.Name == "thumbnail" {
|
|
img, err := jpeg.Decode(bytes.NewReader(o.Data))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if img.Bounds().Dx() != 256 || img.Bounds().Dy() != 160 {
|
|
t.Fatalf("thumbnail real size %v", img.Bounds())
|
|
}
|
|
r, g, b, _ := img.At(10, 10).RGBA()
|
|
if r>>8 < 245 || g>>8 < 245 || b>>8 < 245 {
|
|
t.Errorf("alpha not composited on white: %d %d %d", r>>8, g>>8, b>>8)
|
|
}
|
|
}
|
|
}
|
|
if len(outs) != 3 {
|
|
t.Fatalf("got %d variants", len(outs))
|
|
}
|
|
}
|
|
|
|
func TestExifOrientation(t *testing.T) {
|
|
src := image.NewRGBA(image.Rect(0, 0, 320, 200))
|
|
for y := 0; y < 200; y++ {
|
|
for x := 0; x < 320; x++ {
|
|
src.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
|
|
}
|
|
}
|
|
var buf bytes.Buffer
|
|
_ = jpeg.Encode(&buf, src, nil)
|
|
data := buf.Bytes()
|
|
tiff := []byte{'M', 'M', 0, 42, 0, 0, 0, 8, 0, 1, 0x01, 0x12, 0, 3, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
seg := append([]byte("Exif\x00\x00"), tiff...)
|
|
app1 := append([]byte{0xff, 0xe1, byte((len(seg) + 2) >> 8), byte(len(seg) + 2)}, seg...)
|
|
withExif := append(append([]byte{0xff, 0xd8}, app1...), data[2:]...)
|
|
if o := jpegOrientation(withExif); o != 6 {
|
|
t.Fatalf("orientation %d", o)
|
|
}
|
|
outs, err := Process(context.Background(), withExif, "image/jpeg", lim())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if outs[0].Width != 160 || outs[0].Height != 256 {
|
|
t.Fatalf("thumbnail %dx%d, want 160x256", outs[0].Width, outs[0].Height)
|
|
}
|
|
}
|
|
|
|
func TestRejects(t *testing.T) {
|
|
if _, err := Process(context.Background(), []byte("\x89PNG\r\n\x1a\nnot really"), "image/png", lim()); err == nil {
|
|
t.Fatal("corrupt PNG accepted")
|
|
} else if pe, ok := err.(*PermanentError); !ok || pe.Code != "invalid_image" {
|
|
t.Fatalf("unexpected error %v", err)
|
|
}
|
|
l := lim()
|
|
l.MaxPixels = 100
|
|
src := image.NewRGBA(image.Rect(0, 0, 20, 20))
|
|
var buf bytes.Buffer
|
|
_ = png.Encode(&buf, src)
|
|
if _, err := Process(context.Background(), buf.Bytes(), "image/png", l); err == nil {
|
|
t.Fatal("pixel limit not enforced")
|
|
}
|
|
}
|