Make dump functions const.
[wine/multimedia.git] / programs / winetest / main.c
blobc91876fdfa7763a53ed3519e5c1b7a5755424b26
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 <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 struct rev_info *rev_infos = NULL;
61 static const char whitespace[] = " \t\r\n";
63 static int running_under_wine (void)
65 HMODULE module = GetModuleHandleA("ntdll.dll");
67 if (!module) return 0;
68 return (GetProcAddress(module, "wine_server_call") != NULL);
71 static int running_on_visible_desktop (void)
73 HWND desktop;
74 HMODULE huser32 = GetModuleHandle("user32.dll");
75 FARPROC pGetProcessWindowStation = GetProcAddress(huser32, "GetProcessWindowStation");
76 FARPROC pGetUserObjectInformationA = GetProcAddress(huser32, "GetUserObjectInformationA");
78 desktop = GetDesktopWindow();
79 if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
80 return IsWindowVisible(desktop);
82 if (pGetProcessWindowStation && pGetUserObjectInformationA)
84 DWORD len;
85 HWINSTA wstation;
86 USEROBJECTFLAGS uoflags;
88 wstation = (HWINSTA)pGetProcessWindowStation();
89 assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
90 return (uoflags.dwFlags & WSF_VISIBLE) != 0;
92 return IsWindowVisible(desktop);
95 static void print_version (void)
97 OSVERSIONINFOEX ver;
98 BOOL ext;
99 int is_win2k3_r2;
101 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
102 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
104 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
105 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
106 report (R_FATAL, "Can't get OS version.");
109 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
110 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
111 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
112 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
113 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
114 ver.dwPlatformId, ver.szCSDVersion);
116 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
117 if(is_win2k3_r2)
118 xprintf(" R2 build number=%d\n", is_win2k3_r2);
120 if (!ext) return;
122 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
123 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
124 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
125 ver.wProductType, ver.wReserved);
128 static inline int is_dot_dir(const char* x)
130 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
133 static void remove_dir (const char *dir)
135 HANDLE hFind;
136 WIN32_FIND_DATA wfd;
137 char path[MAX_PATH];
138 size_t dirlen = strlen (dir);
140 /* Make sure the directory exists before going further */
141 memcpy (path, dir, dirlen);
142 strcpy (path + dirlen++, "\\*");
143 hFind = FindFirstFile (path, &wfd);
144 if (hFind == INVALID_HANDLE_VALUE) return;
146 do {
147 char *lp = wfd.cFileName;
149 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
150 if (is_dot_dir (lp)) continue;
151 strcpy (path + dirlen, lp);
152 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
153 remove_dir(path);
154 else if (!DeleteFile (path))
155 report (R_WARNING, "Can't delete file %s: error %d",
156 path, GetLastError ());
157 } while (FindNextFile (hFind, &wfd));
158 FindClose (hFind);
159 if (!RemoveDirectory (dir))
160 report (R_WARNING, "Can't remove directory %s: error %d",
161 dir, GetLastError ());
164 static const char* get_test_source_file(const char* test, const char* subtest)
166 static const char* special_dirs[][2] = {
167 { "gdi32", "gdi"}, { "kernel32", "kernel" },
168 { "msacm32", "msacm" },
169 { "user32", "user" }, { "winspool.drv", "winspool" },
170 { "ws2_32", "winsock" }, { 0, 0 }
172 static char buffer[MAX_PATH];
173 int i;
175 for (i = 0; special_dirs[i][0]; i++) {
176 if (strcmp(test, special_dirs[i][0]) == 0) {
177 test = special_dirs[i][1];
178 break;
182 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
183 return buffer;
186 static const char* get_file_rev(const char* file)
188 const struct rev_info* rev;
190 for(rev = rev_infos; rev->file; rev++) {
191 if (strcmp(rev->file, file) == 0) return rev->rev;
194 return "-";
197 static void extract_rev_infos (void)
199 char revinfo[256], *p;
200 int size = 0, i;
201 unsigned int len;
202 HMODULE module = GetModuleHandle (NULL);
204 for (i = 0; TRUE; i++) {
205 if (i >= size) {
206 size += 100;
207 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
209 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
211 len = LoadStringA (module, REV_INFO+i, revinfo, sizeof(revinfo));
212 if (len == 0) break; /* end of revision info */
213 if (len >= sizeof(revinfo) - 1)
214 report (R_FATAL, "Revision info too long.");
215 if(!(p = strrchr(revinfo, ':')))
216 report (R_FATAL, "Revision info malformed (i=%d)", i);
217 *p = 0;
218 rev_infos[i].file = strdup(revinfo);
219 rev_infos[i].rev = strdup(p + 1);
223 static void* extract_rcdata (int id, int type, DWORD* size)
225 HRSRC rsrc;
226 HGLOBAL hdl;
227 LPVOID addr;
229 if (!(rsrc = FindResource (NULL, (LPTSTR)id, MAKEINTRESOURCE(type))) ||
230 !(*size = SizeofResource (0, rsrc)) ||
231 !(hdl = LoadResource (0, rsrc)) ||
232 !(addr = LockResource (hdl)))
233 return NULL;
234 return addr;
237 /* Fills in the name and exename fields */
238 static void
239 extract_test (struct wine_test *test, const char *dir, int id)
241 BYTE* code;
242 DWORD size;
243 FILE* fout;
244 int strlen, bufflen = 128;
245 char *exepos;
247 code = extract_rcdata (id, TESTRES, &size);
248 if (!code) report (R_FATAL, "Can't find test resource %d: %d",
249 id, GetLastError ());
250 test->name = xmalloc (bufflen);
251 while ((strlen = LoadStringA (NULL, id, test->name, bufflen))
252 == bufflen - 1) {
253 bufflen *= 2;
254 test->name = xrealloc (test->name, bufflen);
256 if (!strlen) report (R_FATAL, "Can't read name of test %d.", id);
257 test->exename = strmake (NULL, "%s/%s", dir, test->name);
258 exepos = strstr (test->name, "_test.exe");
259 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
260 *exepos = 0;
261 test->name = xrealloc (test->name, exepos - test->name + 1);
262 report (R_STEP, "Extracting: %s", test->name);
264 if (!(fout = fopen (test->exename, "wb")) ||
265 (fwrite (code, size, 1, fout) != 1) ||
266 fclose (fout)) report (R_FATAL, "Failed to write file %s.",
267 test->exename);
270 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
271 stdout to there.
273 Return the exit status, -2 if can't create process or the return
274 value of WaitForSingleObject.
276 static int
277 run_ex (char *cmd, const char *out, DWORD ms)
279 STARTUPINFO si;
280 PROCESS_INFORMATION pi;
281 int fd, oldstdout = -1;
282 DWORD wait, status;
284 GetStartupInfo (&si);
285 si.wShowWindow = SW_HIDE;
286 si.dwFlags = STARTF_USESHOWWINDOW;
288 if (out) {
289 fd = open (out, O_WRONLY | O_CREAT, 0666);
290 if (-1 == fd)
291 report (R_FATAL, "Can't open '%s': %d", out, errno);
292 oldstdout = dup (1);
293 if (-1 == oldstdout)
294 report (R_FATAL, "Can't save stdout: %d", errno);
295 if (-1 == dup2 (fd, 1))
296 report (R_FATAL, "Can't redirect stdout: %d", errno);
297 close (fd);
300 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
301 NULL, NULL, &si, &pi)) {
302 status = -2;
303 } else {
304 CloseHandle (pi.hThread);
305 wait = WaitForSingleObject (pi.hProcess, ms);
306 if (wait == WAIT_OBJECT_0) {
307 GetExitCodeProcess (pi.hProcess, &status);
308 } else {
309 switch (wait) {
310 case WAIT_FAILED:
311 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
312 GetLastError ());
313 break;
314 case WAIT_TIMEOUT:
315 report (R_ERROR, "Process '%s' timed out.", cmd);
316 break;
317 default:
318 report (R_ERROR, "Wait returned %d", wait);
320 status = wait;
321 if (!TerminateProcess (pi.hProcess, 257))
322 report (R_ERROR, "TerminateProcess failed: %d",
323 GetLastError ());
324 wait = WaitForSingleObject (pi.hProcess, 5000);
325 switch (wait) {
326 case WAIT_FAILED:
327 report (R_ERROR,
328 "Wait for termination of '%s' failed: %d",
329 cmd, GetLastError ());
330 break;
331 case WAIT_OBJECT_0:
332 break;
333 case WAIT_TIMEOUT:
334 report (R_ERROR, "Can't kill process '%s'", cmd);
335 break;
336 default:
337 report (R_ERROR, "Waiting for termination: %d",
338 wait);
341 CloseHandle (pi.hProcess);
344 if (out) {
345 close (1);
346 if (-1 == dup2 (oldstdout, 1))
347 report (R_FATAL, "Can't recover stdout: %d", errno);
348 close (oldstdout);
350 return status;
353 static void
354 get_subtests (const char *tempdir, struct wine_test *test, int id)
356 char *subname, *cmd;
357 FILE *subfile;
358 size_t total;
359 char buffer[8192], *index;
360 static const char header[] = "Valid test names:";
361 int allocated;
363 test->subtest_count = 0;
365 subname = tempnam (0, "sub");
366 if (!subname) report (R_FATAL, "Can't name subtests file.");
368 extract_test (test, tempdir, id);
369 cmd = strmake (NULL, "%s --list", test->exename);
370 run_ex (cmd, subname, 5000);
371 free (cmd);
373 subfile = fopen (subname, "r");
374 if (!subfile) {
375 report (R_ERROR, "Can't open subtests output of %s: %d",
376 test->name, errno);
377 goto quit;
379 total = fread (buffer, 1, sizeof buffer, subfile);
380 fclose (subfile);
381 if (sizeof buffer == total) {
382 report (R_ERROR, "Subtest list of %s too big.",
383 test->name, sizeof buffer);
384 goto quit;
386 buffer[total] = 0;
388 index = strstr (buffer, header);
389 if (!index) {
390 report (R_ERROR, "Can't parse subtests output of %s",
391 test->name);
392 goto quit;
394 index += sizeof header;
396 allocated = 10;
397 test->subtests = xmalloc (allocated * sizeof(char*));
398 index = strtok (index, whitespace);
399 while (index) {
400 if (test->subtest_count == allocated) {
401 allocated *= 2;
402 test->subtests = xrealloc (test->subtests,
403 allocated * sizeof(char*));
405 test->subtests[test->subtest_count++] = strdup (index);
406 index = strtok (NULL, whitespace);
408 test->subtests = xrealloc (test->subtests,
409 test->subtest_count * sizeof(char*));
411 quit:
412 if (remove (subname))
413 report (R_WARNING, "Can't delete file '%s': %d",
414 subname, errno);
415 free (subname);
418 static void
419 run_test (struct wine_test* test, const char* subtest)
421 int status;
422 const char* file = get_test_source_file(test->name, subtest);
423 const char* rev = get_file_rev(file);
424 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
426 xprintf ("%s:%s start %s %s\n", test->name, subtest, file, rev);
427 status = run_ex (cmd, NULL, 120000);
428 free (cmd);
429 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
432 static BOOL CALLBACK
433 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
434 LPTSTR lpszName, LONG_PTR lParam)
436 (*(int*)lParam)++;
437 return TRUE;
440 static char *
441 run_tests (char *logname)
443 int nr_of_files = 0, nr_of_tests = 0, i;
444 char *tempdir, *shorttempdir;
445 int logfile;
446 char *strres, *eol, *nextline;
447 DWORD strsize;
449 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
451 if (!logname) {
452 logname = tempnam (0, "res");
453 if (!logname) report (R_FATAL, "Can't name logfile.");
455 report (R_OUT, logname);
457 logfile = open (logname, O_WRONLY | O_CREAT | O_EXCL | O_APPEND,
458 0666);
459 if (-1 == logfile) {
460 if (EEXIST == errno)
461 report (R_FATAL, "File %s already exists.", logname);
462 else report (R_FATAL, "Could not open logfile: %d", errno);
464 if (-1 == dup2 (logfile, 1))
465 report (R_FATAL, "Can't redirect stdout: %d", errno);
466 close (logfile);
468 tempdir = tempnam (0, "wct");
469 if (!tempdir)
470 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
471 shorttempdir = strdup (tempdir);
472 if (shorttempdir) { /* try stable path for ZoneAlarm */
473 strstr (shorttempdir, "wct")[3] = 0;
474 if (CreateDirectoryA (shorttempdir, NULL)) {
475 free (tempdir);
476 tempdir = shorttempdir;
477 } else free (shorttempdir);
479 if (tempdir != shorttempdir && !CreateDirectoryA (tempdir, NULL))
480 report (R_FATAL, "Could not create directory: %s", tempdir);
481 report (R_DIR, tempdir);
483 xprintf ("Version 3\n");
484 strres = extract_rcdata (WINE_BUILD, STRINGRES, &strsize);
485 xprintf ("Tests from build ");
486 if (strres) xprintf ("%.*s", strsize, strres);
487 else xprintf ("-\n");
488 strres = extract_rcdata (TESTS_URL, STRINGRES, &strsize);
489 xprintf ("Archive: ");
490 if (strres) xprintf ("%.*s", strsize, strres);
491 else xprintf ("-\n");
492 xprintf ("Tag: %s\n", tag);
493 xprintf ("Build info:\n");
494 strres = extract_rcdata (BUILD_INFO, STRINGRES, &strsize);
495 while (strres) {
496 eol = memchr (strres, '\n', strsize);
497 if (!eol) {
498 nextline = NULL;
499 eol = strres + strsize;
500 } else {
501 strsize -= eol - strres + 1;
502 nextline = strsize?eol+1:NULL;
503 if (eol > strres && *(eol-1) == '\r') eol--;
505 xprintf (" %.*s\n", eol-strres, strres);
506 strres = nextline;
508 xprintf ("Operating system version:\n");
509 print_version ();
510 xprintf ("Test output:\n" );
512 report (R_STATUS, "Counting tests");
513 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
514 EnumTestFileProc, (LPARAM)&nr_of_files))
515 report (R_FATAL, "Can't enumerate test files: %d",
516 GetLastError ());
517 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
519 report (R_STATUS, "Extracting tests");
520 report (R_PROGRESS, 0, nr_of_files);
521 for (i = 0; i < nr_of_files; i++) {
522 get_subtests (tempdir, wine_tests+i, i);
523 nr_of_tests += wine_tests[i].subtest_count;
525 report (R_DELTA, 0, "Extracting: Done");
527 report (R_STATUS, "Running tests");
528 report (R_PROGRESS, 1, nr_of_tests);
529 for (i = 0; i < nr_of_files; i++) {
530 struct wine_test *test = wine_tests + i;
531 int j;
533 for (j = 0; j < test->subtest_count; j++) {
534 report (R_STEP, "Running: %s:%s", test->name,
535 test->subtests[j]);
536 run_test (test, test->subtests[j]);
539 report (R_DELTA, 0, "Running: Done");
541 report (R_STATUS, "Cleaning up");
542 close (1);
543 remove_dir (tempdir);
544 free (tempdir);
545 free (wine_tests);
547 return logname;
550 static void
551 usage (void)
553 fprintf (stderr, "\
554 Usage: winetest [OPTION]...\n\n\
555 -c console mode, no GUI\n\
556 -e preserve the environment\n\
557 -h print this message and exit\n\
558 -q quiet mode, no output at all\n\
559 -o FILE put report into FILE, do not submit\n\
560 -s FILE submit FILE, do not run tests\n\
561 -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n");
564 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
565 LPSTR cmdLine, int cmdShow)
567 char *logname = NULL;
568 const char *cp, *submit = NULL;
569 int reset_env = 1;
570 int interactive = 1;
572 /* initialize the revision information first */
573 extract_rev_infos();
575 cmdLine = strtok (cmdLine, whitespace);
576 while (cmdLine) {
577 if (cmdLine[0] != '-' || cmdLine[2]) {
578 report (R_ERROR, "Not a single letter option: %s", cmdLine);
579 usage ();
580 exit (2);
582 switch (cmdLine[1]) {
583 case 'c':
584 report (R_TEXTMODE);
585 interactive = 0;
586 break;
587 case 'e':
588 reset_env = 0;
589 break;
590 case 'h':
591 usage ();
592 exit (0);
593 case 'q':
594 report (R_QUIET);
595 interactive = 0;
596 break;
597 case 's':
598 submit = strtok (NULL, whitespace);
599 if (tag)
600 report (R_WARNING, "ignoring tag for submission");
601 send_file (submit);
602 break;
603 case 'o':
604 logname = strtok (NULL, whitespace);
605 break;
606 case 't':
607 tag = strtok (NULL, whitespace);
608 if (strlen (tag) > MAXTAGLEN)
609 report (R_FATAL, "tag is too long (maximum %d characters)",
610 MAXTAGLEN);
611 cp = findbadtagchar (tag);
612 if (cp) {
613 report (R_ERROR, "invalid char in tag: %c", *cp);
614 usage ();
615 exit (2);
617 break;
618 default:
619 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
620 usage ();
621 exit (2);
623 cmdLine = strtok (NULL, whitespace);
625 if (!submit) {
626 report (R_STATUS, "Starting up");
628 if (!running_on_visible_desktop ())
629 report (R_FATAL, "Tests must be run on a visible desktop");
631 if (reset_env && (putenv ("WINETEST_PLATFORM=windows") ||
632 putenv ("WINETEST_DEBUG=1") ||
633 putenv ("WINETEST_INTERACTIVE=0") ||
634 putenv ("WINETEST_REPORT_SUCCESS=0")))
635 report (R_FATAL, "Could not reset environment: %d", errno);
637 if (!tag) {
638 if (!interactive)
639 report (R_FATAL, "Please specify a tag (-t option) if "
640 "running noninteractive!");
641 if (guiAskTag () == IDABORT) exit (1);
643 report (R_TAG);
645 if (!logname) {
646 logname = run_tests (NULL);
647 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
648 "test results?") == IDYES)
649 if (!send_file (logname) && remove (logname))
650 report (R_WARNING, "Can't remove logfile: %d.", errno);
651 free (logname);
652 } else run_tests (logname);
653 report (R_STATUS, "Finished");
655 exit (0);