SGI/t/10read.t

filter: Statementstmt 100% 103 / 103 Branchbran 50.0% 12 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 90.9% 121 / 133 CC 3 SCAR 5.6
CC
3
Cov
91%
load_patched_file
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
« blib/lib/Imager/File/SGI.pm ↑ Summary blib/lib/Imager/Color/Table.pm »
1 #!perl -w
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Imager::Test qw(is_image is_color3);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶-d 'testout' or mkdir 'testout', 0777;
Branch: 1 branch
BranchTrueFalseTotal
-d "testout" or mkdir "testout", 511 0 1 1
8  
9 1 Imager::init_log('testout/10read.log', 2);
10  
11 {
12 1 my $im_verb = Imager->new;
13 1 ▶ ok($im_verb->read(file => 'testimg/verb.rgb'), "read verbatim")
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_verb->read("file", "testimg/verb.rgb"), "read verbatim") or print "# ", $im_verb->errstr, "\n" 1 0 1
14 or print "# ", $im_verb->errstr, "\n";
15 1 is($im_verb->getchannels, 3, "check channels");
16 1 is($im_verb->getwidth, 20, "check width");
17 1 is($im_verb->getheight, 20, "check height");
18 1 is_color3($im_verb->getpixel(x => 0, 'y' => 0), 255, 0, 0, "check 0,0");
19 1 is_color3($im_verb->getpixel(x => 1, 'y' => 2), 255, 255, 0, "check 0,2");
20 1 is_color3($im_verb->getpixel(x => 2, 'y' => 4), 0, 255, 255, "check 2,5");
21 1 is($im_verb->tags(name => 'i_format'), 'sgi', "check i_format tag");
22 1 is($im_verb->tags(name => 'sgi_rle'), 0, "check sgi_rgb");
23 1 is($im_verb->tags(name => 'sgi_pixmin'), 0, "check pixmin");
24 1 is($im_verb->tags(name => 'sgi_pixmax'), 255, "check pixmax");
25 1 is($im_verb->tags(name => 'sgi_bpc'), 1, "check bpc");
26 1 is($im_verb->tags(name => 'i_comment'), 'test image',
27 "check name string");
28  
29 1 my $im_rle = Imager->new;
30 1 ▶ ok($im_rle->read(file => 'testimg/rle.rgb'), "read rle")
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_rle->read("file", "testimg/rle.rgb"), "read rle") or print "# ", $im_rle->errstr, "\n" 1 0 1
31 or print "# ", $im_rle->errstr, "\n";
32 1 is($im_rle->tags(name => 'sgi_rle'), 1, "check sgi_rgb");
33  
34 1 my $im_rleagr = Imager->new;
35 1 ▶ ok($im_rleagr->read(file => 'testimg/rleagr.rgb'), "read rleagr")
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_rleagr->read("file", "testimg/rleagr.rgb"), "read rleagr") or print "# ", $im_rleagr->errstr, "\n" 1 0 1
36 or print "# ", $im_rleagr->errstr, "\n";
37  
38 1 my $im6 = Imager->new;
39 1 ▶ ok($im6->read(file => 'testimg/verb6.rgb'), "read verbatim 6-bit")
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im6->read("file", "testimg/verb6.rgb"), "read verbatim 6-bit") or print "# ", $im6->errstr, "\n" 1 0 1
40 or print "# ", $im6->errstr, "\n";
41 1 is($im6->tags(name => 'sgi_pixmax'), 63, "check pixmax");
42  
43 1 is_image($im_verb, $im_rle, "compare verbatim to rle");
44 1 is_image($im_verb, $im_rleagr, "compare verbatim to rleagr");
45 1 is_image($im_verb, $im6, "compare verbatim to verb 6-bit");
46  
47 1 my $im_verb12 = Imager->new;
48 1 ▶ ok($im_verb12->read(file => 'testimg/verb12.rgb'), "read verbatim 12")
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_verb12->read("file", "testimg/verb12.rgb"), "read verbatim 12") or print "# ", $im_verb12->errstr, "\n" 1 0 1
49 or print "# ", $im_verb12->errstr, "\n";
50 1 is($im_verb12->bits, 16, "check bits on verb12");
51 1 is($im_verb12->tags(name => 'sgi_pixmax'), 4095, "check pixmax");
52  
53 1 my $im_verb16 = Imager->new;
54 1 ▶ ok($im_verb16->read(file => 'testimg/verb16.rgb'), "read verbatim 16")
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_verb16->read("file", "testimg/verb16.rgb"), "read verbatim 16") or print "# ", $im_verb16->errstr, "\n" 1 0 1
55 or print "# ", $im_verb16->errstr, "\n";
56 1 is($im_verb16->bits, 16, "check bits on verb16");
57 1 is($im_verb16->tags(name => 'sgi_pixmax'), 65535, "check pixmax");
58
59 1 is_image($im_verb, $im_verb12, "compare verbatim to verb12");
60 1 is_image($im_verb, $im_verb16, "compare verbatim to verb16");
61  
62 1 my $im_rle6 = Imager->new;
63 1 ok($im_rle6->read(file => 'testimg/rle6.rgb'), "read rle 6 bit");
64 1 is($im_rle6->tags(name => 'sgi_pixmax'), 63, 'check pixmax');
65 1 is_image($im_verb, $im_rle6, 'compare verbatim to rle6');
66
67 1 my $im_rle12 = Imager->new;
68 1 ▶ ok($im_rle12->read(file => 'testimg/rle12.rgb'), 'read rle 12 bit')
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_rle12->read("file", "testimg/rle12.rgb"), "read rle 12 bit") or print "# ", $im_rle12->errstr, "\n" 1 0 1
69 or print "# ", $im_rle12->errstr, "\n";
70 1 is($im_rle12->tags(name => 'sgi_pixmax'), 4095, 'check pixmax');
71 1 is_image($im_verb, $im_rle12, 'compare verbatim to rle12');
72  
73 1 my $im_rle16 = Imager->new;
74 1 ▶ ok($im_rle16->read(file => 'testimg/rle16.rgb'), 'read rle 16 bit')
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im_rle16->read("file", "testimg/rle16.rgb"), "read rle 16 bit") or print "# ", $im_rle16->errstr, "\n" 1 0 1
75 or print "# ", $im_rle16->errstr, "\n";
76 1 is($im_rle16->tags(name => 'sgi_pixmax'), 65535, 'check pixmax');
77 1 is($im_rle16->tags(name => 'sgi_bpc'), 2, "check bpc");
78 1 is_image($im_verb, $im_rle16, 'compare verbatim to rle16');
79 }
80  
81 {
82 # short read tests, each is source file, limit, match, description
83 1 my @tests =
84 (
85 [
86 'verb.rgb', 100,
87 'SGI image: could not read header', 'header',
88 ],
89 [
90 'verb.rgb', 512,
91 'SGI image: cannot read image data',
92 'verbatim image data'
93 ],
94 [
95 'rle.rgb', 512,
96 'SGI image: short read reading RLE start table',
97 'rle start table'
98 ],
99 [
100 'rle.rgb', 752,
101 'SGI image: short read reading RLE length table',
102 'rle length table'
103 ],
104 [
105 'rle.rgb', 0x510,
106 "SGI image: cannot read RLE data",
107 'read rle data'
108 ],
109 [
110 'rle.rgb', 0x50E,
111 "SGI image: cannot seek to RLE data",
112 'seek rle data'
113 ],
114 [
115 'verb16.rgb', 512,
116 'SGI image: cannot read image data',
117 'read image data (16-bit)'
118 ],
119 [
120 'rle16.rgb', 512,
121 'SGI image: short read reading RLE start table',
122 'rle start table (16-bit)',
123 ],
124 [
125 'rle16.rgb', 0x42f,
126 'SGI image: cannot seek to RLE data',
127 'seek RLE data (16-bit)'
128 ],
129 [
130 'rle16.rgb', 0x64A,
131 'SGI image: cannot read RLE data',
132 'read rle image data (16-bit)'
133 ],
134 );
135 1 for my $test (@tests) {
136 10 my ($src, $size, $match, $desc) = @$test;
137 10 ▶ open SRC, "< testimg/$src"
Branch: 1 branch
BranchTrueFalseTotal
open SRC, "< testimg/$src" or die "Cannot open testimg/${src}: $!" 10 0 10
138 or die "Cannot open testimg/$src: $!";
139 10 binmode SRC;
140 10 my $data;
141 10 ▶ read(SRC, $data, $size) == $size
Branch: 1 branch
BranchTrueFalseTotal
read(SRC, $data, $size) == $size or die "Could not read $size bytes from $src" 10 0 10
142 or die "Could not read $size bytes from $src";
143 10 close SRC;
144 10 my $im = Imager->new;
145 10 ok(!$im->read(data => $data, type => 'sgi'),
146 "read: $desc");
147 10 is($im->errstr, $match, "error match: $desc");
148 }
149 }
150  
151 {
152 # each entry is: source file, patches, expected error, description
153 1 my @tests =
154 (
155 [
156 'verb.rgb',
157 { 0 => '00 00' },
158 'SGI image: invalid magic number',
159 'bad magic',
160 ],
161 [
162 'verb.rgb',
163 { 104 => '00 00 00 01' },
164 'SGI image: invalid value for colormap (1)',
165 'invalid colormap field',
166 ],
167 [
168 'verb.rgb',
169 { 3 => '03' },
170 'SGI image: invalid value for BPC (3)',
171 'invalid bpc field',
172 ],
173 [
174 'verb.rgb',
175 { 2 => '03' },
176 'SGI image: invalid storage type field',
177 'invalid storage type field',
178 ],
179 [
180 'verb.rgb',
181 { 4 => '00 04' },
182 'SGI image: invalid dimension field',
183 'invalid dimension field',
184 ],
185 [
186 'rle.rgb',
187 { 0x2f0 => '00 00 00 2b' },
188 'SGI image: ridiculous RLE line length 43',
189 'invalid rle length',
190 ],
191 [
192 'rle.rgb',
193 { 0x3E0 => '95' },
194 'SGI image: literal run overflows scanline',
195 'literal run overflow scanline',
196 ],
197 [
198 'rle.rgb',
199 { 0x3E0 => '87' },
200 'SGI image: literal run consumes more data than available',
201 'literal run consuming too much data',
202 ],
203 [
204 'rle.rgb',
205 { 0x3E0 => '15' },
206 'SGI image: RLE run overflows scanline',
207 'RLE run overflows scanline',
208 ],
209 [
210 'rle.rgb',
211 { 0x3E0 => '81 FF 12 00 01' },
212 'SGI image: RLE run has no data for pixel',
213 'RLE run has no data for pixel',
214 ],
215 [
216 'rle.rgb',
217 { 0x3E0 => '81 FF 12 00' },
218 'SGI image: incomplete RLE scanline',
219 'incomplete RLE scanline',
220 ],
221 [
222 'rle.rgb',
223 { 0x2F0 => '00 00 00 06' },
224 'SGI image: unused RLE data',
225 'unused RLE data',
226 ],
227 [
228 'verb.rgb',
229 { 0x0c => '00 00 00 FF 00 00 00 00' },
230 'SGI image: invalid pixmin >= pixmax',
231 'bad pixmin/pixmax',
232 ],
233 [
234 'rle16.rgb',
235 { 0x2f0 => '00 00 00 0B' },
236 'SGI image: invalid RLE length value for BPC=2',
237 'bad RLE table (length) (bpc=2)'
238 ],
239 [
240 'rle16.rgb',
241 { 0x2f0 => '00 00 00 53' },
242 'SGI image: ridiculous RLE line length 83',
243 'way too big RLE line length (16-bit)'
244 ],
245 [
246 'rle16.rgb',
247 { 0x426 => '00 95' },
248 'SGI image: literal run overflows scanline',
249 'literal overflow scanline (bpc=2)'
250 ],
251 [
252 'rle16.rgb',
253 { 0x426 => '00 93' },
254 'SGI image: literal run consumes more data than available',
255 'literal overflow data (bpc=2)'
256 ],
257 [
258 'rle16.rgb',
259 { 0x3EA => '00 15' },
260 'SGI image: RLE run overflows scanline',
261 'rle overflow scanline (bpc=2)'
262 ],
263 [
264 'rle16.rgb',
265 { 0x3EA => '00 15' },
266 'SGI image: RLE run overflows scanline',
267 'rle overflow scanline (bpc=2)'
268 ],
269 [
270 'rle16.rgb',
271 { 0x3EA => '00 83 ff ff ff ff ff ff 00 01' },
272 'SGI image: RLE run has no data for pixel',
273 'rle code no argument (bpc=2)'
274 ],
275 [
276 'rle16.rgb',
277 { 0x3EA => '00 14 ff ff 00 00' },
278 'SGI image: unused RLE data',
279 'unused RLE data (bpc=2)'
280 ],
281 [
282 'rle16.rgb',
283 { 0x3EA => '00 12 ff ff' },
284 'SGI image: incomplete RLE scanline',
285 'incomplete rle scanline (bpc=2)'
286 ],
287 );
288  
289 # invalid file tests - take our original files and patch them a
290 # little to make them invalid
291 1 my $test_index = 0;
292 1 for my $test (@tests) {
293 22 my ($filename, $patches, $error, $desc) = @$test;
294  
295 22 my $data = load_patched_file("testimg/$filename", $patches);
296 22 my $im = Imager->new;
297 22 ok(!$im->read(data => $data, type=>'sgi'),
298 "$test_index - $desc:should fail to read");
299 22 is($im->errstr, $error, "$test_index - $desc:check message");
300 22 ++$test_index;
301 }
302 }
303  
304 {
305 # CVE-2026-13705
306 # the file here is invalid, and should fail to load
307 # without the fix expect a valgrind buffer read overflow
308 # causing the test to fail on valgrind
309 1 my $im = Imager->new;
310 1 ok(!$im->read(file => "testimg/overflow.rgb"),
311 "fail to load file with had last scanline");
312 }
313  
314 1 done_testing();
315  
316 22 ▶sub load_patched_file {
Subroutine load_patched_file: called CC=3 SCAR=11.0
317 22 my ($filename, $patches) = @_;
318  
319 22 ▶ open IMDATA, "< $filename"
Branch: 1 branch
BranchTrueFalseTotal
open IMDATA, "< $filename" or die "Cannot open ${filename}: $!" 22 0 22
320 or die "Cannot open $filename: $!";
321 22 binmode IMDATA;
322 22 my $data = do { local $/; <IMDATA> };
323 22 for my $offset (keys %$patches) {
324 22 (my $hdata = $patches->{$offset}) =~ tr/ //d;
325 22 my $pdata = pack("H*", $hdata);
326 22 substr($data, $offset, length $pdata) = $pdata;
327 }
328  
329 22 return $data;
330 }
« blib/lib/Imager/File/SGI.pm ↑ Summary blib/lib/Imager/Color/Table.pm »