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