summaryrefslogtreecommitdiff
path: root/cpan/Compress-Raw-Zlib/t/09limitoutput.t
blob: 2532f9c692669f604d0978202e08be6cb77054cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
BEGIN {
    if ($ENV{PERL_CORE}) {
	chdir 't' if -d 't';
	@INC = ("../lib", "lib/compress");
    }
}

use lib qw(t t/compress);
use strict;
use warnings;
use bytes;

use Test::More ;
use CompTestUtils;

BEGIN 
{ 
    # use Test::NoWarnings, if available
    my $extra = 0 ;
    $extra = 1
        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };

    plan tests => 107 + $extra ;

    use_ok('Compress::Raw::Zlib', 2) ; 
}



my $hello = "I am a HAL 9000 computer" x 2001;
my $tmp = $hello ;

my ($err, $x, $X, $status); 

ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (-AppendOutput => 1));
ok $x ;
cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;

my $out ;
$status = $x->deflate($tmp, $out) ;
cmp_ok $status, '==', Z_OK, "  status is Z_OK" ;

cmp_ok $x->flush($out), '==', Z_OK, "  flush returned Z_OK" ;
     
     
sub getOut { my $x = ''; return \$x }

for my $bufsize (1, 2, 3, 13, 4096, 1024*10)
{
    print "#\n#Bufsize $bufsize\n#\n";
    $tmp = $out;

    my $k;
    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( AppendOutput => 1,
                                                      LimitOutput => 1,
                                                      Bufsize => $bufsize
                                                    ));
    ok $k ;
    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;
 
    ok ! defined $k->msg(), "  no msg" ;
    is $k->total_in(), 0, "  total_in == 0" ;
    is $k->total_out(), 0, "  total_out == 0" ;
    my $GOT = getOut();
    my $prev;
    my $deltaOK = 1;
    my $looped = 0;
    while (length $tmp)
    {
        ++ $looped;
        my $prev = length $GOT;
        $status = $k->inflate($tmp, $GOT) ;
        last if $status == Z_STREAM_END || $status == Z_DATA_ERROR || $status == Z_STREAM_ERROR ;
        $deltaOK = 0 if length($GOT) - $prev > $bufsize;
    }
     
    ok $deltaOK, "  Output Delta never > $bufsize";
    cmp_ok $looped, '>=', 1, "  looped $looped";
    is length($tmp), 0, "  length of input buffer is zero";

    cmp_ok $status, '==', Z_STREAM_END, "  status is Z_STREAM_END" ;
    is $$GOT, $hello, "  got expected output" ;
    ok ! defined $k->msg(), "  no msg" ;
    is $k->total_in(), length $out, "  length total_in ok" ;
    is $k->total_out(), length $hello, "  length total_out ok " .  $k->total_out() ;
}

sub getit
{
    my $obj = shift ;
    my $input = shift;
    
    my $data ;
    1 while $obj->inflate($input, $data) != Z_STREAM_END ;
    return \$data ;
}

{
    title "regression test";
    
    my ($err, $x, $X, $status); 
    
    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (-AppendOutput => 1));
    ok $x ;
    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;

    my $line1 = ("abcdefghijklmnopq" x 1000) . "\n" ;
    my $line2 = "second line\n" ;
    my $text = $line1 . $line2 ;
    my $tmp = $text;
   
    my $out ;
    $status = $x->deflate($tmp, $out) ;
    cmp_ok $status, '==', Z_OK, "  status is Z_OK" ;
    
    cmp_ok $x->flush($out), '==', Z_OK, "  flush returned Z_OK" ;

    my $k;
    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( AppendOutput => 1,
                                                      LimitOutput => 1
                                                    ));

                                                        
    my $c = getit($k, $out);
    is $$c, $text;
    
                                              
}

{
    title "regression test for #92521: Z_OK instead of Z_BUF_ERROR";
    
    # 1M "aaa..." 
    my $in = 'a' x 100000; 
    my ($deflate, $err) = Compress::Raw::Zlib::Deflate->new(WindowBits => -15, 
        MemLevel => 8); 
    ok $deflate ;
    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;

    my $status = $deflate->deflate($in, my $zip); 
    cmp_ok $status, '==', Z_OK, "  status is Z_OK" ;

    cmp_ok $deflate->flush($zip, Z_SYNC_FLUSH), "==", Z_OK; 

    # Compression should stop after 10K "aaa..." with Z_BUF_ERROR 
    my $inflate;
    ($inflate, $err)  = Compress::Raw::Zlib::Inflate->new( Bufsize => 10000, 
        LimitOutput => 1, WindowBits => -15 ); 
    ok $inflate ;
    cmp_ok $err, '==', Z_OK, "  status is Z_OK" ;

    $status = $inflate->inflate($zip, my $out); 

    cmp_ok length($out), ">=", 10000;
    #warn 'RESULT: ', length($out), ' of ', length($in), "\n"; 
    cmp_ok $status, '==', Z_BUF_ERROR, "  status is Z_BUF_ERROR" ;

}