1 #! /usr/bin/perl -w -I ..
3 # Test check_http by having an actual HTTP server running
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
25 my $servers = { http => 0 }; # HTTP::Daemon should always be available
26 eval { require HTTP::Daemon::SSL };
28 diag "Cannot load HTTP::Daemon::SSL: $@";
30 $servers->{https} = 0;
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
46 if (exists $servers->{https}) {
47 # Fork a normal HTTPS server
52 # Fork an expired cert server
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",
63 print "Please contact https expired at: <URL:", $d->url, ">\n";
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",
74 print "Please contact https at: <URL:", $d->url, ">\n";
79 # give our webservers some time to startup
84 my $d = HTTP::Daemon->new(
85 LocalPort => $port_http,
86 LocalAddr => "127.0.0.1",
88 print "Please contact http at: <URL:", $d->url, ">\n";
93 # Run the same server on http and https
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);
101 } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
102 $c->send_basic_header;
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;
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);
116 $c->send_status_line(200, $r->method);
118 } elsif ($r->url->path eq "/postdata") {
119 $c->send_basic_header;
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;
127 $c->send_response("redirected");
129 $c->send_error(RC_FORBIDDEN);
137 foreach my $pid (@pids) {
138 if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
142 if ($ARGV[0] && $ARGV[0] eq "-d") {
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;
153 plan skip_all => "No check_http compiled";
157 my $command = "./check_http -H 127.0.0.1";
159 run_common_tests( { command => "$command -p $port_http" } );
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 } );
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" );
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" );
177 'CRITICAL - Certificate expired on 03/05/2009 00:13.',
182 sub run_common_tests {
184 my $command = $opts->{command};
186 $command .= " --ssl";
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" );
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 );