~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/misc/base64.pl

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
# Exercise base64.
 
3
 
 
4
# Copyright (C) 2006-2012 Free Software Foundation, Inc.
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
use strict;
 
20
 
 
21
(my $program_name = $0) =~ s|.*/||;
 
22
 
 
23
# Turn off localization of executable's output.
 
24
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
 
25
 
 
26
# Return the encoding of a string of N 'a's.
 
27
sub enc($)
 
28
{
 
29
  my ($n) = @_;
 
30
  my %remainder = ( 0 => '', 1 => 'YQ==', 2 => 'YWE=' );
 
31
  return 'YWFh' x ($n / 3) . $remainder{$n % 3};
 
32
}
 
33
 
 
34
# Construct an encoded string of length 4KB, using 3K "a"s.
 
35
my $a3k = enc 3072;
 
36
my @a3k_nl;
 
37
# A few copies, each with different number of newlines at the start.
 
38
for my $k (0..3)
 
39
  {
 
40
    (my $t = $a3k) =~ s/^/"\n"x $k/e;
 
41
    push @a3k_nl, $t;
 
42
  }
 
43
 
 
44
# Return a copy of S, with newlines inserted every WIDTH bytes.
 
45
# Ensure that the result (if not the empty string) is newline-terminated.
 
46
sub wrap($$)
 
47
{
 
48
  my ($s, $width) = @_;
 
49
  $s =~ s/(.{$width})/$1\n/g;
 
50
  substr ($s, -1, 1) ne "\n"
 
51
    and $s .= "\n";
 
52
  return $s;
 
53
}
 
54
 
 
55
my @Tests =
 
56
    (
 
57
     ['empty', {IN=>''}, {OUT=>""}],
 
58
     ['inout', {IN=>'a'}, {OUT=>"YQ==\n"}],
 
59
     ['wrap', '--wrap 0', {IN=>'foo'}, {OUT=>'Zm9v'}],
 
60
     ['wrap5-39', '--wrap=5', {IN=>'a' x 39}, {OUT=>wrap enc(39),5}],
 
61
     ['wrap5-40', '--wrap=5', {IN=>'a' x 40}, {OUT=>wrap enc(40),5}],
 
62
     ['wrap5-41', '--wrap=5', {IN=>'a' x 41}, {OUT=>wrap enc(41),5}],
 
63
     ['wrap5-42', '--wrap=5', {IN=>'a' x 42}, {OUT=>wrap enc(42),5}],
 
64
     ['wrap5-43', '--wrap=5', {IN=>'a' x 43}, {OUT=>wrap enc(43),5}],
 
65
     ['wrap5-44', '--wrap=5', {IN=>'a' x 44}, {OUT=>wrap enc(44),5}],
 
66
     ['wrap5-45', '--wrap=5', {IN=>'a' x 45}, {OUT=>wrap enc(45),5}],
 
67
     ['wrap5-46', '--wrap=5', {IN=>'a' x 46}, {OUT=>wrap enc(46),5}],
 
68
 
 
69
     ['buf-1',   '--decode', {IN=>enc 1}, {OUT=>'a' x 1}],
 
70
     ['buf-2',   '--decode', {IN=>enc 2}, {OUT=>'a' x 2}],
 
71
     ['buf-3',   '--decode', {IN=>enc 3}, {OUT=>'a' x 3}],
 
72
     ['buf-4',   '--decode', {IN=>enc 4}, {OUT=>'a' x 4}],
 
73
     # 4KB worth of input.
 
74
     ['buf-4k0', '--decode', {IN=>enc 3072+0}, {OUT=>'a' x (3072+0)}],
 
75
     ['buf-4k1', '--decode', {IN=>enc 3072+1}, {OUT=>'a' x (3072+1)}],
 
76
     ['buf-4k2', '--decode', {IN=>enc 3072+2}, {OUT=>'a' x (3072+2)}],
 
77
     ['buf-4k3', '--decode', {IN=>enc 3072+3}, {OUT=>'a' x (3072+3)}],
 
78
     ['buf-4km1','--decode', {IN=>enc 3072-1}, {OUT=>'a' x (3072-1)}],
 
79
     ['buf-4km2','--decode', {IN=>enc 3072-2}, {OUT=>'a' x (3072-2)}],
 
80
     ['buf-4km3','--decode', {IN=>enc 3072-3}, {OUT=>'a' x (3072-3)}],
 
81
     ['buf-4km4','--decode', {IN=>enc 3072-4}, {OUT=>'a' x (3072-4)}],
 
82
 
 
83
     # Exercise the case in which the final base-64 byte is
 
84
     # in a buffer all by itself.
 
85
     ['b4k-1',   '--decode', {IN=>$a3k_nl[1]}, {OUT=>'a' x (3072+0)}],
 
86
     ['b4k-2',   '--decode', {IN=>$a3k_nl[2]}, {OUT=>'a' x (3072+0)}],
 
87
     ['b4k-3',   '--decode', {IN=>$a3k_nl[3]}, {OUT=>'a' x (3072+0)}],
 
88
 
 
89
     ['baddecode', '--decode', {IN=>'a'}, {OUT=>""},
 
90
      {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
 
91
     ['baddecode2', '--decode', {IN=>'ab'}, {OUT=>"i"},
 
92
      {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
 
93
     ['baddecode3', '--decode', {IN=>'Zzz'}, {OUT=>"g<"},
 
94
      {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
 
95
     ['baddecode4', '--decode', {IN=>'Zz='}, {OUT=>"g"},
 
96
      {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}],
 
97
     ['baddecode5', '--decode', {IN=>'Z==='}, {OUT=>""},
 
98
      {ERR_SUBST => 's/.*: invalid input//'}, {ERR => "\n"}, {EXIT => 1}]
 
99
    );
 
100
 
 
101
# For each non-failing test, create a --decode test using the
 
102
# expected output as input.  Also, add tests inserting newlines.
 
103
my @new;
 
104
foreach my $t (@Tests)
 
105
  {
 
106
    my $exit_val;
 
107
    my $in;
 
108
    my @out;
 
109
 
 
110
    # If the test has a single option of "--decode", then skip it.
 
111
    !ref $t->[1] && $t->[1] eq '--decode'
 
112
      and next;
 
113
 
 
114
    foreach my $e (@$t)
 
115
      {
 
116
        ref $e && ref $e eq 'HASH'
 
117
          or next;
 
118
        defined $e->{EXIT}
 
119
          and $exit_val = $e->{EXIT};
 
120
        defined $e->{IN}
 
121
          and $in = $e->{IN};
 
122
        if (defined $e->{OUT})
 
123
          {
 
124
            my $t = $e->{OUT};
 
125
            push @out, $t;
 
126
            my $len = length $t;
 
127
            foreach my $i (0..$len)
 
128
              {
 
129
                my $u = $t;
 
130
                substr ($u, $i, 0) = "\n";
 
131
                push @out, $u;
 
132
                10 <= $i
 
133
                  and last;
 
134
              }
 
135
          }
 
136
      }
 
137
    $exit_val
 
138
      and next;
 
139
 
 
140
    my $i = 0;
 
141
    foreach my $o (@out)
 
142
      {
 
143
        push @new, ["d$i-$t->[0]", '--decode', {IN => $o}, {OUT => $in}];
 
144
        ++$i;
 
145
      }
 
146
  }
 
147
push @Tests, @new;
 
148
 
 
149
my $save_temps = $ENV{DEBUG};
 
150
my $verbose = $ENV{VERBOSE};
 
151
 
 
152
my $prog = 'base64';
 
153
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
 
154
exit $fail;