4 @INC = ("../lib", "lib");
17 use Compress::Zlib 2 ;
19 use IO::Compress::Gzip qw($GzipError);
20 use IO::Uncompress::Gunzip qw($GunzipError);
22 use IO::Compress::Deflate qw($DeflateError);
23 use IO::Uncompress::Inflate qw($InflateError);
25 use IO::Compress::RawDeflate qw($RawDeflateError);
26 use IO::Uncompress::RawInflate qw($RawInflateError);
31 plan(skip_all => "Merge needs Zlib 1.2.1 or better - you have Zlib "
32 . Compress::Zlib::zlib_version())
33 if ZLIB_VERNUM() < 0x1210 ;
35 # use Test::NoWarnings, if available
38 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
40 plan tests => 490 + $extra ;
45 # Check zlib_version and ZLIB_VERSION are the same.
46 is Compress::Zlib::zlib_version, ZLIB_VERSION,
47 "ZLIB_VERSION matches Compress::Zlib::zlib_version" ;
50 # destination is a file that doesn't exist -- should work ok unless AnyDeflate
51 # destination isn't compressed at all
52 # destination is compressed but wrong format
53 # destination is corrupt - error messages should be correct
54 # use apend mode with old zlib - check that this is trapped
55 # destination is not seekable, readable, writable - test for filename & handle
58 title "Misc error cases";
60 eval { new Compress::Zlib::InflateScan Bufsize => 0} ;
61 like $@, mkErr("^Compress::Zlib::InflateScan::new: Bufsize must be >= 1, you specified 0"), " catch bufsize == 0";
63 eval { Compress::Zlib::inflateScanStream::createDeflateStream(undef, Bufsize => 0) } ;
64 like $@, mkErr("^Compress::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified 0"), " catch bufsize == 0";
68 # output file/handle not writable
69 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
72 my $Error = getErrorRef($CompressClass);
74 foreach my $to_file (0,1)
77 { title "$CompressClass - Merge to filename that isn't writable" }
79 { title "$CompressClass - Merge to filehandle that isn't writable" }
81 my $lex = new LexFile my $out_file ;
84 open F, ">$out_file" ; print F "x"; close F;
85 ok -e $out_file, " file exists" ;
86 ok !-z $out_file, " and is not empty" ;
89 is chmod(0444, $out_file), 1, " chmod worked" ;
90 ok -e $out_file, " still exists after chmod" ;
94 skip "Cannot create non-writable file", 3
97 ok ! -w $out_file, " chmod made file unwritable" ;
101 { $dest = $out_file }
103 { $dest = new IO::File "<$out_file" }
105 my $gz = $CompressClass->new($dest, Merge => 1) ;
107 ok ! $gz, " Did not create $CompressClass object";
111 is $$Error, "Output file '$out_file' is not writable",
112 " Got non-writable filename message" ;
115 is $$Error, "Output filehandle is not writable",
116 " Got non-writable filehandle message" ;
121 chmod 0777, $out_file ;
125 # output is not compressed at all
126 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
129 my $Error = getErrorRef($CompressClass);
131 my $lex = new LexFile my $out_file ;
133 foreach my $to_file ( qw(buffer file handle ) )
135 title "$CompressClass to $to_file, content is not compressed";
137 my $content = "abc" x 300 ;
139 my $disp_content = defined $content ? $content : '<undef>' ;
140 my $str_content = defined $content ? $content : '' ;
142 if ($to_file eq 'buffer')
144 $buffer = \$content ;
148 writeFile($out_file, $content);
150 if ($to_file eq 'handle')
152 $buffer = new IO::File "+<$out_file"
153 or die "# Cannot open $out_file: $!";
156 { $buffer = $out_file }
159 ok ! $CompressClass->new($buffer, Merge => 1), " constructor fails";
161 like $$Error, '/Cannot create InflateScan object: (Header Error|unexpected end of file)/', " got Bad Magic" ;
168 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
171 my $Error = getErrorRef($CompressClass);
173 my $lex = new LexFile my $out_file ;
175 foreach my $to_file ( qw(buffer file handle ) )
177 title "$CompressClass to $to_file, content is empty";
183 if ($to_file eq 'buffer')
185 $dest = $buffer = \$content ;
189 writeFile($out_file, $content);
192 if ($to_file eq 'handle')
194 $buffer = new IO::File "+<$out_file"
195 or die "# Cannot open $out_file: $!";
198 { $buffer = $out_file }
201 ok my $gz = $CompressClass->new($buffer, Merge => 1, AutoClose => 1), " constructor passes";
207 my $out = anyUncompress($dest);
209 is $out, "FGHI", ' Merge OK';
213 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
215 my $Error = getErrorRef($CompressClass);
217 title "$CompressClass - Merge to file that doesn't exist";
219 my $lex = new LexFile my $out_file ;
221 ok ! -e $out_file, " Destination file, '$out_file', does not exist";
223 ok my $gz1 = $CompressClass->new($out_file, Merge => 1)
224 or die "# $CompressClass->new failed: $GzipError\n";
230 my $out = anyUncompress($out_file);
232 is $out, "FGHI", ' Merged OK';
235 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
237 my $Error = getErrorRef($CompressClass);
239 my $lex = new LexFile my $out_file ;
241 foreach my $to_file ( qw( buffer file handle ) )
243 foreach my $content (undef, '', 'x', 'abcde')
245 #next if ! defined $content && $to_file;
248 my $disp_content = defined $content ? $content : '<undef>' ;
249 my $str_content = defined $content ? $content : '' ;
251 if ($to_file eq 'buffer')
255 title "$CompressClass to Buffer, content is '$disp_content'";
259 $buffer = $out_file ;
260 if ($to_file eq 'handle')
262 title "$CompressClass to Filehandle, content is '$disp_content'";
266 title "$CompressClass to File, content is '$disp_content'";
270 my $gz = $CompressClass->new($buffer);
271 my $len = defined $content ? length($content) : 0 ;
272 is $gz->write($content), $len, " write ok";
273 ok $gz->close(), " close ok";
276 is anyUncompress($buffer), $str_content, ' Destination is ok';
280 # next if $TopTypes eq 'RawDeflate' && $content eq '';
285 if ($to_file eq 'handle')
287 $dest = new IO::File "+<$buffer" ;
290 my $gz1 = $CompressClass->new($dest, Merge => 1, AutoClose => 1)
291 or die "## Error is $$Error\n";
296 is $gz1->write("FGHI"), 4, " write returned 4";
297 ok $gz1->close(), " close ok";
300 my $out = anyUncompress($buffer);
302 is $out, $str_content . "FGHI", ' Merged OK';
311 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
313 my $Error = getErrorRef($CompressClass);
315 my $Func = getTopFuncRef($CompressClass);
316 my $TopType = getTopFuncName($CompressClass);
320 my $lex = new LexFile my $out_file ;
322 foreach my $to_file (0, 1)
324 foreach my $content (undef, '', 'x', 'abcde')
326 my $disp_content = defined $content ? $content : '<undef>' ;
327 my $str_content = defined $content ? $content : '' ;
331 $buffer = $out_file ;
332 title "$TopType to File, content is '$disp_content'";
338 title "$TopType to Buffer, content is '$disp_content'";
342 ok $Func->(\$content, $buffer), " Compress content";
344 is anyUncompress($buffer), $str_content, ' Destination is ok';
347 ok $Func->(\"FGHI", $buffer, Merge => 1), " Merge content";
350 my $out = anyUncompress($buffer);
352 is $out, $str_content . "FGHI", ' Merged OK';