IO::Compress* 2.000_12
[p5sagit/p5-mst-13.2.git] / t / lib / compress / CompTestUtils.pm
CommitLineData
25f0751f 1package CompTestUtils;
2
3package main ;
4
5use strict ;
6use warnings;
7use bytes;
8
9use Carp ;
10
11
12sub title
13{
14 #diag "" ;
15 ok 1, $_[0] ;
16 #diag "" ;
17}
18
19sub like_eval
20{
21 like $@, @_ ;
22}
23
24{
25 package LexFile ;
26
27 our ($index);
28 $index = '00000';
29
30 sub new
31 {
32 my $self = shift ;
33 foreach (@_)
34 {
35 # autogenerate the name unless if none supplied
36 $_ = "tst" . $index ++ . ".tmp"
37 unless defined $_;
38 }
39 chmod 0777, @_;
40 for (@_) { 1 while unlink $_ } ;
41 bless [ @_ ], $self ;
42 }
43
44 sub DESTROY
45 {
46 my $self = shift ;
47 chmod 0777, @{ $self } ;
48 for (@$self) { 1 while unlink $_ } ;
49 }
50
51}
52
53{
54 package LexDir ;
55
56 use File::Path;
57 sub new
58 {
59 my $self = shift ;
60 foreach (@_) { rmtree $_ }
61 bless [ @_ ], $self ;
62 }
63
64 sub DESTROY
65 {
66 my $self = shift ;
67 foreach (@$self) { rmtree $_ }
68 }
69}
70sub readFile
71{
72 my $f = shift ;
73
74 my @strings ;
75
76 if (IO::Compress::Base::Common::isaFilehandle($f))
77 {
78 my $pos = tell($f);
79 seek($f, 0,0);
80 @strings = <$f> ;
81 seek($f, 0, $pos);
82 }
83 else
84 {
85 open (F, "<$f")
86 or croak "Cannot open $f: $!\n" ;
87 binmode F;
88 @strings = <F> ;
89 close F ;
90 }
91
92 return @strings if wantarray ;
93 return join "", @strings ;
94}
95
96sub touch
97{
98 foreach (@_) { writeFile($_, '') }
99}
100
101sub writeFile
102{
103 my($filename, @strings) = @_ ;
104 1 while unlink $filename ;
105 open (F, ">$filename")
106 or croak "Cannot open $filename: $!\n" ;
107 binmode F;
108 foreach (@strings) {
109 no warnings ;
110 print F $_ ;
111 }
112 close F ;
113}
114
115sub GZreadFile
116{
117 my ($filename) = shift ;
118
119 my ($uncomp) = "" ;
120 my $line = "" ;
121 my $fil = gzopen($filename, "rb")
122 or croak "Cannopt open '$filename': $Compress::Zlib::gzerrno" ;
123
124 $uncomp .= $line
125 while $fil->gzread($line) > 0;
126
127 $fil->gzclose ;
128 return $uncomp ;
129}
130
131sub hexDump
132{
133 my $d = shift ;
134
135 if (IO::Compress::Base::Common::isaFilehandle($d))
136 {
137 $d = readFile($d);
138 }
139 elsif (IO::Compress::Base::Common::isaFilename($d))
140 {
141 $d = readFile($d);
142 }
143 else
144 {
145 $d = $$d ;
146 }
147
148 my $offset = 0 ;
149
150 $d = '' unless defined $d ;
151 #while (read(STDIN, $data, 16)) {
152 while (my $data = substr($d, 0, 16)) {
153 substr($d, 0, 16) = '' ;
154 printf "# %8.8lx ", $offset;
155 $offset += 16;
156
157 my @array = unpack('C*', $data);
158 foreach (@array) {
159 printf('%2.2x ', $_);
160 }
161 print " " x (16 - @array)
162 if @array < 16 ;
163 $data =~ tr/\0-\37\177-\377/./;
164 print " $data\n";
165 }
166
167}
168
169sub readHeaderInfo
170{
171 my $name = shift ;
172 my %opts = @_ ;
173
174 my $string = <<EOM;
175some text
176EOM
177
178 ok my $x = new IO::Compress::Gzip $name, %opts
179 or diag "GzipError is $IO::Compress::Gzip::GzipError" ;
180 ok $x->write($string) ;
181 ok $x->close ;
182
183 #is GZreadFile($name), $string ;
184
185 ok my $gunz = new IO::Uncompress::Gunzip $name, Strict => 0
186 or diag "GunzipError is $IO::Uncompress::Gunzip::GunzipError" ;
187 ok my $hdr = $gunz->getHeaderInfo();
188 my $uncomp ;
189 ok $gunz->read($uncomp) ;
190 ok $uncomp eq $string;
191 ok $gunz->close ;
192
193 return $hdr ;
194}
195
196sub cmpFile
197{
198 my ($filename, $uue) = @_ ;
199 return readFile($filename) eq unpack("u", $uue) ;
200}
201
202sub uncompressBuffer
203{
204 my $compWith = shift ;
205 my $buffer = shift ;
206
207 my %mapping = ( 'IO::Compress::Gzip' => 'IO::Uncompress::Gunzip',
208 'IO::Compress::Gzip::gzip' => 'IO::Uncompress::Gunzip',
209 'IO::Compress::Deflate' => 'IO::Uncompress::Inflate',
210 'IO::Compress::Deflate::deflate' => 'IO::Uncompress::Inflate',
211 'IO::Compress::RawDeflate' => 'IO::Uncompress::RawInflate',
212 'IO::Compress::RawDeflate::rawdeflate' => 'IO::Uncompress::RawInflate',
213 'IO::Compress::Bzip2' => 'IO::Uncompress::Bunzip2',
214 'IO::Compress::Bzip2::bzip2' => 'IO::Uncompress::Bunzip2',
215 'IO::Compress::Zip' => 'IO::Uncompress::Unzip',
216 'IO::Compress::Zip::zip' => 'IO::Uncompress::Unzip',
217 'IO::Compress::Lzop' => 'IO::Uncompress::UnLzop',
218 'IO::Compress::Lzop::lzop' => 'IO::Uncompress::UnLzop',
219 'IO::Compress::DummyComp' => 'IO::Uncompress::DummyUncomp',
220 'IO::Compress::DummyComp::dummycomp' => 'IO::Uncompress::DummyUncomp',
221 );
222
223 my $out ;
224 my $obj = $mapping{$compWith}->new( \$buffer, -Append => 1);
225 1 while $obj->read($out) > 0 ;
226 return $out ;
227
228}
229
230my %ErrorMap = ( 'IO::Compress::Gzip' => \$IO::Compress::Gzip::GzipError,
231 'IO::Compress::Gzip::gzip' => \$IO::Compress::Gzip::GzipError,
232 'IO::Uncompress::Gunzip' => \$IO::Uncompress::Gunzip::GunzipError,
233 'IO::Uncompress::Gunzip::gunzip' => \$IO::Uncompress::Gunzip::GunzipError,
234 'IO::Uncompress::Inflate' => \$IO::Uncompress::Inflate::InflateError,
235 'IO::Uncompress::Inflate::inflate' => \$IO::Uncompress::Inflate::InflateError,
236 'IO::Compress::Deflate' => \$IO::Compress::Deflate::DeflateError,
237 'IO::Compress::Deflate::deflate' => \$IO::Compress::Deflate::DeflateError,
238 'IO::Uncompress::RawInflate' => \$IO::Uncompress::RawInflate::RawInflateError,
239 'IO::Uncompress::RawInflate::rawinflate' => \$IO::Uncompress::RawInflate::RawInflateError,
240 'IO::Uncompress::AnyInflate' => \$IO::Uncompress::AnyInflate::AnyInflateError,
241 'IO::Uncompress::AnyInflate::anyinflate' => \$IO::Uncompress::AnyInflate::AnyInflateError,
242 'IO::Uncompress::AnyUncompress' => \$IO::Uncompress::AnyUncompress::AnyUncompressError,
243 'IO::Uncompress::AnyUncompress::anyUncompress' => \$IO::Uncompress::AnyUncompress::AnyUncompressError,
244 'IO::Compress::RawDeflate' => \$IO::Compress::RawDeflate::RawDeflateError,
245 'IO::Compress::RawDeflate::rawdeflate' => \$IO::Compress::RawDeflate::RawDeflateError,
246 'IO::Compress::Bzip2' => \$IO::Compress::Bzip2::Bzip2Error,
247 'IO::Compress::Bzip2::bzip2' => \$IO::Compress::Bzip2::Bzip2Error,
248 'IO::Uncompress::Bunzip2' => \$IO::Uncompress::Bunzip2::Bunzip2Error,
249 'IO::Uncompress::Bunzip2::bunzip2' => \$IO::Uncompress::Bunzip2::Bunzip2Error,
250 'IO::Compress::Zip' => \$IO::Compress::Zip::ZipError,
251 'IO::Compress::Zip::zip' => \$IO::Compress::Zip::ZipError,
252 'IO::Uncompress::Unzip' => \$IO::Uncompress::Unzip::UnzipError,
253 'IO::Uncompress::Unzip::unzip' => \$IO::Uncompress::Unzip::UnzipError,
254 'IO::Compress::Lzop' => \$IO::Compress::Lzop::LzopError,
255 'IO::Compress::Lzop::lzop' => \$IO::Compress::Lzop::LzopError,
256 'IO::Uncompress::UnLzop' => \$IO::Uncompress::UnLzop::UnLzopError,
257 'IO::Uncompress::UnLzop::unlzop' => \$IO::Uncompress::UnLzop::UnLzopError,
258
259 'IO::Compress::DummyComp' => \$IO::Compress::DummyComp::DummyCompError,
260 'IO::Compress::DummyComp::dummycomp'=> \$IO::Compress::DummyComp::DummyCompError,
261 'IO::Uncompress::DummyUncomp' => \$IO::Uncompress::DummyUncomp::DummyUncompError,
262 'IO::Uncompress::DummyUncomp::dummyuncomp' => \$IO::Uncompress::DummyUncomp::DummyUncompError,
263 );
264
265my %TopFuncMap = ( 'IO::Compress::Gzip' => 'IO::Compress::Gzip::gzip',
266 'IO::Uncompress::Gunzip' => 'IO::Uncompress::Gunzip::gunzip',
267
268 'IO::Compress::Deflate' => 'IO::Compress::Deflate::deflate',
269 'IO::Uncompress::Inflate' => 'IO::Uncompress::Inflate::inflate',
270
271 'IO::Compress::RawDeflate' => 'IO::Compress::RawDeflate::rawdeflate',
272 'IO::Uncompress::RawInflate' => 'IO::Uncompress::RawInflate::rawinflate',
273
274 'IO::Uncompress::AnyInflate' => 'IO::Uncompress::AnyInflate::anyinflate',
275 'IO::Uncompress::AnyUncompress' => 'IO::Uncompress::AnyUncompress::anyuncompress',
276
277 'IO::Compress::Bzip2' => 'IO::Compress::Bzip2::bzip2',
278 'IO::Uncompress::Bunzip2' => 'IO::Uncompress::Bunzip2::bunzip2',
279
280 'IO::Compress::Zip' => 'IO::Compress::Zip::zip',
281 'IO::Uncompress::Unzip' => 'IO::Uncompress::Unzip::unzip',
282 'IO::Compress::Lzop' => 'IO::Compress::Lzop::lzop',
283 'IO::Uncompress::UnLzop' => 'IO::Uncompress::UnLzop::unlzop',
284 'IO::Compress::DummyComp' => 'IO::Compress::DummyComp::dummyuncomp',
285 'IO::Uncompress::DummyUncomp' => 'IO::Uncompress::DummyUncomp::dummyuncomp',
286 );
287
288 %TopFuncMap = map { ($_ => $TopFuncMap{$_},
289 $TopFuncMap{$_} => $TopFuncMap{$_}) }
290 keys %TopFuncMap ;
291
292 #%TopFuncMap = map { ($_ => \&{ $TopFuncMap{$_} ) }
293 #keys %TopFuncMap ;
294
295
296my %inverse = ( 'IO::Compress::Gzip' => 'IO::Uncompress::Gunzip',
297 'IO::Compress::Gzip::gzip' => 'IO::Uncompress::Gunzip::gunzip',
298 'IO::Compress::Deflate' => 'IO::Uncompress::Inflate',
299 'IO::Compress::Deflate::deflate' => 'IO::Uncompress::Inflate::inflate',
300 'IO::Compress::RawDeflate' => 'IO::Uncompress::RawInflate',
301 'IO::Compress::RawDeflate::rawdeflate' => 'IO::Uncompress::RawInflate::rawinflate',
302 'IO::Compress::Bzip2::bzip2' => 'IO::Uncompress::Bunzip2::bunzip2',
303 'IO::Compress::Bzip2' => 'IO::Uncompress::Bunzip2',
304 'IO::Compress::Zip::zip' => 'IO::Uncompress::Unzip::unzip',
305 'IO::Compress::Zip' => 'IO::Uncompress::Unzip',
306 'IO::Compress::Lzop::lzop' => 'IO::Uncompress::UnLzop::unlzop',
307 'IO::Compress::Lzop' => 'IO::Uncompress::UnLzop',
308 'IO::Compress::DummyComp::dummycomp' => 'IO::Uncompress::DummyUncomp::dummyuncomp',
309 'IO::Compress::DummyComp' => 'IO::Uncompress::DummyUncomp',
310 );
311
312%inverse = map { ($_ => $inverse{$_}, $inverse{$_} => $_) } keys %inverse;
313
314sub getInverse
315{
316 my $class = shift ;
317
318 return $inverse{$class} ;
319}
320
321sub getErrorRef
322{
323 my $class = shift ;
324
325 return $ErrorMap{$class} ;
326}
327
328sub getTopFuncRef
329{
330 my $class = shift ;
331
332 return \&{ $TopFuncMap{$class} } ;
333}
334
335sub getTopFuncName
336{
337 my $class = shift ;
338
339 return $TopFuncMap{$class} ;
340}
341
342sub compressBuffer
343{
344 my $compWith = shift ;
345 my $buffer = shift ;
346
347 my %mapping = ( 'IO::Uncompress::Gunzip' => 'IO::Compress::Gzip',
348 'IO::Uncompress::Gunzip::gunzip' => 'IO::Compress::Gzip',
349 'IO::Uncompress::Inflate' => 'IO::Compress::Deflate',
350 'IO::Uncompress::Inflate::inflate' => 'IO::Compress::Deflate',
351 'IO::Uncompress::RawInflate' => 'IO::Compress::RawDeflate',
352 'IO::Uncompress::RawInflate::rawinflate' => 'IO::Compress::RawDeflate',
353 'IO::Uncompress::Bunzip2' => 'IO::Compress::Bzip2',
354 'IO::Uncompress::Bunzip2::bunzip2' => 'IO::Compress::Bzip2',
355 'IO::Uncompress::Unzip' => 'IO::Compress::Zip',
356 'IO::Uncompress::Unzip::unzip' => 'IO::Compress::Zip',
357 'IO::Uncompress::UnLzop' => 'IO::Compress::Lzop',
358 'IO::Uncompress::UnLzop::unlzop' => 'IO::Compress::Lzop',
359 'IO::Uncompress::AnyInflate' => 'IO::Compress::Gzip',
360 'IO::Uncompress::AnyInflate::anyinflate' => 'IO::Compress::Gzip',
361 'IO::Uncompress::AnyUncompress' => 'IO::Compress::Gzip',
362 'IO::Uncompress::AnyUncompress::anyuncompress' => 'IO::Compress::Gzip',
363 'IO::Uncompress::DummyUncomp' => 'IO::Compress::DummyComp',
364 'IO::Uncompress::DummyUncomp::dummyuncomp'=> 'IO::Compress::DummyComp',
365 );
366
367 my $out ;
368 my $obj = $mapping{$compWith}->new( \$out);
369 $obj->write($buffer) ;
370 $obj->close();
371 return $out ;
372}
373
374our ($AnyUncompressError);
375BEGIN
376{
377 eval { require IO::Uncompress::AnyUncompress ;
378 import IO::Uncompress::AnyUncompress qw($AnyUncompressError) } ;
379}
380
381sub anyUncompress
382{
383 my $buffer = shift ;
384 my $already = shift;
385
386 my @opts = ();
387 if (ref $buffer && ref $buffer eq 'ARRAY')
388 {
389 @opts = @$buffer;
390 $buffer = shift @opts;
391 }
392
393 if (ref $buffer)
394 {
395 croak "buffer is undef" unless defined $$buffer;
396 croak "buffer is empty" unless length $$buffer;
397
398 }
399
400
401 my $data ;
402 if (IO::Compress::Base::Common::isaFilehandle($buffer))
403 {
404 $data = readFile($buffer);
405 }
406 elsif (IO::Compress::Base::Common::isaFilename($buffer))
407 {
408 $data = readFile($buffer);
409 }
410 else
411 {
412 $data = $$buffer ;
413 }
414
415 if (defined $already && length $already)
416 {
417
418 my $got = substr($data, 0, length($already));
419 substr($data, 0, length($already)) = '';
420
421 is $got, $already, ' Already OK' ;
422 }
423
424 my $out = '';
425 my $o = new IO::Uncompress::AnyUncompress \$data, -Append => 1, Transparent => 0, @opts
426 or croak "Cannot open buffer/file: $AnyUncompressError" ;
427
428 1 while $o->read($out) > 0 ;
429
430 croak "Error uncompressing -- " . $o->error()
431 if $o->error() ;
432
433 return $out ;
434
435}
436
437sub getHeaders
438{
439 my $buffer = shift ;
440 my $already = shift;
441
442 my @opts = ();
443 if (ref $buffer && ref $buffer eq 'ARRAY')
444 {
445 @opts = @$buffer;
446 $buffer = shift @opts;
447 }
448
449 if (ref $buffer)
450 {
451 croak "buffer is undef" unless defined $$buffer;
452 croak "buffer is empty" unless length $$buffer;
453
454 }
455
456
457 my $data ;
458 if (IO::Compress::Base::Common::isaFilehandle($buffer))
459 {
460 $data = readFile($buffer);
461 }
462 elsif (IO::Compress::Base::Common::isaFilename($buffer))
463 {
464 $data = readFile($buffer);
465 }
466 else
467 {
468 $data = $$buffer ;
469 }
470
471 if (defined $already && length $already)
472 {
473
474 my $got = substr($data, 0, length($already));
475 substr($data, 0, length($already)) = '';
476
477 is $got, $already, ' Already OK' ;
478 }
479
480 my $out = '';
481 my $o = new IO::Uncompress::AnyUncompress \$data, MultiStream => 1, -Append => 1, Transparent => 0, @opts
482 or croak "Cannot open buffer/file: $AnyUncompressError" ;
483
484 1 while $o->read($out) > 0 ;
485
486 croak "Error uncompressing -- " . $o->error()
487 if $o->error() ;
488
489 return ($o->getHeaderInfo()) ;
490
491}
492
493sub mkComplete
494{
495 my $class = shift ;
496 my $data = shift;
497 my $Error = getErrorRef($class);
498
499 my $buffer ;
500 my %params = ();
501
502 if ($class eq 'IO::Compress::Gzip') {
503 %params = (
c70c1701 504 Name => "My name",
505 Comment => "a comment",
506 ExtraField => ['ab' => "extra"],
507 HeaderCRC => 1);
25f0751f 508 }
509 elsif ($class eq 'IO::Compress::Zip'){
510 %params = (
c70c1701 511 Name => "My name",
512 Comment => "a comment",
513 ZipComment => "last comment",
514 exTime => [100, 200, 300],
515 ExtraFieldLocal => ["ab" => "extra1"],
516 ExtraFieldCentral => ["cd" => "extra2"],
25f0751f 517 );
518 }
519
520 my $z = new $class( \$buffer, %params)
521 or croak "Cannot create $class object: $$Error";
522 $z->write($data);
523 $z->close();
524
525 my $unc = getInverse($class);
526 anyUncompress(\$buffer) eq $data
527 or die "bad bad bad";
528 my $u = new $unc( \$buffer);
529 my $info = $u->getHeaderInfo() ;
530
531
532 return wantarray ? ($info, $buffer) : $buffer ;
533}
534
535sub mkErr
536{
537 my $string = shift ;
538 my ($dummy, $file, $line) = caller ;
539 -- $line ;
540
541 $file = quotemeta($file);
542
543 return "/$string\\s+at $file line $line/" if $] >= 5.006 ;
544 return "/$string\\s+at /" ;
545}
546
547sub mkEvalErr
548{
549 my $string = shift ;
550
551 return "/$string\\s+at \\(eval /" if $] > 5.006 ;
552 return "/$string\\s+at /" ;
553}
554
555sub dumpObj
556{
557 my $obj = shift ;
558
559 my ($dummy, $file, $line) = caller ;
560
561 if (@_)
562 {
563 print "#\n# dumpOBJ from $file line $line @_\n" ;
564 }
565 else
566 {
567 print "#\n# dumpOBJ from $file line $line \n" ;
568 }
569
570 my $max = 0 ;;
571 foreach my $k (keys %{ *$obj })
572 {
573 $max = length $k if length $k > $max ;
574 }
575
576 foreach my $k (sort keys %{ *$obj })
577 {
578 my $v = $obj->{$k} ;
579 $v = '-undef-' unless defined $v;
580 my $pad = ' ' x ($max - length($k) + 2) ;
581 print "# $k$pad: [$v]\n";
582 }
583 print "#\n" ;
584}
585
586
587package CompTestUtils;
588
5891;