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