summaryrefslogtreecommitdiff
path: root/cpan/IO-Compress/t/105oneshot-zip-only.t
blob: 0906bf6e160d5acbf0e686010e8a3e32614fadab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
BEGIN {
    if ($ENV{PERL_CORE}) {
	chdir 't' if -d 't';
	@INC = ("../lib", "lib/compress");
    }
}

use lib qw(t t/compress);
use strict;
use warnings;
use bytes;

use Test::More ;
use CompTestUtils;

BEGIN {
    plan(skip_all => "oneshot needs Perl 5.005 or better - you have Perl $]" )
        if $] < 5.005 ;


    # use Test::NoWarnings, if available
    my $extra = 0 ;
    $extra = 1
        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };

    plan tests => 162 + $extra ;

    #use_ok('IO::Compress::Zip', qw(zip $ZipError :zip_method)) ;
    use_ok('IO::Compress::Zip', qw(:all)) ;
    use_ok('IO::Uncompress::Unzip', qw(unzip $UnzipError)) ;


}


sub zipGetHeader
{
    my $in = shift;
    my $content = shift ;
    my %opts = @_ ;

    my $out ;
    my $got ;

    ok zip($in, \$out, %opts), "  zip ok" ;
    ok unzip(\$out, \$got), "  unzip ok" 
        or diag $UnzipError ;
    is $got, $content, "  got expected content" ;

    my $gunz = new IO::Uncompress::Unzip \$out, Strict => 0
        or diag "UnzipError is $IO::Uncompress::Unzip::UnzipError" ;
    ok $gunz, "  Created IO::Uncompress::Unzip object";
    my $hdr = $gunz->getHeaderInfo();
    ok $hdr, "  got Header info";
    my $uncomp ;
    ok $gunz->read($uncomp), " read ok" ;
    is $uncomp, $content, "  got expected content";
    ok $gunz->close, "  closed ok" ;

    return $hdr ;
    
}

{
    title "Check zip header default NAME & MTIME settings" ;

    my $lex = new LexFile my $file1;

    my $content = "hello ";
    my $hdr ;
    my $mtime ;

    writeFile($file1, $content);
    $mtime = (stat($file1))[9];
    # make sure that the zip file isn't created in the same
    # second as the input file
    sleep 3 ; 
    $hdr = zipGetHeader($file1, $content);

    is $hdr->{Name}, $file1, "  Name is '$file1'";
    is $hdr->{Time}>>1, $mtime>>1, "  Time is ok";

    title "Override Name" ;

    writeFile($file1, $content);
    $mtime = (stat($file1))[9];
    sleep 3 ; 
    $hdr = zipGetHeader($file1, $content, Name => "abcde");

    is $hdr->{Name}, "abcde", "  Name is 'abcde'" ;
    is $hdr->{Time} >> 1, $mtime >> 1, "  Time is ok";

    title "Override Time" ;

    writeFile($file1, $content);
    my $useTime = time + 2000 ;
    $hdr = zipGetHeader($file1, $content, Time => $useTime);

    is $hdr->{Name}, $file1, "  Name is '$file1'" ;
    is $hdr->{Time} >> 1 , $useTime >> 1 ,  "  Time is $useTime";

    title "Override Name and Time" ;

    $useTime = time + 5000 ;
    writeFile($file1, $content);
    $hdr = zipGetHeader($file1, $content, Time => $useTime, Name => "abcde");

    is $hdr->{Name}, "abcde", "  Name is 'abcde'" ;
    is $hdr->{Time} >> 1 , $useTime >> 1 , "  Time is $useTime";

    title "Filehandle doesn't have default Name or Time" ;
    my $fh = new IO::File "< $file1"
        or diag "Cannot open '$file1': $!\n" ;
    sleep 3 ; 
    my $before = time ;
    $hdr = zipGetHeader($fh, $content);
    my $after = time ;

    ok ! defined $hdr->{Name}, "  Name is undef";
    cmp_ok $hdr->{Time} >> 1, '>=', $before >> 1, "  Time is ok";
    cmp_ok $hdr->{Time} >> 1, '<=', $after >> 1, "  Time is ok";

    $fh->close;

    title "Buffer doesn't have default Name or Time" ;
    my $buffer = $content;
    $before = time ;
    $hdr = zipGetHeader(\$buffer, $content);
    $after = time ;

    ok ! defined $hdr->{Name}, "  Name is undef";
    cmp_ok $hdr->{Time} >> 1, '>=', $before >> 1, "  Time is ok";
    cmp_ok $hdr->{Time} >> 1, '<=', $after >> 1, "  Time is ok";
}

for my $stream (0, 1)
{
    for my $zip64 (0, 1)
    {
        #next if $zip64 && ! $stream;

        for my $method (ZIP_CM_STORE, ZIP_CM_DEFLATE)
        {

            title "Stream $stream, Zip64 $zip64, Method $method";

            my $lex = new LexFile my $file1;

            my $content = "hello ";
            #writeFile($file1, $content);

            my $status = zip(\$content => $file1 , 
                               Method => $method, 
                               Stream => $stream,
                               Zip64  => $zip64);

             ok $status, "  zip ok" 
                or diag $ZipError ;

            my $got ;
            if ($stream && $method == ZIP_CM_STORE ) {
                #eval ' unzip($file1 => \$got) ';
                ok ! unzip($file1 => \$got), "  unzip fails"; 
                like $UnzipError, "/Streamed Stored content not supported/",
                    "  Streamed Stored content not supported";
                    next ;
            }

            ok unzip($file1 => \$got), "  unzip ok"
                or diag $UnzipError ;

            is $got, $content, "  content ok";

            my $u = new IO::Uncompress::Unzip $file1
                or diag $ZipError ;

            my $hdr = $u->getHeaderInfo();
            ok $hdr, "  got header";

            is $hdr->{Stream}, $stream, "  stream is $stream" ;
            is $hdr->{MethodID}, $method, "  MethodID is $method" ;
            is $hdr->{Zip64}, $zip64, "  Zip64 is $zip64" ;
        }
    }
}

for my $stream (0, 1)
{
    for my $zip64 (0, 1)
    {
        next if $zip64 && ! $stream;
        for my $method (ZIP_CM_STORE, ZIP_CM_DEFLATE)
        {
            title "Stream $stream, Zip64 $zip64, Method $method";

            my $file1;
            my $file2;
            my $zipfile;
            my $lex = new LexFile $file1, $file2, $zipfile;

            my $content1 = "hello ";
            writeFile($file1, $content1);

            my $content2 = "goodbye ";
            writeFile($file2, $content2);

            my %content = ( $file1 => $content1,
                            $file2 => $content2,
                          );

            ok zip([$file1, $file2] => $zipfile , Method => $method, 
                                                  Zip64  => $zip64,
                                                  Stream => $stream), " zip ok" 
                or diag $ZipError ;

            for my $file ($file1, $file2)
            {
                my $got ;
                if ($stream &&  $method == ZIP_CM_STORE ) {
                    #eval ' unzip($zipfile => \$got) ';
                    ok ! unzip($zipfile => \$got, Name => $file), "  unzip fails"; 
                    like $UnzipError, "/Streamed Stored content not supported/",
                        "  Streamed Stored content not supported";
                        next ;
                }

                ok unzip($zipfile => \$got, Name => $file), "  unzip $file ok"
                    or diag $UnzipError ;

                is $got, $content{$file}, "  content ok";
            }
        }
    }
}

# TODO add more error cases