3 ############################################################################
5 # Privoxy-Regression-Test
7 # A regression test "framework" for Privoxy. For documentation see:
8 # perldoc privoxy-regression-test.pl
10 # $Id: privoxy-regression-test.pl,v 1.114 2008/03/18 16:52:09 fk Exp $
14 # - Update documentation
15 # - Validate HTTP times.
16 # - Understand default.action.master comment syntax
17 # and verify that we actually block and unblock what
18 # the comments claim we do.
19 # - Implement a HTTP_VERSION directive or allow to
20 # specify whole request lines.
21 # - Support filter regression tests.
22 # - Add option to fork regression tests and run them in parallel,
23 # possibly optional forever.
24 # - Document magic Expect Header values
25 # - Internal fuzz support?
27 # Copyright (c) 2007-2008 Fabian Keil <fk@fabiankeil.de>
29 # Permission to use, copy, modify, and distribute this software for any
30 # purpose with or without fee is hereby granted, provided that the above
31 # copyright notice and this permission notice appear in all copies.
33 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
34 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
35 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
36 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
37 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
38 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
39 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 ############################################################################
48 PRT_VERSION => 'Privoxy-Regression-Test 0.2',
59 PRIVOXY_CGI_URL => 'http://p.p/',
60 FELLATIO_URL => 'http://10.0.0.1:8080/',
61 LEADING_LOG_DATE => 1,
62 LEADING_LOG_TIME => 1,
64 DEBUG_LEVEL_FILE_LOADING => 0,
65 DEBUG_LEVEL_PAGE_FETCHING => 0,
67 VERBOSE_TEST_DESCRIPTION => 1,
69 DEBUG_LEVEL_VERBOSE_FAILURE => 1,
70 # XXX: Only partly implemented and mostly useless.
71 DEBUG_LEVEL_VERBOSE_SUCCESS => 0,
72 DEBUG_LEVEL_STATUS => 1,
74 # Internal use, don't modify
75 # Available debug bits:
77 LL_VERBOSE_FAILURE => 2,
78 LL_PAGE_FETCHING => 4,
80 LL_VERBOSE_SUCCESS => 16,
84 CLIENT_HEADER_TEST => 1,
85 SERVER_HEADER_TEST => 2,
88 TRUSTED_CGI_REQUEST => 6,
92 sub init_our_variables () {
94 our $leading_log_time = LEADING_LOG_TIME;
95 our $leading_log_date = LEADING_LOG_DATE;
97 our $privoxy_cgi_url = PRIVOXY_CGI_URL;
99 our $verbose_test_description = VERBOSE_TEST_DESCRIPTION;
101 our $log_level = get_default_log_level();
105 sub get_default_log_level () {
109 $log_level |= LL_FILE_LOADING if DEBUG_LEVEL_FILE_LOADING;
110 $log_level |= LL_PAGE_FETCHING if DEBUG_LEVEL_PAGE_FETCHING;
111 $log_level |= LL_VERBOSE_FAILURE if DEBUG_LEVEL_VERBOSE_FAILURE;
112 $log_level |= LL_VERBOSE_SUCCESS if DEBUG_LEVEL_VERBOSE_SUCCESS;
113 $log_level |= LL_STATUS if DEBUG_LEVEL_STATUS;
115 # These are intended to be always on.
116 $log_level |= LL_SOFT_ERROR;
117 $log_level |= LL_ERROR;
122 ############################################################################
124 # File loading functions
126 ############################################################################
134 # Unescape brackets and dots
135 $tag =~ s@\\(?=[{}().+])@@g;
137 # log_message("Parsed tag: " . $tag);
139 check_for_forbidden_characters($tag);
144 sub check_for_forbidden_characters ($) {
146 my $tag = shift; # XXX: also used to check values though.
147 my $allowed = '[-=\dA-Za-z{}:.\/();\s,+@"_%\?&]';
149 unless ($tag =~ m/^$allowed*$/) {
150 my $forbidden = $tag;
151 $forbidden =~ s@^$allowed*(.).*@$1@;
153 l(LL_ERROR, "'" . $tag . "' contains character '" . $forbidden. "' which is unacceptable.");
157 sub load_regressions_tests () {
159 our $privoxy_cgi_url;
165 $curl_url .= $privoxy_cgi_url;
166 $curl_url .= 'show-status';
168 l(LL_STATUS, "Asking Privoxy for the number of action files available ...");
170 foreach (@{get_cgi_page_or_else($curl_url)}) {
173 if (/<td>(.*?)<\/td><td class=\"buttons\"><a href=\"\/show-status\?file=actions&index=(\d+)\">/) {
175 my $url = $privoxy_cgi_url . 'show-status?file=actions&index=' . $2;
176 $actionfiles[$file_number++] = $url;
178 } elsif (m@config\.html#.*\">([^<]*)</a>\s+(.*)<br>@) {
180 my $directive = $1 . " " . $2;
181 push (@privoxy_config, $directive);
185 l(LL_FILE_LOADING, "Recognized " . @actionfiles . " actions files");
187 load_action_files(\@actionfiles);
190 sub token_starts_new_test ($) {
193 my @new_test_directives =
194 ('set header', 'fetch test', 'trusted cgi request', 'request header', 'method test', 'block test');
196 foreach my $new_test_directive (@new_test_directives) {
197 return 1 if $new_test_directive eq $token;
205 my ($token, $value) = (undef, undef);
207 # Remove leading and trailing white space.
211 # Reverse HTML-encoding
212 # XXX: Seriously imcomplete.
216 if (/^\#\s*([^=]*?)\s*[=]\s*(.*?)\s*$/) {
219 $token =~ tr/[A-Z]/[a-z]/;
222 } elsif (/^TAG\s*:(.*)$/) {
229 return ($token, $value);
232 sub enlist_new_test ($$$$$$) {
234 my ($regression_tests, $token, $value, $si, $ri, $number) = @_;
236 if ($token eq 'set header') {
238 l(LL_FILE_LOADING, "Header to set: " . $value);
239 ${$regression_tests}[$si][$ri]{'type'} = CLIENT_HEADER_TEST;
241 $$regression_tests[$si][$ri]{'level'} = CLIENT_HEADER_TEST;
243 } elsif ($token eq 'request header') {
245 l(LL_FILE_LOADING, "Header to request: " . $value);
246 $$regression_tests[$si][$ri]{'type'} = SERVER_HEADER_TEST;
248 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
249 $$regression_tests[$si][$ri]{'level'} = SERVER_HEADER_TEST;
251 } elsif ($token eq 'trusted cgi request') {
253 l(LL_FILE_LOADING, "CGI URL to test in a dumb way: " . $value);
254 $$regression_tests[$si][$ri]{'type'} = TRUSTED_CGI_REQUEST;
256 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
257 $$regression_tests[$si][$ri]{'level'} = TRUSTED_CGI_REQUEST;
259 } elsif ($token eq 'fetch test') {
261 l(LL_FILE_LOADING, "URL to test in a dumb way: " . $value);
262 $$regression_tests[$si][$ri]{'type'} = DUMB_FETCH_TEST;
264 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
265 $$regression_tests[$si][$ri]{'level'} = DUMB_FETCH_TEST;
267 } elsif ($token eq 'method test') {
269 l(LL_FILE_LOADING, "Method to test: " . $value);
270 $$regression_tests[$si][$ri]{'type'} = METHOD_TEST;
272 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
273 $$regression_tests[$si][$ri]{'level'} = METHOD_TEST;
275 } elsif ($token eq 'block test') {
277 l(LL_FILE_LOADING, "URL to block-test: " . $value);
278 $$regression_tests[$si][$ri]{'type'} = BLOCK_TEST;
280 $$regression_tests[$si][$ri]{'expected-status-code'} = 403;
281 $$regression_tests[$si][$ri]{'level'} = BLOCK_TEST;
285 die "Incomplete '" . $token . "' support detected.";
289 check_for_forbidden_characters($value);
291 $$regression_tests[$si][$ri]{'data'} = $value;
293 # For function that only get passed single tests
294 $$regression_tests[$si][$ri]{'section-id'} = $si;
295 $$regression_tests[$si][$ri]{'regression-test-id'} = $ri;
296 $$regression_tests[$si][$ri]{'number'} = $number - 1;
298 "Regression test " . $number . " (section:" . $si . "):");
301 sub load_action_files ($) {
305 our @regression_tests;
307 my $actionfiles_ref = shift;
308 my @actionfiles = @{$actionfiles_ref};
310 my $si = 0; # Section index
311 my $ri = -1; # Regression test index
316 l(LL_STATUS, "Loading regression tests from action file(s) delivered by Privoxy.");
318 for my $file_number (0 .. @actionfiles - 1) {
320 my $curl_url = ' "' . $actionfiles[$file_number] . '"';
321 my $actionfile = undef;
323 foreach (@{get_cgi_page_or_else($curl_url)}) {
328 if (/<h2>Contents of Actions File (.*?)</) {
332 next unless defined $actionfile;
336 my ($token, $value) = tokenize($_);
338 next unless defined $token;
340 # Load regression tests
342 if (token_starts_new_test($token)) {
344 # Beginning of new regression test.
347 enlist_new_test(\@regression_tests, $token, $value, $si, $ri, $count);
350 if ($token =~ /level\s+(\d+)/i) {
353 register_dependency($level, $value);
356 if ($si == -1 || $ri == -1) {
357 # No beginning of a test detected yet,
358 # so we don't care about any other test
363 if ($token eq 'expect header') {
365 l(LL_FILE_LOADING, "Detected expectation: " . $value);
366 $regression_tests[$si][$ri]{'expect-header'} = $value;
368 } elsif ($token eq 'tag') {
372 my $tag = parse_tag($value);
374 # We already checked in parse_tag() after filtering
377 l(LL_FILE_LOADING, "Detected TAG: " . $tag);
379 # Save tag for all tests in this section
381 $regression_tests[$si][$ri]{'tag'} = $tag;
387 } elsif ($token eq 'ignore' && $value =~ /Yes/i) {
389 l(LL_FILE_LOADING, "Ignoring section: " . test_content_as_string($regression_tests[$si][$ri]));
390 $regression_tests[$si][$ri]{'ignore'} = 1;
393 } elsif ($token eq 'expect status code') {
395 l(LL_FILE_LOADING, "Expecting status code: " . $value);
396 $regression_tests[$si][$ri]{'expected-status-code'} = $value;
398 } elsif ($token eq 'level') { # XXX: stupid name
400 $value =~ s@(\d+).*@$1@;
401 l(LL_FILE_LOADING, "Level: " . $value);
402 $regression_tests[$si][$ri]{'level'} = $value;
404 } elsif ($token eq 'method') {
406 l(LL_FILE_LOADING, "Method: " . $value);
407 $regression_tests[$si][$ri]{'method'} = $value;
411 # We don't use it, so we don't need
415 check_for_forbidden_characters($value) unless $no_checks;
416 check_for_forbidden_characters($token);
420 l(LL_FILE_LOADING, "Done loading " . $count . " regression tests."
421 . " Of which " . $ignored. " will be ignored)\n");
424 ############################################################################
426 # Regression test executing functions
428 ############################################################################
430 sub execute_regression_tests () {
432 our @regression_tests;
433 my $loops = get_cli_option('loops');
435 my $all_failures = 0;
436 my $all_successes = 0;
438 unless (@regression_tests) {
440 l(LL_STATUS, "No regression tests found.");
444 l(LL_STATUS, "Executing regression tests ...");
446 while ($loops-- > 0) {
453 for my $s (0 .. @regression_tests - 1) {
457 while (defined $regression_tests[$s][$r]) {
459 die "Section id mismatch" if ($s != $regression_tests[$s][$r]{'section-id'});
460 die "Regression test id mismatch" if ($r != $regression_tests[$s][$r]{'regression-test-id'});
462 my $number = $regression_tests[$s][$r]{'number'};
464 if ($regression_tests[$s][$r]{'ignore'}
465 or level_is_unacceptable($regression_tests[$s][$r]{'level'})
466 or test_number_is_unacceptable($number)) {
472 my $result = execute_regression_test($regression_tests[$s][$r]);
474 log_result($regression_tests[$s][$r], $result, $tests);
476 $successes += $result;
482 $failures = $tests - $successes;
484 log_message("Executed " . $tests . " regression tests. " .
485 'Skipped ' . $skipped . '. ' .
486 $successes . " successes, " . $failures . " failures.");
488 $all_tests += $tests;
489 $all_failures += $failures;
490 $all_successes += $successes;
494 if (get_cli_option('loops') > 1) {
495 log_message("Total: Executed " . $all_tests . " regression tests. " .
496 $all_successes . " successes, " . $all_failures . " failures.");
500 sub level_is_unacceptable ($) {
502 return ((cli_option_is_set('level') and get_cli_option('level') != $level)
503 or ($level < get_cli_option('min-level'))
504 or ($level > get_cli_option('max-level'))
505 or dependency_unsatisfied($level)
509 sub test_number_is_unacceptable ($) {
510 my $test_number = shift;
511 return (cli_option_is_set('test-number')
512 and get_cli_option('test-number') != $test_number)
515 sub dependency_unsatisfied ($) {
520 my $dependency_problem = 0;
522 if (defined ($dependencies{$level}{'config line'})) {
524 my $dependency = $dependencies{$level}{'config line'};
525 $dependency_problem = 1;
527 foreach (@privoxy_config) {
529 $dependency_problem = 0 if (/$dependency/);
533 return $dependency_problem;
536 sub register_dependency ($$) {
539 my $dependency = shift;
542 if ($dependency =~ /config line\s+(.*)/) {
544 $dependencies{$level}{'config line'} = $1;
548 # XXX: somewhat misleading name
549 sub execute_regression_test ($) {
551 my $test_ref = shift;
552 my %test = %{$test_ref};
555 if ($test{'type'} == CLIENT_HEADER_TEST) {
557 $result = execute_client_header_regression_test($test_ref);
559 } elsif ($test{'type'} == SERVER_HEADER_TEST) {
561 $result = execute_server_header_regression_test($test_ref);
563 } elsif ($test{'type'} == DUMB_FETCH_TEST
564 or $test{'type'} == TRUSTED_CGI_REQUEST) {
566 $result = execute_dumb_fetch_test($test_ref);
568 } elsif ($test{'type'} == METHOD_TEST) {
570 $result = execute_method_test($test_ref);
572 } elsif ($test{'type'} == BLOCK_TEST) {
574 $result = execute_block_test($test_ref);
578 die "Unsupported test type detected: " . $test{'type'};
585 sub execute_method_test ($) {
587 my $test_ref = shift;
588 my %test = %{$test_ref};
592 my $method = $test{'data'};
594 my $curl_parameters = '';
595 my $expected_status_code = $test{'expected-status-code'};
597 $curl_parameters .= '--request ' . $method . ' ';
598 # Don't complain in case about the 'missing' body
599 $curl_parameters .= '--head ' if ($method =~ /^HEAD$/i);
601 $curl_parameters .= PRIVOXY_CGI_URL;
603 $buffer_ref = get_page_with_curl($curl_parameters);
604 $status_code = get_status_code($buffer_ref);
606 $result = check_status_code_result($status_code, $expected_status_code);
611 sub execute_dumb_fetch_test ($) {
613 my $test_ref = shift;
614 my %test = %{$test_ref};
619 my $curl_parameters = '';
620 my $expected_status_code = $test{'expected-status-code'};
622 if (defined $test{method}) {
623 $curl_parameters .= '--request ' . $test{method} . ' ';
625 if ($test{type} == TRUSTED_CGI_REQUEST) {
626 $curl_parameters .= '--referer ' . PRIVOXY_CGI_URL . ' ';
629 $curl_parameters .= $test{'data'};
631 $buffer_ref = get_page_with_curl($curl_parameters);
632 $status_code = get_status_code($buffer_ref);
634 $result = check_status_code_result($status_code, $expected_status_code);
639 sub execute_block_test ($) {
642 my $url = $test->{'data'};
643 my $final_results = get_final_results($url);
645 return defined $final_results->{'+block'};
648 sub get_final_results ($) {
651 my $curl_parameters = '';
652 my %final_results = ();
653 my $final_results_reached = 0;
655 $curl_parameters .= PRIVOXY_CGI_URL . 'show-url-info?url=' . $url;
657 foreach (@{get_cgi_page_or_else($curl_parameters)}) {
659 $final_results_reached = 1 if (m@<h2>Final results:</h2>@);
661 next unless ($final_results_reached);
664 if (m@<br>([-+])<a.*>([^>]*)</a>( \{.*\})@) {
667 $final_results{$action}{$value};
671 return \%final_results;
674 sub check_status_code_result ($$) {
676 my $status_code = shift;
677 my $expected_status_code = shift;
680 if ($expected_status_code == $status_code) {
683 l(LL_VERBOSE_SUCCESS,
684 "Yay. We expected status code " . $expected_status_code . ", and received: " . $status_code . '.');
686 } elsif (cli_option_is_set('fuzzer-feeding') and $status_code == 123) {
688 l(LL_VERBOSE_FAILURE,
689 "Oh well. Status code lost while fuzzing. Can't check if it was " . $expected_status_code . '.');
693 l(LL_VERBOSE_FAILURE,
694 "Ooops. We expected status code " . $expected_status_code . ", but received: " . $status_code . '.');
701 sub execute_client_header_regression_test ($) {
703 my $test_ref = shift;
708 $buffer_ref = get_show_request_with_curl($test_ref);
710 $header = get_header($buffer_ref, $test_ref);
711 $result = check_header_result($test_ref, $header);
716 sub execute_server_header_regression_test ($) {
718 my $test_ref = shift;
723 $buffer_ref = get_head_with_curl($test_ref);
725 $header = get_server_header($buffer_ref, $test_ref);
726 $result = check_header_result($test_ref, $header);
732 sub interpret_result ($) {
734 return $success ? "Success" : "Failure";
737 sub check_header_result ($$) {
739 my $test_ref = shift;
742 my %test = %{$test_ref};
743 my $expect_header = $test{'expect-header'};
746 $header =~ s@ @ @g if defined($header);
748 if ($expect_header eq 'NO CHANGE') {
750 if (defined($header) and $header eq $test{'data'}) {
756 $header //= "REMOVAL";
757 l(LL_VERBOSE_FAILURE,
758 "Ooops. Got: " . $header . " while expecting: " . $expect_header);
761 } elsif ($expect_header eq 'REMOVAL') {
763 if (defined($header) and $header eq $test{'data'}) {
765 l(LL_VERBOSE_FAILURE,
766 "Ooops. Expected removal but: " . $header . " is still there.");
770 # XXX: Use more reliable check here and make sure
771 # the header has a different name.
776 } elsif ($expect_header eq 'SOME CHANGE') {
778 if (defined($header) and not $header eq $test{'data'}) {
784 $header //= "REMOVAL";
785 l(LL_VERBOSE_FAILURE,
786 "Ooops. Got: " . $header . " while expecting: SOME CHANGE");
792 if (defined($header) and $header eq $expect_header) {
798 $header //= "'No matching header'"; # XXX: No header detected to be precise
799 l(LL_VERBOSE_FAILURE,
800 "Ooops. Got: " . $header . " while expecting: " . $expect_header);
806 sub get_header_name ($) {
810 $header =~ s@(.*?: ).*@$1@;
815 sub get_header ($$) {
817 our $filtered_request = '';
819 my $buffer_ref = shift;
820 my $test_ref = shift;
822 my %test = %{$test_ref};
823 my @buffer = @{$buffer_ref};
825 my $expect_header = $test{'expect-header'};
828 my $processed_request_reached = 0;
830 my $processed_request = '';
834 if ($expect_header eq 'REMOVAL'
835 or $expect_header eq 'NO CHANGE'
836 or $expect_header eq 'SOME CHANGE') {
838 $expect_header = $test{'data'};
842 $header_to_get = get_header_name($expect_header);
846 # Skip everything before the Processed request
847 if (/Processed Request/) {
848 $processed_request_reached = 1;
851 next unless $processed_request_reached;
853 # End loop after the Processed request
856 # Ditch tags and leading/trailing white space.
860 $filtered_request .= "\n" . $_;
862 if (/^$header_to_get/) {
872 sub get_server_header ($$) {
874 my $buffer_ref = shift;
875 my $test_ref = shift;
877 my %test = %{$test_ref};
878 my @buffer = @{$buffer_ref};
880 my $expect_header = $test{'expect-header'};
884 if ($expect_header eq 'REMOVAL'
885 or $expect_header eq 'NO CHANGE'
886 or $expect_header eq 'SOME CHANGE') {
888 $expect_header = $test{'data'};
892 $header_to_get = get_header_name($expect_header);
896 # XXX: should probably verify that the request
897 # was actually answered by Fellatio.
898 if (/^$header_to_get/) {
900 $header =~ s@\s*$@@g;
908 sub get_status_code ($) {
910 my $buffer_ref = shift;
911 my @buffer = @{$buffer_ref};
915 if (/^HTTP\/\d\.\d (\d{3})/) {
921 return '123' if cli_option_is_set('fuzzer-feeding');
923 l(LL_ERROR, 'Unexpected buffer line: "' . $_ . '"');
928 sub get_test_keys () {
929 return ('tag', 'data', 'expect-header', 'ignore');
933 sub test_content_as_string ($) {
935 my $test_ref = shift;
936 my %test = %{$test_ref};
940 foreach my $key (get_test_keys()) {
941 $test{$key} = 'Not set' unless (defined $test{$key});
944 $s .= 'Tag: ' . $test{'tag'};
946 $s .= 'Set header: ' . $test{'data'}; # XXX: adjust for other test types
948 $s .= 'Expected header: ' . $test{'expect-header'};
950 $s .= 'Ignore: ' . $test{'ignore'};
955 ############################################################################
957 # HTTP fetch functions
959 ############################################################################
961 sub check_for_curl () {
963 l(LL_ERROR, "No curl found.") unless (`which $curl`);
966 sub get_cgi_page_or_else ($) {
969 my $content_ref = get_page_with_curl($cgi_url);
970 my $status_code = get_status_code($content_ref);
972 if (200 != $status_code) {
974 my $log_message = "Failed to fetch Privoxy CGI Page. " .
975 "Received status code ". $status_code .
976 " while only 200 is acceptable.";
978 if (cli_option_is_set('fuzzer-feeding')) {
980 $log_message .= " Ignored due to fuzzer feeding.";
981 l(LL_SOFT_ERROR, $log_message)
985 l(LL_ERROR, $log_message);
993 sub get_show_request_with_curl ($) {
995 our $privoxy_cgi_url;
996 my $test_ref = shift;
997 my %test = %{$test_ref};
999 my $curl_parameters = ' ';
1001 # Enable the action to test
1002 $curl_parameters .= '-H \'X-Privoxy-Control: ' . $test{'tag'} . '\' ';
1003 # The header to filter
1004 $curl_parameters .= '-H \'' . $test{'data'} . '\' ';
1006 $curl_parameters .= ' ';
1007 $curl_parameters .= $privoxy_cgi_url;
1008 $curl_parameters .= 'show-request';
1010 return get_cgi_page_or_else($curl_parameters);
1014 sub get_head_with_curl ($) {
1016 our $fellatio_url = FELLATIO_URL;
1017 my $test_ref = shift;
1018 my %test = %{$test_ref};
1020 my $curl_parameters = ' ';
1022 # Enable the action to test
1023 $curl_parameters .= '-H \'X-Privoxy-Control: ' . $test{'tag'} . '\' ';
1024 # The header to filter
1025 $curl_parameters .= '-H \'X-Gimme-Head-With: ' . $test{'data'} . '\' ';
1026 $curl_parameters .= '--head ';
1028 $curl_parameters .= ' ';
1029 $curl_parameters .= $fellatio_url;
1031 return get_page_with_curl($curl_parameters);
1035 sub get_page_with_curl ($) {
1037 my $parameters = shift;
1039 my $curl_line = CURL;
1040 my $retries_left = get_cli_option('retries') + 1;
1043 if (cli_option_is_set('privoxy-address')) {
1044 $curl_line .= ' --proxy ' . get_cli_option('privoxy-address');
1047 # We want to see the HTTP status code
1048 $curl_line .= " --include ";
1049 # Let Privoxy emit two log messages less.
1050 $curl_line .= ' -H \'Proxy-Connection:\' ' unless $parameters =~ /Proxy-Connection:/;
1051 $curl_line .= ' -H \'Connection: close\' ' unless $parameters =~ /Connection:/;
1052 # We don't care about fetch statistic.
1053 $curl_line .= " -s ";
1054 # We do care about the failure reason if any.
1055 $curl_line .= " -S ";
1056 # We want to advertise ourselves
1057 $curl_line .= " --user-agent '" . PRT_VERSION . "' ";
1058 # We aren't too patient
1059 $curl_line .= " --max-time '" . get_cli_option('max-time') . "' ";
1061 $curl_line .= $parameters;
1062 # XXX: still necessary?
1063 $curl_line .= ' 2>&1';
1065 l(LL_PAGE_FETCHING, "Executing: " . $curl_line);
1068 @buffer = `$curl_line`;
1071 $failure_reason = array_as_string(\@buffer);
1072 chomp $failure_reason;
1073 l(LL_SOFT_ERROR, "Fetch failure: '" . $failure_reason . $! ."'");
1075 } while ($? && --$retries_left);
1077 unless ($retries_left) {
1079 "Running curl failed " . get_cli_option('retries') .
1080 " times in a row. Last error: '" . $failure_reason . "'.");
1087 ############################################################################
1091 ############################################################################
1093 sub array_as_string ($) {
1094 my $array_ref = shift;
1097 foreach (@{$array_ref}) {
1106 my $test_ref = shift;
1107 log_message('Test is:' . test_content_as_string($test_ref));
1113 my $this_level = shift;
1114 my $message = shift;
1116 return unless ($log_level & $this_level);
1118 if (LL_ERROR & $this_level) {
1119 $message = 'Oh noes. ' . $message . ' Fatal error. Exiting.';
1122 log_message($message);
1124 if (LL_ERROR & $this_level) {
1129 sub log_message ($) {
1131 my $message = shift;
1135 our $leading_log_date;
1136 our $leading_log_time;
1138 my $time_stamp = '';
1139 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime time;
1141 if ($leading_log_date || $leading_log_time) {
1143 if ($leading_log_date) {
1146 $time_stamp = sprintf("%i/%.2i/%.2i", $year, $mon, $mday);
1149 if ($leading_log_time) {
1150 $time_stamp .= ' ' if $leading_log_date;
1151 $time_stamp.= sprintf("%.2i:%.2i:%.2i", $hour, $min, $sec);
1154 $message = $time_stamp . ": " . $message;
1158 printf(STDERR "%s\n", $message);
1162 sub log_result ($$) {
1164 our $verbose_test_description;
1165 our $filtered_request;
1167 my $test_ref = shift;
1171 my %test = %{$test_ref};
1174 $message .= interpret_result($result);
1175 $message .= " for test ";
1176 $message .= $number;
1178 $message .= $test{'number'};
1180 $message .= $test{'section-id'};
1182 $message .= $test{'regression-test-id'};
1185 if ($verbose_test_description) {
1187 if ($test{'type'} == CLIENT_HEADER_TEST) {
1189 $message .= ' Header ';
1190 $message .= quote($test{'data'});
1191 $message .= ' and tag ';
1192 $message .= quote($test{'tag'});
1194 } elsif ($test{'type'} == SERVER_HEADER_TEST) {
1196 $message .= ' Request Header ';
1197 $message .= quote($test{'data'});
1198 $message .= ' and tag ';
1199 $message .= quote($test{'tag'});
1201 } elsif ($test{'type'} == DUMB_FETCH_TEST) {
1203 $message .= ' URL ';
1204 $message .= quote($test{'data'});
1205 $message .= ' and expected status code ';
1206 $message .= quote($test{'expected-status-code'});
1208 } elsif ($test{'type'} == TRUSTED_CGI_REQUEST) {
1210 $message .= ' CGI URL ';
1211 $message .= quote($test{'data'});
1212 $message .= ' and expected status code ';
1213 $message .= quote($test{'expected-status-code'});
1215 } elsif ($test{'type'} == METHOD_TEST) {
1217 $message .= ' HTTP method ';
1218 $message .= quote($test{'data'});
1219 $message .= ' and expected status code ';
1220 $message .= quote($test{'expected-status-code'});
1222 } elsif ($test{'type'} == BLOCK_TEST) {
1224 $message .= ' Supposedly-blocked URL: ';
1225 $message .= quote($test{'data'});
1229 die "Incomplete support for test type " . $test{'type'} . " detected.";
1234 log_message($message) unless ($result && cli_option_is_set('silent'));
1239 return '\'' . $s . '\'';
1242 sub print_version () {
1243 printf PRT_VERSION . "\n" . 'Copyright (C) 2007-2008 Fabian Keil <fk@fabiankeil.de>' . "\n";
1254 Options and their default values if they have any:
1255 [--debug $cli_options{'debug'}]
1259 [--loops $cli_options{'loops'}]
1260 [--max-level $cli_options{'max-level'}]
1261 [--max-time $cli_options{'max-time'}]
1262 [--min-level $cli_options{'min-level'}]
1264 [--retries $cli_options{'retries'}]
1267 see "perldoc $0" for more information
1273 sub init_cli_options () {
1278 $cli_options{'min-level'} = CLI_MIN_LEVEL;
1279 $cli_options{'max-level'} = CLI_MAX_LEVEL;
1280 $cli_options{'debug'} = $log_level;
1281 $cli_options{'loops'} = CLI_LOOPS;
1282 $cli_options{'max-time'} = CLI_MAX_TIME;
1283 $cli_options{'retries'} = CLI_RETRIES;
1286 sub parse_cli_options () {
1294 'debug=s' => \$cli_options{'debug'},
1295 'help' => sub { help },
1296 'silent' => \$cli_options{'silent'},
1297 'min-level=s' => \$cli_options{'min-level'},
1298 'max-level=s' => \$cli_options{'max-level'},
1299 'privoxy-address=s' => \$cli_options{'privoxy-address'},
1300 'level=s' => \$cli_options{'level'},
1301 'loops=s' => \$cli_options{'loops'},
1302 'test-number=s' => \$cli_options{'test-number'},
1303 'fuzzer-feeding' => \$cli_options{'fuzzer-feeding'},
1304 'retries=s' => \$cli_options{'retries'},
1305 'max-time=s' => \$cli_options{'max-time'},
1306 'version' => sub { print_version && exit(0) }
1308 $log_level |= $cli_options{'debug'};
1311 sub cli_option_is_set ($) {
1314 my $cli_option = shift;
1316 return defined $cli_options{$cli_option};
1319 sub get_cli_option ($) {
1322 my $cli_option = shift;
1324 die "Unknown CLI option: $cli_option" unless defined $cli_options{$cli_option};
1326 return $cli_options{$cli_option};
1331 init_our_variables();
1332 parse_cli_options();
1334 load_regressions_tests();
1335 execute_regression_tests();
1342 B<privoxy-regression-test> - A regression test "framework" for Privoxy.
1346 B<privoxy-regression-test> [B<--debug bitmask>] [B<--fuzzer-feeding>] [B<--help>]
1347 [B<--level level>] [B<--loops count>] [B<--max-level max-level>]
1348 [B<--max-time max-time>] [B<--min-level min-level>] B<--privoxy-address proxy-address>
1349 [B<--retries retries>] [B<--silent>] [B<--version>]
1353 Privoxy-Regression-Test is supposed to one day become
1354 a regression test suite for Privoxy. It's not quite there
1355 yet, however, and can currently only test client header
1356 actions and check the returned status code for requests
1359 Client header actions are tested by requesting
1360 B<http://config.privoxy.org/show-request> and checking whether
1361 or not Privoxy modified the original request as expected.
1363 The original request contains both the header the action-to-be-tested
1364 acts upon and an additional tagger-triggering header that enables
1367 =head1 CONFIGURATION FILE SYNTAX
1369 Privoxy-Regression-Test's configuration is embedded in
1370 Privoxy action files and loaded through Privoxy's web interface.
1372 It makes testing a Privoxy version running on a remote system easier
1373 and should prevent you from updating your tests without updating Privoxy's
1374 configuration accordingly.
1376 A client-header-action test section looks like this:
1378 # Set Header = Referer: http://www.example.org.zwiebelsuppe.exit/
1379 # Expect Header = Referer: http://www.example.org/
1380 {+client-header-filter{hide-tor-exit-notation} -hide-referer}
1381 TAG:^client-header-filter\{hide-tor-exit-notation\}$
1383 The example above causes Privoxy-Regression-Test to set
1384 the header B<Referer: http://www.example.org.zwiebelsuppe.exit/>
1385 and to expect it to be modified to
1386 B<Referer: http://www.example.org/>.
1388 When testing this section, Privoxy-Regression-Test will set the header
1389 B<X-Privoxy-Control: client-header-filter{hide-tor-exit-notation}>
1390 causing the B<privoxy-control> tagger to create the tag
1391 B<client-header-filter{hide-tor-exit-notation}> which will finally
1392 cause Privoxy to enable the action section.
1394 Note that the actions itself are only used by Privoxy,
1395 Privoxy-Regression-Test ignores them and will be happy
1396 as long as the expectations are satisfied.
1398 A fetch test looks like this:
1400 # Fetch Test = http://p.p/user-manual
1401 # Expect Status Code = 302
1403 It tells Privoxy-Regression-Test to request B<http://p.p/user-manual>
1404 and to expect a response with the HTTP status code B<302>. Obviously that's
1405 not a very thorough test and mainly useful to get some code coverage
1406 for Valgrind or to verify that the templates are installed correctly.
1408 If you want to test CGI pages that require a trusted
1409 referer, you can use:
1411 # Trusted CGI Request = http://p.p/edit-actions
1413 It works like ordinary fetch tests, but sets the referer
1414 header to a trusted value.
1416 If no explicit status code expectation is set, B<200> is used.
1418 Additionally all tests have test levels to let the user
1419 control which ones to execute (see I<OPTIONS> below).
1420 Test levels are either set with the B<Level> directive,
1421 or implicitly through the test type.
1423 Fetch tests default to level 6, tests for trusted
1424 CGI requests to level 3 and client-header-action tests
1429 B<--debug bitmask> Add the bitmask provided as integer
1430 to the debug settings.
1432 B<--fuzzer-feeding> Ignore some errors that would otherwise
1433 cause Privoxy-Regression-Test to abort the test because
1434 they shouldn't happen in normal operation. This option is
1435 intended to be used if Privoxy-Regression-Test is only
1436 used to feed a fuzzer in which case there's a high chance
1437 that Privoxy gets an invalid request and returns an error
1440 B<--help> Shows available command line options.
1442 B<--level level> Only execute tests with the specified B<level>.
1444 B<--loop count> Loop through the regression tests B<count> times.
1445 Useful to feed a fuzzer, or when doing stress tests with
1446 several Privoxy-Regression-Test instances running at the same
1449 B<--max-level max-level> Only execute tests with a B<level>
1450 below or equal to the numerical B<max-level>.
1452 B<--max-time max-time> Give Privoxy B<max-time> seconds
1453 to return data. Increasing the default may make sense when
1454 Privoxy is run through Valgrind, decreasing the default may
1455 make sense when Privoxy-Regression-Test is used to feed
1458 B<--min-level min-level> Only execute tests with a B<level>
1459 above or equal to the numerical B<min-level>.
1461 B<--privoxy-address proxy-address> Privoxy's listening address.
1462 If it's not set, the value of the environment variable http_proxy
1463 will be used. B<proxy-address> has to be specified in http_proxy
1466 B<--retries retries> Retry B<retries> times.
1468 B<--silent> Don't log succesful test runs.
1470 B<--version> Print version and exit.
1472 The second dash is optional, options can be shortened,
1473 as long as there are no ambiguities.
1475 =head1 PRIVOXY CONFIGURATION
1477 Privoxy-Regression-Test is shipped with B<regression-tests.action>
1478 which aims to test all official client-header modifying actions
1479 and can be used to verify that the templates and the user manual
1480 files are installed correctly.
1482 To use it, it has to be copied in Privoxy's configuration
1483 directory, and afterwards referenced in Privoxy's configuration
1486 actionsfile regression-tests.action
1488 In general, its tests are supposed to work without changing
1489 any other action files, unless you already added lots of
1490 taggers yourself. If you are using taggers that cause problems,
1491 you might have to temporary disable them for Privoxy's CGI pages.
1493 Some of the regression tests rely on Privoxy features that
1494 may be disabled in your configuration. Tests with a level below
1495 7 are supposed to work with all Privoxy configurations (provided
1496 you didn't build with FEATURE_GRACEFUL_TERMINATION).
1498 Tests with level 9 require Privoxy to deliver the User Manual,
1499 tests with level 12 require the CGI editor to be enabled.
1503 Expect the configuration file syntax to change with future releases.
1507 As Privoxy's B<show-request> page only shows client headers,
1508 Privoxy-Regression-Test can't use it to test Privoxy actions
1509 that modify server headers.
1511 As Privoxy-Regression-Test relies on Privoxy's tag feature to
1512 control the actions to test, it currently only works with
1513 Privoxy 3.0.7 or later.
1515 At the moment Privoxy-Regression-Test fetches Privoxy's
1516 configuration page through I<curl>(1), therefore you have to
1517 have I<curl> installed, otherwise you won't be able to run
1518 Privoxy-Regression-Test in a meaningful way.
1526 Fabian Keil <fk@fabiankeil.de>