Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/cmd/tests/batch.c
4389 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
211
static const char pwd_cmd[] = {'@','p','w','d','@'};
212
static const char drive_cmd[] = {'@','d','r','i','v','e','@'};
213
static const char path_cmd[] = {'@','p','a','t','h','@'};
214
static const char shortpath_cmd[] = {'@','s','h','o','r','t','p','a','t','h','@'};
215
static const char space_cmd[] = {'@','s','p','a','c','e','@'};
216
static const char spaces_cmd[] = {'@','s','p','a','c','e','s','@'};
217
static const char tab_cmd[] = {'@','t','a','b','@'};
218
static const char formfeed_cmd[] = {'@','f','o','r','m', 'f', 'e', 'e', 'd', '@'};
219
static const char or_broken_cmd[] = {'@','o','r','_','b','r','o','k','e','n','@'};
220
221
while(exp_ptr < exp_end) {
222
if(*exp_ptr == '@') {
223
if(exp_ptr+sizeof(pwd_cmd) <= exp_end
224
&& !memcmp(exp_ptr, pwd_cmd, sizeof(pwd_cmd))) {
225
exp_ptr += sizeof(pwd_cmd);
226
if(out_end-out_ptr < workdir_len
227
|| (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, out_ptr, workdir_len,
228
workdir, workdir_len) != CSTR_EQUAL)) {
229
err = out_ptr;
230
}else {
231
out_ptr += workdir_len;
232
continue;
233
}
234
} else if(exp_ptr+sizeof(drive_cmd) <= exp_end
235
&& !memcmp(exp_ptr, drive_cmd, sizeof(drive_cmd))) {
236
exp_ptr += sizeof(drive_cmd);
237
if(out_end-out_ptr < drive_len
238
|| (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
239
out_ptr, drive_len, drive, drive_len) != CSTR_EQUAL)) {
240
err = out_ptr;
241
}else {
242
out_ptr += drive_len;
243
continue;
244
}
245
} else if(exp_ptr+sizeof(path_cmd) <= exp_end
246
&& !memcmp(exp_ptr, path_cmd, sizeof(path_cmd))) {
247
exp_ptr += sizeof(path_cmd);
248
if(out_end-out_ptr < path_len
249
|| (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
250
out_ptr, path_len, path, path_len) != CSTR_EQUAL)) {
251
err = out_ptr;
252
}else {
253
out_ptr += path_len;
254
continue;
255
}
256
} else if(exp_ptr+sizeof(shortpath_cmd) <= exp_end
257
&& !memcmp(exp_ptr, shortpath_cmd, sizeof(shortpath_cmd))) {
258
exp_ptr += sizeof(shortpath_cmd);
259
if(out_end-out_ptr < shortpath_len
260
|| (CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
261
out_ptr, shortpath_len, shortpath, shortpath_len) != CSTR_EQUAL)) {
262
err = out_ptr;
263
}else {
264
out_ptr += shortpath_len;
265
continue;
266
}
267
}else if(exp_ptr+sizeof(space_cmd) <= exp_end
268
&& !memcmp(exp_ptr, space_cmd, sizeof(space_cmd))) {
269
exp_ptr += sizeof(space_cmd);
270
if(out_ptr < out_end && *out_ptr == ' ') {
271
out_ptr++;
272
continue;
273
} else {
274
err = out_end;
275
}
276
}else if(exp_ptr+sizeof(spaces_cmd) <= exp_end
277
&& !memcmp(exp_ptr, spaces_cmd, sizeof(spaces_cmd))) {
278
exp_ptr += sizeof(spaces_cmd);
279
if(out_ptr < out_end && *out_ptr == ' ') {
280
while (out_ptr < out_end && *out_ptr == ' ') out_ptr++;
281
continue;
282
} else {
283
err = out_end;
284
}
285
}else if(exp_ptr+sizeof(tab_cmd) <= exp_end
286
&& !memcmp(exp_ptr, tab_cmd, sizeof(tab_cmd))) {
287
exp_ptr += sizeof(tab_cmd);
288
if(out_ptr < out_end && *out_ptr == '\t') {
289
out_ptr++;
290
continue;
291
} else {
292
err = out_end;
293
}
294
}else if(exp_ptr+sizeof(formfeed_cmd) <= exp_end
295
&& !memcmp(exp_ptr, formfeed_cmd, sizeof(formfeed_cmd))) {
296
exp_ptr += sizeof(formfeed_cmd);
297
if(out_ptr < out_end && *out_ptr == '\f') {
298
out_ptr++;
299
continue;
300
} else {
301
err = out_end;
302
}
303
}else if(exp_ptr+sizeof(or_broken_cmd) <= exp_end
304
&& !memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd))) {
305
if(out_ptr == out_end)
306
return NULL;
307
else
308
err = out_ptr;
309
}else if(out_ptr == out_end || *out_ptr != *exp_ptr)
310
err = out_ptr;
311
}else if(out_ptr == out_end || *out_ptr != *exp_ptr) {
312
err = out_ptr;
313
}
314
315
if(err) {
316
if(!broken(1))
317
return err;
318
319
while(exp_ptr+sizeof(or_broken_cmd) <= exp_end && memcmp(exp_ptr, or_broken_cmd, sizeof(or_broken_cmd)))
320
exp_ptr++;
321
exp_ptr += sizeof(or_broken_cmd);
322
if (exp_ptr > exp_end) return err;
323
out_ptr = out_line;
324
err = NULL;
325
continue;
326
}
327
328
exp_ptr++;
329
out_ptr++;
330
}
331
332
if(exp_ptr != exp_end)
333
return out_ptr;
334
else if(out_ptr != out_end)
335
return exp_end;
336
337
return NULL;
338
}
339
340
static void test_output(const char *out_data, DWORD out_size, const char *exp_data, DWORD exp_size)
341
{
342
const char *out_ptr = out_data, *exp_ptr = exp_data, *out_nl, *exp_nl, *err = NULL;
343
DWORD line = 0;
344
static const char todo_wine_cmd[] = {'@','t','o','d','o','_','w','i','n','e','@'};
345
static const char resync_cmd[] = {'-','-','-'};
346
BOOL is_todo_wine, is_out_resync = FALSE, is_exp_resync = FALSE;
347
348
while(out_ptr < out_data+out_size && exp_ptr < exp_data+exp_size) {
349
line++;
350
351
for(exp_nl = exp_ptr; exp_nl < exp_data+exp_size && *exp_nl != '\r' && *exp_nl != '\n'; exp_nl++);
352
for(out_nl = out_ptr; out_nl < out_data+out_size && *out_nl != '\r' && *out_nl != '\n'; out_nl++);
353
354
is_todo_wine = (exp_ptr+sizeof(todo_wine_cmd) <= exp_nl &&
355
!memcmp(exp_ptr, todo_wine_cmd, sizeof(todo_wine_cmd)));
356
if (is_todo_wine)
357
exp_ptr += sizeof(todo_wine_cmd);
358
359
todo_wine_if(is_todo_wine)
360
{
361
is_exp_resync=(exp_ptr+sizeof(resync_cmd) <= exp_nl &&
362
!memcmp(exp_ptr, resync_cmd, sizeof(resync_cmd)));
363
is_out_resync=(out_ptr+sizeof(resync_cmd) <= out_nl &&
364
!memcmp(out_ptr, resync_cmd, sizeof(resync_cmd)));
365
366
err = compare_line(out_ptr, out_nl, exp_ptr, exp_nl);
367
if(err == out_nl)
368
ok(0, "unexpected end of line %ld (got '%.*s', wanted '%.*s')\n",
369
line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
370
else if(err == exp_nl)
371
ok(0, "excess characters on line %ld (got '%.*s', wanted '%.*s')\n",
372
line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
373
else if (!err && is_todo_wine && is_out_resync && is_exp_resync)
374
/* Consider that the todo_wine was to deal with extra lines,
375
* not for the resync line itself
376
*/
377
err = NULL;
378
else
379
ok(!err, "unexpected char 0x%x position %d in line %ld (got '%.*s', wanted '%.*s')\n",
380
(err ? *err : 0), (err ? (int)(err-out_ptr) : -1), line, (int)(out_nl-out_ptr), out_ptr, (int)(exp_nl-exp_ptr), exp_ptr);
381
}
382
383
if (is_exp_resync && err && is_todo_wine)
384
{
385
exp_ptr -= sizeof(todo_wine_cmd);
386
/* If we rewind to the beginning of the line, don't increment line number */
387
line--;
388
}
389
else if (!is_exp_resync || !err ||
390
(is_exp_resync && is_out_resync && err))
391
{
392
exp_ptr = exp_nl+1;
393
if(exp_nl+1 < exp_data+exp_size && exp_nl[0] == '\r' && exp_nl[1] == '\n')
394
exp_ptr++;
395
}
396
397
if (!is_out_resync || !err)
398
{
399
out_ptr = out_nl+1;
400
if(out_nl+1 < out_data+out_size && out_nl[0] == '\r' && out_nl[1] == '\n')
401
out_ptr++;
402
}
403
}
404
405
ok(exp_ptr >= exp_data+exp_size, "unexpected end of output in line %ld, missing %s\n", line, exp_ptr);
406
ok(out_ptr >= out_data+out_size, "too long output, got additional %s\n", out_ptr);
407
}
408
409
static void run_test(const char *res_name, const char *cmd_data, DWORD cmd_size, const char *exp_data, DWORD exp_size)
410
{
411
const char *out_data, *actual_cmd_data;
412
DWORD out_size, actual_cmd_size;
413
414
actual_cmd_data = convert_input_data(cmd_data, cmd_size, &actual_cmd_size);
415
if(!actual_cmd_size || !actual_cmd_data)
416
goto cleanup;
417
418
if(!run_cmd(res_name, actual_cmd_data, actual_cmd_size))
419
goto cleanup;
420
421
out_size = map_file("test.out", &out_data);
422
if(out_size) {
423
test_output(out_data, out_size, exp_data, exp_size);
424
UnmapViewOfFile(out_data);
425
}
426
DeleteFileA("test.out");
427
DeleteFileA("test.err");
428
429
cleanup:
430
HeapFree(GetProcessHeap(), 0, (LPVOID)actual_cmd_data);
431
}
432
433
static void run_from_file(const char *file_name)
434
{
435
char out_name[MAX_PATH];
436
const char *test_data, *out_data;
437
DWORD test_size, out_size;
438
439
test_size = map_file(file_name, &test_data);
440
if(!test_size) {
441
ok(0, "Could not map file %s: %lu\n", file_name, GetLastError());
442
return;
443
}
444
445
sprintf(out_name, "%s.exp", file_name);
446
out_size = map_file(out_name, &out_data);
447
if(!out_size) {
448
ok(0, "Could not map file %s: %lu\n", out_name, GetLastError());
449
UnmapViewOfFile(test_data);
450
return;
451
}
452
453
run_test(file_name, test_data, test_size, out_data, out_size);
454
455
UnmapViewOfFile(test_data);
456
UnmapViewOfFile(out_data);
457
}
458
459
static DWORD load_resource(const char *name, const char *type, const char **ret)
460
{
461
const char *res;
462
HRSRC src;
463
DWORD size;
464
465
src = FindResourceA(NULL, name, type);
466
ok(src != NULL, "Could not find resource %s: %lu\n", name, GetLastError());
467
if(!src)
468
return 0;
469
470
res = LoadResource(NULL, src);
471
size = SizeofResource(NULL, src);
472
while(size && !res[size-1])
473
size--;
474
475
*ret = res;
476
return size;
477
}
478
479
static BOOL WINAPI test_enum_proc(HMODULE module, LPCSTR type, LPSTR name, LONG_PTR param)
480
{
481
const char *cmd_data, *out_data;
482
DWORD cmd_size, out_size;
483
char res_name[100];
484
485
trace("running %s test...\n", name);
486
487
cmd_size = load_resource(name, type, &cmd_data);
488
if(!cmd_size)
489
return TRUE;
490
491
sprintf(res_name, "%s.exp", name);
492
out_size = load_resource(res_name, "TESTOUT", &out_data);
493
if(!out_size)
494
return TRUE;
495
496
run_test(name, cmd_data, cmd_size, out_data, out_size);
497
return TRUE;
498
}
499
500
static int cmd_available(void)
501
{
502
STARTUPINFOA si;
503
PROCESS_INFORMATION pi;
504
char cmd[] = "cmd /c exit 0";
505
506
memset(&si, 0, sizeof(si));
507
si.cb = sizeof(si);
508
if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
509
CloseHandle(pi.hThread);
510
CloseHandle(pi.hProcess);
511
return TRUE;
512
}
513
return FALSE;
514
}
515
516
void create_nul_test_file(void)
517
{
518
HANDLE file;
519
DWORD size;
520
BOOL bres;
521
char contents[] = "a b c\nd e\0f\ng h i";
522
523
file = CreateFileA("nul_test_file", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
524
FILE_ATTRIBUTE_NORMAL, NULL);
525
ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
526
if(file == INVALID_HANDLE_VALUE)
527
return;
528
529
bres = WriteFile(file, contents, ARRAYSIZE(contents), &size, NULL);
530
ok(bres, "Could not write to file: %lu\n", GetLastError());
531
CloseHandle(file);
532
}
533
534
START_TEST(batch)
535
{
536
int argc;
537
char **argv;
538
539
if (!cmd_available()) {
540
win_skip("cmd not installed, skipping cmd tests\n");
541
return;
542
}
543
544
workdir_len = GetCurrentDirectoryA(sizeof(workdir), workdir);
545
drive[0] = workdir[0];
546
drive[1] = workdir[1]; /* Should be ':' */
547
memcpy(path, workdir + drive_len, (workdir_len - drive_len) * sizeof(drive[0]));
548
549
/* Only add trailing backslash to 'path' for non-root directory */
550
if (workdir_len - drive_len > 1) {
551
path[workdir_len - drive_len] = '\\';
552
path_len = workdir_len - drive_len + 1;
553
} else {
554
path_len = 1; /* \ */
555
}
556
shortpath_len = GetShortPathNameA(path, shortpath, ARRAY_SIZE(shortpath));
557
558
create_nul_test_file();
559
560
argc = winetest_get_mainargs(&argv);
561
if(argc > 2)
562
run_from_file(argv[2]);
563
else
564
EnumResourceNamesA(NULL, "TESTCMD", test_enum_proc, 0);
565
566
DeleteFileA("nul_test_file");
567
}
568
569