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";
92 ok $$Error, " Got error message" ;
95 chmod 0777, $out_file ;
99 # output is not compressed at all
102 my $lex = new LexFile my $out_file ;
104 foreach my $to_file ( qw(buffer file handle ) )
106 title "$CompressClass to $to_file, content is not compressed";
108 my $content = "abc" x 300 ;
110 my $disp_content = defined $content ? $content : '<undef>' ;
111 my $str_content = defined $content ? $content : '' ;
113 if ($to_file eq 'buffer')
115 $buffer = \$content ;
119 writeFile($out_file, $content);
121 if ($to_file eq 'handle')
123 $buffer = new IO::File "+<$out_file"
124 or die "# Cannot open $out_file: $!";
127 { $buffer = $out_file }
130 ok ! $CompressClass->new($buffer, Merge => 1), " constructor fails";
132 like $$Error, '/Cannot create InflateScan object: (Header Error|unexpected end of file|Inflation Error: data error)/', " got Bad Magic" ;
141 my $lex = new LexFile my $out_file ;
143 foreach my $to_file ( qw(buffer file handle ) )
145 title "$CompressClass to $to_file, content is empty";
151 if ($to_file eq 'buffer')
153 $dest = $buffer = \$content ;
157 writeFile($out_file, $content);
160 if ($to_file eq 'handle')
162 $buffer = new IO::File "+<$out_file"
163 or die "# Cannot open $out_file: $!";
166 { $buffer = $out_file }
169 ok my $gz = $CompressClass->new($buffer, Merge => 1, AutoClose => 1), " constructor passes"
176 my $out = anyUncompress($dest);
178 is $out, "FGHI", ' Merge OK';
183 title "$CompressClass - Merge to file that doesn't exist";
185 my $lex = new LexFile my $out_file ;
187 ok ! -e $out_file, " Destination file, '$out_file', does not exist";
189 ok my $gz1 = $CompressClass->new($out_file, Merge => 1)
190 or die "# $CompressClass->new failed: $$Error\n";
196 my $out = anyUncompress($out_file);
198 is $out, "FGHI", ' Merged OK';
203 my $lex = new LexFile my $out_file ;
205 foreach my $to_file ( qw( buffer file handle ) )
207 foreach my $content (undef, '', 'x', 'abcde')
209 #next if ! defined $content && $to_file;
212 my $disp_content = defined $content ? $content : '<undef>' ;
213 my $str_content = defined $content ? $content : '' ;
215 if ($to_file eq 'buffer')
219 title "$CompressClass to Buffer, content is '$disp_content'";
223 $buffer = $out_file ;
224 if ($to_file eq 'handle')
226 title "$CompressClass to Filehandle, content is '$disp_content'";
230 title "$CompressClass to File, content is '$disp_content'";
234 my $gz = $CompressClass->new($buffer);
235 my $len = defined $content ? length($content) : 0 ;
236 is $gz->write($content), $len, " write ok";
237 ok $gz->close(), " close ok";
240 is anyUncompress($buffer), $str_content, ' Destination is ok';
244 # next if $TopTypes eq 'RawDeflate' && $content eq '';
249 if ($to_file eq 'handle')
251 $dest = new IO::File "+<$buffer" ;
254 my $gz1 = $CompressClass->new($dest, Merge => 1, AutoClose => 1)
255 or die "## Error is $$Error\n";
260 is $gz1->write("FGHI"), 4, " write returned 4";
261 ok $gz1->close(), " close ok";
264 my $out = anyUncompress($buffer);
266 is $out, $str_content . "FGHI", ' Merged OK';
276 my $Func = getTopFuncRef($CompressClass);
277 my $TopType = getTopFuncName($CompressClass);
281 my $lex = new LexFile my $out_file ;
283 foreach my $to_file (0, 1)
285 foreach my $content (undef, '', 'x', 'abcde')
287 my $disp_content = defined $content ? $content : '<undef>' ;
288 my $str_content = defined $content ? $content : '' ;
292 $buffer = $out_file ;
293 title "$TopType to File, content is '$disp_content'";
299 title "$TopType to Buffer, content is '$disp_content'";
303 ok $Func->(\$content, $buffer), " Compress content";
305 is anyUncompress($buffer), $str_content, ' Destination is ok';
308 ok $Func->(\"FGHI", $buffer, Merge => 1), " Merge content";
311 my $out = anyUncompress($buffer);
313 is $out, $str_content . "FGHI", ' Merged OK';