xref: /openbsd-src/gnu/usr.bin/perl/cpan/Compress-Raw-Zlib/t/compress/CompTestUtils.pm (revision fdcd7346e28d9d56be488c988b2929cfac6342e0)
1b39c5158Smillertpackage CompTestUtils;
2b39c5158Smillert
3b39c5158Smillertpackage main ;
4b39c5158Smillert
5b39c5158Smillertuse strict ;
6b39c5158Smillertuse warnings;
7b39c5158Smillertuse bytes;
8b39c5158Smillert
9b39c5158Smillert#use lib qw(t t/compress);
10b39c5158Smillert
11b39c5158Smillertuse Carp ;
12b39c5158Smillert#use Test::More ;
13b39c5158Smillert
14b39c5158Smillert
15b39c5158Smillert
16b39c5158Smillertsub title
17b39c5158Smillert{
18b39c5158Smillert    #diag "" ;
19b39c5158Smillert    ok(1, $_[0]) ;
20b39c5158Smillert    #diag "" ;
21b39c5158Smillert}
22b39c5158Smillert
23b39c5158Smillertsub like_eval
24b39c5158Smillert{
25b39c5158Smillert    like $@, @_ ;
26b39c5158Smillert}
27b39c5158Smillert
28898184e3SsthenBEGIN {
29898184e3Ssthen    eval {
30898184e3Ssthen       require File::Temp;
31898184e3Ssthen     } ;
32898184e3Ssthen
33898184e3Ssthen}
34898184e3Ssthen
35*fdcd7346Safresh1sub test_zlib_header_matches_library
36*fdcd7346Safresh1{
37*fdcd7346Safresh1SKIP: {
38*fdcd7346Safresh1    skip "TEST_SKIP_VERSION_CHECK is set", 1
39*fdcd7346Safresh1        if $ENV{TEST_SKIP_VERSION_CHECK};
40*fdcd7346Safresh1
41*fdcd7346Safresh1    if (Compress::Raw::Zlib::is_zlibng_native())
42*fdcd7346Safresh1    {
43*fdcd7346Safresh1        my $zlibng_h = Compress::Raw::Zlib::ZLIBNG_VERSION ;
44*fdcd7346Safresh1        my $libzng   = Compress::Raw::Zlib::zlibng_version();
45*fdcd7346Safresh1        is($zlibng_h, $libzng, "ZLIBNG_VERSION ($zlibng_h) matches Compress::Raw::Zlib::zlibng_version")
46*fdcd7346Safresh1            or diag <<EOM;
47*fdcd7346Safresh1
48*fdcd7346Safresh1The version of zlib-ng.h does not match the version of libz-ng
49*fdcd7346Safresh1
50*fdcd7346Safresh1You have zlib-ng.h version $zlibng_h
51*fdcd7346Safresh1     and libz-ng   version $libzng
52*fdcd7346Safresh1
53*fdcd7346Safresh1You probably have two versions of zlib-ng installed on your system.
54*fdcd7346Safresh1Try removing the one you don't want to use and rebuild.
55*fdcd7346Safresh1EOM
56*fdcd7346Safresh1    }
57*fdcd7346Safresh1    else
58*fdcd7346Safresh1    {
59*fdcd7346Safresh1        my $zlib_h = ZLIB_VERSION ;
60*fdcd7346Safresh1        my $libz   = Compress::Raw::Zlib::zlib_version();
61*fdcd7346Safresh1        is($zlib_h, $libz, "ZLIB_VERSION ($zlib_h) matches Compress::Raw::Zlib::zlib_version")
62*fdcd7346Safresh1            or diag <<EOM;
63*fdcd7346Safresh1
64*fdcd7346Safresh1The version of zlib.h does not match the version of libz
65*fdcd7346Safresh1
66*fdcd7346Safresh1You have zlib.h version $zlib_h
67*fdcd7346Safresh1     and libz   version $libz
68*fdcd7346Safresh1
69*fdcd7346Safresh1You probably have two versions of zlib installed on your system.
70*fdcd7346Safresh1Try removing the one you don't want to use and rebuild.
71*fdcd7346Safresh1EOM
72*fdcd7346Safresh1    }
73*fdcd7346Safresh1    }
74*fdcd7346Safresh1}
75*fdcd7346Safresh1
76898184e3Ssthen
77b39c5158Smillert{
78b39c5158Smillert    package LexFile ;
79b39c5158Smillert
80b39c5158Smillert    our ($index);
81b39c5158Smillert    $index = '00000';
82b39c5158Smillert
83b39c5158Smillert    sub new
84b39c5158Smillert    {
85b39c5158Smillert        my $self = shift ;
86b39c5158Smillert        foreach (@_)
87b39c5158Smillert        {
88898184e3Ssthen            Carp::croak "NO!!!!" if defined $_;
89898184e3Ssthen            # autogenerate the name if none supplied
90898184e3Ssthen            $_ = "tst" . $$ . "X" . $index ++ . ".tmp"
91b39c5158Smillert                unless defined $_;
92b39c5158Smillert        }
93b39c5158Smillert        chmod 0777, @_;
94b39c5158Smillert        for (@_) { 1 while unlink $_ } ;
95b39c5158Smillert        bless [ @_ ], $self ;
96b39c5158Smillert    }
97b39c5158Smillert
98b39c5158Smillert    sub DESTROY
99b39c5158Smillert    {
100b39c5158Smillert        my $self = shift ;
101b39c5158Smillert        chmod 0777, @{ $self } ;
102b39c5158Smillert        for (@$self) { 1 while unlink $_ } ;
103b39c5158Smillert    }
104b39c5158Smillert
105b39c5158Smillert}
106b39c5158Smillert
107b39c5158Smillert{
108b39c5158Smillert    package LexDir ;
109b39c5158Smillert
110b39c5158Smillert    use File::Path;
111898184e3Ssthen
112898184e3Ssthen    our ($index);
113898184e3Ssthen    $index = '00000';
1145759b3d2Safresh1    our ($useTempFile);
1155759b3d2Safresh1    our ($useTempDir);
116898184e3Ssthen
117b39c5158Smillert    sub new
118b39c5158Smillert    {
119b39c5158Smillert        my $self = shift ;
120898184e3Ssthen
121898184e3Ssthen        if ( $useTempDir)
122898184e3Ssthen        {
123898184e3Ssthen            foreach (@_)
124898184e3Ssthen            {
125898184e3Ssthen                Carp::croak "NO!!!!" if defined $_;
126898184e3Ssthen                $_ = File::Temp->newdir(DIR => '.');
127898184e3Ssthen                # Subsequent manipulations assume Unix syntax, metacharacters, etc.
128898184e3Ssthen                if ($^O eq 'VMS')
129898184e3Ssthen                {
130898184e3Ssthen                    $_->{DIRNAME} = VMS::Filespec::unixify($_->{DIRNAME});
131898184e3Ssthen                    $_->{DIRNAME} =~ s/\/$//;
132898184e3Ssthen                }
133898184e3Ssthen            }
134898184e3Ssthen            bless [ @_ ], $self ;
135898184e3Ssthen        }
136898184e3Ssthen        elsif ( $useTempFile)
137898184e3Ssthen        {
138898184e3Ssthen            foreach (@_)
139898184e3Ssthen            {
140898184e3Ssthen                Carp::croak "NO!!!!" if defined $_;
141898184e3Ssthen                $_ = File::Temp::tempdir(DIR => '.', CLEANUP => 1);
142898184e3Ssthen                # Subsequent manipulations assume Unix syntax, metacharacters, etc.
143898184e3Ssthen                if ($^O eq 'VMS')
144898184e3Ssthen                {
145898184e3Ssthen                    $_ = VMS::Filespec::unixify($_);
146898184e3Ssthen                    $_ =~ s/\/$//;
147898184e3Ssthen                }
148898184e3Ssthen            }
149898184e3Ssthen            bless [ @_ ], $self ;
150898184e3Ssthen        }
151898184e3Ssthen        else
152898184e3Ssthen        {
153898184e3Ssthen            foreach (@_)
154898184e3Ssthen            {
155898184e3Ssthen                Carp::croak "NO!!!!" if defined $_;
156898184e3Ssthen                # autogenerate the name if none supplied
157898184e3Ssthen                $_ = "tmpdir" . $$ . "X" . $index ++ . ".tmp" ;
158898184e3Ssthen            }
1595759b3d2Safresh1            foreach (@_)
1605759b3d2Safresh1            {
1615759b3d2Safresh1                rmtree $_, {verbose => 0, safe => 1}
1625759b3d2Safresh1                    if -d $_;
1635759b3d2Safresh1                mkdir $_, 0777
1645759b3d2Safresh1            }
165b39c5158Smillert            bless [ @_ ], $self ;
166b39c5158Smillert        }
167b39c5158Smillert
168898184e3Ssthen    }
169898184e3Ssthen
170b39c5158Smillert    sub DESTROY
171b39c5158Smillert    {
172898184e3Ssthen        if (! $useTempFile)
173898184e3Ssthen        {
174b39c5158Smillert            my $self = shift ;
1755759b3d2Safresh1            foreach (@$self)
1765759b3d2Safresh1            {
1775759b3d2Safresh1                rmtree $_, {verbose => 0, safe => 1}
1785759b3d2Safresh1                    if -d $_ ;
1795759b3d2Safresh1            }
180b39c5158Smillert        }
181b39c5158Smillert    }
182898184e3Ssthen}
183898184e3Ssthen
184b39c5158Smillertsub readFile
185b39c5158Smillert{
186b39c5158Smillert    my $f = shift ;
187b39c5158Smillert
188b39c5158Smillert    my @strings ;
189b39c5158Smillert
190b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($f))
191b39c5158Smillert    {
192b39c5158Smillert        my $pos = tell($f);
193b39c5158Smillert        seek($f, 0,0);
194b39c5158Smillert        @strings = <$f> ;
195b39c5158Smillert        seek($f, 0, $pos);
196b39c5158Smillert    }
197b39c5158Smillert    else
198b39c5158Smillert    {
199b39c5158Smillert        open (F, "<$f")
200b39c5158Smillert            or croak "Cannot open $f: $!\n" ;
201b39c5158Smillert        binmode F;
202b39c5158Smillert        @strings = <F> ;
203b39c5158Smillert        close F ;
204b39c5158Smillert    }
205b39c5158Smillert
206b39c5158Smillert    return @strings if wantarray ;
207b39c5158Smillert    return join "", @strings ;
208b39c5158Smillert}
209b39c5158Smillert
210b39c5158Smillertsub touch
211b39c5158Smillert{
212b39c5158Smillert    foreach (@_) { writeFile($_, '') }
213b39c5158Smillert}
214b39c5158Smillert
215b39c5158Smillertsub writeFile
216b39c5158Smillert{
217b39c5158Smillert    my($filename, @strings) = @_ ;
218b39c5158Smillert    1 while unlink $filename ;
219b39c5158Smillert    open (F, ">$filename")
220b39c5158Smillert        or croak "Cannot open $filename: $!\n" ;
221b39c5158Smillert    binmode F;
222b39c5158Smillert    foreach (@strings) {
223b39c5158Smillert        no warnings ;
224b39c5158Smillert        print F $_ ;
225b39c5158Smillert    }
226b39c5158Smillert    close F ;
227b39c5158Smillert}
228b39c5158Smillert
229b39c5158Smillertsub GZreadFile
230b39c5158Smillert{
231b39c5158Smillert    my ($filename) = shift ;
232b39c5158Smillert
233b39c5158Smillert    my ($uncomp) = "" ;
234b39c5158Smillert    my $line = "" ;
235b39c5158Smillert    my $fil = gzopen($filename, "rb")
236b39c5158Smillert        or croak "Cannopt open '$filename': $Compress::Zlib::gzerrno" ;
237b39c5158Smillert
238b39c5158Smillert    $uncomp .= $line
239b39c5158Smillert        while $fil->gzread($line) > 0;
240b39c5158Smillert
241b39c5158Smillert    $fil->gzclose ;
242b39c5158Smillert    return $uncomp ;
243b39c5158Smillert}
244b39c5158Smillert
245b39c5158Smillertsub hexDump
246b39c5158Smillert{
247b39c5158Smillert    my $d = shift ;
248b39c5158Smillert
249b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($d))
250b39c5158Smillert    {
251b39c5158Smillert        $d = readFile($d);
252b39c5158Smillert    }
253b39c5158Smillert    elsif (IO::Compress::Base::Common::isaFilename($d))
254b39c5158Smillert    {
255b39c5158Smillert        $d = readFile($d);
256b39c5158Smillert    }
257b39c5158Smillert    else
258b39c5158Smillert    {
259b39c5158Smillert        $d = $$d ;
260b39c5158Smillert    }
261b39c5158Smillert
262b39c5158Smillert    my $offset = 0 ;
263b39c5158Smillert
264b39c5158Smillert    $d = '' unless defined $d ;
265b39c5158Smillert    #while (read(STDIN, $data, 16)) {
266b39c5158Smillert    while (my $data = substr($d, 0, 16)) {
267b39c5158Smillert        substr($d, 0, 16) = '' ;
268b39c5158Smillert        printf "# %8.8lx    ", $offset;
269b39c5158Smillert        $offset += 16;
270b39c5158Smillert
271b39c5158Smillert        my @array = unpack('C*', $data);
272b39c5158Smillert        foreach (@array) {
273b39c5158Smillert            printf('%2.2x ', $_);
274b39c5158Smillert        }
275b39c5158Smillert        print "   " x (16 - @array)
276b39c5158Smillert            if @array < 16 ;
277b39c5158Smillert        $data =~ tr/\0-\37\177-\377/./;
278b39c5158Smillert        print "  $data\n";
279b39c5158Smillert    }
280b39c5158Smillert
281b39c5158Smillert}
282b39c5158Smillert
283b39c5158Smillertsub readHeaderInfo
284b39c5158Smillert{
285b39c5158Smillert    my $name = shift ;
286b39c5158Smillert    my %opts = @_ ;
287b39c5158Smillert
288b39c5158Smillert    my $string = <<EOM;
289b39c5158Smillertsome text
290b39c5158SmillertEOM
291b39c5158Smillert
292b39c5158Smillert    ok my $x = new IO::Compress::Gzip $name, %opts
293b39c5158Smillert        or diag "GzipError is $IO::Compress::Gzip::GzipError" ;
294b39c5158Smillert    ok $x->write($string) ;
295b39c5158Smillert    ok $x->close ;
296b39c5158Smillert
297b39c5158Smillert    #is GZreadFile($name), $string ;
298b39c5158Smillert
299b39c5158Smillert    ok my $gunz = new IO::Uncompress::Gunzip $name, Strict => 0
300b39c5158Smillert        or diag "GunzipError is $IO::Uncompress::Gunzip::GunzipError" ;
301b39c5158Smillert    ok my $hdr = $gunz->getHeaderInfo();
302b39c5158Smillert    my $uncomp ;
303b39c5158Smillert    ok $gunz->read($uncomp) ;
304b39c5158Smillert    ok $uncomp eq $string;
305b39c5158Smillert    ok $gunz->close ;
306b39c5158Smillert
307b39c5158Smillert    return $hdr ;
308b39c5158Smillert}
309b39c5158Smillert
310b39c5158Smillertsub cmpFile
311b39c5158Smillert{
312b39c5158Smillert    my ($filename, $uue) = @_ ;
313b39c5158Smillert    return readFile($filename) eq unpack("u", $uue) ;
314b39c5158Smillert}
315b39c5158Smillert
316b39c5158Smillert#sub isRawFormat
317b39c5158Smillert#{
318b39c5158Smillert#    my $class = shift;
319b39c5158Smillert#    # TODO -- add Lzma here?
320b39c5158Smillert#    my %raw = map { $_ => 1 } qw( RawDeflate );
321b39c5158Smillert#
322b39c5158Smillert#    return defined $raw{$class};
323b39c5158Smillert#}
324b39c5158Smillert
325b39c5158Smillert
326b39c5158Smillert
327b39c5158Smillertmy %TOP = (
328b39c5158Smillert    'IO::Uncompress::AnyInflate' => { Inverse  => 'IO::Compress::Gzip',
329b39c5158Smillert                                      Error    => 'AnyInflateError',
330b39c5158Smillert                                      TopLevel => 'anyinflate',
331b39c5158Smillert                                      Raw      => 0,
332b39c5158Smillert                            },
333b39c5158Smillert
334b39c5158Smillert    'IO::Uncompress::AnyUncompress' => { Inverse  => 'IO::Compress::Gzip',
335b39c5158Smillert                                         Error    => 'AnyUncompressError',
336b39c5158Smillert                                         TopLevel => 'anyuncompress',
337b39c5158Smillert                                         Raw      => 0,
338b39c5158Smillert                            },
339b39c5158Smillert
340b39c5158Smillert    'IO::Compress::Gzip' => { Inverse  => 'IO::Uncompress::Gunzip',
341b39c5158Smillert                              Error    => 'GzipError',
342b39c5158Smillert                              TopLevel => 'gzip',
343b39c5158Smillert                              Raw      => 0,
344b39c5158Smillert                            },
345b39c5158Smillert    'IO::Uncompress::Gunzip' => { Inverse  => 'IO::Compress::Gzip',
346b39c5158Smillert                                  Error    => 'GunzipError',
347b39c5158Smillert                                  TopLevel => 'gunzip',
348b39c5158Smillert                                  Raw      => 0,
349b39c5158Smillert                            },
350b39c5158Smillert
351b39c5158Smillert    'IO::Compress::Deflate' => { Inverse  => 'IO::Uncompress::Inflate',
352b39c5158Smillert                                 Error    => 'DeflateError',
353b39c5158Smillert                                 TopLevel => 'deflate',
354b39c5158Smillert                                 Raw      => 0,
355b39c5158Smillert                            },
356b39c5158Smillert    'IO::Uncompress::Inflate' => { Inverse  => 'IO::Compress::Deflate',
357b39c5158Smillert                                   Error    => 'InflateError',
358b39c5158Smillert                                   TopLevel => 'inflate',
359b39c5158Smillert                                   Raw      => 0,
360b39c5158Smillert                            },
361b39c5158Smillert
362b39c5158Smillert    'IO::Compress::RawDeflate' => { Inverse  => 'IO::Uncompress::RawInflate',
363b39c5158Smillert                                    Error    => 'RawDeflateError',
364b39c5158Smillert                                    TopLevel => 'rawdeflate',
365b39c5158Smillert                                    Raw      => 1,
366b39c5158Smillert                            },
367b39c5158Smillert    'IO::Uncompress::RawInflate' => { Inverse  => 'IO::Compress::RawDeflate',
368b39c5158Smillert                                      Error    => 'RawInflateError',
369b39c5158Smillert                                      TopLevel => 'rawinflate',
370b39c5158Smillert                                      Raw      => 1,
371b39c5158Smillert                            },
372b39c5158Smillert
373b39c5158Smillert    'IO::Compress::Zip' => { Inverse  => 'IO::Uncompress::Unzip',
374b39c5158Smillert                             Error    => 'ZipError',
375b39c5158Smillert                             TopLevel => 'zip',
376b39c5158Smillert                             Raw      => 0,
377b39c5158Smillert                            },
378b39c5158Smillert    'IO::Uncompress::Unzip' => { Inverse  => 'IO::Compress::Zip',
379b39c5158Smillert                                 Error    => 'UnzipError',
380b39c5158Smillert                                 TopLevel => 'unzip',
381b39c5158Smillert                                 Raw      => 0,
382b39c5158Smillert                            },
383b39c5158Smillert
384b39c5158Smillert    'IO::Compress::Bzip2' => { Inverse  => 'IO::Uncompress::Bunzip2',
385b39c5158Smillert                               Error    => 'Bzip2Error',
386b39c5158Smillert                               TopLevel => 'bzip2',
387b39c5158Smillert                               Raw      => 0,
388b39c5158Smillert                            },
389b39c5158Smillert    'IO::Uncompress::Bunzip2' => { Inverse  => 'IO::Compress::Bzip2',
390b39c5158Smillert                                   Error    => 'Bunzip2Error',
391b39c5158Smillert                                   TopLevel => 'bunzip2',
392b39c5158Smillert                                   Raw      => 0,
393b39c5158Smillert                            },
394b39c5158Smillert
395b39c5158Smillert    'IO::Compress::Lzop' => { Inverse  => 'IO::Uncompress::UnLzop',
396b39c5158Smillert                              Error    => 'LzopError',
397b39c5158Smillert                              TopLevel => 'lzop',
398b39c5158Smillert                              Raw      => 0,
399b39c5158Smillert                            },
400b39c5158Smillert    'IO::Uncompress::UnLzop' => { Inverse  => 'IO::Compress::Lzop',
401b39c5158Smillert                                  Error    => 'UnLzopError',
402b39c5158Smillert                                  TopLevel => 'unlzop',
403b39c5158Smillert                                  Raw      => 0,
404b39c5158Smillert                            },
405b39c5158Smillert
406b39c5158Smillert    'IO::Compress::Lzf' => { Inverse  => 'IO::Uncompress::UnLzf',
407b39c5158Smillert                             Error    => 'LzfError',
408b39c5158Smillert                             TopLevel => 'lzf',
409b39c5158Smillert                             Raw      => 0,
410b39c5158Smillert                            },
411b39c5158Smillert    'IO::Uncompress::UnLzf' => { Inverse  => 'IO::Compress::Lzf',
412b39c5158Smillert                                 Error    => 'UnLzfError',
413b39c5158Smillert                                 TopLevel => 'unlzf',
414b39c5158Smillert                                 Raw      => 0,
415b39c5158Smillert                            },
416b39c5158Smillert
417b39c5158Smillert    'IO::Compress::Lzma' => { Inverse  => 'IO::Uncompress::UnLzma',
418b39c5158Smillert                              Error    => 'LzmaError',
419b39c5158Smillert                              TopLevel => 'lzma',
420b39c5158Smillert                              Raw      => 1,
421b39c5158Smillert                            },
422b39c5158Smillert    'IO::Uncompress::UnLzma' => { Inverse  => 'IO::Compress::Lzma',
423b39c5158Smillert                                  Error    => 'UnLzmaError',
424b39c5158Smillert                                  TopLevel => 'unlzma',
425b39c5158Smillert                                  Raw      => 1,
426b39c5158Smillert                                },
427b39c5158Smillert
428b39c5158Smillert    'IO::Compress::Xz' => { Inverse  => 'IO::Uncompress::UnXz',
429b39c5158Smillert                            Error    => 'XzError',
430b39c5158Smillert                            TopLevel => 'xz',
431b39c5158Smillert                            Raw      => 0,
432b39c5158Smillert                          },
433b39c5158Smillert    'IO::Uncompress::UnXz' => { Inverse  => 'IO::Compress::Xz',
434b39c5158Smillert                                Error    => 'UnXzError',
435b39c5158Smillert                                TopLevel => 'unxz',
436b39c5158Smillert                                Raw      => 0,
437b39c5158Smillert                              },
438b39c5158Smillert
439f3efcd01Safresh1    'IO::Compress::Lzip' => { Inverse  => 'IO::Uncompress::UnLzip',
440f3efcd01Safresh1                            Error    => 'LzipError',
441f3efcd01Safresh1                            TopLevel => 'lzip',
442f3efcd01Safresh1                            Raw      => 0,
443f3efcd01Safresh1                          },
444f3efcd01Safresh1    'IO::Uncompress::UnLzip' => { Inverse  => 'IO::Compress::Lzip',
445f3efcd01Safresh1                                Error    => 'UnLzipError',
446f3efcd01Safresh1                                TopLevel => 'unlzip',
447f3efcd01Safresh1                                Raw      => 0,
448f3efcd01Safresh1                              },
449f3efcd01Safresh1
450b39c5158Smillert    'IO::Compress::PPMd' => { Inverse  => 'IO::Uncompress::UnPPMd',
451b39c5158Smillert                              Error    => 'PPMdError',
452b39c5158Smillert                              TopLevel => 'ppmd',
453b39c5158Smillert                              Raw      => 0,
454b39c5158Smillert                            },
455b39c5158Smillert    'IO::Uncompress::UnPPMd' => { Inverse  => 'IO::Compress::PPMd',
456b39c5158Smillert                                  Error    => 'UnPPMdError',
457b39c5158Smillert                                  TopLevel => 'unppmd',
458b39c5158Smillert                                  Raw      => 0,
459b39c5158Smillert                                },
460f3efcd01Safresh1    'IO::Compress::Zstd' => { Inverse  => 'IO::Uncompress::UnZstd',
461f3efcd01Safresh1                              Error    => 'ZstdError',
462f3efcd01Safresh1                              TopLevel => 'zstd',
463f3efcd01Safresh1                              Raw      => 0,
464f3efcd01Safresh1                            },
465f3efcd01Safresh1    'IO::Uncompress::UnZstd' => { Inverse  => 'IO::Compress::Zstd',
466f3efcd01Safresh1                                  Error    => 'UnZstdError',
467f3efcd01Safresh1                                  TopLevel => 'unzstd',
468f3efcd01Safresh1                                  Raw      => 0,
469f3efcd01Safresh1                                },
470b39c5158Smillert
471b39c5158Smillert    'IO::Compress::DummyComp' => { Inverse  => 'IO::Uncompress::DummyUnComp',
472b39c5158Smillert                                   Error    => 'DummyCompError',
473b39c5158Smillert                                   TopLevel => 'dummycomp',
474b39c5158Smillert                                   Raw      => 0,
475b39c5158Smillert                                 },
476b39c5158Smillert    'IO::Uncompress::DummyUnComp' => { Inverse  => 'IO::Compress::DummyComp',
477b39c5158Smillert                                       Error    => 'DummyUnCompError',
478b39c5158Smillert                                       TopLevel => 'dummyunComp',
479b39c5158Smillert                                       Raw      => 0,
480b39c5158Smillert                                     },
481b39c5158Smillert);
482b39c5158Smillert
483b39c5158Smillert
484b39c5158Smillertfor my $key (keys %TOP)
485b39c5158Smillert{
486b39c5158Smillert    no strict;
487b39c5158Smillert    no warnings;
488b39c5158Smillert    $TOP{$key}{Error}    = \${ $key . '::' . $TOP{$key}{Error}    };
489b39c5158Smillert    $TOP{$key}{TopLevel} =     $key . '::' . $TOP{$key}{TopLevel}  ;
490b39c5158Smillert
491b39c5158Smillert    # Silence used once warning in really old perl
492b39c5158Smillert    my $dummy            = \${ $key . '::' . $TOP{$key}{Error}    };
493b39c5158Smillert
494b39c5158Smillert    #$TOP{$key . "::" . $TOP{$key}{TopLevel} } = $TOP{$key};
495b39c5158Smillert}
496b39c5158Smillert
497b39c5158Smillertsub uncompressBuffer
498b39c5158Smillert{
499b39c5158Smillert    my $compWith = shift ;
500b39c5158Smillert    my $buffer = shift ;
501b39c5158Smillert
502b39c5158Smillert
503b39c5158Smillert    my $out ;
504b39c5158Smillert    my $obj = $TOP{$compWith}{Inverse}->new( \$buffer, -Append => 1);
505b39c5158Smillert    1 while $obj->read($out) > 0 ;
506b39c5158Smillert    return $out ;
507b39c5158Smillert
508b39c5158Smillert}
509b39c5158Smillert
510b39c5158Smillert
511b39c5158Smillertsub getInverse
512b39c5158Smillert{
513b39c5158Smillert    my $class = shift ;
514b39c5158Smillert
515b39c5158Smillert    return $TOP{$class}{Inverse};
516b39c5158Smillert}
517b39c5158Smillert
518b39c5158Smillertsub getErrorRef
519b39c5158Smillert{
520b39c5158Smillert    my $class = shift ;
521b39c5158Smillert
522b39c5158Smillert    return $TOP{$class}{Error};
523b39c5158Smillert}
524b39c5158Smillert
525b39c5158Smillertsub getTopFuncRef
526b39c5158Smillert{
527b39c5158Smillert    my $class = shift ;
528b39c5158Smillert
529b39c5158Smillert    die "Cannot find $class"
530b39c5158Smillert        if ! defined $TOP{$class}{TopLevel};
531b39c5158Smillert    return \&{ $TOP{$class}{TopLevel} } ;
532b39c5158Smillert}
533b39c5158Smillert
534b39c5158Smillertsub getTopFuncName
535b39c5158Smillert{
536b39c5158Smillert    my $class = shift ;
537b39c5158Smillert
538b39c5158Smillert    return $TOP{$class}{TopLevel} ;
539b39c5158Smillert}
540b39c5158Smillert
541b39c5158Smillertsub compressBuffer
542b39c5158Smillert{
543b39c5158Smillert    my $compWith = shift ;
544b39c5158Smillert    my $buffer = shift ;
545b39c5158Smillert
546b39c5158Smillert
547b39c5158Smillert    my $out ;
548b39c5158Smillert    die "Cannot find $compWith"
549b39c5158Smillert        if ! defined $TOP{$compWith}{Inverse};
550b39c5158Smillert    my $obj = $TOP{$compWith}{Inverse}->new( \$out);
551b39c5158Smillert    $obj->write($buffer) ;
552b39c5158Smillert    $obj->close();
553b39c5158Smillert    return $out ;
554b39c5158Smillert}
555b39c5158Smillert
556b39c5158Smillertour ($AnyUncompressError);
557b39c5158SmillertBEGIN
558b39c5158Smillert{
559f3efcd01Safresh1    eval ' use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError); ';
560b39c5158Smillert}
561b39c5158Smillert
562b39c5158Smillertsub anyUncompress
563b39c5158Smillert{
564b39c5158Smillert    my $buffer = shift ;
565b39c5158Smillert    my $already = shift;
566b39c5158Smillert
567b39c5158Smillert    my @opts = ();
568b39c5158Smillert    if (ref $buffer && ref $buffer eq 'ARRAY')
569b39c5158Smillert    {
570b39c5158Smillert        @opts = @$buffer;
571b39c5158Smillert        $buffer = shift @opts;
572b39c5158Smillert    }
573b39c5158Smillert
574b39c5158Smillert    if (ref $buffer)
575b39c5158Smillert    {
576b39c5158Smillert        croak "buffer is undef" unless defined $$buffer;
577b39c5158Smillert        croak "buffer is empty" unless length $$buffer;
578b39c5158Smillert
579b39c5158Smillert    }
580b39c5158Smillert
581b39c5158Smillert
582b39c5158Smillert    my $data ;
583b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($buffer))
584b39c5158Smillert    {
585b39c5158Smillert        $data = readFile($buffer);
586b39c5158Smillert    }
587b39c5158Smillert    elsif (IO::Compress::Base::Common::isaFilename($buffer))
588b39c5158Smillert    {
589b39c5158Smillert        $data = readFile($buffer);
590b39c5158Smillert    }
591b39c5158Smillert    else
592b39c5158Smillert    {
593b39c5158Smillert        $data = $$buffer ;
594b39c5158Smillert    }
595b39c5158Smillert
596b39c5158Smillert    if (defined $already && length $already)
597b39c5158Smillert    {
598b39c5158Smillert
599b39c5158Smillert        my $got = substr($data, 0, length($already));
600b39c5158Smillert        substr($data, 0, length($already)) = '';
601b39c5158Smillert
602b39c5158Smillert        is $got, $already, '  Already OK' ;
603b39c5158Smillert    }
604b39c5158Smillert
605b39c5158Smillert    my $out = '';
606b39c5158Smillert    my $o = new IO::Uncompress::AnyUncompress \$data,
607b39c5158Smillert                    Append => 1,
608b39c5158Smillert                    Transparent => 0,
609b39c5158Smillert                    RawInflate => 1,
610b39c5158Smillert                    UnLzma     => 1,
611b39c5158Smillert                    @opts
612b39c5158Smillert        or croak "Cannot open buffer/file: $AnyUncompressError" ;
613b39c5158Smillert
614b39c5158Smillert    1 while $o->read($out) > 0 ;
615b39c5158Smillert
616b39c5158Smillert    croak "Error uncompressing -- " . $o->error()
617b39c5158Smillert        if $o->error() ;
618b39c5158Smillert
619b39c5158Smillert    return $out ;
620b39c5158Smillert}
621b39c5158Smillert
622b39c5158Smillertsub getHeaders
623b39c5158Smillert{
624b39c5158Smillert    my $buffer = shift ;
625b39c5158Smillert    my $already = shift;
626b39c5158Smillert
627b39c5158Smillert    my @opts = ();
628b39c5158Smillert    if (ref $buffer && ref $buffer eq 'ARRAY')
629b39c5158Smillert    {
630b39c5158Smillert        @opts = @$buffer;
631b39c5158Smillert        $buffer = shift @opts;
632b39c5158Smillert    }
633b39c5158Smillert
634b39c5158Smillert    if (ref $buffer)
635b39c5158Smillert    {
636b39c5158Smillert        croak "buffer is undef" unless defined $$buffer;
637b39c5158Smillert        croak "buffer is empty" unless length $$buffer;
638b39c5158Smillert
639b39c5158Smillert    }
640b39c5158Smillert
641b39c5158Smillert
642b39c5158Smillert    my $data ;
643b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($buffer))
644b39c5158Smillert    {
645b39c5158Smillert        $data = readFile($buffer);
646b39c5158Smillert    }
647b39c5158Smillert    elsif (IO::Compress::Base::Common::isaFilename($buffer))
648b39c5158Smillert    {
649b39c5158Smillert        $data = readFile($buffer);
650b39c5158Smillert    }
651b39c5158Smillert    else
652b39c5158Smillert    {
653b39c5158Smillert        $data = $$buffer ;
654b39c5158Smillert    }
655b39c5158Smillert
656b39c5158Smillert    if (defined $already && length $already)
657b39c5158Smillert    {
658b39c5158Smillert
659b39c5158Smillert        my $got = substr($data, 0, length($already));
660b39c5158Smillert        substr($data, 0, length($already)) = '';
661b39c5158Smillert
662b39c5158Smillert        is $got, $already, '  Already OK' ;
663b39c5158Smillert    }
664b39c5158Smillert
665b39c5158Smillert    my $out = '';
666b39c5158Smillert    my $o = new IO::Uncompress::AnyUncompress \$data,
667b39c5158Smillert                MultiStream => 1,
668b39c5158Smillert                Append => 1,
669b39c5158Smillert                Transparent => 0,
670b39c5158Smillert                RawInflate => 1,
671b39c5158Smillert                UnLzma     => 1,
672b39c5158Smillert                @opts
673b39c5158Smillert        or croak "Cannot open buffer/file: $AnyUncompressError" ;
674b39c5158Smillert
675b39c5158Smillert    1 while $o->read($out) > 0 ;
676b39c5158Smillert
677b39c5158Smillert    croak "Error uncompressing -- " . $o->error()
678b39c5158Smillert        if $o->error() ;
679b39c5158Smillert
680b39c5158Smillert    return ($o->getHeaderInfo()) ;
681b39c5158Smillert
682b39c5158Smillert}
683b39c5158Smillert
684b39c5158Smillertsub mkComplete
685b39c5158Smillert{
686b39c5158Smillert    my $class = shift ;
687b39c5158Smillert    my $data = shift;
688b39c5158Smillert    my $Error = getErrorRef($class);
689b39c5158Smillert
690b39c5158Smillert    my $buffer ;
691b39c5158Smillert    my %params = ();
692b39c5158Smillert
693b39c5158Smillert    if ($class eq 'IO::Compress::Gzip') {
694b39c5158Smillert        %params = (
695b39c5158Smillert            Name       => "My name",
696b39c5158Smillert            Comment    => "a comment",
697b39c5158Smillert            ExtraField => ['ab' => "extra"],
698b39c5158Smillert            HeaderCRC  => 1);
699b39c5158Smillert    }
700b39c5158Smillert    elsif ($class eq 'IO::Compress::Zip'){
701b39c5158Smillert        %params = (
702b39c5158Smillert            Name              => "My name",
703b39c5158Smillert            Comment           => "a comment",
704b39c5158Smillert            ZipComment        => "last comment",
705b39c5158Smillert            exTime            => [100, 200, 300],
706b39c5158Smillert            ExtraFieldLocal   => ["ab" => "extra1"],
707b39c5158Smillert            ExtraFieldCentral => ["cd" => "extra2"],
708b39c5158Smillert        );
709b39c5158Smillert    }
710b39c5158Smillert
711b39c5158Smillert    my $z = new $class( \$buffer, %params)
712b39c5158Smillert        or croak "Cannot create $class object: $$Error";
713b39c5158Smillert    $z->write($data);
714b39c5158Smillert    $z->close();
715b39c5158Smillert
716b39c5158Smillert    my $unc = getInverse($class);
717b39c5158Smillert    anyUncompress(\$buffer) eq $data
718b39c5158Smillert        or die "bad bad bad";
719b39c5158Smillert    my $u = new $unc( \$buffer);
720b39c5158Smillert    my $info = $u->getHeaderInfo() ;
721b39c5158Smillert
722b39c5158Smillert
723b39c5158Smillert    return wantarray ? ($info, $buffer) : $buffer ;
724b39c5158Smillert}
725b39c5158Smillert
726b39c5158Smillertsub mkErr
727b39c5158Smillert{
728b39c5158Smillert    my $string = shift ;
729b39c5158Smillert    my ($dummy, $file, $line) = caller ;
730b39c5158Smillert    -- $line ;
731b39c5158Smillert
732b39c5158Smillert    $file = quotemeta($file);
733b39c5158Smillert
734b39c5158Smillert    #return "/$string\\s+at $file line $line/" if $] >= 5.006 ;
735b39c5158Smillert    return "/$string\\s+at /" ;
736b39c5158Smillert}
737b39c5158Smillert
738b39c5158Smillertsub mkEvalErr
739b39c5158Smillert{
740b39c5158Smillert    my $string = shift ;
741b39c5158Smillert
742b39c5158Smillert    #return "/$string\\s+at \\(eval /" if $] > 5.006 ;
743b39c5158Smillert    return "/$string\\s+at /" ;
744b39c5158Smillert}
745b39c5158Smillert
746b39c5158Smillertsub dumpObj
747b39c5158Smillert{
748b39c5158Smillert    my $obj = shift ;
749b39c5158Smillert
750b39c5158Smillert    my ($dummy, $file, $line) = caller ;
751b39c5158Smillert
752b39c5158Smillert    if (@_)
753b39c5158Smillert    {
754b39c5158Smillert        print "#\n# dumpOBJ from $file line $line @_\n" ;
755b39c5158Smillert    }
756b39c5158Smillert    else
757b39c5158Smillert    {
758b39c5158Smillert        print "#\n# dumpOBJ from $file line $line \n" ;
759b39c5158Smillert    }
760b39c5158Smillert
761b39c5158Smillert    my $max = 0 ;;
762b39c5158Smillert    foreach my $k (keys %{ *$obj })
763b39c5158Smillert    {
764b39c5158Smillert        $max = length $k if length $k > $max ;
765b39c5158Smillert    }
766b39c5158Smillert
767b39c5158Smillert    foreach my $k (sort keys %{ *$obj })
768b39c5158Smillert    {
769b39c5158Smillert        my $v = $obj->{$k} ;
770b39c5158Smillert        $v = '-undef-' unless defined $v;
771b39c5158Smillert        my $pad = ' ' x ($max - length($k) + 2) ;
772b39c5158Smillert        print "# $k$pad: [$v]\n";
773b39c5158Smillert    }
774b39c5158Smillert    print "#\n" ;
775b39c5158Smillert}
776b39c5158Smillert
777b39c5158Smillert
778b39c5158Smillertsub getMultiValues
779b39c5158Smillert{
780b39c5158Smillert    my $class = shift ;
781b39c5158Smillert
782f3efcd01Safresh1    return (0,0) if $class =~ /lzf|lzma|zstd/i;
783b39c5158Smillert    return (1,0);
784b39c5158Smillert}
785b39c5158Smillert
786b39c5158Smillert
787b39c5158Smillertsub gotScalarUtilXS
788b39c5158Smillert{
789b39c5158Smillert    eval ' use Scalar::Util "dualvar" ';
790b39c5158Smillert    return $@ ? 0 : 1 ;
791b39c5158Smillert}
792b39c5158Smillert
793b39c5158Smillertpackage CompTestUtils;
794b39c5158Smillert
795b39c5158Smillert1;
796b39c5158Smillert__END__
797b39c5158Smillert	t/Test/Builder.pm
798b39c5158Smillert	t/Test/More.pm
799b39c5158Smillert	t/Test/Simple.pm
800b39c5158Smillert	t/compress/CompTestUtils.pm
801b39c5158Smillert	t/compress/any.pl
802b39c5158Smillert	t/compress/anyunc.pl
803b39c5158Smillert	t/compress/destroy.pl
804b39c5158Smillert	t/compress/generic.pl
805b39c5158Smillert	t/compress/merge.pl
806b39c5158Smillert	t/compress/multi.pl
807b39c5158Smillert	t/compress/newtied.pl
808b39c5158Smillert	t/compress/oneshot.pl
809b39c5158Smillert	t/compress/prime.pl
810b39c5158Smillert	t/compress/tied.pl
811b39c5158Smillert	t/compress/truncate.pl
812b39c5158Smillert	t/compress/zlib-generic.plParsing config.in...
813b39c5158SmillertBuilding Zlib enabled
814b39c5158SmillertAuto Detect Gzip OS Code..
815b39c5158SmillertSetting Gzip OS Code to 3 [Unix/Default]
816b39c5158SmillertLooks Good.
817