Fix ntsecapi.h so it does not depend on winternl.h.
[wine/winequartzdrv.git] / programs / winetest / main.c
blobc1fc50cf4b2905e114d0202a706d5e612fef93df
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 #include <stdio.h>
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <errno.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <windows.h>
40 #include "winetest.h"
41 #include "resource.h"
43 struct wine_test
45 char *name;
46 int resource;
47 int subtest_count;
48 char **subtests;
49 char *exename;
52 struct rev_info
54 const char* file;
55 const char* rev;
58 char *tag = NULL;
59 static struct wine_test *wine_tests;
60 static int nr_of_files, nr_of_tests;
61 static struct rev_info *rev_infos = NULL;
62 static const char whitespace[] = " \t\r\n";
64 static int running_under_wine (void)
66 HMODULE module = GetModuleHandleA("ntdll.dll");
68 if (!module) return 0;
69 return (GetProcAddress(module, "wine_server_call") != NULL);
72 static int running_on_visible_desktop (void)
74 HWND desktop;
75 HMODULE huser32 = GetModuleHandle("user32.dll");
76 FARPROC pGetProcessWindowStation = GetProcAddress(huser32, "GetProcessWindowStation");
77 FARPROC pGetUserObjectInformationA = GetProcAddress(huser32, "GetUserObjectInformationA");
79 desktop = GetDesktopWindow();
80 if (!GetWindowLongPtrW(desktop, GWLP_WNDPROC)) /* Win9x */
81 return IsWindowVisible(desktop);
83 if (pGetProcessWindowStation && pGetUserObjectInformationA)
85 DWORD len;
86 HWINSTA wstation;
87 USEROBJECTFLAGS uoflags;
89 wstation = (HWINSTA)pGetProcessWindowStation();
90 assert(pGetUserObjectInformationA(wstation, UOI_FLAGS, &uoflags, sizeof(uoflags), &len));
91 return (uoflags.dwFlags & WSF_VISIBLE) != 0;
93 return IsWindowVisible(desktop);
96 static void print_version (void)
98 OSVERSIONINFOEX ver;
99 BOOL ext;
100 int is_win2k3_r2;
102 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
103 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
105 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
106 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
107 report (R_FATAL, "Can't get OS version.");
110 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
111 xprintf (" bRunningOnVisibleDesktop=%d\n", running_on_visible_desktop ());
112 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
113 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
114 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
115 ver.dwPlatformId, ver.szCSDVersion);
117 is_win2k3_r2 = GetSystemMetrics(SM_SERVERR2);
118 if(is_win2k3_r2)
119 xprintf(" R2 build number=%d\n", is_win2k3_r2);
121 if (!ext) return;
123 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
124 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
125 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
126 ver.wProductType, ver.wReserved);
129 static inline int is_dot_dir(const char* x)
131 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
134 static void remove_dir (const char *dir)
136 HANDLE hFind;
137 WIN32_FIND_DATA wfd;
138 char path[MAX_PATH];
139 size_t dirlen = strlen (dir);
141 /* Make sure the directory exists before going further */
142 memcpy (path, dir, dirlen);
143 strcpy (path + dirlen++, "\\*");
144 hFind = FindFirstFile (path, &wfd);
145 if (hFind == INVALID_HANDLE_VALUE) return;
147 do {
148 char *lp = wfd.cFileName;
150 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
151 if (is_dot_dir (lp)) continue;
152 strcpy (path + dirlen, lp);
153 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
154 remove_dir(path);
155 else if (!DeleteFile (path))
156 report (R_WARNING, "Can't delete file %s: error %d",
157 path, GetLastError ());
158 } while (FindNextFile (hFind, &wfd));
159 FindClose (hFind);
160 if (!RemoveDirectory (dir))
161 report (R_WARNING, "Can't remove directory %s: error %d",
162 dir, GetLastError ());
165 static const char* get_test_source_file(const char* test, const char* subtest)
167 static const char* special_dirs[][2] = {
168 { "gdi32", "gdi"}, { "kernel32", "kernel" },
169 { "msacm32", "msacm" },
170 { "user32", "user" }, { "winspool.drv", "winspool" },
171 { "ws2_32", "winsock" }, { 0, 0 }
173 static char buffer[MAX_PATH];
174 int i;
176 for (i = 0; special_dirs[i][0]; i++) {
177 if (strcmp(test, special_dirs[i][0]) == 0) {
178 test = special_dirs[i][1];
179 break;
183 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
184 return buffer;
187 static const char* get_file_rev(const char* file)
189 const struct rev_info* rev;
191 for(rev = rev_infos; rev->file; rev++) {
192 if (strcmp(rev->file, file) == 0) return rev->rev;
195 return "-";
198 static void extract_rev_infos (void)
200 char revinfo[256], *p;
201 int size = 0, i;
202 unsigned int len;
203 HMODULE module = GetModuleHandle (NULL);
205 for (i = 0; TRUE; i++) {
206 if (i >= size) {
207 size += 100;
208 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
210 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
212 len = LoadStringA (module, REV_INFO+i, revinfo, sizeof(revinfo));
213 if (len == 0) break; /* end of revision info */
214 if (len >= sizeof(revinfo) - 1)
215 report (R_FATAL, "Revision info too long.");
216 if(!(p = strrchr(revinfo, ':')))
217 report (R_FATAL, "Revision info malformed (i=%d)", i);
218 *p = 0;
219 rev_infos[i].file = strdup(revinfo);
220 rev_infos[i].rev = strdup(p + 1);
224 static void* extract_rcdata (LPTSTR name, int type, DWORD* size)
226 HRSRC rsrc;
227 HGLOBAL hdl;
228 LPVOID addr;
230 if (!(rsrc = FindResource (NULL, name, MAKEINTRESOURCE(type))) ||
231 !(*size = SizeofResource (0, rsrc)) ||
232 !(hdl = LoadResource (0, rsrc)) ||
233 !(addr = LockResource (hdl)))
234 return NULL;
235 return addr;
238 /* Fills in the name and exename fields */
239 static void
240 extract_test (struct wine_test *test, const char *dir, LPTSTR res_name)
242 BYTE* code;
243 DWORD size;
244 FILE* fout;
245 char *exepos;
247 code = extract_rcdata (res_name, TESTRES, &size);
248 if (!code) report (R_FATAL, "Can't find test resource %s: %d",
249 res_name, GetLastError ());
250 test->name = xstrdup( res_name );
251 CharLowerA( test->name );
252 test->exename = strmake (NULL, "%s/%s", dir, test->name);
253 exepos = strstr (test->name, "_test.exe");
254 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
255 *exepos = 0;
256 test->name = xrealloc (test->name, exepos - test->name + 1);
257 report (R_STEP, "Extracting: %s", test->name);
259 if (!(fout = fopen (test->exename, "wb")) ||
260 (fwrite (code, size, 1, fout) != 1) ||
261 fclose (fout)) report (R_FATAL, "Failed to write file %s.",
262 test->exename);
265 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
266 stdout to there.
268 Return the exit status, -2 if can't create process or the return
269 value of WaitForSingleObject.
271 static int
272 run_ex (char *cmd, const char *out, DWORD ms)
274 STARTUPINFO si;
275 PROCESS_INFORMATION pi;
276 int fd, oldstdout = -1;
277 DWORD wait, status;
279 GetStartupInfo (&si);
280 si.dwFlags = 0;
282 if (out) {
283 fd = open (out, O_WRONLY | O_CREAT, 0666);
284 if (-1 == fd)
285 report (R_FATAL, "Can't open '%s': %d", out, errno);
286 oldstdout = dup (1);
287 if (-1 == oldstdout)
288 report (R_FATAL, "Can't save stdout: %d", errno);
289 if (-1 == dup2 (fd, 1))
290 report (R_FATAL, "Can't redirect stdout: %d", errno);
291 close (fd);
294 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
295 NULL, NULL, &si, &pi)) {
296 status = -2;
297 } else {
298 CloseHandle (pi.hThread);
299 wait = WaitForSingleObject (pi.hProcess, ms);
300 if (wait == WAIT_OBJECT_0) {
301 GetExitCodeProcess (pi.hProcess, &status);
302 } else {
303 switch (wait) {
304 case WAIT_FAILED:
305 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
306 GetLastError ());
307 break;
308 case WAIT_TIMEOUT:
309 report (R_ERROR, "Process '%s' timed out.", cmd);
310 break;
311 default:
312 report (R_ERROR, "Wait returned %d", wait);
314 status = wait;
315 if (!TerminateProcess (pi.hProcess, 257))
316 report (R_ERROR, "TerminateProcess failed: %d",
317 GetLastError ());
318 wait = WaitForSingleObject (pi.hProcess, 5000);
319 switch (wait) {
320 case WAIT_FAILED:
321 report (R_ERROR,
322 "Wait for termination of '%s' failed: %d",
323 cmd, GetLastError ());
324 break;
325 case WAIT_OBJECT_0:
326 break;
327 case WAIT_TIMEOUT:
328 report (R_ERROR, "Can't kill process '%s'", cmd);
329 break;
330 default:
331 report (R_ERROR, "Waiting for termination: %d",
332 wait);
335 CloseHandle (pi.hProcess);
338 if (out) {
339 close (1);
340 if (-1 == dup2 (oldstdout, 1))
341 report (R_FATAL, "Can't recover stdout: %d", errno);
342 close (oldstdout);
344 return status;
347 static void
348 get_subtests (const char *tempdir, struct wine_test *test, LPTSTR res_name)
350 char *subname, *cmd;
351 FILE *subfile;
352 size_t total;
353 char buffer[8192], *index;
354 static const char header[] = "Valid test names:";
355 int allocated;
357 test->subtest_count = 0;
359 subname = tempnam (0, "sub");
360 if (!subname) report (R_FATAL, "Can't name subtests file.");
362 extract_test (test, tempdir, res_name);
363 cmd = strmake (NULL, "%s --list", test->exename);
364 run_ex (cmd, subname, 5000);
365 free (cmd);
367 subfile = fopen (subname, "r");
368 if (!subfile) {
369 report (R_ERROR, "Can't open subtests output of %s: %d",
370 test->name, errno);
371 goto quit;
373 total = fread (buffer, 1, sizeof buffer, subfile);
374 fclose (subfile);
375 if (sizeof buffer == total) {
376 report (R_ERROR, "Subtest list of %s too big.",
377 test->name, sizeof buffer);
378 goto quit;
380 buffer[total] = 0;
382 index = strstr (buffer, header);
383 if (!index) {
384 report (R_ERROR, "Can't parse subtests output of %s",
385 test->name);
386 goto quit;
388 index += sizeof header;
390 allocated = 10;
391 test->subtests = xmalloc (allocated * sizeof(char*));
392 index = strtok (index, whitespace);
393 while (index) {
394 if (test->subtest_count == allocated) {
395 allocated *= 2;
396 test->subtests = xrealloc (test->subtests,
397 allocated * sizeof(char*));
399 test->subtests[test->subtest_count++] = strdup (index);
400 index = strtok (NULL, whitespace);
402 test->subtests = xrealloc (test->subtests,
403 test->subtest_count * sizeof(char*));
405 quit:
406 if (remove (subname))
407 report (R_WARNING, "Can't delete file '%s': %d",
408 subname, errno);
409 free (subname);
412 static void
413 run_test (struct wine_test* test, const char* subtest)
415 int status;
416 const char* file = get_test_source_file(test->name, subtest);
417 const char* rev = get_file_rev(file);
418 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
420 xprintf ("%s:%s start %s %s\n", test->name, subtest, file, rev);
421 status = run_ex (cmd, NULL, 120000);
422 free (cmd);
423 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
426 static BOOL CALLBACK
427 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
428 LPTSTR lpszName, LONG_PTR lParam)
430 (*(int*)lParam)++;
431 return TRUE;
434 static BOOL CALLBACK
435 extract_test_proc (HMODULE hModule, LPCTSTR lpszType,
436 LPTSTR lpszName, LONG_PTR lParam)
438 const char *tempdir = (const char *)lParam;
439 get_subtests( tempdir, &wine_tests[nr_of_files], lpszName );
440 nr_of_tests += wine_tests[nr_of_files].subtest_count;
441 nr_of_files++;
442 return TRUE;
445 static char *
446 run_tests (char *logname)
448 int i;
449 char *tempdir, *shorttempdir;
450 int logfile;
451 char *strres, *eol, *nextline;
452 DWORD strsize;
454 SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
456 if (!logname) {
457 logname = tempnam (0, "res");
458 if (!logname) report (R_FATAL, "Can't name logfile.");
460 report (R_OUT, logname);
462 logfile = open (logname, O_WRONLY | O_CREAT | O_EXCL | O_APPEND,
463 0666);
464 if (-1 == logfile) {
465 if (EEXIST == errno)
466 report (R_FATAL, "File %s already exists.", logname);
467 else report (R_FATAL, "Could not open logfile: %d", errno);
469 if (-1 == dup2 (logfile, 1))
470 report (R_FATAL, "Can't redirect stdout: %d", errno);
471 close (logfile);
473 tempdir = tempnam (0, "wct");
474 if (!tempdir)
475 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
476 shorttempdir = strdup (tempdir);
477 if (shorttempdir) { /* try stable path for ZoneAlarm */
478 strstr (shorttempdir, "wct")[3] = 0;
479 if (CreateDirectoryA (shorttempdir, NULL)) {
480 free (tempdir);
481 tempdir = shorttempdir;
482 } else free (shorttempdir);
484 if (tempdir != shorttempdir && !CreateDirectoryA (tempdir, NULL))
485 report (R_FATAL, "Could not create directory: %s", tempdir);
486 report (R_DIR, tempdir);
488 xprintf ("Version 3\n");
489 strres = extract_rcdata (MAKEINTRESOURCE(WINE_BUILD), STRINGRES, &strsize);
490 xprintf ("Tests from build ");
491 if (strres) xprintf ("%.*s", strsize, strres);
492 else xprintf ("-\n");
493 strres = extract_rcdata (MAKEINTRESOURCE(TESTS_URL), STRINGRES, &strsize);
494 xprintf ("Archive: ");
495 if (strres) xprintf ("%.*s", strsize, strres);
496 else xprintf ("-\n");
497 xprintf ("Tag: %s\n", tag);
498 xprintf ("Build info:\n");
499 strres = extract_rcdata (MAKEINTRESOURCE(BUILD_INFO), STRINGRES, &strsize);
500 while (strres) {
501 eol = memchr (strres, '\n', strsize);
502 if (!eol) {
503 nextline = NULL;
504 eol = strres + strsize;
505 } else {
506 strsize -= eol - strres + 1;
507 nextline = strsize?eol+1:NULL;
508 if (eol > strres && *(eol-1) == '\r') eol--;
510 xprintf (" %.*s\n", eol-strres, strres);
511 strres = nextline;
513 xprintf ("Operating system version:\n");
514 print_version ();
515 xprintf ("Test output:\n" );
517 report (R_STATUS, "Counting tests");
518 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
519 EnumTestFileProc, (LPARAM)&nr_of_files))
520 report (R_FATAL, "Can't enumerate test files: %d",
521 GetLastError ());
522 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
524 report (R_STATUS, "Extracting tests");
525 report (R_PROGRESS, 0, nr_of_files);
526 nr_of_files = 0;
527 nr_of_tests = 0;
528 if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
529 extract_test_proc, (LPARAM)tempdir))
530 report (R_FATAL, "Can't enumerate test files: %d",
531 GetLastError ());
533 report (R_DELTA, 0, "Extracting: Done");
535 report (R_STATUS, "Running tests");
536 report (R_PROGRESS, 1, nr_of_tests);
537 for (i = 0; i < nr_of_files; i++) {
538 struct wine_test *test = wine_tests + i;
539 int j;
541 for (j = 0; j < test->subtest_count; j++) {
542 report (R_STEP, "Running: %s:%s", test->name,
543 test->subtests[j]);
544 run_test (test, test->subtests[j]);
547 report (R_DELTA, 0, "Running: Done");
549 report (R_STATUS, "Cleaning up");
550 close (1);
551 remove_dir (tempdir);
552 free (tempdir);
553 free (wine_tests);
555 return logname;
558 static void
559 usage (void)
561 fprintf (stderr, "\
562 Usage: winetest [OPTION]...\n\n\
563 -c console mode, no GUI\n\
564 -e preserve the environment\n\
565 -h print this message and exit\n\
566 -q quiet mode, no output at all\n\
567 -o FILE put report into FILE, do not submit\n\
568 -s FILE submit FILE, do not run tests\n\
569 -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n");
572 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
573 LPSTR cmdLine, int cmdShow)
575 char *logname = NULL;
576 const char *cp, *submit = NULL;
577 int reset_env = 1;
578 int interactive = 1;
580 /* initialize the revision information first */
581 extract_rev_infos();
583 cmdLine = strtok (cmdLine, whitespace);
584 while (cmdLine) {
585 if (cmdLine[0] != '-' || cmdLine[2]) {
586 report (R_ERROR, "Not a single letter option: %s", cmdLine);
587 usage ();
588 exit (2);
590 switch (cmdLine[1]) {
591 case 'c':
592 report (R_TEXTMODE);
593 interactive = 0;
594 break;
595 case 'e':
596 reset_env = 0;
597 break;
598 case 'h':
599 usage ();
600 exit (0);
601 case 'q':
602 report (R_QUIET);
603 interactive = 0;
604 break;
605 case 's':
606 submit = strtok (NULL, whitespace);
607 if (tag)
608 report (R_WARNING, "ignoring tag for submission");
609 send_file (submit);
610 break;
611 case 'o':
612 logname = strtok (NULL, whitespace);
613 break;
614 case 't':
615 tag = strtok (NULL, whitespace);
616 if (strlen (tag) > MAXTAGLEN)
617 report (R_FATAL, "tag is too long (maximum %d characters)",
618 MAXTAGLEN);
619 cp = findbadtagchar (tag);
620 if (cp) {
621 report (R_ERROR, "invalid char in tag: %c", *cp);
622 usage ();
623 exit (2);
625 break;
626 default:
627 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
628 usage ();
629 exit (2);
631 cmdLine = strtok (NULL, whitespace);
633 if (!submit) {
634 static CHAR platform_windows[] = "WINETEST_PLATFORM=windows",
635 debug_yes[] = "WINETEST_DEBUG=1",
636 interactive_no[] = "WINETEST_INTERACTIVE=0",
637 report_success_no[] = "WINETEST_REPORT_SUCCESS=0";
639 report (R_STATUS, "Starting up");
641 if (!running_on_visible_desktop ())
642 report (R_FATAL, "Tests must be run on a visible desktop");
644 if (reset_env && (putenv (platform_windows) ||
645 putenv (debug_yes) ||
646 putenv (interactive_no) ||
647 putenv (report_success_no)))
648 report (R_FATAL, "Could not reset environment: %d", errno);
650 if (!tag) {
651 if (!interactive)
652 report (R_FATAL, "Please specify a tag (-t option) if "
653 "running noninteractive!");
654 if (guiAskTag () == IDABORT) exit (1);
656 report (R_TAG);
658 if (!logname) {
659 logname = run_tests (NULL);
660 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
661 "test results?") == IDYES)
662 if (!send_file (logname) && remove (logname))
663 report (R_WARNING, "Can't remove logfile: %d.", errno);
664 free (logname);
665 } else run_tests (logname);
666 report (R_STATUS, "Finished");
668 exit (0);