1c84f3f3cSopenharmony_ci# $MirOS: src/bin/mksh/check.pl,v 1.51 2020/06/22 17:10:59 tg Exp $ 2c84f3f3cSopenharmony_ci# $OpenBSD: th,v 1.1 2013/12/02 20:39:44 millert Exp $ 3c84f3f3cSopenharmony_ci#- 4c84f3f3cSopenharmony_ci# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, 5c84f3f3cSopenharmony_ci# 2012, 2013, 2014, 2015, 2017 6c84f3f3cSopenharmony_ci# mirabilos <m@mirbsd.org> 7c84f3f3cSopenharmony_ci# 8c84f3f3cSopenharmony_ci# Provided that these terms and disclaimer and all copyright notices 9c84f3f3cSopenharmony_ci# are retained or reproduced in an accompanying document, permission 10c84f3f3cSopenharmony_ci# is granted to deal in this work without restriction, including un- 11c84f3f3cSopenharmony_ci# limited rights to use, publicly perform, distribute, sell, modify, 12c84f3f3cSopenharmony_ci# merge, give away, or sublicence. 13c84f3f3cSopenharmony_ci# 14c84f3f3cSopenharmony_ci# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to 15c84f3f3cSopenharmony_ci# the utmost extent permitted by applicable law, neither express nor 16c84f3f3cSopenharmony_ci# implied; without malicious intent or gross negligence. In no event 17c84f3f3cSopenharmony_ci# may a licensor, author or contributor be held liable for indirect, 18c84f3f3cSopenharmony_ci# direct, other damage, loss, or other issues arising in any way out 19c84f3f3cSopenharmony_ci# of dealing in the work, even if advised of the possibility of such 20c84f3f3cSopenharmony_ci# damage or existence of a defect, except proven that it results out 21c84f3f3cSopenharmony_ci# of said person's immediate fault when using the work as intended. 22c84f3f3cSopenharmony_ci#- 23c84f3f3cSopenharmony_ci# Example test: 24c84f3f3cSopenharmony_ci# name: a-test 25c84f3f3cSopenharmony_ci# description: 26c84f3f3cSopenharmony_ci# a test to show how tests are done 27c84f3f3cSopenharmony_ci# arguments: !-x!-f! 28c84f3f3cSopenharmony_ci# stdin: 29c84f3f3cSopenharmony_ci# echo -n * 30c84f3f3cSopenharmony_ci# false 31c84f3f3cSopenharmony_ci# expected-stdout: ! 32c84f3f3cSopenharmony_ci# * 33c84f3f3cSopenharmony_ci# expected-stderr: 34c84f3f3cSopenharmony_ci# + echo -n * 35c84f3f3cSopenharmony_ci# + false 36c84f3f3cSopenharmony_ci# expected-exit: 1 37c84f3f3cSopenharmony_ci# --- 38c84f3f3cSopenharmony_ci# This runs the test-program (eg, mksh) with the arguments -x and -f, 39c84f3f3cSopenharmony_ci# standard input is a file containing "echo hi*\nfalse\n". The program 40c84f3f3cSopenharmony_ci# is expected to produce "hi*" (no trailing newline) on standard output, 41c84f3f3cSopenharmony_ci# "+ echo hi*\n+false\n" on standard error, and an exit code of 1. 42c84f3f3cSopenharmony_ci# 43c84f3f3cSopenharmony_ci# 44c84f3f3cSopenharmony_ci# Format of test files: 45c84f3f3cSopenharmony_ci# - blank lines and lines starting with # are ignored 46c84f3f3cSopenharmony_ci# - a test file contains a series of tests 47c84f3f3cSopenharmony_ci# - a test is a series of tag:value pairs ended with a "---" line 48c84f3f3cSopenharmony_ci# (leading/trailing spaces are stripped from the first line of value) 49c84f3f3cSopenharmony_ci# - test tags are: 50c84f3f3cSopenharmony_ci# Tag Flag Description 51c84f3f3cSopenharmony_ci# ----- ---- ----------- 52c84f3f3cSopenharmony_ci# name r The name of the test; should be unique 53c84f3f3cSopenharmony_ci# description m What test does 54c84f3f3cSopenharmony_ci# arguments M Arguments to pass to the program; 55c84f3f3cSopenharmony_ci# default is no arguments. 56c84f3f3cSopenharmony_ci# script m Value is written to a file which 57c84f3f3cSopenharmony_ci# is passed as an argument to the program 58c84f3f3cSopenharmony_ci# (after the arguments from arguments) 59c84f3f3cSopenharmony_ci# stdin m Value is written to a file which is 60c84f3f3cSopenharmony_ci# used as standard-input for the program; 61c84f3f3cSopenharmony_ci# default is to use /dev/null. 62c84f3f3cSopenharmony_ci# perl-setup m Value is a perl script which is executed 63c84f3f3cSopenharmony_ci# just before the test is run. Try to 64c84f3f3cSopenharmony_ci# avoid using this... 65c84f3f3cSopenharmony_ci# perl-cleanup m Value is a perl script which is executed 66c84f3f3cSopenharmony_ci# just after the test is run. Try to 67c84f3f3cSopenharmony_ci# avoid using this... 68c84f3f3cSopenharmony_ci# env-setup M Value is a list of NAME=VALUE elements 69c84f3f3cSopenharmony_ci# which are put in the environment before 70c84f3f3cSopenharmony_ci# the test is run. If the =VALUE is 71c84f3f3cSopenharmony_ci# missing, NAME is removed from the 72c84f3f3cSopenharmony_ci# environment. Programs are run with 73c84f3f3cSopenharmony_ci# the following minimal environment: 74c84f3f3cSopenharmony_ci# HOME, LD_LIBRARY_PATH, LOCPATH, 75c84f3f3cSopenharmony_ci# LOGNAME, PATH, SHELL, UNIXMODE, 76c84f3f3cSopenharmony_ci# UNIXROOT, USER 77c84f3f3cSopenharmony_ci# (values taken from the environment of 78c84f3f3cSopenharmony_ci# the test harness). 79c84f3f3cSopenharmony_ci# CYGWIN is set to nodosfilewarning. 80c84f3f3cSopenharmony_ci# ENV is set to /nonexistant. 81c84f3f3cSopenharmony_ci# __progname is set to the -p argument. 82c84f3f3cSopenharmony_ci# __perlname is set to $^X (perlexe). 83c84f3f3cSopenharmony_ci# @utflocale@ is substituted from -U. 84c84f3f3cSopenharmony_ci# file-setup mps Used to create files, directories 85c84f3f3cSopenharmony_ci# and symlinks. First word is either 86c84f3f3cSopenharmony_ci# file, dir or symlink; second word is 87c84f3f3cSopenharmony_ci# permissions; this is followed by a 88c84f3f3cSopenharmony_ci# quoted word that is the name of the 89c84f3f3cSopenharmony_ci# file; the end-quote should be followed 90c84f3f3cSopenharmony_ci# by a newline, then the file data 91c84f3f3cSopenharmony_ci# (if any). The first word may be 92c84f3f3cSopenharmony_ci# preceded by a ! to strip the trailing 93c84f3f3cSopenharmony_ci# newline in a symlink. 94c84f3f3cSopenharmony_ci# file-result mps Used to verify a file, symlink or 95c84f3f3cSopenharmony_ci# directory is created correctly. 96c84f3f3cSopenharmony_ci# The first word is either 97c84f3f3cSopenharmony_ci# file, dir or symlink; second word is 98c84f3f3cSopenharmony_ci# expected permissions; third word 99c84f3f3cSopenharmony_ci# is user-id; fourth is group-id; 100c84f3f3cSopenharmony_ci# fifth is "exact" or "pattern" 101c84f3f3cSopenharmony_ci# indicating whether the file contents 102c84f3f3cSopenharmony_ci# which follow is to be matched exactly 103c84f3f3cSopenharmony_ci# or if it is a regular expression. 104c84f3f3cSopenharmony_ci# The fifth argument is the quoted name 105c84f3f3cSopenharmony_ci# of the file that should be created. 106c84f3f3cSopenharmony_ci# The end-quote should be followed 107c84f3f3cSopenharmony_ci# by a newline, then the file data 108c84f3f3cSopenharmony_ci# (if any). The first word may be 109c84f3f3cSopenharmony_ci# preceded by a ! to strip the trailing 110c84f3f3cSopenharmony_ci# newline in the file contents. 111c84f3f3cSopenharmony_ci# The permissions, user and group fields 112c84f3f3cSopenharmony_ci# may be * meaning accept any value. 113c84f3f3cSopenharmony_ci# time-limit Time limit - the program is sent a 114c84f3f3cSopenharmony_ci# SIGKILL N seconds. Default is no 115c84f3f3cSopenharmony_ci# limit. 116c84f3f3cSopenharmony_ci# expected-fail 'yes' if the test is expected to fail. 117c84f3f3cSopenharmony_ci# expected-exit expected exit code. Can be a number, 118c84f3f3cSopenharmony_ci# or a C expression using the variables 119c84f3f3cSopenharmony_ci# e, s and w (exit code, termination 120c84f3f3cSopenharmony_ci# signal, and status code). 121c84f3f3cSopenharmony_ci# expected-stdout m What the test should generate on stdout; 122c84f3f3cSopenharmony_ci# default is to expect no output. 123c84f3f3cSopenharmony_ci# expected-stdout-pattern m A perl pattern which matches the 124c84f3f3cSopenharmony_ci# expected output. 125c84f3f3cSopenharmony_ci# expected-stderr m What the test should generate on stderr; 126c84f3f3cSopenharmony_ci# default is to expect no output. 127c84f3f3cSopenharmony_ci# expected-stderr-pattern m A perl pattern which matches the 128c84f3f3cSopenharmony_ci# expected standard error. 129c84f3f3cSopenharmony_ci# category m Specify a comma separated list of 130c84f3f3cSopenharmony_ci# 'categories' of program that the test 131c84f3f3cSopenharmony_ci# is to be run for. A category can be 132c84f3f3cSopenharmony_ci# negated by prefixing the name with a !. 133c84f3f3cSopenharmony_ci# The idea is that some tests in a 134c84f3f3cSopenharmony_ci# test suite may apply to a particular 135c84f3f3cSopenharmony_ci# program version and shouldn't be run 136c84f3f3cSopenharmony_ci# on other versions. The category(s) of 137c84f3f3cSopenharmony_ci# the program being tested can be 138c84f3f3cSopenharmony_ci# specified on the command line. 139c84f3f3cSopenharmony_ci# One category os:XXX is predefined 140c84f3f3cSopenharmony_ci# (XXX is the operating system name, 141c84f3f3cSopenharmony_ci# eg, linux, dec_osf). 142c84f3f3cSopenharmony_ci# need-ctty 'yes' if the test needs a ctty, run 143c84f3f3cSopenharmony_ci# with -C regress:no-ctty to disable. 144c84f3f3cSopenharmony_ci# Flag meanings: 145c84f3f3cSopenharmony_ci# r tag is required (eg, a test must have a name tag). 146c84f3f3cSopenharmony_ci# m value can be multiple lines. Lines must be prefixed with 147c84f3f3cSopenharmony_ci# a tab. If the value part of the initial tag:value line is 148c84f3f3cSopenharmony_ci# - empty: the initial blank line is stripped. 149c84f3f3cSopenharmony_ci# - a lone !: the last newline in the value is stripped; 150c84f3f3cSopenharmony_ci# M value can be multiple lines (prefixed by a tab) and consists 151c84f3f3cSopenharmony_ci# of multiple fields, delimited by a field separator character. 152c84f3f3cSopenharmony_ci# The value must start and end with the f-s-c. 153c84f3f3cSopenharmony_ci# p tag takes parameters (used with m). 154c84f3f3cSopenharmony_ci# s tag can be used several times. 155c84f3f3cSopenharmony_ci 156c84f3f3cSopenharmony_ci# require Config only if it exists 157c84f3f3cSopenharmony_ci# pull EINTR from POSIX.pm or Errno.pm if they exist 158c84f3f3cSopenharmony_ci# otherwise just skip it 159c84f3f3cSopenharmony_ciBEGIN { 160c84f3f3cSopenharmony_ci eval { 161c84f3f3cSopenharmony_ci require Config; 162c84f3f3cSopenharmony_ci import Config; 163c84f3f3cSopenharmony_ci 1; 164c84f3f3cSopenharmony_ci }; 165c84f3f3cSopenharmony_ci $EINTR = 0; 166c84f3f3cSopenharmony_ci eval { 167c84f3f3cSopenharmony_ci require POSIX; 168c84f3f3cSopenharmony_ci $EINTR = POSIX::EINTR(); 169c84f3f3cSopenharmony_ci }; 170c84f3f3cSopenharmony_ci if ($@) { 171c84f3f3cSopenharmony_ci eval { 172c84f3f3cSopenharmony_ci require Errno; 173c84f3f3cSopenharmony_ci $EINTR = Errno::EINTR(); 174c84f3f3cSopenharmony_ci } or do { 175c84f3f3cSopenharmony_ci $EINTR = 0; 176c84f3f3cSopenharmony_ci }; 177c84f3f3cSopenharmony_ci } 178c84f3f3cSopenharmony_ci}; 179c84f3f3cSopenharmony_ci 180c84f3f3cSopenharmony_ciuse Getopt::Std; 181c84f3f3cSopenharmony_ci 182c84f3f3cSopenharmony_ci$os = defined $^O ? $^O : 'unknown'; 183c84f3f3cSopenharmony_ci 184c84f3f3cSopenharmony_ci($prog = $0) =~ s#.*/##; 185c84f3f3cSopenharmony_ci 186c84f3f3cSopenharmony_ci$Usage = <<EOF ; 187c84f3f3cSopenharmony_ciUsage: $prog [-Pv] [-C cat] [-e e=v] [-p prog] [-s fn] [-T dir] \ 188c84f3f3cSopenharmony_ci [-t tmo] [-U lcl] name ... 189c84f3f3cSopenharmony_ci -C c Specify the comma separated list of categories the program 190c84f3f3cSopenharmony_ci belongs to (see category field). 191c84f3f3cSopenharmony_ci -e e=v Set the environment variable e to v for all tests 192c84f3f3cSopenharmony_ci (if no =v is given, the current value is used) 193c84f3f3cSopenharmony_ci Only one -e option can be given at the moment, sadly. 194c84f3f3cSopenharmony_ci -P program (-p) string has multiple words, and the program is in 195c84f3f3cSopenharmony_ci the path (kludge option) 196c84f3f3cSopenharmony_ci -p p Use p as the program to test 197c84f3f3cSopenharmony_ci -s s Read tests from file s; if s is a directory, it is recursively 198c84f3f3cSopenharmony_ci scanned for test files (which end in .t). 199c84f3f3cSopenharmony_ci -T dir Use dir instead of /tmp to hold temporary files 200c84f3f3cSopenharmony_ci -t t Use t as default time limit for tests (default is unlimited) 201c84f3f3cSopenharmony_ci -U lcl Use lcl as UTF-8 locale (e.g. C.UTF-8) instead of the default 202c84f3f3cSopenharmony_ci -v Verbose mode: print reason test failed. 203c84f3f3cSopenharmony_ci name specifies the name of the test(s) to run; if none are 204c84f3f3cSopenharmony_ci specified, all tests are run. 205c84f3f3cSopenharmony_ciEOF 206c84f3f3cSopenharmony_ci 207c84f3f3cSopenharmony_ci# See comment above for flag meanings 208c84f3f3cSopenharmony_ci%test_fields = ( 209c84f3f3cSopenharmony_ci 'name', 'r', 210c84f3f3cSopenharmony_ci 'description', 'm', 211c84f3f3cSopenharmony_ci 'arguments', 'M', 212c84f3f3cSopenharmony_ci 'script', 'm', 213c84f3f3cSopenharmony_ci 'stdin', 'm', 214c84f3f3cSopenharmony_ci 'perl-setup', 'm', 215c84f3f3cSopenharmony_ci 'perl-cleanup', 'm', 216c84f3f3cSopenharmony_ci 'env-setup', 'M', 217c84f3f3cSopenharmony_ci 'file-setup', 'mps', 218c84f3f3cSopenharmony_ci 'file-result', 'mps', 219c84f3f3cSopenharmony_ci 'time-limit', '', 220c84f3f3cSopenharmony_ci 'expected-fail', '', 221c84f3f3cSopenharmony_ci 'expected-exit', '', 222c84f3f3cSopenharmony_ci 'expected-stdout', 'm', 223c84f3f3cSopenharmony_ci 'expected-stdout-pattern', 'm', 224c84f3f3cSopenharmony_ci 'expected-stderr', 'm', 225c84f3f3cSopenharmony_ci 'expected-stderr-pattern', 'm', 226c84f3f3cSopenharmony_ci 'category', 'm', 227c84f3f3cSopenharmony_ci 'need-ctty', '', 228c84f3f3cSopenharmony_ci 'need-pass', '', 229c84f3f3cSopenharmony_ci ); 230c84f3f3cSopenharmony_ci# Filled in by read_test() 231c84f3f3cSopenharmony_ci%internal_test_fields = ( 232c84f3f3cSopenharmony_ci ':full-name', 1, # file:name 233c84f3f3cSopenharmony_ci ':long-name', 1, # dir/file:lineno:name 234c84f3f3cSopenharmony_ci ); 235c84f3f3cSopenharmony_ci 236c84f3f3cSopenharmony_ci# Categories of the program under test. Provide the current 237c84f3f3cSopenharmony_ci# os by default. 238c84f3f3cSopenharmony_ci%categories = ( 239c84f3f3cSopenharmony_ci "os:$os", '1' 240c84f3f3cSopenharmony_ci ); 241c84f3f3cSopenharmony_ci 242c84f3f3cSopenharmony_ci$nfailed = 0; 243c84f3f3cSopenharmony_ci$nifailed = 0; 244c84f3f3cSopenharmony_ci$nxfailed = 0; 245c84f3f3cSopenharmony_ci$npassed = 0; 246c84f3f3cSopenharmony_ci$nxpassed = 0; 247c84f3f3cSopenharmony_ci 248c84f3f3cSopenharmony_ci%known_tests = (); 249c84f3f3cSopenharmony_ci 250c84f3f3cSopenharmony_ciif (!getopts('C:Ee:Pp:s:T:t:U:v')) { 251c84f3f3cSopenharmony_ci print STDERR $Usage; 252c84f3f3cSopenharmony_ci exit 1; 253c84f3f3cSopenharmony_ci} 254c84f3f3cSopenharmony_ci 255c84f3f3cSopenharmony_cidie "$prog: no program specified (use -p)\n" if !defined $opt_p; 256c84f3f3cSopenharmony_cidie "$prog: no test set specified (use -s)\n" if !defined $opt_s; 257c84f3f3cSopenharmony_ci$test_prog = $opt_p; 258c84f3f3cSopenharmony_ci$verbose = defined $opt_v && $opt_v; 259c84f3f3cSopenharmony_ci$is_ebcdic = defined $opt_E && $opt_E; 260c84f3f3cSopenharmony_ci$test_set = $opt_s; 261c84f3f3cSopenharmony_ci$temp_base = $opt_T || "/tmp"; 262c84f3f3cSopenharmony_ci$utflocale = $opt_U || (($os eq "hpux") ? "en_US.utf8" : "en_US.UTF-8"); 263c84f3f3cSopenharmony_ciif (defined $opt_t) { 264c84f3f3cSopenharmony_ci die "$prog: bad -t argument (should be number > 0): $opt_t\n" 265c84f3f3cSopenharmony_ci if $opt_t !~ /^\d+$/ || $opt_t <= 0; 266c84f3f3cSopenharmony_ci $default_time_limit = $opt_t; 267c84f3f3cSopenharmony_ci} 268c84f3f3cSopenharmony_ci$program_kludge = defined $opt_P ? $opt_P : 0; 269c84f3f3cSopenharmony_ci 270c84f3f3cSopenharmony_ciif ($is_ebcdic) { 271c84f3f3cSopenharmony_ci $categories{'shell:ebcdic-yes'} = 1; 272c84f3f3cSopenharmony_ci $categories{'shell:ascii-no'} = 1; 273c84f3f3cSopenharmony_ci} else { 274c84f3f3cSopenharmony_ci $categories{'shell:ebcdic-no'} = 1; 275c84f3f3cSopenharmony_ci $categories{'shell:ascii-yes'} = 1; 276c84f3f3cSopenharmony_ci} 277c84f3f3cSopenharmony_ci 278c84f3f3cSopenharmony_ciif (defined $opt_C) { 279c84f3f3cSopenharmony_ci foreach $c (split(',', $opt_C)) { 280c84f3f3cSopenharmony_ci $c =~ s/\s+//; 281c84f3f3cSopenharmony_ci die "$prog: categories can't be negated on the command line\n" 282c84f3f3cSopenharmony_ci if ($c =~ /^!/); 283c84f3f3cSopenharmony_ci $categories{$c} = 1; 284c84f3f3cSopenharmony_ci } 285c84f3f3cSopenharmony_ci} 286c84f3f3cSopenharmony_ci 287c84f3f3cSopenharmony_ci# Note which tests are to be run. 288c84f3f3cSopenharmony_ci%do_test = (); 289c84f3f3cSopenharmony_cigrep($do_test{$_} = 1, @ARGV); 290c84f3f3cSopenharmony_ci$all_tests = @ARGV == 0; 291c84f3f3cSopenharmony_ci 292c84f3f3cSopenharmony_ci# Set up a very minimal environment 293c84f3f3cSopenharmony_ci%new_env = (); 294c84f3f3cSopenharmony_ciforeach $env (('HOME', 'LD_LIBRARY_PATH', 'LOCPATH', 'LOGNAME', 295c84f3f3cSopenharmony_ci 'PATH', 'PERLIO', 'SHELL', 'UNIXMODE', 'UNIXROOT', 'USER')) { 296c84f3f3cSopenharmony_ci $new_env{$env} = $ENV{$env} if defined $ENV{$env}; 297c84f3f3cSopenharmony_ci} 298c84f3f3cSopenharmony_ci$new_env{'CYGWIN'} = 'nodosfilewarning'; 299c84f3f3cSopenharmony_ci$new_env{'ENV'} = '/nonexistant'; 300c84f3f3cSopenharmony_ci 301c84f3f3cSopenharmony_ciif (($os eq 'VMS') || ($Config{perlpath} =~ m/$Config{_exe}$/i)) { 302c84f3f3cSopenharmony_ci $new_env{'__perlname'} = $Config{perlpath}; 303c84f3f3cSopenharmony_ci} else { 304c84f3f3cSopenharmony_ci $new_env{'__perlname'} = $Config{perlpath} . $Config{_exe}; 305c84f3f3cSopenharmony_ci} 306c84f3f3cSopenharmony_ci$new_env{'__perlname'} = $^X if ($new_env{'__perlname'} eq '') and -f $^X and -x $^X; 307c84f3f3cSopenharmony_ciif ($new_env{'__perlname'} eq '') { 308c84f3f3cSopenharmony_ci foreach $pathelt (split /:/,$ENV{'PATH'}) { 309c84f3f3cSopenharmony_ci chomp($pathelt = `pwd`) if $pathelt eq ''; 310c84f3f3cSopenharmony_ci my $x = $pathelt . '/' . $^X; 311c84f3f3cSopenharmony_ci next unless -f $x and -x $x; 312c84f3f3cSopenharmony_ci $new_env{'__perlname'} = $x; 313c84f3f3cSopenharmony_ci last; 314c84f3f3cSopenharmony_ci } 315c84f3f3cSopenharmony_ci} 316c84f3f3cSopenharmony_ci$new_env{'__perlname'} = $^X if ($new_env{'__perlname'} eq ''); 317c84f3f3cSopenharmony_ci 318c84f3f3cSopenharmony_ciif (defined $opt_e) { 319c84f3f3cSopenharmony_ci # XXX need a way to allow many -e arguments... 320c84f3f3cSopenharmony_ci if ($opt_e =~ /^([a-zA-Z_]\w*)(|=(.*))$/) { 321c84f3f3cSopenharmony_ci $new_env{$1} = $2 eq '' ? $ENV{$1} : $3; 322c84f3f3cSopenharmony_ci } else { 323c84f3f3cSopenharmony_ci die "$0: bad -e argument: $opt_e\n"; 324c84f3f3cSopenharmony_ci } 325c84f3f3cSopenharmony_ci} 326c84f3f3cSopenharmony_ci%old_env = %ENV; 327c84f3f3cSopenharmony_ci 328c84f3f3cSopenharmony_cichop($pwd = `pwd 2>/dev/null`); 329c84f3f3cSopenharmony_cidie "$prog: couldn't get current working directory\n" if $pwd eq ''; 330c84f3f3cSopenharmony_cidie "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd); 331c84f3f3cSopenharmony_ci 332c84f3f3cSopenharmony_cidie "$prog: couldn't cd to $temp_base - $!\n" if !chdir($temp_base); 333c84f3f3cSopenharmony_cidie "$prog: couldn't get temporary directory base\n" unless -d '.'; 334c84f3f3cSopenharmony_ci$temps = sprintf("chk%d-%d.", $$, time()); 335c84f3f3cSopenharmony_ci$tempi = 0; 336c84f3f3cSopenharmony_ciuntil (mkdir(($tempdir = sprintf("%s%03d", $temps, $tempi)), 0700)) { 337c84f3f3cSopenharmony_ci die "$prog: couldn't get temporary directory\n" if $tempi++ >= 999; 338c84f3f3cSopenharmony_ci} 339c84f3f3cSopenharmony_cidie "$prog: couldn't cd to $tempdir - $!\n" if !chdir($tempdir); 340c84f3f3cSopenharmony_cichop($temp_dir = `pwd 2>/dev/null`); 341c84f3f3cSopenharmony_cidie "$prog: couldn't get temporary directory\n" if $temp_dir eq ''; 342c84f3f3cSopenharmony_cidie "$prog: couldn't cd to $pwd - $!\n" if !chdir($pwd); 343c84f3f3cSopenharmony_ci 344c84f3f3cSopenharmony_ciif (!$program_kludge) { 345c84f3f3cSopenharmony_ci $test_prog = "$pwd/$test_prog" if (substr($test_prog, 0, 1) ne '/') && 346c84f3f3cSopenharmony_ci ($os ne 'os2' || substr($test_prog, 1, 1) ne ':'); 347c84f3f3cSopenharmony_ci die "$prog: $test_prog is not executable - bye\n" 348c84f3f3cSopenharmony_ci if (! -x $test_prog && $os ne 'os2'); 349c84f3f3cSopenharmony_ci} 350c84f3f3cSopenharmony_ci 351c84f3f3cSopenharmony_ci@trap_sigs = ('TERM', 'QUIT', 'INT', 'PIPE', 'HUP'); 352c84f3f3cSopenharmony_ci@SIG{@trap_sigs} = ('cleanup_exit') x @trap_sigs; 353c84f3f3cSopenharmony_ci$child_kill_ok = 0; 354c84f3f3cSopenharmony_ci$SIG{'ALRM'} = 'catch_sigalrm'; 355c84f3f3cSopenharmony_ci 356c84f3f3cSopenharmony_ci$| = 1; 357c84f3f3cSopenharmony_ci 358c84f3f3cSopenharmony_ci# Create temp files 359c84f3f3cSopenharmony_ci$temps = "${temp_dir}/rts"; 360c84f3f3cSopenharmony_ci$tempi = "${temp_dir}/rti"; 361c84f3f3cSopenharmony_ci$tempo = "${temp_dir}/rto"; 362c84f3f3cSopenharmony_ci$tempe = "${temp_dir}/rte"; 363c84f3f3cSopenharmony_ci$tempdir = "${temp_dir}/rtd"; 364c84f3f3cSopenharmony_cimkdir($tempdir, 0700) or die "$prog: couldn't mkdir $tempdir - $!\n"; 365c84f3f3cSopenharmony_ci 366c84f3f3cSopenharmony_ciif (-d $test_set) { 367c84f3f3cSopenharmony_ci $file_prefix_skip = length($test_set) + 1; 368c84f3f3cSopenharmony_ci $ret = &process_test_dir($test_set); 369c84f3f3cSopenharmony_ci} else { 370c84f3f3cSopenharmony_ci $file_prefix_skip = 0; 371c84f3f3cSopenharmony_ci $ret = &process_test_file($test_set); 372c84f3f3cSopenharmony_ci} 373c84f3f3cSopenharmony_ci&cleanup_exit() if !defined $ret; 374c84f3f3cSopenharmony_ci 375c84f3f3cSopenharmony_ci$tot_failed = $nfailed + $nifailed + $nxfailed; 376c84f3f3cSopenharmony_ci$tot_passed = $npassed + $nxpassed; 377c84f3f3cSopenharmony_ciif ($tot_failed || $tot_passed) { 378c84f3f3cSopenharmony_ci print "Total failed: $tot_failed"; 379c84f3f3cSopenharmony_ci print " ($nifailed ignored)" if $nifailed; 380c84f3f3cSopenharmony_ci print " ($nxfailed unexpected)" if $nxfailed; 381c84f3f3cSopenharmony_ci print " (as expected)" if $nfailed && !$nxfailed && !$nifailed; 382c84f3f3cSopenharmony_ci print " ($nfailed expected)" if $nfailed && ($nxfailed || $nifailed); 383c84f3f3cSopenharmony_ci print "\nTotal passed: $tot_passed"; 384c84f3f3cSopenharmony_ci print " ($nxpassed unexpected)" if $nxpassed; 385c84f3f3cSopenharmony_ci print "\n"; 386c84f3f3cSopenharmony_ci} 387c84f3f3cSopenharmony_ci 388c84f3f3cSopenharmony_ci&cleanup_exit('ok'); 389c84f3f3cSopenharmony_ci 390c84f3f3cSopenharmony_cisub 391c84f3f3cSopenharmony_cicleanup_exit 392c84f3f3cSopenharmony_ci{ 393c84f3f3cSopenharmony_ci local($sig, $exitcode) = ('', 1); 394c84f3f3cSopenharmony_ci 395c84f3f3cSopenharmony_ci if ($_[0] eq 'ok') { 396c84f3f3cSopenharmony_ci unless ($nxfailed) { 397c84f3f3cSopenharmony_ci $exitcode = 0; 398c84f3f3cSopenharmony_ci } else { 399c84f3f3cSopenharmony_ci $exitcode = 1; 400c84f3f3cSopenharmony_ci } 401c84f3f3cSopenharmony_ci } elsif ($_[0] ne '') { 402c84f3f3cSopenharmony_ci $sig = $_[0]; 403c84f3f3cSopenharmony_ci } 404c84f3f3cSopenharmony_ci 405c84f3f3cSopenharmony_ci unlink($tempi, $tempo, $tempe, $temps); 406c84f3f3cSopenharmony_ci &scrub_dir($tempdir) if defined $tempdir; 407c84f3f3cSopenharmony_ci rmdir($tempdir) if defined $tempdir; 408c84f3f3cSopenharmony_ci rmdir($temp_dir) if defined $temp_dir; 409c84f3f3cSopenharmony_ci 410c84f3f3cSopenharmony_ci if ($sig) { 411c84f3f3cSopenharmony_ci $SIG{$sig} = 'DEFAULT'; 412c84f3f3cSopenharmony_ci kill $sig, $$; 413c84f3f3cSopenharmony_ci return; 414c84f3f3cSopenharmony_ci } 415c84f3f3cSopenharmony_ci exit $exitcode; 416c84f3f3cSopenharmony_ci} 417c84f3f3cSopenharmony_ci 418c84f3f3cSopenharmony_cisub 419c84f3f3cSopenharmony_cicatch_sigalrm 420c84f3f3cSopenharmony_ci{ 421c84f3f3cSopenharmony_ci $SIG{'ALRM'} = 'catch_sigalrm'; 422c84f3f3cSopenharmony_ci kill(9, $child_pid) if $child_kill_ok; 423c84f3f3cSopenharmony_ci $child_killed = 1; 424c84f3f3cSopenharmony_ci} 425c84f3f3cSopenharmony_ci 426c84f3f3cSopenharmony_cisub 427c84f3f3cSopenharmony_ciprocess_test_dir 428c84f3f3cSopenharmony_ci{ 429c84f3f3cSopenharmony_ci local($dir) = @_; 430c84f3f3cSopenharmony_ci local($ret, $file); 431c84f3f3cSopenharmony_ci local(@todo) = (); 432c84f3f3cSopenharmony_ci 433c84f3f3cSopenharmony_ci if (!opendir(DIR, $dir)) { 434c84f3f3cSopenharmony_ci print STDERR "$prog: can't open directory $dir - $!\n"; 435c84f3f3cSopenharmony_ci return undef; 436c84f3f3cSopenharmony_ci } 437c84f3f3cSopenharmony_ci while (defined ($file = readdir(DIR))) { 438c84f3f3cSopenharmony_ci push(@todo, $file) if $file =~ /^[^.].*\.t$/; 439c84f3f3cSopenharmony_ci } 440c84f3f3cSopenharmony_ci closedir(DIR); 441c84f3f3cSopenharmony_ci 442c84f3f3cSopenharmony_ci foreach $file (@todo) { 443c84f3f3cSopenharmony_ci $file = "$dir/$file"; 444c84f3f3cSopenharmony_ci if (-d $file) { 445c84f3f3cSopenharmony_ci $ret = &process_test_dir($file); 446c84f3f3cSopenharmony_ci } elsif (-f _) { 447c84f3f3cSopenharmony_ci $ret = &process_test_file($file); 448c84f3f3cSopenharmony_ci } 449c84f3f3cSopenharmony_ci last if !defined $ret; 450c84f3f3cSopenharmony_ci } 451c84f3f3cSopenharmony_ci 452c84f3f3cSopenharmony_ci return $ret; 453c84f3f3cSopenharmony_ci} 454c84f3f3cSopenharmony_ci 455c84f3f3cSopenharmony_cisub 456c84f3f3cSopenharmony_ciprocess_test_file 457c84f3f3cSopenharmony_ci{ 458c84f3f3cSopenharmony_ci local($file) = @_; 459c84f3f3cSopenharmony_ci local($ret); 460c84f3f3cSopenharmony_ci 461c84f3f3cSopenharmony_ci if (!open(IN, $file)) { 462c84f3f3cSopenharmony_ci print STDERR "$prog: can't open $file - $!\n"; 463c84f3f3cSopenharmony_ci return undef; 464c84f3f3cSopenharmony_ci } 465c84f3f3cSopenharmony_ci binmode(IN); 466c84f3f3cSopenharmony_ci while (1) { 467c84f3f3cSopenharmony_ci $ret = &read_test($file, IN, *test); 468c84f3f3cSopenharmony_ci last if !defined $ret || !$ret; 469c84f3f3cSopenharmony_ci next if !$all_tests && !$do_test{$test{'name'}}; 470c84f3f3cSopenharmony_ci next if !&category_check(*test); 471c84f3f3cSopenharmony_ci $ret = &run_test(*test); 472c84f3f3cSopenharmony_ci last if !defined $ret; 473c84f3f3cSopenharmony_ci } 474c84f3f3cSopenharmony_ci close(IN); 475c84f3f3cSopenharmony_ci 476c84f3f3cSopenharmony_ci return $ret; 477c84f3f3cSopenharmony_ci} 478c84f3f3cSopenharmony_ci 479c84f3f3cSopenharmony_cisub 480c84f3f3cSopenharmony_cirun_test 481c84f3f3cSopenharmony_ci{ 482c84f3f3cSopenharmony_ci local(*test) = @_; 483c84f3f3cSopenharmony_ci local($name) = $test{':full-name'}; 484c84f3f3cSopenharmony_ci 485c84f3f3cSopenharmony_ci return undef if !&scrub_dir($tempdir); 486c84f3f3cSopenharmony_ci 487c84f3f3cSopenharmony_ci if (defined $test{'stdin'}) { 488c84f3f3cSopenharmony_ci return undef if !&write_file($tempi, $test{'stdin'}); 489c84f3f3cSopenharmony_ci $ifile = $tempi; 490c84f3f3cSopenharmony_ci } else { 491c84f3f3cSopenharmony_ci $ifile = '/dev/null'; 492c84f3f3cSopenharmony_ci } 493c84f3f3cSopenharmony_ci 494c84f3f3cSopenharmony_ci if (defined $test{'script'}) { 495c84f3f3cSopenharmony_ci return undef if !&write_file($temps, $test{'script'}); 496c84f3f3cSopenharmony_ci } 497c84f3f3cSopenharmony_ci 498c84f3f3cSopenharmony_ci if (!chdir($tempdir)) { 499c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't cd to $tempdir - $!\n"; 500c84f3f3cSopenharmony_ci return undef; 501c84f3f3cSopenharmony_ci } 502c84f3f3cSopenharmony_ci 503c84f3f3cSopenharmony_ci if (defined $test{'file-setup'}) { 504c84f3f3cSopenharmony_ci local($i); 505c84f3f3cSopenharmony_ci local($type, $perm, $rest, $c, $len, $name); 506c84f3f3cSopenharmony_ci 507c84f3f3cSopenharmony_ci for ($i = 0; $i < $test{'file-setup'}; $i++) { 508c84f3f3cSopenharmony_ci $val = $test{"file-setup:$i"}; 509c84f3f3cSopenharmony_ci 510c84f3f3cSopenharmony_ci # format is: type perm "name" 511c84f3f3cSopenharmony_ci ($type, $perm, $rest) = 512c84f3f3cSopenharmony_ci split(' ', $val, 3); 513c84f3f3cSopenharmony_ci $c = substr($rest, 0, 1); 514c84f3f3cSopenharmony_ci $len = index($rest, $c, 1) - 1; 515c84f3f3cSopenharmony_ci $name = substr($rest, 1, $len); 516c84f3f3cSopenharmony_ci $rest = substr($rest, 2 + $len); 517c84f3f3cSopenharmony_ci $perm = oct($perm) if $perm =~ /^\d+$/; 518c84f3f3cSopenharmony_ci if ($type eq 'file') { 519c84f3f3cSopenharmony_ci return undef if !&write_file($name, $rest); 520c84f3f3cSopenharmony_ci if (!chmod($perm, $name)) { 521c84f3f3cSopenharmony_ci print STDERR 522c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: can't chmod $perm $name - $!\n"; 523c84f3f3cSopenharmony_ci return undef; 524c84f3f3cSopenharmony_ci } 525c84f3f3cSopenharmony_ci } elsif ($type eq 'dir') { 526c84f3f3cSopenharmony_ci if (!mkdir($name, $perm)) { 527c84f3f3cSopenharmony_ci print STDERR 528c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: can't mkdir $perm $name - $!\n"; 529c84f3f3cSopenharmony_ci return undef; 530c84f3f3cSopenharmony_ci } 531c84f3f3cSopenharmony_ci } elsif ($type eq 'symlink') { 532c84f3f3cSopenharmony_ci local($oumask) = umask($perm); 533c84f3f3cSopenharmony_ci local($ret) = symlink($rest, $name); 534c84f3f3cSopenharmony_ci umask($oumask); 535c84f3f3cSopenharmony_ci if (!$ret) { 536c84f3f3cSopenharmony_ci print STDERR 537c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: couldn't create symlink $name - $!\n"; 538c84f3f3cSopenharmony_ci return undef; 539c84f3f3cSopenharmony_ci } 540c84f3f3cSopenharmony_ci } 541c84f3f3cSopenharmony_ci } 542c84f3f3cSopenharmony_ci } 543c84f3f3cSopenharmony_ci 544c84f3f3cSopenharmony_ci if (defined $test{'perl-setup'}) { 545c84f3f3cSopenharmony_ci eval $test{'perl-setup'}; 546c84f3f3cSopenharmony_ci if ($@ ne '') { 547c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: error running perl-setup - $@\n"; 548c84f3f3cSopenharmony_ci return undef; 549c84f3f3cSopenharmony_ci } 550c84f3f3cSopenharmony_ci } 551c84f3f3cSopenharmony_ci 552c84f3f3cSopenharmony_ci $pid = fork; 553c84f3f3cSopenharmony_ci if (!defined $pid) { 554c84f3f3cSopenharmony_ci print STDERR "$prog: can't fork - $!\n"; 555c84f3f3cSopenharmony_ci return undef; 556c84f3f3cSopenharmony_ci } 557c84f3f3cSopenharmony_ci if (!$pid) { 558c84f3f3cSopenharmony_ci @SIG{@trap_sigs} = ('DEFAULT') x @trap_sigs; 559c84f3f3cSopenharmony_ci $SIG{'ALRM'} = 'DEFAULT'; 560c84f3f3cSopenharmony_ci if (defined $test{'env-setup'}) { 561c84f3f3cSopenharmony_ci local($var, $val, $i); 562c84f3f3cSopenharmony_ci 563c84f3f3cSopenharmony_ci foreach $var (split(substr($test{'env-setup'}, 0, 1), 564c84f3f3cSopenharmony_ci $test{'env-setup'})) 565c84f3f3cSopenharmony_ci { 566c84f3f3cSopenharmony_ci $i = index($var, '='); 567c84f3f3cSopenharmony_ci next if $i == 0 || $var eq ''; 568c84f3f3cSopenharmony_ci if ($i < 0) { 569c84f3f3cSopenharmony_ci delete $new_env{$var}; 570c84f3f3cSopenharmony_ci } else { 571c84f3f3cSopenharmony_ci $new_env{substr($var, 0, $i)} = substr($var, $i + 1); 572c84f3f3cSopenharmony_ci } 573c84f3f3cSopenharmony_ci } 574c84f3f3cSopenharmony_ci } 575c84f3f3cSopenharmony_ci if (!open(STDIN, "< $ifile")) { 576c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't open $ifile in child - $!\n"; 577c84f3f3cSopenharmony_ci kill('TERM', $$); 578c84f3f3cSopenharmony_ci } 579c84f3f3cSopenharmony_ci binmode(STDIN); 580c84f3f3cSopenharmony_ci if (!open(STDOUT, "> $tempo")) { 581c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't open $tempo in child - $!\n"; 582c84f3f3cSopenharmony_ci kill('TERM', $$); 583c84f3f3cSopenharmony_ci } 584c84f3f3cSopenharmony_ci binmode(STDOUT); 585c84f3f3cSopenharmony_ci if (!open(STDERR, "> $tempe")) { 586c84f3f3cSopenharmony_ci print STDOUT "$prog: couldn't open $tempe in child - $!\n"; 587c84f3f3cSopenharmony_ci kill('TERM', $$); 588c84f3f3cSopenharmony_ci } 589c84f3f3cSopenharmony_ci binmode(STDERR); 590c84f3f3cSopenharmony_ci if ($program_kludge) { 591c84f3f3cSopenharmony_ci @argv = split(' ', $test_prog); 592c84f3f3cSopenharmony_ci } else { 593c84f3f3cSopenharmony_ci @argv = ($test_prog); 594c84f3f3cSopenharmony_ci } 595c84f3f3cSopenharmony_ci if (defined $test{'arguments'}) { 596c84f3f3cSopenharmony_ci push(@argv, 597c84f3f3cSopenharmony_ci split(substr($test{'arguments'}, 0, 1), 598c84f3f3cSopenharmony_ci substr($test{'arguments'}, 1))); 599c84f3f3cSopenharmony_ci } 600c84f3f3cSopenharmony_ci push(@argv, $temps) if defined $test{'script'}; 601c84f3f3cSopenharmony_ci 602c84f3f3cSopenharmony_ci #XXX realpathise, use command -v/whence -p/which, or sth. like that 603c84f3f3cSopenharmony_ci #XXX if !$program_kludge, we get by with not doing it for now tho 604c84f3f3cSopenharmony_ci $new_env{'__progname'} = $argv[0]; 605c84f3f3cSopenharmony_ci 606c84f3f3cSopenharmony_ci # The following doesn't work with perl5... Need to do it explicitly - yuck. 607c84f3f3cSopenharmony_ci #%ENV = %new_env; 608c84f3f3cSopenharmony_ci foreach $k (keys(%ENV)) { 609c84f3f3cSopenharmony_ci delete $ENV{$k}; 610c84f3f3cSopenharmony_ci } 611c84f3f3cSopenharmony_ci $ENV{$k} = $v while ($k,$v) = each %new_env; 612c84f3f3cSopenharmony_ci 613c84f3f3cSopenharmony_ci exec { $argv[0] } @argv; 614c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't execute $test_prog - $!\n"; 615c84f3f3cSopenharmony_ci kill('TERM', $$); 616c84f3f3cSopenharmony_ci exit(95); 617c84f3f3cSopenharmony_ci } 618c84f3f3cSopenharmony_ci $child_pid = $pid; 619c84f3f3cSopenharmony_ci $child_killed = 0; 620c84f3f3cSopenharmony_ci $child_kill_ok = 1; 621c84f3f3cSopenharmony_ci alarm($test{'time-limit'}) if defined $test{'time-limit'}; 622c84f3f3cSopenharmony_ci while (1) { 623c84f3f3cSopenharmony_ci $xpid = waitpid($pid, 0); 624c84f3f3cSopenharmony_ci $child_kill_ok = 0; 625c84f3f3cSopenharmony_ci if ($xpid < 0) { 626c84f3f3cSopenharmony_ci if ($EINTR) { 627c84f3f3cSopenharmony_ci next if $! == $EINTR; 628c84f3f3cSopenharmony_ci } 629c84f3f3cSopenharmony_ci print STDERR "$prog: error waiting for child - $!\n"; 630c84f3f3cSopenharmony_ci return undef; 631c84f3f3cSopenharmony_ci } 632c84f3f3cSopenharmony_ci last; 633c84f3f3cSopenharmony_ci } 634c84f3f3cSopenharmony_ci $status = $?; 635c84f3f3cSopenharmony_ci alarm(0) if defined $test{'time-limit'}; 636c84f3f3cSopenharmony_ci 637c84f3f3cSopenharmony_ci $failed = 0; 638c84f3f3cSopenharmony_ci $why = ''; 639c84f3f3cSopenharmony_ci 640c84f3f3cSopenharmony_ci if ($child_killed) { 641c84f3f3cSopenharmony_ci $failed = 1; 642c84f3f3cSopenharmony_ci $why .= "\ttest timed out (limit of $test{'time-limit'} seconds)\n"; 643c84f3f3cSopenharmony_ci } 644c84f3f3cSopenharmony_ci 645c84f3f3cSopenharmony_ci $ret = &eval_exit($test{'long-name'}, $status, $test{'expected-exit'}); 646c84f3f3cSopenharmony_ci return undef if !defined $ret; 647c84f3f3cSopenharmony_ci if (!$ret) { 648c84f3f3cSopenharmony_ci local($expl); 649c84f3f3cSopenharmony_ci 650c84f3f3cSopenharmony_ci $failed = 1; 651c84f3f3cSopenharmony_ci if (($status & 0xff) == 0x7f) { 652c84f3f3cSopenharmony_ci $expl = "stopped"; 653c84f3f3cSopenharmony_ci } elsif (($status & 0xff)) { 654c84f3f3cSopenharmony_ci $expl = "signal " . ($status & 0x7f); 655c84f3f3cSopenharmony_ci } else { 656c84f3f3cSopenharmony_ci $expl = "exit-code " . (($status >> 8) & 0xff); 657c84f3f3cSopenharmony_ci } 658c84f3f3cSopenharmony_ci $why .= 659c84f3f3cSopenharmony_ci "\tunexpected exit status $status ($expl), expected $test{'expected-exit'}\n"; 660c84f3f3cSopenharmony_ci } 661c84f3f3cSopenharmony_ci 662c84f3f3cSopenharmony_ci $tmp = &check_output($test{'long-name'}, $tempo, 'stdout', 663c84f3f3cSopenharmony_ci $test{'expected-stdout'}, $test{'expected-stdout-pattern'}); 664c84f3f3cSopenharmony_ci return undef if !defined $tmp; 665c84f3f3cSopenharmony_ci if ($tmp ne '') { 666c84f3f3cSopenharmony_ci $failed = 1; 667c84f3f3cSopenharmony_ci $why .= $tmp; 668c84f3f3cSopenharmony_ci } 669c84f3f3cSopenharmony_ci 670c84f3f3cSopenharmony_ci $tmp = &check_output($test{'long-name'}, $tempe, 'stderr', 671c84f3f3cSopenharmony_ci $test{'expected-stderr'}, $test{'expected-stderr-pattern'}); 672c84f3f3cSopenharmony_ci return undef if !defined $tmp; 673c84f3f3cSopenharmony_ci if ($tmp ne '') { 674c84f3f3cSopenharmony_ci $failed = 1; 675c84f3f3cSopenharmony_ci $why .= $tmp; 676c84f3f3cSopenharmony_ci } 677c84f3f3cSopenharmony_ci 678c84f3f3cSopenharmony_ci $tmp = &check_file_result(*test); 679c84f3f3cSopenharmony_ci return undef if !defined $tmp; 680c84f3f3cSopenharmony_ci if ($tmp ne '') { 681c84f3f3cSopenharmony_ci $failed = 1; 682c84f3f3cSopenharmony_ci $why .= $tmp; 683c84f3f3cSopenharmony_ci } 684c84f3f3cSopenharmony_ci 685c84f3f3cSopenharmony_ci if (defined $test{'perl-cleanup'}) { 686c84f3f3cSopenharmony_ci eval $test{'perl-cleanup'}; 687c84f3f3cSopenharmony_ci if ($@ ne '') { 688c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: error running perl-cleanup - $@\n"; 689c84f3f3cSopenharmony_ci return undef; 690c84f3f3cSopenharmony_ci } 691c84f3f3cSopenharmony_ci } 692c84f3f3cSopenharmony_ci 693c84f3f3cSopenharmony_ci if (!chdir($pwd)) { 694c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't cd to $pwd - $!\n"; 695c84f3f3cSopenharmony_ci return undef; 696c84f3f3cSopenharmony_ci } 697c84f3f3cSopenharmony_ci 698c84f3f3cSopenharmony_ci if ($failed) { 699c84f3f3cSopenharmony_ci if (!$test{'expected-fail'}) { 700c84f3f3cSopenharmony_ci if ($test{'need-pass'}) { 701c84f3f3cSopenharmony_ci print "FAIL $name\n"; 702c84f3f3cSopenharmony_ci $nxfailed++; 703c84f3f3cSopenharmony_ci } else { 704c84f3f3cSopenharmony_ci print "FAIL $name (ignored)\n"; 705c84f3f3cSopenharmony_ci $nifailed++; 706c84f3f3cSopenharmony_ci } 707c84f3f3cSopenharmony_ci } else { 708c84f3f3cSopenharmony_ci print "fail $name (as expected)\n"; 709c84f3f3cSopenharmony_ci $nfailed++; 710c84f3f3cSopenharmony_ci } 711c84f3f3cSopenharmony_ci $why = "\tDescription" 712c84f3f3cSopenharmony_ci . &wrap_lines($test{'description'}, " (missing)\n") 713c84f3f3cSopenharmony_ci . $why; 714c84f3f3cSopenharmony_ci } elsif ($test{'expected-fail'}) { 715c84f3f3cSopenharmony_ci print "PASS $name (unexpectedly)\n"; 716c84f3f3cSopenharmony_ci $nxpassed++; 717c84f3f3cSopenharmony_ci } else { 718c84f3f3cSopenharmony_ci print "pass $name\n"; 719c84f3f3cSopenharmony_ci $npassed++; 720c84f3f3cSopenharmony_ci } 721c84f3f3cSopenharmony_ci print $why if $verbose; 722c84f3f3cSopenharmony_ci return 0; 723c84f3f3cSopenharmony_ci} 724c84f3f3cSopenharmony_ci 725c84f3f3cSopenharmony_cisub 726c84f3f3cSopenharmony_cicategory_check 727c84f3f3cSopenharmony_ci{ 728c84f3f3cSopenharmony_ci local(*test) = @_; 729c84f3f3cSopenharmony_ci local($c); 730c84f3f3cSopenharmony_ci 731c84f3f3cSopenharmony_ci return 0 if ($test{'need-ctty'} && defined $categories{'regress:no-ctty'}); 732c84f3f3cSopenharmony_ci return 1 if (!defined $test{'category'}); 733c84f3f3cSopenharmony_ci local($ok) = 0; 734c84f3f3cSopenharmony_ci foreach $c (split(',', $test{'category'})) { 735c84f3f3cSopenharmony_ci $c =~ s/\s+//; 736c84f3f3cSopenharmony_ci if ($c =~ /^!/) { 737c84f3f3cSopenharmony_ci $c = $'; 738c84f3f3cSopenharmony_ci return 0 if (defined $categories{$c}); 739c84f3f3cSopenharmony_ci $ok = 1; 740c84f3f3cSopenharmony_ci } else { 741c84f3f3cSopenharmony_ci $ok = 1 if (defined $categories{$c}); 742c84f3f3cSopenharmony_ci } 743c84f3f3cSopenharmony_ci } 744c84f3f3cSopenharmony_ci return $ok; 745c84f3f3cSopenharmony_ci} 746c84f3f3cSopenharmony_ci 747c84f3f3cSopenharmony_cisub 748c84f3f3cSopenharmony_ciscrub_dir 749c84f3f3cSopenharmony_ci{ 750c84f3f3cSopenharmony_ci local($dir) = @_; 751c84f3f3cSopenharmony_ci local(@todo) = (); 752c84f3f3cSopenharmony_ci local($file); 753c84f3f3cSopenharmony_ci 754c84f3f3cSopenharmony_ci if (!opendir(DIR, $dir)) { 755c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't open directory $dir - $!\n"; 756c84f3f3cSopenharmony_ci return undef; 757c84f3f3cSopenharmony_ci } 758c84f3f3cSopenharmony_ci while (defined ($file = readdir(DIR))) { 759c84f3f3cSopenharmony_ci push(@todo, $file) if $file ne '.' && $file ne '..'; 760c84f3f3cSopenharmony_ci } 761c84f3f3cSopenharmony_ci closedir(DIR); 762c84f3f3cSopenharmony_ci foreach $file (@todo) { 763c84f3f3cSopenharmony_ci $file = "$dir/$file"; 764c84f3f3cSopenharmony_ci if (-d $file) { 765c84f3f3cSopenharmony_ci return undef if !&scrub_dir($file); 766c84f3f3cSopenharmony_ci if (!rmdir($file)) { 767c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't rmdir $file - $!\n"; 768c84f3f3cSopenharmony_ci return undef; 769c84f3f3cSopenharmony_ci } 770c84f3f3cSopenharmony_ci } else { 771c84f3f3cSopenharmony_ci if (!unlink($file)) { 772c84f3f3cSopenharmony_ci print STDERR "$prog: couldn't unlink $file - $!\n"; 773c84f3f3cSopenharmony_ci return undef; 774c84f3f3cSopenharmony_ci } 775c84f3f3cSopenharmony_ci } 776c84f3f3cSopenharmony_ci } 777c84f3f3cSopenharmony_ci return 1; 778c84f3f3cSopenharmony_ci} 779c84f3f3cSopenharmony_ci 780c84f3f3cSopenharmony_cisub 781c84f3f3cSopenharmony_ciwrite_file 782c84f3f3cSopenharmony_ci{ 783c84f3f3cSopenharmony_ci local($file, $str) = @_; 784c84f3f3cSopenharmony_ci 785c84f3f3cSopenharmony_ci if (!open(TEMP, "> $file")) { 786c84f3f3cSopenharmony_ci print STDERR "$prog: can't open $file - $!\n"; 787c84f3f3cSopenharmony_ci return undef; 788c84f3f3cSopenharmony_ci } 789c84f3f3cSopenharmony_ci binmode(TEMP); 790c84f3f3cSopenharmony_ci print TEMP $str; 791c84f3f3cSopenharmony_ci if (!close(TEMP)) { 792c84f3f3cSopenharmony_ci print STDERR "$prog: error writing $file - $!\n"; 793c84f3f3cSopenharmony_ci return undef; 794c84f3f3cSopenharmony_ci } 795c84f3f3cSopenharmony_ci return 1; 796c84f3f3cSopenharmony_ci} 797c84f3f3cSopenharmony_ci 798c84f3f3cSopenharmony_cisub 799c84f3f3cSopenharmony_cicheck_output 800c84f3f3cSopenharmony_ci{ 801c84f3f3cSopenharmony_ci local($name, $file, $what, $expect, $expect_pat) = @_; 802c84f3f3cSopenharmony_ci local($got) = ''; 803c84f3f3cSopenharmony_ci local($why) = ''; 804c84f3f3cSopenharmony_ci local($ret); 805c84f3f3cSopenharmony_ci 806c84f3f3cSopenharmony_ci if (!open(TEMP, "< $file")) { 807c84f3f3cSopenharmony_ci print STDERR "$prog:$name($what): couldn't open $file after running program - $!\n"; 808c84f3f3cSopenharmony_ci return undef; 809c84f3f3cSopenharmony_ci } 810c84f3f3cSopenharmony_ci binmode(TEMP); 811c84f3f3cSopenharmony_ci while (<TEMP>) { 812c84f3f3cSopenharmony_ci $got .= $_; 813c84f3f3cSopenharmony_ci } 814c84f3f3cSopenharmony_ci close(TEMP); 815c84f3f3cSopenharmony_ci return compare_output($name, $what, $expect, $expect_pat, $got); 816c84f3f3cSopenharmony_ci} 817c84f3f3cSopenharmony_ci 818c84f3f3cSopenharmony_cisub 819c84f3f3cSopenharmony_cicompare_output 820c84f3f3cSopenharmony_ci{ 821c84f3f3cSopenharmony_ci local($name, $what, $expect, $expect_pat, $got) = @_; 822c84f3f3cSopenharmony_ci local($why) = ''; 823c84f3f3cSopenharmony_ci 824c84f3f3cSopenharmony_ci if (defined $expect_pat) { 825c84f3f3cSopenharmony_ci $_ = $got; 826c84f3f3cSopenharmony_ci $ret = eval "$expect_pat"; 827c84f3f3cSopenharmony_ci if ($@ ne '') { 828c84f3f3cSopenharmony_ci print STDERR "$prog:$name($what): error evaluating $what pattern: $expect_pat - $@\n"; 829c84f3f3cSopenharmony_ci return undef; 830c84f3f3cSopenharmony_ci } 831c84f3f3cSopenharmony_ci if (!$ret) { 832c84f3f3cSopenharmony_ci $why = "\tunexpected $what - wanted pattern"; 833c84f3f3cSopenharmony_ci $why .= &wrap_lines($expect_pat); 834c84f3f3cSopenharmony_ci $why .= "\tgot"; 835c84f3f3cSopenharmony_ci $why .= &wrap_lines($got); 836c84f3f3cSopenharmony_ci } 837c84f3f3cSopenharmony_ci } else { 838c84f3f3cSopenharmony_ci $expect = '' if !defined $expect; 839c84f3f3cSopenharmony_ci if ($got ne $expect) { 840c84f3f3cSopenharmony_ci $why .= "\tunexpected $what - " . &first_diff($expect, $got) . "\n"; 841c84f3f3cSopenharmony_ci $why .= "\twanted"; 842c84f3f3cSopenharmony_ci $why .= &wrap_lines($expect); 843c84f3f3cSopenharmony_ci $why .= "\tgot"; 844c84f3f3cSopenharmony_ci $why .= &wrap_lines($got); 845c84f3f3cSopenharmony_ci } 846c84f3f3cSopenharmony_ci } 847c84f3f3cSopenharmony_ci return $why; 848c84f3f3cSopenharmony_ci} 849c84f3f3cSopenharmony_ci 850c84f3f3cSopenharmony_cisub 851c84f3f3cSopenharmony_ciwrap_lines 852c84f3f3cSopenharmony_ci{ 853c84f3f3cSopenharmony_ci local($str, $empty) = @_; 854c84f3f3cSopenharmony_ci local($nonl) = substr($str, -1, 1) ne "\n"; 855c84f3f3cSopenharmony_ci 856c84f3f3cSopenharmony_ci return (defined $empty ? $empty : " nothing\n") if $str eq ''; 857c84f3f3cSopenharmony_ci substr($str, 0, 0) = ":\n"; 858c84f3f3cSopenharmony_ci $str =~ s/\n/\n\t\t/g; 859c84f3f3cSopenharmony_ci if ($nonl) { 860c84f3f3cSopenharmony_ci $str .= "\n\t[incomplete last line]\n"; 861c84f3f3cSopenharmony_ci } else { 862c84f3f3cSopenharmony_ci chop($str); 863c84f3f3cSopenharmony_ci chop($str); 864c84f3f3cSopenharmony_ci } 865c84f3f3cSopenharmony_ci return $str; 866c84f3f3cSopenharmony_ci} 867c84f3f3cSopenharmony_ci 868c84f3f3cSopenharmony_cisub 869c84f3f3cSopenharmony_cifirst_diff 870c84f3f3cSopenharmony_ci{ 871c84f3f3cSopenharmony_ci local($exp, $got) = @_; 872c84f3f3cSopenharmony_ci local($lineno, $char) = (1, 1); 873c84f3f3cSopenharmony_ci local($i, $exp_len, $got_len); 874c84f3f3cSopenharmony_ci local($ce, $cg); 875c84f3f3cSopenharmony_ci 876c84f3f3cSopenharmony_ci $exp_len = length($exp); 877c84f3f3cSopenharmony_ci $got_len = length($got); 878c84f3f3cSopenharmony_ci if ($exp_len != $got_len) { 879c84f3f3cSopenharmony_ci if ($exp_len < $got_len) { 880c84f3f3cSopenharmony_ci if (substr($got, 0, $exp_len) eq $exp) { 881c84f3f3cSopenharmony_ci return "got too much output"; 882c84f3f3cSopenharmony_ci } 883c84f3f3cSopenharmony_ci } elsif (substr($exp, 0, $got_len) eq $got) { 884c84f3f3cSopenharmony_ci return "got too little output"; 885c84f3f3cSopenharmony_ci } 886c84f3f3cSopenharmony_ci } 887c84f3f3cSopenharmony_ci for ($i = 0; $i < $exp_len; $i++) { 888c84f3f3cSopenharmony_ci $ce = substr($exp, $i, 1); 889c84f3f3cSopenharmony_ci $cg = substr($got, $i, 1); 890c84f3f3cSopenharmony_ci last if $ce ne $cg; 891c84f3f3cSopenharmony_ci $char++; 892c84f3f3cSopenharmony_ci if ($ce eq "\n") { 893c84f3f3cSopenharmony_ci $lineno++; 894c84f3f3cSopenharmony_ci $char = 1; 895c84f3f3cSopenharmony_ci } 896c84f3f3cSopenharmony_ci } 897c84f3f3cSopenharmony_ci return "first difference: line $lineno, char $char (wanted " . 898c84f3f3cSopenharmony_ci &format_char($ce) . ", got " . &format_char($cg); 899c84f3f3cSopenharmony_ci} 900c84f3f3cSopenharmony_ci 901c84f3f3cSopenharmony_cisub 902c84f3f3cSopenharmony_ciformat_char 903c84f3f3cSopenharmony_ci{ 904c84f3f3cSopenharmony_ci local($ch, $s, $q); 905c84f3f3cSopenharmony_ci 906c84f3f3cSopenharmony_ci $ch = ord($_[0]); 907c84f3f3cSopenharmony_ci $q = "'"; 908c84f3f3cSopenharmony_ci 909c84f3f3cSopenharmony_ci if ($is_ebcdic) { 910c84f3f3cSopenharmony_ci if ($ch == 0x15) { 911c84f3f3cSopenharmony_ci return $q . '\n' . $q; 912c84f3f3cSopenharmony_ci } elsif ($ch == 0x16) { 913c84f3f3cSopenharmony_ci return $q . '\b' . $q; 914c84f3f3cSopenharmony_ci } elsif ($ch == 0x05) { 915c84f3f3cSopenharmony_ci return $q . '\t' . $q; 916c84f3f3cSopenharmony_ci } elsif ($ch < 64 || $ch == 255) { 917c84f3f3cSopenharmony_ci return sprintf("X'%02X'", $ch); 918c84f3f3cSopenharmony_ci } 919c84f3f3cSopenharmony_ci return sprintf("'%c' (X'%02X')", $ch, $ch); 920c84f3f3cSopenharmony_ci } 921c84f3f3cSopenharmony_ci 922c84f3f3cSopenharmony_ci $s = sprintf("0x%02X (", $ch); 923c84f3f3cSopenharmony_ci if ($ch == 10) { 924c84f3f3cSopenharmony_ci return $s . $q . '\n' . $q . ')'; 925c84f3f3cSopenharmony_ci } elsif ($ch == 13) { 926c84f3f3cSopenharmony_ci return $s . $q . '\r' . $q . ')'; 927c84f3f3cSopenharmony_ci } elsif ($ch == 8) { 928c84f3f3cSopenharmony_ci return $s . $q . '\b' . $q . ')'; 929c84f3f3cSopenharmony_ci } elsif ($ch == 9) { 930c84f3f3cSopenharmony_ci return $s . $q . '\t' . $q . ')'; 931c84f3f3cSopenharmony_ci } elsif ($ch > 127) { 932c84f3f3cSopenharmony_ci $ch -= 128; 933c84f3f3cSopenharmony_ci $s .= "M-"; 934c84f3f3cSopenharmony_ci } 935c84f3f3cSopenharmony_ci if ($ch < 32) { 936c84f3f3cSopenharmony_ci return sprintf("%s^%c)", $s, $ch + ord('@')); 937c84f3f3cSopenharmony_ci } elsif ($ch == 127) { 938c84f3f3cSopenharmony_ci return $s . "^?)"; 939c84f3f3cSopenharmony_ci } 940c84f3f3cSopenharmony_ci return sprintf("%s'%c')", $s, $ch); 941c84f3f3cSopenharmony_ci} 942c84f3f3cSopenharmony_ci 943c84f3f3cSopenharmony_cisub 944c84f3f3cSopenharmony_cieval_exit 945c84f3f3cSopenharmony_ci{ 946c84f3f3cSopenharmony_ci local($name, $status, $expect) = @_; 947c84f3f3cSopenharmony_ci local($expr); 948c84f3f3cSopenharmony_ci local($w, $e, $s) = ($status, ($status >> 8) & 0xff, $status & 0x7f); 949c84f3f3cSopenharmony_ci 950c84f3f3cSopenharmony_ci $e = -1000 if $status & 0xff; 951c84f3f3cSopenharmony_ci $s = -1000 if $s == 0x7f; 952c84f3f3cSopenharmony_ci if (!defined $expect) { 953c84f3f3cSopenharmony_ci $expr = '$w == 0'; 954c84f3f3cSopenharmony_ci } elsif ($expect =~ /^(|-)\d+$/) { 955c84f3f3cSopenharmony_ci $expr = "\$e == $expect"; 956c84f3f3cSopenharmony_ci } else { 957c84f3f3cSopenharmony_ci $expr = $expect; 958c84f3f3cSopenharmony_ci $expr =~ s/\b([wse])\b/\$$1/g; 959c84f3f3cSopenharmony_ci $expr =~ s/\b(SIG[A-Z][A-Z0-9]*)\b/&$1/g; 960c84f3f3cSopenharmony_ci } 961c84f3f3cSopenharmony_ci $w = eval $expr; 962c84f3f3cSopenharmony_ci if ($@ ne '') { 963c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $expect ($@)\n"; 964c84f3f3cSopenharmony_ci return undef; 965c84f3f3cSopenharmony_ci } 966c84f3f3cSopenharmony_ci return $w; 967c84f3f3cSopenharmony_ci} 968c84f3f3cSopenharmony_ci 969c84f3f3cSopenharmony_cisub 970c84f3f3cSopenharmony_ciread_test 971c84f3f3cSopenharmony_ci{ 972c84f3f3cSopenharmony_ci local($file, $in, *test) = @_; 973c84f3f3cSopenharmony_ci local($field, $val, $flags, $do_chop, $need_redo, $start_lineno); 974c84f3f3cSopenharmony_ci local(%cnt, $sfield); 975c84f3f3cSopenharmony_ci 976c84f3f3cSopenharmony_ci %test = (); 977c84f3f3cSopenharmony_ci %cnt = (); 978c84f3f3cSopenharmony_ci while (<$in>) { 979c84f3f3cSopenharmony_ci chop; 980c84f3f3cSopenharmony_ci next if /^\s*$/; 981c84f3f3cSopenharmony_ci next if /^ *#/; 982c84f3f3cSopenharmony_ci last if /^\s*---\s*$/; 983c84f3f3cSopenharmony_ci $start_lineno = $. if !defined $start_lineno; 984c84f3f3cSopenharmony_ci if (!/^([-\w]+):\s*(|\S|\S.*\S)\s*$/) { 985c84f3f3cSopenharmony_ci print STDERR "$prog:$file:$.: unrecognised line \"$_\"\n"; 986c84f3f3cSopenharmony_ci return undef; 987c84f3f3cSopenharmony_ci } 988c84f3f3cSopenharmony_ci ($field, $val) = ($1, $2); 989c84f3f3cSopenharmony_ci $sfield = $field; 990c84f3f3cSopenharmony_ci $flags = $test_fields{$field}; 991c84f3f3cSopenharmony_ci if (!defined $flags) { 992c84f3f3cSopenharmony_ci print STDERR "$prog:$file:$.: unrecognised field \"$field\"\n"; 993c84f3f3cSopenharmony_ci return undef; 994c84f3f3cSopenharmony_ci } 995c84f3f3cSopenharmony_ci if ($flags =~ /s/) { 996c84f3f3cSopenharmony_ci local($cnt) = $cnt{$field}++; 997c84f3f3cSopenharmony_ci $test{$field} = $cnt{$field}; 998c84f3f3cSopenharmony_ci $cnt = 0 if $cnt eq ''; 999c84f3f3cSopenharmony_ci $sfield .= ":$cnt"; 1000c84f3f3cSopenharmony_ci } elsif (defined $test{$field}) { 1001c84f3f3cSopenharmony_ci print STDERR "$prog:$file:$.: multiple \"$field\" fields\n"; 1002c84f3f3cSopenharmony_ci return undef; 1003c84f3f3cSopenharmony_ci } 1004c84f3f3cSopenharmony_ci $do_chop = $flags !~ /m/; 1005c84f3f3cSopenharmony_ci $need_redo = 0; 1006c84f3f3cSopenharmony_ci if ($val eq '' || $val eq '!' || $flags =~ /p/) { 1007c84f3f3cSopenharmony_ci if ($flags =~ /[Mm]/) { 1008c84f3f3cSopenharmony_ci if ($flags =~ /p/) { 1009c84f3f3cSopenharmony_ci if ($val =~ /^!/) { 1010c84f3f3cSopenharmony_ci $do_chop = 1; 1011c84f3f3cSopenharmony_ci $val = $'; 1012c84f3f3cSopenharmony_ci } else { 1013c84f3f3cSopenharmony_ci $do_chop = 0; 1014c84f3f3cSopenharmony_ci } 1015c84f3f3cSopenharmony_ci if ($val eq '') { 1016c84f3f3cSopenharmony_ci print STDERR 1017c84f3f3cSopenharmony_ci "$prog:$file:$.: no parameters given for field \"$field\"\n"; 1018c84f3f3cSopenharmony_ci return undef; 1019c84f3f3cSopenharmony_ci } 1020c84f3f3cSopenharmony_ci } else { 1021c84f3f3cSopenharmony_ci if ($val eq '!') { 1022c84f3f3cSopenharmony_ci $do_chop = 1; 1023c84f3f3cSopenharmony_ci } 1024c84f3f3cSopenharmony_ci $val = ''; 1025c84f3f3cSopenharmony_ci } 1026c84f3f3cSopenharmony_ci while (<$in>) { 1027c84f3f3cSopenharmony_ci last if !/^\t/; 1028c84f3f3cSopenharmony_ci $val .= $'; 1029c84f3f3cSopenharmony_ci } 1030c84f3f3cSopenharmony_ci chop $val if $do_chop; 1031c84f3f3cSopenharmony_ci $do_chop = 1; 1032c84f3f3cSopenharmony_ci $need_redo = 1; 1033c84f3f3cSopenharmony_ci 1034c84f3f3cSopenharmony_ci # Syntax check on fields that can several instances 1035c84f3f3cSopenharmony_ci # (can give useful line numbers this way) 1036c84f3f3cSopenharmony_ci 1037c84f3f3cSopenharmony_ci if ($field eq 'file-setup') { 1038c84f3f3cSopenharmony_ci local($type, $perm, $rest, $c, $len, $name); 1039c84f3f3cSopenharmony_ci 1040c84f3f3cSopenharmony_ci # format is: type perm "name" 1041c84f3f3cSopenharmony_ci if ($val !~ /^[ \t]*(\S+)[ \t]+(\S+)[ \t]+([^ \t].*)/) { 1042c84f3f3cSopenharmony_ci print STDERR 1043c84f3f3cSopenharmony_ci "$prog:$file:$.: bad parameter line for file-setup field\n"; 1044c84f3f3cSopenharmony_ci return undef; 1045c84f3f3cSopenharmony_ci } 1046c84f3f3cSopenharmony_ci ($type, $perm, $rest) = ($1, $2, $3); 1047c84f3f3cSopenharmony_ci if ($type !~ /^(file|dir|symlink)$/) { 1048c84f3f3cSopenharmony_ci print STDERR 1049c84f3f3cSopenharmony_ci "$prog:$file:$.: bad file type for file-setup: $type\n"; 1050c84f3f3cSopenharmony_ci return undef; 1051c84f3f3cSopenharmony_ci } 1052c84f3f3cSopenharmony_ci if ($perm !~ /^\d+$/) { 1053c84f3f3cSopenharmony_ci print STDERR 1054c84f3f3cSopenharmony_ci "$prog:$file:$.: bad permissions for file-setup: $type\n"; 1055c84f3f3cSopenharmony_ci return undef; 1056c84f3f3cSopenharmony_ci } 1057c84f3f3cSopenharmony_ci $c = substr($rest, 0, 1); 1058c84f3f3cSopenharmony_ci if (($len = index($rest, $c, 1) - 1) <= 0) { 1059c84f3f3cSopenharmony_ci print STDERR 1060c84f3f3cSopenharmony_ci "$prog:$file:$.: missing end quote for file name in file-setup: $rest\n"; 1061c84f3f3cSopenharmony_ci return undef; 1062c84f3f3cSopenharmony_ci } 1063c84f3f3cSopenharmony_ci $name = substr($rest, 1, $len); 1064c84f3f3cSopenharmony_ci if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) { 1065c84f3f3cSopenharmony_ci # Note: this is not a security thing - just a sanity 1066c84f3f3cSopenharmony_ci # check - a test can still use symlinks to get at files 1067c84f3f3cSopenharmony_ci # outside the test directory. 1068c84f3f3cSopenharmony_ci print STDERR 1069c84f3f3cSopenharmony_ci"$prog:$file:$.: file name in file-setup is absolute or contains ..: $name\n"; 1070c84f3f3cSopenharmony_ci return undef; 1071c84f3f3cSopenharmony_ci } 1072c84f3f3cSopenharmony_ci } 1073c84f3f3cSopenharmony_ci if ($field eq 'file-result') { 1074c84f3f3cSopenharmony_ci local($type, $perm, $uid, $gid, $matchType, 1075c84f3f3cSopenharmony_ci $rest, $c, $len, $name); 1076c84f3f3cSopenharmony_ci 1077c84f3f3cSopenharmony_ci # format is: type perm uid gid matchType "name" 1078c84f3f3cSopenharmony_ci if ($val !~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/) { 1079c84f3f3cSopenharmony_ci print STDERR 1080c84f3f3cSopenharmony_ci "$prog:$file:$.: bad parameter line for file-result field\n"; 1081c84f3f3cSopenharmony_ci return undef; 1082c84f3f3cSopenharmony_ci } 1083c84f3f3cSopenharmony_ci ($type, $perm, $uid, $gid, $matchType, $rest) 1084c84f3f3cSopenharmony_ci = ($1, $2, $3, $4, $5, $6); 1085c84f3f3cSopenharmony_ci if ($type !~ /^(file|dir|symlink)$/) { 1086c84f3f3cSopenharmony_ci print STDERR 1087c84f3f3cSopenharmony_ci "$prog:$file:$.: bad file type for file-result: $type\n"; 1088c84f3f3cSopenharmony_ci return undef; 1089c84f3f3cSopenharmony_ci } 1090c84f3f3cSopenharmony_ci if ($perm !~ /^\d+$/ && $perm ne '*') { 1091c84f3f3cSopenharmony_ci print STDERR 1092c84f3f3cSopenharmony_ci "$prog:$file:$.: bad permissions for file-result: $perm\n"; 1093c84f3f3cSopenharmony_ci return undef; 1094c84f3f3cSopenharmony_ci } 1095c84f3f3cSopenharmony_ci if ($uid !~ /^\d+$/ && $uid ne '*') { 1096c84f3f3cSopenharmony_ci print STDERR 1097c84f3f3cSopenharmony_ci "$prog:$file:$.: bad user-id for file-result: $uid\n"; 1098c84f3f3cSopenharmony_ci return undef; 1099c84f3f3cSopenharmony_ci } 1100c84f3f3cSopenharmony_ci if ($gid !~ /^\d+$/ && $gid ne '*') { 1101c84f3f3cSopenharmony_ci print STDERR 1102c84f3f3cSopenharmony_ci "$prog:$file:$.: bad group-id for file-result: $gid\n"; 1103c84f3f3cSopenharmony_ci return undef; 1104c84f3f3cSopenharmony_ci } 1105c84f3f3cSopenharmony_ci if ($matchType !~ /^(exact|pattern)$/) { 1106c84f3f3cSopenharmony_ci print STDERR 1107c84f3f3cSopenharmony_ci "$prog:$file:$.: bad match type for file-result: $matchType\n"; 1108c84f3f3cSopenharmony_ci return undef; 1109c84f3f3cSopenharmony_ci } 1110c84f3f3cSopenharmony_ci $c = substr($rest, 0, 1); 1111c84f3f3cSopenharmony_ci if (($len = index($rest, $c, 1) - 1) <= 0) { 1112c84f3f3cSopenharmony_ci print STDERR 1113c84f3f3cSopenharmony_ci "$prog:$file:$.: missing end quote for file name in file-result: $rest\n"; 1114c84f3f3cSopenharmony_ci return undef; 1115c84f3f3cSopenharmony_ci } 1116c84f3f3cSopenharmony_ci $name = substr($rest, 1, $len); 1117c84f3f3cSopenharmony_ci if ($name =~ /^\// || $name =~ /(^|\/)\.\.(\/|$)/) { 1118c84f3f3cSopenharmony_ci # Note: this is not a security thing - just a sanity 1119c84f3f3cSopenharmony_ci # check - a test can still use symlinks to get at files 1120c84f3f3cSopenharmony_ci # outside the test directory. 1121c84f3f3cSopenharmony_ci print STDERR 1122c84f3f3cSopenharmony_ci"$prog:$file:$.: file name in file-result is absolute or contains ..: $name\n"; 1123c84f3f3cSopenharmony_ci return undef; 1124c84f3f3cSopenharmony_ci } 1125c84f3f3cSopenharmony_ci } 1126c84f3f3cSopenharmony_ci } elsif ($val eq '') { 1127c84f3f3cSopenharmony_ci print STDERR 1128c84f3f3cSopenharmony_ci "$prog:$file:$.: no value given for field \"$field\"\n"; 1129c84f3f3cSopenharmony_ci return undef; 1130c84f3f3cSopenharmony_ci } 1131c84f3f3cSopenharmony_ci } 1132c84f3f3cSopenharmony_ci $val .= "\n" if !$do_chop; 1133c84f3f3cSopenharmony_ci $test{$sfield} = $val; 1134c84f3f3cSopenharmony_ci redo if $need_redo; 1135c84f3f3cSopenharmony_ci } 1136c84f3f3cSopenharmony_ci if ($_ eq '') { 1137c84f3f3cSopenharmony_ci if (%test) { 1138c84f3f3cSopenharmony_ci print STDERR 1139c84f3f3cSopenharmony_ci "$prog:$file:$start_lineno: end-of-file while reading test\n"; 1140c84f3f3cSopenharmony_ci return undef; 1141c84f3f3cSopenharmony_ci } 1142c84f3f3cSopenharmony_ci return 0; 1143c84f3f3cSopenharmony_ci } 1144c84f3f3cSopenharmony_ci 1145c84f3f3cSopenharmony_ci while (($field, $val) = each %test_fields) { 1146c84f3f3cSopenharmony_ci if ($val =~ /r/ && !defined $test{$field}) { 1147c84f3f3cSopenharmony_ci print STDERR 1148c84f3f3cSopenharmony_ci "$prog:$file:$start_lineno: required field \"$field\" missing\n"; 1149c84f3f3cSopenharmony_ci return undef; 1150c84f3f3cSopenharmony_ci } 1151c84f3f3cSopenharmony_ci } 1152c84f3f3cSopenharmony_ci 1153c84f3f3cSopenharmony_ci $test{':full-name'} = substr($file, $file_prefix_skip) . ":$test{'name'}"; 1154c84f3f3cSopenharmony_ci $test{':long-name'} = "$file:$start_lineno:$test{'name'}"; 1155c84f3f3cSopenharmony_ci 1156c84f3f3cSopenharmony_ci # Syntax check on specific fields 1157c84f3f3cSopenharmony_ci if (defined $test{'expected-fail'}) { 1158c84f3f3cSopenharmony_ci if ($test{'expected-fail'} !~ /^(yes|no)$/) { 1159c84f3f3cSopenharmony_ci print STDERR 1160c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: bad value for expected-fail field\n"; 1161c84f3f3cSopenharmony_ci return undef; 1162c84f3f3cSopenharmony_ci } 1163c84f3f3cSopenharmony_ci $test{'expected-fail'} = $1 eq 'yes'; 1164c84f3f3cSopenharmony_ci } else { 1165c84f3f3cSopenharmony_ci $test{'expected-fail'} = 0; 1166c84f3f3cSopenharmony_ci } 1167c84f3f3cSopenharmony_ci if (defined $test{'need-ctty'}) { 1168c84f3f3cSopenharmony_ci if ($test{'need-ctty'} !~ /^(yes|no)$/) { 1169c84f3f3cSopenharmony_ci print STDERR 1170c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: bad value for need-ctty field\n"; 1171c84f3f3cSopenharmony_ci return undef; 1172c84f3f3cSopenharmony_ci } 1173c84f3f3cSopenharmony_ci $test{'need-ctty'} = $1 eq 'yes'; 1174c84f3f3cSopenharmony_ci } else { 1175c84f3f3cSopenharmony_ci $test{'need-ctty'} = 0; 1176c84f3f3cSopenharmony_ci } 1177c84f3f3cSopenharmony_ci if (defined $test{'need-pass'}) { 1178c84f3f3cSopenharmony_ci if ($test{'need-pass'} !~ /^(yes|no)$/) { 1179c84f3f3cSopenharmony_ci print STDERR 1180c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: bad value for need-pass field\n"; 1181c84f3f3cSopenharmony_ci return undef; 1182c84f3f3cSopenharmony_ci } 1183c84f3f3cSopenharmony_ci $test{'need-pass'} = $1 eq 'yes'; 1184c84f3f3cSopenharmony_ci } else { 1185c84f3f3cSopenharmony_ci $test{'need-pass'} = 1; 1186c84f3f3cSopenharmony_ci } 1187c84f3f3cSopenharmony_ci if (defined $test{'arguments'}) { 1188c84f3f3cSopenharmony_ci local($firstc) = substr($test{'arguments'}, 0, 1); 1189c84f3f3cSopenharmony_ci 1190c84f3f3cSopenharmony_ci if (substr($test{'arguments'}, -1, 1) ne $firstc) { 1191c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: arguments field doesn't start and end with the same character\n"; 1192c84f3f3cSopenharmony_ci return undef; 1193c84f3f3cSopenharmony_ci } 1194c84f3f3cSopenharmony_ci } 1195c84f3f3cSopenharmony_ci if (defined $test{'env-setup'}) { 1196c84f3f3cSopenharmony_ci local($firstc) = substr($test{'env-setup'}, 0, 1); 1197c84f3f3cSopenharmony_ci 1198c84f3f3cSopenharmony_ci if (substr($test{'env-setup'}, -1, 1) ne $firstc) { 1199c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: env-setup field doesn't start and end with the same character\n"; 1200c84f3f3cSopenharmony_ci return undef; 1201c84f3f3cSopenharmony_ci } 1202c84f3f3cSopenharmony_ci 1203c84f3f3cSopenharmony_ci $test{'env-setup'} =~ s/\@utflocale\@/$utflocale/g; 1204c84f3f3cSopenharmony_ci } 1205c84f3f3cSopenharmony_ci if (defined $test{'expected-exit'}) { 1206c84f3f3cSopenharmony_ci local($val) = $test{'expected-exit'}; 1207c84f3f3cSopenharmony_ci 1208c84f3f3cSopenharmony_ci if ($val =~ /^(|-)\d+$/) { 1209c84f3f3cSopenharmony_ci if ($val < 0 || $val > 255) { 1210c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: expected-exit value $val not in 0..255\n"; 1211c84f3f3cSopenharmony_ci return undef; 1212c84f3f3cSopenharmony_ci } 1213c84f3f3cSopenharmony_ci } elsif ($val !~ /^([\s\d<>+=*%\/&|!()-]|\b[wse]\b|\bSIG[A-Z][A-Z0-9]*\b)+$/) { 1214c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: bad expected-exit expression: $val\n"; 1215c84f3f3cSopenharmony_ci return undef; 1216c84f3f3cSopenharmony_ci } 1217c84f3f3cSopenharmony_ci } else { 1218c84f3f3cSopenharmony_ci $test{'expected-exit'} = 0; 1219c84f3f3cSopenharmony_ci } 1220c84f3f3cSopenharmony_ci if (defined $test{'expected-stdout'} 1221c84f3f3cSopenharmony_ci && defined $test{'expected-stdout-pattern'}) 1222c84f3f3cSopenharmony_ci { 1223c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: can't use both expected-stdout and expected-stdout-pattern\n"; 1224c84f3f3cSopenharmony_ci return undef; 1225c84f3f3cSopenharmony_ci } 1226c84f3f3cSopenharmony_ci if (defined $test{'expected-stderr'} 1227c84f3f3cSopenharmony_ci && defined $test{'expected-stderr-pattern'}) 1228c84f3f3cSopenharmony_ci { 1229c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: can't use both expected-stderr and expected-stderr-pattern\n"; 1230c84f3f3cSopenharmony_ci return undef; 1231c84f3f3cSopenharmony_ci } 1232c84f3f3cSopenharmony_ci if (defined $test{'time-limit'}) { 1233c84f3f3cSopenharmony_ci if ($test{'time-limit'} !~ /^\d+$/ || $test{'time-limit'} == 0) { 1234c84f3f3cSopenharmony_ci print STDERR 1235c84f3f3cSopenharmony_ci "$prog:$test{':long-name'}: bad value for time-limit field\n"; 1236c84f3f3cSopenharmony_ci return undef; 1237c84f3f3cSopenharmony_ci } 1238c84f3f3cSopenharmony_ci } elsif (defined $default_time_limit) { 1239c84f3f3cSopenharmony_ci $test{'time-limit'} = $default_time_limit; 1240c84f3f3cSopenharmony_ci } 1241c84f3f3cSopenharmony_ci 1242c84f3f3cSopenharmony_ci if (defined $known_tests{$test{'name'}}) { 1243c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: warning: duplicate test name ${test{'name'}}\n"; 1244c84f3f3cSopenharmony_ci } 1245c84f3f3cSopenharmony_ci $known_tests{$test{'name'}} = 1; 1246c84f3f3cSopenharmony_ci 1247c84f3f3cSopenharmony_ci return 1; 1248c84f3f3cSopenharmony_ci} 1249c84f3f3cSopenharmony_ci 1250c84f3f3cSopenharmony_cisub 1251c84f3f3cSopenharmony_citty_msg 1252c84f3f3cSopenharmony_ci{ 1253c84f3f3cSopenharmony_ci local($msg) = @_; 1254c84f3f3cSopenharmony_ci 1255c84f3f3cSopenharmony_ci open(TTY, "> /dev/tty") || return 0; 1256c84f3f3cSopenharmony_ci print TTY $msg; 1257c84f3f3cSopenharmony_ci close(TTY); 1258c84f3f3cSopenharmony_ci return 1; 1259c84f3f3cSopenharmony_ci} 1260c84f3f3cSopenharmony_ci 1261c84f3f3cSopenharmony_cisub 1262c84f3f3cSopenharmony_cinever_called_funcs 1263c84f3f3cSopenharmony_ci{ 1264c84f3f3cSopenharmony_ci return 0; 1265c84f3f3cSopenharmony_ci &tty_msg("hi\n"); 1266c84f3f3cSopenharmony_ci &never_called_funcs(); 1267c84f3f3cSopenharmony_ci &catch_sigalrm(); 1268c84f3f3cSopenharmony_ci $old_env{'foo'} = 'bar'; 1269c84f3f3cSopenharmony_ci $internal_test_fields{'foo'} = 'bar'; 1270c84f3f3cSopenharmony_ci} 1271c84f3f3cSopenharmony_ci 1272c84f3f3cSopenharmony_cisub 1273c84f3f3cSopenharmony_cicheck_file_result 1274c84f3f3cSopenharmony_ci{ 1275c84f3f3cSopenharmony_ci local(*test) = @_; 1276c84f3f3cSopenharmony_ci 1277c84f3f3cSopenharmony_ci return '' if (!defined $test{'file-result'}); 1278c84f3f3cSopenharmony_ci 1279c84f3f3cSopenharmony_ci local($why) = ''; 1280c84f3f3cSopenharmony_ci local($i); 1281c84f3f3cSopenharmony_ci local($type, $perm, $uid, $gid, $rest, $c, $len, $name); 1282c84f3f3cSopenharmony_ci local(@stbuf); 1283c84f3f3cSopenharmony_ci 1284c84f3f3cSopenharmony_ci for ($i = 0; $i < $test{'file-result'}; $i++) { 1285c84f3f3cSopenharmony_ci $val = $test{"file-result:$i"}; 1286c84f3f3cSopenharmony_ci 1287c84f3f3cSopenharmony_ci # format is: type perm "name" 1288c84f3f3cSopenharmony_ci ($type, $perm, $uid, $gid, $matchType, $rest) = 1289c84f3f3cSopenharmony_ci split(' ', $val, 6); 1290c84f3f3cSopenharmony_ci $c = substr($rest, 0, 1); 1291c84f3f3cSopenharmony_ci $len = index($rest, $c, 1) - 1; 1292c84f3f3cSopenharmony_ci $name = substr($rest, 1, $len); 1293c84f3f3cSopenharmony_ci $rest = substr($rest, 2 + $len); 1294c84f3f3cSopenharmony_ci $perm = oct($perm) if $perm =~ /^\d+$/; 1295c84f3f3cSopenharmony_ci 1296c84f3f3cSopenharmony_ci @stbuf = lstat($name); 1297c84f3f3cSopenharmony_ci if (!@stbuf) { 1298c84f3f3cSopenharmony_ci $why .= "\texpected $type \"$name\" not created\n"; 1299c84f3f3cSopenharmony_ci next; 1300c84f3f3cSopenharmony_ci } 1301c84f3f3cSopenharmony_ci if ($perm ne '*' && ($stbuf[2] & 07777) != $perm) { 1302c84f3f3cSopenharmony_ci $why .= "\t$type \"$name\" has unexpected permissions\n"; 1303c84f3f3cSopenharmony_ci $why .= sprintf("\t\texpected 0%o, found 0%o\n", 1304c84f3f3cSopenharmony_ci $perm, $stbuf[2] & 07777); 1305c84f3f3cSopenharmony_ci } 1306c84f3f3cSopenharmony_ci if ($uid ne '*' && $stbuf[4] != $uid) { 1307c84f3f3cSopenharmony_ci $why .= "\t$type \"$name\" has unexpected user-id\n"; 1308c84f3f3cSopenharmony_ci $why .= sprintf("\t\texpected %d, found %d\n", 1309c84f3f3cSopenharmony_ci $uid, $stbuf[4]); 1310c84f3f3cSopenharmony_ci } 1311c84f3f3cSopenharmony_ci if ($gid ne '*' && $stbuf[5] != $gid) { 1312c84f3f3cSopenharmony_ci $why .= "\t$type \"$name\" has unexpected group-id\n"; 1313c84f3f3cSopenharmony_ci $why .= sprintf("\t\texpected %d, found %d\n", 1314c84f3f3cSopenharmony_ci $gid, $stbuf[5]); 1315c84f3f3cSopenharmony_ci } 1316c84f3f3cSopenharmony_ci 1317c84f3f3cSopenharmony_ci if ($type eq 'file') { 1318c84f3f3cSopenharmony_ci if (-l _ || ! -f _) { 1319c84f3f3cSopenharmony_ci $why .= "\t$type \"$name\" is not a regular file\n"; 1320c84f3f3cSopenharmony_ci } else { 1321c84f3f3cSopenharmony_ci local $tmp = &check_output($test{'long-name'}, $name, 1322c84f3f3cSopenharmony_ci "$type contents in \"$name\"", 1323c84f3f3cSopenharmony_ci $matchType eq 'exact' ? $rest : undef 1324c84f3f3cSopenharmony_ci $matchType eq 'pattern' ? $rest : undef); 1325c84f3f3cSopenharmony_ci return undef if (!defined $tmp); 1326c84f3f3cSopenharmony_ci $why .= $tmp; 1327c84f3f3cSopenharmony_ci } 1328c84f3f3cSopenharmony_ci } elsif ($type eq 'dir') { 1329c84f3f3cSopenharmony_ci if ($rest !~ /^\s*$/) { 1330c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: file-result test for directory $name should not have content specified\n"; 1331c84f3f3cSopenharmony_ci return undef; 1332c84f3f3cSopenharmony_ci } 1333c84f3f3cSopenharmony_ci if (-l _ || ! -d _) { 1334c84f3f3cSopenharmony_ci $why .= "\t$type \"$name\" is not a directory\n"; 1335c84f3f3cSopenharmony_ci } 1336c84f3f3cSopenharmony_ci } elsif ($type eq 'symlink') { 1337c84f3f3cSopenharmony_ci if (!-l _) { 1338c84f3f3cSopenharmony_ci $why .= "\t$type \"$name\" is not a symlink\n"; 1339c84f3f3cSopenharmony_ci } else { 1340c84f3f3cSopenharmony_ci local $content = readlink($name); 1341c84f3f3cSopenharmony_ci if (!defined $content) { 1342c84f3f3cSopenharmony_ci print STDERR "$prog:$test{':long-name'}: file-result test for $type $name failed - could not readlink - $!\n"; 1343c84f3f3cSopenharmony_ci return undef; 1344c84f3f3cSopenharmony_ci } 1345c84f3f3cSopenharmony_ci local $tmp = &compare_output($test{'long-name'}, 1346c84f3f3cSopenharmony_ci "$type contents in \"$name\"", 1347c84f3f3cSopenharmony_ci $matchType eq 'exact' ? $rest : undef 1348c84f3f3cSopenharmony_ci $matchType eq 'pattern' ? $rest : undef); 1349c84f3f3cSopenharmony_ci return undef if (!defined $tmp); 1350c84f3f3cSopenharmony_ci $why .= $tmp; 1351c84f3f3cSopenharmony_ci } 1352c84f3f3cSopenharmony_ci } 1353c84f3f3cSopenharmony_ci } 1354c84f3f3cSopenharmony_ci 1355c84f3f3cSopenharmony_ci return $why; 1356c84f3f3cSopenharmony_ci} 1357c84f3f3cSopenharmony_ci 1358c84f3f3cSopenharmony_cisub 1359c84f3f3cSopenharmony_ciHELP_MESSAGE 1360c84f3f3cSopenharmony_ci{ 1361c84f3f3cSopenharmony_ci print STDERR $Usage; 1362c84f3f3cSopenharmony_ci exit 0; 1363c84f3f3cSopenharmony_ci} 1364