check_http.t 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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
  14. use strict;
  15. use Test::More;
  16. use NPTest;
  17. use FindBin qw($Bin);
  18. my $common_tests = 66;
  19. my $ssl_only_tests = 6;
  20. # Check that all dependent modules are available
  21. eval {
  22. require HTTP::Daemon;
  23. require HTTP::Status;
  24. require HTTP::Response;
  25. };
  26. if ($@) {
  27. plan skip_all => "Missing required module for test: $@";
  28. } else {
  29. if (-x "./check_http") {
  30. plan tests => $common_tests * 2 + $ssl_only_tests;
  31. } else {
  32. plan skip_all => "No check_http compiled";
  33. }
  34. }
  35. my $servers = { http => 0 }; # HTTP::Daemon should always be available
  36. eval { require HTTP::Daemon::SSL };
  37. if ($@) {
  38. diag "Cannot load HTTP::Daemon::SSL: $@";
  39. } else {
  40. $servers->{https} = 0;
  41. }
  42. # set a fixed version, so the header size doesn't vary
  43. $HTTP::Daemon::VERSION = "1.00";
  44. my $port_http = 50000 + int(rand(1000));
  45. my $port_https = $port_http + 1;
  46. my $port_https_expired = $port_http + 2;
  47. # This array keeps sockets around for implementing timeouts
  48. my @persist;
  49. # Start up all servers
  50. my @pids;
  51. my $pid = fork();
  52. if ($pid) {
  53. # Parent
  54. push @pids, $pid;
  55. if (exists $servers->{https}) {
  56. # Fork a normal HTTPS server
  57. $pid = fork();
  58. if ($pid) {
  59. # Parent
  60. push @pids, $pid;
  61. # Fork an expired cert server
  62. $pid = fork();
  63. if ($pid) {
  64. push @pids, $pid;
  65. } else {
  66. my $d = HTTP::Daemon::SSL->new(
  67. LocalPort => $port_https_expired,
  68. LocalAddr => "127.0.0.1",
  69. SSL_cert_file => "$Bin/certs/expired-cert.pem",
  70. SSL_key_file => "$Bin/certs/expired-key.pem",
  71. ) || die;
  72. print "Please contact https expired at: <URL:", $d->url, ">\n";
  73. run_server( $d );
  74. exit;
  75. }
  76. } else {
  77. my $d = HTTP::Daemon::SSL->new(
  78. LocalPort => $port_https,
  79. LocalAddr => "127.0.0.1",
  80. SSL_cert_file => "$Bin/certs/server-cert.pem",
  81. SSL_key_file => "$Bin/certs/server-key.pem",
  82. ) || die;
  83. print "Please contact https at: <URL:", $d->url, ">\n";
  84. run_server( $d );
  85. exit;
  86. }
  87. }
  88. # give our webservers some time to startup
  89. sleep(1);
  90. } else {
  91. # Child
  92. #print "child\n";
  93. my $d = HTTP::Daemon->new(
  94. LocalPort => $port_http,
  95. LocalAddr => "127.0.0.1",
  96. ) || die;
  97. print "Please contact http at: <URL:", $d->url, ">\n";
  98. run_server( $d );
  99. exit;
  100. }
  101. # Run the same server on http and https
  102. sub run_server {
  103. my $d = shift;
  104. MAINLOOP: while (my $c = $d->accept ) {
  105. while (my $r = $c->get_request) {
  106. if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
  107. $c->send_basic_header($1);
  108. $c->send_crlf;
  109. } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
  110. $c->send_basic_header;
  111. $c->send_crlf;
  112. $c->send_file_response("$Bin/var/$1");
  113. } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
  114. $c->send_basic_header;
  115. $c->send_crlf;
  116. sleep 1;
  117. $c->send_response("slow");
  118. } elsif ($r->url->path eq "/method") {
  119. if ($r->method eq "DELETE") {
  120. $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED);
  121. } elsif ($r->method eq "foo") {
  122. $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED);
  123. } else {
  124. $c->send_status_line(200, $r->method);
  125. }
  126. } elsif ($r->url->path eq "/postdata") {
  127. $c->send_basic_header;
  128. $c->send_crlf;
  129. $c->send_response($r->method.":".$r->content);
  130. } elsif ($r->url->path eq "/redirect") {
  131. $c->send_redirect( "/redirect2" );
  132. } elsif ($r->url->path eq "/redir_external") {
  133. $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
  134. } elsif ($r->url->path eq "/redirect2") {
  135. $c->send_basic_header;
  136. $c->send_crlf;
  137. $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
  138. } elsif ($r->url->path eq "/redir_timeout") {
  139. $c->send_redirect( "/timeout" );
  140. } elsif ($r->url->path eq "/timeout") {
  141. # Keep $c from being destroyed, but prevent severe leaks
  142. unshift @persist, $c;
  143. delete($persist[1000]);
  144. next MAINLOOP;
  145. } else {
  146. $c->send_error(HTTP::Status->RC_FORBIDDEN);
  147. }
  148. $c->close;
  149. }
  150. }
  151. }
  152. END {
  153. foreach my $pid (@pids) {
  154. if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
  155. }
  156. };
  157. if ($ARGV[0] && $ARGV[0] eq "-d") {
  158. while (1) {
  159. sleep 100;
  160. }
  161. }
  162. my $result;
  163. my $command = "./check_http -H 127.0.0.1";
  164. run_common_tests( { command => "$command -p $port_http" } );
  165. SKIP: {
  166. skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
  167. run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
  168. $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
  169. is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
  170. is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on 03/03/2019 21:41.', "output ok" );
  171. $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
  172. is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
  173. like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
  174. # Expired cert tests
  175. $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
  176. is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
  177. is( $result->output,
  178. 'CRITICAL - Certificate \'Ton Voon\' expired on 03/05/2009 00:13.',
  179. "output ok" );
  180. }
  181. sub run_common_tests {
  182. my ($opts) = @_;
  183. my $command = $opts->{command};
  184. if ($opts->{ssl}) {
  185. $command .= " --ssl";
  186. }
  187. $result = NPTest->testCmd( "$command -u /file/root" );
  188. is( $result->return_code, 0, "/file/root");
  189. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
  190. $result = NPTest->testCmd( "$command -u /file/root -s Root" );
  191. is( $result->return_code, 0, "/file/root search for string");
  192. 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 NonRoot" );
  194. is( $result->return_code, 2, "Missing string check");
  195. 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");
  196. $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
  197. is( $result->return_code, 2, "Missing string check");
  198. 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");
  199. my $cmd;
  200. $cmd = "$command -u /slow";
  201. $result = NPTest->testCmd( $cmd );
  202. is( $result->return_code, 0, "$cmd");
  203. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  204. $result->output =~ /in ([\d\.]+) second/;
  205. 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 );
  210. $cmd = "$command -u /statuscode/200 -e 200";
  211. $result = NPTest->testCmd( $cmd );
  212. is( $result->return_code, 0, $cmd);
  213. like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  214. $cmd = "$command -u /statuscode/201";
  215. $result = NPTest->testCmd( $cmd );
  216. is( $result->return_code, 0, $cmd);
  217. like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  218. $cmd = "$command -u /statuscode/201 -e 201";
  219. $result = NPTest->testCmd( $cmd );
  220. is( $result->return_code, 0, $cmd);
  221. like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  222. $cmd = "$command -u /statuscode/201 -e 200";
  223. $result = NPTest->testCmd( $cmd );
  224. is( $result->return_code, 2, $cmd);
  225. like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
  226. $cmd = "$command -u /statuscode/200 -e 200,201,202";
  227. $result = NPTest->testCmd( $cmd );
  228. is( $result->return_code, 0, $cmd);
  229. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  230. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  231. $result = NPTest->testCmd( $cmd );
  232. is( $result->return_code, 0, $cmd);
  233. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  234. $cmd = "$command -u /statuscode/203 -e 200,201,202";
  235. $result = NPTest->testCmd( $cmd );
  236. is( $result->return_code, 2, $cmd);
  237. 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 );
  238. $cmd = "$command -j HEAD -u /method";
  239. $result = NPTest->testCmd( $cmd );
  240. is( $result->return_code, 0, $cmd);
  241. like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  242. $cmd = "$command -j POST -u /method";
  243. $result = NPTest->testCmd( $cmd );
  244. is( $result->return_code, 0, $cmd);
  245. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  246. $cmd = "$command -j GET -u /method";
  247. $result = NPTest->testCmd( $cmd );
  248. is( $result->return_code, 0, $cmd);
  249. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  250. $cmd = "$command -u /method";
  251. $result = NPTest->testCmd( $cmd );
  252. is( $result->return_code, 0, $cmd);
  253. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  254. $cmd = "$command -P foo -u /method";
  255. $result = NPTest->testCmd( $cmd );
  256. is( $result->return_code, 0, $cmd);
  257. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  258. $cmd = "$command -j DELETE -u /method";
  259. $result = NPTest->testCmd( $cmd );
  260. is( $result->return_code, 1, $cmd);
  261. like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
  262. $cmd = "$command -j foo -u /method";
  263. $result = NPTest->testCmd( $cmd );
  264. is( $result->return_code, 2, $cmd);
  265. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  266. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  267. $result = NPTest->testCmd( $cmd );
  268. is( $result->return_code, 0, $cmd);
  269. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  270. $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
  271. $result = NPTest->testCmd( $cmd );
  272. is( $result->return_code, 0, $cmd);
  273. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  274. # To confirm that the free doesn't segfault
  275. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  276. $result = NPTest->testCmd( $cmd );
  277. is( $result->return_code, 0, $cmd);
  278. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  279. $cmd = "$command -u /redirect";
  280. $result = NPTest->testCmd( $cmd );
  281. is( $result->return_code, 0, $cmd);
  282. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  283. $cmd = "$command -f follow -u /redirect";
  284. $result = NPTest->testCmd( $cmd );
  285. is( $result->return_code, 0, $cmd);
  286. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  287. $cmd = "$command -u /redirect -k 'follow: me'";
  288. $result = NPTest->testCmd( $cmd );
  289. is( $result->return_code, 0, $cmd);
  290. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  291. $cmd = "$command -f follow -u /redirect -k 'follow: me'";
  292. $result = NPTest->testCmd( $cmd );
  293. is( $result->return_code, 0, $cmd);
  294. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  295. $cmd = "$command -f sticky -u /redirect -k 'follow: me'";
  296. $result = NPTest->testCmd( $cmd );
  297. is( $result->return_code, 0, $cmd);
  298. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  299. $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
  300. $result = NPTest->testCmd( $cmd );
  301. is( $result->return_code, 0, $cmd);
  302. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  303. # These tests may block
  304. print "ALRM\n";
  305. # stickyport - on full urlS port is set back to 80 otherwise
  306. $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
  307. eval {
  308. local $SIG{ALRM} = sub { die "alarm\n" };
  309. alarm(2);
  310. $result = NPTest->testCmd( $cmd );
  311. alarm(0); };
  312. isnt( $@, "alarm\n", $cmd );
  313. is( $result->return_code, 0, $cmd );
  314. # Let's hope there won't be any web server on :80 returning "redirected"!
  315. $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
  316. eval {
  317. local $SIG{ALRM} = sub { die "alarm\n" };
  318. alarm(2);
  319. $result = NPTest->testCmd( $cmd );
  320. alarm(0); };
  321. isnt( $@, "alarm\n", $cmd );
  322. isnt( $result->return_code, 0, $cmd );
  323. # Test an external address - timeout
  324. SKIP: {
  325. skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
  326. $cmd = "$command -f follow -u /redir_external -t 5";
  327. eval {
  328. local $SIG{ALRM} = sub { die "alarm\n" };
  329. alarm(2);
  330. $result = NPTest->testCmd( $cmd );
  331. alarm(0); };
  332. is( $@, "alarm\n", $cmd );
  333. }
  334. $cmd = "$command -u /timeout -t 5";
  335. eval {
  336. local $SIG{ALRM} = sub { die "alarm\n" };
  337. alarm(2);
  338. $result = NPTest->testCmd( $cmd );
  339. alarm(0); };
  340. is( $@, "alarm\n", $cmd );
  341. $cmd = "$command -f follow -u /redir_timeout -t 2";
  342. eval {
  343. local $SIG{ALRM} = sub { die "alarm\n" };
  344. alarm(5);
  345. $result = NPTest->testCmd( $cmd );
  346. alarm(0); };
  347. isnt( $@, "alarm\n", $cmd );
  348. }