runtests.pl 7.7 KB

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