3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
16 # The Original Code is the Netscape Portable Runtime (NSPR).
18 # The Initial Developer of the Original Code is
19 # Sun Microsystems, Inc.
20 # Portions created by the Initial Developer are Copyright (C) 2008
21 # the Initial Developer. All Rights Reserved.
24 # Christophe Ravel <christophe.ravel@sun.com>, Sun Microsystems
25 # Slavomir Katuscak <slavomir.katuscak@sun.com>, Sun Microsystems
27 # Alternatively, the contents of this file may be used under the terms of
28 # either the GNU General Public License Version 2 or later (the "GPL"), or
29 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 # in which case the provisions of the GPL or the LGPL are applicable instead
31 # of those above. If you wish to allow use of your version of this file only
32 # under the terms of either the GPL or the LGPL, and not to allow others to
33 # use your version of this file under the terms of the MPL, indicate your
34 # decision by deleting the provisions above and replace them with the notice
35 # and other provisions required by the GPL or the LGPL. If you do not delete
36 # the provisions above, a recipient may use your version of this file under
37 # the terms of any one of the MPL, the GPL or the LGPL.
39 # ***** END LICENSE BLOCK *****
41 use POSIX qw(:sys_wait_h);
51 if ($osname =~ $WINOS) {
53 require Win32::Process;
57 # Get environment variables.
58 $output_file = $ENV{NSPR_TEST_LOGFILE};
59 $timeout = $ENV{TEST_TIMEOUT};
61 $timeout = 0 if (!defined($timeout));
64 ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
66 $year = 1900 + $yearOffset;
68 $theTime = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$dayOfMonth,$hour,$minute,$second);
74 if (!defined($output_file)) {
75 print "No output file.\n";
77 if ($osname =~ $WINOS) {
80 $output_file = "/dev/null";
84 # use STDOUT for OF (to print summary of test results)
85 open(OF, ">&STDOUT") or die "Can't reuse STDOUT for OF\n";
87 # reassign STDOUT to $output_file (to print details of test results)
88 open(STDOUT, ">$output_file") or die "Can't open file $output_file for STDOUT\n";
90 # redirect STDERR to STDOUT
91 open(STDERR, ">&STDOUT") or die "Can't redirect STDERR to STDOUT\n";
94 # Print header test in summary
96 print OF "\nNSPR Test Results - tests\n";
97 print OF "\nBEGIN\t\t\t$now\n";
98 print OF "NSPR_TEST_LOGFILE\t$output_file\n";
99 print OF "TEST_TIMEOUT\t$timeout\n\n";
100 print OF "\nTest\t\t\tResult\n\n";
104 # end of test marker in summary
106 print OF "END\t\t\t$now\n";
108 close(OF) or die "Can't close file OF\n";
109 close(STDERR) or die "Can't close STDERR\n";
110 close(STDOUT) or die "Can't close STDOUT\n";
120 print "BEGIN TEST: $lprog ($now)\n\n";
124 ($lprog, $exit_status, $exit_signal, $exit_core) = @_;
126 if (($exit_status == 0) && ($exit_signal == 0) && ($exit_core == 0)) {
127 $str_status = "Passed";
129 $str_status = "FAILED";
131 if ($exit_signal != 0) {
132 $str_signal = " - signal $exit_signal";
136 if ($exit_core != 0) {
137 $str_core = " - core dumped";
143 print "\nEND TEST: $lprog ($now)\n";
144 print "TEST STATUS: $lprog = $str_status (exit status " . $exit_status . $str_signal . $str_core . ")\n";
145 print "--------------------------------------------------\n\n";
147 print OF "\t\t\t$str_status\n";
152 $lprog = shift; # command to run
154 # Create a process group for the child
155 # so we can kill all of it if needed
156 setsid or die "setsid failed: $!";
159 # We should not be here unless exec failed.
160 print "Faild to exec $lprog";
164 sub ux_wait_timeout {
166 $lpid = shift; # child process id
167 $ltimeout = shift; # timeout
169 if ($ltimeout == 0) {
170 # No timeout: use blocking wait
171 $ret = waitpid($lpid,0);
172 # Exit and don't kill
176 while ($ltimeout > 0) {
177 # Check status of child using non blocking wait
178 $ret = waitpid($lpid, WNOHANG);
180 # Child still running
181 # print "Time left=$ltimeout\n";
187 # Exit the wait loop and don't kill
193 if ($ltimeout == 0) {
194 # we ran all the timeout: it's time to kill the child
195 print "Timeout ! Kill child process $lpid\n";
196 # Kill the child process and group
206 $prog = shift; # Program to test
209 if ($child_pid == 0) {
210 # we are in the child process
212 ux_start_prog($prog);
214 # we are in the parent process
215 $status = ux_wait_timeout($child_pid,$timeout);
216 # See Perlvar for documentation of $?
217 # exit status = $status >> 8
218 # exit signal = $status & 127 (no signal = 0)
219 # core dump = $status & 128 (no core = 0)
220 print_end($prog, $status >> 8, $status & 127, $status & 128);
229 # MSYS drive letter = /c/ -> c:/
230 $lpath =~ s/^\/(\w)\//$1:\//;
231 # Cygwin drive letter = /cygdrive/c/ -> c:/
232 $lpath =~ s/^\/cygdrive\/(\w)\//$1:\//;
234 $lpath =~ s/\//\\\\/g;
240 print Win32::FormatMessage( Win32::GetLastError() );
245 $prog = shift; # Program to test
249 $curdir = win_path($curdir);
250 $prog_path = "$curdir\\$prog.exe";
254 Win32::Process::Create($ProcessObj,
258 NORMAL_PRIORITY_CLASS,
259 ".")|| die win_ErrorReport();
260 $retwait = $ProcessObj->Wait($timeout * 1000);
262 if ( $retwait == 0) {
263 # the prog didn't finish after the timeout: kill
264 $ProcessObj->Kill($status);
265 print "Timeout ! Process killed with exit status $status\n";
267 # the prog finished before the timeout: get exit status
268 $ProcessObj->GetExitCode($status);
270 # There is no signal, no core on Windows
271 print_end($prog, $status, 0, 0);
276 # MAIN ---------------
395 foreach $current_prog (@progs) {
396 if ($osname =~ $WINOS) {
397 win_test_prog($current_prog);
399 ux_test_prog($current_prog);