wscript: Fix tests on wow64.
[wine] / programs / cmd / tests / batch.c
1 /*
2  * Copyright 2009 Dan Kegel
3  * Copyright 2010 Jacek Caban for CodeWeavers
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <windows.h>
21 #include <stdio.h>
22
23 #include "wine/test.h"
24
25 static char workdir[MAX_PATH];
26 static DWORD workdir_len;
27
28 /* Convert to DOS line endings, and substitute escaped spaces with real ones */
29 static const char* convert_input_data(const char *data, DWORD size, DWORD *new_size)
30 {
31     static const char escaped_space[] = {'@','s','p','a','c','e','@'};
32     DWORD i, eol_count = 0;
33     char *ptr, *new_data;
34
35     for (i = 0; i < size; i++)
36         if (data[i] == '\n') eol_count++;
37
38     ptr = new_data = HeapAlloc(GetProcessHeap(), 0, size + eol_count + 1);
39
40     for (i = 0; i < size; i++) {
41         switch (data[i]) {
42             case '\n':
43                 *ptr++ = '\r';
44                 *ptr++ = '\n';
45                 break;
46             case '@':
47                 if (data + i + sizeof(escaped_space) - 1 < data + size
48                         && !memcmp(data + i, escaped_space, sizeof(escaped_space))) {
49                     *ptr++ = ' ';
50                     i += sizeof(escaped_space) - 1;
51                 } else {
52                     *ptr++ = data[i];
53                 }
54                 break;
55             default:
56                 *ptr++ = data[i];
57         }
58     }
59     *ptr = '\0';
60
61     *new_size = strlen(new_data);
62     return new_data;
63 }
64
65 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
66 {
67     SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE};
68     char command[] = "test.cmd";
69     STARTUPINFOA si = {sizeof(si)};
70     PROCESS_INFORMATION pi;
71     HANDLE file,fileerr;
72     DWORD size;
73     BOOL bres;
74
75     file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
76             FILE_ATTRIBUTE_NORMAL, NULL);
77     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
78     if(file == INVALID_HANDLE_VALUE)
79         return FALSE;
80
81     bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
82     CloseHandle(file);
83     ok(bres, "Could not write to file: %u\n", GetLastError());
84     if(!bres)
85         return FALSE;
86
87     file = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
88             FILE_ATTRIBUTE_NORMAL, NULL);
89     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
90     if(file == INVALID_HANDLE_VALUE)
91         return FALSE;
92
93     fileerr = CreateFileA("test.err", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
94             FILE_ATTRIBUTE_NORMAL, NULL);
95     ok(fileerr != INVALID_HANDLE_VALUE, "CreateFile stderr failed\n");
96     if(fileerr == INVALID_HANDLE_VALUE)
97         return FALSE;
98
99     si.dwFlags = STARTF_USESTDHANDLES;
100     si.hStdOutput = file;
101     si.hStdError = fileerr;
102     bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
103     ok(bres, "CreateProcess failed: %u\n", GetLastError());
104     if(!bres) {
105         DeleteFileA("test.out");
106         return FALSE;
107     }
108
109     WaitForSingleObject(pi.hProcess, INFINITE);
110     CloseHandle(pi.hThread);
111     CloseHandle(pi.hProcess);
112     CloseHandle(file);
113     CloseHandle(fileerr);
114     DeleteFileA("test.cmd");
115     return TRUE;
116 }
117
118 static DWORD map_file(const char *file_name, const char **ret)
119 {
120     HANDLE file, map;
121     DWORD size;
122
123     file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
124     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
125     if(file == INVALID_HANDLE_VALUE)
126         return 0;
127
128     size = GetFileSize(file, NULL);
129
130     map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
131     CloseHandle(file);
132     ok(map != NULL, "CreateFileMapping(%s) failed: %u\n", file_name, GetLastError());
133     if(!map)
134         return 0;
135
136     *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
137     ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
138     CloseHandle(map);
139     if(!*ret)
140         return 0;
141
142     return size;
143 }
144
145 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
146         const char *exp_end)
147 {
148     const char *out_ptr = out_line, *exp_ptr = exp_line;
149     const char *err = NULL;
150
151     static const char pwd_cmd[] = {'@','p','w','d','@'};
152     static const char space_cmd[] = {'@','s','p','a','c','e','@'};
153     static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
154
155     while(exp_ptr < exp_end) {
156         if(*exp_ptr == '@') {
157             if(exp_ptr+sizeof(pwd_cmd) <= exp_end
158                     && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
159                 exp_ptr += sizeof(pwd_cmd);
160                 if(out_end-out_ptr < workdir_len
161                    || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
162                        workdir, workdir_len) != CSTR_EQUAL)) {
163                     err = out_ptr;
164                 }else {
165                     out_ptr += workdir_len;
166                     continue;
167                 }
168             }else if(exp_ptr+sizeof(space_cmd) <= exp_end
169                     && !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
170                 exp_ptr += sizeof(space_cmd);
171                 if(out_ptr < out_end && *out_ptr == ' ')
172                     out_ptr++;
173                 continue;
174             }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
175                      && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
176                 if(out_ptr == out_end)
177                     return NULL;
178                 else
179                     err = out_ptr;
180             }else if(out_ptr == out_end || *out_ptr != *exp_ptr)
181                 err = out_ptr;
182         }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
183             err = out_ptr;
184         }
185
186         if(err) {
187             if(!broken(1))
188                 return err;
189
190             while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
191                 exp_ptr++;
192             if(!exp_ptr)
193                 return err;
194
195             exp_ptr += sizeof(or_broken_cmd);
196             out_ptr = out_line;
197             err = NULL;
198             continue;
199         }
200
201         exp_ptr++;
202         out_ptr++;
203     }
204
205     if(exp_ptr != exp_end)
206         return out_ptr;
207     else if(out_ptr != out_end)
208         return exp_end;
209
210     return NULL;
211 }
212
213 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
214 {
215     const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err;
216     DWORD line = 0;
217     static const char todo_wine_cmd[] = {'@','t','o','d','o','_','w','i','n','e','@'};
218     BOOL is_todo_wine;
219
220     while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
221         line++;
222
223         for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
224         for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
225
226         is_todo_wine = (exp_ptr+sizeof(todo_wine_cmd) <= exp_nl &&
227                         !memcmp(exp_ptr, todo_wine_cmd, sizeof(todo_wine_cmd)));
228         if (is_todo_wine) {
229             exp_ptr += sizeof(todo_wine_cmd);
230             winetest_start_todo("wine");
231         }
232
233         err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
234         if(err == out_nl)
235             ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
236                line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
237         else if(err == exp_nl)
238             ok(0, "excess characters on line %d (got '%.*s', wanted '%.*s')\n",
239                line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
240         else
241             ok(!err, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
242                (err ? *err : 0), (err ? (int)(err-out_ptr) : -1), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
243
244         if(is_todo_wine) winetest_end_todo("wine");
245
246         exp_ptr = exp_nl+1;
247         out_ptr = out_nl+1;
248         if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
249             out_ptr++;
250         if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
251             exp_ptr++;
252     }
253
254     ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, missing %s\n", line, exp_ptr);
255     ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
256 }
257
258 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
259 {
260     const char *out_data, *actual_cmd_data;
261     DWORD out_size, actual_cmd_size;
262
263     actual_cmd_data = convert_input_data(cmd_data, cmd_size, &actual_cmd_size);
264     if(!actual_cmd_size || !actual_cmd_data)
265         goto cleanup;
266
267     if(!run_cmd(actual_cmd_data, actual_cmd_size))
268         goto cleanup;
269
270     out_size = map_file("test.out", &out_data);
271     if(out_size) {
272         test_output(out_data, out_size, exp_data, exp_size);
273         UnmapViewOfFile(out_data);
274     }
275     DeleteFileA("test.out");
276     DeleteFileA("test.err");
277
278 cleanup:
279     HeapFree(GetProcessHeap(), 0, (LPVOID)actual_cmd_data);
280 }
281
282 static void run_from_file(char *file_name)
283 {
284     char out_name[MAX_PATH];
285     const char *test_data, *out_data;
286     DWORD test_size, out_size;
287
288     test_size = map_file(file_name, &test_data);
289     if(!test_size) {
290         ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
291         return;
292     }
293
294     sprintf(out_name, "%s.exp", file_name);
295     out_size = map_file(out_name, &out_data);
296     if(!out_size) {
297         ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
298         UnmapViewOfFile(test_data);
299         return;
300     }
301
302     run_test(test_data, test_size, out_data, out_size);
303
304     UnmapViewOfFile(test_data);
305     UnmapViewOfFile(out_data);
306 }
307
308 static DWORD load_resource(const char *name, const char *type, const char **ret)
309 {
310     const char *res;
311     HRSRC src;
312     DWORD size;
313
314     src = FindResourceA(NULL, name, type);
315     ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
316     if(!src)
317         return 0;
318
319     res = LoadResource(NULL, src);
320     size = SizeofResource(NULL, src);
321     while(size && !res[size-1])
322         size--;
323
324     *ret = res;
325     return size;
326 }
327
328 static BOOL WINAPI test_enum_proc(HMODULE module, LPCTSTR type, LPSTR name, LONG_PTR param)
329 {
330     const char *cmd_data, *out_data;
331     DWORD cmd_size, out_size;
332     char res_name[100];
333
334     trace("running %s test...\n", name);
335
336     cmd_size = load_resource(name, type, &cmd_data);
337     if(!cmd_size)
338         return TRUE;
339
340     sprintf(res_name, "%s.exp", name);
341     out_size = load_resource(res_name, "TESTOUT", &out_data);
342     if(!out_size)
343         return TRUE;
344
345     run_test(cmd_data, cmd_size, out_data, out_size);
346     return TRUE;
347 }
348
349 static int cmd_available(void)
350 {
351     STARTUPINFOA si;
352     PROCESS_INFORMATION pi;
353     char cmd[] = "cmd /c exit 0";
354
355     memset(&si, 0, sizeof(si));
356     si.cb = sizeof(si);
357     if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
358         CloseHandle(pi.hThread);
359         CloseHandle(pi.hProcess);
360         return TRUE;
361     }
362     return FALSE;
363 }
364
365 START_TEST(batch)
366 {
367     int argc;
368     char **argv;
369
370     if (!cmd_available()) {
371         win_skip("cmd not installed, skipping cmd tests\n");
372         return;
373     }
374
375     workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
376
377     argc = winetest_get_mainargs(&argv);
378     if(argc > 2)
379         run_from_file(argv[2]);
380     else
381         EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);
382 }