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