1 # $MirOS: src/bin/mksh/check.pl,v 1.49 2017/05/05 21:17:31 tg Exp $
2 # $OpenBSD: th,v 1.1 2013/12/02 20:39:44 millert Exp $
4 # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
5 # 2012, 2013, 2014, 2015, 2017
6 # mirabilos <m@mirbsd.org>
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un-
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
14 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person's immediate fault when using the work as intended.
26 # a test to show how tests are done
38 # This runs the test-program (eg, mksh) with the arguments -x and -f,
39 # standard input is a file containing "echo hi*\nfalse\n". The program
40 # is expected to produce "hi*" (no trailing newline) on standard output,
41 # "+ echo hi*\n+false\n" on standard error, and an exit code of 1.
44 # Format of test files:
45 # - blank lines and lines starting with # are ignored
46 # - a test file contains a series of tests
47 # - a test is a series of tag:value pairs ended with a "---" line
48 # (leading/trailing spaces are stripped from the first line of value)
50 # Tag Flag Description
51 # ----- ---- -----------
52 # name r The name of the test; should be unique
53 # description m What test does
54 # arguments M Arguments to pass to the program;
55 # default is no arguments.
56 # script m Value is written to a file which
57 # is passed as an argument to the program
58 # (after the arguments arguments)
59 # stdin m Value is written to a file which is
60 # used as standard-input for the program;
61 # default is to use /dev/null.
62 # perl-setup m Value is a perl script which is executed
63 # just before the test is run. Try to
65 # perl-cleanup m Value is a perl script which is executed
66 # just after the test is run. Try to
68 # env-setup M Value is a list of NAME=VALUE elements
69 # which are put in the environment before
70 # the test is run. If the =VALUE is
71 # missing, NAME is removed from the
72 # environment. Programs are run with
73 # the following minimal environment:
74 # HOME, LD_LIBRARY_PATH, LOCPATH,
75 # LOGNAME, PATH, SHELL, UNIXMODE,
77 # (values taken from the environment of
79 # CYGWIN is set to nodosfilewarning.
80 # ENV is set to /nonexistant.
81 # __progname is set to the -p argument.
82 # __perlname is set to $^X (perlexe).
83 # @utflocale@ is substituted from -U.
84 # file-setup mps Used to create files, directories
85 # and symlinks. First word is either
86 # file, dir or symlink; second word is
87 # permissions; this is followed by a
88 # quoted word that is the name of the
89 # file; the end-quote should be followed
90 # by a newline, then the file data
91 # (if any). The first word may be
92 # preceded by a ! to strip the trailing
93 # newline in a symlink.
94 # file-result mps Used to verify a file, symlink or
95 # directory is created correctly.
96 # The first word is either
97 # file, dir or symlink; second word is
98 # expected permissions; third word
99 # is user-id; fourth is group-id;
100 # fifth is "exact" or "pattern"
101 # indicating whether the file contents
102 # which follow is to be matched exactly
103 # or if it is a regular expression.
104 # The fifth argument is the quoted name
105 # of the file that should be created.
106 # The end-quote should be followed
107 # by a newline, then the file data
108 # (if any). The first word may be
109 # preceded by a ! to strip the trailing
110 # newline in the file contents.
111 # The permissions, user and group fields
112 # may be * meaning accept any value.
113 # time-limit Time limit - the program is sent a
114 # SIGKILL N seconds. Default is no
116 # expected-fail 'yes' if the test is expected to fail.
117 # expected-exit expected exit code. Can be a number,
118 # or a C expression using the variables
119 # e, s and w (exit code, termination
120 # signal, and status code).
121 # expected-stdout m What the test should generate on stdout;
122 # default is to expect no output.
123 # expected-stdout-pattern m A perl pattern which matches the
125 # expected-stderr m What the test should generate on stderr;
126 # default is to expect no output.
127 # expected-stderr-pattern m A perl pattern which matches the
128 # expected standard error.
129 # category m Specify a comma separated list of
130 # 'categories' of program that the test
131 # is to be run for. A category can be
132 # negated by prefixing the name with a !.
133 # The idea is that some tests in a
134 # test suite may apply to a particular
135 # program version and shouldn't be run
136 # on other versions. The category(s) of
137 # the program being tested can be
138 # specified on the command line.
139 # One category os:XXX is predefined
140 # (XXX is the operating system name,
141 # eg, linux, dec_osf).
142 # need-ctty 'yes' if the test needs a ctty, run
143 # with -C regress:no-ctty to disable.
145 # r tag is required (eg, a test must have a name tag).
146 # m value can be multiple lines. Lines must be prefixed with
147 # a tab. If the value part of the initial tag:value line is
148 # - empty: the initial blank line is stripped.
149 # - a lone !: the last newline in the value is stripped;
150 # M value can be multiple lines (prefixed by a tab) and consists
151 # of multiple fields, delimited by a field separator character.
152 # The value must start and end with the f-s-c.
153 # p tag takes parameters (used with m).
154 # s tag can be used several times.
156 # require Config only if it exists
157 # pull EINTR from POSIX.pm or Errno.pm if they exist
158 # otherwise just skip it
168 $EINTR = POSIX::EINTR();
173 $EINTR = Errno::EINTR();
182 $os = defined $^O ? $^O : 'unknown';
184 ($prog = $0) =~ s#.*/##;
187 Usage: $prog [-Pv] [-C cat] [-e e=v] [-p prog] [-s fn] [-T dir] \
188 [-t tmo] [-U lcl] name ...
189 -C c Specify the comma separated list of categories the program
190 belongs to (see category field).
191 -e e=v Set the environment variable e to v for all tests
192 (if no =v is given, the current value is used)
193 Only one -e option can be given at the moment, sadly.
194 -P program (-p) string has multiple words, and the program is in
195 the path (kludge option)
196 -p p Use p as the program to test
197 -s s Read tests from file s; if s is a directory, it is recursively
198 scaned for test files (which end in .t).
199 -T dir Use dir instead of /tmp to hold temporary files
200 -t t Use t as default time limit for tests (default is unlimited)
201 -U lcl Use lcl as UTF-8 locale (e.g. C.UTF-8) instead of the default
202 -v Verbose mode: print reason test failed.
203 name specifies the name of the test(s) to run; if none are
204 specified, all tests are run.
207 # See comment above for flag meanings
218 'file-result', 'mps',
222 'expected-stdout', 'm',
223 'expected-stdout-pattern', 'm',
224 'expected-stderr', 'm',
225 'expected-stderr-pattern', 'm',
230 # Filled in by read_test()
231 %internal_test_fields = (
232 ':full-name', 1, # file:name
233 ':long-name', 1, # dir/file:lineno:name
236 # Categories of the program under test. Provide the current
250 if (!getopts('C:Ee:Pp:s:T:t:U:v')) {
255 die "$prog: no program specified (use -p)\n" if !defined $opt_p;
256 die "$prog: no test set specified (use -s)\n" if !defined $opt_s;
258 $verbose = defined $opt_v && $opt_v;
259 $is_ebcdic = defined $opt_E && $opt_E;
261 $temp_base = $opt_T || "/tmp";
262 $utflocale = $opt_U || (($os eq "hpux") ? "en_US.utf8" : "en_US.UTF-8");
263 if (defined $opt_t) {
264 die "$prog: bad -t argument (should be number > 0): $opt_t\n"
265 if $opt_t !~ /^\d+$/ || $opt_t <= 0;
266 $default_time_limit = $opt_t;
268 $program_kludge = defined $opt_P ? $opt_P : 0;
271 $categories{'shell:ebcdic-yes'} = 1;
272 $categories{'shell:ascii-no'} = 1;
274 $categories{'shell:ebcdic-no'} = 1;
275 $categories{'shell:ascii-yes'} = 1;
278 if (defined $opt_C) {
279 foreach $c (split(',', $opt_C)) {
281 die "$prog: categories can't be negated on the command line\n"
287 # Note which tests are to be run.
289 grep($do_test{$_} = 1, @ARGV);
290 $all_tests = @ARGV == 0;
292 # Set up a very minimal environment
294 foreach $env (('HOME', 'LD_LIBRARY_PATH', 'LOCPATH', 'LOGNAME',
295 'PATH', 'SHELL', 'UNIXMODE', 'UNIXROOT', 'USER')) {
296 $new_env{$env} = $ENV{$env} if defined $ENV{$env};
298 $new_env{'CYGWIN'} = 'nodosfilewarning';
299 $new_env{'ENV'} = '/nonexistant';
301 if (($os eq 'VMS') || ($Config{perlpath} =~ m/$Config{_exe}$/i)) {
302 $new_env{'__perlname'} = $Config{perlpath};
304 $new_env{'__perlname'} = $Config{perlpath} . $Config{_exe};
306 $new_env{'__perlname'} = $^X if ($new_env{'__perlname'} eq '') and -f $^X and -x $^X;
307 if ($new_env{'__perlname'} eq '') {
308 foreach $pathelt (split /:/,$ENV{'PATH'}) {
309 chomp($pathelt = `pwd`) if $pathelt eq '';
310 my $x = $pathelt . '/' . $^X;
311 next unless -f $x and -x $x;
312 $new_env{'__perlname'} = $x;
316 $new_env{'__perlname'} = $^X if ($new_env{'__perlname'} eq '');
318 if (defined $opt_e) {
319 # XXX need a way to allow many -e arguments...
320 if ($opt_e =~ /^([a-zA-Z_]\w*)(|=(.*))$/) {
321 $new_env{$1} = $2 eq '' ? $ENV{$1} : $3;
323 die "$0: bad -e argument: $opt_e\n";
328 chop($pwd = `pwd 2>/dev/null`);
329 die "$prog: couldn't get current working directory\n" if $pwd eq '';
330 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
332 die "$prog: couldn't cd to $temp_base - $!\n" if !chdir($temp_base);
333 die "$prog: couldn't get temporary directory base\n" unless -d '.';
334 $temps = sprintf("chk%d-%d.", $$, time());
336 until (mkdir(($tempdir = sprintf("%s%03d", $temps, $tempi)), 0700)) {
337 die "$prog: couldn't get temporary directory\n" if $tempi++ >= 999;
339 die "$prog: couldn't cd to $tempdir - $!\n" if !chdir($tempdir);
340 chop($temp_dir = `pwd 2>/dev/null`);
341 die "$prog: couldn't get temporary directory\n" if $temp_dir eq '';
342 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
344 if (!$program_kludge) {
345 $test_prog = "$pwd/$test_prog" if (substr($test_prog, 0, 1) ne '/') &&
346 ($os ne 'os2' || substr($test_prog, 1, 1) ne ':');
347 die "$prog: $test_prog is not executable - bye\n"
348 if (! -x $test_prog && $os ne 'os2');
351 @trap_sigs = ('TERM', 'QUIT', 'INT', 'PIPE', 'HUP');
352 @SIG{@trap_sigs} = ('cleanup_exit') x @trap_sigs;
354 $SIG{'ALRM'} = 'catch_sigalrm';
359 $temps = "${temp_dir}/rts";
360 $tempi = "${temp_dir}/rti";
361 $tempo = "${temp_dir}/rto";
362 $tempe = "${temp_dir}/rte";
363 $tempdir = "${temp_dir}/rtd";
364 mkdir($tempdir, 0700) or die "$prog: couldn't mkdir $tempdir - $!\n";
367 $file_prefix_skip = length($test_set) + 1;
368 $ret = &process_test_dir($test_set);
370 $file_prefix_skip = 0;
371 $ret = &process_test_file($test_set);
373 &cleanup_exit() if !defined $ret;
375 $tot_failed = $nfailed + $nifailed + $nxfailed;
376 $tot_passed = $npassed + $nxpassed;
377 if ($tot_failed || $tot_passed) {
378 print "Total failed: $tot_failed";
379 print " ($nifailed ignored)" if $nifailed;
380 print " ($nxfailed unexpected)" if $nxfailed;
381 print " (as expected)" if $nfailed && !$nxfailed && !$nifailed;
382 print " ($nfailed expected)" if $nfailed && ($nxfailed || $nifailed);
383 print "\nTotal passed: $tot_passed";
384 print " ($nxpassed unexpected)" if $nxpassed;
393 local($sig, $exitcode) = ('', 1);
401 } elsif ($_[0] ne '') {
405 unlink($tempi, $tempo, $tempe, $temps);
406 &scrub_dir($tempdir) if defined $tempdir;
407 rmdir($tempdir) if defined $tempdir;
408 rmdir($temp_dir) if defined $temp_dir;
411 $SIG{$sig} = 'DEFAULT';
421 $SIG{'ALRM'} = 'catch_sigalrm';
422 kill(9, $child_pid) if $child_kill_ok;
433 if (!opendir(DIR, $dir)) {
434 print STDERR "$prog: can't open directory $dir - $!\n";
437 while (defined ($file = readdir(DIR))) {
438 push(@todo, $file) if $file =~ /^[^.].*\.t$/;
442 foreach $file (@todo) {
443 $file = "$dir/$file";
445 $ret = &process_test_dir($file);
447 $ret = &process_test_file($file);
449 last if !defined $ret;
461 if (!open(IN, $file)) {
462 print STDERR "$prog: can't open $file - $!\n";
467 $ret = &read_test($file, IN, *test);
468 last if !defined $ret || !$ret;
469 next if !$all_tests && !$do_test{$test{'name'}};
470 next if !&category_check(*test);
471 $ret = &run_test(*test);
472 last if !defined $ret;
483 local($name) = $test{':full-name'};
485 return undef if !&scrub_dir($tempdir);
487 if (defined $test{'stdin'}) {
488 return undef if !&write_file($tempi, $test{'stdin'});
491 $ifile = '/dev/null';
494 if (defined $test{'script'}) {
495 return undef if !&write_file($temps, $test{'script'});
498 if (!chdir($tempdir)) {
499 print STDERR "$prog: couldn't cd to $tempdir - $!\n";
503 if (defined $test{'file-setup'}) {
505 local($type, $perm, $rest, $c, $len, $name);
507 for ($i = 0; $i < $test{'file-setup'}; $i++) {
508 $val = $test{"file-setup:$i"};
510 # format is: type perm "name"
511 ($type, $perm, $rest) =
513 $c = substr($rest, 0, 1);
514 $len = index($rest, $c, 1) - 1;
515 $name = substr($rest, 1, $len);
516 $rest = substr($rest, 2 + $len);
517 $perm = oct($perm) if $perm =~ /^\d+$/;
518 if ($type eq 'file') {
519 return undef if !&write_file($name, $rest);
520 if (!chmod($perm, $name)) {
522 "$prog:$test{':long-name'}: can't chmod $perm $name - $!\n";
525 } elsif ($type eq 'dir') {
526 if (!mkdir($name, $perm)) {
528 "$prog:$test{':long-name'}: can't mkdir $perm $name - $!\n";
531 } elsif ($type eq 'symlink') {
532 local($oumask) = umask($perm);
533 local($ret) = symlink($rest, $name);
537 "$prog:$test{':long-name'}: couldn't create symlink $name - $!\n";
544 if (defined $test{'perl-setup'}) {
545 eval $test{'perl-setup'};
547 print STDERR "$prog:$test{':long-name'}: error running perl-setup - $@\n";
554 print STDERR "$prog: can't fork - $!\n";
558 @SIG{@trap_sigs} = ('DEFAULT') x @trap_sigs;
559 $SIG{'ALRM'} = 'DEFAULT';
560 if (defined $test{'env-setup'}) {
561 local($var, $val, $i);
563 foreach $var (split(substr($test{'env-setup'}, 0, 1),
566 $i = index($var, '=');
567 next if $i == 0 || $var eq '';
569 delete $new_env{$var};
571 $new_env{substr($var, 0, $i)} = substr($var, $i + 1);
575 if (!open(STDIN, "< $ifile")) {
576 print STDERR "$prog: couldn't open $ifile in child - $!\n";
580 if (!open(STDOUT, "> $tempo")) {
581 print STDERR "$prog: couldn't open $tempo in child - $!\n";
585 if (!open(STDERR, "> $tempe")) {
586 print STDOUT "$prog: couldn't open $tempe in child - $!\n";
590 if ($program_kludge) {
591 @argv = split(' ', $test_prog);
593 @argv = ($test_prog);
595 if (defined $test{'arguments'}) {
597 split(substr($test{'arguments'}, 0, 1),
598 substr($test{'arguments'}, 1)));
600 push(@argv, $temps) if defined $test{'script'};
602 #XXX realpathise, use command -v/whence -p/which, or sth. like that
603 #XXX if !$program_kludge, we get by with not doing it for now tho
604 $new_env{'__progname'} = $argv[0];
606 # The following doesn't work with perl5... Need to do it explicitly - yuck.
608 foreach $k (keys(%ENV)) {
611 $ENV{$k} = $v while ($k,$v) = each %new_env;
613 exec { $argv[0] } @argv;
614 print STDERR "$prog: couldn't execute $test_prog - $!\n";
621 alarm($test{'time-limit'}) if defined $test{'time-limit'};
623 $xpid = waitpid($pid, 0);
627 next if $! == $EINTR;
629 print STDERR "$prog: error waiting for child - $!\n";
635 alarm(0) if defined $test{'time-limit'};
642 $why .= "\ttest timed out (limit of $test{'time-limit'} seconds)\n";
645 $ret = &eval_exit($test{'long-name'}, $status, $test{'expected-exit'});
646 return undef if !defined $ret;
651 if (($status & 0xff) == 0x7f) {
653 } elsif (($status & 0xff)) {
654 $expl = "signal " . ($status & 0x7f);
656 $expl = "exit-code " . (($status >> 8) & 0xff);
659 "\tunexpected exit status $status ($expl), expected $test{'expected-exit'}\n";
662 $tmp = &check_output($test{'long-name'}, $tempo, 'stdout',
663 $test{'expected-stdout'}, $test{'expected-stdout-pattern'});
664 return undef if !defined $tmp;
670 $tmp = &check_output($test{'long-name'}, $tempe, 'stderr',
671 $test{'expected-stderr'}, $test{'expected-stderr-pattern'});
672 return undef if !defined $tmp;
678 $tmp = &check_file_result(*test);
679 return undef if !defined $tmp;
685 if (defined $test{'perl-cleanup'}) {
686 eval $test{'perl-cleanup'};
688 print STDERR "$prog:$test{':long-name'}: error running perl-cleanup - $@\n";
694 print STDERR "$prog: couldn't cd to $pwd - $!\n";
699 if (!$test{'expected-fail'}) {
700 if ($test{'need-pass'}) {
701 print "FAIL $name\n";
704 print "FAIL $name (ignored)\n";
708 print "fail $name (as expected)\n";
711 $why = "\tDescription"
712 . &wrap_lines($test{'description'}, " (missing)\n")
714 } elsif ($test{'expected-fail'}) {
715 print "PASS $name (unexpectedly)\n";
718 print "pass $name\n";
721 print $why if $verbose;
731 return 0 if ($test{'need-ctty'} && defined $categories{'regress:no-ctty'});
732 return 1 if (!defined $test{'category'});
734 foreach $c (split(',', $test{'category'})) {
738 return 0 if (defined $categories{$c});
741 $ok = 1 if (defined $categories{$c});
754 if (!opendir(DIR, $dir)) {
755 print STDERR "$prog: couldn't open directory $dir - $!\n";
758 while (defined ($file = readdir(DIR))) {
759 push(@todo, $file) if $file ne '.' && $file ne '..';
762 foreach $file (@todo) {
763 $file = "$dir/$file";
765 return undef if !&scrub_dir($file);
767 print STDERR "$prog: couldn't rmdir $file - $!\n";
771 if (!unlink($file)) {
772 print STDERR "$prog: couldn't unlink $file - $!\n";
783 local($file, $str) = @_;
785 if (!open(TEMP, "> $file")) {
786 print STDERR "$prog: can't open $file - $!\n";
792 print STDERR "$prog: error writing $file - $!\n";
801 local($name, $file, $what, $expect, $expect_pat) = @_;
806 if (!open(TEMP, "< $file")) {
807 print STDERR "$prog:$name($what): couldn't open $file after running program - $!\n";
815 return compare_output($name, $what, $expect, $expect_pat, $got);
821 local($name, $what, $expect, $expect_pat, $got) = @_;
824 if (defined $expect_pat) {
826 $ret = eval "$expect_pat";
828 print STDERR "$prog:$name($what): error evaluating $what pattern: $expect_pat - $@\n";
832 $why = "\tunexpected $what - wanted pattern";
833 $why .= &wrap_lines($expect_pat);
835 $why .= &wrap_lines($got);
838 $expect = '' if !defined $expect;
839 if ($got ne $expect) {
840 $why .= "\tunexpected $what - " . &first_diff($expect, $got) . "\n";
842 $why .= &wrap_lines($expect);
844 $why .= &wrap_lines($got);
853 local($str, $empty) = @_;
854 local($nonl) = substr($str, -1, 1) ne "\n";
856 return (defined $empty ? $empty : " nothing\n") if $str eq '';
857 substr($str, 0, 0) = ":\n";
858 $str =~ s/\n/\n\t\t/g;
860 $str .= "\n\t[incomplete last line]\n";
871 local($exp, $got) = @_;
872 local($lineno, $char) = (1, 1);
873 local($i, $exp_len, $got_len);
876 $exp_len = length($exp);
877 $got_len = length($got);
878 if ($exp_len != $got_len) {
879 if ($exp_len < $got_len) {
880 if (substr($got, 0, $exp_len) eq $exp) {
881 return "got too much output";
883 } elsif (substr($exp, 0, $got_len) eq $got) {
884 return "got too little output";
887 for ($i = 0; $i < $exp_len; $i++) {
888 $ce = substr($exp, $i, 1);
889 $cg = substr($got, $i, 1);
897 return "first difference: line $lineno, char $char (wanted " .
898 &format_char($ce) . ", got " . &format_char($cg);
911 return $q . '\n' . $q;
912 } elsif ($ch == 0x16) {
913 return $q . '\b' . $q;
914 } elsif ($ch == 0x05) {
915 return $q . '\t' . $q;
916 } elsif ($ch < 64 || $ch == 255) {
917 return sprintf("X'%02X'", $ch);
919 return sprintf("'%c' (X'%02X')", $ch, $ch);
922 $s = sprintf("0x%02X (", $ch);
924 return $s . $q . '\n' . $q . ')';
925 } elsif ($ch == 13) {
926 return $s . $q . '\r' . $q . ')';
928 return $s . $q . '\b' . $q . ')';
930 return $s . $q . '\t' . $q . ')';
931 } elsif ($ch > 127) {
936 return sprintf("%s^%c)", $s, $ch + ord('@'));
937 } elsif ($ch == 127) {
940 return sprintf("%s'%c')", $s, $ch);
946 local($name, $status, $expect) = @_;
948 local($w, $e, $s) = ($status, ($status >> 8) & 0xff, $status & 0x7f);
950 $e = -1000 if $status & 0xff;
951 $s = -1000 if $s == 0x7f;
952 if (!defined $expect) {
954 } elsif ($expect =~ /^(|-)\d+$/) {
955 $expr = "\$e == $expect";
958 $expr =~ s/\b([wse])\b/\$$1/g;
959 $expr =~ s/\b(SIG[A-Z][A-Z0-9]*)\b/&$1/g;
963 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $expect ($@)\n";
972 local($file, $in, *test) = @_;
973 local($field, $val, $flags, $do_chop, $need_redo, $start_lineno);
974 local(%cnt, $sfield);
982 last if /^\s*---\s*$/;
983 $start_lineno = $. if !defined $start_lineno;
984 if (!/^([-\w]+):\s*(|\S|\S.*\S)\s*$/) {
985 print STDERR "$prog:$file:$.: unrecognised line \"$_\"\n";
988 ($field, $val) = ($1, $2);
990 $flags = $test_fields{$field};
991 if (!defined $flags) {
992 print STDERR "$prog:$file:$.: unrecognised field \"$field\"\n";
996 local($cnt) = $cnt{$field}++;
997 $test{$field} = $cnt{$field};
998 $cnt = 0 if $cnt eq '';
1000 } elsif (defined $test{$field}) {
1001 print STDERR "$prog:$file:$.: multiple \"$field\" fields\n";
1004 $do_chop = $flags !~ /m/;
1006 if ($val eq '' || $val eq '!' || $flags =~ /p/) {
1007 if ($flags =~ /[Mm]/) {
1008 if ($flags =~ /p/) {
1017 "$prog:$file:$.: no parameters given for field \"$field\"\n";
1030 chop $val if $do_chop;
1034 # Syntax check on fields that can several instances
1035 # (can give useful line numbers this way)
1037 if ($field eq 'file-setup') {
1038 local($type, $perm, $rest, $c, $len, $name);
1040 # format is: type perm "name"
1041 if ($val !~ /^[ \t]*(\S+)[ \t]+(\S+)[ \t]+([^ \t].*)/) {
1043 "$prog:$file:$.: bad parameter line for file-setup field\n";
1046 ($type, $perm, $rest) = ($1, $2, $3);
1047 if ($type !~ /^(file|dir|symlink)$/) {
1049 "$prog:$file:$.: bad file type for file-setup: $type\n";
1052 if ($perm !~ /^\d+$/) {
1054 "$prog:$file:$.: bad permissions for file-setup: $type\n";
1057 $c = substr($rest, 0, 1);
1058 if (($len = index($rest, $c, 1) - 1) <= 0) {
1060 "$prog:$file:$.: missing end quote for file name in file-setup: $rest\n";
1063 $name = substr($rest, 1, $len);
1064 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1065 # Note: this is not a security thing - just a sanity
1066 # check - a test can still use symlinks to get at files
1067 # outside the test directory.
1069 "$prog:$file:$.: file name in file-setup is absolute or contains ..: $name\n";
1073 if ($field eq 'file-result') {
1074 local($type, $perm, $uid, $gid, $matchType,
1075 $rest, $c, $len, $name);
1077 # format is: type perm uid gid matchType "name"
1078 if ($val !~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/) {
1080 "$prog:$file:$.: bad parameter line for file-result field\n";
1083 ($type, $perm, $uid, $gid, $matchType, $rest)
1084 = ($1, $2, $3, $4, $5, $6);
1085 if ($type !~ /^(file|dir|symlink)$/) {
1087 "$prog:$file:$.: bad file type for file-result: $type\n";
1090 if ($perm !~ /^\d+$/ && $perm ne '*') {
1092 "$prog:$file:$.: bad permissions for file-result: $perm\n";
1095 if ($uid !~ /^\d+$/ && $uid ne '*') {
1097 "$prog:$file:$.: bad user-id for file-result: $uid\n";
1100 if ($gid !~ /^\d+$/ && $gid ne '*') {
1102 "$prog:$file:$.: bad group-id for file-result: $gid\n";
1105 if ($matchType !~ /^(exact|pattern)$/) {
1107 "$prog:$file:$.: bad match type for file-result: $matchType\n";
1110 $c = substr($rest, 0, 1);
1111 if (($len = index($rest, $c, 1) - 1) <= 0) {
1113 "$prog:$file:$.: missing end quote for file name in file-result: $rest\n";
1116 $name = substr($rest, 1, $len);
1117 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1118 # Note: this is not a security thing - just a sanity
1119 # check - a test can still use symlinks to get at files
1120 # outside the test directory.
1122 "$prog:$file:$.: file name in file-result is absolute or contains ..: $name\n";
1126 } elsif ($val eq '') {
1128 "$prog:$file:$.: no value given for field \"$field\"\n";
1132 $val .= "\n" if !$do_chop;
1133 $test{$sfield} = $val;
1139 "$prog:$file:$start_lineno: end-of-file while reading test\n";
1145 while (($field, $val) = each %test_fields) {
1146 if ($val =~ /r/ && !defined $test{$field}) {
1148 "$prog:$file:$start_lineno: required field \"$field\" missing\n";
1153 $test{':full-name'} = substr($file, $file_prefix_skip) . ":$test{'name'}";
1154 $test{':long-name'} = "$file:$start_lineno:$test{'name'}";
1156 # Syntax check on specific fields
1157 if (defined $test{'expected-fail'}) {
1158 if ($test{'expected-fail'} !~ /^(yes|no)$/) {
1160 "$prog:$test{':long-name'}: bad value for expected-fail field\n";
1163 $test{'expected-fail'} = $1 eq 'yes';
1165 $test{'expected-fail'} = 0;
1167 if (defined $test{'need-ctty'}) {
1168 if ($test{'need-ctty'} !~ /^(yes|no)$/) {
1170 "$prog:$test{':long-name'}: bad value for need-ctty field\n";
1173 $test{'need-ctty'} = $1 eq 'yes';
1175 $test{'need-ctty'} = 0;
1177 if (defined $test{'need-pass'}) {
1178 if ($test{'need-pass'} !~ /^(yes|no)$/) {
1180 "$prog:$test{':long-name'}: bad value for need-pass field\n";
1183 $test{'need-pass'} = $1 eq 'yes';
1185 $test{'need-pass'} = 1;
1187 if (defined $test{'arguments'}) {
1188 local($firstc) = substr($test{'arguments'}, 0, 1);
1190 if (substr($test{'arguments'}, -1, 1) ne $firstc) {
1191 print STDERR "$prog:$test{':long-name'}: arguments field doesn't start and end with the same character\n";
1195 if (defined $test{'env-setup'}) {
1196 local($firstc) = substr($test{'env-setup'}, 0, 1);
1198 if (substr($test{'env-setup'}, -1, 1) ne $firstc) {
1199 print STDERR "$prog:$test{':long-name'}: env-setup field doesn't start and end with the same character\n";
1203 $test{'env-setup'} =~ s/\@utflocale\@/$utflocale/g;
1205 if (defined $test{'expected-exit'}) {
1206 local($val) = $test{'expected-exit'};
1208 if ($val =~ /^(|-)\d+$/) {
1209 if ($val < 0 || $val > 255) {
1210 print STDERR "$prog:$test{':long-name'}: expected-exit value $val not in 0..255\n";
1213 } elsif ($val !~ /^([\s\d<>+=*%\/&|!()-]|\b[wse]\b|\bSIG[A-Z][A-Z0-9]*\b)+$/) {
1214 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $val\n";
1218 $test{'expected-exit'} = 0;
1220 if (defined $test{'expected-stdout'}
1221 && defined $test{'expected-stdout-pattern'})
1223 print STDERR "$prog:$test{':long-name'}: can't use both expected-stdout and expected-stdout-pattern\n";
1226 if (defined $test{'expected-stderr'}
1227 && defined $test{'expected-stderr-pattern'})
1229 print STDERR "$prog:$test{':long-name'}: can't use both expected-stderr and expected-stderr-pattern\n";
1232 if (defined $test{'time-limit'}) {
1233 if ($test{'time-limit'} !~ /^\d+$/ || $test{'time-limit'} == 0) {
1235 "$prog:$test{':long-name'}: bad value for time-limit field\n";
1238 } elsif (defined $default_time_limit) {
1239 $test{'time-limit'} = $default_time_limit;
1242 if (defined $known_tests{$test{'name'}}) {
1243 print STDERR "$prog:$test{':long-name'}: warning: duplicate test name ${test{'name'}}\n";
1245 $known_tests{$test{'name'}} = 1;
1255 open(TTY, "> /dev/tty") || return 0;
1266 &never_called_funcs();
1268 $old_env{'foo'} = 'bar';
1269 $internal_test_fields{'foo'} = 'bar';
1277 return '' if (!defined $test{'file-result'});
1281 local($type, $perm, $uid, $gid, $rest, $c, $len, $name);
1284 for ($i = 0; $i < $test{'file-result'}; $i++) {
1285 $val = $test{"file-result:$i"};
1287 # format is: type perm "name"
1288 ($type, $perm, $uid, $gid, $matchType, $rest) =
1289 split(' ', $val, 6);
1290 $c = substr($rest, 0, 1);
1291 $len = index($rest, $c, 1) - 1;
1292 $name = substr($rest, 1, $len);
1293 $rest = substr($rest, 2 + $len);
1294 $perm = oct($perm) if $perm =~ /^\d+$/;
1296 @stbuf = lstat($name);
1298 $why .= "\texpected $type \"$name\" not created\n";
1301 if ($perm ne '*' && ($stbuf[2] & 07777) != $perm) {
1302 $why .= "\t$type \"$name\" has unexpected permissions\n";
1303 $why .= sprintf("\t\texpected 0%o, found 0%o\n",
1304 $perm, $stbuf[2] & 07777);
1306 if ($uid ne '*' && $stbuf[4] != $uid) {
1307 $why .= "\t$type \"$name\" has unexpected user-id\n";
1308 $why .= sprintf("\t\texpected %d, found %d\n",
1311 if ($gid ne '*' && $stbuf[5] != $gid) {
1312 $why .= "\t$type \"$name\" has unexpected group-id\n";
1313 $why .= sprintf("\t\texpected %d, found %d\n",
1317 if ($type eq 'file') {
1318 if (-l _ || ! -f _) {
1319 $why .= "\t$type \"$name\" is not a regular file\n";
1321 local $tmp = &check_output($test{'long-name'}, $name,
1322 "$type contents in \"$name\"",
1323 $matchType eq 'exact' ? $rest : undef
1324 $matchType eq 'pattern' ? $rest : undef);
1325 return undef if (!defined $tmp);
1328 } elsif ($type eq 'dir') {
1329 if ($rest !~ /^\s*$/) {
1330 print STDERR "$prog:$test{':long-name'}: file-result test for directory $name should not have content specified\n";
1333 if (-l _ || ! -d _) {
1334 $why .= "\t$type \"$name\" is not a directory\n";
1336 } elsif ($type eq 'symlink') {
1338 $why .= "\t$type \"$name\" is not a symlink\n";
1340 local $content = readlink($name);
1341 if (!defined $content) {
1342 print STDERR "$prog:$test{':long-name'}: file-result test for $type $name failed - could not readlink - $!\n";
1345 local $tmp = &compare_output($test{'long-name'},
1346 "$type contents in \"$name\"",
1347 $matchType eq 'exact' ? $rest : undef
1348 $matchType eq 'pattern' ? $rest : undef);
1349 return undef if (!defined $tmp);
1361 print STDERR $Usage;