Implement conversions between dates and strings.
[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  *
21  * This program is dedicated to Anna Lindh,
22  * Swedish Minister of Foreign Affairs.
23  * Anna was murdered September 11, 2003.
24  *
25  */
26
27 #include "config.h"
28 #include "wine/port.h"
29
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>
37
38 #include "winetest.h"
39
40 struct wine_test
41 {
42     char *name;
43     int resource;
44     int subtest_count;
45     char **subtests;
46     int is_elf;
47     char *exename;
48 };
49
50 static struct wine_test wine_tests[32];
51
52 static const char *wineloader;
53
54 void print_version ()
55 {
56     OSVERSIONINFOEX ver;
57     BOOL ext;
58
59     ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
60     if (!(ext = GetVersionEx ((OSVERSIONINFO *) &ver)))
61     {
62         ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
63         if (!GetVersionEx ((OSVERSIONINFO *) &ver))
64             fatal("Can't get OS version.");
65     }
66
67     xprintf ("    dwMajorVersion=%ld\n    dwMinorVersion=%ld\n"
68              "    dwBuildNumber=%ld\n    PlatformId=%ld\n    szCSDVersion=%s\n",
69              ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber,
70              ver.dwPlatformId, ver.szCSDVersion);
71
72     if (!ext) return;
73
74     xprintf ("    wServicePackMajor=%d\n    wServicePackMinor=%d\n"
75              "    wSuiteMask=%d\n    wProductType=%d\n    wReserved=%d\n",
76              ver.wServicePackMajor, ver.wServicePackMinor, ver.wSuiteMask,
77              ver.wProductType, ver.wReserved);
78 }
79
80 static inline int is_dot_dir(const char* x)
81 {
82     return ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))));
83 }
84
85 void remove_dir (const char *dir)
86 {
87     HANDLE  hFind;
88     WIN32_FIND_DATA wfd;
89     char path[MAX_PATH];
90     size_t dirlen = strlen (dir);
91
92     /* Make sure the directory exists before going further */
93     memcpy (path, dir, dirlen);
94     strcpy (path + dirlen++, "\\*");
95     hFind = FindFirstFile (path, &wfd);
96     if (hFind == INVALID_HANDLE_VALUE) return;
97
98     do {
99         char *lp = wfd.cFileName;
100
101         if (!lp[0]) lp = wfd.cAlternateFileName; /* ? FIXME not (!lp) ? */
102         if (is_dot_dir (lp)) continue;
103         strcpy (path + dirlen, lp);
104         if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
105             remove_dir(path);
106         else if (!DeleteFile (path))
107             warning (strmake (NULL, "Can't delete file %s: error %d", path, GetLastError ()));
108     } while (FindNextFile (hFind, &wfd));
109     FindClose (hFind);
110     if (!RemoveDirectory (dir))
111         warning (strmake (NULL, "Can't remove directory %s: error %d", dir, GetLastError ()));
112 }
113
114 void* extract_rcdata (int id, DWORD* size)
115 {
116     HRSRC rsrc;
117     HGLOBAL hdl;
118
119     rsrc = FindResource (0, (LPTSTR)(id + 1), "USERDATA");
120     if (!rsrc) return 0;
121     *size = SizeofResource (0, rsrc);
122     if (!*size) return 0;
123     hdl = LoadResource (0, rsrc);
124     if (!hdl) return 0;
125     return LockResource (hdl);
126 }
127
128 int extract_test (const char *dir, int id)
129 {
130     BYTE* code;
131     DWORD size;
132     FILE* fout;
133     char buffer[128];
134     int len;
135     struct wine_test *test;
136
137     if (id >= sizeof(wine_tests)/sizeof(wine_tests[0])-1) fatal("Too many tests\n");
138
139     code = extract_rcdata (id, &size);
140     if (!code) return 0;
141
142     test = &wine_tests[id];
143     len = LoadStringA(0, id + 1, buffer, sizeof(buffer) );
144     test->name = xmalloc( len + 1 );
145     memcpy( test->name, buffer, len + 1 );
146     test->is_elf = (code[1] == 'E' && code[2] == 'L' && code[3] == 'F');
147     test->exename = strmake(NULL, "%s/%s", dir, test->name);
148
149     if (!(fout = fopen(test->exename, "wb")) ||
150         (fwrite (code, size, 1, fout) != 1) ||
151         fclose (fout)) fatal (strmake (NULL, "Failed to write file %s.", test->name));
152     return 1;
153 }
154
155 int get_subtests (struct wine_test tests[])
156 {
157     char *subname;
158     FILE *subfile;
159     size_t subsize, bytes_read, total;
160     char buffer[8000], *index;
161     const char header[] = "Valid test names:", seps[] = " \r\n";
162     int oldstdout;
163     const char *argv[] = {"wine", NULL, NULL};
164     struct wine_test* test;
165     int allocated, all_subtests = 0;
166
167     subname = tempnam (0, "sub");
168     if (!subname) fatal ("Can't name subtests file.");
169     oldstdout = dup (1);
170     if (-1 == oldstdout) fatal ("Can't preserve stdout.");
171     subfile = fopen (subname, "w+b");
172     if (!subfile) fatal ("Can't open subtests file.");
173     if (-1 == dup2 (fileno (subfile), 1))
174         fatal ("Can't redirect output to subtests.");
175     fclose (subfile);
176
177     for (test = tests; test->name; test++) {
178         lseek (1, 0, SEEK_SET);
179         argv[1] = test->exename;
180         if (test->is_elf)
181             spawnvp (_P_WAIT, wineloader, argv);
182         else
183             spawnvp (_P_WAIT, test->exename, argv+1);
184         subsize = lseek (1, 0, SEEK_CUR);
185         if (subsize >= sizeof buffer) {
186             fprintf (stderr, "Subtests output too big: %s.\n",
187                      test->name);
188             continue;
189         }
190
191         lseek (1, 0, SEEK_SET);
192         total = 0;
193         while ((bytes_read = read (1, buffer + total, subsize - total))
194                && (signed)bytes_read != -1)
195             total += bytes_read;
196         if (bytes_read) {
197             fprintf (stderr, "Error reading %s.\n", test->name);
198             continue;
199         }
200         buffer[total] = 0;
201         index = strstr (buffer, header);
202         if (!index) {
203             fprintf (stderr, "Can't parse subtests output of %s.\n",
204                      test->name);
205             continue;
206         }
207         index += sizeof(header);
208         allocated = 10;
209         test->subtests = xmalloc (allocated * sizeof (char*));
210         test->subtest_count = 0;
211         index = strtok (index, seps);
212         while (index) {
213             if (test->subtest_count == allocated) {
214                 allocated *= 2;
215                 test->subtests = xrealloc (test->subtests,
216                                            allocated * sizeof (char*));
217             }
218             test->subtests[test->subtest_count++] = strdup (index);
219             index = strtok (NULL, seps);
220         }
221         test->subtests = xrealloc (test->subtests,
222                                    test->subtest_count * sizeof (char*));
223         all_subtests += test->subtest_count;
224     }
225     close (1);
226
227     if (-1 == dup2 (oldstdout, 1)) fatal ("Can't recover old stdout.");
228     close (oldstdout);
229
230     if (remove (subname)) fatal ("Can't remove subtests file.");
231     free (subname);
232
233     return all_subtests;
234 }
235
236 void run_test (struct wine_test* test, const char* subtest)
237 {
238     int status;
239     const char *argv[] = {"wine", test->exename, subtest, NULL};
240
241     fprintf (stderr, "Running %s:%s\n", test->name, subtest);
242     xprintf ("%s:%s start\n", test->name, subtest);
243     if (test->is_elf)
244         status = spawnvp (_P_WAIT, wineloader, argv);
245     else
246         status = spawnvp (_P_WAIT, test->exename, argv+1);
247     if (status == -1)
248         xprintf ("Can't run: %d, errno=%d: %s\n", status, errno, strerror (errno));
249     xprintf ("%s:%s done (%x)\n", test->name, subtest, status);
250 }
251
252 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdline, int cmdshow)
253 {
254     struct wine_test* test;
255     int nr_of_tests, subtest, i;
256     char *tempdir, *logname;
257     FILE *logfile;
258     char build_tag[128];
259
260     SetErrorMode (SEM_FAILCRITICALERRORS);
261
262     if (!(wineloader = getenv("WINELOADER"))) wineloader = "wine";
263     if (setvbuf (stdout, NULL, _IONBF, 0)) fatal ("Can't unbuffer output.");
264
265     tempdir = tempnam (0, "wct");
266     if (!tempdir) fatal ("Can't name temporary dir (check TMP).");
267     fprintf (stderr, "tempdir=%s\n", tempdir);
268     if (!CreateDirectory (tempdir, NULL)) fatal (strmake (NULL, "Could not create directory: %s", tempdir));
269
270     logname = tempnam (0, "res");
271     if (!logname) fatal ("Can't name logfile.");
272     fprintf (stderr, "logname=%s\n", logname);
273
274     logfile = fopen (logname, "ab");
275     if (!logfile) fatal ("Could not open logfile.");
276     if (-1 == dup2 (fileno (logfile), 1)) fatal ("Can't redirect stdout.");
277     fclose (logfile);
278
279     LoadStringA( 0, 0, build_tag, sizeof(build_tag) );
280     xprintf ("Tests from build %s\n", build_tag);
281     xprintf ("Operating system version:\n");
282     print_version ();
283     xprintf ("Test output:\n" );
284
285     i = 0;
286     while (extract_test (tempdir, i)) i++;
287
288     nr_of_tests = get_subtests (wine_tests);
289
290     for (test = wine_tests; test->name; test++)
291         for (subtest = 0; subtest < test->subtest_count; subtest++)
292             run_test (test, test->subtests[subtest]);
293
294     close (1);
295
296     remove_dir (tempdir);
297
298     /* FIXME: add an explanation of what is going on */
299     if (MessageBoxA( 0, "Do you want to submit the test results?", "Confirmation",
300                      MB_YESNO | MB_ICONQUESTION ) == IDYES)
301     {
302         if (send_file (logname))
303             fatal ("Can't submit logfile (network of file error).");
304     }
305
306     if (remove (logname))
307         warning (strmake (NULL, "Can't remove logfile: %d.", errno));
308
309     return 0;
310 }