nsprpub/pr/tests/runtests.pl

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #!/usr/bin/perl
     2 #
     3 # This Source Code Form is subject to the terms of the Mozilla Public
     4 # License, v. 2.0. If a copy of the MPL was not distributed with this
     5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7 use POSIX qw(:sys_wait_h);
     8 use POSIX qw(setsid);
     9 use FileHandle;
    11 # Constants
    12 $WINOS = "MSWin32";
    14 $osname = $^O;
    16 use Cwd;
    17 if ($osname =~ $WINOS) {
    18     # Windows
    19     require Win32::Process;
    20     require Win32;
    21 }
    23 # Get environment variables.
    24 $output_file = $ENV{NSPR_TEST_LOGFILE};
    25 $timeout = $ENV{TEST_TIMEOUT};
    27 $timeout = 0 if (!defined($timeout));
    29 sub getTime {
    30     ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
    32     $year = 1900 + $yearOffset;
    34     $theTime = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$dayOfMonth,$hour,$minute,$second);
    35     return $theTime;
    36 }
    38 sub open_log {
    40     if (!defined($output_file)) {
    41         print "No output file.\n";
    42         # null device
    43         if ($osname =~ $WINOS) {
    44             $output_file = "nul";
    45         } else {
    46             $output_file = "/dev/null";
    47         }
    48     }
    50     # use STDOUT for OF (to print summary of test results)
    51     open(OF, ">&STDOUT") or die "Can't reuse STDOUT for OF\n";
    52     OF->autoflush;
    53     # reassign STDOUT to $output_file (to print details of test results)
    54     open(STDOUT, ">$output_file") or die "Can't open file $output_file for STDOUT\n";
    55     STDOUT->autoflush;
    56     # redirect STDERR to STDOUT
    57     open(STDERR, ">&STDOUT") or die "Can't redirect STDERR to STDOUT\n";
    58     STDERR->autoflush;
    60     # Print header test in summary
    61     $now = getTime;
    62     print OF "\nNSPR Test Results - tests\n";
    63     print OF "\nBEGIN\t\t\t$now\n";
    64     print OF "NSPR_TEST_LOGFILE\t$output_file\n";
    65     print OF "TEST_TIMEOUT\t$timeout\n\n";
    66     print OF "\nTest\t\t\tResult\n\n";
    67 }
    69 sub close_log {
    70     # end of test marker in summary
    71     $now = getTime;
    72     print OF "END\t\t\t$now\n";
    74     close(OF) or die "Can't close file OF\n";
    75     close(STDERR) or die "Can't close STDERR\n";
    76     close(STDOUT) or die "Can't close STDOUT\n";
    77 }
    79 sub print_begin {
    80 $lprog = shift;
    82     # Summary output
    83     print OF "$prog";
    84     # Full output
    85     $now = getTime;
    86     print "BEGIN TEST: $lprog ($now)\n\n";
    87 }
    89 sub print_end {
    90 ($lprog, $exit_status, $exit_signal, $exit_core) = @_;
    92     if (($exit_status == 0) && ($exit_signal == 0) && ($exit_core == 0)) {
    93         $str_status = "Passed";
    94     } else {
    95         $str_status = "FAILED";
    96     }
    97     if ($exit_signal != 0) {
    98     	$str_signal = " - signal $exit_signal";
    99     } else {
   100     	$str_signal = "";
   101     }
   102     if ($exit_core != 0) {
   103     	$str_core = " - core dumped";
   104     } else {
   105     	$str_core = "";
   106     }
   107     $now = getTime;
   108     # Full output
   109     print "\nEND TEST: $lprog ($now)\n";
   110     print "TEST STATUS: $lprog = $str_status (exit status " . $exit_status . $str_signal . $str_core . ")\n";
   111     print "--------------------------------------------------\n\n";
   112     # Summary output
   113     print OF "\t\t\t$str_status\n";
   114 }
   116 sub ux_start_prog {
   117 # parameters:
   118 $lprog = shift; # command to run
   120     # Create a process group for the child
   121     # so we can kill all of it if needed
   122     setsid or die "setsid failed: $!";
   123     # Start test program    
   124     exec("./$lprog");
   125     # We should not be here unless exec failed.
   126     print "Faild to exec $lprog";
   127     exit 1 << 8;
   128 }   
   130 sub ux_wait_timeout {
   131 # parameters:
   132 $lpid = shift;     # child process id
   133 $ltimeout = shift; # timeout
   135     if ($ltimeout == 0) {
   136         # No timeout: use blocking wait
   137         $ret = waitpid($lpid,0);
   138         # Exit and don't kill
   139         $lstatus = $?;
   140         $ltimeout = -1;
   141     } else {
   142         while ($ltimeout > 0) {
   143             # Check status of child using non blocking wait
   144             $ret = waitpid($lpid, WNOHANG);
   145             if ($ret == 0) {
   146                 # Child still running
   147     #           print "Time left=$ltimeout\n";
   148                 sleep 1;
   149                 $ltimeout--;
   150             } else {
   151                 # Child has ended
   152                 $lstatus = $?;
   153                 # Exit the wait loop and don't kill
   154                 $ltimeout = -1;
   155             }
   156         }
   157     }
   159     if ($ltimeout == 0) {
   160         # we ran all the timeout: it's time to kill the child
   161         print "Timeout ! Kill child process $lpid\n";
   162         # Kill the child process and group
   163         kill(-9,$lpid);
   164         $lstatus = 9;
   165     }
   167     return $lstatus;
   168 }
   170 sub ux_test_prog {
   171 # parameters:
   172 $prog = shift;  # Program to test
   174     $child_pid = fork;
   175     if ($child_pid == 0) {
   176         # we are in the child process
   177         print_begin($prog);
   178         ux_start_prog($prog);
   179     } else {
   180         # we are in the parent process
   181         $status = ux_wait_timeout($child_pid,$timeout);
   182         # See Perlvar for documentation of $?
   183         # exit status = $status >> 8
   184         # exit signal = $status & 127 (no signal = 0)
   185         # core dump = $status & 128 (no core = 0)
   186         print_end($prog, $status >> 8, $status & 127, $status & 128);
   187     }
   189     return $status;
   190 }
   192 sub win_path {
   193 $lpath = shift;
   195     # MSYS drive letter = /c/ -> c:/
   196     $lpath =~ s/^\/(\w)\//$1:\//;
   197     # Cygwin drive letter = /cygdrive/c/ -> c:/
   198     $lpath =~ s/^\/cygdrive\/(\w)\//$1:\//;
   199     # replace / with \\
   200     $lpath =~ s/\//\\\\/g;
   202     return $lpath;
   203 }
   205 sub win_ErrorReport{
   206     print Win32::FormatMessage( Win32::GetLastError() );
   207 }
   209 sub win_test_prog {
   210 # parameters:
   211 $prog = shift;  # Program to test
   213     $status = 1;
   214     $curdir = getcwd;
   215     $curdir = win_path($curdir);
   216     $prog_path = "$curdir\\$prog.exe";
   218     print_begin($prog);
   220     Win32::Process::Create($ProcessObj,
   221                            "$prog_path",
   222                            "$prog",
   223                            0,
   224                            NORMAL_PRIORITY_CLASS,
   225                            ".")|| die win_ErrorReport();
   226     $retwait = $ProcessObj->Wait($timeout * 1000);
   228     if ( $retwait == 0) {
   229         # the prog didn't finish after the timeout: kill
   230         $ProcessObj->Kill($status);
   231         print "Timeout ! Process killed with exit status $status\n";
   232     } else {
   233         # the prog finished before the timeout: get exit status
   234         $ProcessObj->GetExitCode($status);
   235     }
   236     # There is no signal, no core on Windows
   237     print_end($prog, $status, 0, 0);
   239     return $status
   240 }
   242 # MAIN ---------------
   243 @progs = (
   244 "accept",
   245 "acceptread",
   246 "acceptreademu",
   247 "affinity",
   248 "alarm",
   249 "anonfm",
   250 "atomic",
   251 "attach",
   252 "bigfile",
   253 "cleanup",
   254 "cltsrv",
   255 "concur",
   256 "cvar",
   257 "cvar2",
   258 "dlltest",
   259 "dtoa",
   260 "errcodes",
   261 "exit",
   262 "fdcach",
   263 "fileio",
   264 "foreign",
   265 "formattm",
   266 "fsync",
   267 "gethost",
   268 "getproto",
   269 "i2l",
   270 "initclk",
   271 "inrval",
   272 "instrumt",
   273 "intrio",
   274 "intrupt",
   275 "io_timeout",
   276 "ioconthr",
   277 "join",
   278 "joinkk",
   279 "joinku",
   280 "joinuk",
   281 "joinuu",
   282 "layer",
   283 "lazyinit",
   284 "libfilename",
   285 "lltest",
   286 "lock",
   287 "lockfile",
   288 "logfile",
   289 "logger",
   290 "many_cv",
   291 "multiwait",
   292 "nameshm1",
   293 "nblayer",
   294 "nonblock",
   295 "ntioto",
   296 "ntoh",
   297 "op_2long",
   298 "op_excl",
   299 "op_filnf",
   300 "op_filok",
   301 "op_nofil",
   302 "parent",
   303 "parsetm",
   304 "peek",
   305 "perf",
   306 "pipeping",
   307 "pipeping2",
   308 "pipeself",
   309 "poll_nm",
   310 "poll_to",
   311 "pollable",
   312 "prftest",
   313 "primblok",
   314 "provider",
   315 "prpollml",
   316 "pushtop",
   317 "ranfile",
   318 "randseed",
   319 "reinit",
   320 "rwlocktest",
   321 "sel_spd",
   322 "selct_er",
   323 "selct_nm",
   324 "selct_to",
   325 "selintr",
   326 "sema",
   327 "semaerr",
   328 "semaping",
   329 "sendzlf",
   330 "server_test",
   331 "servr_kk",
   332 "servr_uk",
   333 "servr_ku",
   334 "servr_uu",
   335 "short_thread",
   336 "sigpipe",
   337 "socket",
   338 "sockopt",
   339 "sockping",
   340 "sprintf",
   341 "stack",
   342 "stdio",
   343 "str2addr",
   344 "strod",
   345 "switch",
   346 "system",
   347 "testbit",
   348 "testfile",
   349 "threads",
   350 "timemac",
   351 "timetest",
   352 "tpd",
   353 "udpsrv",
   354 "vercheck",
   355 "version",
   356 "writev",
   357 "xnotify",
   358 "zerolen");
   360 open_log;
   362 foreach $current_prog (@progs) {
   363     if ($osname =~ $WINOS) {
   364         win_test_prog($current_prog);
   365     } else {
   366         ux_test_prog($current_prog);
   367     }
   368 }
   370 close_log;

mercurial