Make running tests on a visible desktop a mandatory requirement.
[wine/wine-kai.git] / programs / winetest / main.c
blob9167b335eed2c01677db4d1cc3484da9fa3c4d07
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <errno.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <windows.h>
39 #include "winetest.h"
40 #include "resource.h"
42 struct wine_test
44 char *name;
45 int resource;
46 int subtest_count;
47 char **subtests;
48 char *exename;
51 struct rev_info
53 const char* file;
54 const char* rev;
57 static struct wine_test *wine_tests;
58 static struct rev_info *rev_infos = NULL;
59 static const char whitespace[] = " \t\r\n";
61 static int running_under_wine ()
63 HMODULE module = GetModuleHandleA("ntdll.dll");
65 if (!module) return 0;
66 return (GetProcAddress(module, "wine_server_call") != NULL);
69 static int running_on_visible_desktop ()
71 BOOL visible;
72 HWND desktop;
73 HDC hdc;
74 HRGN hrgn;
75 RECT rc;
77 desktop = GetDesktopWindow();
78 hdc = GetDC(desktop);
79 hrgn = CreateRectRgn(0, 0, 0, 0);
80 GetRandomRgn(hdc, hrgn, SYSRGN);
82 visible = GetRgnBox(hrgn, &rc) != NULLREGION;
84 DeleteObject(hrgn);
85 ReleaseDC(desktop, hdc);
87 return visible;
90 void print_version ()
92 OSVERSIONINFOEX ver;
93 BOOL ext;
95 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
96 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
98 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
99 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
100 report (R_FATAL, "Can't get OS version.");
103 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
104 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
105 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
106 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
107 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
108 ver.dwPlatformId, ver.szCSDVersion);
110 if (!ext) return;
112 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
113 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
114 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
115 ver.wProductType, ver.wReserved);
118 static inline int is_dot_dir(const char* x)
120 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
123 void remove_dir (const char *dir)
125 HANDLE hFind;
126 WIN32_FIND_DATA wfd;
127 char path[MAX_PATH];
128 size_t dirlen = strlen (dir);
130 /* Make sure the directory exists before going further */
131 memcpy (path, dir, dirlen);
132 strcpy (path + dirlen++, "\\*");
133 hFind = FindFirstFile (path, &wfd);
134 if (hFind == INVALID_HANDLE_VALUE) return;
136 do {
137 char *lp = wfd.cFileName;
139 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
140 if (is_dot_dir (lp)) continue;
141 strcpy (path + dirlen, lp);
142 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
143 remove_dir(path);
144 else if (!DeleteFile (path))
145 report (R_WARNING, "Can't delete file %s: error %d",
146 path, GetLastError ());
147 } while (FindNextFile (hFind, &wfd));
148 FindClose (hFind);
149 if (!RemoveDirectory (dir))
150 report (R_WARNING, "Can't remove directory %s: error %d",
151 dir, GetLastError ());
154 const char* get_test_source_file(const char* test, const char* subtest)
156 static const char* special_dirs[][2] = {
157 { "gdi32", "gdi"}, { "kernel32", "kernel" },
158 { "msacm32", "msacm" },
159 { "user32", "user" }, { "winspool.drv", "winspool" },
160 { "ws2_32", "winsock" }, { 0, 0 }
162 static char buffer[MAX_PATH];
163 int i;
165 for (i = 0; special_dirs[i][0]; i++) {
166 if (strcmp(test, special_dirs[i][0]) == 0) {
167 test = special_dirs[i][1];
168 break;
172 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
173 return buffer;
176 const char* get_file_rev(const char* file)
178 const struct rev_info* rev;
180 for(rev = rev_infos; rev->file; rev++) {
181 if (strcmp(rev->file, file) == 0) return rev->rev;
184 return "-";
187 void extract_rev_infos ()
189 char revinfo[256], *p;
190 int size = 0, i, len;
191 HMODULE module = GetModuleHandle (NULL);
193 for (i = 0; TRUE; i++) {
194 if (i >= size) {
195 size += 100;
196 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
198 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
200 len = LoadStringA (module, REV_INFO+i, revinfo, sizeof(revinfo));
201 if (len == 0) break; /* end of revision info */
202 if (len >= sizeof(revinfo) - 1)
203 report (R_FATAL, "Revision info too long.");
204 if(!(p = strrchr(revinfo, ':')))
205 report (R_FATAL, "Revision info malformed (i=%d)", i);
206 *p = 0;
207 rev_infos[i].file = strdup(revinfo);
208 rev_infos[i].rev = strdup(p + 1);
212 void* extract_rcdata (int id, int type, DWORD* size)
214 HRSRC rsrc;
215 HGLOBAL hdl;
216 LPVOID addr;
218 if (!(rsrc = FindResource (NULL, (LPTSTR)id, MAKEINTRESOURCE(type))) ||
219 !(*size = SizeofResource (0, rsrc)) ||
220 !(hdl = LoadResource (0, rsrc)) ||
221 !(addr = LockResource (hdl)))
222 return NULL;
223 return addr;
226 /* Fills in the name and exename fields */
227 void
228 extract_test (struct wine_test *test, const char *dir, int id)
230 BYTE* code;
231 DWORD size;
232 FILE* fout;
233 int strlen, bufflen = 128;
234 char *exepos;
236 code = extract_rcdata (id, TESTRES, &size);
237 if (!code) report (R_FATAL, "Can't find test resource %d: %d",
238 id, GetLastError ());
239 test->name = xmalloc (bufflen);
240 while ((strlen = LoadStringA (NULL, id, test->name, bufflen))
241 == bufflen - 1) {
242 bufflen *= 2;
243 test->name = xrealloc (test->name, bufflen);
245 if (!strlen) report (R_FATAL, "Can't read name of test %d.", id);
246 test->exename = strmake (NULL, "%s/%s", dir, test->name);
247 exepos = strstr (test->name, "_test.exe");
248 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
249 *exepos = 0;
250 test->name = xrealloc (test->name, exepos - test->name + 1);
251 report (R_STEP, "Extracting: %s", test->name);
253 if (!(fout = fopen (test->exename, "wb")) ||
254 (fwrite (code, size, 1, fout) != 1) ||
255 fclose (fout)) report (R_FATAL, "Failed to write file %s.",
256 test->exename);
259 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
260 stdout to there.
262 Return the exit status, -2 if can't create process or the return
263 value of WaitForSingleObject.
266 run_ex (char *cmd, const char *out, DWORD ms)
268 STARTUPINFO si;
269 PROCESS_INFORMATION pi;
270 int fd, oldstdout = -1;
271 DWORD wait, status;
273 GetStartupInfo (&si);
274 si.wShowWindow = SW_HIDE;
275 si.dwFlags = STARTF_USESHOWWINDOW;
277 if (out) {
278 fd = open (out, O_WRONLY | O_CREAT, 0666);
279 if (-1 == fd)
280 report (R_FATAL, "Can't open '%s': %d", out, errno);
281 oldstdout = dup (1);
282 if (-1 == oldstdout)
283 report (R_FATAL, "Can't save stdout: %d", errno);
284 if (-1 == dup2 (fd, 1))
285 report (R_FATAL, "Can't redirect stdout: %d", errno);
286 close (fd);
289 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
290 NULL, NULL, &si, &pi)) {
291 status = -2;
292 } else {
293 CloseHandle (pi.hThread);
294 wait = WaitForSingleObject (pi.hProcess, ms);
295 if (wait == WAIT_OBJECT_0) {
296 GetExitCodeProcess (pi.hProcess, &status);
297 } else {
298 switch (wait) {
299 case WAIT_FAILED:
300 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
301 GetLastError ());
302 break;
303 case WAIT_TIMEOUT:
304 report (R_ERROR, "Process '%s' timed out.", cmd);
305 break;
306 default:
307 report (R_ERROR, "Wait returned %d", wait);
309 status = wait;
310 if (!TerminateProcess (pi.hProcess, 257))
311 report (R_ERROR, "TerminateProcess failed: %d",
312 GetLastError ());
313 wait = WaitForSingleObject (pi.hProcess, 5000);
314 switch (wait) {
315 case WAIT_FAILED:
316 report (R_ERROR,
317 "Wait for termination of '%s' failed: %d",
318 cmd, GetLastError ());
319 break;
320 case WAIT_OBJECT_0:
321 break;
322 case WAIT_TIMEOUT:
323 report (R_ERROR, "Can't kill process '%s'", cmd);
324 break;
325 default:
326 report (R_ERROR, "Waiting for termination: %d",
327 wait);
330 CloseHandle (pi.hProcess);
333 if (out) {
334 close (1);
335 if (-1 == dup2 (oldstdout, 1))
336 report (R_FATAL, "Can't recover stdout: %d", errno);
337 close (oldstdout);
339 return status;
342 void
343 get_subtests (const char *tempdir, struct wine_test *test, int id)
345 char *subname;
346 FILE *subfile;
347 size_t total;
348 char buffer[8192], *index;
349 static const char header[] = "Valid test names:";
350 int allocated;
352 test->subtest_count = 0;
354 subname = tempnam (0, "sub");
355 if (!subname) report (R_FATAL, "Can't name subtests file.");
357 extract_test (test, tempdir, id);
358 run_ex (test->exename, subname, 5000);
360 subfile = fopen (subname, "r");
361 if (!subfile) {
362 report (R_ERROR, "Can't open subtests output of %s: %d",
363 test->name, errno);
364 goto quit;
366 total = fread (buffer, 1, sizeof buffer, subfile);
367 fclose (subfile);
368 if (sizeof buffer == total) {
369 report (R_ERROR, "Subtest list of %s too big.",
370 test->name, sizeof buffer);
371 goto quit;
373 buffer[total] = 0;
375 index = strstr (buffer, header);
376 if (!index) {
377 report (R_ERROR, "Can't parse subtests output of %s",
378 test->name);
379 goto quit;
381 index += sizeof header;
383 allocated = 10;
384 test->subtests = xmalloc (allocated * sizeof(char*));
385 index = strtok (index, whitespace);
386 while (index) {
387 if (test->subtest_count == allocated) {
388 allocated *= 2;
389 test->subtests = xrealloc (test->subtests,
390 allocated * sizeof(char*));
392 test->subtests[test->subtest_count++] = strdup (index);
393 index = strtok (NULL, whitespace);
395 test->subtests = xrealloc (test->subtests,
396 test->subtest_count * sizeof(char*));
398 quit:
399 if (remove (subname))
400 report (R_WARNING, "Can't delete file '%s': %d",
401 subname, errno);
402 free (subname);
405 void
406 run_test (struct wine_test* test, const char* subtest)
408 int status;
409 const char* file = get_test_source_file(test->name, subtest);
410 const char* rev = get_file_rev(file);
411 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
413 xprintf ("%s:%s start %s %s\n", test->name, subtest, file, rev);
414 status = run_ex (cmd, NULL, 120000);
415 free (cmd);
416 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
419 BOOL CALLBACK
420 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
421 LPTSTR lpszName, LONG_PTR lParam)
423 (*(int*)lParam)++;
424 return TRUE;
427 char *
428 run_tests (char *logname, const char *tag)
430 int nr_of_files = 0, nr_of_tests = 0, i;
431 char *tempdir;
432 int logfile;
433 char *strres, *eol, *nextline;
434 DWORD strsize;
436 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
438 if (!logname) {
439 logname = tempnam (0, "res");
440 if (!logname) report (R_FATAL, "Can't name logfile.");
442 report (R_OUT, logname);
444 logfile = open (logname, O_WRONLY | O_CREAT | O_EXCL | O_APPEND,
445 0666);
446 if (-1 == logfile) {
447 if (EEXIST == errno)
448 report (R_FATAL, "File %s already exists.", logname);
449 else report (R_FATAL, "Could not open logfile: %d", errno);
451 if (-1 == dup2 (logfile, 1))
452 report (R_FATAL, "Can't redirect stdout: %d", errno);
453 close (logfile);
455 tempdir = tempnam (0, "wct");
456 if (!tempdir)
457 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
458 report (R_DIR, tempdir);
459 if (!CreateDirectory (tempdir, NULL))
460 report (R_FATAL, "Could not create directory: %s", tempdir);
462 xprintf ("Version 3\n");
463 strres = extract_rcdata (WINE_BUILD, STRINGRES, &strsize);
464 xprintf ("Tests from build ");
465 if (strres) xprintf ("%.*s", strsize, strres);
466 else xprintf ("-\n");
467 strres = extract_rcdata (TESTS_URL, STRINGRES, &strsize);
468 xprintf ("Archive: ");
469 if (strres) xprintf ("%.*s", strsize, strres);
470 else xprintf ("-\n");
471 xprintf ("Tag: %s\n", tag?tag:"");
472 xprintf ("Build info:\n");
473 strres = extract_rcdata (BUILD_INFO, STRINGRES, &strsize);
474 while (strres) {
475 eol = memchr (strres, '\n', strsize);
476 if (!eol) {
477 nextline = NULL;
478 eol = strres + strsize;
479 } else {
480 strsize -= eol - strres + 1;
481 nextline = strsize?eol+1:NULL;
482 if (eol > strres && *(eol-1) == '\r') eol--;
484 xprintf (" %.*s\n", eol-strres, strres);
485 strres = nextline;
487 xprintf ("Operating system version:\n");
488 print_version ();
489 xprintf ("Test output:\n" );
491 report (R_STATUS, "Counting tests");
492 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
493 EnumTestFileProc, (LPARAM)&nr_of_files))
494 report (R_FATAL, "Can't enumerate test files: %d",
495 GetLastError ());
496 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
498 report (R_STATUS, "Extracting tests");
499 report (R_PROGRESS, 0, nr_of_files);
500 for (i = 0; i < nr_of_files; i++) {
501 get_subtests (tempdir, wine_tests+i, i);
502 nr_of_tests += wine_tests[i].subtest_count;
504 report (R_DELTA, 0, "Extracting: Done");
506 report (R_STATUS, "Running tests");
507 report (R_PROGRESS, 1, nr_of_tests);
508 for (i = 0; i < nr_of_files; i++) {
509 struct wine_test *test = wine_tests + i;
510 int j;
512 for (j = 0; j < test->subtest_count; j++) {
513 report (R_STEP, "Running: %s:%s", test->name,
514 test->subtests[j]);
515 run_test (test, test->subtests[j]);
518 report (R_DELTA, 0, "Running: Done");
520 report (R_STATUS, "Cleaning up");
521 close (1);
522 remove_dir (tempdir);
523 free (tempdir);
524 free (wine_tests);
526 return logname;
529 void
530 usage ()
532 fprintf (stderr, "\
533 Usage: winetest [OPTION]...\n\n\
534 -c console mode, no GUI\n\
535 -e preserve the environment\n\
536 -h print this message and exit\n\
537 -q quiet mode, no output at all\n\
538 -o FILE put report into FILE, do not submit\n\
539 -s FILE submit FILE, do not run tests\n\
540 -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n");
543 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
544 LPSTR cmdLine, int cmdShow)
546 char *logname = NULL;
547 const char *cp, *submit = NULL, *tag = NULL;
548 int reset_env = 1;
550 if (!running_on_visible_desktop ()) {
551 report (R_ERROR, "Tests must be run on a visible desktop");
552 exit (2);
555 /* initialize the revision information first */
556 extract_rev_infos();
558 cmdLine = strtok (cmdLine, whitespace);
559 while (cmdLine) {
560 if (cmdLine[0] != '-' || cmdLine[2]) {
561 report (R_ERROR, "Not a single letter option: %s", cmdLine);
562 usage ();
563 exit (2);
565 switch (cmdLine[1]) {
566 case 'c':
567 report (R_TEXTMODE);
568 break;
569 case 'e':
570 reset_env = 0;
571 break;
572 case 'h':
573 usage ();
574 exit (0);
575 case 'q':
576 report (R_QUIET);
577 break;
578 case 's':
579 submit = strtok (NULL, whitespace);
580 if (tag)
581 report (R_WARNING, "ignoring tag for submission");
582 send_file (submit);
583 break;
584 case 'o':
585 logname = strtok (NULL, whitespace);
586 break;
587 case 't':
588 tag = strtok (NULL, whitespace);
589 cp = badtagchar (tag);
590 if (cp) {
591 report (R_ERROR, "invalid char in tag: %c", *cp);
592 usage ();
593 exit (2);
595 break;
596 default:
597 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
598 usage ();
599 exit (2);
601 cmdLine = strtok (NULL, whitespace);
603 if (!submit) {
604 if (reset_env && (putenv ("WINETEST_PLATFORM=windows") ||
605 putenv ("WINETEST_DEBUG=1") ||
606 putenv ("WINETEST_INTERACTIVE=0") ||
607 putenv ("WINETEST_REPORT_SUCCESS=0")))
608 report (R_FATAL, "Could not reset environment: %d", errno);
610 report (R_STATUS, "Starting up");
611 if (!logname) {
612 logname = run_tests (NULL, tag);
613 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
614 "test results?") == IDYES)
615 if (!send_file (logname) && remove (logname))
616 report (R_WARNING, "Can't remove logfile: %d.", errno);
617 free (logname);
618 } else run_tests (logname, tag);
619 report (R_STATUS, "Finished");
621 exit (0);