1 # $MirOS: src/bin/mksh/check.pl,v 1.31 2012/04/06 12:22:14 tg Exp $
2 # $OpenBSD: th,v 1.13 2006/05/18 21:27:23 miod Exp $
4 # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012
5 # Thorsten Glaser <tg@mirbsd.org>
7 # Provided that these terms and disclaimer and all copyright notices
8 # are retained or reproduced in an accompanying document, permission
9 # is granted to deal in this work without restriction, including un-
10 # limited rights to use, publicly perform, distribute, sell, modify,
11 # merge, give away, or sublicence.
13 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 # the utmost extent permitted by applicable law, neither express nor
15 # implied; without malicious intent or gross negligence. In no event
16 # may a licensor, author or contributor be held liable for indirect,
17 # direct, other damage, loss, or other issues arising in any way out
18 # of dealing in the work, even if advised of the possibility of such
19 # damage or existence of a defect, except proven that it results out
20 # of said person's immediate fault when using the work as intended.
25 # a test to show how tests are done
37 # This runs the test-program (eg, mksh) with the arguments -x and -f,
38 # standard input is a file containing "echo hi*\nfalse\n". The program
39 # is expected to produce "hi*" (no trailing newline) on standard output,
40 # "+ echo hi*\n+false\n" on standard error, and an exit code of 1.
43 # Format of test files:
44 # - blank lines and lines starting with # are ignored
45 # - a test file contains a series of tests
46 # - a test is a series of tag:value pairs ended with a "---" line
47 # (leading/trailing spaces are stripped from the first line of value)
49 # Tag Flag Description
50 # ----- ---- -----------
51 # name r The name of the test; should be unique
52 # description m What test does
53 # arguments M Arguments to pass to the program;
54 # default is no arguments.
55 # script m Value is written to a file which
56 # is passed as an argument to the program
57 # (after the arguments arguments)
58 # stdin m Value is written to a file which is
59 # used as standard-input for the program;
60 # default is to use /dev/null.
61 # perl-setup m Value is a perl script which is executed
62 # just before the test is run. Try to
64 # perl-cleanup m Value is a perl script which is executed
65 # just after the test is run. Try to
67 # env-setup M Value is a list of NAME=VALUE elements
68 # which are put in the environment before
69 # the test is run. If the =VALUE is
70 # missing, NAME is removed from the
71 # environment. Programs are run with
72 # the following minimal environment:
73 # HOME, LD_LIBRARY_PATH, LOCPATH,
74 # LOGNAME, PATH, SHELL, UNIXMODE,
76 # (values taken from the environment of
78 # CYGWIN is set to nodosfilewarning.
79 # ENV is set to /nonexistant.
80 # __progname is set to the -p argument.
81 # __perlname is set to $^X (perlexe).
82 # file-setup mps Used to create files, directories
83 # and symlinks. First word is either
84 # file, dir or symlink; second word is
85 # permissions; this is followed by a
86 # quoted word that is the name of the
87 # file; the end-quote should be followed
88 # by a newline, then the file data
89 # (if any). The first word may be
90 # preceded by a ! to strip the trailing
91 # newline in a symlink.
92 # file-result mps Used to verify a file, symlink or
93 # directory is created correctly.
94 # The first word is either
95 # file, dir or symlink; second word is
96 # expected permissions; third word
97 # is user-id; fourth is group-id;
98 # fifth is "exact" or "pattern"
99 # indicating whether the file contents
100 # which follow is to be matched exactly
101 # or if it is a regular expression.
102 # The fifth argument is the quoted name
103 # of the file that should be created.
104 # The end-quote should be followed
105 # by a newline, then the file data
106 # (if any). The first word may be
107 # preceded by a ! to strip the trailing
108 # newline in the file contents.
109 # The permissions, user and group fields
110 # may be * meaning accept any value.
111 # time-limit Time limit - the program is sent a
112 # SIGKILL N seconds. Default is no
114 # expected-fail 'yes' if the test is expected to fail.
115 # expected-exit expected exit code. Can be a number,
116 # or a C expression using the variables
117 # e, s and w (exit code, termination
118 # signal, and status code).
119 # expected-stdout m What the test should generate on stdout;
120 # default is to expect no output.
121 # expected-stdout-pattern m A perl pattern which matches the
123 # expected-stderr m What the test should generate on stderr;
124 # default is to expect no output.
125 # expected-stderr-pattern m A perl pattern which matches the
126 # expected standard error.
127 # category m Specify a comma separated list of
128 # 'categories' of program that the test
129 # is to be run for. A category can be
130 # negated by prefixing the name with a !.
131 # The idea is that some tests in a
132 # test suite may apply to a particular
133 # program version and shouldn't be run
134 # on other versions. The category(s) of
135 # the program being tested can be
136 # specified on the command line.
137 # One category os:XXX is predefined
138 # (XXX is the operating system name,
139 # eg, linux, dec_osf).
140 # need-ctty 'yes' if the test needs a ctty, run
141 # with -C regress:no-ctty to disable.
143 # r tag is required (eg, a test must have a name tag).
144 # m value can be multiple lines. Lines must be prefixed with
145 # a tab. If the value part of the initial tag:value line is
146 # - empty: the initial blank line is stripped.
147 # - a lone !: the last newline in the value is stripped;
148 # M value can be multiple lines (prefixed by a tab) and consists
149 # of multiple fields, delimited by a field separator character.
150 # The value must start and end with the f-s-c.
151 # p tag takes parameters (used with m).
152 # s tag can be used several times.
154 # pull EINTR from POSIX.pm or Errno.pm if they exist
155 # otherwise just skip it
160 $EINTR = POSIX::EINTR();
165 $EINTR = Errno::EINTR();
175 $os = defined $^O ? $^O : 'unknown';
177 ($prog = $0) =~ s#.*/##;
180 Usage: $prog [-Pv] [-C cat] [-e e=v] [-p prog] [-s fn] [-t tmo] name ...
181 -C c Specify the comma separated list of categories the program
182 belongs to (see category field).
183 -e e=v Set the environment variable e to v for all tests
184 (if no =v is given, the current value is used)
185 Only one -e option can be given at the moment, sadly.
186 -P program (-p) string has multiple words, and the program is in
187 the path (kludge option)
188 -p p Use p as the program to test
189 -s s Read tests from file s; if s is a directory, it is recursively
190 scaned for test files (which end in .t).
191 -t t Use t as default time limit for tests (default is unlimited)
192 -v Verbose mode: print reason test failed.
193 name specifies the name of the test(s) to run; if none are
194 specified, all tests are run.
197 # See comment above for flag meanings
208 'file-result', 'mps',
212 'expected-stdout', 'm',
213 'expected-stdout-pattern', 'm',
214 'expected-stderr', 'm',
215 'expected-stderr-pattern', 'm',
220 # Filled in by read_test()
221 %internal_test_fields = (
222 ':full-name', 1, # file:name
223 ':long-name', 1, # dir/file:lineno:name
226 # Categories of the program under test. Provide the current
232 $temps = "/tmp/rts$$";
233 $tempi = "/tmp/rti$$";
234 $tempo = "/tmp/rto$$";
235 $tempe = "/tmp/rte$$";
236 $tempdir = "/tmp/rtd$$";
246 if (!getopts('C:e:Pp:s:t:v')) {
251 die "$prog: no program specified (use -p)\n" if !defined $opt_p;
252 die "$prog: no test set specified (use -s)\n" if !defined $opt_s;
254 $verbose = defined $opt_v && $opt_v;
256 if (defined $opt_t) {
257 die "$prog: bad -t argument (should be number > 0): $opt_t\n"
258 if $opt_t !~ /^\d+$/ || $opt_t <= 0;
259 $default_time_limit = $opt_t;
261 $program_kludge = defined $opt_P ? $opt_P : 0;
263 if (defined $opt_C) {
264 foreach $c (split(',', $opt_C)) {
266 die "$prog: categories can't be negated on the command line\n"
272 # Note which tests are to be run.
274 grep($do_test{$_} = 1, @ARGV);
275 $all_tests = @ARGV == 0;
277 # Set up a very minimal environment
279 foreach $env (('HOME', 'LD_LIBRARY_PATH', 'LOCPATH', 'LOGNAME',
280 'PATH', 'SHELL', 'UNIXMODE', 'USER')) {
281 $new_env{$env} = $ENV{$env} if defined $ENV{$env};
283 $new_env{'CYGWIN'} = 'nodosfilewarning';
284 $new_env{'ENV'} = '/nonexistant';
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 die "$prog: couldn't make directory $tempdir - $!\n" if !mkdir($tempdir, 0777);
302 chop($pwd = `pwd 2>/dev/null`);
303 die "$prog: couldn't get current working directory\n" if $pwd eq '';
304 die "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd);
306 if (!$program_kludge) {
307 $test_prog = "$pwd/$test_prog" if substr($test_prog, 0, 1) ne '/';
308 die "$prog: $test_prog is not executable - bye\n"
309 if (! -x $test_prog && $os ne 'os2');
312 @trap_sigs = ('TERM', 'QUIT', 'INT', 'PIPE', 'HUP');
313 @SIG{@trap_sigs} = ('cleanup_exit') x @trap_sigs;
315 $SIG{'ALRM'} = 'catch_sigalrm';
320 $file_prefix_skip = length($test_set) + 1;
321 $ret = &process_test_dir($test_set);
323 $file_prefix_skip = 0;
324 $ret = &process_test_file($test_set);
326 &cleanup_exit() if !defined $ret;
328 $tot_failed = $nfailed + $nifailed + $nxfailed;
329 $tot_passed = $npassed + $nxpassed;
330 if ($tot_failed || $tot_passed) {
331 print "Total failed: $tot_failed";
332 print " ($nifailed ignored)" if $nifailed;
333 print " ($nxfailed unexpected)" if $nxfailed;
334 print " (as expected)" if $nfailed && !$nxfailed && !$nifailed;
335 print "\nTotal passed: $tot_passed";
336 print " ($nxpassed unexpected)" if $nxpassed;
345 local($sig, $exitcode) = ('', 1);
353 } elsif ($_[0] ne '') {
357 unlink($tempi, $tempo, $tempe, $temps);
358 &scrub_dir($tempdir) if defined $tempdir;
359 rmdir($tempdir) if defined $tempdir;
362 $SIG{$sig} = 'DEFAULT';
372 $SIG{'ALRM'} = 'catch_sigalrm';
373 kill(9, $child_pid) if $child_kill_ok;
384 if (!opendir(DIR, $dir)) {
385 print STDERR "$prog: can't open directory $dir - $!\n";
388 while (defined ($file = readdir(DIR))) {
389 push(@todo, $file) if $file =~ /^[^.].*\.t$/;
393 foreach $file (@todo) {
394 $file = "$dir/$file";
396 $ret = &process_test_dir($file);
398 $ret = &process_test_file($file);
400 last if !defined $ret;
412 if (!open(IN, $file)) {
413 print STDERR "$prog: can't open $file - $!\n";
418 $ret = &read_test($file, IN, *test);
419 last if !defined $ret || !$ret;
420 next if !$all_tests && !$do_test{$test{'name'}};
421 next if !&category_check(*test);
422 $ret = &run_test(*test);
423 last if !defined $ret;
434 local($name) = $test{':full-name'};
436 if (defined $test{'stdin'}) {
437 return undef if !&write_file($tempi, $test{'stdin'});
440 $ifile = '/dev/null';
443 if (defined $test{'script'}) {
444 return undef if !&write_file($temps, $test{'script'});
447 return undef if !&scrub_dir($tempdir);
449 if (!chdir($tempdir)) {
450 print STDERR "$prog: couldn't cd to $tempdir - $!\n";
454 if (defined $test{'file-setup'}) {
456 local($type, $perm, $rest, $c, $len, $name);
458 for ($i = 0; $i < $test{'file-setup'}; $i++) {
459 $val = $test{"file-setup:$i"};
461 # format is: type perm "name"
462 ($type, $perm, $rest) =
464 $c = substr($rest, 0, 1);
465 $len = index($rest, $c, 1) - 1;
466 $name = substr($rest, 1, $len);
467 $rest = substr($rest, 2 + $len);
468 $perm = oct($perm) if $perm =~ /^\d+$/;
469 if ($type eq 'file') {
470 return undef if !&write_file($name, $rest);
471 if (!chmod($perm, $name)) {
473 "$prog:$test{':long-name'}: can't chmod $perm $name - $!\n";
476 } elsif ($type eq 'dir') {
477 if (!mkdir($name, $perm)) {
479 "$prog:$test{':long-name'}: can't mkdir $perm $name - $!\n";
482 } elsif ($type eq 'symlink') {
483 local($oumask) = umask($perm);
484 local($ret) = symlink($rest, $name);
488 "$prog:$test{':long-name'}: couldn't create symlink $name - $!\n";
495 if (defined $test{'perl-setup'}) {
496 eval $test{'perl-setup'};
498 print STDERR "$prog:$test{':long-name'}: error running perl-setup - $@\n";
505 print STDERR "$prog: can't fork - $!\n";
509 @SIG{@trap_sigs} = ('DEFAULT') x @trap_sigs;
510 $SIG{'ALRM'} = 'DEFAULT';
511 if (defined $test{'env-setup'}) {
512 local($var, $val, $i);
514 foreach $var (split(substr($test{'env-setup'}, 0, 1),
517 $i = index($var, '=');
518 next if $i == 0 || $var eq '';
520 delete $new_env{$var};
522 $new_env{substr($var, 0, $i)} = substr($var, $i + 1);
526 if (!open(STDIN, "< $ifile")) {
527 print STDERR "$prog: couldn't open $ifile in child - $!\n";
531 if (!open(STDOUT, "> $tempo")) {
532 print STDERR "$prog: couldn't open $tempo in child - $!\n";
536 if (!open(STDERR, "> $tempe")) {
537 print STDOUT "$prog: couldn't open $tempe in child - $!\n";
541 if ($program_kludge) {
542 @argv = split(' ', $test_prog);
544 @argv = ($test_prog);
546 if (defined $test{'arguments'}) {
548 split(substr($test{'arguments'}, 0, 1),
549 substr($test{'arguments'}, 1)));
551 push(@argv, $temps) if defined $test{'script'};
553 #XXX realpathise, use which/whence -p, or sth. like that
554 #XXX if !$program_kludge, we get by with not doing it for now tho
555 $new_env{'__progname'} = $argv[0];
557 # The following doesn't work with perl5... Need to do it explicitly - yuck.
559 foreach $k (keys(%ENV)) {
562 $ENV{$k} = $v while ($k,$v) = each %new_env;
564 exec { $argv[0] } @argv;
565 print STDERR "$prog: couldn't execute $test_prog - $!\n";
572 alarm($test{'time-limit'}) if defined $test{'time-limit'};
574 $xpid = waitpid($pid, 0);
578 next if $! == $EINTR;
580 print STDERR "$prog: error waiting for child - $!\n";
586 alarm(0) if defined $test{'time-limit'};
593 $why .= "\ttest timed out (limit of $test{'time-limit'} seconds)\n";
596 $ret = &eval_exit($test{'long-name'}, $status, $test{'expected-exit'});
597 return undef if !defined $ret;
602 if (($status & 0xff) == 0x7f) {
604 } elsif (($status & 0xff)) {
605 $expl = "signal " . ($status & 0x7f);
607 $expl = "exit-code " . (($status >> 8) & 0xff);
610 "\tunexpected exit status $status ($expl), expected $test{'expected-exit'}\n";
613 $tmp = &check_output($test{'long-name'}, $tempo, 'stdout',
614 $test{'expected-stdout'}, $test{'expected-stdout-pattern'});
615 return undef if !defined $tmp;
621 $tmp = &check_output($test{'long-name'}, $tempe, 'stderr',
622 $test{'expected-stderr'}, $test{'expected-stderr-pattern'});
623 return undef if !defined $tmp;
629 $tmp = &check_file_result(*test);
630 return undef if !defined $tmp;
636 if (defined $test{'perl-cleanup'}) {
637 eval $test{'perl-cleanup'};
639 print STDERR "$prog:$test{':long-name'}: error running perl-cleanup - $@\n";
645 print STDERR "$prog: couldn't cd to $pwd - $!\n";
650 if (!$test{'expected-fail'}) {
651 if ($test{'need-pass'}) {
652 print "FAIL $name\n";
655 print "FAIL $name (ignored)\n";
659 print "fail $name (as expected)\n";
662 $why = "\tDescription"
663 . &wrap_lines($test{'description'}, " (missing)\n")
665 } elsif ($test{'expected-fail'}) {
666 print "PASS $name (unexpectedly)\n";
669 print "pass $name\n";
672 print $why if $verbose;
682 return 0 if ($test{'need-ctty'} && defined $categories{'regress:no-ctty'});
683 return 1 if (!defined $test{'category'});
685 foreach $c (split(',', $test{'category'})) {
689 return 0 if (defined $categories{$c});
692 $ok = 1 if (defined $categories{$c});
705 if (!opendir(DIR, $dir)) {
706 print STDERR "$prog: couldn't open directory $dir - $!\n";
709 while (defined ($file = readdir(DIR))) {
710 push(@todo, $file) if $file ne '.' && $file ne '..';
713 foreach $file (@todo) {
714 $file = "$dir/$file";
716 return undef if !&scrub_dir($file);
718 print STDERR "$prog: couldn't rmdir $file - $!\n";
722 if (!unlink($file)) {
723 print STDERR "$prog: couldn't unlink $file - $!\n";
734 local($file, $str) = @_;
736 if (!open(TEMP, "> $file")) {
737 print STDERR "$prog: can't open $file - $!\n";
743 print STDERR "$prog: error writing $file - $!\n";
752 local($name, $file, $what, $expect, $expect_pat) = @_;
757 if (!open(TEMP, "< $file")) {
758 print STDERR "$prog:$name($what): couldn't open $file after running program - $!\n";
766 return compare_output($name, $what, $expect, $expect_pat, $got);
772 local($name, $what, $expect, $expect_pat, $got) = @_;
775 if (defined $expect_pat) {
777 $ret = eval "$expect_pat";
779 print STDERR "$prog:$name($what): error evaluating $what pattern: $expect_pat - $@\n";
783 $why = "\tunexpected $what - wanted pattern";
784 $why .= &wrap_lines($expect_pat);
786 $why .= &wrap_lines($got);
789 $expect = '' if !defined $expect;
790 if ($got ne $expect) {
791 $why .= "\tunexpected $what - " . &first_diff($expect, $got) . "\n";
793 $why .= &wrap_lines($expect);
795 $why .= &wrap_lines($got);
804 local($str, $empty) = @_;
805 local($nonl) = substr($str, -1, 1) ne "\n";
807 return (defined $empty ? $empty : " nothing\n") if $str eq '';
808 substr($str, 0, 0) = ":\n";
809 $str =~ s/\n/\n\t\t/g;
811 $str .= "\n\t[incomplete last line]\n";
822 local($exp, $got) = @_;
823 local($lineno, $char) = (1, 1);
824 local($i, $exp_len, $got_len);
827 $exp_len = length($exp);
828 $got_len = length($got);
829 if ($exp_len != $got_len) {
830 if ($exp_len < $got_len) {
831 if (substr($got, 0, $exp_len) eq $exp) {
832 return "got too much output";
834 } elsif (substr($exp, 0, $got_len) eq $got) {
835 return "got too little output";
838 for ($i = 0; $i < $exp_len; $i++) {
839 $ce = substr($exp, $i, 1);
840 $cg = substr($got, $i, 1);
848 return "first difference: line $lineno, char $char (wanted '"
849 . &format_char($ce) . "', got '"
850 . &format_char($cg) . "'";
861 } elsif ($ch == 13) {
867 } elsif ($ch > 127) {
876 } elsif ($ch == 127) {
879 return $s . sprintf("%c", $ch);
885 local($name, $status, $expect) = @_;
887 local($w, $e, $s) = ($status, ($status >> 8) & 0xff, $status & 0x7f);
889 $e = -1000 if $status & 0xff;
890 $s = -1000 if $s == 0x7f;
891 if (!defined $expect) {
893 } elsif ($expect =~ /^(|-)\d+$/) {
894 $expr = "\$e == $expect";
897 $expr =~ s/\b([wse])\b/\$$1/g;
898 $expr =~ s/\b(SIG[A-Z0-9]+)\b/&$1/g;
902 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $expect ($@)\n";
911 local($file, $in, *test) = @_;
912 local($field, $val, $flags, $do_chop, $need_redo, $start_lineno);
913 local(%cnt, $sfield);
920 last if /^\s*---\s*$/;
921 $start_lineno = $. if !defined $start_lineno;
922 if (!/^([-\w]+):\s*(|\S|\S.*\S)\s*$/) {
923 print STDERR "$prog:$file:$.: unrecognised line\n";
926 ($field, $val) = ($1, $2);
928 $flags = $test_fields{$field};
929 if (!defined $flags) {
930 print STDERR "$prog:$file:$.: unrecognised field \"$field\"\n";
934 local($cnt) = $cnt{$field}++;
935 $test{$field} = $cnt{$field};
936 $cnt = 0 if $cnt eq '';
938 } elsif (defined $test{$field}) {
939 print STDERR "$prog:$file:$.: multiple \"$field\" fields\n";
942 $do_chop = $flags !~ /m/;
944 if ($val eq '' || $val eq '!' || $flags =~ /p/) {
945 if ($flags =~ /[Mm]/) {
955 "$prog:$file:$.: no parameters given for field \"$field\"\n";
968 chop $val if $do_chop;
972 # Syntax check on fields that can several instances
973 # (can give useful line numbers this way)
975 if ($field eq 'file-setup') {
976 local($type, $perm, $rest, $c, $len, $name);
978 # format is: type perm "name"
979 if ($val !~ /^[ \t]*(\S+)[ \t]+(\S+)[ \t]+([^ \t].*)/) {
981 "$prog:$file:$.: bad parameter line for file-setup field\n";
984 ($type, $perm, $rest) = ($1, $2, $3);
985 if ($type !~ /^(file|dir|symlink)$/) {
987 "$prog:$file:$.: bad file type for file-setup: $type\n";
990 if ($perm !~ /^\d+$/) {
992 "$prog:$file:$.: bad permissions for file-setup: $type\n";
995 $c = substr($rest, 0, 1);
996 if (($len = index($rest, $c, 1) - 1) <= 0) {
998 "$prog:$file:$.: missing end quote for file name in file-setup: $rest\n";
1001 $name = substr($rest, 1, $len);
1002 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1003 # Note: this is not a security thing - just a sanity
1004 # check - a test can still use symlinks to get at files
1005 # outside the test directory.
1007 "$prog:$file:$.: file name in file-setup is absolute or contains ..: $name\n";
1011 if ($field eq 'file-result') {
1012 local($type, $perm, $uid, $gid, $matchType,
1013 $rest, $c, $len, $name);
1015 # format is: type perm uid gid matchType "name"
1016 if ($val !~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/) {
1018 "$prog:$file:$.: bad parameter line for file-result field\n";
1021 ($type, $perm, $uid, $gid, $matchType, $rest)
1022 = ($1, $2, $3, $4, $5, $6);
1023 if ($type !~ /^(file|dir|symlink)$/) {
1025 "$prog:$file:$.: bad file type for file-result: $type\n";
1028 if ($perm !~ /^\d+$/ && $perm ne '*') {
1030 "$prog:$file:$.: bad permissions for file-result: $perm\n";
1033 if ($uid !~ /^\d+$/ && $uid ne '*') {
1035 "$prog:$file:$.: bad user-id for file-result: $uid\n";
1038 if ($gid !~ /^\d+$/ && $gid ne '*') {
1040 "$prog:$file:$.: bad group-id for file-result: $gid\n";
1043 if ($matchType !~ /^(exact|pattern)$/) {
1045 "$prog:$file:$.: bad match type for file-result: $matchType\n";
1048 $c = substr($rest, 0, 1);
1049 if (($len = index($rest, $c, 1) - 1) <= 0) {
1051 "$prog:$file:$.: missing end quote for file name in file-result: $rest\n";
1054 $name = substr($rest, 1, $len);
1055 if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) {
1056 # Note: this is not a security thing - just a sanity
1057 # check - a test can still use symlinks to get at files
1058 # outside the test directory.
1060 "$prog:$file:$.: file name in file-result is absolute or contains ..: $name\n";
1064 } elsif ($val eq '') {
1066 "$prog:$file:$.: no value given for field \"$field\"\n";
1070 $val .= "\n" if !$do_chop;
1071 $test{$sfield} = $val;
1077 "$prog:$file:$start_lineno: end-of-file while reading test\n";
1083 while (($field, $val) = each %test_fields) {
1084 if ($val =~ /r/ && !defined $test{$field}) {
1086 "$prog:$file:$start_lineno: required field \"$field\" missing\n";
1091 $test{':full-name'} = substr($file, $file_prefix_skip) . ":$test{'name'}";
1092 $test{':long-name'} = "$file:$start_lineno:$test{'name'}";
1094 # Syntax check on specific fields
1095 if (defined $test{'expected-fail'}) {
1096 if ($test{'expected-fail'} !~ /^(yes|no)$/) {
1098 "$prog:$test{':long-name'}: bad value for expected-fail field\n";
1101 $test{'expected-fail'} = $1 eq 'yes';
1103 $test{'expected-fail'} = 0;
1105 if (defined $test{'need-ctty'}) {
1106 if ($test{'need-ctty'} !~ /^(yes|no)$/) {
1108 "$prog:$test{':long-name'}: bad value for need-ctty field\n";
1111 $test{'need-ctty'} = $1 eq 'yes';
1113 $test{'need-ctty'} = 0;
1115 if (defined $test{'need-pass'}) {
1116 if ($test{'need-pass'} !~ /^(yes|no)$/) {
1118 "$prog:$test{':long-name'}: bad value for need-pass field\n";
1121 $test{'need-pass'} = $1 eq 'yes';
1123 $test{'need-pass'} = 1;
1125 if (defined $test{'arguments'}) {
1126 local($firstc) = substr($test{'arguments'}, 0, 1);
1128 if (substr($test{'arguments'}, -1, 1) ne $firstc) {
1129 print STDERR "$prog:$test{':long-name'}: arguments field doesn't start and end with the same character\n";
1133 if (defined $test{'env-setup'}) {
1134 local($firstc) = substr($test{'env-setup'}, 0, 1);
1136 if (substr($test{'env-setup'}, -1, 1) ne $firstc) {
1137 print STDERR "$prog:$test{':long-name'}: env-setup field doesn't start and end with the same character\n";
1141 if (defined $test{'expected-exit'}) {
1142 local($val) = $test{'expected-exit'};
1144 if ($val =~ /^(|-)\d+$/) {
1145 if ($val < 0 || $val > 255) {
1146 print STDERR "$prog:$test{':long-name'}: expected-exit value $val not in 0..255\n";
1149 } elsif ($val !~ /^([\s<>+-=*%\/&|!()]|\b[wse]\b|\bSIG[A-Z0-9]+\b)+$/) {
1150 print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $val\n";
1154 $test{'expected-exit'} = 0;
1156 if (defined $test{'expected-stdout'}
1157 && defined $test{'expected-stdout-pattern'})
1159 print STDERR "$prog:$test{':long-name'}: can't use both expected-stdout and expected-stdout-pattern\n";
1162 if (defined $test{'expected-stderr'}
1163 && defined $test{'expected-stderr-pattern'})
1165 print STDERR "$prog:$test{':long-name'}: can't use both expected-stderr and expected-stderr-pattern\n";
1168 if (defined $test{'time-limit'}) {
1169 if ($test{'time-limit'} !~ /^\d+$/ || $test{'time-limit'} == 0) {
1171 "$prog:$test{':long-name'}: bad value for time-limit field\n";
1174 } elsif (defined $default_time_limit) {
1175 $test{'time-limit'} = $default_time_limit;
1178 if (defined $known_tests{$test{'name'}}) {
1179 print STDERR "$prog:$test{':long-name'}: warning: duplicate test name ${test{'name'}}\n";
1181 $known_tests{$test{'name'}} = 1;
1191 open(TTY, "> /dev/tty") || return 0;
1202 &never_called_funcs();
1204 $old_env{'foo'} = 'bar';
1205 $internal_test_fields{'foo'} = 'bar';
1213 return '' if (!defined $test{'file-result'});
1217 local($type, $perm, $uid, $gid, $rest, $c, $len, $name);
1220 for ($i = 0; $i < $test{'file-result'}; $i++) {
1221 $val = $test{"file-result:$i"};
1223 # format is: type perm "name"
1224 ($type, $perm, $uid, $gid, $matchType, $rest) =
1225 split(' ', $val, 6);
1226 $c = substr($rest, 0, 1);
1227 $len = index($rest, $c, 1) - 1;
1228 $name = substr($rest, 1, $len);
1229 $rest = substr($rest, 2 + $len);
1230 $perm = oct($perm) if $perm =~ /^\d+$/;
1232 @stbuf = lstat($name);
1234 $why .= "\texpected $type \"$name\" not created\n";
1237 if ($perm ne '*' && ($stbuf[2] & 07777) != $perm) {
1238 $why .= "\t$type \"$name\" has unexpected permissions\n";
1239 $why .= sprintf("\t\texpected 0%o, found 0%o\n",
1240 $perm, $stbuf[2] & 07777);
1242 if ($uid ne '*' && $stbuf[4] != $uid) {
1243 $why .= "\t$type \"$name\" has unexpected user-id\n";
1244 $why .= sprintf("\t\texpected %d, found %d\n",
1247 if ($gid ne '*' && $stbuf[5] != $gid) {
1248 $why .= "\t$type \"$name\" has unexpected group-id\n";
1249 $why .= sprintf("\t\texpected %d, found %d\n",
1253 if ($type eq 'file') {
1254 if (-l _ || ! -f _) {
1255 $why .= "\t$type \"$name\" is not a regular file\n";
1257 local $tmp = &check_output($test{'long-name'}, $name,
1258 "$type contents in \"$name\"",
1259 $matchType eq 'exact' ? $rest : undef
1260 $matchType eq 'pattern' ? $rest : undef);
1261 return undef if (!defined $tmp);
1264 } elsif ($type eq 'dir') {
1265 if ($rest !~ /^\s*$/) {
1266 print STDERR "$prog:$test{':long-name'}: file-result test for directory $name should not have content specified\n";
1269 if (-l _ || ! -d _) {
1270 $why .= "\t$type \"$name\" is not a directory\n";
1272 } elsif ($type eq 'symlink') {
1274 $why .= "\t$type \"$name\" is not a symlink\n";
1276 local $content = readlink($name);
1277 if (!defined $content) {
1278 print STDERR "$prog:$test{':long-name'}: file-result test for $type $name failed - could not readlink - $!\n";
1281 local $tmp = &compare_output($test{'long-name'},
1282 "$type contents in \"$name\"",
1283 $matchType eq 'exact' ? $rest : undef
1284 $matchType eq 'pattern' ? $rest : undef);
1285 return undef if (!defined $tmp);
1297 print STDERR $Usage;