Cotton code
132 lines · 4.4 KB · GoRaw
package mainimport (	"bytes"	"image"	"image/color"	"image/jpeg"	"image/png"	"testing"	"go.mau.fi/whatsmeow")// A picture of rows of colour: the left third red, the rest blue.func leftRed(w, h int, alpha uint8) *image.NRGBA {	img := image.NewNRGBA(image.Rect(0, 0, w, h))	for y := 0; y < h; y++ {		for x := 0; x < w; x++ {			c := color.NRGBA{0, 0, 255, alpha}			if x < w/3 {				c = color.NRGBA{255, 0, 0, alpha}			}			img.SetNRGBA(x, y, c)		}	}	return img}func jpegOf(t *testing.T, img image.Image, orientation uint16) []byte {	t.Helper()	var out bytes.Buffer	if err := jpeg.Encode(&out, img, &jpeg.Options{Quality: 90}); err != nil {		t.Fatal(err)	}	data := out.Bytes()	if orientation == 0 {		return data	}	// An EXIF block, as a camera writes it, right after the start: one entry, the orientation.	tiff := []byte{'M', 'M', 0, 42, 0, 0, 0, 8, 0, 1, 0x01, 0x12, 0, 3, 0, 0, 0, 1, byte(orientation >> 8), byte(orientation), 0, 0, 0, 0, 0, 0}	segment := append([]byte("Exif\x00\x00"), tiff...)	app1 := append([]byte{0xFF, 0xE1, byte((len(segment) + 2) >> 8), byte(len(segment) + 2)}, segment...)	return append(append(append([]byte{}, data[:2]...), app1...), data[2:]...)}func decoded(t *testing.T, thumb []byte) image.Image {	t.Helper()	img, err := jpeg.Decode(bytes.NewReader(thumb))	if err != nil {		t.Fatalf("the thumbnail is no JPEG: %v", err)	}	return img}func red(c color.Color) bool {	r, _, b, _ := c.RGBA()	return r > 0xA000 && b < 0x6000}// RULE (owner, 24 Nov 2026: a photo sent from Comms showed on WhatsApp Web with the wrong aspect ratio): WhatsApp is// told the shape of the picture that goes, as it is seen, with a thumbnail of the same shape, upright.func TestAPicturesShapeIsReadFromTheFileAsItIsSeen(t *testing.T) {	// Tall, as stored.	w, h, thumb, ok := pictureShape(jpegOf(t, leftRed(300, 900, 255), 0))	if !ok || w != 300 || h != 900 {		t.Fatalf("tall: %d x %d ok=%v", w, h, ok)	}	if b := decoded(t, thumb).Bounds(); b.Dx() != 33 || b.Dy() != 100 {		t.Fatalf("tall thumbnail %v", b)	}	// Stored wide on its side, seen tall (EXIF 6, a phone held upright): tall, and the thumbnail turned so the stored	// left edge (red) is at the top.	w, h, thumb, ok = pictureShape(jpegOf(t, leftRed(900, 300, 255), 6))	if !ok || w != 300 || h != 900 {		t.Fatalf("turned: %d x %d ok=%v", w, h, ok)	}	img := decoded(t, thumb)	if b := img.Bounds(); b.Dx() != 33 || b.Dy() != 100 {		t.Fatalf("turned thumbnail %v", b)	}	if !red(img.At(16, 5)) || red(img.At(16, 90)) {		t.Fatal("the thumbnail is not upright")	}	// Wide and see-through (a PNG): wide, the thumbnail over white.	var p bytes.Buffer	if err := png.Encode(&p, leftRed(1200, 400, 128)); err != nil {		t.Fatal(err)	}	w, h, thumb, ok = pictureShape(p.Bytes())	if !ok || w != 1200 || h != 400 {		t.Fatalf("wide: %d x %d ok=%v", w, h, ok)	}	if b := decoded(t, thumb).Bounds(); b.Dx() != 100 || b.Dy() != 33 {		t.Fatalf("wide thumbnail %v", b)	}	// Not a picture Go reads (WebP, here bytes that say so): nothing said.	if _, _, _, ok := pictureShape([]byte("RIFF\x00\x00\x00\x00WEBPVP8 ")); ok {		t.Fatal("an unreadable file has no shape")	}}// The message WhatsApp gets carries the file's shape over the phone's row (here the row named the stored sides and a// square preview of its own).func TestASentPictureCarriesTheFilesShapeNotThePhones(t *testing.T) {	square := jpegOf(t, leftRed(32, 32, 255), 0)	req := &Request{Kind: "picture", Mimetype: "image/jpeg", Width: 900, Height: 300, Preview: square}	req.takeShapeOf(jpegOf(t, leftRed(900, 300, 255), 6))	msg, _ := mediaMessage(req, whatsmeow.UploadResponse{URL: "u", DirectPath: "/d"})	image := msg.GetImageMessage()	if image.GetWidth() != 300 || image.GetHeight() != 900 {		t.Fatalf("sides %d x %d", image.GetWidth(), image.GetHeight())	}	if b := decoded(t, image.GetJPEGThumbnail()).Bounds(); b.Dx() != 33 || b.Dy() != 100 {		t.Fatalf("thumbnail %v", b)	}	// A file Go cannot read keeps the row's.	kept := &Request{Kind: "picture", Mimetype: "image/webp", Width: 640, Height: 480, Preview: square}	kept.takeShapeOf([]byte("RIFF\x00\x00\x00\x00WEBPVP8 "))	if kept.Width != 640 || kept.Height != 480 || !bytes.Equal(kept.Preview, square) {		t.Fatal("the row's shape is kept for a file Go cannot read")	}	// A video is not touched.	video := &Request{Kind: "video", Width: 640, Height: 480}	video.takeShapeOf(jpegOf(t, leftRed(900, 300, 255), 0))	if video.Width != 640 {		t.Fatal("a video keeps its row's shape")	}}