Unified all tests to use the LighyTest framework

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.3.x@388 152afb58-edef-0310-8abb-c4023f1b3aa9
svn/tags/lighttpd-1.3.14
Jan Kneschke 18 years ago
parent aff653fdba
commit 7a25f1b5f5

@ -0,0 +1,239 @@
#! /usr/bin/perl -w
package LightyTest;
use strict;
use IO::Socket;
use Test::More;
sub new {
my $class = shift;
my $self = {};
$self->{CONFIGFILE} = 'lighttpd.conf';
$self->{BASEDIR} = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
$self->{SRCDIR} = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
$self->{LIGHTTPD_PATH} = $self->{BASEDIR}.'/src/lighttpd';
$self->{LIGHTTPD_PIDFILE} = '/tmp/lighttpd/lighttpd.pid';
$self->{PIDOF_PIDFILE} = '/tmp/lighttpd/pidof.pid';
$self->{PORT} = 2048;
bless($self, $class);
return $self;
}
sub pidof {
my $self = shift;
my $prog = shift;
open F, "ps ax | grep $prog | grep -v grep | awk '{ print \$1 }'|" or
open F, "ps -ef | grep $prog | grep -v grep | awk '{ print \$2 }'|" or
return -1;
my $pid = <F>;
close F;
if (defined $pid) { return $pid; }
return -1;
}
sub stop_proc {
my $self = shift;
open F, $self->{LIGHTTPD_PIDFILE} or return -1;
my $pid = <F>;
close F;
if (defined $pid) {
kill('TERM',$pid) or return -1;
select(undef, undef, undef, 0.01);
}
return 0;
}
sub start_proc {
my $self = shift;
# kill old proc if necessary
$self->stop_proc;
# pre-process configfile if necessary
#
my $pwd = `pwd`;
chomp($pwd);
unlink("/tmp/cfg.file");
system("cat ".$self->{SRCDIR}."/".$self->{CONFIGFILE}.' | perl -pe "s#\@SRCDIR\@#'.$pwd.'/'.$self->{BASEDIR}.'/tests/#" > /tmp/cfg.file');
unlink($self->{LIGHTTPD_PIDFILE});
system($self->{LIGHTTPD_PATH}." -f /tmp/cfg.file");
# system("valgrind --tool=memcheck --show-reachable=yes --leak-check=yes --logfile=foo ".$lighttpd_path." -D -f /tmp/cfg.file &");
sleep(1);
unlink("/tmp/cfg.file");
# no pidfile, we failed
if (not -e $self->{LIGHTTPD_PIDFILE}) {
diag(sprintf('Could not find pidfile: %s', $self->{LIGHTTPD_PIDFILE}));
return -1;
}
# the process is gone, we failed
if (0 == kill 0, `cat $self->{LIGHTTPD_PIDFILE}`) {
diag(sprintf('the process referenced by %s is not up', $self->{LIGHTTPD_PIDFILE}));
return -1;
}
0;
}
sub handle_http {
my $self = shift;
my $t = shift;
my $EOL = "\015\012";
my $BLANK = $EOL x 2;
my $host = "127.0.0.1";
my @request = $t->{REQUEST};
my @response = $t->{RESPONSE};
my $remote =
IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $self->{PORT})
or return -1;
$remote->autoflush(1);
foreach(@request) {
# pipeline requests
s/\r//g;
s/\n/$EOL/g;
print $remote $_.$BLANK;
}
my $lines = "";
# read everything
while(<$remote>) {
$lines .= $_;
}
close $remote;
my $href;
foreach $href (@response) {
# first line is always response header
my %resp_hdr;
my $resp_body;
my $resp_line;
my $conditions = $_;
for (my $ln = 0; defined $lines; $ln++) {
(my $line, $lines) = split($EOL, $lines, 2);
# header finished
last if(length($line) == 0);
if ($ln == 0) {
# response header
$resp_line = $line;
} else {
# response vars
if ($line =~ /^([^:]+):\s*(.+)$/) {
(my $h = $1) =~ tr/[A-Z]/[a-z]/;
$resp_hdr{$h} = $2;
} else {
return -1;
}
}
}
# check length
if (defined $resp_hdr{"content-length"}) {
($resp_body, $lines) = split("^.".$resp_hdr{"content-length"}, $lines, 2);
} else {
$resp_body = $lines;
undef $lines;
}
# check conditions
if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
if ($href->{'HTTP-Protocol'} ne $1) {
diag(sprintf("proto failed: expected '%s', got '%s'\n", $href->{'HTTP-Protocol'}, $1));
return -1;
}
if ($href->{'HTTP-Status'} ne $2) {
diag(sprintf("status failed: expected '%s', got '%s'\n", $href->{'HTTP-Status'}, $2));
return -1;
}
} else {
return -1;
}
if (defined $href->{'HTTP-Content'}) {
if ($href->{'HTTP-Content'} ne $resp_body) {
diag(sprintf("body failed: expected '%s', got '%s'\n", $href->{'HTTP-Content'}, $resp_body));
return -1;
}
}
if (defined $href->{'-HTTP-Content'}) {
if (defined $resp_body && $resp_body ne '') {
diag(sprintf("body failed: expected empty body, got '%s'\n", $resp_body));
return -1;
}
}
foreach (keys %{ $href }) {
next if $_ eq 'HTTP-Protocol';
next if $_ eq 'HTTP-Status';
next if $_ eq 'HTTP-Content';
next if $_ eq '-HTTP-Content';
(my $k = $_) =~ tr/[A-Z]/[a-z]/;
my $no_val = 0;
if (substr($k, 0, 1) eq '+') {
$k = substr($k, 1);
$no_val = 1;
}
if (!defined $resp_hdr{$k}) {
diag(sprintf("required header '%s' is missing\n", $k));
return -1;
}
if ($no_val == 0) {
if ($href->{$_} =~ /^\/(.+)\/$/) {
if ($resp_hdr{$k} !~ /$1/) {
diag(sprintf("response-header failed: expected '%s', got '%s', regex: %s\n",
$href->{$_}, $resp_hdr{$k}, $1));
return -1;
}
} elsif ($href->{$_} ne $resp_hdr{$k}) {
diag(sprintf("response-header failed: expected '%s', got '%s'\n",
$href->{$_}, $resp_hdr{$k}));
return -1;
}
}
}
}
# we should have sucked up everything
return -1 if (defined $lines);
return 0;
}
1;

@ -3,411 +3,233 @@
use strict;
use IO::Socket;
use Test::More tests => 28;
use LightyTest;
my $basedir = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
my $srcdir = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
my $testname;
my @request;
my @response;
my $configfile = $srcdir.'/lighttpd.conf';
my $lighttpd_path = $basedir.'/src/lighttpd';
my $pidfile = '/tmp/lighttpd/lighttpd.pid';
my $pidoffile = '/tmp/lighttpd/pidof.pid';
sub pidof {
my $prog = $_[0];
open F, "ps ax | grep $prog | awk '{ print \$1 }'|" or
open F, "ps -ef | grep $prog | awk '{ print \$2 }'|" or
return -1;
my $pid = <F>;
close F;
return $pid;
}
sub stop_proc {
open F, $pidfile or return -1;
my $pid = <F>;
close F;
kill('TERM',$pid) or return -1;
select(undef, undef, undef, 0.01);
return 0;
}
sub start_proc {
# kill old proc if necessary
stop_proc;
unlink($pidfile);
system($lighttpd_path." -f ".$configfile);
if (-e $pidfile) {
return 0;
} else {
return -1;
}
}
sub handle_http {
my $EOL = "\015\012";
my $BLANK = $EOL x 2;
my $port = 2048;
my $host = "127.0.0.1";
my $remote =
IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or return -1;
$remote->autoflush(1);
foreach(@request) {
# pipeline requests
s/\r//g;
s/\n/$EOL/g;
print $remote $_.$BLANK;
}
my $lines = "";
# read everything
while(<$remote>) {
$lines .= $_;
}
close $remote;
my $href;
foreach $href (@response) {
# first line is always response header
my %resp_hdr;
my $resp_body;
my $resp_line;
my $conditions = $_;
for (my $ln = 0; defined $lines; $ln++) {
(my $line, $lines) = split($EOL, $lines, 2);
# header finished
last if(length($line) == 0);
if ($ln == 0) {
# response header
$resp_line = $line;
} else {
# response vars
if ($line =~ /^([^:]+):\s*(.+)$/) {
(my $h = $1) =~ tr/[A-Z]/[a-z]/;
$resp_hdr{$h} = $2;
} else {
return -1;
}
}
}
# check length
if (defined $resp_hdr{"content-length"}) {
($resp_body, $lines) = split("^.".$resp_hdr{"content-length"}, $lines, 2);
} else {
$resp_body = $lines;
undef $lines;
}
# check conditions
if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
if ($href->{'HTTP-Protocol'} ne $1) {
diag(sprintf("proto failed: expected '%s', got '%s'\n", $href->{'HTTP-Protocol'}, $1));
return -1;
}
if ($href->{'HTTP-Status'} ne $2) {
diag(sprintf("status failed: expected '%s', got '%s'\n", $href->{'HTTP-Status'}, $2));
return -1;
}
} else {
return -1;
}
if (defined $href->{'HTTP-Content'}) {
if ($href->{'HTTP-Content'} ne $resp_body) {
diag(sprintf("body failed: expected '%s', got '%s'\n", $href->{'HTTP-Content'}, $resp_body));
return -1;
}
}
if (defined $href->{'-HTTP-Content'}) {
if (defined $resp_body && $resp_body ne '') {
diag(sprintf("body failed: expected empty body, got '%s'\n", $resp_body));
return -1;
}
}
foreach (keys %{ $href }) {
next if $_ eq 'HTTP-Protocol';
next if $_ eq 'HTTP-Status';
next if $_ eq 'HTTP-Content';
next if $_ eq '-HTTP-Content';
(my $k = $_) =~ tr/[A-Z]/[a-z]/;
my $no_val = 0;
if (substr($k, 0, 1) eq '+') {
$k = substr($k, 1);
$no_val = 1;
}
if (!defined $resp_hdr{$k}) {
diag(sprintf("required header '%s' is missing\n", $k));
return -1;
}
if ($no_val == 0 &&
$href->{$_} ne $resp_hdr{$k}) {
diag(sprintf("response-header failed: expected '%s', got '%s'\n", $href->{$_}, $resp_hdr{$k}));
return -1;
}
}
}
# we should have sucked up everything
return -1 if (defined $lines);
return 0;
}
ok(start_proc == 0, "Starting lighttpd") or die();
my $tf = LightyTest->new();
my $t;
ok($tf->start_proc == 0, "Starting lighttpd") or die();
## Low-Level Request-Header Parsing - URI
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /index%2ehtml HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'URL-encoding');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'URL-encoding');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /index.html%00 HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } );
ok(handle_http == 0, 'URL-encoding, %00');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } );
ok($tf->handle_http($t) == 0, 'URL-encoding, %00');
## Low-Level Request-Header Parsing - Host
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: www.example.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'hostname');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'hostname');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: 127.0.0.1
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'IPv4 address');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'IPv4 address');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: [::1]
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'IPv6 address');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'IPv6 address');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: www.example.org:80
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'hostname + port');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'hostname + port');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: 127.0.0.1:80
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'IPv4 address + port');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'IPv4 address + port');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: [::1]:80
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'IPv6 address + port');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'IPv6 address + port');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: ../123.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'directory traversal');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'directory traversal');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: .jsdh.sfdg.sdfg.
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'leading and trailing dot');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'leading and trailing dot');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: jsdh.sfdg.sdfg.
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'trailing dot is ok');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'trailing dot is ok');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: .jsdh.sfdg.sdfg
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'leading dot');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'leading dot');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: jsdh..sfdg.sdfg
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'two dots');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'two dots');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: jsdh.sfdg.sdfg:asd
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'broken port-number');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'broken port-number');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: jsdh.sfdg.sdfg:-1
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'negative port-number');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'negative port-number');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: :80
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'port given but host missing');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'port given but host missing');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: .jsdh.sfdg.:sdfg.
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'port and host are broken');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'port and host are broken');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: a.b-c.d123
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'allowed characters in host-name');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'allowed characters in host-name');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: -a.c
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'leading dash');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'leading dash');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: .
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'dot only');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'dot only');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: a192.168.2.10:1234
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'broken IPv4 address - non-digit');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'broken IPv4 address - non-digit');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
Host: 192.168.2:1234
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'broken IPv4 address - too short');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'broken IPv4 address - too short');
## Low-Level Request-Header Parsing - Content-Length
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /index.html HTTP/1.0
Content-Length: -2
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'negative Content-Length');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'negative Content-Length');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
POST /12345.txt HTTP/1.0
Host: 123.example.org
Content-Length: 2147483648
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 413 } );
ok(handle_http == 0, 'Content-Length > max-request-size');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 413 } );
ok($tf->handle_http($t) == 0, 'Content-Length > max-request-size');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
POST /12345.txt HTTP/1.0
Host: 123.example.org
Content-Length:
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 411 } );
ok(handle_http == 0, 'Content-Length is empty');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 411 } );
ok($tf->handle_http($t) == 0, 'Content-Length is empty');
print "\nLow-Level Request-Header Parsing - HTTP/1.1\n";
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.1
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'Host missing');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'Host missing');
ok(stop_proc == 0, "Stopping lighttpd");
ok($tf->stop_proc == 0, "Stopping lighttpd");

@ -3,286 +3,100 @@
use strict;
use IO::Socket;
use Test::More tests => 12;
use LightyTest;
my $basedir = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
my $srcdir = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
my $testname;
my @request;
my @response;
my $configfile = $srcdir.'/lighttpd.conf';
my $lighttpd_path = $basedir.'/src/lighttpd';
my $pidfile = '/tmp/lighttpd/lighttpd.pid';
my $pidoffile = '/tmp/lighttpd/pidof.pid';
sub pidof {
my $prog = $_[0];
open F, "ps ax | grep $prog | awk '{ print \$1 }'|" or
open F, "ps -ef | grep $prog | awk '{ print \$2 }'|" or
return -1;
my $pid = <F>;
close F;
return $pid;
}
sub stop_proc {
open F, $pidfile or return -1;
my $pid = <F>;
close F;
kill('TERM',$pid) or return -1;
select(undef, undef, undef, 0.01);
return 0;
}
sub start_proc {
# kill old proc if necessary
stop_proc;
unlink($pidfile);
system($lighttpd_path." -f ".$configfile);
if (-e $pidfile) {
return 0;
} else {
return -1;
}
}
sub handle_http {
my $EOL = "\015\012";
my $BLANK = $EOL x 2;
my $port = 2048;
my $host = "127.0.0.1";
my $remote =
IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or return -1;
$remote->autoflush(1);
foreach(@request) {
# pipeline requests
s/\r//g;
s/\n/$EOL/g;
print $remote $_.$BLANK;
}
my $lines = "";
# read everything
while(<$remote>) {
$lines .= $_;
}
close $remote;
my $href;
foreach $href (@response) {
# first line is always response header
my %resp_hdr;
my $resp_body;
my $resp_line;
my $conditions = $_;
for (my $ln = 0; defined $lines; $ln++) {
(my $line, $lines) = split($EOL, $lines, 2);
# header finished
last if(length($line) == 0);
if ($ln == 0) {
# response header
$resp_line = $line;
} else {
# response vars
if ($line =~ /^([^:]+):\s*(.+)$/) {
(my $h = $1) =~ tr/[A-Z]/[a-z]/;
$resp_hdr{$h} = $2;
} else {
return -1;
}
}
}
# check length
if (defined $resp_hdr{"content-length"}) {
($resp_body, $lines) = split("^.".$resp_hdr{"content-length"}, $lines, 2);
} else {
$resp_body = $lines;
undef $lines;
}
# check conditions
if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
if ($href->{'HTTP-Protocol'} ne $1) {
diag(sprintf("proto failed: expected '%s', got '%s'\n", $href->{'HTTP-Protocol'}, $1));
return -1;
}
if ($href->{'HTTP-Status'} ne $2) {
diag(sprintf("status failed: expected '%s', got '%s'\n", $href->{'HTTP-Status'}, $2));
return -1;
}
} else {
return -1;
}
if (defined $href->{'HTTP-Content'}) {
if ($href->{'HTTP-Content'} ne $resp_body) {
diag(sprintf("body failed: expected '%s', got '%s'\n", $href->{'HTTP-Content'}, $resp_body));
return -1;
}
}
if (defined $href->{'-HTTP-Content'}) {
if (defined $resp_body && $resp_body ne '') {
diag(sprintf("body failed: expected empty body, got '%s'\n", $resp_body));
return -1;
}
}
foreach (keys %{ $href }) {
next if $_ eq 'HTTP-Protocol';
next if $_ eq 'HTTP-Status';
next if $_ eq 'HTTP-Content';
next if $_ eq '-HTTP-Content';
(my $k = $_) =~ tr/[A-Z]/[a-z]/;
my $no_val = 0;
if (substr($k, 0, 1) eq '+') {
$k = substr($k, 1);
$no_val = 1;
}
if (!defined $resp_hdr{$k}) {
diag(sprintf("required header '%s' is missing\n", $k));
return -1;
}
if ($no_val == 0) {
if ($href->{$_} =~ /^\/(.+)\/$/) {
if ($resp_hdr{$k} !~ /$1/) {
diag(sprintf("response-header failed: expected '%s', got '%s', regex: %s\n",
$href->{$_}, $resp_hdr{$k}, $1));
return -1;
}
} elsif ($href->{$_} ne $resp_hdr{$k}) {
diag(sprintf("response-header failed: expected '%s', got '%s'\n",
$href->{$_}, $resp_hdr{$k}));
return -1;
}
}
}
}
# we should have sucked up everything
return -1 if (defined $lines);
return 0;
}
my $tf = LightyTest->new();
my $t;
ok(start_proc == 0, "Starting lighttpd") or die();
ok($tf->start_proc == 0, "Starting lighttpd") or die();
## Low-Level Response-Header Parsing - HTTP/1.1
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.1
Host: www.example.org
Connection: close
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 200, '+Date' => '' } );
ok(handle_http == 0, 'Date header');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 200, '+Date' => '' } );
ok($tf->handle_http($t) == 0, 'Date header');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.1
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 400, 'Connection' => 'close' } );
ok(handle_http == 0, 'Host missing');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 400, 'Connection' => 'close' } );
ok($tf->handle_http($t) == 0, 'Host missing');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '+ETag' => '' } );
ok(handle_http == 0, 'ETag is set');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '+ETag' => '' } );
ok($tf->handle_http($t) == 0, 'ETag is set');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'ETag' => '/^".+"$/' } );
ok(handle_http == 0, 'ETag has quotes');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'ETag' => '/^".+"$/' } );
ok($tf->handle_http($t) == 0, 'ETag has quotes');
## Low-Level Response-Header Parsing - Content-Length
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /12345.html HTTP/1.0
Host: 123.example.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } );
ok(handle_http == 0, 'Content-Length for text/html');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } );
ok($tf->handle_http($t) == 0, 'Content-Length for text/html');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /12345.txt HTTP/1.0
Host: 123.example.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } );
ok(handle_http == 0, 'Content-Length for text/plain');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } );
ok($tf->handle_http($t) == 0, 'Content-Length for text/plain');
## Low-Level Response-Header Parsing - Location
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /dummydir HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/dummydir/' } );
ok(handle_http == 0, 'internal redirect in directory');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/dummydir/' } );
ok($tf->handle_http($t) == 0, 'internal redirect in directory');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /dummydir?foo HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/dummydir/?foo' } );
ok(handle_http == 0, 'internal redirect in directory + querystring');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:2048/dummydir/?foo' } );
ok($tf->handle_http($t) == 0, 'internal redirect in directory + querystring');
## simple-vhost
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /12345.txt HTTP/1.0
Host: no-simple.example.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } );
ok(handle_http == 0, 'disabling simple-vhost via conditionals');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => '6' } );
ok($tf->handle_http($t) == 0, 'disabling simple-vhost via conditionals');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /12345.txt HTTP/1.0
Host: simple.example.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } );
ok(handle_http == 0, 'simple-vhost via conditionals');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } );
ok($tf->handle_http($t) == 0, 'simple-vhost via conditionals');
ok(stop_proc == 0, "Stopping lighttpd");
ok($tf->stop_proc == 0, "Stopping lighttpd");

@ -3,290 +3,112 @@
use strict;
use IO::Socket;
use Test::More tests => 15;
use LightyTest;
my $basedir = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
my $srcdir = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
my $tf = LightyTest->new();
my $t;
my $testname;
my @request;
my @response;
my $configfile = $srcdir.'/lighttpd.conf';
my $lighttpd_path = $basedir.'/src/lighttpd';
my $pidfile = '/tmp/lighttpd/lighttpd.pid';
my $pidoffile = '/tmp/lighttpd/pidof.pid';
ok($tf->start_proc == 0, "Starting lighttpd") or die();
sub pidof {
my $prog = $_[0];
open F, "ps ax | grep $prog | awk '{ print \$1 }'|" or
open F, "ps -ef | grep $prog | awk '{ print \$2 }'|" or
return -1;
my $pid = <F>;
close F;
return $pid;
}
sub stop_proc {
open F, $pidfile or return -1;
my $pid = <F>;
close F;
kill('TERM',$pid) or return -1;
select(undef, undef, undef, 0.01);
return 0;
}
sub start_proc {
# kill old proc if necessary
stop_proc;
unlink($pidfile);
system($lighttpd_path." -f ".$configfile);
if (-e $pidfile) {
return 0;
} else {
return -1;
}
}
sub handle_http {
my $EOL = "\015\012";
my $BLANK = $EOL x 2;
my $port = 2048;
my $host = "127.0.0.1";
my $remote =
IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or return -1;
$remote->autoflush(1);
foreach(@request) {
# pipeline requests
s/\r//g;
s/\n/$EOL/g;
print $remote $_.$BLANK;
}
my $lines = "";
# read everything
while(<$remote>) {
$lines .= $_;
}
close $remote;
my $href;
foreach $href (@response) {
# first line is always response header
my %resp_hdr;
my $resp_body;
my $resp_line;
my $conditions = $_;
for (my $ln = 0; defined $lines; $ln++) {
(my $line, $lines) = split($EOL, $lines, 2);
# header finished
last if(length($line) == 0);
if ($ln == 0) {
# response header
$resp_line = $line;
} else {
# response vars
if ($line =~ /^([^:]+):\s*(.+)$/) {
(my $h = $1) =~ tr/[A-Z]/[a-z]/;
$resp_hdr{$h} = $2;
} else {
return -1;
}
}
}
# check length
if (defined $resp_hdr{"content-length"}) {
($resp_body, $lines) = split("^.".$resp_hdr{"content-length"}, $lines, 2);
} else {
$resp_body = $lines;
undef $lines;
}
# check conditions
if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
if ($href->{'HTTP-Protocol'} ne $1) {
diag(sprintf("proto failed: expected '%s', got '%s'\n", $href->{'HTTP-Protocol'}, $1));
return -1;
}
if ($href->{'HTTP-Status'} ne $2) {
diag(sprintf("status failed: expected '%s', got '%s'\n", $href->{'HTTP-Status'}, $2));
return -1;
}
} else {
return -1;
}
if (defined $href->{'HTTP-Content'}) {
if ($href->{'HTTP-Content'} ne $resp_body) {
diag(sprintf("body failed: expected '%s', got '%s'\n", $href->{'HTTP-Content'}, $resp_body));
return -1;
}
}
if (defined $href->{'-HTTP-Content'}) {
if (defined $resp_body && $resp_body ne '') {
diag(sprintf("body failed: expected empty body, got '%s'\n", $resp_body));
return -1;
}
}
foreach (keys %{ $href }) {
next if $_ eq 'HTTP-Protocol';
next if $_ eq 'HTTP-Status';
next if $_ eq 'HTTP-Content';
next if $_ eq '-HTTP-Content';
(my $k = $_) =~ tr/[A-Z]/[a-z]/;
my $no_val = 0;
if (substr($k, 0, 1) eq '+') {
$k = substr($k, 1);
$no_val = 1;
}
if (!defined $resp_hdr{$k}) {
diag(sprintf("required header '%s' is missing\n", $k));
return -1;
}
if ($no_val == 0 &&
$href->{$_} ne $resp_hdr{$k}) {
diag(sprintf("response-header failed: expected '%s', got '%s'\n", $href->{$_}, $resp_hdr{$k}));
return -1;
}
}
}
# we should have sucked up everything
return -1 if (defined $lines);
return 0;
}
ok(start_proc == 0, "Starting lighttpd") or die();
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'Valid HTTP/1.0 Request') or die();
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'Valid HTTP/1.0 Request') or die();
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'missing Protocol');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'missing Protocol');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
BC /
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'missing protocol + unknown method');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'missing protocol + unknown method');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
ABC
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'missing protocol + unknown method + missing URI');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'missing protocol + unknown method + missing URI');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
ABC / HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 501 } );
ok(handle_http == 0, 'unknown method');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 501 } );
ok($tf->handle_http($t) == 0, 'unknown method');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.3
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 505 } );
ok(handle_http == 0, 'unknown protocol');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 505 } );
ok($tf->handle_http($t) == 0, 'unknown protocol');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET http://www.example.org/ HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'absolute URI');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'absolute URI');
print "\nLow-Level Request-Header Parsing\n";
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
ABC : foo
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'whitespace after key');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'whitespace after key');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
ABC a: foo
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok(handle_http == 0, 'whitespace with-in key');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } );
ok($tf->handle_http($t) == 0, 'whitespace with-in key');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
ABC:foo
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'no whitespace');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'no whitespace');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET / HTTP/1.0
ABC:foo
bc
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'line-folding');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'line-folding');
print "\nLow-Level Request-Header Parsing - URI\n";
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /index%2ehtml HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'URL-encoding');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok($tf->handle_http($t) == 0, 'URL-encoding');
@request = ( <<EOF
$t->{REQUEST} = ( <<EOF
GET /index.html%00 HTTP/1.0
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } );
ok(handle_http == 0, 'URL-encoding, %00');
$t->{RESPONSE} = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } );
ok($tf->handle_http($t) == 0, 'URL-encoding, %00');
ok(stop_proc == 0, "Stopping lighttpd");
ok($tf->stop_proc == 0, "Stopping lighttpd");

@ -3,198 +3,19 @@
use strict;
use IO::Socket;
use Test::More tests => 3;
use LightyTest;
my $basedir = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
my $srcdir = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
my $testname;
my @request;
my @response;
my $configfile = $srcdir.'/lighttpd.conf';
my $lighttpd_path = $basedir.'/src/lighttpd';
my $pidfile = '/tmp/lighttpd/lighttpd.pid';
my $pidoffile = '/tmp/lighttpd/pidof.pid';
sub pidof {
my $prog = $_[0];
open F, "ps ax | grep $prog | awk '{ print \$1 }'|" or