FreeRDP
TestPathCchStripPrefix.c
1 
2 #include <stdio.h>
3 #include <winpr/crt.h>
4 #include <winpr/path.h>
5 #include <winpr/tchar.h>
6 #include <winpr/winpr.h>
7 
13 static const TCHAR testPathPrefixFileNamespace[] = _T("\\\\?\\C:\\Program Files\\");
14 static const TCHAR testPathNoPrefixFileNamespace[] = _T("C:\\Program Files\\");
15 static const TCHAR testPathPrefixFileNamespaceMinimum[] = _T("\\\\?\\C:");
16 static const TCHAR testPathNoPrefixFileNamespaceMinimum[] = _T("C:");
17 
18 static const TCHAR testPathPrefixDeviceNamespace[] = _T("\\\\?\\GLOBALROOT");
19 
20 int TestPathCchStripPrefix(int argc, char* argv[])
21 {
22  HRESULT status = 0;
23  TCHAR Path[PATHCCH_MAX_CCH] = { 0 };
24 
25  WINPR_UNUSED(argc);
26  WINPR_UNUSED(argv);
27 
33  /* Path with prefix (File Namespace) */
34 
35  _tcsncpy(Path, testPathPrefixFileNamespace, ARRAYSIZE(Path));
36 
37  status = PathCchStripPrefix(Path, sizeof(testPathPrefixFileNamespace) / sizeof(TCHAR));
38 
39  if (status != S_OK)
40  {
41  _tprintf(_T("PathCchStripPrefix status: 0x%08") _T(PRIX32) _T("\n"), status);
42  return -1;
43  }
44 
45  if (_tcsncmp(Path, testPathNoPrefixFileNamespace, ARRAYSIZE(Path)) != 0)
46  {
47  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path,
48  testPathNoPrefixFileNamespace);
49  return -1;
50  }
51 
52  /* Path with prefix (Device Namespace) */
53 
54  _tcsncpy(Path, testPathPrefixDeviceNamespace, ARRAYSIZE(Path));
55 
56  status = PathCchStripPrefix(Path, ARRAYSIZE(testPathPrefixDeviceNamespace));
57 
58  if (status != S_FALSE)
59  {
60  _tprintf(_T("PathCchStripPrefix status: 0x%08") _T(PRIX32) _T("\n"), status);
61  return -1;
62  }
63 
64  if (_tcsncmp(Path, testPathPrefixDeviceNamespace, ARRAYSIZE(Path)) != 0)
65  {
66  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path,
67  testPathPrefixDeviceNamespace);
68  return -1;
69  }
70 
71  /* NULL Path */
72  status = PathCchStripPrefix(NULL, PATHCCH_MAX_CCH);
73  if (status != E_INVALIDARG)
74  {
75  _tprintf(
76  _T("PathCchStripPrefix with null path unexpectedly succeeded with status 0x%08") _T(
77  PRIX32) _T("\n"),
78  status);
79  return -1;
80  }
81 
82  /* Invalid cchPath values: 0, 1, 2, 3 and > PATHCCH_MAX_CCH */
83  for (int i = 0; i < 5; i++)
84  {
85  _tcsncpy(Path, testPathPrefixFileNamespace, ARRAYSIZE(Path));
86  if (i == 4)
87  i = PATHCCH_MAX_CCH + 1;
88  status = PathCchStripPrefix(Path, i);
89  if (status != E_INVALIDARG)
90  {
91  _tprintf(_T("PathCchStripPrefix with invalid cchPath value %d unexpectedly succeeded ")
92  _T("with status 0x%08") _T(PRIX32) _T("\n"),
93  i, status);
94  return -1;
95  }
96  }
97 
98  /* Minimum Path that would get successfully stripped on windows */
99  _tcsncpy(Path, testPathPrefixFileNamespaceMinimum, ARRAYSIZE(Path));
100  size_t i = ARRAYSIZE(testPathPrefixFileNamespaceMinimum);
101  i = i - 1; /* include testing of a non-null terminated string */
102  status = PathCchStripPrefix(Path, i);
103  if (status != S_OK)
104  {
105  _tprintf(_T("PathCchStripPrefix with minimum valid strippable path length unexpectedly ")
106  _T("returned status 0x%08") _T(PRIX32) _T("\n"),
107  status);
108  return -1;
109  }
110  if (_tcsncmp(Path, testPathNoPrefixFileNamespaceMinimum, ARRAYSIZE(Path)) != 0)
111  {
112  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path,
113  testPathNoPrefixFileNamespaceMinimum);
114  return -1;
115  }
116 
117  /* Invalid drive letter symbol */
118  _tcsncpy(Path, _T("\\\\?\\5:"), ARRAYSIZE(Path));
119  status = PathCchStripPrefix(Path, 6);
120  if (status == S_OK)
121  {
122  _tprintf(
123  _T("PathCchStripPrefix with invalid drive letter symbol unexpectedly succeeded\n"));
124  return -1;
125  }
126 
127  return 0;
128 }