1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
using namespace System;
using namespace Xunit;
using namespace WixBuildTools::TestSupport;
namespace DutilTests
{
public ref class PathUtil
{
public:
[Fact]
void PathGetDirectoryTest()
{
HRESULT hr = S_OK;
LPWSTR sczPath = NULL;
LPCWSTR rgwzPaths[18] =
{
L"C:\\a\\b", L"C:\\a\\",
L"C:\\a", L"C:\\",
L"C:\\", L"C:\\",
L"\"C:\\a\\b\\c\"", L"\"C:\\a\\b\\",
L"\"C:\\a\\b\\\"c", L"\"C:\\a\\b\\",
L"\"C:\\a\\b\"\\c", L"\"C:\\a\\b\"\\",
L"\"C:\\a\\\"b\\c", L"\"C:\\a\\\"b\\",
L"C:\\a\"\\\"b\\c", L"C:\\a\"\\\"b\\",
L"C:\\a\"\\b\\c\"", L"C:\\a\"\\b\\",
};
try
{
for (DWORD i = 0; i < countof(rgwzPaths); i += 2)
{
hr = PathGetDirectory(rgwzPaths[i], &sczPath);
NativeAssert::Succeeded(hr, "PathGetDirectory: {0}", rgwzPaths[i]);
NativeAssert::StringEqual(rgwzPaths[i + 1], sczPath);
}
}
finally
{
ReleaseStr(sczPath);
}
}
[Fact]
void PathGetHierarchyArrayTest()
{
HRESULT hr = S_OK;
LPWSTR *rgsczPaths = NULL;
UINT cPaths = 0;
try
{
hr = PathGetHierarchyArray(L"c:\\foo\\bar\\bas\\a.txt", &rgsczPaths, &cPaths);
NativeAssert::Succeeded(hr, "Failed to get parent directories array for regular file path");
Assert::Equal<DWORD>(5, cPaths);
NativeAssert::StringEqual(L"c:\\", rgsczPaths[0]);
NativeAssert::StringEqual(L"c:\\foo\\", rgsczPaths[1]);
NativeAssert::StringEqual(L"c:\\foo\\bar\\", rgsczPaths[2]);
NativeAssert::StringEqual(L"c:\\foo\\bar\\bas\\", rgsczPaths[3]);
NativeAssert::StringEqual(L"c:\\foo\\bar\\bas\\a.txt", rgsczPaths[4]);
ReleaseNullStrArray(rgsczPaths, cPaths);
hr = PathGetHierarchyArray(L"c:\\foo\\bar\\bas\\", &rgsczPaths, &cPaths);
NativeAssert::Succeeded(hr, "Failed to get parent directories array for regular directory path");
Assert::Equal<DWORD>(4, cPaths);
NativeAssert::StringEqual(L"c:\\", rgsczPaths[0]);
NativeAssert::StringEqual(L"c:\\foo\\", rgsczPaths[1]);
NativeAssert::StringEqual(L"c:\\foo\\bar\\", rgsczPaths[2]);
NativeAssert::StringEqual(L"c:\\foo\\bar\\bas\\", rgsczPaths[3]);
ReleaseNullStrArray(rgsczPaths, cPaths);
hr = PathGetHierarchyArray(L"\\\\server\\share\\subdir\\file.txt", &rgsczPaths, &cPaths);
NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC file path");
Assert::Equal<DWORD>(3, cPaths);
NativeAssert::StringEqual(L"\\\\server\\share\\", rgsczPaths[0]);
NativeAssert::StringEqual(L"\\\\server\\share\\subdir\\", rgsczPaths[1]);
NativeAssert::StringEqual(L"\\\\server\\share\\subdir\\file.txt", rgsczPaths[2]);
ReleaseNullStrArray(rgsczPaths, cPaths);
hr = PathGetHierarchyArray(L"\\\\server\\share\\subdir\\", &rgsczPaths, &cPaths);
NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC directory path");
Assert::Equal<DWORD>(2, cPaths);
NativeAssert::StringEqual(L"\\\\server\\share\\", rgsczPaths[0]);
NativeAssert::StringEqual(L"\\\\server\\share\\subdir\\", rgsczPaths[1]);
ReleaseNullStrArray(rgsczPaths, cPaths);
hr = PathGetHierarchyArray(L"Software\\Microsoft\\Windows\\ValueName", &rgsczPaths, &cPaths);
NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC directory path");
Assert::Equal<DWORD>(4, cPaths);
NativeAssert::StringEqual(L"Software\\", rgsczPaths[0]);
NativeAssert::StringEqual(L"Software\\Microsoft\\", rgsczPaths[1]);
NativeAssert::StringEqual(L"Software\\Microsoft\\Windows\\", rgsczPaths[2]);
NativeAssert::StringEqual(L"Software\\Microsoft\\Windows\\ValueName", rgsczPaths[3]);
ReleaseNullStrArray(rgsczPaths, cPaths);
hr = PathGetHierarchyArray(L"Software\\Microsoft\\Windows\\", &rgsczPaths, &cPaths);
NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC directory path");
Assert::Equal<DWORD>(3, cPaths);
NativeAssert::StringEqual(L"Software\\", rgsczPaths[0]);
NativeAssert::StringEqual(L"Software\\Microsoft\\", rgsczPaths[1]);
NativeAssert::StringEqual(L"Software\\Microsoft\\Windows\\", rgsczPaths[2]);
ReleaseNullStrArray(rgsczPaths, cPaths);
}
finally
{
ReleaseStrArray(rgsczPaths, cPaths);
}
}
[Fact]
void PathPrefixTest()
{
HRESULT hr = S_OK;
LPWSTR sczPath = NULL;
LPCWSTR rgwzPaths[12] =
{
L"\\\\", L"\\\\?\\UNC\\",
L"C:\\\\foo2", L"\\\\?\\C:\\\\foo2",
L"\\\\a\\b\\", L"\\\\?\\UNC\\a\\b\\",
L"\\\\?\\UNC\\test\\unc\\path\\to\\something", L"\\\\?\\UNC\\test\\unc\\path\\to\\something",
L"\\\\?\\C:\\foo\\bar.txt", L"\\\\?\\C:\\foo\\bar.txt",
L"\\??\\C:\\foo\\bar.txt", L"\\??\\C:\\foo\\bar.txt",
};
try
{
for (DWORD i = 0; i < countof(rgwzPaths) / 2; i += 2)
{
hr = StrAllocString(&sczPath, rgwzPaths[i], 0);
NativeAssert::Succeeded(hr, "Failed to copy string");
hr = PathPrefix(&sczPath);
NativeAssert::Succeeded(hr, "PathPrefix: {0}", rgwzPaths[i]);
NativeAssert::StringEqual(rgwzPaths[i + 1], sczPath);
}
}
finally
{
ReleaseStr(sczPath);
}
}
[Fact]
void PathPrefixFailureTest()
{
HRESULT hr = S_OK;
LPWSTR sczPath = NULL;
LPCWSTR rgwzPaths[8] =
{
L"\\",
L"C:",
L"C:foo.txt",
L"",
L"\\?",
L"\\dir",
L"dir",
L"dir\\subdir",
};
try
{
for (DWORD i = 0; i < countof(rgwzPaths); ++i)
{
hr = StrAllocString(&sczPath, rgwzPaths[i], 0);
NativeAssert::Succeeded(hr, "Failed to copy string");
hr = PathPrefix(&sczPath);
NativeAssert::SpecificReturnCode(E_INVALIDARG, hr, "PathPrefix: {0}, {1}", rgwzPaths[i], sczPath);
}
}
finally
{
ReleaseStr(sczPath);
}
}
[Fact]
void PathIsRootedAndFullyQualifiedTest()
{
LPCWSTR rgwzPaths[15] =
{
L"\\\\",
L"\\\\\\",
L"C:\\",
L"C:\\\\",
L"C:\\foo1",
L"C:\\\\foo2",
L"\\\\test\\unc\\path\\to\\something",
L"\\\\a\\b\\c\\d\\e",
L"\\\\a\\b\\",
L"\\\\a\\b",
L"\\\\test\\unc",
L"\\\\Server",
L"\\\\Server\\Foo.txt",
L"\\\\Server\\Share\\Foo.txt",
L"\\\\Server\\Share\\Test\\Foo.txt",
};
for (DWORD i = 0; i < countof(rgwzPaths); ++i)
{
ValidateFullyQualifiedPath(rgwzPaths[i], TRUE, FALSE);
ValidateRootedPath(rgwzPaths[i], TRUE);
}
}
[Fact]
void PathIsRootedAndFullyQualifiedWithPrefixTest()
{
LPCWSTR rgwzPaths[6] =
{
L"\\\\?\\UNC\\test\\unc\\path\\to\\something",
L"\\\\?\\UNC\\test\\unc",
L"\\\\?\\UNC\\a\\b1",
L"\\\\?\\UNC\\a\\b2\\",
L"\\\\?\\C:\\foo\\bar.txt",
L"\\??\\C:\\foo\\bar.txt",
};
for (DWORD i = 0; i < countof(rgwzPaths); ++i)
{
ValidateFullyQualifiedPath(rgwzPaths[i], TRUE, TRUE);
ValidateRootedPath(rgwzPaths[i], TRUE);
}
}
[Fact]
void PathIsRootedButNotFullyQualifiedTest()
{
LPCWSTR rgwzPaths[7] =
{
L"\\",
L"a:",
L"A:",
L"z:",
L"Z:",
L"C:foo.txt",
L"\\dir",
};
for (DWORD i = 0; i < countof(rgwzPaths); ++i)
{
ValidateFullyQualifiedPath(rgwzPaths[i], FALSE, FALSE);
ValidateRootedPath(rgwzPaths[i], TRUE);
}
}
[Fact]
void PathIsNotRootedAndNotFullyQualifiedTest()
{
LPCWSTR rgwzPaths[9] =
{
NULL,
L"",
L"dir",
L"dir\\subdir",
L"@:\\foo", // 064 = @ 065 = A
L"[:\\\\", // 091 = [ 090 = Z
L"`:\\foo ", // 096 = ` 097 = a
L"{:\\\\", // 123 = { 122 = z
L"[:",
};
for (DWORD i = 0; i < countof(rgwzPaths); ++i)
{
ValidateFullyQualifiedPath(rgwzPaths[i], FALSE, FALSE);
ValidateRootedPath(rgwzPaths[i], FALSE);
}
}
void ValidateFullyQualifiedPath(LPCWSTR wzPath, BOOL fExpected, BOOL fExpectedHasPrefix)
{
BOOL fHasLongPathPrefix = FALSE;
BOOL fRooted = PathIsFullyQualified(wzPath, &fHasLongPathPrefix);
String^ message = String::Format("IsFullyQualified: {0}", gcnew String(wzPath));
if (fExpected)
{
Assert::True(fRooted, message);
}
else
{
Assert::False(fRooted, message);
}
message = String::Format("HasLongPathPrefix: {0}", gcnew String(wzPath));
if (fExpectedHasPrefix)
{
Assert::True(fHasLongPathPrefix, message);
}
else
{
Assert::False(fHasLongPathPrefix, message);
}
}
void ValidateRootedPath(LPCWSTR wzPath, BOOL fExpected)
{
BOOL fRooted = PathIsRooted(wzPath);
String^ message = String::Format("IsRooted: {0}", gcnew String(wzPath));
if (fExpected)
{
Assert::True(fRooted, message);
}
else
{
Assert::False(fRooted, message);
}
}
};
}
|