4 @INC = ("../lib", "lib/compress");
8 use lib qw(t t/compress);
19 # use Test::NoWarnings, if available
22 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
33 plan tests => $count + $extra ;
35 use_ok('Compress::Zlib', qw(:ALL memGunzip memGzip zlib_version));
36 use_ok('IO::Compress::Gzip::Constants') ;
38 use_ok('IO::Compress::Gzip', qw($GzipError)) ;
47 my $len = length $hello ;
49 # Check zlib_version and ZLIB_VERSION are the same.
51 skip "TEST_SKIP_VERSION_CHECK is set", 1
52 if $ENV{TEST_SKIP_VERSION_CHECK};
53 is Compress::Zlib::zlib_version, ZLIB_VERSION,
54 "ZLIB_VERSION matches Compress::Zlib::zlib_version" ;
57 # generate a long random string
60 { $contents .= chr int rand 256 }
65 # compress/uncompress tests
66 # =========================
68 eval { compress([1]); };
69 ok $@ =~ m#not a scalar reference#
72 eval { uncompress([1]); };
73 ok $@ =~ m#not a scalar reference#
76 $hello = "hello mum" ;
77 my $keep_hello = $hello ;
79 my $compr = compress($hello) ;
82 my $keep_compr = $compr ;
84 my $uncompr = uncompress ($compr) ;
86 ok $hello eq $uncompr ;
88 ok $hello eq $keep_hello ;
89 ok $compr eq $keep_compr ;
93 $keep_hello = $hello ;
95 $compr = compress($hello) ;
98 $keep_compr = $compr ;
100 $uncompr = uncompress ($compr) ;
102 ok $hello eq $uncompr ;
104 ok $hello eq $keep_hello ;
105 ok $compr eq $keep_compr ;
109 $compr = compress ($contents) ;
112 $uncompr = uncompress ($compr) ;
114 ok $contents eq $uncompr ;
118 $compr = compress(\$hello) ;
122 $uncompr = uncompress (\$compr) ;
123 ok $hello eq $uncompr ;
126 $compr = compress($hello, 1000) ;
130 $compr = compress($hello, Z_BEST_COMPRESSION) ;
132 $uncompr = uncompress (\$compr) ;
133 ok $hello eq $uncompr ;
136 $compr = compress(\$hello) ;
139 substr($compr,0, 1) = "\xFF";
140 ok !defined uncompress (\$compr) ;
142 # deflate/inflate - small buffer
143 # ==============================
145 $hello = "I am a HAL 9000 computer" ;
146 my @hello = split('', $hello) ;
147 my ($err, $X, $status);
149 ok (($x, $err) = deflateInit( {-Bufsize => 1} ) ) ;
156 ($X, $status) = $x->deflate($_) ;
157 last unless $status == Z_OK ;
164 ok ((($X, $status) = $x->flush())[1] == Z_OK ) ;
168 my @Answer = split('', $Answer) ;
171 ok (($k, $err) = inflateInit( {-Bufsize => 1}) ) ;
179 ($Z, $status) = $k->inflate($_) ;
181 last if $status == Z_STREAM_END or $status != Z_OK ;
185 ok $status == Z_STREAM_END ;
189 title 'deflate/inflate - small buffer with a number';
190 # ==============================
194 ok (($x, $err) = deflateInit( {-Bufsize => 1} ) ) ;
198 ok !defined $x->msg() ;
199 ok $x->total_in() == 0 ;
200 ok $x->total_out() == 0 ;
203 ($X, $status) = $x->deflate($hello) ;
210 ok ((($X, $status) = $x->flush())[1] == Z_OK ) ;
213 ok !defined $x->msg() ;
214 ok $x->total_in() == length $hello ;
215 ok $x->total_out() == length $Answer ;
218 @Answer = split('', $Answer) ;
220 ok (($k, $err) = inflateInit( {-Bufsize => 1}) ) ;
224 ok !defined $k->msg() ;
225 ok $k->total_in() == 0 ;
226 ok $k->total_out() == 0 ;
231 ($Z, $status) = $k->inflate($_) ;
233 last if $status == Z_STREAM_END or $status != Z_OK ;
237 ok $status == Z_STREAM_END ;
240 ok !defined $k->msg() ;
241 is $k->total_in(), length $Answer ;
242 ok $k->total_out() == length $hello ;
246 title 'deflate/inflate - larger buffer';
247 # ==============================
250 ok $x = deflateInit() ;
252 ok ((($X, $status) = $x->deflate($contents))[1] == Z_OK) ;
257 ok ((($X, $status) = $x->flush() )[1] == Z_OK ) ;
262 ok $k = inflateInit() ;
264 ($Z, $status) = $k->inflate($Y) ;
266 ok $status == Z_STREAM_END ;
269 title 'deflate/inflate - preset dictionary';
270 # ===================================
272 my $dictionary = "hello" ;
273 ok $x = deflateInit({-Level => Z_BEST_COMPRESSION,
274 -Dictionary => $dictionary}) ;
276 my $dictID = $x->dict_adler() ;
278 ($X, $status) = $x->deflate($hello) ;
280 ($Y, $status) = $x->flush() ;
285 ok $k = inflateInit(-Dictionary => $dictionary) ;
287 ($Z, $status) = $k->inflate($X);
288 ok $status == Z_STREAM_END ;
289 ok $k->dict_adler() == $dictID;
294 # ($Z, $status) = $k->inflate($X) ;
295 # last if $status == Z_STREAM_END or $status != Z_OK ;
296 #print "status=[$status] hello=[$hello] Z=[$Z]\n";
298 #ok $status == Z_STREAM_END ;
300 # or print "status=[$status] hello=[$hello] Z=[$Z]\n";
307 title 'inflate - check remaining buffer after Z_STREAM_END';
308 # ===================================================
311 ok $x = deflateInit(-Level => Z_BEST_COMPRESSION ) ;
313 ($X, $status) = $x->deflate($hello) ;
315 ($Y, $status) = $x->flush() ;
320 ok $k = inflateInit() ;
322 my $first = substr($X, 0, 2) ;
323 my $last = substr($X, 2) ;
324 ($Z, $status) = $k->inflate($first);
328 $last .= "appendage" ;
330 ($T, $status) = $k->inflate($last);
331 ok $status == Z_STREAM_END ;
332 ok $hello eq $Z . $T ;
333 ok $last eq "appendage" ;
337 title 'memGzip & memGunzip';
339 my ($name, $name1, $name2, $name3);
340 my $lex = new LexFile $name, $name1, $name2, $name3 ;
347 my $len = length $buffer ;
351 # create an in-memory gzip file
352 my $dest = memGzip($buffer) ;
357 ok open(FH, ">$name") ;
362 # uncompress with gzopen
363 ok my $fil = gzopen($name, "rb") ;
365 is $fil->gzread($uncomp, 0), 0 ;
366 ok (($x = $fil->gzread($uncomp)) == $len) ;
370 ok $uncomp eq $buffer ;
372 #1 while unlink $name ;
374 # now check that memGunzip can deal with it.
375 my $ungzip = memGunzip($dest) ;
377 ok $buffer eq $ungzip ;
380 # now do the same but use a reference
382 $dest = memGzip(\$buffer) ;
387 ok open(FH, ">$name1") ;
392 # uncompress with gzopen
393 ok $fil = gzopen($name1, "rb") ;
395 ok (($x = $fil->gzread($uncomp)) == $len) ;
399 ok $uncomp eq $buffer ;
401 # now check that memGunzip can deal with it.
403 $ungzip = memGunzip(\$dest) ;
406 ok $buffer eq $ungzip ;
408 # check memGunzip can cope with missing gzip trailer
409 my $minimal = substr($keep, 0, -1) ;
410 $ungzip = memGunzip(\$minimal) ;
412 ok $buffer eq $ungzip ;
415 $minimal = substr($keep, 0, -2) ;
416 $ungzip = memGunzip(\$minimal) ;
418 ok $buffer eq $ungzip ;
421 $minimal = substr($keep, 0, -3) ;
422 $ungzip = memGunzip(\$minimal) ;
424 ok $buffer eq $ungzip ;
427 $minimal = substr($keep, 0, -4) ;
428 $ungzip = memGunzip(\$minimal) ;
430 ok $buffer eq $ungzip ;
433 $minimal = substr($keep, 0, -5) ;
434 $ungzip = memGunzip(\$minimal) ;
436 ok $buffer eq $ungzip ;
439 $minimal = substr($keep, 0, -6) ;
440 $ungzip = memGunzip(\$minimal) ;
442 ok $buffer eq $ungzip ;
445 $minimal = substr($keep, 0, -7) ;
446 $ungzip = memGunzip(\$minimal) ;
448 ok $buffer eq $ungzip ;
451 $minimal = substr($keep, 0, -8) ;
452 $ungzip = memGunzip(\$minimal) ;
454 ok $buffer eq $ungzip ;
457 $minimal = substr($keep, 0, -9) ;
458 $ungzip = memGunzip(\$minimal) ;
459 ok ! defined $ungzip ;
460 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
463 #1 while unlink $name ;
465 # check corrupt header -- too short
467 my $result = memGunzip($dest) ;
468 ok !defined $result ;
469 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
471 # check corrupt header -- full of junk
473 $result = memGunzip($dest) ;
474 ok !defined $result ;
475 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
477 # corrupt header - 1st byte wrong
479 substr($bad, 0, 1) = "\xFF" ;
480 $ungzip = memGunzip(\$bad) ;
481 ok ! defined $ungzip ;
482 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
484 # corrupt header - 2st byte wrong
486 substr($bad, 1, 1) = "\xFF" ;
487 $ungzip = memGunzip(\$bad) ;
488 ok ! defined $ungzip ;
489 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
491 # corrupt header - method not deflated
493 substr($bad, 2, 1) = "\xFF" ;
494 $ungzip = memGunzip(\$bad) ;
495 ok ! defined $ungzip ;
496 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
498 # corrupt header - reserved bits used
500 substr($bad, 3, 1) = "\xFF" ;
501 $ungzip = memGunzip(\$bad) ;
502 ok ! defined $ungzip ;
503 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
505 # corrupt trailer - length wrong
507 substr($bad, -8, 4) = "\xFF" x 4 ;
508 $ungzip = memGunzip(\$bad) ;
509 ok ! defined $ungzip ;
510 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
512 # corrupt trailer - CRC wrong
514 substr($bad, -4, 4) = "\xFF" x 4 ;
515 $ungzip = memGunzip(\$bad) ;
516 ok ! defined $ungzip ;
517 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
521 title "Check all bytes can be handled";
523 my $lex = new LexFile my $name ;
524 my $data = join '', map { chr } 0x00 .. 0xFF;
525 $data .= "\r\nabd\r\n";
528 ok $fil = gzopen($name, "wb") ;
529 is $fil->gzwrite($data), length $data ;
530 ok ! $fil->gzclose();
533 ok $fil = gzopen($name, "rb") ;
534 is $fil->gzread($input), length $data ;
535 ok ! $fil->gzclose();
538 title "Check all bytes can be handled - transparent mode";
539 writeFile($name, $data);
540 ok $fil = gzopen($name, "rb") ;
541 is $fil->gzread($input), length $data ;
542 ok ! $fil->gzclose();
547 title 'memGunzip with a gzopen created file';
549 my $name = "test.gz" ;
556 ok $fil = gzopen($name, "wb") ;
558 ok $fil->gzwrite($buffer) == length $buffer ;
562 my $compr = readFile($name);
564 my $unc = memGunzip($compr) ;
568 1 while unlink $name ;
576 $hello = "Test test test test test";
577 @hello = split('', $hello) ;
579 ok (($x, $err) = deflateInit( -Bufsize => 1, -WindowBits => -MAX_WBITS() ) ) ;
586 ($X, $status) = $x->deflate($_) ;
587 last unless $status == Z_OK ;
594 ok ((($X, $status) = $x->flush())[1] == Z_OK ) ;
598 @Answer = split('', $Answer) ;
599 # Undocumented corner -- extra byte needed to get inflate to return
600 # Z_STREAM_END when done.
603 ok (($k, $err) = inflateInit(-Bufsize => 1, -WindowBits => -MAX_WBITS()) ) ;
610 ($Z, $status) = $k->inflate($_) ;
612 last if $status == Z_STREAM_END or $status != Z_OK ;
616 ok $status == Z_STREAM_END ;
624 # create a deflate stream with flush points
626 my $hello = "I am a HAL 9000 computer" x 2001 ;
627 my $goodbye = "Will I dream?" x 2010;
628 my ($err, $answer, $X, $status, $Answer);
630 ok (($x, $err) = deflateInit() ) ;
634 ($Answer, $status) = $x->deflate($hello) ;
637 # create a flush point
638 ok ((($X, $status) = $x->flush(Z_FULL_FLUSH))[1] == Z_OK ) ;
641 ($X, $status) = $x->deflate($goodbye) ;
645 ok ((($X, $status) = $x->flush())[1] == Z_OK ) ;
648 my ($first, @Answer) = split('', $Answer) ;
651 ok (($k, $err) = inflateInit()) ;
655 ($Z, $status) = $k->inflate($first) ;
658 # skip to the first flush point.
661 my $byte = shift @Answer;
662 $status = $k->inflateSync($byte) ;
663 last unless $status == Z_DATA_ERROR;
674 ($Z, $status) = $k->inflate($_) ;
675 $GOT .= $Z if defined $Z ;
676 # print "x $status\n";
677 last if $status == Z_STREAM_END or $status != Z_OK ;
681 # zlib 1.0.9 returns Z_STREAM_END here, all others return Z_DATA_ERROR
682 ok $status == Z_DATA_ERROR || $status == Z_STREAM_END ;
683 ok $GOT eq $goodbye ;
686 # Check inflateSync leaves good data in buffer
687 $Answer =~ /^(.)(.*)$/ ;
688 my ($initial, $rest) = ($1, $2);
691 ok (($k, $err) = inflateInit()) ;
695 ($Z, $status) = $k->inflate($initial) ;
698 $status = $k->inflateSync($rest) ;
701 ($GOT, $status) = $k->inflate($rest) ;
703 ok $status == Z_DATA_ERROR ;
704 ok $Z . $GOT eq $goodbye ;
710 my $hello = "I am a HAL 9000 computer" x 2001 ;
711 my $goodbye = "Will I dream?" x 2010;
712 my ($input, $err, $answer, $X, $status, $Answer);
714 ok (($x, $err) = deflateInit(-Level => Z_BEST_COMPRESSION,
715 -Strategy => Z_DEFAULT_STRATEGY) ) ;
719 ok $x->get_Level() == Z_BEST_COMPRESSION;
720 ok $x->get_Strategy() == Z_DEFAULT_STRATEGY;
722 ($Answer, $status) = $x->deflate($hello) ;
727 eval { $x->deflateParams() };
728 #like $@, mkErr("^Compress::Raw::Zlib::deflateParams needs Level and/or Strategy");
729 like $@, "/^Compress::Raw::Zlib::deflateParams needs Level and/or Strategy/";
731 eval { $x->deflateParams(-Joe => 3) };
732 like $@, "/^Compress::Raw::Zlib::deflateStream::deflateParams: unknown key value/";
733 #like $@, mkErr("^Compress::Raw::Zlib::deflateStream::deflateParams: unknown key value(s) Joe");
734 #ok $@ =~ /^Compress::Zlib::deflateStream::deflateParams: unknown key value\(s\) Joe at/
735 # or print "# $@\n" ;
737 ok $x->get_Level() == Z_BEST_COMPRESSION;
738 ok $x->get_Strategy() == Z_DEFAULT_STRATEGY;
740 # change both Level & Strategy
741 $status = $x->deflateParams(-Level => Z_BEST_SPEED, -Strategy => Z_HUFFMAN_ONLY) ;
744 ok $x->get_Level() == Z_BEST_SPEED;
745 ok $x->get_Strategy() == Z_HUFFMAN_ONLY;
747 ($X, $status) = $x->deflate($goodbye) ;
753 $status = $x->deflateParams(-Level => Z_NO_COMPRESSION) ;
756 ok $x->get_Level() == Z_NO_COMPRESSION;
757 ok $x->get_Strategy() == Z_HUFFMAN_ONLY;
759 ($X, $status) = $x->deflate($goodbye) ;
764 # change only Strategy
765 $status = $x->deflateParams(-Strategy => Z_FILTERED) ;
768 ok $x->get_Level() == Z_NO_COMPRESSION;
769 ok $x->get_Strategy() == Z_FILTERED;
771 ($X, $status) = $x->deflate($goodbye) ;
776 ok ((($X, $status) = $x->flush())[1] == Z_OK ) ;
779 my ($first, @Answer) = split('', $Answer) ;
782 ok (($k, $err) = inflateInit()) ;
786 ($Z, $status) = $k->inflate($Answer) ;
788 ok $status == Z_STREAM_END
789 or print "# status $status\n";
796 eval { deflateInit(-Level) };
797 like $@, '/^Compress::Zlib::deflateInit: Expected even number of parameters, got 1/';
799 eval { inflateInit(-Level) };
800 like $@, '/^Compress::Zlib::inflateInit: Expected even number of parameters, got 1/';
802 eval { deflateInit(-Joe => 1) };
803 ok $@ =~ /^Compress::Zlib::deflateInit: unknown key value\(s\) Joe at/;
805 eval { inflateInit(-Joe => 1) };
806 ok $@ =~ /^Compress::Zlib::inflateInit: unknown key value\(s\) Joe at/;
808 eval { deflateInit(-Bufsize => 0) };
809 ok $@ =~ /^.*?: Bufsize must be >= 1, you specified 0 at/;
811 eval { inflateInit(-Bufsize => 0) };
812 ok $@ =~ /^.*?: Bufsize must be >= 1, you specified 0 at/;
814 eval { deflateInit(-Bufsize => -1) };
815 #ok $@ =~ /^.*?: Bufsize must be >= 1, you specified -1 at/;
816 ok $@ =~ /^Compress::Zlib::deflateInit: Parameter 'Bufsize' must be an unsigned int, got '-1'/;
818 eval { inflateInit(-Bufsize => -1) };
819 ok $@ =~ /^Compress::Zlib::inflateInit: Parameter 'Bufsize' must be an unsigned int, got '-1'/;
821 eval { deflateInit(-Bufsize => "xxx") };
822 ok $@ =~ /^Compress::Zlib::deflateInit: Parameter 'Bufsize' must be an unsigned int, got 'xxx'/;
824 eval { inflateInit(-Bufsize => "xxx") };
825 ok $@ =~ /^Compress::Zlib::inflateInit: Parameter 'Bufsize' must be an unsigned int, got 'xxx'/;
827 eval { gzopen([], 0) ; } ;
828 ok $@ =~ /^gzopen: file parameter is not a filehandle or filename at/
831 # my $x = Symbol::gensym() ;
832 # eval { gzopen($x, 0) ; } ;
833 # ok $@ =~ /^gzopen: file parameter is not a filehandle or filename at/
834 # or print "# $@\n" ;
840 # test inflate with a substr
842 ok my $x = deflateInit() ;
844 ok ((my ($X, $status) = $x->deflate($contents))[1] == Z_OK) ;
850 ok ((($X, $status) = $x->flush() )[1] == Z_OK ) ;
853 my $append = "Appended" ;
856 ok $k = inflateInit() ;
858 #($Z, $status) = $k->inflate(substr($Y, 0, -1)) ;
859 ($Z, $status) = $k->inflate(substr($Y, 0)) ;
861 ok $status == Z_STREAM_END ;
869 # deflate/inflate in scalar context
871 ok my $x = deflateInit() ;
873 my $X = $x->deflate($contents);
882 my $append = "Appended" ;
885 ok $k = inflateInit() ;
887 $Z = $k->inflate(substr($Y, 0, -1)) ;
888 #$Z = $k->inflate(substr($Y, 0)) ;
898 # CRC32 of this data should have the high bit set
899 # value in ascii is ZgRNtjgSUW
900 my $data = "\x5a\x67\x52\x4e\x74\x6a\x67\x53\x55\x57";
901 my $expected_crc = 0xCF707A2B ; # 3480255019
903 my $crc = crc32($data) ;
904 is $crc, $expected_crc;
910 # adler of this data should have the high bit set
911 # value in ascii is lpscOVsAJiUfNComkOfWYBcPhHZ[bT
912 my $data = "\x6c\x70\x73\x63\x4f\x56\x73\x41\x4a\x69\x55\x66" .
913 "\x4e\x43\x6f\x6d\x6b\x4f\x66\x57\x59\x42\x63\x50" .
914 "\x68\x48\x5a\x5b\x62\x54";
915 my $expected_crc = 0xAAD60AC7 ; # 2866154183
916 my $crc = adler32($data) ;
917 is $crc, $expected_crc;
921 # memGunzip - input > 4K
925 { $contents .= chr int rand 256 }
927 ok my $compressed = memGzip(\$contents) ;
930 ok length $compressed > 4096 ;
931 ok my $out = memGunzip(\$compressed) ;
934 ok $contents eq $out ;
935 is length $out, length $contents ;
942 # memGunzip Header Corruption Tests
949 ok my $x = new IO::Compress::Gzip \$good, Append => 1, -HeaderCRC => 1 ;
950 ok $x->write($string) ;
954 title "Header Corruption - Fingerprint wrong 1st byte" ;
956 substr($buffer, 0, 1) = 'x' ;
958 ok ! memGunzip(\$buffer) ;
959 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
963 title "Header Corruption - Fingerprint wrong 2nd byte" ;
965 substr($buffer, 1, 1) = "\xFF" ;
967 ok ! memGunzip(\$buffer) ;
968 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
972 title "Header Corruption - CM not 8";
974 substr($buffer, 2, 1) = 'x' ;
976 ok ! memGunzip(\$buffer) ;
977 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
981 title "Header Corruption - Use of Reserved Flags";
983 substr($buffer, 3, 1) = "\xff";
985 ok ! memGunzip(\$buffer) ;
986 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
991 for my $index ( GZIP_MIN_HEADER_SIZE + 1 .. GZIP_MIN_HEADER_SIZE + GZIP_FEXTRA_HEADER_SIZE + 1)
993 title "Header Corruption - Truncated in Extra";
999 ok my $x = new IO::Compress::Gzip \$truncated, Append => 1, -HeaderCRC => 1, Strict => 0,
1000 -ExtraField => "hello" x 10 ;
1001 ok $x->write($string) ;
1004 substr($truncated, $index) = '' ;
1006 ok ! memGunzip(\$truncated) ;
1007 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
1013 for my $index ( GZIP_MIN_HEADER_SIZE .. GZIP_MIN_HEADER_SIZE + length($Name) -1)
1015 title "Header Corruption - Truncated in Name";
1021 ok my $x = new IO::Compress::Gzip \$truncated, Append => 1, -Name => $Name;
1022 ok $x->write($string) ;
1025 substr($truncated, $index) = '' ;
1027 ok ! memGunzip(\$truncated) ;
1028 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
1031 my $Comment = "comment" ;
1032 for my $index ( GZIP_MIN_HEADER_SIZE .. GZIP_MIN_HEADER_SIZE + length($Comment) -1)
1034 title "Header Corruption - Truncated in Comment";
1040 ok my $x = new IO::Compress::Gzip \$truncated, -Comment => $Comment;
1041 ok $x->write($string) ;
1044 substr($truncated, $index) = '' ;
1045 ok ! memGunzip(\$truncated) ;
1046 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
1049 for my $index ( GZIP_MIN_HEADER_SIZE .. GZIP_MIN_HEADER_SIZE + GZIP_FHCRC_SIZE -1)
1051 title "Header Corruption - Truncated in CRC";
1057 ok my $x = new IO::Compress::Gzip \$truncated, -HeaderCRC => 1;
1058 ok $x->write($string) ;
1061 substr($truncated, $index) = '' ;
1063 ok ! memGunzip(\$truncated) ;
1064 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
1068 title "memGunzip can cope with a gzip header with all possible fields";
1074 ok my $x = new IO::Compress::Gzip \$buffer,
1079 -ExtraField => "Extra",
1080 -Comment => 'Comment';
1081 ok $x->write($string) ;
1084 ok defined $buffer ;
1086 ok my $got = memGunzip($buffer)
1087 or diag "gzerrno is $gzerrno" ;
1094 # Trailer Corruption tests
1101 ok my $x = new IO::Compress::Gzip \$good, Append => 1 ;
1102 ok $x->write($string) ;
1105 foreach my $trim (-8 .. -1)
1107 my $got = $trim + 8 ;
1108 title "Trailer Corruption - Trailer truncated to $got bytes" ;
1109 my $buffer = $good ;
1111 substr($buffer, $trim) = '';
1113 ok my $u = memGunzip(\$buffer) ;
1120 title "Trailer Corruption - Length Wrong, CRC Correct" ;
1121 my $buffer = $good ;
1122 substr($buffer, -4, 4) = pack('V', 1234);
1124 ok ! memGunzip(\$buffer) ;
1125 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
1129 title "Trailer Corruption - Length Wrong, CRC Wrong" ;
1130 my $buffer = $good ;
1131 substr($buffer, -4, 4) = pack('V', 1234);
1132 substr($buffer, -8, 4) = pack('V', 1234);
1134 ok ! memGunzip(\$buffer) ;
1135 cmp_ok $gzerrno, "==", Z_DATA_ERROR ;
1146 my $fil = gzopen($name, "rb") ;
1147 ok $fil , "opened $name";
1148 cmp_ok $fil->gzread($input, 50000), ">", 0, "read more than zero bytes";
1149 ok ! $fil->gzclose(), "closed ok";
1160 $fil = gzopen($name, "rb") ;
1161 ok $fil, "opened ok";
1162 while ($fil->gzread($input, 50000) > 0)
1167 ok ! $fil->gzclose(), "closed ok";
1176 title "Append & MultiStream Tests";
1179 my $lex = new LexFile my $name ;
1180 my $data1 = "the is the first";
1181 my $data2 = "and this is the second";
1182 my $trailing = "some trailing data";
1187 $fil = gzopen($name, "wb") ;
1188 ok $fil, "opened first file";
1189 is $fil->gzwrite($data1), length $data1, "write data1" ;
1190 ok ! $fil->gzclose(), "Closed";
1192 is slurp($name), $data1, "got expected data from slurp";
1193 is trickle($name), $data1, "got expected data from trickle";
1196 $fil = gzopen($name, "ab") ;
1197 ok $fil, "opened second file";
1198 is $fil->gzwrite($data2), length $data2, "write data2" ;
1199 ok ! $fil->gzclose(), "Closed";
1201 is slurp($name), $data1 . $data2, "got expected data from slurp";
1202 is trickle($name), $data1 . $data2, "got expected data from trickle";
1204 title "Trailing Data";
1209 is slurp($name), $data1 . $data2 . $trailing, "got expected data from slurp" ;
1210 is trickle($name), $data1 . $data2 . $trailing, "got expected data from trickle" ;
1214 title "gzclose & gzflush return codes";
1217 my $lex = new LexFile my $name ;
1218 my $data1 = "the is some text";
1221 $fil = gzopen($name, "wb") ;
1222 ok $fil, "opened first file";
1223 is $fil->gzwrite($data1), length $data1, "write data1" ;
1224 $status = $fil->gzflush(0xfff);
1225 ok $status, "flush not ok" ;
1226 is $status, Z_STREAM_ERROR;
1227 ok ! $fil->gzflush(), "flush ok" ;
1228 ok ! $fil->gzclose(), "Closed";
1234 title "repeated calls to flush";
1236 my $hello = "I am a HAL 9000 computer" ;
1237 my ($err, $x, $X, $status);
1239 ok( ($x, $err) = deflateInit ( ), "Create deflate object" );
1240 isa_ok $x, "Compress::Raw::Zlib::deflateStream" ;
1241 cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
1243 $status = $x->deflate($hello, $X) ;
1244 cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
1246 cmp_ok $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "flush returned Z_OK" ;
1248 cmp_ok $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "second flush returned Z_OK" ;
1249 is $X, "", "no output from second flush";