Code

88b77d3b8cd7b662a8ebe99045451edd8d18f3e2
[nagiosplug.git] / plugins / tests / check_http.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # Test check_http by having an actual HTTP server running
4 #
5 # To create the https server certificate:
6 # openssl req -new -x509 -keyout server-key.pem -out server-cert.pem -days 3650 -nodes
7 # Country Name (2 letter code) [AU]:UK
8 # State or Province Name (full name) [Some-State]:Derbyshire
9 # Locality Name (eg, city) []:Belper
10 # Organization Name (eg, company) [Internet Widgits Pty Ltd]:Nagios Plugins
11 # Organizational Unit Name (eg, section) []:
12 # Common Name (eg, YOUR name) []:Ton Voon
13 # Email Address []:tonvoon@mac.com
16 use strict;
17 use Test::More;
18 use NPTest;
19 use FindBin qw($Bin);
21 use HTTP::Daemon;
22 use HTTP::Status;
23 use HTTP::Response;
25 my $servers = { http => 0 };    # HTTP::Daemon should always be available
26 eval { require HTTP::Daemon::SSL };
27 if ($@) {
28         diag "Cannot load HTTP::Daemon::SSL: $@";
29 } else {
30         $servers->{https} = 0;
31 }
33 # set a fixed version, so the header size doesn't vary
34 $HTTP::Daemon::VERSION = "1.00";
36 my $port_http = 50000 + int(rand(1000));
37 my $port_https = $port_http + 1;
38 my $port_https_expired = $port_http + 2;
40 # This array keeps sockets around for implementing timeouts
41 my @persist;
43 # Start up all servers
44 my @pids;
45 my $pid = fork();
46 if ($pid) {
47         # Parent
48         push @pids, $pid;
49         if (exists $servers->{https}) {
50                 # Fork a normal HTTPS server
51                 $pid = fork();
52                 if ($pid) {
53                         # Parent
54                         push @pids, $pid;
55                         # Fork an expired cert server
56                         $pid = fork();
57                         if ($pid) {
58                                 push @pids, $pid;
59                         } else {
60                                 my $d = HTTP::Daemon::SSL->new(
61                                         LocalPort => $port_https_expired,
62                                         LocalAddr => "127.0.0.1",
63                                         SSL_cert_file => "$Bin/certs/expired-cert.pem",
64                                         SSL_key_file => "$Bin/certs/expired-key.pem",
65                                 ) || die;
66                                 print "Please contact https expired at: <URL:", $d->url, ">\n";
67                                 run_server( $d );
68                                 exit;
69                         }
70                 } else {
71                         my $d = HTTP::Daemon::SSL->new(
72                                 LocalPort => $port_https,
73                                 LocalAddr => "127.0.0.1",
74                                 SSL_cert_file => "$Bin/certs/server-cert.pem",
75                                 SSL_key_file => "$Bin/certs/server-key.pem",
76                         ) || die;
77                         print "Please contact https at: <URL:", $d->url, ">\n";
78                         run_server( $d );
79                         exit;
80                 }
81         }
82         # give our webservers some time to startup
83         sleep(1);
84 } else {
85         # Child
86         #print "child\n";
87         my $d = HTTP::Daemon->new(
88                 LocalPort => $port_http,
89                 LocalAddr => "127.0.0.1",
90         ) || die;
91         print "Please contact http at: <URL:", $d->url, ">\n";
92         run_server( $d );
93         exit;
94 }
96 # Run the same server on http and https
97 sub run_server {
98         my $d = shift;
99         MAINLOOP: while (my $c = $d->accept ) {
100                 while (my $r = $c->get_request) {
101                         if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
102                                 $c->send_basic_header($1);
103                                 $c->send_crlf;
104                         } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
105                                 $c->send_basic_header;
106                                 $c->send_crlf;
107                                 $c->send_file_response("$Bin/var/$1");
108                         } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
109                                 $c->send_basic_header;
110                                 $c->send_crlf;
111                                 sleep 1;
112                                 $c->send_response("slow");
113                         } elsif ($r->url->path eq "/method") {
114                                 if ($r->method eq "DELETE") {
115                                         $c->send_error(RC_METHOD_NOT_ALLOWED);
116                                 } elsif ($r->method eq "foo") {
117                                         $c->send_error(RC_NOT_IMPLEMENTED);
118                                 } else {
119                                         $c->send_status_line(200, $r->method);
120                                 }
121                         } elsif ($r->url->path eq "/postdata") {
122                                 $c->send_basic_header;
123                                 $c->send_crlf;
124                                 $c->send_response($r->method.":".$r->content);
125                         } elsif ($r->url->path eq "/redirect") {
126                                 $c->send_redirect( "/redirect2" );
127                         } elsif ($r->url->path eq "/redir_external") {
128                                 $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
129                         } elsif ($r->url->path eq "/redirect2") {
130                                 $c->send_basic_header;
131                                 $c->send_crlf;
132                                 $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
133                         } elsif ($r->url->path eq "/redir_timeout") {
134                                 $c->send_redirect( "/timeout" );
135                         } elsif ($r->url->path eq "/timeout") {
136                                 # Keep $c from being destroyed, but prevent severe leaks
137                                 unshift @persist, $c;
138                                 delete($persist[1000]);
139                                 next MAINLOOP;
140                         } else {
141                                 $c->send_error(RC_FORBIDDEN);
142                         }
143                         $c->close;
144                 }
145         }
148 END { 
149         foreach my $pid (@pids) {
150                 if ($pid) { print "Killing $pid\n"; kill "INT", $pid } 
151         }
152 };
154 if ($ARGV[0] && $ARGV[0] eq "-d") {
155         while (1) {
156                 sleep 100;
157         }
160 my $common_tests = 62;
161 my $ssl_only_tests = 6;
162 if (-x "./check_http") {
163         plan tests => $common_tests * 2 + $ssl_only_tests;
164 } else {
165         plan skip_all => "No check_http compiled";
168 my $result;
169 my $command = "./check_http -H 127.0.0.1";
171 run_common_tests( { command => "$command -p $port_http" } );
172 SKIP: {
173         skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
174         run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
175         
176         $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
177         is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
178         is( $result->output, 'OK - Certificate will expire on 03/03/2019 21:41.', "output ok" );
180         $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
181         is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
182         like( $result->output, '/WARNING - Certificate expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
184         # Expired cert tests
185         $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
186         is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
187         is( $result->output, 
188                 'CRITICAL - Certificate expired on 03/05/2009 00:13.',
189                 "output ok" );
193 sub run_common_tests {
194         my ($opts) = @_;
195         my $command = $opts->{command};
196         if ($opts->{ssl}) {
197                 $command .= " --ssl";
198         }
200         $result = NPTest->testCmd( "$command -u /file/root" );
201         is( $result->return_code, 0, "/file/root");
202         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
204         $result = NPTest->testCmd( "$command -u /file/root -s Root" );
205         is( $result->return_code, 0, "/file/root search for string");
206         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
209         my $cmd;
210         $cmd = "$command -u /slow";
211         $result = NPTest->testCmd( $cmd );
212         is( $result->return_code, 0, "$cmd");
213         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
214         $result->output =~ /in ([\d\.]+) second/;
215         cmp_ok( $1, ">", 1, "Time is > 1 second" );
217         $cmd = "$command -u /statuscode/200";
218         $result = NPTest->testCmd( $cmd );
219         is( $result->return_code, 0, $cmd);
220         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
222         $cmd = "$command -u /statuscode/200 -e 200";
223         $result = NPTest->testCmd( $cmd );
224         is( $result->return_code, 0, $cmd);
225         like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
227         $cmd = "$command -u /statuscode/201";
228         $result = NPTest->testCmd( $cmd );
229         is( $result->return_code, 0, $cmd);
230         like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
232         $cmd = "$command -u /statuscode/201 -e 201";
233         $result = NPTest->testCmd( $cmd );
234         is( $result->return_code, 0, $cmd);
235         like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
237         $cmd = "$command -u /statuscode/201 -e 200";
238         $result = NPTest->testCmd( $cmd );
239         is( $result->return_code, 2, $cmd);
240         like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
242         $cmd = "$command -u /statuscode/200 -e 200,201,202";
243         $result = NPTest->testCmd( $cmd );
244         is( $result->return_code, 0, $cmd);
245         like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
247         $cmd = "$command -u /statuscode/201 -e 200,201,202";
248         $result = NPTest->testCmd( $cmd );
249         is( $result->return_code, 0, $cmd);
250         like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
252         $cmd = "$command -u /statuscode/203 -e 200,201,202";
253         $result = NPTest->testCmd( $cmd );
254         is( $result->return_code, 2, $cmd);
255         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 );
257         $cmd = "$command -j HEAD -u /method";
258         $result = NPTest->testCmd( $cmd );
259         is( $result->return_code, 0, $cmd);
260         like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
262         $cmd = "$command -j POST -u /method";
263         $result = NPTest->testCmd( $cmd );
264         is( $result->return_code, 0, $cmd);
265         like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
267         $cmd = "$command -j GET -u /method";
268         $result = NPTest->testCmd( $cmd );
269         is( $result->return_code, 0, $cmd);
270         like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
272         $cmd = "$command -u /method";
273         $result = NPTest->testCmd( $cmd );
274         is( $result->return_code, 0, $cmd);
275         like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
277         $cmd = "$command -P foo -u /method";
278         $result = NPTest->testCmd( $cmd );
279         is( $result->return_code, 0, $cmd);
280         like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
282         $cmd = "$command -j DELETE -u /method";
283         $result = NPTest->testCmd( $cmd );
284         is( $result->return_code, 1, $cmd);
285         like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
287         $cmd = "$command -j foo -u /method";
288         $result = NPTest->testCmd( $cmd );
289         is( $result->return_code, 2, $cmd);
290         like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
292         $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
293         $result = NPTest->testCmd( $cmd );
294         is( $result->return_code, 0, $cmd);
295         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
297         $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
298         $result = NPTest->testCmd( $cmd );
299         is( $result->return_code, 0, $cmd);
300         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
302         # To confirm that the free doesn't segfault
303         $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
304         $result = NPTest->testCmd( $cmd );
305         is( $result->return_code, 0, $cmd);
306         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
308         $cmd = "$command -u /redirect";
309         $result = NPTest->testCmd( $cmd );
310         is( $result->return_code, 0, $cmd);
311         like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
313         $cmd = "$command -f follow -u /redirect";
314         $result = NPTest->testCmd( $cmd );
315         is( $result->return_code, 0, $cmd);
316         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
318         $cmd = "$command -u /redirect -k 'follow: me'";
319         $result = NPTest->testCmd( $cmd );
320         is( $result->return_code, 0, $cmd);
321         like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
323         $cmd = "$command -f follow -u /redirect -k 'follow: me'";
324         $result = NPTest->testCmd( $cmd );
325         is( $result->return_code, 0, $cmd);
326         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
328         $cmd = "$command -f sticky -u /redirect -k 'follow: me'";
329         $result = NPTest->testCmd( $cmd );
330         is( $result->return_code, 0, $cmd);
331         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
333         $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
334         $result = NPTest->testCmd( $cmd );
335         is( $result->return_code, 0, $cmd);
336         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
338   # These tests may block
339         print "ALRM\n";
341         # stickyport - on full urlS port is set back to 80 otherwise
342         $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
343         eval {
344                 local $SIG{ALRM} = sub { die "alarm\n" };
345                 alarm(2);
346                 $result = NPTest->testCmd( $cmd );
347                 alarm(0);       };
348         isnt( $@, "alarm\n", $cmd );
349         is( $result->return_code, 0, $cmd );
351         # Let's hope there won't be any web server on :80 returning "redirected"!
352         $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
353         eval {
354                 local $SIG{ALRM} = sub { die "alarm\n" };
355                 alarm(2);
356                 $result = NPTest->testCmd( $cmd );
357                 alarm(0); };
358         isnt( $@, "alarm\n", $cmd );
359         isnt( $result->return_code, 0, $cmd );
361         # Test an external address - timeout
362         SKIP: {
363                 skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
364                 $cmd = "$command -f follow -u /redir_external -t 5";
365                 eval {
366                         local $SIG{ALRM} = sub { die "alarm\n" };
367                         alarm(2);
368                         $result = NPTest->testCmd( $cmd );
369                         alarm(0); };
370                 is( $@, "alarm\n", $cmd );
371         }
373         $cmd = "$command -u /timeout -t 5";
374         eval {
375                 local $SIG{ALRM} = sub { die "alarm\n" };
376                 alarm(2);
377                 $result = NPTest->testCmd( $cmd );
378                 alarm(0); };
379         is( $@, "alarm\n", $cmd );
381         $cmd = "$command -f follow -u /redir_timeout -t 2";
382         eval {
383                 local $SIG{ALRM} = sub { die "alarm\n" };
384                 alarm(5);
385                 $result = NPTest->testCmd( $cmd );
386                 alarm(0); };
387         isnt( $@, "alarm\n", $cmd );