From 1dc8d30a5a9467cc2370860c6b8b6038245d34fa Mon Sep 17 00:00:00 2001
From: Thomas Faber <thfabba@gmx.de>
Date: Sun, 6 Nov 2011 17:08:06 +0100
Subject: kernel32/tests: add tests for GetDllDirectoryW

---
 dlls/kernel32/tests/module.c |   80 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/dlls/kernel32/tests/module.c b/dlls/kernel32/tests/module.c
index f02ea08..7569fc1 100644
--- a/dlls/kernel32/tests/module.c
+++ b/dlls/kernel32/tests/module.c
@@ -24,6 +24,8 @@
 #include <windows.h>
 
 static DWORD (WINAPI *pGetDllDirectoryA)(DWORD,LPSTR);
+static DWORD (WINAPI *pGetDllDirectoryW)(DWORD,LPWSTR);
+
 static BOOL (WINAPI *pSetDllDirectoryA)(LPCSTR);
 
 static BOOL is_unicode_enabled = TRUE;
@@ -467,12 +469,89 @@ static void testGetDllDirectoryA(void)
     }
 }
 
+static void testGetDllDirectoryW(void)
+{
+    WCHAR buffer[MAX_PATH];
+    size_t length;
+    DWORD ret;
+    int i, j;
+    const int test_count = sizeof(dll_directories) / sizeof(dll_directories[0]);
+
+    if (!pGetDllDirectoryW)
+    {
+        win_skip("GetDllDirectoryW not available\n");
+        return;
+    }
+    if (!pSetDllDirectoryA)
+    {
+        win_skip("SetDllDirectoryA not available\n");
+        return;
+    }
+
+    for (i = 0; i < test_count; i++)
+    {
+        length = strlen(dll_directories[i]);
+        assert(sizeof(buffer) > length + 1);
+        if (!pSetDllDirectoryA(dll_directories[i]))
+        {
+            skip("i=%d, SetDllDirectoryA failed\n", i);
+            continue;
+        }
+
+        SetLastError(0xdeadbeef);
+        ret = pGetDllDirectoryW(0, NULL);
+        ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
+        ok(GetLastError() == 0xdeadbeef, "Error is %u\n", GetLastError());
+
+        /* buffer of exactly the right size */
+        SetLastError(0xdeadbeef);
+        buffer[length] = 'A';
+        buffer[length + 1] = 'A';
+        ret = pGetDllDirectoryW(length + 1, buffer);
+        ok(ret == length, "i=%d, Expected %u, got %u\n", i, length, ret);
+        ok(GetLastError() == 0xdeadbeef, "i=%d, Error is %u\n", i, GetLastError());
+        ok(buffer[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
+        ok(buffer[ret] == 0, "i=%d, Wrong length or not null terminated\n", i);
+        for (j = 0; buffer[j]; j++)
+            if (buffer[j] != dll_directories[i][j])
+            {
+                ok(0, "i=%d, Wrong path returned: %s\n", i, wine_dbgstr_w(buffer));
+                break;
+            }
+
+        /* zero size buffer */
+        SetLastError(0xdeadbeef);
+        buffer[0] = 'A';
+        ret = pGetDllDirectoryW(0, buffer);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(GetLastError() == 0xdeadbeef, "i=%d, Error is %u\n", i, GetLastError());
+        ok(buffer[0] == 0 || /* XP, 2003 */
+           buffer[0] == 'A', "i=%d, Buffer overflow\n", i);
+
+        /* buffer just one too short */
+        SetLastError(0xdeadbeef);
+        buffer[0] = 'A';
+        ret = pGetDllDirectoryW(length, buffer);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(GetLastError() == 0xdeadbeef, "i=%d, Error is %x\n", i, GetLastError());
+        ok(buffer[0] == 0 || /* XP, 2003 */
+           buffer[0] == 'A', "i=%d, Buffer overflow\n", i);
+
+        /* no buffer, but too short length */
+        SetLastError(0xdeadbeef);
+        ret = pGetDllDirectoryW(length, NULL);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(GetLastError() == 0xdeadbeef, "i=%d, Error is %x\n", i, GetLastError());
+    }
+}
+
 static void init_pointers(void)
 {
     HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
 
 #define MAKEFUNC(f) (p##f = (void*)GetProcAddress(hKernel32, #f))
     MAKEFUNC(GetDllDirectoryA);
+    MAKEFUNC(GetDllDirectoryW);
     MAKEFUNC(SetDllDirectoryA);
 #undef MAKEFUNC
 }
@@ -498,6 +577,7 @@ START_TEST(module)
     testGetModuleFileName_Wrong();
 
     testGetDllDirectoryA();
+    testGetDllDirectoryW();
 
     testLoadLibraryA();
     testNestedLoadLibraryA();
-- 
1.7.1.GIT
