[PATCH] [Kernel32]: added a couple of tests about console creation through CreateFile, and fix some corner cases

Eric Pouech eric.pouech at orange.fr
Sun Feb 5 15:08:05 CST 2012


Fix for #22216

A+
---

 dlls/kernel32/file.c          |    4 ++-
 dlls/kernel32/tests/console.c |   61 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletions(-)


diff --git a/dlls/kernel32/file.c b/dlls/kernel32/file.c
index 194960b..a3b4411 100644
--- a/dlls/kernel32/file.c
+++ b/dlls/kernel32/file.c
@@ -1286,7 +1286,9 @@ HANDLE WINAPI CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
 
     if (!strcmpiW(filename, coninW) || !strcmpiW(filename, conoutW))
     {
-        ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle), creation);
+        ret = OpenConsoleW(filename, access, (sa && sa->bInheritHandle),
+                           creation ? OPEN_EXISTING : 0);
+        if (ret == INVALID_HANDLE_VALUE) SetLastError(ERROR_INVALID_PARAMETER);
         goto done;
     }
 
diff --git a/dlls/kernel32/tests/console.c b/dlls/kernel32/tests/console.c
index f248666..bc4bb12 100644
--- a/dlls/kernel32/tests/console.c
+++ b/dlls/kernel32/tests/console.c
@@ -1116,6 +1116,66 @@ static void test_OpenConsoleW(void)
         CloseHandle(ret);
 }
 
+static void test_CreateFileW(void)
+{
+    static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
+    static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
+
+    static const struct
+    {
+        LPCWSTR name;
+        DWORD access;
+        BOOL inherit;
+        DWORD creation;
+        DWORD gle;
+    } cf_table[] = {
+        {coninW,   0,                            FALSE,      0,                 ERROR_INVALID_PARAMETER},
+        {coninW,   0,                            FALSE,      OPEN_ALWAYS,       0},
+        {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_INVALID_PARAMETER},
+        {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_NEW,        0},
+        {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_ALWAYS,     0},
+        {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       0},
+        {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      TRUNCATE_EXISTING, 0},
+        {conoutW,  0,                            FALSE,      0,                 ERROR_INVALID_PARAMETER},
+        {conoutW,  0,                            FALSE,      OPEN_ALWAYS,       0},
+        {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_INVALID_PARAMETER},
+        {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_NEW,        0},
+        {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_ALWAYS,     0},
+        {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       0},
+        {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      TRUNCATE_EXISTING, 0},
+    };
+
+    int index;
+    HANDLE ret;
+    SECURITY_ATTRIBUTES sa;
+
+    for (index = 0; index < sizeof(cf_table)/sizeof(cf_table[0]); index++)
+    {
+        SetLastError(0xdeadbeef);
+
+        sa.nLength = sizeof(sa);
+        sa.lpSecurityDescriptor = NULL;
+        sa.bInheritHandle = cf_table[index].inherit;
+
+        ret = CreateFileW(cf_table[index].name, cf_table[index].access,
+                          FILE_SHARE_READ|FILE_SHARE_WRITE, &sa,
+                          cf_table[index].creation, FILE_ATTRIBUTE_NORMAL, NULL);
+        if (ret == INVALID_HANDLE_VALUE)
+        {
+            ok(cf_table[index].gle,
+               "Expected CreateFileW not to return INVALID_HANDLE_VALUE for index %d\n", index);
+            ok(GetLastError() == cf_table[index].gle,
+                "Expected GetLastError() to return %u for index %d, got %u\n",
+                cf_table[index].gle, index, GetLastError());
+        }
+        else
+        {
+            ok(!cf_table[index].gle, "Expected CreateFileW to succeed for index %d\n", index);
+            CloseHandle(ret);
+        }
+    }
+}
+
 static void test_VerifyConsoleIoHandle( HANDLE handle )
 {
     BOOL ret;
@@ -2546,6 +2606,7 @@ START_TEST(console)
 
     test_GetConsoleProcessList();
     test_OpenConsoleW();
+    test_CreateFileW();
     test_OpenCON();
     test_VerifyConsoleIoHandle(hConOut);
     test_GetSetStdHandle();




More information about the wine-patches mailing list