Files
tenant-registry/internal/branding/image_test.go
T
sharang 9685ec0475
ci / shared (push) Successful in 14s
ci / test (push) Successful in 8m17s
ci / image (push) Successful in 46s
feat(branding): tenant branding store, endpoints and check field (#155, part 1) (#156)
2026-12-01 13:13:22 +00:00

120 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package branding
import (
"bytes"
"encoding/binary"
"errors"
"image"
"image/color"
"image/png"
"testing"
)
func encodePNG(t *testing.T, img image.Image) []byte {
t.Helper()
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func solid(w, h int, c color.NRGBA) *image.NRGBA {
img := image.NewNRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
img.SetNRGBA(x, y, c)
}
}
return img
}
// ico wraps one entry (PNG bytes or a DIB) into an ICO file.
func ico(w, h, bits int, entry []byte) []byte {
var b bytes.Buffer
_ = binary.Write(&b, binary.LittleEndian, []uint16{0, 1, 1})
b.Write([]byte{byte(w % 256), byte(h % 256), 0, 0})
_ = binary.Write(&b, binary.LittleEndian, []uint16{1, uint16(bits)})
_ = binary.Write(&b, binary.LittleEndian, []uint32{uint32(len(entry)), 22})
b.Write(entry)
return b.Bytes()
}
// dib32 is a 32-bit BMP entry: a red left half, a transparent right half.
func dib32(w, h int) []byte {
var b bytes.Buffer
_ = binary.Write(&b, binary.LittleEndian, []uint32{40, uint32(w), uint32(2 * h)})
_ = binary.Write(&b, binary.LittleEndian, []uint16{1, 32})
_ = binary.Write(&b, binary.LittleEndian, []uint32{0, 0, 0, 0, 0, 0})
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
if x < w/2 {
b.Write([]byte{0, 0, 255, 255}) // BGRA red
} else {
b.Write([]byte{0, 0, 0, 0})
}
}
}
b.Write(make([]byte, ((w+31)/32)*4*h)) // AND mask
return b.Bytes()
}
func TestNormalizeLogo(t *testing.T) {
red := color.NRGBA{R: 255, A: 255}
t.Run("deterministic 256 square, aspect kept", func(t *testing.T) {
src := encodePNG(t, solid(100, 50, red))
a, ea, err := NormalizeLogo(src)
if err != nil {
t.Fatal(err)
}
b, eb, _ := NormalizeLogo(src)
if !bytes.Equal(a, b) || ea != eb || len(ea) != 64 {
t.Fatalf("not deterministic: %s %s", ea, eb)
}
img, _ := png.Decode(bytes.NewReader(a))
if img.Bounds().Dx() != 256 || img.Bounds().Dy() != 256 {
t.Fatalf("size %v", img.Bounds())
}
// 100×50 fits as 256×128, centred: the top rows are transparent.
if _, _, _, alpha := img.At(128, 10).RGBA(); alpha != 0 {
t.Fatalf("top is not transparent: %v", img.At(128, 10))
}
if r, _, _, alpha := img.At(128, 128).RGBA(); alpha != 0xffff || r != 0xffff {
t.Fatalf("centre is not red: %v", img.At(128, 128))
}
})
t.Run("ICO with a PNG entry", func(t *testing.T) {
if _, _, err := NormalizeLogo(ico(32, 32, 32, encodePNG(t, solid(32, 32, red)))); err != nil {
t.Fatal(err)
}
})
t.Run("ICO with a 32-bit BMP entry", func(t *testing.T) {
img, err := DecodeImage(ico(16, 16, 32, dib32(16, 16)))
if err != nil {
t.Fatal(err)
}
if c := color.NRGBAModel.Convert(img.At(2, 3)).(color.NRGBA); c != red {
t.Fatalf("left %v", c)
}
if c := color.NRGBAModel.Convert(img.At(12, 3)).(color.NRGBA); c.A != 0 {
t.Fatalf("right %v", c)
}
})
t.Run("refused", func(t *testing.T) {
for name, tc := range map[string]struct {
data []byte
want error
}{
"svg": {[]byte(`<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg"></svg>`), ErrSVG},
"text": {[]byte("hello"), ErrNotImage},
"too big": {make([]byte, MaxImageBytes+1), ErrImageTooLarge},
"short ico": {[]byte{0, 0, 1, 0, 5, 0}, ErrNotImage},
"bad entry": {ico(16, 16, 32, []byte{1, 2, 3}), ErrNotImage},
} {
if _, _, err := NormalizeLogo(tc.data); !errors.Is(err, tc.want) {
t.Errorf("%s: %v, want %v", name, err, tc.want)
}
}
})
}