check_http.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. # Start up all servers
  34. my @pids;
  35. my $pid = fork();
  36. if ($pid) {
  37. # Parent
  38. push @pids, $pid;
  39. if (exists $servers->{https}) {
  40. # Fork a normal HTTPS server
  41. $pid = fork();
  42. if ($pid) {
  43. # Parent
  44. push @pids, $pid;
  45. # Fork an expired cert server
  46. $pid = fork();
  47. if ($pid) {
  48. push @pids, $pid;
  49. } else {
  50. my $d = HTTP::Daemon::SSL->new(
  51. LocalPort => $port_https_expired,
  52. LocalAddr => "127.0.0.1",
  53. SSL_cert_file => "$Bin/certs/expired-cert.pem",
  54. SSL_key_file => "$Bin/certs/expired-key.pem",
  55. ) || die;
  56. print "Please contact https expired at: <URL:", $d->url, ">\n";
  57. run_server( $d );
  58. exit;
  59. }
  60. } else {
  61. my $d = HTTP::Daemon::SSL->new(
  62. LocalPort => $port_https,
  63. LocalAddr => "127.0.0.1",
  64. SSL_cert_file => "$Bin/certs/server-cert.pem",
  65. SSL_key_file => "$Bin/certs/server-key.pem",
  66. ) || die;
  67. print "Please contact https at: <URL:", $d->url, ">\n";
  68. run_server( $d );
  69. exit;
  70. }
  71. }
  72. # give our webservers some time to startup
  73. sleep(1);
  74. } else {
  75. # Child
  76. #print "child\n";
  77. my $d = HTTP::Daemon->new(
  78. LocalPort => $port_http,
  79. LocalAddr => "127.0.0.1",
  80. ) || die;
  81. print "Please contact http at: <URL:", $d->url, ">\n";
  82. run_server( $d );
  83. exit;
  84. }
  85. # Run the same server on http and https
  86. sub run_server {
  87. my $d = shift;
  88. while (my $c = $d->accept ) {
  89. while (my $r = $c->get_request) {
  90. if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) {
  91. $c->send_basic_header($1);
  92. $c->send_crlf;
  93. } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) {
  94. $c->send_basic_header;
  95. $c->send_crlf;
  96. $c->send_file_response("$Bin/var/$1");
  97. } elsif ($r->method eq "GET" and $r->url->path eq "/slow") {
  98. $c->send_basic_header;
  99. $c->send_crlf;
  100. sleep 1;
  101. $c->send_response("slow");
  102. } elsif ($r->url->path eq "/method") {
  103. if ($r->method eq "DELETE") {
  104. $c->send_error(RC_METHOD_NOT_ALLOWED);
  105. } elsif ($r->method eq "foo") {
  106. $c->send_error(RC_NOT_IMPLEMENTED);
  107. } else {
  108. $c->send_status_line(200, $r->method);
  109. }
  110. } elsif ($r->url->path eq "/postdata") {
  111. $c->send_basic_header;
  112. $c->send_crlf;
  113. $c->send_response($r->method.":".$r->content);
  114. } elsif ($r->url->path eq "/redirect") {
  115. $c->send_redirect( "/redirect2" );
  116. } elsif ($r->url->path eq "/redirect2") {
  117. $c->send_basic_header;
  118. $c->send_crlf;
  119. $c->send_response("redirected");
  120. } else {
  121. $c->send_error(RC_FORBIDDEN);
  122. }
  123. $c->close;
  124. }
  125. }
  126. }
  127. END {
  128. foreach my $pid (@pids) {
  129. if ($pid) { print "Killing $pid\n"; kill "INT", $pid }
  130. }
  131. };
  132. if ($ARGV[0] && $ARGV[0] eq "-d") {
  133. while (1) {
  134. `sleep 100`
  135. }
  136. }
  137. my $common_tests = 51;
  138. my $ssl_only_tests = 6;
  139. if (-x "./check_http") {
  140. plan tests => $common_tests * 2 + $ssl_only_tests;
  141. } else {
  142. plan skip_all => "No check_http compiled";
  143. }
  144. my $result;
  145. my $command = "./check_http -H 127.0.0.1";
  146. run_common_tests( { command => "$command -p $port_http" } );
  147. SKIP: {
  148. skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https};
  149. run_common_tests( { command => "$command -p $port_https", ssl => 1 } );
  150. $result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
  151. is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
  152. is( $result->output, 'OK - Certificate will expire on 03/03/2019 21:41 GMT.', "output ok" );
  153. $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
  154. is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
  155. like( $result->output, '/WARNING - Certificate expires in \d+ day\(s\) \(03/03/2019 21:41 GMT\)./', "output ok" );
  156. # Expired cert tests
  157. $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
  158. is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
  159. is( $result->output,
  160. 'CRITICAL - Certificate expired on 03/05/2009 00:13 GMT.',
  161. "output ok" );
  162. }
  163. sub run_common_tests {
  164. my ($opts) = @_;
  165. my $command = $opts->{command};
  166. if ($opts->{ssl}) {
  167. $command .= " --ssl";
  168. }
  169. $result = NPTest->testCmd( "$command -u /file/root" );
  170. is( $result->return_code, 0, "/file/root");
  171. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
  172. $result = NPTest->testCmd( "$command -u /file/root -s Root" );
  173. is( $result->return_code, 0, "/file/root search for string");
  174. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" );
  175. my $cmd;
  176. $cmd = "$command -u /slow";
  177. $result = NPTest->testCmd( $cmd );
  178. is( $result->return_code, 0, "$cmd");
  179. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  180. $result->output =~ /in ([\d\.]+) second/;
  181. cmp_ok( $1, ">", 1, "Time is > 1 second" );
  182. $cmd = "$command -u /statuscode/200";
  183. $result = NPTest->testCmd( $cmd );
  184. is( $result->return_code, 0, $cmd);
  185. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  186. $cmd = "$command -u /statuscode/200 -e 200";
  187. $result = NPTest->testCmd( $cmd );
  188. is( $result->return_code, 0, $cmd);
  189. like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  190. $cmd = "$command -u /statuscode/201";
  191. $result = NPTest->testCmd( $cmd );
  192. is( $result->return_code, 0, $cmd);
  193. like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  194. $cmd = "$command -u /statuscode/201 -e 201";
  195. $result = NPTest->testCmd( $cmd );
  196. is( $result->return_code, 0, $cmd);
  197. like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output );
  198. $cmd = "$command -u /statuscode/201 -e 200";
  199. $result = NPTest->testCmd( $cmd );
  200. is( $result->return_code, 2, $cmd);
  201. like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output );
  202. $cmd = "$command -u /statuscode/200 -e 200,201,202";
  203. $result = NPTest->testCmd( $cmd );
  204. is( $result->return_code, 0, $cmd);
  205. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  206. $cmd = "$command -u /statuscode/201 -e 200,201,202";
  207. $result = NPTest->testCmd( $cmd );
  208. is( $result->return_code, 0, $cmd);
  209. like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  210. $cmd = "$command -u /statuscode/203 -e 200,201,202";
  211. $result = NPTest->testCmd( $cmd );
  212. is( $result->return_code, 2, $cmd);
  213. 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 );
  214. $cmd = "$command -j HEAD -u /method";
  215. $result = NPTest->testCmd( $cmd );
  216. is( $result->return_code, 0, $cmd);
  217. like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  218. $cmd = "$command -j POST -u /method";
  219. $result = NPTest->testCmd( $cmd );
  220. is( $result->return_code, 0, $cmd);
  221. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  222. $cmd = "$command -j GET -u /method";
  223. $result = NPTest->testCmd( $cmd );
  224. is( $result->return_code, 0, $cmd);
  225. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  226. $cmd = "$command -u /method";
  227. $result = NPTest->testCmd( $cmd );
  228. is( $result->return_code, 0, $cmd);
  229. like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  230. $cmd = "$command -P foo -u /method";
  231. $result = NPTest->testCmd( $cmd );
  232. is( $result->return_code, 0, $cmd);
  233. like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  234. $cmd = "$command -j DELETE -u /method";
  235. $result = NPTest->testCmd( $cmd );
  236. is( $result->return_code, 1, $cmd);
  237. like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output );
  238. $cmd = "$command -j foo -u /method";
  239. $result = NPTest->testCmd( $cmd );
  240. is( $result->return_code, 2, $cmd);
  241. like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output );
  242. $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude";
  243. $result = NPTest->testCmd( $cmd );
  244. is( $result->return_code, 0, $cmd);
  245. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  246. $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude";
  247. $result = NPTest->testCmd( $cmd );
  248. is( $result->return_code, 0, $cmd);
  249. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  250. # To confirm that the free doesn't segfault
  251. $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude";
  252. $result = NPTest->testCmd( $cmd );
  253. is( $result->return_code, 0, $cmd);
  254. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  255. $cmd = "$command -u /redirect";
  256. $result = NPTest->testCmd( $cmd );
  257. is( $result->return_code, 0, $cmd);
  258. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  259. $cmd = "$command -f follow -u /redirect";
  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 -u /redirect -k 'follow: me'";
  264. $result = NPTest->testCmd( $cmd );
  265. is( $result->return_code, 0, $cmd);
  266. like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  267. $cmd = "$command -f follow -u /redirect -k 'follow: me'";
  268. $result = NPTest->testCmd( $cmd );
  269. is( $result->return_code, 0, $cmd);
  270. like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output );
  271. }