Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / tests / runtests.pl
1 #!/usr/bin/perl
2 #
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
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/
10 #
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
14 # License.
15 #
16 # The Original Code is the Netscape Portable Runtime (NSPR).
17 #
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.
22 #
23 # Contributor(s):
24 #   Christophe Ravel <christophe.ravel@sun.com>, Sun Microsystems
25 #   Slavomir Katuscak <slavomir.katuscak@sun.com>, Sun Microsystems
26 #
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.
38 #
39 # ***** END LICENSE BLOCK *****
40
41 use POSIX qw(:sys_wait_h);
42 use POSIX qw(setsid);
43 use FileHandle;
44
45 # Constants
46 $WINOS = "MSWin32";
47
48 $osname = $^O;
49
50 use Cwd;
51 if ($osname =~ $WINOS) {
52     # Windows
53     require Win32::Process;
54     require Win32;
55 }
56
57 # Get environment variables.
58 $output_file = $ENV{NSPR_TEST_LOGFILE};
59 $timeout = $ENV{TEST_TIMEOUT};
60
61 $timeout = 0 if (!defined($timeout));
62
63 sub getTime {
64     ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
65
66     $year = 1900 + $yearOffset;
67
68     $theTime = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$dayOfMonth,$hour,$minute,$second);
69     return $theTime;
70 }
71
72 sub open_log {
73
74     if (!defined($output_file)) {
75         print "No output file.\n";
76         # null device
77         if ($osname =~ $WINOS) {
78             $output_file = "nul";
79         } else {
80             $output_file = "/dev/null";
81         }
82     }
83     
84     # use STDOUT for OF (to print summary of test results)
85     open(OF, ">&STDOUT") or die "Can't reuse STDOUT for OF\n";
86     OF->autoflush;
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";
89     STDOUT->autoflush;
90     # redirect STDERR to STDOUT
91     open(STDERR, ">&STDOUT") or die "Can't redirect STDERR to STDOUT\n";
92     STDERR->autoflush;
93     
94     # Print header test in summary
95     $now = getTime;
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";
101 }
102
103 sub close_log {
104     # end of test marker in summary
105     $now = getTime;
106     print OF "END\t\t\t$now\n";
107
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";
111 }
112
113 sub print_begin {
114 $lprog = shift;
115
116     # Summary output
117     print OF "$prog";
118     # Full output
119     $now = getTime;
120     print "BEGIN TEST: $lprog ($now)\n\n";
121 }
122
123 sub print_end {
124 ($lprog, $exit_status, $exit_signal, $exit_core) = @_;
125
126     if (($exit_status == 0) && ($exit_signal == 0) && ($exit_core == 0)) {
127         $str_status = "Passed";
128     } else {
129         $str_status = "FAILED";
130     }
131     if ($exit_signal != 0) {
132         $str_signal = " - signal $exit_signal";
133     } else {
134         $str_signal = "";
135     }
136     if ($exit_core != 0) {
137         $str_core = " - core dumped";
138     } else {
139         $str_core = "";
140     }
141     $now = getTime;
142     # Full output
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";
146     # Summary output
147     print OF "\t\t\t$str_status\n";
148 }
149
150 sub ux_start_prog {
151 # parameters:
152 $lprog = shift; # command to run
153
154     # Create a process group for the child
155     # so we can kill all of it if needed
156     setsid or die "setsid failed: $!";
157     # Start test program    
158     exec("./$lprog");
159     # We should not be here unless exec failed.
160     print "Faild to exec $lprog";
161     exit 1 << 8;
162 }   
163
164 sub ux_wait_timeout {
165 # parameters:
166 $lpid = shift;     # child process id
167 $ltimeout = shift; # timeout
168
169     if ($ltimeout == 0) {
170         # No timeout: use blocking wait
171         $ret = waitpid($lpid,0);
172         # Exit and don't kill
173         $lstatus = $?;
174         $ltimeout = -1;
175     } else {
176         while ($ltimeout > 0) {
177             # Check status of child using non blocking wait
178             $ret = waitpid($lpid, WNOHANG);
179             if ($ret == 0) {
180                 # Child still running
181     #           print "Time left=$ltimeout\n";
182                 sleep 1;
183                 $ltimeout--;
184             } else {
185                 # Child has ended
186                 $lstatus = $?;
187                 # Exit the wait loop and don't kill
188                 $ltimeout = -1;
189             }
190         }
191     }
192     
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
197         kill(-9,$lpid);
198         $lstatus = 9;
199     }
200     
201     return $lstatus;
202 }
203
204 sub ux_test_prog {
205 # parameters:
206 $prog = shift;  # Program to test
207
208     $child_pid = fork;
209     if ($child_pid == 0) {
210         # we are in the child process
211         print_begin($prog);
212         ux_start_prog($prog);
213     } else {
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);
221     }
222
223     return $status;
224 }
225
226 sub win_path {
227 $lpath = shift;
228
229     # MSYS drive letter = /c/ -> c:/
230     $lpath =~ s/^\/(\w)\//$1:\//;
231     # Cygwin drive letter = /cygdrive/c/ -> c:/
232     $lpath =~ s/^\/cygdrive\/(\w)\//$1:\//;
233     # replace / with \\
234     $lpath =~ s/\//\\\\/g;
235     
236     return $lpath;
237 }
238
239 sub win_ErrorReport{
240     print Win32::FormatMessage( Win32::GetLastError() );
241 }
242
243 sub win_test_prog {
244 # parameters:
245 $prog = shift;  # Program to test
246
247     $status = 1;
248     $curdir = getcwd;
249     $curdir = win_path($curdir);
250     $prog_path = "$curdir\\$prog.exe";
251     
252     print_begin($prog);
253     
254     Win32::Process::Create($ProcessObj,
255                            "$prog_path",
256                            "$prog",
257                            0,
258                            NORMAL_PRIORITY_CLASS,
259                            ".")|| die win_ErrorReport();
260     $retwait = $ProcessObj->Wait($timeout * 1000);
261         
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";
266     } else {
267         # the prog finished before the timeout: get exit status
268         $ProcessObj->GetExitCode($status);
269     }
270     # There is no signal, no core on Windows
271     print_end($prog, $status, 0, 0);
272
273     return $status
274 }
275
276 # MAIN ---------------
277 @progs = (
278 "accept",
279 "acceptread",
280 "acceptreademu",
281 "affinity",
282 "alarm",
283 "anonfm",
284 "atomic",
285 "attach",
286 "bigfile",
287 "cleanup",
288 "cltsrv",
289 "concur",
290 "cvar",
291 "cvar2",
292 "dlltest",
293 "dtoa",
294 "errcodes",
295 "exit",
296 "fdcach",
297 "fileio",
298 "foreign",
299 "formattm",
300 "fsync",
301 "gethost",
302 "getproto",
303 "i2l",
304 "initclk",
305 "inrval",
306 "instrumt",
307 "intrio",
308 "intrupt",
309 "io_timeout",
310 "ioconthr",
311 "join",
312 "joinkk",
313 "joinku",
314 "joinuk",
315 "joinuu",
316 "layer",
317 "lazyinit",
318 "libfilename",
319 "lltest",
320 "lock",
321 "lockfile",
322 "logfile",
323 "logger",
324 "many_cv",
325 "multiwait",
326 "nameshm1",
327 "nblayer",
328 "nonblock",
329 "ntioto",
330 "ntoh",
331 "op_2long",
332 "op_excl",
333 "op_filnf",
334 "op_filok",
335 "op_nofil",
336 "parent",
337 "parsetm",
338 "peek",
339 "perf",
340 "pipeping",
341 "pipeping2",
342 "pipeself",
343 "poll_nm",
344 "poll_to",
345 "pollable",
346 "prftest",
347 "primblok",
348 "provider",
349 "prpollml",
350 "ranfile",
351 "randseed",
352 "reinit",
353 "rwlocktest",
354 "sel_spd",
355 "selct_er",
356 "selct_nm",
357 "selct_to",
358 "selintr",
359 "sema",
360 "semaerr",
361 "semaping",
362 "sendzlf",
363 "server_test",
364 "servr_kk",
365 "servr_uk",
366 "servr_ku",
367 "servr_uu",
368 "short_thread",
369 "sigpipe",
370 "socket",
371 "sockopt",
372 "sockping",
373 "sprintf",
374 "stack",
375 "stdio",
376 "str2addr",
377 "strod",
378 "switch",
379 "system",
380 "testbit",
381 "testfile",
382 "threads",
383 "timemac",
384 "timetest",
385 "tpd",
386 "udpsrv",
387 "vercheck",
388 "version",
389 "writev",
390 "xnotify",
391 "zerolen");
392
393 open_log;
394
395 foreach $current_prog (@progs) {
396     if ($osname =~ $WINOS) {
397         win_test_prog($current_prog);
398     } else {
399         ux_test_prog($current_prog);
400     }
401 }
402
403 close_log;