push 52d6b63ba2f2d4f9b02b6b922d27bff05a60596f
[wine/hacks.git] / programs / winetest / main.c
blobee092e5405f8d5475e0ce630a695be7ff56348eb
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 /* check if Gecko is present, trying to trigger the install if not */
179 static BOOL gecko_check(void)
181 IHTMLDocument2 *doc;
182 IHTMLElement *body;
183 BOOL ret = FALSE;
185 CoInitialize( NULL );
186 if (FAILED( CoCreateInstance( &CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER,
187 &IID_IHTMLDocument2, (void **)&doc ))) return FALSE;
188 if ((ret = SUCCEEDED( IHTMLDocument2_get_body( doc, &body )))) IHTMLElement_Release( body );
189 IHTMLDocument_Release( doc );
190 return ret;
193 static void print_version (void)
195 #ifdef __i386__
196 static const char platform[] = "i386";
197 #elif defined(__x86_64__)
198 static const char platform[] = "x86_64";
199 #elif defined(__sparc__)
200 static const char platform[] = "sparc";
201 #elif defined(__ALPHA__)
202 static const char platform[] = "alpha";
203 #elif defined(__powerpc__)
204 static const char platform[] = "powerpc";
205 #endif
206 OSVERSIONINFOEX ver;
207 BOOL ext, wow64;
208 int is_win2k3_r2;
209 const char *(CDECL *wine_get_build_id)(void);
210 void (CDECL *wine_get_host_version)( const char **sysname, const char **release );
211 BOOL (WINAPI *pIsWow64Process)(HANDLE hProcess, PBOOL Wow64Process);
213 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
214 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
216 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
217 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
218 report (R_FATAL, "Can't get OS version.");
220 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"IsWow64Process");
221 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &wow64 )) wow64 = FALSE;
223 xprintf (" Platform=%s%s\n", platform, wow64 ? " (WOW64)" : "");
224 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
225 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
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);
252 static inline int is_dot_dir(const char* x)
254 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
257 static void remove_dir (const char *dir)
259 HANDLE hFind;
260 WIN32_FIND_DATA wfd;
261 char path[MAX_PATH];
262 size_t dirlen = strlen (dir);
264 /* Make sure the directory exists before going further */
265 memcpy (path, dir, dirlen);
266 strcpy (path + dirlen++, "\\*");
267 hFind = FindFirstFile (path, &wfd);
268 if (hFind == INVALID_HANDLE_VALUE) return;
270 do {
271 char *lp = wfd.cFileName;
273 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
274 if (is_dot_dir (lp)) continue;
275 strcpy (path + dirlen, lp);
276 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
277 remove_dir(path);
278 else if (!DeleteFile (path))
279 report (R_WARNING, "Can't delete file %s: error %d",
280 path, GetLastError ());
281 } while (FindNextFile (hFind, &wfd));
282 FindClose (hFind);
283 if (!RemoveDirectory (dir))
284 report (R_WARNING, "Can't remove directory %s: error %d",
285 dir, GetLastError ());
288 static const char* get_test_source_file(const char* test, const char* subtest)
290 static const char* special_dirs[][2] = {
291 { 0, 0 }
293 static char buffer[MAX_PATH];
294 int i;
296 for (i = 0; special_dirs[i][0]; i++) {
297 if (strcmp(test, special_dirs[i][0]) == 0) {
298 test = special_dirs[i][1];
299 break;
303 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
304 return buffer;
307 static void* extract_rcdata (LPCTSTR name, LPCTSTR type, DWORD* size)
309 HRSRC rsrc;
310 HGLOBAL hdl;
311 LPVOID addr;
313 if (!(rsrc = FindResource (NULL, name, type)) ||
314 !(*size = SizeofResource (0, rsrc)) ||
315 !(hdl = LoadResource (0, rsrc)) ||
316 !(addr = LockResource (hdl)))
317 return NULL;
318 return addr;
321 /* Fills in the name and exename fields */
322 static void
323 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
325 BYTE* code;
326 DWORD size;
327 char *exepos;
328 HANDLE hfile;
329 DWORD written;
331 code = extract_rcdata (res_name, "TESTRES", &size);
332 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
333 res_name, GetLastError ());
334 test->name = heap_strdup( res_name );
335 test->exename = strmake (NULL, "%s\\%s", dir, test->name);
336 exepos = strstr (test->name, testexe);
337 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
338 *exepos = 0;
339 test->name = heap_realloc (test->name, exepos - test->name + 1);
340 report (R_STEP, "Extracting: %s", test->name);
342 hfile = CreateFileA(test->exename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
343 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
344 if (hfile == INVALID_HANDLE_VALUE)
345 report (R_FATAL, "Failed to open file %s.", test->exename);
347 if (!WriteFile(hfile, code, size, &written, NULL))
348 report (R_FATAL, "Failed to write file %s.", test->exename);
350 CloseHandle(hfile);
353 static DWORD wait_process( HANDLE process, DWORD timeout )
355 DWORD wait, diff = 0, start = GetTickCount();
356 MSG msg;
358 while (diff < timeout)
360 wait = MsgWaitForMultipleObjects( 1, &process, FALSE, timeout - diff, QS_ALLINPUT );
361 if (wait != WAIT_OBJECT_0 + 1) return wait;
362 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
363 diff = GetTickCount() - start;
365 return WAIT_TIMEOUT;
368 static void append_path( const char *path)
370 char *newpath;
372 newpath = heap_alloc(strlen(curpath) + 1 + strlen(path) + 1);
373 strcpy(newpath, curpath);
374 strcat(newpath, ";");
375 strcat(newpath, path);
376 SetEnvironmentVariableA("PATH", newpath);
378 heap_free(newpath);
381 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
382 stdout to there.
384 Return the exit status, -2 if can't create process or the return
385 value of WaitForSingleObject.
387 static int
388 run_ex (char *cmd, HANDLE out_file, const char *tempdir, DWORD ms)
390 STARTUPINFO si;
391 PROCESS_INFORMATION pi;
392 DWORD wait, status;
394 GetStartupInfo (&si);
395 si.dwFlags = STARTF_USESTDHANDLES;
396 si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
397 si.hStdOutput = out_file ? out_file : GetStdHandle( STD_OUTPUT_HANDLE );
398 si.hStdError = out_file ? out_file : GetStdHandle( STD_ERROR_HANDLE );
400 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE,
401 NULL, tempdir, &si, &pi))
402 return -2;
404 CloseHandle (pi.hThread);
405 status = wait_process( pi.hProcess, ms );
406 switch (status)
408 case WAIT_OBJECT_0:
409 GetExitCodeProcess (pi.hProcess, &status);
410 CloseHandle (pi.hProcess);
411 return status;
412 case WAIT_FAILED:
413 report (R_ERROR, "Wait for '%s' failed: %d", cmd, GetLastError ());
414 break;
415 case WAIT_TIMEOUT:
416 break;
417 default:
418 report (R_ERROR, "Wait returned %d", status);
419 break;
421 if (!TerminateProcess (pi.hProcess, 257))
422 report (R_ERROR, "TerminateProcess failed: %d", GetLastError ());
423 wait = wait_process( pi.hProcess, 5000 );
424 switch (wait)
426 case WAIT_OBJECT_0:
427 break;
428 case WAIT_FAILED:
429 report (R_ERROR, "Wait for termination of '%s' failed: %d", cmd, GetLastError ());
430 break;
431 case WAIT_TIMEOUT:
432 report (R_ERROR, "Can't kill process '%s'", cmd);
433 break;
434 default:
435 report (R_ERROR, "Waiting for termination: %d", wait);
436 break;
438 CloseHandle (pi.hProcess);
439 return status;
442 static DWORD
443 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
445 char *cmd;
446 HANDLE subfile;
447 DWORD err, total;
448 char buffer[8192], *index;
449 static const char header[] = "Valid test names:";
450 int status, allocated;
451 char tmpdir[MAX_PATH], subname[MAX_PATH];
452 SECURITY_ATTRIBUTES sa;
454 test->subtest_count = 0;
456 if (!GetTempPathA( MAX_PATH, tmpdir ) ||
457 !GetTempFileNameA( tmpdir, "sub", 0, subname ))
458 report (R_FATAL, "Can't name subtests file.");
460 /* make handle inheritable */
461 sa.nLength = sizeof(sa);
462 sa.lpSecurityDescriptor = NULL;
463 sa.bInheritHandle = TRUE;
465 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
466 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
467 &sa, CREATE_ALWAYS, 0, NULL );
469 if ((subfile == INVALID_HANDLE_VALUE) &&
470 (GetLastError() == ERROR_INVALID_PARAMETER)) {
471 /* FILE_SHARE_DELETE not supported on win9x */
472 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
473 FILE_SHARE_READ | FILE_SHARE_WRITE,
474 &sa, CREATE_ALWAYS, 0, NULL );
476 if (subfile == INVALID_HANDLE_VALUE) {
477 err = GetLastError();
478 report (R_ERROR, "Can't open subtests output of %s: %u",
479 test->name, GetLastError());
480 goto quit;
483 extract_test (test, tempdir, res_name);
484 cmd = strmake (NULL, "%s --list", test->exename);
485 if (test->maindllpath) {
486 /* We need to add the path (to the main dll) to PATH */
487 append_path(test->maindllpath);
489 status = run_ex (cmd, subfile, tempdir, 5000);
490 err = GetLastError();
491 if (test->maindllpath) {
492 /* Restore PATH again */
493 SetEnvironmentVariableA("PATH", curpath);
495 heap_free (cmd);
497 if (status == -2)
499 report (R_ERROR, "Cannot run %s error %u", test->exename, err);
500 goto quit;
503 SetFilePointer( subfile, 0, NULL, FILE_BEGIN );
504 ReadFile( subfile, buffer, sizeof(buffer), &total, NULL );
505 CloseHandle( subfile );
506 if (sizeof buffer == total) {
507 report (R_ERROR, "Subtest list of %s too big.",
508 test->name, sizeof buffer);
509 err = ERROR_OUTOFMEMORY;
510 goto quit;
512 buffer[total] = 0;
514 index = strstr (buffer, header);
515 if (!index) {
516 report (R_ERROR, "Can't parse subtests output of %s",
517 test->name);
518 err = ERROR_INTERNAL_ERROR;
519 goto quit;
521 index += sizeof header;
523 allocated = 10;
524 test->subtests = heap_alloc (allocated * sizeof(char*));
525 index = strtok (index, whitespace);
526 while (index) {
527 if (test->subtest_count == allocated) {
528 allocated *= 2;
529 test->subtests = heap_realloc (test->subtests,
530 allocated * sizeof(char*));
532 if (!test_filtered_out( test->name, index ))
533 test->subtests[test->subtest_count++] = heap_strdup(index);
534 index = strtok (NULL, whitespace);
536 test->subtests = heap_realloc (test->subtests,
537 test->subtest_count * sizeof(char*));
538 err = 0;
540 quit:
541 if (!DeleteFileA (subname))
542 report (R_WARNING, "Can't delete file '%s': %u", subname, GetLastError());
543 return err;
546 static void
547 run_test (struct wine_test* test, const char* subtest, HANDLE out_file, const char *tempdir)
549 int status;
550 const char* file = get_test_source_file(test->name, subtest);
551 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
553 xprintf ("%s:%s start %s -\n", test->name, subtest, file);
554 status = run_ex (cmd, out_file, tempdir, 120000);
555 heap_free (cmd);
556 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
559 static BOOL CALLBACK
560 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
561 LPTSTR lpszName, LONG_PTR lParam)
563 if (!test_filtered_out( lpszName, NULL )) (*(int*)lParam)++;
564 return TRUE;
567 static const struct clsid_mapping
569 const char *name;
570 CLSID clsid;
571 } clsid_list[] =
573 {"oledb32", {0xc8b522d1, 0x5cf3, 0x11ce, {0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d}}},
574 {NULL, {0, 0, 0, {0,0,0,0,0,0,0,0}}}
578 static BOOL get_main_clsid(const char *name, CLSID *clsid)
580 const struct clsid_mapping *mapping;
582 for(mapping = clsid_list; mapping->name; mapping++)
584 if(!strcasecmp(name, mapping->name))
586 *clsid = mapping->clsid;
587 return TRUE;
590 return FALSE;
593 static HMODULE load_com_dll(const char *name, char **path, char *filename)
595 HMODULE dll = NULL;
596 HKEY hkey;
597 char keyname[100];
598 char dllname[MAX_PATH];
599 char *p;
600 CLSID clsid;
602 if(!get_main_clsid(name, &clsid)) return NULL;
604 sprintf(keyname, "CLSID\\{%08x-%04x-%04x-%02x%2x-%02x%2x%02x%2x%02x%2x}\\InprocServer32",
605 clsid.Data1, clsid.Data2, clsid.Data3, clsid.Data4[0], clsid.Data4[1],
606 clsid.Data4[2], clsid.Data4[3], clsid.Data4[4], clsid.Data4[5],
607 clsid.Data4[6], clsid.Data4[7]);
609 if(RegOpenKeyA(HKEY_CLASSES_ROOT, keyname, &hkey) == ERROR_SUCCESS)
611 LONG size = sizeof(dllname);
612 if(RegQueryValueA(hkey, NULL, dllname, &size) == ERROR_SUCCESS)
614 if ((dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE)))
616 strcpy( filename, dllname );
617 p = strrchr(dllname, '\\');
618 if (p) *p = 0;
619 *path = heap_strdup( dllname );
622 RegCloseKey(hkey);
625 return dll;
628 static void get_dll_path(HMODULE dll, char **path, char *filename)
630 char dllpath[MAX_PATH];
632 GetModuleFileNameA(dll, dllpath, MAX_PATH);
633 strcpy(filename, dllpath);
634 *strrchr(dllpath, '\\') = '\0';
635 *path = heap_strdup( dllpath );
638 static BOOL CALLBACK
639 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
640 LPTSTR lpszName, LONG_PTR lParam)
642 const char *tempdir = (const char *)lParam;
643 char dllname[MAX_PATH];
644 char filename[MAX_PATH];
645 WCHAR dllnameW[MAX_PATH];
646 HMODULE dll;
647 DWORD err;
649 if (test_filtered_out( lpszName, NULL )) return TRUE;
651 /* Check if the main dll is present on this system */
652 CharLowerA(lpszName);
653 strcpy(dllname, lpszName);
654 *strstr(dllname, testexe) = 0;
656 wine_tests[nr_of_files].maindllpath = NULL;
657 strcpy(filename, dllname);
658 dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE);
660 if (!dll) dll = load_com_dll(dllname, &wine_tests[nr_of_files].maindllpath, filename);
662 if (!dll && pLoadLibraryShim)
664 MultiByteToWideChar(CP_ACP, 0, dllname, -1, dllnameW, MAX_PATH);
665 if (SUCCEEDED( pLoadLibraryShim(dllnameW, NULL, NULL, &dll) ) && dll)
667 get_dll_path(dll, &wine_tests[nr_of_files].maindllpath, filename);
668 FreeLibrary(dll);
669 dll = LoadLibraryExA(filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
671 else dll = 0;
674 if (!dll)
676 xprintf (" %s=dll is missing\n", dllname);
677 return TRUE;
679 if (is_native_dll(dll))
681 FreeLibrary(dll);
682 xprintf (" %s=load error Configured as native\n", dllname);
683 nr_native_dlls++;
684 return TRUE;
686 if (!strcmp( dllname, "mshtml" ) && running_under_wine() && !gecko_check())
688 FreeLibrary(dll);
689 xprintf (" %s=load error Gecko is not installed\n", dllname);
690 return TRUE;
692 FreeLibrary(dll);
694 if (!(err = get_subtests( tempdir, &wine_tests[nr_of_files], lpszName )))
696 xprintf (" %s=%s\n", dllname, get_file_version(filename));
697 nr_of_tests += wine_tests[nr_of_files].subtest_count;
698 nr_of_files++;
700 else
702 xprintf (" %s=load error %u\n", dllname, err);
704 return TRUE;
707 static char *
708 run_tests (char *logname, char *outdir)
710 int i;
711 char *strres, *eol, *nextline;
712 DWORD strsize;
713 SECURITY_ATTRIBUTES sa;
714 char tmppath[MAX_PATH], tempdir[MAX_PATH+4];
715 DWORD needed;
717 /* Get the current PATH only once */
718 needed = GetEnvironmentVariableA("PATH", NULL, 0);
719 curpath = heap_alloc(needed);
720 GetEnvironmentVariableA("PATH", curpath, needed);
722 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
724 if (!GetTempPathA( MAX_PATH, tmppath ))
725 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
727 if (!logname) {
728 static char tmpname[MAX_PATH];
729 if (!GetTempFileNameA( tmppath, "res", 0, tmpname ))
730 report (R_FATAL, "Can't name logfile.");
731 logname = tmpname;
733 report (R_OUT, logname);
735 /* make handle inheritable */
736 sa.nLength = sizeof(sa);
737 sa.lpSecurityDescriptor = NULL;
738 sa.bInheritHandle = TRUE;
740 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
741 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
742 &sa, CREATE_ALWAYS, 0, NULL );
744 if ((logfile == INVALID_HANDLE_VALUE) &&
745 (GetLastError() == ERROR_INVALID_PARAMETER)) {
746 /* FILE_SHARE_DELETE not supported on win9x */
747 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
748 FILE_SHARE_READ | FILE_SHARE_WRITE,
749 &sa, CREATE_ALWAYS, 0, NULL );
751 if (logfile == INVALID_HANDLE_VALUE)
752 report (R_FATAL, "Could not open logfile: %u", GetLastError());
754 /* try stable path for ZoneAlarm */
755 if (!outdir) {
756 strcpy( tempdir, tmppath );
757 strcat( tempdir, "wct" );
759 if (!CreateDirectoryA( tempdir, NULL ))
761 if (!GetTempFileNameA( tmppath, "wct", 0, tempdir ))
762 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
763 DeleteFileA( tempdir );
764 if (!CreateDirectoryA( tempdir, NULL ))
765 report (R_FATAL, "Could not create directory: %s", tempdir);
768 else
769 strcpy( tempdir, outdir);
771 report (R_DIR, tempdir);
773 xprintf ("Version 4\n");
774 xprintf ("Tests from build %s\n", build_id[0] ? build_id : "-" );
775 xprintf ("Archive: -\n"); /* no longer used */
776 xprintf ("Tag: %s\n", tag);
777 xprintf ("Build info:\n");
778 strres = extract_rcdata ("BUILD_INFO", "STRINGRES", &strsize);
779 while (strres) {
780 eol = memchr (strres, '\n', strsize);
781 if (!eol) {
782 nextline = NULL;
783 eol = strres + strsize;
784 } else {
785 strsize -= eol - strres + 1;
786 nextline = strsize?eol+1:NULL;
787 if (eol > strres && *(eol-1) == '\r') eol--;
789 xprintf (" %.*s\n", eol-strres, strres);
790 strres = nextline;
792 xprintf ("Operating system version:\n");
793 print_version ();
794 xprintf ("Dll info:\n" );
796 report (R_STATUS, "Counting tests");
797 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
798 report (R_FATAL, "Can't enumerate test files: %d",
799 GetLastError ());
800 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0]);
802 /* Do this only once during extraction (and version checking) */
803 hmscoree = LoadLibraryA("mscoree.dll");
804 pLoadLibraryShim = NULL;
805 if (hmscoree)
806 pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
808 report (R_STATUS, "Extracting tests");
809 report (R_PROGRESS, 0, nr_of_files);
810 nr_of_files = 0;
811 nr_of_tests = 0;
812 if (!EnumResourceNames (NULL, "TESTRES", extract_test_proc, (LPARAM)tempdir))
813 report (R_FATAL, "Can't enumerate test files: %d",
814 GetLastError ());
816 FreeLibrary(hmscoree);
818 xprintf ("Test output:\n" );
820 report (R_DELTA, 0, "Extracting: Done");
822 if (nr_native_dlls)
823 report( R_WARNING, "Some dlls are configured as native, you won't be able to submit results." );
825 report (R_STATUS, "Running tests");
826 report (R_PROGRESS, 1, nr_of_tests);
827 for (i = 0; i < nr_of_files; i++) {
828 struct wine_test *test = wine_tests + i;
829 int j;
831 if (test->maindllpath) {
832 /* We need to add the path (to the main dll) to PATH */
833 append_path(test->maindllpath);
836 for (j = 0; j < test->subtest_count; j++) {
837 report (R_STEP, "Running: %s:%s", test->name,
838 test->subtests[j]);
839 run_test (test, test->subtests[j], logfile, tempdir);
842 if (test->maindllpath) {
843 /* Restore PATH again */
844 SetEnvironmentVariableA("PATH", curpath);
847 report (R_DELTA, 0, "Running: Done");
849 report (R_STATUS, "Cleaning up");
850 CloseHandle( logfile );
851 logfile = 0;
852 if (!outdir)
853 remove_dir (tempdir);
854 heap_free(wine_tests);
855 heap_free(curpath);
857 return logname;
860 static BOOL WINAPI ctrl_handler(DWORD ctrl_type)
862 if (ctrl_type == CTRL_C_EVENT) {
863 printf("Ignoring Ctrl-C, use Ctrl-Break if you really want to terminate\n");
864 return TRUE;
867 return FALSE;
871 static BOOL CALLBACK
872 extract_only_proc (HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
874 const char *target_dir = (const char *)lParam;
875 char filename[MAX_PATH];
877 if (test_filtered_out( lpszName, NULL )) return TRUE;
879 strcpy(filename, lpszName);
880 CharLowerA(filename);
882 extract_test( &wine_tests[nr_of_files], target_dir, filename );
883 nr_of_files++;
884 return TRUE;
887 static void extract_only (const char *target_dir)
889 BOOL res;
891 report (R_DIR, target_dir);
892 res = CreateDirectoryA( target_dir, NULL );
893 if (!res && GetLastError() != ERROR_ALREADY_EXISTS)
894 report (R_FATAL, "Could not create directory: %s (%d)", target_dir, GetLastError ());
896 nr_of_files = 0;
897 report (R_STATUS, "Counting tests");
898 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
899 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
901 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0] );
903 report (R_STATUS, "Extracting tests");
904 report (R_PROGRESS, 0, nr_of_files);
905 nr_of_files = 0;
906 if (!EnumResourceNames (NULL, "TESTRES", extract_only_proc, (LPARAM)target_dir))
907 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
909 report (R_DELTA, 0, "Extracting: Done");
912 static void
913 usage (void)
915 fprintf (stderr,
916 "Usage: winetest [OPTION]... [TESTS]\n\n"
917 " --help print this message and exit\n"
918 " --version print the build version and exit\n"
919 " -c console mode, no GUI\n"
920 " -d DIR Use DIR as temp directory (default: %%TEMP%%\\wct)\n"
921 " -e preserve the environment\n"
922 " -h print this message and exit\n"
923 " -p shutdown when the tests are done\n"
924 " -q quiet mode, no output at all\n"
925 " -o FILE put report into FILE, do not submit\n"
926 " -s FILE submit FILE, do not run tests\n"
927 " -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n"
928 " -x DIR Extract tests to DIR (default: .\\wct) and exit\n");
931 int main( int argc, char *argv[] )
933 char *logname = NULL, *outdir = NULL;
934 const char *extract = NULL;
935 const char *cp, *submit = NULL;
936 int reset_env = 1;
937 int poweroff = 0;
938 int interactive = 1;
939 int i;
941 if (!LoadStringA( 0, IDS_BUILD_ID, build_id, sizeof(build_id) )) build_id[0] = 0;
943 for (i = 1; i < argc && argv[i]; i++)
945 if (!strcmp(argv[i], "--help")) {
946 usage ();
947 exit (0);
949 else if (!strcmp(argv[i], "--version")) {
950 printf("%-12.12s\n", build_id[0] ? build_id : "unknown");
951 exit (0);
953 else if ((argv[i][0] != '-' && argv[i][0] != '/') || argv[i][2]) {
954 if (nb_filters == sizeof(filters)/sizeof(filters[0]))
956 report (R_ERROR, "Too many test filters specified");
957 exit (2);
959 filters[nb_filters++] = argv[i];
961 else switch (argv[i][1]) {
962 case 'c':
963 report (R_TEXTMODE);
964 interactive = 0;
965 break;
966 case 'e':
967 reset_env = 0;
968 break;
969 case 'h':
970 case '?':
971 usage ();
972 exit (0);
973 case 'p':
974 poweroff = 1;
975 break;
976 case 'q':
977 report (R_QUIET);
978 interactive = 0;
979 break;
980 case 's':
981 if (!(submit = argv[++i]))
983 usage();
984 exit( 2 );
986 if (tag)
987 report (R_WARNING, "ignoring tag for submission");
988 send_file (submit);
989 break;
990 case 'o':
991 if (!(logname = argv[++i]))
993 usage();
994 exit( 2 );
996 break;
997 case 't':
998 if (!(tag = argv[++i]))
1000 usage();
1001 exit( 2 );
1003 if (strlen (tag) > MAXTAGLEN)
1004 report (R_FATAL, "tag is too long (maximum %d characters)",
1005 MAXTAGLEN);
1006 cp = findbadtagchar (tag);
1007 if (cp) {
1008 report (R_ERROR, "invalid char in tag: %c", *cp);
1009 usage ();
1010 exit (2);
1012 break;
1013 case 'x':
1014 report (R_TEXTMODE);
1015 if (!(extract = argv[++i]))
1016 extract = ".\\wct";
1018 extract_only (extract);
1019 break;
1020 case 'd':
1021 outdir = argv[++i];
1022 break;
1023 default:
1024 report (R_ERROR, "invalid option: -%c", argv[i][1]);
1025 usage ();
1026 exit (2);
1029 if (!submit && !extract) {
1030 report (R_STATUS, "Starting up");
1032 if (!running_on_visible_desktop ())
1033 report (R_FATAL, "Tests must be run on a visible desktop");
1035 SetConsoleCtrlHandler(ctrl_handler, TRUE);
1037 if (reset_env)
1039 SetEnvironmentVariableA( "WINETEST_PLATFORM", running_under_wine () ? "wine" : "windows" );
1040 SetEnvironmentVariableA( "WINETEST_DEBUG", "1" );
1041 SetEnvironmentVariableA( "WINETEST_INTERACTIVE", "0" );
1042 SetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", "0" );
1045 if (!nb_filters) /* don't submit results when filtering */
1047 while (!tag) {
1048 if (!interactive)
1049 report (R_FATAL, "Please specify a tag (-t option) if "
1050 "running noninteractive!");
1051 if (guiAskTag () == IDABORT) exit (1);
1053 report (R_TAG);
1055 if (!build_id[0])
1056 report( R_WARNING, "You won't be able to submit results without a valid build id.\n"
1057 "To submit results, winetest needs to be built from a git checkout." );
1060 if (!logname) {
1061 logname = run_tests (NULL, outdir);
1062 if (build_id[0] && !nb_filters && !nr_native_dlls &&
1063 report (R_ASK, MB_YESNO, "Do you want to submit the test results?") == IDYES)
1064 if (!send_file (logname) && !DeleteFileA(logname))
1065 report (R_WARNING, "Can't remove logfile: %u", GetLastError());
1066 } else run_tests (logname, outdir);
1067 report (R_STATUS, "Finished");
1069 if (poweroff)
1071 HANDLE hToken;
1072 TOKEN_PRIVILEGES npr;
1074 /* enable the shutdown privilege for the current process */
1075 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1077 LookupPrivilegeValueA(0, SE_SHUTDOWN_NAME, &npr.Privileges[0].Luid);
1078 npr.PrivilegeCount = 1;
1079 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1080 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
1081 CloseHandle(hToken);
1083 ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCEIFHUNG, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER);
1085 exit (0);