2012-10-02 00:49:34 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/path.h>
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/winpr.h>
|
|
|
|
|
2012-11-18 07:03:04 +04:00
|
|
|
static const char testPathExtension[] = "C:\\Windows\\System32\\cmd.exe";
|
|
|
|
|
2012-10-02 00:49:34 +04:00
|
|
|
int TestPathCchFindExtension(int argc, char* argv[])
|
|
|
|
{
|
2012-11-18 07:03:04 +04:00
|
|
|
PCSTR pszExt;
|
2015-06-08 11:24:38 +03:00
|
|
|
PCSTR pszTmp;
|
|
|
|
HRESULT hr;
|
2012-11-18 07:03:04 +04:00
|
|
|
|
2015-06-08 11:24:38 +03:00
|
|
|
/* Test invalid args */
|
|
|
|
|
|
|
|
hr = PathCchFindExtensionA(NULL, sizeof(testPathExtension), &pszExt);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("PathCchFindExtensionA unexpectedly succeeded with pszPath = NULL. result: 0x%08"PRIX32"\n", hr);
|
2015-06-08 11:24:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = PathCchFindExtensionA(testPathExtension, 0, &pszExt);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("PathCchFindExtensionA unexpectedly succeeded with cchPath = 0. result: 0x%08"PRIX32"\n", hr);
|
2015-06-08 11:24:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = PathCchFindExtensionA(testPathExtension, sizeof(testPathExtension), NULL);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("PathCchFindExtensionA unexpectedly succeeded with ppszExt = NULL. result: 0x%08"PRIX32"\n", hr);
|
2015-06-08 11:24:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Test missing null-termination of pszPath */
|
|
|
|
|
2016-05-26 19:36:02 +03:00
|
|
|
hr = PathCchFindExtensionA("c:\\45.789", 9, &pszExt); /* nb: correct would be 10 */
|
2015-06-08 11:24:38 +03:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("PathCchFindExtensionA unexpectedly succeeded with unterminated pszPath. result: 0x%08"PRIX32"\n", hr);
|
2015-06-08 11:24:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Test passing of an empty terminated string (must succeed) */
|
|
|
|
|
|
|
|
pszExt = NULL;
|
2016-05-26 19:36:02 +03:00
|
|
|
pszTmp = "";
|
2015-06-08 11:24:38 +03:00
|
|
|
hr = PathCchFindExtensionA(pszTmp, 1, &pszExt);
|
|
|
|
if (hr != S_OK)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("PathCchFindExtensionA failed with an empty terminated string. result: 0x%08"PRIX32"\n", hr);
|
2015-06-08 11:24:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* pszExt must point to the strings terminating 0 now */
|
|
|
|
if (pszExt != pszTmp)
|
|
|
|
{
|
|
|
|
printf("PathCchFindExtensionA failed with an empty terminated string: pszExt pointer mismatch\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Test a path without file extension (must succeed) */
|
|
|
|
|
|
|
|
pszExt = NULL;
|
2016-05-26 19:36:02 +03:00
|
|
|
pszTmp = "c:\\4.678\\";
|
2015-06-08 11:24:38 +03:00
|
|
|
hr = PathCchFindExtensionA(pszTmp, 10, &pszExt);
|
|
|
|
if (hr != S_OK)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("PathCchFindExtensionA failed with a directory path. result: 0x%08"PRIX32"\n", hr);
|
2015-06-08 11:24:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* The extension must not have been found and pszExt must point to the
|
|
|
|
* strings terminating NULL now */
|
|
|
|
if (pszExt != &pszTmp[9])
|
|
|
|
{
|
|
|
|
printf("PathCchFindExtensionA failed with a directory path: pszExt pointer mismatch\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Non-special tests */
|
|
|
|
|
|
|
|
pszExt = NULL;
|
2012-11-18 07:03:04 +04:00
|
|
|
if (PathCchFindExtensionA(testPathExtension, sizeof(testPathExtension), &pszExt) != S_OK)
|
|
|
|
{
|
|
|
|
printf("PathCchFindExtensionA failure: expected S_OK\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-26 19:36:02 +03:00
|
|
|
if (!pszExt || strcmp(pszExt, ".exe"))
|
2015-06-08 11:24:38 +03:00
|
|
|
{
|
|
|
|
printf("PathCchFindExtensionA failure: unexpected extension\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-18 07:03:04 +04:00
|
|
|
printf("Extension: %s\n", pszExt);
|
|
|
|
|
2012-10-02 00:49:34 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|