From bd27a79488a5ca6f126eb594e7e734b24f4c0207 Mon Sep 17 00:00:00 2001
From: Thomas Faber <thfabba@gmx.de>
Date: Tue, 8 Nov 2011 19:42:18 +0100
Subject: kernel32/tests: add tests for GetDllDirectory

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

diff --git a/dlls/kernel32/tests/module.c b/dlls/kernel32/tests/module.c
index 07d5892..06cf557 100644
--- a/dlls/kernel32/tests/module.c
+++ b/dlls/kernel32/tests/module.c
@@ -21,6 +21,11 @@
 #include "wine/test.h"
 #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;
 
 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
@@ -386,6 +391,114 @@ static void testLoadLibraryEx(void)
 
 }
 
+static void testGetDllDirectory(void)
+{
+    CHAR bufferA[MAX_PATH];
+    WCHAR bufferW[MAX_PATH];
+    size_t length;
+    DWORD ret;
+    int i;
+    static const char *dll_directories[] =
+    {
+        "",
+        "C:\\Some\\Path",
+        "C:\\Some\\Path\\",
+        "Q:\\A\\Long\\Path with spaces that\\probably\\doesn't exist!",
+    };
+    const int test_count = sizeof(dll_directories) / sizeof(dll_directories[0]);
+
+    if (!pGetDllDirectoryA || !pGetDllDirectoryW)
+    {
+        win_skip("GetDllDirectory 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]);
+        if (!pSetDllDirectoryA(dll_directories[i]))
+        {
+            skip("i=%d, SetDllDirectoryA failed\n", i);
+            continue;
+        }
+
+        /* no buffer, determine length */
+        ret = pGetDllDirectoryA(0, NULL);
+        ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
+
+        ret = pGetDllDirectoryW(0, NULL);
+        ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
+
+        /* buffer of exactly the right size */
+        bufferA[length] = 'A';
+        bufferA[length + 1] = 'A';
+        ret = pGetDllDirectoryA(length + 1, bufferA);
+        ok(ret == length, "i=%d, Expected %u, got %u\n", i, length, ret);
+        ok(bufferA[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
+        ok(strcmp(bufferA, dll_directories[i]) == 0, "i=%d, Wrong path returned: '%s'\n", i, bufferA);
+
+        bufferW[length] = 'A';
+        bufferW[length + 1] = 'A';
+        ret = pGetDllDirectoryW(length + 1, bufferW);
+        ok(ret == length, "i=%d, Expected %u, got %u\n", i, length, ret);
+        ok(bufferW[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
+        ok(cmpStrAW(dll_directories[i], bufferW, length, length),
+           "i=%d, Wrong path returned: %s\n", i, wine_dbgstr_w(bufferW));
+
+        /* zero size buffer
+         * the A version always null-terminates the buffer,
+         * the W version doesn't do it on some platforms */
+        bufferA[0] = 'A';
+        ret = pGetDllDirectoryA(0, bufferA);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(bufferA[0] == 0, "i=%d, Buffer not null terminated\n", i);
+
+        bufferW[0] = 'A';
+        ret = pGetDllDirectoryW(0, bufferW);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(bufferW[0] == 0 || /* XP, 2003 */
+           broken(bufferW[0] == 'A'), "i=%d, Buffer overflow\n", i);
+
+        /* buffer just one too short */
+        bufferA[0] = 'A';
+        ret = pGetDllDirectoryA(length, bufferA);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(bufferA[0] == 0, "i=%d, Buffer not null terminated\n", i);
+
+        bufferW[0] = 'A';
+        ret = pGetDllDirectoryW(length, bufferW);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+        ok(bufferW[0] == 0 || /* XP, 2003 */
+           broken(bufferW[0] == 'A'), "i=%d, Buffer overflow\n", i);
+
+        /* no buffer, but too short length */
+        ret = pGetDllDirectoryA(length, NULL);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+
+        ret = pGetDllDirectoryW(length, NULL);
+        ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
+    }
+
+    /* unset whatever we did so following tests won't be affected */
+    pSetDllDirectoryA(NULL);
+}
+
+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
+}
+
 START_TEST(module)
 {
     WCHAR filenameW[MAX_PATH];
@@ -400,10 +513,14 @@ START_TEST(module)
         is_unicode_enabled = FALSE;
     }
 
+    init_pointers();
+
     testGetModuleFileName(NULL);
     testGetModuleFileName("kernel32.dll");
     testGetModuleFileName_Wrong();
 
+    testGetDllDirectory();
+
     testLoadLibraryA();
     testNestedLoadLibraryA();
     testLoadLibraryA_Wrong();
-- 
1.7.1.GIT
