- add stubbed support for IEnumScript interface
[wine] / programs / winetest / main.c
1 /*
2  * Wine Conformance Test EXE
3  *
4  * Copyright 2003 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 <errno.h>
34 #ifdef HAVE_UNISTD_H
35 #  include <unistd.h>
36 #endif
37 #include <windows.h>
38
39 #include "winetest.h"
40 #include "resource.h"
41
42 struct wine_test
43 {
44     char *name;
45     int resource;
46     int subtest_count;
47     char **subtests;
48     char *exename;
49 };
50
51 struct rev_info
52 {
53     const char* file;
54     const char* rev;
55 };
56
57 static struct wine_test *wine_tests;
58 static struct rev_info *rev_infos = NULL;
59
60 static int running_under_wine ()
61 {
62     HMODULE module = GetModuleHandleA("ntdll.dll");
63
64     if (!module) return 0;
65     return (GetProcAddress(module, "wine_server_call") != NULL);
66 }
67
68 void print_version ()
69 {
70     OSVERSIONINFOEX ver;
71     BOOL ext;
72
73     ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
74     if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
75     {
76         ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
77         if (!GetVersionEx ((OSVERSIONINFO *) &ver))
78             report (R_FATAL, "Can't get OS version.");
79     }
80
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);
86
87     if (!ext) return;
88
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);
93 }
94
95 static inline int is_dot_dir(const char* x)
96 {
97     return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
98 }
99
100 void remove_dir (const char *dir)
101 {
102     HANDLE  hFind;
103     WIN32_FIND_DATA wfd;
104     char path[MAX_PATH];
105     size_t dirlen = strlen (dir);
106
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;
112
113     do {
114         char *lp = wfd.cFileName;
115
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 ());
129 }
130
131 const char* get_test_source_file(const char* test, const char* subtest)
132 {
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 }
138     };
139     static char buffer[MAX_PATH];
140     int i;
141
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;
146         }
147     }
148
149     snprintf(buffer, sizeof(buffer), "dlls/%s/tests/%s.c", test, subtest);
150     return buffer;
151 }
152
153 const char* get_file_rev(const char* file)
154 {
155     const struct rev_info* rev;
156  
157     for(rev = rev_infos; rev->file; rev++) {
158         if (strcmp(rev->file, file) == 0) return rev->rev;
159     }
160
161     return "-";
162 }
163
164 void extract_rev_infos ()
165 {
166     char revinfo[256], *p;
167     int size = 0, i, len;
168     HMODULE module = GetModuleHandle (NULL);
169
170     for (i = 0; TRUE; i++) {
171         if (i >= size) {
172             size += 100;
173             rev_infos = xrealloc (rev_infos, size * sizeof (*rev_infos));
174         }
175         memset(rev_infos + i, 0, sizeof(rev_infos[i]));
176
177         len = LoadStringA (module, REV_INFO+i, 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);
186     }
187 }
188
189 void* extract_rcdata (int id, int type, DWORD* size)
190 {
191     HRSRC rsrc;
192     HGLOBAL hdl;
193     LPVOID addr;
194     
195     if (!(rsrc = FindResource (NULL, (LPTSTR)id, MAKEINTRESOURCE(type))) ||
196         !(*size = SizeofResource (0, rsrc)) ||
197         !(hdl = LoadResource (0, rsrc)) ||
198         !(addr = LockResource (hdl)))
199         return NULL;
200     return addr;
201 }
202
203 /* Fills in the name and exename fields */
204 void
205 extract_test (struct wine_test *test, const char *dir, int id)
206 {
207     BYTE* code;
208     DWORD size;
209     FILE* fout;
210     int strlen, bufflen = 128;
211     char *exepos;
212
213     code = extract_rcdata (id, TESTRES, &size);
214     if (!code) report (R_FATAL, "Can't find test resource %d: %d",
215                        id, GetLastError ());
216     test->name = xmalloc (bufflen);
217     while ((strlen = LoadStringA (NULL, id, test->name, bufflen))
218            == bufflen - 1) {
219         bufflen *= 2;
220         test->name = xrealloc (test->name, bufflen);
221     }
222     if (!strlen) report (R_FATAL, "Can't read name of test %d.", id);
223     test->exename = strmake (NULL, "%s/%s", dir, test->name);
224     exepos = strstr (test->name, "_test.exe");
225     if (!exepos) report (R_FATAL, "Not an .exe file: %s", test->name);
226     *exepos = 0;
227     test->name = xrealloc (test->name, exepos - test->name + 1);
228     report (R_STEP, "Extracting: %s", test->name);
229
230     if (!(fout = fopen (test->exename, "wb")) ||
231         (fwrite (code, size, 1, fout) != 1) ||
232         fclose (fout)) report (R_FATAL, "Failed to write file %s.",
233                                test->exename);
234 }
235
236 /* Run a command for MS milliseconds.  If OUT != NULL, also redirect
237    stdout to there.
238
239    Return the exit status, -2 if can't create process or the return
240    value of WaitForSingleObject.
241  */
242 int
243 run_ex (char *cmd, const char *out, DWORD ms)
244 {
245     STARTUPINFO si;
246     PROCESS_INFORMATION pi;
247     int fd, oldstdout = -1;
248     DWORD wait, status;
249
250     GetStartupInfo (&si);
251     si.wShowWindow = SW_HIDE;
252     si.dwFlags = STARTF_USESHOWWINDOW;
253
254     if (out) {
255         fd = open (out, O_WRONLY | O_CREAT, 0666);
256         if (-1 == fd)
257             report (R_FATAL, "Can't open '%s': %d", out, errno);
258         oldstdout = dup (1);
259         if (-1 == oldstdout)
260             report (R_FATAL, "Can't save stdout: %d", errno);
261         if (-1 == dup2 (fd, 1))
262             report (R_FATAL, "Can't redirect stdout: %d", errno);
263         close (fd);
264     }
265
266     if (!CreateProcessA (NULL, cmd, NULL, NULL, TRUE, 0,
267                          NULL, NULL, &si, &pi)) {
268         status = -2;
269     } else {
270         CloseHandle (pi.hThread);
271         wait = WaitForSingleObject (pi.hProcess, ms);
272         if (wait == WAIT_OBJECT_0) {
273             GetExitCodeProcess (pi.hProcess, &status);
274         } else {
275             switch (wait) {
276             case WAIT_FAILED:
277                 report (R_ERROR, "Wait for '%s' failed: %d", cmd,
278                         GetLastError ());
279                 break;
280             case WAIT_TIMEOUT:
281                 report (R_ERROR, "Process '%s' timed out.", cmd);
282                 break;
283             default:
284                 report (R_ERROR, "Wait returned %d", wait);
285             }
286             status = wait;
287             if (!TerminateProcess (pi.hProcess, 257))
288                 report (R_ERROR, "TerminateProcess failed: %d",
289                         GetLastError ());
290             wait = WaitForSingleObject (pi.hProcess, 5000);
291             switch (wait) {
292             case WAIT_FAILED:
293                 report (R_ERROR,
294                         "Wait for termination of '%s' failed: %d",
295                         cmd, GetLastError ());
296                 break;
297             case WAIT_OBJECT_0:
298                 break;
299             case WAIT_TIMEOUT:
300                 report (R_ERROR, "Can't kill process '%s'", cmd);
301                 break;
302             default:
303                 report (R_ERROR, "Waiting for termination: %d",
304                         wait);
305             }
306         }
307         CloseHandle (pi.hProcess);
308     }
309
310     if (out) {
311         close (1);
312         if (-1 == dup2 (oldstdout, 1))
313             report (R_FATAL, "Can't recover stdout: %d", errno);
314         close (oldstdout);
315     }
316     return status;
317 }
318
319 void
320 get_subtests (const char *tempdir, struct wine_test *test, int id)
321 {
322     char *subname;
323     FILE *subfile;
324     size_t total;
325     char buffer[8192], *index;
326     static const char header[] = "Valid test names:";
327     static const char seps[] = " \r\n";
328     int allocated;
329
330     test->subtest_count = 0;
331
332     subname = tempnam (0, "sub");
333     if (!subname) report (R_FATAL, "Can't name subtests file.");
334
335     extract_test (test, tempdir, id);
336     run_ex (test->exename, subname, 5000);
337
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;
343     }
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;
350     }
351     buffer[total] = 0;
352
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;
358     }
359     index += sizeof header;
360
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*));
369         }
370         test->subtests[test->subtest_count++] = strdup (index);
371         index = strtok (NULL, seps);
372     }
373     test->subtests = xrealloc (test->subtests,
374                                test->subtest_count * sizeof(char*));
375
376  quit:
377     if (remove (subname))
378         report (R_WARNING, "Can't delete file '%s': %d",
379                 subname, errno);
380     free (subname);
381 }
382
383 void
384 run_test (struct wine_test* test, const char* subtest)
385 {
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);
390
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);
395 }
396
397 BOOL CALLBACK
398 EnumTestFileProc (HMODULE hModule, LPCTSTR lpszType,
399                   LPTSTR lpszName, LONG_PTR lParam)
400 {
401     (*(int*)lParam)++;
402     return TRUE;
403 }
404
405 char *
406 run_tests (char *logname, const char *tag)
407 {
408     int nr_of_files = 0, nr_of_tests = 0, i;
409     char *tempdir;
410     int logfile;
411     char *strres, *eol, *nextline;
412     DWORD strsize;
413
414     SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
415
416     if (!logname) {
417         logname = tempnam (0, "res");
418         if (!logname) report (R_FATAL, "Can't name logfile.");
419     }
420     report (R_OUT, logname);
421
422     logfile = open (logname, O_WRONLY | O_CREAT | O_EXCL | O_APPEND,
423                     0666);
424     if (-1 == logfile) {
425         if (EEXIST == errno)
426             report (R_FATAL, "File %s already exists.");
427         else report (R_FATAL, "Could not open logfile: %d", errno);
428     }
429     if (-1 == dup2 (logfile, 1))
430         report (R_FATAL, "Can't redirect stdout: %d", errno);
431     close (logfile);
432
433     tempdir = tempnam (0, "wct");
434     if (!tempdir)
435         report (R_FATAL, "Can't name temporary dir (check %%TEMP%%).");
436     report (R_DIR, tempdir);
437     if (!CreateDirectory (tempdir, NULL))
438         report (R_FATAL, "Could not create directory: %s", tempdir);
439
440     xprintf ("Version 3\n");
441     strres = extract_rcdata (WINE_BUILD, STRINGRES, &strsize);
442     xprintf ("Tests from build ");
443     if (strres) xprintf ("%.*s", strsize, strres);
444     else xprintf ("-\n");
445     strres = extract_rcdata (TESTS_URL, STRINGRES, &strsize);
446     xprintf ("Archive: ");
447     if (strres) xprintf ("%.*s", strsize, strres);
448     else xprintf ("-\n");
449     xprintf ("Tag: %s\n", tag?tag:"");
450     xprintf ("Build info:\n");
451     strres = extract_rcdata (BUILD_INFO, STRINGRES, &strsize);
452     while (strres) {
453         eol = memchr (strres, '\n', strsize);
454         if (!eol) {
455             nextline = NULL;
456             eol = strres + strsize;
457         } else {
458             strsize -= eol - strres + 1;
459             nextline = strsize?eol+1:NULL;
460             if (eol > strres && *(eol-1) == '\r') eol--;
461         }
462         xprintf ("    %.*s\n", eol-strres, strres);
463         strres = nextline;
464     }
465     xprintf ("Operating system version:\n");
466     print_version ();
467     xprintf ("Test output:\n" );
468
469     report (R_STATUS, "Counting tests");
470     if (!EnumResourceNames (NULL, MAKEINTRESOURCE(TESTRES),
471                             EnumTestFileProc, (LPARAM)&nr_of_files))
472         report (R_FATAL, "Can't enumerate test files: %d",
473                 GetLastError ());
474     wine_tests = xmalloc (nr_of_files * sizeof wine_tests[0]);
475
476     report (R_STATUS, "Extracting tests");
477     report (R_PROGRESS, 0, nr_of_files);
478     for (i = 0; i < nr_of_files; i++) {
479         get_subtests (tempdir, wine_tests+i, i);
480         nr_of_tests += wine_tests[i].subtest_count;
481     }
482     report (R_DELTA, 0, "Extracting: Done");
483
484     report (R_STATUS, "Running tests");
485     report (R_PROGRESS, 1, nr_of_tests);
486     for (i = 0; i < nr_of_files; i++) {
487         struct wine_test *test = wine_tests + i;
488         int j;
489
490         for (j = 0; j < test->subtest_count; j++) {
491             report (R_STEP, "Running: %s:%s", test->name,
492                     test->subtests[j]);
493             run_test (test, test->subtests[j]);
494         }
495     }
496     report (R_DELTA, 0, "Running: Done");
497
498     report (R_STATUS, "Cleaning up");
499     close (1);
500     remove_dir (tempdir);
501     free (tempdir);
502     free (wine_tests);
503
504     return logname;
505 }
506
507 void
508 usage ()
509 {
510     fprintf (stderr, "\
511 Usage: winetest [OPTION]...\n\n\
512   -c       console mode, no GUI\n\
513   -e       preserve the environment\n\
514   -h       print this message and exit\n\
515   -q       quiet mode, no output at all\n\
516   -o FILE  put report into FILE, do not submit\n\
517   -s FILE  submit FILE, do not run tests\n\
518   -t TAG   include TAG of characters [-.0-9a-zA-Z] in the report\n");
519 }
520
521 /* One can't nest strtok()-s, so here is a replacement. */
522 char *
523 mystrtok (char *newstr)
524 {
525     static char *start, *end;
526     static int finish = 1;
527
528     if (newstr) {
529         start = newstr;
530         finish = 0;
531     } else start = end;
532     if (finish) return NULL;
533     while (*start == ' ') start++;
534     if (*start == 0) return NULL;
535     end = start;
536     while (*end != ' ')
537         if (*end == 0) {
538             finish = 1;
539             return start;
540         } else end++;
541     *end++ = 0;
542     return start;
543 }
544
545 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
546                     LPSTR cmdLine, int cmdShow)
547 {
548     char *logname = NULL;
549     const char *cp, *submit = NULL, *tag = NULL;
550     int reset_env = 1;
551
552     /* initialize the revision information first */
553     extract_rev_infos();
554
555     cmdLine = mystrtok (cmdLine);
556     while (cmdLine) {
557         if (cmdLine[0] != '-' || cmdLine[2]) {
558             report (R_ERROR, "Not a single letter option: %s", cmdLine);
559             usage ();
560             exit (2);
561         }
562         switch (cmdLine[1]) {
563         case 'c':
564             report (R_TEXTMODE);
565             break;
566         case 'e':
567             reset_env = 0;
568             break;
569         case 'h':
570             usage ();
571             exit (0);
572         case 'q':
573             report (R_QUIET);
574             break;
575         case 's':
576             submit = mystrtok (NULL);
577             if (tag)
578                 report (R_WARNING, "ignoring tag for submission");
579             send_file (submit);
580             break;
581         case 'o':
582             logname = mystrtok (NULL);
583             break;
584         case 't':
585             tag = mystrtok (NULL);
586             cp = badtagchar (tag);
587             if (cp) {
588                 report (R_ERROR, "invalid char in tag: %c", *cp);
589                 usage ();
590                 exit (2);
591             }
592             break;
593         default:
594             report (R_ERROR, "invalid option: -%c", cmdLine[1]);
595             usage ();
596             exit (2);
597         }
598         cmdLine = mystrtok (NULL);
599     }
600     if (!submit) {
601         if (reset_env && (putenv ("WINETEST_PLATFORM=windows") ||
602                           putenv ("WINETEST_DEBUG=1") || 
603                           putenv ("WINETEST_INTERACTIVE=0") ||
604                           putenv ("WINETEST_REPORT_SUCCESS=0")))
605             report (R_FATAL, "Could not reset environment: %d", errno);
606
607         report (R_STATUS, "Starting up");
608         logname = run_tests (logname, tag);
609         if (report (R_ASK, MB_YESNO, "Do you want to submit the "
610                     "test results?") == IDYES)
611             if (!send_file (logname) && remove (logname))
612                 report (R_WARNING, "Can't remove logfile: %d.", errno);
613         free (logname);
614         report (R_STATUS, "Finished");
615     }
616     exit (0);
617 }