summaryrefslogtreecommitdiffstats
path: root/plugins/tests/check_http.t
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tests/check_http.t')
-rwxr-xr-xplugins/tests/check_http.t89
1 files changed, 69 insertions, 20 deletions
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t
index e72d243a..188f5e75 100755
--- a/plugins/tests/check_http.t
+++ b/plugins/tests/check_http.t
@@ -4,20 +4,25 @@
4# 4#
5# To create the https server certificate: 5# To create the https server certificate:
6# openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes 6# openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes
7# Country Name (2 letter code) [AU]:UK 7# to create a new expired certificate:
8# State or Province Name (full name) [Some-State]:Derbyshire 8# faketime '2008-01-01 12:00:00' openssl req -new -x509 -keyout expired-key.pem -out expired-cert.pem -days 1 -nodes
9# Locality Name (eg, city) []:Belper 9# Country Name (2 letter code) [AU]:DE
10# State or Province Name (full name) [Some-State]:Bavaria
11# Locality Name (eg, city) []:Munich
10# Organization Name (eg, company) [Internet Widgits Pty Ltd]:Monitoring Plugins 12# Organization Name (eg, company) [Internet Widgits Pty Ltd]:Monitoring Plugins
11# Organizational Unit Name (eg, section) []: 13# Organizational Unit Name (eg, section) []:
12# Common Name (eg, YOUR name) []:Ton Voon 14# Common Name (e.g. server FQDN or YOUR name) []:Monitoring Plugins
13# Email Address []:tonvoon@mac.com 15# Email Address []:devel@monitoring-plugins.org
14 16
15use strict; 17use strict;
16use Test::More; 18use Test::More;
17use NPTest; 19use NPTest;
18use FindBin qw($Bin); 20use FindBin qw($Bin);
19 21
22$ENV{'LC_TIME'} = "C";
23
20my $common_tests = 70; 24my $common_tests = 70;
25my $virtual_port_tests = 8;
21my $ssl_only_tests = 8; 26my $ssl_only_tests = 8;
22# Check that all dependent modules are available 27# Check that all dependent modules are available
23eval "use HTTP::Daemon 6.01;"; 28eval "use HTTP::Daemon 6.01;";
@@ -27,13 +32,16 @@ eval {
27 require HTTP::Response; 32 require HTTP::Response;
28}; 33};
29 34
35my $plugin = 'check_http';
36$plugin = 'check_curl' if $0 =~ m/check_curl/mx;
37
30if ($@) { 38if ($@) {
31 plan skip_all => "Missing required module for test: $@"; 39 plan skip_all => "Missing required module for test: $@";
32} else { 40} else {
33 if (-x "./check_http") { 41 if (-x "./$plugin") {
34 plan tests => $common_tests * 2 + $ssl_only_tests; 42 plan tests => $common_tests * 2 + $ssl_only_tests + $virtual_port_tests;
35 } else { 43 } else {
36 plan skip_all => "No check_http compiled"; 44 plan skip_all => "No $plugin compiled";
37 } 45 }
38} 46}
39 47
@@ -83,6 +91,8 @@ if ($pid) {
83 exit; 91 exit;
84 } 92 }
85 } else { 93 } else {
94 # closing the connection after -C cert checks make the daemon exit with a sigpipe otherwise
95 local $SIG{'PIPE'} = 'IGNORE';
86 my $d = HTTP::Daemon::SSL->new( 96 my $d = HTTP::Daemon::SSL->new(
87 LocalPort => $port_https, 97 LocalPort => $port_https,
88 LocalAddr => "127.0.0.1", 98 LocalAddr => "127.0.0.1",
@@ -94,8 +104,6 @@ if ($pid) {
94 exit; 104 exit;
95 } 105 }
96 } 106 }
97 # give our webservers some time to startup
98 sleep(1);
99} else { 107} else {
100 # Child 108 # Child
101 #print "child\n"; 109 #print "child\n";
@@ -108,6 +116,9 @@ if ($pid) {
108 exit; 116 exit;
109} 117}
110 118
119# give our webservers some time to startup
120sleep(3);
121
111# Run the same server on http and https 122# Run the same server on http and https
112sub run_server { 123sub run_server {
113 my $d = shift; 124 my $d = shift;
@@ -156,6 +167,11 @@ sub run_server {
156 $c->send_basic_header; 167 $c->send_basic_header;
157 $c->send_header('foo'); 168 $c->send_header('foo');
158 $c->send_crlf; 169 $c->send_crlf;
170 } elsif ($r->url->path eq "/virtual_port") {
171 # return sent Host header
172 $c->send_basic_header;
173 $c->send_crlf;
174 $c->send_response(HTTP::Response->new( 200, 'OK', undef, $r->header ('Host')));
159 } else { 175 } else {
160 $c->send_error(HTTP::Status->RC_FORBIDDEN); 176 $c->send_error(HTTP::Status->RC_FORBIDDEN);
161 } 177 }
@@ -177,7 +193,7 @@ if ($ARGV[0] && $ARGV[0] eq "-d") {
177} 193}
178 194
179my $result; 195my $result;
180my $command = "./check_http -H 127.0.0.1"; 196my $command = "./$plugin -H 127.0.0.1";
181 197
182run_common_tests( { command => "$command -p $port_http" } ); 198run_common_tests( { command => "$command -p $port_http" } );
183SKIP: { 199SKIP: {
@@ -186,25 +202,56 @@ SKIP: {
186 202
187 $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); 203 $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
188 is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); 204 is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
189 is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar 3 21:41:28 2019.', "output ok" ); 205 is( $result->output, "OK - Certificate 'Monitoring Plugins' will expire on Fri Feb 16 15:31:44 2029 +0000.", "output ok" );
190 206
191 $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); 207 $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
192 is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); 208 is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
193 like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019\)./', "output ok" ); 209 like( $result->output, '/WARNING - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" );
194 210
195 # Expired cert tests 211 # Expired cert tests
196 $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); 212 $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
197 is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); 213 is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" );
198 like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019\)./', "output ok" ); 214 like( $result->output, '/CRITICAL - Certificate \'Monitoring Plugins\' expires in \d+ day\(s\) \(Fri Feb 16 15:31:44 2029 \+0000\)./', "output ok" );
199 215
200 $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); 216 $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
201 is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); 217 is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
202 is( $result->output, 218 is( $result->output,
203 'CRITICAL - Certificate \'Ton Voon\' expired on Thu Mar 5 00:13:16 2009.', 219 'CRITICAL - Certificate \'Monitoring Plugins\' expired on Wed Jan 2 11:00:26 2008 +0000.',
204 "output ok" ); 220 "output ok" );
205 221
206} 222}
207 223
224my $cmd;
225# check virtual port behaviour
226#
227# http without virtual port
228$cmd = "$command -p $port_http -u /virtual_port -r ^127.0.0.1:$port_http\$";
229$result = NPTest->testCmd( $cmd );
230is( $result->return_code, 0, $cmd);
231like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
232
233# http with virtual port
234$cmd = "$command:80 -p $port_http -u /virtual_port -r ^127.0.0.1\$";
235$result = NPTest->testCmd( $cmd );
236is( $result->return_code, 0, $cmd);
237like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
238
239SKIP: {
240 skip "HTTP::Daemon::SSL not installed", 4 if ! exists $servers->{https};
241 # https without virtual port
242 $cmd = "$command -p $port_https --ssl -u /virtual_port -r ^127.0.0.1:$port_https\$";
243 $result = NPTest->testCmd( $cmd );
244 is( $result->return_code, 0, $cmd);
245 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
246
247 # https with virtual port
248 $cmd = "$command:443 -p $port_https --ssl -u /virtual_port -r ^127.0.0.1\$";
249 $result = NPTest->testCmd( $cmd );
250 is( $result->return_code, 0, $cmd);
251 like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
252}
253
254
208sub run_common_tests { 255sub run_common_tests {
209 my ($opts) = @_; 256 my ($opts) = @_;
210 my $command = $opts->{command}; 257 my $command = $opts->{command};
@@ -370,27 +417,29 @@ sub run_common_tests {
370 417
371 # stickyport - on full urlS port is set back to 80 otherwise 418 # stickyport - on full urlS port is set back to 80 otherwise
372 $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected"; 419 $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
420 alarm(2);
373 eval { 421 eval {
374 local $SIG{ALRM} = sub { die "alarm\n" }; 422 local $SIG{ALRM} = sub { die "alarm\n" };
375 alarm(2);
376 $result = NPTest->testCmd( $cmd ); 423 $result = NPTest->testCmd( $cmd );
377 alarm(0); }; 424 };
378 isnt( $@, "alarm\n", $cmd ); 425 isnt( $@, "alarm\n", $cmd );
426 alarm(0);
379 is( $result->return_code, 0, $cmd ); 427 is( $result->return_code, 0, $cmd );
380 428
381 # Let's hope there won't be any web server on :80 returning "redirected"! 429 # Let's hope there won't be any web server on :80 returning "redirected"!
382 $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected"; 430 $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
431 alarm(2);
383 eval { 432 eval {
384 local $SIG{ALRM} = sub { die "alarm\n" }; 433 local $SIG{ALRM} = sub { die "alarm\n" };
385 alarm(2);
386 $result = NPTest->testCmd( $cmd ); 434 $result = NPTest->testCmd( $cmd );
387 alarm(0); }; 435 };
388 isnt( $@, "alarm\n", $cmd ); 436 isnt( $@, "alarm\n", $cmd );
437 alarm(0);
389 isnt( $result->return_code, 0, $cmd ); 438 isnt( $result->return_code, 0, $cmd );
390 439
391 # Test an external address - timeout 440 # Test an external address - timeout
392 SKIP: { 441 SKIP: {
393 skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL}); 442 skip "This doesn't seem to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
394 $cmd = "$command -f follow -u /redir_external -t 5"; 443 $cmd = "$command -f follow -u /redir_external -t 5";
395 eval { 444 eval {
396 $result = NPTest->testCmd( $cmd, 2 ); 445 $result = NPTest->testCmd( $cmd, 2 );