FreeRDP
Loading...
Searching...
No Matches
TestPathCchAppend.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
8static const TCHAR testBasePathBackslash[] = _T("C:\\Program Files\\");
9static const TCHAR testBasePathNoBackslash[] = _T("C:\\Program Files");
10static const TCHAR testMorePathBackslash[] = _T("\\Microsoft Visual Studio 11.0");
11static const TCHAR testMorePathNoBackslash[] = _T("Microsoft Visual Studio 11.0");
12static const TCHAR testPathOut[] = _T("C:\\Program Files\\Microsoft Visual Studio 11.0");
13
14int TestPathCchAppend(int argc, char* argv[])
15{
16 HRESULT status = 0;
17 TCHAR Path[PATHCCH_MAX_CCH];
18
19 WINPR_UNUSED(argc);
20 WINPR_UNUSED(argv);
21
22 /* Base Path: Backslash, More Path: No Backslash */
23
24 _tcsncpy(Path, testBasePathBackslash, ARRAYSIZE(Path));
25
26 status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathNoBackslash);
27
28 if (status != S_OK)
29 {
30 _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
31 return -1;
32 }
33
34 if (_tcsncmp(Path, testPathOut, ARRAYSIZE(Path)) != 0)
35 {
36 _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
37 return -1;
38 }
39
40 /* Base Path: Backslash, More Path: Backslash */
41
42 _tcsncpy(Path, testBasePathBackslash, ARRAYSIZE(Path));
43
44 status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathBackslash);
45
46 if (status != S_OK)
47 {
48 _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
49 return -1;
50 }
51
52 if (_tcsncmp(Path, testPathOut, ARRAYSIZE(Path)) != 0)
53 {
54 _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
55 return -1;
56 }
57
58 /* Base Path: No Backslash, More Path: Backslash */
59
60 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
61
62 status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathBackslash);
63
64 if (status != S_OK)
65 {
66 _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
67 return -1;
68 }
69
70 if (_tcsncmp(Path, testPathOut, ARRAYSIZE(Path)) != 0)
71 {
72 _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
73 return -1;
74 }
75
76 /* Base Path: No Backslash, More Path: No Backslash */
77
78 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
79
80 status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathNoBackslash);
81
82 if (status != S_OK)
83 {
84 _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
85 return -1;
86 }
87
88 if (_tcsncmp(Path, testPathOut, ARRAYSIZE(Path)) != 0)
89 {
90 _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
91 return -1;
92 }
93
94 /* According to msdn a nullptr Path is an invalid argument */
95 status = PathCchAppend(nullptr, PATHCCH_MAX_CCH, testMorePathNoBackslash);
96 if (status != E_INVALIDARG)
97 {
98 _tprintf(_T("PathCchAppend with nullptr path unexpectedly returned status: 0x%08") _T(
99 PRIX32) _T("\n"),
100 status);
101 return -1;
102 }
103
104 /* According to msdn a nullptr pszMore is an invalid argument (although optional !?)
105 * Testing the real thing on windows reveals that it returns S_OK...
106 */
107 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
108 status = PathCchAppend(Path, PATHCCH_MAX_CCH, nullptr);
109 if (status != S_OK)
110 {
111 _tprintf(_T("PathCchAppend with nullptr pszMore unexpectedly returned status: 0x%08") _T(
112 PRIX32) _T("\n"),
113 status);
114 return -1;
115 }
116
117 /* According to msdn cchPath must be > 0 and <= PATHCCH_MAX_CCH */
118 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
119 status = PathCchAppend(Path, 0, testMorePathNoBackslash);
120 if (status != E_INVALIDARG)
121 {
122 _tprintf(_T("PathCchAppend with cchPath value 0 unexpectedly returned status: 0x%08") _T(
123 PRIX32) _T("\n"),
124 status);
125 return -1;
126 }
127 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
128 status = PathCchAppend(Path, PATHCCH_MAX_CCH + 1, testMorePathNoBackslash);
129 if (status != E_INVALIDARG)
130 {
131 _tprintf(_T("PathCchAppend with cchPath value > PATHCCH_MAX_CCH unexpectedly returned ")
132 _T("status: 0x%08") _T(PRIX32) _T("\n"),
133 status);
134 return -1;
135 }
136
137 /* Resulting file must not exceed PATHCCH_MAX_CCH */
138
139 for (size_t i = 0; i < PATHCCH_MAX_CCH - 1; i++)
140 Path[i] = _T('X');
141
142 Path[PATHCCH_MAX_CCH - 1] = 0;
143
144 status = PathCchAppend(Path, PATHCCH_MAX_CCH, _T("\\This cannot be appended to Path"));
145 if (SUCCEEDED(status))
146 {
147 _tprintf(_T("PathCchAppend unexpectedly succeeded with status: 0x%08") _T(PRIX32) _T("\n"),
148 status);
149 return -1;
150 }
151
152 return 0;
153}