1 # $MirOS: src/bin/mksh/check.pl,v 1.36 2014/06/09 13:25:50 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,
6 # Thorsten Glaser <tg@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 # file-setup mps Used to create files, directories
84 # and symlinks. First word is either
85 # file, dir or symlink; second word is
86 # permissions; this is followed by a
87 # quoted word that is the name of the
88 # file; the end-quote should be followed
89 # by a newline, then the file data
90 # (if any). The first word may be
91 # preceded by a ! to strip the trailing
92 # newline in a symlink.
93 # file-result mps Used to verify a file, symlink or
94 # directory is created correctly.
95 # The first word is either
96 # file, dir or symlink; second word is
97 # expected permissions; third word
98 # is user-id; fourth is group-id;
99 # fifth is "exact" or "pattern"
100 # indicating whether the file contents
101 # which follow is to be matched exactly
102 # or if it is a regular expression.
103 # The fifth argument is the quoted name
104 # of the file that should be created.
105 # The end-quote should be followed
106 # by a newline, then the file data
107 # (if any). The first word may be
108 # preceded by a ! to strip the trailing
109 # newline in the file contents.
110 # The permissions, user and group fields
111 # may be * meaning accept any value.
112 # time-limit Time limit - the program is sent a
113 # SIGKILL N seconds. Default is no
115 # expected-fail 'yes' if the test is expected to fail.
116 # expected-exit expected exit code. Can be a number,
117 # or a C expression using the variables
118 # e, s and w (exit code, termination
119 # signal, and status code).
120 # expected-stdout m What the test should generate on stdout;
121 # default is to expect no output.
122 # expected-stdout-pattern m A perl pattern which matches the
124 # expected-stderr m What the test should generate on stderr;
125 # default is to expect no output.
126 # expected-stderr-pattern m A perl pattern which matches the
127 # expected standard error.
128 # category m Specify a comma separated list of
129 # 'categories' of program that the test
130 # is to be run for. A category can be
131 # negated by prefixing the name with a !.
132 # The idea is that some tests in a
133 # test suite may apply to a particular
134 # program version and shouldn't be run
135 # on other versions. The category(s) of
136 # the program being tested can be
137 # specified on the command line.
138 # One category os:XXX is predefined
139 # (XXX is the operating system name,
140 # eg, linux, dec_osf).
141 # need-ctty 'yes' if the test needs a ctty, run
142 # with -C regress:no-ctty to disable.
144 # r tag is required (eg, a test must have a name tag).
145 # m value can be multiple lines. Lines must be prefixed with
146 # a tab. If the value part of the initial tag:value line is
147 # - empty: the initial blank line is stripped.
148 # - a lone !: the last newline in the value is stripped;
149 # M value can be multiple lines (prefixed by a tab) and consists
150 # of multiple fields, delimited by a field separator character.
151 # The value must start and end with the f-s-c.
152 # p tag takes parameters (used with m).
153 # s tag can be used several times.
155 # pull EINTR from POSIX.pm or Errno.pm if they exist
156 # otherwise just skip it
161 $EINTR = POSIX::EINTR();
166 $EINTR = Errno::EINTR();
176 $os = defined $^O ? $^O : 'unknown';
178 ($prog = $0) =~ s#.*/##;
181 Usage: $prog [-Pv] [-C cat] [-e e=v] [-p prog] [-s fn] [-T dir] \
183 -C c Specify the comma separated list of categories the program
184 belongs to (see category field).
185 -e e=v Set the environment variable e to v for all tests
186 (if no =v is given, the current value is used)
187 Only one -e option can be given at the moment, sadly.
188 -P program (-p) string has multiple words, and the program is in
189 the path (kludge option)
190 -p p Use p as the program to test
191 -s s Read tests from file s; if s is a directory, it is recursively
192 scaned for test files (which end in .t).
193 -T dir Use dir instead of /tmp to hold temporary files
194 -t t Use t as default time limit for tests (default is unlimited)
195 -v Verbose mode: print reason test failed.
196 name specifies the name of the test(s) to run; if none are
197 specified, all tests are run.
200 # See comment above for flag meanings
211 'file-result', 'mps',
215 'expected-stdout', 'm',
216 'expected-stdout-pattern', 'm',
217 'expected-stderr', 'm',
218 'expected-stderr-pattern', 'm',
223 # Filled in by read_test()
224 %internal_test_fields = (
225 ':full-name', 1, # file:name
226 ':long-name', 1, # dir/file:lineno:name
229 # Categories of the program under test. Provide the current
243 if (!getopts('C:e:Pp:s:T:t:v')) {
248 die "$prog: no program specified (use -p)\n" if !defined $opt_p;
249 die "$prog: no test set specified (use -s)\n" if !defined $opt_s;
251 $verbose = defined $opt_v && $opt_v;
253 $temp_base = $opt_T || "/tmp";
254 if (defined $opt_t) {
255 die "$prog: bad -t argument (should be number > 0): $opt_t\n"
256 if $opt_t !~ /^\d+$/ || $opt_t <= 0;
257 $default_time_limit = $opt_t;
259 $program_kludge = defined $opt_P ? $opt_P : 0;
261 if (defined $opt_C) {
262 foreach $c (split(',', $opt_C)) {
264 die "$prog: categories can't be negated on the command line\n"
270 # Note which tests are to be run.
272 grep($do_test{$_} = 1, @ARGV);
273 $all_tests = @ARGV == 0;
275 # Set up a very minimal environment
277 foreach $env (('HOME', 'LD_LIBRARY_PATH', 'LOCPATH', 'LOGNAME',
278 'PATH', 'SHELL', 'UNIXMODE', 'USER')) {
279 $new_env{$env} = $ENV{$env} if defined $ENV{$env};
281 $new_env{'CYGWIN'} = 'nodosfilewarning';
282 $new_env{'ENV'} = '/nonexistant';
283 if (($os eq 'VMS') || ($Config{perlpath} =~ m/$Config{_exe}$/i)) {
284 $new_env{'__perlname'} = $Config{perlpath};
286 $new_env{'__perlname'} = $Config{perlpath} . $Config{_exe};
288 if (defined $opt_e) {
289 # XXX need a way to allow many -e arguments...
290 if ($opt_e =~ /^([a-zA-Z_]\w*)(|=(.*))$/) {
291 $new_env{$1} = $2 eq '' ? $ENV{$1} : $3;
293 die "$0: bad -e argument: $opt_e\n";
298 chop($pwd = `pwd 2>/dev/null`);
299 die "$prog: couldn't get current working directory\n" if $pwd eq '';
300 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
302 die "$prog: couldn't cd to $temp_base - $!\n" if !chdir($temp_base);
303 die "$prog: couldn't get temporary directory base\n" unless -d '.';
304 $temps = sprintf("chk%d-%d.", $$, time());
306 until (mkdir(($tempdir = sprintf("%s%03d", $temps, $tempi)), 0700)) {
307 die "$prog: couldn't get temporary directory\n" if $tempi++ >= 999;
309 die "$prog: couldn't cd to $tempdir - $!\n" if !chdir($tempdir);
310 chop($temp_dir = `pwd 2>/dev/null`);
311 die "$prog: couldn't get temporary directory\n" if $temp_dir eq '';
312 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
314 if (!$program_kludge) {
315 $test_prog = "$pwd/$test_prog" if substr($test_prog, 0, 1) ne '/';
316 die "$prog: $test_prog is not executable - bye\n"
317 if (! -x $test_prog && $os ne 'os2');
320 @trap_sigs = ('TERM', 'QUIT', 'INT', 'PIPE', 'HUP');
321 @SIG{@trap_sigs} = ('cleanup_exit') x @trap_sigs;
323 $SIG{'ALRM'} = 'catch_sigalrm';
328 $temps = "${temp_dir}/rts";
329 $tempi = "${temp_dir}/rti";
330 $tempo = "${temp_dir}/rto";
331 $tempe = "${temp_dir}/rte";
332 $tempdir = "${temp_dir}/rtd";
333 mkdir($tempdir, 0700) or die "$prog: couldn't mkdir $tempdir - $!\n";
336 $file_prefix_skip = length($test_set) + 1;
337 $ret = &process_test_dir($test_set);
339 $file_prefix_skip = 0;
340 $ret = &process_test_file($test_set);
342 &cleanup_exit() if !defined $ret;
344 $tot_failed = $nfailed + $nifailed + $nxfailed;
345 $tot_passed = $npassed + $nxpassed;
346 if ($tot_failed || $tot_passed) {
347 print "Total failed: $tot_failed";
348 print " ($nifailed ignored)" if $nifailed;
349 print " ($nxfailed unexpected)" if $nxfailed;
350 print " (as expected)" if $nfailed && !$nxfailed && !$nifailed;
351 print "\nTotal passed: $tot_passed";
352 print " ($nxpassed unexpected)" if $nxpassed;
361 local($sig, $exitcode) = ('', 1);
369 } elsif ($_[0] ne '') {
373 unlink($tempi, $tempo, $tempe, $temps);
374 &scrub_dir($tempdir) if defined $tempdir;
375 rmdir($tempdir) if defined $tempdir;
376 rmdir($temp_dir) if defined $temp_dir;
379 $SIG{$sig} = 'DEFAULT';
389 $SIG{'ALRM'} = 'catch_sigalrm';
390 kill(9, $child_pid) if $child_kill_ok;
401 if (!opendir(DIR, $dir)) {
402 print STDERR "$prog: can't open directory $dir - $!\n";
405 while (defined ($file = readdir(DIR))) {
406 push(@todo, $file) if $file =~ /^[^.].*\.t$/;
410 foreach $file (@todo) {
411 $file = "$dir/$file";
413 $ret = &process_test_dir($file);
415 $ret = &process_test_file($file);
417 last if !defined $ret;
429 if (!open(IN, $file)) {
430 print STDERR "$prog: can't open $file - $!\n";
435 $ret = &read_test($file, IN, *test);
436 last if !defined $ret || !$ret;
437 next if !$all_tests && !$do_test{$test{'name'}};
438 next if !&category_check(*test);
439 $ret = &run_test(*test);
440 last if !defined $ret;
451 local($name) = $test{':full-name'};
453 return undef if !&scrub_dir($tempdir);
455 if (defined $test{'stdin'}) {
456 return undef if !&write_file($tempi, $test{'stdin'});
459 $ifile = '/dev/null';
462 if (defined $test{'script'}) {
463 return undef if !&write_file($temps, $test{'script'});
466 if (!chdir($tempdir)) {
467 print STDERR "$prog: couldn't cd to $tempdir - $!\n";
471 if (defined $test{'file-setup'}) {
473 local($type, $perm, $rest, $c, $len, $name);
475 for ($i = 0; $i < $test{'file-setup'}; $i++) {
476 $val = $test{"file-setup:$i"};
478 # format is: type perm "name"
479 ($type, $perm, $rest) =
481 $c = substr($rest, 0, 1);
482 $len = index($rest, $c, 1) - 1;
483 $name = substr($rest, 1, $len);
484 $rest = substr($rest, 2 + $len);
485 $perm = oct($perm) if $perm =~ /^\d+$/;
486 if ($type eq 'file') {
487 return undef if !&write_file($name, $rest);
488 if (!chmod($perm, $name)) {
490 "$prog:$test{':long-name'}: can't chmod $perm $name - $!\n";
493 } elsif ($type eq 'dir') {
494 if (!mkdir($name, $perm)) {
496 "$prog:$test{':long-name'}: can't mkdir $perm $name - $!\n";
499 } elsif ($type eq 'symlink') {
500 local($oumask) = umask($perm);
501 local($ret) = symlink($rest, $name);
505 "$prog:$test{':long-name'}: couldn't create symlink $name - $!\n";
512 if (defined $test{'perl-setup'}) {
513 eval $test{'perl-setup'};
515 print STDERR "$prog:$test{':long-name'}: error running perl-setup - $@\n";
522 print STDERR "$prog: can't fork - $!\n";
526 @SIG{@trap_sigs} = ('DEFAULT') x @trap_sigs;
527 $SIG{'ALRM'} = 'DEFAULT';
528 if (defined $test{'env-setup'}) {
529 local($var, $val, $i);
531 foreach $var (split(substr($test{'env-setup'}, 0, 1),
534 $i = index($var, '=');
535 next if $i == 0 || $var eq '';
537 delete $new_env{$var};
539 $new_env{substr($var, 0, $i)} = substr($var, $i + 1);
543 if (!open(STDIN, "< $ifile")) {
544 print STDERR "$prog: couldn't open $ifile in child - $!\n";
548 if (!open(STDOUT, "> $tempo")) {
549 print STDERR "$prog: couldn't open $tempo in child - $!\n";
553 if (!open(STDERR, "> $tempe")) {
554 print STDOUT "$prog: couldn't open $tempe in child - $!\n";
558 if ($program_kludge) {
559 @argv = split(' ', $test_prog);
561 @argv = ($test_prog);
563 if (defined $test{'arguments'}) {
565 split(substr($test{'arguments'}, 0, 1),
566 substr($test{'arguments'}, 1)));
568 push(@argv, $temps) if defined $test{'script'};
570 #XXX realpathise, use which/whence -p, or sth. like that
571 #XXX if !$program_kludge, we get by with not doing it for now tho
572 $new_env{'__progname'} = $argv[0];
574 # The following doesn't work with perl5... Need to do it explicitly - yuck.
576 foreach $k (keys(%ENV)) {
579 $ENV{$k} = $v while ($k,$v) = each %new_env;
581 exec { $argv[0] } @argv;
582 print STDERR "$prog: couldn't execute $test_prog - $!\n";
589 alarm($test{'time-limit'}) if defined $test{'time-limit'};
591 $xpid = waitpid($pid, 0);
595 next if $! == $EINTR;
597 print STDERR "$prog: error waiting for child - $!\n";
603 alarm(0) if defined $test{'time-limit'};
610 $why .= "\ttest timed out (limit of $test{'time-limit'} seconds)\n";
613 $ret = &eval_exit($test{'long-name'}, $status, $test{'expected-exit'});
614 return undef if !defined $ret;
619 if (($status & 0xff) == 0x7f) {
621 } elsif (($status & 0xff)) {
622 $expl = "signal " . ($status & 0x7f);
624 $expl = "exit-code " . (($status >> 8) & 0xff);
627 "\tunexpected exit status $status ($expl), expected $test{'expected-exit'}\n";
630 $tmp = &check_output($test{'long-name'}, $tempo, 'stdout',
631 $test{'expected-stdout'}, $test{'expected-stdout-pattern'});
632 return undef if !defined $tmp;
638 $tmp = &check_output($test{'long-name'}, $tempe, 'stderr',
639 $test{'expected-stderr'}, $test{'expected-stderr-pattern'});
640 return undef if !defined $tmp;
646 $tmp = &check_file_result(*test);
647 return undef if !defined $tmp;
653 if (defined $test{'perl-cleanup'}) {
654 eval $test{'perl-cleanup'};
656 print STDERR "$prog:$test{':long-name'}: error running perl-cleanup - $@\n";
662 print STDERR "$prog: couldn't cd to $pwd - $!\n";
667 if (!$test{'expected-fail'}) {
668 if ($test{'need-pass'}) {
669 print "FAIL $name\n";
672 print "FAIL $name (ignored)\n";
676 print "fail $name (as expected)\n";
679 $why = "\tDescription"
680 . &wrap_lines($test{'description'}, " (missing)\n")
682 } elsif ($test{'expected-fail'}) {
683 print "PASS $name (unexpectedly)\n";
686 print "pass $name\n";
689 print $why if $verbose;
699 return 0 if ($test{'need-ctty'} && defined $categories{'regress:no-ctty'});
700 return 1 if (!defined $test{'category'});
702 foreach $c (split(',', $test{'category'})) {
706 return 0 if (defined $categories{$c});
709 $ok = 1 if (defined $categories{$c});
722 if (!opendir(DIR, $dir)) {
723 print STDERR "$prog: couldn't open directory $dir - $!\n";
726 while (defined ($file = readdir(DIR))) {
727 push(@todo, $file) if $file ne '.' && $file ne '..';
730 foreach $file (@todo) {
731 $file = "$dir/$file";
733 return undef if !&scrub_dir($file);
735 print STDERR "$prog: couldn't rmdir $file - $!\n";
739 if (!unlink($file)) {
740 print STDERR "$prog: couldn't unlink $file - $!\n";
751 local($file, $str) = @_;
753 if (!open(TEMP, "> $file")) {
754 print STDERR "$prog: can't open $file - $!\n";
760 print STDERR "$prog: error writing $file - $!\n";
769 local($name, $file, $what, $expect, $expect_pat) = @_;
774 if (!open(TEMP, "< $file")) {
775 print STDERR "$prog:$name($what): couldn't open $file after running program - $!\n";
783 return compare_output($name, $what, $expect, $expect_pat, $got);
789 local($name, $what, $expect, $expect_pat, $got) = @_;
792 if (defined $expect_pat) {
794 $ret = eval "$expect_pat";
796 print STDERR "$prog:$name($what): error evaluating $what pattern: $expect_pat - $@\n";
800 $why = "\tunexpected $what - wanted pattern";
801 $why .= &wrap_lines($expect_pat);
803 $why .= &wrap_lines($got);
806 $expect = '' if !defined $expect;
807 if ($got ne $expect) {
808 $why .= "\tunexpected $what - " . &first_diff($expect, $got) . "\n";
810 $why .= &wrap_lines($expect);
812 $why .= &wrap_lines($got);
821 local($str, $empty) = @_;
822 local($nonl) = substr($str, -1, 1) ne "\n";
824 return (defined $empty ? $empty : " nothing\n") if $str eq '';
825 substr($str, 0, 0) = ":\n";
826 $str =~ s/\n/\n\t\t/g;
828 $str .= "\n\t[incomplete last line]\n";
839 local($exp, $got) = @_;
840 local($lineno, $char) = (1, 1);
841 local($i, $exp_len, $got_len);
844 $exp_len = length($exp);
845 $got_len = length($got);
846 if ($exp_len != $got_len) {
847 if ($exp_len < $got_len) {
848 if (substr($got, 0, $exp_len) eq $exp) {
849 return "got too much output";
851 } elsif (substr($exp, 0, $got_len) eq $got) {
852 return "got too little output";
855 for ($i = 0; $i < $exp_len; $i++) {
856 $ce = substr($exp, $i, 1);
857 $cg = substr($got, $i, 1);
865 return "first difference: line $lineno, char $char (wanted '"
866 . &format_char($ce) . "', got '"
867 . &format_char($cg) . "'";
878 } elsif ($ch == 13) {
884 } elsif ($ch > 127) {
893 } elsif ($ch == 127) {
896 return $s . sprintf("%c", $ch);
902 local($name, $status, $expect) = @_;
904 local($w, $e, $s) = ($status, ($status >> 8) & 0xff, $status & 0x7f);
906 $e = -1000 if $status & 0xff;
907 $s = -1000 if $s == 0x7f;
908 if (!defined $expect) {
910 } elsif ($expect =~ /^(|-)\d+$/) {
911 $expr = "\$e == $expect";
914 $expr =~ s/\b([wse])\b/\$$1/g;
915 $expr =~ s/\b(SIG[A-Z][A-Z0-9]*)\b/&$1/g;
919 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $expect ($@)\n";
928 local($file, $in, *test) = @_;
929 local($field, $val, $flags, $do_chop, $need_redo, $start_lineno);
930 local(%cnt, $sfield);
938 last if /^\s*---\s*$/;
939 $start_lineno = $. if !defined $start_lineno;
940 if (!/^([-\w]+):\s*(|\S|\S.*\S)\s*$/) {
941 print STDERR "$prog:$file:$.: unrecognised line \"$_\"\n";
944 ($field, $val) = ($1, $2);
946 $flags = $test_fields{$field};
947 if (!defined $flags) {
948 print STDERR "$prog:$file:$.: unrecognised field \"$field\"\n";
952 local($cnt) = $cnt{$field}++;
953 $test{$field} = $cnt{$field};
954 $cnt = 0 if $cnt eq '';
956 } elsif (defined $test{$field}) {
957 print STDERR "$prog:$file:$.: multiple \"$field\" fields\n";
960 $do_chop = $flags !~ /m/;
962 if ($val eq '' || $val eq '!' || $flags =~ /p/) {
963 if ($flags =~ /[Mm]/) {
973 "$prog:$file:$.: no parameters given for field \"$field\"\n";
986 chop $val if $do_chop;
990 # Syntax check on fields that can several instances
991 # (can give useful line numbers this way)
993 if ($field eq 'file-setup') {
994 local($type, $perm, $rest, $c, $len, $name);
996 # format is: type perm "name"
997 if ($val !~ /^[ \t]*(\S+)[ \t]+(\S+)[ \t]+([^ \t].*)/) {
999 "$prog:$file:$.: bad parameter line for file-setup field\n";
1002 ($type, $perm, $rest) = ($1, $2, $3);
1003 if ($type !~ /^(file|dir|symlink)$/) {
1005 "$prog:$file:$.: bad file type for file-setup: $type\n";
1008 if ($perm !~ /^\d+$/) {
1010 "$prog:$file:$.: bad permissions for file-setup: $type\n";
1013 $c = substr($rest, 0, 1);
1014 if (($len = index($rest, $c, 1) - 1) <= 0) {
1016 "$prog:$file:$.: missing end quote for file name in file-setup: $rest\n";
1019 $name = substr($rest, 1, $len);
1020 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1021 # Note: this is not a security thing - just a sanity
1022 # check - a test can still use symlinks to get at files
1023 # outside the test directory.
1025 "$prog:$file:$.: file name in file-setup is absolute or contains ..: $name\n";
1029 if ($field eq 'file-result') {
1030 local($type, $perm, $uid, $gid, $matchType,
1031 $rest, $c, $len, $name);
1033 # format is: type perm uid gid matchType "name"
1034 if ($val !~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/) {
1036 "$prog:$file:$.: bad parameter line for file-result field\n";
1039 ($type, $perm, $uid, $gid, $matchType, $rest)
1040 = ($1, $2, $3, $4, $5, $6);
1041 if ($type !~ /^(file|dir|symlink)$/) {
1043 "$prog:$file:$.: bad file type for file-result: $type\n";
1046 if ($perm !~ /^\d+$/ && $perm ne '*') {
1048 "$prog:$file:$.: bad permissions for file-result: $perm\n";
1051 if ($uid !~ /^\d+$/ && $uid ne '*') {
1053 "$prog:$file:$.: bad user-id for file-result: $uid\n";
1056 if ($gid !~ /^\d+$/ && $gid ne '*') {
1058 "$prog:$file:$.: bad group-id for file-result: $gid\n";
1061 if ($matchType !~ /^(exact|pattern)$/) {
1063 "$prog:$file:$.: bad match type for file-result: $matchType\n";
1066 $c = substr($rest, 0, 1);
1067 if (($len = index($rest, $c, 1) - 1) <= 0) {
1069 "$prog:$file:$.: missing end quote for file name in file-result: $rest\n";
1072 $name = substr($rest, 1, $len);
1073 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1074 # Note: this is not a security thing - just a sanity
1075 # check - a test can still use symlinks to get at files
1076 # outside the test directory.
1078 "$prog:$file:$.: file name in file-result is absolute or contains ..: $name\n";
1082 } elsif ($val eq '') {
1084 "$prog:$file:$.: no value given for field \"$field\"\n";
1088 $val .= "\n" if !$do_chop;
1089 $test{$sfield} = $val;
1095 "$prog:$file:$start_lineno: end-of-file while reading test\n";
1101 while (($field, $val) = each %test_fields) {
1102 if ($val =~ /r/ && !defined $test{$field}) {
1104 "$prog:$file:$start_lineno: required field \"$field\" missing\n";
1109 $test{':full-name'} = substr($file, $file_prefix_skip) . ":$test{'name'}";
1110 $test{':long-name'} = "$file:$start_lineno:$test{'name'}";
1112 # Syntax check on specific fields
1113 if (defined $test{'expected-fail'}) {
1114 if ($test{'expected-fail'} !~ /^(yes|no)$/) {
1116 "$prog:$test{':long-name'}: bad value for expected-fail field\n";
1119 $test{'expected-fail'} = $1 eq 'yes';
1121 $test{'expected-fail'} = 0;
1123 if (defined $test{'need-ctty'}) {
1124 if ($test{'need-ctty'} !~ /^(yes|no)$/) {
1126 "$prog:$test{':long-name'}: bad value for need-ctty field\n";
1129 $test{'need-ctty'} = $1 eq 'yes';
1131 $test{'need-ctty'} = 0;
1133 if (defined $test{'need-pass'}) {
1134 if ($test{'need-pass'} !~ /^(yes|no)$/) {
1136 "$prog:$test{':long-name'}: bad value for need-pass field\n";
1139 $test{'need-pass'} = $1 eq 'yes';
1141 $test{'need-pass'} = 1;
1143 if (defined $test{'arguments'}) {
1144 local($firstc) = substr($test{'arguments'}, 0, 1);
1146 if (substr($test{'arguments'}, -1, 1) ne $firstc) {
1147 print STDERR "$prog:$test{':long-name'}: arguments field doesn't start and end with the same character\n";
1151 if (defined $test{'env-setup'}) {
1152 local($firstc) = substr($test{'env-setup'}, 0, 1);
1154 if (substr($test{'env-setup'}, -1, 1) ne $firstc) {
1155 print STDERR "$prog:$test{':long-name'}: env-setup field doesn't start and end with the same character\n";
1159 if (defined $test{'expected-exit'}) {
1160 local($val) = $test{'expected-exit'};
1162 if ($val =~ /^(|-)\d+$/) {
1163 if ($val < 0 || $val > 255) {
1164 print STDERR "$prog:$test{':long-name'}: expected-exit value $val not in 0..255\n";
1167 } elsif ($val !~ /^([\s<>+-=*%\/&|!()]|\b[wse]\b|\bSIG[A-Z][A-Z0-9]*\b)+$/) {
1168 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $val\n";
1172 $test{'expected-exit'} = 0;
1174 if (defined $test{'expected-stdout'}
1175 && defined $test{'expected-stdout-pattern'})
1177 print STDERR "$prog:$test{':long-name'}: can't use both expected-stdout and expected-stdout-pattern\n";
1180 if (defined $test{'expected-stderr'}
1181 && defined $test{'expected-stderr-pattern'})
1183 print STDERR "$prog:$test{':long-name'}: can't use both expected-stderr and expected-stderr-pattern\n";
1186 if (defined $test{'time-limit'}) {
1187 if ($test{'time-limit'} !~ /^\d+$/ || $test{'time-limit'} == 0) {
1189 "$prog:$test{':long-name'}: bad value for time-limit field\n";
1192 } elsif (defined $default_time_limit) {
1193 $test{'time-limit'} = $default_time_limit;
1196 if (defined $known_tests{$test{'name'}}) {
1197 print STDERR "$prog:$test{':long-name'}: warning: duplicate test name ${test{'name'}}\n";
1199 $known_tests{$test{'name'}} = 1;
1209 open(TTY, "> /dev/tty") || return 0;
1220 &never_called_funcs();
1222 $old_env{'foo'} = 'bar';
1223 $internal_test_fields{'foo'} = 'bar';
1231 return '' if (!defined $test{'file-result'});
1235 local($type, $perm, $uid, $gid, $rest, $c, $len, $name);
1238 for ($i = 0; $i < $test{'file-result'}; $i++) {
1239 $val = $test{"file-result:$i"};
1241 # format is: type perm "name"
1242 ($type, $perm, $uid, $gid, $matchType, $rest) =
1243 split(' ', $val, 6);
1244 $c = substr($rest, 0, 1);
1245 $len = index($rest, $c, 1) - 1;
1246 $name = substr($rest, 1, $len);
1247 $rest = substr($rest, 2 + $len);
1248 $perm = oct($perm) if $perm =~ /^\d+$/;
1250 @stbuf = lstat($name);
1252 $why .= "\texpected $type \"$name\" not created\n";
1255 if ($perm ne '*' && ($stbuf[2] & 07777) != $perm) {
1256 $why .= "\t$type \"$name\" has unexpected permissions\n";
1257 $why .= sprintf("\t\texpected 0%o, found 0%o\n",
1258 $perm, $stbuf[2] & 07777);
1260 if ($uid ne '*' && $stbuf[4] != $uid) {
1261 $why .= "\t$type \"$name\" has unexpected user-id\n";
1262 $why .= sprintf("\t\texpected %d, found %d\n",
1265 if ($gid ne '*' && $stbuf[5] != $gid) {
1266 $why .= "\t$type \"$name\" has unexpected group-id\n";
1267 $why .= sprintf("\t\texpected %d, found %d\n",
1271 if ($type eq 'file') {
1272 if (-l _ || ! -f _) {
1273 $why .= "\t$type \"$name\" is not a regular file\n";
1275 local $tmp = &check_output($test{'long-name'}, $name,
1276 "$type contents in \"$name\"",
1277 $matchType eq 'exact' ? $rest : undef
1278 $matchType eq 'pattern' ? $rest : undef);
1279 return undef if (!defined $tmp);
1282 } elsif ($type eq 'dir') {
1283 if ($rest !~ /^\s*$/) {
1284 print STDERR "$prog:$test{':long-name'}: file-result test for directory $name should not have content specified\n";
1287 if (-l _ || ! -d _) {
1288 $why .= "\t$type \"$name\" is not a directory\n";
1290 } elsif ($type eq 'symlink') {
1292 $why .= "\t$type \"$name\" is not a symlink\n";
1294 local $content = readlink($name);
1295 if (!defined $content) {
1296 print STDERR "$prog:$test{':long-name'}: file-result test for $type $name failed - could not readlink - $!\n";
1299 local $tmp = &compare_output($test{'long-name'},
1300 "$type contents in \"$name\"",
1301 $matchType eq 'exact' ? $rest : undef
1302 $matchType eq 'pattern' ? $rest : undef);
1303 return undef if (!defined $tmp);
1315 print STDERR $Usage;