diff options
author | Ton Voon <tonvoon@macbook.local> | 2009-03-05 23:37:45 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@macbook.local> | 2009-03-05 23:37:45 +0000 |
commit | 8580335779b44f23a97b5534c90262b9658a37d7 (patch) | |
tree | d77f6639cd0c09dbebc886e55580f5be36f8d047 /plugins/tests/check_http.t | |
parent | d0695b26c1368d82166df627878b4a4a2e479f0b (diff) | |
download | monitoring-plugins-8580335779b44f23a97b5534c90262b9658a37d7.tar.gz |
Added inline tests for HTTPS using HTTP::Daemon::SSL
Diffstat (limited to 'plugins/tests/check_http.t')
-rwxr-xr-x | plugins/tests/check_http.t | 322 |
1 files changed, 194 insertions, 128 deletions
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t index c5f90803..20078c20 100755 --- a/plugins/tests/check_http.t +++ b/plugins/tests/check_http.t @@ -12,26 +12,59 @@ use HTTP::Daemon; use HTTP::Status; use HTTP::Response; +my $servers = { http => 0 }; # HTTP::Daemon should always be available +eval { require HTTP::Daemon::SSL }; +if ($@) { + diag "Cannot load HTTP::Daemon::SSL: $@"; +} else { + $servers->{https} = 0; +} + # set a fixed version, so the header size doesn't vary $HTTP::Daemon::VERSION = "1.00"; -my $port = 50000 + int(rand(1000)); +my $port_http = 50000 + int(rand(1000)); +my $port_https = $port_http + 1; -my $pid = fork(); -if ($pid) { +# Start up both servers +my $pid_https; +my $pid_http = fork(); +if ($pid_http) { # Parent - #print "parent\n"; - # give our webserver some time to startup + if (exists $servers->{https}) { + # Fork another server + $pid_https = fork(); + if ($pid_https) { + # Parent + } else { + my $d = HTTP::Daemon::SSL->new( + LocalPort => $port_https, + LocalAddr => "127.0.0.1", + SSL_cert_file => "$Bin/certs/server-cert.pem", + SSL_key_file => "$Bin/certs/server-key.pem", + ) || die; + print "Please contact https at: <URL:", $d->url, ">\n"; + run_server( $d ); + exit; + } + } + # give our webservers some time to startup sleep(1); } else { # Child #print "child\n"; - my $d = HTTP::Daemon->new( - LocalPort => $port, + LocalPort => $port_http, LocalAddr => "127.0.0.1", ) || die; - print "Please contact me at: <URL:", $d->url, ">\n"; + print "Please contact http at: <URL:", $d->url, ">\n"; + run_server( $d ); + exit; +} + +# Run the same server on http and https +sub run_server { + my $d = shift; while (my $c = $d->accept ) { while (my $r = $c->get_request) { if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) { @@ -70,140 +103,173 @@ if ($pid) { $c->close; } } - exit; } -END { if ($pid) { print "Killing $pid\n"; kill "INT", $pid } }; +END { + foreach my $pid ($pid_http, $pid_https) { + if ($pid) { print "Killing $pid\n"; kill "INT", $pid } + } +}; if ($ARGV[0] && $ARGV[0] eq "-d") { sleep 1000; } +my $common_tests = 47; if (-x "./check_http") { - plan tests => 47; + plan tests => $common_tests * 2; } else { plan skip_all => "No check_http compiled"; } my $result; -my $command = "./check_http -H 127.0.0.1 -p $port"; - -$result = NPTest->testCmd( "$command -u /file/root" ); -is( $result->return_code, 0, "/file/root"); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); +my $command = "./check_http -H 127.0.0.1"; -$result = NPTest->testCmd( "$command -u /file/root -s Root" ); -is( $result->return_code, 0, "/file/root search for string"); -TODO: { -local $TODO = "Output is different if a string is requested - should this be right?"; -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); +run_common_tests( { command => "$command -p $port_http" } ); +SKIP: { + skip "HTTP::Daemon::SSL not installed", $common_tests if ! exists $servers->{https}; + run_common_tests( { command => "$command -p $port_https", ssl => 1 } ); } -$result = NPTest->testCmd( "$command -u /slow" ); -is( $result->return_code, 0, "/file/root"); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 177 bytes in ([\d\.]+) seconds/', "Output correct" ); -$result->output =~ /in ([\d\.]+) seconds/; -cmp_ok( $1, ">", 1, "Time is > 1 second" ); - -my $cmd; -$cmd = "$command -u /statuscode/200 -e 200"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -u /statuscode/201 -e 201"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output ); - -$cmd = "$command -u /statuscode/201 -e 200"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 2, $cmd); -like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); - -$cmd = "$command -u /statuscode/200 -e 200,201,202"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 89 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -u /statuscode/201 -e 200,201,202"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - 94 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -u /statuscode/203 -e 200,201,202"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 2, $cmd); -like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information/', "Output correct: ".$result->output ); - -$cmd = "$command -j HEAD -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -j POST -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -j GET -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - 18 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -P foo -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - 19 bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); - -$cmd = "$command -j DELETE -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 1, $cmd); -like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); - -$cmd = "$command -j foo -u /method"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 2, $cmd); -like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); - -$cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); - -$cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); - -# To confirm that the free doesn't segfault -$cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); - -$cmd = "$command -u /redirect"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); - -$cmd = "$command -f follow -u /redirect"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 183 bytes in [\d\.]+ second/', "Output correct: ".$result->output ); - -$cmd = "$command -u /redirect -k 'follow: me'"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); - -$cmd = "$command -f follow -u /redirect -k 'follow: me'"; -$result = NPTest->testCmd( $cmd ); -is( $result->return_code, 0, $cmd); -like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 183 bytes in [\d\.]+ second/', "Output correct: ".$result->output ); +sub run_common_tests { + my ($opts) = @_; + my $command = $opts->{command}; + my $b; + my $add = 0; + if ($opts->{ssl}) { + $command .= " --ssl"; + } + $result = NPTest->testCmd( "$command -u /file/root" ); + is( $result->return_code, 0, "/file/root"); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); + + $result = NPTest->testCmd( "$command -u /file/root -s Root" ); + is( $result->return_code, 0, "/file/root search for string"); + TODO: { + local $TODO = "Output is different if a string is requested - should this be right?"; + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - 274 bytes in [\d\.]+ seconds/', "Output correct" ); + } + + + $b = 177 + $add; + $result = NPTest->testCmd( "$command -u /slow" ); + is( $result->return_code, 0, "/file/root"); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct" ); + $result->output =~ /in ([\d\.]+) seconds/; + cmp_ok( $1, ">", 1, "Time is > 1 second" ); + + my $cmd; + $b = 89 + $add; + $cmd = "$command -u /statuscode/200 -e 200"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $b = 94 + $add; + $cmd = "$command -u /statuscode/201 -e 201"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - '.$b.' bytes in ([\d\.]+) seconds /', "Output correct: ".$result->output ); + + $cmd = "$command -u /statuscode/201 -e 200"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 2, $cmd); + like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); + + $b = 89 + $add; + $cmd = "$command -u /statuscode/200 -e 200,201,202"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $b = 94 + $add; + $cmd = "$command -u /statuscode/201 -e 200,201,202"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 201 Created - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $cmd = "$command -u /statuscode/203 -e 200,201,202"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 2, $cmd); + like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port (\d+): HTTP/1.1 203 Non-Authoritative Information/', "Output correct: ".$result->output ); + + $b = 19 + $add; + $cmd = "$command -j HEAD -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 HEAD - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $b = 19 + $add; + $cmd = "$command -j POST -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $b = 18 + $add; + $cmd = "$command -j GET -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $b = 18 + $add; + $cmd = "$command -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 GET - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $b = 19 + $add; + $cmd = "$command -P foo -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 POST - '.$b.' bytes in ([\d\.]+) seconds/', "Output correct: ".$result->output ); + + $cmd = "$command -j DELETE -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 1, $cmd); + like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); + + $cmd = "$command -j foo -u /method"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 2, $cmd); + like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); + + $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); + + $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); + + # To confirm that the free doesn't segfault + $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - ([\d\.]+) second/', "Output correct: ".$result->output ); + + $cmd = "$command -u /redirect"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); + + $b = 183 + $add; + $cmd = "$command -f follow -u /redirect"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in [\d\.]+ second/', "Output correct: ".$result->output ); + + $cmd = "$command -u /redirect -k 'follow: me'"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK - HTTP/1.1 301 Moved Permanently - [\d\.]+ second/', "Output correct: ".$result->output ); + + $b = 183 + $add; + $cmd = "$command -f follow -u /redirect -k 'follow: me'"; + $result = NPTest->testCmd( $cmd ); + is( $result->return_code, 0, $cmd); + like( $result->output, '/^HTTP OK HTTP/1.1 200 OK - '.$b.' bytes in [\d\.]+ second/', "Output correct: ".$result->output ); + +} |