Remove the last remaining wine options when running programs.
[wine/hacks.git] / programs / winetest / main.c
blob8b9db14d57e259515f49408680c8318d93bb7e5a
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 int running_under_wine ()
62 HMODULE module = GetModuleHandleA("ntdll.dll");
64 if (!module) return 0;
65 return (GetProcAddress(module, "wine_server_call") != NULL);
68 void print_version ()
70 OSVERSIONINFOEX ver;
71 BOOL ext;
73 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
74 if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
76 ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
77 if (!GetVersionEx ((OSVERSIONINFO *) &ver))
78 report (R_FATAL, "Can't get OS version.");
81 xprintf (" bRunningUnderWine=%d\n", running_under_wine ());
82 xprintf (" dwMajorVersion=%ld\n dwMinorVersion=%ld\n"
83 " dwBuildNumber=%ld\n PlatformId=%ld\n szCSDVersion=%s\n",
84 ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
85 ver.dwPlatformId, ver.szCSDVersion);
87 if (!ext) return;
89 xprintf (" wServicePackMajor=%d\n wServicePackMinor=%d\n"
90 " wSuiteMask=%d\n wProductType=%d\n wReserved=%d\n",
91 ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
92 ver.wProductType, ver.wReserved);
95 static inline int is_dot_dir(const char* x)
97 return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
100 void remove_dir (const char *dir)
102 HANDLE hFind;
103 WIN32_FIND_DATA wfd;
104 char path[MAX_PATH];
105 size_t dirlen = strlen (dir);
107 /* Make sure the directory exists before going further */
108 memcpy (path, dir, dirlen);
109 strcpy (path + dirlen++, "\\*");
110 hFind = FindFirstFile (path, &wfd);
111 if (hFind == INVALID_HANDLE_VALUE) return;
113 do {
114 char *lp = wfd.cFileName;
116 if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
117 if (is_dot_dir (lp)) continue;
118 strcpy (path + dirlen, lp);
119 if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
120 remove_dir(path);
121 else if (!DeleteFile (path))
122 report (R_WARNING, "Can't delete file %s: error %d",
123 path, GetLastError ());
124 } while (FindNextFile (hFind, &wfd));
125 FindClose (hFind);
126 if (!RemoveDirectory (dir))
127 report (R_WARNING, "Can't remove directory %s: error %d",
128 dir, GetLastError ());
131 const char* get_test_source_file(const char* test, const char* subtest)
133 static const char* special_dirs[][2] = {
134 { "gdi32", "gdi"}, { "kernel32", "kernel" },
135 { "msacm32", "msacm" },
136 { "user32", "user" }, { "winspool.drv", "winspool" },
137 { "ws2_32", "winsock" }, { 0, 0 }
139 static char buffer[MAX_PATH];
140 int i;
142 for (i = 0; special_dirs[i][0]; i++) {
143 if (strcmp(test, special_dirs[i][0]) == 0) {
144 test = special_dirs[i][1];
145 break;
149 snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
150 return buffer;
153 const char* get_file_rev(const char* file)
155 const struct rev_info* rev;
157 for(rev = rev_infos; rev->file; rev++) {
158 if (strcmp(rev->file, file) == 0) return rev->rev;
161 return "-";
164 void extract_rev_infos ()
166 char revinfo[256], *p;
167 int size = 0, i, len;
168 HMODULE module = GetModuleHandle (NULL);
170 for (i = 0; TRUE; i++) {
171 if (i >= size) {
172 size += 100;
173 rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
175 memset(rev_infos + i, 0, sizeof(rev_infos[i]));
177 len = LoadStringA (module, i + 30000, revinfo, sizeof(revinfo));
178 if (len == 0) break; /* end of revision info */
179 if (len >= sizeof(revinfo) - 1)
180 report (R_FATAL, "Revision info too long.");
181 if(!(p = strrchr(revinfo, ':')))
182 report (R_FATAL, "Revision info malformed (i=%d)", i);
183 *p = 0;
184 rev_infos[i].file = strdup(revinfo);
185 rev_infos[i].rev = strdup(p + 1);
189 void* extract_rcdata (int id, DWORD* size)
191 HRSRC rsrc;
192 HGLOBAL hdl;
193 LPVOID addr = NULL;
195 if (!(rsrc = FindResource (0, (LPTSTR)id, TESTRESOURCE)) ||
196 !(*size = SizeofResource (0, rsrc)) ||
197 !(hdl = LoadResource (0, rsrc)) ||
198 !(addr = LockResource (hdl)))
199 report (R_FATAL, "Can't extract test file of id %d: %d",
200 id, GetLastError ());
201 return addr;
204 /* Fills in the name and exename fields */
205 void
206 extract_test (struct wine_test *test, const char *dir, int id)
208 BYTE* code;
209 DWORD size;
210 FILE* fout;
211 int strlen, bufflen = 128;
212 char *exepos;
214 code = extract_rcdata (id, &size);
215 test->name = xmalloc (bufflen);
216 while ((strlen = LoadStringA (NULL, id, test->name, bufflen))
217 == bufflen - 1) {
218 bufflen *= 2;
219 test->name = xrealloc (test->name, bufflen);
221 if (!strlen) report (R_FATAL, "Can't read name of test %d.", id);
222 test->exename = strmake (NULL, "%s/%s", dir, test->name);
223 exepos = strstr (test->name, "_test.exe");
224 if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
225 *exepos = 0;
226 test->name = xrealloc (test->name, exepos - test->name + 1);
227 report (R_STEP, "Extracting: %s", test->name);
229 if (!(fout = fopen (test->exename, "wb")) ||
230 (fwrite (code, size, 1, fout) != 1) ||
231 fclose (fout)) report (R_FATAL, "Failed to write file %s.",
232 test->exename);
235 /* Run a command for MS milliseconds. If OUT != NULL, also redirect
236 stdout to there.
238 Return the exit status, -2 if can't create process or the return
239 value of WaitForSingleObject.
242 run_ex (char *cmd, const char *out, DWORD ms)
244 STARTUPINFO si;
245 PROCESS_INFORMATION pi;
246 int fd, oldstdout = -1;
247 DWORD wait, status;
249 GetStartupInfo (&si);
250 si.wShowWindow = SW_HIDE;
251 si.dwFlags = STARTF_USESHOWWINDOW;
253 if (out) {
254 fd = open (out, O_WRONLY | O_CREAT, 0666);
255 if (-1 == fd)
256 report (R_FATAL, "Can't open '%s': %d", out, errno);
257 oldstdout = dup (1);
258 if (-1 == oldstdout)
259 report (R_FATAL, "Can't save stdout: %d", errno);
260 if (-1 == dup2 (fd, 1))
261 report (R_FATAL, "Can't redirect stdout: %d", errno);
262 close (fd);
265 if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
266 NULL, NULL, &si, &pi)) {
267 status = -2;
268 } else {
269 CloseHandle (pi.hThread);
270 wait = WaitForSingleObject (pi.hProcess, ms);
271 if (wait == WAIT_OBJECT_0) {
272 GetExitCodeProcess (pi.hProcess, &status);
273 } else {
274 switch (wait) {
275 case WAIT_FAILED:
276 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
277 GetLastError ());
278 break;
279 case WAIT_TIMEOUT:
280 report (R_ERROR, "Process '%s' timed out.", cmd);
281 break;
282 default:
283 report (R_ERROR, "Wait returned %d", wait);
285 status = wait;
286 if (!TerminateProcess (pi.hProcess, 257))
287 report (R_ERROR, "TerminateProcess failed: %d",
288 GetLastError ());
289 wait = WaitForSingleObject (pi.hProcess, 5000);
290 switch (wait) {
291 case WAIT_FAILED:
292 report (R_ERROR,
293 "Wait for termination of '%s' failed: %d",
294 cmd, GetLastError ());
295 break;
296 case WAIT_OBJECT_0:
297 break;
298 case WAIT_TIMEOUT:
299 report (R_ERROR, "Can't kill process '%s'", cmd);
300 break;
301 default:
302 report (R_ERROR, "Waiting for termination: %d",
303 wait);
306 CloseHandle (pi.hProcess);
309 if (out) {
310 close (1);
311 if (-1 == dup2 (oldstdout, 1))
312 report (R_FATAL, "Can't recover stdout: %d", errno);
313 close (oldstdout);
315 return status;
318 void
319 get_subtests (const char *tempdir, struct wine_test *test, int id)
321 char *subname;
322 FILE *subfile;
323 size_t total;
324 char buffer[8192], *index;
325 static const char header[] = "Valid test names:";
326 static const char seps[] = " \r\n";
327 int allocated;
329 test->subtest_count = 0;
331 subname = tempnam (0, "sub");
332 if (!subname) report (R_FATAL, "Can't name subtests file.");
334 extract_test (test, tempdir, id);
335 run_ex (test->exename, subname, 5000);
337 subfile = fopen (subname, "r");
338 if (!subfile) {
339 report (R_ERROR, "Can't open subtests output of %s: %d",
340 test->name, errno);
341 goto quit;
343 total = fread (buffer, 1, sizeof buffer, subfile);
344 fclose (subfile);
345 if (sizeof buffer == total) {
346 report (R_ERROR, "Subtest list of %s too big.",
347 test->name, sizeof buffer);
348 goto quit;
350 buffer[total] = 0;
352 index = strstr (buffer, header);
353 if (!index) {
354 report (R_ERROR, "Can't parse subtests output of %s",
355 test->name);
356 goto quit;
358 index += sizeof header;
360 allocated = 10;
361 test->subtests = xmalloc (allocated * sizeof(char*));
362 index = strtok (index, seps);
363 while (index) {
364 if (test->subtest_count == allocated) {
365 allocated *= 2;
366 test->subtests = xrealloc (test->subtests,
367 allocated * sizeof(char*));
369 test->subtests[test->subtest_count++] = strdup (index);
370 index = strtok (NULL, seps);
372 test->subtests = xrealloc (test->subtests,
373 test->subtest_count * sizeof(char*));
375 quit:
376 if (remove (subname))
377 report (R_WARNING, "Can't delete file '%s': %d",
378 subname, errno);
379 free (subname);
382 void
383 run_test (struct wine_test* test, const char* subtest)
385 int status;
386 const char* file = get_test_source_file(test->name, subtest);
387 const char* rev = get_file_rev(file);
388 char *cmd = strmake (NULL, "%s %s", test->exename, subtest);
390 xprintf ("%s:%s start %s %s\n", test->name, subtest, file, rev);
391 status = run_ex (cmd, NULL, 120000);
392 free (cmd);
393 xprintf ("%s:%s done (%d)\n", test->name, subtest, status);
396 BOOL CALLBACK
397 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
398 LPTSTR lpszName, LONG_PTR lParam)
400 (*(int*)lParam)++;
401 return TRUE;
404 char *
405 run_tests (char *logname, const char *tag, const char *url)
407 int nr_of_files = 0, nr_of_tests = 0, i;
408 char *tempdir;
409 FILE *logfile;
410 char build_tag[128];
412 SetErrorMode (SEM_NOGPFAULTERRORBOX);
414 tempdir = tempnam (0, "wct");
415 if (!tempdir)
416 report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
417 report (R_DIR, tempdir);
418 if (!CreateDirectory (tempdir, NULL))
419 report (R_FATAL, "Could not create directory: %s", tempdir);
421 if (!logname) {
422 logname = tempnam (0, "res");
423 if (!logname) report (R_FATAL, "Can't name logfile.");
425 report (R_OUT, logname);
427 logfile = fopen (logname, "a");
428 if (!logfile) report (R_FATAL, "Could not open logfile.");
429 if (-1 == dup2 (fileno (logfile), 1))
430 report (R_FATAL, "Can't redirect stdout.");
431 fclose (logfile);
433 xprintf ("Version 3\n");
434 i = LoadStringA (GetModuleHandle (NULL), 0,
435 build_tag, sizeof build_tag);
436 if (i == 0) report (R_FATAL, "Build descriptor not found: %d",
437 GetLastError ());
438 if (i >= sizeof build_tag)
439 report (R_FATAL, "Build descriptor too long.");
440 xprintf ("Tests from build %s\n", build_tag);
441 xprintf ("Archive: %s\n", url?url:"");
442 xprintf ("Tag: %s\n", tag?tag:"");
443 xprintf ("Build info:\n");
444 xprintf ("Operating system version:\n");
445 print_version ();
446 xprintf ("Test output:\n" );
448 report (R_STATUS, "Counting tests");
449 if (!EnumResourceNames (NULL, TESTRESOURCE,
450 EnumTestFileProc, (LPARAM)&nr_of_files))
451 report (R_FATAL, "Can't enumerate test files: %d",
452 GetLastError ());
453 wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
455 report (R_STATUS, "Extracting tests");
456 report (R_PROGRESS, 0, nr_of_files);
457 for (i = 0; i < nr_of_files; i++) {
458 get_subtests (tempdir, wine_tests+i, i+1);
459 nr_of_tests += wine_tests[i].subtest_count;
461 report (R_DELTA, 0, "Extracting: Done");
463 report (R_STATUS, "Running tests");
464 report (R_PROGRESS, 1, nr_of_tests);
465 for (i = 0; i < nr_of_files; i++) {
466 struct wine_test *test = wine_tests + i;
467 int j;
469 for (j = 0; j < test->subtest_count; j++) {
470 report (R_STEP, "Running: %s:%s", test->name,
471 test->subtests[j]);
472 run_test (test, test->subtests[j]);
475 report (R_DELTA, 0, "Running: Done");
477 report (R_STATUS, "Cleaning up");
478 close (1);
479 remove_dir (tempdir);
480 free (tempdir);
481 free (wine_tests);
483 return logname;
486 void
487 usage ()
489 fprintf (stderr, "\
490 Usage: winetest [OPTION]...\n\n\
491 -c console mode, no GUI\n\
492 -h print this message and exit\n\
493 -q quiet mode, no output at all\n\
494 -o FILE put report into FILE, do not submit\n\
495 -s FILE submit FILE, do not run tests\n\
496 -t TAG include TAG of characters [-.0-9a-zA-Z] in the report\n\
497 -u URL archive URL of this executable\n");
500 /* One can't nest strtok()-s, so here is a replacement. */
501 char *
502 mystrtok (char *newstr)
504 static char *start, *end;
505 static int finish = 1;
507 if (newstr) {
508 start = newstr;
509 finish = 0;
510 } else start = end;
511 if (finish) return NULL;
512 while (*start == ' ') start++;
513 if (*start == 0) return NULL;
514 end = start;
515 while (*end != ' ')
516 if (*end == 0) {
517 finish = 1;
518 return start;
519 } else end++;
520 *end++ = 0;
521 return start;
524 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
525 LPSTR cmdLine, int cmdShow)
527 char *logname = NULL;
528 const char *cp, *submit = NULL, *tag = NULL, *url = NULL;
530 /* initialize the revision information first */
531 extract_rev_infos();
533 cmdLine = mystrtok (cmdLine);
534 while (cmdLine) {
535 if (cmdLine[0] != '-' || cmdLine[2]) {
536 report (R_ERROR, "Not a single letter option: %s", cmdLine);
537 usage ();
538 exit (2);
540 switch (cmdLine[1]) {
541 case 'c':
542 report (R_TEXTMODE);
543 break;
544 case 'h':
545 usage ();
546 exit (0);
547 case 'q':
548 report (R_QUIET);
549 break;
550 case 's':
551 submit = mystrtok (NULL);
552 if (tag||url)
553 report (R_WARNING, "ignoring tag and url for submit");
554 send_file (submit);
555 break;
556 case 'o':
557 logname = mystrtok (NULL);
558 run_tests (logname, tag, url);
559 break;
560 case 't':
561 tag = mystrtok (NULL);
562 cp = badtagchar (tag);
563 if (cp) {
564 report (R_ERROR, "invalid char in tag: %c", *cp);
565 usage ();
566 exit (2);
568 break;
569 case 'u':
570 url = mystrtok (NULL);
571 break;
572 default:
573 report (R_ERROR, "invalid option: -%c", cmdLine[1]);
574 usage ();
575 exit (2);
577 cmdLine = mystrtok (NULL);
579 if (!logname && !submit) {
580 report (R_STATUS, "Starting up");
581 logname = run_tests (NULL, tag, url);
582 if (report (R_ASK, MB_YESNO, "Do you want to submit the "
583 "test results?") == IDYES)
584 if (!send_file (logname) && remove (logname))
585 report (R_WARNING, "Can't remove logfile: %d.", errno);
586 free (logname);
587 report (R_STATUS, "Finished");
589 exit (0);