FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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 NULL Path is an invalid argument */
95 status = PathCchAppend(NULL, PATHCCH_MAX_CCH, testMorePathNoBackslash);
96 if (status != E_INVALIDARG)
97 {
98 _tprintf(_T("PathCchAppend with NULL path unexpectedly returned status: 0x%08") _T(
99 PRIX32) _T("\n"),
100 status);
101 return -1;
102 }
103
104 /* According to msdn a NULL pszMore is an invalid argument (although optional !?) */
105 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
106 status = PathCchAppend(Path, PATHCCH_MAX_CCH, NULL);
107 if (status != E_INVALIDARG)
108 {
109 _tprintf(_T("PathCchAppend with NULL pszMore unexpectedly returned status: 0x%08") _T(
110 PRIX32) _T("\n"),
111 status);
112 return -1;
113 }
114
115 /* According to msdn cchPath must be > 0 and <= PATHCCH_MAX_CCH */
116 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
117 status = PathCchAppend(Path, 0, testMorePathNoBackslash);
118 if (status != E_INVALIDARG)
119 {
120 _tprintf(_T("PathCchAppend with cchPath value 0 unexpectedly returned status: 0x%08") _T(
121 PRIX32) _T("\n"),
122 status);
123 return -1;
124 }
125 _tcsncpy(Path, testBasePathNoBackslash, ARRAYSIZE(Path));
126 status = PathCchAppend(Path, PATHCCH_MAX_CCH + 1, testMorePathNoBackslash);
127 if (status != E_INVALIDARG)
128 {
129 _tprintf(_T("PathCchAppend with cchPath value > PATHCCH_MAX_CCH unexpectedly returned ")
130 _T("status: 0x%08") _T(PRIX32) _T("\n"),
131 status);
132 return -1;
133 }
134
135 /* Resulting file must not exceed PATHCCH_MAX_CCH */
136
137 for (size_t i = 0; i < PATHCCH_MAX_CCH - 1; i++)
138 Path[i] = _T('X');
139
140 Path[PATHCCH_MAX_CCH - 1] = 0;
141
142 status = PathCchAppend(Path, PATHCCH_MAX_CCH, _T("\\This cannot be appended to Path"));
143 if (SUCCEEDED(status))
144 {
145 _tprintf(_T("PathCchAppend unexpectedly succeeded with status: 0x%08") _T(PRIX32) _T("\n"),
146 status);
147 return -1;
148 }
149
150 return 0;
151}