Revert "winex11.drv: Optimise getting the bits of a DIB after calling SetDIBits."
[wine] / programs / winetest / main.c
1 /*
2  * Wine Conformance Test EXE
3  *
4  * Copyright 2003, 2004 Jakob Eriksson   (for Solid Form Sweden AB)
5  * Copyright 2003 Dimitrie O. Paun
6  * Copyright 2003 Ferenc Wagner
7  *
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.
12  *
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.
17  *
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
21  *
22  * This program is dedicated to Anna Lindh,
23  * Swedish Minister of Foreign Affairs.
24  * Anna was murdered September 11, 2003.
25  *
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <stdio.h>
32 #include <assert.h>
33 #include <windows.h>
34
35 #include "winetest.h"
36 #include "resource.h"
37
38 struct wine_test
39 {
40     char *name;
41     int resource;
42     int subtest_count;
43     char **subtests;
44     char *exename;
45 };
46
47 char *tag = NULL;
48 static struct wine_test *wine_tests;
49 static int nr_of_files, nr_of_tests;
50 static const char whitespace[] = " \t\r\n";
51 static const char testexe[] = "_test.exe";
52 static char build_id[64];
53
54 static char * get_file_version(char * file_name)
55 {
56     static char version[32];
57     DWORD size;
58     DWORD handle;
59
60     size = GetFileVersionInfoSizeA(file_name, &handle);
61     if (size) {
62         char * data = xmalloc(size);
63         if (data) {
64             if (GetFileVersionInfoA(file_name, handle, size, data)) {
65                 static char backslash[] = "\\";
66                 VS_FIXEDFILEINFO *pFixedVersionInfo;
67                 UINT len;
68                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
69                     sprintf(version, "%d.%d.%d.%d",
70                             pFixedVersionInfo->dwFileVersionMS >> 16,
71                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
72                             pFixedVersionInfo->dwFileVersionLS >> 16,
73                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
74                 } else
75                     sprintf(version, "version not available");
76             } else
77                 sprintf(version, "unknown");
78             free(data);
79         } else
80             sprintf(version, "failed");
81     } else
82         sprintf(version, "version not available");
83
84     return version;
85 }
86
87 static int running_under_wine (void)
88 {
89     HMODULE module = GetModuleHandleA("ntdll.dll");
90
91     if (!module) return 0;
92     return (GetProcAddress(module, "wine_server_call") != NULL);
93 }
94
95 static int running_on_visible_desktop (void)
96 {
97     HWND desktop;
98     HMODULE huser32 = GetModuleHandle("user32.dll");
99     FARPROC pGetProcessWindowStation = GetProcAddress(huser32, "GetProcessWindowStation");
100     FARPROC pGetUserObjectInformationA = GetProcAddress(huser32, "GetUserObjectInformationA");
101
102     desktop = GetDesktopWindow();
103     if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
104         return IsWindowVisible(desktop);
105
106     if (pGetProcessWindowStation && pGetUserObjectInformationA)
107     {
108         DWORD len;
109         HWINSTA wstation;
110         USEROBJECTFLAGS uoflags;
111
112         wstation = (HWINSTA)pGetProcessWindowStation();
113         assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
114         return (uoflags.dwFlags & WSF_VISIBLE) != 0;
115     }
116     return IsWindowVisible(desktop);
117 }
118
119 static void print_version (void)
120 {
121     OSVERSIONINFOEX ver;
122     BOOL ext;
123     int is_win2k3_r2;
124     const char *(*wine_get_build_id)(void);
125
126     ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
127     if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
128     {
129         ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
130         if (!GetVersionEx ((OSVERSIONINFO *) &ver))
131             report (R_FATAL, "Can't get OS version.");
132     }
133
134     xprintf ("    bRunningUnderWine=%d\n", running_under_wine ());
135     xprintf ("    bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
136     xprintf ("    dwMajorVersion=%u\n    dwMinorVersion=%u\n"
137              "    dwBuildNumber=%u\n    PlatformId=%u\n    szCSDVersion=%s\n",
138              ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
139              ver.dwPlatformId, ver.szCSDVersion);
140
141     wine_get_build_id = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_build_id");
142     if (wine_get_build_id) xprintf( "    WineBuild=%s\n", wine_get_build_id() );
143
144     is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
145     if(is_win2k3_r2)
146         xprintf("    R2 build number=%d\n", is_win2k3_r2);
147
148     if (!ext) return;
149
150     xprintf ("    wServicePackMajor=%d\n    wServicePackMinor=%d\n"
151              "    wSuiteMask=%d\n    wProductType=%d\n    wReserved=%d\n",
152              ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
153              ver.wProductType, ver.wReserved);
154 }
155
156 static inline int is_dot_dir(const char* x)
157 {
158     return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
159 }
160
161 static void remove_dir (const char *dir)
162 {
163     HANDLE  hFind;
164     WIN32_FIND_DATA wfd;
165     char path[MAX_PATH];
166     size_t dirlen = strlen (dir);
167
168     /* Make sure the directory exists before going further */
169     memcpy (path, dir, dirlen);
170     strcpy (path + dirlen++, "\\*");
171     hFind = FindFirstFile (path, &wfd);
172     if (hFind == INVALID_HANDLE_VALUE) return;
173
174     do {
175         char *lp = wfd.cFileName;
176
177         if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
178         if (is_dot_dir (lp)) continue;
179         strcpy (path + dirlen, lp);
180         if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
181             remove_dir(path);
182         else if (!DeleteFile (path))
183             report (R_WARNING, "Can't delete file %s: error %d",
184                     path, GetLastError ());
185     } while (FindNextFile (hFind, &wfd));
186     FindClose (hFind);
187     if (!RemoveDirectory (dir))
188         report (R_WARNING, "Can't remove directory %s: error %d",
189                 dir, GetLastError ());
190 }
191
192 static const char* get_test_source_file(const char* test, const char* subtest)
193 {
194     static const char* special_dirs[][2] = {
195         { 0, 0 }
196     };
197     static char buffer[MAX_PATH];
198     int i;
199
200     for (i = 0; special_dirs[i][0]; i++) {
201         if (strcmp(test, special_dirs[i][0]) == 0) {
202             test = special_dirs[i][1];
203             break;
204         }
205     }
206
207     snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
208     return buffer;
209 }
210
211 static void* extract_rcdata (LPTSTR name, int type, DWORD* size)
212 {
213     HRSRC rsrc;
214     HGLOBAL hdl;
215     LPVOID addr;
216     
217     if (!(rsrc = FindResource (NULL, name, MAKEINTRESOURCE(type))) ||
218         !(*size = SizeofResource (0, rsrc)) ||
219         !(hdl = LoadResource (0, rsrc)) ||
220         !(addr = LockResource (hdl)))
221         return NULL;
222     return addr;
223 }
224
225 /* Fills in the name and exename fields */
226 static void
227 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
228 {
229     BYTE* code;
230     DWORD size;
231     char *exepos;
232     HANDLE hfile;
233     DWORD written;
234
235     code = extract_rcdata (res_name, TESTRES, &size);
236     if (!code) report (R_FATAL, "Can't find test resource %s: %d",
237                        res_name, GetLastError ());
238     test->name = xstrdup( res_name );
239     test->exename = strmake (NULL, "%s\\%s", dir, test->name);
240     exepos = strstr (test->name, testexe);
241     if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
242     *exepos = 0;
243     test->name = xrealloc (test->name, exepos - test->name + 1);
244     report (R_STEP, "Extracting: %s", test->name);
245
246     hfile = CreateFileA(test->exename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
247                         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
248     if (hfile == INVALID_HANDLE_VALUE)
249         report (R_FATAL, "Failed to open file %s.", test->exename);
250
251     if (!WriteFile(hfile, code, size, &written, NULL))
252         report (R_FATAL, "Failed to write file %s.", test->exename);
253
254     CloseHandle(hfile);
255 }
256
257 /* Run a command for MS milliseconds.  If OUT != NULL, also redirect
258    stdout to there.
259
260    Return the exit status, -2 if can't create process or the return
261    value of WaitForSingleObject.
262  */
263 static int
264 run_ex (char *cmd, HANDLE out_file, const char *tempdir, DWORD ms)
265 {
266     STARTUPINFO si;
267     PROCESS_INFORMATION pi;
268     DWORD wait, status;
269
270     GetStartupInfo (&si);
271     si.dwFlags    = STARTF_USESTDHANDLES;
272     si.hStdInput  = GetStdHandle( STD_INPUT_HANDLE );
273     si.hStdOutput = out_file ? out_file : GetStdHandle( STD_OUTPUT_HANDLE );
274     si.hStdError  = GetStdHandle( STD_ERROR_HANDLE );
275
276     if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE,
277                          NULL, tempdir, &si, &pi)) {
278         status = -2;
279     } else {
280         CloseHandle (pi.hThread);
281         wait = WaitForSingleObject (pi.hProcess, ms);
282         if (wait == WAIT_OBJECT_0) {
283             GetExitCodeProcess (pi.hProcess, &status);
284         } else {
285             switch (wait) {
286             case WAIT_FAILED:
287                 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
288                         GetLastError ());
289                 break;
290             case WAIT_TIMEOUT:
291                 report (R_ERROR, "Process '%s' timed out.", cmd);
292                 break;
293             default:
294                 report (R_ERROR, "Wait returned %d", wait);
295             }
296             status = wait;
297             if (!TerminateProcess (pi.hProcess, 257))
298                 report (R_ERROR, "TerminateProcess failed: %d",
299                         GetLastError ());
300             wait = WaitForSingleObject (pi.hProcess, 5000);
301             switch (wait) {
302             case WAIT_FAILED:
303                 report (R_ERROR,
304                         "Wait for termination of '%s' failed: %d",
305                         cmd, GetLastError ());
306                 break;
307             case WAIT_OBJECT_0:
308                 break;
309             case WAIT_TIMEOUT:
310                 report (R_ERROR, "Can't kill process '%s'", cmd);
311                 break;
312             default:
313                 report (R_ERROR, "Waiting for termination: %d",
314                         wait);
315             }
316         }
317         CloseHandle (pi.hProcess);
318     }
319
320     return status;
321 }
322
323 static void
324 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
325 {
326     char *cmd;
327     HANDLE subfile;
328     DWORD total;
329     char buffer[8192], *index;
330     static const char header[] = "Valid test names:";
331     int allocated;
332     char tmpdir[MAX_PATH], subname[MAX_PATH];
333     SECURITY_ATTRIBUTES sa;
334
335     test->subtest_count = 0;
336
337     if (!GetTempPathA( MAX_PATH, tmpdir ) ||
338         !GetTempFileNameA( tmpdir, "sub", 0, subname ))
339         report (R_FATAL, "Can't name subtests file.");
340
341     /* make handle inheritable */
342     sa.nLength = sizeof(sa);
343     sa.lpSecurityDescriptor = NULL;
344     sa.bInheritHandle = TRUE;
345
346     subfile = CreateFileA( subname, GENERIC_READ|GENERIC_WRITE,
347                            FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
348                            &sa, CREATE_ALWAYS, 0, NULL );
349     if (subfile == INVALID_HANDLE_VALUE) {
350         report (R_ERROR, "Can't open subtests output of %s: %u",
351                 test->name, GetLastError());
352         goto quit;
353     }
354
355     extract_test (test, tempdir, res_name);
356     cmd = strmake (NULL, "%s --list", test->exename);
357     run_ex (cmd, subfile, tempdir, 5000);
358     free (cmd);
359
360     SetFilePointer( subfile, 0, NULL, FILE_BEGIN );
361     ReadFile( subfile, buffer, sizeof(buffer), &total, NULL );
362     CloseHandle( subfile );
363     if (sizeof buffer == total) {
364         report (R_ERROR, "Subtest list of %s too big.",
365                 test->name, sizeof buffer);
366         goto quit;
367     }
368     buffer[total] = 0;
369
370     index = strstr (buffer, header);
371     if (!index) {
372         report (R_ERROR, "Can't parse subtests output of %s",
373                 test->name);
374         goto quit;
375     }
376     index += sizeof header;
377
378     allocated = 10;
379     test->subtests = xmalloc (allocated * sizeof(char*));
380     index = strtok (index, whitespace);
381     while (index) {
382         if (test->subtest_count == allocated) {
383             allocated *= 2;
384             test->subtests = xrealloc (test->subtests,
385                                        allocated * sizeof(char*));
386         }
387         test->subtests[test->subtest_count++] = strdup (index);
388         index = strtok (NULL, whitespace);
389     }
390     test->subtests = xrealloc (test->subtests,
391                                test->subtest_count * sizeof(char*));
392
393  quit:
394     if (!DeleteFileA (subname))
395         report (R_WARNING, "Can't delete file '%s': %u", subname, GetLastError());
396 }
397
398 static void
399 run_test (struct wine_test* test, const char* subtest, HANDLE out_file, const char *tempdir)
400 {
401     int status;
402     const char* file = get_test_source_file(test->name, subtest);
403     char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
404
405     xprintf ("%s:%s start %s -\n", test->name, subtest, file);
406     status = run_ex (cmd, out_file, tempdir, 120000);
407     free (cmd);
408     xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
409 }
410
411 static BOOL CALLBACK
412 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
413                   LPTSTR lpszName, LONG_PTR lParam)
414 {
415     (*(int*)lParam)++;
416     return TRUE;
417 }
418
419 static BOOL CALLBACK
420 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
421                    LPTSTR lpszName, LONG_PTR lParam)
422 {
423     const char *tempdir = (const char *)lParam;
424     char dllname[MAX_PATH];
425     HMODULE dll;
426
427     /* Check if the main dll is present on this system */
428     CharLowerA(lpszName);
429     strcpy(dllname, lpszName);
430     *strstr(dllname, testexe) = 0;
431
432     dll = LoadLibraryExA(dllname, NULL, LOAD_LIBRARY_AS_DATAFILE);
433     if (!dll) {
434         xprintf ("    %s=dll is missing\n", dllname);
435         return TRUE;
436     }
437     FreeLibrary(dll);
438
439     xprintf ("    %s=%s\n", dllname, get_file_version(dllname));
440
441     get_subtests( tempdir, &wine_tests[nr_of_files], lpszName );
442     nr_of_tests += wine_tests[nr_of_files].subtest_count;
443     nr_of_files++;
444     return TRUE;
445 }
446
447 static char *
448 run_tests (char *logname)
449 {
450     int i;
451     char *strres, *eol, *nextline;
452     DWORD strsize;
453     SECURITY_ATTRIBUTES sa;
454     char tmppath[MAX_PATH], tempdir[MAX_PATH+4];
455
456     SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
457
458     if (!GetTempPathA( MAX_PATH, tmppath ))
459         report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
460
461     if (!logname) {
462         static char tmpname[MAX_PATH];
463         if (!GetTempFileNameA( tmppath, "res", 0, tmpname ))
464             report (R_FATAL, "Can't name logfile.");
465         logname = tmpname;
466     }
467     report (R_OUT, logname);
468
469     /* make handle inheritable */
470     sa.nLength = sizeof(sa);
471     sa.lpSecurityDescriptor = NULL;
472     sa.bInheritHandle = TRUE;
473
474     logfile = CreateFileA( logname, GENERIC_READ|GENERIC_WRITE,
475                            FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
476                            &sa, CREATE_ALWAYS, 0, NULL );
477     if (logfile == INVALID_HANDLE_VALUE)
478         report (R_FATAL, "Could not open logfile: %u", GetLastError());
479
480     if (!GetTempPathA( MAX_PATH, tmppath ))
481         report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
482
483     /* try stable path for ZoneAlarm */
484     strcpy( tempdir, tmppath );
485     strcat( tempdir, "wct" );
486     if (!CreateDirectoryA( tempdir, NULL ))
487     {
488         if (!GetTempFileNameA( tmppath, "wct", 0, tempdir ))
489             report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
490         DeleteFileA( tempdir );
491         if (!CreateDirectoryA( tempdir, NULL ))
492             report (R_FATAL, "Could not create directory: %s", tempdir);
493     }
494     report (R_DIR, tempdir);
495
496     xprintf ("Version 4\n");
497     xprintf ("Tests from build %s\n", build_id[0] ? build_id : "-" );
498     strres = extract_rcdata (MAKEINTRESOURCE(TESTS_URL), STRINGRES, &strsize);
499     xprintf ("Archive: ");
500     if (strres) xprintf ("%.*s", strsize, strres);
501     else xprintf ("-\n");
502     xprintf ("Tag: %s\n", tag);
503     xprintf ("Build info:\n");
504     strres = extract_rcdata (MAKEINTRESOURCE(BUILD_INFO), STRINGRES, &strsize);
505     while (strres) {
506         eol = memchr (strres, '\n', strsize);
507         if (!eol) {
508             nextline = NULL;
509             eol = strres + strsize;
510         } else {
511             strsize -= eol - strres + 1;
512             nextline = strsize?eol+1:NULL;
513             if (eol > strres && *(eol-1) == '\r') eol--;
514         }
515         xprintf ("    %.*s\n", eol-strres, strres);
516         strres = nextline;
517     }
518     xprintf ("Operating system version:\n");
519     print_version ();
520     xprintf ("Dll info:\n" );
521
522     report (R_STATUS, "Counting tests");
523     if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
524                             EnumTestFileProc, (LPARAM)&nr_of_files))
525         report (R_FATAL, "Can't enumerate test files: %d",
526                 GetLastError ());
527     wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
528
529     report (R_STATUS, "Extracting tests");
530     report (R_PROGRESS, 0, nr_of_files);
531     nr_of_files = 0;
532     nr_of_tests = 0;
533     if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
534                             extract_test_proc, (LPARAM)tempdir))
535         report (R_FATAL, "Can't enumerate test files: %d",
536                 GetLastError ());
537
538     xprintf ("Test output:\n" );
539
540     report (R_DELTA, 0, "Extracting: Done");
541
542     report (R_STATUS, "Running tests");
543     report (R_PROGRESS, 1, nr_of_tests);
544     for (i = 0; i < nr_of_files; i++) {
545         struct wine_test *test = wine_tests + i;
546         int j;
547
548         for (j = 0; j < test->subtest_count; j++) {
549             report (R_STEP, "Running: %s:%s", test->name,
550                     test->subtests[j]);
551             run_test (test, test->subtests[j], logfile, tempdir);
552         }
553     }
554     report (R_DELTA, 0, "Running: Done");
555
556     report (R_STATUS, "Cleaning up");
557     CloseHandle( logfile );
558     logfile = 0;
559     remove_dir (tempdir);
560     free (wine_tests);
561
562     return logname;
563 }
564
565 static void
566 usage (void)
567 {
568     fprintf (stderr,
569 "Usage: winetest [OPTION]...\n\n"
570 "  -c       console mode, no GUI\n"
571 "  -e       preserve the environment\n"
572 "  -h       print this message and exit\n"
573 "  -p       shutdown when the tests are done\n"
574 "  -q       quiet mode, no output at all\n"
575 "  -o FILE  put report into FILE, do not submit\n"
576 "  -s FILE  submit FILE, do not run tests\n"
577 "  -t TAG   include TAG of characters [-.0-9a-zA-Z] in the report\n");
578 }
579
580 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
581                     LPSTR cmdLine, int cmdShow)
582 {
583     char *logname = NULL;
584     const char *cp, *submit = NULL;
585     int reset_env = 1;
586     int poweroff = 0;
587     int interactive = 1;
588
589     if (!LoadStringA( 0, IDS_BUILD_ID, build_id, sizeof(build_id) )) build_id[0] = 0;
590
591     cmdLine = strtok (cmdLine, whitespace);
592     while (cmdLine) {
593         if (cmdLine[0] != '-' || cmdLine[2]) {
594             report (R_ERROR, "Not a single letter option: %s", cmdLine);
595             usage ();
596             exit (2);
597         }
598         switch (cmdLine[1]) {
599         case 'c':
600             report (R_TEXTMODE);
601             interactive = 0;
602             break;
603         case 'e':
604             reset_env = 0;
605             break;
606         case 'h':
607         case '?':
608             usage ();
609             exit (0);
610         case 'p':
611             poweroff = 1;
612             break;
613         case 'q':
614             report (R_QUIET);
615             interactive = 0;
616             break;
617         case 's':
618             submit = strtok (NULL, whitespace);
619             if (tag)
620                 report (R_WARNING, "ignoring tag for submission");
621             send_file (submit);
622             break;
623         case 'o':
624             logname = strtok (NULL, whitespace);
625             break;
626         case 't':
627             tag = strtok (NULL, whitespace);
628             if (strlen (tag) > MAXTAGLEN)
629                 report (R_FATAL, "tag is too long (maximum %d characters)",
630                         MAXTAGLEN);
631             cp = findbadtagchar (tag);
632             if (cp) {
633                 report (R_ERROR, "invalid char in tag: %c", *cp);
634                 usage ();
635                 exit (2);
636             }
637             break;
638         default:
639             report (R_ERROR, "invalid option: -%c", cmdLine[1]);
640             usage ();
641             exit (2);
642         }
643         cmdLine = strtok (NULL, whitespace);
644     }
645     if (!submit) {
646         static CHAR platform_windows[]  = "WINETEST_PLATFORM=windows",
647                     platform_wine[]     = "WINETEST_PLATFORM=wine",
648                     debug_yes[]         = "WINETEST_DEBUG=1",
649                     interactive_no[]    = "WINETEST_INTERACTIVE=0",
650                     report_success_no[] = "WINETEST_REPORT_SUCCESS=0";
651         CHAR *platform;
652
653         report (R_STATUS, "Starting up");
654
655         if (!running_on_visible_desktop ())
656             report (R_FATAL, "Tests must be run on a visible desktop");
657
658         platform = running_under_wine () ? platform_wine : platform_windows;
659
660         if (reset_env && (putenv (platform) ||
661                           putenv (debug_yes)        ||
662                           putenv (interactive_no)   ||
663                           putenv (report_success_no)))
664             report (R_FATAL, "Could not reset environment");
665
666         if (!tag) {
667             if (!interactive)
668                 report (R_FATAL, "Please specify a tag (-t option) if "
669                         "running noninteractive!");
670             if (guiAskTag () == IDABORT) exit (1);
671         }
672         report (R_TAG);
673
674         if (!build_id[0])
675             report( R_WARNING, "You won't be able to submit results without a valid build id.\n"
676                     "To submit results, winetest needs to be built from a git checkout." );
677
678         if (!logname) {
679             logname = run_tests (NULL);
680             if (build_id[0] &&
681                 report (R_ASK, MB_YESNO, "Do you want to submit the test results?") == IDYES)
682                 if (!send_file (logname) && !DeleteFileA(logname))
683                     report (R_WARNING, "Can't remove logfile: %u", GetLastError());
684         } else run_tests (logname);
685         report (R_STATUS, "Finished");
686     }
687     if (poweroff)
688     {
689         HANDLE hToken;
690         TOKEN_PRIVILEGES npr;
691
692         /* enable the shutdown privilege for the current process */
693         if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
694         {
695             LookupPrivilegeValueA(0, SE_SHUTDOWN_NAME, &npr.Privileges[0].Luid);
696             npr.PrivilegeCount = 1;
697             npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
698             AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
699             CloseHandle(hToken);
700         }
701         ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCEIFHUNG, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER);
702     }
703     exit (0);
704 }