Code

d7f4148c32b8105562d911cb4583d1541bb4706b
[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 # Start up all servers
41 my @pids;
42 my $pid = fork();
43 if ($pid) {
44         # Parent
45         push @pids, $pid;
46         if (exists $servers->{https}) {
47                 # Fork a normal HTTPS server
48                 $pid = fork();
49                 if ($pid) {
50                         # Parent
51                         push @pids, $pid;
52                         # Fork an expired cert server
53                         $pid = fork();
54                         if ($pid) {
55                                 push @pids, $pid;
56                         } else {
57                                 my $d = HTTP::Daemon::SSL->new(
58                                         LocalPort => $port_https_expired,
59                                         LocalAddr => "127.0.0.1",
60                                         SSL_cert_file => "$Bin/certs/expired-cert.pem",
61                                         SSL_key_file => "$Bin/certs/expired-key.pem",
62                                 ) || die;
63                                 print "Please contact https expired at: <URL:", $d->url, ">\n";
64                                 run_server( $d );
65                                 exit;
66                         }
67                 } else {
68                         my $d = HTTP::Daemon::SSL->new(
69                                 LocalPort => $port_https,
70                                 LocalAddr => "127.0.0.1",
71                                 SSL_cert_file => "$Bin/certs/server-cert.pem",
72                                 SSL_key_file => "$Bin/certs/server-key.pem",
73                         ) || die;
74                         print "Please contact https at: <URL:", $d->url, ">\n";
75                         run_server( $d );
76                         exit;
77                 }
78         }
79         # give our webservers some time to startup
80         sleep(1);
81 } else {
82         # Child
83         #print "child\n";
84         my $d = HTTP::Daemon->new(
85                 LocalPort => $port_http,
86                 LocalAddr => "127.0.0.1",
87         ) || die;
88         print "Please contact http at: <URL:", $d->url, ">\n";
89         run_server( $d );
90         exit;
91 }
93 # Run the same server on http and https
94 sub run_server {
95         my $d = shift;
96         while (my $c = $d->accept ) {
97                 while (my $r = $c->get_request) {
98                         if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
99                                 $c->send_basic_header($1);
100                                 $c->send_crlf;
101                         } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
102                                 $c->send_basic_header;
103                                 $c->send_crlf;
104                                 $c->send_file_response("$Bin/var/$1");
105                         } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
106                                 $c->send_basic_header;
107                                 $c->send_crlf;
108                                 sleep 1;
109                                 $c->send_response("slow");
110                         } elsif ($r->url->path eq "/method") {
111                                 if ($r->method eq "DELETE") {
112                                         $c->send_error(RC_METHOD_NOT_ALLOWED);
113                                 } elsif ($r->method eq "foo") {
114                                         $c->send_error(RC_NOT_IMPLEMENTED);
115                                 } else {
116                                         $c->send_status_line(200, $r->method);
117                                 }
118                         } elsif ($r->url->path eq "/postdata") {
119                                 $c->send_basic_header;
120                                 $c->send_crlf;
121                                 $c->send_response($r->method.":".$r->content);
122                         } elsif ($r->url->path eq "/redirect") {
123                                 $c->send_redirect( "/redirect2" );
124                         } elsif ($r->url->path eq "/redirect2") {
125                                 $c->send_basic_header;
126                                 $c->send_crlf;
127                                 $c->send_response("redirected");
128                         } else {
129                                 $c->send_error(RC_FORBIDDEN);
130                         }
131                         $c->close;
132                 }
133         }
136 END { 
137         foreach my $pid (@pids) {
138                 if ($pid) { print "Killing $pid\n"; kill "INT", $pid } 
139         }
140 };
142 if ($ARGV[0] && $ARGV[0] eq "-d") {
143         while (1) {
144                 `sleep 100`
145         }
148 my $common_tests = 51;
149 my $ssl_only_tests = 6;
150 if (-x "./check_http") {
151         plan tests => $common_tests * 2 + $ssl_only_tests;
152 } else {
153         plan skip_all => "No check_http compiled";
156 my $result;
157 my $command = "./check_http -H 127.0.0.1";
159 run_common_tests( { command => "$command -p $port_http" } );
160 SKIP: {
161         skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
162         run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
163         
164         $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
165         is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
166         is( $result->output, 'OK - Certificate will expire on 03/03/2019 21:41.', "output ok" );
168         $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
169         is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
170         like( $result->output, '/WARNING - Certificate expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
173         # Expired cert tests
174         $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
175         is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
176         is( $result->output, 
177                 'CRITICAL - Certificate expired on 03/05/2009 00:13.',
178                 "output ok" );
182 sub run_common_tests {
183         my ($opts) = @_;
184         my $command = $opts->{command};
185         if ($opts->{ssl}) {
186                 $command .= " --ssl";
187         }
189         $result = NPTest->testCmd( "$command -u /file/root" );
190         is( $result->return_code, 0, "/file/root");
191         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
193         $result = NPTest->testCmd( "$command -u /file/root -s Root" );
194         is( $result->return_code, 0, "/file/root search for string");
195         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
198         my $cmd;
199         $cmd = "$command -u /slow";
200         $result = NPTest->testCmd( $cmd );
201         is( $result->return_code, 0, "$cmd");
202         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
203         $result->output =~ /in ([\d\.]+) second/;
204         cmp_ok( $1, ">", 1, "Time is > 1 second" );
206         $cmd = "$command -u /statuscode/200";
207         $result = NPTest->testCmd( $cmd );
208         is( $result->return_code, 0, $cmd);
209         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
211         $cmd = "$command -u /statuscode/200 -e 200";
212         $result = NPTest->testCmd( $cmd );
213         is( $result->return_code, 0, $cmd);
214         like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
216         $cmd = "$command -u /statuscode/201";
217         $result = NPTest->testCmd( $cmd );
218         is( $result->return_code, 0, $cmd);
219         like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
221         $cmd = "$command -u /statuscode/201 -e 201";
222         $result = NPTest->testCmd( $cmd );
223         is( $result->return_code, 0, $cmd);
224         like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
226         $cmd = "$command -u /statuscode/201 -e 200";
227         $result = NPTest->testCmd( $cmd );
228         is( $result->return_code, 2, $cmd);
229         like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
231         $cmd = "$command -u /statuscode/200 -e 200,201,202";
232         $result = NPTest->testCmd( $cmd );
233         is( $result->return_code, 0, $cmd);
234         like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
236         $cmd = "$command -u /statuscode/201 -e 200,201,202";
237         $result = NPTest->testCmd( $cmd );
238         is( $result->return_code, 0, $cmd);
239         like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
241         $cmd = "$command -u /statuscode/203 -e 200,201,202";
242         $result = NPTest->testCmd( $cmd );
243         is( $result->return_code, 2, $cmd);
244         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 );
246         $cmd = "$command -j HEAD -u /method";
247         $result = NPTest->testCmd( $cmd );
248         is( $result->return_code, 0, $cmd);
249         like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
251         $cmd = "$command -j POST -u /method";
252         $result = NPTest->testCmd( $cmd );
253         is( $result->return_code, 0, $cmd);
254         like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
256         $cmd = "$command -j GET -u /method";
257         $result = NPTest->testCmd( $cmd );
258         is( $result->return_code, 0, $cmd);
259         like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
261         $cmd = "$command -u /method";
262         $result = NPTest->testCmd( $cmd );
263         is( $result->return_code, 0, $cmd);
264         like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
266         $cmd = "$command -P foo -u /method";
267         $result = NPTest->testCmd( $cmd );
268         is( $result->return_code, 0, $cmd);
269         like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
271         $cmd = "$command -j DELETE -u /method";
272         $result = NPTest->testCmd( $cmd );
273         is( $result->return_code, 1, $cmd);
274         like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
276         $cmd = "$command -j foo -u /method";
277         $result = NPTest->testCmd( $cmd );
278         is( $result->return_code, 2, $cmd);
279         like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
281         $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
282         $result = NPTest->testCmd( $cmd );
283         is( $result->return_code, 0, $cmd);
284         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
286         $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
287         $result = NPTest->testCmd( $cmd );
288         is( $result->return_code, 0, $cmd);
289         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
291         # To confirm that the free doesn't segfault
292         $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT: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 -u /redirect";
298         $result = NPTest->testCmd( $cmd );
299         is( $result->return_code, 0, $cmd);
300         like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
302         $cmd = "$command -f follow -u /redirect";
303         $result = NPTest->testCmd( $cmd );
304         is( $result->return_code, 0, $cmd);
305         like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
308         $cmd = "$command -u /redirect -k 'follow: me'";
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 -k 'follow: me'";
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 );