|
| 1 | +package imageproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "image" |
| 6 | + "image/jpeg" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// syntheticProfile builds an ICC blob of n bytes whose header declares the |
| 11 | +// given data color space. |
| 12 | +func syntheticProfile(n int, space string) []byte { |
| 13 | + p := make([]byte, n) |
| 14 | + if n >= 20 { |
| 15 | + copy(p[16:20], space) |
| 16 | + } |
| 17 | + for i := 20; i < n; i++ { |
| 18 | + p[i] = byte(i % 251) |
| 19 | + } |
| 20 | + return p |
| 21 | +} |
| 22 | + |
| 23 | +func TestICCRoundTrip(t *testing.T) { |
| 24 | + // a minimal JPEG skeleton: SOI, a DQT-ish segment, SOS, EOI |
| 25 | + base := []byte{0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x04, 0xAA, 0xBB, 0xFF, 0xDA, 0x00, 0x02, 0xFF, 0xD9} |
| 26 | + |
| 27 | + for _, size := range []int{1, iccHeaderSize, maxICCChunk, maxICCChunk + 1, maxICCProfileSize} { |
| 28 | + profile := syntheticProfile(size, "RGB ") |
| 29 | + embedded := embedICCProfile(base, profile) |
| 30 | + got := extractICCProfile(embedded) |
| 31 | + if !bytes.Equal(got, profile) { |
| 32 | + t.Errorf("size %d: round trip lost the profile (got %d bytes, want %d)", size, len(got), len(profile)) |
| 33 | + } |
| 34 | + if !bytes.HasSuffix(embedded, base[2:]) { |
| 35 | + t.Errorf("size %d: original segments were not preserved", size) |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestExtractICCProfileRejectsMalformed(t *testing.T) { |
| 41 | + tests := map[string][]byte{ |
| 42 | + "not a jpeg": {0x00, 0x01, 0x02}, |
| 43 | + "truncated": {0xFF, 0xD8, 0xFF, 0xE2, 0x00}, |
| 44 | + "length past end": {0xFF, 0xD8, 0xFF, 0xE2, 0xFF, 0xFF, 0x01}, |
| 45 | + "no icc segment": {0xFF, 0xD8, 0xFF, 0xDA, 0x00, 0x02, 0xFF, 0xD9}, |
| 46 | + } |
| 47 | + for name, img := range tests { |
| 48 | + if got := extractICCProfile(img); got != nil { |
| 49 | + t.Errorf("%s: expected no profile, got %d bytes", name, len(got)) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestExtractICCProfileRejectsIncompleteChunkSet(t *testing.T) { |
| 55 | + base := []byte{0xFF, 0xD8, 0xFF, 0xDA, 0x00, 0x02, 0xFF, 0xD9} |
| 56 | + full := embedICCProfile(base, syntheticProfile(maxICCChunk+1, "RGB ")) |
| 57 | + |
| 58 | + // drop the first APP2 segment, leaving chunk 2 claiming a set of 2 |
| 59 | + partial := append([]byte{0xFF, 0xD8}, full[2+4+iccChunkHeaderLen+maxICCChunk:]...) |
| 60 | + if got := extractICCProfile(partial); got != nil { |
| 61 | + t.Errorf("expected no profile from an incomplete chunk set, got %d bytes", len(got)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestExtractICCProfileRejectsOversizedProfile(t *testing.T) { |
| 66 | + base := []byte{0xFF, 0xD8, 0xFF, 0xDA, 0x00, 0x02, 0xFF, 0xD9} |
| 67 | + |
| 68 | + if got := extractICCProfile(embedICCProfile(base, syntheticProfile(maxICCProfileSize+1, "RGB "))); got != nil { |
| 69 | + t.Errorf("expected an oversized profile to be refused before reassembly, got %d bytes", len(got)) |
| 70 | + } |
| 71 | + if got := extractICCProfile(embedICCProfile(base, syntheticProfile(maxICCProfileSize, "RGB "))); len(got) != maxICCProfileSize { |
| 72 | + t.Errorf("a profile exactly at the cap should survive, got %d bytes", len(got)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestPreservesSourceColorSpace(t *testing.T) { |
| 77 | + rgb := image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 78 | + tests := []struct { |
| 79 | + name string |
| 80 | + m image.Image |
| 81 | + p []byte |
| 82 | + want bool |
| 83 | + }{ |
| 84 | + {"rgb profile on rgb pixels", rgb, syntheticProfile(512, "RGB "), true}, |
| 85 | + {"cmyk profile", rgb, syntheticProfile(512, "CMYK"), false}, |
| 86 | + {"gray source", image.NewGray(image.Rect(0, 0, 1, 1)), syntheticProfile(512, "RGB "), false}, |
| 87 | + {"cmyk source", image.NewCMYK(image.Rect(0, 0, 1, 1)), syntheticProfile(512, "RGB "), false}, |
| 88 | + {"oversized profile", rgb, syntheticProfile(maxICCProfileSize+1, "RGB "), false}, |
| 89 | + {"short of a full icc header", rgb, syntheticProfile(iccHeaderSize-1, "RGB "), false}, |
| 90 | + {"exactly one icc header", rgb, syntheticProfile(iccHeaderSize, "RGB "), true}, |
| 91 | + {"no profile", rgb, nil, false}, |
| 92 | + {"runt profile", rgb, []byte{1, 2, 3}, false}, |
| 93 | + } |
| 94 | + for _, tt := range tests { |
| 95 | + if got := preservesSourceColorSpace(tt.m, tt.p); got != tt.want { |
| 96 | + t.Errorf("%s: got %v, want %v", tt.name, got, tt.want) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestTransformPassthroughICC(t *testing.T) { |
| 102 | + profile := syntheticProfile(256, "RGB ") |
| 103 | + |
| 104 | + var buf bytes.Buffer |
| 105 | + if err := jpeg.Encode(&buf, image.NewRGBA(image.Rect(0, 0, 32, 32)), nil); err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + img := embedICCProfile(buf.Bytes(), profile) |
| 109 | + if got := extractICCProfile(img); !bytes.Equal(got, profile) { |
| 110 | + t.Fatalf("fixture lost its profile before Transform ran") |
| 111 | + } |
| 112 | + |
| 113 | + off, err := Transform(img, Options{Width: 16}) |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + if got := extractICCProfile(off); got != nil { |
| 118 | + t.Errorf("PassthroughICC off: expected no profile, got %d bytes", len(got)) |
| 119 | + } |
| 120 | + |
| 121 | + on, err := Transform(img, Options{Width: 16, PassthroughICC: true}) |
| 122 | + if err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + if got := extractICCProfile(on); !bytes.Equal(got, profile) { |
| 126 | + t.Errorf("PassthroughICC on: got %d profile bytes, want %d", len(got), len(profile)) |
| 127 | + } |
| 128 | +} |
0 commit comments