diff options
-rw-r--r--[l---------] | plugins/t/check_curl.t | 205 | ||||
-rwxr-xr-x[l---------] | plugins/tests/check_curl.t | 419 |
2 files changed, 622 insertions, 2 deletions
diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t index a54db96..e67fafb 120000..100644 --- a/plugins/t/check_curl.t +++ b/plugins/t/check_curl.t | |||
@@ -1 +1,204 @@ | |||
1 | check_http.t \ No newline at end of file | 1 | #! /usr/bin/perl -w -I .. |
2 | # | ||
3 | # HyperText Transfer Protocol (HTTP) Test via check_http | ||
4 | # | ||
5 | # | ||
6 | |||
7 | use strict; | ||
8 | use Test::More; | ||
9 | use POSIX qw/mktime strftime/; | ||
10 | use NPTest; | ||
11 | |||
12 | plan tests => 49; | ||
13 | |||
14 | my $successOutput = '/OK.*HTTP.*second/'; | ||
15 | |||
16 | my $res; | ||
17 | my $plugin = 'check_http'; | ||
18 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; | ||
19 | |||
20 | my $host_tcp_http = getTestParameter( "NP_HOST_TCP_HTTP", | ||
21 | "A host providing the HTTP Service (a web server)", | ||
22 | "localhost" ); | ||
23 | |||
24 | my $host_tls_http = getTestParameter( "host_tls_http", "NP_HOST_TLS_HTTP", "localhost", | ||
25 | "A host providing the HTTPS Service (a tls web server)" ); | ||
26 | |||
27 | my $host_tls_cert = getTestParameter( "host_tls_cert", "NP_HOST_TLS_CERT", "localhost", | ||
28 | "the common name of the certificate." ); | ||
29 | |||
30 | |||
31 | my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", | ||
32 | "The hostname of system not responsive to network requests", | ||
33 | "10.0.0.1" ); | ||
34 | |||
35 | my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", | ||
36 | "An invalid (not known to DNS) hostname", | ||
37 | "nosuchhost"); | ||
38 | |||
39 | my $internet_access = getTestParameter( "NP_INTERNET_ACCESS", | ||
40 | "Is this system directly connected to the internet?", | ||
41 | "yes"); | ||
42 | |||
43 | my $host_tcp_http2 = getTestParameter( "NP_HOST_TCP_HTTP2", | ||
44 | "A host providing an index page containing the string 'monitoring'", | ||
45 | "test.monitoring-plugins.org" ); | ||
46 | |||
47 | my $faketime = -x '/usr/bin/faketime' ? 1 : 0; | ||
48 | |||
49 | |||
50 | $res = NPTest->testCmd( | ||
51 | "./$plugin $host_tcp_http -wt 300 -ct 600" | ||
52 | ); | ||
53 | cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" ); | ||
54 | like( $res->output, $successOutput, "Output OK" ); | ||
55 | |||
56 | $res = NPTest->testCmd( | ||
57 | "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'" | ||
58 | ); | ||
59 | like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" ); | ||
60 | |||
61 | $res = NPTest->testCmd( | ||
62 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" | ||
63 | ); | ||
64 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); | ||
65 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) | ||
66 | cmp_ok( $res->output, 'eq', "HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Timeout was reached", "Output OK"); | ||
67 | |||
68 | $res = NPTest->testCmd( | ||
69 | "./$plugin $hostname_invalid -wt 1 -ct 2" | ||
70 | ); | ||
71 | cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" ); | ||
72 | # The first part of the message comes from the OS catalogue, so cannot check this. | ||
73 | # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename | ||
74 | # Is also possible to get a socket timeout if DNS is not responding fast enough | ||
75 | # cURL gives us consistent strings from it's own 'lib/strerror.c' | ||
76 | like( $res->output, "/cURL returned 6 - Couldn't resolve host name/", "Output OK"); | ||
77 | |||
78 | # host header checks | ||
79 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http"); | ||
80 | like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); | ||
81 | |||
82 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http -p 80"); | ||
83 | like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); | ||
84 | |||
85 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); | ||
86 | like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); | ||
87 | |||
88 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); | ||
89 | like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); | ||
90 | |||
91 | SKIP: { | ||
92 | skip "No internet access", 3 if $internet_access eq "no"; | ||
93 | |||
94 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -S"); | ||
95 | like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); | ||
96 | |||
97 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:8080 -S -p 443"); | ||
98 | like( $res->output, '/^Host: '.$host_tls_http.':8080\s*$/ms', "Host Header OK" ); | ||
99 | |||
100 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:443 -S -p 443"); | ||
101 | like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); | ||
102 | }; | ||
103 | |||
104 | SKIP: { | ||
105 | skip "No host serving monitoring in index file", 7 unless $host_tcp_http2; | ||
106 | |||
107 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring'" ); | ||
108 | cmp_ok( $res->return_code, "==", 0, "Got a reference to 'monitoring'"); | ||
109 | |||
110 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" ); | ||
111 | cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'"); | ||
112 | like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); | ||
113 | |||
114 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" ); | ||
115 | cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'"); | ||
116 | |||
117 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" ); | ||
118 | cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); | ||
119 | like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); | ||
120 | |||
121 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" ); | ||
122 | cmp_ok( $res->return_code, "==", 0, "And also when not found"); | ||
123 | } | ||
124 | SKIP: { | ||
125 | skip "No internet access", 16 if $internet_access eq "no"; | ||
126 | |||
127 | $res = NPTest->testCmd( | ||
128 | "./$plugin --ssl $host_tls_http" | ||
129 | ); | ||
130 | cmp_ok( $res->return_code, '==', 0, "Can read https for $host_tls_http" ); | ||
131 | |||
132 | $res = NPTest->testCmd( "./$plugin -C 1 --ssl $host_tls_http" ); | ||
133 | cmp_ok( $res->return_code, '==', 0, "Checking certificate for $host_tls_http"); | ||
134 | like ( $res->output, "/Certificate '$host_tls_cert' will expire on/", "Output OK" ); | ||
135 | my $saved_cert_output = $res->output; | ||
136 | |||
137 | $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" ); | ||
138 | cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http"); | ||
139 | like ( $res->output, qr/WARNING - Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" ); | ||
140 | |||
141 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); | ||
142 | is( $res->return_code, 0, "Old syntax for cert checking okay" ); | ||
143 | is( $res->output, $saved_cert_output, "Same output as new syntax" ); | ||
144 | |||
145 | $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" ); | ||
146 | is( $res->return_code, 0, "Updated syntax for cert checking okay" ); | ||
147 | is( $res->output, $saved_cert_output, "Same output as new syntax" ); | ||
148 | |||
149 | $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" ); | ||
150 | cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added"); | ||
151 | |||
152 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); | ||
153 | cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works"); | ||
154 | |||
155 | # run some certificate checks with faketime | ||
156 | SKIP: { | ||
157 | skip "No faketime binary found", 12 if !$faketime; | ||
158 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http"); | ||
159 | like($res->output, qr/OK - Certificate '$host_tls_cert' will expire on/, "Catch cert output"); | ||
160 | is( $res->return_code, 0, "Catch cert output exit code" ); | ||
161 | my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/); | ||
162 | if(!defined $year) { | ||
163 | die("parsing date failed from: ".$res->output); | ||
164 | } | ||
165 | my $months = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}; | ||
166 | my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900); | ||
167 | my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts)); | ||
168 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./$plugin -C 1 $host_tls_http"); | ||
169 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' just expired/, "Output on expire date"); | ||
170 | is( $res->return_code, 2, "Output on expire date" ); | ||
171 | |||
172 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./$plugin -C 1 $host_tls_http"); | ||
173 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output"); | ||
174 | is( $res->return_code, 2, "cert expires in 1 second exit code" ); | ||
175 | |||
176 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./$plugin -C 1 $host_tls_http"); | ||
177 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output"); | ||
178 | is( $res->return_code, 2, "cert expires in 2 minutes exit code" ); | ||
179 | |||
180 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./$plugin -C 1 $host_tls_http"); | ||
181 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output"); | ||
182 | is( $res->return_code, 2, "cert expires in 2 hours exit code" ); | ||
183 | |||
184 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./$plugin -C 1 $host_tls_http"); | ||
185 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expired on/, "Certificate expired output"); | ||
186 | is( $res->return_code, 2, "Certificate expired exit code" ); | ||
187 | }; | ||
188 | |||
189 | $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" ); | ||
190 | like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); | ||
191 | like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' ); | ||
192 | |||
193 | $res = NPTest->testCmd( | ||
194 | "./$plugin --ssl -H www.e-paycobalt.com" | ||
195 | ); | ||
196 | cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" ); | ||
197 | |||
198 | |||
199 | $res = NPTest->testCmd( "./$plugin -H www.mozilla.com -u /firefox -f follow" ); | ||
200 | is( $res->return_code, 0, "Redirection based on location is okay"); | ||
201 | |||
202 | $res = NPTest->testCmd( "./$plugin -H www.mozilla.com --extended-perfdata" ); | ||
203 | like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); | ||
204 | } | ||
diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t index a54db96..dd56706 120000..100755 --- a/plugins/tests/check_curl.t +++ b/plugins/tests/check_curl.t | |||
@@ -1 +1,418 @@ | |||
1 | check_http.t \ No newline at end of file | 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]:Monitoring Plugins | ||
11 | # Organizational Unit Name (eg, section) []: | ||
12 | # Common Name (eg, YOUR name) []:Ton Voon | ||
13 | # Email Address []:tonvoon@mac.com | ||
14 | |||
15 | use strict; | ||
16 | use Test::More; | ||
17 | use NPTest; | ||
18 | use FindBin qw($Bin); | ||
19 | |||
20 | $ENV{'LC_TIME'} = "C"; | ||
21 | |||
22 | my $common_tests = 70; | ||
23 | my $ssl_only_tests = 8; | ||
24 | # Check that all dependent modules are available | ||
25 | eval "use HTTP::Daemon 6.01;"; | ||
26 | plan skip_all => 'HTTP::Daemon >= 6.01 required' if $@; | ||
27 | eval { | ||
28 | require HTTP::Status; | ||
29 | require HTTP::Response; | ||
30 | }; | ||
31 | |||
32 | my $plugin = 'check_http'; | ||
33 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; | ||
34 | |||
35 | if ($@) { | ||
36 | plan skip_all => "Missing required module for test: $@"; | ||
37 | } else { | ||
38 | if (-x "./$plugin") { | ||
39 | plan tests => $common_tests * 2 + $ssl_only_tests; | ||
40 | } else { | ||
41 | plan skip_all => "No $plugin compiled"; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | my $servers = { http => 0 }; # HTTP::Daemon should always be available | ||
46 | eval { require HTTP::Daemon::SSL }; | ||
47 | if ($@) { | ||
48 | diag "Cannot load HTTP::Daemon::SSL: $@"; | ||
49 | } else { | ||
50 | $servers->{https} = 0; | ||
51 | } | ||
52 | |||
53 | # set a fixed version, so the header size doesn't vary | ||
54 | $HTTP::Daemon::VERSION = "1.00"; | ||
55 | |||
56 | my $port_http = 50000 + int(rand(1000)); | ||
57 | my $port_https = $port_http + 1; | ||
58 | my $port_https_expired = $port_http + 2; | ||
59 | |||
60 | # This array keeps sockets around for implementing timeouts | ||
61 | my @persist; | ||
62 | |||
63 | # Start up all servers | ||
64 | my @pids; | ||
65 | my $pid = fork(); | ||
66 | if ($pid) { | ||
67 | # Parent | ||
68 | push @pids, $pid; | ||
69 | if (exists $servers->{https}) { | ||
70 | # Fork a normal HTTPS server | ||
71 | $pid = fork(); | ||
72 | if ($pid) { | ||
73 | # Parent | ||
74 | push @pids, $pid; | ||
75 | # Fork an expired cert server | ||
76 | $pid = fork(); | ||
77 | if ($pid) { | ||
78 | push @pids, $pid; | ||
79 | } else { | ||
80 | my $d = HTTP::Daemon::SSL->new( | ||
81 | LocalPort => $port_https_expired, | ||
82 | LocalAddr => "127.0.0.1", | ||
83 | SSL_cert_file => "$Bin/certs/expired-cert.pem", | ||
84 | SSL_key_file => "$Bin/certs/expired-key.pem", | ||
85 | ) || die; | ||
86 | print "Please contact https expired at: <URL:", $d->url, ">\n"; | ||
87 | run_server( $d ); | ||
88 | exit; | ||
89 | } | ||
90 | } else { | ||
91 | my $d = HTTP::Daemon::SSL->new( | ||
92 | LocalPort => $port_https, | ||
93 | LocalAddr => "127.0.0.1", | ||
94 | SSL_cert_file => "$Bin/certs/server-cert.pem", | ||
95 | SSL_key_file => "$Bin/certs/server-key.pem", | ||
96 | ) || die; | ||
97 | print "Please contact https at: <URL:", $d->url, ">\n"; | ||
98 | run_server( $d ); | ||
99 | exit; | ||
100 | } | ||
101 | } | ||
102 | # give our webservers some time to startup | ||
103 | sleep(1); | ||
104 | } else { | ||
105 | # Child | ||
106 | #print "child\n"; | ||
107 | my $d = HTTP::Daemon->new( | ||
108 | LocalPort => $port_http, | ||
109 | LocalAddr => "127.0.0.1", | ||
110 | ) || die; | ||
111 | print "Please contact http at: <URL:", $d->url, ">\n"; | ||
112 | run_server( $d ); | ||
113 | exit; | ||
114 | } | ||
115 | |||
116 | # Run the same server on http and https | ||
117 | sub run_server { | ||
118 | my $d = shift; | ||
119 | MAINLOOP: while (my $c = $d->accept ) { | ||
120 | while (my $r = $c->get_request) { | ||
121 | if ($r->method eq "GET" and $r->url->path =~ m^/statuscode/(\d+)^) { | ||
122 | $c->send_basic_header($1); | ||
123 | $c->send_crlf; | ||
124 | } elsif ($r->method eq "GET" and $r->url->path =~ m^/file/(.*)^) { | ||
125 | $c->send_basic_header; | ||
126 | $c->send_crlf; | ||
127 | $c->send_file_response("$Bin/var/$1"); | ||
128 | } elsif ($r->method eq "GET" and $r->url->path eq "/slow") { | ||
129 | $c->send_basic_header; | ||
130 | $c->send_crlf; | ||
131 | sleep 1; | ||
132 | $c->send_response("slow"); | ||
133 | } elsif ($r->url->path eq "/method") { | ||
134 | if ($r->method eq "DELETE") { | ||
135 | $c->send_error(HTTP::Status->RC_METHOD_NOT_ALLOWED); | ||
136 | } elsif ($r->method eq "foo") { | ||
137 | $c->send_error(HTTP::Status->RC_NOT_IMPLEMENTED); | ||
138 | } else { | ||
139 | $c->send_status_line(200, $r->method); | ||
140 | } | ||
141 | } elsif ($r->url->path eq "/postdata") { | ||
142 | $c->send_basic_header; | ||
143 | $c->send_crlf; | ||
144 | $c->send_response($r->method.":".$r->content); | ||
145 | } elsif ($r->url->path eq "/redirect") { | ||
146 | $c->send_redirect( "/redirect2" ); | ||
147 | } elsif ($r->url->path eq "/redir_external") { | ||
148 | $c->send_redirect(($d->isa('HTTP::Daemon::SSL') ? "https" : "http") . "://169.254.169.254/redirect2" ); | ||
149 | } elsif ($r->url->path eq "/redirect2") { | ||
150 | $c->send_basic_header; | ||
151 | $c->send_crlf; | ||
152 | $c->send_response(HTTP::Response->new( 200, 'OK', undef, 'redirected' )); | ||
153 | } elsif ($r->url->path eq "/redir_timeout") { | ||
154 | $c->send_redirect( "/timeout" ); | ||
155 | } elsif ($r->url->path eq "/timeout") { | ||
156 | # Keep $c from being destroyed, but prevent severe leaks | ||
157 | unshift @persist, $c; | ||
158 | delete($persist[1000]); | ||
159 | next MAINLOOP; | ||
160 | } elsif ($r->url->path eq "/header_check") { | ||
161 | $c->send_basic_header; | ||
162 | $c->send_header('foo'); | ||
163 | $c->send_crlf; | ||
164 | } else { | ||
165 | $c->send_error(HTTP::Status->RC_FORBIDDEN); | ||
166 | } | ||
167 | $c->close; | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | |||
172 | END { | ||
173 | foreach my $pid (@pids) { | ||
174 | if ($pid) { print "Killing $pid\n"; kill "INT", $pid } | ||
175 | } | ||
176 | }; | ||
177 | |||
178 | if ($ARGV[0] && $ARGV[0] eq "-d") { | ||
179 | while (1) { | ||
180 | sleep 100; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | my $result; | ||
185 | my $command = "./$plugin -H 127.0.0.1"; | ||
186 | |||
187 | run_common_tests( { command => "$command -p $port_http" } ); | ||
188 | SKIP: { | ||
189 | skip "HTTP::Daemon::SSL not installed", $common_tests + $ssl_only_tests if ! exists $servers->{https}; | ||
190 | run_common_tests( { command => "$command -p $port_https", ssl => 1 } ); | ||
191 | |||
192 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14" ); | ||
193 | is( $result->return_code, 0, "$command -p $port_https -S -C 14" ); | ||
194 | is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar 3 21:41:28 2019 +0000.', "output ok" ); | ||
195 | |||
196 | $result = NPTest->testCmd( "$command -p $port_https -S -C 14000" ); | ||
197 | is( $result->return_code, 1, "$command -p $port_https -S -C 14000" ); | ||
198 | like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019 \+0000\)./', "output ok" ); | ||
199 | |||
200 | # Expired cert tests | ||
201 | $result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" ); | ||
202 | is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" ); | ||
203 | like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar 3 21:41:28 2019 \+0000\)./', "output ok" ); | ||
204 | |||
205 | $result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" ); | ||
206 | is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" ); | ||
207 | is( $result->output, | ||
208 | 'CRITICAL - Certificate \'Ton Voon\' expired on Thu Mar 5 00:13:16 2009 +0000.', | ||
209 | "output ok" ); | ||
210 | |||
211 | } | ||
212 | |||
213 | sub run_common_tests { | ||
214 | my ($opts) = @_; | ||
215 | my $command = $opts->{command}; | ||
216 | if ($opts->{ssl}) { | ||
217 | $command .= " --ssl"; | ||
218 | } | ||
219 | |||
220 | $result = NPTest->testCmd( "$command -u /file/root" ); | ||
221 | is( $result->return_code, 0, "/file/root"); | ||
222 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" ); | ||
223 | |||
224 | $result = NPTest->testCmd( "$command -u /file/root -s Root" ); | ||
225 | is( $result->return_code, 0, "/file/root search for string"); | ||
226 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 274 bytes in [\d\.]+ second/', "Output correct" ); | ||
227 | |||
228 | $result = NPTest->testCmd( "$command -u /file/root -s NonRoot" ); | ||
229 | is( $result->return_code, 2, "Missing string check"); | ||
230 | 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"); | ||
231 | |||
232 | $result = NPTest->testCmd( "$command -u /file/root -s NonRootWithOver30charsAndMoreFunThanAWetFish" ); | ||
233 | is( $result->return_code, 2, "Missing string check"); | ||
234 | 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"); | ||
235 | |||
236 | $result = NPTest->testCmd( "$command -u /header_check -d foo" ); | ||
237 | is( $result->return_code, 0, "header_check search for string"); | ||
238 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - 96 bytes in [\d\.]+ second/', "Output correct" ); | ||
239 | |||
240 | $result = NPTest->testCmd( "$command -u /header_check -d bar" ); | ||
241 | is( $result->return_code, 2, "Missing header string check"); | ||
242 | 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"); | ||
243 | |||
244 | my $cmd; | ||
245 | $cmd = "$command -u /slow"; | ||
246 | $result = NPTest->testCmd( $cmd ); | ||
247 | is( $result->return_code, 0, "$cmd"); | ||
248 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
249 | $result->output =~ /in ([\d\.]+) second/; | ||
250 | cmp_ok( $1, ">", 1, "Time is > 1 second" ); | ||
251 | |||
252 | $cmd = "$command -u /statuscode/200"; | ||
253 | $result = NPTest->testCmd( $cmd ); | ||
254 | is( $result->return_code, 0, $cmd); | ||
255 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
256 | |||
257 | $cmd = "$command -u /statuscode/200 -e 200"; | ||
258 | $result = NPTest->testCmd( $cmd ); | ||
259 | is( $result->return_code, 0, $cmd); | ||
260 | like( $result->output, '/^HTTP OK: Status line output matched "200" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
261 | |||
262 | $cmd = "$command -u /statuscode/201"; | ||
263 | $result = NPTest->testCmd( $cmd ); | ||
264 | is( $result->return_code, 0, $cmd); | ||
265 | like( $result->output, '/^HTTP OK: HTTP/1.1 201 Created - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output ); | ||
266 | |||
267 | $cmd = "$command -u /statuscode/201 -e 201"; | ||
268 | $result = NPTest->testCmd( $cmd ); | ||
269 | is( $result->return_code, 0, $cmd); | ||
270 | like( $result->output, '/^HTTP OK: Status line output matched "201" - \d+ bytes in [\d\.]+ second /', "Output correct: ".$result->output ); | ||
271 | |||
272 | $cmd = "$command -u /statuscode/201 -e 200"; | ||
273 | $result = NPTest->testCmd( $cmd ); | ||
274 | is( $result->return_code, 2, $cmd); | ||
275 | like( $result->output, '/^HTTP CRITICAL - Invalid HTTP response received from host on port \d+: HTTP/1.1 201 Created/', "Output correct: ".$result->output ); | ||
276 | |||
277 | $cmd = "$command -u /statuscode/200 -e 200,201,202"; | ||
278 | $result = NPTest->testCmd( $cmd ); | ||
279 | is( $result->return_code, 0, $cmd); | ||
280 | like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
281 | |||
282 | $cmd = "$command -u /statuscode/201 -e 200,201,202"; | ||
283 | $result = NPTest->testCmd( $cmd ); | ||
284 | is( $result->return_code, 0, $cmd); | ||
285 | like( $result->output, '/^HTTP OK: Status line output matched "200,201,202" - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
286 | |||
287 | $cmd = "$command -u /statuscode/203 -e 200,201,202"; | ||
288 | $result = NPTest->testCmd( $cmd ); | ||
289 | is( $result->return_code, 2, $cmd); | ||
290 | 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 ); | ||
291 | |||
292 | $cmd = "$command -j HEAD -u /method"; | ||
293 | $result = NPTest->testCmd( $cmd ); | ||
294 | is( $result->return_code, 0, $cmd); | ||
295 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 HEAD - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
296 | |||
297 | $cmd = "$command -j POST -u /method"; | ||
298 | $result = NPTest->testCmd( $cmd ); | ||
299 | is( $result->return_code, 0, $cmd); | ||
300 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
301 | |||
302 | $cmd = "$command -j GET -u /method"; | ||
303 | $result = NPTest->testCmd( $cmd ); | ||
304 | is( $result->return_code, 0, $cmd); | ||
305 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
306 | |||
307 | $cmd = "$command -u /method"; | ||
308 | $result = NPTest->testCmd( $cmd ); | ||
309 | is( $result->return_code, 0, $cmd); | ||
310 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 GET - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
311 | |||
312 | $cmd = "$command -P foo -u /method"; | ||
313 | $result = NPTest->testCmd( $cmd ); | ||
314 | is( $result->return_code, 0, $cmd); | ||
315 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 POST - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
316 | |||
317 | $cmd = "$command -j DELETE -u /method"; | ||
318 | $result = NPTest->testCmd( $cmd ); | ||
319 | is( $result->return_code, 1, $cmd); | ||
320 | like( $result->output, '/^HTTP WARNING: HTTP/1.1 405 Method Not Allowed/', "Output correct: ".$result->output ); | ||
321 | |||
322 | $cmd = "$command -j foo -u /method"; | ||
323 | $result = NPTest->testCmd( $cmd ); | ||
324 | is( $result->return_code, 2, $cmd); | ||
325 | like( $result->output, '/^HTTP CRITICAL: HTTP/1.1 501 Not Implemented/', "Output correct: ".$result->output ); | ||
326 | |||
327 | $cmd = "$command -P stufftoinclude -u /postdata -s POST:stufftoinclude"; | ||
328 | $result = NPTest->testCmd( $cmd ); | ||
329 | is( $result->return_code, 0, $cmd); | ||
330 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
331 | |||
332 | $cmd = "$command -j PUT -P stufftoinclude -u /postdata -s PUT:stufftoinclude"; | ||
333 | $result = NPTest->testCmd( $cmd ); | ||
334 | is( $result->return_code, 0, $cmd); | ||
335 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
336 | |||
337 | # To confirm that the free doesn't segfault | ||
338 | $cmd = "$command -P stufftoinclude -j PUT -u /postdata -s PUT:stufftoinclude"; | ||
339 | $result = NPTest->testCmd( $cmd ); | ||
340 | is( $result->return_code, 0, $cmd); | ||
341 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
342 | |||
343 | $cmd = "$command -u /redirect"; | ||
344 | $result = NPTest->testCmd( $cmd ); | ||
345 | is( $result->return_code, 0, $cmd); | ||
346 | like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
347 | |||
348 | $cmd = "$command -f follow -u /redirect"; | ||
349 | $result = NPTest->testCmd( $cmd ); | ||
350 | is( $result->return_code, 0, $cmd); | ||
351 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
352 | |||
353 | $cmd = "$command -u /redirect -k 'follow: me'"; | ||
354 | $result = NPTest->testCmd( $cmd ); | ||
355 | is( $result->return_code, 0, $cmd); | ||
356 | like( $result->output, '/^HTTP OK: HTTP/1.1 301 Moved Permanently - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
357 | |||
358 | $cmd = "$command -f follow -u /redirect -k 'follow: me'"; | ||
359 | $result = NPTest->testCmd( $cmd ); | ||
360 | is( $result->return_code, 0, $cmd); | ||
361 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
362 | |||
363 | $cmd = "$command -f sticky -u /redirect -k 'follow: me'"; | ||
364 | $result = NPTest->testCmd( $cmd ); | ||
365 | is( $result->return_code, 0, $cmd); | ||
366 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
367 | |||
368 | $cmd = "$command -f stickyport -u /redirect -k 'follow: me'"; | ||
369 | $result = NPTest->testCmd( $cmd ); | ||
370 | is( $result->return_code, 0, $cmd); | ||
371 | like( $result->output, '/^HTTP OK: HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second/', "Output correct: ".$result->output ); | ||
372 | |||
373 | # These tests may block | ||
374 | print "ALRM\n"; | ||
375 | |||
376 | # stickyport - on full urlS port is set back to 80 otherwise | ||
377 | $cmd = "$command -f stickyport -u /redir_external -t 5 -s redirected"; | ||
378 | eval { | ||
379 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
380 | alarm(2); | ||
381 | $result = NPTest->testCmd( $cmd ); | ||
382 | alarm(0); }; | ||
383 | isnt( $@, "alarm\n", $cmd ); | ||
384 | is( $result->return_code, 0, $cmd ); | ||
385 | |||
386 | # Let's hope there won't be any web server on :80 returning "redirected"! | ||
387 | $cmd = "$command -f sticky -u /redir_external -t 5 -s redirected"; | ||
388 | eval { | ||
389 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
390 | alarm(2); | ||
391 | $result = NPTest->testCmd( $cmd ); | ||
392 | alarm(0); }; | ||
393 | isnt( $@, "alarm\n", $cmd ); | ||
394 | isnt( $result->return_code, 0, $cmd ); | ||
395 | |||
396 | # Test an external address - timeout | ||
397 | SKIP: { | ||
398 | skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL}); | ||
399 | $cmd = "$command -f follow -u /redir_external -t 5"; | ||
400 | eval { | ||
401 | $result = NPTest->testCmd( $cmd, 2 ); | ||
402 | }; | ||
403 | like( $@, "/timeout in command: $cmd/", $cmd ); | ||
404 | } | ||
405 | |||
406 | $cmd = "$command -u /timeout -t 5"; | ||
407 | eval { | ||
408 | $result = NPTest->testCmd( $cmd, 2 ); | ||
409 | }; | ||
410 | like( $@, "/timeout in command: $cmd/", $cmd ); | ||
411 | |||
412 | $cmd = "$command -f follow -u /redir_timeout -t 2"; | ||
413 | eval { | ||
414 | $result = NPTest->testCmd( $cmd, 5 ); | ||
415 | }; | ||
416 | is( $@, "", $cmd ); | ||
417 | |||
418 | } | ||