1BEGIN {
2    if ($ENV{PERL_CORE}) {
3	chdir 't' if -d 't';
4	@INC = ("../lib", "lib/compress");
5    }
6}
7
8use lib qw(t t/compress);
9use strict;
10use warnings;
11use bytes;
12
13use Test::More ;
14use CompTestUtils;
15
16BEGIN {
17    plan(skip_all => "oneshot needs Perl 5.005 or better - you have Perl $]" )
18        if $] < 5.005 ;
19
20    plan(skip_all => "IO::Compress::Bzip2 not available" )
21        unless eval { require IO::Compress::Bzip2;
22                      require IO::Uncompress::Bunzip2;
23                      1
24                    } ;
25
26    # use Test::NoWarnings, if available
27    my $extra = 0 ;
28    $extra = 1
29        if eval { require Test::NoWarnings ;  Test::NoWarnings->import; 1 };
30
31    plan tests => 248 + $extra ;
32
33    #use_ok('IO::Compress::Zip', qw(zip $ZipError :zip_method)) ;
34    use_ok('IO::Compress::Zip', qw(:all)) ;
35    use_ok('IO::Uncompress::Unzip', qw(unzip $UnzipError)) ;
36
37
38}
39
40
41sub zipGetHeader
42{
43    my $in = shift;
44    my $content = shift ;
45    my %opts = @_ ;
46
47    my $out ;
48    my $got ;
49
50    ok zip($in, \$out, %opts), "  zip ok" ;
51    ok unzip(\$out, \$got), "  unzip ok"
52        or diag $UnzipError ;
53    is $got, $content, "  got expected content" ;
54
55    my $gunz = IO::Uncompress::Unzip->new( \$out, Strict => 0 )
56        or diag "UnzipError is $IO::Uncompress::Unzip::UnzipError" ;
57    ok $gunz, "  Created IO::Uncompress::Unzip object";
58    my $hdr = $gunz->getHeaderInfo();
59    ok $hdr, "  got Header info";
60    my $uncomp ;
61    ok $gunz->read($uncomp), " read ok" ;
62    is $uncomp, $content, "  got expected content";
63    ok $gunz->close, "  closed ok" ;
64
65    return $hdr ;
66
67}
68
69
70for my $input (0, 1)
71{
72    for my $stream (0, 1)
73    {
74        for my $zip64 (0, 1)
75        {
76            #next if $zip64 && ! $stream;
77
78            for my $method (ZIP_CM_STORE, ZIP_CM_DEFLATE, ZIP_CM_BZIP2)
79            {
80                title "Input $input, Stream $stream, Zip64 $zip64, Method $method";
81
82                my $lex1 = LexFile->new( my $file1 );
83                my $lex2 = LexFile->new( my $file2 );
84                my $content = "hello ";
85                my $in ;
86
87                if ($input)
88                {
89                    writeFile($file2, $content);
90                    $in = $file2;
91                }
92                else
93                {
94                    $in = \$content;
95                }
96
97
98                ok zip($in => $file1 , Method => $method,
99                                       Zip64  => $zip64,
100                                       Stream => $stream), " zip ok"
101                    or diag $ZipError ;
102
103                my $got ;
104                ok unzip($file1 => \$got), "  unzip ok"
105                    or diag $UnzipError ;
106
107                is $got, $content, "  content ok";
108
109                my $u = IO::Uncompress::Unzip->new( $file1 )
110                    or diag $ZipError ;
111
112                my $hdr = $u->getHeaderInfo();
113                ok $hdr, "  got header";
114
115                is $hdr->{Stream}, $stream, "  stream is $stream" ;
116                is $hdr->{MethodID}, $method, "  MethodID is $method" ;
117                is $hdr->{Zip64}, $zip64, "  Zip64 is $zip64" ;
118            }
119        }
120    }
121}
122
123for my $stream (0, 1)
124{
125    for my $zip64 (0, 1)
126    {
127        next if $zip64 && ! $stream;
128
129        for my $method (ZIP_CM_STORE, ZIP_CM_DEFLATE, ZIP_CM_BZIP2)
130        {
131            title "Stream $stream, Zip64 $zip64, Method $method";
132
133            my $file1;
134            my $file2;
135            my $zipfile;
136            my $lex = LexFile->new( $file1, $file2, $zipfile );
137
138            my $content1 = "hello ";
139            writeFile($file1, $content1);
140
141            my $content2 = "goodbye ";
142            writeFile($file2, $content2);
143
144            my %content = ( $file1 => $content1,
145                            $file2 => $content2,
146                          );
147
148            ok zip([$file1, $file2] => $zipfile , Method => $method,
149                                                  Zip64  => $zip64,
150                                                  Stream => $stream), " zip ok"
151                or diag $ZipError ;
152
153            for my $file ($file1, $file2)
154            {
155                my $got ;
156                ok unzip($zipfile => \$got, Name => $file), "  unzip $file ok"
157                    or diag $UnzipError ;
158
159                is $got, $content{$file}, "  content ok";
160            }
161        }
162    }
163}
164
165# TODO add more error cases
166