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