7f7936d7a6459305dace5f15c69a795fdab3de18
[reactos.git] / rostests / apitests / ntdll / RtlGetFullPathName_U.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for RtlGetFullPathName_U
5 * PROGRAMMER: Thomas Faber <thfabba@gmx.de>
6 */
7
8 #define WIN32_NO_STATUS
9 #include <wine/test.h>
10 #include <pseh/pseh2.h>
11 #include <ndk/rtlfuncs.h>
12
13 /*
14 ULONG
15 NTAPI
16 RtlGetFullPathName_U(
17 IN PCWSTR FileName,
18 IN ULONG Size,
19 IN PWSTR Buffer,
20 OUT PWSTR *ShortName
21 );
22 */
23
24 #define StartSeh() ExceptionStatus = STATUS_SUCCESS; _SEH2_TRY {
25 #define EndSeh(ExpectedStatus) } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { ExceptionStatus = _SEH2_GetExceptionCode(); } _SEH2_END; ok(ExceptionStatus == ExpectedStatus, "Exception %lx, expected %lx\n", ExceptionStatus, ExpectedStatus)
26
27 static
28 BOOLEAN
29 CheckStringBuffer(
30 PCWSTR Buffer,
31 ULONG Length,
32 SIZE_T MaximumLength,
33 PCWSTR Expected)
34 {
35 USHORT ExpectedLength = wcslen(Expected) * sizeof(WCHAR);
36 SIZE_T EqualLength;
37 BOOLEAN Result = TRUE;
38 SIZE_T i;
39
40 if (Length != ExpectedLength)
41 {
42 ok(0, "String length is %u, expected %u\n", Length, ExpectedLength);
43 Result = FALSE;
44 }
45
46 EqualLength = RtlCompareMemory(Buffer, Expected, Length);
47 if (EqualLength != Length)
48 {
49 ok(0, "String is '%S', expected '%S'\n", Buffer, Expected);
50 Result = FALSE;
51 }
52
53 if (Buffer[Length / sizeof(WCHAR)] != UNICODE_NULL)
54 {
55 ok(0, "Not null terminated\n");
56 Result = FALSE;
57 }
58
59 /* The function nulls the rest of the buffer! */
60 for (i = Length + sizeof(UNICODE_NULL); i < MaximumLength; i++)
61 {
62 UCHAR Char = ((PUCHAR)Buffer)[i];
63 if (Char != 0)
64 {
65 ok(0, "Found 0x%x at offset %lu, expected 0x%x\n", Char, (ULONG)i, 0);
66 /* Don't count this as a failure unless the string was actually wrong */
67 //Result = FALSE;
68 /* Don't flood the log */
69 break;
70 }
71 }
72
73 return Result;
74 }
75
76 #define InvalidPointer ((PVOID)0x0123456789ABCDEFULL)
77
78 /* winetest_platform is "windows" for us, so broken() doesn't do what it should :( */
79 #undef broken
80 #define broken(x) 0
81
82 typedef enum
83 {
84 PrefixNone,
85 PrefixCurrentDrive,
86 PrefixCurrentPath,
87 PrefixCurrentPathWithoutLastPart
88 } PREFIX_TYPE;
89
90 static
91 VOID
92 RunTestCases(VOID)
93 {
94 /* TODO: don't duplicate this in the other tests */
95 /* TODO: Drive Relative tests don't work yet if the current drive isn't C: */
96 struct
97 {
98 PCWSTR FileName;
99 PREFIX_TYPE PrefixType;
100 PCWSTR FullPathName;
101 PREFIX_TYPE FilePartPrefixType;
102 SIZE_T FilePartSize;
103 } TestCases[] =
104 {
105 { L"C:", PrefixCurrentPath, L"", PrefixCurrentPathWithoutLastPart },
106 { L"C:\\", PrefixNone, L"C:\\" },
107 { L"C:\\test", PrefixNone, L"C:\\test", PrefixCurrentDrive },
108 { L"C:\\test\\", PrefixNone, L"C:\\test\\" },
109 { L"C:/test/", PrefixNone, L"C:\\test\\" },
110
111 { L"C:\\\\test", PrefixNone, L"C:\\test", PrefixCurrentDrive },
112 { L"test", PrefixCurrentPath, L"\\test", PrefixCurrentPath, sizeof(WCHAR) },
113 { L"\\test", PrefixCurrentDrive, L"test", PrefixCurrentDrive },
114 { L"/test", PrefixCurrentDrive, L"test", PrefixCurrentDrive },
115 { L".\\test", PrefixCurrentPath, L"\\test", PrefixCurrentPath, sizeof(WCHAR) },
116
117 { L"\\.", PrefixCurrentDrive, L"" },
118 { L"\\.\\", PrefixCurrentDrive, L"" },
119 { L"\\\\.", PrefixNone, L"\\\\.\\" },
120 { L"\\\\.\\", PrefixNone, L"\\\\.\\" },
121 { L"\\\\.\\Something\\", PrefixNone, L"\\\\.\\Something\\" },
122
123 { L"\\??\\", PrefixCurrentDrive, L"??\\" },
124 { L"\\??\\C:", PrefixCurrentDrive, L"??\\C:", PrefixCurrentDrive, 3 * sizeof(WCHAR) },
125 { L"\\??\\C:\\", PrefixCurrentDrive, L"??\\C:\\" },
126 { L"\\??\\C:\\test", PrefixCurrentDrive, L"??\\C:\\test", PrefixCurrentDrive, 6 * sizeof(WCHAR) },
127 { L"\\??\\C:\\test\\", PrefixCurrentDrive, L"??\\C:\\test\\" },
128
129 { L"\\\\??\\", PrefixNone, L"\\\\??\\" },
130 { L"\\\\??\\C:", PrefixNone, L"\\\\??\\C:" },
131 { L"\\\\??\\C:\\", PrefixNone, L"\\\\??\\C:\\" },
132 { L"\\\\??\\C:\\test", PrefixNone, L"\\\\??\\C:\\test", PrefixNone, sizeof(L"\\\\??\\C:\\") },
133 { L"\\\\??\\C:\\test\\", PrefixNone, L"\\\\??\\C:\\test\\" },
134 };
135 NTSTATUS ExceptionStatus;
136 WCHAR FullPathNameBuffer[MAX_PATH];
137 PWSTR ShortName;
138 SIZE_T Length;
139 WCHAR ExpectedPathName[MAX_PATH];
140 SIZE_T FilePartSize;
141 SIZE_T ExpectedFilePartSize;
142 const INT TestCount = sizeof(TestCases) / sizeof(TestCases[0]);
143 INT i;
144
145 for (i = 0; i < TestCount; i++)
146 {
147 trace("i = %d\n", i);
148 switch (TestCases[i].PrefixType)
149 {
150 case PrefixNone:
151 ExpectedPathName[0] = UNICODE_NULL;
152 break;
153 case PrefixCurrentDrive:
154 GetCurrentDirectoryW(sizeof(ExpectedPathName) / sizeof(WCHAR), ExpectedPathName);
155 ExpectedPathName[3] = UNICODE_NULL;
156 break;
157 case PrefixCurrentPath:
158 Length = GetCurrentDirectoryW(sizeof(ExpectedPathName) / sizeof(WCHAR), ExpectedPathName);
159 if (Length == 3 && TestCases[i].FullPathName[0])
160 ExpectedPathName[2] = UNICODE_NULL;
161 break;
162 default:
163 skip(0, "Invalid test!\n");
164 continue;
165 }
166 wcscat(ExpectedPathName, TestCases[i].FullPathName);
167 RtlFillMemory(FullPathNameBuffer, sizeof(FullPathNameBuffer), 0xAA);
168 Length = 0;
169 StartSeh()
170 Length = RtlGetFullPathName_U(TestCases[i].FileName,
171 sizeof(FullPathNameBuffer),
172 FullPathNameBuffer,
173 &ShortName);
174 EndSeh(STATUS_SUCCESS);
175
176 ok(CheckStringBuffer(FullPathNameBuffer, Length, sizeof(FullPathNameBuffer), ExpectedPathName),
177 "Wrong path name '%S', expected '%S'\n", FullPathNameBuffer, ExpectedPathName);
178
179 if (!ShortName)
180 FilePartSize = 0;
181 else
182 FilePartSize = ShortName - FullPathNameBuffer;
183
184 switch (TestCases[i].FilePartPrefixType)
185 {
186 case PrefixNone:
187 ExpectedFilePartSize = 0;
188 break;
189 case PrefixCurrentDrive:
190 ExpectedFilePartSize = sizeof(L"C:\\");
191 break;
192 case PrefixCurrentPath:
193 ExpectedFilePartSize = GetCurrentDirectoryW(0, NULL) * sizeof(WCHAR);
194 if (ExpectedFilePartSize == sizeof(L"C:\\"))
195 ExpectedFilePartSize -= sizeof(WCHAR);
196 break;
197 case PrefixCurrentPathWithoutLastPart:
198 {
199 WCHAR CurrentPath[MAX_PATH];
200 PCWSTR BackSlash;
201 ExpectedFilePartSize = GetCurrentDirectoryW(sizeof(CurrentPath) / sizeof(WCHAR), CurrentPath) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
202 if (ExpectedFilePartSize == sizeof(L"C:\\"))
203 ExpectedFilePartSize = 0;
204 else
205 {
206 BackSlash = wcsrchr(CurrentPath, L'\\');
207 if (BackSlash)
208 ExpectedFilePartSize -= wcslen(BackSlash + 1) * sizeof(WCHAR);
209 else
210 ok(0, "GetCurrentDirectory returned %S\n", CurrentPath);
211 }
212 break;
213 }
214 default:
215 skip(0, "Invalid test!\n");
216 continue;
217 }
218 ExpectedFilePartSize += TestCases[i].FilePartSize;
219 if (ExpectedFilePartSize != 0)
220 ExpectedFilePartSize = (ExpectedFilePartSize - sizeof(UNICODE_NULL)) / sizeof(WCHAR);
221 ok(FilePartSize == ExpectedFilePartSize,
222 "FilePartSize = %lu, expected %lu\n", (ULONG)FilePartSize, (ULONG)ExpectedFilePartSize);
223 }
224 }
225
226 START_TEST(RtlGetFullPathName_U)
227 {
228 NTSTATUS ExceptionStatus;
229 PCWSTR FileName;
230 PWSTR ShortName;
231 ULONG Length;
232
233 /* Parameter checks */
234 StartSeh()
235 Length = RtlGetFullPathName_U(NULL, 0, NULL, NULL);
236 ok(Length == 0, "Length = %lu\n", Length);
237 EndSeh(STATUS_SUCCESS);
238
239 StartSeh()
240 Length = RtlGetFullPathName_U(L"", 0, NULL, NULL);
241 ok(Length == 0, "Length = %lu\n", Length);
242 EndSeh(STATUS_SUCCESS);
243
244 ShortName = InvalidPointer;
245 StartSeh()
246 Length = RtlGetFullPathName_U(NULL, 0, NULL, &ShortName);
247 ok(Length == 0, "Length = %lu\n", Length);
248 EndSeh(STATUS_SUCCESS);
249 ok(ShortName == InvalidPointer ||
250 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
251
252 StartSeh()
253 Length = RtlGetFullPathName_U(L"", 0, NULL, NULL);
254 ok(Length == 0, "Length = %lu\n", Length);
255 EndSeh(STATUS_SUCCESS);
256
257 ShortName = InvalidPointer;
258 StartSeh()
259 Length = RtlGetFullPathName_U(L"", 0, NULL, &ShortName);
260 ok(Length == 0, "Length = %lu\n", Length);
261 EndSeh(STATUS_SUCCESS);
262 ok(ShortName == InvalidPointer ||
263 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
264
265 StartSeh()
266 Length = RtlGetFullPathName_U(L"C:\\test", 0, NULL, NULL);
267 ok(Length == sizeof(L"C:\\test"), "Length = %lu\n", Length);
268 EndSeh(STATUS_SUCCESS);
269
270 FileName = L"C:\\test";
271 ShortName = InvalidPointer;
272 StartSeh()
273 Length = RtlGetFullPathName_U(FileName, 0, NULL, &ShortName);
274 ok(Length == sizeof(L"C:\\test"), "Length = %lu\n", Length);
275 EndSeh(STATUS_SUCCESS);
276 ok(ShortName == InvalidPointer ||
277 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
278
279 /* Check the actual functionality with different paths */
280 RunTestCases();
281 }