blib/lib/Test/CGI/External.pm

filter: Statementstmt 64.2% 388 / 604 Branchbran 45.6% 125 / 274 Conditioncond 23.0% 9 / 39 MC/DCmcdc 7.6% 2 / 26 Subroutinesub 77.7% 42 / 54 Podpod 39.5% 17 / 43 totaltotal 56.0% 583 / 1040 CC 155 SCAR 38.1
CC
155
Cov
57%
run
168.7
validate_html
156.0
test_411
56.0
CRAP 45.4
↑ Summary examples/bad-method.cgi »
1 package Test::CGI::External;
2 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use File::Temp 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use FindBin '$Bin';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '0.23';
14  
15 sub new
16 6 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 6 my %tester;
18  
19 6 my $tb = Test::Builder->new ();
20 6 $tester{tb} = $tb;
21 # $tester{html_validator} = '/home/ben/bin/validate';
22  
23 6 return bless \%tester;
24 }
25  
26 sub note
27 126 ▶{
Subroutine note: called CC=2 SCAR=6.9
Pod undocumented
28 126 my ($self, $note) = @_;
29 126 my (undef, $file, $line) = caller ();
30 126 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 29 97 126
31 29 $self->{tb}->note ("$file:$line: $note");
32 }
33 }
34  
35 sub on_off_msg
36 4 ▶{
Subroutine on_off_msg: called CC=3 SCAR=11.1
Pod undocumented
37 4 my ($self, $switch, $type) = @_;
38 4 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 1 3 4
39 1 my $msg = "You have asked me to turn ";
40 1 ▶ if ($switch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) { } 1 0 1
41 1 $msg .= "on";
42 }
43 else {
44 0 $msg .= "off";
45 }
46 1 $msg .= " testing of $type";
47 1 my (undef, $file, $line) = caller ();
48 1 $self->{tb}->note ("$file:$line: $msg");
49 }
50 }
51  
52 sub set_cgi_executable
53 17 ▶{
Subroutine set_cgi_executable: called CC=3 SCAR=11.1
Pod documented
54 17 my ($self, $cgi_executable, @command_line_options) = @_;
55 17 $self->note ("I am setting the CGI executable to be tested to '$cgi_executable'.");
56 17 $self->do_test (-f $cgi_executable, "found executable $cgi_executable");
57 17 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 17 17
58 # These tests don't do anything useful on Windows, see
59 # http://perldoc.perl.org/perlport.html#-X
60 0 $self->pass_test ('Invalid test for MS Windows');
61 }
62 else {
63 17 $self->do_test (-x $cgi_executable, "$cgi_executable is executable");
64 }
65 17 $self->{cgi_executable} = $cgi_executable;
66 17 ▶ if (@command_line_options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@command_line_options) { } 10 7 17
67 10 $self->{command_line_options} = \@command_line_options;
68 }
69 else {
70 7 $self->{command_line_options} = [];
71 }
72 }
73  
74 sub do_compression_test
75 4 ▶{
Subroutine do_compression_test: called CC=5 SCAR=18.3
Pod documented
76 4 my ($self, $switch) = @_;
77 4 $switch = !! $switch;
78 4 $self->on_off_msg ($switch, "compression");
79 4 $self->{comp_test} = $switch;
80 4 ▶ if ($switch && ! $self->{_use_io_uncompress_gunzip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch and not $self->{"_use_io_uncompress_gunzip"}) 3 1 4
Condition66%2/3
$switch and not $self->{"_use_io_uncompress_gunzip"}
!ll&&!rl&&r
103
MC/DC50%1/2
$switch and not $self->{"_use_io_uncompress_gunzip"}
$switch not $self->{"_use_io_uncompress_gunzip"}
Truth table66%2/3
A and not(B)
A= $switchB= not $self->{"_use_io_uncompress_gunzip"}
ABresult
0X0
100
111
81 3 eval "use Gzip::Faster;";
82 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
83 0 $self->{_use_io_uncompress_gunzip} = 1;
84 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"no_warn"}) 0 0 0
85 0 carp "Gzip::Faster is not installed, using IO::Uncompress::Gunzip";
86 }
87 }
88 }
89 }
90  
91 sub do_caching_test
92 0 ▶{
Subroutine do_caching_test: not called CC=4 SCAR=30.0
Pod documented
93 0 my ($self, $switch) = @_;
94 0 $switch = !! $switch;
95 0 $self->on_off_msg ($switch, "if-modified/last-modified response");
96 0 $self->{cache_test} = $switch;
97 0 ▶ if ($switch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) 0 0 0
98 0 eval "use HTTP::Date;";
99 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
100 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
101 0 carp "HTTP::Date is not installed, cannot do caching test";
102 }
103 0 $self->{cache_test} = undef;
104 }
105 }
106 }
107  
108 sub expect_charset
109 4 ▶{
Subroutine expect_charset: called CC=4 SCAR=20.7
Pod documented
110 4 my ($self, $charset) = @_;
111 4 eval "use Unicode::UTF8 qw/decode_utf8 encode_utf8/";
112 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
113 0 Encode->import (qw/decode_utf8 encode_utf8/);
114 0 ▶ if (! $self->{no_warn} && ! $self->{_warned_unicode_utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"} or $self->{"_warned_unicode_utf8"}) 0 0 0
Condition0%0/3
$self->{"no_warn"} or $self->{"_warned_unicode_utf8"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"no_warn"} or $self->{"_warned_unicode_utf8"}
$self->{"no_warn"} $self->{"_warned_unicode_utf8"}
Truth table0%0/3
A or B
A= $self->{"no_warn"}B= $self->{"_warned_unicode_utf8"}
ABresult
1X1
011
000
115 0 carp "Unicode::UTF8 is not installed, using Encode";
116 0 $self->{_warned_unicode_utf8} = 1;
117 }
118 }
119 4 $self->note ("You have told me to expect a 'charset' value of '$charset'.");
120 4 $self->{expected_charset} = $charset;
121 }
122  
123 sub expect_mime_type
124 2 ▶{
Subroutine expect_mime_type: called CC=2 SCAR=7.4
Pod documented
125 2 my ($self, $mime_type) = @_;
126 2 ▶ if ($mime_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type) { } 2 0 2
127 2 $self->note ("You have told me to expect a mime type of '$mime_type'.");
128 }
129 else {
130 0 $self->note ("You have deleted the mime type.");
131 }
132 2 $self->{mime_type} = $mime_type;
133 }
134  
135 sub set_verbosity
136 1 ▶{
Subroutine set_verbosity: called CC=1 SCAR=0.0
Pod documented
137 1 my ($self, $verbosity) = @_;
138 1 $self->{verbose} = !! $verbosity;
139 1 $self->note ("You have asked me to print messages as I work.");
140 }
141  
142 sub set_no_warnings
143 0 ▶{
Subroutine set_no_warnings: not called CC=1 SCAR=6.9
Pod documented
144 0 my ($self, $onoff) = @_;
145 0 $self->{no_warn} = !! $onoff;
146 0 $self->on_off_msg ($onoff, "warnings");
147 }
148  
149 sub test_if_modified_since
150 0 ▶{
Subroutine test_if_modified_since: not called CC=5 SCAR=34.0
Pod undocumented
151 0 my ($self, $last_modified) = @_;
152 0 ▶ die unless defined $last_modified;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_modified 0 0 0
153 0 my $saved = $ENV{HTTP_IF_MODIFIED_SINCE};
154 0 $ENV{HTTP_IF_MODIFIED_SINCE} = $last_modified;
155 0 $self->note ("Testing response with last modified time $last_modified");
156 0 my $saved_no_check_content = $self->{no_check_content};
157 0 $self->{no_check_content} = 1;
158 # Copy the hash of options into a private copy, so that we can run
159 # the thing again without overwriting our precious stuff.
160 0 my $saved_run_options = $self->{run_options};
161 0 my %run_options = %$saved_run_options;
162 0 ▶ if ($run_options{expect_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_options{"expect_errors"}) 0 0 0
163 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
164 0 carp "The expect_errors option is often incompatible with do_caching_test (1), suggest switching off testing of caching when expecting errors";
165 }
166 }
167 0 $self->{run_options} = \%run_options;
168 0 my $saved_no_warn = $self->{no_warn};
169 0 $self->{no_warn} = 1;
170 0 run_private ($self);
171 0 $self->check_headers_private ($self);
172 0 $self->test_status (304);
173 0 my $body = $run_options{body};
174 0 ▶ $self->do_test (! defined ($body) || length ($body) == 0,
Condition0%0/3
!defined($body) || length $body == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($body) || length $body == 0
!defined($body) length $body == 0
Truth table0%0/3
not(A) || B
A= !defined($body)B= length $body == 0
ABresult
1X1
011
000
175 "No body returned with 304 response");
176 0 $ENV{HTTP_IF_MODIFIED_SINCE} = $saved;
177 # Restore our precious stuff.
178 0 $self->{run_options} = $saved_run_options;
179 0 $self->{no_warn} = $saved_no_warn;
180 0 $self->{no_check_content} = $saved_no_check_content;
181 }
182  
183 sub check_caching_private
184 0 ▶{
Subroutine check_caching_private: not called CC=3 SCAR=24.8
Pod undocumented
185 0 my ($self) = @_;
186 0 my $output = $self->{run_options};
187 0 my $headers = $output->{headers};
188 0 ▶ if (! $headers) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers) 0 0 0
189 0 die "There are no headers in object, did the tests really run?";
190 }
191 0 my $last_modified = $headers->{'last-modified'};
192 0 $self->do_test ($last_modified, "Has last modified header");
193 # for my $k (keys %$headers) {
194 # print "$k $headers->{$k}\n";
195 # }
196 0 my $time = str2time ($last_modified);
197 0 $self->do_test (defined $time, "Last modified time can be parsed by HTTP::Date");
198 0 ▶ if ($last_modified) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_modified) { } 0 0 0
199 0 $self->test_if_modified_since ($last_modified);
200 }
201 else {
202 0 $self->note ("Not doing last modified test due to no-header failure");
203 }
204 # Restore the headers because they were overwritten when we did
205 # the caching test.
206 0 $output->{headers} = $headers;
207 }
208  
209 5 my @request_method_list = qw/POST GET HEAD/;
210 5 my %valid_request_method = map {$_ => 1} @request_method_list;
211  
212 sub check_request_method
213 18 ▶{
Subroutine check_request_method: called CC=6 SCAR=27.3
Pod undocumented
214 18 my ($self, $request_method) = @_;
215 18 my $default_request_method = 'GET';
216 18 ▶ if ($request_method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_method) { } 18 0 18
217 18 ▶ if ($request_method && ! $valid_request_method{$request_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_method and not $valid_request_method{$request_method}) 0 18 18
Condition33%1/3
$request_method and not $valid_request_method{$request_method}
!ll&&!rl&&r
0180
MC/DC0%0/2
$request_method and not $valid_request_method{$request_method}
$request_method not $valid_request_method{$request_method}
Truth table33%1/3
A and not(B)
A= $request_methodB= not $valid_request_method{$request_method}
ABresult
0X0
100
111
218 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
219 0 carp "You have set the request method to a value '$request_method' which is not one of the ones I know about, which are ", join (', ', @request_method_list), " so I am setting it to the default, '$default_request_method'";
220 }
221 0 $request_method = $default_request_method;
222 }
223 }
224 else {
225 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
226 0 carp "You have not set the request method, so I am setting it to the default, '$default_request_method'";
227 }
228 0 $request_method = $default_request_method;
229 }
230 18 return $request_method;
231 }
232  
233 sub do_test
234 122 ▶{
Subroutine do_test: called CC=1 SCAR=0.0
Pod undocumented
235 122 my ($self, $test, $message) = @_;
236 122 $self->{tb}->ok ($test, $message);
237 }
238  
239 # Register a successful test (deprecated legacy from pre-Test::Builder days)
240  
241 sub pass_test
242 59 ▶{
Subroutine pass_test: called CC=1 SCAR=0.0
Pod undocumented
243 59 my ($self, $test) = @_;
244 59 $self->{tb}->ok (1, $test);
245 }
246  
247 # Fail a test and keep going (deprecated legacy from pre-Test::Builder days)
248  
249 sub fail_test
250 4 ▶{
Subroutine fail_test: called CC=1 SCAR=0.0
Pod undocumented
251 4 my ($self, $test) = @_;
252 4 $self->{tb}->ok (0, $test);
253 }
254  
255 # Print the TAP plan
256  
257 sub plan
258 0 ▶{
Subroutine plan: not called CC=1 SCAR=6.9
Pod documented
259 0 my ($self) = @_;
260 0 $self->{tb}->done_testing ();
261 }
262  
263 # Fail a test which means that we cannot keep going.
264  
265 sub abort_test
266 0 ▶{
Subroutine abort_test: not called CC=1 SCAR=6.9
Pod undocumented
267 0 my ($self, $test) = @_;
268 0 $self->{tb}->skip_all ($test);
269 }
270  
271 # Set an environment variable, with warning about collisions.
272  
273 sub setenv_private
274 49 ▶{
Subroutine setenv_private: called CC=4 SCAR=15.2
Pod undocumented
275 49 my ($self, $name, $value) = @_;
276 49 ▶ if (! $self->{set_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'set_env'}) { } 20 29 49
277 20 $self->{set_env} = [$name];
278 }
279 else {
280 29 push @{$self->{set_env}}, $name;
281 }
282 49 ▶ if ($ENV{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$name}) 0 49 49
283 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
284 0 carp "A variable '$name' is already set in the environment.\n";
285 }
286 }
287 49 $ENV{$name} = $value;
288 }
289  
290 sub encode_utf8_safe
291 1 ▶{
Subroutine encode_utf8_safe: called CC=4 SCAR=20.7
Pod undocumented
292 1 my ($self) = @_;
293 1 my $input = $self->{input};
294 1 eval "use Unicode::UTF8;";
295 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
296 0 ▶ if (! $self->{no_warn} && ! $self->{_warned_unicode_utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"} or $self->{"_warned_unicode_utf8"}) 0 0 0
Condition0%0/3
$self->{"no_warn"} or $self->{"_warned_unicode_utf8"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"no_warn"} or $self->{"_warned_unicode_utf8"}
$self->{"no_warn"} $self->{"_warned_unicode_utf8"}
Truth table0%0/3
A or B
A= $self->{"no_warn"}B= $self->{"_warned_unicode_utf8"}
ABresult
1X1
011
000
297 0 carp "Unicode::UTF8 is not installed, using Encode";
298 0 $self->{_warned_unicode_utf8} = 1;
299 }
300 # Encode::encode_utf8 uses prototypes so we have to hassle this up.
301 0 return Encode::encode_utf8 ($input);
302 }
303 1 return Unicode::UTF8::encode_utf8 ($input);
304 }
305  
306 # Internal routine to run a CGI program.
307  
308 sub run_private
309 20 ▶{
Subroutine run_private: called CC=16 SCAR=29.9
Pod undocumented
310 20 my ($self) = @_;
311  
312 # Pull everything out of the object and into normal variables.
313  
314 20 my $verbose = $self->{verbose};
315 20 my $options = $self->{run_options};
316 20 my $cgi_executable = $self->{cgi_executable};
317 20 my $comp_test = $self->{comp_test};
318  
319 # Hassle up the CGI inputs, including environment variables, from
320 # the options the user has given.
321  
322 # mwforum requires GATEWAY_INTERFACE to be set to CGI/1.1
323 # setenv_private ($o, 'GATEWAY_INTERFACE', 'CGI/1.1');
324  
325 20 my $query_string = $options->{QUERY_STRING};
326 20 ▶ if (defined $query_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query_string) { } 0 20 20
327 0 $self->note ("I am setting the query string to '$query_string'.");
328 0 setenv_private ($self, 'QUERY_STRING', $query_string);
329 }
330 else {
331 20 $self->note ("There is no query string.");
332 20 setenv_private ($self, 'QUERY_STRING', "");
333 }
334  
335 20 my $request_method;
336 20 ▶ if ($options->{no_check_request_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'no_check_request_method'}) { } 2 18 20
337 2 $request_method = $options->{REQUEST_METHOD};
338 }
339 else {
340 18 $request_method = $self->check_request_method ($options->{REQUEST_METHOD});
341 }
342 20 $self->note ("The request method is '$request_method'.");
343 20 setenv_private ($self, 'REQUEST_METHOD', $request_method);
344 20 my $content_type = $options->{CONTENT_TYPE};
345 20 ▶ if ($content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type) 0 20 20
346 0 $self->note ("The content type is '$content_type'.");
347 0 setenv_private ($self, 'CONTENT_TYPE', $content_type);
348 }
349 20 ▶ if ($options->{HTTP_COOKIE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"HTTP_COOKIE"}) 0 20 20
350 0 setenv_private ($self, 'HTTP_COOKIE', $options->{HTTP_COOKIE});
351 }
352 20 my $remote_addr = $self->{run_options}->{REMOTE_ADDR};
353 20 ▶ if ($remote_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_addr) 0 20 20
354 0 $self->note ("I am setting the remote address to '$remote_addr'.");
355 0 setenv_private ($self, 'REMOTE_ADDR', $remote_addr);
356 }
357 20 ▶ if (defined $options->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"input"}) 3 17 20
358 3 $self->{input} = $options->{input};
359 3 ▶ if (utf8::is_utf8 ($self->{input})) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($self->{"input"})) 1 2 3
360 1 $self->{input} = $self->encode_utf8_safe ();
361 }
362 3 ▶ if ($self->{bad_content_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bad_content_length'}) { } 0 3 3
363 0 setenv_private ($self, 'CONTENT_LENGTH', '0');
364 }
365 else {
366 3 my $content_length = length ($self->{input});
367 3 setenv_private ($self, 'CONTENT_LENGTH', $content_length);
368 3 $self->note ("I am setting the CGI program's standard input to a string of length $content_length taken from the input options.");
369 3 $options->{content_length} = $content_length;
370 }
371 }
372  
373 20 ▶ if ($comp_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_test) 6 14 20
374 6 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 2 6
375 4 $self->{tb}->note ("I am requesting gzip encoding from the CGI executable.\n");
376 }
377 6 setenv_private ($self, 'HTTP_ACCEPT_ENCODING', 'gzip, fake');
378 }
379  
380 # Actually run the executable under the current circumstances.
381  
382 20 my @cmd = ($cgi_executable);
383 20 ▶ if ($self->{command_line_options}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command_line_options"}) 20 0 20
384 20 push @cmd, @{$self->{command_line_options}};
385 }
386 20 $self->note ("I am running '@cmd'");
387 20 $self->run3 (\@cmd);
388 20 $options->{output} = $self->{output};
389 20 $options->{error_output} = $self->{errors};
390 20 $options->{exit_code} = $?;
391 20 $self->note (sprintf ("The program has now finished running. There were %d bytes of output.", length ($self->{output})));
392 20 ▶ if ($options->{expect_failure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'expect_failure'}) { } 0 20 20
393 }
394 else {
395 20 $self->do_test ($options->{exit_code} == 0,
396 "The CGI executable exited with zero status");
397 }
398 20 $self->do_test ($options->{output}, "The CGI executable produced some output");
399 20 ▶ if ($options->{expect_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'expect_errors'}) { } 0 20 20
400 0 ▶ if ($options->{error_output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'error_output'}) { } 0 0 0
401 0 $self->pass_test ("The CGI executable produced some output on the error stream as follows:\n$self->{errors}\n");
402 }
403 else {
404 0 $self->fail_test ("Expecting errors, but the CGI executable did not produce any output on the error stream");
405 }
406 }
407 else {
408 20 ▶ if ($self->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'errors'}) { } 0 20 20
409 0 $self->fail_test ("Not expecting errors, but the CGI executable produced some output on the error stream as follows:\n$self->{errors}\n");
410 }
411 else {
412 20 $self->pass_test ("The CGI executable did not produce any output on the error stream");
413 }
414 }
415  
416 20 $self->tidy_files ();
417  
418 20 return;
419 }
420  
421  
422 # my %token_valid_chars;
423 # @token_valid_chars{0..127} = (1) x 128;
424 # my @ctls = (0..31,127);
425 # @token_valid_chars{@ctls} = (0) x @ctls;
426 # my @tspecials =
427 # ('(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
428 # '/', '[', ']', '?', '=', '{', '}', \x32, \x09 );
429 # @token_valid_chars{@tspecials} = (0) x @tspecials;
430  
431 # These regexes are for testing the validity of the HTTP headers
432 # produced by the CGI script.
433  
434 5 my $HTTP_CTL = qr/[\x{0}-\x{1F}\x{7f}]/;
435  
436 5 my $HTTP_TSPECIALS = qr/[\x{09}\x{20}\x{22}\x{28}\x{29}\x{2C}\x{2F}\x{3A}-\x{3F}\x{5B}-\x{5D}\x{7B}\x{7D}]/;
437  
438 5 my $HTTP_TOKEN = '[\x{21}\x{23}-\x{27}\x{2a}\x{2b}\x{2d}\x{2e}\x{30}-\x{39}\x{40}-\x{5a}\x{5e}-\x{7A}\x{7c}\x{7e}]';
439  
440 5 my $HTTP_TEXT = qr/[^\x{0}-\x{1F}\x{7f}]/;
441  
442 # This does not include [CRLF].
443  
444 5 my $HTTP_LWS = '[\x{09}\x{20}]';
445  
446 5 my $qd_text = qr/[^"\x{0}-\x{1f}\x{7f}]/;
447 5 my $quoted_string = qr/"$qd_text+"/;
448 5 my $field_content = qr/(?:$HTTP_TEXT)*|
449 (?:
450 $HTTP_TOKEN|
451 $HTTP_TSPECIALS|
452 $quoted_string
453 )*
454 /x;
455  
456 5 my $http_token = qr/(?:$HTTP_TOKEN+)/;
457  
458 # Check for a valid content type line.
459  
460 sub check_content_line_private
461 15 ▶{
Subroutine check_content_line_private: called CC=6 SCAR=17.9
Pod undocumented
462 15 my ($self, $header, $verbose) = @_;
463  
464 15 my $expected_charset = $self->{expected_charset};
465  
466 15 $self->note ("I am checking to see if the output contains a valid content type line.");
467 15 my $content_type_ok;
468 15 my $has_content_type = ($header =~ m!(Content-Type:\s*.*)!i);
469 15 my $content_type_line = $1;
470 15 $self->do_test ($has_content_type, "There is a Content-Type header");
471 15 ▶ if (! $has_content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_content_type) 1 14 15
472 1 return;
473 }
474 14 my $lineok = ($content_type_line =~ m!^Content-Type:(?:$HTTP_LWS)+
475 ($http_token/$http_token)
476 !xi);
477 14 my $mime_type = $1;
478 14 $self->do_test ($lineok, "The Content-Type header is well-formed");
479 14 ▶ if (! $lineok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lineok) 0 14 14
480 0 return;
481 }
482 14 ▶ if ($self->{mime_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mime_type"}) 2 12 14
483 $self->do_test ($mime_type eq $self->{mime_type},
484 2 "Got expected mime type $mime_type = $self->{mime_type}");
485 }
486 14 ▶ if ($expected_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_charset) 9 5 14
487 9 my $has_charset = ($content_type_line =~ /charset
488 =
489 (
490 $http_token|
491 $quoted_string
492 )/xi);
493 9 my $charset = $1;
494 9 $self->do_test ($has_charset, "Specifies a charset");
495 9 ▶ if ($has_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_charset) 8 1 9
496 8 $charset =~ s/^"(.*)"$/$1/;
497 8 $self->do_test (lc $charset eq lc $expected_charset,
498 "Got expected charset $charset = $expected_charset");
499 }
500 }
501 }
502  
503 sub check_http_header_syntax_private
504 20 ▶{
Subroutine check_http_header_syntax_private: called CC=7 SCAR=20.0
Pod undocumented
505 20 my ($self, $header, $verbose) = @_;
506 20 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 16 20
507 4 $self->note ("Checking the HTTP header.");
508 }
509 20 my @lines = split /\r?\n/, $header;
510 20 my $line_number = 0;
511 20 my $bad_headers = 0;
512 20 my %headers;
513 20 my $line_re = qr/($HTTP_TOKEN+):$HTTP_LWS+(.*)/;
514 # print "Line regex is $line_re\n";
515 20 for my $line (@lines) {
516 29 ▶ if ($line =~ /^$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$/) 0 29 29
517 0 ▶ if ($line_number == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_number == 0) { } 0 0 0
518 0 $self->fail_test ("The output of the CGI executable has a blank line as its first line");
519 }
520 else {
521 0 $self->pass_test ("There are $line_number valid header lines");
522 }
523 # We have finished looking at the headers.
524 0 last;
525 }
526 29 $line_number += 1;
527 29 ▶ if ($line !~ $line_re) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /$line_re/) { } 1 28 29
528 1 $self->fail_test ("The header on line $line_number, '$line', appears not to be a correctly-formed HTTP header");
529 1 $bad_headers++;
530 }
531 else {
532 28 my $key = lc $1;
533 28 my $value = $2;
534 28 $headers{$key} = $value;
535 28 $self->pass_test ("The header on line $line_number, '$line', appears to be a correctly-formed HTTP header");
536 }
537 }
538 20 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 16 20
539 4 print "# I have finished checking the HTTP header for consistency.\n";
540 }
541 20 $self->{run_options}{headers} = \%headers;
542 }
543  
544 # The output is required to have a blank line even if it has no body.
545  
546 sub check_blank_line
547 20 ▶{
Subroutine check_blank_line: called CC=1 SCAR=0.0
Pod undocumented
548 20 my ($self, $output) = @_;
549 20 my $blank = ($output =~ /\r?\n\r?\n/);
550 20 $self->{tb}->ok ($blank, "Output contains a blank line");
551 }
552  
553 # Check whether the headers of the CGI output are well-formed.
554  
555 sub check_headers_private
556 20 ▶{
Subroutine check_headers_private: called CC=3 SCAR=11.1
Pod undocumented
557 20 my ($self) = @_;
558  
559 # Extract variables from the object
560  
561 20 my $verbose = $self->{verbose};
562 20 my $output = $self->{run_options}->{output};
563 20 ▶ if (! $output) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output) 0 20 20
564 0 $self->note ("No output, skipping header tests");
565 0 return;
566 }
567 20 check_blank_line ($self, $output);
568 20 my ($header, $body) = split /\r?\n\r?\n/, $output, 2;
569 20 check_http_header_syntax_private ($self, $header, $verbose);
570 20 ▶ if (! $self->{no_check_content}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_check_content"}) 15 5 20
571 15 check_content_line_private ($self, $header, $verbose);
572 }
573  
574 20 $self->{run_options}->{header} = $header;
575 20 $self->{run_options}->{body} = $body;
576 }
577  
578 # This is "safe" in the sense that it falls back to using
579 # IO::Uncompress::Gunzip if it can't find Gzip::Faster. However, it
580 # throws an exception if it fails, so it's not really "safe".
581  
582 sub gunzip_safe
583 5 ▶{
Subroutine gunzip_safe: called CC=3 SCAR=14.8
Pod undocumented
584 5 my ($self, $content) = @_;
585 5 my $out;
586 5 ▶ if ($self->{_use_io_uncompress_gunzip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_use_io_uncompress_gunzip'}) { } 0 5 5
587 # gunzip_safe is called within an eval block. It's possible
588 # that the require might fail, but trying to fix these kinds
589 # of problems goes beyond the scope of this module.
590 0 eval "use IO::Uncompress::Gunzip;";
591 0 my $status = IO::Uncompress::Gunzip::gunzip (\$content, \$out);
592 0 ▶ if (! $status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
593 0 die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError";
594 }
595 }
596 else {
597 # We have already loaded Gzip::Faster within
598 # do_compression_test.
599 5 $out = Gzip::Faster::gunzip ($content);
600 }
601 5 return $out;
602 }
603  
604 sub check_compression_private
605 6 ▶{
Subroutine check_compression_private: called CC=7 SCAR=21.1
Pod undocumented
606 6 my ($self) = @_;
607 6 my $body = $self->{run_options}->{body};
608 6 my $header = $self->{run_options}->{header};
609 6 my $verbose = $self->{verbose};
610 6 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 2 6
611 4 print "# I am testing whether compression has been applied to the output.\n";
612 }
613 6 ▶ if ($header !~ /Content-Encoding:.*\bgzip\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $header =~ /Content-Encoding:.*\bgzip\b/i) { } 1 5 6
614 1 $self->fail_test ("Output does not have a header indicating compression");
615 }
616 else {
617 5 $self->pass_test ("The header claims that the output is compressed");
618 5 my $uncompressed;
619 #printf "The length of the body is %d\n", length ($body);
620 5 eval {
621 5 $uncompressed = $self->gunzip_safe ($body);
622 };
623 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 5 5
624 0 $self->fail_test ("Output claims to be in gzip format but gunzip on the output failed with the error '$@'");
625 0 my $failedfile = "$0.gunzip-failure.$$";
626 0 ▶ open my $temp, ">:bytes", $failedfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $temp, ">:bytes", $failedfile or die $! 0 0 0
627 0 print $temp $body;
628 0 ▶ close $temp or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $temp or die $! 0 0 0
629 0 print "# Saved failed output to $failedfile.\n";
630 }
631 else {
632 5 my $uncomp_size = length $uncompressed;
633 5 my $percent_comp = sprintf ("%.1f%%", (100 * length ($body)) / $uncomp_size);
634 5 $self->pass_test ("The body of the CGI output was able to be decompressed using 'gunzip'. The uncompressed size is $uncomp_size. The compressed output is $percent_comp of the uncompressed size.");
635
636 5 $self->{run_options}->{body} = $uncompressed;
637 }
638 }
639 6 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 2 6
640 4 print "# I have finished testing the compression.\n";
641 }
642 }
643  
644 sub set_no_check_content
645 2 ▶{
Subroutine set_no_check_content: called CC=2 SCAR=7.4
Pod documented
646 2 my ($self, $value) = @_;
647 2 my $verbose = $self->{verbose};
648 2 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
649 0 print "# I am setting no content check to $value.\n";
650 }
651 2 $self->{no_check_content} = $value;
652 }
653  
654 sub test_not_implemented
655 1 ▶{
Subroutine test_not_implemented: called CC=2 SCAR=7.0
Pod documented
656 1 my ($self, $method) = @_;
657 1 my %options;
658 1 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 0 1 1
659 0 $options{REQUEST_METHOD} = $method;
660 }
661 else {
662 1 $options{REQUEST_METHOD} = 'GOBBLEDIGOOK';
663 }
664 1 $options{no_check_request_method} = 1;
665 1 my $saved_no_check_content = $self->{no_check_content};
666 1 $self->{no_check_content} = 1;
667 1 $self->{run_options} = \%options;
668 1 run_private ($self);
669 #print $options{output}, "\n";
670 1 $self->check_headers_private ();
671 1 $self->test_status (501);
672 1 $self->{no_check_content} = $saved_no_check_content;
673 1 $self->clear_env ();
674 }
675  
676 sub test_status
677 3 ▶{
Subroutine test_status: called CC=3 SCAR=11.0
Pod documented
678 3 my ($self, $status) = @_;
679 3 ▶ if ($status !~ /^[0-9]{3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status =~ /^[0-9]{3}$/) 1 2 3
680 1 carp "$status is not a valid HTTP status, use a number like 301 or 503";
681 1 return;
682 }
683 2 my $headers = $self->{run_options}{headers};
684 2 ▶ if (! $headers) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers) 1 1 2
685 1 carp "no headers in this object; have you run a test yet?";
686 1 return;
687 }
688 1 $self->{tb}->ok ($headers->{status}, "Got status header");
689 1 $self->{tb}->like ($headers->{status}, qr/$status/, "Got $status status");
690 }
691  
692  
693 sub test_method_not_allowed
694 1 ▶{
Subroutine test_method_not_allowed: called CC=5 SCAR=16.3
Pod documented
695 1 my ($self, $bad_method) = @_;
696 1 my $tb = $self->{tb};
697 1 my %options;
698 1 $options{REQUEST_METHOD} = $bad_method;
699 1 $options{no_check_request_method} = 1;
700 1 my $saved_no_check_content = $self->{no_check_content};
701 1 $self->{no_check_content} = 1;
702 1 $self->{run_options} = \%options;
703 1 run_private ($self);
704 1 $self->check_headers_private ();
705 1 my $headers = $options{headers};
706 1 $tb->ok ($headers->{allow}, "Got Allow header");
707 1 $tb->like ($headers->{status}, qr/405/, "Got method not allowed status");
708 1 $self->clear_env ();
709 1 ▶ if ($headers->{allow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->{"allow"}) 1 0 1
710 1 my @allow = split /,\s*/, $headers->{allow};
711 1 my $saved_no_warn = $self->{no_warn};
712 1 $self->{no_warn} = 1;
713 1 for my $ok_method (@allow) {
714 # Run the program with each of the headers we were told were
715 # allowed, and see whether the program executes correctly.
716 2 my %op2;
717 2 $op2{REQUEST_METHOD} = $ok_method;
718 2 ▶ if ($ok_method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok_method eq "POST") 0 2 2
719 0 $op2{CONTENT_TYPE} = 'application/x-www-form-urlencoded';
720 0 $op2{input} = 'a=b';
721 # $op2{CONTENT_LENGTH} = length ($op2{input});
722 }
723 2 $self->{run_options} = \%op2;
724 2 run_private ($self);
725 2 $self->check_headers_private ();
726 2 my $headers2 = $op2{headers};
727 # Check that either there is no status line (defaults to 200),
728 # or that there is a status line, and it has status 200.
729 2 ▶ $tb->ok (! $headers2->{status} || $headers2->{status} =~ /200/,
Condition33%1/3
!$headers2->{'status'} || $headers2->{'status'} =~ /200/
l!l&&r!l&&!r
200
MC/DC0%0/2
!$headers2->{'status'} || $headers2->{'status'} =~ /200/
!$headers2->{'status'} $headers2->{'status'} =~ /200/
Truth table33%1/3
not(A) || B
A= !$headers2->{'status'}B= $headers2->{'status'} =~ /200/
ABresult
1X1
011
000
730 "Method $ok_method specified by Allow: header was allowed");
731 2 $self->clear_env ();
732 }
733 1 $self->{no_warn} = $saved_no_warn;
734 }
735 1 $self->{no_check_content} = $saved_no_check_content;
736 }
737  
738 # Make a request with CONTENT_LENGTH set to zero and see if the
739 # executable produces a 411 status (content length required).
740  
741 sub test_411
742 0 ▶{
Subroutine test_411: not called CC=7 SCAR=40.3
Pod documented
743 0 my ($self, $options) = @_;
744 0 ▶ if (! $options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options) 0 0 0
745 0 $options = {};
746 }
747 0 $self->{bad_content_length} = 1;
748 0 my $rm;
749 0 ▶ if ($options->{REQUEST_METHOD} && $options->{REQUEST_METHOD} ne 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"REQUEST_METHOD"} and $options->{"REQUEST_METHOD"} ne "POST") 0 0 0
Condition0%0/3
$options->{"REQUEST_METHOD"} and $options->{"REQUEST_METHOD"} ne "POST"
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"REQUEST_METHOD"} and $options->{"REQUEST_METHOD"} ne "POST"
$options->{"REQUEST_METHOD"} $options->{"REQUEST_METHOD"} ne "POST"
Truth table0%0/3
A and B
A= $options->{"REQUEST_METHOD"}B= $options->{"REQUEST_METHOD"} ne "POST"
ABresult
0X0
100
111
750 0 $rm = $options->{REQUEST_METHOD};
751 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
752 0 carp "test_411 requires REQUEST_METHOD to be POST";
753 }
754 }
755 0 $options->{REQUEST_METHOD} = 'POST';
756 0 ▶ if (! $options->{CONTENT_TYPE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"CONTENT_TYPE"}) 0 0 0
757 0 $options->{CONTENT_TYPE} = 'application/x-www-form-urlencoded';
758 }
759 0 ▶ if (! $options->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"input"}) 0 0 0
760 0 $options->{input} = 'this does not have a zero length';
761 }
762 0 my $saved_no_check_content = $self->{no_check_content};
763 0 $self->{no_check_content} = 1;
764 0 $self->{run_options} = $options;
765 0 $self->run_private ();
766 # This has to be run to parse the headers.
767 0 $self->check_headers_private ();
768 0 $self->test_status (411);
769 # Delete everything from $self so that it can be used again.
770 0 $self->{bad_content_length} = undef;
771 0 $self->{run_options} = undef;
772 0 $self->clear_env ();
773 0 $self->{no_check_content} = $saved_no_check_content;
774 # Put the user's %options back to how it was.
775 0 $options->{REQUEST_METHOD} = $rm;
776 }
777  
778 sub test_options
779 0 ▶{
Subroutine test_options: not called CC=1 SCAR=6.9
Pod documented
780 0 my ($self) = @_;
781 0 my %options = (
782 REQUEST_METHOD => 'OPTIONS',
783 QUERY_STRING => '',
784 );
785 0 $self->{run_options} = \%options;
786 0 $valid_request_method{OPTIONS} = 1;
787 0 $self->run_private ();
788 0 $self->check_headers_private ();
789 0 my $headers = $options{headers};
790 0 $self->do_test ($headers->{allow}, "Got allow header");
791 0 delete $valid_request_method{OPTIONS};
792 }
793  
794 # Send bullshit queries expecting a 400 response.
795  
796 sub test_broken_queries
797 0 ▶{
Subroutine test_broken_queries: not called CC=2 SCAR=17.9
Pod undocumented
798 0 my ($self, $options, $queries) = @_;
799 0 for my $query (@$queries) {
800 0 $ENV{QUERY_STRING} = $query;
801 0 $self->run ($options);
802 # test for 400 header
803 0 $self->test_status (400);
804 }
805 }
806  
807 # Clear all the environment variables we have set ourselves.
808  
809 sub clear_env
810 20 ▶{
Subroutine clear_env: called CC=2 SCAR=6.9
Pod undocumented
811 20 my ($self) = @_;
812 20 for my $e (@{$self->{set_env}}) {
813 # print "Deleting environment variable $e\n";
814 49 $ENV{$e} = undef;
815 }
816 20 $self->{set_env} = undef;
817 }
818  
819 sub run
820 16 ▶{
Subroutine run: called CC=35 SCAR=51.3
Pod documented
821 16 my ($self, $options) = @_;
822 16 ▶ if (ref $options ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options ne "HASH") 0 16 16
823 0 carp "Use a hash reference as argument, \$tester->run (\\\%options);";
824 0 return;
825 }
826 16 my $verbose = $self->{verbose};
827 16 local $Test::Builder::Level = $Test::Builder::Level + 1;
828 16 ▶ if (! $self->{cgi_executable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cgi_executable"}) 0 16 16
829 0 croak "You have requested me to run a CGI executable with 'run' without telling me what it is you want me to run. Please tell me the name of the CGI executable using the method 'set_cgi_executable'.";
830 }
831 16 ▶ if (! $options) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $options) { } 0 16 16
832 0 $self->{run_options} = {};
833 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
834 0 carp "You have requested me to run a CGI executable with 'run' without specifying a hash reference to store the input, output, and error output. I can only run basic tests of correctness";
835 }
836 }
837 else {
838 16 $self->{run_options} = $options;
839 }
840 16 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 4 12 16
841 4 print "# I am commencing the testing of CGI executable '$self->{cgi_executable}'.\n";
842 }
843 16 ▶ if ($options->{html} && ! $self->{no_warn}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options->{'html'} and not $self->{'no_warn'}) { } 0 16 16
elsif ($options->{'json'} and not $self->{'no_warn'}) { } 2 14 16
elsif ($options->{'png'} and not $self->{'no_warn'}) { } 0 28 28
Condition33%1/3
$options->{"html"} and not $self->{"no_warn"}
!ll&&!rl&&r
1600
MC/DC0%0/2
$options->{"html"} and not $self->{"no_warn"}
$options->{"html"} not $self->{"no_warn"}
Truth table33%1/3
A and not(B)
A= $options->{"html"}B= not $self->{"no_warn"}
ABresult
0X0
100
111
844 0 ▶ if ($self->{mime_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mime_type'}) { } 0 0 0
845 0 ▶ if ($self->{mime_type} ne 'text/html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mime_type"} ne "text/html") 0 0 0
846 0 carp "If you want to test for HTML output, you should also specify a mime type 'text/html', but you have specified '$self->{mime_type}'";
847 }
848 }
849 else {
850 0 carp "If you want to check for html validity, you should also check the mime type is 'text/html' using expect_mime_type";
851 }
852 }
853 ▶ elsif ($options->{json} && ! $self->{no_warn}) {
Condition66%2/3
$options->{"json"} and not $self->{"no_warn"}
!ll&&!rl&&r
1402
MC/DC50%1/2
$options->{"json"} and not $self->{"no_warn"}
$options->{"json"} not $self->{"no_warn"}
Truth table66%2/3
A and not(B)
A= $options->{"json"}B= not $self->{"no_warn"}
ABresult
0X0
100
111
854 2 my $mime_type = $self->{mime_type};
855 2 ▶ if ($mime_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type) { } 2 0 2
856 2 ▶ if ($mime_type ne 'text/plain' && $mime_type ne 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type ne "text/plain" and $mime_type ne "application/json") 0 2 2
Condition33%1/3
$mime_type ne "text/plain" and $mime_type ne "application/json"
!ll&&!rl&&r
020
MC/DC0%0/2
$mime_type ne "text/plain" and $mime_type ne "application/json"
$mime_type ne "text/plain" $mime_type ne "application/json"
Truth table33%1/3
A and B
A= $mime_type ne "text/plain"B= $mime_type ne "application/json"
ABresult
0X0
100
111
857 0 carp "Your expected mime type of $mime_type is not valid for JSON";
858 }
859 }
860 else {
861 0 carp "There is no expected mime type, use expect_mime_type ('application/json') or expect_mime_type ('text/plain') for JSON output";
862 }
863 }
864 ▶ elsif ($options->{png} && ! $self->{no_warn}) {
Condition33%1/3
$options->{"png"} and not $self->{"no_warn"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$options->{"png"} and not $self->{"no_warn"}
$options->{"png"} not $self->{"no_warn"}
Truth table33%1/3
A and not(B)
A= $options->{"png"}B= not $self->{"no_warn"}
ABresult
0X0
100
111
865 0 my $mime_type = $self->{mime_type};
866 0 ▶ if ($mime_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type) { } 0 0 0
867 0 ▶ if ($mime_type ne 'image/png') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type ne "image/png") 0 0 0
868 0 carp "Your expected mime type of $mime_type is not valid for PNG";
869 }
870 }
871 else {
872 0 carp "There is no expected mime type, use image/png for PNG output";
873 }
874 }
875  
876 16 ▶ if ($options->{png}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'png'}) { } 0 16 16
elsif ($options->{'html'}) { } 0 16 16
877 0 ▶ if ($options->{html} || $options->{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"html"} or $options->{"json"}) 0 0 0
Condition0%0/3
$options->{"html"} or $options->{"json"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"html"} or $options->{"json"}
$options->{"html"} $options->{"json"}
Truth table0%0/3
A or B
A= $options->{"html"}B= $options->{"json"}
ABresult
1X1
011
000
878 0 carp "Contradictory options png and json/html";
879 }
880 }
881 elsif ($options->{html}) {
882 0 ▶ if ($options->{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"json"}) 0 0 0
883 0 carp "Contradictory options json and html";
884 }
885 }
886  
887 # eval {
888 16 run_private ($self);
889 16 my $output = $self->{run_options}->{output};
890 # Jump over the following tests if there is no output. This used
891 # to complain a lot about output and fail tests but this proved a
892 # huge nuisance when creating TODO tests, so just skip over the
893 # output tests if we have already failed the basic "did not
894 # produce output" issue.
895 16 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 16 0 16
896 16 check_headers_private ($self);
897 16 ▶ if ($self->{comp_test}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"comp_test"}) 6 10 16
898 6 check_compression_private ($self);
899 }
900 16 my $ecs = $self->{expected_charset};
901 16 ▶ if ($ecs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ecs) 11 5 16
902 11 ▶ if ($ecs =~ /utf\-?8/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ecs =~ /utf\-?8/i) { } 10 1 11
903 10 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 4 6 10
904 4 print ("# Expected charset '$ecs' looks like UTF-8, sending it to Unicode::UTF8.\n");
905 }
906 10 $options->{body} = decode_utf8 ($options->{body});
907 }
908 else {
909 1 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 1 1
910 0 print ("# Expected charset '$ecs' doesn't look like UTF-8, sending it to Encode.\n");
911 }
912 1 eval {
913 1 $options->{body} = decode ($options->{body}, $ecs);
914 };
915 1 ▶ if (! $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 1 1
916 0 $self->pass_test ("decoded from $ecs encoding");
917 }
918 else {
919 1 $self->fail_test ("decoded from $ecs encoding");
920 }
921 }
922 }
923 16 ▶ if ($self->{cache_test}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_test"}) 0 16 16
924 0 $self->check_caching_private ();
925 }
926 }
927 16 ▶ if ($options->{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"html"}) 0 16 16
928 0 validate_html ($self);
929 }
930 16 ▶ if ($options->{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"json"}) 2 14 16
931 2 validate_json ($self);
932 }
933 16 ▶ if ($options->{png}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"png"}) 0 16 16
934 0 validate_png ($self);
935 }
936 16 $self->clear_env ();
937 }
938  
939 sub tidy_files
940 20 ▶{
Subroutine tidy_files: called CC=5 SCAR=16.7
Pod undocumented
941 20 my ($self) = @_;
942 20 ▶ if ($self->{infile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"infile"}) 3 17 20
943 3 ▶ unlink $self->{infile} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"infile"} or die $! 3 0 3
944 }
945  
946 # Insert HTML test here?
947  
948 20 ▶ unlink $self->{outfile} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"outfile"} or die $! 20 0 20
949 20 ▶ unlink $self->{errfile} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"errfile"} or die $! 20 0 20
950 }
951  
952 sub tfilename
953 3 ▶{
Subroutine tfilename: called CC=1 SCAR=0.0
Pod undocumented
954 3 my $dir = "/tmp";
955 3 my $file = "$dir/temp.$$-" . scalar(time ()) . "-" . int (rand (10000));
956 3 return $file;
957 }
958  
959 sub run3
960 20 ▶{
Subroutine run3: called CC=14 SCAR=27.7
Pod undocumented
961 20 my ($self, $exe) = @_;
962 20 my $cmd = "@$exe";
963 20 ▶ if (defined $self->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'input'}) { } 3 17 20
964 3 $self->{infile} = tfilename ();
965 3 ▶ open my $in, ">:raw", $self->{infile} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, ">:raw", $self->{"infile"} or die $! 3 0 3
966 3 print $in $self->{input};
967 3 ▶ close $in or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $in or die $! 3 0 3
968 3 $cmd .= " < " . $self->{infile};
969 }
970 else {
971 # Make sure that the program does not hang waiting for STDIN
972 # to complete.
973 17 $cmd .= " < /dev/null ";
974 }
975 20 my $out;
976 20 ($out, $self->{outfile}) = tempfile ("/tmp/output-XXXXXX");
977 20 ▶ close $out or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die $! 20 0 20
978 20 my $err;
979 20 ($err, $self->{errfile}) = tempfile ("/tmp/errors-XXXXXX");
980 20 ▶ close $err or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $err or die $! 20 0 20
981
982 20 my $status = system ("$cmd > $self->{outfile} 2> $self->{errfile}");
983  
984 20 $self->{output} = '';
985 20 ▶ if (-f $self->{outfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"outfile"}) 20 0 20
986 20 ▶ open my $out, "<", $self->{outfile} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, "<", $self->{"outfile"} or die $! 20 0 20
987 20 ▶ while (<$out>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $out)) 66 20 86
988 66 $self->{output} .= $_;
989 }
990 20 ▶ close $out or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die $! 20 0 20
991 }
992 20 $self->{errors} = '';
993 20 ▶ if (-f $self->{errfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"errfile"}) 20 0 20
994 20 ▶ open my $err, "<", $self->{errfile} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $err, "<", $self->{"errfile"} or die $! 20 0 20
995 20 ▶ while (<$err>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $err)) 0 20 20
996 0 $self->{errors} .= $_;
997 }
998 20 ▶ close $err or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $err or die $! 20 0 20
999 }
1000  
1001 # print "OUTPUT IS $self->{output}\n";
1002 # print "$$errors\n";
1003 # exit;
1004  
1005 20 return $status;
1006 }
1007  
1008 sub set_html_validator
1009 0 ▶{
Subroutine set_html_validator: not called CC=5 SCAR=34.0
Pod documented
1010 0 my ($self, $hvc) = @_;
1011 0 ▶ if (! $hvc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hvc) 0 0 0
1012 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
1013 0 carp "Invalid value for validator";
1014 }
1015 0 return;
1016 }
1017 0 ▶ if (! -x $hvc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-x $hvc) 0 0 0
1018 0 ▶ if (! $self->{no_warn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_warn"}) 0 0 0
1019 0 carp "$hvc doesn't seem to be an executable program";
1020 }
1021 }
1022 0 $self->{html_validator} = $hvc;
1023 }
1024  
1025 sub validate_html
1026 0 ▶{
Subroutine validate_html: not called CC=12 SCAR=50.5
Pod undocumented
1027 0 my ($self) = @_;
1028 0 my $html_validator = $self->{html_validator};
1029 0 ▶ if (! $html_validator || ! -x $html_validator) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($html_validator and -x $html_validator) 0 0 0
Condition0%0/3
$html_validator and -x $html_validator
!ll&&!rl&&r
000
MC/DC0%0/2
$html_validator and -x $html_validator
$html_validator -x $html_validator
Truth table0%0/3
A and B
A= $html_validatorB= -x $html_validator
ABresult
0X0
100
111
1030 0 warn "HTML validation could not be completed, set validator to executable program using \$tce->set_html_validator ('command')";
1031 0 return;
1032 }
1033 0 my $html_validate = "$Bin/html-validate-temp-out.$$";
1034 0 my $html_temp_file = "$Bin/html-validate-temp.$$.html";
1035 0 ▶ open my $htmltovalidate, ">:encoding(utf8)", $html_temp_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $htmltovalidate, ">:encoding(utf8)", $html_temp_file or die $! 0 0 0
1036 0 print $htmltovalidate $self->{run_options}->{body};
1037 0 ▶ close $htmltovalidate or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $htmltovalidate or die $! 0 0 0
1038 0 my $status = system ("$html_validator $html_temp_file > $html_validate");
1039
1040 0 $self->do_test (! -s $html_validate, "HTML is valid");
1041 0 ▶ if (-s $html_validate) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $html_validate) 0 0 0
1042 0 ▶ open my $in, "<", $html_validate or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $html_validate or die $! 0 0 0
1043 0 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 0 0 0
1044 0 print ("# $_");
1045 }
1046 0 ▶ close $in or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $in or die $! 0 0 0
1047 }
1048 0 ▶ unlink $html_temp_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $html_temp_file or die $! 0 0 0
1049 0 ▶ if (-f $html_validate) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $html_validate) 0 0 0
1050 0 ▶ unlink $html_validate or die $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $html_validate or die $! 0 0 0
1051 }
1052 }
1053  
1054 sub validate_json
1055 2 ▶{
Subroutine validate_json: called CC=3 SCAR=11.0
Pod undocumented
1056 2 my ($self) = @_;
1057 2 my $json = $self->{run_options}->{body};
1058 2 ▶ eval "use JSON::Parse 'valid_json';";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1059 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
1060 0 croak "JSON::Parse is not installed, cannot validate JSON";
1061 }
1062 2 my $valid = valid_json ($json);
1063 2 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) { } 1 1 2
1064 1 $self->pass_test ("Valid JSON");
1065 }
1066 else {
1067 1 $self->fail_test ("Valid JSON");
1068 }
1069 }
1070  
1071 sub validate_png
1072 0 ▶{
Subroutine validate_png: not called CC=2 SCAR=17.9
Pod undocumented
1073 0 my ($self) = @_;
1074 0 eval "use Image::PNG::Libpng 'read_from_scalar';";
1075 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1076 0 croak "Image::PNG::Libpng is not installed, cannot validate PNG";
1077 }
1078 0 my $body = $self->{run_options}->{body};
1079 0 my $png;
1080 0 eval {
1081 0 $png = read_from_scalar ($body);
1082 };
1083 0 $self->{tb}->ok (!$@, "Could read PNG from body");
1084 0 $self->{tb}->ok ($png, "Got a valid value for PNG");
1085 0 $self->{run_options}{pngdata} = $png;
1086 }
1087  
1088 5 1;
1089  
↑ Summary examples/bad-method.cgi »