1 #include <stdio.h>
2 #include <winpr/crt.h>
3 #include <winpr/path.h>
4 #include <winpr/tchar.h>
5 #include <winpr/winpr.h>
6 
7 static const char testPathExtension[] = "C:\\Windows\\System32\\cmd.exe";
8 
TestPathCchFindExtension(int argc,char * argv[])9 int TestPathCchFindExtension(int argc, char* argv[])
10 {
11 	PCSTR pszExt;
12 	PCSTR pszTmp;
13 	HRESULT hr;
14 
15 	/* Test invalid args */
16 
17 	hr = PathCchFindExtensionA(NULL, sizeof(testPathExtension), &pszExt);
18 	if (SUCCEEDED(hr))
19 	{
20 		printf(
21 		    "PathCchFindExtensionA unexpectedly succeeded with pszPath = NULL. result: 0x%08" PRIX32
22 		    "\n",
23 		    hr);
24 		return -1;
25 	}
26 
27 	hr = PathCchFindExtensionA(testPathExtension, 0, &pszExt);
28 	if (SUCCEEDED(hr))
29 	{
30 		printf("PathCchFindExtensionA unexpectedly succeeded with cchPath = 0. result: 0x%08" PRIX32
31 		       "\n",
32 		       hr);
33 		return -1;
34 	}
35 
36 	hr = PathCchFindExtensionA(testPathExtension, sizeof(testPathExtension), NULL);
37 	if (SUCCEEDED(hr))
38 	{
39 		printf(
40 		    "PathCchFindExtensionA unexpectedly succeeded with ppszExt = NULL. result: 0x%08" PRIX32
41 		    "\n",
42 		    hr);
43 		return -1;
44 	}
45 
46 	/* Test missing null-termination of pszPath */
47 
48 	hr = PathCchFindExtensionA("c:\\45.789", 9, &pszExt); /* nb: correct would be 10 */
49 	if (SUCCEEDED(hr))
50 	{
51 		printf("PathCchFindExtensionA unexpectedly succeeded with unterminated pszPath. result: "
52 		       "0x%08" PRIX32 "\n",
53 		       hr);
54 		return -1;
55 	}
56 
57 	/* Test passing of an empty terminated string (must succeed) */
58 
59 	pszExt = NULL;
60 	pszTmp = "";
61 	hr = PathCchFindExtensionA(pszTmp, 1, &pszExt);
62 	if (hr != S_OK)
63 	{
64 		printf("PathCchFindExtensionA failed with an empty terminated string. result: 0x%08" PRIX32
65 		       "\n",
66 		       hr);
67 		return -1;
68 	}
69 	/* pszExt must point to the strings terminating 0 now */
70 	if (pszExt != pszTmp)
71 	{
72 		printf("PathCchFindExtensionA failed with an empty terminated string:  pszExt pointer "
73 		       "mismatch\n");
74 		return -1;
75 	}
76 
77 	/* Test a path without file extension (must succeed) */
78 
79 	pszExt = NULL;
80 	pszTmp = "c:\\4.678\\";
81 	hr = PathCchFindExtensionA(pszTmp, 10, &pszExt);
82 	if (hr != S_OK)
83 	{
84 		printf("PathCchFindExtensionA failed with a directory path. result: 0x%08" PRIX32 "\n", hr);
85 		return -1;
86 	}
87 	/* The extension must not have been found and pszExt must point to the
88 	 * strings terminating NULL now */
89 	if (pszExt != &pszTmp[9])
90 	{
91 		printf("PathCchFindExtensionA failed with a directory path: pszExt pointer mismatch\n");
92 		return -1;
93 	}
94 
95 	/* Non-special tests */
96 
97 	pszExt = NULL;
98 	if (PathCchFindExtensionA(testPathExtension, sizeof(testPathExtension), &pszExt) != S_OK)
99 	{
100 		printf("PathCchFindExtensionA failure: expected S_OK\n");
101 		return -1;
102 	}
103 
104 	if (!pszExt || strcmp(pszExt, ".exe"))
105 	{
106 		printf("PathCchFindExtensionA failure: unexpected extension\n");
107 		return -1;
108 	}
109 
110 	printf("Extension: %s\n", pszExt);
111 
112 	return 0;
113 }
114