Merge pull request #3399 from nfedera/fix-winpr-library2

winpr/library: fix GetModuleFileName and tests
This commit is contained in:
Bernhard Miklautz 2016-06-10 15:08:49 +02:00 committed by GitHub
commit f818bab4b6
7 changed files with 113 additions and 126 deletions

View 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;
}

View 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")

View 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")

View 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")

View 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);

View 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"));
return -1;
}
MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR));
BasePath[length] = 0;
#else
BasePath = _strdup(str);
if (!BasePath)
{
printf("Memory allocation failed");
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 */
/* 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);
library = LoadLibrary(LibraryPath);
if (!library)
{
_tprintf(_T("LoadLibrary failure\n"));
printf("%s: GetModuleFilenameA failed: 0x%08X\n", __FUNCTION__, GetLastError());
return -1;
}
pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA");
/* PathCchRemoveFileSpec is not implemented in WinPR */
if (!pFunctionA)
if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
{
_tprintf(_T("GetProcAddress failure (FunctionA)\n"));
printf("%s: Error identifying module directory path\n", __FUNCTION__);
return -1;
}
*p = 0;
NativePathCchAppendA(LibraryPath, PATHCCH_MAX_CCH, "TestLibraryA");
SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
NativePathCchAddExtensionA(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension);
printf("%s: Loading Library: '%s'\n", __FUNCTION__, LibraryPath);
if (!(library = LoadLibraryA(LibraryPath)))
{
printf("%s: LoadLibraryA failure: 0x%08X\n", __FUNCTION__, GetLastError());
return -1;
}
pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB");
if (!pFunctionB)
if (!(pFunctionA = (TEST_AB_FN) GetProcAddress(library, "FunctionA")))
{
_tprintf(_T("GetProcAddress failure (FunctionB)\n"));
printf("%s: GetProcAddress failure (FunctionA)\n", __FUNCTION__);
return -1;
}
if (!(pFunctionB = (TEST_AB_FN) GetProcAddress(library, "FunctionB")))
{
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;
}

View 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"));
return -1;
}
MultiByteToWideChar(CP_UTF8, 0, str, length, (LPWSTR) BasePath, length * sizeof(WCHAR));
BasePath[length] = 0;
#else
BasePath = _strdup(str);
if (!BasePath)
{
printf("Memory allocation failed");
printf("%s: GetModuleFilenameA failed: 0x%08X\n", __FUNCTION__, GetLastError());
return -1;
}
length = strlen(BasePath);
#endif
/* PathCchRemoveFileSpec is not implemented in WinPR */
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);
library = LoadLibrary(LibraryPath);
if (!library)
if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
{
_tprintf(_T("LoadLibrary failure\n"));
printf("%s: Error identifying module directory path\n", __FUNCTION__);
return -1;
}
*p = 0;
NativePathCchAppendA(LibraryPath, PATHCCH_MAX_CCH, "TestLibraryA");
SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
NativePathCchAddExtensionA(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension);
printf("%s: Loading Library: '%s'\n", __FUNCTION__, LibraryPath);
if (!(library = LoadLibraryA(LibraryPath)))
{
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;
}