1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/runtests.pl Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,370 @@ 1.4 +#!/usr/bin/perl 1.5 +# 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +use POSIX qw(:sys_wait_h); 1.11 +use POSIX qw(setsid); 1.12 +use FileHandle; 1.13 + 1.14 +# Constants 1.15 +$WINOS = "MSWin32"; 1.16 + 1.17 +$osname = $^O; 1.18 + 1.19 +use Cwd; 1.20 +if ($osname =~ $WINOS) { 1.21 + # Windows 1.22 + require Win32::Process; 1.23 + require Win32; 1.24 +} 1.25 + 1.26 +# Get environment variables. 1.27 +$output_file = $ENV{NSPR_TEST_LOGFILE}; 1.28 +$timeout = $ENV{TEST_TIMEOUT}; 1.29 + 1.30 +$timeout = 0 if (!defined($timeout)); 1.31 + 1.32 +sub getTime { 1.33 + ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(); 1.34 + 1.35 + $year = 1900 + $yearOffset; 1.36 + 1.37 + $theTime = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$dayOfMonth,$hour,$minute,$second); 1.38 + return $theTime; 1.39 +} 1.40 + 1.41 +sub open_log { 1.42 + 1.43 + if (!defined($output_file)) { 1.44 + print "No output file.\n"; 1.45 + # null device 1.46 + if ($osname =~ $WINOS) { 1.47 + $output_file = "nul"; 1.48 + } else { 1.49 + $output_file = "/dev/null"; 1.50 + } 1.51 + } 1.52 + 1.53 + # use STDOUT for OF (to print summary of test results) 1.54 + open(OF, ">&STDOUT") or die "Can't reuse STDOUT for OF\n"; 1.55 + OF->autoflush; 1.56 + # reassign STDOUT to $output_file (to print details of test results) 1.57 + open(STDOUT, ">$output_file") or die "Can't open file $output_file for STDOUT\n"; 1.58 + STDOUT->autoflush; 1.59 + # redirect STDERR to STDOUT 1.60 + open(STDERR, ">&STDOUT") or die "Can't redirect STDERR to STDOUT\n"; 1.61 + STDERR->autoflush; 1.62 + 1.63 + # Print header test in summary 1.64 + $now = getTime; 1.65 + print OF "\nNSPR Test Results - tests\n"; 1.66 + print OF "\nBEGIN\t\t\t$now\n"; 1.67 + print OF "NSPR_TEST_LOGFILE\t$output_file\n"; 1.68 + print OF "TEST_TIMEOUT\t$timeout\n\n"; 1.69 + print OF "\nTest\t\t\tResult\n\n"; 1.70 +} 1.71 + 1.72 +sub close_log { 1.73 + # end of test marker in summary 1.74 + $now = getTime; 1.75 + print OF "END\t\t\t$now\n"; 1.76 + 1.77 + close(OF) or die "Can't close file OF\n"; 1.78 + close(STDERR) or die "Can't close STDERR\n"; 1.79 + close(STDOUT) or die "Can't close STDOUT\n"; 1.80 +} 1.81 + 1.82 +sub print_begin { 1.83 +$lprog = shift; 1.84 + 1.85 + # Summary output 1.86 + print OF "$prog"; 1.87 + # Full output 1.88 + $now = getTime; 1.89 + print "BEGIN TEST: $lprog ($now)\n\n"; 1.90 +} 1.91 + 1.92 +sub print_end { 1.93 +($lprog, $exit_status, $exit_signal, $exit_core) = @_; 1.94 + 1.95 + if (($exit_status == 0) && ($exit_signal == 0) && ($exit_core == 0)) { 1.96 + $str_status = "Passed"; 1.97 + } else { 1.98 + $str_status = "FAILED"; 1.99 + } 1.100 + if ($exit_signal != 0) { 1.101 + $str_signal = " - signal $exit_signal"; 1.102 + } else { 1.103 + $str_signal = ""; 1.104 + } 1.105 + if ($exit_core != 0) { 1.106 + $str_core = " - core dumped"; 1.107 + } else { 1.108 + $str_core = ""; 1.109 + } 1.110 + $now = getTime; 1.111 + # Full output 1.112 + print "\nEND TEST: $lprog ($now)\n"; 1.113 + print "TEST STATUS: $lprog = $str_status (exit status " . $exit_status . $str_signal . $str_core . ")\n"; 1.114 + print "--------------------------------------------------\n\n"; 1.115 + # Summary output 1.116 + print OF "\t\t\t$str_status\n"; 1.117 +} 1.118 + 1.119 +sub ux_start_prog { 1.120 +# parameters: 1.121 +$lprog = shift; # command to run 1.122 + 1.123 + # Create a process group for the child 1.124 + # so we can kill all of it if needed 1.125 + setsid or die "setsid failed: $!"; 1.126 + # Start test program 1.127 + exec("./$lprog"); 1.128 + # We should not be here unless exec failed. 1.129 + print "Faild to exec $lprog"; 1.130 + exit 1 << 8; 1.131 +} 1.132 + 1.133 +sub ux_wait_timeout { 1.134 +# parameters: 1.135 +$lpid = shift; # child process id 1.136 +$ltimeout = shift; # timeout 1.137 + 1.138 + if ($ltimeout == 0) { 1.139 + # No timeout: use blocking wait 1.140 + $ret = waitpid($lpid,0); 1.141 + # Exit and don't kill 1.142 + $lstatus = $?; 1.143 + $ltimeout = -1; 1.144 + } else { 1.145 + while ($ltimeout > 0) { 1.146 + # Check status of child using non blocking wait 1.147 + $ret = waitpid($lpid, WNOHANG); 1.148 + if ($ret == 0) { 1.149 + # Child still running 1.150 + # print "Time left=$ltimeout\n"; 1.151 + sleep 1; 1.152 + $ltimeout--; 1.153 + } else { 1.154 + # Child has ended 1.155 + $lstatus = $?; 1.156 + # Exit the wait loop and don't kill 1.157 + $ltimeout = -1; 1.158 + } 1.159 + } 1.160 + } 1.161 + 1.162 + if ($ltimeout == 0) { 1.163 + # we ran all the timeout: it's time to kill the child 1.164 + print "Timeout ! Kill child process $lpid\n"; 1.165 + # Kill the child process and group 1.166 + kill(-9,$lpid); 1.167 + $lstatus = 9; 1.168 + } 1.169 + 1.170 + return $lstatus; 1.171 +} 1.172 + 1.173 +sub ux_test_prog { 1.174 +# parameters: 1.175 +$prog = shift; # Program to test 1.176 + 1.177 + $child_pid = fork; 1.178 + if ($child_pid == 0) { 1.179 + # we are in the child process 1.180 + print_begin($prog); 1.181 + ux_start_prog($prog); 1.182 + } else { 1.183 + # we are in the parent process 1.184 + $status = ux_wait_timeout($child_pid,$timeout); 1.185 + # See Perlvar for documentation of $? 1.186 + # exit status = $status >> 8 1.187 + # exit signal = $status & 127 (no signal = 0) 1.188 + # core dump = $status & 128 (no core = 0) 1.189 + print_end($prog, $status >> 8, $status & 127, $status & 128); 1.190 + } 1.191 + 1.192 + return $status; 1.193 +} 1.194 + 1.195 +sub win_path { 1.196 +$lpath = shift; 1.197 + 1.198 + # MSYS drive letter = /c/ -> c:/ 1.199 + $lpath =~ s/^\/(\w)\//$1:\//; 1.200 + # Cygwin drive letter = /cygdrive/c/ -> c:/ 1.201 + $lpath =~ s/^\/cygdrive\/(\w)\//$1:\//; 1.202 + # replace / with \\ 1.203 + $lpath =~ s/\//\\\\/g; 1.204 + 1.205 + return $lpath; 1.206 +} 1.207 + 1.208 +sub win_ErrorReport{ 1.209 + print Win32::FormatMessage( Win32::GetLastError() ); 1.210 +} 1.211 + 1.212 +sub win_test_prog { 1.213 +# parameters: 1.214 +$prog = shift; # Program to test 1.215 + 1.216 + $status = 1; 1.217 + $curdir = getcwd; 1.218 + $curdir = win_path($curdir); 1.219 + $prog_path = "$curdir\\$prog.exe"; 1.220 + 1.221 + print_begin($prog); 1.222 + 1.223 + Win32::Process::Create($ProcessObj, 1.224 + "$prog_path", 1.225 + "$prog", 1.226 + 0, 1.227 + NORMAL_PRIORITY_CLASS, 1.228 + ".")|| die win_ErrorReport(); 1.229 + $retwait = $ProcessObj->Wait($timeout * 1000); 1.230 + 1.231 + if ( $retwait == 0) { 1.232 + # the prog didn't finish after the timeout: kill 1.233 + $ProcessObj->Kill($status); 1.234 + print "Timeout ! Process killed with exit status $status\n"; 1.235 + } else { 1.236 + # the prog finished before the timeout: get exit status 1.237 + $ProcessObj->GetExitCode($status); 1.238 + } 1.239 + # There is no signal, no core on Windows 1.240 + print_end($prog, $status, 0, 0); 1.241 + 1.242 + return $status 1.243 +} 1.244 + 1.245 +# MAIN --------------- 1.246 +@progs = ( 1.247 +"accept", 1.248 +"acceptread", 1.249 +"acceptreademu", 1.250 +"affinity", 1.251 +"alarm", 1.252 +"anonfm", 1.253 +"atomic", 1.254 +"attach", 1.255 +"bigfile", 1.256 +"cleanup", 1.257 +"cltsrv", 1.258 +"concur", 1.259 +"cvar", 1.260 +"cvar2", 1.261 +"dlltest", 1.262 +"dtoa", 1.263 +"errcodes", 1.264 +"exit", 1.265 +"fdcach", 1.266 +"fileio", 1.267 +"foreign", 1.268 +"formattm", 1.269 +"fsync", 1.270 +"gethost", 1.271 +"getproto", 1.272 +"i2l", 1.273 +"initclk", 1.274 +"inrval", 1.275 +"instrumt", 1.276 +"intrio", 1.277 +"intrupt", 1.278 +"io_timeout", 1.279 +"ioconthr", 1.280 +"join", 1.281 +"joinkk", 1.282 +"joinku", 1.283 +"joinuk", 1.284 +"joinuu", 1.285 +"layer", 1.286 +"lazyinit", 1.287 +"libfilename", 1.288 +"lltest", 1.289 +"lock", 1.290 +"lockfile", 1.291 +"logfile", 1.292 +"logger", 1.293 +"many_cv", 1.294 +"multiwait", 1.295 +"nameshm1", 1.296 +"nblayer", 1.297 +"nonblock", 1.298 +"ntioto", 1.299 +"ntoh", 1.300 +"op_2long", 1.301 +"op_excl", 1.302 +"op_filnf", 1.303 +"op_filok", 1.304 +"op_nofil", 1.305 +"parent", 1.306 +"parsetm", 1.307 +"peek", 1.308 +"perf", 1.309 +"pipeping", 1.310 +"pipeping2", 1.311 +"pipeself", 1.312 +"poll_nm", 1.313 +"poll_to", 1.314 +"pollable", 1.315 +"prftest", 1.316 +"primblok", 1.317 +"provider", 1.318 +"prpollml", 1.319 +"pushtop", 1.320 +"ranfile", 1.321 +"randseed", 1.322 +"reinit", 1.323 +"rwlocktest", 1.324 +"sel_spd", 1.325 +"selct_er", 1.326 +"selct_nm", 1.327 +"selct_to", 1.328 +"selintr", 1.329 +"sema", 1.330 +"semaerr", 1.331 +"semaping", 1.332 +"sendzlf", 1.333 +"server_test", 1.334 +"servr_kk", 1.335 +"servr_uk", 1.336 +"servr_ku", 1.337 +"servr_uu", 1.338 +"short_thread", 1.339 +"sigpipe", 1.340 +"socket", 1.341 +"sockopt", 1.342 +"sockping", 1.343 +"sprintf", 1.344 +"stack", 1.345 +"stdio", 1.346 +"str2addr", 1.347 +"strod", 1.348 +"switch", 1.349 +"system", 1.350 +"testbit", 1.351 +"testfile", 1.352 +"threads", 1.353 +"timemac", 1.354 +"timetest", 1.355 +"tpd", 1.356 +"udpsrv", 1.357 +"vercheck", 1.358 +"version", 1.359 +"writev", 1.360 +"xnotify", 1.361 +"zerolen"); 1.362 + 1.363 +open_log; 1.364 + 1.365 +foreach $current_prog (@progs) { 1.366 + if ($osname =~ $WINOS) { 1.367 + win_test_prog($current_prog); 1.368 + } else { 1.369 + ux_test_prog($current_prog); 1.370 + } 1.371 +} 1.372 + 1.373 +close_log;