inkobj: Remove break after return. Found by Smatch.
[wine/testsucceed.git] / programs / winetest / main.c
blobf43358461946eec25a28b03557e85c776bf6ba9f
1 /*
2 * Wine Conformance Test EXE
4 * Copyright 2003, 2004 Jakob Eriksson (for Solid Form Sweden AB)
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2003 Ferenc Wagner
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * This program is dedicated to Anna Lindh,
23 * Swedish Minister of Foreign Affairs.
24 * Anna was murdered September 11, 2003.
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <errno.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <windows.h>
40 #include "winetest.h"
41 #include "resource.h"
43 struct wine_test
45 char *name;
46 int resource;
47 int subtest_count;
48 char **subtests;
49 char *exename;
52 struct rev_info
54 const char* file;
55 const char* rev;
58 char *tag = NULL;
59 static struct wine_test *wine_tests;
60 static int nr_of_files, nr_of_tests;
61 static struct rev_info *rev_infos = NULL;
62 static const char whitespace[] = " \t\r\n";
63 static const char testexe[] = "_test.exe";
65 static int running_under_wine (void)
67 HMODULE module = GetModuleHandleA("ntdll.dll");
69 if (!module) return 0;
70 return (GetProcAddress(module, "wine_server_call") != NULL);
73 static int running_on_visible_desktop (void)
75 HWND desktop;
76 HMODULE huser32 = GetModuleHandle("user32.dll");
77 FARPROC pGetProcessWindowStation = GetProcAddress(huser32, "GetProcessWindowStation");
78 FARPROC pGetUserObjectInformationA = GetProcAddress(huser32, "GetUserObjectInformationA");
80 desktop = GetDesktopWindow();
81 if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
82 return IsWindowVisible(desktop);
84 if (pGetProcessWindowStation && pGetUserObjectInformationA)
86 DWORD len;
87 HWINSTA wstation;
88 USEROBJECTFLAGS uoflags;
90 wstation = (HWINSTA)pGetProcessWindowStation();
91 assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
92 return (uoflags.dwFlags & WSF_VISIBLE) != 0;
94 return IsWindowVisible(desktop);
97 static void print_version (void)
99 OSVERSIONINFOEX ver;
100 BOOL ext;
101 int is_win2k3_r2;
103 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
104 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
106 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
107 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
108 report (R_FATAL, "Can't get OS version.");
111 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
112 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
113 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
114 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
115 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
116 ver.dwPlatformId, ver.szCSDVersion);
118 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
119 if(is_win2k3_r2)
120 xprintf(" R2 build number=%d\n", is_win2k3_r2);
122 if (!ext) return;
124 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
125 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
126 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
127 ver.wProductType, ver.wReserved);
130 static inline int is_dot_dir(const char* x)
132 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
135 static void remove_dir (const char *dir)
137 HANDLE hFind;
138 WIN32_FIND_DATA wfd;
139 char path[MAX_PATH];
140 size_t dirlen = strlen (dir);
142 /* Make sure the directory exists before going further */
143 memcpy (path, dir, dirlen);
144 strcpy (path + dirlen++, "\\*");
145 hFind = FindFirstFile (path, &wfd);
146 if (hFind == INVALID_HANDLE_VALUE) return;
148 do {
149 char *lp = wfd.cFileName;
151 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
152 if (is_dot_dir (lp)) continue;
153 strcpy (path + dirlen, lp);
154 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
155 remove_dir(path);
156 else if (!DeleteFile (path))
157 report (R_WARNING, "Can't delete file %s: error %d",
158 path, GetLastError ());
159 } while (FindNextFile (hFind, &wfd));
160 FindClose (hFind);
161 if (!RemoveDirectory (dir))
162 report (R_WARNING, "Can't remove directory %s: error %d",
163 dir, GetLastError ());
166 static const char* get_test_source_file(const char* test, const char* subtest)
168 static const char* special_dirs[][2] = {
169 { 0, 0 }
171 static char buffer[MAX_PATH];
172 int i;
174 for (i = 0; special_dirs[i][0]; i++) {
175 if (strcmp(test, special_dirs[i][0]) == 0) {
176 test = special_dirs[i][1];
177 break;
181 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
182 return buffer;
185 static const char* get_file_rev(const char* file)
187 const struct rev_info* rev;
189 for(rev = rev_infos; rev->file; rev++) {
190 if (strcmp(rev->file, file) == 0) return rev->rev;
193 return "-";
196 static void extract_rev_infos (void)
198 char revinfo[256], *p;
199 int size = 0, i;
200 unsigned int len;
201 HMODULE module = GetModuleHandle (NULL);
203 for (i = 0; TRUE; i++) {
204 if (i >= size) {
205 size += 100;
206 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
208 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
210 len = LoadStringA (module, REV_INFO+i, revinfo, sizeof(revinfo));
211 if (len == 0) break; /* end of revision info */
212 if (len >= sizeof(revinfo) - 1)
213 report (R_FATAL, "Revision info too long.");
214 if(!(p = strrchr(revinfo, ':')))
215 report (R_FATAL, "Revision info malformed (i=%d)", i);
216 *p = 0;
217 rev_infos[i].file = strdup(revinfo);
218 rev_infos[i].rev = strdup(p + 1);
222 static void* extract_rcdata (LPTSTR name, int type, DWORD* size)
224 HRSRC rsrc;
225 HGLOBAL hdl;
226 LPVOID addr;
228 if (!(rsrc = FindResource (NULL, name, MAKEINTRESOURCE(type))) ||
229 !(*size = SizeofResource (0, rsrc)) ||
230 !(hdl = LoadResource (0, rsrc)) ||
231 !(addr = LockResource (hdl)))
232 return NULL;
233 return addr;
236 /* Fills in the name and exename fields */
237 static void
238 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
240 BYTE* code;
241 DWORD size;
242 FILE* fout;
243 char *exepos;
245 code = extract_rcdata (res_name, TESTRES, &size);
246 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
247 res_name, GetLastError ());
248 test->name = xstrdup( res_name );
249 test->exename = strmake (NULL, "%s/%s", dir, test->name);
250 exepos = strstr (test->name, testexe);
251 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
252 *exepos = 0;
253 test->name = xrealloc (test->name, exepos - test->name + 1);
254 report (R_STEP, "Extracting: %s", test->name);
256 if (!(fout = fopen (test->exename, "wb")) ||
257 (fwrite (code, size, 1, fout) != 1) ||
258 fclose (fout)) report (R_FATAL, "Failed to write file %s.",
259 test->exename);
262 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
263 stdout to there.
265 Return the exit status, -2 if can't create process or the return
266 value of WaitForSingleObject.
268 static int
269 run_ex (char *cmd, const char *out, const char *tempdir, DWORD ms)
271 STARTUPINFO si;
272 PROCESS_INFORMATION pi;
273 int fd, oldstdout = -1;
274 DWORD wait, status;
276 GetStartupInfo (&si);
277 si.dwFlags = 0;
279 if (out) {
280 fd = open (out, O_WRONLY | O_CREAT, 0666);
281 if (-1 == fd)
282 report (R_FATAL, "Can't open '%s': %d", out, errno);
283 oldstdout = dup (1);
284 if (-1 == oldstdout)
285 report (R_FATAL, "Can't save stdout: %d", errno);
286 if (-1 == dup2 (fd, 1))
287 report (R_FATAL, "Can't redirect stdout: %d", errno);
288 close (fd);
291 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
292 NULL, tempdir, &si, &pi)) {
293 status = -2;
294 } else {
295 CloseHandle (pi.hThread);
296 wait = WaitForSingleObject (pi.hProcess, ms);
297 if (wait == WAIT_OBJECT_0) {
298 GetExitCodeProcess (pi.hProcess, &status);
299 } else {
300 switch (wait) {
301 case WAIT_FAILED:
302 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
303 GetLastError ());
304 break;
305 case WAIT_TIMEOUT:
306 report (R_ERROR, "Process '%s' timed out.", cmd);
307 break;
308 default:
309 report (R_ERROR, "Wait returned %d", wait);
311 status = wait;
312 if (!TerminateProcess (pi.hProcess, 257))
313 report (R_ERROR, "TerminateProcess failed: %d",
314 GetLastError ());
315 wait = WaitForSingleObject (pi.hProcess, 5000);
316 switch (wait) {
317 case WAIT_FAILED:
318 report (R_ERROR,
319 "Wait for termination of '%s' failed: %d",
320 cmd, GetLastError ());
321 break;
322 case WAIT_OBJECT_0:
323 break;
324 case WAIT_TIMEOUT:
325 report (R_ERROR, "Can't kill process '%s'", cmd);
326 break;
327 default:
328 report (R_ERROR, "Waiting for termination: %d",
329 wait);
332 CloseHandle (pi.hProcess);
335 if (out) {
336 close (1);
337 if (-1 == dup2 (oldstdout, 1))
338 report (R_FATAL, "Can't recover stdout: %d", errno);
339 close (oldstdout);
341 return status;
344 static void
345 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
347 char *subname, *cmd;
348 FILE *subfile;
349 size_t total;
350 char buffer[8192], *index;
351 static const char header[] = "Valid test names:";
352 int allocated;
354 test->subtest_count = 0;
356 subname = tempnam (0, "sub");
357 if (!subname) report (R_FATAL, "Can't name subtests file.");
359 extract_test (test, tempdir, res_name);
360 cmd = strmake (NULL, "%s --list", test->exename);
361 run_ex (cmd, subname, tempdir, 5000);
362 free (cmd);
364 subfile = fopen (subname, "r");
365 if (!subfile) {
366 report (R_ERROR, "Can't open subtests output of %s: %d",
367 test->name, errno);
368 goto quit;
370 total = fread (buffer, 1, sizeof buffer, subfile);
371 fclose (subfile);
372 if (sizeof buffer == total) {
373 report (R_ERROR, "Subtest list of %s too big.",
374 test->name, sizeof buffer);
375 goto quit;
377 buffer[total] = 0;
379 index = strstr (buffer, header);
380 if (!index) {
381 report (R_ERROR, "Can't parse subtests output of %s",
382 test->name);
383 goto quit;
385 index += sizeof header;
387 allocated = 10;
388 test->subtests = xmalloc (allocated * sizeof(char*));
389 index = strtok (index, whitespace);
390 while (index) {
391 if (test->subtest_count == allocated) {
392 allocated *= 2;
393 test->subtests = xrealloc (test->subtests,
394 allocated * sizeof(char*));
396 test->subtests[test->subtest_count++] = strdup (index);
397 index = strtok (NULL, whitespace);
399 test->subtests = xrealloc (test->subtests,
400 test->subtest_count * sizeof(char*));
402 quit:
403 if (remove (subname))
404 report (R_WARNING, "Can't delete file '%s': %d",
405 subname, errno);
406 free (subname);
409 static void
410 run_test (struct wine_test* test, const char* subtest, const char *tempdir)
412 int status;
413 const char* file = get_test_source_file(test->name, subtest);
414 const char* rev = get_file_rev(file);
415 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
417 xprintf ("%s:%s start %s %s\n", test->name, subtest, file, rev);
418 status = run_ex (cmd, NULL, tempdir, 120000);
419 free (cmd);
420 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
423 static BOOL CALLBACK
424 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
425 LPTSTR lpszName, LONG_PTR lParam)
427 (*(int*)lParam)++;
428 return TRUE;
431 static BOOL CALLBACK
432 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
433 LPTSTR lpszName, LONG_PTR lParam)
435 const char *tempdir = (const char *)lParam;
436 char dllname[MAX_PATH];
437 HMODULE dll;
439 /* Check if the main dll is present on this system */
440 CharLowerA(lpszName);
441 strcpy(dllname, lpszName);
442 *strstr(dllname, testexe) = 0;
444 dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE);
445 if (!dll) {
446 xprintf ("%s:%s_dll_missing start 0 0\n", dllname, dllname);
447 xprintf ("%s_dll_missing: -1 tests executed (-1 marked as todo, -1 failures), -1 skipped.\n", dllname);
448 xprintf ("%s:%s_dll_missing done (0)\n", dllname, dllname);
449 return TRUE;
451 FreeLibrary(dll);
453 get_subtests( tempdir, &wine_tests[nr_of_files], lpszName );
454 nr_of_tests += wine_tests[nr_of_files].subtest_count;
455 nr_of_files++;
456 return TRUE;
459 static char *
460 run_tests (char *logname)
462 int i;
463 char *tempdir, *shorttempdir;
464 int logfile;
465 char *strres, *eol, *nextline;
466 DWORD strsize;
468 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
470 if (!logname) {
471 logname = tempnam (0, "res");
472 if (!logname) report (R_FATAL, "Can't name logfile.");
474 report (R_OUT, logname);
476 logfile = open (logname, O_WRONLY | O_CREAT | O_EXCL | O_APPEND,
477 0666);
478 if (-1 == logfile) {
479 if (EEXIST == errno)
480 report (R_FATAL, "File %s already exists.", logname);
481 else report (R_FATAL, "Could not open logfile: %d", errno);
483 if (-1 == dup2 (logfile, 1))
484 report (R_FATAL, "Can't redirect stdout: %d", errno);
485 close (logfile);
487 tempdir = tempnam (0, "wct");
488 if (!tempdir)
489 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
490 shorttempdir = strdup (tempdir);
491 if (shorttempdir) { /* try stable path for ZoneAlarm */
492 strstr (shorttempdir, "wct")[3] = 0;
493 if (CreateDirectoryA (shorttempdir, NULL)) {
494 free (tempdir);
495 tempdir = shorttempdir;
496 } else free (shorttempdir);
498 if (tempdir != shorttempdir && !CreateDirectoryA (tempdir, NULL))
499 report (R_FATAL, "Could not create directory: %s", tempdir);
500 report (R_DIR, tempdir);
502 xprintf ("Version 4\n");
503 strres = extract_rcdata (MAKEINTRESOURCE(WINE_BUILD), STRINGRES, &strsize);
504 xprintf ("Tests from build ");
505 if (strres) xprintf ("%.*s", strsize, strres);
506 else xprintf ("-\n");
507 strres = extract_rcdata (MAKEINTRESOURCE(TESTS_URL), STRINGRES, &strsize);
508 xprintf ("Archive: ");
509 if (strres) xprintf ("%.*s", strsize, strres);
510 else xprintf ("-\n");
511 xprintf ("Tag: %s\n", tag);
512 xprintf ("Build info:\n");
513 strres = extract_rcdata (MAKEINTRESOURCE(BUILD_INFO), STRINGRES, &strsize);
514 while (strres) {
515 eol = memchr (strres, '\n', strsize);
516 if (!eol) {
517 nextline = NULL;
518 eol = strres + strsize;
519 } else {
520 strsize -= eol - strres + 1;
521 nextline = strsize?eol+1:NULL;
522 if (eol > strres && *(eol-1) == '\r') eol--;
524 xprintf (" %.*s\n", eol-strres, strres);
525 strres = nextline;
527 xprintf ("Operating system version:\n");
528 print_version ();
529 xprintf ("Test output:\n" );
531 report (R_STATUS, "Counting tests");
532 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
533 EnumTestFileProc, (LPARAM)&nr_of_files))
534 report (R_FATAL, "Can't enumerate test files: %d",
535 GetLastError ());
536 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
538 report (R_STATUS, "Extracting tests");
539 report (R_PROGRESS, 0, nr_of_files);
540 nr_of_files = 0;
541 nr_of_tests = 0;
542 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
543 extract_test_proc, (LPARAM)tempdir))
544 report (R_FATAL, "Can't enumerate test files: %d",
545 GetLastError ());
547 report (R_DELTA, 0, "Extracting: Done");
549 report (R_STATUS, "Running tests");
550 report (R_PROGRESS, 1, nr_of_tests);
551 for (i = 0; i < nr_of_files; i++) {
552 struct wine_test *test = wine_tests + i;
553 int j;
555 for (j = 0; j < test->subtest_count; j++) {
556 report (R_STEP, "Running: %s:%s", test->name,
557 test->subtests[j]);
558 run_test (test, test->subtests[j], tempdir);
561 report (R_DELTA, 0, "Running: Done");
563 report (R_STATUS, "Cleaning up");
564 close (1);
565 remove_dir (tempdir);
566 free (tempdir);
567 free (wine_tests);
569 return logname;
572 static void
573 usage (void)
575 fprintf (stderr,
576 "Usage: winetest [OPTION]...\n\n"
577 " -c console mode, no GUI\n"
578 " -e preserve the environment\n"
579 " -h print this message and exit\n"
580 " -q quiet mode, no output at all\n"
581 " -o FILE put report into FILE, do not submit\n"
582 " -s FILE submit FILE, do not run tests\n"
583 " -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n");
586 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
587 LPSTR cmdLine, int cmdShow)
589 char *logname = NULL;
590 const char *cp, *submit = NULL;
591 int reset_env = 1;
592 int interactive = 1;
594 /* initialize the revision information first */
595 extract_rev_infos();
597 cmdLine = strtok (cmdLine, whitespace);
598 while (cmdLine) {
599 if (cmdLine[0] != '-' || cmdLine[2]) {
600 report (R_ERROR, "Not a single letter option: %s", cmdLine);
601 usage ();
602 exit (2);
604 switch (cmdLine[1]) {
605 case 'c':
606 report (R_TEXTMODE);
607 interactive = 0;
608 break;
609 case 'e':
610 reset_env = 0;
611 break;
612 case 'h':
613 usage ();
614 exit (0);
615 case 'q':
616 report (R_QUIET);
617 interactive = 0;
618 break;
619 case 's':
620 submit = strtok (NULL, whitespace);
621 if (tag)
622 report (R_WARNING, "ignoring tag for submission");
623 send_file (submit);
624 break;
625 case 'o':
626 logname = strtok (NULL, whitespace);
627 break;
628 case 't':
629 tag = strtok (NULL, whitespace);
630 if (strlen (tag) > MAXTAGLEN)
631 report (R_FATAL, "tag is too long (maximum %d characters)",
632 MAXTAGLEN);
633 cp = findbadtagchar (tag);
634 if (cp) {
635 report (R_ERROR, "invalid char in tag: %c", *cp);
636 usage ();
637 exit (2);
639 break;
640 default:
641 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
642 usage ();
643 exit (2);
645 cmdLine = strtok (NULL, whitespace);
647 if (!submit) {
648 static CHAR platform_windows[] = "WINETEST_PLATFORM=windows",
649 platform_wine[] = "WINETEST_PLATFORM=wine",
650 debug_yes[] = "WINETEST_DEBUG=1",
651 interactive_no[] = "WINETEST_INTERACTIVE=0",
652 report_success_no[] = "WINETEST_REPORT_SUCCESS=0";
653 CHAR *platform;
655 report (R_STATUS, "Starting up");
657 if (!running_on_visible_desktop ())
658 report (R_FATAL, "Tests must be run on a visible desktop");
660 platform = running_under_wine () ? platform_wine : platform_windows;
662 if (reset_env && (putenv (platform) ||
663 putenv (debug_yes) ||
664 putenv (interactive_no) ||
665 putenv (report_success_no)))
666 report (R_FATAL, "Could not reset environment: %d", errno);
668 if (!tag) {
669 if (!interactive)
670 report (R_FATAL, "Please specify a tag (-t option) if "
671 "running noninteractive!");
672 if (guiAskTag () == IDABORT) exit (1);
674 report (R_TAG);
676 if (!logname) {
677 logname = run_tests (NULL);
678 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
679 "test results?") == IDYES)
680 if (!send_file (logname) && remove (logname))
681 report (R_WARNING, "Can't remove logfile: %d.", errno);
682 free (logname);
683 } else run_tests (logname);
684 report (R_STATUS, "Finished");
686 exit (0);