tests: add test for locale decimal processing
[coreutils.git] / tests / misc / base64.pl
blob314b44b66d27ec69a5c9ab12cbbd1d27780cca71
1 #!/usr/bin/perl
2 # Exercise base{32,64}.
4 # Copyright (C) 2006-2019 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 use strict;
21 (my $program_name = $0) =~ s|.*/||;
23 # Turn off localization of executable's output.
24 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
26 # Return the encoding of a string of N 'a's.
27 sub enc64($)
29 my ($n) = @_;
30 my %remainder = ( 0 => '', 1 => 'YQ==', 2 => 'YWE=' );
31 return 'YWFh' x ($n / 3) . $remainder{$n % 3};
34 sub enc32($)
36 my ($n) = @_;
37 my %remainder = ( 0 => '', 1 => 'ME======', 2 => 'MFQQ====',
38 3 => 'MFQWC===', 4 => 'MFQWCYI=');
39 return 'MFQWCYLB' x ($n / 5) . $remainder{$n % 5};
42 # Function reference to appropriate encoder
43 my $enc;
45 # An encoded string of length 4KB, using 3K "a"s.
46 my $a3k;
47 my @a3k_nl;
49 # Return a copy of S, with newlines inserted every WIDTH bytes.
50 # Ensure that the result (if not the empty string) is newline-terminated.
51 sub wrap($$)
53 my ($s, $width) = @_;
54 $s =~ s/(.{$width})/$1\n/g;
55 substr ($s, -1, 1) ne "\n"
56 and $s .= "\n";
57 return $s;
60 my @Tests;
62 sub gen_tests($)
64 my ($prog) = @_;
65 my $try_help = "Try '$prog --help' for more information.\n";
66 @Tests=
68 ['empty', {IN=>''}, {OUT=>""}],
69 ['inout1', {IN=>'a'x1}, {OUT=>&$enc(1)."\n"}],
70 ['inout2', {IN=>'a'x2}, {OUT=>&$enc(2)."\n"}],
71 ['inout3', {IN=>'a'x3}, {OUT=>&$enc(3)."\n"}],
72 ['inout4', {IN=>'a'x4}, {OUT=>&$enc(4)."\n"}],
73 ['inout5', {IN=>'a'x5}, {OUT=>&$enc(5)."\n"}],
74 ['wrap', '--wrap 0', {IN=>'a'}, {OUT=>&$enc(1)}],
75 ['wrap-zero', '--wrap 08', {IN=>'a'}, {OUT=>&$enc(1)."\n"}],
76 ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>wrap &$enc(39),5}],
77 ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>wrap &$enc(40),5}],
78 ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>wrap &$enc(41),5}],
79 ['wrap5-42', '--wrap=5', {IN=>'a' x 42}, {OUT=>wrap &$enc(42),5}],
80 ['wrap5-43', '--wrap=5', {IN=>'a' x 43}, {OUT=>wrap &$enc(43),5}],
81 ['wrap5-44', '--wrap=5', {IN=>'a' x 44}, {OUT=>wrap &$enc(44),5}],
82 ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>wrap &$enc(45),5}],
83 ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>wrap &$enc(46),5}],
85 ['wrap-bad-1', '-w0x0', {IN=>''}, {OUT=>""},
86 {ERR_SUBST => 's/base..:/base..:/'},
87 {ERR => "base..: invalid wrap size: '0x0'\n"}, {EXIT => 1}],
88 ['wrap-bad-2', '-w1k', {IN=>''}, {OUT=>""},
89 {ERR_SUBST => 's/base..:/base..:/'},
90 {ERR => "base..: invalid wrap size: '1k'\n"}, {EXIT => 1}],
91 ['wrap-bad-3', '-w-1', {IN=>''}, {OUT=>""},
92 {ERR_SUBST => 's/base..:/base..:/'},
93 {ERR => "base..: invalid wrap size: '-1'\n"}, {EXIT => 1}],
94 ['wrap-bad-4', '-w-0', {IN=>''}, {OUT=>""},
95 {ERR_SUBST => 's/base..:/base..:/'},
96 {ERR => "base..: invalid wrap size: '-0'\n"}, {EXIT => 1}],
98 ['buf-1', '--decode', {IN=>&$enc(1)}, {OUT=>'a' x 1}],
99 ['buf-2', '--decode', {IN=>&$enc(2)}, {OUT=>'a' x 2}],
100 ['buf-3', '--decode', {IN=>&$enc(3)}, {OUT=>'a' x 3}],
101 ['buf-4', '--decode', {IN=>&$enc(4)}, {OUT=>'a' x 4}],
102 # 4KB worth of input.
103 ['buf-4k0', '--decode', {IN=>&$enc(3072+0)}, {OUT=>'a' x (3072+0)}],
104 ['buf-4k1', '--decode', {IN=>&$enc(3072+1)}, {OUT=>'a' x (3072+1)}],
105 ['buf-4k2', '--decode', {IN=>&$enc(3072+2)}, {OUT=>'a' x (3072+2)}],
106 ['buf-4k3', '--decode', {IN=>&$enc(3072+3)}, {OUT=>'a' x (3072+3)}],
107 ['buf-4km1','--decode', {IN=>&$enc(3072-1)}, {OUT=>'a' x (3072-1)}],
108 ['buf-4km2','--decode', {IN=>&$enc(3072-2)}, {OUT=>'a' x (3072-2)}],
109 ['buf-4km3','--decode', {IN=>&$enc(3072-3)}, {OUT=>'a' x (3072-3)}],
110 ['buf-4km4','--decode', {IN=>&$enc(3072-4)}, {OUT=>'a' x (3072-4)}],
112 # Exercise the case in which the final base-64 byte is
113 # in a buffer all by itself.
114 ['b4k-1', '--decode', {IN=>$a3k_nl[1]}, {OUT=>'a' x (3072+0)}],
115 ['b4k-2', '--decode', {IN=>$a3k_nl[2]}, {OUT=>'a' x (3072+0)}],
116 ['b4k-3', '--decode', {IN=>$a3k_nl[3]}, {OUT=>'a' x (3072+0)}],
118 ['ext-op1', 'a b', {IN=>''}, {EXIT=>1},
119 {ERR => "$prog: extra operand 'b'\n" . $try_help}],
120 # Again, with more option arguments
121 ['ext-op2', '-di --wrap=40 a b', {IN=>''}, {EXIT=>1},
122 {ERR => "$prog: extra operand 'b'\n" . $try_help}],
125 if ($prog eq "base64")
127 push @Tests, (
128 ['baddecode', '--decode', {IN=>'a'}, {OUT=>""},
129 {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
130 ['baddecode2', '--decode', {IN=>'ab'}, {OUT=>"i"},
131 {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
132 ['baddecode3', '--decode', {IN=>'Zzz'}, {OUT=>"g<"},
133 {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
134 ['baddecode4', '--decode', {IN=>'Zz='}, {OUT=>"g"},
135 {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
136 ['baddecode5', '--decode', {IN=>'Z==='}, {OUT=>""},
137 {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}]
141 # For each non-failing test, create a --decode test using the
142 # expected output as input. Also, add tests inserting newlines.
143 my @new;
144 foreach my $t (@Tests)
146 my $exit_val;
147 my $in;
148 my @out;
150 # If the test has a single option of "--decode", then skip it.
151 !ref $t->[1] && $t->[1] eq '--decode'
152 and next;
154 foreach my $e (@$t)
156 ref $e && ref $e eq 'HASH'
157 or next;
158 defined $e->{EXIT}
159 and $exit_val = $e->{EXIT};
160 defined $e->{IN}
161 and $in = $e->{IN};
162 if (defined $e->{OUT})
164 my $t = $e->{OUT};
165 push @out, $t;
166 my $len = length $t;
167 foreach my $i (0..$len)
169 my $u = $t;
170 substr ($u, $i, 0) = "\n";
171 push @out, $u;
172 10 <= $i
173 and last;
177 $exit_val
178 and next;
180 my $i = 0;
181 foreach my $o (@out)
183 push @new, ["d$i-$t->[0]", '--decode', {IN => $o}, {OUT => $in}];
184 ++$i;
187 push @Tests, @new;
190 my $save_temps = $ENV{DEBUG};
191 my $verbose = $ENV{VERBOSE};
193 my $fail = 0;
194 foreach my $prog (qw(base32 base64))
196 $enc = $prog eq "base32" ? \&enc32 : \&enc64;
198 # Construct an encoded string of length 4KB, using 3K "a"s.
199 $a3k = &$enc(3072);
200 @a3k_nl = ();
201 # A few copies, each with different number of newlines at the start.
202 for my $k (0..3)
204 (my $t = $a3k) =~ s/^/"\n"x $k/e;
205 push @a3k_nl, $t;
208 gen_tests($prog);
210 $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
211 if ($fail != 0)
213 last;
217 exit $fail;