check_http.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. ReuseAddr => 1,
  70. SSL_cert_file => "$Bin/certs/expired-cert.pem",
  71. SSL_key_file => "$Bin/certs/expired-key.pem",
  72. ) || die;
  73. print "Please contact https expired at: <URL:", $d->url, ">\n";
  74. run_server( $d );
  75. exit;
  76. }
  77. } else {
  78. my $d = HTTP::Daemon::SSL->new(
  79. LocalPort => $port_https,
  80. LocalAddr => "127.0.0.1",
  81. ReuseAddr => 1,
  82. SSL_cert_file => "$Bin/certs/server-cert.pem",
  83. SSL_key_file => "$Bin/certs/server-key.pem",
  84. ) || die;
  85. print "Please contact https at: <URL:", $d->url, ">\n";
  86. run_server( $d );
  87. exit;
  88. }
  89. }
  90. # give our webservers some time to startup
  91. sleep(1);
  92. } else {
  93. # Child
  94. #print "child\n";
  95. my $d = HTTP::Daemon->new(
  96. LocalPort => $port_http,
  97. LocalAddr => "127.0.0.1",
  98. ReuseAddr => 1,
  99. ) || die;
  100. print "Please contact http at: <URL:", $d->url, ">\n";
  101. run_server( $d );
  102. exit;
  103. }
  104. # Run the same server on http and https
  105. sub run_server {
  106. my $d = shift;
  107. MAINLOOP: while (my $c = $d->accept ) {
  108. while (my $r = $c->get_request) {
  109. if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
  110. $c->send_basic_header($1);
  111. $c->send_crlf;
  112. } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
  113. $c->send_basic_header;
  114. $c->send_crlf;
  115. $c->send_file_response("$Bin/var/$1");
  116. } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
  117. $c->send_basic_header;
  118. $c->send_crlf;
  119. sleep 1;
  120. $c->send_response("slow");
  121. } elsif ($r->url->path eq "/method") {
  122. if ($r->method eq "DELETE") {
  123. $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED);
  124. } elsif ($r->method eq "foo") {
  125. $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED);
  126. } else {
  127. $c->send_status_line(200, $r->method);
  128. }
  129. } elsif ($r->url->path eq "/postdata") {
  130. $c->send_basic_header;
  131. $c->send_crlf;
  132. $c->send_response($r->method.":".$r->content);
  133. } elsif ($r->url->path eq "/redirect") {
  134. $c->send_redirect( "/redirect2" );
  135. } elsif ($r->url->path eq "/redir_external") {
  136. $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" );
  137. } elsif ($r->url->path eq "/redirect2") {
  138. $c->send_basic_header;
  139. $c->send_crlf;
  140. $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' ));
  141. } elsif ($r->url->path eq "/redir_timeout") {
  142. $c->send_redirect( "/timeout" );
  143. } elsif ($r->url->path eq "/timeout") {
  144. # Keep $c from being destroyed, but prevent severe leaks
  145. unshift @persist, $c;
  146. delete($persist[1000]);
  147. next MAINLOOP;
  148. } elsif ($r->url->path eq "/header_check") {
  149. $c->send_basic_header;
  150. $c->send_header('foo');
  151. $c->send_crlf;
  152. } else {
  153. $c->send_error(HTTP::Status->RC_FORBIDDEN);
  154. }
  155. $c->close;
  156. }
  157. }
  158. }
  159. END {
  160. foreach my $pid (@pids) {
  161. if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
  162. }
  163. };
  164. if ($ARGV[0] && $ARGV[0] eq "-d") {
  165. while (1) {
  166. sleep 100;
  167. }
  168. }
  169. my $result;
  170. 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. $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
  176. is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
  177. is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar 3 21:41:00 2019.', "output ok" );
  178. $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
  179. is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
  180. like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:00 2019\)./', "output ok" );
  181. # Expired cert tests
  182. $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
  183. is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" );
  184. like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:00 2019\)./', "output ok" );
  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 \'Ton Voon\' expired on Thu Mar 5 00:13:00 2009.',
  189. "output ok" );
  190. }
  191. sub run_common_tests {
  192. my ($opts) = @_;
  193. my $command = $opts->{command};
  194. if ($opts->{ssl}) {
  195. $command .= " --ssl";
  196. }
  197. $result = NPTest->testCmd( "$command -u /file/root" );
  198. is( $result->return_code, 0, "/file/root");
  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 Root" );
  201. is( $result->return_code, 0, "/file/root search for string");
  202. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
  203. $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
  204. is( $result->return_code, 2, "Missing string check");
  205. 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");
  206. $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
  207. is( $result->return_code, 2, "Missing string check");
  208. 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");
  209. $result = NPTest->testCmd( "$command -u /header_check -d foo" );
  210. is( $result->return_code, 0, "header_check search for string");
  211. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" );
  212. $result = NPTest->testCmd( "$command -u /header_check -d bar" );
  213. is( $result->return_code, 2, "Missing header string check");
  214. 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");
  215. my $cmd;
  216. $cmd = "$command -u /slow";
  217. $result = NPTest->testCmd( $cmd );
  218. is( $result->return_code, 0, "$cmd");
  219. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  220. $result->output =~ /in ([\d\.]+) second/;
  221. cmp_ok( $1, ">", 1, "Time is > 1 second" );
  222. $cmd = "$command -u /statuscode/200";
  223. $result = NPTest->testCmd( $cmd );
  224. is( $result->return_code, 0, $cmd);
  225. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  226. $cmd = "$command -u /statuscode/200 -e 200";
  227. $result = NPTest->testCmd( $cmd );
  228. is( $result->return_code, 0, $cmd);
  229. like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  230. $cmd = "$command -u /statuscode/201";
  231. $result = NPTest->testCmd( $cmd );
  232. is( $result->return_code, 0, $cmd);
  233. like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  234. $cmd = "$command -u /statuscode/201 -e 201";
  235. $result = NPTest->testCmd( $cmd );
  236. is( $result->return_code, 0, $cmd);
  237. like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  238. $cmd = "$command -u /statuscode/201 -e 200";
  239. $result = NPTest->testCmd( $cmd );
  240. is( $result->return_code, 2, $cmd);
  241. 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 );
  246. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  247. $result = NPTest->testCmd( $cmd );
  248. is( $result->return_code, 0, $cmd);
  249. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  250. $cmd = "$command -u /statuscode/203 -e 200,201,202";
  251. $result = NPTest->testCmd( $cmd );
  252. is( $result->return_code, 2, $cmd);
  253. 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 );
  254. $cmd = "$command -j HEAD -u /method";
  255. $result = NPTest->testCmd( $cmd );
  256. is( $result->return_code, 0, $cmd);
  257. like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  258. $cmd = "$command -j POST -u /method";
  259. $result = NPTest->testCmd( $cmd );
  260. is( $result->return_code, 0, $cmd);
  261. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  262. $cmd = "$command -j GET -u /method";
  263. $result = NPTest->testCmd( $cmd );
  264. is( $result->return_code, 0, $cmd);
  265. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  266. $cmd = "$command -u /method";
  267. $result = NPTest->testCmd( $cmd );
  268. is( $result->return_code, 0, $cmd);
  269. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  270. $cmd = "$command -P foo -u /method";
  271. $result = NPTest->testCmd( $cmd );
  272. is( $result->return_code, 0, $cmd);
  273. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  274. $cmd = "$command -j DELETE -u /method";
  275. $result = NPTest->testCmd( $cmd );
  276. is( $result->return_code, 1, $cmd);
  277. like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
  278. $cmd = "$command -j foo -u /method";
  279. $result = NPTest->testCmd( $cmd );
  280. is( $result->return_code, 2, $cmd);
  281. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  282. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  283. $result = NPTest->testCmd( $cmd );
  284. is( $result->return_code, 0, $cmd);
  285. 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 );
  290. # To confirm that the free doesn't segfault
  291. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  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 -u /redirect";
  296. $result = NPTest->testCmd( $cmd );
  297. is( $result->return_code, 0, $cmd);
  298. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  299. $cmd = "$command -f follow -u /redirect";
  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. $cmd = "$command -u /redirect -k 'follow: me'";
  304. $result = NPTest->testCmd( $cmd );
  305. is( $result->return_code, 0, $cmd);
  306. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  307. $cmd = "$command -f follow -u /redirect -k 'follow: me'";
  308. $result = NPTest->testCmd( $cmd );
  309. is( $result->return_code, 0, $cmd);
  310. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  311. $cmd = "$command -f sticky -u /redirect -k 'follow: me'";
  312. $result = NPTest->testCmd( $cmd );
  313. is( $result->return_code, 0, $cmd);
  314. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  315. $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
  316. $result = NPTest->testCmd( $cmd );
  317. is( $result->return_code, 0, $cmd);
  318. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  319. # These tests may block
  320. print "ALRM\n";
  321. # stickyport - on full urlS port is set back to 80 otherwise
  322. $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
  323. eval {
  324. local $SIG{ALRM} = sub { die "alarm\n" };
  325. alarm(2);
  326. $result = NPTest->testCmd( $cmd );
  327. alarm(0); };
  328. isnt( $@, "alarm\n", $cmd );
  329. is( $result->return_code, 0, $cmd );
  330. # Let's hope there won't be any web server on :80 returning "redirected"!
  331. $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
  332. eval {
  333. local $SIG{ALRM} = sub { die "alarm\n" };
  334. alarm(2);
  335. $result = NPTest->testCmd( $cmd );
  336. alarm(0); };
  337. isnt( $@, "alarm\n", $cmd );
  338. isnt( $result->return_code, 0, $cmd );
  339. # Test an external address - timeout
  340. SKIP: {
  341. skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
  342. $cmd = "$command -f follow -u /redir_external -t 5";
  343. eval {
  344. local $SIG{ALRM} = sub { die "alarm\n" };
  345. alarm(2);
  346. $result = NPTest->testCmd( $cmd );
  347. alarm(0); };
  348. is( $@, "alarm\n", $cmd );
  349. }
  350. $cmd = "$command -u /timeout -t 5";
  351. eval {
  352. local $SIG{ALRM} = sub { die "alarm\n" };
  353. alarm(2);
  354. $result = NPTest->testCmd( $cmd );
  355. alarm(0); };
  356. is( $@, "alarm\n", $cmd );
  357. $cmd = "$command -f follow -u /redir_timeout -t 2";
  358. eval {
  359. local $SIG{ALRM} = sub { die "alarm\n" };
  360. alarm(5);
  361. $result = NPTest->testCmd( $cmd );
  362. alarm(0); };
  363. isnt( $@, "alarm\n", $cmd );
  364. }