Debian Codegraph @

tests/Coreutils.pm

raw
1 package Coreutils;
2 # This file (Coreutils.pm) was copied from GNU Coretils.
3 # It has the following copyright notice and license:
4
5 # This is a testing framework.
6
7 # Copyright (C) 1998-2013 Free Software Foundation, Inc.
8
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 use strict;
23 use vars qw($VERSION @ISA @EXPORT);
24
25 use FileHandle;
26 use File::Compare qw(compare);
27
28 @ISA = qw(Exporter);
29 ($VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
30 @EXPORT = qw (run_tests triple_test getlimits);
31
32 my $debug = $ENV{DEBUG};
33
34 my @Types = qw (IN IN_PIPE OUT ERR AUX CMP EXIT PRE POST OUT_SUBST
35 ERR_SUBST ENV ENV_DEL);
36 my %Types = map {$_ => 1} @Types;
37 my %Zero_one_type = map {$_ => 1}
38 qw (OUT ERR EXIT PRE POST OUT_SUBST ERR_SUBST ENV);
39 my $srcdir = "$ENV{srcdir}";
40 my $Global_count = 1;
41
42 # When running in a DJGPP environment, make $ENV{SHELL} point to bash.
43 # Otherwise, a bad shell might be used (e.g. command.com) and many
44 # tests would fail.
45 defined $ENV{DJDIR}
46 and $ENV{SHELL} = "$ENV{DJDIR}/bin/bash.exe";
47
48 # A file spec: a scalar or a reference to a single-keyed hash
49 # ================
50 # 'contents' contents only (file name is derived from test name)
51 # {filename => 'contents'} filename and contents
52 # {filename => undef} filename only -- $(srcdir)/tests/filename must exist
53 #
54 # FIXME: If there is more than one input file, then you can't specify 'REDIR'.
55 # PIPE is still ok.
56 #
57 # I/O spec: a hash ref with the following properties
58 # ================
59 # - one key/value pair
60 # - the key must be one of these strings: IN, OUT, ERR, AUX, CMP, EXIT
61 # - the value must be a file spec
62 # {OUT => 'data'} put data in a temp file and compare it to stdout from cmd
63 # {OUT => {'filename'=>undef}} compare contents of existing filename to
64 # stdout from cmd
65 # {OUT => {'filename'=>[$CTOR, $DTOR]}} $CTOR and $DTOR are references to
66 # functions, each which is passed the single argument 'filename'.
67 # $CTOR must create 'filename'.
68 # DTOR may be omitted in which case 'sub{unlink @_[0]}' is used.
69 # FIXME: implement this
70 # {ERR => ...}
71 # Same as for OUT, but compare with stderr, not stdout.
72 # {OUT_SUBST => 's/variable_output/expected_output/'}
73 # Transform actual standard output before comparing it against expected.
74 # This is useful e.g. for programs like du that produce output that
75 # varies a lot from system. E.g., an empty file may consume zero file
76 # blocks, or more, depending on the OS and on the file system type.
77 # {ERR_SUBST => 's/variable_output/expected_output/'}
78 # Transform actual stderr output before comparing it against expected.
79 # This is useful when verifying that we get a meaningful diagnostic.
80 # For example, in rm/fail-2eperm, we have to account for three different
81 # diagnostics: Operation not permitted, Not owner, and Permission denied.
82 # {EXIT => N} expect exit status of cmd to be N
83 # {ENV => 'VAR=val ...'}
84 # Prepend 'VAR=val ...' to the command that we execute via 'system'.
85 # {ENV_DEL => 'VAR'}
86 # Remove VAR from the environment just before running the corresponding
87 # command, and restore any value just afterwards.
88 #
89 # There may be many input file specs. File names from the input specs
90 # are concatenated in order on the command line.
91 # There may be at most one of the OUT-, ERR-, and EXIT-keyed specs.
92 # If the OUT-(or ERR)-keyed hash ref is omitted, then expect no output
93 # on stdout (or stderr).
94 # If the EXIT-keyed one is omitted, then expect the exit status to be zero.
95
96 # FIXME: Make sure that no junkfile is also listed as a
97 # non-junkfile (i.e. with undef for contents)
98
99 sub _shell_quote ($)
100 {
101 my ($string) = @_;
102 $string =~ s/\'/\'\\\'\'/g;
103 return "'$string'";
104 }
105
106 sub _create_file ($$$$)
107 {
108 my ($program_name, $test_name, $file_name, $data) = @_;
109 my $file;
110 if (defined $file_name)
111 {
112 $file = $file_name;
113 }
114 else
115 {
116 $file = "$test_name.$Global_count";
117 ++$Global_count;
118 }
119
120 warn "creating file '$file' with contents '$data'\n" if $debug;
121
122 # The test spec gave a string.
123 # Write it to a temp file and return tempfile name.
124 my $fh = new FileHandle "> $file";
125 die "$program_name: $file: $!\n" if ! $fh;
126 print $fh $data;
127 $fh->close || die "$program_name: $file: $!\n";
128
129 return $file;
130 }
131
132 sub _compare_files ($$$$$)
133 {
134 my ($program_name, $test_name, $in_or_out, $actual, $expected) = @_;
135
136 my $differ = compare ($actual, $expected);
137 if ($differ)
138 {
139 my $info = (defined $in_or_out ? "std$in_or_out " : '');
140 warn "$program_name: test $test_name: ${info}mismatch, comparing "
141 . "$expected (expected) and $actual (actual)\n";
142 # Ignore any failure, discard stderr.
143 system "diff -c $expected $actual 2>/dev/null";
144 }
145
146 return $differ;
147 }
148
149 sub _process_file_spec ($$$$$)
150 {
151 my ($program_name, $test_name, $file_spec, $type, $junk_files) = @_;
152
153 my ($file_name, $contents);
154 if (!ref $file_spec)
155 {
156 ($file_name, $contents) = (undef, $file_spec);
157 }
158 elsif (ref $file_spec eq 'HASH')
159 {
160 my $n = keys %$file_spec;
161 die "$program_name: $test_name: $type spec has $n elements --"
162 . " expected 1\n"
163 if $n != 1;
164 ($file_name, $contents) = each %$file_spec;
165
166 # This happens for the AUX hash in an io_spec like this:
167 # {CMP=> ['zy123utsrqponmlkji', {'@AUX@'=> undef}]},
168 defined $contents
169 or return $file_name;
170 }
171 else
172 {
173 die "$program_name: $test_name: invalid RHS in $type-spec\n"
174 }
175
176 my $is_junk_file = (! defined $file_name
177 || (($type eq 'IN' || $type eq 'AUX' || $type eq 'CMP')
178 && defined $contents));
179 my $file = _create_file ($program_name, $test_name,
180 $file_name, $contents);
181
182 if ($is_junk_file)
183 {
184 push @$junk_files, $file
185 }
186 else
187 {
188 # FIXME: put $srcdir in here somewhere
189 warn "$program_name: $test_name: specified file '$file' does"
190 . " not exist\n"
191 if ! -f "$srcdir/tests/$file";
192 }
193
194 return $file;
195 }
196
197 sub _at_replace ($$)
198 {
199 my ($map, $s) = @_;
200 foreach my $eo (qw (AUX OUT ERR))
201 {
202 my $f = $map->{$eo};
203 $f
204 and $s =~ /\@$eo\@/
205 and $s =~ s/\@$eo\@/$f/g;
206 }
207 return $s;
208 }
209
210 sub getlimits()
211 {
212 my $NV;
213 open $NV, "getlimits |" or die "Error running getlimits\n";
214 my %limits = map {split /=|\n/} <$NV>;
215 return \%limits;
216 }
217
218 # FIXME: cleanup on interrupt
219 # FIXME: extract 'do_1_test' function
220
221 # FIXME: having to include $program_name here is an expedient kludge.
222 # Library code doesn't 'die'.
223 sub run_tests ($$$$$)
224 {
225 my ($program_name, $prog, $t_spec, $save_temps, $verbose) = @_;
226
227 # To indicate that $prog is a shell built-in, you'd make it a string 'ref'.
228 # E.g., call run_tests ($prog, \$prog, \@Tests, $save_temps, $verbose);
229 # If it's a ref, invoke it via "env":
230 my @prog = ref $prog ? (qw(env --), $$prog) : $prog;
231
232 # Warn about empty t_spec.
233 # FIXME
234
235 # Remove all temp files upon interrupt.
236 # FIXME
237
238 # Verify that test names are distinct.
239 my $bad_test_name = 0;
240 my %seen;
241 my %seen_8dot3;
242 my $t;
243 foreach $t (@$t_spec)
244 {
245 my $test_name = $t->[0];
246 if ($seen{$test_name})
247 {
248 warn "$program_name: $test_name: duplicate test name\n";
249 $bad_test_name = 1;
250 }
251 $seen{$test_name} = 1;
252
253 if (0)
254 {
255 my $t8 = lc substr $test_name, 0, 8;
256 if ($seen_8dot3{$t8})
257 {
258 warn "$program_name: 8.3 test name conflict: "
259 . "$test_name, $seen_8dot3{$t8}\n";
260 $bad_test_name = 1;
261 }
262 $seen_8dot3{$t8} = $test_name;
263 }
264
265 # The test name may be no longer than 30 bytes.
266 # Yes, this is an arbitrary limit. If it causes trouble,
267 # consider removing it.
268 my $max = 30;
269 if ($max < length $test_name)
270 {
271 warn "$program_name: $test_name: test name is too long (> $max)\n";
272 $bad_test_name = 1;
273 }
274 }
275 return 1 if $bad_test_name;
276
277 # FIXME check exit status
278 system (@prog, '--version') if $verbose;
279
280 my @junk_files;
281 my $fail = 0;
282 foreach my $tt (@$t_spec)
283 {
284 my @post_compare;
285 my @dummy = @$tt;
286 my $t = \@dummy;
287 my $test_name = shift @$t;
288 my $expect = {};
289 my ($pre, $post);
290
291 # FIXME: maybe don't reset this.
292 $Global_count = 1;
293 my @args;
294 my $io_spec;
295 my %seen_type;
296 my @env_delete;
297 my $env_prefix = '';
298 my $input_pipe_cmd;
299 foreach $io_spec (@$t)
300 {
301 if (!ref $io_spec)
302 {
303 push @args, $io_spec;
304 next;
305 }
306
307 if (ref $io_spec ne 'HASH')
308 {
309 eval 'use Data::Dumper';
310 die "$program_name: $test_name: invalid entry in test spec; "
311 . "expected HASH-ref,\nbut got this:\n"
312 . Data::Dumper->Dump ([\$io_spec], ['$io_spec']) . "\n";
313 }
314
315 my $n = keys %$io_spec;
316 die "$program_name: $test_name: spec has $n elements --"
317 . " expected 1\n"
318 if $n != 1;
319 my ($type, $val) = each %$io_spec;
320 die "$program_name: $test_name: invalid key '$type' in test spec\n"
321 if ! $Types{$type};
322
323 # Make sure there's no more than one of OUT, ERR, EXIT, etc.
324 die "$program_name: $test_name: more than one $type spec\n"
325 if $Zero_one_type{$type} and $seen_type{$type}++;
326
327 if ($type eq 'PRE' or $type eq 'POST')
328 {
329 $expect->{$type} = $val;
330 next;
331 }
332
333 if ($type eq 'CMP')
334 {
335 my $t = ref $val;
336 $t && $t eq 'ARRAY'
337 or die "$program_name: $test_name: invalid CMP spec\n";
338 @$val == 2
339 or die "$program_name: $test_name: invalid CMP list; must have"
340 . " exactly 2 elements\n";
341 my @cmp_files;
342 foreach my $e (@$val)
343 {
344 my $r = ref $e;
345 $r && $r ne 'HASH'
346 and die "$program_name: $test_name: invalid element ($r)"
347 . " in CMP list; only scalars and hash references "
348 . "are allowed\n";
349 if ($r && $r eq 'HASH')
350 {
351 my $n = keys %$e;
352 $n == 1
353 or die "$program_name: $test_name: CMP spec has $n "
354 . "elements -- expected 1\n";
355
356 # Replace any '@AUX@' in the key of %$e.
357 my ($ff, $val) = each %$e;
358 my $new_ff = _at_replace $expect, $ff;
359 if ($new_ff ne $ff)
360 {
361 $e->{$new_ff} = $val;
362 delete $e->{$ff};
363 }
364 }
365 my $cmp_file = _process_file_spec ($program_name, $test_name,
366 $e, $type, \@junk_files);
367 push @cmp_files, $cmp_file;
368 }
369 push @post_compare, [@cmp_files];
370
371 $expect->{$type} = $val;
372 next;
373 }
374
375 if ($type eq 'EXIT')
376 {
377 die "$program_name: $test_name: invalid EXIT code\n"
378 if $val !~ /^\d+$/;
379 # FIXME: make sure $data is numeric
380 $expect->{EXIT} = $val;
381 next;
382 }
383
384 if ($type =~ /^(OUT|ERR)_SUBST$/)
385 {
386 $expect->{RESULT_SUBST} ||= {};
387 $expect->{RESULT_SUBST}->{$1} = $val;
388 next;
389 }
390
391 if ($type eq 'ENV')
392 {
393 $env_prefix = "$val ";
394 next;
395 }
396
397 if ($type eq 'ENV_DEL')
398 {
399 push @env_delete, $val;
400 next;
401 }
402
403 my $file = _process_file_spec ($program_name, $test_name, $val,
404 $type, \@junk_files);
405
406 if ($type eq 'IN' || $type eq 'IN_PIPE')
407 {
408 my $quoted_file = _shell_quote $file;
409 if ($type eq 'IN_PIPE')
410 {
411 defined $input_pipe_cmd
412 and die "$program_name: $test_name: only one input"
413 . " may be specified with IN_PIPE\n";
414 $input_pipe_cmd = "cat $quoted_file |";
415 }
416 else
417 {
418 push @args, $quoted_file;
419 }
420 }
421 elsif ($type eq 'AUX' || $type eq 'OUT' || $type eq 'ERR')
422 {
423 $expect->{$type} = $file;
424 }
425 else
426 {
427 die "$program_name: $test_name: invalid type: $type\n"
428 }
429 }
430
431 # Expect an exit status of zero if it's not specified.
432 $expect->{EXIT} ||= 0;
433
434 # Allow ERR to be omitted -- in that case, expect no error output.
435 foreach my $eo (qw (OUT ERR))
436 {
437 if (!exists $expect->{$eo})
438 {
439 $expect->{$eo} = _create_file ($program_name, $test_name,
440 undef, '');
441 push @junk_files, $expect->{$eo};
442 }
443 }
444
445 # FIXME: Does it ever make sense to specify a filename *and* contents
446 # in OUT or ERR spec?
447
448 # FIXME: this is really suboptimal...
449 my @new_args;
450 foreach my $a (@args)
451 {
452 $a = _at_replace $expect, $a;
453 push @new_args, $a;
454 }
455 @args = @new_args;
456
457 warn "$test_name...\n" if $verbose;
458 &{$expect->{PRE}} if $expect->{PRE};
459 my %actual;
460 $actual{OUT} = "$test_name.O";
461 $actual{ERR} = "$test_name.E";
462 push @junk_files, $actual{OUT}, $actual{ERR};
463 my @cmd = (@prog, @args, "> $actual{OUT}", "2> $actual{ERR}");
464 $env_prefix
465 and unshift @cmd, $env_prefix;
466 defined $input_pipe_cmd
467 and unshift @cmd, $input_pipe_cmd;
468 my $cmd_str = join (' ', @cmd);
469
470 # Delete from the environment any symbols specified by syntax
471 # like this: {ENV_DEL => 'TZ'}.
472 my %pushed_env;
473 foreach my $env_sym (@env_delete)
474 {
475 my $val = delete $ENV{$env_sym};
476 defined $val
477 and $pushed_env{$env_sym} = $val;
478 }
479
480 warn "Running command: '$cmd_str'\n" if $debug;
481 my $rc = 0xffff & system $cmd_str;
482
483 # Restore any environment setting we changed via a deletion.
484 foreach my $env_sym (keys %pushed_env)
485 {
486 $ENV{$env_sym} = $pushed_env{$env_sym};
487 }
488
489 if ($rc == 0xff00)
490 {
491 warn "$program_name: test $test_name failed: command failed:\n"
492 . " '$cmd_str': $!\n";
493 $fail = 1;
494 goto cleanup;
495 }
496 $rc >>= 8 if $rc > 0x80;
497 if ($expect->{EXIT} != $rc)
498 {
499 warn "$program_name: test $test_name failed: exit status mismatch:"
500 . " expected $expect->{EXIT}, got $rc\n";
501 $fail = 1;
502 goto cleanup;
503 }
504
505 my %actual_data;
506 # Record actual stdout and stderr contents, if POST may need them.
507 if ($expect->{POST})
508 {
509 foreach my $eo (qw (OUT ERR))
510 {
511 my $out_file = $actual{$eo};
512 open IN, $out_file
513 or (warn
514 "$program_name: cannot open $out_file for reading: $!\n"),
515 $fail = 1, next;
516 $actual_data{$eo} = <IN>;
517 close IN
518 or (warn "$program_name: failed to read $out_file: $!\n"),
519 $fail = 1;
520 }
521 }
522
523 foreach my $eo (qw (OUT ERR))
524 {
525 my $subst_expr = $expect->{RESULT_SUBST}->{$eo};
526 if (defined $subst_expr)
527 {
528 my $out = $actual{$eo};
529 my $orig = "$out.orig";
530
531 # Move $out aside (to $orig), then recreate $out
532 # by transforming each line of $orig via $subst_expr.
533 rename $out, $orig
534 or (warn "$program_name: cannot rename $out to $orig: $!\n"),
535 $fail = 1, next;
536 open IN, $orig
537 or (warn "$program_name: cannot open $orig for reading: $!\n"),
538 $fail = 1, (unlink $orig), next;
539 unlink $orig
540 or (warn "$program_name: cannot unlink $orig: $!\n"),
541 $fail = 1;
542 open OUT, ">$out"
543 or (warn "$program_name: cannot open $out for writing: $!\n"),
544 $fail = 1, next;
545 while (defined (my $line = <IN>))
546 {
547 eval "\$_ = \$line; $subst_expr; \$line = \$_";
548 print OUT $line;
549 }
550 close IN;
551 close OUT
552 or (warn "$program_name: failed to write $out: $!\n"),
553 $fail = 1, next;
554 }
555
556 my $eo_lower = lc $eo;
557 _compare_files ($program_name, $test_name, $eo_lower,
558 $actual{$eo}, $expect->{$eo})
559 and $fail = 1;
560 }
561
562 foreach my $pair (@post_compare)
563 {
564 my ($expected, $actual) = @$pair;
565 _compare_files $program_name, $test_name, undef, $actual, $expected
566 and $fail = 1;
567 }
568
569 cleanup:
570 $expect->{POST}
571 and &{$expect->{POST}} ($actual_data{OUT}, $actual_data{ERR});
572
573 }
574
575 # FIXME: maybe unlink files inside the big foreach loop?
576 unlink @junk_files if ! $save_temps;
577
578 return $fail;
579 }
580
581 # For each test in @$TESTS, generate two additional tests,
582 # one using stdin, the other using a pipe. I.e., given this one
583 # ['idem-0', {IN=>''}, {OUT=>''}],
584 # generate these:
585 # ['idem-0.r', '<', {IN=>''}, {OUT=>''}],
586 # ['idem-0.p', {IN_PIPE=>''}, {OUT=>''}],
587 # Generate new tests only if there is exactly one input spec.
588 # The returned list of tests contains each input test, followed
589 # by zero or two derived tests.
590 sub triple_test($)
591 {
592 my ($tests) = @_;
593 my @new;
594 foreach my $t (@$tests)
595 {
596 push @new, $t;
597
598 my @in;
599 my @args;
600 my @list_of_hash;
601 foreach my $e (@$t)
602 {
603 !ref $e
604 and push (@args, $e), next;
605
606 ref $e && ref $e eq 'HASH'
607 or (warn "$0: $t->[0]: unexpected entry type\n"), next;
608 defined $e->{IN}
609 and (push @in, $e->{IN}), next;
610 push @list_of_hash, $e;
611 }
612 # Add variants IFF there is exactly one input file.
613 @in == 1
614 or next;
615 shift @args; # discard test name
616 push @new, ["$t->[0].r", @args, '<', {IN => $in[0]}, @list_of_hash];
617 push @new, ["$t->[0].p", @args, {IN_PIPE => $in[0]}, @list_of_hash];
618 }
619 return @new;
620 }
621
622 ## package return
623 1;