pop b0283e26815279d45d201d5585820bb1d1997663
[wine/hacks.git] / programs / cmd / tests / batch.c
blobe31fdc0d244519b89f8c9beb4b67e9b7e1e52435
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 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
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;
35 DWORD size;
36 BOOL bres;
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;
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;
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;
56 si.dwFlags = STARTF_USESTDHANDLES;
57 si.hStdOutput = file;
58 bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
59 ok(bres, "CreateProcess failed: %u\n", GetLastError());
60 if(!bres) {
61 DeleteFileA("test.out");
62 return FALSE;
65 WaitForSingleObject(pi.hProcess, INFINITE);
66 CloseHandle(pi.hThread);
67 CloseHandle(pi.hProcess);
68 CloseHandle(file);
69 DeleteFileA("test.cmd");
70 return TRUE;
73 static DWORD map_file(const char *file_name, const char **ret)
75 HANDLE file, map;
76 DWORD size;
78 file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
79 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
80 if(file == INVALID_HANDLE_VALUE)
81 return 0;
83 size = GetFileSize(file, NULL);
85 map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
86 CloseHandle(file);
87 ok(map != NULL, "CreateFileMapping(%s) failed: %u\n", file_name, GetLastError());
88 if(!map)
89 return 0;
91 *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
92 ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
93 CloseHandle(map);
94 if(!*ret)
95 return 0;
97 return size;
100 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
101 const char *exp_end)
103 const char *out_ptr = out_line, *exp_ptr = exp_line;
104 const char *err = NULL;
106 static const char pwd_cmd[] = {'@','p','w','d','@'};
107 static const char todo_space_cmd[] = {'@','t','o','d','o','_','s','p','a','c','e','@'};
108 static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
110 while(exp_ptr < exp_end) {
111 if(*exp_ptr == '@') {
112 if(exp_ptr+sizeof(pwd_cmd) <= exp_end
113 && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
114 exp_ptr += sizeof(pwd_cmd);
115 if(out_end-out_ptr < workdir_len
116 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
117 workdir, workdir_len) != CSTR_EQUAL)) {
118 err = out_ptr;
119 }else {
120 out_ptr += workdir_len;
121 continue;
123 }else if(exp_ptr+sizeof(todo_space_cmd) <= exp_end
124 && !memcmp(exp_ptr, todo_space_cmd, sizeof(todo_space_cmd))) {
125 exp_ptr += sizeof(todo_space_cmd);
126 todo_wine ok(*out_ptr == ' ', "expected space\n");
127 if(out_ptr < out_end && *out_ptr == ' ')
128 out_ptr++;
129 continue;
130 }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
131 && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
132 exp_ptr = exp_end;
133 continue;
135 }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
136 err = out_ptr;
139 if(err) {
140 if(!broken(1))
141 return err;
143 while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
144 exp_ptr++;
145 if(!exp_ptr)
146 return err;
148 exp_ptr += sizeof(or_broken_cmd);
149 out_ptr = out_line;
150 err = NULL;
151 continue;
154 exp_ptr++;
155 out_ptr++;
158 return exp_ptr == exp_end ? NULL : out_ptr;
161 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
163 const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err;
164 DWORD line = 0;
166 while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
167 line++;
169 for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
170 for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
172 err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
173 if(err == out_nl)
174 ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
175 line, out_nl-out_ptr, out_ptr, exp_nl-exp_ptr, exp_ptr);
176 else if(err)
177 ok(0, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
178 *err, err-out_ptr, line, out_nl-out_ptr, out_ptr, exp_nl-exp_ptr, exp_ptr);
180 exp_ptr = exp_nl+1;
181 out_ptr = out_nl+1;
182 if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
183 out_ptr++;
184 if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
185 exp_ptr++;
188 ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, expected 0x%x\n", line, *exp_ptr);
189 ok(out_ptr >= out_data+out_size, "too long output\n");
192 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
194 const char *out_data;
195 DWORD out_size;
197 if(!run_cmd(cmd_data, cmd_size))
198 return;
200 out_size = map_file("test.out", &out_data);
201 if(out_size) {
202 test_output(out_data, out_size, exp_data, exp_size);
203 UnmapViewOfFile(out_data);
205 DeleteFileA("test.out");
208 static void run_from_file(char *file_name)
210 char out_name[MAX_PATH];
211 const char *test_data, *out_data;
212 DWORD test_size, out_size;
214 test_size = map_file(file_name, &test_data);
215 if(!test_size) {
216 ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
217 return;
220 sprintf(out_name, "%s.exp", file_name);
221 out_size = map_file(out_name, &out_data);
222 if(!out_size) {
223 ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
224 UnmapViewOfFile(test_data);
225 return;
228 run_test(test_data, test_size, out_data, out_size);
230 UnmapViewOfFile(test_data);
231 UnmapViewOfFile(out_data);
234 static DWORD load_resource(const char *name, const char *type, const char **ret)
236 const char *res;
237 HRSRC src;
238 DWORD size;
240 src = FindResourceA(NULL, name, type);
241 ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
242 if(!src)
243 return 0;
245 res = LoadResource(NULL, src);
246 size = SizeofResource(NULL, src);
247 while(size && !res[size-1])
248 size--;
250 *ret = res;
251 return size;
254 static BOOL WINAPI test_enum_proc(HMODULE module, LPCTSTR type, LPSTR name, LONG_PTR param)
256 const char *cmd_data, *out_data;
257 DWORD cmd_size, out_size;
258 char res_name[100];
260 trace("running %s test...\n", name);
262 cmd_size = load_resource(name, type, &cmd_data);
263 if(!cmd_size)
264 return TRUE;
266 sprintf(res_name, "%s.exp", name);
267 out_size = load_resource(res_name, "TESTOUT", &out_data);
268 if(!out_size)
269 return TRUE;
271 run_test(cmd_data, cmd_size, out_data, out_size);
272 return TRUE;
275 static int cmd_available(void)
277 STARTUPINFOA si;
278 PROCESS_INFORMATION pi;
279 char cmd[] = "cmd /c exit 0";
281 memset(&si, 0, sizeof(si));
282 si.cb = sizeof(si);
283 if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
284 CloseHandle(pi.hThread);
285 CloseHandle(pi.hProcess);
286 return TRUE;
288 return FALSE;
291 START_TEST(batch)
293 int argc;
294 char **argv;
296 if (!cmd_available()) {
297 win_skip("cmd not installed, skipping cmd tests\n");
298 return;
301 workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
303 argc = winetest_get_mainargs(&argv);
304 if(argc > 2)
305 run_from_file(argv[2]);
306 else
307 EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);