Blame winpr/libwinpr/path/test/TestPathCchAddBackslashEx.c

Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/path.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
#include <winpr/winpr.h>
Packit 1fb8d4
Packit 1fb8d4
static const TCHAR testPathBackslash[] = _T("C:\\Program Files\\");
Packit 1fb8d4
static const TCHAR testPathNoBackslash[] = _T("C:\\Program Files");
Packit 1fb8d4
Packit 1fb8d4
int TestPathCchAddBackslashEx(int argc, char* argv[])
Packit 1fb8d4
{
Packit 1fb8d4
	HRESULT status;
Packit 1fb8d4
	LPTSTR pszEnd;
Packit 1fb8d4
	size_t cchRemaining;
Packit 1fb8d4
	TCHAR Path[PATHCCH_MAX_CCH];
Packit 1fb8d4
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * PathCchAddBackslashEx returns S_OK if the function was successful,
Packit 1fb8d4
	 * S_FALSE if the path string already ends in a backslash,
Packit 1fb8d4
	 * or an error code otherwise.
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit 1fb8d4
	_tcscpy(Path, testPathNoBackslash);
Packit 1fb8d4
Packit 1fb8d4
	/* Add a backslash to a path without a trailing backslash, expect S_OK */
Packit 1fb8d4
Packit 1fb8d4
	status = PathCchAddBackslashEx(Path, sizeof(Path) / sizeof(TCHAR), &pszEnd, &cchRemaining);
Packit 1fb8d4
Packit 1fb8d4
	if (status != S_OK)
Packit 1fb8d4
	{
Packit Service 5a9772
		_tprintf(_T("PathCchAddBackslash status: 0x%08") _T(PRIX32) _T("\n"), status);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (_tcscmp(Path, testPathBackslash) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Add a backslash to a path with a trailing backslash, expect S_FALSE */
Packit 1fb8d4
Packit 1fb8d4
	_tcscpy(Path, testPathBackslash);
Packit 1fb8d4
Packit 1fb8d4
	status = PathCchAddBackslashEx(Path, sizeof(Path) / sizeof(TCHAR), &pszEnd, &cchRemaining);
Packit 1fb8d4
Packit 1fb8d4
	if (status != S_FALSE)
Packit 1fb8d4
	{
Packit Service 5a9772
		_tprintf(_T("PathCchAddBackslash status: 0x%08") _T(PRIX32) _T("\n"), status);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (_tcscmp(Path, testPathBackslash) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Use NULL PSTR, expect FAILED(status) */
Packit 1fb8d4
Packit 1fb8d4
	status = PathCchAddBackslashEx(NULL, PATHCCH_MAX_CCH, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (SUCCEEDED(status))
Packit 1fb8d4
	{
Packit Service 5a9772
		_tprintf(
Packit Service 5a9772
		    _T("PathCchAddBackslashEx unexpectedly succeded with null buffer. Status: 0x%08") _T(
Packit Service 5a9772
		        PRIX32) _T("\n"),
Packit Service 5a9772
		    status);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Use insufficient size value, expect FAILED(status)  */
Packit 1fb8d4
Packit 1fb8d4
	_tcscpy(Path, _T("C:\\tmp"));
Packit 1fb8d4
Packit 1fb8d4
	status = PathCchAddBackslashEx(Path, 7, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (SUCCEEDED(status))
Packit 1fb8d4
	{
Packit Service 5a9772
		_tprintf(_T("PathCchAddBackslashEx unexpectedly succeded with insufficient buffer size. ")
Packit Service 5a9772
		         _T("Status: 0x%08") _T(PRIX32) _T("\n"),
Packit Service 5a9772
		         status);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Use minimum required size value, expect S_OK  */
Packit 1fb8d4
Packit 1fb8d4
	_tcscpy(Path, _T("C:\\tmp"));
Packit 1fb8d4
Packit 1fb8d4
	status = PathCchAddBackslashEx(Path, 8, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status != S_OK)
Packit 1fb8d4
	{
Packit Service 5a9772
		_tprintf(_T("PathCchAddBackslashEx failed with status: 0x%08") _T(PRIX32) _T("\n"), status);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}