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 ;
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 => 108 + $extra ;
23
24    use_ok('Compress::Raw::Zlib', 2) ;
25}
26
27use CompTestUtils;
28
29test_zlib_header_matches_library();
30
31my $hello = "I am a HAL 9000 computer" x 2001;
32my $tmp = $hello ;
33
34my ($err, $x, $X, $status);
35
36ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (-AppendOutput => 1));
37ok $x ;
38cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;
39
40my $out ;
41$status = $x->deflate($tmp, $out) ;
42cmp_ok $status, '==', Z_OK, "  status is Z_OK" ;
43
44cmp_ok $x->flush($out), '==', Z_OK, "  flush returned Z_OK" ;
45
46
47sub getOut { my $x = ''; return \$x }
48
49for my $bufsize (1, 2, 3, 13, 4096, 1024*10)
50{
51    print "#\n#Bufsize $bufsize\n#\n";
52    $tmp = $out;
53
54    my $k;
55    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( AppendOutput => 1,
56                                                      LimitOutput => 1,
57                                                      Bufsize => $bufsize
58                                                    ));
59    ok $k ;
60    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;
61
62    ok ! defined $k->msg(), "  no msg" ;
63    is $k->total_in(), 0, "  total_in == 0" ;
64    is $k->total_out(), 0, "  total_out == 0" ;
65    my $GOT = getOut();
66    my $prev;
67    my $deltaOK = 1;
68    my $looped = 0;
69    while (length $tmp)
70    {
71        ++ $looped;
72        my $prev = length $GOT;
73        $status = $k->inflate($tmp, $GOT) ;
74        last if $status == Z_STREAM_END || $status == Z_DATA_ERROR || $status == Z_STREAM_ERROR ;
75        $deltaOK = 0 if length($GOT) - $prev > $bufsize;
76    }
77
78    ok $deltaOK, "  Output Delta never > $bufsize";
79    cmp_ok $looped, '>=', 1, "  looped $looped";
80    is length($tmp), 0, "  length of input buffer is zero";
81
82    cmp_ok $status, '==', Z_STREAM_END, "  status is Z_STREAM_END" ;
83    is $$GOT, $hello, "  got expected output" ;
84    ok ! defined $k->msg(), "  no msg" ;
85    is $k->total_in(), length $out, "  length total_in ok" ;
86    is $k->total_out(), length $hello, "  length total_out ok " .  $k->total_out() ;
87}
88
89sub getit
90{
91    my $obj = shift ;
92    my $input = shift;
93
94    my $data ;
95    1 while $obj->inflate($input, $data) != Z_STREAM_END ;
96    return \$data ;
97}
98
99{
100    title "regression test";
101
102    my ($err, $x, $X, $status);
103
104    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (-AppendOutput => 1));
105    ok $x ;
106    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;
107
108    my $line1 = ("abcdefghijklmnopq" x 1000) . "\n" ;
109    my $line2 = "second line\n" ;
110    my $text = $line1 . $line2 ;
111    my $tmp = $text;
112
113    my $out ;
114    $status = $x->deflate($tmp, $out) ;
115    cmp_ok $status, '==', Z_OK, "  status is Z_OK" ;
116
117    cmp_ok $x->flush($out), '==', Z_OK, "  flush returned Z_OK" ;
118
119    my $k;
120    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( AppendOutput => 1,
121                                                      LimitOutput => 1
122                                                    ));
123
124
125    my $c = getit($k, $out);
126    is $$c, $text;
127
128
129}
130
131{
132    title "regression test for #92521: Z_OK instead of Z_BUF_ERROR";
133
134    # 1M "aaa..."
135    my $in = 'a' x 100000;
136    my ($deflate, $err) = Compress::Raw::Zlib::Deflate->new(WindowBits => -15,
137        MemLevel => 8);
138    ok $deflate ;
139    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;
140
141    my $status = $deflate->deflate($in, my $zip);
142    cmp_ok $status, '==', Z_OK, "  status is Z_OK" ;
143
144    cmp_ok $deflate->flush($zip, Z_SYNC_FLUSH), "==", Z_OK;
145
146    # Compression should stop after 10K "aaa..." with Z_BUF_ERROR
147    my $inflate;
148    ($inflate, $err)  = Compress::Raw::Zlib::Inflate->new( Bufsize => 10000,
149        LimitOutput => 1, WindowBits => -15 );
150    ok $inflate ;
151    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;
152
153    $status = $inflate->inflate($zip, my $out);
154
155    cmp_ok length($out), ">=", 10000;
156    #warn 'RESULT: ', length($out), ' of ', length($in), "\n";
157    cmp_ok $status, '==', Z_BUF_ERROR, "  status is Z_BUF_ERROR" ;
158
159}
160