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 $out_file = 'abcde.out';
82 my $lex = new LexFile($out_file) ;
85 open F, ">$out_file" ; print F "x"; close F;
86 ok -e $out_file, " file exists" ;
87 ok !-z $out_file, " and is not empty" ;
90 is chmod(0444, $out_file), 1, " chmod worked" ;
91 ok -e $out_file, " still exists after chmod" ;
95 skip "Cannot create non-writable file", 3
98 ok ! -w $out_file, " chmod made file unwritable" ;
102 { $dest = $out_file }
104 { $dest = new IO::File "<$out_file" }
106 my $gz = $CompressClass->new($dest, Merge => 1) ;
108 ok ! $gz, " Did not create $CompressClass object";
112 is $$Error, "Output file '$out_file' is not writable",
113 " Got non-writable filename message" ;
116 is $$Error, "Output filehandle is not writable",
117 " Got non-writable filehandle message" ;
122 chmod 0777, $out_file ;
126 # output is not compressed at all
127 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
130 my $Error = getErrorRef($CompressClass);
132 my $out_file = 'abcde.out';
133 my $lex = new LexFile($out_file) ;
135 foreach my $to_file ( qw(buffer file handle ) )
137 title "$CompressClass to $to_file, content is not compressed";
139 my $content = "abc" x 300 ;
141 my $disp_content = defined $content ? $content : '<undef>' ;
142 my $str_content = defined $content ? $content : '' ;
144 if ($to_file eq 'buffer')
146 $buffer = \$content ;
150 writeFile($out_file, $content);
152 if ($to_file eq 'handle')
154 $buffer = new IO::File "+<$out_file"
155 or die "# Cannot open $out_file: $!";
158 { $buffer = $out_file }
161 ok ! $CompressClass->new($buffer, Merge => 1), " constructor fails";
163 like $$Error, '/Cannot create InflateScan object: (Header Error|unexpected end of file)/', " got Bad Magic" ;
170 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
173 my $Error = getErrorRef($CompressClass);
175 my $out_file = 'abcde.out';
176 my $lex = new LexFile($out_file) ;
178 foreach my $to_file ( qw(buffer file handle ) )
180 title "$CompressClass to $to_file, content is empty";
186 if ($to_file eq 'buffer')
188 $dest = $buffer = \$content ;
192 writeFile($out_file, $content);
195 if ($to_file eq 'handle')
197 $buffer = new IO::File "+<$out_file"
198 or die "# Cannot open $out_file: $!";
201 { $buffer = $out_file }
204 ok my $gz = $CompressClass->new($buffer, Merge => 1, AutoClose => 1), " constructor passes";
210 my $out = anyUncompress($dest);
212 is $out, "FGHI", ' Merge OK';
216 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
218 my $Error = getErrorRef($CompressClass);
220 title "$CompressClass - Merge to file that doesn't exist";
222 my $out_file = 'abcd.out';
223 my $lex = new LexFile($out_file) ;
225 ok ! -e $out_file, " Destination file, '$out_file', does not exist";
227 ok my $gz1 = $CompressClass->new($out_file, Merge => 1)
228 or die "# $CompressClass->new failed: $GzipError\n";
234 my $out = anyUncompress($out_file);
236 is $out, "FGHI", ' Merged OK';
239 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
241 my $Error = getErrorRef($CompressClass);
243 my $out_file = 'abcde.out';
244 my $lex = new LexFile($out_file) ;
246 foreach my $to_file ( qw( buffer file handle ) )
248 foreach my $content (undef, '', 'x', 'abcde')
250 #next if ! defined $content && $to_file;
253 my $disp_content = defined $content ? $content : '<undef>' ;
254 my $str_content = defined $content ? $content : '' ;
256 if ($to_file eq 'buffer')
260 title "$CompressClass to Buffer, content is '$disp_content'";
264 $buffer = $out_file ;
265 if ($to_file eq 'handle')
267 title "$CompressClass to Filehandle, content is '$disp_content'";
271 title "$CompressClass to File, content is '$disp_content'";
275 my $gz = $CompressClass->new($buffer);
276 my $len = defined $content ? length($content) : 0 ;
277 is $gz->write($content), $len, " write ok";
278 ok $gz->close(), " close ok";
281 is anyUncompress($buffer), $str_content, ' Destination is ok';
285 # next if $TopTypes eq 'RawDeflate' && $content eq '';
290 if ($to_file eq 'handle')
292 $dest = new IO::File "+<$buffer" ;
295 my $gz1 = $CompressClass->new($dest, Merge => 1, AutoClose => 1)
296 or die "## $GzipError\n";
300 is $gz1->write("FGHI"), 4, " write returned 4";
301 ok $gz1->close(), " close ok";
304 my $out = anyUncompress($buffer);
306 is $out, $str_content . "FGHI", ' Merged OK';
314 foreach my $CompressClass ( map { "IO::Compress::$_" } qw( Gzip RawDeflate Deflate) )
316 my $Error = getErrorRef($CompressClass);
318 my $Func = getTopFuncRef($CompressClass);
319 my $TopType = getTopFuncName($CompressClass);
323 my $out_file = 'abcde.out';
324 my $lex = new LexFile($out_file) ;
326 foreach my $to_file (0, 1)
328 foreach my $content (undef, '', 'x', 'abcde')
330 my $disp_content = defined $content ? $content : '<undef>' ;
331 my $str_content = defined $content ? $content : '' ;
335 $buffer = $out_file ;
336 title "$TopType to File, content is '$disp_content'";
342 title "$TopType to Buffer, content is '$disp_content'";
346 ok $Func->(\$content, $buffer), " Compress content";
348 is anyUncompress($buffer), $str_content, ' Destination is ok';
351 ok $Func->(\"FGHI", $buffer, Merge => 1), " Merge content";
354 my $out = anyUncompress($buffer);
356 is $out, $str_content . "FGHI", ' Merged OK';