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; 11 12use Test::More ; 13use CompTestUtils; 14 15BEGIN 16{ 17 # use Test::NoWarnings, if available 18 my $extra = 0 ; 19 $extra = 1 20 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 }; 21 22 plan tests => 38 + $extra ; 23 24 use_ok('Compress::Raw::Zlib', 2) ; 25} 26 27 28 29my $hello = <<EOM ; 30hello world 31this is a test 32EOM 33 34my $len = length $hello ; 35 36# Check zlib_version and ZLIB_VERSION are the same. 37SKIP: { 38 skip "TEST_SKIP_VERSION_CHECK is set", 1 39 if $ENV{TEST_SKIP_VERSION_CHECK}; 40 is Compress::Raw::Zlib::zlib_version, ZLIB_VERSION, 41 "ZLIB_VERSION matches Compress::Raw::Zlib::zlib_version" ; 42} 43 44 45{ 46 title 'non-PV dictionary'; 47 # ============================== 48 49 my $dictionary = *hello ; 50 51 ok my $x = new Compress::Raw::Zlib::Deflate({-Level => Z_BEST_COMPRESSION, 52 -Dictionary => $dictionary}) ; 53 54 my $dictID = $x->dict_adler() ; 55 56 my ($X, $Y, $Z); 57 cmp_ok $x->deflate($hello, $X), '==', Z_OK; 58 cmp_ok $x->flush($Y), '==', Z_OK; 59 $X .= $Y ; 60 61 ok my $k = new Compress::Raw::Zlib::Inflate(-Dictionary => $dictionary) ; 62 63 cmp_ok $k->inflate($X, $Z), '==', Z_STREAM_END; 64 is $k->dict_adler(), $dictID; 65 is $hello, $Z ; 66 67} 68 69{ 70 71 title "deflate/inflate - non-PV buffers"; 72 # ============================== 73 74 my $hello = *hello ; 75 my ($err, $x, $X, $status); 76 77 ok( ($x, $err) = new Compress::Raw::Zlib::Deflate, "Create deflate object" ); 78 ok $x, "Compress::Raw::Zlib::Deflate ok" ; 79 cmp_ok $err, '==', Z_OK, "status is Z_OK" ; 80 81 ok ! defined $x->msg() ; 82 is $x->total_in(), 0, "total_in() == 0" ; 83 is $x->total_out(), 0, "total_out() == 0" ; 84 85 $X = *X; 86 my $Answer = ''; 87 $status = $x->deflate($hello, $X) ; 88 $Answer .= $X ; 89 90 cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ; 91 92 $X = *X; 93 cmp_ok $x->flush($X), '==', Z_OK, "flush returned Z_OK" ; 94 $Answer .= $X ; 95 96 ok ! defined $x->msg() ; 97 is $x->total_in(), length $hello, "total_in ok" ; 98 is $x->total_out(), length $Answer, "total_out ok" ; 99 100 my $k; 101 ok(($k, $err) = new Compress::Raw::Zlib::Inflate); 102 ok $k, "Compress::Raw::Zlib::Inflate ok" ; 103 cmp_ok $err, '==', Z_OK, "status is Z_OK" ; 104 105 ok ! defined $k->msg(), "No error messages" ; 106 is $k->total_in(), 0, "total_in() == 0" ; 107 is $k->total_out(), 0, "total_out() == 0" ; 108 my $GOT = ''; 109 my $Z; 110 $Z = *Z; 111 my $Alen = length $Answer; 112 $status = $k->inflate($Answer, $Z) ; 113 $GOT .= $Z ; 114 115 cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ; 116 is $GOT, $hello, "uncompressed data matches ok" ; 117 ok ! defined $k->msg(), "No error messages" ; 118 is $k->total_in(), $Alen, "total_in ok" ; 119 is $k->total_out(), length $hello , "total_out ok"; 120 121 122 ok(($k, $err) = new Compress::Raw::Zlib::Inflate); 123 ok $k, "Compress::Raw::Zlib::Inflate ok" ; 124 cmp_ok $err, '==', Z_OK, "status is Z_OK" ; 125 126 $Z = *Z; 127 $status = $k->inflate($hello, $Z); 128 is $Z, "", 'inflating *hello does not crash'; 129 130 $hello = *hello; 131 $status = $k->inflateSync($hello); 132 cmp_ok $status, "!=", Z_OK, 133 "inflateSync on *hello returns error (and does not crash)"; 134} 135 136