check_http.t 14 KB

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