[ADVAPI32_WINETEST] Revert service.c changes from 3c1b7834e1 to avoid testbot hangs...
[reactos.git] / modules / rostests / winetests / cmd / 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 static char drive[2];
28 static const DWORD drive_len = sizeof(drive)/sizeof(drive[0]);
29 static char path[MAX_PATH];
30 static DWORD path_len;
31 static char shortpath[MAX_PATH];
32 static DWORD shortpath_len;
33
34 /* Convert to DOS line endings, and substitute escaped whitespace chars with real ones */
35 static const char* convert_input_data(const char *data, DWORD size, DWORD *new_size)
36 {
37 static const char escaped_space[] = {'@','s','p','a','c','e','@'};
38 static const char escaped_tab[] = {'@','t','a','b','@'};
39 DWORD i, eol_count = 0;
40 char *ptr, *new_data;
41
42 for (i = 0; i < size; i++)
43 if (data[i] == '\n') eol_count++;
44
45 ptr = new_data = HeapAlloc(GetProcessHeap(), 0, size + eol_count + 1);
46
47 for (i = 0; i < size; i++) {
48 switch (data[i]) {
49 case '\n':
50 if (data[i-1] != '\r')
51 *ptr++ = '\r';
52 *ptr++ = '\n';
53 break;
54 case '@':
55 if (data + i + sizeof(escaped_space) - 1 < data + size
56 && !memcmp(data + i, escaped_space, sizeof(escaped_space))) {
57 *ptr++ = ' ';
58 i += sizeof(escaped_space) - 1;
59 } else if (data + i + sizeof(escaped_tab) - 1 < data + size
60 && !memcmp(data + i, escaped_tab, sizeof(escaped_tab))) {
61 *ptr++ = '\t';
62 i += sizeof(escaped_tab) - 1;
63 } else {
64 *ptr++ = data[i];
65 }
66 break;
67 default:
68 *ptr++ = data[i];
69 }
70 }
71 *ptr = '\0';
72
73 *new_size = strlen(new_data);
74 return new_data;
75 }
76
77 static BOOL run_cmd(const char *cmd_data, DWORD cmd_size)
78 {
79 SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, TRUE};
80 char command[] = "test.cmd";
81 STARTUPINFOA si = {sizeof(si)};
82 PROCESS_INFORMATION pi;
83 HANDLE file,fileerr;
84 DWORD size;
85 BOOL bres;
86
87 file = CreateFileA("test.cmd", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
88 FILE_ATTRIBUTE_NORMAL, NULL);
89 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
90 if(file == INVALID_HANDLE_VALUE)
91 return FALSE;
92
93 bres = WriteFile(file, cmd_data, cmd_size, &size, NULL);
94 CloseHandle(file);
95 ok(bres, "Could not write to file: %u\n", GetLastError());
96 if(!bres)
97 return FALSE;
98
99 file = CreateFileA("test.out", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
100 FILE_ATTRIBUTE_NORMAL, NULL);
101 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
102 if(file == INVALID_HANDLE_VALUE)
103 return FALSE;
104
105 fileerr = CreateFileA("test.err", GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, &sa, CREATE_ALWAYS,
106 FILE_ATTRIBUTE_NORMAL, NULL);
107 ok(fileerr != INVALID_HANDLE_VALUE, "CreateFile stderr failed\n");
108 if(fileerr == INVALID_HANDLE_VALUE)
109 return FALSE;
110
111 si.dwFlags = STARTF_USESTDHANDLES;
112 si.hStdOutput = file;
113 si.hStdError = fileerr;
114 bres = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
115 ok(bres, "CreateProcess failed: %u\n", GetLastError());
116 if(!bres) {
117 DeleteFileA("test.out");
118 return FALSE;
119 }
120
121 WaitForSingleObject(pi.hProcess, INFINITE);
122 CloseHandle(pi.hThread);
123 CloseHandle(pi.hProcess);
124 CloseHandle(file);
125 CloseHandle(fileerr);
126 DeleteFileA("test.cmd");
127 return TRUE;
128 }
129
130 static DWORD map_file(const char *file_name, const char **ret)
131 {
132 HANDLE file, map;
133 DWORD size;
134
135 file = CreateFileA(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
136 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %08x\n", GetLastError());
137 if(file == INVALID_HANDLE_VALUE)
138 return 0;
139
140 size = GetFileSize(file, NULL);
141
142 map = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL);
143 CloseHandle(file);
144 ok(map != NULL, "CreateFileMappingA(%s) failed: %u\n", file_name, GetLastError());
145 if(!map)
146 return 0;
147
148 *ret = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
149 ok(*ret != NULL, "MapViewOfFile failed: %u\n", GetLastError());
150 CloseHandle(map);
151 if(!*ret)
152 return 0;
153
154 return size;
155 }
156
157 static const char *compare_line(const char *out_line, const char *out_end, const char *exp_line,
158 const char *exp_end)
159 {
160 const char *out_ptr = out_line, *exp_ptr = exp_line;
161 const char *err = NULL;
162
163 static const char pwd_cmd[] = {'@','p','w','d','@'};
164 static const char drive_cmd[] = {'@','d','r','i','v','e','@'};
165 static const char path_cmd[] = {'@','p','a','t','h','@'};
166 static const char shortpath_cmd[] = {'@','s','h','o','r','t','p','a','t','h','@'};
167 static const char space_cmd[] = {'@','s','p','a','c','e','@'};
168 static const char tab_cmd[] = {'@','t','a','b','@'};
169 static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
170
171 while(exp_ptr < exp_end) {
172 if(*exp_ptr == '@') {
173 if(exp_ptr+sizeof(pwd_cmd) <= exp_end
174 && !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
175 exp_ptr += sizeof(pwd_cmd);
176 if(out_end-out_ptr < workdir_len
177 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
178 workdir, workdir_len) != CSTR_EQUAL)) {
179 err = out_ptr;
180 }else {
181 out_ptr += workdir_len;
182 continue;
183 }
184 } else if(exp_ptr+sizeof(drive_cmd) <= exp_end
185 && !memcmp(exp_ptr, drive_cmd, sizeof(drive_cmd))) {
186 exp_ptr += sizeof(drive_cmd);
187 if(out_end-out_ptr < drive_len
188 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
189 out_ptr, drive_len, drive, drive_len) != CSTR_EQUAL)) {
190 err = out_ptr;
191 }else {
192 out_ptr += drive_len;
193 continue;
194 }
195 } else if(exp_ptr+sizeof(path_cmd) <= exp_end
196 && !memcmp(exp_ptr, path_cmd, sizeof(path_cmd))) {
197 exp_ptr += sizeof(path_cmd);
198 if(out_end-out_ptr < path_len
199 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
200 out_ptr, path_len, path, path_len) != CSTR_EQUAL)) {
201 err = out_ptr;
202 }else {
203 out_ptr += path_len;
204 continue;
205 }
206 } else if(exp_ptr+sizeof(shortpath_cmd) <= exp_end
207 && !memcmp(exp_ptr, shortpath_cmd, sizeof(shortpath_cmd))) {
208 exp_ptr += sizeof(shortpath_cmd);
209 if(out_end-out_ptr < shortpath_len
210 || (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
211 out_ptr, shortpath_len, shortpath, shortpath_len) != CSTR_EQUAL)) {
212 err = out_ptr;
213 }else {
214 out_ptr += shortpath_len;
215 continue;
216 }
217 }else if(exp_ptr+sizeof(space_cmd) <= exp_end
218 && !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
219 exp_ptr += sizeof(space_cmd);
220 if(out_ptr < out_end && *out_ptr == ' ') {
221 out_ptr++;
222 continue;
223 } else {
224 err = out_end;
225 }
226 }else if(exp_ptr+sizeof(tab_cmd) <= exp_end
227 && !memcmp(exp_ptr, tab_cmd, sizeof(tab_cmd))) {
228 exp_ptr += sizeof(tab_cmd);
229 if(out_ptr < out_end && *out_ptr == '\t') {
230 out_ptr++;
231 continue;
232 } else {
233 err = out_end;
234 }
235 }else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
236 && !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
237 if(out_ptr == out_end)
238 return NULL;
239 else
240 err = out_ptr;
241 }else if(out_ptr == out_end || *out_ptr != *exp_ptr)
242 err = out_ptr;
243 }else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
244 err = out_ptr;
245 }
246
247 if(err) {
248 if(!broken(1))
249 return err;
250
251 while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
252 exp_ptr++;
253 if(!exp_ptr)
254 return err;
255
256 exp_ptr += sizeof(or_broken_cmd);
257 out_ptr = out_line;
258 err = NULL;
259 continue;
260 }
261
262 exp_ptr++;
263 out_ptr++;
264 }
265
266 if(exp_ptr != exp_end)
267 return out_ptr;
268 else if(out_ptr != out_end)
269 return exp_end;
270
271 return NULL;
272 }
273
274 static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
275 {
276 const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err = NULL;
277 DWORD line = 0;
278 static const char todo_wine_cmd[] = {'@','t','o','d','o','_','w','i','n','e','@'};
279 static const char resync_cmd[] = {'-','-','-'};
280 BOOL is_todo_wine, is_out_resync = FALSE, is_exp_resync = FALSE;
281
282 while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
283 line++;
284
285 for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
286 for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
287
288 is_todo_wine = (exp_ptr+sizeof(todo_wine_cmd) <= exp_nl &&
289 !memcmp(exp_ptr, todo_wine_cmd, sizeof(todo_wine_cmd)));
290 if (is_todo_wine)
291 exp_ptr += sizeof(todo_wine_cmd);
292
293 todo_wine_if(is_todo_wine)
294 {
295 is_exp_resync=(exp_ptr+sizeof(resync_cmd) <= exp_nl &&
296 !memcmp(exp_ptr, resync_cmd, sizeof(resync_cmd)));
297 is_out_resync=(out_ptr+sizeof(resync_cmd) <= out_nl &&
298 !memcmp(out_ptr, resync_cmd, sizeof(resync_cmd)));
299
300 err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
301 if(err == out_nl)
302 ok(0, "unexpected end of line %d (got '%.*s', wanted '%.*s')\n",
303 line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
304 else if(err == exp_nl)
305 ok(0, "excess characters on line %d (got '%.*s', wanted '%.*s')\n",
306 line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
307 else if (!err && is_todo_wine && is_out_resync && is_exp_resync)
308 /* Consider that the todo_wine was to deal with extra lines,
309 * not for the resync line itself
310 */
311 err = NULL;
312 else
313 ok(!err, "unexpected char 0x%x position %d in line %d (got '%.*s', wanted '%.*s')\n",
314 (err ? *err : 0), (err ? (int)(err-out_ptr) : -1), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
315 }
316
317 if (is_exp_resync && err && is_todo_wine)
318 {
319 exp_ptr -= sizeof(todo_wine_cmd);
320 /* If we rewind to the beginning of the line, don't increment line number */
321 line--;
322 }
323 else if (!is_exp_resync || !err ||
324 (is_exp_resync && is_out_resync && err))
325 {
326 exp_ptr = exp_nl+1;
327 if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
328 exp_ptr++;
329 }
330
331 if (!is_out_resync || !err)
332 {
333 out_ptr = out_nl+1;
334 if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
335 out_ptr++;
336 }
337 }
338
339 ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %d, missing %s\n", line, exp_ptr);
340 ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
341 }
342
343 static void run_test(const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
344 {
345 const char *out_data, *actual_cmd_data;
346 DWORD out_size, actual_cmd_size;
347
348 actual_cmd_data = convert_input_data(cmd_data, cmd_size, &actual_cmd_size);
349 if(!actual_cmd_size || !actual_cmd_data)
350 goto cleanup;
351
352 if(!run_cmd(actual_cmd_data, actual_cmd_size))
353 goto cleanup;
354
355 out_size = map_file("test.out", &out_data);
356 if(out_size) {
357 test_output(out_data, out_size, exp_data, exp_size);
358 UnmapViewOfFile(out_data);
359 }
360 DeleteFileA("test.out");
361 DeleteFileA("test.err");
362
363 cleanup:
364 HeapFree(GetProcessHeap(), 0, (LPVOID)actual_cmd_data);
365 }
366
367 static void run_from_file(const char *file_name)
368 {
369 char out_name[MAX_PATH];
370 const char *test_data, *out_data;
371 DWORD test_size, out_size;
372
373 test_size = map_file(file_name, &test_data);
374 if(!test_size) {
375 ok(0, "Could not map file %s: %u\n", file_name, GetLastError());
376 return;
377 }
378
379 sprintf(out_name, "%s.exp", file_name);
380 out_size = map_file(out_name, &out_data);
381 if(!out_size) {
382 ok(0, "Could not map file %s: %u\n", out_name, GetLastError());
383 UnmapViewOfFile(test_data);
384 return;
385 }
386
387 run_test(test_data, test_size, out_data, out_size);
388
389 UnmapViewOfFile(test_data);
390 UnmapViewOfFile(out_data);
391 }
392
393 static DWORD load_resource(const char *name, const char *type, const char **ret)
394 {
395 const char *res;
396 HRSRC src;
397 DWORD size;
398
399 src = FindResourceA(NULL, name, type);
400 ok(src != NULL, "Could not find resource %s: %u\n", name, GetLastError());
401 if(!src)
402 return 0;
403
404 res = LoadResource(NULL, src);
405 size = SizeofResource(NULL, src);
406 while(size && !res[size-1])
407 size--;
408
409 *ret = res;
410 return size;
411 }
412
413 static BOOL WINAPI test_enum_proc(HMODULE module, LPCSTR type, LPSTR name, LONG_PTR param)
414 {
415 const char *cmd_data, *out_data;
416 DWORD cmd_size, out_size;
417 char res_name[100];
418
419 trace("running %s test...\n", name);
420
421 cmd_size = load_resource(name, type, &cmd_data);
422 if(!cmd_size)
423 return TRUE;
424
425 sprintf(res_name, "%s.exp", name);
426 out_size = load_resource(res_name, "TESTOUT", &out_data);
427 if(!out_size)
428 return TRUE;
429
430 run_test(cmd_data, cmd_size, out_data, out_size);
431 return TRUE;
432 }
433
434 static int cmd_available(void)
435 {
436 STARTUPINFOA si;
437 PROCESS_INFORMATION pi;
438 char cmd[] = "cmd /c exit 0";
439
440 memset(&si, 0, sizeof(si));
441 si.cb = sizeof(si);
442 if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
443 CloseHandle(pi.hThread);
444 CloseHandle(pi.hProcess);
445 return TRUE;
446 }
447 return FALSE;
448 }
449
450 START_TEST(batch)
451 {
452 int argc;
453 char **argv;
454
455 if (!cmd_available()) {
456 win_skip("cmd not installed, skipping cmd tests\n");
457 return;
458 }
459
460 workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
461 drive[0] = workdir[0];
462 drive[1] = workdir[1]; /* Should be ':' */
463 memcpy(path, workdir + drive_len, (workdir_len - drive_len) * sizeof(drive[0]));
464
465 /* Only add trailing backslash to 'path' for non-root directory */
466 if (workdir_len - drive_len > 1) {
467 path[workdir_len - drive_len] = '\\';
468 path_len = workdir_len - drive_len + 1;
469 } else {
470 path_len = 1; /* \ */
471 }
472 shortpath_len = GetShortPathNameA(path, shortpath,
473 sizeof(shortpath)/sizeof(shortpath[0]));
474
475 argc = winetest_get_mainargs(&argv);
476 if(argc > 2)
477 run_from_file(argv[2]);
478 else
479 EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);
480 }