diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/misc/base64.pl | 192 | ||||
-rwxr-xr-x | tests/misc/tty-eof.pl | 1 |
2 files changed, 118 insertions, 75 deletions
diff --git a/tests/misc/base64.pl b/tests/misc/base64.pl index fd75c624f..872535afb 100755 --- a/tests/misc/base64.pl +++ b/tests/misc/base64.pl @@ -1,5 +1,5 @@ #!/usr/bin/perl -# Exercise base64. +# Exercise base{32,64}. # Copyright (C) 2006-2015 Free Software Foundation, Inc. @@ -24,22 +24,27 @@ use strict; @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; # Return the encoding of a string of N 'a's. -sub enc($) +sub enc64($) { my ($n) = @_; my %remainder = ( 0 => '', 1 => 'YQ==', 2 => 'YWE=' ); return 'YWFh' x ($n / 3) . $remainder{$n % 3}; } -# Construct an encoded string of length 4KB, using 3K "a"s. -my $a3k = enc 3072; +sub enc32($) +{ + my ($n) = @_; + my %remainder = ( 0 => '', 1 => 'ME======', 2 => 'MFQQ====', + 3 => 'MFQWC===', 4 => 'MFQWCYI='); + return 'MFQWCYLB' x ($n / 5) . $remainder{$n % 5}; +} + +# Function reference to appropriate encoder +my $enc; + +# An encoded string of length 4KB, using 3K "a"s. +my $a3k; my @a3k_nl; -# A few copies, each with different number of newlines at the start. -for my $k (0..3) - { - (my $t = $a3k) =~ s/^/"\n"x $k/e; - push @a3k_nl, $t; - } # Return a copy of S, with newlines inserted every WIDTH bytes. # Ensure that the result (if not the empty string) is newline-terminated. @@ -52,103 +57,140 @@ sub wrap($$) return $s; } -my @Tests = +my @Tests; + +sub gen_tests($) +{ + my ($prog) = @_; + @Tests= ( ['empty', {IN=>''}, {OUT=>""}], - ['inout', {IN=>'a'}, {OUT=>"YQ==\n"}], - ['wrap', '--wrap 0', {IN=>'foo'}, {OUT=>'Zm9v'}], - ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>wrap enc(39),5}], - ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>wrap enc(40),5}], - ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>wrap enc(41),5}], - ['wrap5-42', '--wrap=5', {IN=>'a' x 42}, {OUT=>wrap enc(42),5}], - ['wrap5-43', '--wrap=5', {IN=>'a' x 43}, {OUT=>wrap enc(43),5}], - ['wrap5-44', '--wrap=5', {IN=>'a' x 44}, {OUT=>wrap enc(44),5}], - ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>wrap enc(45),5}], - ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>wrap enc(46),5}], - - ['buf-1', '--decode', {IN=>enc 1}, {OUT=>'a' x 1}], - ['buf-2', '--decode', {IN=>enc 2}, {OUT=>'a' x 2}], - ['buf-3', '--decode', {IN=>enc 3}, {OUT=>'a' x 3}], - ['buf-4', '--decode', {IN=>enc 4}, {OUT=>'a' x 4}], + ['inout1', {IN=>'a'x1}, {OUT=>&$enc(1)."\n"}], + ['inout2', {IN=>'a'x2}, {OUT=>&$enc(2)."\n"}], + ['inout3', {IN=>'a'x3}, {OUT=>&$enc(3)."\n"}], + ['inout4', {IN=>'a'x4}, {OUT=>&$enc(4)."\n"}], + ['inout5', {IN=>'a'x5}, {OUT=>&$enc(5)."\n"}], + ['wrap', '--wrap 0', {IN=>'a'}, {OUT=>&$enc(1)}], + ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>wrap &$enc(39),5}], + ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>wrap &$enc(40),5}], + ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>wrap &$enc(41),5}], + ['wrap5-42', '--wrap=5', {IN=>'a' x 42}, {OUT=>wrap &$enc(42),5}], + ['wrap5-43', '--wrap=5', {IN=>'a' x 43}, {OUT=>wrap &$enc(43),5}], + ['wrap5-44', '--wrap=5', {IN=>'a' x 44}, {OUT=>wrap &$enc(44),5}], + ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>wrap &$enc(45),5}], + ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>wrap &$enc(46),5}], + + ['buf-1', '--decode', {IN=>&$enc(1)}, {OUT=>'a' x 1}], + ['buf-2', '--decode', {IN=>&$enc(2)}, {OUT=>'a' x 2}], + ['buf-3', '--decode', {IN=>&$enc(3)}, {OUT=>'a' x 3}], + ['buf-4', '--decode', {IN=>&$enc(4)}, {OUT=>'a' x 4}], # 4KB worth of input. - ['buf-4k0', '--decode', {IN=>enc 3072+0}, {OUT=>'a' x (3072+0)}], - ['buf-4k1', '--decode', {IN=>enc 3072+1}, {OUT=>'a' x (3072+1)}], - ['buf-4k2', '--decode', {IN=>enc 3072+2}, {OUT=>'a' x (3072+2)}], - ['buf-4k3', '--decode', {IN=>enc 3072+3}, {OUT=>'a' x (3072+3)}], - ['buf-4km1','--decode', {IN=>enc 3072-1}, {OUT=>'a' x (3072-1)}], - ['buf-4km2','--decode', {IN=>enc 3072-2}, {OUT=>'a' x (3072-2)}], - ['buf-4km3','--decode', {IN=>enc 3072-3}, {OUT=>'a' x (3072-3)}], - ['buf-4km4','--decode', {IN=>enc 3072-4}, {OUT=>'a' x (3072-4)}], + ['buf-4k0', '--decode', {IN=>&$enc(3072+0)}, {OUT=>'a' x (3072+0)}], + ['buf-4k1', '--decode', {IN=>&$enc(3072+1)}, {OUT=>'a' x (3072+1)}], + ['buf-4k2', '--decode', {IN=>&$enc(3072+2)}, {OUT=>'a' x (3072+2)}], + ['buf-4k3', '--decode', {IN=>&$enc(3072+3)}, {OUT=>'a' x (3072+3)}], + ['buf-4km1','--decode', {IN=>&$enc(3072-1)}, {OUT=>'a' x (3072-1)}], + ['buf-4km2','--decode', {IN=>&$enc(3072-2)}, {OUT=>'a' x (3072-2)}], + ['buf-4km3','--decode', {IN=>&$enc(3072-3)}, {OUT=>'a' x (3072-3)}], + ['buf-4km4','--decode', {IN=>&$enc(3072-4)}, {OUT=>'a' x (3072-4)}], # Exercise the case in which the final base-64 byte is # in a buffer all by itself. ['b4k-1', '--decode', {IN=>$a3k_nl[1]}, {OUT=>'a' x (3072+0)}], ['b4k-2', '--decode', {IN=>$a3k_nl[2]}, {OUT=>'a' x (3072+0)}], ['b4k-3', '--decode', {IN=>$a3k_nl[3]}, {OUT=>'a' x (3072+0)}], - - ['baddecode', '--decode', {IN=>'a'}, {OUT=>""}, - {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], - ['baddecode2', '--decode', {IN=>'ab'}, {OUT=>"i"}, - {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], - ['baddecode3', '--decode', {IN=>'Zzz'}, {OUT=>"g<"}, - {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], - ['baddecode4', '--decode', {IN=>'Zz='}, {OUT=>"g"}, - {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], - ['baddecode5', '--decode', {IN=>'Z==='}, {OUT=>""}, - {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}] ); -# For each non-failing test, create a --decode test using the -# expected output as input. Also, add tests inserting newlines. -my @new; -foreach my $t (@Tests) + if ($prog eq "base64") + { + push @Tests, ( + ['baddecode', '--decode', {IN=>'a'}, {OUT=>""}, + {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], + ['baddecode2', '--decode', {IN=>'ab'}, {OUT=>"i"}, + {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], + ['baddecode3', '--decode', {IN=>'Zzz'}, {OUT=>"g<"}, + {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], + ['baddecode4', '--decode', {IN=>'Zz='}, {OUT=>"g"}, + {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}], + ['baddecode5', '--decode', {IN=>'Z==='}, {OUT=>""}, + {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}] + ); + } + + # For each non-failing test, create a --decode test using the + # expected output as input. Also, add tests inserting newlines. + my @new; + foreach my $t (@Tests) { - my $exit_val; - my $in; - my @out; + my $exit_val; + my $in; + my @out; - # If the test has a single option of "--decode", then skip it. - !ref $t->[1] && $t->[1] eq '--decode' + # If the test has a single option of "--decode", then skip it. + !ref $t->[1] && $t->[1] eq '--decode' and next; - foreach my $e (@$t) + foreach my $e (@$t) { - ref $e && ref $e eq 'HASH' + ref $e && ref $e eq 'HASH' or next; - defined $e->{EXIT} + defined $e->{EXIT} and $exit_val = $e->{EXIT}; - defined $e->{IN} + defined $e->{IN} and $in = $e->{IN}; - if (defined $e->{OUT}) + if (defined $e->{OUT}) { - my $t = $e->{OUT}; - push @out, $t; - my $len = length $t; - foreach my $i (0..$len) + my $t = $e->{OUT}; + push @out, $t; + my $len = length $t; + foreach my $i (0..$len) { - my $u = $t; - substr ($u, $i, 0) = "\n"; - push @out, $u; - 10 <= $i + my $u = $t; + substr ($u, $i, 0) = "\n"; + push @out, $u; + 10 <= $i and last; } } } - $exit_val + $exit_val and next; - my $i = 0; - foreach my $o (@out) + my $i = 0; + foreach my $o (@out) { - push @new, ["d$i-$t->[0]", '--decode', {IN => $o}, {OUT => $in}]; - ++$i; + push @new, ["d$i-$t->[0]", '--decode', {IN => $o}, {OUT => $in}]; + ++$i; } } -push @Tests, @new; + push @Tests, @new; +} my $save_temps = $ENV{DEBUG}; my $verbose = $ENV{VERBOSE}; -my $prog = 'base64'; -my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); +my $fail = 0; +foreach my $prog (qw(base32 base64)) + { + $enc = $prog eq "base32" ? \&enc32 : \&enc64; + + # Construct an encoded string of length 4KB, using 3K "a"s. + $a3k = &$enc(3072); + @a3k_nl = (); + # A few copies, each with different number of newlines at the start. + for my $k (0..3) + { + (my $t = $a3k) =~ s/^/"\n"x $k/e; + push @a3k_nl, $t; + } + + gen_tests($prog); + + $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); + if ($fail != 0) + { + last; + } + } + exit $fail; diff --git a/tests/misc/tty-eof.pl b/tests/misc/tty-eof.pl index c49efc24f..1bd912460 100755 --- a/tests/misc/tty-eof.pl +++ b/tests/misc/tty-eof.pl @@ -31,6 +31,7 @@ $@ { my $fail = 0; my @stdin_reading_commands = qw( + base32 base64 cat cksum |