Skip to content

Commit

Permalink
Extend ExcHndl tests for ExcHndlSetLogFileNameW
Browse files Browse the repository at this point in the history
As a lot of testing code also had to be adjusted for usage of wide character strings, it was easiest to split the existing test code by adding another compile time flag `TEST_UNICODE`.
The flag is not named `UNICODE`, as this name is already used by the Windows API.

A macro `WFILE` is added that expands to a wide character string literal containing `__FILE__`.

The output file name for the unicode tests contains additional unicode characters to properly test the behavior.
  • Loading branch information
Robyt3 committed Oct 1, 2022
1 parent 91949b8 commit f5a7978
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 20 deletions.
72 changes: 56 additions & 16 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,41 +193,81 @@ endif ()


#
# test_exchndl
# test_exchndl_static_unicode
#

include_directories (
${CMAKE_CURRENT_SOURCE_DIR}/apps
)
add_executable (test_exchndl_static
test_exchndl_static.cpp
add_executable (test_exchndl_static_unicode
test_exchndl_static_unicode.cpp
)
add_dependencies (test_exchndl_static exchndl_implib)
target_link_libraries (test_exchndl_static ${EXCHNDL_IMPLIB} shlwapi)
add_dependencies (check test_exchndl_static)
add_dependencies (test_exchndl_static_unicode exchndl_implib)
target_link_libraries (test_exchndl_static_unicode ${EXCHNDL_IMPLIB} shlwapi)
add_dependencies (check test_exchndl_static_unicode)
add_test (
NAME test_exchndl_static
COMMAND test_exchndl_static
NAME test_exchndl_static_unicode
COMMAND test_exchndl_static_unicode
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)


#
# test_exchndl
# test_exchndl_static_ansi
#

include_directories (
${CMAKE_CURRENT_SOURCE_DIR}/apps
)
add_executable (test_exchndl_dynamic
test_exchndl_dynamic.cpp
add_executable (test_exchndl_static_ansi
test_exchndl_static_ansi.cpp
)
target_link_libraries (test_exchndl_dynamic shlwapi)
add_dependencies (test_exchndl_dynamic exchndl)
add_dependencies (check test_exchndl_dynamic)
add_dependencies (test_exchndl_static_ansi exchndl_implib)
target_link_libraries (test_exchndl_static_ansi ${EXCHNDL_IMPLIB} shlwapi)
add_dependencies (check test_exchndl_static_ansi)
add_test (
NAME test_exchndl_dynamic
COMMAND test_exchndl_dynamic
NAME test_exchndl_static_ansi
COMMAND test_exchndl_static_ansi
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)


#
# test_exchndl_dynamic_unicode
#

include_directories (
${CMAKE_CURRENT_SOURCE_DIR}/apps
)
add_executable (test_exchndl_dynamic_unicode
test_exchndl_dynamic_unicode.cpp
)
target_link_libraries (test_exchndl_dynamic_unicode shlwapi)
add_dependencies (test_exchndl_dynamic_unicode exchndl)
add_dependencies (check test_exchndl_dynamic_unicode)
add_test (
NAME test_exchndl_dynamic_unicode
COMMAND test_exchndl_dynamic_unicode
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)


#
# test_exchndl_dynamic_ansi
#

include_directories (
${CMAKE_CURRENT_SOURCE_DIR}/apps
)
add_executable (test_exchndl_dynamic_ansi
test_exchndl_dynamic_ansi.cpp
)
target_link_libraries (test_exchndl_dynamic_ansi shlwapi)
add_dependencies (test_exchndl_dynamic_ansi exchndl)
add_dependencies (check test_exchndl_dynamic_ansi)
add_test (
NAME test_exchndl_dynamic_ansi
COMMAND test_exchndl_dynamic_ansi
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

Expand Down
7 changes: 7 additions & 0 deletions tests/apps/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@
#else
#error Unsupported compiler
#endif


// Define WFILE as a wide-character variant of __FILE__
// See https://stackoverflow.com/a/14421702/1708371
#define WIDE2(x) L##x
#define WIDE1(x) WIDE2(x)
#define WFILE WIDE1(__FILE__)
43 changes: 41 additions & 2 deletions tests/test_exchndl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,30 @@ main(int argc, char **argv)
{
bool ok;

#if !TEST_UNICODE
const char *szReport = PROG_NAME ".RPT";

DeleteFileA(szReport);
#else
// Add additional unicode wide characters to the name to test this behavior
const WCHAR *szReport = PROG_NAME L"_ぬるぽ.RPT";

DeleteFileW(szReport);
#endif // !TEST_UNICODE

g_prevExceptionFilter = SetUnhandledExceptionFilter(topLevelExceptionHandler);

#if !DYNAMIC

ExcHndlInit();

#if !TEST_UNICODE
ok = ExcHndlSetLogFileNameA(szReport);
test_line(ok, "ExcHndlSetLogFileNameA(\"%s\")", szReport);
#else
ok = ExcHndlSetLogFileNameW(szReport);
test_line(ok, "ExcHndlSetLogFileNameW(\"%S\")", szReport);
#endif // !TEST_UNICODE

#else

Expand All @@ -102,9 +114,10 @@ main(int argc, char **argv)
test_exit();
}

#if !TEST_UNICODE
test_line(!GetProcAddress(hModule, "ExcHndlSetLogFileNameA@4"), "!GetProcAddress(\"ExcHndlSetLogFileNameA@4\")");

typedef BOOL (APIENTRY * PFN_SETLOGFILENAMEA)(const char *szLogFileName);
typedef BOOL (APIENTRY * PFN_SETLOGFILENAMEA)(const char *pszLogFileName);
PFN_SETLOGFILENAMEA pfnSetLogFileNameA = (PFN_SETLOGFILENAMEA)GetProcAddress(hModule, "ExcHndlSetLogFileNameA");
ok = pfnSetLogFileNameA != NULL;
test_line(ok, "GetProcAddress(\"ExcHndlSetLogFileNameA\")");
Expand All @@ -115,14 +128,34 @@ main(int argc, char **argv)

ok = pfnSetLogFileNameA(szReport);
test_line(ok, "ExcHndlSetLogFileNameA(\"%s\")", szReport);
#else
test_line(!GetProcAddress(hModule, "ExcHndlSetLogFileNameW@4"), "!GetProcAddress(\"ExcHndlSetLogFileNameW@4\")");

#endif
typedef BOOL (APIENTRY * PFN_SETLOGFILENAMEW)(const WCHAR *pszLogFileName);
PFN_SETLOGFILENAMEW pfnSetLogFileNameW = (PFN_SETLOGFILENAMEW)GetProcAddress(hModule, "ExcHndlSetLogFileNameW");
ok = pfnSetLogFileNameW != NULL;
test_line(ok, "GetProcAddress(\"ExcHndlSetLogFileNameW\")");
if (!ok) {
test_diagnostic_last_error();
test_exit();
}

ok = pfnSetLogFileNameW(szReport);
test_line(ok, "ExcHndlSetLogFileNameW(\"%S\")", szReport);
#endif // !TEST_UNICODE

#endif // !DYNAMIC

_snprintf(g_szExceptionFunctionPattern, sizeof g_szExceptionFunctionPattern, " %s!%s+0x", PROG_NAME ".exe", __FUNCTION__);

if (!setjmp(g_JmpBuf) ) {
#if !TEST_UNICODE
_snprintf(g_szExceptionLinePattern, sizeof g_szExceptionLinePattern, "%s @ %u]",
PathFindFileNameA(__FILE__), __LINE__); *((volatile int *)0) = 0; LINE_BARRIER
#else
_snprintf(g_szExceptionLinePattern, sizeof g_szExceptionLinePattern, "%S @ %u]",
PathFindFileNameW(WFILE), __LINE__); *((volatile int *)0) = 0; LINE_BARRIER
#endif // !TEST_UNICODE
test_line(false, "longjmp"); exit(1);
} else {
test_line(true, "longjmp");
Expand All @@ -131,9 +164,15 @@ main(int argc, char **argv)
normalizePath(g_szExceptionFunctionPattern);
normalizePath(g_szExceptionLinePattern);

#if !TEST_UNICODE
FILE *fp = fopen(szReport, "rt");
ok = fp != NULL;
test_line(ok, "fopen(\"%s\")", szReport);
#else
FILE *fp = _wfopen(szReport, L"rt");
ok = fp != NULL;
test_line(ok, "_wfopen(\"%S\")", szReport);
#endif // !TEST_UNICODE
if (ok) {
const unsigned nPatterns = _countof(g_szPatterns);
bool found[nPatterns];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/


#define PROG_NAME "test_exchndl_dynamic"
#define PROG_NAME "test_exchndl_dynamic_ansi"
#define DYNAMIC 1
#define TEST_UNICODE 0

#include "test_exchndl.h"
24 changes: 24 additions & 0 deletions tests/test_exchndl_dynamic_unicode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2015 Jose Fonseca
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/


#define PROG_NAME "test_exchndl_dynamic_unicode"
#define DYNAMIC 1
#define TEST_UNICODE 1

#include "test_exchndl.h"
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/


#define PROG_NAME "test_exchndl_static"
#define PROG_NAME "test_exchndl_static_ansi"
#define DYNAMIC 0
#define TEST_UNICODE 0

#include "test_exchndl.h"
24 changes: 24 additions & 0 deletions tests/test_exchndl_static_unicode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2015 Jose Fonseca
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/


#define PROG_NAME "test_exchndl_static_unicode"
#define DYNAMIC 0
#define TEST_UNICODE 1

#include "test_exchndl.h"

0 comments on commit f5a7978

Please sign in to comment.