Release 1.3.7.
[wine/gsoc-2012-control.git] / programs / cmd / tests / batch.c
blob0f01dbf6e2dadc76d018263766ad51b7f7951b5e
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 const char *a, *b;
33 char *new_data;
34 DWORD len_space = sizeof(escaped_space) -1;
36 a = b = data;
37 *new_size = 0;
39 new_data = HeapAlloc(GetProcessHeap(), 0, size*sizeof(char));
40 ok(new_data != NULL, "HeapAlloc failed\n");
41 if(!new_data)
42 return NULL;
44 while( (b = strstr(a, escaped_space)) )
46 strncpy(new_data + *new_size, a, b-a + 1);
47 *new_size += b-a + 1;
48 new_data[*new_size - 1] = ' ';
49 a = b + len_space;
52 strncpy(new_data + *new_size, a, strlen(a) + 1);
53 *new_size += strlen(a);
55 return new_data;
58 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
60 SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE};
61 char command[] = "test.cmd";
62 STARTUPINFOA si = {sizeof(si)};
63 PROCESS_INFORMATION pi;
64 HANDLE file,fileerr;
65 DWORD size;
66 BOOL bres;
68 file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
69 FILE_ATTRIBUTE_NORMAL, NULL);
70 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
71 if(file == INVALID_HANDLE_VALUE)
72 return FALSE;
74 bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
75 CloseHandle(file);
76 ok(bres, "Could not write to file: %u\n", GetLastError());
77 if(!bres)
78 return FALSE;
80 file = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
81 FILE_ATTRIBUTE_NORMAL, NULL);
82 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
83 if(file == INVALID_HANDLE_VALUE)
84 return FALSE;
86 fileerr = CreateFileA("test.err", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
87 FILE_ATTRIBUTE_NORMAL, NULL);
88 ok(fileerr != INVALID_HANDLE_VALUE, "CreateFile stderr failed\n");
89 if(fileerr == INVALID_HANDLE_VALUE)
90 return FALSE;
92 si.dwFlags = STARTF_USESTDHANDLES;
93 si.hStdOutput = file;
94 si.hStdError = fileerr;
95 bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
96 ok(bres, "CreateProcess failed: %u\n", GetLastError());
97 if(!bres) {
98 DeleteFileA("test.out");
99 return FALSE;
102 WaitForSingleObject(pi.hProcess, INFINITE);
103 CloseHandle(pi.hThread);
104 CloseHandle(pi.hProcess);
105 CloseHandle(file);
106 CloseHandle(fileerr);
107 DeleteFileA("test.cmd");
108 return TRUE;
111 static DWORD map_file(const char *file_name, const char **ret)
113 HANDLE file, map;
114 DWORD size;
116 file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
117 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
118 if(file == INVALID_HANDLE_VALUE)
119 return 0;
121 size = GetFileSize(file, NULL);
123 map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
124 CloseHandle(file);
125 ok(map != NULL, "CreateFileMapping(%s) failed: %u\n", file_name, GetLastError());
126 if(!map)
127 return 0;
129 *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
130 ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
131 CloseHandle(map);
132 if(!*ret)
133 return 0;
135 return size;
138 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
139 const char *exp_end)
141 const char *out_ptr = out_line, *exp_ptr = exp_line;
142 const char *err = NULL;
144 static const char pwd_cmd[] = {'@','p','w','d','@'};
145 static const char todo_space_cmd[] = {'@','t','o','d','o','_','s','p','a','c','e','@'};
146 static const char space_cmd[] = {'@','s','p','a','c','e','@'};
147 static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
149 while(exp_ptr < exp_end) {
150 if(*exp_ptr == '@') {
151 if(exp_ptr+sizeof(pwd_cmd) <= exp_end
152 && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
153 exp_ptr += sizeof(pwd_cmd);
154 if(out_end-out_ptr < workdir_len
155 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
156 workdir, workdir_len) != CSTR_EQUAL)) {
157 err = out_ptr;
158 }else {
159 out_ptr += workdir_len;
160 continue;
162 }else if(exp_ptr+sizeof(todo_space_cmd) <= exp_end
163 && !memcmp(exp_ptr, todo_space_cmd, sizeof(todo_space_cmd))) {
164 exp_ptr += sizeof(todo_space_cmd);
165 todo_wine ok(*out_ptr == ' ', "expected space\n");
166 if(out_ptr < out_end && *out_ptr == ' ')
167 out_ptr++;
168 continue;
169 }else if(exp_ptr+sizeof(space_cmd) <= exp_end
170 && !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
171 exp_ptr += sizeof(space_cmd);
172 ok(*out_ptr == ' ', "expected space\n");
173 if(out_ptr < out_end && *out_ptr == ' ')
174 out_ptr++;
175 continue;
176 }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
177 && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
178 exp_ptr = exp_end;
179 continue;
181 }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
182 err = out_ptr;
185 if(err) {
186 if(!broken(1))
187 return err;
189 while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
190 exp_ptr++;
191 if(!exp_ptr)
192 return err;
194 exp_ptr += sizeof(or_broken_cmd);
195 out_ptr = out_line;
196 err = NULL;
197 continue;
200 exp_ptr++;
201 out_ptr++;
204 return exp_ptr == exp_end ? NULL : out_ptr;
207 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
209 const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err;
210 DWORD line = 0;
212 while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
213 line++;
215 for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
216 for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
218 err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
219 if(err == out_nl)
220 ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
221 line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
222 else if(err)
223 ok(0, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
224 *err, (int)(err-out_ptr), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
226 exp_ptr = exp_nl+1;
227 out_ptr = out_nl+1;
228 if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
229 out_ptr++;
230 if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
231 exp_ptr++;
234 ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, missing %s\n", line, exp_ptr);
235 ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
238 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
240 const char *out_data, *actual_cmd_data;
241 DWORD out_size, actual_cmd_size;
243 actual_cmd_data = replace_escaped_spaces(cmd_data, cmd_size, &actual_cmd_size);
244 if(!actual_cmd_size || !actual_cmd_data)
245 goto cleanup;
247 if(!run_cmd(actual_cmd_data, actual_cmd_size))
248 goto cleanup;
250 out_size = map_file("test.out", &out_data);
251 if(out_size) {
252 test_output(out_data, out_size, exp_data, exp_size);
253 UnmapViewOfFile(out_data);
255 DeleteFileA("test.out");
256 DeleteFileA("test.err");
258 cleanup:
259 HeapFree(GetProcessHeap(), 0, (LPVOID)actual_cmd_data);
262 static void run_from_file(char *file_name)
264 char out_name[MAX_PATH];
265 const char *test_data, *out_data;
266 DWORD test_size, out_size;
268 test_size = map_file(file_name, &test_data);
269 if(!test_size) {
270 ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
271 return;
274 sprintf(out_name, "%s.exp", file_name);
275 out_size = map_file(out_name, &out_data);
276 if(!out_size) {
277 ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
278 UnmapViewOfFile(test_data);
279 return;
282 run_test(test_data, test_size, out_data, out_size);
284 UnmapViewOfFile(test_data);
285 UnmapViewOfFile(out_data);
288 static DWORD load_resource(const char *name, const char *type, const char **ret)
290 const char *res;
291 HRSRC src;
292 DWORD size;
294 src = FindResourceA(NULL, name, type);
295 ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
296 if(!src)
297 return 0;
299 res = LoadResource(NULL, src);
300 size = SizeofResource(NULL, src);
301 while(size && !res[size-1])
302 size--;
304 *ret = res;
305 return size;
308 static BOOL WINAPI test_enum_proc(HMODULE module, LPCTSTR type, LPSTR name, LONG_PTR param)
310 const char *cmd_data, *out_data;
311 DWORD cmd_size, out_size;
312 char res_name[100];
314 trace("running %s test...\n", name);
316 cmd_size = load_resource(name, type, &cmd_data);
317 if(!cmd_size)
318 return TRUE;
320 sprintf(res_name, "%s.exp", name);
321 out_size = load_resource(res_name, "TESTOUT", &out_data);
322 if(!out_size)
323 return TRUE;
325 run_test(cmd_data, cmd_size, out_data, out_size);
326 return TRUE;
329 static int cmd_available(void)
331 STARTUPINFOA si;
332 PROCESS_INFORMATION pi;
333 char cmd[] = "cmd /c exit 0";
335 memset(&si, 0, sizeof(si));
336 si.cb = sizeof(si);
337 if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
338 CloseHandle(pi.hThread);
339 CloseHandle(pi.hProcess);
340 return TRUE;
342 return FALSE;
345 START_TEST(batch)
347 int argc;
348 char **argv;
350 if (!cmd_available()) {
351 win_skip("cmd not installed, skipping cmd tests\n");
352 return;
355 workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
357 argc = winetest_get_mainargs(&argv);
358 if(argc > 2)
359 run_from_file(argv[2]);
360 else
361 EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);