From e325e4db2831caf38f60ede0223a19604426046b Mon Sep 17 00:00:00 2001
From: Thomas Faber <thfabba@gmx.de>
Date: Sun, 6 Nov 2011 12:57:36 +0100
Subject: kernel32/tests: add tests for GetDllDirectoryA

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

diff --git a/dlls/kernel32/tests/module.c b/dlls/kernel32/tests/module.c
index 07d5892..f02ea08 100644
--- a/dlls/kernel32/tests/module.c
+++ b/dlls/kernel32/tests/module.c
@@ -18,9 +18,14 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include <assert.h>
+
 #include "wine/test.h"
 #include <windows.h>
 
+static DWORD (WINAPI *pGetDllDirectoryA)(DWORD,LPSTR);
+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,92 @@ static void testLoadLibraryEx(void)
 
 }
 
+static const char *dll_directories[] =
+{
+    "C:\\Windows",
+    "Q:\\A\\Long\\Path with spaces that\\probably\\doesn't exist!",
+    "" /* check this last, so the path will be unset again */
+};
+
+static void testGetDllDirectoryA(void)
+{
+    CHAR buffer[MAX_PATH];
+    size_t length;
+    DWORD ret;
+    int i;
+    const int test_count = sizeof(dll_directories) / sizeof(dll_directories[0]);
+
+    if (!pGetDllDirectoryA)
+    {
+        win_skip("GetDllDirectoryA 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 = pGetDllDirectoryA(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 = pGetDllDirectoryA(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);
+        ok(strcmp(buffer, dll_directories[i]) == 0, "i=%d, Wrong path returned: '%s'\n", i, buffer);
+
+        /* zero size buffer */
+        SetLastError(0xdeadbeef);
+        buffer[0] = 'A';
+        ret = pGetDllDirectoryA(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, "i=%d, Buffer not null terminated\n", i);
+
+        /* buffer just one too short. The A version always null-terminates the buffer */
+        SetLastError(0xdeadbeef);
+        buffer[0] = 'A';
+        ret = pGetDllDirectoryA(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, "i=%d, Buffer not null terminated\n", i);
+
+        /* no buffer, but too short length */
+        SetLastError(0xdeadbeef);
+        ret = pGetDllDirectoryA(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(SetDllDirectoryA);
+#undef MAKEFUNC
+}
+
 START_TEST(module)
 {
     WCHAR filenameW[MAX_PATH];
@@ -400,10 +491,14 @@ START_TEST(module)
         is_unicode_enabled = FALSE;
     }
 
+    init_pointers();
+
     testGetModuleFileName(NULL);
     testGetModuleFileName("kernel32.dll");
     testGetModuleFileName_Wrong();
 
+    testGetDllDirectoryA();
+
     testLoadLibraryA();
     testNestedLoadLibraryA();
     testLoadLibraryA_Wrong();
-- 
1.7.1.GIT
