Added Wine detection.
[wine/gsoc_dplay.git] / programs / winetest / main.c
blob92befbf7adc0c32ee5b4421e30f5f9f363020ae7
1 /*
2 * Wine Conformance Test EXE
4 * Copyright 2003 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
21 * This program is dedicated to Anna Lindh,
22 * Swedish Minister of Foreign Affairs.
23 * Anna was murdered September 11, 2003.
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <windows.h>
38 #include "winetest.h"
40 #define TESTRESOURCE "USERDATA"
42 struct wine_test
44 char *name;
45 int resource;
46 int subtest_count;
47 char **subtests;
48 char *exename;
51 struct rev_info
53 const char* file;
54 const char* rev;
57 static struct wine_test *wine_tests;
58 static struct rev_info *rev_infos = NULL;
60 static const char *wineloader;
62 static int running_under_wine ()
64 HMODULE module = GetModuleHandleA("ntdll.dll");
66 if (!module) return 0;
67 return (GetProcAddress(module, "wine_get_unix_file_name") != NULL);
70 void print_version ()
72 OSVERSIONINFOEX ver;
73 BOOL ext;
75 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
76 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
78 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
79 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
80 report (R_FATAL, "Can't get OS version.");
83 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
84 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
85 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
86 ver.dwPlatformId, ver.szCSDVersion);
88 if (!ext) return;
90 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
91 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
92 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
93 ver.wProductType, ver.wReserved);
95 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
98 static inline int is_dot_dir(const char* x)
100 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
103 void remove_dir (const char *dir)
105 HANDLE hFind;
106 WIN32_FIND_DATA wfd;
107 char path[MAX_PATH];
108 size_t dirlen = strlen (dir);
110 /* Make sure the directory exists before going further */
111 memcpy (path, dir, dirlen);
112 strcpy (path + dirlen++, "\\*");
113 hFind = FindFirstFile (path, &wfd);
114 if (hFind == INVALID_HANDLE_VALUE) return;
116 do {
117 char *lp = wfd.cFileName;
119 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
120 if (is_dot_dir (lp)) continue;
121 strcpy (path + dirlen, lp);
122 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
123 remove_dir(path);
124 else if (!DeleteFile (path))
125 report (R_WARNING, "Can't delete file %s: error %d",
126 path, GetLastError ());
127 } while (FindNextFile (hFind, &wfd));
128 FindClose (hFind);
129 if (!RemoveDirectory (dir))
130 report (R_WARNING, "Can't remove directory %s: error %d",
131 dir, GetLastError ());
134 const char* get_test_source_file(const char* test, const char* subtest)
136 static const char* special_dirs[][2] = {
137 { "gdi32", "gdi"}, { "kernel32", "kernel" },
138 { "user32", "user" }, { "winspool.drv", "winspool" },
139 { "ws2_32", "winsock" }, { 0, 0 }
141 static char buffer[MAX_PATH];
142 int i;
144 for (i = 0; special_dirs[i][0]; i++) {
145 if (strcmp(test, special_dirs[i][0]) == 0) {
146 test = special_dirs[i][1];
147 break;
151 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
152 return buffer;
155 const char* get_file_rev(const char* file)
157 const struct rev_info* rev;
159 for(rev = rev_infos; rev->file; rev++) {
160 if (strcmp(rev->file, file) == 0) return rev->rev;
163 return "-";
166 void extract_rev_infos ()
168 char revinfo[256], *p;
169 int size = 0, i, len;
170 HMODULE module = GetModuleHandle (NULL);
172 for (i = 0; TRUE; i++) {
173 if (i >= size) {
174 size += 100;
175 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
177 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
179 len = LoadStringA (module, i + 30000, revinfo, sizeof(revinfo));
180 if (len == 0) break; /* end of revision info */
181 if (len >= sizeof(revinfo) - 1)
182 report (R_FATAL, "Revision info too long.");
183 if(!(p = strrchr(revinfo, ':')))
184 report (R_FATAL, "Revision info malformed (i=%d)", i);
185 *p = 0;
186 rev_infos[i].file = strdup(revinfo);
187 rev_infos[i].rev = strdup(p + 1);
191 void* extract_rcdata (int id, DWORD* size)
193 HRSRC rsrc;
194 HGLOBAL hdl;
195 LPVOID addr = NULL;
197 if (!(rsrc = FindResource (0, (LPTSTR)id, TESTRESOURCE)) ||
198 !(*size = SizeofResource (0, rsrc)) ||
199 !(hdl = LoadResource (0, rsrc)) ||
200 !(addr = LockResource (hdl)))
201 report (R_FATAL, "Can't extract test file of id %d: %d",
202 id, GetLastError ());
203 return addr;
206 /* Fills in the name and exename fields */
207 void
208 extract_test (struct wine_test *test, const char *dir, int id)
210 BYTE* code;
211 DWORD size;
212 FILE* fout;
213 int strlen, bufflen = 128;
214 char *exepos;
216 code = extract_rcdata (id, &size);
217 test->name = xmalloc (bufflen);
218 while ((strlen = LoadStringA (NULL, id, test->name, bufflen))
219 == bufflen - 1) {
220 bufflen *= 2;
221 test->name = xrealloc (test->name, bufflen);
223 if (!strlen) report (R_FATAL, "Can't read name of test %d.", id);
224 test->exename = strmake (NULL, "%s/%s", dir, test->name);
225 exepos = strstr (test->name, "_test.exe");
226 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
227 *exepos = 0;
228 test->name = xrealloc (test->name, exepos - test->name + 1);
229 report (R_STEP, "Extracting: %s", test->name);
231 if (!(fout = fopen (test->exename, "wb")) ||
232 (fwrite (code, size, 1, fout) != 1) ||
233 fclose (fout)) report (R_FATAL, "Failed to write file %s.",
234 test->exename);
237 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
238 stdout to there.
240 Return the exit status, -2 if can't create process or the return
241 value of WaitForSingleObject.
244 run_ex (char *cmd, const char *out, DWORD ms)
246 STARTUPINFO si;
247 PROCESS_INFORMATION pi;
248 int fd, oldstdout = -1;
249 DWORD wait, status;
251 GetStartupInfo (&si);
252 si.wShowWindow = SW_HIDE;
253 si.dwFlags = STARTF_USESHOWWINDOW;
255 if (out) {
256 fd = open (out, O_WRONLY | O_CREAT, 0666);
257 if (-1 == fd)
258 report (R_FATAL, "Can't open '%s': %d", out, errno);
259 oldstdout = dup (1);
260 if (-1 == oldstdout)
261 report (R_FATAL, "Can't save stdout: %d", errno);
262 if (-1 == dup2 (fd, 1))
263 report (R_FATAL, "Can't redirect stdout: %d", errno);
264 close (fd);
267 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
268 NULL, NULL, &si, &pi)) {
269 status = -2;
270 } else {
271 CloseHandle (pi.hThread);
272 wait = WaitForSingleObject (pi.hProcess, ms);
273 if (wait == WAIT_OBJECT_0) {
274 GetExitCodeProcess (pi.hProcess, &status);
275 } else {
276 switch (wait) {
277 case WAIT_FAILED:
278 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
279 GetLastError ());
280 break;
281 case WAIT_TIMEOUT:
282 report (R_ERROR, "Process '%s' timed out.", cmd);
283 break;
284 default:
285 report (R_ERROR, "Wait returned %d", wait);
287 status = wait;
288 if (!TerminateProcess (pi.hProcess, 257))
289 report (R_ERROR, "TerminateProcess failed: %d",
290 GetLastError ());
291 wait = WaitForSingleObject (pi.hProcess, 5000);
292 switch (wait) {
293 case WAIT_FAILED:
294 report (R_ERROR,
295 "Wait for termination of '%s' failed: %d",
296 cmd, GetLastError ());
297 break;
298 case WAIT_OBJECT_0:
299 break;
300 case WAIT_TIMEOUT:
301 report (R_ERROR, "Can't kill process '%s'", cmd);
302 break;
303 default:
304 report (R_ERROR, "Waiting for termination: %d",
305 wait);
308 CloseHandle (pi.hProcess);
311 if (out) {
312 close (1);
313 if (-1 == dup2 (oldstdout, 1))
314 report (R_FATAL, "Can't recover stdout: %d", errno);
315 close (oldstdout);
317 return status;
320 void
321 get_subtests (const char *tempdir, struct wine_test *test, int id)
323 char *subname;
324 FILE *subfile;
325 size_t total;
326 char buffer[8192], *index;
327 const char header[] = "Valid test names:", seps[] = " \r\n";
328 int allocated;
330 test->subtest_count = 0;
332 subname = tempnam (0, "sub");
333 if (!subname) report (R_FATAL, "Can't name subtests file.");
335 extract_test (test, tempdir, id);
336 run_ex (test->exename, subname, 5000);
338 subfile = fopen (subname, "r");
339 if (!subfile) {
340 report (R_ERROR, "Can't open subtests output of %s: %d",
341 test->name, errno);
342 goto quit;
344 total = fread (buffer, 1, sizeof buffer, subfile);
345 fclose (subfile);
346 if (sizeof buffer == total) {
347 report (R_ERROR, "Subtest list of %s too big.",
348 test->name, sizeof buffer);
349 goto quit;
351 buffer[total] = 0;
353 index = strstr (buffer, header);
354 if (!index) {
355 report (R_ERROR, "Can't parse subtests output of %s",
356 test->name);
357 goto quit;
359 index += sizeof header;
361 allocated = 10;
362 test->subtests = xmalloc (allocated * sizeof(char*));
363 index = strtok (index, seps);
364 while (index) {
365 if (test->subtest_count == allocated) {
366 allocated *= 2;
367 test->subtests = xrealloc (test->subtests,
368 allocated * sizeof(char*));
370 test->subtests[test->subtest_count++] = strdup (index);
371 index = strtok (NULL, seps);
373 test->subtests = xrealloc (test->subtests,
374 test->subtest_count * sizeof(char*));
376 quit:
377 if (remove (subname))
378 report (R_WARNING, "Can't delete file '%s': %d",
379 subname, errno);
380 free (subname);
383 void
384 run_test (struct wine_test* test, const char* subtest)
386 int status;
387 const char* file = get_test_source_file(test->name, subtest);
388 const char* rev = get_file_rev(file);
389 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
391 xprintf ("%s:%s start %s %s\n", test->name, subtest, file, rev);
392 status = run_ex (cmd, NULL, 120000);
393 free (cmd);
394 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
397 BOOL CALLBACK
398 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
399 LPTSTR lpszName, LONG_PTR lParam)
401 (*(int*)lParam)++;
402 return TRUE;
405 char *
406 run_tests (char *logname, const char *tag, const char *url)
408 int nr_of_files = 0, nr_of_tests = 0, i;
409 char *tempdir;
410 FILE *logfile;
411 char build_tag[128];
413 SetErrorMode (SEM_NOGPFAULTERRORBOX);
415 if (!(wineloader = getenv("WINELOADER"))) wineloader = "wine";
417 tempdir = tempnam (0, "wct");
418 if (!tempdir)
419 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
420 report (R_DIR, tempdir);
421 if (!CreateDirectory (tempdir, NULL))
422 report (R_FATAL, "Could not create directory: %s", tempdir);
424 if (!logname) {
425 logname = tempnam (0, "res");
426 if (!logname) report (R_FATAL, "Can't name logfile.");
428 report (R_OUT, logname);
430 logfile = fopen (logname, "a");
431 if (!logfile) report (R_FATAL, "Could not open logfile.");
432 if (-1 == dup2 (fileno (logfile), 1))
433 report (R_FATAL, "Can't redirect stdout.");
434 fclose (logfile);
436 xprintf ("Version 3\n");
437 i = LoadStringA (GetModuleHandle (NULL), 0,
438 build_tag, sizeof build_tag);
439 if (i == 0) report (R_FATAL, "Build descriptor not found: %d",
440 GetLastError ());
441 if (i >= sizeof build_tag)
442 report (R_FATAL, "Build descriptor too long.");
443 xprintf ("Tests from build %s\n", build_tag);
444 xprintf ("Archive: %s\n", url?url:"");
445 xprintf ("Tag: %s\n", tag?tag:"");
446 xprintf ("Operating system version:\n");
447 print_version ();
448 xprintf ("Test output:\n" );
450 report (R_STATUS, "Counting tests");
451 if (!EnumResourceNames (NULL, TESTRESOURCE,
452 EnumTestFileProc, (LPARAM)&nr_of_files))
453 report (R_FATAL, "Can't enumerate test files: %d",
454 GetLastError ());
455 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
457 report (R_STATUS, "Extracting tests");
458 report (R_PROGRESS, 0, nr_of_files);
459 for (i = 0; i < nr_of_files; i++) {
460 get_subtests (tempdir, wine_tests+i, i+1);
461 nr_of_tests += wine_tests[i].subtest_count;
463 report (R_DELTA, 0, "Extracting: Done");
465 report (R_STATUS, "Running tests");
466 report (R_PROGRESS, 1, nr_of_tests);
467 for (i = 0; i < nr_of_files; i++) {
468 struct wine_test *test = wine_tests + i;
469 int j;
471 for (j = 0; j < test->subtest_count; j++) {
472 report (R_STEP, "Running: %s:%s", test->name,
473 test->subtests[j]);
474 run_test (test, test->subtests[j]);
477 report (R_DELTA, 0, "Running: Done");
479 report (R_STATUS, "Cleaning up");
480 close (1);
481 remove_dir (tempdir);
482 free (tempdir);
483 free (wine_tests);
485 return logname;
488 void
489 usage ()
491 fprintf (stderr, "\
492 Usage: winetest [OPTION]...\n\n\
493 -c console mode, no GUI\n\
494 -h print this message and exit\n\
495 -q quiet mode, no output at all\n\
496 -o FILE put report into FILE, do not submit\n\
497 -s FILE submit FILE, do not run tests\n\
498 -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n\
499 -u URL archive URL of this executable\n");
502 /* One can't nest strtok()-s, so here is a replacement. */
503 char *
504 mystrtok (char *newstr)
506 static char *start, *end;
507 static int finish = 1;
509 if (newstr) {
510 start = newstr;
511 finish = 0;
512 } else start = end;
513 if (finish) return NULL;
514 while (*start == ' ') start++;
515 if (*start == 0) return NULL;
516 end = start;
517 while (*end != ' ')
518 if (*end == 0) {
519 finish = 1;
520 return start;
521 } else end++;
522 *end++ = 0;
523 return start;
526 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
527 LPSTR cmdLine, int cmdShow)
529 char *logname = NULL;
530 const char *cp, *submit = NULL, *tag = NULL, *url = NULL;
532 /* initialize the revision information first */
533 extract_rev_infos();
535 cmdLine = mystrtok (cmdLine);
536 while (cmdLine) {
537 if (cmdLine[0] != '-' || cmdLine[2]) {
538 report (R_ERROR, "Not a single letter option: %s", cmdLine);
539 usage ();
540 exit (2);
542 switch (cmdLine[1]) {
543 case 'c':
544 report (R_TEXTMODE);
545 break;
546 case 'h':
547 usage ();
548 exit (0);
549 case 'q':
550 report (R_QUIET);
551 break;
552 case 's':
553 submit = mystrtok (NULL);
554 if (tag||url)
555 report (R_WARNING, "ignoring tag and url for submit");
556 send_file (submit);
557 break;
558 case 'o':
559 logname = mystrtok (NULL);
560 run_tests (logname, tag, url);
561 break;
562 case 't':
563 tag = mystrtok (NULL);
564 cp = badtagchar (tag);
565 if (cp) {
566 report (R_ERROR, "invalid char in tag: %c", *cp);
567 usage ();
568 exit (2);
570 break;
571 case 'u':
572 url = mystrtok (NULL);
573 break;
574 default:
575 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
576 usage ();
577 exit (2);
579 cmdLine = mystrtok (NULL);
581 if (!logname && !submit) {
582 report (R_STATUS, "Starting up");
583 logname = run_tests (NULL, tag, url);
584 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
585 "test results?") == IDYES)
586 if (!send_file (logname) && remove (logname))
587 report (R_WARNING, "Can't remove logfile: %d.", errno);
588 free (logname);
589 report (R_STATUS, "Finished");
591 exit (0);