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