ntdll: Initialize needs_close to FALSE.
[wine/hacks.git] / programs / winetest / main.c
blob2488e30052323011cd582a445be9841462827f3c
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";
64 static int running_under_wine (void)
66 HMODULE module = GetModuleHandleA("ntdll.dll");
68 if (!module) return 0;
69 return (GetProcAddress(module, "wine_server_call") != NULL);
72 static int running_on_visible_desktop (void)
74 HWND desktop;
75 HMODULE huser32 = GetModuleHandle("user32.dll");
76 FARPROC pGetProcessWindowStation = GetProcAddress(huser32, "GetProcessWindowStation");
77 FARPROC pGetUserObjectInformationA = GetProcAddress(huser32, "GetUserObjectInformationA");
79 desktop = GetDesktopWindow();
80 if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
81 return IsWindowVisible(desktop);
83 if (pGetProcessWindowStation && pGetUserObjectInformationA)
85 DWORD len;
86 HWINSTA wstation;
87 USEROBJECTFLAGS uoflags;
89 wstation = (HWINSTA)pGetProcessWindowStation();
90 assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
91 return (uoflags.dwFlags & WSF_VISIBLE) != 0;
93 return IsWindowVisible(desktop);
96 static void print_version (void)
98 OSVERSIONINFOEX ver;
99 BOOL ext;
100 int is_win2k3_r2;
102 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
103 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
105 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
106 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
107 report (R_FATAL, "Can't get OS version.");
110 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
111 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
112 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
113 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
114 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
115 ver.dwPlatformId, ver.szCSDVersion);
117 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
118 if(is_win2k3_r2)
119 xprintf(" R2 build number=%d\n", is_win2k3_r2);
121 if (!ext) return;
123 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
124 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
125 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
126 ver.wProductType, ver.wReserved);
129 static inline int is_dot_dir(const char* x)
131 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
134 static void remove_dir (const char *dir)
136 HANDLE hFind;
137 WIN32_FIND_DATA wfd;
138 char path[MAX_PATH];
139 size_t dirlen = strlen (dir);
141 /* Make sure the directory exists before going further */
142 memcpy (path, dir, dirlen);
143 strcpy (path + dirlen++, "\\*");
144 hFind = FindFirstFile (path, &wfd);
145 if (hFind == INVALID_HANDLE_VALUE) return;
147 do {
148 char *lp = wfd.cFileName;
150 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
151 if (is_dot_dir (lp)) continue;
152 strcpy (path + dirlen, lp);
153 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
154 remove_dir(path);
155 else if (!DeleteFile (path))
156 report (R_WARNING, "Can't delete file %s: error %d",
157 path, GetLastError ());
158 } while (FindNextFile (hFind, &wfd));
159 FindClose (hFind);
160 if (!RemoveDirectory (dir))
161 report (R_WARNING, "Can't remove directory %s: error %d",
162 dir, GetLastError ());
165 static const char* get_test_source_file(const char* test, const char* subtest)
167 static const char* special_dirs[][2] = {
168 { 0, 0 }
170 static char buffer[MAX_PATH];
171 int i;
173 for (i = 0; special_dirs[i][0]; i++) {
174 if (strcmp(test, special_dirs[i][0]) == 0) {
175 test = special_dirs[i][1];
176 break;
180 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
181 return buffer;
184 static const char* get_file_rev(const char* file)
186 const struct rev_info* rev;
188 for(rev = rev_infos; rev->file; rev++) {
189 if (strcmp(rev->file, file) == 0) return rev->rev;
192 return "-";
195 static void extract_rev_infos (void)
197 char revinfo[256], *p;
198 int size = 0, i;
199 unsigned int len;
200 HMODULE module = GetModuleHandle (NULL);
202 for (i = 0; TRUE; i++) {
203 if (i >= size) {
204 size += 100;
205 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
207 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
209 len = LoadStringA (module, REV_INFO+i, revinfo, sizeof(revinfo));
210 if (len == 0) break; /* end of revision info */
211 if (len >= sizeof(revinfo) - 1)
212 report (R_FATAL, "Revision info too long.");
213 if(!(p = strrchr(revinfo, ':')))
214 report (R_FATAL, "Revision info malformed (i=%d)", i);
215 *p = 0;
216 rev_infos[i].file = strdup(revinfo);
217 rev_infos[i].rev = strdup(p + 1);
221 static void* extract_rcdata (LPTSTR name, int type, DWORD* size)
223 HRSRC rsrc;
224 HGLOBAL hdl;
225 LPVOID addr;
227 if (!(rsrc = FindResource (NULL, name, MAKEINTRESOURCE(type))) ||
228 !(*size = SizeofResource (0, rsrc)) ||
229 !(hdl = LoadResource (0, rsrc)) ||
230 !(addr = LockResource (hdl)))
231 return NULL;
232 return addr;
235 /* Fills in the name and exename fields */
236 static void
237 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
239 BYTE* code;
240 DWORD size;
241 FILE* fout;
242 char *exepos;
244 code = extract_rcdata (res_name, TESTRES, &size);
245 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
246 res_name, GetLastError ());
247 test->name = xstrdup( res_name );
248 CharLowerA( test->name );
249 test->exename = strmake (NULL, "%s/%s", dir, test->name);
250 exepos = strstr (test->name, "_test.exe");
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 get_subtests( tempdir, &wine_tests[nr_of_files], lpszName );
437 nr_of_tests += wine_tests[nr_of_files].subtest_count;
438 nr_of_files++;
439 return TRUE;
442 static char *
443 run_tests (char *logname)
445 int i;
446 char *tempdir, *shorttempdir;
447 int logfile;
448 char *strres, *eol, *nextline;
449 DWORD strsize;
451 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
453 if (!logname) {
454 logname = tempnam (0, "res");
455 if (!logname) report (R_FATAL, "Can't name logfile.");
457 report (R_OUT, logname);
459 logfile = open (logname, O_WRONLY | O_CREAT | O_EXCL | O_APPEND,
460 0666);
461 if (-1 == logfile) {
462 if (EEXIST == errno)
463 report (R_FATAL, "File %s already exists.", logname);
464 else report (R_FATAL, "Could not open logfile: %d", errno);
466 if (-1 == dup2 (logfile, 1))
467 report (R_FATAL, "Can't redirect stdout: %d", errno);
468 close (logfile);
470 tempdir = tempnam (0, "wct");
471 if (!tempdir)
472 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
473 shorttempdir = strdup (tempdir);
474 if (shorttempdir) { /* try stable path for ZoneAlarm */
475 strstr (shorttempdir, "wct")[3] = 0;
476 if (CreateDirectoryA (shorttempdir, NULL)) {
477 free (tempdir);
478 tempdir = shorttempdir;
479 } else free (shorttempdir);
481 if (tempdir != shorttempdir && !CreateDirectoryA (tempdir, NULL))
482 report (R_FATAL, "Could not create directory: %s", tempdir);
483 report (R_DIR, tempdir);
485 xprintf ("Version 3\n");
486 strres = extract_rcdata (MAKEINTRESOURCE(WINE_BUILD), STRINGRES, &strsize);
487 xprintf ("Tests from build ");
488 if (strres) xprintf ("%.*s", strsize, strres);
489 else xprintf ("-\n");
490 strres = extract_rcdata (MAKEINTRESOURCE(TESTS_URL), STRINGRES, &strsize);
491 xprintf ("Archive: ");
492 if (strres) xprintf ("%.*s", strsize, strres);
493 else xprintf ("-\n");
494 xprintf ("Tag: %s\n", tag);
495 xprintf ("Build info:\n");
496 strres = extract_rcdata (MAKEINTRESOURCE(BUILD_INFO), STRINGRES, &strsize);
497 while (strres) {
498 eol = memchr (strres, '\n', strsize);
499 if (!eol) {
500 nextline = NULL;
501 eol = strres + strsize;
502 } else {
503 strsize -= eol - strres + 1;
504 nextline = strsize?eol+1:NULL;
505 if (eol > strres && *(eol-1) == '\r') eol--;
507 xprintf (" %.*s\n", eol-strres, strres);
508 strres = nextline;
510 xprintf ("Operating system version:\n");
511 print_version ();
512 xprintf ("Test output:\n" );
514 report (R_STATUS, "Counting tests");
515 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
516 EnumTestFileProc, (LPARAM)&nr_of_files))
517 report (R_FATAL, "Can't enumerate test files: %d",
518 GetLastError ());
519 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
521 report (R_STATUS, "Extracting tests");
522 report (R_PROGRESS, 0, nr_of_files);
523 nr_of_files = 0;
524 nr_of_tests = 0;
525 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
526 extract_test_proc, (LPARAM)tempdir))
527 report (R_FATAL, "Can't enumerate test files: %d",
528 GetLastError ());
530 report (R_DELTA, 0, "Extracting: Done");
532 report (R_STATUS, "Running tests");
533 report (R_PROGRESS, 1, nr_of_tests);
534 for (i = 0; i < nr_of_files; i++) {
535 struct wine_test *test = wine_tests + i;
536 int j;
538 for (j = 0; j < test->subtest_count; j++) {
539 report (R_STEP, "Running: %s:%s", test->name,
540 test->subtests[j]);
541 run_test (test, test->subtests[j], tempdir);
544 report (R_DELTA, 0, "Running: Done");
546 report (R_STATUS, "Cleaning up");
547 close (1);
548 remove_dir (tempdir);
549 free (tempdir);
550 free (wine_tests);
552 return logname;
555 static void
556 usage (void)
558 fprintf (stderr,
559 "Usage: winetest [OPTION]...\n\n"
560 " -c console mode, no GUI\n"
561 " -e preserve the environment\n"
562 " -h print this message and exit\n"
563 " -q quiet mode, no output at all\n"
564 " -o FILE put report into FILE, do not submit\n"
565 " -s FILE submit FILE, do not run tests\n"
566 " -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n");
569 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
570 LPSTR cmdLine, int cmdShow)
572 char *logname = NULL;
573 const char *cp, *submit = NULL;
574 int reset_env = 1;
575 int interactive = 1;
577 /* initialize the revision information first */
578 extract_rev_infos();
580 cmdLine = strtok (cmdLine, whitespace);
581 while (cmdLine) {
582 if (cmdLine[0] != '-' || cmdLine[2]) {
583 report (R_ERROR, "Not a single letter option: %s", cmdLine);
584 usage ();
585 exit (2);
587 switch (cmdLine[1]) {
588 case 'c':
589 report (R_TEXTMODE);
590 interactive = 0;
591 break;
592 case 'e':
593 reset_env = 0;
594 break;
595 case 'h':
596 usage ();
597 exit (0);
598 case 'q':
599 report (R_QUIET);
600 interactive = 0;
601 break;
602 case 's':
603 submit = strtok (NULL, whitespace);
604 if (tag)
605 report (R_WARNING, "ignoring tag for submission");
606 send_file (submit);
607 break;
608 case 'o':
609 logname = strtok (NULL, whitespace);
610 break;
611 case 't':
612 tag = strtok (NULL, whitespace);
613 if (strlen (tag) > MAXTAGLEN)
614 report (R_FATAL, "tag is too long (maximum %d characters)",
615 MAXTAGLEN);
616 cp = findbadtagchar (tag);
617 if (cp) {
618 report (R_ERROR, "invalid char in tag: %c", *cp);
619 usage ();
620 exit (2);
622 break;
623 default:
624 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
625 usage ();
626 exit (2);
628 cmdLine = strtok (NULL, whitespace);
630 if (!submit) {
631 static CHAR platform_windows[] = "WINETEST_PLATFORM=windows",
632 debug_yes[] = "WINETEST_DEBUG=1",
633 interactive_no[] = "WINETEST_INTERACTIVE=0",
634 report_success_no[] = "WINETEST_REPORT_SUCCESS=0";
636 report (R_STATUS, "Starting up");
638 if (!running_on_visible_desktop ())
639 report (R_FATAL, "Tests must be run on a visible desktop");
641 if (reset_env && (putenv (platform_windows) ||
642 putenv (debug_yes) ||
643 putenv (interactive_no) ||
644 putenv (report_success_no)))
645 report (R_FATAL, "Could not reset environment: %d", errno);
647 if (!tag) {
648 if (!interactive)
649 report (R_FATAL, "Please specify a tag (-t option) if "
650 "running noninteractive!");
651 if (guiAskTag () == IDABORT) exit (1);
653 report (R_TAG);
655 if (!logname) {
656 logname = run_tests (NULL);
657 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
658 "test results?") == IDYES)
659 if (!send_file (logname) && remove (logname))
660 report (R_WARNING, "Can't remove logfile: %d.", errno);
661 free (logname);
662 } else run_tests (logname);
663 report (R_STATUS, "Finished");
665 exit (0);