1 # $MirOS: src/bin/mksh/check.pl,v 1.42 2015/11/29 17:05:00 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
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 # PATHSEP is set to either : or ;.
82 # __progname is set to the -p argument.
83 # __perlname is set to $^X (perlexe).
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 # pull EINTR from POSIX.pm or Errno.pm if they exist
157 # otherwise just skip it
162 $EINTR = POSIX::EINTR();
167 $EINTR = Errno::EINTR();
177 $os = defined $^O ? $^O : 'unknown';
179 ($prog = $0) =~ s#.*/##;
182 Usage: $prog [-Pv] [-C cat] [-e e=v] [-p prog] [-s fn] [-T dir] \
184 -C c Specify the comma separated list of categories the program
185 belongs to (see category field).
186 -e e=v Set the environment variable e to v for all tests
187 (if no =v is given, the current value is used)
188 Only one -e option can be given at the moment, sadly.
189 -P program (-p) string has multiple words, and the program is in
190 the path (kludge option)
191 -p p Use p as the program to test
192 -s s Read tests from file s; if s is a directory, it is recursively
193 scaned for test files (which end in .t).
194 -T dir Use dir instead of /tmp to hold temporary files
195 -t t Use t as default time limit for tests (default is unlimited)
196 -v Verbose mode: print reason test failed.
197 name specifies the name of the test(s) to run; if none are
198 specified, all tests are run.
201 # See comment above for flag meanings
212 'file-result', 'mps',
216 'expected-stdout', 'm',
217 'expected-stdout-pattern', 'm',
218 'expected-stderr', 'm',
219 'expected-stderr-pattern', 'm',
224 # Filled in by read_test()
225 %internal_test_fields = (
226 ':full-name', 1, # file:name
227 ':long-name', 1, # dir/file:lineno:name
230 # Categories of the program under test. Provide the current
244 if (!getopts('C:e:Pp:s:T:t:v')) {
249 die "$prog: no program specified (use -p)\n" if !defined $opt_p;
250 die "$prog: no test set specified (use -s)\n" if !defined $opt_s;
252 $verbose = defined $opt_v && $opt_v;
254 $temp_base = $opt_T || "/tmp";
255 if (defined $opt_t) {
256 die "$prog: bad -t argument (should be number > 0): $opt_t\n"
257 if $opt_t !~ /^\d+$/ || $opt_t <= 0;
258 $default_time_limit = $opt_t;
260 $program_kludge = defined $opt_P ? $opt_P : 0;
262 if (defined $opt_C) {
263 foreach $c (split(',', $opt_C)) {
265 die "$prog: categories can't be negated on the command line\n"
271 # Note which tests are to be run.
273 grep($do_test{$_} = 1, @ARGV);
274 $all_tests = @ARGV == 0;
276 # Set up a very minimal environment
278 foreach $env (('HOME', 'LD_LIBRARY_PATH', 'LOCPATH', 'LOGNAME',
279 'PATH', 'SHELL', 'UNIXMODE', 'UNIXROOT', 'USER')) {
280 $new_env{$env} = $ENV{$env} if defined $ENV{$env};
282 $new_env{'CYGWIN'} = 'nodosfilewarning';
283 $new_env{'ENV'} = '/nonexistant';
284 $new_env{'PATHSEP'} = $os eq 'os2' ? ';' : ':';
285 if (($os eq 'VMS') || ($Config{perlpath} =~ m/$Config{_exe}$/i)) {
286 $new_env{'__perlname'} = $Config{perlpath};
288 $new_env{'__perlname'} = $Config{perlpath} . $Config{_exe};
290 if (defined $opt_e) {
291 # XXX need a way to allow many -e arguments...
292 if ($opt_e =~ /^([a-zA-Z_]\w*)(|=(.*))$/) {
293 $new_env{$1} = $2 eq '' ? $ENV{$1} : $3;
295 die "$0: bad -e argument: $opt_e\n";
300 chop($pwd = `pwd 2>/dev/null`);
301 die "$prog: couldn't get current working directory\n" if $pwd eq '';
302 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
304 die "$prog: couldn't cd to $temp_base - $!\n" if !chdir($temp_base);
305 die "$prog: couldn't get temporary directory base\n" unless -d '.';
306 $temps = sprintf("chk%d-%d.", $$, time());
308 until (mkdir(($tempdir = sprintf("%s%03d", $temps, $tempi)), 0700)) {
309 die "$prog: couldn't get temporary directory\n" if $tempi++ >= 999;
311 die "$prog: couldn't cd to $tempdir - $!\n" if !chdir($tempdir);
312 chop($temp_dir = `pwd 2>/dev/null`);
313 die "$prog: couldn't get temporary directory\n" if $temp_dir eq '';
314 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
316 if (!$program_kludge) {
317 $test_prog = "$pwd/$test_prog" if (substr($test_prog, 0, 1) ne '/') &&
318 ($os ne 'os2' || substr($test_prog, 1, 1) ne ':');
319 die "$prog: $test_prog is not executable - bye\n"
320 if (! -x $test_prog && $os ne 'os2');
323 @trap_sigs = ('TERM', 'QUIT', 'INT', 'PIPE', 'HUP');
324 @SIG{@trap_sigs} = ('cleanup_exit') x @trap_sigs;
326 $SIG{'ALRM'} = 'catch_sigalrm';
331 $temps = "${temp_dir}/rts";
332 $tempi = "${temp_dir}/rti";
333 $tempo = "${temp_dir}/rto";
334 $tempe = "${temp_dir}/rte";
335 $tempdir = "${temp_dir}/rtd";
336 mkdir($tempdir, 0700) or die "$prog: couldn't mkdir $tempdir - $!\n";
339 $file_prefix_skip = length($test_set) + 1;
340 $ret = &process_test_dir($test_set);
342 $file_prefix_skip = 0;
343 $ret = &process_test_file($test_set);
345 &cleanup_exit() if !defined $ret;
347 $tot_failed = $nfailed + $nifailed + $nxfailed;
348 $tot_passed = $npassed + $nxpassed;
349 if ($tot_failed || $tot_passed) {
350 print "Total failed: $tot_failed";
351 print " ($nifailed ignored)" if $nifailed;
352 print " ($nxfailed unexpected)" if $nxfailed;
353 print " (as expected)" if $nfailed && !$nxfailed && !$nifailed;
354 print " ($nfailed expected)" if $nfailed && ($nxfailed || $nifailed);
355 print "\nTotal passed: $tot_passed";
356 print " ($nxpassed unexpected)" if $nxpassed;
365 local($sig, $exitcode) = ('', 1);
373 } elsif ($_[0] ne '') {
377 unlink($tempi, $tempo, $tempe, $temps);
378 &scrub_dir($tempdir) if defined $tempdir;
379 rmdir($tempdir) if defined $tempdir;
380 rmdir($temp_dir) if defined $temp_dir;
383 $SIG{$sig} = 'DEFAULT';
393 $SIG{'ALRM'} = 'catch_sigalrm';
394 kill(9, $child_pid) if $child_kill_ok;
405 if (!opendir(DIR, $dir)) {
406 print STDERR "$prog: can't open directory $dir - $!\n";
409 while (defined ($file = readdir(DIR))) {
410 push(@todo, $file) if $file =~ /^[^.].*\.t$/;
414 foreach $file (@todo) {
415 $file = "$dir/$file";
417 $ret = &process_test_dir($file);
419 $ret = &process_test_file($file);
421 last if !defined $ret;
433 if (!open(IN, $file)) {
434 print STDERR "$prog: can't open $file - $!\n";
439 $ret = &read_test($file, IN, *test);
440 last if !defined $ret || !$ret;
441 next if !$all_tests && !$do_test{$test{'name'}};
442 next if !&category_check(*test);
443 $ret = &run_test(*test);
444 last if !defined $ret;
455 local($name) = $test{':full-name'};
457 return undef if !&scrub_dir($tempdir);
459 if (defined $test{'stdin'}) {
460 return undef if !&write_file($tempi, $test{'stdin'});
463 $ifile = '/dev/null';
466 if (defined $test{'script'}) {
467 return undef if !&write_file($temps, $test{'script'});
470 if (!chdir($tempdir)) {
471 print STDERR "$prog: couldn't cd to $tempdir - $!\n";
475 if (defined $test{'file-setup'}) {
477 local($type, $perm, $rest, $c, $len, $name);
479 for ($i = 0; $i < $test{'file-setup'}; $i++) {
480 $val = $test{"file-setup:$i"};
482 # format is: type perm "name"
483 ($type, $perm, $rest) =
485 $c = substr($rest, 0, 1);
486 $len = index($rest, $c, 1) - 1;
487 $name = substr($rest, 1, $len);
488 $rest = substr($rest, 2 + $len);
489 $perm = oct($perm) if $perm =~ /^\d+$/;
490 if ($type eq 'file') {
491 return undef if !&write_file($name, $rest);
492 if (!chmod($perm, $name)) {
494 "$prog:$test{':long-name'}: can't chmod $perm $name - $!\n";
497 } elsif ($type eq 'dir') {
498 if (!mkdir($name, $perm)) {
500 "$prog:$test{':long-name'}: can't mkdir $perm $name - $!\n";
503 } elsif ($type eq 'symlink') {
504 local($oumask) = umask($perm);
505 local($ret) = symlink($rest, $name);
509 "$prog:$test{':long-name'}: couldn't create symlink $name - $!\n";
516 if (defined $test{'perl-setup'}) {
517 eval $test{'perl-setup'};
519 print STDERR "$prog:$test{':long-name'}: error running perl-setup - $@\n";
526 print STDERR "$prog: can't fork - $!\n";
530 @SIG{@trap_sigs} = ('DEFAULT') x @trap_sigs;
531 $SIG{'ALRM'} = 'DEFAULT';
532 if (defined $test{'env-setup'}) {
533 local($var, $val, $i);
535 foreach $var (split(substr($test{'env-setup'}, 0, 1),
538 $i = index($var, '=');
539 next if $i == 0 || $var eq '';
541 delete $new_env{$var};
543 $new_env{substr($var, 0, $i)} = substr($var, $i + 1);
547 if (!open(STDIN, "< $ifile")) {
548 print STDERR "$prog: couldn't open $ifile in child - $!\n";
552 if (!open(STDOUT, "> $tempo")) {
553 print STDERR "$prog: couldn't open $tempo in child - $!\n";
557 if (!open(STDERR, "> $tempe")) {
558 print STDOUT "$prog: couldn't open $tempe in child - $!\n";
562 if ($program_kludge) {
563 @argv = split(' ', $test_prog);
565 @argv = ($test_prog);
567 if (defined $test{'arguments'}) {
569 split(substr($test{'arguments'}, 0, 1),
570 substr($test{'arguments'}, 1)));
572 push(@argv, $temps) if defined $test{'script'};
574 #XXX realpathise, use command -v/whence -p/which, or sth. like that
575 #XXX if !$program_kludge, we get by with not doing it for now tho
576 $new_env{'__progname'} = $argv[0];
578 # The following doesn't work with perl5... Need to do it explicitly - yuck.
580 foreach $k (keys(%ENV)) {
583 $ENV{$k} = $v while ($k,$v) = each %new_env;
585 exec { $argv[0] } @argv;
586 print STDERR "$prog: couldn't execute $test_prog - $!\n";
593 alarm($test{'time-limit'}) if defined $test{'time-limit'};
595 $xpid = waitpid($pid, 0);
599 next if $! == $EINTR;
601 print STDERR "$prog: error waiting for child - $!\n";
607 alarm(0) if defined $test{'time-limit'};
614 $why .= "\ttest timed out (limit of $test{'time-limit'} seconds)\n";
617 $ret = &eval_exit($test{'long-name'}, $status, $test{'expected-exit'});
618 return undef if !defined $ret;
623 if (($status & 0xff) == 0x7f) {
625 } elsif (($status & 0xff)) {
626 $expl = "signal " . ($status & 0x7f);
628 $expl = "exit-code " . (($status >> 8) & 0xff);
631 "\tunexpected exit status $status ($expl), expected $test{'expected-exit'}\n";
634 $tmp = &check_output($test{'long-name'}, $tempo, 'stdout',
635 $test{'expected-stdout'}, $test{'expected-stdout-pattern'});
636 return undef if !defined $tmp;
642 $tmp = &check_output($test{'long-name'}, $tempe, 'stderr',
643 $test{'expected-stderr'}, $test{'expected-stderr-pattern'});
644 return undef if !defined $tmp;
650 $tmp = &check_file_result(*test);
651 return undef if !defined $tmp;
657 if (defined $test{'perl-cleanup'}) {
658 eval $test{'perl-cleanup'};
660 print STDERR "$prog:$test{':long-name'}: error running perl-cleanup - $@\n";
666 print STDERR "$prog: couldn't cd to $pwd - $!\n";
671 if (!$test{'expected-fail'}) {
672 if ($test{'need-pass'}) {
673 print "FAIL $name\n";
676 print "FAIL $name (ignored)\n";
680 print "fail $name (as expected)\n";
683 $why = "\tDescription"
684 . &wrap_lines($test{'description'}, " (missing)\n")
686 } elsif ($test{'expected-fail'}) {
687 print "PASS $name (unexpectedly)\n";
690 print "pass $name\n";
693 print $why if $verbose;
703 return 0 if ($test{'need-ctty'} && defined $categories{'regress:no-ctty'});
704 return 1 if (!defined $test{'category'});
706 foreach $c (split(',', $test{'category'})) {
710 return 0 if (defined $categories{$c});
713 $ok = 1 if (defined $categories{$c});
726 if (!opendir(DIR, $dir)) {
727 print STDERR "$prog: couldn't open directory $dir - $!\n";
730 while (defined ($file = readdir(DIR))) {
731 push(@todo, $file) if $file ne '.' && $file ne '..';
734 foreach $file (@todo) {
735 $file = "$dir/$file";
737 return undef if !&scrub_dir($file);
739 print STDERR "$prog: couldn't rmdir $file - $!\n";
743 if (!unlink($file)) {
744 print STDERR "$prog: couldn't unlink $file - $!\n";
755 local($file, $str) = @_;
757 if (!open(TEMP, "> $file")) {
758 print STDERR "$prog: can't open $file - $!\n";
764 print STDERR "$prog: error writing $file - $!\n";
773 local($name, $file, $what, $expect, $expect_pat) = @_;
778 if (!open(TEMP, "< $file")) {
779 print STDERR "$prog:$name($what): couldn't open $file after running program - $!\n";
787 return compare_output($name, $what, $expect, $expect_pat, $got);
793 local($name, $what, $expect, $expect_pat, $got) = @_;
796 if (defined $expect_pat) {
798 $ret = eval "$expect_pat";
800 print STDERR "$prog:$name($what): error evaluating $what pattern: $expect_pat - $@\n";
804 $why = "\tunexpected $what - wanted pattern";
805 $why .= &wrap_lines($expect_pat);
807 $why .= &wrap_lines($got);
810 $expect = '' if !defined $expect;
811 if ($got ne $expect) {
812 $why .= "\tunexpected $what - " . &first_diff($expect, $got) . "\n";
814 $why .= &wrap_lines($expect);
816 $why .= &wrap_lines($got);
825 local($str, $empty) = @_;
826 local($nonl) = substr($str, -1, 1) ne "\n";
828 return (defined $empty ? $empty : " nothing\n") if $str eq '';
829 substr($str, 0, 0) = ":\n";
830 $str =~ s/\n/\n\t\t/g;
832 $str .= "\n\t[incomplete last line]\n";
843 local($exp, $got) = @_;
844 local($lineno, $char) = (1, 1);
845 local($i, $exp_len, $got_len);
848 $exp_len = length($exp);
849 $got_len = length($got);
850 if ($exp_len != $got_len) {
851 if ($exp_len < $got_len) {
852 if (substr($got, 0, $exp_len) eq $exp) {
853 return "got too much output";
855 } elsif (substr($exp, 0, $got_len) eq $got) {
856 return "got too little output";
859 for ($i = 0; $i < $exp_len; $i++) {
860 $ce = substr($exp, $i, 1);
861 $cg = substr($got, $i, 1);
869 return "first difference: line $lineno, char $char (wanted '"
870 . &format_char($ce) . "', got '"
871 . &format_char($cg) . "'";
882 } elsif ($ch == 13) {
888 } elsif ($ch > 127) {
897 } elsif ($ch == 127) {
900 return $s . sprintf("%c", $ch);
906 local($name, $status, $expect) = @_;
908 local($w, $e, $s) = ($status, ($status >> 8) & 0xff, $status & 0x7f);
910 $e = -1000 if $status & 0xff;
911 $s = -1000 if $s == 0x7f;
912 if (!defined $expect) {
914 } elsif ($expect =~ /^(|-)\d+$/) {
915 $expr = "\$e == $expect";
918 $expr =~ s/\b([wse])\b/\$$1/g;
919 $expr =~ s/\b(SIG[A-Z][A-Z0-9]*)\b/&$1/g;
923 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $expect ($@)\n";
932 local($file, $in, *test) = @_;
933 local($field, $val, $flags, $do_chop, $need_redo, $start_lineno);
934 local(%cnt, $sfield);
942 last if /^\s*---\s*$/;
943 $start_lineno = $. if !defined $start_lineno;
944 if (!/^([-\w]+):\s*(|\S|\S.*\S)\s*$/) {
945 print STDERR "$prog:$file:$.: unrecognised line \"$_\"\n";
948 ($field, $val) = ($1, $2);
950 $flags = $test_fields{$field};
951 if (!defined $flags) {
952 print STDERR "$prog:$file:$.: unrecognised field \"$field\"\n";
956 local($cnt) = $cnt{$field}++;
957 $test{$field} = $cnt{$field};
958 $cnt = 0 if $cnt eq '';
960 } elsif (defined $test{$field}) {
961 print STDERR "$prog:$file:$.: multiple \"$field\" fields\n";
964 $do_chop = $flags !~ /m/;
966 if ($val eq '' || $val eq '!' || $flags =~ /p/) {
967 if ($flags =~ /[Mm]/) {
977 "$prog:$file:$.: no parameters given for field \"$field\"\n";
990 chop $val if $do_chop;
994 # Syntax check on fields that can several instances
995 # (can give useful line numbers this way)
997 if ($field eq 'file-setup') {
998 local($type, $perm, $rest, $c, $len, $name);
1000 # format is: type perm "name"
1001 if ($val !~ /^[ \t]*(\S+)[ \t]+(\S+)[ \t]+([^ \t].*)/) {
1003 "$prog:$file:$.: bad parameter line for file-setup field\n";
1006 ($type, $perm, $rest) = ($1, $2, $3);
1007 if ($type !~ /^(file|dir|symlink)$/) {
1009 "$prog:$file:$.: bad file type for file-setup: $type\n";
1012 if ($perm !~ /^\d+$/) {
1014 "$prog:$file:$.: bad permissions for file-setup: $type\n";
1017 $c = substr($rest, 0, 1);
1018 if (($len = index($rest, $c, 1) - 1) <= 0) {
1020 "$prog:$file:$.: missing end quote for file name in file-setup: $rest\n";
1023 $name = substr($rest, 1, $len);
1024 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1025 # Note: this is not a security thing - just a sanity
1026 # check - a test can still use symlinks to get at files
1027 # outside the test directory.
1029 "$prog:$file:$.: file name in file-setup is absolute or contains ..: $name\n";
1033 if ($field eq 'file-result') {
1034 local($type, $perm, $uid, $gid, $matchType,
1035 $rest, $c, $len, $name);
1037 # format is: type perm uid gid matchType "name"
1038 if ($val !~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/) {
1040 "$prog:$file:$.: bad parameter line for file-result field\n";
1043 ($type, $perm, $uid, $gid, $matchType, $rest)
1044 = ($1, $2, $3, $4, $5, $6);
1045 if ($type !~ /^(file|dir|symlink)$/) {
1047 "$prog:$file:$.: bad file type for file-result: $type\n";
1050 if ($perm !~ /^\d+$/ && $perm ne '*') {
1052 "$prog:$file:$.: bad permissions for file-result: $perm\n";
1055 if ($uid !~ /^\d+$/ && $uid ne '*') {
1057 "$prog:$file:$.: bad user-id for file-result: $uid\n";
1060 if ($gid !~ /^\d+$/ && $gid ne '*') {
1062 "$prog:$file:$.: bad group-id for file-result: $gid\n";
1065 if ($matchType !~ /^(exact|pattern)$/) {
1067 "$prog:$file:$.: bad match type for file-result: $matchType\n";
1070 $c = substr($rest, 0, 1);
1071 if (($len = index($rest, $c, 1) - 1) <= 0) {
1073 "$prog:$file:$.: missing end quote for file name in file-result: $rest\n";
1076 $name = substr($rest, 1, $len);
1077 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1078 # Note: this is not a security thing - just a sanity
1079 # check - a test can still use symlinks to get at files
1080 # outside the test directory.
1082 "$prog:$file:$.: file name in file-result is absolute or contains ..: $name\n";
1086 } elsif ($val eq '') {
1088 "$prog:$file:$.: no value given for field \"$field\"\n";
1092 $val .= "\n" if !$do_chop;
1093 $test{$sfield} = $val;
1099 "$prog:$file:$start_lineno: end-of-file while reading test\n";
1105 while (($field, $val) = each %test_fields) {
1106 if ($val =~ /r/ && !defined $test{$field}) {
1108 "$prog:$file:$start_lineno: required field \"$field\" missing\n";
1113 $test{':full-name'} = substr($file, $file_prefix_skip) . ":$test{'name'}";
1114 $test{':long-name'} = "$file:$start_lineno:$test{'name'}";
1116 # Syntax check on specific fields
1117 if (defined $test{'expected-fail'}) {
1118 if ($test{'expected-fail'} !~ /^(yes|no)$/) {
1120 "$prog:$test{':long-name'}: bad value for expected-fail field\n";
1123 $test{'expected-fail'} = $1 eq 'yes';
1125 $test{'expected-fail'} = 0;
1127 if (defined $test{'need-ctty'}) {
1128 if ($test{'need-ctty'} !~ /^(yes|no)$/) {
1130 "$prog:$test{':long-name'}: bad value for need-ctty field\n";
1133 $test{'need-ctty'} = $1 eq 'yes';
1135 $test{'need-ctty'} = 0;
1137 if (defined $test{'need-pass'}) {
1138 if ($test{'need-pass'} !~ /^(yes|no)$/) {
1140 "$prog:$test{':long-name'}: bad value for need-pass field\n";
1143 $test{'need-pass'} = $1 eq 'yes';
1145 $test{'need-pass'} = 1;
1147 if (defined $test{'arguments'}) {
1148 local($firstc) = substr($test{'arguments'}, 0, 1);
1150 if (substr($test{'arguments'}, -1, 1) ne $firstc) {
1151 print STDERR "$prog:$test{':long-name'}: arguments field doesn't start and end with the same character\n";
1155 if (defined $test{'env-setup'}) {
1156 local($firstc) = substr($test{'env-setup'}, 0, 1);
1158 if (substr($test{'env-setup'}, -1, 1) ne $firstc) {
1159 print STDERR "$prog:$test{':long-name'}: env-setup field doesn't start and end with the same character\n";
1163 if (defined $test{'expected-exit'}) {
1164 local($val) = $test{'expected-exit'};
1166 if ($val =~ /^(|-)\d+$/) {
1167 if ($val < 0 || $val > 255) {
1168 print STDERR "$prog:$test{':long-name'}: expected-exit value $val not in 0..255\n";
1171 } elsif ($val !~ /^([\s\d<>+=*%\/&|!()-]|\b[wse]\b|\bSIG[A-Z][A-Z0-9]*\b)+$/) {
1172 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $val\n";
1176 $test{'expected-exit'} = 0;
1178 if (defined $test{'expected-stdout'}
1179 && defined $test{'expected-stdout-pattern'})
1181 print STDERR "$prog:$test{':long-name'}: can't use both expected-stdout and expected-stdout-pattern\n";
1184 if (defined $test{'expected-stderr'}
1185 && defined $test{'expected-stderr-pattern'})
1187 print STDERR "$prog:$test{':long-name'}: can't use both expected-stderr and expected-stderr-pattern\n";
1190 if (defined $test{'time-limit'}) {
1191 if ($test{'time-limit'} !~ /^\d+$/ || $test{'time-limit'} == 0) {
1193 "$prog:$test{':long-name'}: bad value for time-limit field\n";
1196 } elsif (defined $default_time_limit) {
1197 $test{'time-limit'} = $default_time_limit;
1200 if (defined $known_tests{$test{'name'}}) {
1201 print STDERR "$prog:$test{':long-name'}: warning: duplicate test name ${test{'name'}}\n";
1203 $known_tests{$test{'name'}} = 1;
1213 open(TTY, "> /dev/tty") || return 0;
1224 &never_called_funcs();
1226 $old_env{'foo'} = 'bar';
1227 $internal_test_fields{'foo'} = 'bar';
1235 return '' if (!defined $test{'file-result'});
1239 local($type, $perm, $uid, $gid, $rest, $c, $len, $name);
1242 for ($i = 0; $i < $test{'file-result'}; $i++) {
1243 $val = $test{"file-result:$i"};
1245 # format is: type perm "name"
1246 ($type, $perm, $uid, $gid, $matchType, $rest) =
1247 split(' ', $val, 6);
1248 $c = substr($rest, 0, 1);
1249 $len = index($rest, $c, 1) - 1;
1250 $name = substr($rest, 1, $len);
1251 $rest = substr($rest, 2 + $len);
1252 $perm = oct($perm) if $perm =~ /^\d+$/;
1254 @stbuf = lstat($name);
1256 $why .= "\texpected $type \"$name\" not created\n";
1259 if ($perm ne '*' && ($stbuf[2] & 07777) != $perm) {
1260 $why .= "\t$type \"$name\" has unexpected permissions\n";
1261 $why .= sprintf("\t\texpected 0%o, found 0%o\n",
1262 $perm, $stbuf[2] & 07777);
1264 if ($uid ne '*' && $stbuf[4] != $uid) {
1265 $why .= "\t$type \"$name\" has unexpected user-id\n";
1266 $why .= sprintf("\t\texpected %d, found %d\n",
1269 if ($gid ne '*' && $stbuf[5] != $gid) {
1270 $why .= "\t$type \"$name\" has unexpected group-id\n";
1271 $why .= sprintf("\t\texpected %d, found %d\n",
1275 if ($type eq 'file') {
1276 if (-l _ || ! -f _) {
1277 $why .= "\t$type \"$name\" is not a regular file\n";
1279 local $tmp = &check_output($test{'long-name'}, $name,
1280 "$type contents in \"$name\"",
1281 $matchType eq 'exact' ? $rest : undef
1282 $matchType eq 'pattern' ? $rest : undef);
1283 return undef if (!defined $tmp);
1286 } elsif ($type eq 'dir') {
1287 if ($rest !~ /^\s*$/) {
1288 print STDERR "$prog:$test{':long-name'}: file-result test for directory $name should not have content specified\n";
1291 if (-l _ || ! -d _) {
1292 $why .= "\t$type \"$name\" is not a directory\n";
1294 } elsif ($type eq 'symlink') {
1296 $why .= "\t$type \"$name\" is not a symlink\n";
1298 local $content = readlink($name);
1299 if (!defined $content) {
1300 print STDERR "$prog:$test{':long-name'}: file-result test for $type $name failed - could not readlink - $!\n";
1303 local $tmp = &compare_output($test{'long-name'},
1304 "$type contents in \"$name\"",
1305 $matchType eq 'exact' ? $rest : undef
1306 $matchType eq 'pattern' ? $rest : undef);
1307 return undef if (!defined $tmp);
1319 print STDERR $Usage;