diff options
| author | Ahmet Oeztuerk <Ahmet.Oeztuerk@consol.de> | 2025-12-12 11:16:09 +0100 |
|---|---|---|
| committer | Ahmet Oeztuerk <Ahmet.Oeztuerk@consol.de> | 2025-12-12 11:29:35 +0100 |
| commit | f8db90c206e777a21ff69a301406a11ca627034c (patch) | |
| tree | 2cc8a313cb5797a01d5f14234a312ef1c4230ff9 | |
| parent | 737af667a212f7058143b34770f7f0af70aa8d9b (diff) | |
| download | monitoring-plugins-f8db90c206e777a21ff69a301406a11ca627034c.tar.gz | |
check_curl redirection test improvements
previously, the fragment was sent in the request from client, and the
server would parse and increment its value. the incremented value would
be set in the redirected URI.
this does not work as fragments are meaningless to servers and clients
like check_curl strip them in their GET request.
rewrite the fragment handling . if client sends a URI parameter with
'fragment' as its key, the server will set its value for its redirected
URI. it will come up both as a parameter and the fragment at the end.
use this new logic to rewrite the fragment redirection test. remove -p
$http_port argument on tests for this endpoint, which was making https
tests fail. correct the common test count from 75 to 95, as there are 20
total test assertions in the 8 times it uses the new endpoint. remove
unused code on that endpoint as well
| -rwxr-xr-x | plugins/tests/check_curl.t | 66 |
1 files changed, 20 insertions, 46 deletions
diff --git a/plugins/tests/check_curl.t b/plugins/tests/check_curl.t index 374c74b3..248eb4c5 100755 --- a/plugins/tests/check_curl.t +++ b/plugins/tests/check_curl.t | |||
| @@ -27,7 +27,7 @@ use HTTP::Daemon::SSL; | |||
| 27 | 27 | ||
| 28 | $ENV{'LC_TIME'} = "C"; | 28 | $ENV{'LC_TIME'} = "C"; |
| 29 | 29 | ||
| 30 | my $common_tests = 75; | 30 | my $common_tests = 95; |
| 31 | my $ssl_only_tests = 8; | 31 | my $ssl_only_tests = 8; |
| 32 | # Check that all dependent modules are available | 32 | # Check that all dependent modules are available |
| 33 | eval "use HTTP::Daemon 6.01;"; | 33 | eval "use HTTP::Daemon 6.01;"; |
| @@ -211,19 +211,6 @@ sub run_server { | |||
| 211 | $content .= " query: ${query}\n"; | 211 | $content .= " query: ${query}\n"; |
| 212 | $content .= " fragment: ${fragment}\n"; | 212 | $content .= " fragment: ${fragment}\n"; |
| 213 | 213 | ||
| 214 | # Sets and returns the scheme-specific part of the $uri (everything between the scheme and the fragment) as an escaped string. | ||
| 215 | #my $opaque = $uri->opaque; | ||
| 216 | #$content .= " opaque: ${opaque}\n"; | ||
| 217 | |||
| 218 | # group 1 is captured: anything that is not '/' : ([^/]*) | ||
| 219 | # / matches the / directly | ||
| 220 | # group 2 is captured: anything : (.*) | ||
| 221 | #my ($before_slash, $after_slash) = $opaque =~ m{^/([^/]*)/(.*)$}; | ||
| 222 | #$before_slash //= ''; | ||
| 223 | #$after_slash //= ''; | ||
| 224 | #$content .= " before_slash: ${before_slash}\n"; | ||
| 225 | #$content .= " after_slash: ${after_slash}\n"; | ||
| 226 | |||
| 227 | # split the URI part and parameters. URI package cannot do this | 214 | # split the URI part and parameters. URI package cannot do this |
| 228 | # group 1 is captured: anything without a semicolon: ([^;]*) | 215 | # group 1 is captured: anything without a semicolon: ([^;]*) |
| 229 | # group 2 is uncaptured: (?:;(.*))? | 216 | # group 2 is uncaptured: (?:;(.*))? |
| @@ -257,20 +244,6 @@ sub run_server { | |||
| 257 | $content .= " query: ${key} -> ${value}\n"; | 244 | $content .= " query: ${key} -> ${value}\n"; |
| 258 | } | 245 | } |
| 259 | 246 | ||
| 260 | # fragment: try to split into key=value pairs on ';' or '&' if present | ||
| 261 | my @fragment_pairs; | ||
| 262 | my $fragment_separator = ''; | ||
| 263 | if ($fragment ne '') { | ||
| 264 | $fragment_separator = ($fragment =~ /&/ ? '&' : ';'); | ||
| 265 | for my $f (split /[&;]/, $fragment) { | ||
| 266 | next unless length $f; | ||
| 267 | my ($key,$value) = split /=/, $f, 2; | ||
| 268 | $value //= ''; | ||
| 269 | push @fragment_pairs, [ $key, $value ]; | ||
| 270 | $content .= " fragment: ${key} -> ${value}\n"; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | # helper to increment value | 247 | # helper to increment value |
| 275 | my $increment = sub { | 248 | my $increment = sub { |
| 276 | my ($v) = @_; | 249 | my ($v) = @_; |
| @@ -296,10 +269,6 @@ sub run_server { | |||
| 296 | $pair->[1] = $increment->($pair->[1]); | 269 | $pair->[1] = $increment->($pair->[1]); |
| 297 | $content .= " query parameter new: " . $pair->[0] . " -> " . $pair->[1] . "\n"; | 270 | $content .= " query parameter new: " . $pair->[0] . " -> " . $pair->[1] . "\n"; |
| 298 | } | 271 | } |
| 299 | for my $pair (@fragment_pairs) { | ||
| 300 | $pair->[1] = $increment->($pair->[1]); | ||
| 301 | $content .= " fragment new: " . $pair->[0] . " -> " . $pair->[1] . "\n"; | ||
| 302 | } | ||
| 303 | 272 | ||
| 304 | # rebuild strings | 273 | # rebuild strings |
| 305 | my $new_parameter_str = join(';', map { $_->[0] . '=' . $_->[1] } @parameter_pairs); | 274 | my $new_parameter_str = join(';', map { $_->[0] . '=' . $_->[1] } @parameter_pairs); |
| @@ -309,9 +278,13 @@ sub run_server { | |||
| 309 | my @new_query_form; | 278 | my @new_query_form; |
| 310 | for my $p (@query_parameter_pairs) { push @new_query_form, $p->[0], $p->[1] } | 279 | for my $p (@query_parameter_pairs) { push @new_query_form, $p->[0], $p->[1] } |
| 311 | 280 | ||
| 312 | my $new_fragment_str = ''; | 281 | my $new_fragment_str = ''; |
| 313 | if (@fragment_pairs) { | 282 | for my $pair (@parameter_pairs) { |
| 314 | $new_fragment_str = join($fragment_separator, map { $_->[0] . '=' . $_->[1] } @fragment_pairs); | 283 | my $key = $pair->[0]; |
| 284 | my $value = $pair->[1]; | ||
| 285 | if ($key eq "fragment") { | ||
| 286 | $new_fragment_str = $value | ||
| 287 | } | ||
| 315 | } | 288 | } |
| 316 | $content .= " new_fragment_str: ${new_fragment_str}\n"; | 289 | $content .= " new_fragment_str: ${new_fragment_str}\n"; |
| 317 | 290 | ||
| @@ -635,57 +608,58 @@ sub run_common_tests { | |||
| 635 | # The server at this point has dynamic redirection. It tries to increment values that it sees in these fields, then redirects. | 608 | # The server at this point has dynamic redirection. It tries to increment values that it sees in these fields, then redirects. |
| 636 | # It also appends some debug log and writes it into HTTP content, pass the -vvv parameter to see them. | 609 | # It also appends some debug log and writes it into HTTP content, pass the -vvv parameter to see them. |
| 637 | 610 | ||
| 638 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2/path3/path4' --onredirect=follow -vvv"; | 611 | $cmd = "$command -u '/redirect_with_increment/path1/path2/path3/path4' --onredirect=follow -vvv"; |
| 639 | $result = NPTest->testCmd( "$cmd" ); | 612 | $result = NPTest->testCmd( "$cmd" ); |
| 640 | is( $result->return_code, 1, $cmd); | 613 | is( $result->return_code, 1, $cmd); |
| 641 | like( $result->output, '/.*HTTP/1.1 403 Forbidden - \d+ bytes in [\d\.]+ second.*/', "Output correct, redirect_count was not present, got redirected to / : ".$result->output ); | 614 | like( $result->output, '/.*HTTP/1.1 403 Forbidden - \d+ bytes in [\d\.]+ second.*/', "Output correct, redirect_count was not present, got redirected to / : ".$result->output ); |
| 642 | 615 | ||
| 643 | # redirect_count=0 is parsed as a parameter and incremented. When it goes up to 3, the redirection returns HTTP OK | 616 | # redirect_count=0 is parsed as a parameter and incremented. When it goes up to 3, the redirection returns HTTP OK |
| 644 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; | 617 | $cmd = "$command -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; |
| 645 | $result = NPTest->testCmd( "$cmd" ); | 618 | $result = NPTest->testCmd( "$cmd" ); |
| 646 | is( $result->return_code, 0, $cmd); | 619 | is( $result->return_code, 0, $cmd); |
| 647 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, redirect_count went up to 3, and returned OK: ".$result->output ); | 620 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, redirect_count went up to 3, and returned OK: ".$result->output ); |
| 648 | 621 | ||
| 649 | # location_redirect_count=0 goes up to 3, which uses the HTTP 302 style of redirection with 'Location' header | 622 | # location_redirect_count=0 goes up to 3, which uses the HTTP 302 style of redirection with 'Location' header |
| 650 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2;location_redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; | 623 | $cmd = "$command -u '/redirect_with_increment/path1/path2;location_redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; |
| 651 | $result = NPTest->testCmd( "$cmd" ); | 624 | $result = NPTest->testCmd( "$cmd" ); |
| 652 | is( $result->return_code, 0, $cmd); | 625 | is( $result->return_code, 0, $cmd); |
| 653 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); | 626 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); |
| 654 | 627 | ||
| 655 | # fail_count parameter may also go up to 3, which returns a HTTP 403 | 628 | # fail_count parameter may also go up to 3, which returns a HTTP 403 |
| 656 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2;redirect_count=0;fail_count=2' --onredirect=follow -vvv"; | 629 | $cmd = "$command -u '/redirect_with_increment/path1/path2;redirect_count=0;fail_count=2' --onredirect=follow -vvv"; |
| 657 | $result = NPTest->testCmd( "$cmd" ); | 630 | $result = NPTest->testCmd( "$cmd" ); |
| 658 | is( $result->return_code, 1, $cmd); | 631 | is( $result->return_code, 1, $cmd); |
| 659 | like( $result->output, '/.*HTTP/1.1 403 Forbidden - \d+ bytes in [\d\.]+ second.*/', "Output correct, early due to fail_count reaching 3: ".$result->output ); | 632 | like( $result->output, '/.*HTTP/1.1 403 Forbidden - \d+ bytes in [\d\.]+ second.*/', "Output correct, early due to fail_count reaching 3: ".$result->output ); |
| 660 | 633 | ||
| 661 | # redirect_count=0, p1=1 , p2=ab => redirect_count=1, p1=2 , p2=bc => redirect_count=2, p1=3 , p2=cd => redirect_count=3 , p1=4 , p2=de | 634 | # redirect_count=0, p1=1 , p2=ab => redirect_count=1, p1=2 , p2=bc => redirect_count=2, p1=3 , p2=cd => redirect_count=3 , p1=4 , p2=de |
| 662 | # Last visited URI returns HTTP OK instead of redirect, and the one before that contains the new_uri in its content | 635 | # Last visited URI returns HTTP OK instead of redirect, and the one before that contains the new_uri in its content |
| 663 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; | 636 | $cmd = "$command -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; |
| 664 | $result = NPTest->testCmd( "$cmd" ); | 637 | $result = NPTest->testCmd( "$cmd" ); |
| 665 | is( $result->return_code, 0, $cmd); | 638 | is( $result->return_code, 0, $cmd); |
| 666 | like( $result->output, '/.*redirect_count=3;p1=4;p2=de\?*/', "Output correct, parsed and incremented both parameters p1 and p2 : ".$result->output ); | 639 | like( $result->output, '/.*redirect_count=3;p1=4;p2=de\?*/', "Output correct, parsed and incremented both parameters p1 and p2 : ".$result->output ); |
| 667 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); | 640 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); |
| 668 | 641 | ||
| 669 | # Same incrementation as before, uses the query parameters that come after the first '?' : qp1 and qp2 | 642 | # Same incrementation as before, uses the query parameters that come after the first '?' : qp1 and qp2 |
| 670 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; | 643 | $cmd = "$command -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; |
| 671 | $result = NPTest->testCmd( "$cmd" ); | 644 | $result = NPTest->testCmd( "$cmd" ); |
| 672 | is( $result->return_code, 0, $cmd); | 645 | is( $result->return_code, 0, $cmd); |
| 673 | like( $result->output, '/.*\?qp1=13&qp2=no*/', "Output correct, parsed and incremented both query parameters qp1 and qp2 : ".$result->output ); | 646 | like( $result->output, '/.*\?qp1=13&qp2=no*/', "Output correct, parsed and incremented both query parameters qp1 and qp2 : ".$result->output ); |
| 674 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); | 647 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); |
| 675 | 648 | ||
| 676 | # Check if the query parameter order is kept intact | 649 | # Check if the query parameter order is kept intact |
| 677 | $cmd = "$command -p $port_http -u '/redirect_with_increment;redirect_count=0;?qp0=0&qp1=1&qp2=2&qp3=3&qp4=4&qp5=5' --onredirect=follow -vvv"; | 650 | $cmd = "$command -u '/redirect_with_increment;redirect_count=0;?qp0=0&qp1=1&qp2=2&qp3=3&qp4=4&qp5=5' --onredirect=follow -vvv"; |
| 678 | $result = NPTest->testCmd( "$cmd" ); | 651 | $result = NPTest->testCmd( "$cmd" ); |
| 679 | is( $result->return_code, 0, $cmd); | 652 | is( $result->return_code, 0, $cmd); |
| 680 | like( $result->output, '/.*\?qp0=3&qp1=4&qp2=5&qp3=6&qp4=7&qp5=8*/', "Output correct, parsed and incremented query parameters qp1,qp2,qp3,qp4,qp5 in order : ".$result->output ); | 653 | like( $result->output, '/.*\?qp0=3&qp1=4&qp2=5&qp3=6&qp4=7&qp5=8*/', "Output correct, parsed and incremented query parameters qp1,qp2,qp3,qp4,qp5 in order : ".$result->output ); |
| 681 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); | 654 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); |
| 682 | 655 | ||
| 683 | # The fragment is a single item, and it should be kept during redirections as well. | 656 | # The fragment is passed as another parameter. |
| 684 | # Increase the chars in strings. 'test' => 'uftu' => 'vguv' => 'whvw' | 657 | # During the server redirects the fragment will be set to its value, if such a key is present. |
| 685 | $cmd = "$command -p $port_http -u '/redirect_with_increment/path1/path2;redirect_count=0;p1=1;p2=ab?qp1=10&qp2=kl#f1=test' --onredirect=follow -vvv"; | 658 | # 'ebiil' => 'fcjjm' => 'gdkkn' => 'hello' |
| 659 | $cmd = "$command -u '/redirect_with_increment/path1/path2;redirect_count=0;fragment=ebiil?qp1=0' --onredirect=follow -vvv"; | ||
| 686 | $result = NPTest->testCmd( "$cmd" ); | 660 | $result = NPTest->testCmd( "$cmd" ); |
| 687 | is( $result->return_code, 0, $cmd); | 661 | is( $result->return_code, 0, $cmd); |
| 688 | like( $result->output, '/.*#f1=whvw*/', "Output correct, parsed and incremented fragment f1 : ".$result->output ); | 662 | like( $result->output, '/.*redirect_count=3;fragment=hello\?qp1=3#hello*/', "Output correct, fragments are specified by server and followed by check_curl: ".$result->output ); |
| 689 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); | 663 | like( $result->output, '/.*HTTP/1.1 200 OK - \d+ bytes in [\d\.]+ second.*/', "Output correct, location_redirect_count went up to 3: ".$result->output ); |
| 690 | 664 | ||
| 691 | # These tests may block | 665 | # These tests may block |
