winpr/library: fix GetModuleFileName and tests
authorNorbert Federa <norbert.federa@thincast.com>
Fri, 10 Jun 2016 11:10:39 +0000 (13:10 +0200)
committerNorbert Federa <norbert.federa@thincast.com>
Fri, 10 Jun 2016 11:12:08 +0000 (13:12 +0200)
- Use correct SetLastError values in GetModuleFileName
- Fix wrong return codes in GetModuleFileName
- Build the TestLibraryA/TestLibraryB libraries always shared and
  put them in the test output directory
- TestLibraryGetModuleFileName always returned success
- Improve TestLibraryGetModuleFileName to also check last error values
  and insufficient buffer sizes
- Change TestLibraryGetProcAddress and TestLibraryLoadLibrary to load
  the TestLibrary from the test executable's directory

winpr/libwinpr/library/library.c
winpr/libwinpr/library/test/CMakeLists.txt
winpr/libwinpr/library/test/TestLibraryA/CMakeLists.txt
winpr/libwinpr/library/test/TestLibraryB/CMakeLists.txt
winpr/libwinpr/library/test/TestLibraryGetModuleFileName.c
winpr/libwinpr/library/test/TestLibraryGetProcAddress.c
winpr/libwinpr/library/test/TestLibraryLoadLibrary.c

index 2bd6ecf..c27d256 100644 (file)
@@ -213,6 +213,8 @@ HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
 
 DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
 {
+       WLog_ERR(TAG, "%s is not implemented", __FUNCTION__);
+       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
        return 0;
 }
 
@@ -230,7 +232,10 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
                status = readlink(path, buffer, sizeof(buffer));
 
                if (status < 0)
+               {
+                       SetLastError(ERROR_INTERNAL_ERROR);
                        return 0;
+               }
 
                buffer[status] = '\0';
                length = strlen(buffer);
@@ -239,14 +244,13 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
                {
                        CopyMemory(lpFilename, buffer, length);
                        lpFilename[length] = '\0';
-               }
-               else
-               {
-                       CopyMemory(lpFilename, buffer, nSize - 1);
-                       lpFilename[nSize - 1] = '\0';
+                       return length;
                }
 
-               return 0;
+               CopyMemory(lpFilename, buffer, nSize - 1);
+               lpFilename[nSize - 1] = '\0';
+               SetLastError(ERROR_INSUFFICIENT_BUFFER);
+               return nSize;
        }
 
 #elif defined(__MACOSX__)
@@ -263,6 +267,7 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
                if (status != 0)
                {
                        /* path too small */
+                       SetLastError(ERROR_INTERNAL_ERROR);
                        return 0;
                }
 
@@ -277,17 +282,18 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
                {
                        CopyMemory(lpFilename, buffer, length);
                        lpFilename[length] = '\0';
-               }
-               else
-               {
-                       CopyMemory(lpFilename, buffer, nSize - 1);
-                       lpFilename[nSize - 1] = '\0';
+                       return length;
                }
 
-               return 0;
+               CopyMemory(lpFilename, buffer, nSize - 1);
+               lpFilename[nSize - 1] = '\0';
+               SetLastError(ERROR_INSUFFICIENT_BUFFER);
+               return nSize;
        }
 
 #endif
+       WLog_ERR(TAG, "%s is not implemented", __FUNCTION__);
+       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
        return 0;
 }
 
index ca57ad3..dca2b25 100644 (file)
@@ -23,7 +23,7 @@ set(TEST_AREA "${MODULE_NAME}Area")
 
 foreach(test ${${MODULE_PREFIX}_TESTS})
        get_filename_component(TestName ${test} NAME_WE)
-       add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName} "${TESTING_OUTPUT_DIRECTORY}/${TEST_AREA}")
+       add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName})
 endforeach()
 
 set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")
index f933d20..d67d2b3 100644 (file)
@@ -28,6 +28,6 @@ add_library(${MODULE_NAME} SHARED ${${MODULE_PREFIX}_SRCS})
 
 set_target_properties(${MODULE_NAME} PROPERTIES PREFIX "")
 
-set_target_properties(${MODULE_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}/${TEST_AREA}/${MODULE_NAME}")
+set_target_properties(${MODULE_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
 
 set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test/Extra")
index 3562c88..7491901 100644 (file)
@@ -28,7 +28,7 @@ add_library(${MODULE_NAME} SHARED ${${MODULE_PREFIX}_SRCS})
 
 set_target_properties(${MODULE_NAME} PROPERTIES PREFIX "")
 
-set_target_properties(${MODULE_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}/${TEST_AREA}/${MODULE_NAME}")
+set_target_properties(${MODULE_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
 
 set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test/Extra")
 
index df9fa16..ddbb3e0 100644 (file)
@@ -9,8 +9,45 @@
 int TestLibraryGetModuleFileName(int argc, char* argv[])
 {
        char ModuleFileName[4096];
+       DWORD len;
 
-       GetModuleFileNameA(NULL, ModuleFileName, sizeof(ModuleFileName));
+       /* Test insufficient buffer size behaviour */
+       SetLastError(ERROR_SUCCESS);
+       len = GetModuleFileNameA(NULL, ModuleFileName, 2);
+       if (len != 2)
+       {
+               printf("%s: GetModuleFileNameA unexpectedly returned %u instead of 2\n",
+                       __FUNCTION__, len);
+               return -1;
+       }
+       if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+       {
+               printf("%s: Invalid last error value: 0x%08X. Expected 0x%08X (ERROR_INSUFFICIENT_BUFFER)\n",
+                       __FUNCTION__, GetLastError(), ERROR_INSUFFICIENT_BUFFER);
+               return -1;
+       }
+
+       /* Test with real/sufficient buffer size */
+       SetLastError(ERROR_SUCCESS);
+       len = GetModuleFileNameA(NULL, ModuleFileName, sizeof(ModuleFileName));
+       if (len == 0)
+       {
+               printf("%s: GetModuleFileNameA failed with error 0x%08X\n",
+                       __FUNCTION__, GetLastError());
+               return -1;
+       }
+       if (len == sizeof(ModuleFileName))
+       {
+               printf("%s: GetModuleFileNameA unexpectedly returned nSize\n",
+                       __FUNCTION__);
+               return -1;
+       }
+       if (GetLastError() != ERROR_SUCCESS)
+       {
+               printf("%s: Invalid last error value: 0x%08X. Expected 0x%08X (ERROR_SUCCESS)\n",
+                       __FUNCTION__, GetLastError(), ERROR_SUCCESS);
+               return -1;
+       }
 
        printf("GetModuleFileNameA: %s\n", ModuleFileName);
 
index 47f145a..319fea4 100644 (file)
@@ -14,76 +14,46 @@ int TestLibraryGetProcAddress(int argc, char* argv[])
        HINSTANCE library;
        TEST_AB_FN pFunctionA;
        TEST_AB_FN pFunctionB;
-       LPCTSTR SharedLibraryExtension;
-       TCHAR LibraryPath[PATHCCH_MAX_CCH];
+       LPCSTR SharedLibraryExtension;
+       CHAR LibraryPath[PATHCCH_MAX_CCH];
+       PCHAR p;
 
-#ifndef _WIN32
-       char* str;
-       int length;
-       LPTSTR BasePath;
-
-       str = argv[1];
-
-#ifdef UNICODE
-       length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0);
-       BasePath = (WCHAR*) malloc((length + 1) * sizeof(WCHAR));
-       if (!BasePath)
+       if (!GetModuleFileNameA(NULL, LibraryPath, PATHCCH_MAX_CCH))
        {
-               _tprintf(_T("Memory allocation failed\n"));
+               printf("%s: GetModuleFilenameA failed: 0x%08X\n", __FUNCTION__, GetLastError());
                return -1;
        }
-       MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR));
-       BasePath[length] = 0;
-#else
-       BasePath = _strdup(str);
-       if (!BasePath)
+
+       /* PathCchRemoveFileSpec is not implemented in WinPR */
+
+       if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
        {
-               printf("Memory allocation failed");
+               printf("%s: Error identifying module directory path\n", __FUNCTION__);
                return -1;
        }
-       length = strlen(BasePath);
-#endif
-       
-       CopyMemory(LibraryPath, BasePath, length * sizeof(TCHAR));
-       LibraryPath[length] = 0;
-
-       NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* subdirectory */
-
-#else /* _WIN32 */
+       *p = 0;
 
-       /* On Windows the test libraries are in same folder as the test executable */
-       GetModuleFileName(NULL, LibraryPath, PATHCCH_MAX_CCH);
-       PathRemoveFileSpec(LibraryPath);
-#endif
+       NativePathCchAppendA(LibraryPath, PATHCCH_MAX_CCH, "TestLibraryA");
+       SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
+       NativePathCchAddExtensionA(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension);
 
-       NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* file name without extension */
+       printf("%s: Loading Library: '%s'\n", __FUNCTION__, LibraryPath);
 
-       SharedLibraryExtension = PathGetSharedLibraryExtension(PATH_SHARED_LIB_EXT_WITH_DOT);
-       NativePathCchAddExtension(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension); /* add shared library extension */
-
-       _tprintf(_T("Loading Library: %s\n"), LibraryPath);
-
-       library = LoadLibrary(LibraryPath);
-
-       if (!library)
+       if (!(library = LoadLibraryA(LibraryPath)))
        {
-               _tprintf(_T("LoadLibrary failure\n"));
+               printf("%s: LoadLibraryA failure: 0x%08X\n", __FUNCTION__, GetLastError());
                return -1;
        }
 
-       pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA");
-
-       if (!pFunctionA)
+       if (!(pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA")))
        {
-               _tprintf(_T("GetProcAddress failure (FunctionA)\n"));
+               printf("%s: GetProcAddress failure (FunctionA)\n", __FUNCTION__);
                return -1;
        }
 
-       pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB");
-
-       if (!pFunctionB)
+       if (!(pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB")))
        {
-               _tprintf(_T("GetProcAddress failure (FunctionB)\n"));
+               printf("%s: GetProcAddress failure (FunctionB)\n", __FUNCTION__);
                return -1;
        }
 
@@ -94,7 +64,7 @@ int TestLibraryGetProcAddress(int argc, char* argv[])
 
        if (c != (a * b))
        {
-               _tprintf(_T("pFunctionA call failed\n"));
+               printf("%s: pFunctionA call failed\n", __FUNCTION__);
                return -1;
        }
 
@@ -105,13 +75,13 @@ int TestLibraryGetProcAddress(int argc, char* argv[])
 
        if (c != (a / b))
        {
-               _tprintf(_T("pFunctionB call failed\n"));
+               printf("%s: pFunctionB call failed\n", __FUNCTION__);
                return -1;
        }
 
        if (!FreeLibrary(library))
        {
-               _tprintf(_T("FreeLibrary failure\n"));
+               printf("%s: FreeLibrary failure: 0x%08X\n", __FUNCTION__, GetLastError());
                return -1;
        }
 
index 084fb81..9421f8a 100644 (file)
@@ -9,66 +9,40 @@
 int TestLibraryLoadLibrary(int argc, char* argv[])
 {
        HINSTANCE library;
-       LPCTSTR SharedLibraryExtension;
-       TCHAR LibraryPath[PATHCCH_MAX_CCH];
+       LPCSTR SharedLibraryExtension;
+       CHAR LibraryPath[PATHCCH_MAX_CCH];
+       PCHAR p;
 
-#ifndef _WIN32
-       char* str;
-       int length;
-       LPTSTR BasePath;
-       str = argv[1];
-
-#ifdef UNICODE
-       length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0);
-       BasePath = (WCHAR*) malloc((length + 1) * sizeof(WCHAR));
-       if (!BasePath)
+       if (!GetModuleFileNameA(NULL, LibraryPath, PATHCCH_MAX_CCH))
        {
-               _tprintf(_T("Memory allocation failed\n"));
+               printf("%s: GetModuleFilenameA failed: 0x%08X\n", __FUNCTION__, GetLastError());
                return -1;
        }
-       MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR));
-       BasePath[length] = 0;
-#else
-       BasePath = _strdup(str);
-       if (!BasePath)
+
+       /* PathCchRemoveFileSpec is not implemented in WinPR */
+
+       if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
        {
-               printf("Memory allocation failed");
+               printf("%s: Error identifying module directory path\n", __FUNCTION__);
                return -1;
        }
+       *p = 0;
 
-       length = strlen(BasePath);
-#endif
-
-       CopyMemory(LibraryPath, BasePath, length * sizeof(TCHAR));
-       LibraryPath[length] = 0;
-
-       NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* subdirectory */
-
-#else /* _WIN32 */
-
-       /* On Windows the test libraries are in same folder as the test executable */
-       GetModuleFileName(NULL, LibraryPath, PATHCCH_MAX_CCH);
-       PathRemoveFileSpec(LibraryPath);
-#endif
-
-       NativePathCchAppend(LibraryPath, PATHCCH_MAX_CCH, _T("TestLibraryA")); /* file name without extension */
-
-       SharedLibraryExtension = PathGetSharedLibraryExtension(PATH_SHARED_LIB_EXT_WITH_DOT);
-       NativePathCchAddExtension(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension); /* add shared library extension */
-
-       _tprintf(_T("Loading Library: %s\n"), LibraryPath);
+       NativePathCchAppendA(LibraryPath, PATHCCH_MAX_CCH, "TestLibraryA");
+       SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
+       NativePathCchAddExtensionA(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension);
 
-       library = LoadLibrary(LibraryPath);
+       printf("%s: Loading Library: '%s'\n", __FUNCTION__, LibraryPath);
 
-       if (!library)
+       if (!(library = LoadLibraryA(LibraryPath)))
        {
-               _tprintf(_T("LoadLibrary failure\n"));
+               printf("%s: LoadLibraryA failure: 0x%08X\n", __FUNCTION__, GetLastError());
                return -1;
        }
 
        if (!FreeLibrary(library))
        {
-               _tprintf(_T("FreeLibrary failure\n"));
+               printf("%s: FreeLibrary failure: 0x%08X\n", __FUNCTION__, GetLastError());
                return -1;
        }