| 1 | package imagefile |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "image" |
| 6 | "image/color" |
| 7 | "image/png" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // IMG5 (images spec §6): every fixture loses its canaries, keeps its |
| 15 | // ICC profile and (JPEG) its orientation, and decodes to the same |
| 16 | // pixels. Mapa itself never decodes; the test does, before and after. |
| 17 | |
| 18 | type fixture struct { |
| 19 | name, kind string |
| 20 | icc bool |
| 21 | orientation int |
| 22 | width, height int |
| 23 | } |
| 24 | |
| 25 | var fixtures = []fixture{ |
| 26 | {name: "meta.png", kind: PNG, icc: true, width: 64, height: 48}, |
| 27 | {name: "anim.png", kind: PNG, width: 64, height: 48}, |
| 28 | {name: "baseline.jpg", kind: JPEG, icc: true, orientation: 6, |
| 29 | width: 48, height: 64}, |
| 30 | {name: "progressive.jpg", kind: JPEG, icc: true, orientation: 6, |
| 31 | width: 48, height: 64}, |
| 32 | {name: "restart.jpg", kind: JPEG, icc: true, orientation: 6, |
| 33 | width: 48, height: 64}, |
| 34 | {name: "cmyk.jpg", kind: JPEG, icc: true, orientation: 6, width: 48, |
| 35 | height: 64}, |
| 36 | {name: "thumbnail.jpg", kind: JPEG, width: 64, height: 48}, |
| 37 | {name: "motion.jpg", kind: JPEG, icc: true, orientation: 6, width: 48, |
| 38 | height: 64}, |
| 39 | {name: "lossy.webp", kind: WebP, icc: true, width: 64, height: 48}, |
| 40 | {name: "lossless.webp", kind: WebP, icc: true, width: 64, height: 48}, |
| 41 | {name: "anim.webp", kind: WebP, icc: true, width: 64, height: 48}, |
| 42 | {name: "anim.gif", kind: GIF, width: 64, height: 48}, |
| 43 | } |
| 44 | |
| 45 | func read(t *testing.T, name string) []byte { |
| 46 | t.Helper() |
| 47 | raw, err := os.ReadFile(filepath.Join("testdata", name)) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | return raw |
| 52 | } |
| 53 | |
| 54 | func TestCleanStripsEveryCanary(t *testing.T) { |
| 55 | for _, each := range fixtures { |
| 56 | t.Run(each.name, func(t *testing.T) { |
| 57 | raw := read(t, each.name) |
| 58 | if !bytes.Contains(raw, []byte("CANARY-")) { |
| 59 | t.Fatal("the fixture carries no canary") |
| 60 | } |
| 61 | cleaned, info, err := Clean(raw) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if at := bytes.Index(cleaned, []byte("CANARY")); at >= 0 { |
| 66 | t.Errorf("a canary survived: %q", |
| 67 | cleaned[at:min(at+24, len(cleaned))]) |
| 68 | } |
| 69 | want := Info{Type: each.kind, Width: each.width, |
| 70 | Height: each.height} |
| 71 | if info != want { |
| 72 | t.Errorf("info %+v, want %+v", info, want) |
| 73 | } |
| 74 | checkKept(t, each, cleaned) |
| 75 | if each.name == "anim.webp" { |
| 76 | sameFrames(t, raw, cleaned) |
| 77 | return |
| 78 | } |
| 79 | samePixels(t, raw, cleaned) |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // checkKept: the ICC profile stays, and a JPEG's orientation is the |
| 85 | // only EXIF left. |
| 86 | func checkKept(t *testing.T, each fixture, cleaned []byte) { |
| 87 | t.Helper() |
| 88 | profile := map[string]string{PNG: "iCCP", JPEG: "ICC_PROFILE", |
| 89 | WebP: "ICCP"}[each.kind] |
| 90 | if profile != "" && |
| 91 | each.icc != bytes.Contains(cleaned, []byte(profile)) { |
| 92 | t.Errorf("ICC profile kept: %v, want %v", !each.icc, each.icc) |
| 93 | } |
| 94 | if each.kind != JPEG { |
| 95 | return |
| 96 | } |
| 97 | at := bytes.Index(cleaned, []byte("Exif\x00\x00")) |
| 98 | switch { |
| 99 | case each.orientation == 0 && at >= 0: |
| 100 | t.Error("an upright JPEG kept an EXIF segment") |
| 101 | case each.orientation != 0 && at < 0: |
| 102 | t.Error("the orientation was lost") |
| 103 | case each.orientation != 0 && |
| 104 | orientation(cleaned[at+6:]) != each.orientation: |
| 105 | t.Errorf("orientation %d", orientation(cleaned[at+6:])) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func samePixels(t *testing.T, before, after []byte) { |
| 110 | t.Helper() |
| 111 | old, _, err := image.Decode(bytes.NewReader(before)) |
| 112 | if err != nil { |
| 113 | t.Fatalf("decoding the original: %v", err) |
| 114 | } |
| 115 | fresh, _, err := image.Decode(bytes.NewReader(after)) |
| 116 | if err != nil { |
| 117 | t.Fatalf("decoding the cleaned file: %v", err) |
| 118 | } |
| 119 | if old.Bounds() != fresh.Bounds() { |
| 120 | t.Fatalf("bounds %v became %v", old.Bounds(), fresh.Bounds()) |
| 121 | } |
| 122 | bounds := old.Bounds() |
| 123 | for row := bounds.Min.Y; row < bounds.Max.Y; row++ { |
| 124 | for column := bounds.Min.X; column < bounds.Max.X; column++ { |
| 125 | if !sameColour(old.At(column, row), fresh.At(column, row)) { |
| 126 | t.Fatalf("pixel (%d, %d) changed", column, row) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // sameFrames is samePixels for an animated WebP, which Go's decoder |
| 133 | // does not draw: every frame chunk is kept byte for byte. |
| 134 | func sameFrames(t *testing.T, before, after []byte) { |
| 135 | t.Helper() |
| 136 | frames := 0 |
| 137 | for at := 12; at < len(before); { |
| 138 | kind, next, err := webpChunk(before, at, len(before)) |
| 139 | if err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | if kind == "ANMF" { |
| 143 | frames++ |
| 144 | if !bytes.Contains(after, before[at:next]) { |
| 145 | t.Errorf("frame %d changed", frames) |
| 146 | } |
| 147 | } |
| 148 | at = next |
| 149 | } |
| 150 | if frames < 2 { |
| 151 | t.Errorf("%d frames", frames) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func sameColour(left, right color.Color) bool { |
| 156 | red, green, blue, alpha := left.RGBA() |
| 157 | red2, green2, blue2, alpha2 := right.RGBA() |
| 158 | return red == red2 && green == green2 && blue == blue2 && |
| 159 | alpha == alpha2 |
| 160 | } |
| 161 | |
| 162 | func TestRefusals(t *testing.T) { |
| 163 | jpeg := read(t, "baseline.jpg") |
| 164 | pngFile := read(t, "meta.png") |
| 165 | webp := read(t, "lossy.webp") |
| 166 | cases := map[string]struct { |
| 167 | raw []byte |
| 168 | want error |
| 169 | }{ |
| 170 | "svg": {[]byte(`<svg xmlns="http://www.w3.org/2000/svg">` + |
| 171 | `<script>alert(1)</script></svg>`), ErrType}, |
| 172 | "html": {[]byte("<!doctype html><p>hi"), ErrType}, |
| 173 | "heic": {append([]byte("\x00\x00\x00\x18ftypheic"), |
| 174 | make([]byte, 64)...), ErrType}, |
| 175 | "truncated jpeg": {jpeg[:len(jpeg)/2], ErrMalformed}, |
| 176 | "truncated png": {pngFile[:len(pngFile)/2], ErrMalformed}, |
| 177 | "truncated webp": {webp[:len(webp)/2], ErrMalformed}, |
| 178 | "png bad crc": {flip(pngFile, 40), ErrMalformed}, |
| 179 | "jpeg bad length": {oversized(jpeg), ErrMalformed}, |
| 180 | "webp bad size": {riffSize(webp, 1<<30), ErrMalformed}, |
| 181 | "too wide": {encodePNG(t, MaxSide+1, 1), ErrDimensions}, |
| 182 | "too many pixels": {encodePNG(t, 7100, 7100), ErrDimensions}, |
| 183 | "png then nothing": {[]byte(pngSignature), ErrMalformed}, |
| 184 | } |
| 185 | for name, each := range cases { |
| 186 | if _, _, err := Clean(each.raw); err != each.want { |
| 187 | t.Errorf("%s: %v, want %v", name, err, each.want) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func flip(raw []byte, at int) []byte { |
| 193 | out := append([]byte{}, raw...) |
| 194 | out[at] ^= 0xff |
| 195 | return out |
| 196 | } |
| 197 | |
| 198 | // oversized gives the JPEG's first segment a length past the file. |
| 199 | func oversized(raw []byte) []byte { |
| 200 | out := append([]byte{}, raw...) |
| 201 | out[4], out[5] = 0xff, 0xff |
| 202 | return out |
| 203 | } |
| 204 | |
| 205 | func riffSize(raw []byte, size uint32) []byte { |
| 206 | out := append([]byte{}, raw...) |
| 207 | out[4], out[5], out[6], out[7] = byte(size), byte(size>>8), |
| 208 | byte(size>>16), byte(size>>24) |
| 209 | return out |
| 210 | } |
| 211 | |
| 212 | func encodePNG(t *testing.T, width, height int) []byte { |
| 213 | t.Helper() |
| 214 | var out bytes.Buffer |
| 215 | picture := image.NewGray(image.Rect(0, 0, width, height)) |
| 216 | if err := png.Encode(&out, picture); err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | return out.Bytes() |
| 220 | } |
| 221 | |
| 222 | func TestSniffIgnoresWhatIsClaimed(t *testing.T) { |
| 223 | if Sniff([]byte("GIF89a")) != GIF || Sniff([]byte("hello")) != "" { |
| 224 | t.Error("Sniff reads the bytes") |
| 225 | } |
| 226 | if !strings.HasPrefix(string(read(t, "lossy.webp")[8:12]), "WEBP") { |
| 227 | t.Error("fixture") |
| 228 | } |
| 229 | } |
| 230 | |