check_http.t 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 = 70;
  19. my $ssl_only_tests = 8;
  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. } elsif ($r->url->path eq "/header_check") {
  146. $c->send_basic_header;
  147. $c->send_header('foo');
  148. $c->send_crlf;
  149. } else {
  150. $c->send_error(HTTP::Status->RC_FORBIDDEN);
  151. }
  152. $c->close;
  153. }
  154. }
  155. }
  156. END {
  157. foreach my $pid (@pids) {
  158. if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
  159. }
  160. };
  161. if ($ARGV[0] && $ARGV[0] eq "-d") {
  162. while (1) {
  163. sleep 100;
  164. }
  165. }
  166. my $result;
  167. my $command = "./check_http -H 127.0.0.1";
  168. run_common_tests( { command => "$command -p $port_http" } );
  169. SKIP: {
  170. skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
  171. run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
  172. $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
  173. is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
  174. is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on 03/03/2019 21:41.', "output ok" );
  175. $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
  176. is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
  177. like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
  178. # Expired cert tests
  179. $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
  180. is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" );
  181. like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
  182. $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
  183. is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
  184. is( $result->output,
  185. 'CRITICAL - Certificate \'Ton Voon\' expired on 03/05/2009 00:13.',
  186. "output ok" );
  187. }
  188. sub run_common_tests {
  189. my ($opts) = @_;
  190. my $command = $opts->{command};
  191. if ($opts->{ssl}) {
  192. $command .= " --ssl";
  193. }
  194. $result = NPTest->testCmd( "$command -u /file/root" );
  195. is( $result->return_code, 0, "/file/root");
  196. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
  197. $result = NPTest->testCmd( "$command -u /file/root -s Root" );
  198. is( $result->return_code, 0, "/file/root search for string");
  199. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
  200. $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
  201. is( $result->return_code, 2, "Missing string check");
  202. 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");
  203. $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
  204. is( $result->return_code, 2, "Missing string check");
  205. 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");
  206. $result = NPTest->testCmd( "$command -u /header_check -d foo" );
  207. is( $result->return_code, 0, "header_check search for string");
  208. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" );
  209. $result = NPTest->testCmd( "$command -u /header_check -d bar" );
  210. is( $result->return_code, 2, "Missing header string check");
  211. like( $result->output, qr%^HTTP CRITICAL: HTTP/1\.1 200 OK - header 'bar' not found on 'https?://127\.0\.0\.1:\d+/header_check'%, "Shows search string and location");
  212. my $cmd;
  213. $cmd = "$command -u /slow";
  214. $result = NPTest->testCmd( $cmd );
  215. is( $result->return_code, 0, "$cmd");
  216. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  217. $result->output =~ /in ([\d\.]+) second/;
  218. cmp_ok( $1, ">", 1, "Time is > 1 second" );
  219. $cmd = "$command -u /statuscode/200";
  220. $result = NPTest->testCmd( $cmd );
  221. is( $result->return_code, 0, $cmd);
  222. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  223. $cmd = "$command -u /statuscode/200 -e 200";
  224. $result = NPTest->testCmd( $cmd );
  225. is( $result->return_code, 0, $cmd);
  226. 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 );
  231. $cmd = "$command -u /statuscode/201 -e 201";
  232. $result = NPTest->testCmd( $cmd );
  233. is( $result->return_code, 0, $cmd);
  234. like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  235. $cmd = "$command -u /statuscode/201 -e 200";
  236. $result = NPTest->testCmd( $cmd );
  237. is( $result->return_code, 2, $cmd);
  238. like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
  239. $cmd = "$command -u /statuscode/200 -e 200,201,202";
  240. $result = NPTest->testCmd( $cmd );
  241. is( $result->return_code, 0, $cmd);
  242. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  243. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  244. $result = NPTest->testCmd( $cmd );
  245. is( $result->return_code, 0, $cmd);
  246. 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/203 -e 200,201,202";
  248. $result = NPTest->testCmd( $cmd );
  249. is( $result->return_code, 2, $cmd);
  250. 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 );
  251. $cmd = "$command -j HEAD -u /method";
  252. $result = NPTest->testCmd( $cmd );
  253. is( $result->return_code, 0, $cmd);
  254. like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  255. $cmd = "$command -j POST -u /method";
  256. $result = NPTest->testCmd( $cmd );
  257. is( $result->return_code, 0, $cmd);
  258. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  259. $cmd = "$command -j GET -u /method";
  260. $result = NPTest->testCmd( $cmd );
  261. is( $result->return_code, 0, $cmd);
  262. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  263. $cmd = "$command -u /method";
  264. $result = NPTest->testCmd( $cmd );
  265. is( $result->return_code, 0, $cmd);
  266. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  267. $cmd = "$command -P foo -u /method";
  268. $result = NPTest->testCmd( $cmd );
  269. is( $result->return_code, 0, $cmd);
  270. 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 );
  275. $cmd = "$command -j foo -u /method";
  276. $result = NPTest->testCmd( $cmd );
  277. is( $result->return_code, 2, $cmd);
  278. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  279. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  280. $result = NPTest->testCmd( $cmd );
  281. is( $result->return_code, 0, $cmd);
  282. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  283. $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
  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. # To confirm that the free doesn't segfault
  288. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  289. $result = NPTest->testCmd( $cmd );
  290. is( $result->return_code, 0, $cmd);
  291. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  292. $cmd = "$command -u /redirect";
  293. $result = NPTest->testCmd( $cmd );
  294. is( $result->return_code, 0, $cmd);
  295. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  296. $cmd = "$command -f follow -u /redirect";
  297. $result = NPTest->testCmd( $cmd );
  298. is( $result->return_code, 0, $cmd);
  299. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  300. $cmd = "$command -u /redirect -k 'follow: me'";
  301. $result = NPTest->testCmd( $cmd );
  302. is( $result->return_code, 0, $cmd);
  303. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  304. $cmd = "$command -f follow -u /redirect -k 'follow: me'";
  305. $result = NPTest->testCmd( $cmd );
  306. is( $result->return_code, 0, $cmd);
  307. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  308. $cmd = "$command -f sticky -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 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  312. $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
  313. $result = NPTest->testCmd( $cmd );
  314. is( $result->return_code, 0, $cmd);
  315. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  316. # These tests may block
  317. print "ALRM\n";
  318. # stickyport - on full urlS port is set back to 80 otherwise
  319. $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
  320. eval {
  321. local $SIG{ALRM} = sub { die "alarm\n" };
  322. alarm(2);
  323. $result = NPTest->testCmd( $cmd );
  324. alarm(0); };
  325. isnt( $@, "alarm\n", $cmd );
  326. is( $result->return_code, 0, $cmd );
  327. # Let's hope there won't be any web server on :80 returning "redirected"!
  328. $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
  329. eval {
  330. local $SIG{ALRM} = sub { die "alarm\n" };
  331. alarm(2);
  332. $result = NPTest->testCmd( $cmd );
  333. alarm(0); };
  334. isnt( $@, "alarm\n", $cmd );
  335. isnt( $result->return_code, 0, $cmd );
  336. # Test an external address - timeout
  337. SKIP: {
  338. skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
  339. $cmd = "$command -f follow -u /redir_external -t 5";
  340. eval {
  341. local $SIG{ALRM} = sub { die "alarm\n" };
  342. alarm(2);
  343. $result = NPTest->testCmd( $cmd );
  344. alarm(0); };
  345. is( $@, "alarm\n", $cmd );
  346. }
  347. $cmd = "$command -u /timeout -t 5";
  348. eval {
  349. local $SIG{ALRM} = sub { die "alarm\n" };
  350. alarm(2);
  351. $result = NPTest->testCmd( $cmd );
  352. alarm(0); };
  353. is( $@, "alarm\n", $cmd );
  354. $cmd = "$command -f follow -u /redir_timeout -t 2";
  355. eval {
  356. local $SIG{ALRM} = sub { die "alarm\n" };
  357. alarm(5);
  358. $result = NPTest->testCmd( $cmd );
  359. alarm(0); };
  360. isnt( $@, "alarm\n", $cmd );
  361. }