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