check_http.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 = 62;
  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. my $cmd;
  187. $cmd = "$command -u /slow";
  188. $result = NPTest->testCmd( $cmd );
  189. is( $result->return_code, 0, "$cmd");
  190. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  191. $result->output =~ /in ([\d\.]+) second/;
  192. cmp_ok( $1, ">", 1, "Time is > 1 second" );
  193. $cmd = "$command -u /statuscode/200";
  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. $cmd = "$command -u /statuscode/200 -e 200";
  198. $result = NPTest->testCmd( $cmd );
  199. is( $result->return_code, 0, $cmd);
  200. like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  201. $cmd = "$command -u /statuscode/201";
  202. $result = NPTest->testCmd( $cmd );
  203. is( $result->return_code, 0, $cmd);
  204. like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  205. $cmd = "$command -u /statuscode/201 -e 201";
  206. $result = NPTest->testCmd( $cmd );
  207. is( $result->return_code, 0, $cmd);
  208. like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  209. $cmd = "$command -u /statuscode/201 -e 200";
  210. $result = NPTest->testCmd( $cmd );
  211. is( $result->return_code, 2, $cmd);
  212. like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
  213. $cmd = "$command -u /statuscode/200 -e 200,201,202";
  214. $result = NPTest->testCmd( $cmd );
  215. is( $result->return_code, 0, $cmd);
  216. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  217. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  218. $result = NPTest->testCmd( $cmd );
  219. is( $result->return_code, 0, $cmd);
  220. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  221. $cmd = "$command -u /statuscode/203 -e 200,201,202";
  222. $result = NPTest->testCmd( $cmd );
  223. is( $result->return_code, 2, $cmd);
  224. 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 );
  225. $cmd = "$command -j HEAD -u /method";
  226. $result = NPTest->testCmd( $cmd );
  227. is( $result->return_code, 0, $cmd);
  228. like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  229. $cmd = "$command -j POST -u /method";
  230. $result = NPTest->testCmd( $cmd );
  231. is( $result->return_code, 0, $cmd);
  232. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  233. $cmd = "$command -j GET -u /method";
  234. $result = NPTest->testCmd( $cmd );
  235. is( $result->return_code, 0, $cmd);
  236. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  237. $cmd = "$command -u /method";
  238. $result = NPTest->testCmd( $cmd );
  239. is( $result->return_code, 0, $cmd);
  240. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  241. $cmd = "$command -P foo -u /method";
  242. $result = NPTest->testCmd( $cmd );
  243. is( $result->return_code, 0, $cmd);
  244. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  245. $cmd = "$command -j DELETE -u /method";
  246. $result = NPTest->testCmd( $cmd );
  247. is( $result->return_code, 1, $cmd);
  248. like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
  249. $cmd = "$command -j foo -u /method";
  250. $result = NPTest->testCmd( $cmd );
  251. is( $result->return_code, 2, $cmd);
  252. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  253. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  254. $result = NPTest->testCmd( $cmd );
  255. is( $result->return_code, 0, $cmd);
  256. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  257. $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
  258. $result = NPTest->testCmd( $cmd );
  259. is( $result->return_code, 0, $cmd);
  260. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  261. # To confirm that the free doesn't segfault
  262. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  263. $result = NPTest->testCmd( $cmd );
  264. is( $result->return_code, 0, $cmd);
  265. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  266. $cmd = "$command -u /redirect";
  267. $result = NPTest->testCmd( $cmd );
  268. is( $result->return_code, 0, $cmd);
  269. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  270. $cmd = "$command -f follow -u /redirect";
  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. $cmd = "$command -u /redirect -k 'follow: me'";
  275. $result = NPTest->testCmd( $cmd );
  276. is( $result->return_code, 0, $cmd);
  277. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  278. $cmd = "$command -f follow -u /redirect -k 'follow: me'";
  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 -f sticky -u /redirect -k 'follow: me'";
  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 -f stickyport -u /redirect -k 'follow: me'";
  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. # These tests may block
  291. print "ALRM\n";
  292. # stickyport - on full urlS port is set back to 80 otherwise
  293. $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected";
  294. eval {
  295. local $SIG{ALRM} = sub { die "alarm\n" };
  296. alarm(2);
  297. $result = NPTest->testCmd( $cmd );
  298. alarm(0); };
  299. isnt( $@, "alarm\n", $cmd );
  300. is( $result->return_code, 0, $cmd );
  301. # Let's hope there won't be any web server on :80 returning "redirected"!
  302. $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected";
  303. eval {
  304. local $SIG{ALRM} = sub { die "alarm\n" };
  305. alarm(2);
  306. $result = NPTest->testCmd( $cmd );
  307. alarm(0); };
  308. isnt( $@, "alarm\n", $cmd );
  309. isnt( $result->return_code, 0, $cmd );
  310. # Test an external address - timeout
  311. SKIP: {
  312. skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
  313. $cmd = "$command -f follow -u /redir_external -t 5";
  314. eval {
  315. local $SIG{ALRM} = sub { die "alarm\n" };
  316. alarm(2);
  317. $result = NPTest->testCmd( $cmd );
  318. alarm(0); };
  319. is( $@, "alarm\n", $cmd );
  320. }
  321. $cmd = "$command -u /timeout -t 5";
  322. eval {
  323. local $SIG{ALRM} = sub { die "alarm\n" };
  324. alarm(2);
  325. $result = NPTest->testCmd( $cmd );
  326. alarm(0); };
  327. is( $@, "alarm\n", $cmd );
  328. $cmd = "$command -f follow -u /redir_timeout -t 2";
  329. eval {
  330. local $SIG{ALRM} = sub { die "alarm\n" };
  331. alarm(5);
  332. $result = NPTest->testCmd( $cmd );
  333. alarm(0); };
  334. isnt( $@, "alarm\n", $cmd );
  335. }