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