xref: /openbsd-src/gnu/usr.bin/perl/cpan/Compress-Raw-Zlib/t/19nonpv.t (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
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 ;
13
14BEGIN
15{
16    # use Test::NoWarnings, if available
17    my $extra = 0;
18    $extra = 1
19        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
20
21    plan tests => 38 + $extra ;
22
23    use_ok('Compress::Raw::Zlib', 2) ;
24}
25
26use CompTestUtils;
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.
37test_zlib_header_matches_library();
38
39
40SKIP:
41{
42    title 'non-PV dictionary';
43    # ==============================
44
45    # # temp workaround for
46    # # https://github.com/pmqs/Compress-Raw-Zlib/issues/27
47    # skip "skipping tests for Perl 5.6.*", 7
48    #     if $] < 5.008 ;
49
50    my $dictionary = *hello ;
51
52    ok my $x = new Compress::Raw::Zlib::Deflate({-Level => Z_BEST_COMPRESSION,
53			     -Dictionary => $dictionary}) ;
54
55    my $dictID = $x->dict_adler() ;
56
57    my ($X, $Y, $Z);
58    cmp_ok $x->deflate($hello, $X), '==', Z_OK;
59    cmp_ok $x->flush($Y), '==', Z_OK;
60    $X .= $Y ;
61
62    ok my $k = new Compress::Raw::Zlib::Inflate(-Dictionary => $dictionary) ;
63
64    cmp_ok $k->inflate($X, $Z), '==', Z_STREAM_END;
65    is $k->dict_adler(), $dictID;
66    is $hello, $Z ;
67
68}
69
70SKIP:
71{
72
73    title  "deflate/inflate - non-PV buffers";
74    # ==============================
75
76    # # temp workaround for
77    # # https://github.com/pmqs/Compress-Raw-Zlib/issues/27
78    # skip "skipping tests for Perl 5.6.*", 27
79    #     if $] < 5.008 ;
80
81    my $hello = *hello ;
82    my ($err, $x, $X, $status);
83
84    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate, "Create deflate object" );
85    ok $x, "Compress::Raw::Zlib::Deflate ok" ;
86    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
87
88    ok ! defined $x->msg() ;
89    is $x->total_in(), 0, "total_in() == 0" ;
90    is $x->total_out(), 0, "total_out() == 0" ;
91
92    $X = *X;
93    my $Answer = '';
94    $status = $x->deflate($hello, $X) ;
95    $Answer .= $X ;
96
97    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
98
99    $X = *X;
100    cmp_ok  $x->flush($X), '==', Z_OK, "flush returned Z_OK" ;
101    $Answer .= $X ;
102
103    ok ! defined $x->msg()  ;
104    is $x->total_in(), length $hello, "total_in ok" ;
105    is $x->total_out(), length $Answer, "total_out ok" ;
106
107    my $k;
108    ok(($k, $err) = new Compress::Raw::Zlib::Inflate);
109    ok $k, "Compress::Raw::Zlib::Inflate ok" ;
110    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
111
112    ok ! defined $k->msg(), "No error messages" ;
113    is $k->total_in(), 0, "total_in() == 0" ;
114    is $k->total_out(), 0, "total_out() == 0" ;
115    my $GOT = '';
116    my $Z;
117    $Z = *Z;
118    my $Alen = length $Answer;
119    $status = $k->inflate($Answer, $Z) ;
120    $GOT .= $Z ;
121
122    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
123    is $GOT, $hello, "uncompressed data matches ok" ;
124    ok ! defined $k->msg(), "No error messages" ;
125    is $k->total_in(), $Alen, "total_in ok" ;
126    is $k->total_out(), length $hello , "total_out ok";
127
128
129    ok(($k, $err) = new Compress::Raw::Zlib::Inflate);
130    ok $k, "Compress::Raw::Zlib::Inflate ok" ;
131    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
132
133    $Z = *Z;
134    $status = $k->inflate($hello, $Z);
135    is $Z, "", 'inflating *hello does not crash';
136
137    $hello = *hello;
138    $status = $k->inflateSync($hello);
139    cmp_ok $status, "!=", Z_OK,
140       "inflateSync on *hello returns error (and does not crash)";
141}
142