server: Also store a file descriptor object for mappings.
[wine/multimedia.git] / programs / winetest / main.c
blob21dbaef54d3f11a447a0a70a0ba7f2082c3175a5
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);
212 BOOL (WINAPI *pGetProductInfo)(DWORD, DWORD, DWORD, DWORD, DWORD *);
214 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
215 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
217 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
218 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
219 report (R_FATAL, "Can't get OS version.");
221 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"IsWow64Process");
222 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &wow64 )) wow64 = FALSE;
224 xprintf (" Platform=%s%s\n", platform, wow64 ? " (WOW64)" : "");
225 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
226 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
227 xprintf (" dwMajorVersion=%u\n dwMinorVersion=%u\n"
228 " dwBuildNumber=%u\n PlatformId=%u\n szCSDVersion=%s\n",
229 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
230 ver.dwPlatformId, ver.szCSDVersion);
232 wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
233 wine_get_host_version = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_host_version");
234 if (wine_get_build_id) xprintf( " WineBuild=%s\n", wine_get_build_id() );
235 if (wine_get_host_version)
237 const char *sysname, *release;
238 wine_get_host_version( &sysname, &release );
239 xprintf( " Host system=%s\n Host version=%s\n", sysname, release );
241 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
242 if(is_win2k3_r2)
243 xprintf(" R2 build number=%d\n", is_win2k3_r2);
245 if (!ext) return;
247 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
248 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
249 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
250 ver.wProductType, ver.wReserved);
252 pGetProductInfo = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),"GetProductInfo");
253 if (pGetProductInfo && !running_under_wine())
255 DWORD prodtype = 0;
257 pGetProductInfo(ver.dwMajorVersion, ver.dwMinorVersion, ver.wServicePackMajor, ver.wServicePackMinor, &prodtype);
258 xprintf(" dwProductInfo=%u\n", prodtype);
262 static inline int is_dot_dir(const char* x)
264 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
267 static void remove_dir (const char *dir)
269 HANDLE hFind;
270 WIN32_FIND_DATA wfd;
271 char path[MAX_PATH];
272 size_t dirlen = strlen (dir);
274 /* Make sure the directory exists before going further */
275 memcpy (path, dir, dirlen);
276 strcpy (path + dirlen++, "\\*");
277 hFind = FindFirstFile (path, &wfd);
278 if (hFind == INVALID_HANDLE_VALUE) return;
280 do {
281 char *lp = wfd.cFileName;
283 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
284 if (is_dot_dir (lp)) continue;
285 strcpy (path + dirlen, lp);
286 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
287 remove_dir(path);
288 else if (!DeleteFile (path))
289 report (R_WARNING, "Can't delete file %s: error %d",
290 path, GetLastError ());
291 } while (FindNextFile (hFind, &wfd));
292 FindClose (hFind);
293 if (!RemoveDirectory (dir))
294 report (R_WARNING, "Can't remove directory %s: error %d",
295 dir, GetLastError ());
298 static const char* get_test_source_file(const char* test, const char* subtest)
300 static const char* special_dirs[][2] = {
301 { 0, 0 }
303 static char buffer[MAX_PATH];
304 int i;
306 for (i = 0; special_dirs[i][0]; i++) {
307 if (strcmp(test, special_dirs[i][0]) == 0) {
308 test = special_dirs[i][1];
309 break;
313 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
314 return buffer;
317 static void* extract_rcdata (LPCTSTR name, LPCTSTR type, DWORD* size)
319 HRSRC rsrc;
320 HGLOBAL hdl;
321 LPVOID addr;
323 if (!(rsrc = FindResource (NULL, name, type)) ||
324 !(*size = SizeofResource (0, rsrc)) ||
325 !(hdl = LoadResource (0, rsrc)) ||
326 !(addr = LockResource (hdl)))
327 return NULL;
328 return addr;
331 /* Fills in the name and exename fields */
332 static void
333 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
335 BYTE* code;
336 DWORD size;
337 char *exepos;
338 HANDLE hfile;
339 DWORD written;
341 code = extract_rcdata (res_name, "TESTRES", &size);
342 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
343 res_name, GetLastError ());
344 test->name = heap_strdup( res_name );
345 test->exename = strmake (NULL, "%s\\%s", dir, test->name);
346 exepos = strstr (test->name, testexe);
347 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
348 *exepos = 0;
349 test->name = heap_realloc (test->name, exepos - test->name + 1);
350 report (R_STEP, "Extracting: %s", test->name);
352 hfile = CreateFileA(test->exename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
353 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
354 if (hfile == INVALID_HANDLE_VALUE)
355 report (R_FATAL, "Failed to open file %s.", test->exename);
357 if (!WriteFile(hfile, code, size, &written, NULL))
358 report (R_FATAL, "Failed to write file %s.", test->exename);
360 CloseHandle(hfile);
363 static DWORD wait_process( HANDLE process, DWORD timeout )
365 DWORD wait, diff = 0, start = GetTickCount();
366 MSG msg;
368 while (diff < timeout)
370 wait = MsgWaitForMultipleObjects( 1, &process, FALSE, timeout - diff, QS_ALLINPUT );
371 if (wait != WAIT_OBJECT_0 + 1) return wait;
372 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
373 diff = GetTickCount() - start;
375 return WAIT_TIMEOUT;
378 static void append_path( const char *path)
380 char *newpath;
382 newpath = heap_alloc(strlen(curpath) + 1 + strlen(path) + 1);
383 strcpy(newpath, curpath);
384 strcat(newpath, ";");
385 strcat(newpath, path);
386 SetEnvironmentVariableA("PATH", newpath);
388 heap_free(newpath);
391 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
392 stdout to there.
394 Return the exit status, -2 if can't create process or the return
395 value of WaitForSingleObject.
397 static int
398 run_ex (char *cmd, HANDLE out_file, const char *tempdir, DWORD ms)
400 STARTUPINFO si;
401 PROCESS_INFORMATION pi;
402 DWORD wait, status;
404 GetStartupInfo (&si);
405 si.dwFlags = STARTF_USESTDHANDLES;
406 si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
407 si.hStdOutput = out_file ? out_file : GetStdHandle( STD_OUTPUT_HANDLE );
408 si.hStdError = out_file ? out_file : GetStdHandle( STD_ERROR_HANDLE );
410 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE,
411 NULL, tempdir, &si, &pi))
412 return -2;
414 CloseHandle (pi.hThread);
415 status = wait_process( pi.hProcess, ms );
416 switch (status)
418 case WAIT_OBJECT_0:
419 GetExitCodeProcess (pi.hProcess, &status);
420 CloseHandle (pi.hProcess);
421 return status;
422 case WAIT_FAILED:
423 report (R_ERROR, "Wait for '%s' failed: %d", cmd, GetLastError ());
424 break;
425 case WAIT_TIMEOUT:
426 break;
427 default:
428 report (R_ERROR, "Wait returned %d", status);
429 break;
431 if (!TerminateProcess (pi.hProcess, 257))
432 report (R_ERROR, "TerminateProcess failed: %d", GetLastError ());
433 wait = wait_process( pi.hProcess, 5000 );
434 switch (wait)
436 case WAIT_OBJECT_0:
437 break;
438 case WAIT_FAILED:
439 report (R_ERROR, "Wait for termination of '%s' failed: %d", cmd, GetLastError ());
440 break;
441 case WAIT_TIMEOUT:
442 report (R_ERROR, "Can't kill process '%s'", cmd);
443 break;
444 default:
445 report (R_ERROR, "Waiting for termination: %d", wait);
446 break;
448 CloseHandle (pi.hProcess);
449 return status;
452 static DWORD
453 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
455 char *cmd;
456 HANDLE subfile;
457 DWORD err, total;
458 char buffer[8192], *index;
459 static const char header[] = "Valid test names:";
460 int status, allocated;
461 char tmpdir[MAX_PATH], subname[MAX_PATH];
462 SECURITY_ATTRIBUTES sa;
464 test->subtest_count = 0;
466 if (!GetTempPathA( MAX_PATH, tmpdir ) ||
467 !GetTempFileNameA( tmpdir, "sub", 0, subname ))
468 report (R_FATAL, "Can't name subtests file.");
470 /* make handle inheritable */
471 sa.nLength = sizeof(sa);
472 sa.lpSecurityDescriptor = NULL;
473 sa.bInheritHandle = TRUE;
475 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
476 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
477 &sa, CREATE_ALWAYS, 0, NULL );
479 if ((subfile == INVALID_HANDLE_VALUE) &&
480 (GetLastError() == ERROR_INVALID_PARAMETER)) {
481 /* FILE_SHARE_DELETE not supported on win9x */
482 subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
483 FILE_SHARE_READ | FILE_SHARE_WRITE,
484 &sa, CREATE_ALWAYS, 0, NULL );
486 if (subfile == INVALID_HANDLE_VALUE) {
487 err = GetLastError();
488 report (R_ERROR, "Can't open subtests output of %s: %u",
489 test->name, GetLastError());
490 goto quit;
493 extract_test (test, tempdir, res_name);
494 cmd = strmake (NULL, "%s --list", test->exename);
495 if (test->maindllpath) {
496 /* We need to add the path (to the main dll) to PATH */
497 append_path(test->maindllpath);
499 status = run_ex (cmd, subfile, tempdir, 5000);
500 err = GetLastError();
501 if (test->maindllpath) {
502 /* Restore PATH again */
503 SetEnvironmentVariableA("PATH", curpath);
505 heap_free (cmd);
507 if (status == -2)
509 report (R_ERROR, "Cannot run %s error %u", test->exename, err);
510 goto quit;
513 SetFilePointer( subfile, 0, NULL, FILE_BEGIN );
514 ReadFile( subfile, buffer, sizeof(buffer), &total, NULL );
515 CloseHandle( subfile );
516 if (sizeof buffer == total) {
517 report (R_ERROR, "Subtest list of %s too big.",
518 test->name, sizeof buffer);
519 err = ERROR_OUTOFMEMORY;
520 goto quit;
522 buffer[total] = 0;
524 index = strstr (buffer, header);
525 if (!index) {
526 report (R_ERROR, "Can't parse subtests output of %s",
527 test->name);
528 err = ERROR_INTERNAL_ERROR;
529 goto quit;
531 index += sizeof header;
533 allocated = 10;
534 test->subtests = heap_alloc (allocated * sizeof(char*));
535 index = strtok (index, whitespace);
536 while (index) {
537 if (test->subtest_count == allocated) {
538 allocated *= 2;
539 test->subtests = heap_realloc (test->subtests,
540 allocated * sizeof(char*));
542 if (!test_filtered_out( test->name, index ))
543 test->subtests[test->subtest_count++] = heap_strdup(index);
544 index = strtok (NULL, whitespace);
546 test->subtests = heap_realloc (test->subtests,
547 test->subtest_count * sizeof(char*));
548 err = 0;
550 quit:
551 if (!DeleteFileA (subname))
552 report (R_WARNING, "Can't delete file '%s': %u", subname, GetLastError());
553 return err;
556 static void
557 run_test (struct wine_test* test, const char* subtest, HANDLE out_file, const char *tempdir)
559 int status;
560 const char* file = get_test_source_file(test->name, subtest);
561 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
563 xprintf ("%s:%s start %s -\n", test->name, subtest, file);
564 status = run_ex (cmd, out_file, tempdir, 120000);
565 heap_free (cmd);
566 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
569 static BOOL CALLBACK
570 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
571 LPTSTR lpszName, LONG_PTR lParam)
573 if (!test_filtered_out( lpszName, NULL )) (*(int*)lParam)++;
574 return TRUE;
577 static const struct clsid_mapping
579 const char *name;
580 CLSID clsid;
581 } clsid_list[] =
583 {"oledb32", {0xc8b522d1, 0x5cf3, 0x11ce, {0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d}}},
584 {NULL, {0, 0, 0, {0,0,0,0,0,0,0,0}}}
588 static BOOL get_main_clsid(const char *name, CLSID *clsid)
590 const struct clsid_mapping *mapping;
592 for(mapping = clsid_list; mapping->name; mapping++)
594 if(!strcasecmp(name, mapping->name))
596 *clsid = mapping->clsid;
597 return TRUE;
600 return FALSE;
603 static HMODULE load_com_dll(const char *name, char **path, char *filename)
605 HMODULE dll = NULL;
606 HKEY hkey;
607 char keyname[100];
608 char dllname[MAX_PATH];
609 char *p;
610 CLSID clsid;
612 if(!get_main_clsid(name, &clsid)) return NULL;
614 sprintf(keyname, "CLSID\\{%08x-%04x-%04x-%02x%2x-%02x%2x%02x%2x%02x%2x}\\InprocServer32",
615 clsid.Data1, clsid.Data2, clsid.Data3, clsid.Data4[0], clsid.Data4[1],
616 clsid.Data4[2], clsid.Data4[3], clsid.Data4[4], clsid.Data4[5],
617 clsid.Data4[6], clsid.Data4[7]);
619 if(RegOpenKeyA(HKEY_CLASSES_ROOT, keyname, &hkey) == ERROR_SUCCESS)
621 LONG size = sizeof(dllname);
622 if(RegQueryValueA(hkey, NULL, dllname, &size) == ERROR_SUCCESS)
624 if ((dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE)))
626 strcpy( filename, dllname );
627 p = strrchr(dllname, '\\');
628 if (p) *p = 0;
629 *path = heap_strdup( dllname );
632 RegCloseKey(hkey);
635 return dll;
638 static void get_dll_path(HMODULE dll, char **path, char *filename)
640 char dllpath[MAX_PATH];
642 GetModuleFileNameA(dll, dllpath, MAX_PATH);
643 strcpy(filename, dllpath);
644 *strrchr(dllpath, '\\') = '\0';
645 *path = heap_strdup( dllpath );
648 static BOOL CALLBACK
649 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
650 LPTSTR lpszName, LONG_PTR lParam)
652 const char *tempdir = (const char *)lParam;
653 char dllname[MAX_PATH];
654 char filename[MAX_PATH];
655 WCHAR dllnameW[MAX_PATH];
656 HMODULE dll;
657 DWORD err;
659 if (test_filtered_out( lpszName, NULL )) return TRUE;
661 /* Check if the main dll is present on this system */
662 CharLowerA(lpszName);
663 strcpy(dllname, lpszName);
664 *strstr(dllname, testexe) = 0;
666 wine_tests[nr_of_files].maindllpath = NULL;
667 strcpy(filename, dllname);
668 dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE);
670 if (!dll) dll = load_com_dll(dllname, &wine_tests[nr_of_files].maindllpath, filename);
672 if (!dll && pLoadLibraryShim)
674 MultiByteToWideChar(CP_ACP, 0, dllname, -1, dllnameW, MAX_PATH);
675 if (SUCCEEDED( pLoadLibraryShim(dllnameW, NULL, NULL, &dll) ) && dll)
677 get_dll_path(dll, &wine_tests[nr_of_files].maindllpath, filename);
678 FreeLibrary(dll);
679 dll = LoadLibraryExA(filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
681 else dll = 0;
684 if (!dll)
686 xprintf (" %s=dll is missing\n", dllname);
687 return TRUE;
689 if (is_native_dll(dll))
691 FreeLibrary(dll);
692 xprintf (" %s=load error Configured as native\n", dllname);
693 nr_native_dlls++;
694 return TRUE;
696 if (!strcmp( dllname, "mshtml" ) && running_under_wine() && !gecko_check())
698 FreeLibrary(dll);
699 xprintf (" %s=load error Gecko is not installed\n", dllname);
700 return TRUE;
702 FreeLibrary(dll);
704 if (!(err = get_subtests( tempdir, &wine_tests[nr_of_files], lpszName )))
706 xprintf (" %s=%s\n", dllname, get_file_version(filename));
707 nr_of_tests += wine_tests[nr_of_files].subtest_count;
708 nr_of_files++;
710 else
712 xprintf (" %s=load error %u\n", dllname, err);
714 return TRUE;
717 static char *
718 run_tests (char *logname, char *outdir)
720 int i;
721 char *strres, *eol, *nextline;
722 DWORD strsize;
723 SECURITY_ATTRIBUTES sa;
724 char tmppath[MAX_PATH], tempdir[MAX_PATH+4];
725 DWORD needed;
727 /* Get the current PATH only once */
728 needed = GetEnvironmentVariableA("PATH", NULL, 0);
729 curpath = heap_alloc(needed);
730 GetEnvironmentVariableA("PATH", curpath, needed);
732 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
734 if (!GetTempPathA( MAX_PATH, tmppath ))
735 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
737 if (!logname) {
738 static char tmpname[MAX_PATH];
739 if (!GetTempFileNameA( tmppath, "res", 0, tmpname ))
740 report (R_FATAL, "Can't name logfile.");
741 logname = tmpname;
743 report (R_OUT, logname);
745 /* make handle inheritable */
746 sa.nLength = sizeof(sa);
747 sa.lpSecurityDescriptor = NULL;
748 sa.bInheritHandle = TRUE;
750 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
751 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
752 &sa, CREATE_ALWAYS, 0, NULL );
754 if ((logfile == INVALID_HANDLE_VALUE) &&
755 (GetLastError() == ERROR_INVALID_PARAMETER)) {
756 /* FILE_SHARE_DELETE not supported on win9x */
757 logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
758 FILE_SHARE_READ | FILE_SHARE_WRITE,
759 &sa, CREATE_ALWAYS, 0, NULL );
761 if (logfile == INVALID_HANDLE_VALUE)
762 report (R_FATAL, "Could not open logfile: %u", GetLastError());
764 /* try stable path for ZoneAlarm */
765 if (!outdir) {
766 strcpy( tempdir, tmppath );
767 strcat( tempdir, "wct" );
769 if (!CreateDirectoryA( tempdir, NULL ))
771 if (!GetTempFileNameA( tmppath, "wct", 0, tempdir ))
772 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
773 DeleteFileA( tempdir );
774 if (!CreateDirectoryA( tempdir, NULL ))
775 report (R_FATAL, "Could not create directory: %s", tempdir);
778 else
779 strcpy( tempdir, outdir);
781 report (R_DIR, tempdir);
783 xprintf ("Version 4\n");
784 xprintf ("Tests from build %s\n", build_id[0] ? build_id : "-" );
785 xprintf ("Archive: -\n"); /* no longer used */
786 xprintf ("Tag: %s\n", tag);
787 xprintf ("Build info:\n");
788 strres = extract_rcdata ("BUILD_INFO", "STRINGRES", &strsize);
789 while (strres) {
790 eol = memchr (strres, '\n', strsize);
791 if (!eol) {
792 nextline = NULL;
793 eol = strres + strsize;
794 } else {
795 strsize -= eol - strres + 1;
796 nextline = strsize?eol+1:NULL;
797 if (eol > strres && *(eol-1) == '\r') eol--;
799 xprintf (" %.*s\n", eol-strres, strres);
800 strres = nextline;
802 xprintf ("Operating system version:\n");
803 print_version ();
804 xprintf ("Dll info:\n" );
806 report (R_STATUS, "Counting tests");
807 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
808 report (R_FATAL, "Can't enumerate test files: %d",
809 GetLastError ());
810 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0]);
812 /* Do this only once during extraction (and version checking) */
813 hmscoree = LoadLibraryA("mscoree.dll");
814 pLoadLibraryShim = NULL;
815 if (hmscoree)
816 pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
818 report (R_STATUS, "Extracting tests");
819 report (R_PROGRESS, 0, nr_of_files);
820 nr_of_files = 0;
821 nr_of_tests = 0;
822 if (!EnumResourceNames (NULL, "TESTRES", extract_test_proc, (LPARAM)tempdir))
823 report (R_FATAL, "Can't enumerate test files: %d",
824 GetLastError ());
826 FreeLibrary(hmscoree);
828 xprintf ("Test output:\n" );
830 report (R_DELTA, 0, "Extracting: Done");
832 if (nr_native_dlls)
833 report( R_WARNING, "Some dlls are configured as native, you won't be able to submit results." );
835 report (R_STATUS, "Running tests");
836 report (R_PROGRESS, 1, nr_of_tests);
837 for (i = 0; i < nr_of_files; i++) {
838 struct wine_test *test = wine_tests + i;
839 int j;
841 if (test->maindllpath) {
842 /* We need to add the path (to the main dll) to PATH */
843 append_path(test->maindllpath);
846 for (j = 0; j < test->subtest_count; j++) {
847 report (R_STEP, "Running: %s:%s", test->name,
848 test->subtests[j]);
849 run_test (test, test->subtests[j], logfile, tempdir);
852 if (test->maindllpath) {
853 /* Restore PATH again */
854 SetEnvironmentVariableA("PATH", curpath);
857 report (R_DELTA, 0, "Running: Done");
859 report (R_STATUS, "Cleaning up");
860 CloseHandle( logfile );
861 logfile = 0;
862 if (!outdir)
863 remove_dir (tempdir);
864 heap_free(wine_tests);
865 heap_free(curpath);
867 return logname;
870 static BOOL WINAPI ctrl_handler(DWORD ctrl_type)
872 if (ctrl_type == CTRL_C_EVENT) {
873 printf("Ignoring Ctrl-C, use Ctrl-Break if you really want to terminate\n");
874 return TRUE;
877 return FALSE;
881 static BOOL CALLBACK
882 extract_only_proc (HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
884 const char *target_dir = (const char *)lParam;
885 char filename[MAX_PATH];
887 if (test_filtered_out( lpszName, NULL )) return TRUE;
889 strcpy(filename, lpszName);
890 CharLowerA(filename);
892 extract_test( &wine_tests[nr_of_files], target_dir, filename );
893 nr_of_files++;
894 return TRUE;
897 static void extract_only (const char *target_dir)
899 BOOL res;
901 report (R_DIR, target_dir);
902 res = CreateDirectoryA( target_dir, NULL );
903 if (!res && GetLastError() != ERROR_ALREADY_EXISTS)
904 report (R_FATAL, "Could not create directory: %s (%d)", target_dir, GetLastError ());
906 nr_of_files = 0;
907 report (R_STATUS, "Counting tests");
908 if (!EnumResourceNames (NULL, "TESTRES", EnumTestFileProc, (LPARAM)&nr_of_files))
909 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
911 wine_tests = heap_alloc (nr_of_files * sizeof wine_tests[0] );
913 report (R_STATUS, "Extracting tests");
914 report (R_PROGRESS, 0, nr_of_files);
915 nr_of_files = 0;
916 if (!EnumResourceNames (NULL, "TESTRES", extract_only_proc, (LPARAM)target_dir))
917 report (R_FATAL, "Can't enumerate test files: %d", GetLastError ());
919 report (R_DELTA, 0, "Extracting: Done");
922 static void
923 usage (void)
925 fprintf (stderr,
926 "Usage: winetest [OPTION]... [TESTS]\n\n"
927 " --help print this message and exit\n"
928 " --version print the build version and exit\n"
929 " -c console mode, no GUI\n"
930 " -d DIR Use DIR as temp directory (default: %%TEMP%%\\wct)\n"
931 " -e preserve the environment\n"
932 " -h print this message and exit\n"
933 " -p shutdown when the tests are done\n"
934 " -q quiet mode, no output at all\n"
935 " -o FILE put report into FILE, do not submit\n"
936 " -s FILE submit FILE, do not run tests\n"
937 " -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n"
938 " -x DIR Extract tests to DIR (default: .\\wct) and exit\n");
941 int main( int argc, char *argv[] )
943 char *logname = NULL, *outdir = NULL;
944 const char *extract = NULL;
945 const char *cp, *submit = NULL;
946 int reset_env = 1;
947 int poweroff = 0;
948 int interactive = 1;
949 int i;
951 if (!LoadStringA( 0, IDS_BUILD_ID, build_id, sizeof(build_id) )) build_id[0] = 0;
953 for (i = 1; i < argc && argv[i]; i++)
955 if (!strcmp(argv[i], "--help")) {
956 usage ();
957 exit (0);
959 else if (!strcmp(argv[i], "--version")) {
960 printf("%-12.12s\n", build_id[0] ? build_id : "unknown");
961 exit (0);
963 else if ((argv[i][0] != '-' && argv[i][0] != '/') || argv[i][2]) {
964 if (nb_filters == sizeof(filters)/sizeof(filters[0]))
966 report (R_ERROR, "Too many test filters specified");
967 exit (2);
969 filters[nb_filters++] = argv[i];
971 else switch (argv[i][1]) {
972 case 'c':
973 report (R_TEXTMODE);
974 interactive = 0;
975 break;
976 case 'e':
977 reset_env = 0;
978 break;
979 case 'h':
980 case '?':
981 usage ();
982 exit (0);
983 case 'p':
984 poweroff = 1;
985 break;
986 case 'q':
987 report (R_QUIET);
988 interactive = 0;
989 break;
990 case 's':
991 if (!(submit = argv[++i]))
993 usage();
994 exit( 2 );
996 if (tag)
997 report (R_WARNING, "ignoring tag for submission");
998 send_file (submit);
999 break;
1000 case 'o':
1001 if (!(logname = argv[++i]))
1003 usage();
1004 exit( 2 );
1006 break;
1007 case 't':
1008 if (!(tag = argv[++i]))
1010 usage();
1011 exit( 2 );
1013 if (strlen (tag) > MAXTAGLEN)
1014 report (R_FATAL, "tag is too long (maximum %d characters)",
1015 MAXTAGLEN);
1016 cp = findbadtagchar (tag);
1017 if (cp) {
1018 report (R_ERROR, "invalid char in tag: %c", *cp);
1019 usage ();
1020 exit (2);
1022 break;
1023 case 'x':
1024 report (R_TEXTMODE);
1025 if (!(extract = argv[++i]))
1026 extract = ".\\wct";
1028 extract_only (extract);
1029 break;
1030 case 'd':
1031 outdir = argv[++i];
1032 break;
1033 default:
1034 report (R_ERROR, "invalid option: -%c", argv[i][1]);
1035 usage ();
1036 exit (2);
1039 if (!submit && !extract) {
1040 report (R_STATUS, "Starting up");
1042 if (!running_on_visible_desktop ())
1043 report (R_FATAL, "Tests must be run on a visible desktop");
1045 SetConsoleCtrlHandler(ctrl_handler, TRUE);
1047 if (reset_env)
1049 SetEnvironmentVariableA( "WINETEST_PLATFORM", running_under_wine () ? "wine" : "windows" );
1050 SetEnvironmentVariableA( "WINETEST_DEBUG", "1" );
1051 SetEnvironmentVariableA( "WINETEST_INTERACTIVE", "0" );
1052 SetEnvironmentVariableA( "WINETEST_REPORT_SUCCESS", "0" );
1055 if (!nb_filters) /* don't submit results when filtering */
1057 while (!tag) {
1058 if (!interactive)
1059 report (R_FATAL, "Please specify a tag (-t option) if "
1060 "running noninteractive!");
1061 if (guiAskTag () == IDABORT) exit (1);
1063 report (R_TAG);
1065 if (!build_id[0])
1066 report( R_WARNING, "You won't be able to submit results without a valid build id.\n"
1067 "To submit results, winetest needs to be built from a git checkout." );
1070 if (!logname) {
1071 logname = run_tests (NULL, outdir);
1072 if (build_id[0] && !nb_filters && !nr_native_dlls &&
1073 report (R_ASK, MB_YESNO, "Do you want to submit the test results?") == IDYES)
1074 if (!send_file (logname) && !DeleteFileA(logname))
1075 report (R_WARNING, "Can't remove logfile: %u", GetLastError());
1076 } else run_tests (logname, outdir);
1077 report (R_STATUS, "Finished");
1079 if (poweroff)
1081 HANDLE hToken;
1082 TOKEN_PRIVILEGES npr;
1084 /* enable the shutdown privilege for the current process */
1085 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1087 LookupPrivilegeValueA(0, SE_SHUTDOWN_NAME, &npr.Privileges[0].Luid);
1088 npr.PrivilegeCount = 1;
1089 npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1090 AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
1091 CloseHandle(hToken);
1093 ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCEIFHUNG, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER);
1095 exit (0);