[v2 5/5] kernel32/tests: Add tests for LoadLibrary with LIBRARY_SEARCH_* flags

Carlos Palminha CARLOS.PALMINHA at synopsys.com
Tue Aug 8 09:01:52 CDT 2017


Test LIBRARY_SEARCH_* flags when loading libraries.
Uses helper from path.c to create the test dll file.

Signed-off-by: Carlos Palminha <palminha at synopsys.com>
---
 dlls/kernel32/tests/module.c | 107 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 106 insertions(+), 1 deletion(-)

diff --git a/dlls/kernel32/tests/module.c b/dlls/kernel32/tests/module.c
index bb22c24316c..d8be5aee82a 100644
--- a/dlls/kernel32/tests/module.c
+++ b/dlls/kernel32/tests/module.c
@@ -407,6 +407,111 @@ static void testLoadLibraryEx(void)
     FreeLibrary(hmodule);
 }
 
+/* use some functions and strcutures from loader.c */
+extern DWORD create_test_dll( const IMAGE_DOS_HEADER *dos_header, UINT dos_size,
+                              const IMAGE_NT_HEADERS *nt_header, const char *dll_name );
+extern const IMAGE_NT_HEADERS nt_header_template;
+
+static void testLoadLibraryExSearchFlags(void)
+{
+    HMODULE hmodule;
+    char tmpPath[MAX_PATH];
+    char dllName[] = "testFlags.dll";
+    char tmpFullFile[MAX_PATH];
+    char sysFullFile[MAX_PATH];
+    IMAGE_DOS_HEADER dos_header;
+    IMAGE_NT_HEADERS nt_header;
+    ULONG file_size;
+    char fullPath[MAX_PATH];
+
+    GetTempPathA(MAX_PATH, tmpPath);
+    lstrcpyA(tmpFullFile, tmpPath);
+    lstrcatA(tmpFullFile, dllName);
+
+    /* check for required function */
+    if (!pSetDllDirectoryA)
+    {
+        win_skip("SetDllDirectoryA not available\n");
+        return;
+    }
+
+    /* try to load test dll from test path before creating it*/
+    SetLastError(0xdeadbeef);
+    pSetDllDirectoryA(tmpPath);
+    hmodule = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_USER_DIRS);
+    ok (hmodule == NULL, "Test library %s should not be loaded from %s\n", dllName, tmpPath);
+    ok( GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_DLL_NOT_FOUND || GetLastError() == ERROR_INVALID_PARAMETER,
+        "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND (win9x) or ERROR_INVALID_PARAMETER (win2003), got %d\n", GetLastError());
+    FreeLibrary(hmodule);
+    pSetDllDirectoryA(NULL);
+
+    /* create test library */
+    lstrcpyA(fullPath, tmpPath);
+    lstrcatA(fullPath, dllName);
+
+    dos_header.e_magic = IMAGE_DOS_SIGNATURE;
+    dos_header.e_lfanew = sizeof(dos_header);
+
+    nt_header = nt_header_template;
+
+    nt_header.FileHeader.NumberOfSections = 1;
+    nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
+    nt_header.OptionalHeader.SectionAlignment = 0x1000;
+    nt_header.OptionalHeader.FileAlignment = 0x1000;
+    nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000;
+    nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER);
+
+    file_size = create_test_dll(&dos_header, sizeof(dos_header), &nt_header, fullPath);
+    ok( file_size, "could not create '%s'\n",fullPath);
+
+    /* try to load test dll from test path */
+    SetLastError(0xdeadbeef);
+    pSetDllDirectoryA(tmpPath);
+    hmodule = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_USER_DIRS);
+    ok (hmodule!=NULL, "Could not load test library %s from %s\n", dllName, tmpPath);
+    ok(GetLastError() == 0xdeadbeef || GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_INVALID_PARAMETER,
+       "Expected 0xdeadbeef or ERROR_SUCCESS or ERROR_INVALID_PARAMETER (win2003), got %d\n", GetLastError());
+    FreeLibrary(hmodule);
+    pSetDllDirectoryA(NULL);
+
+    /* load non-existing test dll from system32 */
+    SetLastError(0xdeadbeef);
+    hmodule = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
+    ok(hmodule == NULL, "%s should not be loaded\n", dllName);
+    ok( GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_DLL_NOT_FOUND || GetLastError() == ERROR_INVALID_PARAMETER,
+        "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND (win9x) or ERROR_INVALID_PARAMETER (win2003), got %d\n", GetLastError());
+    FreeLibrary(hmodule);
+
+    /* move library from temp path to system32 and delete from old path */
+    GetWindowsDirectoryA(sysFullFile,MAX_PATH);
+    lstrcatA(sysFullFile,"\\system32\\");
+    lstrcatA(sysFullFile, dllName);
+    MoveFileA(tmpFullFile,sysFullFile);
+    DeleteFileA(tmpFullFile);
+
+    /* try to load dll using more than one flag */
+    pSetDllDirectoryA(tmpPath);
+    SetLastError(0xdeadbeef);
+    hmodule = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_LIBRARY_SEARCH_SYSTEM32);
+    ok (hmodule != NULL, "Library %s was expected to be loaded from 'system32'\n", dllName);
+    ok(GetLastError() == 0xdeadbeef || GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_INVALID_PARAMETER,
+       "Expected 0xdeadbeef or ERROR_SUCCESS or ERROR_INVALID_PARAMETER (win2003), got %d\n", GetLastError());
+    FreeLibrary(hmodule);
+    pSetDllDirectoryA(NULL);
+
+    /* try to load test dll from system32 */
+    SetLastError(0xdeadbeef);
+    hmodule = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
+    ok (hmodule != NULL, "Library %s was expected to be loaded from 'system32'\n", dllName);
+    ok(GetLastError() == 0xdeadbeef || GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_INVALID_PARAMETER,
+       "Expected 0xdeadbeef or ERROR_SUCCESS or ERROR_INVALID_PARAMETER (win2003), got %d\n", GetLastError());
+    FreeLibrary(hmodule);
+
+    /* delete test dll from system32 directory */
+    DeleteFileA(sysFullFile);
+
+}
+
 static void testGetDllDirectory(void)
 {
     CHAR bufferA[MAX_PATH];
@@ -744,7 +849,6 @@ START_TEST(module)
     WCHAR filenameW[MAX_PATH];
 
     /* Test if we can use GetModuleFileNameW */
-
     SetLastError(0xdeadbeef);
     GetModuleFileNameW(NULL, filenameW, MAX_PATH);
     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
@@ -766,6 +870,7 @@ START_TEST(module)
     testLoadLibraryA_Wrong();
     testGetProcAddress_Wrong();
     testLoadLibraryEx();
+    testLoadLibraryExSearchFlags();
     testGetModuleHandleEx();
     testK32GetModuleInformation();
 }
-- 
2.11.0




More information about the wine-patches mailing list