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.9 2008/03/21 13:00:37 fabiankeil 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', 'blocked url');
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.
217 if (/^\#\s*([^=:]*?)\s*[=]\s*(.+?)\s*$/) {
222 $token =~ s@\s\s+@ @g;
223 $token =~ tr/[A-Z]/[a-z]/;
225 } elsif (/^TAG\s*:(.*)$/) {
232 return ($token, $value);
235 sub enlist_new_test ($$$$$$) {
237 my ($regression_tests, $token, $value, $si, $ri, $number) = @_;
239 if ($token eq 'set header') {
241 l(LL_FILE_LOADING, "Header to set: " . $value);
242 ${$regression_tests}[$si][$ri]{'type'} = CLIENT_HEADER_TEST;
244 $$regression_tests[$si][$ri]{'level'} = CLIENT_HEADER_TEST;
246 } elsif ($token eq 'request header') {
248 l(LL_FILE_LOADING, "Header to request: " . $value);
249 $$regression_tests[$si][$ri]{'type'} = SERVER_HEADER_TEST;
251 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
252 $$regression_tests[$si][$ri]{'level'} = SERVER_HEADER_TEST;
254 } elsif ($token eq 'trusted cgi request') {
256 l(LL_FILE_LOADING, "CGI URL to test in a dumb way: " . $value);
257 $$regression_tests[$si][$ri]{'type'} = TRUSTED_CGI_REQUEST;
259 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
260 $$regression_tests[$si][$ri]{'level'} = TRUSTED_CGI_REQUEST;
262 } elsif ($token eq 'fetch test') {
264 l(LL_FILE_LOADING, "URL to test in a dumb way: " . $value);
265 $$regression_tests[$si][$ri]{'type'} = DUMB_FETCH_TEST;
267 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
268 $$regression_tests[$si][$ri]{'level'} = DUMB_FETCH_TEST;
270 } elsif ($token eq 'method test') {
272 l(LL_FILE_LOADING, "Method to test: " . $value);
273 $$regression_tests[$si][$ri]{'type'} = METHOD_TEST;
275 $$regression_tests[$si][$ri]{'expected-status-code'} = 200;
276 $$regression_tests[$si][$ri]{'level'} = METHOD_TEST;
278 } elsif ($token eq 'blocked url') {
280 l(LL_FILE_LOADING, "URL to block-test: " . $value);
281 $$regression_tests[$si][$ri]{'type'} = BLOCK_TEST;
283 $$regression_tests[$si][$ri]{'expected-status-code'} = 403;
284 $$regression_tests[$si][$ri]{'level'} = BLOCK_TEST;
288 die "Incomplete '" . $token . "' support detected.";
292 check_for_forbidden_characters($value);
294 $$regression_tests[$si][$ri]{'data'} = $value;
296 # For function that only get passed single tests
297 $$regression_tests[$si][$ri]{'section-id'} = $si;
298 $$regression_tests[$si][$ri]{'regression-test-id'} = $ri;
299 $$regression_tests[$si][$ri]{'number'} = $number - 1;
301 "Regression test " . $number . " (section:" . $si . "):");
304 sub load_action_files ($) {
308 our @regression_tests;
310 my $actionfiles_ref = shift;
311 my @actionfiles = @{$actionfiles_ref};
313 my $si = 0; # Section index
314 my $ri = -1; # Regression test index
319 l(LL_STATUS, "Loading regression tests from action file(s) delivered by Privoxy.");
321 for my $file_number (0 .. @actionfiles - 1) {
323 my $curl_url = ' "' . $actionfiles[$file_number] . '"';
324 my $actionfile = undef;
326 foreach (@{get_cgi_page_or_else($curl_url)}) {
331 if (/<h2>Contents of Actions File (.*?)</) {
335 next unless defined $actionfile;
339 my ($token, $value) = tokenize($_);
341 next unless defined $token;
343 # Load regression tests
345 if (token_starts_new_test($token)) {
347 # Beginning of new regression test.
350 enlist_new_test(\@regression_tests, $token, $value, $si, $ri, $count);
353 if ($token =~ /level\s+(\d+)/i) {
356 register_dependency($level, $value);
359 if ($si == -1 || $ri == -1) {
360 # No beginning of a test detected yet,
361 # so we don't care about any other test
366 if ($token eq 'expect header') {
368 l(LL_FILE_LOADING, "Detected expectation: " . $value);
369 $regression_tests[$si][$ri]{'expect-header'} = $value;
371 } elsif ($token eq 'tag') {
375 my $tag = parse_tag($value);
377 # We already checked in parse_tag() after filtering
380 l(LL_FILE_LOADING, "Detected TAG: " . $tag);
382 # Save tag for all tests in this section
384 $regression_tests[$si][$ri]{'tag'} = $tag;
390 } elsif ($token eq 'ignore' && $value =~ /Yes/i) {
392 l(LL_FILE_LOADING, "Ignoring section: " . test_content_as_string($regression_tests[$si][$ri]));
393 $regression_tests[$si][$ri]{'ignore'} = 1;
396 } elsif ($token eq 'expect status code') {
398 l(LL_FILE_LOADING, "Expecting status code: " . $value);
399 $regression_tests[$si][$ri]{'expected-status-code'} = $value;
401 } elsif ($token eq 'level') { # XXX: stupid name
403 $value =~ s@(\d+).*@$1@;
404 l(LL_FILE_LOADING, "Level: " . $value);
405 $regression_tests[$si][$ri]{'level'} = $value;
407 } elsif ($token eq 'method') {
409 l(LL_FILE_LOADING, "Method: " . $value);
410 $regression_tests[$si][$ri]{'method'} = $value;
414 # We don't use it, so we don't need
418 check_for_forbidden_characters($value) unless $no_checks;
419 check_for_forbidden_characters($token);
423 l(LL_FILE_LOADING, "Done loading " . $count . " regression tests."
424 . " Of which " . $ignored. " will be ignored)\n");
427 ############################################################################
429 # Regression test executing functions
431 ############################################################################
433 sub execute_regression_tests () {
435 our @regression_tests;
436 my $loops = get_cli_option('loops');
438 my $all_failures = 0;
439 my $all_successes = 0;
441 unless (@regression_tests) {
443 l(LL_STATUS, "No regression tests found.");
447 l(LL_STATUS, "Executing regression tests ...");
449 while ($loops-- > 0) {
456 for my $s (0 .. @regression_tests - 1) {
460 while (defined $regression_tests[$s][$r]) {
462 die "Section id mismatch" if ($s != $regression_tests[$s][$r]{'section-id'});
463 die "Regression test id mismatch" if ($r != $regression_tests[$s][$r]{'regression-test-id'});
465 my $number = $regression_tests[$s][$r]{'number'};
467 if ($regression_tests[$s][$r]{'ignore'}
468 or level_is_unacceptable($regression_tests[$s][$r]{'level'})
469 or test_number_is_unacceptable($number)) {
475 my $result = execute_regression_test($regression_tests[$s][$r]);
477 log_result($regression_tests[$s][$r], $result, $tests);
479 $successes += $result;
485 $failures = $tests - $successes;
487 log_message("Executed " . $tests . " regression tests. " .
488 'Skipped ' . $skipped . '. ' .
489 $successes . " successes, " . $failures . " failures.");
491 $all_tests += $tests;
492 $all_failures += $failures;
493 $all_successes += $successes;
497 if (get_cli_option('loops') > 1) {
498 log_message("Total: Executed " . $all_tests . " regression tests. " .
499 $all_successes . " successes, " . $all_failures . " failures.");
503 sub level_is_unacceptable ($) {
505 return ((cli_option_is_set('level') and get_cli_option('level') != $level)
506 or ($level < get_cli_option('min-level'))
507 or ($level > get_cli_option('max-level'))
508 or dependency_unsatisfied($level)
512 sub test_number_is_unacceptable ($) {
513 my $test_number = shift;
514 return (cli_option_is_set('test-number')
515 and get_cli_option('test-number') != $test_number)
518 sub dependency_unsatisfied ($) {
523 my $dependency_problem = 0;
525 if (defined ($dependencies{$level}{'config line'})) {
527 my $dependency = $dependencies{$level}{'config line'};
528 $dependency_problem = 1;
530 foreach (@privoxy_config) {
532 $dependency_problem = 0 if (/$dependency/);
536 return $dependency_problem;
539 sub register_dependency ($$) {
542 my $dependency = shift;
545 if ($dependency =~ /config line\s+(.*)/) {
547 $dependencies{$level}{'config line'} = $1;
551 # XXX: somewhat misleading name
552 sub execute_regression_test ($) {
554 my $test_ref = shift;
555 my %test = %{$test_ref};
558 if ($test{'type'} == CLIENT_HEADER_TEST) {
560 $result = execute_client_header_regression_test($test_ref);
562 } elsif ($test{'type'} == SERVER_HEADER_TEST) {
564 $result = execute_server_header_regression_test($test_ref);
566 } elsif ($test{'type'} == DUMB_FETCH_TEST
567 or $test{'type'} == TRUSTED_CGI_REQUEST) {
569 $result = execute_dumb_fetch_test($test_ref);
571 } elsif ($test{'type'} == METHOD_TEST) {
573 $result = execute_method_test($test_ref);
575 } elsif ($test{'type'} == BLOCK_TEST) {
577 $result = execute_block_test($test_ref);
581 die "Unsupported test type detected: " . $test{'type'};
588 sub execute_method_test ($) {
590 my $test_ref = shift;
591 my %test = %{$test_ref};
595 my $method = $test{'data'};
597 my $curl_parameters = '';
598 my $expected_status_code = $test{'expected-status-code'};
600 $curl_parameters .= '--request ' . $method . ' ';
601 # Don't complain in case about the 'missing' body
602 $curl_parameters .= '--head ' if ($method =~ /^HEAD$/i);
604 $curl_parameters .= PRIVOXY_CGI_URL;
606 $buffer_ref = get_page_with_curl($curl_parameters);
607 $status_code = get_status_code($buffer_ref);
609 $result = check_status_code_result($status_code, $expected_status_code);
614 sub execute_dumb_fetch_test ($) {
616 my $test_ref = shift;
617 my %test = %{$test_ref};
622 my $curl_parameters = '';
623 my $expected_status_code = $test{'expected-status-code'};
625 if (defined $test{method}) {
626 $curl_parameters .= '--request ' . $test{method} . ' ';
628 if ($test{type} == TRUSTED_CGI_REQUEST) {
629 $curl_parameters .= '--referer ' . PRIVOXY_CGI_URL . ' ';
632 $curl_parameters .= $test{'data'};
634 $buffer_ref = get_page_with_curl($curl_parameters);
635 $status_code = get_status_code($buffer_ref);
637 $result = check_status_code_result($status_code, $expected_status_code);
642 sub execute_block_test ($) {
645 my $url = $test->{'data'};
646 my $final_results = get_final_results($url);
648 return defined $final_results->{'+block'};
651 sub get_final_results ($) {
654 my $curl_parameters = '';
655 my %final_results = ();
656 my $final_results_reached = 0;
658 die "Unacceptable characterss in $url" if $url =~ m@[\\'"]@;
661 $curl_parameters .= "'" . PRIVOXY_CGI_URL . 'show-url-info?url=' . $url . "'";
663 foreach (@{get_cgi_page_or_else($curl_parameters)}) {
665 $final_results_reached = 1 if (m@<h2>Final results:</h2>@);
667 next unless ($final_results_reached);
670 if (m@<br>([-+])<a.*>([^>]*)</a>( \{.*\})@) {
673 $final_results{$action} = $value;
677 return \%final_results;
680 sub check_status_code_result ($$) {
682 my $status_code = shift;
683 my $expected_status_code = shift;
686 if ($expected_status_code == $status_code) {
689 l(LL_VERBOSE_SUCCESS,
690 "Yay. We expected status code " . $expected_status_code . ", and received: " . $status_code . '.');
692 } elsif (cli_option_is_set('fuzzer-feeding') and $status_code == 123) {
694 l(LL_VERBOSE_FAILURE,
695 "Oh well. Status code lost while fuzzing. Can't check if it was " . $expected_status_code . '.');
699 l(LL_VERBOSE_FAILURE,
700 "Ooops. We expected status code " . $expected_status_code . ", but received: " . $status_code . '.');
707 sub execute_client_header_regression_test ($) {
709 my $test_ref = shift;
714 $buffer_ref = get_show_request_with_curl($test_ref);
716 $header = get_header($buffer_ref, $test_ref);
717 $result = check_header_result($test_ref, $header);
722 sub execute_server_header_regression_test ($) {
724 my $test_ref = shift;
729 $buffer_ref = get_head_with_curl($test_ref);
731 $header = get_server_header($buffer_ref, $test_ref);
732 $result = check_header_result($test_ref, $header);
738 sub interpret_result ($) {
740 return $success ? "Success" : "Failure";
743 sub check_header_result ($$) {
745 my $test_ref = shift;
748 my %test = %{$test_ref};
749 my $expect_header = $test{'expect-header'};
752 $header =~ s@ @ @g if defined($header);
754 if ($expect_header eq 'NO CHANGE') {
756 if (defined($header) and $header eq $test{'data'}) {
762 $header //= "REMOVAL";
763 l(LL_VERBOSE_FAILURE,
764 "Ooops. Got: " . $header . " while expecting: " . $expect_header);
767 } elsif ($expect_header eq 'REMOVAL') {
769 if (defined($header) and $header eq $test{'data'}) {
771 l(LL_VERBOSE_FAILURE,
772 "Ooops. Expected removal but: " . $header . " is still there.");
776 # XXX: Use more reliable check here and make sure
777 # the header has a different name.
782 } elsif ($expect_header eq 'SOME CHANGE') {
784 if (defined($header) and not $header eq $test{'data'}) {
790 $header //= "REMOVAL";
791 l(LL_VERBOSE_FAILURE,
792 "Ooops. Got: " . $header . " while expecting: SOME CHANGE");
798 if (defined($header) and $header eq $expect_header) {
804 $header //= "'No matching header'"; # XXX: No header detected to be precise
805 l(LL_VERBOSE_FAILURE,
806 "Ooops. Got: " . $header . " while expecting: " . $expect_header);
812 sub get_header_name ($) {
816 $header =~ s@(.*?: ).*@$1@;
821 sub get_header ($$) {
823 our $filtered_request = '';
825 my $buffer_ref = shift;
826 my $test_ref = shift;
828 my %test = %{$test_ref};
829 my @buffer = @{$buffer_ref};
831 my $expect_header = $test{'expect-header'};
834 my $processed_request_reached = 0;
836 my $processed_request = '';
840 if ($expect_header eq 'REMOVAL'
841 or $expect_header eq 'NO CHANGE'
842 or $expect_header eq 'SOME CHANGE') {
844 $expect_header = $test{'data'};
848 $header_to_get = get_header_name($expect_header);
852 # Skip everything before the Processed request
853 if (/Processed Request/) {
854 $processed_request_reached = 1;
857 next unless $processed_request_reached;
859 # End loop after the Processed request
862 # Ditch tags and leading/trailing white space.
866 $filtered_request .= "\n" . $_;
868 if (/^$header_to_get/) {
878 sub get_server_header ($$) {
880 my $buffer_ref = shift;
881 my $test_ref = shift;
883 my %test = %{$test_ref};
884 my @buffer = @{$buffer_ref};
886 my $expect_header = $test{'expect-header'};
890 if ($expect_header eq 'REMOVAL'
891 or $expect_header eq 'NO CHANGE'
892 or $expect_header eq 'SOME CHANGE') {
894 $expect_header = $test{'data'};
898 $header_to_get = get_header_name($expect_header);
902 # XXX: should probably verify that the request
903 # was actually answered by Fellatio.
904 if (/^$header_to_get/) {
906 $header =~ s@\s*$@@g;
914 sub get_status_code ($) {
916 my $buffer_ref = shift;
917 my @buffer = @{$buffer_ref};
921 if (/^HTTP\/\d\.\d (\d{3})/) {
927 return '123' if cli_option_is_set('fuzzer-feeding');
929 l(LL_ERROR, 'Unexpected buffer line: "' . $_ . '"');
934 sub get_test_keys () {
935 return ('tag', 'data', 'expect-header', 'ignore');
939 sub test_content_as_string ($) {
941 my $test_ref = shift;
942 my %test = %{$test_ref};
946 foreach my $key (get_test_keys()) {
947 $test{$key} = 'Not set' unless (defined $test{$key});
950 $s .= 'Tag: ' . $test{'tag'};
952 $s .= 'Set header: ' . $test{'data'}; # XXX: adjust for other test types
954 $s .= 'Expected header: ' . $test{'expect-header'};
956 $s .= 'Ignore: ' . $test{'ignore'};
961 ############################################################################
963 # HTTP fetch functions
965 ############################################################################
967 sub check_for_curl () {
969 l(LL_ERROR, "No curl found.") unless (`which $curl`);
972 sub get_cgi_page_or_else ($) {
975 my $content_ref = get_page_with_curl($cgi_url);
976 my $status_code = get_status_code($content_ref);
978 if (200 != $status_code) {
980 my $log_message = "Failed to fetch Privoxy CGI Page. " .
981 "Received status code ". $status_code .
982 " while only 200 is acceptable.";
984 if (cli_option_is_set('fuzzer-feeding')) {
986 $log_message .= " Ignored due to fuzzer feeding.";
987 l(LL_SOFT_ERROR, $log_message)
991 l(LL_ERROR, $log_message);
999 sub get_show_request_with_curl ($) {
1001 our $privoxy_cgi_url;
1002 my $test_ref = shift;
1003 my %test = %{$test_ref};
1005 my $curl_parameters = ' ';
1007 # Enable the action to test
1008 $curl_parameters .= '-H \'X-Privoxy-Control: ' . $test{'tag'} . '\' ';
1009 # The header to filter
1010 $curl_parameters .= '-H \'' . $test{'data'} . '\' ';
1012 $curl_parameters .= ' ';
1013 $curl_parameters .= $privoxy_cgi_url;
1014 $curl_parameters .= 'show-request';
1016 return get_cgi_page_or_else($curl_parameters);
1020 sub get_head_with_curl ($) {
1022 our $fellatio_url = FELLATIO_URL;
1023 my $test_ref = shift;
1024 my %test = %{$test_ref};
1026 my $curl_parameters = ' ';
1028 # Enable the action to test
1029 $curl_parameters .= '-H \'X-Privoxy-Control: ' . $test{'tag'} . '\' ';
1030 # The header to filter
1031 $curl_parameters .= '-H \'X-Gimme-Head-With: ' . $test{'data'} . '\' ';
1032 $curl_parameters .= '--head ';
1034 $curl_parameters .= ' ';
1035 $curl_parameters .= $fellatio_url;
1037 return get_page_with_curl($curl_parameters);
1041 sub get_page_with_curl ($) {
1043 my $parameters = shift;
1045 my $curl_line = CURL;
1046 my $retries_left = get_cli_option('retries') + 1;
1049 if (cli_option_is_set('privoxy-address')) {
1050 $curl_line .= ' --proxy ' . get_cli_option('privoxy-address');
1053 # We want to see the HTTP status code
1054 $curl_line .= " --include ";
1055 # Let Privoxy emit two log messages less.
1056 $curl_line .= ' -H \'Proxy-Connection:\' ' unless $parameters =~ /Proxy-Connection:/;
1057 $curl_line .= ' -H \'Connection: close\' ' unless $parameters =~ /Connection:/;
1058 # We don't care about fetch statistic.
1059 $curl_line .= " -s ";
1060 # We do care about the failure reason if any.
1061 $curl_line .= " -S ";
1062 # We want to advertise ourselves
1063 $curl_line .= " --user-agent '" . PRT_VERSION . "' ";
1064 # We aren't too patient
1065 $curl_line .= " --max-time '" . get_cli_option('max-time') . "' ";
1067 $curl_line .= $parameters;
1068 # XXX: still necessary?
1069 $curl_line .= ' 2>&1';
1071 l(LL_PAGE_FETCHING, "Executing: " . $curl_line);
1074 @buffer = `$curl_line`;
1077 $failure_reason = array_as_string(\@buffer);
1078 chomp $failure_reason;
1079 l(LL_SOFT_ERROR, "Fetch failure: '" . $failure_reason . $! ."'");
1081 } while ($? && --$retries_left);
1083 unless ($retries_left) {
1085 "Running curl failed " . get_cli_option('retries') .
1086 " times in a row. Last error: '" . $failure_reason . "'.");
1093 ############################################################################
1097 ############################################################################
1099 sub array_as_string ($) {
1100 my $array_ref = shift;
1103 foreach (@{$array_ref}) {
1112 my $test_ref = shift;
1113 log_message('Test is:' . test_content_as_string($test_ref));
1119 my $this_level = shift;
1120 my $message = shift;
1122 return unless ($log_level & $this_level);
1124 if (LL_ERROR & $this_level) {
1125 $message = 'Oh noes. ' . $message . ' Fatal error. Exiting.';
1128 log_message($message);
1130 if (LL_ERROR & $this_level) {
1135 sub log_message ($) {
1137 my $message = shift;
1141 our $leading_log_date;
1142 our $leading_log_time;
1144 my $time_stamp = '';
1145 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime time;
1147 if ($leading_log_date || $leading_log_time) {
1149 if ($leading_log_date) {
1152 $time_stamp = sprintf("%i/%.2i/%.2i", $year, $mon, $mday);
1155 if ($leading_log_time) {
1156 $time_stamp .= ' ' if $leading_log_date;
1157 $time_stamp.= sprintf("%.2i:%.2i:%.2i", $hour, $min, $sec);
1160 $message = $time_stamp . ": " . $message;
1164 printf(STDERR "%s\n", $message);
1168 sub log_result ($$) {
1170 our $verbose_test_description;
1171 our $filtered_request;
1173 my $test_ref = shift;
1177 my %test = %{$test_ref};
1180 $message .= interpret_result($result);
1181 $message .= " for test ";
1182 $message .= $number;
1184 $message .= $test{'number'};
1186 $message .= $test{'section-id'};
1188 $message .= $test{'regression-test-id'};
1191 if ($verbose_test_description) {
1193 if ($test{'type'} == CLIENT_HEADER_TEST) {
1195 $message .= ' Header ';
1196 $message .= quote($test{'data'});
1197 $message .= ' and tag ';
1198 $message .= quote($test{'tag'});
1200 } elsif ($test{'type'} == SERVER_HEADER_TEST) {
1202 $message .= ' Request Header ';
1203 $message .= quote($test{'data'});
1204 $message .= ' and tag ';
1205 $message .= quote($test{'tag'});
1207 } elsif ($test{'type'} == DUMB_FETCH_TEST) {
1209 $message .= ' URL ';
1210 $message .= quote($test{'data'});
1211 $message .= ' and expected status code ';
1212 $message .= quote($test{'expected-status-code'});
1214 } elsif ($test{'type'} == TRUSTED_CGI_REQUEST) {
1216 $message .= ' CGI URL ';
1217 $message .= quote($test{'data'});
1218 $message .= ' and expected status code ';
1219 $message .= quote($test{'expected-status-code'});
1221 } elsif ($test{'type'} == METHOD_TEST) {
1223 $message .= ' HTTP method ';
1224 $message .= quote($test{'data'});
1225 $message .= ' and expected status code ';
1226 $message .= quote($test{'expected-status-code'});
1228 } elsif ($test{'type'} == BLOCK_TEST) {
1230 $message .= ' Supposedly-blocked URL: ';
1231 $message .= quote($test{'data'});
1235 die "Incomplete support for test type " . $test{'type'} . " detected.";
1240 log_message($message) unless ($result && cli_option_is_set('silent'));
1245 return '\'' . $s . '\'';
1248 sub print_version () {
1249 printf PRT_VERSION . "\n" . 'Copyright (C) 2007-2008 Fabian Keil <fk@fabiankeil.de>' . "\n";
1260 Options and their default values if they have any:
1261 [--debug $cli_options{'debug'}]
1265 [--loops $cli_options{'loops'}]
1266 [--max-level $cli_options{'max-level'}]
1267 [--max-time $cli_options{'max-time'}]
1268 [--min-level $cli_options{'min-level'}]
1270 [--retries $cli_options{'retries'}]
1273 see "perldoc $0" for more information
1279 sub init_cli_options () {
1284 $cli_options{'min-level'} = CLI_MIN_LEVEL;
1285 $cli_options{'max-level'} = CLI_MAX_LEVEL;
1286 $cli_options{'debug'} = $log_level;
1287 $cli_options{'loops'} = CLI_LOOPS;
1288 $cli_options{'max-time'} = CLI_MAX_TIME;
1289 $cli_options{'retries'} = CLI_RETRIES;
1292 sub parse_cli_options () {
1300 'debug=s' => \$cli_options{'debug'},
1301 'help' => sub { help },
1302 'silent' => \$cli_options{'silent'},
1303 'min-level=s' => \$cli_options{'min-level'},
1304 'max-level=s' => \$cli_options{'max-level'},
1305 'privoxy-address=s' => \$cli_options{'privoxy-address'},
1306 'level=s' => \$cli_options{'level'},
1307 'loops=s' => \$cli_options{'loops'},
1308 'test-number=s' => \$cli_options{'test-number'},
1309 'fuzzer-feeding' => \$cli_options{'fuzzer-feeding'},
1310 'retries=s' => \$cli_options{'retries'},
1311 'max-time=s' => \$cli_options{'max-time'},
1312 'version' => sub { print_version && exit(0) }
1314 $log_level |= $cli_options{'debug'};
1317 sub cli_option_is_set ($) {
1320 my $cli_option = shift;
1322 return defined $cli_options{$cli_option};
1325 sub get_cli_option ($) {
1328 my $cli_option = shift;
1330 die "Unknown CLI option: $cli_option" unless defined $cli_options{$cli_option};
1332 return $cli_options{$cli_option};
1337 init_our_variables();
1338 parse_cli_options();
1340 load_regressions_tests();
1341 execute_regression_tests();
1348 B<privoxy-regression-test> - A regression test "framework" for Privoxy.
1352 B<privoxy-regression-test> [B<--debug bitmask>] [B<--fuzzer-feeding>] [B<--help>]
1353 [B<--level level>] [B<--loops count>] [B<--max-level max-level>]
1354 [B<--max-time max-time>] [B<--min-level min-level>] B<--privoxy-address proxy-address>
1355 [B<--retries retries>] [B<--silent>] [B<--version>]
1359 Privoxy-Regression-Test is supposed to one day become
1360 a regression test suite for Privoxy. It's not quite there
1361 yet, however, and can currently only test client header
1362 actions and check the returned status code for requests
1365 Client header actions are tested by requesting
1366 B<http://config.privoxy.org/show-request> and checking whether
1367 or not Privoxy modified the original request as expected.
1369 The original request contains both the header the action-to-be-tested
1370 acts upon and an additional tagger-triggering header that enables
1373 =head1 CONFIGURATION FILE SYNTAX
1375 Privoxy-Regression-Test's configuration is embedded in
1376 Privoxy action files and loaded through Privoxy's web interface.
1378 It makes testing a Privoxy version running on a remote system easier
1379 and should prevent you from updating your tests without updating Privoxy's
1380 configuration accordingly.
1382 A client-header-action test section looks like this:
1384 # Set Header = Referer: http://www.example.org.zwiebelsuppe.exit/
1385 # Expect Header = Referer: http://www.example.org/
1386 {+client-header-filter{hide-tor-exit-notation} -hide-referer}
1387 TAG:^client-header-filter\{hide-tor-exit-notation\}$
1389 The example above causes Privoxy-Regression-Test to set
1390 the header B<Referer: http://www.example.org.zwiebelsuppe.exit/>
1391 and to expect it to be modified to
1392 B<Referer: http://www.example.org/>.
1394 When testing this section, Privoxy-Regression-Test will set the header
1395 B<X-Privoxy-Control: client-header-filter{hide-tor-exit-notation}>
1396 causing the B<privoxy-control> tagger to create the tag
1397 B<client-header-filter{hide-tor-exit-notation}> which will finally
1398 cause Privoxy to enable the action section.
1400 Note that the actions itself are only used by Privoxy,
1401 Privoxy-Regression-Test ignores them and will be happy
1402 as long as the expectations are satisfied.
1404 A fetch test looks like this:
1406 # Fetch Test = http://p.p/user-manual
1407 # Expect Status Code = 302
1409 It tells Privoxy-Regression-Test to request B<http://p.p/user-manual>
1410 and to expect a response with the HTTP status code B<302>. Obviously that's
1411 not a very thorough test and mainly useful to get some code coverage
1412 for Valgrind or to verify that the templates are installed correctly.
1414 If you want to test CGI pages that require a trusted
1415 referer, you can use:
1417 # Trusted CGI Request = http://p.p/edit-actions
1419 It works like ordinary fetch tests, but sets the referer
1420 header to a trusted value.
1422 If no explicit status code expectation is set, B<200> is used.
1424 To verify that a URL is blocked, use:
1426 # Blocked URL = http://www.example.com/blocked
1428 Additionally all tests have test levels to let the user
1429 control which ones to execute (see I<OPTIONS> below).
1430 Test levels are either set with the B<Level> directive,
1431 or implicitly through the test type.
1433 Block tests default to level 7, fetch tests to level 6,
1434 tests for trusted CGI requests to level 3 and
1435 client-header-action tests to level 1.
1439 B<--debug bitmask> Add the bitmask provided as integer
1440 to the debug settings.
1442 B<--fuzzer-feeding> Ignore some errors that would otherwise
1443 cause Privoxy-Regression-Test to abort the test because
1444 they shouldn't happen in normal operation. This option is
1445 intended to be used if Privoxy-Regression-Test is only
1446 used to feed a fuzzer in which case there's a high chance
1447 that Privoxy gets an invalid request and returns an error
1450 B<--help> Shows available command line options.
1452 B<--level level> Only execute tests with the specified B<level>.
1454 B<--loop count> Loop through the regression tests B<count> times.
1455 Useful to feed a fuzzer, or when doing stress tests with
1456 several Privoxy-Regression-Test instances running at the same
1459 B<--max-level max-level> Only execute tests with a B<level>
1460 below or equal to the numerical B<max-level>.
1462 B<--max-time max-time> Give Privoxy B<max-time> seconds
1463 to return data. Increasing the default may make sense when
1464 Privoxy is run through Valgrind, decreasing the default may
1465 make sense when Privoxy-Regression-Test is used to feed
1468 B<--min-level min-level> Only execute tests with a B<level>
1469 above or equal to the numerical B<min-level>.
1471 B<--privoxy-address proxy-address> Privoxy's listening address.
1472 If it's not set, the value of the environment variable http_proxy
1473 will be used. B<proxy-address> has to be specified in http_proxy
1476 B<--retries retries> Retry B<retries> times.
1478 B<--silent> Don't log succesful test runs.
1480 B<--version> Print version and exit.
1482 The second dash is optional, options can be shortened,
1483 as long as there are no ambiguities.
1485 =head1 PRIVOXY CONFIGURATION
1487 Privoxy-Regression-Test is shipped with B<regression-tests.action>
1488 which aims to test all official client-header modifying actions
1489 and can be used to verify that the templates and the user manual
1490 files are installed correctly.
1492 To use it, it has to be copied in Privoxy's configuration
1493 directory, and afterwards referenced in Privoxy's configuration
1496 actionsfile regression-tests.action
1498 In general, its tests are supposed to work without changing
1499 any other action files, unless you already added lots of
1500 taggers yourself. If you are using taggers that cause problems,
1501 you might have to temporary disable them for Privoxy's CGI pages.
1503 Some of the regression tests rely on Privoxy features that
1504 may be disabled in your configuration. Tests with a level below
1505 7 are supposed to work with all Privoxy configurations (provided
1506 you didn't build with FEATURE_GRACEFUL_TERMINATION).
1508 Tests with level 9 require Privoxy to deliver the User Manual,
1509 tests with level 12 require the CGI editor to be enabled.
1513 Expect the configuration file syntax to change with future releases.
1517 As Privoxy's B<show-request> page only shows client headers,
1518 Privoxy-Regression-Test can't use it to test Privoxy actions
1519 that modify server headers.
1521 As Privoxy-Regression-Test relies on Privoxy's tag feature to
1522 control the actions to test, it currently only works with
1523 Privoxy 3.0.7 or later.
1525 At the moment Privoxy-Regression-Test fetches Privoxy's
1526 configuration page through I<curl>(1), therefore you have to
1527 have I<curl> installed, otherwise you won't be able to run
1528 Privoxy-Regression-Test in a meaningful way.
1536 Fabian Keil <fk@fabiankeil.de>