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 => 165 + $extra ;
30 my $CompressClass = identify();
31 my $UncompressClass = getInverse($CompressClass);
32 my $Error = getErrorRef($CompressClass);
33 my $UnError = getErrorRef($UncompressClass);
36 # destination is a file that doesn't exist -- should work ok unless AnyDeflate
37 # destination isn't compressed at all
38 # destination is compressed but wrong format
39 # destination is corrupt - error messages should be correct
40 # use apend mode with old zlib - check that this is trapped
41 # destination is not seekable, readable, writable - test for filename & handle
44 title "Misc error cases";
46 eval { new Compress::Raw::Zlib::InflateScan Bufsize => 0} ;
47 like $@, mkErr("^Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified 0"), " catch bufsize == 0";
49 eval { Compress::Raw::Zlib::inflateScanStream::createDeflateStream(undef, Bufsize => 0) } ;
50 like $@, mkErr("^Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified 0"), " catch bufsize == 0";
54 # output file/handle not writable
57 foreach my $to_file (0,1)
60 { title "$CompressClass - Merge to filename that isn't writable" }
62 { title "$CompressClass - Merge to filehandle that isn't writable" }
64 my $lex = new LexFile my $out_file ;
67 open F, ">$out_file" ; print F "x"; close F;
68 ok -e $out_file, " file exists" ;
69 ok !-z $out_file, " and is not empty" ;
72 is chmod(0444, $out_file), 1, " chmod worked" ;
73 ok -e $out_file, " still exists after chmod" ;
77 skip "Cannot create non-writable file", 3
80 ok ! -w $out_file, " chmod made file unwritable" ;
86 { $dest = new IO::File "<$out_file" }
88 my $gz = $CompressClass->new($dest, Merge => 1) ;
90 ok ! $gz, " Did not create $CompressClass object";
94 is $$Error, "Output file '$out_file' is not writable",
95 " Got non-writable filename message" ;
98 ok $$Error, " Got error message" ;
103 chmod 0777, $out_file ;
107 # output is not compressed at all
110 my $lex = new LexFile my $out_file ;
112 foreach my $to_file ( qw(buffer file handle ) )
114 title "$CompressClass to $to_file, content is not compressed";
116 my $content = "abc" x 300 ;
118 my $disp_content = defined $content ? $content : '<undef>' ;
119 my $str_content = defined $content ? $content : '' ;
121 if ($to_file eq 'buffer')
123 $buffer = \$content ;
127 writeFile($out_file, $content);
129 if ($to_file eq 'handle')
131 $buffer = new IO::File "+<$out_file"
132 or die "# Cannot open $out_file: $!";
135 { $buffer = $out_file }
138 ok ! $CompressClass->new($buffer, Merge => 1), " constructor fails";
140 like $$Error, '/Cannot create InflateScan object: (Header Error|unexpected end of file)/', " got Bad Magic" ;
149 my $lex = new LexFile my $out_file ;
151 foreach my $to_file ( qw(buffer file handle ) )
153 title "$CompressClass to $to_file, content is empty";
159 if ($to_file eq 'buffer')
161 $dest = $buffer = \$content ;
165 writeFile($out_file, $content);
168 if ($to_file eq 'handle')
170 $buffer = new IO::File "+<$out_file"
171 or die "# Cannot open $out_file: $!";
174 { $buffer = $out_file }
177 ok my $gz = $CompressClass->new($buffer, Merge => 1, AutoClose => 1), " constructor passes"
184 my $out = anyUncompress($dest);
186 is $out, "FGHI", ' Merge OK';
191 title "$CompressClass - Merge to file that doesn't exist";
193 my $lex = new LexFile my $out_file ;
195 ok ! -e $out_file, " Destination file, '$out_file', does not exist";
197 ok my $gz1 = $CompressClass->new($out_file, Merge => 1)
198 or die "# $CompressClass->new failed: $$Error\n";
204 my $out = anyUncompress($out_file);
206 is $out, "FGHI", ' Merged OK';
211 my $lex = new LexFile my $out_file ;
213 foreach my $to_file ( qw( buffer file handle ) )
215 foreach my $content (undef, '', 'x', 'abcde')
217 #next if ! defined $content && $to_file;
220 my $disp_content = defined $content ? $content : '<undef>' ;
221 my $str_content = defined $content ? $content : '' ;
223 if ($to_file eq 'buffer')
227 title "$CompressClass to Buffer, content is '$disp_content'";
231 $buffer = $out_file ;
232 if ($to_file eq 'handle')
234 title "$CompressClass to Filehandle, content is '$disp_content'";
238 title "$CompressClass to File, content is '$disp_content'";
242 my $gz = $CompressClass->new($buffer);
243 my $len = defined $content ? length($content) : 0 ;
244 is $gz->write($content), $len, " write ok";
245 ok $gz->close(), " close ok";
248 is anyUncompress($buffer), $str_content, ' Destination is ok';
252 # next if $TopTypes eq 'RawDeflate' && $content eq '';
257 if ($to_file eq 'handle')
259 $dest = new IO::File "+<$buffer" ;
262 my $gz1 = $CompressClass->new($dest, Merge => 1, AutoClose => 1)
263 or die "## Error is $$Error\n";
268 is $gz1->write("FGHI"), 4, " write returned 4";
269 ok $gz1->close(), " close ok";
272 my $out = anyUncompress($buffer);
274 is $out, $str_content . "FGHI", ' Merged OK';
284 my $Func = getTopFuncRef($CompressClass);
285 my $TopType = getTopFuncName($CompressClass);
289 my $lex = new LexFile my $out_file ;
291 foreach my $to_file (0, 1)
293 foreach my $content (undef, '', 'x', 'abcde')
295 my $disp_content = defined $content ? $content : '<undef>' ;
296 my $str_content = defined $content ? $content : '' ;
300 $buffer = $out_file ;
301 title "$TopType to File, content is '$disp_content'";
307 title "$TopType to Buffer, content is '$disp_content'";
311 ok $Func->(\$content, $buffer), " Compress content";
313 is anyUncompress($buffer), $str_content, ' Destination is ok';
316 ok $Func->(\"FGHI", $buffer, Merge => 1), " Merge content";
319 my $out = anyUncompress($buffer);
321 is $out, $str_content . "FGHI", ' Merged OK';