Piotr Caban : msvcrt/tests: Add close tests on file descriptors that use the same HANDLE.

Alexandre Julliard julliard at wine.codeweavers.com
Mon Feb 22 08:57:38 CST 2016


Module: wine
Branch: master
Commit: 96d476584e771bb352c8b8a84ff51f67a63f0d1e
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=96d476584e771bb352c8b8a84ff51f67a63f0d1e

Author: Piotr Caban <piotr at codeweavers.com>
Date:   Fri Feb 19 18:43:31 2016 +0100

msvcrt/tests: Add close tests on file descriptors that use the same HANDLE.

Based on Qian Hong patch.

Signed-off-by: Piotr Caban <piotr at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/msvcrt/tests/file.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index ba8c3e3..dbb6cef 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -2327,6 +2327,64 @@ static void test_write_flush(void)
     free(tempf);
 }
 
+static void test_close(void)
+{
+    ioinfo *stdout_info, stdout_copy, *stderr_info, stderr_copy;
+    int fd1, fd2, ret1, ret2, ret3, ret4;
+    DWORD flags;
+    HANDLE h;
+
+    /* test close on fds that use the same handle */
+    h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
+            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+    ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
+
+    fd1 = _open_osfhandle((intptr_t)h, 0);
+    ok(fd1 != -1, "_open_osfhandle failed (%d)\n", errno);
+    fd2 = _open_osfhandle((intptr_t)h, 0);
+    ok(fd2 != -1, "_open_osfhandle failed (%d)\n", errno);
+    ok(fd1 != fd2, "fd1 == fd2\n");
+
+    ok((HANDLE)_get_osfhandle(fd1) == h, "handles mismatch (%p != %p)\n",
+            (HANDLE)_get_osfhandle(fd1), h);
+    ok((HANDLE)_get_osfhandle(fd2) == h, "handles mismatch (%p != %p)\n",
+            (HANDLE)_get_osfhandle(fd2), h);
+    ok(!close(fd1), "close(fd1) failed (%d)\n", errno);
+    ok(!GetHandleInformation(h, &flags), "GetHandleInformation succeeded\n");
+    ok(close(fd2), "close(fd2) succeeded\n");
+
+    /* test close on stdout and stderr that use the same handle */
+    h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
+            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+    ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
+
+    /* tests output will not be visible from now on */
+    stdout_info = &__pioinfo[STDOUT_FILENO/MSVCRT_FD_BLOCK_SIZE][STDOUT_FILENO%MSVCRT_FD_BLOCK_SIZE];
+    stderr_info = &__pioinfo[STDERR_FILENO/MSVCRT_FD_BLOCK_SIZE][STDERR_FILENO%MSVCRT_FD_BLOCK_SIZE];
+    stdout_copy = *stdout_info;
+    stderr_copy = *stderr_info;
+    stdout_info->handle = h;
+    stderr_info->handle = h;
+
+    ret1 = close(STDOUT_FILENO);
+    ret2 = GetHandleInformation(h, &flags);
+    ret3 = close(STDERR_FILENO);
+    ret4 = GetHandleInformation(h, &flags);
+
+    *stdout_info = stdout_copy;
+    *stderr_info = stderr_copy;
+    SetStdHandle(STD_OUTPUT_HANDLE, stdout_info->handle);
+    SetStdHandle(STD_ERROR_HANDLE, stderr_info->handle);
+    /* stdout and stderr restored */
+
+    ok(!ret1, "close(STDOUT_FILENO) failed\n");
+    ok(ret2, "GetHandleInformation failed\n");
+    ok(!ret3, "close(STDERR_FILENO) failed\n");
+    ok(!ret4, "GetHandleInformation succeeded\n");
+
+    DeleteFileA( "fdopen.tst" );
+}
+
 START_TEST(file)
 {
     int arg_c;
@@ -2393,6 +2451,7 @@ START_TEST(file)
     test_mktemp();
     test__open_osfhandle();
     test_write_flush();
+    test_close();
 
     /* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report
      * file contains lines in the correct order




More information about the wine-cvs mailing list