ICO/t/t60writefail.t

filter: Statementstmt 100% 173 / 173 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 183 / 183 CC 2 SCAR 2.0
CC
2
Cov
100%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Flines/t/t00flines.t ↑ Summary dynaload.c »
1 #!perl -w
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Test::More tests => 69;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Imager ':handy';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # this file tries to cover as much of the write error handling cases in
7 # msicon.c/imicon.c as possible.
8 #
9 # coverage checked with gcc/gcov
10  
11 # image too big for format tests, for each entry point
12 {
13 1 my $im = Imager->new(xsize => 257, ysize => 256);
14 1 my $data;
15 1 ok(!$im->write(data => \$data, type=>'ico'),
16 "image too large");
17 1 is($im->errstr, "image too large for ico file", "check message");
18 }
19  
20 {
21 1 my $im = Imager->new(xsize => 257, ysize => 256);
22 1 my $data;
23 1 ok(!Imager->write_multi({ data => \$data, type=>'ico' }, $im, $im),
24 "image too large");
25 1 is(Imager->errstr, "image too large for ico file", "check message");
26 1 Imager->_set_error('');
27 }
28  
29 {
30 1 my $im = Imager->new(xsize => 257, ysize => 256);
31 1 my $data;
32 1 ok(!$im->write(data => \$data, type=>'cur'),
33 "image too large");
34 1 is($im->errstr, "image too large for ico file", "check message");
35 }
36  
37 {
38 1 my $im = Imager->new(xsize => 257, ysize => 256);
39 1 my $data;
40 1 ok(!Imager->write_multi({ data => \$data, type=>'cur' }, $im),
41 "image too large");
42 1 is(Imager->errstr, "image too large for ico file", "check message");
43 1 Imager->_set_error('');
44 }
45  
46 # low level write failure tests for each entry point (fail on close)
47 {
48 1 my $im = Imager->new(xsize => 10, ysize => 10);
49 1 ok(!$im->write(callback => \&write_failure, type=>'ico'),
50 "low level write failure (ico)");
51 1 is($im->errstr, "error closing output: synthetic error", "check message");
52 }
53  
54 {
55 1 my $im = Imager->new(xsize => 10, ysize => 10);
56 1 ok(!$im->write(callback => \&write_failure, type=>'cur'),
57 "low level write failure (cur)");
58 1 is($im->errstr, "error closing output: synthetic error", "check message");
59 }
60  
61 {
62 1 my $im = Imager->new(xsize => 10, ysize => 10);
63 1 ok(!Imager->write_multi({ callback => \&write_failure, type=>'ico' }, $im, $im),
64 "low level write_multi failure (ico)");
65 1 is(Imager->errstr, "error closing output: synthetic error", "check message");
66 1 Imager->_set_error('');
67 }
68  
69 {
70 1 my $im = Imager->new(xsize => 10, ysize => 10);
71 1 ok(!Imager->write_multi({ callback => \&write_failure, type=>'cur' }, $im, $im),
72 "low level write_multi failure (cur)");
73 1 is(Imager->errstr, "error closing output: synthetic error", "check message");
74 1 Imager->_set_error('');
75 }
76  
77 # low level write failure tests for each entry point (fail on write)
78 {
79 1 my $im = Imager->new(xsize => 10, ysize => 10);
80 1 my $io = Imager::io_new_cb(\&write_failure, undef, undef, undef, 1);
81 1 $io->set_buffered(0);
82 1 ok(!$im->write(io => $io, type=>'ico'),
83 "low level write failure (ico)");
84 1 is($im->errstr, "Write failure: synthetic error", "check message");
85 }
86  
87 {
88 1 my $im = Imager->new(xsize => 10, ysize => 10);
89 1 my $io = Imager::io_new_cb(\&write_failure, undef, undef, undef, 1);
90 1 $io->set_buffered(0);
91 1 ok(!$im->write(io => $io, type=>'cur'),
92 "low level write failure (cur)");
93 1 is($im->errstr, "Write failure: synthetic error", "check message");
94 }
95  
96 {
97 1 my $im = Imager->new(xsize => 10, ysize => 10);
98 1 my $io = Imager::io_new_cb(\&write_failure, undef, undef, undef, 1);
99 1 $io->set_buffered(0);
100 1 ok(!Imager->write_multi({ io => $io, type=>'ico' }, $im, $im),
101 "low level write_multi failure (ico)");
102 1 is(Imager->errstr, "Write failure: synthetic error", "check message");
103 1 Imager->_set_error('');
104 }
105  
106 {
107 1 my $im = Imager->new(xsize => 10, ysize => 10);
108 1 my $io = Imager::io_new_cb(\&write_failure, undef, undef, undef, 1);
109 1 $io->set_buffered(0);
110 1 ok(!Imager->write_multi({ io => $io, type=>'cur' }, $im, $im),
111 "low level write_multi failure (cur)");
112 1 is(Imager->errstr, "Write failure: synthetic error", "check message");
113 1 Imager->_set_error('');
114 }
115  
116 {
117 1 my $im = Imager->new(xsize => 10, ysize => 10);
118 1 ok(!$im->write(type => 'ico', io => limited_write_io(6)),
119 "second write (resource) should fail (ico)");
120 1 is($im->errstr, "Write failure: limit reached", "check message");
121 1 $im->_set_error('');
122  
123 1 ok(!$im->write(type => 'cur', io => limited_write_io(6)),
124 "second (resource) write should fail (cur)");
125 1 is($im->errstr, "Write failure: limit reached", "check message");
126 1 $im->_set_error('');
127  
128 1 ok(!$im->write(type => 'ico', io => limited_write_io(22)),
129 "third write (bmi) should fail (32-bit)");
130 1 is($im->errstr, "Write failure: limit reached", "check message");
131 1 $im->_set_error('');
132  
133 1 ok(!$im->write(type => 'ico', io => limited_write_io(62)),
134 "fourth write (data) should fail (32-bit)");
135 1 is($im->errstr, "Write failure: limit reached", "check message");
136 1 $im->_set_error('');
137  
138 1 ok(!$im->write(type => 'ico', io => limited_write_io(462)),
139 "mask write should fail (32-bit)");
140 1 is($im->errstr, "Write failure: limit reached", "check message");
141 }
142  
143 { # 1 bit write fails
144 1 my $im = Imager->new(xsize => 10, ysize => 10, type => 'paletted');
145 1 my $red = NC(255, 0, 0);
146 1 my $blue = NC(0, 0, 255);
147 1 $im->addcolors(colors => [ $red, $blue ]);
148 1 $im->box(filled => 1, color => $red, ymax => 5);
149 1 $im->box(filled => 1, color => $blue, ymin => 6);
150 1 ok(!$im->write(type => 'ico', io => limited_write_io(22)),
151 "third write (bmi) should fail (1-bit)");
152 1 is($im->errstr, "Write failure: limit reached", "check message");
153
154 1 ok(!$im->write(type => 'ico', io => limited_write_io(66)),
155 "fourth write (palette) should fail (1-bit)");
156 1 is($im->errstr, "Write failure: limit reached", "check message");
157 1 ok(!$im->write(type => 'ico', io => limited_write_io(74)),
158 "fifth write (image) should fail (1-bit)");
159 1 is($im->errstr, "Write failure: limit reached", "check message");
160 1 my $data;
161 1 ok($im->write(data => \$data, type => 'ico'), "write 1 bit successfully");
162 1 my $read = Imager->new;
163 1 ok($read->read(data => $data), "read it back");
164 1 is($read->type, 'paletted', "check type");
165 1 is($read->tags(name => 'ico_bits'), 1, "check bits");
166 1 is(Imager::i_img_diff($read, $im), 0, "check image correct");
167 }
168  
169 { # 4 bit write fails
170 1 my $im = Imager->new(xsize => 10, ysize => 10, type => 'paletted');
171 1 my $red = NC(255, 0, 0);
172 1 my $blue = NC(0, 0, 255);
173 1 $im->addcolors(colors => [ ($red, $blue) x 8 ]);
174 1 $im->box(filled => 1, color => $red, ymax => 5);
175 1 $im->box(filled => 1, color => $blue, ymin => 6);
176 1 ok(!$im->write(type => 'ico', io => limited_write_io(22)),
177 "third write (bmi) should fail (4-bit)");
178 1 is($im->errstr, "Write failure: limit reached", "check message");
179
180 1 ok(!$im->write(type => 'ico', io => limited_write_io(66)),
181 "fourth write (palette) should fail (4-bit)");
182 1 is($im->errstr, "Write failure: limit reached", "check message");
183 1 ok(!$im->write(type => 'ico', io => limited_write_io(130)),
184 "fifth write (image) should fail (4-bit)");
185 1 is($im->errstr, "Write failure: limit reached", "check message");
186 1 my $data;
187 1 ok($im->write(data => \$data, type => 'ico'), "write 4 bit successfully");
188 1 my $read = Imager->new;
189 1 ok($read->read(data => $data), "read it back");
190 1 is($read->type, 'paletted', "check type");
191 1 is($read->tags(name => 'ico_bits'), 4, "check bits");
192 1 is(Imager::i_img_diff($read, $im), 0, "check image correct");
193 }
194  
195 { # 8 bit write fails
196 1 my $im = Imager->new(xsize => 10, ysize => 10, type => 'paletted');
197 1 my $red = NC(255, 0, 0);
198 1 my $blue = NC(0, 0, 255);
199 1 $im->addcolors(colors => [ ($red, $blue) x 9 ]);
200 1 $im->box(filled => 1, color => $red, ymax => 5);
201 1 $im->box(filled => 1, color => $blue, ymin => 6);
202 1 ok(!$im->write(type => 'ico', io => limited_write_io(22)),
203 "third write (bmi) should fail (8-bit)");
204 1 is($im->errstr, "Write failure: limit reached", "check message");
205
206 1 ok(!$im->write(type => 'ico', io => limited_write_io(62)),
207 "fourth write (palette) should fail (8-bit)");
208 1 is($im->errstr, "Write failure: limit reached", "check message");
209 1 ok(!$im->write(type => 'ico', io => limited_write_io(62 + 1024)),
210 "fifth write (image) should fail (8-bit)");
211 1 is($im->errstr, "Write failure: limit reached", "check message");
212 1 ok(!$im->write(type => 'ico', io => limited_write_io(62 + 1024 + 10)),
213 "sixth write (zeroes) should fail (8-bit)");
214 1 is($im->errstr, "Write failure: limit reached", "check message");
215 1 my $data;
216 1 ok($im->write(data => \$data, type => 'ico'), "write 8 bit successfully");
217 1 my $read = Imager->new;
218 1 ok($read->read(data => $data), "read it back");
219 1 is($read->type, 'paletted', "check type");
220 1 is($read->tags(name => 'ico_bits'), 8, "check bits");
221 1 is(Imager::i_img_diff($read, $im), 0, "check image correct");
222 }
223  
224 # write callback that fails
225 8 ▶sub write_failure {
Subroutine write_failure: called CC=1 SCAR=0.0
226 8 print "# synthesized write failure\n";
227 8 Imager::i_push_error(0, "synthetic error");
228 8 return;
229 }
230  
231 15 ▶sub limited_write_io {
Subroutine limited_write_io: called CC=1 SCAR=0.0
232 15 my ($limit) = @_;
233  
234 15 my $io = Imager::io_new_cb(limited_write($limit), undef, undef, undef, 1);
235 15 $io->set_buffered(0);
236  
237 15 return $io;
238 }
239  
240 15 ▶sub limited_write {
Subroutine limited_write: called CC=1 SCAR=0.0
241 15 my ($limit) = @_;
242  
243 return
244 68 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
245 68 my ($data) = @_;
246 68 $limit -= length $data;
247 68 ▶ if ($limit >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit >= 0) { } 53 15 68
248 53 print "# write of ", length $data, " bytes successful ($limit left)\n";
249 53 return 1;
250 }
251 else {
252 15 print "# write of ", length $data, " bytes failed\n";
253 15 Imager::i_push_error(0, "limit reached");
254 15 return;
255 }
256 15 };
257 }
« Flines/t/t00flines.t ↑ Summary dynaload.c »