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