Code

check_http: check for and print the certificate cn
[nagiosplug.git] / plugins / tests / check_http.t
index b0df960c3032a4d441e579e79dc70d483cf0dcf2..9ae6bbdcb21bfd0909b177dd1da0d312f962965a 100755 (executable)
@@ -18,9 +18,24 @@ use Test::More;
 use NPTest;
 use FindBin qw($Bin);
 
-use HTTP::Daemon;
-use HTTP::Status;
-use HTTP::Response;
+my $common_tests = 66;
+my $ssl_only_tests = 6;
+# Check that all dependent modules are available
+eval {
+       require HTTP::Daemon;
+       require HTTP::Status;
+       require HTTP::Response;
+};
+
+if ($@) {
+       plan skip_all => "Missing required module for test: $@";
+} else {
+       if (-x "./check_http") {
+               plan tests => $common_tests * 2 + $ssl_only_tests;
+       } else {
+               plan skip_all => "No check_http compiled";
+       }
+}
 
 my $servers = { http => 0 };   # HTTP::Daemon should always be available
 eval { require HTTP::Daemon::SSL };
@@ -112,9 +127,9 @@ sub run_server {
                                $c->send_response("slow");
                        } elsif ($r->url->path eq "/method") {
                                if ($r->method eq "DELETE") {
-                                       $c->send_error(RC_METHOD_NOT_ALLOWED);
+                                       $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED);
                                } elsif ($r->method eq "foo") {
-                                       $c->send_error(RC_NOT_IMPLEMENTED);
+                                       $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED);
                                } else {
                                        $c->send_status_line(200, $r->method);
                                }
@@ -125,11 +140,11 @@ sub run_server {
                        } elsif ($r->url->path eq "/redirect") {
                                $c->send_redirect( "/redirect2" );
                        } elsif ($r->url->path eq "/redir_external") {
-                               $c->send_redirect( "http://169.254.169.254/redirect2" );
+                               $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
                        } elsif ($r->url->path eq "/redirect2") {
                                $c->send_basic_header;
                                $c->send_crlf;
-                               $c->send_response("redirected");
+                               $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
                        } elsif ($r->url->path eq "/redir_timeout") {
                                $c->send_redirect( "/timeout" );
                        } elsif ($r->url->path eq "/timeout") {
@@ -138,7 +153,7 @@ sub run_server {
                                delete($persist[1000]);
                                next MAINLOOP;
                        } else {
-                               $c->send_error(RC_FORBIDDEN);
+                               $c->send_error(HTTP::Status->RC_FORBIDDEN);
                        }
                        $c->close;
                }
@@ -157,14 +172,6 @@ if ($ARGV[0] && $ARGV[0] eq "-d") {
        }
 }
 
-my $common_tests = 55;
-my $ssl_only_tests = 6;
-if (-x "./check_http") {
-       plan tests => $common_tests * 2 + $ssl_only_tests;
-} else {
-       plan skip_all => "No check_http compiled";
-}
-
 my $result;
 my $command = "./check_http -H 127.0.0.1";
 
@@ -175,18 +182,17 @@ SKIP: {
        
        $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
        is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
-       is( $result->output, 'OK - Certificate will expire on 03/03/2019 21:41.', "output ok" );
+       is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on 03/03/2019 21:41.', "output ok" );
 
        $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
        is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
-       like( $result->output, '/WARNING - Certificate expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
-
+       like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
 
        # Expired cert tests
        $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
        is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
        is( $result->output, 
-               'CRITICAL - Certificate expired on 03/05/2009 00:13.',
+               'CRITICAL - Certificate \'Ton Voon\' expired on 03/05/2009 00:13.',
                "output ok" );
 
 }
@@ -206,6 +212,14 @@ sub run_common_tests {
        is( $result->return_code, 0, "/file/root search for string");
        like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
 
+       $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
+       is( $result->return_code, 2, "Missing string check");
+       like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRoot' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
+
+       $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
+       is( $result->return_code, 2, "Missing string check");
+       like( $result->output, qr%HTTP CRITICAL: HTTP/1\.1 200 OK - string 'NonRootWithOver30charsAndM...' not found on 'https?://127\.0\.0\.1:\d+/file/root'%, "Shows search string and location");
+
 
        my $cmd;
        $cmd = "$command -u /slow";
@@ -316,7 +330,6 @@ sub run_common_tests {
        is( $result->return_code, 0, $cmd);
        like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ 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);
@@ -327,25 +340,50 @@ sub run_common_tests {
        is( $result->return_code, 0, $cmd);
        like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
 
+       $cmd = "$command -f sticky -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 - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
+
+       $cmd = "$command -f stickyport -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 - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
+
   # These tests may block
        print "ALRM\n";
 
-       $cmd = "$command -f sticky -u /redir_external -t 5";
+       # stickyport - on full urlS port is set back to 80 otherwise
+       $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
        eval {
                local $SIG{ALRM} = sub { die "alarm\n" };
                alarm(2);
                $result = NPTest->testCmd( $cmd );
                alarm(0);       };
-       isnt( $@, "alarm\n", $cmd);
+       isnt( $@, "alarm\n", $cmd );
+       is( $result->return_code, 0, $cmd );
 
-       # Will this one work everywhere???
-       $cmd = "$command -f follow -u /redir_external -t 5";
+       # Let's hope there won't be any web server on :80 returning "redirected"!
+       $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
        eval {
                local $SIG{ALRM} = sub { die "alarm\n" };
                alarm(2);
                $result = NPTest->testCmd( $cmd );
                alarm(0); };
-       is( $@, "alarm\n", $cmd);
+       isnt( $@, "alarm\n", $cmd );
+       isnt( $result->return_code, 0, $cmd );
+
+       # Test an external address - timeout
+       SKIP: {
+               skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
+               $cmd = "$command -f follow -u /redir_external -t 5";
+               eval {
+                       local $SIG{ALRM} = sub { die "alarm\n" };
+                       alarm(2);
+                       $result = NPTest->testCmd( $cmd );
+                       alarm(0); };
+               is( $@, "alarm\n", $cmd );
+       }
 
        $cmd = "$command -u /timeout -t 5";
        eval {
@@ -353,7 +391,7 @@ sub run_common_tests {
                alarm(2);
                $result = NPTest->testCmd( $cmd );
                alarm(0); };
-       is( $@, "alarm\n", $cmd);
+       is( $@, "alarm\n", $cmd );
 
        $cmd = "$command -f follow -u /redir_timeout -t 2";
        eval {
@@ -361,6 +399,7 @@ sub run_common_tests {
                alarm(5);
                $result = NPTest->testCmd( $cmd );
                alarm(0); };
-       isnt( $@, "alarm\n", $cmd);
+       isnt( $@, "alarm\n", $cmd );
 
 }
+