| 1 | package imagefile |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // Fable's round on 49d8df3: M2 (a GIF frame past its logical screen) |
| 10 | // and L10 (data the walkers kept that names nothing a picture needs). |
| 11 | |
| 12 | // TestAGIFFrameStaysInsideItsScreen: a 35-byte GIF that says 1×1 but |
| 13 | // carries a 60,000×60,000 frame is refused; the limits read only the |
| 14 | // logical screen. |
| 15 | func TestAGIFFrameStaysInsideItsScreen(t *testing.T) { |
| 16 | gif := []byte("GIF89a\x01\x00\x01\x00\x00\x00\x00" + |
| 17 | "\x2c\x00\x00\x00\x00\x60\xea\x60\xea\x00" + |
| 18 | "\x02\x01\x00\x00\x3b") |
| 19 | if _, _, err := Clean(gif); err != ErrMalformed { |
| 20 | t.Fatalf("a frame past the screen: %v", err) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // replaceSegment puts payload in place of the first segment marker |
| 25 | // holds, in a JPEG. |
| 26 | func withSegment(t *testing.T, raw []byte, marker byte, |
| 27 | change func([]byte) []byte) []byte { |
| 28 | t.Helper() |
| 29 | for at := 2; at+4 < len(raw); { |
| 30 | length := int(binary.BigEndian.Uint16(raw[at+2:])) |
| 31 | if raw[at+1] == marker { |
| 32 | payload := change(append([]byte{}, raw[at+4:at+2+length]...)) |
| 33 | head := []byte{0xff, marker, 0, 0} |
| 34 | binary.BigEndian.PutUint16(head[2:], uint16(len(payload)+2)) |
| 35 | out := append(append([]byte{}, raw[:at]...), head...) |
| 36 | out = append(out, payload...) |
| 37 | return append(out, raw[at+2+length:]...) |
| 38 | } |
| 39 | at += 2 + length |
| 40 | } |
| 41 | t.Fatalf("no segment %x", marker) |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | func TestAdobesSegmentKeepsOnlyItsHeader(t *testing.T) { |
| 46 | raw := withSegment(t, read(t, "cmyk.jpg"), markerAPPE, |
| 47 | func(payload []byte) []byte { |
| 48 | return append(payload, []byte("CANARY-ADOBE-TAIL")...) |
| 49 | }) |
| 50 | cleaned, _, err := Clean(raw) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if bytes.Contains(cleaned, []byte("CANARY")) { |
| 55 | t.Error("data after Adobe's header was kept") |
| 56 | } |
| 57 | samePixels(t, raw, cleaned) |
| 58 | } |
| 59 | |
| 60 | func TestNetscapesLoopKeepsOnlyItsCount(t *testing.T) { |
| 61 | raw := read(t, "anim.gif") |
| 62 | at := bytes.Index(raw, []byte("NETSCAPE2.0")) |
| 63 | // The loop sub-block, then one more sub-block with a canary in it. |
| 64 | loop := at + 11 |
| 65 | extra := append([]byte{byte(len("CANARY-LOOP"))}, "CANARY-LOOP"...) |
| 66 | end := loop + 1 + int(raw[loop]) |
| 67 | grown := append(append(append([]byte{}, raw[:end]...), extra...), |
| 68 | raw[end:]...) |
| 69 | cleaned, _, err := Clean(grown) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if bytes.Contains(cleaned, []byte("CANARY")) { |
| 74 | t.Error("an extra NETSCAPE2.0 sub-block was kept") |
| 75 | } |
| 76 | if !bytes.Contains(cleaned, []byte("NETSCAPE2.0")) { |
| 77 | t.Error("the loop count was lost") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestAWebPFrameKeepsOnlyItsPicture(t *testing.T) { |
| 82 | raw := read(t, "anim.webp") |
| 83 | at := bytes.Index(raw, []byte("ANMF")) |
| 84 | size := int(binary.LittleEndian.Uint32(raw[at+4:])) |
| 85 | inner := append([]byte("XTRA"), 0, 0, 0, 0) |
| 86 | binary.LittleEndian.PutUint32(inner[4:], uint32(len("CANARY-FRAME!!"))) |
| 87 | inner = append(inner, "CANARY-FRAME!!"...) |
| 88 | end := at + 8 + size |
| 89 | grown := append(append(append([]byte{}, raw[:end]...), inner...), |
| 90 | raw[end:]...) |
| 91 | binary.LittleEndian.PutUint32(grown[at+4:], uint32(size+len(inner))) |
| 92 | binary.LittleEndian.PutUint32(grown[4:], uint32(len(grown)-8)) |
| 93 | cleaned, _, err := Clean(grown) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | if bytes.Contains(cleaned, []byte("CANARY")) { |
| 98 | t.Error("an unknown chunk inside a frame was kept") |
| 99 | } |
| 100 | sameFrames(t, raw, cleaned) |
| 101 | } |
| 102 | |