| 92 | } |
| 93 | |
| 94 | func TestTransform(t *testing.T) { |
| 95 | src := newImage(2, 2, red, green, blue, yellow) |
| 96 | |
| 97 | buf := new(bytes.Buffer) |
| 98 | if err := png.Encode(buf, src); err != nil { |
| 99 | t.Errorf("error encoding reference image: %v", err) |
| 100 | } |
| 101 | |
| 102 | tests := []struct { |
| 103 | name string |
| 104 | encode func(io.Writer, image.Image) error |
| 105 | exactOutput bool // whether input and output should match exactly |
| 106 | }{ |
| 107 | {"bmp", func(w io.Writer, m image.Image) error { return bmp.Encode(w, m) }, true}, |
| 108 | {"gif", func(w io.Writer, m image.Image) error { return gif.Encode(w, m, nil) }, true}, |
| 109 | {"jpeg", func(w io.Writer, m image.Image) error { return jpeg.Encode(w, m, nil) }, false}, |
| 110 | {"png", func(w io.Writer, m image.Image) error { return png.Encode(w, m) }, true}, |
| 111 | } |
| 112 | |
| 113 | for _, tt := range tests { |
| 114 | buf := new(bytes.Buffer) |
| 115 | if err := tt.encode(buf, src); err != nil { |
| 116 | t.Errorf("error encoding image: %v", err) |
| 117 | } |
| 118 | in := buf.Bytes() |
| 119 | |
| 120 | out, err := Transform(in, emptyOptions) |
| 121 | if err != nil { |
| 122 | t.Errorf("Transform with encoder %s returned unexpected error: %v", tt.name, err) |
| 123 | } |
| 124 | if !reflect.DeepEqual(in, out) { |
| 125 | t.Errorf("Transform with with encoder %s with empty options returned modified result", tt.name) |
| 126 | } |
| 127 | |
| 128 | out, err = Transform(in, Options{Width: -1, Height: -1}) |
| 129 | if err != nil { |
| 130 | t.Errorf("Transform with encoder %s returned unexpected error: %v", tt.name, err) |
| 131 | } |
| 132 | if len(out) == 0 { |
| 133 | t.Errorf("Transform with encoder %s returned empty bytes", tt.name) |
| 134 | } |
| 135 | if tt.exactOutput && !reflect.DeepEqual(in, out) { |
| 136 | t.Errorf("Transform with encoder %s with noop Options returned modified result", tt.name) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if _, err := Transform([]byte{}, Options{Width: 1}); err == nil { |
| 141 | t.Errorf("Transform with invalid image input did not return expected err") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestTranform_ImageTooLarge(t *testing.T) { |
| 146 | // lottapixel.jpg (https://hackerone.com/reports/390) |