check_http.t 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
  175. is( $result->return_code, 1, "$command -p $port_https -S -C 139600,14000" );
  176. like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(03/03/2019 21:41\)./', "output ok" );
  177. # Expired cert tests
  178. $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
  179. is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
  180. is( $result->output,
  181. 'CRITICAL - Certificate \'Ton Voon\' expired on 03/05/2009 00:13.',
  182. "output ok" );
  183. }
  184. sub run_common_tests {
  185. my ($opts) = @_;
  186. my $command = $opts->{command};
  187. if ($opts->{ssl}) {
  188. $command .= " --ssl";
  189. }
  190. $result = NPTest->testCmd( "$command -u /file/root" );
  191. is( $result->return_code, 0, "/file/root");
  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 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" );
  196. $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" );
  197. is( $result->return_code, 2, "Missing string check");
  198. 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");
  199. $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" );
  200. is( $result->return_code, 2, "Missing string check");
  201. 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");
  202. my $cmd;
  203. $cmd = "$command -u /slow";
  204. $result = NPTest->testCmd( $cmd );
  205. is( $result->return_code, 0, "$cmd");
  206. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  207. $result->output =~ /in ([\d\.]+) second/;
  208. cmp_ok( $1, ">", 1, "Time is > 1 second" );
  209. $cmd = "$command -u /statuscode/200";
  210. $result = NPTest->testCmd( $cmd );
  211. is( $result->return_code, 0, $cmd);
  212. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  213. $cmd = "$command -u /statuscode/200 -e 200";
  214. $result = NPTest->testCmd( $cmd );
  215. is( $result->return_code, 0, $cmd);
  216. like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  217. $cmd = "$command -u /statuscode/201";
  218. $result = NPTest->testCmd( $cmd );
  219. is( $result->return_code, 0, $cmd);
  220. 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 );
  225. $cmd = "$command -u /statuscode/201 -e 200";
  226. $result = NPTest->testCmd( $cmd );
  227. is( $result->return_code, 2, $cmd);
  228. like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
  229. $cmd = "$command -u /statuscode/200 -e 200,201,202";
  230. $result = NPTest->testCmd( $cmd );
  231. is( $result->return_code, 0, $cmd);
  232. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  233. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  234. $result = NPTest->testCmd( $cmd );
  235. is( $result->return_code, 0, $cmd);
  236. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  237. $cmd = "$command -u /statuscode/203 -e 200,201,202";
  238. $result = NPTest->testCmd( $cmd );
  239. is( $result->return_code, 2, $cmd);
  240. 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 );
  241. $cmd = "$command -j HEAD -u /method";
  242. $result = NPTest->testCmd( $cmd );
  243. is( $result->return_code, 0, $cmd);
  244. like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  245. $cmd = "$command -j POST -u /method";
  246. $result = NPTest->testCmd( $cmd );
  247. is( $result->return_code, 0, $cmd);
  248. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  249. $cmd = "$command -j GET -u /method";
  250. $result = NPTest->testCmd( $cmd );
  251. is( $result->return_code, 0, $cmd);
  252. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  253. $cmd = "$command -u /method";
  254. $result = NPTest->testCmd( $cmd );
  255. is( $result->return_code, 0, $cmd);
  256. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  257. $cmd = "$command -P foo -u /method";
  258. $result = NPTest->testCmd( $cmd );
  259. is( $result->return_code, 0, $cmd);
  260. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  261. $cmd = "$command -j DELETE -u /method";
  262. $result = NPTest->testCmd( $cmd );
  263. is( $result->return_code, 1, $cmd);
  264. like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
  265. $cmd = "$command -j foo -u /method";
  266. $result = NPTest->testCmd( $cmd );
  267. is( $result->return_code, 2, $cmd);
  268. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  269. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  270. $result = NPTest->testCmd( $cmd );
  271. is( $result->return_code, 0, $cmd);
  272. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  273. $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
  274. $result = NPTest->testCmd( $cmd );
  275. is( $result->return_code, 0, $cmd);
  276. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  277. # To confirm that the free doesn't segfault
  278. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  279. $result = NPTest->testCmd( $cmd );
  280. is( $result->return_code, 0, $cmd);
  281. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  282. $cmd = "$command -u /redirect";
  283. $result = NPTest->testCmd( $cmd );
  284. is( $result->return_code, 0, $cmd);
  285. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  286. $cmd = "$command -f follow -u /redirect";
  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. $cmd = "$command -u /redirect -k 'follow: me'";
  291. $result = NPTest->testCmd( $cmd );
  292. is( $result->return_code, 0, $cmd);
  293. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  294. $cmd = "$command -f follow -u /redirect -k 'follow: me'";
  295. $result = NPTest->testCmd( $cmd );
  296. is( $result->return_code, 0, $cmd);
  297. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  298. $cmd = "$command -f sticky -u /redirect -k 'follow: me'";
  299. $result = NPTest->testCmd( $cmd );
  300. is( $result->return_code, 0, $cmd);
  301. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  302. $cmd = "$command -f stickyport -u /redirect -k 'follow: me'";
  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 );
  306. # These tests may block
  307. print "ALRM\n";
  308. # stickyport - on full urlS port is set back to 80 otherwise
  309. $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
  310. eval {
  311. local $SIG{ALRM} = sub { die "alarm\n" };
  312. alarm(2);
  313. $result = NPTest->testCmd( $cmd );
  314. alarm(0); };
  315. isnt( $@, "alarm\n", $cmd );
  316. is( $result->return_code, 0, $cmd );
  317. # Let's hope there won't be any web server on :80 returning "redirected"!
  318. $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
  319. eval {
  320. local $SIG{ALRM} = sub { die "alarm\n" };
  321. alarm(2);
  322. $result = NPTest->testCmd( $cmd );
  323. alarm(0); };
  324. isnt( $@, "alarm\n", $cmd );
  325. isnt( $result->return_code, 0, $cmd );
  326. # Test an external address - timeout
  327. SKIP: {
  328. skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
  329. $cmd = "$command -f follow -u /redir_external -t 5";
  330. eval {
  331. local $SIG{ALRM} = sub { die "alarm\n" };
  332. alarm(2);
  333. $result = NPTest->testCmd( $cmd );
  334. alarm(0); };
  335. is( $@, "alarm\n", $cmd );
  336. }
  337. $cmd = "$command -u /timeout -t 5";
  338. eval {
  339. local $SIG{ALRM} = sub { die "alarm\n" };
  340. alarm(2);
  341. $result = NPTest->testCmd( $cmd );
  342. alarm(0); };
  343. is( $@, "alarm\n", $cmd );
  344. $cmd = "$command -f follow -u /redir_timeout -t 2";
  345. eval {
  346. local $SIG{ALRM} = sub { die "alarm\n" };
  347. alarm(5);
  348. $result = NPTest->testCmd( $cmd );
  349. alarm(0); };
  350. isnt( $@, "alarm\n", $cmd );
  351. }