Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / base64
1 #!/bin/sh
2 # -*- perl -*-
3 # Exercise base64.
4
5 # Copyright (C) 2006, 2007 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 : ${PERL=perl}
23 : ${srcdir=.}
24
25 $PERL -e 1 > /dev/null 2>&1 || {
26   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
27     "so can't run this test"
28   exit 77
29 }
30
31 exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
32 require 5.003;
33 use strict;
34
35 (my $program_name = $0) =~ s|.*/||;
36
37 # Turn off localisation of executable's ouput.
38 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
39
40 # Return the encoding of a string of N 'a's.
41 sub enc($)
42 {
43   my ($n) = @_;
44   my %remainder = ( 0 => '', 1 => 'YQ==', 2 => 'YWE=' );
45   return 'YWFh' x ($n / 3) . $remainder{$n % 3};
46 }
47
48 # Construct an encoded string of length 4KB, using 3K "a"s.
49 my $a3k = enc 3072;
50 my @a3k_nl;
51 # A few copies, each with different number of newlines at the start.
52 for my $k (0..3)
53   {
54     (my $t = $a3k) =~ s/^/"\n"x $k/e;
55     push @a3k_nl, $t;
56   }
57
58 # Return a copy of S, with newlines inserted every WIDTH bytes.
59 # Ensure that the result (if not the empty string) is newline-terminated.
60 sub wrap($$)
61 {
62   my ($s, $width) = @_;
63   $s =~ s/(.{$width})/$1\n/g;
64   substr ($s, -1, 1) ne "\n"
65     and $s .= "\n";
66   return $s;
67 }
68
69 my @Tests =
70     (
71      ['empty', {IN=>''}, {OUT=>""}],
72      ['inout', {IN=>'a'}, {OUT=>"YQ==\n"}],
73      ['wrap', '--wrap 0', {IN=>'foo'}, {OUT=>'Zm9v'}],
74      ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>wrap enc(39),5}],
75      ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>wrap enc(40),5}],
76      ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>wrap enc(41),5}],
77      ['wrap5-42', '--wrap=5', {IN=>'a' x 42}, {OUT=>wrap enc(42),5}],
78      ['wrap5-43', '--wrap=5', {IN=>'a' x 43}, {OUT=>wrap enc(43),5}],
79      ['wrap5-44', '--wrap=5', {IN=>'a' x 44}, {OUT=>wrap enc(44),5}],
80      ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>wrap enc(45),5}],
81      ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>wrap enc(46),5}],
82
83      ['buf-1',   '--decode', {IN=>enc 1}, {OUT=>'a' x 1}],
84      ['buf-2',   '--decode', {IN=>enc 2}, {OUT=>'a' x 2}],
85      ['buf-3',   '--decode', {IN=>enc 3}, {OUT=>'a' x 3}],
86      ['buf-4',   '--decode', {IN=>enc 4}, {OUT=>'a' x 4}],
87      # 4KB worth of input.
88      ['buf-4k0', '--decode', {IN=>enc 3072+0}, {OUT=>'a' x (3072+0)}],
89      ['buf-4k1', '--decode', {IN=>enc 3072+1}, {OUT=>'a' x (3072+1)}],
90      ['buf-4k2', '--decode', {IN=>enc 3072+2}, {OUT=>'a' x (3072+2)}],
91      ['buf-4k3', '--decode', {IN=>enc 3072+3}, {OUT=>'a' x (3072+3)}],
92      ['buf-4km1','--decode', {IN=>enc 3072-1}, {OUT=>'a' x (3072-1)}],
93      ['buf-4km2','--decode', {IN=>enc 3072-2}, {OUT=>'a' x (3072-2)}],
94      ['buf-4km3','--decode', {IN=>enc 3072-3}, {OUT=>'a' x (3072-3)}],
95      ['buf-4km4','--decode', {IN=>enc 3072-4}, {OUT=>'a' x (3072-4)}],
96
97      # Exercise the case in which the final base-64 byte is
98      # in a buffer all by itself.
99      ['b4k-1',   '--decode', {IN=>$a3k_nl[1]}, {OUT=>'a' x (3072+0)}],
100      ['b4k-2',   '--decode', {IN=>$a3k_nl[2]}, {OUT=>'a' x (3072+0)}],
101      ['b4k-3',   '--decode', {IN=>$a3k_nl[3]}, {OUT=>'a' x (3072+0)}],
102
103      ['baddecode', '--decode', {IN=>'a'}, {OUT=>""},
104       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
105      ['baddecode2', '--decode', {IN=>'ab'}, {OUT=>"i"},
106       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
107      ['baddecode3', '--decode', {IN=>'Zzz'}, {OUT=>"g<"},
108       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
109      ['baddecode4', '--decode', {IN=>'Zz='}, {OUT=>"g"},
110       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
111      ['baddecode5', '--decode', {IN=>'Z==='}, {OUT=>""},
112       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}]
113     );
114
115 # For each non-failing test, create a --decode test using the
116 # expected output as input.  Also, add tests inserting newlines.
117 my @new;
118 foreach my $t (@Tests)
119   {
120     my $exit_val;
121     my $in;
122     my @out;
123
124     # If the test has a single option of "--decode", then skip it.
125     !ref $t->[1] && $t->[1] eq '--decode'
126       and next;
127
128     foreach my $e (@$t)
129       {
130         ref $e && ref $e eq 'HASH'
131           or next;
132         defined $e->{EXIT}
133           and $exit_val = $e->{EXIT};
134         defined $e->{IN}
135           and $in = $e->{IN};
136         if (defined $e->{OUT})
137           {
138             my $t = $e->{OUT};
139             push @out, $t;
140             my $len = length $t;
141             foreach my $i (0..$len)
142               {
143                 my $u = $t;
144                 substr ($u, $i, 0) = "\n";
145                 push @out, $u;
146                 10 <= $i
147                   and last;
148               }
149           }
150       }
151     $exit_val
152       and next;
153
154     my $i = 0;
155     foreach my $o (@out)
156       {
157         push @new, ["d$i-$t->[0]", '--decode', {IN => $o}, {OUT => $in}];
158         ++$i;
159       }
160   }
161 push @Tests, @new;
162
163 my $save_temps = $ENV{DEBUG};
164 my $verbose = $ENV{VERBOSE};
165
166 my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
167 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
168 exit $fail;
169 EOF