]> git.imager.perl.org - imager.git/blob - ICO/t/t50readfail.t
use $Config{path_sep} instead of working it out on our own
[imager.git] / ICO / t / t50readfail.t
1 #!perl -w
2 use strict;
3 use Imager;
4 use Test::More tests => 40;
5
6 sub get_data;
7
8 { # test file limits are obeyed (paletted)
9   Imager->set_file_limits(reset => 1, width => 10);
10   my $im = Imager->new;
11   ok(!$im->read(file => 'testimg/pal13232.ico'), "can't read overwide image");
12   like($im->errstr, qr/image width/, "check message");
13 }
14
15 { # test file limits are obeyed (direct)
16   Imager->set_file_limits(reset => 1, width => 10);
17   my $im = Imager->new;
18   ok(!$im->read(file => 'testimg/rgba3232.ico'), "can't read overwide image");
19   like($im->errstr, qr/image width/, "check message");
20 }
21
22 Imager->set_file_limits(reset => 1);
23
24 { # file too short for magic
25   my $im = Imager->new;
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");
29 }
30
31 { # read non-icon
32   my $im = Imager->new;
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", 
36      "check message");
37 }
38
39 { # file with not enough icon structures
40   my $im = Imager->new;
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");
46 }
47 {
48   my $im = Imager->new;
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");
54 }
55
56 { # read negative index image
57   my $im = Imager->new;
58   ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>-1),
59      "read page -1");
60   is($im->errstr, "error reading ICO/CUR image: Image index out of range", 
61      "check error message");
62 }
63
64 { # read too high image index
65   my $im = Imager->new;
66   ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>1),
67      "read page 1");
68   is($im->errstr, "error reading ICO/CUR image: Image index out of range",
69      "check error message");
70 }
71
72 { # image offset beyond end of file
73   my $im = Imager->new;
74   my $data = get_data <<EOS;
75 ; header - icon with 1 image
76 0000 0100 0100
77 ; image record 32 x 32, offset 0xFFFF
78 20 20 00 00 0000 0000 00200000 FFFF0000
79 EOS
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");
86 }
87
88 { # short read on bmiheader
89   my $im = Imager->new;
90   my $data = get_data <<EOS;
91 ; header - icon with 1 image
92 0000 0100 0100
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
97 ; short here
98 EOS
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");
103 }
104
105 { # invalid bmiheader
106   my $im = Imager->new;
107   my $data = get_data <<EOS;
108 ; header - icon with 1 image
109 0000 0100 0100
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
117 EOS
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");
122 }
123
124 { # invalid bit count for "direct" image
125   my $im = Imager->new;
126   my $data = get_data <<EOS;
127 ; header - icon with 1 image
128 0000 0100 0100
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
136 EOS
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");
141 }
142
143 { # short file reading palette
144   my $im = Imager->new;
145   my $data = get_data <<EOS;
146 ; header - icon with 1 image
147 0000 0100 0100
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
156 FFFFFF00
157 EOS
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");
162 }
163
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
168 0000 0100 0100
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
176 ; palette
177 00000000
178 FFFFFF00
179 ; image data - short
180 00 ff
181 EOS
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");
186 }
187
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
192 0000 0100 0100
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
200 ; nopalette
201 ; image data - short
202 FFFFFFFF 
203 EOS
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");
208 }
209
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
214 0000 0100 0100
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
222 ; 16-color palette
223 00000000 FFFFFF00 00000000 FFFFFF00
224 00000000 FFFFFF00 00000000 FFFFFF00
225 00000000 FFFFFF00 00000000 FFFFFF00
226 00000000 FFFFFF00 00000000 FFFFFF00
227 ; image data - short
228 FFFFFFFF 
229 EOS
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");
234 }
235
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
241 0000 0100 0100
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
249 EOS;
250 ; palette and data above
251 EOS
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");
256 }
257
258 { # short file reading mask data
259   my $im = Imager->new;
260   my $data = get_data <<EOS;
261 ; header - icon with 1 image
262 0000 0100 0100
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
271 ; palette
272 00000000
273 FFFFFF00
274 ; image data - 16 x 16 bits
275 ; note that each line needs to be aligned on a 32-bit boundary
276 00ff00ff 00000000
277 00ff00ff 00000000
278 00ff00ff 00000000
279 00ff00ff 00000000
280 00ff00ff 00000000
281 00ff00ff 00000000
282 00ff00ff 00000000
283 00ff00ff 00000000
284 00ff00ff 00000000
285 00ff00ff 00000000
286 00ff00ff 00000000
287 00ff00ff 00000000
288 00ff00ff 00000000
289 00ff00ff 00000000
290 00ff00ff 00000000
291 00ff00ff 00000000
292 ; mask, short
293 0ff0
294 EOS
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");
299 }
300
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", 
305      "check message");
306 }
307
308 { # invalid bit count for "direct" image (read_multi)
309   my $data = get_data <<EOS;
310 ; header - icon with 1 image
311 0000 0100 0100
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
319 EOS
320   ok(!Imager->read_multi(data => $data, type=>'ico'), 
321      "read from icon with an invalid 'direct' bits per pixel (multi)");
322   is(Imager->errstr, 
323      "error reading ICO/CUR image: Unknown value for bits/pixel", 
324      "check error message");
325 }
326
327
328 # extract hex data from text
329 # allows comments
330 sub get_data {
331   my ($src) = @_;
332
333   $src =~ s/[\#;].*//mg;
334   $src =~ tr/0-9A-F//cd;
335
336   pack("H*", $src);
337 }