9 use Compress::Raw::Zlib 2 ;
13 plan(skip_all => "Merge needs Zlib 1.2.1 or better - you have Zlib "
14 . Compress::Raw::Zlib::zlib_version())
15 if ZLIB_VERNUM() < 0x1210 ;
17 # use Test::NoWarnings, if available
20 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
22 plan tests => 166 + $extra ;
30 my $CompressClass = identify();
31 my $UncompressClass = getInverse($CompressClass);
32 my $Error = getErrorRef($CompressClass);
33 my $UnError = getErrorRef($UncompressClass);
38 # Check zlib_version and ZLIB_VERSION are the same.
39 is Compress::Raw::Zlib::zlib_version, ZLIB_VERSION,
40 "ZLIB_VERSION matches Compress::Raw::Zlib::zlib_version" ;
43 # destination is a file that doesn't exist -- should work ok unless AnyDeflate
44 # destination isn't compressed at all
45 # destination is compressed but wrong format
46 # destination is corrupt - error messages should be correct
47 # use apend mode with old zlib - check that this is trapped
48 # destination is not seekable, readable, writable - test for filename & handle
51 title "Misc error cases";
53 eval { new Compress::Raw::Zlib::InflateScan Bufsize => 0} ;
54 like $@, mkErr("^Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified 0"), " catch bufsize == 0";
56 eval { Compress::Raw::Zlib::inflateScanStream::createDeflateStream(undef, Bufsize => 0) } ;
57 like $@, mkErr("^Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified 0"), " catch bufsize == 0";
61 # output file/handle not writable
64 foreach my $to_file (0,1)
67 { title "$CompressClass - Merge to filename that isn't writable" }
69 { title "$CompressClass - Merge to filehandle that isn't writable" }
71 my $lex = new LexFile my $out_file ;
74 open F, ">$out_file" ; print F "x"; close F;
75 ok -e $out_file, " file exists" ;
76 ok !-z $out_file, " and is not empty" ;
79 is chmod(0444, $out_file), 1, " chmod worked" ;
80 ok -e $out_file, " still exists after chmod" ;
84 skip "Cannot create non-writable file", 3
87 ok ! -w $out_file, " chmod made file unwritable" ;
93 { $dest = new IO::File "<$out_file" }
95 my $gz = $CompressClass->new($dest, Merge => 1) ;
97 ok ! $gz, " Did not create $CompressClass object";
101 is $$Error, "Output file '$out_file' is not writable",
102 " Got non-writable filename message" ;
105 is $$Error, "Output filehandle is not writable",
106 " Got non-writable filehandle message" ;
111 chmod 0777, $out_file ;
115 # output is not compressed at all
118 my $lex = new LexFile my $out_file ;
120 foreach my $to_file ( qw(buffer file handle ) )
122 title "$CompressClass to $to_file, content is not compressed";
124 my $content = "abc" x 300 ;
126 my $disp_content = defined $content ? $content : '<undef>' ;
127 my $str_content = defined $content ? $content : '' ;
129 if ($to_file eq 'buffer')
131 $buffer = \$content ;
135 writeFile($out_file, $content);
137 if ($to_file eq 'handle')
139 $buffer = new IO::File "+<$out_file"
140 or die "# Cannot open $out_file: $!";
143 { $buffer = $out_file }
146 ok ! $CompressClass->new($buffer, Merge => 1), " constructor fails";
148 like $$Error, '/Cannot create InflateScan object: (Header Error|unexpected end of file)/', " got Bad Magic" ;
157 my $lex = new LexFile my $out_file ;
159 foreach my $to_file ( qw(buffer file handle ) )
161 title "$CompressClass to $to_file, content is empty";
167 if ($to_file eq 'buffer')
169 $dest = $buffer = \$content ;
173 writeFile($out_file, $content);
176 if ($to_file eq 'handle')
178 $buffer = new IO::File "+<$out_file"
179 or die "# Cannot open $out_file: $!";
182 { $buffer = $out_file }
185 ok my $gz = $CompressClass->new($buffer, Merge => 1, AutoClose => 1), " constructor passes"
192 my $out = anyUncompress($dest);
194 is $out, "FGHI", ' Merge OK';
199 title "$CompressClass - Merge to file that doesn't exist";
201 my $lex = new LexFile my $out_file ;
203 ok ! -e $out_file, " Destination file, '$out_file', does not exist";
205 ok my $gz1 = $CompressClass->new($out_file, Merge => 1)
206 or die "# $CompressClass->new failed: $$Error\n";
212 my $out = anyUncompress($out_file);
214 is $out, "FGHI", ' Merged OK';
219 my $lex = new LexFile my $out_file ;
221 foreach my $to_file ( qw( buffer file handle ) )
223 foreach my $content (undef, '', 'x', 'abcde')
225 #next if ! defined $content && $to_file;
228 my $disp_content = defined $content ? $content : '<undef>' ;
229 my $str_content = defined $content ? $content : '' ;
231 if ($to_file eq 'buffer')
235 title "$CompressClass to Buffer, content is '$disp_content'";
239 $buffer = $out_file ;
240 if ($to_file eq 'handle')
242 title "$CompressClass to Filehandle, content is '$disp_content'";
246 title "$CompressClass to File, content is '$disp_content'";
250 my $gz = $CompressClass->new($buffer);
251 my $len = defined $content ? length($content) : 0 ;
252 is $gz->write($content), $len, " write ok";
253 ok $gz->close(), " close ok";
256 is anyUncompress($buffer), $str_content, ' Destination is ok';
260 # next if $TopTypes eq 'RawDeflate' && $content eq '';
265 if ($to_file eq 'handle')
267 $dest = new IO::File "+<$buffer" ;
270 my $gz1 = $CompressClass->new($dest, Merge => 1, AutoClose => 1)
271 or die "## Error is $$Error\n";
276 is $gz1->write("FGHI"), 4, " write returned 4";
277 ok $gz1->close(), " close ok";
280 my $out = anyUncompress($buffer);
282 is $out, $str_content . "FGHI", ' Merged OK';
292 my $Func = getTopFuncRef($CompressClass);
293 my $TopType = getTopFuncName($CompressClass);
297 my $lex = new LexFile my $out_file ;
299 foreach my $to_file (0, 1)
301 foreach my $content (undef, '', 'x', 'abcde')
303 my $disp_content = defined $content ? $content : '<undef>' ;
304 my $str_content = defined $content ? $content : '' ;
308 $buffer = $out_file ;
309 title "$TopType to File, content is '$disp_content'";
315 title "$TopType to Buffer, content is '$disp_content'";
319 ok $Func->(\$content, $buffer), " Compress content";
321 is anyUncompress($buffer), $str_content, ' Destination is ok';
324 ok $Func->(\"FGHI", $buffer, Merge => 1), " Merge content";
327 my $out = anyUncompress($buffer);
329 is $out, $str_content . "FGHI", ' Merged OK';