FreeRDP
TestPathCchAddBackslashEx.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 
8 static const TCHAR testPathBackslash[] = _T("C:\\Program Files\\");
9 static const TCHAR testPathNoBackslash[] = _T("C:\\Program Files");
10 
11 int TestPathCchAddBackslashEx(int argc, char* argv[])
12 {
13  HRESULT status = 0;
14  LPTSTR pszEnd = NULL;
15  size_t cchRemaining = 0;
16  TCHAR Path[PATHCCH_MAX_CCH] = { 0 };
17 
18  WINPR_UNUSED(argc);
19  WINPR_UNUSED(argv);
20 
27  _tcsncpy(Path, testPathNoBackslash, ARRAYSIZE(Path));
28 
29  /* Add a backslash to a path without a trailing backslash, expect S_OK */
30 
31  status = PathCchAddBackslashEx(Path, sizeof(Path) / sizeof(TCHAR), &pszEnd, &cchRemaining);
32 
33  if (status != S_OK)
34  {
35  _tprintf(_T("PathCchAddBackslash status: 0x%08") _T(PRIX32) _T("\n"), status);
36  return -1;
37  }
38 
39  if (_tcsncmp(Path, testPathBackslash, ARRAYSIZE(Path)) != 0)
40  {
41  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
42  return -1;
43  }
44 
45  /* Add a backslash to a path with a trailing backslash, expect S_FALSE */
46 
47  _tcsncpy(Path, testPathBackslash, ARRAYSIZE(Path));
48 
49  status = PathCchAddBackslashEx(Path, sizeof(Path) / sizeof(TCHAR), &pszEnd, &cchRemaining);
50 
51  if (status != S_FALSE)
52  {
53  _tprintf(_T("PathCchAddBackslash status: 0x%08") _T(PRIX32) _T("\n"), status);
54  return -1;
55  }
56 
57  if (_tcsncmp(Path, testPathBackslash, ARRAYSIZE(Path)) != 0)
58  {
59  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathBackslash);
60  return -1;
61  }
62 
63  /* Use NULL PSTR, expect FAILED(status) */
64 
65  status = PathCchAddBackslashEx(NULL, PATHCCH_MAX_CCH, NULL, NULL);
66 
67  if (SUCCEEDED(status))
68  {
69  _tprintf(
70  _T("PathCchAddBackslashEx unexpectedly succeded with null buffer. Status: 0x%08") _T(
71  PRIX32) _T("\n"),
72  status);
73  return -1;
74  }
75 
76  /* Use insufficient size value, expect FAILED(status) */
77 
78  _tcsncpy(Path, _T("C:\\tmp"), ARRAYSIZE(Path));
79 
80  status = PathCchAddBackslashEx(Path, 7, NULL, NULL);
81 
82  if (SUCCEEDED(status))
83  {
84  _tprintf(_T("PathCchAddBackslashEx unexpectedly succeded with insufficient buffer size. ")
85  _T("Status: 0x%08") _T(PRIX32) _T("\n"),
86  status);
87  return -1;
88  }
89 
90  /* Use minimum required size value, expect S_OK */
91 
92  _tcsncpy(Path, _T("C:\\tmp"), ARRAYSIZE(Path));
93 
94  status = PathCchAddBackslashEx(Path, 8, NULL, NULL);
95 
96  if (status != S_OK)
97  {
98  _tprintf(_T("PathCchAddBackslashEx failed with status: 0x%08") _T(PRIX32) _T("\n"), status);
99  return -1;
100  }
101 
102  return 0;
103 }