makefiles: Rename the SRCDIR, TOPSRCDIR and TOPOBJDIR variables to follow autoconf...
[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 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
29 {
30     SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE};
31     char command[] = "test.cmd";
32     STARTUPINFOA si = {sizeof(si)};
33     PROCESS_INFORMATION pi;
34     HANDLE file,fileerr;
35     DWORD size;
36     BOOL bres;
37
38     file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
39             FILE_ATTRIBUTE_NORMAL, NULL);
40     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
41     if(file == INVALID_HANDLE_VALUE)
42         return FALSE;
43
44     bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
45     CloseHandle(file);
46     ok(bres, "Could not write to file: %u\n", GetLastError());
47     if(!bres)
48         return FALSE;
49
50     file = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
51             FILE_ATTRIBUTE_NORMAL, NULL);
52     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
53     if(file == INVALID_HANDLE_VALUE)
54         return FALSE;
55
56     fileerr = CreateFileA("test.err", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
57             FILE_ATTRIBUTE_NORMAL, NULL);
58     ok(fileerr != INVALID_HANDLE_VALUE, "CreateFile stderr failed\n");
59     if(fileerr == INVALID_HANDLE_VALUE)
60         return FALSE;
61
62     si.dwFlags = STARTF_USESTDHANDLES;
63     si.hStdOutput = file;
64     si.hStdError = fileerr;
65     bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
66     ok(bres, "CreateProcess failed: %u\n", GetLastError());
67     if(!bres) {
68         DeleteFileA("test.out");
69         return FALSE;
70     }
71
72     WaitForSingleObject(pi.hProcess, INFINITE);
73     CloseHandle(pi.hThread);
74     CloseHandle(pi.hProcess);
75     CloseHandle(file);
76     CloseHandle(fileerr);
77     DeleteFileA("test.cmd");
78     return TRUE;
79 }
80
81 static DWORD map_file(const char *file_name, const char **ret)
82 {
83     HANDLE file, map;
84     DWORD size;
85
86     file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
87     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
88     if(file == INVALID_HANDLE_VALUE)
89         return 0;
90
91     size = GetFileSize(file, NULL);
92
93     map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
94     CloseHandle(file);
95     ok(map != NULL, "CreateFileMapping(%s) failed: %u\n", file_name, GetLastError());
96     if(!map)
97         return 0;
98
99     *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
100     ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
101     CloseHandle(map);
102     if(!*ret)
103         return 0;
104
105     return size;
106 }
107
108 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
109         const char *exp_end)
110 {
111     const char *out_ptr = out_line, *exp_ptr = exp_line;
112     const char *err = NULL;
113
114     static const char pwd_cmd[] = {'@','p','w','d','@'};
115     static const char todo_space_cmd[] = {'@','t','o','d','o','_','s','p','a','c','e','@'};
116     static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
117
118     while(exp_ptr < exp_end) {
119         if(*exp_ptr == '@') {
120             if(exp_ptr+sizeof(pwd_cmd) <= exp_end
121                     && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
122                 exp_ptr += sizeof(pwd_cmd);
123                 if(out_end-out_ptr < workdir_len
124                    || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
125                        workdir, workdir_len) != CSTR_EQUAL)) {
126                     err = out_ptr;
127                 }else {
128                     out_ptr += workdir_len;
129                     continue;
130                 }
131             }else if(exp_ptr+sizeof(todo_space_cmd) <= exp_end
132                     && !memcmp(exp_ptr, todo_space_cmd, sizeof(todo_space_cmd))) {
133                 exp_ptr += sizeof(todo_space_cmd);
134                 todo_wine ok(*out_ptr == ' ', "expected space\n");
135                 if(out_ptr < out_end && *out_ptr == ' ')
136                     out_ptr++;
137                 continue;
138             }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
139                      && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
140                 exp_ptr = exp_end;
141                 continue;
142             }
143         }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
144             err = out_ptr;
145         }
146
147         if(err) {
148             if(!broken(1))
149                 return err;
150
151             while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
152                 exp_ptr++;
153             if(!exp_ptr)
154                 return err;
155
156             exp_ptr += sizeof(or_broken_cmd);
157             out_ptr = out_line;
158             err = NULL;
159             continue;
160         }
161
162         exp_ptr++;
163         out_ptr++;
164     }
165
166     return exp_ptr == exp_end ? NULL : out_ptr;
167 }
168
169 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
170 {
171     const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err;
172     DWORD line = 0;
173
174     while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
175         line++;
176
177         for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
178         for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
179
180         err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
181         if(err == out_nl)
182             ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
183                line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
184         else if(err)
185             ok(0, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
186                *err, (int)(err-out_ptr), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
187
188         exp_ptr = exp_nl+1;
189         out_ptr = out_nl+1;
190         if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
191             out_ptr++;
192         if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
193             exp_ptr++;
194     }
195
196     ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, missing %s\n", line, exp_ptr);
197     ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
198 }
199
200 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
201 {
202     const char *out_data;
203     DWORD out_size;
204
205     if(!run_cmd(cmd_data, cmd_size))
206         return;
207
208     out_size = map_file("test.out", &out_data);
209     if(out_size) {
210         test_output(out_data, out_size, exp_data, exp_size);
211         UnmapViewOfFile(out_data);
212     }
213     DeleteFileA("test.out");
214     DeleteFileA("test.err");
215 }
216
217 static void run_from_file(char *file_name)
218 {
219     char out_name[MAX_PATH];
220     const char *test_data, *out_data;
221     DWORD test_size, out_size;
222
223     test_size = map_file(file_name, &test_data);
224     if(!test_size) {
225         ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
226         return;
227     }
228
229     sprintf(out_name, "%s.exp", file_name);
230     out_size = map_file(out_name, &out_data);
231     if(!out_size) {
232         ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
233         UnmapViewOfFile(test_data);
234         return;
235     }
236
237     run_test(test_data, test_size, out_data, out_size);
238
239     UnmapViewOfFile(test_data);
240     UnmapViewOfFile(out_data);
241 }
242
243 static DWORD load_resource(const char *name, const char *type, const char **ret)
244 {
245     const char *res;
246     HRSRC src;
247     DWORD size;
248
249     src = FindResourceA(NULL, name, type);
250     ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
251     if(!src)
252         return 0;
253
254     res = LoadResource(NULL, src);
255     size = SizeofResource(NULL, src);
256     while(size && !res[size-1])
257         size--;
258
259     *ret = res;
260     return size;
261 }
262
263 static BOOL WINAPI test_enum_proc(HMODULE module, LPCTSTR type, LPSTR name, LONG_PTR param)
264 {
265     const char *cmd_data, *out_data;
266     DWORD cmd_size, out_size;
267     char res_name[100];
268
269     trace("running %s test...\n", name);
270
271     cmd_size = load_resource(name, type, &cmd_data);
272     if(!cmd_size)
273         return TRUE;
274
275     sprintf(res_name, "%s.exp", name);
276     out_size = load_resource(res_name, "TESTOUT", &out_data);
277     if(!out_size)
278         return TRUE;
279
280     run_test(cmd_data, cmd_size, out_data, out_size);
281     return TRUE;
282 }
283
284 static int cmd_available(void)
285 {
286     STARTUPINFOA si;
287     PROCESS_INFORMATION pi;
288     char cmd[] = "cmd /c exit 0";
289
290     memset(&si, 0, sizeof(si));
291     si.cb = sizeof(si);
292     if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
293         CloseHandle(pi.hThread);
294         CloseHandle(pi.hProcess);
295         return TRUE;
296     }
297     return FALSE;
298 }
299
300 START_TEST(batch)
301 {
302     int argc;
303     char **argv;
304
305     if (!cmd_available()) {
306         win_skip("cmd not installed, skipping cmd tests\n");
307         return;
308     }
309
310     workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
311
312     argc = winetest_get_mainargs(&argv);
313     if(argc > 2)
314         run_from_file(argv[2]);
315     else
316         EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);
317 }