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{
18    plan skip_all => "Lengthy Tests Disabled\n" .
19                     "set COMPRESS_ZLIB_RUN_ALL to run this test suite"
20        unless defined $ENV{COMPRESS_ZLIB_RUN_ALL} ;
21
22    # use Test::NoWarnings, if available
23    my $extra = 0 ;
24    $extra = 1
25        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
26
27    plan tests => 76 + $extra ;
28
29
30    use_ok('Compress::Zlib', 2) ;
31    use_ok('IO::Compress::Gzip', qw($GzipError)) ;
32    use_ok('IO::Uncompress::Gunzip', qw($GunzipError)) ;
33    use_ok('IO::Compress::Gzip::Constants');
34}
35
36my $compressed ;
37my $expected_crc ;
38
39for my $wrap (0 .. 2)
40{
41    for my $offset ( -1 .. 1 )
42    {
43        next if $wrap == 0 && $offset < 0 ;
44
45        title "Wrap $wrap, Offset $offset" ;
46
47        my $size = (GZIP_ISIZE_MAX * $wrap) + $offset ;
48
49        my $expected_isize ;
50        if ($wrap == 0) {
51            $expected_isize = $offset ;
52        }
53        elsif ($wrap == 1 && $offset <= 0) {
54            $expected_isize = GZIP_ISIZE_MAX + $offset ;
55        }
56        elsif ($wrap > 1) {
57            $expected_isize = GZIP_ISIZE_MAX + $offset - 1;
58        }
59        else {
60            $expected_isize = $offset - 1;
61        }
62
63        sub gzipClosure
64        {
65            my $gzip = shift ;
66            my $max = shift  ;
67
68            my $index = 0 ;
69            my $inc = 1024 * 5000 ;
70            my $buff = 'x' x $inc ;
71            my $left = $max ;
72
73            return
74                sub {
75
76                    if ($max == 0 && $index == 0) {
77                        $expected_crc = crc32('') ;
78                        ok $gzip->close(), '  IO::Compress::Gzip::close ok X' ;
79                        ++ $index ;
80                        $_[0] .= $compressed;
81                        return length $compressed ;
82                    }
83
84                    return 0 if $index >= $max ;
85
86                    while ( ! length $compressed )
87                    {
88                        $index += $inc ;
89
90                        if ($index <= $max) {
91                            $gzip->write($buff) ;
92                            #print "Write " . length($buff) . "\n" ;
93                            #print "# LEN Compressed " . length($compressed) . "\n" ;
94                            $expected_crc = crc32($buff, $expected_crc) ;
95                            $left -= $inc ;
96                        }
97                        else  {
98                            #print "Write $left\n" ;
99                            $gzip->write('x' x $left) ;
100                            #print "# LEN Compressed " . length($compressed) . "\n" ;
101                            $expected_crc = crc32('x' x $left, $expected_crc) ;
102                            ok $gzip->close(), '  IO::Compress::Gzip::close ok ' ;
103                            last ;
104                        }
105                    }
106
107                    my $len = length $compressed ;
108                    $_[0] .= $compressed ;
109                    $compressed = '';
110                    #print "# LEN $len\n" if $len <=0 ;
111
112                    return $len ;
113                };
114        }
115
116        my $gzip = new IO::Compress::Gzip \$compressed,
117                                -Append     => 0,
118                                -HeaderCRC  => 1;
119
120        ok $gzip, "  Created IO::Compress::Gzip object";
121
122        my $gunzip = new IO::Uncompress::Gunzip gzipClosure($gzip, $size),
123                                    -BlockSize  => 1024 * 500 ,
124                                    -Append => 0,
125                                    -Strict => 1;
126
127        ok $gunzip, "  Created IO::Uncompress::Gunzip object";
128
129        my $inflate = *$gunzip->{Inflate} ;
130        my $deflate = *$gzip->{Deflate} ;
131
132        my $status ;
133        my $uncompressed;
134        my $actual = 0 ;
135        while (($status = $gunzip->read($uncompressed)) > 0) {
136            #print "# READ $status\n" ;
137            $actual += $status ;
138        }
139
140        is $status, 0, '  IO::Uncompress::Gunzip::read returned 0'
141            or diag "error status is $status, error is $GunzipError" ;
142
143        ok $gunzip->close(), "  IO::Uncompress::Gunzip Closed ok" ;
144
145        is $actual, $size, "  Length of Gunzipped data is $size"
146            or diag "Expected $size, got $actual";
147
148        my $gunzip_hdr = $gunzip->getHeaderInfo();
149
150        is $gunzip_hdr->{ISIZE}, $expected_isize,
151            sprintf("  ISIZE is $expected_isize [0x%X]", $expected_isize);
152        is $gunzip_hdr->{CRC32}, $expected_crc,
153            sprintf("  CRC32 is $expected_crc [0x%X]", $expected_crc);
154
155        $expected_crc = 0 ;
156    }
157}
158
159