[NTDLL_APITEST]
[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 SIZE_T Length,
32 SIZE_T MaximumLength,
33 PCWSTR Expected)
34 {
35 SIZE_T 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 %lu, expected %lu\n", (ULONG)Length, (ULONG)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 BOOLEAN Okay;
145
146 for (i = 0; i < TestCount; i++)
147 {
148 trace("i = %d\n", i);
149 switch (TestCases[i].PrefixType)
150 {
151 case PrefixNone:
152 ExpectedPathName[0] = UNICODE_NULL;
153 break;
154 case PrefixCurrentDrive:
155 GetCurrentDirectoryW(sizeof(ExpectedPathName) / sizeof(WCHAR), ExpectedPathName);
156 ExpectedPathName[3] = UNICODE_NULL;
157 break;
158 case PrefixCurrentPath:
159 Length = GetCurrentDirectoryW(sizeof(ExpectedPathName) / sizeof(WCHAR), ExpectedPathName);
160 if (Length == 3 && TestCases[i].FullPathName[0])
161 ExpectedPathName[2] = UNICODE_NULL;
162 break;
163 default:
164 skip(0, "Invalid test!\n");
165 continue;
166 }
167 wcscat(ExpectedPathName, TestCases[i].FullPathName);
168 RtlFillMemory(FullPathNameBuffer, sizeof(FullPathNameBuffer), 0xAA);
169 Length = 0;
170 StartSeh()
171 Length = RtlGetFullPathName_U(TestCases[i].FileName,
172 sizeof(FullPathNameBuffer),
173 FullPathNameBuffer,
174 &ShortName);
175 EndSeh(STATUS_SUCCESS);
176
177 Okay = CheckStringBuffer(FullPathNameBuffer, Length, sizeof(FullPathNameBuffer), ExpectedPathName);
178 ok(Okay, "Wrong path name '%S', expected '%S'\n", FullPathNameBuffer, ExpectedPathName);
179
180 if (!ShortName)
181 FilePartSize = 0;
182 else
183 FilePartSize = ShortName - FullPathNameBuffer;
184
185 switch (TestCases[i].FilePartPrefixType)
186 {
187 case PrefixNone:
188 ExpectedFilePartSize = 0;
189 break;
190 case PrefixCurrentDrive:
191 ExpectedFilePartSize = sizeof(L"C:\\");
192 break;
193 case PrefixCurrentPath:
194 ExpectedFilePartSize = GetCurrentDirectoryW(0, NULL) * sizeof(WCHAR);
195 if (ExpectedFilePartSize == sizeof(L"C:\\"))
196 ExpectedFilePartSize -= sizeof(WCHAR);
197 break;
198 case PrefixCurrentPathWithoutLastPart:
199 {
200 WCHAR CurrentPath[MAX_PATH];
201 PCWSTR BackSlash;
202 ExpectedFilePartSize = GetCurrentDirectoryW(sizeof(CurrentPath) / sizeof(WCHAR), CurrentPath) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
203 if (ExpectedFilePartSize == sizeof(L"C:\\"))
204 ExpectedFilePartSize = 0;
205 else
206 {
207 BackSlash = wcsrchr(CurrentPath, L'\\');
208 if (BackSlash)
209 ExpectedFilePartSize -= wcslen(BackSlash + 1) * sizeof(WCHAR);
210 else
211 ok(0, "GetCurrentDirectory returned %S\n", CurrentPath);
212 }
213 break;
214 }
215 default:
216 skip(0, "Invalid test!\n");
217 continue;
218 }
219 ExpectedFilePartSize += TestCases[i].FilePartSize;
220 if (ExpectedFilePartSize != 0)
221 ExpectedFilePartSize = (ExpectedFilePartSize - sizeof(UNICODE_NULL)) / sizeof(WCHAR);
222 ok(FilePartSize == ExpectedFilePartSize,
223 "FilePartSize = %lu, expected %lu\n", (ULONG)FilePartSize, (ULONG)ExpectedFilePartSize);
224 }
225 }
226
227 START_TEST(RtlGetFullPathName_U)
228 {
229 NTSTATUS ExceptionStatus;
230 PCWSTR FileName;
231 PWSTR ShortName;
232 ULONG Length;
233
234 /* Parameter checks */
235 StartSeh()
236 Length = RtlGetFullPathName_U(NULL, 0, NULL, NULL);
237 ok(Length == 0, "Length = %lu\n", Length);
238 EndSeh(STATUS_SUCCESS);
239
240 StartSeh()
241 Length = RtlGetFullPathName_U(L"", 0, NULL, NULL);
242 ok(Length == 0, "Length = %lu\n", Length);
243 EndSeh(STATUS_SUCCESS);
244
245 ShortName = InvalidPointer;
246 StartSeh()
247 Length = RtlGetFullPathName_U(NULL, 0, NULL, &ShortName);
248 ok(Length == 0, "Length = %lu\n", Length);
249 EndSeh(STATUS_SUCCESS);
250 ok(ShortName == InvalidPointer ||
251 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
252
253 StartSeh()
254 Length = RtlGetFullPathName_U(L"", 0, NULL, NULL);
255 ok(Length == 0, "Length = %lu\n", Length);
256 EndSeh(STATUS_SUCCESS);
257
258 ShortName = InvalidPointer;
259 StartSeh()
260 Length = RtlGetFullPathName_U(L"", 0, NULL, &ShortName);
261 ok(Length == 0, "Length = %lu\n", Length);
262 EndSeh(STATUS_SUCCESS);
263 ok(ShortName == InvalidPointer ||
264 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
265
266 StartSeh()
267 Length = RtlGetFullPathName_U(L"C:\\test", 0, NULL, NULL);
268 ok(Length == sizeof(L"C:\\test"), "Length = %lu\n", Length);
269 EndSeh(STATUS_SUCCESS);
270
271 FileName = L"C:\\test";
272 ShortName = InvalidPointer;
273 StartSeh()
274 Length = RtlGetFullPathName_U(FileName, 0, NULL, &ShortName);
275 ok(Length == sizeof(L"C:\\test"), "Length = %lu\n", Length);
276 EndSeh(STATUS_SUCCESS);
277 ok(ShortName == InvalidPointer ||
278 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
279
280 /* Check the actual functionality with different paths */
281 RunTestCases();
282 }