push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / programs / winetest / main.c
blobbb68ed107857f02281509b0bc0e0dceebb06467d
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 #define COBJMACROS
32 #include <stdio.h>
33 #include <assert.h>
34 #include <windows.h>
35 #include <mshtml.h>
37 #include "winetest.h"
38 #include "resource.h"
40 struct wine_test
42 char *name;
43 int resource;
44 int subtest_count;
45 char **subtests;
46 char *exename;
47 char *maindllpath;
50 char *tag = NULL;
51 static struct wine_test *wine_tests;
52 static int nr_of_files, nr_of_tests;
53 static int nr_native_dlls;
54 static const char whitespace[] = " \t\r\n";
55 static const char testexe[] = "_test.exe";
56 static char build_id[64];
58 /* filters for running only specific tests */
59 static char *filters[64];
60 static unsigned int nb_filters = 0;
62 /* Needed to check for .NET dlls */
63 static HMODULE hmscoree;
64 static HRESULT (WINAPI *pLoadLibraryShim)(LPCWSTR, LPCWSTR, LPVOID, HMODULE *);
66 /* To store the current PATH setting (related to .NET only provided dlls) */
67 static char *curpath;
69 /* check if test is being filtered out */
70 static BOOL test_filtered_out( LPCSTR module, LPCSTR testname )
72 char *p, dllname[MAX_PATH];
73 unsigned int i, len;
75 strcpy( dllname, module );
76 CharLowerA( dllname );
77 p = strstr( dllname, testexe );
78 if (p) *p = 0;
79 len = strlen(dllname);
81 if (!nb_filters) return FALSE;
82 for (i = 0; i < nb_filters; i++)
84 if (!strncmp( dllname, filters[i], len ))
86 if (!filters[i][len]) return FALSE;
87 if (filters[i][len] != ':') continue;
88 if (!testname || !strcmp( testname, &filters[i][len+1] )) return FALSE;
91 return TRUE;
94 static char * get_file_version(char * file_name)
96 static char version[32];
97 DWORD size;
98 DWORD handle;
100 size = GetFileVersionInfoSizeA(file_name, &handle);
101 if (size) {
102 char * data = heap_alloc(size);
103 if (data) {
104 if (GetFileVersionInfoA(file_name, handle, size, data)) {
105 static char backslash[] = "\\";
106 VS_FIXEDFILEINFO *pFixedVersionInfo;
107 UINT len;
108 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
109 sprintf(version, "%d.%d.%d.%d",
110 pFixedVersionInfo->dwFileVersionMS >> 16,
111 pFixedVersionInfo->dwFileVersionMS & 0xffff,
112 pFixedVersionInfo->dwFileVersionLS >> 16,
113 pFixedVersionInfo->dwFileVersionLS & 0xffff);
114 } else
115 sprintf(version, "version not available");
116 } else
117 sprintf(version, "unknown");
118 heap_free(data);
119 } else
120 sprintf(version, "failed");
121 } else
122 sprintf(version, "version not available");
124 return version;
127 static int running_under_wine (void)
129 HMODULE module = GetModuleHandleA("ntdll.dll");
131 if (!module) return 0;
132 return (GetProcAddress(module, "wine_server_call") != NULL);
135 static int running_on_visible_desktop (void)
137 HWND desktop;
138 HMODULE huser32 = GetModuleHandle("user32.dll");
139 HWINSTA (WINAPI *pGetProcessWindowStation)(void);
140 BOOL (WINAPI *pGetUserObjectInformationA)(HANDLE,INT,LPVOID,DWORD,LPDWORD);
142 pGetProcessWindowStation = (void *)GetProcAddress(huser32, "GetProcessWindowStation");
143 pGetUserObjectInformationA = (void *)GetProcAddress(huser32, "GetUserObjectInformationA");
145 desktop = GetDesktopWindow();
146 if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
147 return IsWindowVisible(desktop);
149 if (pGetProcessWindowStation && pGetUserObjectInformationA)
151 DWORD len;
152 HWINSTA wstation;
153 USEROBJECTFLAGS uoflags;
155 wstation = (HWINSTA)pGetProcessWindowStation();
156 assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
157 return (uoflags.dwFlags & WSF_VISIBLE) != 0;
159 return IsWindowVisible(desktop);
162 /* check for native dll when running under wine */
163 static BOOL is_native_dll( HMODULE module )
165 static const char fakedll_signature[] = "Wine placeholder DLL";
166 const IMAGE_DOS_HEADER *dos;
168 if (!running_under_wine()) return FALSE;
169 if (!((ULONG_PTR)module & 1)) return FALSE; /* not loaded as datafile */
170 /* builtin dlls can't be loaded as datafile, so we must have native or fake dll */
171 dos = (const IMAGE_DOS_HEADER *)((const char *)module - 1);
172 if (dos->e_magic != IMAGE_DOS_SIGNATURE) return FALSE;
173 if (dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
174 !memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return FALSE;
175 return TRUE;
178 static void print_version (void)
180 #ifdef __i386__
181 static const char platform[] = "i386";
182 #elif defined(__x86_64__)
183 static const char platform[] = "x86_64";
184 #elif defined(__sparc__)
185 static const char platform[] = "sparc";
186 #elif defined(__ALPHA__)
187 static const char platform[] = "alpha";
188 #elif defined(__powerpc__)
189 static const char platform[] = "powerpc";
190 #endif
191 OSVERSIONINFOEX ver;
192 BOOL ext, wow64;
193 int is_win2k3_r2;
194 const char *(CDECL *wine_get_build_id)(void);
195 void (CDECL *wine_get_host_version)( const char **sysname, const char **release );
196 BOOL (WINAPI *pIsWow64Process)(HANDLE hProcess, PBOOL Wow64Process);
197 BOOL (WINAPI *pGetProductInfo)(DWORD, DWORD, DWORD, DWORD, DWORD *);
199 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
200 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
202 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
203 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
204 report (R_FATAL, "Can't get OS version.");
206 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"IsWow64Process");
207 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &wow64 )) wow64 = FALSE;
209 xprintf (" Platform=%s%s\n", platform, wow64 ? " (WOW64)" : "");
210 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
211 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
212 xprintf (" dwMajorVersion=%u\n dwMinorVersion=%u\n"
213 " dwBuildNumber=%u\n PlatformId=%u\n szCSDVersion=%s\n",
214 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
215 ver.dwPlatformId, ver.szCSDVersion);
217 wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
218 wine_get_host_version = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
219 if (wine_get_build_id) xprintf( " WineBuild=%s\n", wine_get_build_id() );
220 if (wine_get_host_version)
222 const char *sysname, *release;
223 wine_get_host_version( &sysname, &release );
224 xprintf( " Host system=%s\n Host version=%s\n", sysname, release );
226 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
227 if(is_win2k3_r2)
228 xprintf(" R2 build number=%d\n", is_win2k3_r2);
230 if (!ext) return;
232 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
233 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
234 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
235 ver.wProductType, ver.wReserved);
237 pGetProductInfo = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"GetProductInfo");
238 if (pGetProductInfo && !running_under_wine())
240 DWORD prodtype = 0;
242 pGetProductInfo(ver.dwMajorVersion, ver.dwMinorVersion, ver.wServicePackMajor, ver.wServicePackMinor, &prodtype);
243 xprintf(" dwProductInfo=%u\n", prodtype);
247 static inline int is_dot_dir(const char* x)
249 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
252 static void remove_dir (const char *dir)
254 HANDLE hFind;
255 WIN32_FIND_DATA wfd;
256 char path[MAX_PATH];
257 size_t dirlen = strlen (dir);
259 /* Make sure the directory exists before going further */
260 memcpy (path, dir, dirlen);
261 strcpy (path + dirlen++, "\\*");
262 hFind = FindFirstFile (path, &wfd);
263 if (hFind == INVALID_HANDLE_VALUE) return;
265 do {
266 char *lp = wfd.cFileName;
268 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
269 if (is_dot_dir (lp)) continue;
270 strcpy (path + dirlen, lp);
271 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
272 remove_dir(path);
273 else if (!DeleteFile (path))
274 report (R_WARNING, "Can't delete file %s: error %d",
275 path, GetLastError ());
276 } while (FindNextFile (hFind, &wfd));
277 FindClose (hFind);
278 if (!RemoveDirectory (dir))
279 report (R_WARNING, "Can't remove directory %s: error %d",
280 dir, GetLastError ());
283 static const char* get_test_source_file(const char* test, const char* subtest)
285 static const char* special_dirs[][2] = {
286 { 0, 0 }
288 static char buffer[MAX_PATH];
289 int i;
291 for (i = 0; special_dirs[i][0]; i++) {
292 if (strcmp(test, special_dirs[i][0]) == 0) {
293 test = special_dirs[i][1];
294 break;
298 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
299 return buffer;
302 static void* extract_rcdata (LPCTSTR name, LPCTSTR type, DWORD* size)
304 HRSRC rsrc;
305 HGLOBAL hdl;
306 LPVOID addr;
308 if (!(rsrc = FindResource (NULL, name, type)) ||
309 !(*size = SizeofResource (0, rsrc)) ||
310 !(hdl = LoadResource (0, rsrc)) ||
311 !(addr = LockResource (hdl)))
312 return NULL;
313 return addr;
316 /* Fills in the name and exename fields */
317 static void
318 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
320 BYTE* code;
321 DWORD size;
322 char *exepos;
323 HANDLE hfile;
324 DWORD written;
326 code = extract_rcdata (res_name, "TESTRES", &size);
327 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
328 res_name, GetLastError ());
329 test->name = heap_strdup( res_name );
330 test->exename = strmake (NULL, "%s\\%s", dir, test->name);
331 exepos = strstr (test->name, testexe);
332 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
333 *exepos = 0;
334 test->name = heap_realloc (test->name, exepos - test->name + 1);
335 report (R_STEP, "Extracting: %s", test->name);
337 hfile = CreateFileA(test->exename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
338 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
339 if (hfile == INVALID_HANDLE_VALUE)
340 report (R_FATAL, "Failed to open file %s.", test->exename);
342 if (!WriteFile(hfile, code, size, &written, NULL))
343 report (R_FATAL, "Failed to write file %s.", test->exename);
345 CloseHandle(hfile);
348 static DWORD wait_process( HANDLE process, DWORD timeout )
350 DWORD wait, diff = 0, start = GetTickCount();
351 MSG msg;
353 while (diff < timeout)
355 wait = MsgWaitForMultipleObjects( 1, &process, FALSE, timeout - diff, QS_ALLINPUT );
356 if (wait != WAIT_OBJECT_0 + 1) return wait;
357 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
358 diff = GetTickCount() - start;
360 return WAIT_TIMEOUT;
363 static void append_path( const char *path)
365 char *newpath;
367 newpath = heap_alloc(strlen(curpath) + 1 + strlen(path) + 1);
368 strcpy(newpath, curpath);
369 strcat(newpath, ";");
370 strcat(newpath, path);
371 SetEnvironmentVariableA("PATH", newpath);
373 heap_free(newpath);
376 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
377 stdout to there.
379 Return the exit status, -2 if can't create process or the return
380 value of WaitForSingleObject.
382 static int
383 run_ex (char *cmd, HANDLE out_file, const char *tempdir, DWORD ms)
385 STARTUPINFO si;
386 PROCESS_INFORMATION pi;
387 DWORD wait, status;
389 GetStartupInfo (&si);
390 si.dwFlags = STARTF_USESTDHANDLES;
391 si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
392 si.hStdOutput = out_file ? out_file : GetStdHandle( STD_OUTPUT_HANDLE );
393 si.hStdError = out_file ? out_file : GetStdHandle( STD_ERROR_HANDLE );
395 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE,
396 NULL, tempdir, &si, &pi))
397 return -2;
399 CloseHandle (pi.hThread);
400 status = wait_process( pi.hProcess, ms );
401 switch (status)
403 case WAIT_OBJECT_0:
404 GetExitCodeProcess (pi.hProcess, &status);
405 CloseHandle (pi.hProcess);
406 return status;
407 case WAIT_FAILED:
408 report (R_ERROR, "Wait for '%s' failed: %d", cmd, GetLastError ());
409 break;
410 case WAIT_TIMEOUT:
411 break;
412 default:
413 report (R_ERROR, "Wait returned %d", status);
414 break;
416 if (!TerminateProcess (pi.hProcess, 257))
417 report (R_ERROR, "TerminateProcess failed: %d", GetLastError ());
418 wait = wait_process( pi.hProcess, 5000 );
419 switch (wait)
421 case WAIT_OBJECT_0:
422 break;
423 case WAIT_FAILED:
424 report (R_ERROR, "Wait for termination of '%s' failed: %d", cmd, GetLastError ());
425 break;
426 case WAIT_TIMEOUT:
427 report (R_ERROR, "Can't kill process '%s'", cmd);
428 break;
429 default:
430 report (R_ERROR, "Waiting for termination: %d", wait);
431 break;
433 CloseHandle (pi.hProcess);
434 return status;
437 static DWORD
438 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
440 char *cmd;
441 HANDLE subfile;
442 DWORD err, total;
443 char buffer[8192], *index;
444 static const char header[] = "Valid test names:";
445 int status, allocated;
446 char tmpdir[MAX_PATH], subname[MAX_PATH];
447 SECURITY_ATTRIBUTES sa;
449 test->subtest_count = 0;
451 if (!GetTempPathA( MAX_PATH, tmpdir ) ||
452 !GetTempFileNameA( tmpdir, "sub", 0, subname ))
453 report (R_FATAL, "Can't name subtests file.");
455 /* make handle inheritable */
456 sa.nLength = sizeof(sa);
457 sa.lpSecurityDescriptor = NULL;
458 sa.bInheritHandle = TRUE;
460 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
461 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
462 &sa, CREATE_ALWAYS, 0, NULL );
464 if ((subfile == INVALID_HANDLE_VALUE) &&
465 (GetLastError() == ERROR_INVALID_PARAMETER)) {
466 /* FILE_SHARE_DELETE not supported on win9x */
467 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
468 FILE_SHARE_READ | FILE_SHARE_WRITE,
469 &sa, CREATE_ALWAYS, 0, NULL );
471 if (subfile == INVALID_HANDLE_VALUE) {
472 err = GetLastError();
473 report (R_ERROR, "Can't open subtests output of %s: %u",
474 test->name, GetLastError());
475 goto quit;
478 extract_test (test, tempdir, res_name);
479 cmd = strmake (NULL, "%s --list", test->exename);
480 if (test->maindllpath) {
481 /* We need to add the path (to the main dll) to PATH */
482 append_path(test->maindllpath);
484 status = run_ex (cmd, subfile, tempdir, 5000);
485 err = GetLastError();
486 if (test->maindllpath) {
487 /* Restore PATH again */
488 SetEnvironmentVariableA("PATH", curpath);
490 heap_free (cmd);
492 if (status == -2)
494 report (R_ERROR, "Cannot run %s error %u", test->exename, err);
495 goto quit;
498 SetFilePointer( subfile, 0, NULL, FILE_BEGIN );
499 ReadFile( subfile, buffer, sizeof(buffer), &total, NULL );
500 CloseHandle( subfile );
501 if (sizeof buffer == total) {
502 report (R_ERROR, "Subtest list of %s too big.",
503 test->name, sizeof buffer);
504 err = ERROR_OUTOFMEMORY;
505 goto quit;
507 buffer[total] = 0;
509 index = strstr (buffer, header);
510 if (!index) {
511 report (R_ERROR, "Can't parse subtests output of %s",
512 test->name);
513 err = ERROR_INTERNAL_ERROR;
514 goto quit;
516 index += sizeof header;
518 allocated = 10;
519 test->subtests = heap_alloc (allocated * sizeof(char*));
520 index = strtok (index, whitespace);
521 while (index) {
522 if (test->subtest_count == allocated) {
523 allocated *= 2;
524 test->subtests = heap_realloc (test->subtests,
525 allocated * sizeof(char*));
527 if (!test_filtered_out( test->name, index ))
528 test->subtests[test->subtest_count++] = heap_strdup(index);
529 index = strtok (NULL, whitespace);
531 test->subtests = heap_realloc (test->subtests,
532 test->subtest_count * sizeof(char*));
533 err = 0;
535 quit:
536 if (!DeleteFileA (subname))
537 report (R_WARNING, "Can't delete file '%s': %u", subname, GetLastError());
538 return err;
541 static void
542 run_test (struct wine_test* test, const char* subtest, HANDLE out_file, const char *tempdir)
544 int status;
545 const char* file = get_test_source_file(test->name, subtest);
546 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
548 xprintf ("%s:%s start %s -\n", test->name, subtest, file);
549 status = run_ex (cmd, out_file, tempdir, 120000);
550 heap_free (cmd);
551 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
554 static BOOL CALLBACK
555 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
556 LPTSTR lpszName, LONG_PTR lParam)
558 if (!test_filtered_out( lpszName, NULL )) (*(int*)lParam)++;
559 return TRUE;
562 static const struct clsid_mapping
564 const char *name;
565 CLSID clsid;
566 } clsid_list[] =
568 {"oledb32", {0xc8b522d1, 0x5cf3, 0x11ce, {0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d}}},
569 {NULL, {0, 0, 0, {0,0,0,0,0,0,0,0}}}
573 static BOOL get_main_clsid(const char *name, CLSID *clsid)
575 const struct clsid_mapping *mapping;
577 for(mapping = clsid_list; mapping->name; mapping++)
579 if(!strcasecmp(name, mapping->name))
581 *clsid = mapping->clsid;
582 return TRUE;
585 return FALSE;
588 static HMODULE load_com_dll(const char *name, char **path, char *filename)
590 HMODULE dll = NULL;
591 HKEY hkey;
592 char keyname[100];
593 char dllname[MAX_PATH];
594 char *p;
595 CLSID clsid;
597 if(!get_main_clsid(name, &clsid)) return NULL;
599 sprintf(keyname, "CLSID\\{%08x-%04x-%04x-%02x%2x-%02x%2x%02x%2x%02x%2x}\\InprocServer32",
600 clsid.Data1, clsid.Data2, clsid.Data3, clsid.Data4[0], clsid.Data4[1],
601 clsid.Data4[2], clsid.Data4[3], clsid.Data4[4], clsid.Data4[5],
602 clsid.Data4[6], clsid.Data4[7]);
604 if(RegOpenKeyA(HKEY_CLASSES_ROOT, keyname, &hkey) == ERROR_SUCCESS)
606 LONG size = sizeof(dllname);
607 if(RegQueryValueA(hkey, NULL, dllname, &size) == ERROR_SUCCESS)
609 if ((dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE)))
611 strcpy( filename, dllname );
612 p = strrchr(dllname, '\\');
613 if (p) *p = 0;
614 *path = heap_strdup( dllname );
617 RegCloseKey(hkey);
620 return dll;
623 static void get_dll_path(HMODULE dll, char **path, char *filename)
625 char dllpath[MAX_PATH];
627 GetModuleFileNameA(dll, dllpath, MAX_PATH);
628 strcpy(filename, dllpath);
629 *strrchr(dllpath, '\\') = '\0';
630 *path = heap_strdup( dllpath );
633 static BOOL CALLBACK
634 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
635 LPTSTR lpszName, LONG_PTR lParam)
637 const char *tempdir = (const char *)lParam;
638 char dllname[MAX_PATH];
639 char filename[MAX_PATH];
640 WCHAR dllnameW[MAX_PATH];
641 HMODULE dll;
642 DWORD err;
644 if (test_filtered_out( lpszName, NULL )) return TRUE;
646 /* Check if the main dll is present on this system */
647 CharLowerA(lpszName);
648 strcpy(dllname, lpszName);
649 *strstr(dllname, testexe) = 0;
651 wine_tests[nr_of_files].maindllpath = NULL;
652 strcpy(filename, dllname);
653 dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE);
655 if (!dll) dll = load_com_dll(dllname, &wine_tests[nr_of_files].maindllpath, filename);
657 if (!dll && pLoadLibraryShim)
659 MultiByteToWideChar(CP_ACP, 0, dllname, -1, dllnameW, MAX_PATH);
660 if (SUCCEEDED( pLoadLibraryShim(dllnameW, NULL, NULL, &dll) ) && dll)
662 get_dll_path(dll, &wine_tests[nr_of_files].maindllpath, filename);
663 FreeLibrary(dll);
664 dll = LoadLibraryExA(filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
666 else dll = 0;
669 if (!dll)
671 xprintf (" %s=dll is missing\n", dllname);
672 return TRUE;
674 if (is_native_dll(dll))
676 FreeLibrary(dll);
677 xprintf (" %s=load error Configured as native\n", dllname);
678 nr_native_dlls++;
679 return TRUE;
681 FreeLibrary(dll);
683 if (!(err = get_subtests( tempdir, &wine_tests[nr_of_files], lpszName )))
685 xprintf (" %s=%s\n", dllname, get_file_version(filename));
686 nr_of_tests += wine_tests[nr_of_files].subtest_count;
687 nr_of_files++;
689 else
691 xprintf (" %s=load error %u\n", dllname, err);
693 return TRUE;
696 static char *
697 run_tests (char *logname, char *outdir)
699 int i;
700 char *strres, *eol, *nextline;
701 DWORD strsize;
702 SECURITY_ATTRIBUTES sa;
703 char tmppath[MAX_PATH], tempdir[MAX_PATH+4];
704 DWORD needed;
706 /* Get the current PATH only once */
707 needed = GetEnvironmentVariableA("PATH", NULL, 0);
708 curpath = heap_alloc(needed);
709 GetEnvironmentVariableA("PATH", curpath, needed);
711 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
713 if (!GetTempPathA( MAX_PATH, tmppath ))
714 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
716 if (!logname) {
717 static char tmpname[MAX_PATH];
718 if (!GetTempFileNameA( tmppath, "res", 0, tmpname ))
719 report (R_FATAL, "Can't name logfile.");
720 logname = tmpname;
722 report (R_OUT, logname);
724 /* make handle inheritable */
725 sa.nLength = sizeof(sa);
726 sa.lpSecurityDescriptor = NULL;
727 sa.bInheritHandle = TRUE;
729 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
730 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
731 &sa, CREATE_ALWAYS, 0, NULL );
733 if ((logfile == INVALID_HANDLE_VALUE) &&
734 (GetLastError() == ERROR_INVALID_PARAMETER)) {
735 /* FILE_SHARE_DELETE not supported on win9x */
736 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
737 FILE_SHARE_READ | FILE_SHARE_WRITE,
738 &sa, CREATE_ALWAYS, 0, NULL );
740 if (logfile == INVALID_HANDLE_VALUE)
741 report (R_FATAL, "Could not open logfile: %u", GetLastError());
743 /* try stable path for ZoneAlarm */
744 if (!outdir) {
745 strcpy( tempdir, tmppath );
746 strcat( tempdir, "wct" );
748 if (!CreateDirectoryA( tempdir, NULL ))
750 if (!GetTempFileNameA( tmppath, "wct", 0, tempdir ))
751 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
752 DeleteFileA( tempdir );
753 if (!CreateDirectoryA( tempdir, NULL ))
754 report (R_FATAL, "Could not create directory: %s", tempdir);
757 else
758 strcpy( tempdir, outdir);
760 report (R_DIR, tempdir);
762 xprintf ("Version 4\n");
763 xprintf ("Tests from build %s\n", build_id[0] ? build_id : "-" );
764 xprintf ("Archive: -\n"); /* no longer used */
765 xprintf ("Tag: %s\n", tag);
766 xprintf ("Build info:\n");
767 strres = extract_rcdata ("BUILD_INFO", "STRINGRES", &strsize);
768 while (strres) {
769 eol = memchr (strres, '\n', strsize);
770 if (!eol) {
771 nextline = NULL;
772 eol = strres + strsize;
773 } else {
774 strsize -= eol - strres + 1;
775 nextline = strsize?eol+1:NULL;
776 if (eol > strres && *(eol-1) == '\r') eol--;
778 xprintf (" %.*s\n", eol-strres, strres);
779 strres = nextline;
781 xprintf ("Operating system version:\n");
782 print_version ();
783 xprintf ("Dll info:\n" );
785 report (R_STATUS, "Counting tests");
786 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
787 report (R_FATAL, "Can't enumerate test files: %d",
788 GetLastError ());
789 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0]);
791 /* Do this only once during extraction (and version checking) */
792 hmscoree = LoadLibraryA("mscoree.dll");
793 pLoadLibraryShim = NULL;
794 if (hmscoree)
795 pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
797 report (R_STATUS, "Extracting tests");
798 report (R_PROGRESS, 0, nr_of_files);
799 nr_of_files = 0;
800 nr_of_tests = 0;
801 if (!EnumResourceNames (NULL, "TESTRES", extract_test_proc, (LPARAM)tempdir))
802 report (R_FATAL, "Can't enumerate test files: %d",
803 GetLastError ());
805 FreeLibrary(hmscoree);
807 xprintf ("Test output:\n" );
809 report (R_DELTA, 0, "Extracting: Done");
811 if (nr_native_dlls)
812 report( R_WARNING, "Some dlls are configured as native, you won't be able to submit results." );
814 report (R_STATUS, "Running tests");
815 report (R_PROGRESS, 1, nr_of_tests);
816 for (i = 0; i < nr_of_files; i++) {
817 struct wine_test *test = wine_tests + i;
818 int j;
820 if (test->maindllpath) {
821 /* We need to add the path (to the main dll) to PATH */
822 append_path(test->maindllpath);
825 for (j = 0; j < test->subtest_count; j++) {
826 report (R_STEP, "Running: %s:%s", test->name,
827 test->subtests[j]);
828 run_test (test, test->subtests[j], logfile, tempdir);
831 if (test->maindllpath) {
832 /* Restore PATH again */
833 SetEnvironmentVariableA("PATH", curpath);
836 report (R_DELTA, 0, "Running: Done");
838 report (R_STATUS, "Cleaning up");
839 CloseHandle( logfile );
840 logfile = 0;
841 if (!outdir)
842 remove_dir (tempdir);
843 heap_free(wine_tests);
844 heap_free(curpath);
846 return logname;
849 static BOOL WINAPI ctrl_handler(DWORD ctrl_type)
851 if (ctrl_type == CTRL_C_EVENT) {
852 printf("Ignoring Ctrl-C, use Ctrl-Break if you really want to terminate\n");
853 return TRUE;
856 return FALSE;
860 static BOOL CALLBACK
861 extract_only_proc (HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
863 const char *target_dir = (const char *)lParam;
864 char filename[MAX_PATH];
866 if (test_filtered_out( lpszName, NULL )) return TRUE;
868 strcpy(filename, lpszName);
869 CharLowerA(filename);
871 extract_test( &wine_tests[nr_of_files], target_dir, filename );
872 nr_of_files++;
873 return TRUE;
876 static void extract_only (const char *target_dir)
878 BOOL res;
880 report (R_DIR, target_dir);
881 res = CreateDirectoryA( target_dir, NULL );
882 if (!res && GetLastError() != ERROR_ALREADY_EXISTS)
883 report (R_FATAL, "Could not create directory: %s (%d)", target_dir, GetLastError ());
885 nr_of_files = 0;
886 report (R_STATUS, "Counting tests");
887 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
888 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
890 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0] );
892 report (R_STATUS, "Extracting tests");
893 report (R_PROGRESS, 0, nr_of_files);
894 nr_of_files = 0;
895 if (!EnumResourceNames (NULL, "TESTRES", extract_only_proc, (LPARAM)target_dir))
896 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
898 report (R_DELTA, 0, "Extracting: Done");
901 static void
902 usage (void)
904 fprintf (stderr,
905 "Usage: winetest [OPTION]... [TESTS]\n\n"
906 " --help print this message and exit\n"
907 " --version print the build version and exit\n"
908 " -c console mode, no GUI\n"
909 " -d DIR Use DIR as temp directory (default: %%TEMP%%\\wct)\n"
910 " -e preserve the environment\n"
911 " -h print this message and exit\n"
912 " -p shutdown when the tests are done\n"
913 " -q quiet mode, no output at all\n"
914 " -o FILE put report into FILE, do not submit\n"
915 " -s FILE submit FILE, do not run tests\n"
916 " -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n"
917 " -x DIR Extract tests to DIR (default: .\\wct) and exit\n");
920 int main( int argc, char *argv[] )
922 char *logname = NULL, *outdir = NULL;
923 const char *extract = NULL;
924 const char *cp, *submit = NULL;
925 int reset_env = 1;
926 int poweroff = 0;
927 int interactive = 1;
928 int i;
930 if (!LoadStringA( 0, IDS_BUILD_ID, build_id, sizeof(build_id) )) build_id[0] = 0;
932 for (i = 1; i < argc && argv[i]; i++)
934 if (!strcmp(argv[i], "--help")) {
935 usage ();
936 exit (0);
938 else if (!strcmp(argv[i], "--version")) {
939 printf("%-12.12s\n", build_id[0] ? build_id : "unknown");
940 exit (0);
942 else if ((argv[i][0] != '-' && argv[i][0] != '/') || argv[i][2]) {
943 if (nb_filters == sizeof(filters)/sizeof(filters[0]))
945 report (R_ERROR, "Too many test filters specified");
946 exit (2);
948 filters[nb_filters++] = argv[i];
950 else switch (argv[i][1]) {
951 case 'c':
952 report (R_TEXTMODE);
953 interactive = 0;
954 break;
955 case 'e':
956 reset_env = 0;
957 break;
958 case 'h':
959 case '?':
960 usage ();
961 exit (0);
962 case 'p':
963 poweroff = 1;
964 break;
965 case 'q':
966 report (R_QUIET);
967 interactive = 0;
968 break;
969 case 's':
970 if (!(submit = argv[++i]))
972 usage();
973 exit( 2 );
975 if (tag)
976 report (R_WARNING, "ignoring tag for submission");
977 send_file (submit);
978 break;
979 case 'o':
980 if (!(logname = argv[++i]))
982 usage();
983 exit( 2 );
985 break;
986 case 't':
987 if (!(tag = argv[++i]))
989 usage();
990 exit( 2 );
992 if (strlen (tag) > MAXTAGLEN)
993 report (R_FATAL, "tag is too long (maximum %d characters)",
994 MAXTAGLEN);
995 cp = findbadtagchar (tag);
996 if (cp) {
997 report (R_ERROR, "invalid char in tag: %c", *cp);
998 usage ();
999 exit (2);
1001 break;
1002 case 'x':
1003 report (R_TEXTMODE);
1004 if (!(extract = argv[++i]))
1005 extract = ".\\wct";
1007 extract_only (extract);
1008 break;
1009 case 'd':
1010 outdir = argv[++i];
1011 break;
1012 default:
1013 report (R_ERROR, "invalid option: -%c", argv[i][1]);
1014 usage ();
1015 exit (2);
1018 if (!submit && !extract) {
1019 report (R_STATUS, "Starting up");
1021 if (!running_on_visible_desktop ())
1022 report (R_FATAL, "Tests must be run on a visible desktop");
1024 SetConsoleCtrlHandler(ctrl_handler, TRUE);
1026 if (reset_env)
1028 SetEnvironmentVariableA( "WINETEST_PLATFORM", running_under_wine () ? "wine" : "windows" );
1029 SetEnvironmentVariableA( "WINETEST_DEBUG", "1" );
1030 SetEnvironmentVariableA( "WINETEST_INTERACTIVE", "0" );
1031 SetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", "0" );
1034 if (!nb_filters) /* don't submit results when filtering */
1036 while (!tag) {
1037 if (!interactive)
1038 report (R_FATAL, "Please specify a tag (-t option) if "
1039 "running noninteractive!");
1040 if (guiAskTag () == IDABORT) exit (1);
1042 report (R_TAG);
1044 if (!build_id[0])
1045 report( R_WARNING, "You won't be able to submit results without a valid build id.\n"
1046 "To submit results, winetest needs to be built from a git checkout." );
1049 if (!logname) {
1050 logname = run_tests (NULL, outdir);
1051 if (build_id[0] && !nb_filters && !nr_native_dlls &&
1052 report (R_ASK, MB_YESNO, "Do you want to submit the test results?") == IDYES)
1053 if (!send_file (logname) && !DeleteFileA(logname))
1054 report (R_WARNING, "Can't remove logfile: %u", GetLastError());
1055 } else run_tests (logname, outdir);
1056 report (R_STATUS, "Finished");
1058 if (poweroff)
1060 HANDLE hToken;
1061 TOKEN_PRIVILEGES npr;
1063 /* enable the shutdown privilege for the current process */
1064 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1066 LookupPrivilegeValueA(0, SE_SHUTDOWN_NAME, &npr.Privileges[0].Luid);
1067 npr.PrivilegeCount = 1;
1068 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1069 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
1070 CloseHandle(hToken);
1072 ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCEIFHUNG, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER);
1074 exit (0);