From: Eric Pouech epouech@codeweavers.com
Signed-off-by: Eric Pouech epouech@codeweavers.com --- dlls/msvcrt/tests/file.c | 95 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-)
diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c index 32f4bddc208..c7c9597860a 100644 --- a/dlls/msvcrt/tests/file.c +++ b/dlls/msvcrt/tests/file.c @@ -1823,6 +1823,7 @@ static void test_invalid_stdin_child( void ) ioinfo *info; int ret; char c; + int fd;
errno = 0xdeadbeef; handle = (HANDLE)_get_osfhandle(STDIN_FILENO); @@ -1835,6 +1836,9 @@ static void test_invalid_stdin_child( void )
ok(stdin->_file == -2, "stdin->_file = %d\n", stdin->_file);
+ fd = fileno(stdin); + ok(fd == -2, "fileno(stdin) returned %d\n", fd); + errno = 0xdeadbeef; ret = fread(&c, 1, 1, stdin); ok(!ret, "fread(stdin) returned %d\n", ret); @@ -1886,6 +1890,81 @@ static void test_invalid_stdin_child( void ) ok((ret==-1 && errno==EBADF) || (!ret && errno==0xdeadbeef), "errno = %d\n", errno); }
+static void test_invalid_stdin_child_subcmd( const char *subcmd ) +{ + if (!strcmp(subcmd, "/freopen")) + { + char name[12+1] = "abcdefXXXXXX"; + FILE *new; + HANDLE handle; + int ret; + char c; + int fd; + + errno = 0xdeadbeef; + handle = (HANDLE)_get_osfhandle(STDIN_FILENO); + ok(handle == (HANDLE)-2, "handle = %p\n", handle); + ok(errno == 0xdeadbeef, "errno = %d\n", errno); + + /* invalid file name */ + new = freopen("_:", "rb", stdin); + ok(new == NULL, "Shouldn't be able to reopen stdin\n"); + + errno = 0xdeadbeef; + handle = (HANDLE)_get_osfhandle(STDIN_FILENO); + ok(handle == (HANDLE)-2, "handle = %p\n", handle); + ok(errno == 0xdeadbeef, "errno = %d\n", errno); + + mktemp(name); + new = fopen(name, "wb"); + ok(new != NULL, "couldn't open %s\n", name); + c = ' '; + ret = fwrite(&c, 1, 1, new); + ok(ret == 1, "got ret %d\n", ret); + fclose(new); + + new = freopen(name, "rb", stdin); + ok(new != NULL, "freopen(..., stdin) returned NULL\n"); + ok(new == stdin, "freopen(..., stdin) didn't return stdin\n"); + + errno = 0xdeadbeef; + handle = (HANDLE)_get_osfhandle(STDIN_FILENO); + ok(handle == (HANDLE)-2, "handle = %p\n", handle); + ok(errno == 0xdeadbeef, "errno = %d\n", errno); + + fd = fileno(stdin); + todo_wine + ok(fd > 0, "fileno(stdin) returned %d\n", fd); + ok(fd != STDIN_FILENO, "fileno(stdin) returned STDIN_FILENO\n"); + + errno = 0xdeadbeef; + handle = (HANDLE)_get_osfhandle(fd); + ok(handle != (HANDLE)-2, "handle = %p\n", handle); + todo_wine + ok(errno == 0xdeadbeef, "errno = %d\n", errno); + + errno = 0xdeadbeef; + ret = fread(&c, 1, 1, stdin); + todo_wine + ok(ret == 1, "fread(stdin) returned %d\n", ret); + todo_wine + ok(errno == 0xdeadbeef, "errno = %d\n", errno); + ok(c == ' ', "Unexpected char\n"); + + /* invalid file name */ + new = freopen("_:", "rb", stdin); + ok(new == NULL, "Shouldn't be able to reopen stdin\n"); + + errno = 0xdeadbeef; + ret = fclose(stdin); + ok(ret == -1, "fclose(stdin) returned %d\n", ret); + ok(errno == 0xdeadbeef, "errno is %d\n", errno); + + DeleteFileA(name); + } + else ok(0, "Unexpected subcommand %s\n", subcmd); +} + static void test_invalid_stdin( const char* selfname ) { char cmdline[MAX_PATH]; @@ -1919,6 +1998,15 @@ static void test_invalid_stdin( const char* selfname ) CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS, NULL, NULL, &startup, &proc); wait_child_process(proc.hProcess); + CloseHandle(proc.hProcess); + CloseHandle(proc.hThread); + + sprintf(cmdline, "%s file stdin /freopen", selfname); + CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, + CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS, NULL, NULL, &startup, &proc); + wait_child_process(proc.hProcess); + CloseHandle(proc.hProcess); + CloseHandle(proc.hThread);
ret = RegCloseKey(key); ok(!ret, "RegCloseKey failed: %lx\n", ret); @@ -3004,7 +3092,12 @@ START_TEST(file) else if (strcmp(arg_v[2], "pipes") == 0) test_pipes_child(arg_c, arg_v); else if (strcmp(arg_v[2], "stdin") == 0) - test_invalid_stdin_child(); + { + if (arg_c == 3) + test_invalid_stdin_child(); + else + test_invalid_stdin_child_subcmd(arg_v[3]); + } else ok(0, "invalid argument '%s'\n", arg_v[2]); return;