libwinpr-path: start writing tests

This commit is contained in:
Marc-André Moreau 2012-10-01 16:49:34 -04:00
parent 659a901450
commit 9155bf3eab
27 changed files with 761 additions and 0 deletions

View File

@ -25,6 +25,9 @@
#ifndef _WIN32
#define S_OK ((HRESULT) 0L)
#define S_FALSE ((HRESULT) 1L)
/* System Error Codes (0-499) */
#define ERROR_SUCCESS 0x00000000

View File

@ -21,8 +21,29 @@
#define WINPR_PATH_H
#include <winpr/winpr.h>
#include <winpr/tchar.h>
#include <winpr/wtypes.h>
//#define HAVE_PATHCCH_H 1
#ifdef HAVE_PATHCCH_H
#include <Pathcch.h>
#else
#define PATHCCH_ALLOW_LONG_PATHS 0x00000001 /* Allow building of \\?\ paths if longer than MAX_PATH */
#define VOLUME_PREFIX _T("\\\\?\\Volume")
#define VOLUME_PREFIX_LEN ((sizeof(VOLUME_PREFIX) / sizeof(TCHAR)) - 1)
/*
* Maximum number of characters we support using the "\\?\" syntax
* (0x7FFF + 1 for NULL terminator)
*/
#define PATHCCH_MAX_CCH 0x8000
WINPR_API HRESULT PathCchAddBackslashA(PSTR pszPath, size_t cchPath);
WINPR_API HRESULT PathCchAddBackslashW(PWSTR pszPath, size_t cchPath);
@ -137,4 +158,6 @@ WINPR_API HRESULT PathCchRemoveFileSpecW(PWSTR pszPath, size_t cchPath);
#define PathCchRemoveFileSpec PathCchRemoveFileSpecA
#endif
#endif
#endif /* WINPR_PATH_H */

View File

@ -36,3 +36,7 @@ else()
endif()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR")
if(BUILD_TESTING)
add_subdirectory(test)
endif()

3
winpr/libwinpr/path/test/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
TestPath
TestPath.c

View File

@ -0,0 +1,48 @@
set(MODULE_NAME "TestPath")
set(MODULE_PREFIX "TEST_PATH")
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS
TestPathCchAddBackslash.c
TestPathCchRemoveBackslash.c
TestPathCchAddBackslashEx.c
TestPathCchRemoveBackslashEx.c
TestPathCchAddExtension.c
TestPathCchAppend.c
TestPathCchAppendEx.c
TestPathCchCanonicalize.c
TestPathCchCanonicalizeEx.c
TestPathAllocCanonicalize.c
TestPathCchCombine.c
TestPathCchCombineEx.c
TestPathAllocCombine.c
TestPathCchFindExtension.c
TestPathCchRenameExtension.c
TestPathCchRemoveExtension.c
TestPathCchIsRoot.c
TestPathIsUNCEx.c
TestPathCchSkipRoot.c
TestPathCchStripToRoot.c
TestPathCchStripPrefix.c
TestPathCchRemoveFileSpec.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS
${${MODULE_PREFIX}_DRIVER}
${${MODULE_PREFIX}_TESTS})
add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
if(WIN32)
target_link_libraries(${MODULE_NAME} Pathcch)
else()
target_link_libraries(${MODULE_NAME} winpr-path)
endif()
foreach(test ${${MODULE_PREFIX}_TESTS})
get_filename_component(TestName ${test} NAME_WE)
add_test(${TestName} ${EXECUTABLE_OUTPUT_PATH}/${MODULE_NAME} ${TestName})
endforeach()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathAllocCanonicalize(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,94 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
static const TCHAR testBasePathBackslash[] = _T("C:\\Program Files\\");
static const TCHAR testBasePathNoBackslash[] = _T("C:\\Program Files");
static const TCHAR testMorePathBackslash[] = _T("\\Microsoft Visual Studio 11.0");
static const TCHAR testMorePathNoBackslash[] = _T("Microsoft Visual Studio 11.0");
static const TCHAR testPathOut[] = _T("C:\\Program Files\\Microsoft Visual Studio 11.0");
static const TCHAR testPathOutMorePathBackslash[] = _T("C:\\Microsoft Visual Studio 11.0");
int TestPathAllocCombine(int argc, char* argv[])
{
HRESULT status;
LPTSTR PathOut;
/* Base Path: Backslash, More Path: No Backslash */
status = PathAllocCombine(testBasePathBackslash, testMorePathNoBackslash, 0, &PathOut);
if (status != S_OK)
{
_tprintf(_T("PathAllocCombine status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(PathOut, testPathOut) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), PathOut, testPathOut);
return -1;
}
HeapFree(GetProcessHeap(), 0, PathOut);
/* Base Path: Backslash, More Path: Backslash */
status = PathAllocCombine(testBasePathBackslash, testMorePathBackslash, 0, &PathOut);
if (status != S_OK)
{
_tprintf(_T("PathAllocCombine status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(PathOut, testPathOutMorePathBackslash) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), PathOut, testPathOutMorePathBackslash);
return -1;
}
HeapFree(GetProcessHeap(), 0, PathOut);
/* Base Path: No Backslash, More Path: Backslash */
status = PathAllocCombine(testBasePathNoBackslash, testMorePathBackslash, 0, &PathOut);
if (status != S_OK)
{
_tprintf(_T("PathAllocCombine status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(PathOut, testPathOutMorePathBackslash) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), PathOut, testPathOutMorePathBackslash);
return -1;
}
HeapFree(GetProcessHeap(), 0, PathOut);
/* Base Path: No Backslash, More Path: No Backslash */
status = PathAllocCombine(testBasePathNoBackslash, testMorePathNoBackslash, 0, &PathOut);
if (status != S_OK)
{
_tprintf(_T("PathAllocCombine status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(PathOut, testPathOut) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), PathOut, testPathOut);
return -1;
}
HeapFree(GetProcessHeap(), 0, PathOut);
return 0;
}

View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
static const TCHAR testPathBackslash[] = _T("C:\\Program Files\\");
static const TCHAR testPathNoBackslash[] = _T("C:\\Program Files");
int TestPathCchAddBackslash(int argc, char* argv[])
{
HRESULT status;
TCHAR Path[PATHCCH_MAX_CCH];
_tcscpy(Path, testPathNoBackslash);
/* Add a backslash to a path without a trailing backslash, expect S_OK */
status = PathCchAddBackslash(Path, PATHCCH_MAX_CCH);
if (status != S_OK)
{
_tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathBackslash) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
return -1;
}
/* Add a backslash to a path with a trailing backslash, expect S_FALSE */
_tcscpy(Path, testPathBackslash);
status = PathCchAddBackslash(Path, PATHCCH_MAX_CCH);
if (status != S_FALSE)
{
_tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathBackslash) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
return -1;
}
return 0;
}

View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
static const TCHAR testPathBackslash[] = _T("C:\\Program Files\\");
static const TCHAR testPathNoBackslash[] = _T("C:\\Program Files");
int TestPathCchAddBackslashEx(int argc, char* argv[])
{
HRESULT status;
LPTSTR pszEnd;
size_t cchRemaining;
TCHAR Path[PATHCCH_MAX_CCH];
_tcscpy(Path, testPathNoBackslash);
/* Add a backslash to a path without a trailing backslash, expect S_OK */
status = PathCchAddBackslashEx(Path, sizeof(Path) / sizeof(TCHAR), &pszEnd, &cchRemaining);
if (status != S_OK)
{
_tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathBackslash) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
return -1;
}
/* Add a backslash to a path with a trailing backslash, expect S_FALSE */
_tcscpy(Path, testPathBackslash);
status = PathCchAddBackslashEx(Path, sizeof(Path) / sizeof(TCHAR), &pszEnd, &cchRemaining);
if (status != S_FALSE)
{
_tprintf(_T("PathCchAddBackslash status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathBackslash) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
return -1;
}
return 0;
}

View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
static const TCHAR testExtDot[] = _T(".exe");
static const TCHAR testExtNoDot[] = _T("exe");
static const TCHAR testPathNoExtension[] = _T("C:\\Windows\\System32\\cmd");
static const TCHAR testPathExtension[] = _T("C:\\Windows\\System32\\cmd.exe");
int TestPathCchAddExtension(int argc, char* argv[])
{
HRESULT status;
TCHAR Path[PATHCCH_MAX_CCH];
/* Path: no extension, Extension: dot */
_tcscpy(Path, testPathNoExtension);
status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);
if (status != S_OK)
{
_tprintf(_T("PathCchAddExtension status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathExtension) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
return -1;
}
/* Path: no extension, Extension: no dot */
_tcscpy(Path, testPathNoExtension);
status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtNoDot);
if (status != S_OK)
{
_tprintf(_T("PathCchAddExtension status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathExtension) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
return -1;
}
/* Path: extension, Extension: dot */
_tcscpy(Path, testPathExtension);
status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);
if (status != S_FALSE)
{
_tprintf(_T("PathCchAddExtension status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathExtension) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
return -1;
}
/* Path: extension, Extension: no dot */
_tcscpy(Path, testPathExtension);
status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);
if (status != S_FALSE)
{
_tprintf(_T("PathCchAddExtension status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathExtension) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
return -1;
}
return 0;
}

View File

@ -0,0 +1,93 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
static const TCHAR testBasePathBackslash[] = _T("C:\\Program Files\\");
static const TCHAR testBasePathNoBackslash[] = _T("C:\\Program Files");
static const TCHAR testMorePathBackslash[] = _T("\\Microsoft Visual Studio 11.0");
static const TCHAR testMorePathNoBackslash[] = _T("Microsoft Visual Studio 11.0");
static const TCHAR testPathOut[] = _T("C:\\Program Files\\Microsoft Visual Studio 11.0");
int TestPathCchAppend(int argc, char* argv[])
{
HRESULT status;
TCHAR Path[PATHCCH_MAX_CCH];
/* Base Path: Backslash, More Path: No Backslash */
_tcscpy(Path, testBasePathBackslash);
status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathNoBackslash);
if (status != S_OK)
{
_tprintf(_T("PathCchAppend status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathOut) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
return -1;
}
/* Base Path: Backslash, More Path: Backslash */
_tcscpy(Path, testBasePathBackslash);
status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathBackslash);
if (status != S_OK)
{
_tprintf(_T("PathCchAppend status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathOut) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
return -1;
}
/* Base Path: No Backslash, More Path: Backslash */
_tcscpy(Path, testBasePathNoBackslash);
status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathBackslash);
if (status != S_OK)
{
_tprintf(_T("PathCchAppend status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathOut) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
return -1;
}
/* Base Path: No Backslash, More Path: No Backslash */
_tcscpy(Path, testBasePathNoBackslash);
status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathNoBackslash);
if (status != S_OK)
{
_tprintf(_T("PathCchAppend status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathOut) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
return -1;
}
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchAppendEx(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchCanonicalize(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchCanonicalizeEx(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchCombine(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchCombineEx(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchFindExtension(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchIsRoot(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchRemoveBackslash(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchRemoveBackslashEx(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchRemoveExtension(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchRemoveFileSpec(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchRenameExtension(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchSkipRoot(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
/**
* Naming Files, Paths, and Namespaces:
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247/
*/
static const TCHAR testPathPrefixFileNamespace[] = _T("\\\\?\\C:\\Program Files\\");
static const TCHAR testPathNoPrefixFileNamespace[] = _T("C:\\Program Files\\");
static const TCHAR testPathPrefixDeviceNamespace[] = _T("\\\\?\\GLOBALROOT");
int TestPathCchStripPrefix(int argc, char* argv[])
{
HRESULT status;
TCHAR Path[PATHCCH_MAX_CCH];
/* Path with prefix (File Namespace) */
_tcscpy(Path, testPathPrefixFileNamespace);
status = PathCchStripPrefix(Path, sizeof(testPathPrefixFileNamespace) / sizeof(TCHAR));
if (status != S_OK)
{
_tprintf(_T("PathCchStripPrefix status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathNoPrefixFileNamespace) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathNoPrefixFileNamespace);
return -1;
}
/* Path with prefix (Device Namespace) */
_tcscpy(Path, testPathPrefixDeviceNamespace);
status = PathCchStripPrefix(Path, sizeof(testPathPrefixDeviceNamespace) / sizeof(TCHAR));
if (status != S_FALSE)
{
_tprintf(_T("PathCchStripPrefix status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Path, testPathPrefixDeviceNamespace) != 0)
{
_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathPrefixDeviceNamespace);
return -1;
}
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathCchStripToRoot(int argc, char* argv[])
{
return 0;
}

View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
static const TCHAR testServer[] = _T("server\\share\\path\\file");
static const TCHAR testPathUNC[] = _T("\\\\server\\share\\path\\file");
static const TCHAR testPathNotUNC[] = _T("C:\\share\\path\\file");
int TestPathIsUNCEx(int argc, char* argv[])
{
BOOL status;
LPTSTR Server;
TCHAR Path[PATHCCH_MAX_CCH];
/* Path is UNC */
_tcscpy(Path, testPathUNC);
status = PathIsUNCEx(Path, &Server);
if (!status)
{
_tprintf(_T("PathIsUNCEx status: 0x%08X\n"), status);
return -1;
}
if (_tcscmp(Server, testServer) != 0)
{
_tprintf(_T("Server Name Mismatch: Actual: %s, Expected: %s\n"), Server, testServer);
return -1;
}
/* Path is not UNC */
_tcscpy(Path, testPathNotUNC);
status = PathIsUNCEx(Path, &Server);
if (status)
{
_tprintf(_T("PathIsUNCEx status: 0x%08X\n"), status);
return -1;
}
return 0;
}