11 # use Test::NoWarnings, if available
14 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
16 plan tests => 595 + $extra ;
18 use_ok('Compress::Zlib', 2) ;
20 use_ok('IO::Compress::Deflate', qw($DeflateError)) ;
21 use_ok('IO::Uncompress::Inflate', qw($InflateError)) ;
23 use_ok('Compress::Zlib::FileConstants');
30 my $string = shift || '' ;
34 ok my $def = new IO::Compress::Deflate \$buffer, %opts ;
35 is $def->write($string), length($string) ;
37 #print "ReadHeaderInfo\n"; hexDump(\$buffer);
39 ok my $inf = new IO::Uncompress::Inflate \$buffer ;
41 #ok $inf->read($uncomp) ;
44 while (($status = $inf->read($uncomp)) > 0) {
48 is $actual, length($string) ;
52 ok my $hdr = $inf->getHeaderInfo();
58 sub ReadHeaderInfoZlib
60 my $string = shift || '' ;
64 ok my $def = new Compress::Zlib::Deflate AppendOutput => 1, %opts ;
65 cmp_ok $def->deflate($string, $buffer), '==', Z_OK;
66 cmp_ok $def->flush($buffer), '==', Z_OK;
67 #print "ReadHeaderInfoZlib\n"; hexDump(\$buffer);
69 ok my $inf = new IO::Uncompress::Inflate \$buffer ;
71 #ok $inf->read($uncomp) ;
74 while (($status = $inf->read($uncomp)) > 0) {
78 is $actual, length($string) ;
82 ok my $hdr = $inf->getHeaderInfo();
91 my $inf = new IO::Uncompress::Inflate \$buffer ;
92 my $hdr = $inf->getHeaderInfo();
94 no warnings 'uninitialized' ;
95 while (my ($k, $v) = each %$hdr) {
101 # Check the Deflate Header Parameters
102 #========================================
104 my $name = "test.gz" ;
105 my $lex = new LexFile $name ;
108 title "Check default header settings" ;
114 my $hdr = ReadHeaderInfo($string);
116 is $hdr->{CM}, 8, " CM is 8";
117 is $hdr->{FDICT}, 0, " FDICT is 0";
122 title "Check user-defined header settings match zlib" ;
129 [ {-Level => 0}, { FLEVEL => ZLIB_FLG_LEVEL_FASTEST} ],
130 [ {-Level => 1}, { FLEVEL => ZLIB_FLG_LEVEL_FASTEST} ],
131 [ {-Level => 2}, { FLEVEL => ZLIB_FLG_LEVEL_FAST } ],
132 [ {-Level => 3}, { FLEVEL => ZLIB_FLG_LEVEL_FAST } ],
133 [ {-Level => 4}, { FLEVEL => ZLIB_FLG_LEVEL_FAST } ],
134 [ {-Level => 5}, { FLEVEL => ZLIB_FLG_LEVEL_FAST } ],
135 [ {-Level => 6}, { FLEVEL => ZLIB_FLG_LEVEL_DEFAULT} ],
136 [ {-Level => 7}, { FLEVEL => ZLIB_FLG_LEVEL_SLOWEST} ],
137 [ {-Level => 8}, { FLEVEL => ZLIB_FLG_LEVEL_SLOWEST} ],
138 [ {-Level => 9}, { FLEVEL => ZLIB_FLG_LEVEL_SLOWEST} ],
140 [ {-Level => Z_NO_COMPRESSION }, { FLEVEL => ZLIB_FLG_LEVEL_FASTEST} ],
141 [ {-Level => Z_BEST_SPEED }, { FLEVEL => ZLIB_FLG_LEVEL_FASTEST} ],
142 [ {-Level => Z_BEST_COMPRESSION}, { FLEVEL => ZLIB_FLG_LEVEL_SLOWEST} ],
143 [ {-Level => Z_DEFAULT_COMPRESSION}, { FLEVEL => ZLIB_FLG_LEVEL_DEFAULT} ],
145 [ {-Strategy => Z_HUFFMAN_ONLY}, { FLEVEL => ZLIB_FLG_LEVEL_FASTEST} ],
146 [ {-Strategy => Z_HUFFMAN_ONLY,
147 -Level => 3 }, { FLEVEL => ZLIB_FLG_LEVEL_FASTEST} ],
150 foreach my $test (@tests)
152 my $opts = $test->[0] ;
153 my $expect = $test->[1] ;
156 while (my ($k, $v) = each %$opts)
158 push @title, "$k => $v";
162 my $hdr = ReadHeaderInfo($string, %$opts);
164 my $hdr1 = ReadHeaderInfoZlib($string, %$opts);
166 is $hdr->{CM}, 8, " CM is 8";
167 is $hdr->{CINFO}, 7, " CINFO is 7";
168 is $hdr->{FDICT}, 0, " FDICT is 0";
170 while (my ($k, $v) = each %$expect)
172 if (ZLIB_VERNUM >= 0x1220)
173 { is $hdr->{$k}, $v, " $k is $v" }
175 { ok 1, " Skip test for $k" }
178 is $hdr->{CM}, $hdr1->{CM}, " CM matches";
179 is $hdr->{CINFO}, $hdr1->{CINFO}, " CINFO matches";
180 is $hdr->{FDICT}, $hdr1->{FDICT}, " FDICT matches";
181 is $hdr->{FLEVEL}, $hdr1->{FLEVEL}, " FLEVEL matches";
182 is $hdr->{FCHECK}, $hdr1->{FCHECK}, " FCHECK matches";
189 title "No compressed data at all";
191 my $hdr = ReadHeaderInfo("");
193 is $hdr->{CM}, 8, " CM is 8";
194 is $hdr->{FDICT}, 0, " FDICT is 0";
196 ok defined $hdr->{ADLER32}, " ADLER32 is defined" ;
197 is $hdr->{ADLER32}, 1, " ADLER32 is 1";
201 # Header Corruption Tests
208 ok my $x = new IO::Compress::Deflate \$good ;
209 ok $x->write($string) ;
213 title "Header Corruption - FCHECK failure - 1st byte wrong";
215 substr($buffer, 0, 1) = "\x00" ;
217 ok ! new IO::Uncompress::Inflate \$buffer, -Transparent => 0 ;
218 like $IO::Uncompress::Inflate::InflateError, '/Header Error: CRC mismatch/',
223 title "Header Corruption - FCHECK failure - 2nd byte wrong";
225 substr($buffer, 1, 1) = "\x00" ;
227 ok ! new IO::Uncompress::Inflate \$buffer, -Transparent => 0 ;
228 like $IO::Uncompress::Inflate::InflateError, '/Header Error: CRC mismatch/',
240 my $cmf = ($method & 0x0F) ;
241 $cmf |= (($cinfo & 0x0F) << 4) ;
242 my $flg = (($level & 0x03) << 6) ;
243 $flg |= (($fdict & 0x01) << 5) ;
244 my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ;
246 #print "check $fcheck\n";
248 return pack("CC", $cmf, $flg) ;
252 title "Header Corruption - CM not 8";
254 my $header = mkZlibHdr(3, 6, 0, 3);
256 substr($buffer, 0, 2) = $header;
258 my $un = new IO::Uncompress::Inflate \$buffer, -Transparent => 0 ;
259 ok ! new IO::Uncompress::Inflate \$buffer, -Transparent => 0 ;
260 like $IO::Uncompress::Inflate::InflateError, '/Header Error: Not Deflate \(CM is 3\)/',
267 # Trailer Corruption tests
274 ok my $x = new IO::Compress::Deflate \$good ;
275 ok $x->write($string) ;
278 foreach my $trim (-4 .. -1)
280 my $got = $trim + 4 ;
283 title "Trailer Corruption - Trailer truncated to $got bytes, strict $s" ;
285 my $expected_trailing = substr($good, -4, 4) ;
286 substr($expected_trailing, $trim) = '';
288 substr($buffer, $trim) = '';
289 writeFile($name, $buffer) ;
291 ok my $gunz = new IO::Uncompress::Inflate $name, Strict => $s;
295 ok $gunz->read($uncomp) < 0 ;
296 like $IO::Uncompress::Inflate::InflateError,"/Trailer Error: trailer truncated. Expected 4 bytes, got $got/",
301 is $gunz->read($uncomp), length $string ;
304 ok $uncomp eq $string;
311 title "Trailer Corruption - CRC Wrong, strict" ;
313 my $crc = unpack("N", substr($buffer, -4, 4));
314 substr($buffer, -4, 4) = pack('N', $crc+1);
315 writeFile($name, $buffer) ;
317 ok my $gunz = new IO::Uncompress::Inflate $name, Strict => 1;
319 ok $gunz->read($uncomp) < 0 ;
320 like $IO::Uncompress::Inflate::InflateError,'/Trailer Error: CRC mismatch/',
321 "Trailer Error: CRC mismatch";
323 ok ! ${ $gunz->trailingData() } ;
324 ok $uncomp eq $string;
329 title "Trailer Corruption - CRC Wrong, no strict" ;
331 my $crc = unpack("N", substr($buffer, -4, 4));
332 substr($buffer, -4, 4) = pack('N', $crc+1);
333 writeFile($name, $buffer) ;
335 ok my $gunz = new IO::Uncompress::Inflate $name, Strict => 0;
337 ok $gunz->read($uncomp) >= 0 ;
339 ok ! ${ $gunz->trailingData() } ;
340 ok $uncomp eq $string;