Add/fix copyright notices and adjust to latest GNU FDL.
[platform/upstream/coreutils.git] / tests / misc / base64
1 #!/bin/sh
2 # -*- perl -*-
3 # Exercise base64.
4
5 # Copyright (C) 2006 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 my $a39 = "YWFhY\nWFhYW\nFhYWF\nhYWFh\nYWFhY\nWFhYW\nFhYWF\n"
41   . "hYWFh\nYWFhY\nWFhYW\nFh";
42
43 my @Tests =
44     (
45      ['empty', {IN=>''}, {OUT=>""}],
46      ['inout', {IN=>'a'}, {OUT=>"YQ==\n"}],
47      ['wrap', '--wrap 0', {IN=>'foo'}, {OUT=>'Zm9v'}],
48      ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>"${a39}\n"}],
49      ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>"${a39}YQ=\n=\n"}],
50      ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>"${a39}YWE\n=\n"}],
51      ['wrap5-42', '--wrap=5', {IN=>'a' x 42}, {OUT=>"${a39}YWF\nh\n"}],
52      ['wrap5-43', '--wrap=5', {IN=>'a' x 43}, {OUT=>"${a39}YWF\nhYQ==\n"}],
53      ['wrap5-44', '--wrap=5', {IN=>'a' x 44}, {OUT=>"${a39}YWF\nhYWE=\n"}],
54      ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>"${a39}YWF\nhYWFh\n"}],
55      ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>"${a39}YWF\nhYWFh\nYQ==\n"}],
56      ['baddecode', '--decode', {IN=>'a'}, {OUT=>""},
57       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
58      ['baddecode2', '--decode', {IN=>'ab'}, {OUT=>"i"},
59       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
60      ['baddecode3', '--decode', {IN=>'Zzz'}, {OUT=>"g<"},
61       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
62      ['baddecode4', '--decode', {IN=>'Zz='}, {OUT=>"g"},
63       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
64      ['baddecode5', '--decode', {IN=>'Z==='}, {OUT=>""},
65       {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}]
66     );
67
68 # For each non-failing test, create a --decode test using the
69 # expected output (with newlines removed) as input.
70 my @new;
71 foreach my $t (@Tests)
72   {
73     my $exit_val;
74     my $in;
75     my $out;
76     foreach my $e (@$t)
77       {
78         ref $e && ref $e eq 'HASH'
79           or next;
80         defined $e->{EXIT}
81           and $exit_val = $e->{EXIT};
82         defined $e->{IN}
83           and $in = $e->{IN};
84         defined $e->{OUT}
85           and ($out = $e->{OUT}) =~ tr/\n//d;
86       }
87     defined $out && ! $exit_val
88       and push @new, ["d-$t->[0]", '--decode', {IN => $out}, {OUT => $in}];
89   }
90 push @Tests, @new;
91
92 my $save_temps = $ENV{DEBUG};
93 my $verbose = $ENV{VERBOSE};
94
95 my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
96 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
97 exit $fail;
98 EOF