summaryrefslogtreecommitdiff
path: root/test/run
blob: cf1525226f68173f6e44b0c1ce70590cfc82ba57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/perl -w -U

# Copyright (c) 2007, 2008 Andreas Gruenbacher.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer,
#    without modification, immediately at the beginning of the file.
# 2. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Alternatively, this software may be distributed under the terms of the
# GNU Public License ("GPL"):
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# Possible improvements:
#
# - distinguish stdout and stderr output
# - add environment variable like assignments
# - run up to a specific line
# - resume at a specific line
#

use strict;
use FileHandle;
use Getopt::Std;
use POSIX qw(isatty setuid getcwd);
use vars qw($opt_l $opt_v);

no warnings qw(taint);

$opt_l = ~0;  # a really huge number
getopts('l:v');

my ($OK, $FAILED) = ("ok", "failed");
if (isatty(fileno(STDOUT))) {
	$OK = "\033[32m" . $OK . "\033[m";
	$FAILED = "\033[31m\033[1m" . $FAILED . "\033[m";
}

$ENV{"TUSER"} = getpwuid($>);
$ENV{"TGROUP"} = getgrgid($));

sub exec_test($$);
sub process_test($$$$);

my ($prog, $in, $out) = ([], [], []);
my $prog_line = 0;
my ($tests, $failed) = (0,0);
my $lineno;
my $width = ($ENV{COLUMNS} || 80) >> 1;

for (;;) {
  my $line = <>; $lineno++;
  if (defined $line) {
    # Substitute %VAR and %{VAR} with environment variables.
    $line =~ s[%(\w+)][$ENV{$1}]eg;
    $line =~ s[%{(\w+)}][$ENV{$1}]eg;
  }
  if (defined $line) {
    if ($line =~ s/^\s*< ?//) {
      push @$in, $line;
    } elsif ($line =~ s/^\s*> ?//) {
      push @$out, $line;
    } else {
      process_test($prog, $prog_line, $in, $out);
      last if $prog_line >= $opt_l;

      $prog = [];
      $prog_line = 0;
    }
    if ($line =~ s/^\s*\$ ?//) {
      $prog = [ map { s/\\(.)/$1/g; $_ } split /(?<!\\)\s+/, $line ];
      $prog_line = $lineno;
      $in = [];
      $out = [];
    }
  } else {
    process_test($prog, $prog_line, $in, $out);
    last;
  }
}

my $status = sprintf("%d commands (%d passed, %d failed)",
	$tests, $tests-$failed, $failed);
if (isatty(fileno(STDOUT))) {
	if ($failed) {
		$status = "\033[31m\033[1m" . $status . "\033[m";
	} else {
		$status = "\033[32m" . $status . "\033[m";
	}
}
print $status, "\n";
exit $failed ? 1 : 0;


sub process_test($$$$) {
  my ($prog, $prog_line, $in, $out) = @_;

  return unless @$prog;

       my $p = [ @$prog ];
       print "[$prog_line] \$ ", join(' ',
             map { s/\s/\\$&/g; $_ } @$p), " -- ";
       my $result = exec_test($prog, $in);
       my @good = ();
       my $nmax = (@$out > @$result) ? @$out : @$result;
       for (my $n=0; $n < $nmax; $n++) {
	   my $use_re;
	   if (defined $out->[$n] && $out->[$n] =~ /^~ /) {
		$use_re = 1;
		$out->[$n] =~ s/^~ //g;
	   }

           if (!defined($out->[$n]) || !defined($result->[$n]) ||
               (!$use_re && $result->[$n] ne $out->[$n]) ||
               ( $use_re && $result->[$n] !~ /^$out->[$n]/)) {
               push @good, ($use_re ? '!~' : '!=');
	   }
	   else {
               push @good, ($use_re ? '=~' : '==');
           }
       }
       my $good = !(grep /!/, @good);
       $tests++;
       $failed++ unless $good;
       print $good ? $OK : $FAILED, "\n";
       if (!$good || $opt_v) {
         for (my $n=0; $n < $nmax; $n++) {
	   my $l = defined($out->[$n]) ? $out->[$n] : "~";
	   chomp $l;
	   my $r = defined($result->[$n]) ? $result->[$n] : "~";
	   chomp $r;
	   print sprintf("%-" . ($width-3) . "s %s %s\n",
			 $r, $good[$n], $l);
         }
       }
}


sub su($) {
  my ($user) = @_;

  $user ||= "root";

  my ($login, $pass, $uid, $gid) = getpwnam($user)
    or return [ "su: user $user does not exist\n" ];
  my @groups = ();
  my $fh = new FileHandle("/etc/group")
    or return [ "opening /etc/group: $!\n" ];
  while (<$fh>) {
    chomp;
    my ($group, $passwd, $gid, $users) = split /:/;
    foreach my $u (split /,/, $users) {
      push @groups, $gid
	if ($user eq $u);
    }
  }
  $fh->close;

  my $groups = join(" ", ($gid, $gid, @groups));
  #print STDERR "[[$groups]]\n";
  $! = 0;  # reset errno
  $> = 0;
  $( = $gid;
  $) = $groups;
  if ($!) {
    return [ "su: $!\n" ];
  }
  if ($uid != 0) {
    $> = $uid;
    #$< = $uid;
    if ($!) {
      return [ "su: $prog->[1]: $!\n" ];
    }
  }
  #print STDERR "[($>,$<)($(,$))]";
  return [];
}


sub sg($) {
  my ($group) = @_;

  my $gid = getgrnam($group)
    or return [ "sg: group $group does not exist\n" ];
  my %groups = map { $_ eq $gid ? () : ($_ => 1) } (split /\s/, $));
  
  #print STDERR "<<", join("/", keys %groups), ">>\n";
  my $groups = join(" ", ($gid, $gid, keys %groups));
  #print STDERR "[[$groups]]\n";
  $! = 0;  # reset errno
  if ($> != 0) {
	  my $uid = $>;
	  $> = 0;
	  $( = $gid;
	  $) = $groups;
	  $> = $uid;
  } else {
	  $( = $gid;
	  $) = $groups;
  }
  if ($!) {
    return [ "sg: $!\n" ];
  }
  print STDERR "[($>,$<)($(,$))]";
  return [];
}


sub exec_test($$) {
  my ($prog, $in) = @_;
  local (*IN, *IN_DUP, *IN2, *OUT_DUP, *OUT, *OUT2);
  my $needs_shell = (join('', @$prog) =~ /[][|<>"'`\$\*\?]/);

  if ($prog->[0] eq "umask") {
    umask oct $prog->[1];
    return [];
  } elsif ($prog->[0] eq "cd") {
    if (!chdir $prog->[1]) {
      return [ "chdir: $prog->[1]: $!\n" ];
    }
    $ENV{PWD} = getcwd;
    return [];
  } elsif ($prog->[0] eq "su") {
    return su($prog->[1]);
  } elsif ($prog->[0] eq "sg") {
    return sg($prog->[1]);
  } elsif ($prog->[0] eq "export") {
    my ($name, $value) = split /=/, $prog->[1];
    # FIXME: need to evaluate $value, so that things like this will work:
    # export dir=$PWD/dir
    $ENV{$name} = $value;
    return [];
  } elsif ($prog->[0] eq "unset") {
    delete $ENV{$prog->[1]};
    return [];
  }

  pipe *IN2, *OUT
    or die "Can't create pipe for reading: $!";
  open *IN_DUP, "<&STDIN"
    or *IN_DUP = undef;
  open *STDIN, "<&IN2"
    or die "Can't duplicate pipe for reading: $!";
  close *IN2;

  open *OUT_DUP, ">&STDOUT"
    or die "Can't duplicate STDOUT: $!";
  pipe *IN, *OUT2
    or die "Can't create pipe for writing: $!";
  open *STDOUT, ">&OUT2"
    or die "Can't duplicate pipe for writing: $!";
  close *OUT2;

  *STDOUT->autoflush();
  *OUT->autoflush();

  if (fork()) {
    # Server
    if (*IN_DUP) {
      open *STDIN, "<&IN_DUP"
        or die "Can't duplicate STDIN: $!";
      close *IN_DUP
        or die "Can't close STDIN duplicate: $!";
    }
    open *STDOUT, ">&OUT_DUP"
      or die "Can't duplicate STDOUT: $!";
    close *OUT_DUP
      or die "Can't close STDOUT duplicate: $!";

    foreach my $line (@$in) {
      #print "> $line";
      print OUT $line;
    }
    close *OUT
      or die "Can't close pipe for writing: $!";

    my $result = [];
    while (<IN>) {
      #print "< $_";
      # remove libtool 'lt-' prefixes on prog name output
      s#^lt-##g;
      if ($needs_shell) {
	s#^/bin/sh: line \d+: ##;
      }
      push @$result, $_;
    }
    return $result;
  } else {
    # Client
    $< = $>;
    close IN
      or die "Can't close read end for input pipe: $!";
    close OUT
      or die "Can't close write end for output pipe: $!";
    close OUT_DUP
      or die "Can't close STDOUT duplicate: $!";
    local *ERR_DUP;
    open ERR_DUP, ">&STDERR"
      or die "Can't duplicate STDERR: $!";
    open STDERR, ">&STDOUT"
      or die "Can't join STDOUT and STDERR: $!";

    if ($needs_shell) {
      exec ('/bin/sh', '-c', join(" ", @$prog));
    } else {
      exec @$prog;
    }
    print STDERR $prog->[0], ": $!\n";
    exit;
  }
}