[NTDLL_WINETEST]
[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)0x1234)
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 here and in the RtlGetFullPathName_UstrEx test */
95 struct
96 {
97 PCWSTR FileName;
98 PREFIX_TYPE PrefixType;
99 PCWSTR FullPathName;
100 PREFIX_TYPE FilePartPrefixType;
101 SIZE_T FilePartSize;
102 } TestCases[] =
103 {
104 { L"C:", PrefixCurrentPath, L"", PrefixCurrentPathWithoutLastPart },
105 { L"C:\\", PrefixNone, L"C:\\" },
106 { L"C:\\test", PrefixNone, L"C:\\test", PrefixCurrentDrive },
107 { L"C:\\test\\", PrefixNone, L"C:\\test\\" },
108 { L"C:/test/", PrefixNone, L"C:\\test\\" },
109
110 { L"C:\\\\test", PrefixNone, L"C:\\test", PrefixCurrentDrive },
111 { L"test", PrefixCurrentPath, L"\\test", PrefixCurrentPath, sizeof(WCHAR) },
112 { L"\\test", PrefixCurrentDrive, L"test", PrefixCurrentDrive },
113 { L"/test", PrefixCurrentDrive, L"test", PrefixCurrentDrive },
114 { L".\\test", PrefixCurrentPath, L"\\test", PrefixCurrentPath, sizeof(WCHAR) },
115
116 { L"\\.", PrefixCurrentDrive, L"" },
117 { L"\\.\\", PrefixCurrentDrive, L"" },
118 { L"\\\\.", PrefixNone, L"\\\\.\\" },
119 { L"\\\\.\\", PrefixNone, L"\\\\.\\" },
120 { L"\\\\.\\Something\\", PrefixNone, L"\\\\.\\Something\\" },
121
122 { L"\\??\\", PrefixCurrentDrive, L"??\\" },
123 { L"\\??\\C:", PrefixCurrentDrive, L"??\\C:", PrefixCurrentDrive, 3 * sizeof(WCHAR) },
124 { L"\\??\\C:\\", PrefixCurrentDrive, L"??\\C:\\" },
125 { L"\\??\\C:\\test", PrefixCurrentDrive, L"??\\C:\\test", PrefixCurrentDrive, 6 * sizeof(WCHAR) },
126 { L"\\??\\C:\\test\\", PrefixCurrentDrive, L"??\\C:\\test\\" },
127
128 { L"\\\\??\\", PrefixNone, L"\\\\??\\" },
129 { L"\\\\??\\C:", PrefixNone, L"\\\\??\\C:" },
130 { L"\\\\??\\C:\\", PrefixNone, L"\\\\??\\C:\\" },
131 { L"\\\\??\\C:\\test", PrefixNone, L"\\\\??\\C:\\test", PrefixNone, sizeof(L"\\\\??\\C:\\") },
132 { L"\\\\??\\C:\\test\\", PrefixNone, L"\\\\??\\C:\\test\\" },
133 };
134 NTSTATUS ExceptionStatus;
135 WCHAR FullPathNameBuffer[MAX_PATH];
136 PWSTR ShortName;
137 SIZE_T Length;
138 WCHAR ExpectedPathName[MAX_PATH];
139 SIZE_T FilePartSize;
140 SIZE_T ExpectedFilePartSize;
141 const INT TestCount = sizeof(TestCases) / sizeof(TestCases[0]);
142 INT i;
143
144 for (i = 0; i < TestCount; i++)
145 {
146 trace("i = %d\n", i);
147 switch (TestCases[i].PrefixType)
148 {
149 case PrefixNone:
150 ExpectedPathName[0] = UNICODE_NULL;
151 break;
152 case PrefixCurrentDrive:
153 GetCurrentDirectoryW(sizeof(ExpectedPathName) / sizeof(WCHAR), ExpectedPathName);
154 ExpectedPathName[3] = UNICODE_NULL;
155 break;
156 case PrefixCurrentPath:
157 Length = GetCurrentDirectoryW(sizeof(ExpectedPathName) / sizeof(WCHAR), ExpectedPathName);
158 if (Length == 3 && TestCases[i].FullPathName[0])
159 ExpectedPathName[2] = UNICODE_NULL;
160 break;
161 default:
162 skip(0, "Invalid test!\n");
163 continue;
164 }
165 wcscat(ExpectedPathName, TestCases[i].FullPathName);
166 RtlFillMemory(FullPathNameBuffer, sizeof(FullPathNameBuffer), 0xAA);
167 Length = 0;
168 StartSeh()
169 Length = RtlGetFullPathName_U(TestCases[i].FileName,
170 sizeof(FullPathNameBuffer),
171 FullPathNameBuffer,
172 &ShortName);
173 EndSeh(STATUS_SUCCESS);
174
175 /* TODO: remove SEH here */
176 StartSeh()
177 ok(CheckStringBuffer(FullPathNameBuffer, Length, sizeof(FullPathNameBuffer), ExpectedPathName),
178 "Wrong path name '%S', expected '%S'\n", FullPathNameBuffer, ExpectedPathName);
179 EndSeh(STATUS_SUCCESS);
180
181 if (!ShortName)
182 FilePartSize = 0;
183 else
184 FilePartSize = ShortName - FullPathNameBuffer;
185
186 switch (TestCases[i].FilePartPrefixType)
187 {
188 case PrefixNone:
189 ExpectedFilePartSize = 0;
190 break;
191 case PrefixCurrentDrive:
192 ExpectedFilePartSize = sizeof(L"C:\\");
193 break;
194 case PrefixCurrentPath:
195 ExpectedFilePartSize = GetCurrentDirectoryW(0, NULL) * sizeof(WCHAR);
196 if (ExpectedFilePartSize == sizeof(L"C:\\"))
197 ExpectedFilePartSize -= sizeof(WCHAR);
198 break;
199 case PrefixCurrentPathWithoutLastPart:
200 {
201 WCHAR CurrentPath[MAX_PATH];
202 PCWSTR BackSlash;
203 ExpectedFilePartSize = GetCurrentDirectoryW(sizeof(CurrentPath) / sizeof(WCHAR), CurrentPath) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
204 if (ExpectedFilePartSize == sizeof(L"C:\\"))
205 ExpectedFilePartSize = 0;
206 else
207 {
208 BackSlash = wcsrchr(CurrentPath, L'\\');
209 if (BackSlash)
210 ExpectedFilePartSize -= wcslen(BackSlash + 1) * sizeof(WCHAR);
211 else
212 ok(0, "GetCurrentDirectory returned %S\n", CurrentPath);
213 }
214 break;
215 }
216 default:
217 skip(0, "Invalid test!\n");
218 continue;
219 }
220 ExpectedFilePartSize += TestCases[i].FilePartSize;
221 if (ExpectedFilePartSize != 0)
222 ExpectedFilePartSize = (ExpectedFilePartSize - sizeof(UNICODE_NULL)) / sizeof(WCHAR);
223 ok(FilePartSize == ExpectedFilePartSize,
224 "FilePartSize = %lu, expected %lu\n", (ULONG)FilePartSize, (ULONG)ExpectedFilePartSize);
225 }
226 }
227
228 START_TEST(RtlGetFullPathName_U)
229 {
230 NTSTATUS ExceptionStatus;
231 PCWSTR FileName;
232 PWSTR ShortName;
233 ULONG Length;
234
235 /* Parameter checks */
236 StartSeh()
237 Length = RtlGetFullPathName_U(NULL, 0, NULL, NULL);
238 ok(Length == 0, "Length = %lu\n", Length);
239 EndSeh(STATUS_SUCCESS);
240
241 StartSeh()
242 Length = RtlGetFullPathName_U(L"", 0, NULL, NULL);
243 ok(Length == 0, "Length = %lu\n", Length);
244 EndSeh(STATUS_SUCCESS);
245
246 ShortName = InvalidPointer;
247 StartSeh()
248 Length = RtlGetFullPathName_U(NULL, 0, NULL, &ShortName);
249 ok(Length == 0, "Length = %lu\n", Length);
250 EndSeh(STATUS_SUCCESS);
251 ok(ShortName == InvalidPointer ||
252 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
253
254 StartSeh()
255 Length = RtlGetFullPathName_U(L"", 0, NULL, NULL);
256 ok(Length == 0, "Length = %lu\n", Length);
257 EndSeh(STATUS_SUCCESS);
258
259 ShortName = InvalidPointer;
260 StartSeh()
261 Length = RtlGetFullPathName_U(L"", 0, NULL, &ShortName);
262 ok(Length == 0, "Length = %lu\n", Length);
263 EndSeh(STATUS_SUCCESS);
264 ok(ShortName == InvalidPointer ||
265 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
266
267 StartSeh()
268 Length = RtlGetFullPathName_U(L"C:\\test", 0, NULL, NULL);
269 ok(Length == sizeof(L"C:\\test"), "Length = %lu\n", Length);
270 EndSeh(STATUS_SUCCESS);
271
272 FileName = L"C:\\test";
273 ShortName = InvalidPointer;
274 StartSeh()
275 Length = RtlGetFullPathName_U(FileName, 0, NULL, &ShortName);
276 ok(Length == sizeof(L"C:\\test"), "Length = %lu\n", Length);
277 EndSeh(STATUS_SUCCESS);
278 ok(ShortName == InvalidPointer ||
279 broken(ShortName == NULL) /* Win7 */, "ShortName = %p\n", ShortName);
280
281 /* check the actual functionality with different paths */
282 RunTestCases();
283 }