File: t50readfail.t

package info (click to toggle)
libimager-perl 1.019%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,824 kB
  • sloc: perl: 32,886; ansic: 28,193; makefile: 52; cpp: 4
file content (337 lines) | stat: -rw-r--r-- 10,003 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!perl -w
use strict;
use Imager;
use Test::More tests => 40;

sub get_data;

{ # test file limits are obeyed (paletted)
  Imager->set_file_limits(reset => 1, width => 10);
  my $im = Imager->new;
  ok(!$im->read(file => 'testimg/pal13232.ico'), "can't read overwide image");
  like($im->errstr, qr/image width/, "check message");
}

{ # test file limits are obeyed (direct)
  Imager->set_file_limits(reset => 1, width => 10);
  my $im = Imager->new;
  ok(!$im->read(file => 'testimg/rgba3232.ico'), "can't read overwide image");
  like($im->errstr, qr/image width/, "check message");
}

Imager->set_file_limits(reset => 1);

{ # file too short for magic
  my $im = Imager->new;
  ok(!$im->read(data=>"XXXX", type=>'ico'), "Can't read short image file");
  is($im->errstr, "error opening ICO/CUR file: Short read", 
     "check error message");
}

{ # read non-icon
  my $im = Imager->new;
  ok(!$im->read(file=>'t/t50readfail.t', type=>'ico'),
     "script isn't an icon");
  is($im->errstr, "error opening ICO/CUR file: Not an icon file", 
     "check message");
}

{ # file with not enough icon structures
  my $im = Imager->new;
  my $data = pack "H*", "00000100010000";
  ok(!$im->read(data => $data, type=>'ico'), 
     "ico file broken at resource entries");
  is($im->errstr, "error opening ICO/CUR file: Short read",
     "check error message");
}
{
  my $im = Imager->new;
  my $data = pack "H*", "00000200010000";
  ok(!$im->read(data => $data, type=>'cur'), 
     "cursor file broken at resource entries");
  is($im->errstr, "error opening ICO/CUR file: Short read",
     "check error message");
}

{ # read negative index image
  my $im = Imager->new;
  ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>-1),
     "read page -1");
  is($im->errstr, "error reading ICO/CUR image: Image index out of range", 
     "check error message");
}

{ # read too high image index
  my $im = Imager->new;
  ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>1),
     "read page 1");
  is($im->errstr, "error reading ICO/CUR image: Image index out of range",
     "check error message");
}

{ # image offset beyond end of file
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0xFFFF
20 20 00 00 0000 0000 00200000 FFFF0000
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with bad offset");
  # bad offset causes the seek to fail on an in-memory "file"
  # it may not fail this way on a real file.
  is($im->errstr, "error reading ICO/CUR image: I/O error", 
     "check error message");
}

{ # short read on bmiheader
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0xFFFF
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
; short here
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with a short bitmap header");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # invalid bmiheader
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0xFFFF
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2000 0000 2000 0000 4000 0000 ; size should be 0x28, width, height
0100 2000 ; planes, bit count
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with an invalid sub-image header");
  is($im->errstr, "error reading ICO/CUR image: Not an icon file",
     "check error message");
}

{ # invalid bit count for "direct" image
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0xFFFF
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 2100 ; planes, bit count
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with an invalid 'direct' bits per pixel");
  is($im->errstr, "error reading ICO/CUR image: Unknown value for bits/pixel", 
     "check error message");
}

{ # short file reading palette
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0xFFFF
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 0100 ; planes, bit count == 1
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
; dummy palette - one color but 2 needed
FFFFFF00
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with short palette");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # short file reading 1 bit image data
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0x20
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 0100 ; planes, bit count == 1
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
; palette
00000000
FFFFFF00
; image data - short
00 ff
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with short image data (1 bit)");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # short file reading 32 bit image data
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0x20
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 2000 ; planes, bit count == 32
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
; nopalette
; image data - short
FFFFFFFF 
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with short image data (32 bit)");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # short file reading 4 bit image data
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0x20
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 0400 ; planes, bit count == 4
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
; 16-color palette
00000000 FFFFFF00 00000000 FFFFFF00
00000000 FFFFFF00 00000000 FFFFFF00
00000000 FFFFFF00 00000000 FFFFFF00
00000000 FFFFFF00 00000000 FFFFFF00
; image data - short
FFFFFFFF 
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with short image data (4 bit)");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # short file reading 8 bit image data
  my $im = Imager->new;
  # base image header + palette + a little data
  my $data = get_data <<EOS . "FFFFFFFF" x 256 . "FFFF FFFF";
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0x20
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 0800 ; planes, bit count == 8
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
EOS;
; palette and data above
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with short image data (8 bit)");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # short file reading mask data
  my $im = Imager->new;
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 16 x 16, 2 colors, reserved=0, planes=1,
; sizeinbytes (ignored), offset 0x16
10 10 02 00 0100 0100 00000000 16000000
; bmiheader for the first image
2800 0000 1000 0000 2000 0000 ; size, width, height
0100 0100 ; planes, bit count == 1
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
; palette
00000000
FFFFFF00
; image data - 16 x 16 bits
; note that each line needs to be aligned on a 32-bit boundary
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
00ff00ff 00000000
; mask, short
0ff0
EOS
  ok(!$im->read(data => $data, type=>'ico'), 
     "read from icon with short mask data");
  is($im->errstr, "error reading ICO/CUR image: Short read",
     "check error message");
}

{ # fail opening on a multi-read
  ok(!Imager->read_multi(file=>'t/t50readfail.t', type=>'ico'),
     "multi-read on non-icon");
  is(Imager->errstr, "error opening ICO/CUR file: Not an icon file", 
     "check message");
}

{ # invalid bit count for "direct" image (read_multi)
  my $data = get_data <<EOS;
; header - icon with 1 image
0000 0100 0100
; image record 32 x 32, offset 0xFFFF
20 20 00 00 0000 0000 00200000 16000000
; bmiheader for the first image
2800 0000 2000 0000 4000 0000 ; size, width, height
0100 2100 ; planes, bit count
; data we read but ignore
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
EOS
  ok(!Imager->read_multi(data => $data, type=>'ico'), 
     "read from icon with an invalid 'direct' bits per pixel (multi)");
  is(Imager->errstr, 
     "error reading ICO/CUR image: Unknown value for bits/pixel", 
     "check error message");
}


# extract hex data from text
# allows comments
sub get_data {
  my ($src) = @_;

  $src =~ s/[\#;].*//mg;
  $src =~ tr/0-9A-F//cd;

  pack("H*", $src);
}