summaryrefslogtreecommitdiffstats
path: root/debian/perl-framework/t/modules/cgi.t
blob: d191d8d01d183612f15e0d52d50cbb449a48c514 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use strict;
use warnings FATAL => 'all';

use Apache::Test;
use Apache::TestRequest;
use Apache::TestUtil;
use File::stat;

my $have_apache_2 = have_apache 2;
my $have_apache_2050 = have_min_apache_version "2.0.50";

my $script_log_length = 8192;
if (have_module 'mod_cgi') {
    $script_log_length = 40960;
}

## mod_cgi test
##
## extra.conf.in:
## <IfModule mod_cgi.c>
## AddHandler cgi-script .sh
## AddHandler cgi-script .pl
## ScriptLog logs/mod_cgi.log
## <IfModule mod_cgi.c>
##     ScriptLogLength 40960
## </IfModule mod_cgi>
## <IfModule !mod_cgi.c>
##     ScriptLogLength 8192
## </IfModule mod_cgi>
## ScriptLogBuffer 256
## <Directory @SERVERROOT@/htdocs/modules/cgi>
## Options +ExecCGI
## [some AcceptPathInfo stuff]
## </Directory>
## </IfModule>
## 

my @post_content = (10, 99, 250, 255, 256, 257, 258, 1024);

my %test = (
    'perl.pl' => {
        'rc' => 200,
        'expect' => 'perl cgi'
    },
    'bogus-perl.pl' => {
        'rc' => 500,
        'expect' => 'none'
    },
    'nph-test.pl' => {
        'rc' => 200,
        'expect' => 'ok'
    },
    'sh.sh' => {
        'rc' => 200,
        'expect' => 'sh cgi'
    },
    'bogus-sh.sh' => {
        'rc' => 500,
        'expect' => 'none'
    },
    'acceptpathinfoon.sh' => {
        'rc' => 200,
        'expect' => ''
    },
    'acceptpathinfoon.sh/foo' => {
        'rc' => 200,
        'expect' => '/foo'
    },
    'acceptpathinfooff.sh' => {
        'rc' => 200,
        'expect' => ''
    },
    'acceptpathinfooff.sh/foo' => {
        'rc' => 404,
        'expect' => 'none'
    },
    'acceptpathinfodefault.sh' => {
        'rc' => 200,
        'expect' => ''
    },
    'acceptpathinfodefault.sh/foo' => {
        'rc' => 200,
        'expect' => '/foo'
    },
    'stderr1.pl' => {
        'rc' => 200,
        'expect' => 'this is stdout'
    },
    'stderr2.pl' => {
        'rc' => 200,
        'expect' => 'this is also stdout'
    },
    'stderr3.pl' => {
        'rc' => 200,
        'expect' => 'this is more stdout'
    },
    'nph-stderr.pl' => {
        'rc' => 200,
        'expect' => 'this is nph-stdout'
    },
);

#XXX: find something that'll on other platforms (/bin/sh aint it)
if (Apache::TestConfig::WINFU()) {
    delete @test{qw(sh.sh bogus-sh.sh)};
}
if (Apache::TestConfig::WINFU() || !$have_apache_2) {
    delete @test{qw(acceptpathinfoon.sh acceptpathinfoon.sh/foo)};
    delete @test{qw(acceptpathinfooff.sh acceptpathinfooff.sh/foo)};
    delete @test{qw(acceptpathinfodefault.sh acceptpathinfodefault.sh/foo)};
}

# CGI stderr handling works in 2.0.50 and later only on Unixes.
if (!$have_apache_2050 || Apache::TestConfig::WINFU()) {
    delete @test{qw(stderr1.pl stderr2.pl stderr3.pl nph-stderr.pl)};
}

my $tests = ((keys %test) * 2) + (@post_content * 3) + 4;
plan tests => $tests, \&need_cgi;

my ($expected, $actual);
my $path = "/modules/cgi";
my $vars = Apache::Test::vars();
my $t_logs = $vars->{t_logs};
my $cgi_log = "$t_logs/mod_cgi.log";
my ($bogus,$log_size,$stat) = (0,0,0);

unlink $cgi_log if -e $cgi_log;

foreach (sort keys %test) {
    $expected = $test{$_}{rc};
    $actual = GET_RC "$path/$_";
    ok t_cmp($actual,
             $expected,
             "return code for $_"
            );

    if ($test{$_}{expect} ne 'none') {
        $expected = $test{$_}{expect};
        $actual = GET_BODY "$path/$_";
        chomp $actual if $actual =~ /\n$/;

        ok t_cmp($actual,
                 $expected,
                 "body for $_"
                );
    }
    elsif ($_ !~ /^bogus/) {
        print "# no body test for this one\n";
        ok 1;
    }

    ## verify bogus cgi's get handled correctly
    ## logging to the cgi log
    if ($_ =~ /^bogus/) {
        $bogus++;
        if ($bogus == 1) {

            ## make sure cgi log got created, get size.
            if (-e $cgi_log) {
                print "# cgi log created ok.\n";
                ok 1;
                $stat = stat($cgi_log);
                $log_size = $$stat[7];
            } else {
                print "# error: cgi log not created!\n";
                ok 0;
            }
        } else {

            ## make sure log got bigger.
            if (-e $cgi_log) {
                $stat = stat($cgi_log);
                print "# checking that log size ($$stat[7]) is bigger than it used to be ($log_size)\n";
                ok ($$stat[7] > $log_size);
                $log_size = $$stat[7];
            } else {
                print "# error: cgi log does not exist!\n";
                ok 0;
            }
        }
    }
}

## post lots of content to a bad cgi, so we can verify
## ScriptLogBuffer is working.
my $content = 0;
foreach my $length (@post_content) {
    $content++;
    $expected = '500';
    $actual = POST_RC "$path/bogus-perl.pl", content => "$content"x$length;

    print "# posted content (length $length) to bogus-perl.pl\n";
    print "# got return code of: $actual, expecting: $expected\n";
    ## should get rc 500
    ok ($actual eq $expected);

    if (-e $cgi_log) {
        ## cgi log should be bigger.
        ## as long as it's under ScriptLogLength
        $stat = stat($cgi_log);
        if ($log_size < $script_log_length) {
            print "# checking that log size ($$stat[7]) is greater than $log_size\n";
            ok ($$stat[7] > $log_size);
        } else {
            ## should not fall in here at this point,
            ## but just in case...
            print "# verifying log did not increase in size...\n";
            ok ($$stat[7] eq $log_size);
        }
        $log_size = $$stat[7];
    
        ## there should be less than ScriptLogBuffer (256)
        ## characters logged from the post content
        open (LOG, $cgi_log) or die "died opening cgi log: $!";
        my $multiplier = 256;
        my $log;
        {
            local $/;
            $log = <LOG>;
        }
        close (LOG);
        $multiplier = $length unless $length > $multiplier;
        print "# verifying that logged content is $multiplier characters\n";
        if ($log =~ /^(?:$content){$multiplier}\n?$/m) {
            ok 1;
        }
        else {
            $log =~ s{^}{# }m;
            print "# no log line found with $multiplier '$content' characters\n";
            print "# log is:\n'$log'\n";
            ok 0;
        }
    } else {
        ## log does not exist ##
        print "# cgi log does not exist, test fails.\n";
        ok 0;
    }
}

## make sure cgi log does not 
## keep logging after it is bigger
## than ScriptLogLength
for (my $i=1 ; $i<=40 ; $i++) {

    ## get out if log does not exist ##
    last unless -e $cgi_log;

    ## request the 1k bad cgi
    ## (1k of data logged per request)
    GET_RC "$path/bogus1k.pl";

    ## when log goes over max size stop making requests
    $stat = stat($cgi_log);
    $log_size = $$stat[7];
    last if ($log_size > $script_log_length);

}
## make sure its over (or equal) our ScriptLogLength
print "# verifying log is greater than $script_log_length bytes.\n";
ok ($log_size >= $script_log_length);

## make sure it does not grow now.
GET_RC "$path/bogus1k.pl";
print "# verifying log did not grow after making bogus request.\n";
if (-e $cgi_log) {
    $stat = stat($cgi_log);
    ok ($log_size eq $$stat[7]);
} else {
    print "# log does not exist!\n";
    ok 0;
}

GET_RC "$path/bogus-perl.pl";
print "# verifying log did not grow after making another bogus request.\n";
if (-e $cgi_log) {
    $stat = stat($cgi_log);
    ok ($log_size eq $$stat[7]);
} else {
    print "# log does not exist!\n";
    ok 0;
}

print "# checking that HEAD $path/perl.pl returns 200.\n";
ok HEAD_RC("$path/perl.pl") == 200;

## clean up
unlink $cgi_log;