CGI-Info
view release on metacpan or search on metacpan
delete $ENV{'QUERY_STRING'};
$ENV{'REQUEST_METHOD'} = 'GET';
$i = new_ok('CGI::Info');
ok(!defined($i->params()));
ok($i->as_string() eq '');
$ENV{'REQUEST_METHOD'} = 'POST';
delete $ENV{'CONTENT_LENGTH'};
$i = new_ok('CGI::Info');
ok(!defined($i->params()));
ok($i->as_string() eq '');
my $input = 'foo=bar';
$ENV{'CONTENT_LENGTH'} = length($input);
open (my $fin, '<', \$input);
local *STDIN = $fin;
$i = new_ok('CGI::Info');
%p = %{$i->params()};
ok($p{foo} eq 'bar'); # Fails on Perl 5.6.2
ok(!defined($p{fred}));
ok($i->as_string() eq 'foo=bar');
close $fin;
# Creating a second object should give the same parameters, without
# reading
$i = new_ok('CGI::Info');
%p = %{$i->params()};
ok($p{foo} eq 'bar');
ok(!defined($p{fred}));
ok($i->as_string() eq 'foo=bar');
# TODO: find and use a free filename, otherwise /tmp/hello.txt
# will be overwritten if it exists
$ENV{'CONTENT_TYPE'} = 'Multipart/form-data; boundary=-----xyz';
$input = <<'EOF';
-------xyz
Content-Disposition: form-data; name="country"
44
-------xyz
Content-Disposition: form-data; name="datafile"; filename="hello.txt"
Content-Type: text/plain
Hello, World
-------xyz--
EOF
$ENV{'CONTENT_LENGTH'} = length($input);
open ($fin, '<', \$input);
local *STDIN = $fin;
CGI::Info->reset(); # Force stdin re-read
my $tmpdir = File::Spec->tmpdir();
if(!-w $tmpdir) {
BAIL_OUT("Your temporary directory ' $tmpdir' isn't writable, fix your configuration and try again");
}
$i = new_ok('CGI::Info' => [
upload_dir => $tmpdir
]);
%p = %{$i->params()};
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
my $filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(-e $filename);
ok(-r $filename);
unlink($filename);
close $fin;
$ENV{'REQUEST_METHOD'} = 'GET';
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => $tmpdir
]);
$ENV{'QUERY_STRING'} = 'foo=bar';
eval { %p = $i->params() };
ok($@ =~ /Multipart.+ not supported for GET/);
delete $ENV{'QUERY_STRING'};
open ($fin, '<', \$input);
local *STDIN = $fin;
$ENV{'REQUEST_METHOD'} = 'POST';
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info');
%p = %{$i->params(upload_dir => $tmpdir)};
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(-e $filename) || diag("$filename doesn't exist");
ok(-r $filename);
unlink($filename);
close $fin;
$input = <<'EOF';
-------xyz
Content-Disposition: form-data; name="country"
44
-------xyz
Content-Disposition: form-data; name=".hidden"; filename="/.trojanhorse.js"
Content-Type: text/plain
I would do nasty things, but my upload will be disallowed
-------xyz--
EOF
$ENV{'CONTENT_LENGTH'} = length($input);
open ($fin, '<', \$input);
local *STDIN = $fin;
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => $tmpdir
]);
eval { %p = %{$i->params()} };
ok(defined($@));
like($@, qr/Disallowing invalid filename/);
ok(defined($p{country}));
ok($p{country} == 44);
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
$input = <<'EOF';
-------xyz
Content-Disposition: form-data; name="country"
44
-------xyz
Content-Disposition: form-data; name="datafile"; filename="hello.txt"
Content-Type: text/plain
Hello, World
-------xyz--
EOF
$ENV{'CONTENT_LENGTH'} = length($input);
open ($fin, '<', \$input);
local *STDIN = $fin;
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => '/does_not_exist11',
]);
eval { %p = %{$i->params()} };
ok($@ =~ /isn't a directory/);
ok(defined($p{country}));
ok($p{country} == 44);
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
open ($fin, '<', \$input);
local *STDIN = $fin;
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => undef,
]);
eval { %p = $i->params() };
ok($@ =~ /Attempt to upload a file when upload_dir has not been set/);
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
SKIP: {
# e.g. running as root, or on Windows
skip 'Root directory is writable', 7 if(-w '/');
open ($fin, '<', \$input);
local *STDIN = $fin;
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => '/',
]);
eval { %p = $i->params() };
ok($@ =~ /isn't writeable/);
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
}
open ($fin, '<', \$input);
local *STDIN = $fin;
my $script_path = $i->script_path();
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => $script_path,
]);
eval { %p = $i->params() };
ok($@ =~ /isn't a directory/);
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
open ($fin, '<', \$input);
local *STDIN = $fin;
$script_path = $i->script_path();
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => '.',
]);
eval { %p = $i->params() };
ok($@ =~ /isn't a full pathname/);
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
$ENV{'CONTENT_TYPE'} = 'xyzzy';
open ($fin, '<', \$input);
local *STDIN = $fin;
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => $tmpdir
]);
eval { %p = $i->params() };
ok($@ =~ /POST: Invalid or unsupported content type: xyzzy/);
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
$ENV{'CONTENT_TYPE'} = 'Multipart/form-data; boundary=-----xyz';
$input = <<'EOF';
-------xyz
Content-Disposition: form-data; name="country"
44
-------xyz
Content-Disposition: form-data; name="datafile"; filename="../../../passwd"
Content-Type: text/plain
Hello, World
-------xyz--
EOF
open ($fin, '<', \$input);
local *STDIN = $fin;
$script_path = $i->script_path();
CGI::Info->reset(); # Force stdin re-read
$i = new_ok('CGI::Info' => [
upload_dir => $tmpdir
]);
eval { %p = $i->params() };
ok($@ =~ /Disallowing invalid filename/);
ok(defined($p{country}));
ok($p{country} eq '44');
ok($p{datafile} =~ /^hello.txt_.+/);
$filename = File::Spec->catfile($tmpdir, $p{datafile});
ok(!-e $filename);
ok(!-r $filename);
close $fin;
$ENV{'REQUEST_METHOD'} = 'DELETE';
$ENV{'QUERY_STRING'} = 'laleh=tulip';
$i = new_ok('CGI::Info');
eval { %p = $i->params() };
cmp_ok(scalar(keys(%p)), '==', 0, 'params: DELETE mode is not supported');
cmp_ok($i->status(), '==', 405, 'params: DELETE sets HTTP status to 405');
# Check params are read from command line arguments for testing scripts
delete $ENV{'GATEWAY_INTERFACE'};
delete $ENV{'REQUEST_METHOD'};
delete $ENV{'QUERY_STRING'};
@ARGV = ('foo=bar', 'fred=wilma' );
$i = new_ok('CGI::Info');
%p = %{$i->params(logger => MyLogger->new())};
ok($p{fred} eq 'wilma');
ok($i->as_string() eq 'foo=bar; fred=wilma');
ok(!$i->is_mobile());
@ARGV= ('file=/../../../../etc/passwd%00');
$i = new_ok('CGI::Info');
dies_ok { %p = %{$i->params()} }; # Warns because logger isn't set
like($@, qr/Blocked directory traversal attack/);
diag(Data::Dumper->new([$i->messages()])->Dump()) if($ENV{'TEST_VERBOSE'});
like(
$i->messages()->[1]->{'message'},
qr/^Blocked directory traversal attack for 'file'/,
'Warning generated for disallowed parameter'
);
cmp_ok($i->messages()->[1]->{'level'}, 'eq', 'warn');
like($i->messages_as_string(), qr/Blocked directory traversal attack/, 'messages_as_string works');
@ARGV= ('file=/etc/passwd%00');
$i = new_ok('CGI::Info');
lives_ok { %p = %{$i->params()}; };
like($p{'file'}, qr/passwd$/, 'strip NUL byte poison');
@ARGV = ('--mobile', 'foo=bar', 'fred=wilma' );
$i = new_ok('CGI::Info');
%p = %{$i->params()};
ok($p{fred} eq 'wilma');
ok($i->as_string() eq 'foo=bar; fred=wilma');
ok($i->is_mobile());
@ARGV = ('--tablet', 'foo=bar', 'fred=wilma' );
$i = new_ok('CGI::Info');
%p = %{$i->params()};
ok($p{fred} eq 'wilma');
ok($i->as_string() eq 'foo=bar; fred=wilma');
ok(!$i->is_mobile());
( run in 2.830 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )