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