1b39c5158SmillertBEGIN {
2b39c5158Smillert    if ($ENV{PERL_CORE}) {
3b39c5158Smillert	chdir 't' if -d 't';
4b39c5158Smillert	@INC = ("../lib", "lib/compress");
5b39c5158Smillert    }
6b39c5158Smillert}
7b39c5158Smillert
8b39c5158Smillertuse lib qw(t t/compress);
9b39c5158Smillertuse strict;
10b39c5158Smillertuse warnings;
11b39c5158Smillertuse bytes;
12b39c5158Smillert
13b39c5158Smillertuse Test::More ;
14b39c5158Smillertuse CompTestUtils;
15b39c5158Smillert
16b39c5158SmillertBEGIN {
17b39c5158Smillert    # use Test::NoWarnings, if available
18b39c5158Smillert    my $extra = 0 ;
19b39c5158Smillert    $extra = 1
20*5486feefSafresh1        if eval { require Test::NoWarnings ;  Test::NoWarnings->import; 1 };
21b39c5158Smillert
22b39c5158Smillert
23898184e3Ssthen    plan tests => 918 + $extra ;
24b39c5158Smillert
25b39c5158Smillert    use_ok('Compress::Raw::Zlib') ;
26b39c5158Smillert    use_ok('IO::Compress::Gzip::Constants') ;
27b39c5158Smillert
28b39c5158Smillert    use_ok('IO::Compress::Gzip', qw($GzipError)) ;
29b39c5158Smillert    use_ok('IO::Uncompress::Gunzip', qw($GunzipError)) ;
30b39c5158Smillert
31b39c5158Smillert}
32b39c5158Smillert
33b39c5158Smillert
34b39c5158Smillert
35b39c5158Smillert# Check the Gzip Header Parameters
36b39c5158Smillert#========================================
37b39c5158Smillert
38b39c5158Smillertmy $ThisOS_code = $Compress::Raw::Zlib::gzip_os_code;
39b39c5158Smillert
40256a93a4Safresh1my $lex = LexFile->new( my $name );
41b39c5158Smillert
42b39c5158Smillert{
43b39c5158Smillert    title "Check Defaults";
44b39c5158Smillert    # Check Name defaults undef, no name, no comment
45b39c5158Smillert    # and Time can be explicitly set.
46b39c5158Smillert
47b39c5158Smillert    my $hdr = readHeaderInfo($name, -Time => 1234);
48b39c5158Smillert
49b39c5158Smillert    is $hdr->{Time}, 1234;
50b39c5158Smillert    ok ! defined $hdr->{Name};
51b39c5158Smillert    is $hdr->{MethodName}, 'Deflated';
52b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
53b39c5158Smillert    is $hdr->{MethodID}, Z_DEFLATED;
54b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
55b39c5158Smillert    ok ! defined $hdr->{Comment} ;
56b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
57b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
58b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
59b39c5158Smillert}
60b39c5158Smillert
61b39c5158Smillert{
62b39c5158Smillert
63b39c5158Smillert    title "Check name can be different from filename" ;
64b39c5158Smillert    # Check Name can be different from filename
65b39c5158Smillert    # Comment and Extra can be set
66b39c5158Smillert    # Can specify a zero Time
67b39c5158Smillert
68b39c5158Smillert    my $comment = "This is a Comment" ;
69b39c5158Smillert    my $extra = "A little something extra" ;
70b39c5158Smillert    my $aname = "a new name" ;
71b39c5158Smillert    my $hdr = readHeaderInfo $name,
72b39c5158Smillert				      -Strict     => 0,
73b39c5158Smillert				      -Name       => $aname,
74b39c5158Smillert    				  -Comment    => $comment,
75b39c5158Smillert    				  -ExtraField => $extra,
76b39c5158Smillert    				  -Time       => 0 ;
77b39c5158Smillert
78b39c5158Smillert    ok $hdr->{Time} == 0;
79b39c5158Smillert    ok $hdr->{Name} eq $aname;
80b39c5158Smillert    ok $hdr->{MethodName} eq 'Deflated';
81b39c5158Smillert    ok $hdr->{MethodID} == 8;
82b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
83b39c5158Smillert    ok $hdr->{Comment} eq $comment ;
84b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
85b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
86b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
87b39c5158Smillert}
88b39c5158Smillert
89b39c5158Smillert{
90b39c5158Smillert    title "Check Time defaults to now" ;
91b39c5158Smillert
92b39c5158Smillert    # Check Time defaults to now
93b39c5158Smillert    # and that can have empty name, comment and extrafield
94b39c5158Smillert    my $before = time ;
95b39c5158Smillert    my $hdr = readHeaderInfo $name,
96b39c5158Smillert		          -TextFlag   => 1,
97b39c5158Smillert		          -Name       => "",
98b39c5158Smillert    		      -Comment    => "",
99b39c5158Smillert    		      -ExtraField => "";
100b39c5158Smillert    my $after = time ;
101b39c5158Smillert
102b39c5158Smillert    ok $hdr->{Time} >= $before ;
103b39c5158Smillert    ok $hdr->{Time} <= $after ;
104b39c5158Smillert
105b39c5158Smillert    ok defined $hdr->{Name} ;
106b39c5158Smillert    ok $hdr->{Name} eq "";
107b39c5158Smillert    ok defined $hdr->{Comment} ;
108b39c5158Smillert    ok $hdr->{Comment} eq "";
109b39c5158Smillert    ok defined $hdr->{ExtraFieldRaw} ;
110b39c5158Smillert    ok $hdr->{ExtraFieldRaw} eq "";
111b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
112b39c5158Smillert
113b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
114b39c5158Smillert    ok   $hdr->{TextFlag} ;
115b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
116b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
117b39c5158Smillert
118b39c5158Smillert}
119b39c5158Smillert
120b39c5158Smillert{
121b39c5158Smillert    title "can have null extrafield" ;
122b39c5158Smillert
123b39c5158Smillert    my $before = time ;
124b39c5158Smillert    my $hdr = readHeaderInfo $name,
125b39c5158Smillert				      -strict     => 0,
126b39c5158Smillert		              -Name       => "a",
127b39c5158Smillert    			      -Comment    => "b",
128b39c5158Smillert    			      -ExtraField => "\x00";
129b39c5158Smillert    my $after = time ;
130b39c5158Smillert
131b39c5158Smillert    ok $hdr->{Time} >= $before ;
132b39c5158Smillert    ok $hdr->{Time} <= $after ;
133b39c5158Smillert    ok $hdr->{Name} eq "a";
134b39c5158Smillert    ok $hdr->{Comment} eq "b";
135b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
136b39c5158Smillert    ok $hdr->{ExtraFieldRaw} eq "\x00";
137b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
138b39c5158Smillert    ok ! $hdr->{TextFlag} ;
139b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
140b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
141b39c5158Smillert
142b39c5158Smillert}
143b39c5158Smillert
144b39c5158Smillert{
145b39c5158Smillert    title "can have undef name, comment, time and extrafield" ;
146b39c5158Smillert
147b39c5158Smillert    my $hdr = readHeaderInfo $name,
148b39c5158Smillert	                  -Name       => undef,
149b39c5158Smillert    		          -Comment    => undef,
150b39c5158Smillert    		          -ExtraField => undef,
151b39c5158Smillert                      -Time       => undef;
152b39c5158Smillert
153b39c5158Smillert    ok $hdr->{Time} == 0;
154b39c5158Smillert    ok ! defined $hdr->{Name} ;
155b39c5158Smillert    ok ! defined $hdr->{Comment} ;
156b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
157b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
158b39c5158Smillert    ok ! $hdr->{TextFlag} ;
159b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
160b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
161b39c5158Smillert
162b39c5158Smillert}
163b39c5158Smillert
164b39c5158Smillertfor my $value ( "0D", "0A", "0A0D", "0D0A", "0A0A", "0D0D")
165b39c5158Smillert{
166b39c5158Smillert    title "Comment with $value" ;
167b39c5158Smillert
168b39c5158Smillert    my $v = pack "h*", $value;
169b39c5158Smillert    my $comment = "my${v}comment$v";
170b39c5158Smillert    my $hdr = readHeaderInfo $name,
171b39c5158Smillert                    Time => 0,
172b39c5158Smillert                  -TextFlag   => 1,
173b39c5158Smillert                  -Name       => "",
174b39c5158Smillert                  -Comment    => $comment,
175b39c5158Smillert                  -ExtraField => "";
176b39c5158Smillert    my $after = time ;
177b39c5158Smillert
178b39c5158Smillert    is $hdr->{Time}, 0 ;
179b39c5158Smillert
180b39c5158Smillert    ok defined $hdr->{Name} ;
181b39c5158Smillert    ok $hdr->{Name} eq "";
182b39c5158Smillert    ok defined $hdr->{Comment} ;
183b39c5158Smillert    is $hdr->{Comment}, $comment;
184b39c5158Smillert    ok defined $hdr->{ExtraFieldRaw} ;
185b39c5158Smillert    ok $hdr->{ExtraFieldRaw} eq "";
186b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
187b39c5158Smillert
188b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
189b39c5158Smillert    ok   $hdr->{TextFlag} ;
190b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
191b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
192b39c5158Smillert}
193b39c5158Smillert
194b39c5158Smillert{
195b39c5158Smillert    title "Check crchdr" ;
196b39c5158Smillert
197b39c5158Smillert    my $hdr = readHeaderInfo $name, -HeaderCRC  => 1;
198b39c5158Smillert
199b39c5158Smillert    ok ! defined $hdr->{Name};
200b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
201b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
202b39c5158Smillert    ok ! defined $hdr->{Comment} ;
203b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
204b39c5158Smillert    ok ! $hdr->{TextFlag} ;
205b39c5158Smillert    ok   defined $hdr->{HeaderCRC} ;
206b39c5158Smillert    is $hdr->{OsID}, $ThisOS_code ;
207b39c5158Smillert}
208b39c5158Smillert
209b39c5158Smillert{
210b39c5158Smillert    title "Check ExtraFlags" ;
211b39c5158Smillert
212b39c5158Smillert    my $hdr = readHeaderInfo $name, -Level  => Z_BEST_SPEED;
213b39c5158Smillert
214b39c5158Smillert    ok ! defined $hdr->{Name};
215898184e3Ssthen    is $hdr->{ExtraFlags}, 4;
216b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
217b39c5158Smillert    ok ! defined $hdr->{Comment} ;
218b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
219b39c5158Smillert    ok ! $hdr->{TextFlag} ;
220b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
221b39c5158Smillert
222b39c5158Smillert    $hdr = readHeaderInfo $name, -Level  => Z_BEST_COMPRESSION;
223b39c5158Smillert
224b39c5158Smillert    ok ! defined $hdr->{Name};
225898184e3Ssthen    is $hdr->{ExtraFlags}, 2;
226b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
227b39c5158Smillert    ok ! defined $hdr->{Comment} ;
228b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
229b39c5158Smillert    ok ! $hdr->{TextFlag} ;
230b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
231b39c5158Smillert
232b39c5158Smillert    $hdr = readHeaderInfo $name, -Level  => Z_BEST_COMPRESSION,
233b39c5158Smillert                                 -ExtraFlags => 42;
234b39c5158Smillert
235b39c5158Smillert    ok ! defined $hdr->{Name};
236b39c5158Smillert    is $hdr->{ExtraFlags}, 42;
237b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
238b39c5158Smillert    ok ! defined $hdr->{Comment} ;
239b39c5158Smillert    ok ! $hdr->{isMinimalHeader} ;
240b39c5158Smillert    ok ! $hdr->{TextFlag} ;
241b39c5158Smillert    ok ! defined $hdr->{HeaderCRC} ;
242b39c5158Smillert
243b39c5158Smillert
244b39c5158Smillert}
245b39c5158Smillert
246b39c5158Smillert{
247b39c5158Smillert    title "OS Code" ;
248b39c5158Smillert
249b39c5158Smillert    for my $code ( -1, undef, '', 'fred' )
250b39c5158Smillert    {
251b39c5158Smillert        my $code_name = defined $code ? "'$code'" : "'undef'";
252256a93a4Safresh1        eval { IO::Compress::Gzip->new( $name, -OS_Code => $code ) } ;
253b39c5158Smillert        like $@, mkErr("^IO::Compress::Gzip: Parameter 'OS_Code' must be an unsigned int, got $code_name"),
254b39c5158Smillert            " Trap OS Code $code_name";
255b39c5158Smillert    }
256b39c5158Smillert
257b39c5158Smillert    for my $code ( qw( 256 ) )
258b39c5158Smillert    {
259256a93a4Safresh1        eval { ok ! IO::Compress::Gzip->new($name, OS_Code => $code) };
260b39c5158Smillert        like $@, mkErr("OS_Code must be between 0 and 255, got '$code'"),
261b39c5158Smillert            " Trap OS Code $code";
262b39c5158Smillert        like $GzipError, "/OS_Code must be between 0 and 255, got '$code'/",
263b39c5158Smillert            " Trap OS Code $code";
264b39c5158Smillert    }
265b39c5158Smillert
266b39c5158Smillert    for my $code ( qw(0 1 12 254 255) )
267b39c5158Smillert    {
268b39c5158Smillert        my $hdr = readHeaderInfo $name, OS_Code => $code;
269b39c5158Smillert
270b39c5158Smillert        is $hdr->{OsID}, $code, "  Code is $code" ;
271b39c5158Smillert    }
272b39c5158Smillert
273b39c5158Smillert
274b39c5158Smillert
275b39c5158Smillert}
276b39c5158Smillert
277b39c5158Smillert{
278b39c5158Smillert    title 'Check ExtraField';
279b39c5158Smillert
280b39c5158Smillert    my @tests = (
281b39c5158Smillert        [1, ['AB' => '']                   => [['AB'=>'']] ],
282b39c5158Smillert        [1, {'AB' => ''}                   => [['AB'=>'']] ],
283b39c5158Smillert        [1, ['AB' => 'Fred']               => [['AB'=>'Fred']] ],
284b39c5158Smillert        [1, {'AB' => 'Fred'}               => [['AB'=>'Fred']] ],
285b39c5158Smillert        [1, ['Xx' => '','AB' => 'Fred']    => [['Xx' => ''],['AB'=>'Fred']] ],
286b39c5158Smillert        [1, ['Xx' => '','Xx' => 'Fred']    => [['Xx' => ''],['Xx'=>'Fred']] ],
287b39c5158Smillert        [1, ['Xx' => '',
288b39c5158Smillert             'Xx' => 'Fred',
289b39c5158Smillert             'Xx' => 'Fred']               => [['Xx' => ''],['Xx'=>'Fred'],
290b39c5158Smillert                                               ['Xx'=>'Fred']] ],
291b39c5158Smillert        [1, [ ['Xx' => 'a'],
292b39c5158Smillert              ['AB' => 'Fred'] ]           => [['Xx' => 'a'],['AB'=>'Fred']] ],
293b39c5158Smillert        [0, {'AB' => 'Fred',
294b39c5158Smillert             'Pq' => 'r',
295b39c5158Smillert             "\x01\x02" => "\x03"}         => [['AB'=>'Fred'],
296b39c5158Smillert                                               ['Pq'=>'r'],
297b39c5158Smillert                                               ["\x01\x02"=>"\x03"]] ],
298b39c5158Smillert        [1, ['AB' => 'z' x GZIP_FEXTRA_SUBFIELD_MAX_SIZE] =>
299b39c5158Smillert                            [['AB'=>'z' x GZIP_FEXTRA_SUBFIELD_MAX_SIZE]] ],
300b39c5158Smillert                );
301b39c5158Smillert
302b39c5158Smillert    foreach my $test (@tests) {
303b39c5158Smillert        my ($order, $input, $result) = @$test ;
304256a93a4Safresh1        ok my $x = IO::Compress::Gzip->new( $name,
305b39c5158Smillert                                -ExtraField  => $input,
306256a93a4Safresh1                                -HeaderCRC   => 1 )
307b39c5158Smillert            or diag "GzipError is $GzipError" ;                            ;
308b39c5158Smillert        my $string = "abcd" ;
309b39c5158Smillert        ok $x->write($string) ;
310b39c5158Smillert        ok $x->close ;
311b39c5158Smillert        #is GZreadFile($name), $string ;
312b39c5158Smillert
313256a93a4Safresh1        ok $x = IO::Uncompress::Gunzip->new( $name,
314b39c5158Smillert                              #-Strict     => 1,
315256a93a4Safresh1                               -ParseExtra => 1 )
316b39c5158Smillert            or diag "GunzipError is $GunzipError" ;                            ;
317b39c5158Smillert        my $hdr = $x->getHeaderInfo();
318b39c5158Smillert        ok $hdr;
319b39c5158Smillert        ok ! defined $hdr->{Name};
320b39c5158Smillert        ok ! defined $hdr->{Comment} ;
321b39c5158Smillert        ok ! $hdr->{isMinimalHeader} ;
322b39c5158Smillert        ok ! $hdr->{TextFlag} ;
323b39c5158Smillert        ok   defined $hdr->{HeaderCRC} ;
324b39c5158Smillert
325b39c5158Smillert        ok   defined $hdr->{ExtraFieldRaw} ;
326b39c5158Smillert        ok   defined $hdr->{ExtraField} ;
327b39c5158Smillert
328b39c5158Smillert        my $extra = $hdr->{ExtraField} ;
329b39c5158Smillert
330b39c5158Smillert        if ($order) {
331b39c5158Smillert            eq_array $extra, $result;
332b39c5158Smillert        } else {
333b39c5158Smillert            eq_set $extra, $result;
334b39c5158Smillert        }
335b39c5158Smillert    }
336b39c5158Smillert
337b39c5158Smillert}
338b39c5158Smillert
339b39c5158Smillert{
340b39c5158Smillert    title 'Write Invalid ExtraField';
341b39c5158Smillert
342b39c5158Smillert    my $prefix = 'Error with ExtraField Parameter: ';
343b39c5158Smillert    my @tests = (
344b39c5158Smillert            [ sub{ "abc" }        => "Not a scalar, array ref or hash ref"],
345b39c5158Smillert            [ [ "a" ]             => "Not even number of elements"],
346b39c5158Smillert            [ [ "a" => "fred" ]   => 'SubField ID not two chars long'],
347b39c5158Smillert            [ [ "a\x00" => "fred" ]   => 'SubField ID 2nd byte is 0x00'],
348b39c5158Smillert            [ [ [ {}, "abc" ]]    => "SubField ID is a reference"],
349b39c5158Smillert            [ [ [ "ab", \1 ]]     => "SubField Data is a reference"],
350b39c5158Smillert            [ [ {"a" => "fred"} ] => "Not list of lists"],
351b39c5158Smillert            [ [ ['ab'=>'x'],{"a" => "fred"} ] => "Not list of lists"],
352b39c5158Smillert            [ [ ["aa"] ]          => "SubField must have two parts"],
353b39c5158Smillert            [ [ ["aa", "b", "c"] ] => "SubField must have two parts"],
354b39c5158Smillert            [ [ ["ab" => 'x' x (GZIP_FEXTRA_SUBFIELD_MAX_SIZE + 1) ] ]
355b39c5158Smillert                                   => "SubField Data too long"],
356b39c5158Smillert
357b39c5158Smillert            [ { 'abc', 1 }        => "SubField ID not two chars long"],
358b39c5158Smillert            [ { \1 , "abc" }    => "SubField ID not two chars long"],
359b39c5158Smillert            [ { "ab", \1 }     => "SubField Data is a reference"],
360b39c5158Smillert        );
361b39c5158Smillert
362b39c5158Smillert
363b39c5158Smillert
364b39c5158Smillert    foreach my $test (@tests) {
365b39c5158Smillert        my ($input, $string) = @$test ;
366b39c5158Smillert        my $buffer ;
367b39c5158Smillert        my $x ;
368256a93a4Safresh1        eval { $x = IO::Compress::Gzip->new( \$buffer, -ExtraField  => $input ); };
369b39c5158Smillert        like $@, mkErr("$prefix$string");
370b39c5158Smillert        like $GzipError, "/$prefix$string/";
371b39c5158Smillert        ok ! $x ;
372b39c5158Smillert
373b39c5158Smillert    }
374b39c5158Smillert
375b39c5158Smillert}
376b39c5158Smillert
377b39c5158Smillert{
378b39c5158Smillert    # Corrupt ExtraField
379b39c5158Smillert
380b39c5158Smillert    my @tests = (
381b39c5158Smillert        ["Sub-field truncated",
382b39c5158Smillert            "Error with ExtraField Parameter: Truncated in FEXTRA Body Section",
383b39c5158Smillert            "Header Error: Truncated in FEXTRA Body Section",
384b39c5158Smillert            ['a', undef, undef]              ],
385b39c5158Smillert        ["Length of field incorrect",
386b39c5158Smillert            "Error with ExtraField Parameter: Truncated in FEXTRA Body Section",
387b39c5158Smillert            "Header Error: Truncated in FEXTRA Body Section",
388b39c5158Smillert            ["ab", 255, "abc"]               ],
389b39c5158Smillert        ["Length of 2nd field incorrect",
390b39c5158Smillert            "Error with ExtraField Parameter: Truncated in FEXTRA Body Section",
391b39c5158Smillert            "Header Error: Truncated in FEXTRA Body Section",
392b39c5158Smillert            ["ab", 3, "abc"], ["de", 7, "x"] ],
393b39c5158Smillert        ["Length of 2nd field incorrect",
394b39c5158Smillert            "Error with ExtraField Parameter: SubField ID 2nd byte is 0x00",
395b39c5158Smillert            "Header Error: SubField ID 2nd byte is 0x00",
396b39c5158Smillert            ["a\x00", 3, "abc"], ["de", 7, "x"] ],
397b39c5158Smillert        );
398b39c5158Smillert
399b39c5158Smillert    foreach my $test (@tests)
400b39c5158Smillert    {
401b39c5158Smillert        my $name = shift @$test;
402b39c5158Smillert        my $gzip_error = shift @$test;
403b39c5158Smillert        my $gunzip_error = shift @$test;
404b39c5158Smillert
405b39c5158Smillert        title "Read Corrupt ExtraField - $name" ;
406b39c5158Smillert
407b39c5158Smillert        my $input = '';
408b39c5158Smillert
409b39c5158Smillert        for my $field (@$test)
410b39c5158Smillert        {
411b39c5158Smillert            my ($id, $len, $data) = @$field;
412b39c5158Smillert
413b39c5158Smillert            $input .= $id if defined $id ;
414b39c5158Smillert            $input .= pack("v", $len) if defined $len ;
415b39c5158Smillert            $input .= $data if defined $data;
416b39c5158Smillert        }
417b39c5158Smillert        #hexDump(\$input);
418b39c5158Smillert
419b39c5158Smillert        my $buffer ;
420b39c5158Smillert        my $x ;
421256a93a4Safresh1        eval {$x = IO::Compress::Gzip->new( \$buffer, -ExtraField  => $input, Strict => 1 ); };
422b39c5158Smillert        like $@, mkErr("$gzip_error"), "  $name";
423b39c5158Smillert        like $GzipError, "/$gzip_error/", "  $name";
424b39c5158Smillert
425b39c5158Smillert        ok ! $x, "  IO::Compress::Gzip fails";
426b39c5158Smillert        like $GzipError, "/$gzip_error/", "  $name";
427b39c5158Smillert
428b39c5158Smillert        foreach my $check (0, 1)
429b39c5158Smillert        {
430256a93a4Safresh1            ok $x = IO::Compress::Gzip->new( \$buffer,
431b39c5158Smillert                                           ExtraField => $input,
432256a93a4Safresh1                                           Strict     => 0 )
433b39c5158Smillert                or diag "GzipError is $GzipError" ;
434b39c5158Smillert            my $string = "abcd" ;
435b39c5158Smillert            $x->write($string) ;
436b39c5158Smillert            $x->close ;
437b39c5158Smillert            is anyUncompress(\$buffer), $string ;
438b39c5158Smillert
439256a93a4Safresh1            $x = IO::Uncompress::Gunzip->new( \$buffer,
440b39c5158Smillert                                       Strict      => 0,
441b39c5158Smillert                                       Transparent => 0,
442256a93a4Safresh1                                       ParseExtra  => $check );
443b39c5158Smillert            if ($check) {
444b39c5158Smillert                ok ! $x ;
445b39c5158Smillert                like $GunzipError, "/^$gunzip_error/";
446b39c5158Smillert            }
447b39c5158Smillert            else {
448b39c5158Smillert                ok $x ;
449b39c5158Smillert            }
450b39c5158Smillert
451b39c5158Smillert        }
452b39c5158Smillert    }
453b39c5158Smillert}
454b39c5158Smillert
455b39c5158Smillert
456b39c5158Smillert{
457b39c5158Smillert    title 'Check Minimal';
458b39c5158Smillert
459256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( $name, -Minimal => 1 );
460b39c5158Smillert    my $string = "abcd" ;
461b39c5158Smillert    ok $x->write($string) ;
462b39c5158Smillert    ok $x->close ;
463b39c5158Smillert    #is GZreadFile($name), $string ;
464b39c5158Smillert
465256a93a4Safresh1    ok $x = IO::Uncompress::Gunzip->new( $name );
466b39c5158Smillert    my $hdr = $x->getHeaderInfo();
467b39c5158Smillert    ok $hdr;
468b39c5158Smillert    ok $hdr->{Time} == 0;
469b39c5158Smillert    is $hdr->{ExtraFlags}, 0;
470b39c5158Smillert    ok ! defined $hdr->{Name} ;
471b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
472b39c5158Smillert    ok ! defined $hdr->{Comment} ;
473b39c5158Smillert    is $hdr->{OsName}, 'Unknown' ;
474b39c5158Smillert    is $hdr->{MethodName}, "Deflated";
475b39c5158Smillert    is $hdr->{Flags}, 0;
476b39c5158Smillert    ok $hdr->{isMinimalHeader} ;
477b39c5158Smillert    ok ! $hdr->{TextFlag} ;
478b39c5158Smillert    ok $x->close ;
479b39c5158Smillert}
480b39c5158Smillert
481b39c5158Smillert{
482898184e3Ssthen    title "Check Minimal + no compressed data";
483b39c5158Smillert    # This is the smallest possible gzip file (20 bytes)
484b39c5158Smillert
485256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( $name, -Minimal => 1 );
486898184e3Ssthen    isa_ok $x, "IO::Compress::Gzip";
487898184e3Ssthen    ok $x->close, "closed" ;
488b39c5158Smillert
489256a93a4Safresh1    ok $x = IO::Uncompress::Gunzip->new( $name, -Append => 0 );
490898184e3Ssthen    isa_ok $x, "IO::Uncompress::Gunzip";
491b39c5158Smillert    my $data ;
492b39c5158Smillert    my $status  = 1;
493b39c5158Smillert
494898184e3Ssthen    ok $x->eof(), "eof" ;
495b39c5158Smillert    $status = $x->read($data)
496b39c5158Smillert        while $status >  0;
497898184e3Ssthen    is $status, 0, "status == 0" ;
498898184e3Ssthen    is $data, '', "empty string";
499898184e3Ssthen    ok ! $x->error(), "no error" ;
500898184e3Ssthen    ok $x->eof(), "eof" ;
501b39c5158Smillert
502b39c5158Smillert    my $hdr = $x->getHeaderInfo();
503b39c5158Smillert    ok $hdr;
504b39c5158Smillert
505b39c5158Smillert    ok defined $hdr->{ISIZE} ;
506b39c5158Smillert    is $hdr->{ISIZE}, 0;
507b39c5158Smillert
508b39c5158Smillert    ok defined $hdr->{CRC32} ;
509b39c5158Smillert    is $hdr->{CRC32}, 0;
510b39c5158Smillert
511b39c5158Smillert    is $hdr->{Time}, 0;
512b39c5158Smillert    ok ! defined $hdr->{Name} ;
513b39c5158Smillert    ok ! defined $hdr->{ExtraFieldRaw} ;
514b39c5158Smillert    ok ! defined $hdr->{Comment} ;
515b39c5158Smillert    is $hdr->{OsName}, 'Unknown' ;
516b39c5158Smillert    is $hdr->{MethodName}, "Deflated";
517b39c5158Smillert    is $hdr->{Flags}, 0;
518b39c5158Smillert    ok $hdr->{isMinimalHeader} ;
519b39c5158Smillert    ok ! $hdr->{TextFlag} ;
520b39c5158Smillert    ok $x->close ;
521b39c5158Smillert}
522b39c5158Smillert
523b39c5158Smillert{
524898184e3Ssthen    title "Header Corruption Tests";
525b39c5158Smillert
526b39c5158Smillert    my $string = <<EOM;
527b39c5158Smillertsome text
528b39c5158SmillertEOM
529b39c5158Smillert
530b39c5158Smillert    my $good = '';
531256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( \$good, -HeaderCRC => 1 );
532b39c5158Smillert    ok $x->write($string) ;
533b39c5158Smillert    ok $x->close ;
534b39c5158Smillert
535b39c5158Smillert    {
536b39c5158Smillert        title "Header Corruption - Fingerprint wrong 1st byte" ;
537b39c5158Smillert        my $buffer = $good ;
538b39c5158Smillert        substr($buffer, 0, 1) = 'x' ;
539b39c5158Smillert
540256a93a4Safresh1        ok ! IO::Uncompress::Gunzip->new( \$buffer, -Transparent => 0 );
541b39c5158Smillert        ok $GunzipError =~ /Header Error: Bad Magic/;
542b39c5158Smillert    }
543b39c5158Smillert
544b39c5158Smillert    {
545b39c5158Smillert        title "Header Corruption - Fingerprint wrong 2nd byte" ;
546b39c5158Smillert        my $buffer = $good ;
547b39c5158Smillert        substr($buffer, 1, 1) = "\xFF" ;
548b39c5158Smillert
549256a93a4Safresh1        ok ! IO::Uncompress::Gunzip->new( \$buffer, -Transparent => 0 );
550b39c5158Smillert        ok $GunzipError =~ /Header Error: Bad Magic/;
551b39c5158Smillert        #print "$GunzipError\n";
552b39c5158Smillert    }
553b39c5158Smillert
554b39c5158Smillert    {
555b39c5158Smillert        title "Header Corruption - CM not 8";
556b39c5158Smillert        my $buffer = $good ;
557b39c5158Smillert        substr($buffer, 2, 1) = 'x' ;
558b39c5158Smillert
559256a93a4Safresh1        ok ! IO::Uncompress::Gunzip->new( \$buffer, -Transparent => 0 );
560b39c5158Smillert        like $GunzipError, '/Header Error: Not Deflate \(CM is \d+\)/';
561b39c5158Smillert    }
562b39c5158Smillert
563b39c5158Smillert    {
564b39c5158Smillert        title "Header Corruption - Use of Reserved Flags";
565b39c5158Smillert        my $buffer = $good ;
566b39c5158Smillert        substr($buffer, 3, 1) = "\xff";
567b39c5158Smillert
568256a93a4Safresh1        ok ! IO::Uncompress::Gunzip->new( \$buffer, -Transparent => 0 );
569b39c5158Smillert        like $GunzipError, '/Header Error: Use of Reserved Bits in FLG field./';
570b39c5158Smillert    }
571b39c5158Smillert
572b39c5158Smillert    {
573b39c5158Smillert        title "Header Corruption - Fail HeaderCRC";
574b39c5158Smillert        my $buffer = $good ;
575b39c5158Smillert        substr($buffer, 10, 1) = chr((ord(substr($buffer, 10, 1)) + 1) & 0xFF);
576b39c5158Smillert
577256a93a4Safresh1        ok ! IO::Uncompress::Gunzip->new( \$buffer, -Transparent => 0, Strict => 1 )
578b39c5158Smillert         or print "# $GunzipError\n";
579b39c5158Smillert        like $GunzipError, '/Header Error: CRC16 mismatch/'
580b39c5158Smillert            #or diag "buffer length " . length($buffer);
581b39c5158Smillert            or hexDump(\$good), hexDump(\$buffer);
582b39c5158Smillert    }
583b39c5158Smillert}
584b39c5158Smillert
585b39c5158Smillert{
586b39c5158Smillert    title "ExtraField max raw size";
587b39c5158Smillert    my $x ;
588b39c5158Smillert    my $store = "x" x GZIP_FEXTRA_MAX_SIZE ;
589898184e3Ssthen    {
590256a93a4Safresh1        my $z = IO::Compress::Gzip->new(\$x, ExtraField => $store, Strict => 0) ;
591b39c5158Smillert        ok $z,  "Created IO::Compress::Gzip object" ;
592898184e3Ssthen    }
593256a93a4Safresh1    my $gunz = IO::Uncompress::Gunzip->new( \$x, Strict => 0 );
594b39c5158Smillert    ok $gunz, "Created IO::Uncompress::Gunzip object" ;
595b39c5158Smillert    my $hdr = $gunz->getHeaderInfo();
596b39c5158Smillert    ok $hdr;
597b39c5158Smillert
598b39c5158Smillert    is $hdr->{ExtraFieldRaw}, $store ;
599b39c5158Smillert}
600b39c5158Smillert
601b39c5158Smillert{
602b39c5158Smillert    title "Header Corruption - ExtraField too big";
603b39c5158Smillert    my $x;
604256a93a4Safresh1    eval { IO::Compress::Gzip->new(\$x, -ExtraField => "x" x (GZIP_FEXTRA_MAX_SIZE + 1)) ;};
605b39c5158Smillert    like $@, mkErr('Error with ExtraField Parameter: Too Large');
606b39c5158Smillert    like $GzipError, '/Error with ExtraField Parameter: Too Large/';
607b39c5158Smillert}
608b39c5158Smillert
609b39c5158Smillert{
610b39c5158Smillert    title "Header Corruption - Create Name with Illegal Chars";
611b39c5158Smillert
612b39c5158Smillert    my $x;
613256a93a4Safresh1    eval { IO::Compress::Gzip->new( \$x, -Name => "fred\x02" ) };
614b39c5158Smillert    like $@, mkErr('Non ISO 8859-1 Character found in Name');
615b39c5158Smillert    like $GzipError, '/Non ISO 8859-1 Character found in Name/';
616b39c5158Smillert
617256a93a4Safresh1    ok  my $gz = IO::Compress::Gzip->new( \$x,
618b39c5158Smillert		                      -Strict => 0,
619256a93a4Safresh1		                      -Name => "fred\x02" );
620b39c5158Smillert    ok $gz->close();
621b39c5158Smillert
622256a93a4Safresh1    ok ! IO::Uncompress::Gunzip->new( \$x,
623b39c5158Smillert                        -Transparent => 0,
624256a93a4Safresh1                        -Strict => 1 );
625b39c5158Smillert
626b39c5158Smillert    like $GunzipError, '/Header Error: Non ISO 8859-1 Character found in Name/';
627256a93a4Safresh1    ok my $gunzip = IO::Uncompress::Gunzip->new( \$x,
628256a93a4Safresh1                                   -Strict => 0 );
629b39c5158Smillert
630b39c5158Smillert    my $hdr = $gunzip->getHeaderInfo() ;
631b39c5158Smillert
632b39c5158Smillert    is $hdr->{Name}, "fred\x02";
633b39c5158Smillert
634b39c5158Smillert}
635b39c5158Smillert
636b39c5158Smillert{
637b39c5158Smillert    title "Header Corruption - Null Chars in Name";
638b39c5158Smillert    my $x;
639256a93a4Safresh1    eval { IO::Compress::Gzip->new( \$x, -Name => "\x00" ) };
640b39c5158Smillert    like $@, mkErr('Null Character found in Name');
641b39c5158Smillert    like $GzipError, '/Null Character found in Name/';
642b39c5158Smillert
643256a93a4Safresh1    eval { IO::Compress::Gzip->new( \$x, -Name => "abc\x00" ) };
644b39c5158Smillert    like $@, mkErr('Null Character found in Name');
645b39c5158Smillert    like $GzipError, '/Null Character found in Name/';
646b39c5158Smillert
647256a93a4Safresh1    ok my $gz = IO::Compress::Gzip->new( \$x,
648b39c5158Smillert		                     -Strict  => 0,
649256a93a4Safresh1		                     -Name => "abc\x00de" );
650b39c5158Smillert    ok $gz->close() ;
651256a93a4Safresh1    ok my $gunzip = IO::Uncompress::Gunzip->new( \$x,
652256a93a4Safresh1                                   -Strict => 0 );
653b39c5158Smillert
654b39c5158Smillert    my $hdr = $gunzip->getHeaderInfo() ;
655b39c5158Smillert
656b39c5158Smillert    is $hdr->{Name}, "abc";
657b39c5158Smillert
658b39c5158Smillert}
659b39c5158Smillert
660b39c5158Smillert{
661b39c5158Smillert    title "Header Corruption - Create Comment with Illegal Chars";
662b39c5158Smillert
663b39c5158Smillert    my $x;
664256a93a4Safresh1    eval { IO::Compress::Gzip->new( \$x, -Comment => "fred\x02" ) };
665b39c5158Smillert    like $@, mkErr('Non ISO 8859-1 Character found in Comment');
666b39c5158Smillert    like $GzipError, '/Non ISO 8859-1 Character found in Comment/';
667b39c5158Smillert
668256a93a4Safresh1    ok  my $gz = IO::Compress::Gzip->new( \$x,
669b39c5158Smillert		                      -Strict => 0,
670256a93a4Safresh1		                      -Comment => "fred\x02" );
671b39c5158Smillert    ok $gz->close();
672b39c5158Smillert
673256a93a4Safresh1    ok ! IO::Uncompress::Gunzip->new( \$x, Strict => 1,
674256a93a4Safresh1                        -Transparent => 0 );
675b39c5158Smillert
676b39c5158Smillert    like $GunzipError, '/Header Error: Non ISO 8859-1 Character found in Comment/';
677256a93a4Safresh1    ok my $gunzip = IO::Uncompress::Gunzip->new( \$x, Strict => 0 );
678b39c5158Smillert
679b39c5158Smillert    my $hdr = $gunzip->getHeaderInfo() ;
680b39c5158Smillert
681b39c5158Smillert    is $hdr->{Comment}, "fred\x02";
682b39c5158Smillert
683b39c5158Smillert}
684b39c5158Smillert
685b39c5158Smillert{
686b39c5158Smillert    title "Header Corruption - Null Char in Comment";
687b39c5158Smillert    my $x;
688256a93a4Safresh1    eval { IO::Compress::Gzip->new( \$x, -Comment => "\x00" ) };
689b39c5158Smillert    like $@, mkErr('Null Character found in Comment');
690b39c5158Smillert    like $GzipError, '/Null Character found in Comment/';
691b39c5158Smillert
692256a93a4Safresh1    eval { IO::Compress::Gzip->new( \$x, -Comment => "abc\x00" ) } ;
693b39c5158Smillert    like $@, mkErr('Null Character found in Comment');
694b39c5158Smillert    like $GzipError, '/Null Character found in Comment/';
695b39c5158Smillert
696256a93a4Safresh1    ok my $gz = IO::Compress::Gzip->new( \$x,
697b39c5158Smillert		                     -Strict  => 0,
698256a93a4Safresh1		                     -Comment => "abc\x00de" );
699b39c5158Smillert    ok $gz->close() ;
700256a93a4Safresh1    ok my $gunzip = IO::Uncompress::Gunzip->new( \$x,
701256a93a4Safresh1                                   -Strict => 0 );
702b39c5158Smillert
703b39c5158Smillert    my $hdr = $gunzip->getHeaderInfo() ;
704b39c5158Smillert
705b39c5158Smillert    is $hdr->{Comment}, "abc";
706b39c5158Smillert
707b39c5158Smillert}
708b39c5158Smillert
709b39c5158Smillert
710b39c5158Smillertfor my $index ( GZIP_MIN_HEADER_SIZE + 1 ..  GZIP_MIN_HEADER_SIZE + GZIP_FEXTRA_HEADER_SIZE + 1)
711b39c5158Smillert{
712b39c5158Smillert    title "Header Corruption - Truncated in Extra";
713b39c5158Smillert    my $string = <<EOM;
714b39c5158Smillertsome text
715b39c5158SmillertEOM
716b39c5158Smillert
717b39c5158Smillert    my $truncated ;
718256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( \$truncated, -HeaderCRC => 1, Strict => 0,
719256a93a4Safresh1				-ExtraField => "hello" x 10 );
720b39c5158Smillert    ok $x->write($string) ;
721b39c5158Smillert    ok $x->close ;
722b39c5158Smillert
723b39c5158Smillert    substr($truncated, $index) = '' ;
724256a93a4Safresh1    #my $lex = LexFile->new( my $name );
725b39c5158Smillert    #writeFile($name, $truncated) ;
726b39c5158Smillert
727256a93a4Safresh1    #my $g = IO::Uncompress::Gunzip->new( $name, -Transparent => 0 );
728256a93a4Safresh1    my $g = IO::Uncompress::Gunzip->new( \$truncated, -Transparent => 0 );
729b39c5158Smillert    ok ! $g
730b39c5158Smillert	or print "# $g\n" ;
731b39c5158Smillert
732b39c5158Smillert    like($GunzipError, '/^Header Error: Truncated in FEXTRA/');
733b39c5158Smillert
734b39c5158Smillert
735b39c5158Smillert}
736b39c5158Smillert
737b39c5158Smillertmy $Name = "fred" ;
738b39c5158Smillert    my $truncated ;
739b39c5158Smillertfor my $index ( GZIP_MIN_HEADER_SIZE ..  GZIP_MIN_HEADER_SIZE + length($Name) -1)
740b39c5158Smillert{
741b39c5158Smillert    title "Header Corruption - Truncated in Name";
742b39c5158Smillert    my $string = <<EOM;
743b39c5158Smillertsome text
744b39c5158SmillertEOM
745b39c5158Smillert
746b39c5158Smillert    my $truncated ;
747256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( \$truncated, -Name => $Name );
748b39c5158Smillert    ok $x->write($string) ;
749b39c5158Smillert    ok $x->close ;
750b39c5158Smillert
751b39c5158Smillert    substr($truncated, $index) = '' ;
752b39c5158Smillert
753256a93a4Safresh1    my $g = IO::Uncompress::Gunzip->new( \$truncated, -Transparent => 0 );
754b39c5158Smillert    ok ! $g
755b39c5158Smillert	or print "# $g\n" ;
756b39c5158Smillert
757b39c5158Smillert    like $GunzipError, '/^Header Error: Truncated in FNAME Section/';
758b39c5158Smillert
759b39c5158Smillert}
760b39c5158Smillert
761b39c5158Smillertmy $Comment = "comment" ;
762b39c5158Smillertfor my $index ( GZIP_MIN_HEADER_SIZE ..  GZIP_MIN_HEADER_SIZE + length($Comment) -1)
763b39c5158Smillert{
764b39c5158Smillert    title "Header Corruption - Truncated in Comment";
765b39c5158Smillert    my $string = <<EOM;
766b39c5158Smillertsome text
767b39c5158SmillertEOM
768b39c5158Smillert
769b39c5158Smillert    my $truncated ;
770256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( \$truncated, -Comment => $Comment );
771b39c5158Smillert    ok $x->write($string) ;
772b39c5158Smillert    ok $x->close ;
773b39c5158Smillert
774b39c5158Smillert    substr($truncated, $index) = '' ;
775256a93a4Safresh1    #my $lex = LexFile->new( my $name );
776b39c5158Smillert    #writeFile($name, $truncated) ;
777b39c5158Smillert
778256a93a4Safresh1    #my $g = IO::Uncompress::Gunzip->new( $name, -Transparent => 0 );
779256a93a4Safresh1    my $g = IO::Uncompress::Gunzip->new( \$truncated, -Transparent => 0 );
780b39c5158Smillert    ok ! $g
781b39c5158Smillert	or print "# $g\n" ;
782b39c5158Smillert
783b39c5158Smillert    like $GunzipError, '/^Header Error: Truncated in FCOMMENT Section/';
784b39c5158Smillert
785b39c5158Smillert}
786b39c5158Smillert
787b39c5158Smillertfor my $index ( GZIP_MIN_HEADER_SIZE ..  GZIP_MIN_HEADER_SIZE + GZIP_FHCRC_SIZE -1)
788b39c5158Smillert{
789b39c5158Smillert    title "Header Corruption - Truncated in CRC";
790b39c5158Smillert    my $string = <<EOM;
791b39c5158Smillertsome text
792b39c5158SmillertEOM
793b39c5158Smillert
794b39c5158Smillert    my $truncated ;
795256a93a4Safresh1    ok my $x = IO::Compress::Gzip->new( \$truncated, -HeaderCRC => 1 );
796b39c5158Smillert    ok $x->write($string) ;
797b39c5158Smillert    ok $x->close ;
798b39c5158Smillert
799b39c5158Smillert    substr($truncated, $index) = '' ;
800256a93a4Safresh1    my $lex = LexFile->new( my $name );
801b39c5158Smillert    writeFile($name, $truncated) ;
802b39c5158Smillert
803256a93a4Safresh1    my $g = IO::Uncompress::Gunzip->new( $name, -Transparent => 0 );
804b39c5158Smillert    ok ! $g
805b39c5158Smillert	or print "# $g\n" ;
806b39c5158Smillert
807b39c5158Smillert    like $GunzipError, '/^Header Error: Truncated in FHCRC Section/';
808b39c5158Smillert
809b39c5158Smillert}
810b39c5158Smillert
811b39c5158Smillert
812b39c5158Smillert{
813b39c5158Smillert    # Trailer Corruption tests
814b39c5158Smillert
815b39c5158Smillert    my $string = <<EOM;
816b39c5158Smillertsome text
817b39c5158SmillertEOM
818898184e3Ssthen    $string = $string x 1000;
819b39c5158Smillert
820b39c5158Smillert    my $good ;
821b39c5158Smillert    {
822256a93a4Safresh1        ok my $x = IO::Compress::Gzip->new( \$good );
823b39c5158Smillert        ok $x->write($string) ;
824b39c5158Smillert        ok $x->close ;
825b39c5158Smillert    }
826b39c5158Smillert
827b39c5158Smillert    writeFile($name, $good) ;
828256a93a4Safresh1    ok my $gunz = IO::Uncompress::Gunzip->new( $name,
829b39c5158Smillert                                       -Append   => 1,
830256a93a4Safresh1                                       -Strict   => 1 );
831b39c5158Smillert    my $uncomp ;
832b39c5158Smillert    1 while  $gunz->read($uncomp) > 0 ;
833b39c5158Smillert    ok $gunz->close() ;
834b39c5158Smillert    ok $uncomp eq $string
835b39c5158Smillert	or print "# got [$uncomp] wanted [$string]\n";;
836b39c5158Smillert
837b39c5158Smillert    foreach my $trim (-8 .. -1)
838b39c5158Smillert    {
839b39c5158Smillert        my $got = $trim + 8 ;
840b39c5158Smillert        title "Trailer Corruption - Trailer truncated to $got bytes" ;
841b39c5158Smillert        my $buffer = $good ;
842b39c5158Smillert        my $expected_trailing = substr($good, -8, 8) ;
843b39c5158Smillert        substr($expected_trailing, $trim) = '';
844b39c5158Smillert
845b39c5158Smillert        substr($buffer, $trim) = '';
846b39c5158Smillert        writeFile($name, $buffer) ;
847b39c5158Smillert
848b39c5158Smillert        foreach my $strict (0, 1)
849b39c5158Smillert        {
850256a93a4Safresh1            ok my $gunz = IO::Uncompress::Gunzip->new( $name, Append => 1, -Strict   => $strict );
851b39c5158Smillert            my $uncomp ;
852898184e3Ssthen            my $status = 1;
853898184e3Ssthen            $status = $gunz->read($uncomp) while $status > 0;
854b39c5158Smillert            if ($strict)
855b39c5158Smillert            {
856f3efcd01Safresh1                cmp_ok $status, '<', 0, "status 0" ;
857f3efcd01Safresh1                like $GunzipError, "/Trailer Error: trailer truncated. Expected 8 bytes, got $got/", "got Trailer Error";
858b39c5158Smillert            }
859b39c5158Smillert            else
860b39c5158Smillert            {
861898184e3Ssthen                is $status, 0, "status 0";
862898184e3Ssthen                ok ! $GunzipError, "no error"
863898184e3Ssthen                    or diag "$GunzipError";
864b39c5158Smillert                my $expected = substr($buffer, - $got);
865898184e3Ssthen                is  $gunz->trailingData(),  $expected_trailing, "trailing data";
866b39c5158Smillert            }
867b39c5158Smillert            ok $gunz->eof() ;
868b39c5158Smillert            ok $uncomp eq $string;
869b39c5158Smillert            ok $gunz->close ;
870b39c5158Smillert        }
871b39c5158Smillert
872b39c5158Smillert    }
873b39c5158Smillert
874b39c5158Smillert    {
875b39c5158Smillert        title "Trailer Corruption - Length Wrong, CRC Correct" ;
876b39c5158Smillert        my $buffer = $good ;
877b39c5158Smillert        my $actual_len = unpack("V", substr($buffer, -4, 4));
878b39c5158Smillert        substr($buffer, -4, 4) = pack('V', $actual_len + 1);
879b39c5158Smillert        writeFile($name, $buffer) ;
880b39c5158Smillert
881b39c5158Smillert        foreach my $strict (0, 1)
882b39c5158Smillert        {
883256a93a4Safresh1            ok my $gunz = IO::Uncompress::Gunzip->new( $name,
884898184e3Ssthen                                               Append   => 1,
885256a93a4Safresh1                                               -Strict   => $strict );
886b39c5158Smillert            my $uncomp ;
887898184e3Ssthen            my $status = 1;
888898184e3Ssthen            $status = $gunz->read($uncomp) while $status > 0;
889b39c5158Smillert            if ($strict)
890b39c5158Smillert            {
891898184e3Ssthen                cmp_ok $status, '<', 0 ;
892b39c5158Smillert                my $got_len = $actual_len + 1;
893b39c5158Smillert                like $GunzipError, "/Trailer Error: ISIZE mismatch. Got $got_len, expected $actual_len/";
894b39c5158Smillert            }
895b39c5158Smillert            else
896b39c5158Smillert            {
897898184e3Ssthen                is $status, 0;
898b39c5158Smillert                ok ! $GunzipError ;
899b39c5158Smillert                #is   $gunz->trailingData(), substr($buffer, - $got) ;
900b39c5158Smillert            }
901b39c5158Smillert            ok ! $gunz->trailingData() ;
902b39c5158Smillert            ok $gunz->eof() ;
903b39c5158Smillert            ok $uncomp eq $string;
904b39c5158Smillert            ok $gunz->close ;
905b39c5158Smillert        }
906b39c5158Smillert
907b39c5158Smillert    }
908b39c5158Smillert
909b39c5158Smillert    {
910b39c5158Smillert        title "Trailer Corruption - Length Correct, CRC Wrong" ;
911b39c5158Smillert        my $buffer = $good ;
912b39c5158Smillert        my $actual_crc = unpack("V", substr($buffer, -8, 4));
913b39c5158Smillert        substr($buffer, -8, 4) = pack('V', $actual_crc+1);
914b39c5158Smillert        writeFile($name, $buffer) ;
915b39c5158Smillert
916b39c5158Smillert        foreach my $strict (0, 1)
917b39c5158Smillert        {
918256a93a4Safresh1            ok my $gunz = IO::Uncompress::Gunzip->new( $name,
919898184e3Ssthen                                               -Append   => 1,
920256a93a4Safresh1                                               -Strict   => $strict );
921b39c5158Smillert            my $uncomp ;
922898184e3Ssthen            my $status = 1;
923898184e3Ssthen            $status = $gunz->read($uncomp) while $status > 0;
924b39c5158Smillert            if ($strict)
925b39c5158Smillert            {
926898184e3Ssthen                cmp_ok $status, '<', 0 ;
927b39c5158Smillert                like $GunzipError, '/Trailer Error: CRC mismatch/';
928b39c5158Smillert            }
929b39c5158Smillert            else
930b39c5158Smillert            {
931898184e3Ssthen                is $status, 0;
932b39c5158Smillert                ok ! $GunzipError ;
933b39c5158Smillert            }
934b39c5158Smillert            ok ! $gunz->trailingData() ;
935b39c5158Smillert            ok $gunz->eof() ;
936b39c5158Smillert            ok $uncomp eq $string;
937b39c5158Smillert            ok $gunz->close ;
938b39c5158Smillert        }
939b39c5158Smillert
940b39c5158Smillert    }
941b39c5158Smillert
942b39c5158Smillert    {
943b39c5158Smillert        title "Trailer Corruption - Length Wrong, CRC Wrong" ;
944b39c5158Smillert        my $buffer = $good ;
945b39c5158Smillert        my $actual_len = unpack("V", substr($buffer, -4, 4));
946b39c5158Smillert        my $actual_crc = unpack("V", substr($buffer, -8, 4));
947b39c5158Smillert        substr($buffer, -4, 4) = pack('V', $actual_len+1);
948b39c5158Smillert        substr($buffer, -8, 4) = pack('V', $actual_crc+1);
949b39c5158Smillert        writeFile($name, $buffer) ;
950b39c5158Smillert
951b39c5158Smillert        foreach my $strict (0, 1)
952b39c5158Smillert        {
953256a93a4Safresh1            ok my $gunz = IO::Uncompress::Gunzip->new( $name,
954898184e3Ssthen                                               -Append   => 1,
955256a93a4Safresh1                                               -Strict   => $strict );
956b39c5158Smillert            my $uncomp ;
957898184e3Ssthen            my $status = 1;
958898184e3Ssthen            $status = $gunz->read($uncomp) while $status > 0;
959b39c5158Smillert            if ($strict)
960b39c5158Smillert            {
961898184e3Ssthen                cmp_ok $status, '<', 0 ;
962b39c5158Smillert                like $GunzipError, '/Trailer Error: CRC mismatch/';
963b39c5158Smillert            }
964b39c5158Smillert            else
965b39c5158Smillert            {
966898184e3Ssthen                is $status, 0;
967b39c5158Smillert                ok ! $GunzipError ;
968b39c5158Smillert            }
969b39c5158Smillert            ok $gunz->eof() ;
970b39c5158Smillert            ok $uncomp eq $string;
971b39c5158Smillert            ok $gunz->close ;
972b39c5158Smillert        }
973b39c5158Smillert
974b39c5158Smillert    }
975898184e3Ssthen
976898184e3Ssthen    {
977898184e3Ssthen        # RT #72329
978898184e3Ssthen        my $error = 'Error with ExtraField Parameter: ' .
979898184e3Ssthen                    'SubField ID not two chars long' ;
980898184e3Ssthen        my $buffer ;
981898184e3Ssthen        my $x ;
982256a93a4Safresh1        eval { $x = IO::Compress::Gzip->new( \$buffer,
983256a93a4Safresh1                -ExtraField  => [ at => 'mouse', bad => 'dog'] );
984898184e3Ssthen             };
985898184e3Ssthen        like $@, mkErr("$error");
986898184e3Ssthen        like $GzipError, "/$error/";
987898184e3Ssthen        ok ! $x ;
988898184e3Ssthen    }
989b39c5158Smillert}
990b39c5158Smillert
991b39c5158Smillert
992b39c5158Smillert
993