[NTDLL_WINETEST]
[reactos.git] / rostests / winetests / ntdll / file.c
1 /* Unit test suite for Ntdll file functions
2 *
3 * Copyright 2007 Jeff Latimer
4 * Copyright 2007 Andrey Turkin
5 * Copyright 2008 Jeff Zaroyko
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * NOTES
22 * We use function pointers here as there is no import library for NTDLL on
23 * windows.
24 */
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include "ntstatus.h"
30 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
31 * definition errors when we get to winnt.h
32 */
33 #define WIN32_NO_STATUS
34
35 #include "wine/test.h"
36 #include "winternl.h"
37 #include "winuser.h"
38
39 #ifndef IO_COMPLETION_ALL_ACCESS
40 #define IO_COMPLETION_ALL_ACCESS 0x001F0003
41 #endif
42
43 static BOOL (WINAPI * pGetVolumePathNameW)(LPCWSTR, LPWSTR, DWORD);
44 static UINT (WINAPI *pGetSystemWow64DirectoryW)( LPWSTR, UINT );
45
46 static VOID (WINAPI *pRtlFreeUnicodeString)( PUNICODE_STRING );
47 static VOID (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
48 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)( LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR* );
49 static NTSTATUS (WINAPI *pRtlWow64EnableFsRedirectionEx)( ULONG, ULONG * );
50
51 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
52 ULONG, ULONG, ULONG, PLARGE_INTEGER );
53 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,PLARGE_INTEGER,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG);
54 static NTSTATUS (WINAPI *pNtOpenFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,ULONG,ULONG);
55 static NTSTATUS (WINAPI *pNtDeleteFile)(POBJECT_ATTRIBUTES ObjectAttributes);
56 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
57 PIO_APC_ROUTINE apc, void* apc_user,
58 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
59 PLARGE_INTEGER offset, PULONG key);
60 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
61 PIO_APC_ROUTINE apc, void* apc_user,
62 PIO_STATUS_BLOCK io_status,
63 const void* buffer, ULONG length,
64 PLARGE_INTEGER offset, PULONG key);
65 static NTSTATUS (WINAPI *pNtCancelIoFile)(HANDLE hFile, PIO_STATUS_BLOCK io_status);
66 static NTSTATUS (WINAPI *pNtCancelIoFileEx)(HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status);
67 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
68
69 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
70 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
71 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
72 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
73 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
74 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
75 static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
76 static NTSTATUS (WINAPI *pNtQueryDirectoryFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,
77 PVOID,ULONG,FILE_INFORMATION_CLASS,BOOLEAN,PUNICODE_STRING,BOOLEAN);
78
79 static inline BOOL is_signaled( HANDLE obj )
80 {
81 return WaitForSingleObject( obj, 0 ) == 0;
82 }
83
84 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
85 #define TEST_BUF_LEN 3
86
87 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
88 {
89 *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
90 1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
91 ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
92
93 *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
94 ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
95
96 return TRUE;
97 }
98
99 static HANDLE create_temp_file( ULONG flags )
100 {
101 char buffer[MAX_PATH];
102 HANDLE handle;
103
104 GetTempFileNameA( ".", "foo", 0, buffer );
105 handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
106 flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
107 ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
108 return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
109 }
110
111 #define CVALUE_FIRST 0xfffabbcc
112 #define CKEY_FIRST 0x1030341
113 #define CKEY_SECOND 0x132E46
114
115 ULONG_PTR completionKey;
116 IO_STATUS_BLOCK ioSb;
117 ULONG_PTR completionValue;
118
119 static long get_pending_msgs(HANDLE h)
120 {
121 NTSTATUS res;
122 ULONG a, req;
123
124 res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, &a, sizeof(a), &req );
125 ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
126 if (res != STATUS_SUCCESS) return -1;
127 ok( req == sizeof(a), "Unexpected response size: %x\n", req );
128 return a;
129 }
130
131 static BOOL get_msg(HANDLE h)
132 {
133 LARGE_INTEGER timeout = {{-10000000*3}};
134 DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
135 ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
136 if (res != STATUS_SUCCESS)
137 {
138 completionKey = completionValue = 0;
139 memset(&ioSb, 0, sizeof(ioSb));
140 return FALSE;
141 }
142 return TRUE;
143 }
144
145
146 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
147 {
148 int *count = arg;
149
150 trace( "apc called block %p iosb.status %x iosb.info %lu\n",
151 iosb, U(*iosb).Status, iosb->Information );
152 (*count)++;
153 ok( !reserved, "reserved is not 0: %x\n", reserved );
154 }
155
156 static void create_file_test(void)
157 {
158 static const WCHAR systemrootW[] = {'\\','S','y','s','t','e','m','R','o','o','t',
159 '\\','f','a','i','l','i','n','g',0};
160 NTSTATUS status;
161 HANDLE dir;
162 WCHAR path[MAX_PATH];
163 OBJECT_ATTRIBUTES attr;
164 IO_STATUS_BLOCK io;
165 UNICODE_STRING nameW;
166 UINT len;
167
168 len = GetCurrentDirectoryW( MAX_PATH, path );
169 pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
170 attr.Length = sizeof(attr);
171 attr.RootDirectory = 0;
172 attr.ObjectName = &nameW;
173 attr.Attributes = OBJ_CASE_INSENSITIVE;
174 attr.SecurityDescriptor = NULL;
175 attr.SecurityQualityOfService = NULL;
176
177 /* try various open modes and options on directories */
178 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
179 FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0 );
180 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
181 CloseHandle( dir );
182
183 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
184 FILE_CREATE, FILE_DIRECTORY_FILE, NULL, 0 );
185 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
186 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
187
188 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
189 FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0 );
190 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
191 CloseHandle( dir );
192
193 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
194 FILE_SUPERSEDE, FILE_DIRECTORY_FILE, NULL, 0 );
195 ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
196
197 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
198 FILE_OVERWRITE, FILE_DIRECTORY_FILE, NULL, 0 );
199 ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
200
201 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
202 FILE_OVERWRITE_IF, FILE_DIRECTORY_FILE, NULL, 0 );
203 ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
204
205 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
206 FILE_OPEN, 0, NULL, 0 );
207 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
208 CloseHandle( dir );
209
210 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
211 FILE_CREATE, 0, NULL, 0 );
212 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
213 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
214
215 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
216 FILE_OPEN_IF, 0, NULL, 0 );
217 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
218 CloseHandle( dir );
219
220 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
221 FILE_SUPERSEDE, 0, NULL, 0 );
222 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
223 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
224
225 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
226 FILE_OVERWRITE, 0, NULL, 0 );
227 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
228 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
229
230 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
231 FILE_OVERWRITE_IF, 0, NULL, 0 );
232 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
233 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
234
235 pRtlFreeUnicodeString( &nameW );
236
237 pRtlInitUnicodeString( &nameW, systemrootW );
238 attr.Length = sizeof(attr);
239 attr.RootDirectory = NULL;
240 attr.ObjectName = &nameW;
241 attr.Attributes = OBJ_CASE_INSENSITIVE;
242 attr.SecurityDescriptor = NULL;
243 attr.SecurityQualityOfService = NULL;
244 dir = NULL;
245 status = pNtCreateFile( &dir, FILE_APPEND_DATA, &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
246 FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
247 todo_wine
248 ok( status == STATUS_INVALID_PARAMETER,
249 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
250 pRtlFreeUnicodeString( &nameW );
251 }
252
253 static void open_file_test(void)
254 {
255 NTSTATUS status;
256 HANDLE dir, root, handle;
257 WCHAR path[MAX_PATH];
258 BYTE data[8192];
259 OBJECT_ATTRIBUTES attr;
260 IO_STATUS_BLOCK io;
261 UNICODE_STRING nameW;
262 UINT i, len;
263 BOOL restart = TRUE;
264
265 len = GetWindowsDirectoryW( path, MAX_PATH );
266 pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
267 attr.Length = sizeof(attr);
268 attr.RootDirectory = 0;
269 attr.ObjectName = &nameW;
270 attr.Attributes = OBJ_CASE_INSENSITIVE;
271 attr.SecurityDescriptor = NULL;
272 attr.SecurityQualityOfService = NULL;
273 status = pNtOpenFile( &dir, GENERIC_READ, &attr, &io,
274 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
275 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
276 pRtlFreeUnicodeString( &nameW );
277
278 path[3] = 0; /* root of the drive */
279 pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
280 status = pNtOpenFile( &root, GENERIC_READ, &attr, &io,
281 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
282 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
283 pRtlFreeUnicodeString( &nameW );
284
285 /* test opening system dir with RootDirectory set to windows dir */
286 GetSystemDirectoryW( path, MAX_PATH );
287 while (path[len] == '\\') len++;
288 nameW.Buffer = path + len;
289 nameW.Length = lstrlenW(path + len) * sizeof(WCHAR);
290 attr.RootDirectory = dir;
291 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
292 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
293 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
294 CloseHandle( handle );
295
296 /* try uppercase name */
297 for (i = len; path[i]; i++) if (path[i] >= 'a' && path[i] <= 'z') path[i] -= 'a' - 'A';
298 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
299 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
300 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
301 CloseHandle( handle );
302
303 /* try with leading backslash */
304 nameW.Buffer--;
305 nameW.Length += sizeof(WCHAR);
306 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
307 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
308 ok( status == STATUS_INVALID_PARAMETER ||
309 status == STATUS_OBJECT_NAME_INVALID ||
310 status == STATUS_OBJECT_PATH_SYNTAX_BAD,
311 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
312 if (!status) CloseHandle( handle );
313
314 /* try with empty name */
315 nameW.Length = 0;
316 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
317 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
318 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
319 CloseHandle( handle );
320
321 /* try open by file id */
322
323 while (!pNtQueryDirectoryFile( dir, NULL, NULL, NULL, &io, data, sizeof(data),
324 FileIdBothDirectoryInformation, FALSE, NULL, restart ))
325 {
326 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = (FILE_ID_BOTH_DIRECTORY_INFORMATION *)data;
327
328 restart = FALSE;
329 for (;;)
330 {
331 if (!info->FileId.QuadPart) goto next;
332 nameW.Buffer = (WCHAR *)&info->FileId;
333 nameW.Length = sizeof(info->FileId);
334 info->FileName[info->FileNameLength/sizeof(WCHAR)] = 0;
335 attr.RootDirectory = dir;
336 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
337 FILE_SHARE_READ|FILE_SHARE_WRITE,
338 FILE_OPEN_BY_FILE_ID |
339 ((info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FILE_DIRECTORY_FILE : 0) );
340 ok( status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED || status == STATUS_NOT_IMPLEMENTED,
341 "open %s failed %x\n", wine_dbgstr_w(info->FileName), status );
342 if (status == STATUS_NOT_IMPLEMENTED)
343 {
344 win_skip( "FILE_OPEN_BY_FILE_ID not supported\n" );
345 break;
346 }
347 if (!status)
348 {
349 FILE_ALL_INFORMATION all_info;
350
351 if (!pNtQueryInformationFile( handle, &io, &all_info, sizeof(all_info), FileAllInformation ))
352 {
353 /* check that it's the same file */
354 ok( info->EndOfFile.QuadPart == all_info.StandardInformation.EndOfFile.QuadPart,
355 "mismatched file size for %s\n", wine_dbgstr_w(info->FileName));
356 ok( info->LastWriteTime.QuadPart == all_info.BasicInformation.LastWriteTime.QuadPart,
357 "mismatched write time for %s\n", wine_dbgstr_w(info->FileName));
358 }
359 CloseHandle( handle );
360
361 /* try same thing from drive root */
362 attr.RootDirectory = root;
363 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
364 FILE_SHARE_READ|FILE_SHARE_WRITE,
365 FILE_OPEN_BY_FILE_ID |
366 ((info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FILE_DIRECTORY_FILE : 0) );
367 ok( status == STATUS_SUCCESS || status == STATUS_NOT_IMPLEMENTED,
368 "open %s failed %x\n", wine_dbgstr_w(info->FileName), status );
369 if (!status) CloseHandle( handle );
370 }
371 next:
372 if (!info->NextEntryOffset) break;
373 info = (FILE_ID_BOTH_DIRECTORY_INFORMATION *)((char *)info + info->NextEntryOffset);
374 }
375 }
376
377 CloseHandle( dir );
378 CloseHandle( root );
379 }
380
381 static void delete_file_test(void)
382 {
383 NTSTATUS ret;
384 OBJECT_ATTRIBUTES attr;
385 UNICODE_STRING nameW;
386 WCHAR pathW[MAX_PATH];
387 WCHAR pathsubW[MAX_PATH];
388 static const WCHAR testdirW[] = {'n','t','d','e','l','e','t','e','f','i','l','e',0};
389 static const WCHAR subdirW[] = {'\\','s','u','b',0};
390
391 ret = GetTempPathW(MAX_PATH, pathW);
392 if (!ret)
393 {
394 ok(0, "couldn't get temp dir\n");
395 return;
396 }
397 if (ret + sizeof(testdirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
398 {
399 ok(0, "MAX_PATH exceeded in constructing paths\n");
400 return;
401 }
402
403 lstrcatW(pathW, testdirW);
404 lstrcpyW(pathsubW, pathW);
405 lstrcatW(pathsubW, subdirW);
406
407 ret = CreateDirectoryW(pathW, NULL);
408 ok(ret == TRUE, "couldn't create directory ntdeletefile\n");
409 if (!pRtlDosPathNameToNtPathName_U(pathW, &nameW, NULL, NULL))
410 {
411 ok(0,"RtlDosPathNametoNtPathName_U failed\n");
412 return;
413 }
414
415 attr.Length = sizeof(attr);
416 attr.RootDirectory = 0;
417 attr.Attributes = OBJ_CASE_INSENSITIVE;
418 attr.ObjectName = &nameW;
419 attr.SecurityDescriptor = NULL;
420 attr.SecurityQualityOfService = NULL;
421
422 /* test NtDeleteFile on an empty directory */
423 ret = pNtDeleteFile(&attr);
424 ok(ret == STATUS_SUCCESS, "NtDeleteFile should succeed in removing an empty directory\n");
425 ret = RemoveDirectoryW(pathW);
426 ok(ret == FALSE, "expected to fail removing directory, NtDeleteFile should have removed it\n");
427
428 /* test NtDeleteFile on a non-empty directory */
429 ret = CreateDirectoryW(pathW, NULL);
430 ok(ret == TRUE, "couldn't create directory ntdeletefile ?!\n");
431 ret = CreateDirectoryW(pathsubW, NULL);
432 ok(ret == TRUE, "couldn't create directory subdir\n");
433 ret = pNtDeleteFile(&attr);
434 ok(ret == STATUS_SUCCESS, "expected NtDeleteFile to ret STATUS_SUCCESS\n");
435 ret = RemoveDirectoryW(pathsubW);
436 ok(ret == TRUE, "expected to remove directory ntdeletefile\\sub\n");
437 ret = RemoveDirectoryW(pathW);
438 ok(ret == TRUE, "expected to remove directory ntdeletefile, NtDeleteFile failed.\n");
439
440 pRtlFreeUnicodeString( &nameW );
441 }
442
443 static void read_file_test(void)
444 {
445 const char text[] = "foobar";
446 HANDLE handle, read, write;
447 NTSTATUS status;
448 IO_STATUS_BLOCK iosb, iosb2;
449 DWORD written;
450 int apc_count = 0;
451 char buffer[128];
452 LARGE_INTEGER offset;
453 HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
454
455 buffer[0] = 1;
456
457 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
458
459 /* try read with no data */
460 U(iosb).Status = 0xdeadbabe;
461 iosb.Information = 0xdeadbeef;
462 ok( is_signaled( read ), "read handle is not signaled\n" );
463 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
464 ok( status == STATUS_PENDING, "wrong status %x\n", status );
465 ok( !is_signaled( read ), "read handle is signaled\n" );
466 ok( !is_signaled( event ), "event is signaled\n" );
467 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
468 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
469 ok( !apc_count, "apc was called\n" );
470 WriteFile( write, buffer, 1, &written, NULL );
471 /* iosb updated here by async i/o */
472 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
473 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
474 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
475 ok( !is_signaled( read ), "read handle is signaled\n" );
476 ok( is_signaled( event ), "event is not signaled\n" );
477 ok( !apc_count, "apc was called\n" );
478 apc_count = 0;
479 SleepEx( 1, FALSE ); /* non-alertable sleep */
480 ok( !apc_count, "apc was called\n" );
481 SleepEx( 1, TRUE ); /* alertable sleep */
482 ok( apc_count == 1, "apc not called\n" );
483
484 /* with no event, the pipe handle itself gets signaled */
485 apc_count = 0;
486 U(iosb).Status = 0xdeadbabe;
487 iosb.Information = 0xdeadbeef;
488 ok( !is_signaled( read ), "read handle is not signaled\n" );
489 status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
490 ok( status == STATUS_PENDING, "wrong status %x\n", status );
491 ok( !is_signaled( read ), "read handle is signaled\n" );
492 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
493 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
494 ok( !apc_count, "apc was called\n" );
495 WriteFile( write, buffer, 1, &written, NULL );
496 /* iosb updated here by async i/o */
497 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
498 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
499 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
500 ok( is_signaled( read ), "read handle is signaled\n" );
501 ok( !apc_count, "apc was called\n" );
502 apc_count = 0;
503 SleepEx( 1, FALSE ); /* non-alertable sleep */
504 ok( !apc_count, "apc was called\n" );
505 SleepEx( 1, TRUE ); /* alertable sleep */
506 ok( apc_count == 1, "apc not called\n" );
507
508 /* now read with data ready */
509 apc_count = 0;
510 U(iosb).Status = 0xdeadbabe;
511 iosb.Information = 0xdeadbeef;
512 ResetEvent( event );
513 WriteFile( write, buffer, 1, &written, NULL );
514 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
515 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
516 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
517 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
518 ok( is_signaled( event ), "event is not signaled\n" );
519 ok( !apc_count, "apc was called\n" );
520 SleepEx( 1, FALSE ); /* non-alertable sleep */
521 ok( !apc_count, "apc was called\n" );
522 SleepEx( 1, TRUE ); /* alertable sleep */
523 ok( apc_count == 1, "apc not called\n" );
524
525 /* try read with no data */
526 apc_count = 0;
527 U(iosb).Status = 0xdeadbabe;
528 iosb.Information = 0xdeadbeef;
529 ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
530 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
531 ok( status == STATUS_PENDING, "wrong status %x\n", status );
532 ok( !is_signaled( event ), "event is signaled\n" );
533 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
534 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
535 ok( !apc_count, "apc was called\n" );
536 WriteFile( write, buffer, 1, &written, NULL );
537 /* partial read is good enough */
538 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
539 ok( is_signaled( event ), "event is signaled\n" );
540 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
541 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
542 ok( !apc_count, "apc was called\n" );
543 SleepEx( 1, TRUE ); /* alertable sleep */
544 ok( apc_count == 1, "apc was not called\n" );
545
546 /* read from disconnected pipe */
547 apc_count = 0;
548 U(iosb).Status = 0xdeadbabe;
549 iosb.Information = 0xdeadbeef;
550 CloseHandle( write );
551 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
552 ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
553 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
554 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
555 ok( !is_signaled( event ), "event is signaled\n" );
556 ok( !apc_count, "apc was called\n" );
557 SleepEx( 1, TRUE ); /* alertable sleep */
558 ok( !apc_count, "apc was called\n" );
559 CloseHandle( read );
560
561 /* read from closed handle */
562 apc_count = 0;
563 U(iosb).Status = 0xdeadbabe;
564 iosb.Information = 0xdeadbeef;
565 SetEvent( event );
566 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
567 ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
568 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
569 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
570 ok( is_signaled( event ), "event is signaled\n" ); /* not reset on invalid handle */
571 ok( !apc_count, "apc was called\n" );
572 SleepEx( 1, TRUE ); /* alertable sleep */
573 ok( !apc_count, "apc was called\n" );
574
575 /* disconnect while async read is in progress */
576 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
577 apc_count = 0;
578 U(iosb).Status = 0xdeadbabe;
579 iosb.Information = 0xdeadbeef;
580 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
581 ok( status == STATUS_PENDING, "wrong status %x\n", status );
582 ok( !is_signaled( event ), "event is signaled\n" );
583 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
584 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
585 ok( !apc_count, "apc was called\n" );
586 CloseHandle( write );
587 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
588 ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
589 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
590 ok( is_signaled( event ), "event is signaled\n" );
591 ok( !apc_count, "apc was called\n" );
592 SleepEx( 1, TRUE ); /* alertable sleep */
593 ok( apc_count == 1, "apc was not called\n" );
594 CloseHandle( read );
595
596 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
597 ok(DuplicateHandle(GetCurrentProcess(), read, GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS),
598 "Failed to duplicate handle: %d\n", GetLastError());
599
600 apc_count = 0;
601 U(iosb).Status = 0xdeadbabe;
602 iosb.Information = 0xdeadbeef;
603 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
604 ok( status == STATUS_PENDING, "wrong status %x\n", status );
605 ok( !is_signaled( event ), "event is signaled\n" );
606 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
607 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
608 ok( !apc_count, "apc was called\n" );
609 /* Cancel by other handle */
610 status = pNtCancelIoFile( read, &iosb2 );
611 ok(status == STATUS_SUCCESS, "failed to cancel by different handle: %x\n", status);
612 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
613 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
614 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
615 ok( is_signaled( event ), "event is signaled\n" );
616 todo_wine ok( !apc_count, "apc was called\n" );
617 SleepEx( 1, TRUE ); /* alertable sleep */
618 ok( apc_count == 1, "apc was not called\n" );
619
620 apc_count = 0;
621 U(iosb).Status = 0xdeadbabe;
622 iosb.Information = 0xdeadbeef;
623 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
624 ok( status == STATUS_PENDING, "wrong status %x\n", status );
625 ok( !is_signaled( event ), "event is signaled\n" );
626 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
627 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
628 ok( !apc_count, "apc was called\n" );
629 /* Close queued handle */
630 CloseHandle( read );
631 SleepEx( 1, TRUE ); /* alertable sleep */
632 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
633 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
634 status = pNtCancelIoFile( read, &iosb2 );
635 ok(status == STATUS_INVALID_HANDLE, "cancelled by closed handle?\n");
636 status = pNtCancelIoFile( handle, &iosb2 );
637 ok(status == STATUS_SUCCESS, "failed to cancel: %x\n", status);
638 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
639 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
640 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
641 ok( is_signaled( event ), "event is signaled\n" );
642 todo_wine ok( !apc_count, "apc was called\n" );
643 SleepEx( 1, TRUE ); /* alertable sleep */
644 ok( apc_count == 1, "apc was not called\n" );
645 CloseHandle( handle );
646 CloseHandle( write );
647
648 if (pNtCancelIoFileEx)
649 {
650 /* Basic Cancel Ex */
651 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
652
653 apc_count = 0;
654 U(iosb).Status = 0xdeadbabe;
655 iosb.Information = 0xdeadbeef;
656 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
657 ok( status == STATUS_PENDING, "wrong status %x\n", status );
658 ok( !is_signaled( event ), "event is signaled\n" );
659 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
660 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
661 ok( !apc_count, "apc was called\n" );
662 status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
663 ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
664 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
665 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
666 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
667 ok( is_signaled( event ), "event is signaled\n" );
668 todo_wine ok( !apc_count, "apc was called\n" );
669 SleepEx( 1, TRUE ); /* alertable sleep */
670 ok( apc_count == 1, "apc was not called\n" );
671
672 /* Duplicate iosb */
673 apc_count = 0;
674 U(iosb).Status = 0xdeadbabe;
675 iosb.Information = 0xdeadbeef;
676 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
677 ok( status == STATUS_PENDING, "wrong status %x\n", status );
678 ok( !is_signaled( event ), "event is signaled\n" );
679 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
680 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
681 ok( !apc_count, "apc was called\n" );
682 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
683 ok( status == STATUS_PENDING, "wrong status %x\n", status );
684 ok( !is_signaled( event ), "event is signaled\n" );
685 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
686 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
687 ok( !apc_count, "apc was called\n" );
688 status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
689 ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
690 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
691 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
692 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
693 ok( is_signaled( event ), "event is signaled\n" );
694 todo_wine ok( !apc_count, "apc was called\n" );
695 SleepEx( 1, TRUE ); /* alertable sleep */
696 ok( apc_count == 2, "apc was not called\n" );
697
698 CloseHandle( read );
699 CloseHandle( write );
700 }
701
702 /* now try a real file */
703 if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
704 apc_count = 0;
705 U(iosb).Status = 0xdeadbabe;
706 iosb.Information = 0xdeadbeef;
707 offset.QuadPart = 0;
708 ResetEvent( event );
709 status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
710 ok( status == STATUS_SUCCESS || status == STATUS_PENDING, "wrong status %x\n", status );
711 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
712 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
713 ok( is_signaled( event ), "event is signaled\n" );
714 ok( !apc_count, "apc was called\n" );
715 SleepEx( 1, TRUE ); /* alertable sleep */
716 ok( apc_count == 1, "apc was not called\n" );
717
718 apc_count = 0;
719 U(iosb).Status = 0xdeadbabe;
720 iosb.Information = 0xdeadbeef;
721 offset.QuadPart = 0;
722 ResetEvent( event );
723 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
724 ok( status == STATUS_SUCCESS ||
725 status == STATUS_PENDING, /* vista */
726 "wrong status %x\n", status );
727 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
728 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
729 ok( is_signaled( event ), "event is signaled\n" );
730 ok( !apc_count, "apc was called\n" );
731 SleepEx( 1, TRUE ); /* alertable sleep */
732 ok( apc_count == 1, "apc was not called\n" );
733
734 /* read beyond eof */
735 apc_count = 0;
736 U(iosb).Status = 0xdeadbabe;
737 iosb.Information = 0xdeadbeef;
738 offset.QuadPart = strlen(text) + 2;
739 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
740 if (status == STATUS_PENDING) /* vista */
741 {
742 ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
743 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
744 ok( is_signaled( event ), "event is signaled\n" );
745 ok( !apc_count, "apc was called\n" );
746 SleepEx( 1, TRUE ); /* alertable sleep */
747 ok( apc_count == 1, "apc was not called\n" );
748 }
749 else
750 {
751 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
752 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
753 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
754 ok( !is_signaled( event ), "event is signaled\n" );
755 ok( !apc_count, "apc was called\n" );
756 SleepEx( 1, TRUE ); /* alertable sleep */
757 ok( !apc_count, "apc was called\n" );
758 }
759 CloseHandle( handle );
760
761 /* now a non-overlapped file */
762 if (!(handle = create_temp_file(0))) return;
763 apc_count = 0;
764 U(iosb).Status = 0xdeadbabe;
765 iosb.Information = 0xdeadbeef;
766 offset.QuadPart = 0;
767 status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
768 ok( status == STATUS_END_OF_FILE ||
769 status == STATUS_SUCCESS ||
770 status == STATUS_PENDING, /* vista */
771 "wrong status %x\n", status );
772 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
773 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
774 ok( is_signaled( event ), "event is signaled\n" );
775 ok( !apc_count, "apc was called\n" );
776 SleepEx( 1, TRUE ); /* alertable sleep */
777 ok( apc_count == 1, "apc was not called\n" );
778
779 apc_count = 0;
780 U(iosb).Status = 0xdeadbabe;
781 iosb.Information = 0xdeadbeef;
782 offset.QuadPart = 0;
783 ResetEvent( event );
784 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
785 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
786 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
787 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
788 ok( is_signaled( event ), "event is signaled\n" );
789 ok( !apc_count, "apc was called\n" );
790 SleepEx( 1, TRUE ); /* alertable sleep */
791 todo_wine ok( !apc_count, "apc was called\n" );
792
793 /* read beyond eof */
794 apc_count = 0;
795 U(iosb).Status = 0xdeadbabe;
796 iosb.Information = 0xdeadbeef;
797 offset.QuadPart = strlen(text) + 2;
798 ResetEvent( event );
799 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
800 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
801 todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
802 todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
803 todo_wine ok( is_signaled( event ), "event is not signaled\n" );
804 ok( !apc_count, "apc was called\n" );
805 SleepEx( 1, TRUE ); /* alertable sleep */
806 ok( !apc_count, "apc was called\n" );
807
808 CloseHandle( handle );
809
810 CloseHandle( event );
811 }
812
813 static void nt_mailslot_test(void)
814 {
815 HANDLE hslot;
816 ACCESS_MASK DesiredAccess;
817 OBJECT_ATTRIBUTES attr;
818
819 ULONG CreateOptions;
820 ULONG MailslotQuota;
821 ULONG MaxMessageSize;
822 LARGE_INTEGER TimeOut;
823 IO_STATUS_BLOCK IoStatusBlock;
824 NTSTATUS rc;
825 UNICODE_STRING str;
826 WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
827 'R',':','\\','F','R','E','D','\0' };
828
829 TimeOut.QuadPart = -1;
830
831 pRtlInitUnicodeString(&str, buffer1);
832 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
833 CreateOptions = MailslotQuota = MaxMessageSize = 0;
834 DesiredAccess = GENERIC_READ;
835
836 /*
837 * Check for NULL pointer handling
838 */
839 rc = pNtCreateMailslotFile(NULL, DesiredAccess,
840 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
841 &TimeOut);
842 ok( rc == STATUS_ACCESS_VIOLATION ||
843 rc == STATUS_INVALID_PARAMETER, /* win2k3 */
844 "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
845
846 /*
847 * Test to see if the Timeout can be NULL
848 */
849 hslot = (HANDLE)0xdeadbeef;
850 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
851 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
852 NULL);
853 ok( rc == STATUS_SUCCESS ||
854 rc == STATUS_INVALID_PARAMETER, /* win2k3 */
855 "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
856 ok( hslot != 0, "Handle is invalid\n");
857
858 if ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
859
860 /*
861 * Test that the length field is checked properly
862 */
863 attr.Length = 0;
864 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
865 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
866 &TimeOut);
867 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
868
869 if (rc == STATUS_SUCCESS) pNtClose(hslot);
870
871 attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
872 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
873 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
874 &TimeOut);
875 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
876
877 if (rc == STATUS_SUCCESS) pNtClose(hslot);
878
879 /*
880 * Test handling of a NULL unicode string in ObjectName
881 */
882 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
883 attr.ObjectName = NULL;
884 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
885 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
886 &TimeOut);
887 ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
888 rc == STATUS_INVALID_PARAMETER,
889 "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
890
891 if (rc == STATUS_SUCCESS) pNtClose(hslot);
892
893 /*
894 * Test a valid call
895 */
896 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
897 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
898 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
899 &TimeOut);
900 ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
901 ok( hslot != 0, "Handle is invalid\n");
902
903 rc = pNtClose(hslot);
904 ok( rc == STATUS_SUCCESS, "NtClose failed\n");
905 }
906
907 static void test_iocp_setcompletion(HANDLE h)
908 {
909 NTSTATUS res;
910 long count;
911
912 res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
913 ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
914
915 count = get_pending_msgs(h);
916 ok( count == 1, "Unexpected msg count: %ld\n", count );
917
918 if (get_msg(h))
919 {
920 ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
921 ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
922 ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
923 ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
924 }
925
926 count = get_pending_msgs(h);
927 ok( !count, "Unexpected msg count: %ld\n", count );
928 }
929
930 static void test_iocp_fileio(HANDLE h)
931 {
932 static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
933
934 IO_STATUS_BLOCK iosb;
935 FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
936 HANDLE hPipeSrv, hPipeClt;
937 NTSTATUS res;
938
939 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
940 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
941 if (hPipeSrv != INVALID_HANDLE_VALUE )
942 {
943 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
944 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
945 if (hPipeClt != INVALID_HANDLE_VALUE)
946 {
947 res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
948 ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
949 CloseHandle(hPipeClt);
950 }
951 CloseHandle( hPipeSrv );
952 }
953
954 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
955 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
956 if (hPipeSrv == INVALID_HANDLE_VALUE )
957 return;
958
959 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
960 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
961 if (hPipeClt != INVALID_HANDLE_VALUE)
962 {
963 OVERLAPPED o = {0,};
964 BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
965 DWORD read;
966 long count;
967
968 NTSTATUS res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
969 ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
970 ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
971
972 memset( send_buf, 0, TEST_BUF_LEN );
973 memset( recv_buf, 0xde, TEST_BUF_LEN );
974 count = get_pending_msgs(h);
975 ok( !count, "Unexpected msg count: %ld\n", count );
976 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
977 count = get_pending_msgs(h);
978 ok( !count, "Unexpected msg count: %ld\n", count );
979 WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
980
981 if (get_msg(h))
982 {
983 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
984 ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
985 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
986 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
987 ok( !memcmp( send_buf, recv_buf, TEST_BUF_LEN ), "Receive buffer (%x %x %x) did not match send buffer (%x %x %x)\n", recv_buf[0], recv_buf[1], recv_buf[2], send_buf[0], send_buf[1], send_buf[2] );
988 }
989 count = get_pending_msgs(h);
990 ok( !count, "Unexpected msg count: %ld\n", count );
991
992 memset( send_buf, 0, TEST_BUF_LEN );
993 memset( recv_buf, 0xde, TEST_BUF_LEN );
994 WriteFile( hPipeClt, send_buf, 2, &read, NULL );
995 count = get_pending_msgs(h);
996 ok( !count, "Unexpected msg count: %ld\n", count );
997 ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
998 count = get_pending_msgs(h);
999 ok( count == 1, "Unexpected msg count: %ld\n", count );
1000 if (get_msg(h))
1001 {
1002 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1003 ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1004 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1005 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1006 ok( !memcmp( send_buf, recv_buf, 2 ), "Receive buffer (%x %x) did not match send buffer (%x %x)\n", recv_buf[0], recv_buf[1], send_buf[0], send_buf[1] );
1007 }
1008
1009 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
1010 CloseHandle( hPipeSrv );
1011 count = get_pending_msgs(h);
1012 ok( count == 1, "Unexpected msg count: %ld\n", count );
1013 if (get_msg(h))
1014 {
1015 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1016 ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1017 /* wine sends wrong status here */
1018 todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1019 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1020 }
1021 }
1022
1023 CloseHandle( hPipeClt );
1024 }
1025
1026 static void test_file_basic_information(void)
1027 {
1028 IO_STATUS_BLOCK io;
1029 FILE_BASIC_INFORMATION fbi;
1030 HANDLE h;
1031 int res;
1032 int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
1033
1034 if (!(h = create_temp_file(0))) return;
1035
1036 /* Check default first */
1037 memset(&fbi, 0, sizeof(fbi));
1038 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1039 ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1040 ok ( (fbi.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
1041 "attribute %x not expected\n", fbi.FileAttributes );
1042
1043 /* Then SYSTEM */
1044 /* Clear fbi to avoid setting times */
1045 memset(&fbi, 0, sizeof(fbi));
1046 fbi.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
1047 res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1048 ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
1049
1050 memset(&fbi, 0, sizeof(fbi));
1051 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1052 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1053 todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_SYSTEM, "attribute %x not FILE_ATTRIBUTE_SYSTEM\n", fbi.FileAttributes );
1054
1055 /* Then HIDDEN */
1056 memset(&fbi, 0, sizeof(fbi));
1057 fbi.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1058 res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1059 ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
1060
1061 memset(&fbi, 0, sizeof(fbi));
1062 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1063 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1064 todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_HIDDEN, "attribute %x not FILE_ATTRIBUTE_HIDDEN\n", fbi.FileAttributes );
1065
1066 /* Check NORMAL last of all (to make sure we can clear attributes) */
1067 memset(&fbi, 0, sizeof(fbi));
1068 fbi.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1069 res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1070 ok ( res == STATUS_SUCCESS, "can't set normal attribute\n");
1071
1072 memset(&fbi, 0, sizeof(fbi));
1073 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1074 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1075 todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_NORMAL, "attribute %x not 0\n", fbi.FileAttributes );
1076
1077 CloseHandle( h );
1078 }
1079
1080 static void test_file_all_information(void)
1081 {
1082 IO_STATUS_BLOCK io;
1083 /* FileAllInformation, like FileNameInformation, has a variable-length pathname
1084 * buffer at the end. Vista objects with STATUS_BUFFER_OVERFLOW if you
1085 * don't leave enough room there.
1086 */
1087 struct {
1088 FILE_ALL_INFORMATION fai;
1089 WCHAR buf[256];
1090 } fai_buf;
1091 HANDLE h;
1092 int res;
1093 int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
1094
1095 if (!(h = create_temp_file(0))) return;
1096
1097 /* Check default first */
1098 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1099 ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1100 ok ( (fai_buf.fai.BasicInformation.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
1101 "attribute %x not expected\n", fai_buf.fai.BasicInformation.FileAttributes );
1102
1103 /* Then SYSTEM */
1104 /* Clear fbi to avoid setting times */
1105 memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1106 fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
1107 res = pNtSetInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1108 ok ( res == STATUS_INVALID_INFO_CLASS || res == STATUS_NOT_IMPLEMENTED, "shouldn't be able to set FileAllInformation, res %x\n", res);
1109 res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1110 ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
1111
1112 memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1113 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1114 ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1115 todo_wine ok ( (fai_buf.fai.BasicInformation.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_SYSTEM, "attribute %x not FILE_ATTRIBUTE_SYSTEM\n", fai_buf.fai.BasicInformation.FileAttributes );
1116
1117 /* Then HIDDEN */
1118 memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1119 fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1120 res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1121 ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
1122
1123 memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1124 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1125 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1126 todo_wine ok ( (fai_buf.fai.BasicInformation.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_HIDDEN, "attribute %x not FILE_ATTRIBUTE_HIDDEN\n", fai_buf.fai.BasicInformation.FileAttributes );
1127
1128 /* Check NORMAL last of all (to make sure we can clear attributes) */
1129 memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1130 fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1131 res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1132 ok ( res == STATUS_SUCCESS, "can't set normal attribute\n");
1133
1134 memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1135 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1136 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1137 todo_wine ok ( (fai_buf.fai.BasicInformation.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_NORMAL, "attribute %x not FILE_ATTRIBUTE_NORMAL\n", fai_buf.fai.BasicInformation.FileAttributes );
1138
1139 CloseHandle( h );
1140 }
1141
1142 static void test_file_both_information(void)
1143 {
1144 IO_STATUS_BLOCK io;
1145 FILE_BOTH_DIR_INFORMATION fbi;
1146 HANDLE h;
1147 int res;
1148
1149 if (!(h = create_temp_file(0))) return;
1150
1151 memset(&fbi, 0, sizeof(fbi));
1152 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBothDirectoryInformation);
1153 ok ( res == STATUS_INVALID_INFO_CLASS || res == STATUS_NOT_IMPLEMENTED, "shouldn't be able to query FileBothDirectoryInformation, res %x\n", res);
1154
1155 CloseHandle( h );
1156 }
1157
1158 static void test_iocompletion(void)
1159 {
1160 HANDLE h = INVALID_HANDLE_VALUE;
1161 NTSTATUS res;
1162
1163 res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
1164
1165 ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
1166 ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
1167
1168 if ( h && h != INVALID_HANDLE_VALUE)
1169 {
1170 test_iocp_setcompletion(h);
1171 test_iocp_fileio(h);
1172 pNtClose(h);
1173 }
1174 }
1175
1176 static void test_file_name_information(void)
1177 {
1178 WCHAR *file_name, *volume_prefix, *expected;
1179 FILE_NAME_INFORMATION *info;
1180 ULONG old_redir = 1, tmp;
1181 UINT file_name_size;
1182 IO_STATUS_BLOCK io;
1183 UINT info_size;
1184 HRESULT hr;
1185 HANDLE h;
1186 UINT len;
1187
1188 /* GetVolumePathName is not present before w2k */
1189 if (!pGetVolumePathNameW) {
1190 win_skip("GetVolumePathNameW not found\n");
1191 return;
1192 }
1193
1194 file_name_size = GetSystemDirectoryW( NULL, 0 );
1195 file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1196 volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1197 expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1198
1199 len = GetSystemDirectoryW( file_name, file_name_size );
1200 ok(len == file_name_size - 1,
1201 "GetSystemDirectoryW returned %u, expected %u.\n",
1202 len, file_name_size - 1);
1203
1204 len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1205 ok(len, "GetVolumePathNameW failed.\n");
1206
1207 len = lstrlenW( volume_prefix );
1208 if (len && volume_prefix[len - 1] == '\\') --len;
1209 memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1210 expected[file_name_size - len - 1] = '\0';
1211
1212 /* A bit more than we actually need, but it keeps the calculation simple. */
1213 info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1214 info = HeapAlloc( GetProcessHeap(), 0, info_size );
1215
1216 if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( TRUE, &old_redir );
1217 h = CreateFileW( file_name, GENERIC_READ,
1218 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1219 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1220 if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( old_redir, &tmp );
1221 ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1222
1223 hr = pNtQueryInformationFile( h, &io, info, sizeof(*info) - 1, FileNameInformation );
1224 ok(hr == STATUS_INFO_LENGTH_MISMATCH, "NtQueryInformationFile returned %#x.\n", hr);
1225
1226 memset( info, 0xcc, info_size );
1227 hr = pNtQueryInformationFile( h, &io, info, sizeof(*info), FileNameInformation );
1228 ok(hr == STATUS_BUFFER_OVERFLOW, "NtQueryInformationFile returned %#x, expected %#x.\n",
1229 hr, STATUS_BUFFER_OVERFLOW);
1230 ok(U(io).Status == STATUS_BUFFER_OVERFLOW, "io.Status is %#x, expected %#x.\n",
1231 U(io).Status, STATUS_BUFFER_OVERFLOW);
1232 ok(info->FileNameLength == lstrlenW( expected ) * sizeof(WCHAR), "info->FileNameLength is %u\n", info->FileNameLength);
1233 ok(info->FileName[2] == 0xcccc, "info->FileName[2] is %#x, expected 0xcccc.\n", info->FileName[2]);
1234 ok(CharLowerW((LPWSTR)(UINT_PTR)info->FileName[1]) == CharLowerW((LPWSTR)(UINT_PTR)expected[1]),
1235 "info->FileName[1] is %p, expected %p.\n",
1236 CharLowerW((LPWSTR)(UINT_PTR)info->FileName[1]), CharLowerW((LPWSTR)(UINT_PTR)expected[1]));
1237 ok(io.Information == sizeof(*info), "io.Information is %lu\n", io.Information);
1238
1239 memset( info, 0xcc, info_size );
1240 hr = pNtQueryInformationFile( h, &io, info, info_size, FileNameInformation );
1241 ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1242 ok(U(io).Status == STATUS_SUCCESS, "io.Status is %#x, expected %#x.\n", U(io).Status, STATUS_SUCCESS);
1243 ok(info->FileNameLength == lstrlenW( expected ) * sizeof(WCHAR), "info->FileNameLength is %u\n", info->FileNameLength);
1244 ok(info->FileName[info->FileNameLength / sizeof(WCHAR)] == 0xcccc, "info->FileName[len] is %#x, expected 0xcccc.\n",
1245 info->FileName[info->FileNameLength / sizeof(WCHAR)]);
1246 info->FileName[info->FileNameLength / sizeof(WCHAR)] = '\0';
1247 ok(!lstrcmpiW( info->FileName, expected ), "info->FileName is %s, expected %s.\n",
1248 wine_dbgstr_w( info->FileName ), wine_dbgstr_w( expected ));
1249 ok(io.Information == FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + info->FileNameLength,
1250 "io.Information is %lu, expected %u.\n",
1251 io.Information, FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + info->FileNameLength);
1252
1253 CloseHandle( h );
1254 HeapFree( GetProcessHeap(), 0, info );
1255 HeapFree( GetProcessHeap(), 0, expected );
1256 HeapFree( GetProcessHeap(), 0, volume_prefix );
1257
1258 if (old_redir || !pGetSystemWow64DirectoryW || !(file_name_size = pGetSystemWow64DirectoryW( NULL, 0 )))
1259 {
1260 skip("Not running on WoW64, skipping test.\n");
1261 HeapFree( GetProcessHeap(), 0, file_name );
1262 return;
1263 }
1264
1265 h = CreateFileW( file_name, GENERIC_READ,
1266 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1267 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1268 ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1269 HeapFree( GetProcessHeap(), 0, file_name );
1270
1271 file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1272 volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1273 expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*expected) );
1274
1275 len = pGetSystemWow64DirectoryW( file_name, file_name_size );
1276 ok(len == file_name_size - 1,
1277 "GetSystemWow64DirectoryW returned %u, expected %u.\n",
1278 len, file_name_size - 1);
1279
1280 len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1281 ok(len, "GetVolumePathNameW failed.\n");
1282
1283 len = lstrlenW( volume_prefix );
1284 if (len && volume_prefix[len - 1] == '\\') --len;
1285 memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1286 expected[file_name_size - len - 1] = '\0';
1287
1288 info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1289 info = HeapAlloc( GetProcessHeap(), 0, info_size );
1290
1291 memset( info, 0xcc, info_size );
1292 hr = pNtQueryInformationFile( h, &io, info, info_size, FileNameInformation );
1293 ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1294 info->FileName[info->FileNameLength / sizeof(WCHAR)] = '\0';
1295 ok(!lstrcmpiW( info->FileName, expected ), "info->FileName is %s, expected %s.\n",
1296 wine_dbgstr_w( info->FileName ), wine_dbgstr_w( expected ));
1297
1298 CloseHandle( h );
1299 HeapFree( GetProcessHeap(), 0, info );
1300 HeapFree( GetProcessHeap(), 0, expected );
1301 HeapFree( GetProcessHeap(), 0, volume_prefix );
1302 HeapFree( GetProcessHeap(), 0, file_name );
1303 }
1304
1305 static void test_file_all_name_information(void)
1306 {
1307 WCHAR *file_name, *volume_prefix, *expected;
1308 FILE_ALL_INFORMATION *info;
1309 ULONG old_redir = 1, tmp;
1310 UINT file_name_size;
1311 IO_STATUS_BLOCK io;
1312 UINT info_size;
1313 HRESULT hr;
1314 HANDLE h;
1315 UINT len;
1316
1317 /* GetVolumePathName is not present before w2k */
1318 if (!pGetVolumePathNameW) {
1319 win_skip("GetVolumePathNameW not found\n");
1320 return;
1321 }
1322
1323 file_name_size = GetSystemDirectoryW( NULL, 0 );
1324 file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1325 volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1326 expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1327
1328 len = GetSystemDirectoryW( file_name, file_name_size );
1329 ok(len == file_name_size - 1,
1330 "GetSystemDirectoryW returned %u, expected %u.\n",
1331 len, file_name_size - 1);
1332
1333 len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1334 ok(len, "GetVolumePathNameW failed.\n");
1335
1336 len = lstrlenW( volume_prefix );
1337 if (len && volume_prefix[len - 1] == '\\') --len;
1338 memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1339 expected[file_name_size - len - 1] = '\0';
1340
1341 /* A bit more than we actually need, but it keeps the calculation simple. */
1342 info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1343 info = HeapAlloc( GetProcessHeap(), 0, info_size );
1344
1345 if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( TRUE, &old_redir );
1346 h = CreateFileW( file_name, GENERIC_READ,
1347 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1348 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1349 if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( old_redir, &tmp );
1350 ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1351
1352 hr = pNtQueryInformationFile( h, &io, info, sizeof(*info) - 1, FileAllInformation );
1353 ok(hr == STATUS_INFO_LENGTH_MISMATCH, "NtQueryInformationFile returned %#x, expected %#x.\n",
1354 hr, STATUS_INFO_LENGTH_MISMATCH);
1355
1356 memset( info, 0xcc, info_size );
1357 hr = pNtQueryInformationFile( h, &io, info, sizeof(*info), FileAllInformation );
1358 ok(hr == STATUS_BUFFER_OVERFLOW, "NtQueryInformationFile returned %#x, expected %#x.\n",
1359 hr, STATUS_BUFFER_OVERFLOW);
1360 ok(U(io).Status == STATUS_BUFFER_OVERFLOW, "io.Status is %#x, expected %#x.\n",
1361 U(io).Status, STATUS_BUFFER_OVERFLOW);
1362 ok(info->NameInformation.FileNameLength == lstrlenW( expected ) * sizeof(WCHAR),
1363 "info->NameInformation.FileNameLength is %u\n", info->NameInformation.FileNameLength );
1364 ok(info->NameInformation.FileName[2] == 0xcccc,
1365 "info->NameInformation.FileName[2] is %#x, expected 0xcccc.\n", info->NameInformation.FileName[2]);
1366 ok(CharLowerW((LPWSTR)(UINT_PTR)info->NameInformation.FileName[1]) == CharLowerW((LPWSTR)(UINT_PTR)expected[1]),
1367 "info->NameInformation.FileName[1] is %p, expected %p.\n",
1368 CharLowerW((LPWSTR)(UINT_PTR)info->NameInformation.FileName[1]), CharLowerW((LPWSTR)(UINT_PTR)expected[1]));
1369 ok(io.Information == sizeof(*info), "io.Information is %lu\n", io.Information);
1370
1371 memset( info, 0xcc, info_size );
1372 hr = pNtQueryInformationFile( h, &io, info, info_size, FileAllInformation );
1373 ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1374 ok(U(io).Status == STATUS_SUCCESS, "io.Status is %#x, expected %#x.\n", U(io).Status, STATUS_SUCCESS);
1375 ok(info->NameInformation.FileNameLength == lstrlenW( expected ) * sizeof(WCHAR),
1376 "info->NameInformation.FileNameLength is %u\n", info->NameInformation.FileNameLength );
1377 ok(info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)] == 0xcccc,
1378 "info->NameInformation.FileName[len] is %#x, expected 0xcccc.\n",
1379 info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)]);
1380 info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)] = '\0';
1381 ok(!lstrcmpiW( info->NameInformation.FileName, expected ),
1382 "info->NameInformation.FileName is %s, expected %s.\n",
1383 wine_dbgstr_w( info->NameInformation.FileName ), wine_dbgstr_w( expected ));
1384 ok(io.Information == FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName)
1385 + info->NameInformation.FileNameLength,
1386 "io.Information is %lu\n", io.Information );
1387
1388 CloseHandle( h );
1389 HeapFree( GetProcessHeap(), 0, info );
1390 HeapFree( GetProcessHeap(), 0, expected );
1391 HeapFree( GetProcessHeap(), 0, volume_prefix );
1392
1393 if (old_redir || !pGetSystemWow64DirectoryW || !(file_name_size = pGetSystemWow64DirectoryW( NULL, 0 )))
1394 {
1395 skip("Not running on WoW64, skipping test.\n");
1396 HeapFree( GetProcessHeap(), 0, file_name );
1397 return;
1398 }
1399
1400 h = CreateFileW( file_name, GENERIC_READ,
1401 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1402 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1403 ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1404 HeapFree( GetProcessHeap(), 0, file_name );
1405
1406 file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1407 volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1408 expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*expected) );
1409
1410 len = pGetSystemWow64DirectoryW( file_name, file_name_size );
1411 ok(len == file_name_size - 1,
1412 "GetSystemWow64DirectoryW returned %u, expected %u.\n",
1413 len, file_name_size - 1);
1414
1415 len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1416 ok(len, "GetVolumePathNameW failed.\n");
1417
1418 len = lstrlenW( volume_prefix );
1419 if (len && volume_prefix[len - 1] == '\\') --len;
1420 memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1421 expected[file_name_size - len - 1] = '\0';
1422
1423 info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1424 info = HeapAlloc( GetProcessHeap(), 0, info_size );
1425
1426 memset( info, 0xcc, info_size );
1427 hr = pNtQueryInformationFile( h, &io, info, info_size, FileAllInformation );
1428 ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1429 info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)] = '\0';
1430 ok(!lstrcmpiW( info->NameInformation.FileName, expected ), "info->NameInformation.FileName is %s, expected %s.\n",
1431 wine_dbgstr_w( info->NameInformation.FileName ), wine_dbgstr_w( expected ));
1432
1433 CloseHandle( h );
1434 HeapFree( GetProcessHeap(), 0, info );
1435 HeapFree( GetProcessHeap(), 0, expected );
1436 HeapFree( GetProcessHeap(), 0, volume_prefix );
1437 HeapFree( GetProcessHeap(), 0, file_name );
1438 }
1439
1440 START_TEST(file)
1441 {
1442 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
1443 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1444 if (!hntdll)
1445 {
1446 skip("not running on NT, skipping test\n");
1447 return;
1448 }
1449
1450 pGetVolumePathNameW = (void *)GetProcAddress(hkernel32, "GetVolumePathNameW");
1451 pGetSystemWow64DirectoryW = (void *)GetProcAddress(hkernel32, "GetSystemWow64DirectoryW");
1452
1453 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
1454 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
1455 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
1456 pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll, "RtlWow64EnableFsRedirectionEx");
1457 pNtCreateMailslotFile = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
1458 pNtCreateFile = (void *)GetProcAddress(hntdll, "NtCreateFile");
1459 pNtOpenFile = (void *)GetProcAddress(hntdll, "NtOpenFile");
1460 pNtDeleteFile = (void *)GetProcAddress(hntdll, "NtDeleteFile");
1461 pNtReadFile = (void *)GetProcAddress(hntdll, "NtReadFile");
1462 pNtWriteFile = (void *)GetProcAddress(hntdll, "NtWriteFile");
1463 pNtCancelIoFile = (void *)GetProcAddress(hntdll, "NtCancelIoFile");
1464 pNtCancelIoFileEx = (void *)GetProcAddress(hntdll, "NtCancelIoFileEx");
1465 pNtClose = (void *)GetProcAddress(hntdll, "NtClose");
1466 pNtCreateIoCompletion = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
1467 pNtOpenIoCompletion = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
1468 pNtQueryIoCompletion = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
1469 pNtRemoveIoCompletion = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
1470 pNtSetIoCompletion = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
1471 pNtSetInformationFile = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
1472 pNtQueryInformationFile = (void *)GetProcAddress(hntdll, "NtQueryInformationFile");
1473 pNtQueryDirectoryFile = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
1474
1475 create_file_test();
1476 ok(0, "broken test: open_file_test\n");
1477 //open_file_test();
1478 delete_file_test();
1479 read_file_test();
1480 nt_mailslot_test();
1481 test_iocompletion();
1482 test_file_basic_information();
1483 test_file_all_information();
1484 test_file_both_information();
1485 test_file_name_information();
1486 ok(0, "broken test: test_file_all_name_information \n");
1487 //test_file_all_name_information();
1488 }