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