4 use Test::More tests => 40;
8 { # test file limits are obeyed (paletted)
9 Imager->set_file_limits(reset => 1, width => 10);
11 ok(!$im->read(file => 'testimg/pal13232.ico'), "can't read overwide image");
12 like($im->errstr, qr/image width/, "check message");
15 { # test file limits are obeyed (direct)
16 Imager->set_file_limits(reset => 1, width => 10);
18 ok(!$im->read(file => 'testimg/rgba3232.ico'), "can't read overwide image");
19 like($im->errstr, qr/image width/, "check message");
22 Imager->set_file_limits(reset => 1);
24 { # file too short for magic
26 ok(!$im->read(data=>"XXXX", type=>'ico'), "Can't read short image file");
27 is($im->errstr, "error opening ICO/CUR file: Short read",
28 "check error message");
33 ok(!$im->read(file=>'t/t50readfail.t', type=>'ico'),
34 "script isn't an icon");
35 is($im->errstr, "error opening ICO/CUR file: Not an icon file",
39 { # file with not enough icon structures
41 my $data = pack "H*", "00000100010000";
42 ok(!$im->read(data => $data, type=>'ico'),
43 "ico file broken at resource entries");
44 is($im->errstr, "error opening ICO/CUR file: Short read",
45 "check error message");
49 my $data = pack "H*", "00000200010000";
50 ok(!$im->read(data => $data, type=>'cur'),
51 "cursor file broken at resource entries");
52 is($im->errstr, "error opening ICO/CUR file: Short read",
53 "check error message");
56 { # read negative index image
58 ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>-1),
60 is($im->errstr, "error reading ICO/CUR image: Image index out of range",
61 "check error message");
64 { # read too high image index
66 ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>1),
68 is($im->errstr, "error reading ICO/CUR image: Image index out of range",
69 "check error message");
72 { # image offset beyond end of file
74 my $data = get_data <<EOS;
75 ; header - icon with 1 image
77 ; image record 32 x 32, offset 0xFFFF
78 20 20 00 00 0000 0000 00200000 FFFF0000
80 ok(!$im->read(data => $data, type=>'ico'),
81 "read from icon with bad offset");
82 # bad offset causes the seek to fail on an in-memory "file"
83 # it may not fail this way on a real file.
84 is($im->errstr, "error reading ICO/CUR image: I/O error",
85 "check error message");
88 { # short read on bmiheader
90 my $data = get_data <<EOS;
91 ; header - icon with 1 image
93 ; image record 32 x 32, offset 0xFFFF
94 20 20 00 00 0000 0000 00200000 16000000
95 ; bmiheader for the first image
96 2800 0000 2000 0000 4000 0000 ; size, width, height
99 ok(!$im->read(data => $data, type=>'ico'),
100 "read from icon with a short bitmap header");
101 is($im->errstr, "error reading ICO/CUR image: Short read",
102 "check error message");
105 { # invalid bmiheader
106 my $im = Imager->new;
107 my $data = get_data <<EOS;
108 ; header - icon with 1 image
110 ; image record 32 x 32, offset 0xFFFF
111 20 20 00 00 0000 0000 00200000 16000000
112 ; bmiheader for the first image
113 2000 0000 2000 0000 4000 0000 ; size should be 0x28, width, height
114 0100 2000 ; planes, bit count
115 ; data we read but ignore
116 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
118 ok(!$im->read(data => $data, type=>'ico'),
119 "read from icon with an invalid sub-image header");
120 is($im->errstr, "error reading ICO/CUR image: Not an icon file",
121 "check error message");
124 { # invalid bit count for "direct" image
125 my $im = Imager->new;
126 my $data = get_data <<EOS;
127 ; header - icon with 1 image
129 ; image record 32 x 32, offset 0xFFFF
130 20 20 00 00 0000 0000 00200000 16000000
131 ; bmiheader for the first image
132 2800 0000 2000 0000 4000 0000 ; size, width, height
133 0100 2100 ; planes, bit count
134 ; data we read but ignore
135 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
137 ok(!$im->read(data => $data, type=>'ico'),
138 "read from icon with an invalid 'direct' bits per pixel");
139 is($im->errstr, "error reading ICO/CUR image: Unknown value for bits/pixel",
140 "check error message");
143 { # short file reading palette
144 my $im = Imager->new;
145 my $data = get_data <<EOS;
146 ; header - icon with 1 image
148 ; image record 32 x 32, offset 0xFFFF
149 20 20 00 00 0000 0000 00200000 16000000
150 ; bmiheader for the first image
151 2800 0000 2000 0000 4000 0000 ; size, width, height
152 0100 0100 ; planes, bit count == 1
153 ; data we read but ignore
154 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
155 ; dummy palette - one color but 2 needed
158 ok(!$im->read(data => $data, type=>'ico'),
159 "read from icon with short palette");
160 is($im->errstr, "error reading ICO/CUR image: Short read",
161 "check error message");
164 { # short file reading 1 bit image data
165 my $im = Imager->new;
166 my $data = get_data <<EOS;
167 ; header - icon with 1 image
169 ; image record 32 x 32, offset 0x20
170 20 20 00 00 0000 0000 00200000 16000000
171 ; bmiheader for the first image
172 2800 0000 2000 0000 4000 0000 ; size, width, height
173 0100 0100 ; planes, bit count == 1
174 ; data we read but ignore
175 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
182 ok(!$im->read(data => $data, type=>'ico'),
183 "read from icon with short image data (1 bit)");
184 is($im->errstr, "error reading ICO/CUR image: Short read",
185 "check error message");
188 { # short file reading 32 bit image data
189 my $im = Imager->new;
190 my $data = get_data <<EOS;
191 ; header - icon with 1 image
193 ; image record 32 x 32, offset 0x20
194 20 20 00 00 0000 0000 00200000 16000000
195 ; bmiheader for the first image
196 2800 0000 2000 0000 4000 0000 ; size, width, height
197 0100 2000 ; planes, bit count == 32
198 ; data we read but ignore
199 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
204 ok(!$im->read(data => $data, type=>'ico'),
205 "read from icon with short image data (32 bit)");
206 is($im->errstr, "error reading ICO/CUR image: Short read",
207 "check error message");
210 { # short file reading 4 bit image data
211 my $im = Imager->new;
212 my $data = get_data <<EOS;
213 ; header - icon with 1 image
215 ; image record 32 x 32, offset 0x20
216 20 20 00 00 0000 0000 00200000 16000000
217 ; bmiheader for the first image
218 2800 0000 2000 0000 4000 0000 ; size, width, height
219 0100 0400 ; planes, bit count == 4
220 ; data we read but ignore
221 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
223 00000000 FFFFFF00 00000000 FFFFFF00
224 00000000 FFFFFF00 00000000 FFFFFF00
225 00000000 FFFFFF00 00000000 FFFFFF00
226 00000000 FFFFFF00 00000000 FFFFFF00
230 ok(!$im->read(data => $data, type=>'ico'),
231 "read from icon with short image data (4 bit)");
232 is($im->errstr, "error reading ICO/CUR image: Short read",
233 "check error message");
236 { # short file reading 8 bit image data
237 my $im = Imager->new;
238 # base image header + palette + a little data
239 my $data = get_data <<EOS . "FFFFFFFF" x 256 . "FFFF FFFF";
240 ; header - icon with 1 image
242 ; image record 32 x 32, offset 0x20
243 20 20 00 00 0000 0000 00200000 16000000
244 ; bmiheader for the first image
245 2800 0000 2000 0000 4000 0000 ; size, width, height
246 0100 0800 ; planes, bit count == 8
247 ; data we read but ignore
248 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
250 ; palette and data above
252 ok(!$im->read(data => $data, type=>'ico'),
253 "read from icon with short image data (8 bit)");
254 is($im->errstr, "error reading ICO/CUR image: Short read",
255 "check error message");
258 { # short file reading mask data
259 my $im = Imager->new;
260 my $data = get_data <<EOS;
261 ; header - icon with 1 image
263 ; image record 16 x 16, 2 colors, reserved=0, planes=1,
264 ; sizeinbytes (ignored), offset 0x16
265 10 10 02 00 0100 0100 00000000 16000000
266 ; bmiheader for the first image
267 2800 0000 1000 0000 2000 0000 ; size, width, height
268 0100 0100 ; planes, bit count == 1
269 ; data we read but ignore
270 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
274 ; image data - 16 x 16 bits
275 ; note that each line needs to be aligned on a 32-bit boundary
295 ok(!$im->read(data => $data, type=>'ico'),
296 "read from icon with short mask data");
297 is($im->errstr, "error reading ICO/CUR image: Short read",
298 "check error message");
301 { # fail opening on a multi-read
302 ok(!Imager->read_multi(file=>'t/t50readfail.t', type=>'ico'),
303 "multi-read on non-icon");
304 is(Imager->errstr, "error opening ICO/CUR file: Not an icon file",
308 { # invalid bit count for "direct" image (read_multi)
309 my $data = get_data <<EOS;
310 ; header - icon with 1 image
312 ; image record 32 x 32, offset 0xFFFF
313 20 20 00 00 0000 0000 00200000 16000000
314 ; bmiheader for the first image
315 2800 0000 2000 0000 4000 0000 ; size, width, height
316 0100 2100 ; planes, bit count
317 ; data we read but ignore
318 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
320 ok(!Imager->read_multi(data => $data, type=>'ico'),
321 "read from icon with an invalid 'direct' bits per pixel (multi)");
323 "error reading ICO/CUR image: Unknown value for bits/pixel",
324 "check error message");
328 # extract hex data from text
333 $src =~ s/[\#;].*//mg;
334 $src =~ tr/0-9A-F//cd;