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 ok $$Error, " Got error message" ;
110 chmod 0777, $out_file ;
114 # output is not compressed at all
117 my $lex = new LexFile my $out_file ;
119 foreach my $to_file ( qw(buffer file handle ) )
121 title "$CompressClass to $to_file, content is not compressed";
123 my $content = "abc" x 300 ;
125 my $disp_content = defined $content ? $content : '<undef>' ;
126 my $str_content = defined $content ? $content : '' ;
128 if ($to_file eq 'buffer')
130 $buffer = \$content ;
134 writeFile($out_file, $content);
136 if ($to_file eq 'handle')
138 $buffer = new IO::File "+<$out_file"
139 or die "# Cannot open $out_file: $!";
142 { $buffer = $out_file }
145 ok ! $CompressClass->new($buffer, Merge => 1), " constructor fails";
147 like $$Error, '/Cannot create InflateScan object: (Header Error|unexpected end of file)/', " got Bad Magic" ;
156 my $lex = new LexFile my $out_file ;
158 foreach my $to_file ( qw(buffer file handle ) )
160 title "$CompressClass to $to_file, content is empty";
166 if ($to_file eq 'buffer')
168 $dest = $buffer = \$content ;
172 writeFile($out_file, $content);
175 if ($to_file eq 'handle')
177 $buffer = new IO::File "+<$out_file"
178 or die "# Cannot open $out_file: $!";
181 { $buffer = $out_file }
184 ok my $gz = $CompressClass->new($buffer, Merge => 1, AutoClose => 1), " constructor passes"
191 my $out = anyUncompress($dest);
193 is $out, "FGHI", ' Merge OK';
198 title "$CompressClass - Merge to file that doesn't exist";
200 my $lex = new LexFile my $out_file ;
202 ok ! -e $out_file, " Destination file, '$out_file', does not exist";
204 ok my $gz1 = $CompressClass->new($out_file, Merge => 1)
205 or die "# $CompressClass->new failed: $$Error\n";
211 my $out = anyUncompress($out_file);
213 is $out, "FGHI", ' Merged OK';
218 my $lex = new LexFile my $out_file ;
220 foreach my $to_file ( qw( buffer file handle ) )
222 foreach my $content (undef, '', 'x', 'abcde')
224 #next if ! defined $content && $to_file;
227 my $disp_content = defined $content ? $content : '<undef>' ;
228 my $str_content = defined $content ? $content : '' ;
230 if ($to_file eq 'buffer')
234 title "$CompressClass to Buffer, content is '$disp_content'";
238 $buffer = $out_file ;
239 if ($to_file eq 'handle')
241 title "$CompressClass to Filehandle, content is '$disp_content'";
245 title "$CompressClass to File, content is '$disp_content'";
249 my $gz = $CompressClass->new($buffer);
250 my $len = defined $content ? length($content) : 0 ;
251 is $gz->write($content), $len, " write ok";
252 ok $gz->close(), " close ok";
255 is anyUncompress($buffer), $str_content, ' Destination is ok';
259 # next if $TopTypes eq 'RawDeflate' && $content eq '';
264 if ($to_file eq 'handle')
266 $dest = new IO::File "+<$buffer" ;
269 my $gz1 = $CompressClass->new($dest, Merge => 1, AutoClose => 1)
270 or die "## Error is $$Error\n";
275 is $gz1->write("FGHI"), 4, " write returned 4";
276 ok $gz1->close(), " close ok";
279 my $out = anyUncompress($buffer);
281 is $out, $str_content . "FGHI", ' Merged OK';
291 my $Func = getTopFuncRef($CompressClass);
292 my $TopType = getTopFuncName($CompressClass);
296 my $lex = new LexFile my $out_file ;
298 foreach my $to_file (0, 1)
300 foreach my $content (undef, '', 'x', 'abcde')
302 my $disp_content = defined $content ? $content : '<undef>' ;
303 my $str_content = defined $content ? $content : '' ;
307 $buffer = $out_file ;
308 title "$TopType to File, content is '$disp_content'";
314 title "$TopType to Buffer, content is '$disp_content'";
318 ok $Func->(\$content, $buffer), " Compress content";
320 is anyUncompress($buffer), $str_content, ' Destination is ok';
323 ok $Func->(\"FGHI", $buffer, Merge => 1), " Merge content";
326 my $out = anyUncompress($buffer);
328 is $out, $str_content . "FGHI", ' Merged OK';