push 87b6981010d7405c33b14cddcceec21b47729eba
[wine/hacks.git] / programs / winetest / main.c
blobe0b9ced38d6f7f72ec78ea551e7410f3ca5920a4
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 char *email = NULL;
52 static struct wine_test *wine_tests;
53 static int nr_of_files, nr_of_tests;
54 static int nr_native_dlls;
55 static const char whitespace[] = " \t\r\n";
56 static const char testexe[] = "_test.exe";
57 static char build_id[64];
59 /* filters for running only specific tests */
60 static char *filters[64];
61 static unsigned int nb_filters = 0;
63 /* Needed to check for .NET dlls */
64 static HMODULE hmscoree;
65 static HRESULT (WINAPI *pLoadLibraryShim)(LPCWSTR, LPCWSTR, LPVOID, HMODULE *);
67 /* To store the current PATH setting (related to .NET only provided dlls) */
68 static char *curpath;
70 /* check if test is being filtered out */
71 static BOOL test_filtered_out( LPCSTR module, LPCSTR testname )
73 char *p, dllname[MAX_PATH];
74 unsigned int i, len;
76 strcpy( dllname, module );
77 CharLowerA( dllname );
78 p = strstr( dllname, testexe );
79 if (p) *p = 0;
80 len = strlen(dllname);
82 if (!nb_filters) return FALSE;
83 for (i = 0; i < nb_filters; i++)
85 if (!strncmp( dllname, filters[i], len ))
87 if (!filters[i][len]) return FALSE;
88 if (filters[i][len] != ':') continue;
89 if (!testname || !strcmp( testname, &filters[i][len+1] )) return FALSE;
92 return TRUE;
95 static char * get_file_version(char * file_name)
97 static char version[32];
98 DWORD size;
99 DWORD handle;
101 size = GetFileVersionInfoSizeA(file_name, &handle);
102 if (size) {
103 char * data = heap_alloc(size);
104 if (data) {
105 if (GetFileVersionInfoA(file_name, handle, size, data)) {
106 static char backslash[] = "\\";
107 VS_FIXEDFILEINFO *pFixedVersionInfo;
108 UINT len;
109 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
110 sprintf(version, "%d.%d.%d.%d",
111 pFixedVersionInfo->dwFileVersionMS >> 16,
112 pFixedVersionInfo->dwFileVersionMS & 0xffff,
113 pFixedVersionInfo->dwFileVersionLS >> 16,
114 pFixedVersionInfo->dwFileVersionLS & 0xffff);
115 } else
116 sprintf(version, "version not available");
117 } else
118 sprintf(version, "unknown");
119 heap_free(data);
120 } else
121 sprintf(version, "failed");
122 } else
123 sprintf(version, "version not available");
125 return version;
128 static int running_under_wine (void)
130 HMODULE module = GetModuleHandleA("ntdll.dll");
132 if (!module) return 0;
133 return (GetProcAddress(module, "wine_server_call") != NULL);
136 static int check_mount_mgr(void)
138 if (running_under_wine())
140 HANDLE handle = CreateFileA( "\\\\.\\MountPointManager", GENERIC_READ,
141 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
142 if (handle == INVALID_HANDLE_VALUE) return FALSE;
143 CloseHandle( handle );
145 return TRUE;
148 static int running_on_visible_desktop (void)
150 HWND desktop;
151 HMODULE huser32 = GetModuleHandle("user32.dll");
152 HWINSTA (WINAPI *pGetProcessWindowStation)(void);
153 BOOL (WINAPI *pGetUserObjectInformationA)(HANDLE,INT,LPVOID,DWORD,LPDWORD);
155 pGetProcessWindowStation = (void *)GetProcAddress(huser32, "GetProcessWindowStation");
156 pGetUserObjectInformationA = (void *)GetProcAddress(huser32, "GetUserObjectInformationA");
158 desktop = GetDesktopWindow();
159 if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
160 return IsWindowVisible(desktop);
162 if (pGetProcessWindowStation && pGetUserObjectInformationA)
164 DWORD len;
165 HWINSTA wstation;
166 USEROBJECTFLAGS uoflags;
168 wstation = (HWINSTA)pGetProcessWindowStation();
169 assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
170 return (uoflags.dwFlags & WSF_VISIBLE) != 0;
172 return IsWindowVisible(desktop);
175 /* check for native dll when running under wine */
176 static BOOL is_native_dll( HMODULE module )
178 static const char fakedll_signature[] = "Wine placeholder DLL";
179 const IMAGE_DOS_HEADER *dos;
181 if (!running_under_wine()) return FALSE;
182 if (!((ULONG_PTR)module & 1)) return FALSE; /* not loaded as datafile */
183 /* builtin dlls can't be loaded as datafile, so we must have native or fake dll */
184 dos = (const IMAGE_DOS_HEADER *)((const char *)module - 1);
185 if (dos->e_magic != IMAGE_DOS_SIGNATURE) return FALSE;
186 if (dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
187 !memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return FALSE;
188 return TRUE;
191 static void print_version (void)
193 #ifdef __i386__
194 static const char platform[] = "i386";
195 #elif defined(__x86_64__)
196 static const char platform[] = "x86_64";
197 #elif defined(__sparc__)
198 static const char platform[] = "sparc";
199 #elif defined(__ALPHA__)
200 static const char platform[] = "alpha";
201 #elif defined(__powerpc__)
202 static const char platform[] = "powerpc";
203 #endif
204 OSVERSIONINFOEX ver;
205 BOOL ext, wow64;
206 int is_win2k3_r2;
207 const char *(CDECL *wine_get_build_id)(void);
208 void (CDECL *wine_get_host_version)( const char **sysname, const char **release );
209 BOOL (WINAPI *pIsWow64Process)(HANDLE hProcess, PBOOL Wow64Process);
210 BOOL (WINAPI *pGetProductInfo)(DWORD, DWORD, DWORD, DWORD, DWORD *);
212 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
213 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
215 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
216 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
217 report (R_FATAL, "Can't get OS version.");
219 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"IsWow64Process");
220 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &wow64 )) wow64 = FALSE;
222 xprintf (" Platform=%s%s\n", platform, wow64 ? " (WOW64)" : "");
223 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
224 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
225 xprintf (" Submitter=%s\n", email );
226 xprintf (" dwMajorVersion=%u\n dwMinorVersion=%u\n"
227 " dwBuildNumber=%u\n PlatformId=%u\n szCSDVersion=%s\n",
228 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
229 ver.dwPlatformId, ver.szCSDVersion);
231 wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
232 wine_get_host_version = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
233 if (wine_get_build_id) xprintf( " WineBuild=%s\n", wine_get_build_id() );
234 if (wine_get_host_version)
236 const char *sysname, *release;
237 wine_get_host_version( &sysname, &release );
238 xprintf( " Host system=%s\n Host version=%s\n", sysname, release );
240 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
241 if(is_win2k3_r2)
242 xprintf(" R2 build number=%d\n", is_win2k3_r2);
244 if (!ext) return;
246 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
247 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
248 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
249 ver.wProductType, ver.wReserved);
251 pGetProductInfo = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"GetProductInfo");
252 if (pGetProductInfo && !running_under_wine())
254 DWORD prodtype = 0;
256 pGetProductInfo(ver.dwMajorVersion, ver.dwMinorVersion, ver.wServicePackMajor, ver.wServicePackMinor, &prodtype);
257 xprintf(" dwProductInfo=%u\n", prodtype);
261 static inline int is_dot_dir(const char* x)
263 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
266 static void remove_dir (const char *dir)
268 HANDLE hFind;
269 WIN32_FIND_DATA wfd;
270 char path[MAX_PATH];
271 size_t dirlen = strlen (dir);
273 /* Make sure the directory exists before going further */
274 memcpy (path, dir, dirlen);
275 strcpy (path + dirlen++, "\\*");
276 hFind = FindFirstFile (path, &wfd);
277 if (hFind == INVALID_HANDLE_VALUE) return;
279 do {
280 char *lp = wfd.cFileName;
282 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
283 if (is_dot_dir (lp)) continue;
284 strcpy (path + dirlen, lp);
285 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
286 remove_dir(path);
287 else if (!DeleteFile (path))
288 report (R_WARNING, "Can't delete file %s: error %d",
289 path, GetLastError ());
290 } while (FindNextFile (hFind, &wfd));
291 FindClose (hFind);
292 if (!RemoveDirectory (dir))
293 report (R_WARNING, "Can't remove directory %s: error %d",
294 dir, GetLastError ());
297 static const char* get_test_source_file(const char* test, const char* subtest)
299 static const char* special_dirs[][2] = {
300 { 0, 0 }
302 static char buffer[MAX_PATH];
303 int i, len = strlen(test);
305 if (len > 4 && !strcmp( test + len - 4, ".exe" ))
307 len = sprintf(buffer, "programs/%s", test) - 4;
308 buffer[len] = 0;
310 else len = sprintf(buffer, "dlls/%s", test);
312 for (i = 0; special_dirs[i][0]; i++) {
313 if (strcmp(test, special_dirs[i][0]) == 0) {
314 strcpy( buffer, special_dirs[i][1] );
315 len = strlen(buffer);
316 break;
320 sprintf(buffer + len, "/tests/%s.c", subtest);
321 return buffer;
324 static void* extract_rcdata (LPCTSTR name, LPCTSTR type, DWORD* size)
326 HRSRC rsrc;
327 HGLOBAL hdl;
328 LPVOID addr;
330 if (!(rsrc = FindResource (NULL, name, type)) ||
331 !(*size = SizeofResource (0, rsrc)) ||
332 !(hdl = LoadResource (0, rsrc)) ||
333 !(addr = LockResource (hdl)))
334 return NULL;
335 return addr;
338 /* Fills in the name and exename fields */
339 static void
340 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
342 BYTE* code;
343 DWORD size;
344 char *exepos;
345 HANDLE hfile;
346 DWORD written;
348 code = extract_rcdata (res_name, "TESTRES", &size);
349 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
350 res_name, GetLastError ());
351 test->name = heap_strdup( res_name );
352 test->exename = strmake (NULL, "%s\\%s", dir, test->name);
353 exepos = strstr (test->name, testexe);
354 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
355 *exepos = 0;
356 test->name = heap_realloc (test->name, exepos - test->name + 1);
357 report (R_STEP, "Extracting: %s", test->name);
359 hfile = CreateFileA(test->exename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
360 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
361 if (hfile == INVALID_HANDLE_VALUE)
362 report (R_FATAL, "Failed to open file %s.", test->exename);
364 if (!WriteFile(hfile, code, size, &written, NULL))
365 report (R_FATAL, "Failed to write file %s.", test->exename);
367 CloseHandle(hfile);
370 static DWORD wait_process( HANDLE process, DWORD timeout )
372 DWORD wait, diff = 0, start = GetTickCount();
373 MSG msg;
375 while (diff < timeout)
377 wait = MsgWaitForMultipleObjects( 1, &process, FALSE, timeout - diff, QS_ALLINPUT );
378 if (wait != WAIT_OBJECT_0 + 1) return wait;
379 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
380 diff = GetTickCount() - start;
382 return WAIT_TIMEOUT;
385 static void append_path( const char *path)
387 char *newpath;
389 newpath = heap_alloc(strlen(curpath) + 1 + strlen(path) + 1);
390 strcpy(newpath, curpath);
391 strcat(newpath, ";");
392 strcat(newpath, path);
393 SetEnvironmentVariableA("PATH", newpath);
395 heap_free(newpath);
398 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
399 stdout to there.
401 Return the exit status, -2 if can't create process or the return
402 value of WaitForSingleObject.
404 static int
405 run_ex (char *cmd, HANDLE out_file, const char *tempdir, DWORD ms)
407 STARTUPINFO si;
408 PROCESS_INFORMATION pi;
409 DWORD wait, status;
411 GetStartupInfo (&si);
412 si.dwFlags = STARTF_USESTDHANDLES;
413 si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
414 si.hStdOutput = out_file ? out_file : GetStdHandle( STD_OUTPUT_HANDLE );
415 si.hStdError = out_file ? out_file : GetStdHandle( STD_ERROR_HANDLE );
417 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE,
418 NULL, tempdir, &si, &pi))
419 return -2;
421 CloseHandle (pi.hThread);
422 status = wait_process( pi.hProcess, ms );
423 switch (status)
425 case WAIT_OBJECT_0:
426 GetExitCodeProcess (pi.hProcess, &status);
427 CloseHandle (pi.hProcess);
428 return status;
429 case WAIT_FAILED:
430 report (R_ERROR, "Wait for '%s' failed: %d", cmd, GetLastError ());
431 break;
432 case WAIT_TIMEOUT:
433 break;
434 default:
435 report (R_ERROR, "Wait returned %d", status);
436 break;
438 if (!TerminateProcess (pi.hProcess, 257))
439 report (R_ERROR, "TerminateProcess failed: %d", GetLastError ());
440 wait = wait_process( pi.hProcess, 5000 );
441 switch (wait)
443 case WAIT_OBJECT_0:
444 break;
445 case WAIT_FAILED:
446 report (R_ERROR, "Wait for termination of '%s' failed: %d", cmd, GetLastError ());
447 break;
448 case WAIT_TIMEOUT:
449 report (R_ERROR, "Can't kill process '%s'", cmd);
450 break;
451 default:
452 report (R_ERROR, "Waiting for termination: %d", wait);
453 break;
455 CloseHandle (pi.hProcess);
456 return status;
459 static DWORD
460 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
462 char *cmd;
463 HANDLE subfile;
464 DWORD err, total;
465 char buffer[8192], *index;
466 static const char header[] = "Valid test names:";
467 int status, allocated;
468 char tmpdir[MAX_PATH], subname[MAX_PATH];
469 SECURITY_ATTRIBUTES sa;
471 test->subtest_count = 0;
473 if (!GetTempPathA( MAX_PATH, tmpdir ) ||
474 !GetTempFileNameA( tmpdir, "sub", 0, subname ))
475 report (R_FATAL, "Can't name subtests file.");
477 /* make handle inheritable */
478 sa.nLength = sizeof(sa);
479 sa.lpSecurityDescriptor = NULL;
480 sa.bInheritHandle = TRUE;
482 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
483 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
484 &sa, CREATE_ALWAYS, 0, NULL );
486 if ((subfile == INVALID_HANDLE_VALUE) &&
487 (GetLastError() == ERROR_INVALID_PARAMETER)) {
488 /* FILE_SHARE_DELETE not supported on win9x */
489 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
490 FILE_SHARE_READ | FILE_SHARE_WRITE,
491 &sa, CREATE_ALWAYS, 0, NULL );
493 if (subfile == INVALID_HANDLE_VALUE) {
494 err = GetLastError();
495 report (R_ERROR, "Can't open subtests output of %s: %u",
496 test->name, GetLastError());
497 goto quit;
500 extract_test (test, tempdir, res_name);
501 cmd = strmake (NULL, "%s --list", test->exename);
502 if (test->maindllpath) {
503 /* We need to add the path (to the main dll) to PATH */
504 append_path(test->maindllpath);
506 status = run_ex (cmd, subfile, tempdir, 5000);
507 err = GetLastError();
508 if (test->maindllpath) {
509 /* Restore PATH again */
510 SetEnvironmentVariableA("PATH", curpath);
512 heap_free (cmd);
514 if (status == -2)
516 report (R_ERROR, "Cannot run %s error %u", test->exename, err);
517 goto quit;
520 SetFilePointer( subfile, 0, NULL, FILE_BEGIN );
521 ReadFile( subfile, buffer, sizeof(buffer), &total, NULL );
522 CloseHandle( subfile );
523 if (sizeof buffer == total) {
524 report (R_ERROR, "Subtest list of %s too big.",
525 test->name, sizeof buffer);
526 err = ERROR_OUTOFMEMORY;
527 goto quit;
529 buffer[total] = 0;
531 index = strstr (buffer, header);
532 if (!index) {
533 report (R_ERROR, "Can't parse subtests output of %s",
534 test->name);
535 err = ERROR_INTERNAL_ERROR;
536 goto quit;
538 index += sizeof header;
540 allocated = 10;
541 test->subtests = heap_alloc (allocated * sizeof(char*));
542 index = strtok (index, whitespace);
543 while (index) {
544 if (test->subtest_count == allocated) {
545 allocated *= 2;
546 test->subtests = heap_realloc (test->subtests,
547 allocated * sizeof(char*));
549 if (!test_filtered_out( test->name, index ))
550 test->subtests[test->subtest_count++] = heap_strdup(index);
551 index = strtok (NULL, whitespace);
553 test->subtests = heap_realloc (test->subtests,
554 test->subtest_count * sizeof(char*));
555 err = 0;
557 quit:
558 if (!DeleteFileA (subname))
559 report (R_WARNING, "Can't delete file '%s': %u", subname, GetLastError());
560 return err;
563 static void
564 run_test (struct wine_test* test, const char* subtest, HANDLE out_file, const char *tempdir)
566 int status;
567 const char* file = get_test_source_file(test->name, subtest);
568 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
570 xprintf ("%s:%s start %s -\n", test->name, subtest, file);
571 status = run_ex (cmd, out_file, tempdir, 120000);
572 heap_free (cmd);
573 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
576 static BOOL CALLBACK
577 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
578 LPTSTR lpszName, LONG_PTR lParam)
580 if (!test_filtered_out( lpszName, NULL )) (*(int*)lParam)++;
581 return TRUE;
584 static const struct clsid_mapping
586 const char *name;
587 CLSID clsid;
588 } clsid_list[] =
590 {"oledb32", {0xc8b522d1, 0x5cf3, 0x11ce, {0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d}}},
591 {NULL, {0, 0, 0, {0,0,0,0,0,0,0,0}}}
595 static BOOL get_main_clsid(const char *name, CLSID *clsid)
597 const struct clsid_mapping *mapping;
599 for(mapping = clsid_list; mapping->name; mapping++)
601 if(!strcasecmp(name, mapping->name))
603 *clsid = mapping->clsid;
604 return TRUE;
607 return FALSE;
610 static HMODULE load_com_dll(const char *name, char **path, char *filename)
612 HMODULE dll = NULL;
613 HKEY hkey;
614 char keyname[100];
615 char dllname[MAX_PATH];
616 char *p;
617 CLSID clsid;
619 if(!get_main_clsid(name, &clsid)) return NULL;
621 sprintf(keyname, "CLSID\\{%08x-%04x-%04x-%02x%2x-%02x%2x%02x%2x%02x%2x}\\InprocServer32",
622 clsid.Data1, clsid.Data2, clsid.Data3, clsid.Data4[0], clsid.Data4[1],
623 clsid.Data4[2], clsid.Data4[3], clsid.Data4[4], clsid.Data4[5],
624 clsid.Data4[6], clsid.Data4[7]);
626 if(RegOpenKeyA(HKEY_CLASSES_ROOT, keyname, &hkey) == ERROR_SUCCESS)
628 LONG size = sizeof(dllname);
629 if(RegQueryValueA(hkey, NULL, dllname, &size) == ERROR_SUCCESS)
631 if ((dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE)))
633 strcpy( filename, dllname );
634 p = strrchr(dllname, '\\');
635 if (p) *p = 0;
636 *path = heap_strdup( dllname );
639 RegCloseKey(hkey);
642 return dll;
645 static void get_dll_path(HMODULE dll, char **path, char *filename)
647 char dllpath[MAX_PATH];
649 GetModuleFileNameA(dll, dllpath, MAX_PATH);
650 strcpy(filename, dllpath);
651 *strrchr(dllpath, '\\') = '\0';
652 *path = heap_strdup( dllpath );
655 static BOOL CALLBACK
656 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
657 LPTSTR lpszName, LONG_PTR lParam)
659 const char *tempdir = (const char *)lParam;
660 char dllname[MAX_PATH];
661 char filename[MAX_PATH];
662 WCHAR dllnameW[MAX_PATH];
663 HMODULE dll;
664 DWORD err;
666 if (test_filtered_out( lpszName, NULL )) return TRUE;
668 /* Check if the main dll is present on this system */
669 CharLowerA(lpszName);
670 strcpy(dllname, lpszName);
671 *strstr(dllname, testexe) = 0;
673 wine_tests[nr_of_files].maindllpath = NULL;
674 strcpy(filename, dllname);
675 dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE);
677 if (!dll) dll = load_com_dll(dllname, &wine_tests[nr_of_files].maindllpath, filename);
679 if (!dll && pLoadLibraryShim)
681 MultiByteToWideChar(CP_ACP, 0, dllname, -1, dllnameW, MAX_PATH);
682 if (SUCCEEDED( pLoadLibraryShim(dllnameW, NULL, NULL, &dll) ) && dll)
684 get_dll_path(dll, &wine_tests[nr_of_files].maindllpath, filename);
685 FreeLibrary(dll);
686 dll = LoadLibraryExA(filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
688 else dll = 0;
691 if (!dll)
693 xprintf (" %s=dll is missing\n", dllname);
694 return TRUE;
696 if (is_native_dll(dll))
698 FreeLibrary(dll);
699 xprintf (" %s=load error Configured as native\n", dllname);
700 nr_native_dlls++;
701 return TRUE;
703 FreeLibrary(dll);
705 if (!(err = get_subtests( tempdir, &wine_tests[nr_of_files], lpszName )))
707 xprintf (" %s=%s\n", dllname, get_file_version(filename));
708 nr_of_tests += wine_tests[nr_of_files].subtest_count;
709 nr_of_files++;
711 else
713 xprintf (" %s=load error %u\n", dllname, err);
715 return TRUE;
718 static char *
719 run_tests (char *logname, char *outdir)
721 int i;
722 char *strres, *eol, *nextline;
723 DWORD strsize;
724 SECURITY_ATTRIBUTES sa;
725 char tmppath[MAX_PATH], tempdir[MAX_PATH+4];
726 DWORD needed;
728 /* Get the current PATH only once */
729 needed = GetEnvironmentVariableA("PATH", NULL, 0);
730 curpath = heap_alloc(needed);
731 GetEnvironmentVariableA("PATH", curpath, needed);
733 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
735 if (!GetTempPathA( MAX_PATH, tmppath ))
736 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
738 if (!logname) {
739 static char tmpname[MAX_PATH];
740 if (!GetTempFileNameA( tmppath, "res", 0, tmpname ))
741 report (R_FATAL, "Can't name logfile.");
742 logname = tmpname;
744 report (R_OUT, logname);
746 /* make handle inheritable */
747 sa.nLength = sizeof(sa);
748 sa.lpSecurityDescriptor = NULL;
749 sa.bInheritHandle = TRUE;
751 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
752 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
753 &sa, CREATE_ALWAYS, 0, NULL );
755 if ((logfile == INVALID_HANDLE_VALUE) &&
756 (GetLastError() == ERROR_INVALID_PARAMETER)) {
757 /* FILE_SHARE_DELETE not supported on win9x */
758 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
759 FILE_SHARE_READ | FILE_SHARE_WRITE,
760 &sa, CREATE_ALWAYS, 0, NULL );
762 if (logfile == INVALID_HANDLE_VALUE)
763 report (R_FATAL, "Could not open logfile: %u", GetLastError());
765 /* try stable path for ZoneAlarm */
766 if (!outdir) {
767 strcpy( tempdir, tmppath );
768 strcat( tempdir, "wct" );
770 if (!CreateDirectoryA( tempdir, NULL ))
772 if (!GetTempFileNameA( tmppath, "wct", 0, tempdir ))
773 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
774 DeleteFileA( tempdir );
775 if (!CreateDirectoryA( tempdir, NULL ))
776 report (R_FATAL, "Could not create directory: %s", tempdir);
779 else
780 strcpy( tempdir, outdir);
782 report (R_DIR, tempdir);
784 xprintf ("Version 4\n");
785 xprintf ("Tests from build %s\n", build_id[0] ? build_id : "-" );
786 xprintf ("Archive: -\n"); /* no longer used */
787 xprintf ("Tag: %s\n", tag);
788 xprintf ("Build info:\n");
789 strres = extract_rcdata ("BUILD_INFO", "STRINGRES", &strsize);
790 while (strres) {
791 eol = memchr (strres, '\n', strsize);
792 if (!eol) {
793 nextline = NULL;
794 eol = strres + strsize;
795 } else {
796 strsize -= eol - strres + 1;
797 nextline = strsize?eol+1:NULL;
798 if (eol > strres && *(eol-1) == '\r') eol--;
800 xprintf (" %.*s\n", eol-strres, strres);
801 strres = nextline;
803 xprintf ("Operating system version:\n");
804 print_version ();
805 xprintf ("Dll info:\n" );
807 report (R_STATUS, "Counting tests");
808 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
809 report (R_FATAL, "Can't enumerate test files: %d",
810 GetLastError ());
811 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0]);
813 /* Do this only once during extraction (and version checking) */
814 hmscoree = LoadLibraryA("mscoree.dll");
815 pLoadLibraryShim = NULL;
816 if (hmscoree)
817 pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
819 report (R_STATUS, "Extracting tests");
820 report (R_PROGRESS, 0, nr_of_files);
821 nr_of_files = 0;
822 nr_of_tests = 0;
823 if (!EnumResourceNames (NULL, "TESTRES", extract_test_proc, (LPARAM)tempdir))
824 report (R_FATAL, "Can't enumerate test files: %d",
825 GetLastError ());
827 FreeLibrary(hmscoree);
829 xprintf ("Test output:\n" );
831 report (R_DELTA, 0, "Extracting: Done");
833 if (nr_native_dlls)
834 report( R_WARNING, "Some dlls are configured as native, you won't be able to submit results." );
836 report (R_STATUS, "Running tests");
837 report (R_PROGRESS, 1, nr_of_tests);
838 for (i = 0; i < nr_of_files; i++) {
839 struct wine_test *test = wine_tests + i;
840 int j;
842 if (test->maindllpath) {
843 /* We need to add the path (to the main dll) to PATH */
844 append_path(test->maindllpath);
847 for (j = 0; j < test->subtest_count; j++) {
848 report (R_STEP, "Running: %s:%s", test->name,
849 test->subtests[j]);
850 run_test (test, test->subtests[j], logfile, tempdir);
853 if (test->maindllpath) {
854 /* Restore PATH again */
855 SetEnvironmentVariableA("PATH", curpath);
858 report (R_DELTA, 0, "Running: Done");
860 report (R_STATUS, "Cleaning up");
861 CloseHandle( logfile );
862 logfile = 0;
863 if (!outdir)
864 remove_dir (tempdir);
865 heap_free(wine_tests);
866 heap_free(curpath);
868 return logname;
871 static BOOL WINAPI ctrl_handler(DWORD ctrl_type)
873 if (ctrl_type == CTRL_C_EVENT) {
874 printf("Ignoring Ctrl-C, use Ctrl-Break if you really want to terminate\n");
875 return TRUE;
878 return FALSE;
882 static BOOL CALLBACK
883 extract_only_proc (HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
885 const char *target_dir = (const char *)lParam;
886 char filename[MAX_PATH];
888 if (test_filtered_out( lpszName, NULL )) return TRUE;
890 strcpy(filename, lpszName);
891 CharLowerA(filename);
893 extract_test( &wine_tests[nr_of_files], target_dir, filename );
894 nr_of_files++;
895 return TRUE;
898 static void extract_only (const char *target_dir)
900 BOOL res;
902 report (R_DIR, target_dir);
903 res = CreateDirectoryA( target_dir, NULL );
904 if (!res && GetLastError() != ERROR_ALREADY_EXISTS)
905 report (R_FATAL, "Could not create directory: %s (%d)", target_dir, GetLastError ());
907 nr_of_files = 0;
908 report (R_STATUS, "Counting tests");
909 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
910 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
912 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0] );
914 report (R_STATUS, "Extracting tests");
915 report (R_PROGRESS, 0, nr_of_files);
916 nr_of_files = 0;
917 if (!EnumResourceNames (NULL, "TESTRES", extract_only_proc, (LPARAM)target_dir))
918 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
920 report (R_DELTA, 0, "Extracting: Done");
923 static void
924 usage (void)
926 fprintf (stderr,
927 "Usage: winetest [OPTION]... [TESTS]\n\n"
928 " --help print this message and exit\n"
929 " --version print the build version and exit\n"
930 " -c console mode, no GUI\n"
931 " -d DIR Use DIR as temp directory (default: %%TEMP%%\\wct)\n"
932 " -e preserve the environment\n"
933 " -h print this message and exit\n"
934 " -m MAIL an email address to enable developers to contact you\n"
935 " -p shutdown when the tests are done\n"
936 " -q quiet mode, no output at all\n"
937 " -o FILE put report into FILE, do not submit\n"
938 " -s FILE submit FILE, do not run tests\n"
939 " -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n"
940 " -x DIR Extract tests to DIR (default: .\\wct) and exit\n");
943 int main( int argc, char *argv[] )
945 char *logname = NULL, *outdir = NULL;
946 const char *extract = NULL;
947 const char *cp, *submit = NULL;
948 int reset_env = 1;
949 int poweroff = 0;
950 int interactive = 1;
951 int i;
953 if (!LoadStringA( 0, IDS_BUILD_ID, build_id, sizeof(build_id) )) build_id[0] = 0;
955 for (i = 1; i < argc && argv[i]; i++)
957 if (!strcmp(argv[i], "--help")) {
958 usage ();
959 exit (0);
961 else if (!strcmp(argv[i], "--version")) {
962 printf("%-12.12s\n", build_id[0] ? build_id : "unknown");
963 exit (0);
965 else if ((argv[i][0] != '-' && argv[i][0] != '/') || argv[i][2]) {
966 if (nb_filters == sizeof(filters)/sizeof(filters[0]))
968 report (R_ERROR, "Too many test filters specified");
969 exit (2);
971 filters[nb_filters++] = argv[i];
973 else switch (argv[i][1]) {
974 case 'c':
975 report (R_TEXTMODE);
976 interactive = 0;
977 break;
978 case 'e':
979 reset_env = 0;
980 break;
981 case 'h':
982 case '?':
983 usage ();
984 exit (0);
985 case 'm':
986 if (!(email = argv[++i]))
988 usage();
989 exit( 2 );
991 break;
992 case 'p':
993 poweroff = 1;
994 break;
995 case 'q':
996 report (R_QUIET);
997 interactive = 0;
998 break;
999 case 's':
1000 if (!(submit = argv[++i]))
1002 usage();
1003 exit( 2 );
1005 if (tag)
1006 report (R_WARNING, "ignoring tag for submission");
1007 send_file (submit);
1008 break;
1009 case 'o':
1010 if (!(logname = argv[++i]))
1012 usage();
1013 exit( 2 );
1015 break;
1016 case 't':
1017 if (!(tag = argv[++i]))
1019 usage();
1020 exit( 2 );
1022 if (strlen (tag) > MAXTAGLEN)
1023 report (R_FATAL, "tag is too long (maximum %d characters)",
1024 MAXTAGLEN);
1025 cp = findbadtagchar (tag);
1026 if (cp) {
1027 report (R_ERROR, "invalid char in tag: %c", *cp);
1028 usage ();
1029 exit (2);
1031 break;
1032 case 'x':
1033 report (R_TEXTMODE);
1034 if (!(extract = argv[++i]))
1035 extract = ".\\wct";
1037 extract_only (extract);
1038 break;
1039 case 'd':
1040 outdir = argv[++i];
1041 break;
1042 default:
1043 report (R_ERROR, "invalid option: -%c", argv[i][1]);
1044 usage ();
1045 exit (2);
1048 if (!submit && !extract) {
1049 report (R_STATUS, "Starting up");
1051 if (!running_on_visible_desktop ())
1052 report (R_FATAL, "Tests must be run on a visible desktop");
1054 if (!check_mount_mgr())
1055 report (R_FATAL, "Mount manager not running, most likely your WINEPREFIX wasn't created correctly");
1057 SetConsoleCtrlHandler(ctrl_handler, TRUE);
1059 if (reset_env)
1061 SetEnvironmentVariableA( "WINETEST_PLATFORM", running_under_wine () ? "wine" : "windows" );
1062 SetEnvironmentVariableA( "WINETEST_DEBUG", "1" );
1063 SetEnvironmentVariableA( "WINETEST_INTERACTIVE", "0" );
1064 SetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", "0" );
1067 if (!nb_filters) /* don't submit results when filtering */
1069 while (!tag) {
1070 if (!interactive)
1071 report (R_FATAL, "Please specify a tag (-t option) if "
1072 "running noninteractive!");
1073 if (guiAskTag () == IDABORT) exit (1);
1075 report (R_TAG);
1077 while (!email) {
1078 if (!interactive)
1079 report (R_FATAL, "Please specify an email address (-m option) to enable developers\n"
1080 " to contact you about your report if necessary.");
1081 if (guiAskEmail () == IDABORT) exit (1);
1084 if (!build_id[0])
1085 report( R_WARNING, "You won't be able to submit results without a valid build id.\n"
1086 "To submit results, winetest needs to be built from a git checkout." );
1089 if (!logname) {
1090 logname = run_tests (NULL, outdir);
1091 if (build_id[0] && !nb_filters && !nr_native_dlls &&
1092 report (R_ASK, MB_YESNO, "Do you want to submit the test results?") == IDYES)
1093 if (!send_file (logname) && !DeleteFileA(logname))
1094 report (R_WARNING, "Can't remove logfile: %u", GetLastError());
1095 } else run_tests (logname, outdir);
1096 report (R_STATUS, "Finished");
1098 if (poweroff)
1100 HANDLE hToken;
1101 TOKEN_PRIVILEGES npr;
1103 /* enable the shutdown privilege for the current process */
1104 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1106 LookupPrivilegeValueA(0, SE_SHUTDOWN_NAME, &npr.Privileges[0].Luid);
1107 npr.PrivilegeCount = 1;
1108 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1109 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
1110 CloseHandle(hToken);
1112 ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCEIFHUNG, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER);
1114 exit (0);