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
23 #include "wine/test.h"
25 static char workdir[MAX_PATH];
26 static DWORD workdir_len;
28 /* Convert to DOS line endings, and substitute escaped whitespace chars with real ones */
29 static const char* convert_input_data(const char *data, DWORD size, DWORD *new_size)
31 static const char escaped_space[] = {'@','s','p','a','c','e','@'};
32 static const char escaped_tab[] = {'@','t','a','b','@'};
33 DWORD i, eol_count = 0;
36 for (i = 0; i < size; i++)
37 if (data[i] == '\n') eol_count++;
39 ptr = new_data = HeapAlloc(GetProcessHeap(), 0, size + eol_count + 1);
41 for (i = 0; i < size; i++) {
48 if (data + i + sizeof(escaped_space) - 1 < data + size
49 && !memcmp(data + i, escaped_space, sizeof(escaped_space))) {
51 i += sizeof(escaped_space) - 1;
52 } else if (data + i + sizeof(escaped_tab) - 1 < data + size
53 && !memcmp(data + i, escaped_tab, sizeof(escaped_tab))) {
55 i += sizeof(escaped_tab) - 1;
66 *new_size = strlen(new_data);
70 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
72 SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE};
73 char command[] = "test.cmd";
74 STARTUPINFOA si = {sizeof(si)};
75 PROCESS_INFORMATION pi;
80 file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
81 FILE_ATTRIBUTE_NORMAL, NULL);
82 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
83 if(file == INVALID_HANDLE_VALUE)
86 bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
88 ok(bres, "Could not write to file: %u\n", GetLastError());
92 file = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
93 FILE_ATTRIBUTE_NORMAL, NULL);
94 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
95 if(file == INVALID_HANDLE_VALUE)
98 fileerr = CreateFileA("test.err", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
99 FILE_ATTRIBUTE_NORMAL, NULL);
100 ok(fileerr != INVALID_HANDLE_VALUE, "CreateFile stderr failed\n");
101 if(fileerr == INVALID_HANDLE_VALUE)
104 si.dwFlags = STARTF_USESTDHANDLES;
105 si.hStdOutput = file;
106 si.hStdError = fileerr;
107 bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
108 ok(bres, "CreateProcess failed: %u\n", GetLastError());
110 DeleteFileA("test.out");
114 WaitForSingleObject(pi.hProcess, INFINITE);
115 CloseHandle(pi.hThread);
116 CloseHandle(pi.hProcess);
118 CloseHandle(fileerr);
119 DeleteFileA("test.cmd");
123 static DWORD map_file(const char *file_name, const char **ret)
128 file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
129 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
130 if(file == INVALID_HANDLE_VALUE)
133 size = GetFileSize(file, NULL);
135 map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
137 ok(map != NULL, "CreateFileMapping(%s) failed: %u\n", file_name, GetLastError());
141 *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
142 ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
150 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
153 const char *out_ptr = out_line, *exp_ptr = exp_line;
154 const char *err = NULL;
156 static const char pwd_cmd[] = {'@','p','w','d','@'};
157 static const char space_cmd[] = {'@','s','p','a','c','e','@'};
158 static const char tab_cmd[] = {'@','t','a','b','@'};
159 static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
161 while(exp_ptr < exp_end) {
162 if(*exp_ptr == '@') {
163 if(exp_ptr+sizeof(pwd_cmd) <= exp_end
164 && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
165 exp_ptr += sizeof(pwd_cmd);
166 if(out_end-out_ptr < workdir_len
167 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
168 workdir, workdir_len) != CSTR_EQUAL)) {
171 out_ptr += workdir_len;
174 }else if(exp_ptr+sizeof(space_cmd) <= exp_end
175 && !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
176 exp_ptr += sizeof(space_cmd);
177 if(out_ptr < out_end && *out_ptr == ' ') {
183 }else if(exp_ptr+sizeof(tab_cmd) <= exp_end
184 && !memcmp(exp_ptr, tab_cmd, sizeof(tab_cmd))) {
185 exp_ptr += sizeof(tab_cmd);
186 if(out_ptr < out_end && *out_ptr == '\t') {
192 }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
193 && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
194 if(out_ptr == out_end)
198 }else if(out_ptr == out_end || *out_ptr != *exp_ptr)
200 }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
208 while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
213 exp_ptr += sizeof(or_broken_cmd);
223 if(exp_ptr != exp_end)
225 else if(out_ptr != out_end)
231 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
233 const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err;
235 static const char todo_wine_cmd[] = {'@','t','o','d','o','_','w','i','n','e','@'};
238 while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
241 for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
242 for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
244 is_todo_wine = (exp_ptr+sizeof(todo_wine_cmd) <= exp_nl &&
245 !memcmp(exp_ptr, todo_wine_cmd, sizeof(todo_wine_cmd)));
247 exp_ptr += sizeof(todo_wine_cmd);
248 winetest_start_todo("wine");
251 err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
253 ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
254 line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
255 else if(err == exp_nl)
256 ok(0, "excess characters on line %d (got '%.*s', wanted '%.*s')\n",
257 line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
259 ok(!err, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
260 (err ? *err : 0), (err ? (int)(err-out_ptr) : -1), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
262 if(is_todo_wine) winetest_end_todo("wine");
266 if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
268 if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
272 ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, missing %s\n", line, exp_ptr);
273 ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
276 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
278 const char *out_data, *actual_cmd_data;
279 DWORD out_size, actual_cmd_size;
281 actual_cmd_data = convert_input_data(cmd_data, cmd_size, &actual_cmd_size);
282 if(!actual_cmd_size || !actual_cmd_data)
285 if(!run_cmd(actual_cmd_data, actual_cmd_size))
288 out_size = map_file("test.out", &out_data);
290 test_output(out_data, out_size, exp_data, exp_size);
291 UnmapViewOfFile(out_data);
293 DeleteFileA("test.out");
294 DeleteFileA("test.err");
297 HeapFree(GetProcessHeap(), 0, (LPVOID)actual_cmd_data);
300 static void run_from_file(const char *file_name)
302 char out_name[MAX_PATH];
303 const char *test_data, *out_data;
304 DWORD test_size, out_size;
306 test_size = map_file(file_name, &test_data);
308 ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
312 sprintf(out_name, "%s.exp", file_name);
313 out_size = map_file(out_name, &out_data);
315 ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
316 UnmapViewOfFile(test_data);
320 run_test(test_data, test_size, out_data, out_size);
322 UnmapViewOfFile(test_data);
323 UnmapViewOfFile(out_data);
326 static DWORD load_resource(const char *name, const char *type, const char **ret)
332 src = FindResourceA(NULL, name, type);
333 ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
337 res = LoadResource(NULL, src);
338 size = SizeofResource(NULL, src);
339 while(size && !res[size-1])
346 static BOOL WINAPI test_enum_proc(HMODULE module, LPCTSTR type, LPSTR name, LONG_PTR param)
348 const char *cmd_data, *out_data;
349 DWORD cmd_size, out_size;
352 trace("running %s test...\n", name);
354 cmd_size = load_resource(name, type, &cmd_data);
358 sprintf(res_name, "%s.exp", name);
359 out_size = load_resource(res_name, "TESTOUT", &out_data);
363 run_test(cmd_data, cmd_size, out_data, out_size);
367 static int cmd_available(void)
370 PROCESS_INFORMATION pi;
371 char cmd[] = "cmd /c exit 0";
373 memset(&si, 0, sizeof(si));
375 if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
376 CloseHandle(pi.hThread);
377 CloseHandle(pi.hProcess);
388 if (!cmd_available()) {
389 win_skip("cmd not installed, skipping cmd tests\n");
393 workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
395 argc = winetest_get_mainargs(&argv);
397 run_from_file(argv[2]);
399 EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);