[COMMENTS]
[reactos.git] / rostests / apitests / ntdll / RtlDoesFileExists.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for RtlDoesFileExists_U*
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <stdio.h>
12 #include <ndk/rtlfuncs.h>
13
14 #define ok_bool_file(value, expected, file) do { \
15 if (expected) \
16 ok(value == TRUE, "File '%ls' should exist, but does not\n", file); \
17 else \
18 ok(value == FALSE, "File '%ls' should not exist, but does\n", file);\
19 } while (0)
20
21 #define ok_eq_ustr(str1, str2) do { \
22 ok((str1)->Buffer == (str2)->Buffer, "Buffer modified\n"); \
23 ok((str1)->Length == (str2)->Length, "Length modified\n"); \
24 ok((str1)->MaximumLength == (str2)->MaximumLength, "MaximumLength modified\n"); \
25 } while (0)
26
27 /*
28 BOOLEAN
29 NTAPI
30 RtlDoesFileExists_U(
31 IN PCWSTR FileName
32 );
33
34 BOOLEAN
35 NTAPI
36 RtlDoesFileExists_UEx(
37 IN PCWSTR FileName,
38 IN BOOLEAN SucceedIfBusy
39 );
40
41 BOOLEAN
42 NTAPI
43 RtlDoesFileExists_UStr(
44 IN PUNICODE_STRING FileName
45 );
46
47 BOOLEAN
48 NTAPI
49 RtlDoesFileExists_UstrEx(
50 IN PCUNICODE_STRING FileName,
51 IN BOOLEAN SucceedIfBusy
52 );
53 */
54
55 static
56 BOOLEAN
57 (NTAPI
58 *RtlDoesFileExists_UEx)(
59 IN PCWSTR FileName,
60 IN BOOLEAN SucceedIfBusy
61 )
62 //= (PVOID)0x7c8187d0 // 2003 sp1 x86
63 //= (PVOID)0x7769aeb2 // win7 sp1 wow64
64 ;
65
66 static
67 BOOLEAN
68 (NTAPI
69 *RtlDoesFileExists_UStr)(
70 IN PUNICODE_STRING FileName
71 )
72 //= (PVOID)0x7c8474e5 // 2003 sp1 x86
73 //= (PVOID)0x776ff304 // win7 sp1 wow64
74 ;
75
76 static
77 BOOLEAN
78 (NTAPI
79 *RtlDoesFileExists_UstrEx)(
80 IN PCUNICODE_STRING FileName,
81 IN BOOLEAN SucceedIfBusy
82 )
83 //= (PVOID)0x7c830f89 // 2003 sp1 x86
84 //= (PVOID)0x7769addb // win7 sp1 wow64
85 ;
86
87 START_TEST(RtlDoesFileExists)
88 {
89 BOOLEAN Ret;
90 struct
91 {
92 PCWSTR FileName;
93 BOOLEAN Exists;
94 } Tests[] =
95 {
96 { L"", FALSE },
97 { L"?", FALSE },
98 { L"*", FALSE },
99 { L":", FALSE },
100 { L";", FALSE },
101 { L"\"", FALSE },
102 { L".", TRUE },
103 { L"..", TRUE },
104 { L"/", TRUE },
105 { L"//", FALSE },
106 { L"///", FALSE },
107 { L"\\/", FALSE },
108 { L"\\//", FALSE },
109 { L"\\\\/", FALSE },
110 { L"\\/\\", FALSE },
111 { L"\\/\\\\", FALSE },
112 { L"\\/\\/\\", FALSE },
113 { L"\\", TRUE },
114 { L"\\\\", FALSE },
115 { L"\\\\\\", FALSE },
116 { L"\\\\.", FALSE },
117 { L"\\\\.\\", FALSE },
118 { L"\\\\.\\GLOBAL??", FALSE },
119 { L"\\\\.\\GLOBAL??\\", FALSE },
120 { L"\\\\?", FALSE },
121 { L"\\\\??", FALSE },
122 { L"\\\\??\\", FALSE },
123 { L"\\\\??\\C:\\", FALSE },
124 { L"\\\\.", FALSE },
125 { L"\\\\.\\", FALSE },
126 { L"C:", TRUE },
127 { L"C:/", TRUE },
128 { L"C:/\\", TRUE },
129 { L"C:\\/", TRUE },
130 { L"C:\\/\\", TRUE },
131 { L"C://", TRUE },
132 { L"C:\\", TRUE },
133 { L"C:\\\\", TRUE },
134 { L"C:\\%ls", TRUE },
135 { L"C:/%ls", TRUE },
136 { L"C://%ls", TRUE },
137 { L"C:\\/%ls", TRUE },
138 { L"C:/\\%ls", TRUE },
139 { L"C:\\/\\%ls", TRUE },
140 { L"C:\\%ls\\", TRUE },
141 { L"C:\\%ls\\ThisFolderExists", TRUE },
142 { L"C:\\%ls\\ThisDoesntExist", FALSE },
143 { L"C:\\\\%ls\\\\ThisFolderExists", TRUE },
144 { L"C:\\%ls\\ThisFolderExists\\ThisFileExists", TRUE },
145 { L"c:\\%ls\\thisfolderexists\\thisfileexists", TRUE },
146 { L"C:\\%ls\\THISFOLDEREXISTS\\THISFILEEXISTS", TRUE },
147 { L"C:\\%ls abc", FALSE },
148 { L"\"C:\\%ls\" abc", FALSE },
149 { L"\"C:\\\"", FALSE },
150 { L"C:\\%ls;C:\\", FALSE },
151 { L"%%SystemRoot%%", FALSE },
152 { L"%%SystemRoot%%\\", FALSE },
153 { L"%%SystemRoot%%\\system32", FALSE },
154 { L"NUL", FALSE },
155 { L"CON", FALSE },
156 { L"COM1", FALSE },
157 };
158 ULONG i;
159 WCHAR FileName[MAX_PATH];
160 WCHAR CustomPath[MAX_PATH] = L"RtlDoesFileExists_U_TestPath";
161 BOOL Success;
162 HANDLE Handle;
163
164 if (!RtlDoesFileExists_UEx)
165 {
166 RtlDoesFileExists_UEx = (PVOID)GetProcAddress(GetModuleHandleW(L"ntdll"), "RtlDoesFileExists_UEx");
167 if (!RtlDoesFileExists_UEx)
168 skip("RtlDoesFileExists_UEx unavailable\n");
169 }
170
171 if (!RtlDoesFileExists_UStr)
172 {
173 RtlDoesFileExists_UStr = (PVOID)GetProcAddress(GetModuleHandleW(L"ntdll"), "RtlDoesFileExists_UStr");
174 if (!RtlDoesFileExists_UStr)
175 skip("RtlDoesFileExists_UStr unavailable\n");
176 }
177
178 if (!RtlDoesFileExists_UstrEx)
179 {
180 RtlDoesFileExists_UstrEx = (PVOID)GetProcAddress(GetModuleHandleW(L"ntdll"), "RtlDoesFileExists_UstrEx");
181 if (!RtlDoesFileExists_UstrEx)
182 skip("RtlDoesFileExists_UstrEx unavailable\n");
183 }
184
185 StartSeh()
186 Ret = RtlDoesFileExists_U(NULL);
187 ok(Ret == FALSE, "NULL file exists?!\n");
188 EndSeh(STATUS_SUCCESS);
189
190 if (RtlDoesFileExists_UEx)
191 {
192 StartSeh()
193 Ret = RtlDoesFileExists_UEx(NULL, TRUE);
194 ok(Ret == FALSE, "NULL file exists?!\n");
195 Ret = RtlDoesFileExists_UEx(NULL, FALSE);
196 ok(Ret == FALSE, "NULL file exists?!\n");
197 EndSeh(STATUS_SUCCESS);
198 }
199
200 if (RtlDoesFileExists_UStr)
201 {
202 StartSeh() Ret = RtlDoesFileExists_UStr(NULL); EndSeh(STATUS_ACCESS_VIOLATION);
203 }
204
205 if (RtlDoesFileExists_UstrEx)
206 {
207 StartSeh() RtlDoesFileExists_UstrEx(NULL, FALSE); EndSeh(STATUS_ACCESS_VIOLATION);
208 StartSeh() RtlDoesFileExists_UstrEx(NULL, TRUE); EndSeh(STATUS_ACCESS_VIOLATION);
209 }
210
211 swprintf(FileName, L"C:\\%ls", CustomPath);
212 /* Make sure this directory doesn't exist */
213 while (GetFileAttributesW(FileName) != INVALID_FILE_ATTRIBUTES)
214 {
215 wcscat(CustomPath, L"X");
216 swprintf(FileName, L"C:\\%ls", CustomPath);
217 }
218 Success = CreateDirectoryW(FileName, NULL);
219 ok(Success, "CreateDirectory failed, results might not be accurate\n");
220 swprintf(FileName, L"C:\\%ls\\ThisFolderExists", CustomPath);
221 Success = CreateDirectoryW(FileName, NULL);
222 ok(Success, "CreateDirectory failed, results might not be accurate\n");
223 swprintf(FileName, L"C:\\%ls\\ThisFolderExists\\ThisFileExists", CustomPath);
224 Handle = CreateFileW(FileName, 0, 0, NULL, CREATE_NEW, 0, NULL);
225 ok(Handle != INVALID_HANDLE_VALUE, "CreateFile failed, results might not be accurate\n");
226 if (Handle != INVALID_HANDLE_VALUE)
227 {
228 /* Check SucceedIfBusy behavior */
229 if (RtlDoesFileExists_UEx)
230 {
231 Ret = RtlDoesFileExists_UEx(FileName, TRUE);
232 ok_bool_file(Ret, TRUE, FileName);
233 /* TODO: apparently we have to do something worse to make this fail */
234 Ret = RtlDoesFileExists_UEx(FileName, FALSE);
235 ok_bool_file(Ret, TRUE, FileName);
236 }
237 if (RtlDoesFileExists_UstrEx)
238 {
239 UNICODE_STRING FileNameString;
240 UNICODE_STRING TempString;
241 RtlInitUnicodeString(&FileNameString, FileName);
242 TempString = FileNameString;
243 Ret = RtlDoesFileExists_UstrEx(&FileNameString, TRUE);
244 ok_eq_ustr(&FileNameString, &TempString);
245 ok_bool_file(Ret, TRUE, FileName);
246 /* TODO: apparently we have to do something worse to make this fail */
247 Ret = RtlDoesFileExists_UstrEx(&FileNameString, FALSE);
248 ok_eq_ustr(&FileNameString, &TempString);
249 ok_bool_file(Ret, TRUE, FileName);
250 }
251 CloseHandle(Handle);
252 }
253
254 for (i = 0; i < sizeof(Tests) / sizeof(Tests[0]); i++)
255 {
256 swprintf(FileName, Tests[i].FileName, CustomPath);
257 StartSeh()
258 Ret = RtlDoesFileExists_U(FileName);
259 ok_bool_file(Ret, Tests[i].Exists, FileName);
260 EndSeh(STATUS_SUCCESS);
261 if (RtlDoesFileExists_UEx)
262 {
263 StartSeh()
264 Ret = RtlDoesFileExists_UEx(FileName, TRUE);
265 ok_bool_file(Ret, Tests[i].Exists, FileName);
266 EndSeh(STATUS_SUCCESS);
267 StartSeh()
268 Ret = RtlDoesFileExists_UEx(FileName, FALSE);
269 ok_bool_file(Ret, Tests[i].Exists, FileName);
270 EndSeh(STATUS_SUCCESS);
271 }
272 /* TODO: use guarded memory to make sure these don't touch the null terminator */
273 if (RtlDoesFileExists_UStr)
274 {
275 UNICODE_STRING FileNameString;
276 UNICODE_STRING TempString;
277 RtlInitUnicodeString(&FileNameString, FileName);
278 TempString = FileNameString;
279 StartSeh()
280 Ret = RtlDoesFileExists_UStr(&FileNameString);
281 ok_bool_file(Ret, Tests[i].Exists, FileName);
282 EndSeh(STATUS_SUCCESS);
283 ok_eq_ustr(&FileNameString, &TempString);
284 }
285 if (RtlDoesFileExists_UstrEx)
286 {
287 UNICODE_STRING FileNameString;
288 UNICODE_STRING TempString;
289 RtlInitUnicodeString(&FileNameString, FileName);
290 TempString = FileNameString;
291 StartSeh()
292 Ret = RtlDoesFileExists_UstrEx(&FileNameString, TRUE);
293 ok_bool_file(Ret, Tests[i].Exists, FileName);
294 EndSeh(STATUS_SUCCESS);
295 ok_eq_ustr(&FileNameString, &TempString);
296 StartSeh()
297 Ret = RtlDoesFileExists_UstrEx(&FileNameString, FALSE);
298 ok_bool_file(Ret, Tests[i].Exists, FileName);
299 EndSeh(STATUS_SUCCESS);
300 ok_eq_ustr(&FileNameString, &TempString);
301 }
302 }
303
304 swprintf(FileName, L"C:\\%ls\\ThisFolderExists\\ThisFileExists", CustomPath);
305 Success = DeleteFileW(FileName);
306 ok(Success, "DeleteFile failed, test might leave stale file\n");
307 swprintf(FileName, L"C:\\%ls\\ThisFolderExists", CustomPath);
308 Success = RemoveDirectoryW(FileName);
309 ok(Success, "RemoveDirectory failed, test might leave stale directory\n");
310 swprintf(FileName, L"C:\\%ls", CustomPath);
311 Success = RemoveDirectoryW(FileName);
312 ok(Success, "RemoveDirectory failed, test might leave stale directory\n");
313 }