File: flatten.pm

package info (click to toggle)
libapache2-mod-perl2 2.0.9~1624218-2%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 11,912 kB
  • ctags: 4,588
  • sloc: perl: 95,064; ansic: 14,527; makefile: 49; sh: 18
file content (124 lines) | stat: -rw-r--r-- 2,936 bytes parent folder | download | duplicates (7)
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
# please insert nothing before this line: -*- mode: cperl; cperl-indent-level: 4; cperl-continued-statement-offset: 4; indent-tabs-mode: nil -*-
package TestAPR::flatten;

use strict;
use warnings FATAL => 'all';

use Apache::Test;
use Apache::TestUtil;
use TestCommon::Utils;

use Apache2::RequestRec ();
use APR::Bucket ();
use APR::Brigade ();

use Apache2::Const -compile => 'OK';

sub handler {

    my $r = shift;

    plan $r, tests => 26;

    # first, create a brigade
    my $pool = $r->pool;
    my $ba   = $r->connection->bucket_alloc;

    my $bb   = APR::Brigade->new($pool, $ba);

    # now, let's put several buckets in it
    for (1 .. 10) {
        my $data = 'x' x 20000;
        my $bucket = APR::Bucket->new($ba, $data);
        $bb->insert_tail($bucket);
    }

    # ok, that's 10 buckets of 20,000 = 200,000 characters
    ok t_cmp($bb->length,
             200000,
             'APR::Brigade::length()');

    # syntax: require a $bb
    eval { APR::Brigade::flatten("") };

    ok t_cmp($@,
             qr!usage: \$bb->flatten\(\$buf, \[\$wanted\]\)!,
             'APR::Brigade::flatten() requires a brigade');

    # flatten() will slurp up the entire brigade
    # equivalent to calling apr_brigade_pflatten
    {
        my $len = $bb->flatten(my $data);

        verify($len, 200000, $data, 1);
    }

    # flatten(0) returns 0 bytes
    {
        my $len = $bb->flatten(my $data, 0);

        t_debug('$bb->flatten(0) returns a defined value');
        ok (defined $data);

        verify($len, 0, $data, 0);
    }


    # flatten($length) will return the first $length bytes
    # equivalent to calling apr_brigade_flatten
    {
        # small
        my $len = $bb->flatten(my $data, 30);
        verify($len, 30, $data, 1);
    }

    {
        # large
        my $len = $bb->flatten(my $data, 190000);
        verify($len, 190000, $data, 1);
    }

    {
        # more than enough
        my $len = $bb->flatten(my $data, 300000);
        verify($len, 200000, $data, 1);
    }

    # fetch from a brigade with no data in it
    {
        my $len = APR::Brigade->new($pool, $ba)->flatten(my $data);

        t_debug('empty brigade returns a defined value');
        ok (defined $data);

        verify($len, 0, $data, 0);
    }

    Apache2::Const::OK;
}

# this sub runs 3 sub-tests with a false $check_content
# and 4 otherwise
sub verify {
    my ($len, $expected_len, $data, $check_content) = @_;

    ok t_cmp($len,
             $expected_len,
             "\$bb->flatten(\$data, $len) returned $len bytes");
    ok t_cmp(length($data),
             $len,
             "\$bb->flatten(\$data, $len) returned all expected data");

    ok TestCommon::Utils::is_tainted($data);

    if ($check_content) {
        # don't use t_cmp() here, else we get 200,000 characters
        # to look at in verbose mode
        t_debug("data all 'x' characters");
        ok ($data !~ m/[^x]/);
    }

}


1;