diff options
Diffstat (limited to 'winpr/libwinpr/library/test')
8 files changed, 316 insertions, 0 deletions
diff --git a/winpr/libwinpr/library/test/CMakeLists.txt b/winpr/libwinpr/library/test/CMakeLists.txt new file mode 100644 index 0000000..dca2b25 --- /dev/null +++ b/winpr/libwinpr/library/test/CMakeLists.txt @@ -0,0 +1,33 @@ + +set(MODULE_NAME "TestLibrary") +set(MODULE_PREFIX "TEST_LIBRARY") + +set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c) + +set(${MODULE_PREFIX}_TESTS + TestLibraryLoadLibrary.c + TestLibraryGetProcAddress.c + TestLibraryGetModuleFileName.c) + +create_test_sourcelist(${MODULE_PREFIX}_SRCS + ${${MODULE_PREFIX}_DRIVER} + ${${MODULE_PREFIX}_TESTS}) + +add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS}) + +target_link_libraries(${MODULE_NAME} winpr) + +set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}") + +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}) +endforeach() + +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test") + +add_subdirectory(TestLibraryA) +add_subdirectory(TestLibraryB) + diff --git a/winpr/libwinpr/library/test/TestLibraryA/CMakeLists.txt b/winpr/libwinpr/library/test/TestLibraryA/CMakeLists.txt new file mode 100644 index 0000000..73d8b7e --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryA/CMakeLists.txt @@ -0,0 +1,29 @@ +# WinPR: Windows Portable Runtime +# libwinpr-library cmake build script +# +# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set(MODULE_NAME "TestLibraryA") +set(MODULE_PREFIX "TEST_LIBRARY_A") + +set(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_SRCS} TestLibraryA.c) + +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}") + +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test/Extra") diff --git a/winpr/libwinpr/library/test/TestLibraryA/TestLibraryA.c b/winpr/libwinpr/library/test/TestLibraryA/TestLibraryA.c new file mode 100644 index 0000000..d11bc4d --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryA/TestLibraryA.c @@ -0,0 +1,14 @@ +#include <winpr/spec.h> + +DECLSPEC_EXPORT int FunctionA(int a, int b); +DECLSPEC_EXPORT int FunctionB(int a, int b); + +int FunctionA(int a, int b) +{ + return (a * b); /* multiply */ +} + +int FunctionB(int a, int b) +{ + return (a / b); /* divide */ +} diff --git a/winpr/libwinpr/library/test/TestLibraryB/CMakeLists.txt b/winpr/libwinpr/library/test/TestLibraryB/CMakeLists.txt new file mode 100644 index 0000000..860df8c --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryB/CMakeLists.txt @@ -0,0 +1,30 @@ +# WinPR: Windows Portable Runtime +# libwinpr-library cmake build script +# +# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set(MODULE_NAME "TestLibraryB") +set(MODULE_PREFIX "TEST_LIBRARY_B") + +set(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_SRCS} TestLibraryB.c) + +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}") + +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test/Extra") + diff --git a/winpr/libwinpr/library/test/TestLibraryB/TestLibraryB.c b/winpr/libwinpr/library/test/TestLibraryB/TestLibraryB.c new file mode 100644 index 0000000..eac586e --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryB/TestLibraryB.c @@ -0,0 +1,14 @@ +#include <winpr/spec.h> + +DECLSPEC_EXPORT int FunctionA(int a, int b); +DECLSPEC_EXPORT int FunctionB(int a, int b); + +int FunctionA(int a, int b) +{ + return (a + b); /* add */ +} + +int FunctionB(int a, int b) +{ + return (a - b); /* subtract */ +} diff --git a/winpr/libwinpr/library/test/TestLibraryGetModuleFileName.c b/winpr/libwinpr/library/test/TestLibraryGetModuleFileName.c new file mode 100644 index 0000000..714522a --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryGetModuleFileName.c @@ -0,0 +1,56 @@ + +#include <stdio.h> +#include <winpr/crt.h> +#include <winpr/path.h> +#include <winpr/tchar.h> +#include <winpr/windows.h> +#include <winpr/library.h> + +int TestLibraryGetModuleFileName(int argc, char* argv[]) +{ + char ModuleFileName[4096]; + DWORD len = 0; + WINPR_UNUSED(argc); + WINPR_UNUSED(argv); + /* Test insufficient buffer size behaviour */ + SetLastError(ERROR_SUCCESS); + len = GetModuleFileNameA(NULL, ModuleFileName, 2); + if (len != 2) + { + printf("%s: GetModuleFileNameA unexpectedly returned %" PRIu32 " instead of 2\n", __func__, + len); + return -1; + } + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { + printf("%s: Invalid last error value: 0x%08" PRIX32 + ". Expected 0x%08X (ERROR_INSUFFICIENT_BUFFER)\n", + __func__, 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%08" PRIX32 "\n", __func__, + GetLastError()); + return -1; + } + if (len == sizeof(ModuleFileName)) + { + printf("%s: GetModuleFileNameA unexpectedly returned nSize\n", __func__); + return -1; + } + if (GetLastError() != ERROR_SUCCESS) + { + printf("%s: Invalid last error value: 0x%08" PRIX32 ". Expected 0x%08X (ERROR_SUCCESS)\n", + __func__, GetLastError(), ERROR_SUCCESS); + return -1; + } + + printf("GetModuleFileNameA: %s\n", ModuleFileName); + + return 0; +} diff --git a/winpr/libwinpr/library/test/TestLibraryGetProcAddress.c b/winpr/libwinpr/library/test/TestLibraryGetProcAddress.c new file mode 100644 index 0000000..f8f54a6 --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryGetProcAddress.c @@ -0,0 +1,89 @@ + +#include <stdio.h> +#include <winpr/crt.h> +#include <winpr/path.h> +#include <winpr/tchar.h> +#include <winpr/windows.h> +#include <winpr/library.h> + +typedef int (*TEST_AB_FN)(int a, int b); + +int TestLibraryGetProcAddress(int argc, char* argv[]) +{ + int a = 0; + int b = 0; + int c = 0; + HINSTANCE library = NULL; + TEST_AB_FN pFunctionA = NULL; + TEST_AB_FN pFunctionB = NULL; + LPCSTR SharedLibraryExtension = NULL; + CHAR LibraryPath[PATHCCH_MAX_CCH]; + PCHAR p = NULL; + WINPR_UNUSED(argc); + WINPR_UNUSED(argv); + if (!GetModuleFileNameA(NULL, LibraryPath, PATHCCH_MAX_CCH)) + { + printf("%s: GetModuleFilenameA failed: 0x%08" PRIX32 "\n", __func__, GetLastError()); + return -1; + } + + /* PathCchRemoveFileSpec is not implemented in WinPR */ + + if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE)))) + { + printf("%s: Error identifying module directory path\n", __func__); + 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", __func__, LibraryPath); + + if (!(library = LoadLibraryA(LibraryPath))) + { + printf("%s: LoadLibraryA failure: 0x%08" PRIX32 "\n", __func__, GetLastError()); + return -1; + } + + if (!(pFunctionA = (TEST_AB_FN)GetProcAddress(library, "FunctionA"))) + { + printf("%s: GetProcAddress failure (FunctionA)\n", __func__); + return -1; + } + + if (!(pFunctionB = (TEST_AB_FN)GetProcAddress(library, "FunctionB"))) + { + printf("%s: GetProcAddress failure (FunctionB)\n", __func__); + return -1; + } + + a = 2; + b = 3; + c = pFunctionA(a, b); /* LibraryA / FunctionA multiplies a and b */ + + if (c != (a * b)) + { + printf("%s: pFunctionA call failed\n", __func__); + return -1; + } + + a = 10; + b = 5; + c = pFunctionB(a, b); /* LibraryA / FunctionB divides a by b */ + + if (c != (a / b)) + { + printf("%s: pFunctionB call failed\n", __func__); + return -1; + } + + if (!FreeLibrary(library)) + { + printf("%s: FreeLibrary failure: 0x%08" PRIX32 "\n", __func__, GetLastError()); + return -1; + } + + return 0; +} diff --git a/winpr/libwinpr/library/test/TestLibraryLoadLibrary.c b/winpr/libwinpr/library/test/TestLibraryLoadLibrary.c new file mode 100644 index 0000000..4bd6e7c --- /dev/null +++ b/winpr/libwinpr/library/test/TestLibraryLoadLibrary.c @@ -0,0 +1,51 @@ + +#include <stdio.h> +#include <winpr/crt.h> +#include <winpr/path.h> +#include <winpr/tchar.h> +#include <winpr/windows.h> +#include <winpr/library.h> + +int TestLibraryLoadLibrary(int argc, char* argv[]) +{ + HINSTANCE library = NULL; + LPCSTR SharedLibraryExtension = NULL; + CHAR LibraryPath[PATHCCH_MAX_CCH]; + PCHAR p = NULL; + WINPR_UNUSED(argc); + WINPR_UNUSED(argv); + if (!GetModuleFileNameA(NULL, LibraryPath, PATHCCH_MAX_CCH)) + { + printf("%s: GetModuleFilenameA failed: 0x%08" PRIX32 "\n", __func__, GetLastError()); + return -1; + } + + /* PathCchRemoveFileSpec is not implemented in WinPR */ + + if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE)))) + { + printf("%s: Error identifying module directory path\n", __func__); + 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", __func__, LibraryPath); + + if (!(library = LoadLibraryA(LibraryPath))) + { + printf("%s: LoadLibraryA failure: 0x%08" PRIX32 "\n", __func__, GetLastError()); + return -1; + } + + if (!FreeLibrary(library)) + { + printf("%s: FreeLibrary failure: 0x%08" PRIX32 "\n", __func__, GetLastError()); + return -1; + } + + return 0; +} |