cmd/tests: Add support for todo_wine constructions.
[wine.git] / programs / cmd / tests / batch.c
blob03c2b568c0ceb311028997d4c9daad29fd8e7c75
1 /*
2 * Copyright 2009 Dan Kegel
3 * Copyright 2010 Jacek Caban for CodeWeavers
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.
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.
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
20 #include <windows.h>
21 #include <stdio.h>
23 #include "wine/test.h"
25 static char workdir[MAX_PATH];
26 static DWORD workdir_len;
28 /* Substitute escaped spaces with real ones */
29 static const char* replace_escaped_spaces(const char *data, DWORD size, DWORD *new_size)
31 static const char escaped_space[] = {'@','s','p','a','c','e','@','\0'};
32 char *ptr, *new_data;
34 new_data = ptr = HeapAlloc(GetProcessHeap(), 0, size + 1);
35 memcpy( new_data, data, size );
36 new_data[size] = 0;
38 while ((ptr = strstr(ptr, escaped_space)))
40 char *end = ptr + sizeof(escaped_space) - 1;
41 *ptr++ = ' ';
42 memmove( ptr, end, strlen(end) + 1 );
44 *new_size = strlen(new_data);
45 return new_data;
48 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
50 SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE};
51 char command[] = "test.cmd";
52 STARTUPINFOA si = {sizeof(si)};
53 PROCESS_INFORMATION pi;
54 HANDLE file,fileerr;
55 DWORD size;
56 BOOL bres;
58 file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
59 FILE_ATTRIBUTE_NORMAL, NULL);
60 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
61 if(file == INVALID_HANDLE_VALUE)
62 return FALSE;
64 bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
65 CloseHandle(file);
66 ok(bres, "Could not write to file: %u\n", GetLastError());
67 if(!bres)
68 return FALSE;
70 file = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
71 FILE_ATTRIBUTE_NORMAL, NULL);
72 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
73 if(file == INVALID_HANDLE_VALUE)
74 return FALSE;
76 fileerr = CreateFileA("test.err", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
77 FILE_ATTRIBUTE_NORMAL, NULL);
78 ok(fileerr != INVALID_HANDLE_VALUE, "CreateFile stderr failed\n");
79 if(fileerr == INVALID_HANDLE_VALUE)
80 return FALSE;
82 si.dwFlags = STARTF_USESTDHANDLES;
83 si.hStdOutput = file;
84 si.hStdError = fileerr;
85 bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
86 ok(bres, "CreateProcess failed: %u\n", GetLastError());
87 if(!bres) {
88 DeleteFileA("test.out");
89 return FALSE;
92 WaitForSingleObject(pi.hProcess, INFINITE);
93 CloseHandle(pi.hThread);
94 CloseHandle(pi.hProcess);
95 CloseHandle(file);
96 CloseHandle(fileerr);
97 DeleteFileA("test.cmd");
98 return TRUE;
101 static DWORD map_file(const char *file_name, const char **ret)
103 HANDLE file, map;
104 DWORD size;
106 file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
107 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
108 if(file == INVALID_HANDLE_VALUE)
109 return 0;
111 size = GetFileSize(file, NULL);
113 map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
114 CloseHandle(file);
115 ok(map != NULL, "CreateFileMapping(%s) failed: %u\n", file_name, GetLastError());
116 if(!map)
117 return 0;
119 *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
120 ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
121 CloseHandle(map);
122 if(!*ret)
123 return 0;
125 return size;
128 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
129 const char *exp_end)
131 const char *out_ptr = out_line, *exp_ptr = exp_line;
132 const char *err = NULL;
134 static const char pwd_cmd[] = {'@','p','w','d','@'};
135 static const char space_cmd[] = {'@','s','p','a','c','e','@'};
136 static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
138 while(exp_ptr < exp_end) {
139 if(*exp_ptr == '@') {
140 if(exp_ptr+sizeof(pwd_cmd) <= exp_end
141 && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
142 exp_ptr += sizeof(pwd_cmd);
143 if(out_end-out_ptr < workdir_len
144 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
145 workdir, workdir_len) != CSTR_EQUAL)) {
146 err = out_ptr;
147 }else {
148 out_ptr += workdir_len;
149 continue;
151 }else if(exp_ptr+sizeof(space_cmd) <= exp_end
152 && !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
153 exp_ptr += sizeof(space_cmd);
154 ok(*out_ptr == ' ', "expected space\n");
155 if(out_ptr < out_end && *out_ptr == ' ')
156 out_ptr++;
157 continue;
158 }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
159 && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
160 exp_ptr = exp_end;
161 continue;
163 }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
164 err = out_ptr;
167 if(err) {
168 if(!broken(1))
169 return err;
171 while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
172 exp_ptr++;
173 if(!exp_ptr)
174 return err;
176 exp_ptr += sizeof(or_broken_cmd);
177 out_ptr = out_line;
178 err = NULL;
179 continue;
182 exp_ptr++;
183 out_ptr++;
186 return exp_ptr == exp_end ? NULL : out_ptr;
189 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
191 const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err;
192 DWORD line = 0;
193 static const char todo_wine_cmd[] = {'@','t','o','d','o','_','w','i','n','e','@'};
194 BOOL is_todo_wine;
196 while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
197 line++;
199 for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
200 for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
202 is_todo_wine = (exp_ptr+sizeof(todo_wine_cmd) <= exp_nl &&
203 !memcmp(exp_ptr, todo_wine_cmd, sizeof(todo_wine_cmd)));
204 if (is_todo_wine) {
205 exp_ptr += sizeof(todo_wine_cmd);
206 winetest_start_todo("wine");
209 err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
210 if(err == out_nl)
211 ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
212 line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
213 else
214 ok(!err, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
215 (err ? *err : 0), (err ? (int)(err-out_ptr) : -1), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
217 if(is_todo_wine) winetest_end_todo("wine");
219 exp_ptr = exp_nl+1;
220 out_ptr = out_nl+1;
221 if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
222 out_ptr++;
223 if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
224 exp_ptr++;
227 ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, missing %s\n", line, exp_ptr);
228 ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
231 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
233 const char *out_data, *actual_cmd_data;
234 DWORD out_size, actual_cmd_size;
236 actual_cmd_data = replace_escaped_spaces(cmd_data, cmd_size, &actual_cmd_size);
237 if(!actual_cmd_size || !actual_cmd_data)
238 goto cleanup;
240 if(!run_cmd(actual_cmd_data, actual_cmd_size))
241 goto cleanup;
243 out_size = map_file("test.out", &out_data);
244 if(out_size) {
245 test_output(out_data, out_size, exp_data, exp_size);
246 UnmapViewOfFile(out_data);
248 DeleteFileA("test.out");
249 DeleteFileA("test.err");
251 cleanup:
252 HeapFree(GetProcessHeap(), 0, (LPVOID)actual_cmd_data);
255 static void run_from_file(char *file_name)
257 char out_name[MAX_PATH];
258 const char *test_data, *out_data;
259 DWORD test_size, out_size;
261 test_size = map_file(file_name, &test_data);
262 if(!test_size) {
263 ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
264 return;
267 sprintf(out_name, "%s.exp", file_name);
268 out_size = map_file(out_name, &out_data);
269 if(!out_size) {
270 ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
271 UnmapViewOfFile(test_data);
272 return;
275 run_test(test_data, test_size, out_data, out_size);
277 UnmapViewOfFile(test_data);
278 UnmapViewOfFile(out_data);
281 static DWORD load_resource(const char *name, const char *type, const char **ret)
283 const char *res;
284 HRSRC src;
285 DWORD size;
287 src = FindResourceA(NULL, name, type);
288 ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
289 if(!src)
290 return 0;
292 res = LoadResource(NULL, src);
293 size = SizeofResource(NULL, src);
294 while(size && !res[size-1])
295 size--;
297 *ret = res;
298 return size;
301 static BOOL WINAPI test_enum_proc(HMODULE module, LPCTSTR type, LPSTR name, LONG_PTR param)
303 const char *cmd_data, *out_data;
304 DWORD cmd_size, out_size;
305 char res_name[100];
307 trace("running %s test...\n", name);
309 cmd_size = load_resource(name, type, &cmd_data);
310 if(!cmd_size)
311 return TRUE;
313 sprintf(res_name, "%s.exp", name);
314 out_size = load_resource(res_name, "TESTOUT", &out_data);
315 if(!out_size)
316 return TRUE;
318 run_test(cmd_data, cmd_size, out_data, out_size);
319 return TRUE;
322 static int cmd_available(void)
324 STARTUPINFOA si;
325 PROCESS_INFORMATION pi;
326 char cmd[] = "cmd /c exit 0";
328 memset(&si, 0, sizeof(si));
329 si.cb = sizeof(si);
330 if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
331 CloseHandle(pi.hThread);
332 CloseHandle(pi.hProcess);
333 return TRUE;
335 return FALSE;
338 START_TEST(batch)
340 int argc;
341 char **argv;
343 if (!cmd_available()) {
344 win_skip("cmd not installed, skipping cmd tests\n");
345 return;
348 workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
350 argc = winetest_get_mainargs(&argv);
351 if(argc > 2)
352 run_from_file(argv[2]);
353 else
354 EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);