mshtml: Added IHTMLElement2::get_readyState implementation.
[wine] / dlls / ntdll / tests / 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 NTSTATUS (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 *pNtDeleteFile)(POBJECT_ATTRIBUTES ObjectAttributes);
54 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
55                                       PIO_APC_ROUTINE apc, void* apc_user,
56                                       PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
57                                       PLARGE_INTEGER offset, PULONG key);
58 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
59                                        PIO_APC_ROUTINE apc, void* apc_user,
60                                        PIO_STATUS_BLOCK io_status,
61                                        const void* buffer, ULONG length,
62                                        PLARGE_INTEGER offset, PULONG key);
63 static NTSTATUS (WINAPI *pNtCancelIoFile)(HANDLE hFile, PIO_STATUS_BLOCK io_status);
64 static NTSTATUS (WINAPI *pNtCancelIoFileEx)(HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status);
65 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
66
67 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
68 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
69 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
70 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
71 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
72 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
73 static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
74
75 static inline BOOL is_signaled( HANDLE obj )
76 {
77     return WaitForSingleObject( obj, 0 ) == 0;
78 }
79
80 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
81 #define TEST_BUF_LEN 3
82
83 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
84 {
85     *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
86                             1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
87     ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
88
89     *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
90     ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
91
92     return TRUE;
93 }
94
95 static HANDLE create_temp_file( ULONG flags )
96 {
97     char buffer[MAX_PATH];
98     HANDLE handle;
99
100     GetTempFileNameA( ".", "foo", 0, buffer );
101     handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
102                          flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
103     ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
104     return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
105 }
106
107 #define CVALUE_FIRST 0xfffabbcc
108 #define CKEY_FIRST 0x1030341
109 #define CKEY_SECOND 0x132E46
110
111 ULONG_PTR completionKey;
112 IO_STATUS_BLOCK ioSb;
113 ULONG_PTR completionValue;
114
115 static long get_pending_msgs(HANDLE h)
116 {
117     NTSTATUS res;
118     ULONG a, req;
119
120     res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, &a, sizeof(a), &req );
121     ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
122     if (res != STATUS_SUCCESS) return -1;
123     ok( req == sizeof(a), "Unexpected response size: %x\n", req );
124     return a;
125 }
126
127 static BOOL get_msg(HANDLE h)
128 {
129     LARGE_INTEGER timeout = {{-10000000*3}};
130     DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
131     ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
132     if (res != STATUS_SUCCESS)
133     {
134         completionKey = completionValue = 0;
135         memset(&ioSb, 0, sizeof(ioSb));
136         return FALSE;
137     }
138     return TRUE;
139 }
140
141
142 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
143 {
144     int *count = arg;
145
146     trace( "apc called block %p iosb.status %x iosb.info %lu\n",
147            iosb, U(*iosb).Status, iosb->Information );
148     (*count)++;
149     ok( !reserved, "reserved is not 0: %x\n", reserved );
150 }
151
152 static void delete_file_test(void)
153 {
154     NTSTATUS ret;
155     OBJECT_ATTRIBUTES attr;
156     UNICODE_STRING nameW;
157     WCHAR pathW[MAX_PATH];
158     WCHAR pathsubW[MAX_PATH];
159     static const WCHAR testdirW[] = {'n','t','d','e','l','e','t','e','f','i','l','e',0};
160     static const WCHAR subdirW[]  = {'\\','s','u','b',0};
161
162     ret = GetTempPathW(MAX_PATH, pathW);
163     if (!ret)
164     {
165         ok(0, "couldn't get temp dir\n");
166         return;
167     }
168     if (ret + sizeof(testdirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
169     {
170         ok(0, "MAX_PATH exceeded in constructing paths\n");
171         return;
172     }
173
174     lstrcatW(pathW, testdirW);
175     lstrcpyW(pathsubW, pathW);
176     lstrcatW(pathsubW, subdirW);
177
178     ret = CreateDirectoryW(pathW, NULL);
179     ok(ret == TRUE, "couldn't create directory ntdeletefile\n");
180     if (!pRtlDosPathNameToNtPathName_U(pathW, &nameW, NULL, NULL))
181     {
182         ok(0,"RtlDosPathNametoNtPathName_U failed\n");
183         return;
184     }
185
186     attr.Length = sizeof(attr);
187     attr.RootDirectory = 0;
188     attr.Attributes = OBJ_CASE_INSENSITIVE;
189     attr.ObjectName = &nameW;
190     attr.SecurityDescriptor = NULL;
191     attr.SecurityQualityOfService = NULL;
192
193     /* test NtDeleteFile on an empty directory */
194     ret = pNtDeleteFile(&attr);
195     ok(ret == STATUS_SUCCESS, "NtDeleteFile should succeed in removing an empty directory\n");
196     ret = RemoveDirectoryW(pathW);
197     ok(ret == FALSE, "expected to fail removing directory, NtDeleteFile should have removed it\n");
198
199     /* test NtDeleteFile on a non-empty directory */
200     ret = CreateDirectoryW(pathW, NULL);
201     ok(ret == TRUE, "couldn't create directory ntdeletefile ?!\n");
202     ret = CreateDirectoryW(pathsubW, NULL);
203     ok(ret == TRUE, "couldn't create directory subdir\n");
204     ret = pNtDeleteFile(&attr);
205     ok(ret == STATUS_SUCCESS, "expected NtDeleteFile to ret STATUS_SUCCESS\n");
206     ret = RemoveDirectoryW(pathsubW);
207     ok(ret == TRUE, "expected to remove directory ntdeletefile\\sub\n");
208     ret = RemoveDirectoryW(pathW);
209     ok(ret == TRUE, "expected to remove directory ntdeletefile, NtDeleteFile failed.\n");
210
211     pRtlFreeUnicodeString( &nameW );
212 }
213
214 static void read_file_test(void)
215 {
216     const char text[] = "foobar";
217     HANDLE handle, read, write;
218     NTSTATUS status;
219     IO_STATUS_BLOCK iosb, iosb2;
220     DWORD written;
221     int apc_count = 0;
222     char buffer[128];
223     LARGE_INTEGER offset;
224     HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
225
226     buffer[0] = 1;
227
228     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
229
230     /* try read with no data */
231     U(iosb).Status = 0xdeadbabe;
232     iosb.Information = 0xdeadbeef;
233     ok( is_signaled( read ), "read handle is not signaled\n" );
234     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
235     ok( status == STATUS_PENDING, "wrong status %x\n", status );
236     ok( !is_signaled( read ), "read handle is signaled\n" );
237     ok( !is_signaled( event ), "event is signaled\n" );
238     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
239     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
240     ok( !apc_count, "apc was called\n" );
241     WriteFile( write, buffer, 1, &written, NULL );
242     /* iosb updated here by async i/o */
243     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
244     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
245     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
246     ok( !is_signaled( read ), "read handle is signaled\n" );
247     ok( is_signaled( event ), "event is not signaled\n" );
248     ok( !apc_count, "apc was called\n" );
249     apc_count = 0;
250     SleepEx( 1, FALSE ); /* non-alertable sleep */
251     ok( !apc_count, "apc was called\n" );
252     SleepEx( 1, TRUE ); /* alertable sleep */
253     ok( apc_count == 1, "apc not called\n" );
254
255     /* with no event, the pipe handle itself gets signaled */
256     apc_count = 0;
257     U(iosb).Status = 0xdeadbabe;
258     iosb.Information = 0xdeadbeef;
259     ok( !is_signaled( read ), "read handle is not signaled\n" );
260     status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
261     ok( status == STATUS_PENDING, "wrong status %x\n", status );
262     ok( !is_signaled( read ), "read handle is signaled\n" );
263     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
264     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
265     ok( !apc_count, "apc was called\n" );
266     WriteFile( write, buffer, 1, &written, NULL );
267     /* iosb updated here by async i/o */
268     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
269     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
270     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
271     ok( is_signaled( read ), "read handle is signaled\n" );
272     ok( !apc_count, "apc was called\n" );
273     apc_count = 0;
274     SleepEx( 1, FALSE ); /* non-alertable sleep */
275     ok( !apc_count, "apc was called\n" );
276     SleepEx( 1, TRUE ); /* alertable sleep */
277     ok( apc_count == 1, "apc not called\n" );
278
279     /* now read with data ready */
280     apc_count = 0;
281     U(iosb).Status = 0xdeadbabe;
282     iosb.Information = 0xdeadbeef;
283     ResetEvent( event );
284     WriteFile( write, buffer, 1, &written, NULL );
285     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
286     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
287     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
288     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
289     ok( is_signaled( event ), "event is not signaled\n" );
290     ok( !apc_count, "apc was called\n" );
291     SleepEx( 1, FALSE ); /* non-alertable sleep */
292     ok( !apc_count, "apc was called\n" );
293     SleepEx( 1, TRUE ); /* alertable sleep */
294     ok( apc_count == 1, "apc not called\n" );
295
296     /* try read with no data */
297     apc_count = 0;
298     U(iosb).Status = 0xdeadbabe;
299     iosb.Information = 0xdeadbeef;
300     ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
301     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
302     ok( status == STATUS_PENDING, "wrong status %x\n", status );
303     ok( !is_signaled( event ), "event is signaled\n" );
304     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
305     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
306     ok( !apc_count, "apc was called\n" );
307     WriteFile( write, buffer, 1, &written, NULL );
308     /* partial read is good enough */
309     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
310     ok( is_signaled( event ), "event is signaled\n" );
311     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
312     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
313     ok( !apc_count, "apc was called\n" );
314     SleepEx( 1, TRUE ); /* alertable sleep */
315     ok( apc_count == 1, "apc was not called\n" );
316
317     /* read from disconnected pipe */
318     apc_count = 0;
319     U(iosb).Status = 0xdeadbabe;
320     iosb.Information = 0xdeadbeef;
321     CloseHandle( write );
322     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
323     ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
324     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
325     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
326     ok( !is_signaled( event ), "event is signaled\n" );
327     ok( !apc_count, "apc was called\n" );
328     SleepEx( 1, TRUE ); /* alertable sleep */
329     ok( !apc_count, "apc was called\n" );
330     CloseHandle( read );
331
332     /* read from closed handle */
333     apc_count = 0;
334     U(iosb).Status = 0xdeadbabe;
335     iosb.Information = 0xdeadbeef;
336     SetEvent( event );
337     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
338     ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
339     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
340     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
341     ok( is_signaled( event ), "event is signaled\n" );  /* not reset on invalid handle */
342     ok( !apc_count, "apc was called\n" );
343     SleepEx( 1, TRUE ); /* alertable sleep */
344     ok( !apc_count, "apc was called\n" );
345
346     /* disconnect while async read is in progress */
347     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
348     apc_count = 0;
349     U(iosb).Status = 0xdeadbabe;
350     iosb.Information = 0xdeadbeef;
351     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
352     ok( status == STATUS_PENDING, "wrong status %x\n", status );
353     ok( !is_signaled( event ), "event is signaled\n" );
354     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
355     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
356     ok( !apc_count, "apc was called\n" );
357     CloseHandle( write );
358     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
359     ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
360     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
361     ok( is_signaled( event ), "event is signaled\n" );
362     ok( !apc_count, "apc was called\n" );
363     SleepEx( 1, TRUE ); /* alertable sleep */
364     ok( apc_count == 1, "apc was not called\n" );
365     CloseHandle( read );
366
367     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
368     ok(DuplicateHandle(GetCurrentProcess(), read, GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS),
369         "Failed to duplicate handle: %d\n", GetLastError());
370
371     apc_count = 0;
372     U(iosb).Status = 0xdeadbabe;
373     iosb.Information = 0xdeadbeef;
374     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
375     ok( status == STATUS_PENDING, "wrong status %x\n", status );
376     ok( !is_signaled( event ), "event is signaled\n" );
377     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
378     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
379     ok( !apc_count, "apc was called\n" );
380     /* Cancel by other handle */
381     status = pNtCancelIoFile( read, &iosb2 );
382     ok(status == STATUS_SUCCESS, "failed to cancel by different handle: %x\n", status);
383     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
384     ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
385     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
386     ok( is_signaled( event ), "event is signaled\n" );
387     todo_wine ok( !apc_count, "apc was called\n" );
388     SleepEx( 1, TRUE ); /* alertable sleep */
389     ok( apc_count == 1, "apc was not called\n" );
390
391     apc_count = 0;
392     U(iosb).Status = 0xdeadbabe;
393     iosb.Information = 0xdeadbeef;
394     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
395     ok( status == STATUS_PENDING, "wrong status %x\n", status );
396     ok( !is_signaled( event ), "event is signaled\n" );
397     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
398     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
399     ok( !apc_count, "apc was called\n" );
400     /* Close queued handle */
401     CloseHandle( read );
402     SleepEx( 1, TRUE ); /* alertable sleep */
403     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
404     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
405     status = pNtCancelIoFile( read, &iosb2 );
406     ok(status == STATUS_INVALID_HANDLE, "cancelled by closed handle?\n");
407     status = pNtCancelIoFile( handle, &iosb2 );
408     ok(status == STATUS_SUCCESS, "failed to cancel: %x\n", status);
409     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
410     ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
411     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
412     ok( is_signaled( event ), "event is signaled\n" );
413     todo_wine ok( !apc_count, "apc was called\n" );
414     SleepEx( 1, TRUE ); /* alertable sleep */
415     ok( apc_count == 1, "apc was not called\n" );
416     CloseHandle( handle );
417     CloseHandle( write );
418
419     if (pNtCancelIoFileEx)
420     {
421         /* Basic Cancel Ex */
422         if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
423
424         apc_count = 0;
425         U(iosb).Status = 0xdeadbabe;
426         iosb.Information = 0xdeadbeef;
427         status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
428         ok( status == STATUS_PENDING, "wrong status %x\n", status );
429         ok( !is_signaled( event ), "event is signaled\n" );
430         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
431         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
432         ok( !apc_count, "apc was called\n" );
433         status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
434         ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
435         Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
436         ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
437         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
438         ok( is_signaled( event ), "event is signaled\n" );
439         todo_wine ok( !apc_count, "apc was called\n" );
440         SleepEx( 1, TRUE ); /* alertable sleep */
441         ok( apc_count == 1, "apc was not called\n" );
442
443         /* Duplicate iosb */
444         apc_count = 0;
445         U(iosb).Status = 0xdeadbabe;
446         iosb.Information = 0xdeadbeef;
447         status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
448         ok( status == STATUS_PENDING, "wrong status %x\n", status );
449         ok( !is_signaled( event ), "event is signaled\n" );
450         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
451         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
452         ok( !apc_count, "apc was called\n" );
453         status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
454         ok( status == STATUS_PENDING, "wrong status %x\n", status );
455         ok( !is_signaled( event ), "event is signaled\n" );
456         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
457         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
458         ok( !apc_count, "apc was called\n" );
459         status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
460         ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
461         Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
462         ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
463         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
464         ok( is_signaled( event ), "event is signaled\n" );
465         todo_wine ok( !apc_count, "apc was called\n" );
466         SleepEx( 1, TRUE ); /* alertable sleep */
467         ok( apc_count == 2, "apc was not called\n" );
468
469         CloseHandle( read );
470         CloseHandle( write );
471     }
472
473     /* now try a real file */
474     if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
475     apc_count = 0;
476     U(iosb).Status = 0xdeadbabe;
477     iosb.Information = 0xdeadbeef;
478     offset.QuadPart = 0;
479     ResetEvent( event );
480     status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
481     ok( status == STATUS_SUCCESS || status == STATUS_PENDING, "wrong status %x\n", status );
482     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
483     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
484     ok( is_signaled( event ), "event is signaled\n" );
485     ok( !apc_count, "apc was called\n" );
486     SleepEx( 1, TRUE ); /* alertable sleep */
487     ok( apc_count == 1, "apc was not called\n" );
488
489     apc_count = 0;
490     U(iosb).Status = 0xdeadbabe;
491     iosb.Information = 0xdeadbeef;
492     offset.QuadPart = 0;
493     ResetEvent( event );
494     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
495     ok( status == STATUS_SUCCESS ||
496         status == STATUS_PENDING, /* vista */
497         "wrong status %x\n", status );
498     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
499     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
500     ok( is_signaled( event ), "event is signaled\n" );
501     ok( !apc_count, "apc was called\n" );
502     SleepEx( 1, TRUE ); /* alertable sleep */
503     ok( apc_count == 1, "apc was not called\n" );
504
505     /* read beyond eof */
506     apc_count = 0;
507     U(iosb).Status = 0xdeadbabe;
508     iosb.Information = 0xdeadbeef;
509     offset.QuadPart = strlen(text) + 2;
510     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
511     if (status == STATUS_PENDING)  /* vista */
512     {
513         ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
514         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
515         ok( is_signaled( event ), "event is signaled\n" );
516         ok( !apc_count, "apc was called\n" );
517         SleepEx( 1, TRUE ); /* alertable sleep */
518         ok( apc_count == 1, "apc was not called\n" );
519     }
520     else
521     {
522         ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
523         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
524         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
525         ok( !is_signaled( event ), "event is signaled\n" );
526         ok( !apc_count, "apc was called\n" );
527         SleepEx( 1, TRUE ); /* alertable sleep */
528         ok( !apc_count, "apc was called\n" );
529     }
530     CloseHandle( handle );
531
532     /* now a non-overlapped file */
533     if (!(handle = create_temp_file(0))) return;
534     apc_count = 0;
535     U(iosb).Status = 0xdeadbabe;
536     iosb.Information = 0xdeadbeef;
537     offset.QuadPart = 0;
538     status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
539     ok( status == STATUS_END_OF_FILE ||
540         status == STATUS_SUCCESS ||
541         status == STATUS_PENDING,  /* vista */
542         "wrong status %x\n", status );
543     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
544     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
545     ok( is_signaled( event ), "event is signaled\n" );
546     ok( !apc_count, "apc was called\n" );
547     SleepEx( 1, TRUE ); /* alertable sleep */
548     ok( apc_count == 1, "apc was not called\n" );
549
550     apc_count = 0;
551     U(iosb).Status = 0xdeadbabe;
552     iosb.Information = 0xdeadbeef;
553     offset.QuadPart = 0;
554     ResetEvent( event );
555     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
556     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
557     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
558     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
559     ok( is_signaled( event ), "event is signaled\n" );
560     ok( !apc_count, "apc was called\n" );
561     SleepEx( 1, TRUE ); /* alertable sleep */
562     todo_wine ok( !apc_count, "apc was called\n" );
563
564     /* read beyond eof */
565     apc_count = 0;
566     U(iosb).Status = 0xdeadbabe;
567     iosb.Information = 0xdeadbeef;
568     offset.QuadPart = strlen(text) + 2;
569     ResetEvent( event );
570     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
571     ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
572     todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
573     todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
574     todo_wine ok( is_signaled( event ), "event is not signaled\n" );
575     ok( !apc_count, "apc was called\n" );
576     SleepEx( 1, TRUE ); /* alertable sleep */
577     ok( !apc_count, "apc was called\n" );
578
579     CloseHandle( handle );
580
581     CloseHandle( event );
582 }
583
584 static void nt_mailslot_test(void)
585 {
586     HANDLE hslot;
587     ACCESS_MASK DesiredAccess;
588     OBJECT_ATTRIBUTES attr;
589
590     ULONG CreateOptions;
591     ULONG MailslotQuota;
592     ULONG MaxMessageSize;
593     LARGE_INTEGER TimeOut;
594     IO_STATUS_BLOCK IoStatusBlock;
595     NTSTATUS rc;
596     UNICODE_STRING str;
597     WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
598                         'R',':','\\','F','R','E','D','\0' };
599
600     TimeOut.QuadPart = -1;
601
602     pRtlInitUnicodeString(&str, buffer1);
603     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
604     CreateOptions = MailslotQuota = MaxMessageSize = 0;
605     DesiredAccess = GENERIC_READ;
606
607     /*
608      * Check for NULL pointer handling
609      */
610     rc = pNtCreateMailslotFile(NULL, DesiredAccess,
611          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
612          &TimeOut);
613     ok( rc == STATUS_ACCESS_VIOLATION ||
614         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
615         "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
616
617     /*
618      * Test to see if the Timeout can be NULL
619      */
620     hslot = (HANDLE)0xdeadbeef;
621     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
622          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
623          NULL);
624     ok( rc == STATUS_SUCCESS ||
625         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
626         "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
627     ok( hslot != 0, "Handle is invalid\n");
628
629     if  ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
630
631     /*
632      * Test that the length field is checked properly
633      */
634     attr.Length = 0;
635     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
636          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
637          &TimeOut);
638     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
639
640     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
641
642     attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
643     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
644          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
645          &TimeOut);
646     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
647
648     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
649
650     /*
651      * Test handling of a NULL unicode string in ObjectName
652      */
653     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
654     attr.ObjectName = NULL;
655     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
656          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
657          &TimeOut);
658     ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
659         rc == STATUS_INVALID_PARAMETER,
660         "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
661
662     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
663
664     /*
665      * Test a valid call
666      */
667     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
668     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
669          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
670          &TimeOut);
671     ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
672     ok( hslot != 0, "Handle is invalid\n");
673
674     rc = pNtClose(hslot);
675     ok( rc == STATUS_SUCCESS, "NtClose failed\n");
676 }
677
678 static void test_iocp_setcompletion(HANDLE h)
679 {
680     NTSTATUS res;
681     long count;
682
683     res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
684     ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
685
686     count = get_pending_msgs(h);
687     ok( count == 1, "Unexpected msg count: %ld\n", count );
688
689     if (get_msg(h))
690     {
691         ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
692         ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
693         ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
694         ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
695     }
696
697     count = get_pending_msgs(h);
698     ok( !count, "Unexpected msg count: %ld\n", count );
699 }
700
701 static void test_iocp_fileio(HANDLE h)
702 {
703     static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
704
705     IO_STATUS_BLOCK iosb;
706     FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
707     HANDLE hPipeSrv, hPipeClt;
708     NTSTATUS res;
709
710     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
711     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
712     if (hPipeSrv != INVALID_HANDLE_VALUE )
713     {
714         hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
715         ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
716         if (hPipeClt != INVALID_HANDLE_VALUE)
717         {
718             res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
719             ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
720             CloseHandle(hPipeClt);
721         }
722         CloseHandle( hPipeSrv );
723     }
724
725     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
726     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
727     if (hPipeSrv == INVALID_HANDLE_VALUE )
728         return;
729
730     hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
731     ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
732     if (hPipeClt != INVALID_HANDLE_VALUE)
733     {
734         OVERLAPPED o = {0,};
735         BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
736         DWORD read;
737         long count;
738
739         NTSTATUS res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
740         ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
741         ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
742
743         memset( send_buf, 0, TEST_BUF_LEN );
744         memset( recv_buf, 0xde, TEST_BUF_LEN );
745         count = get_pending_msgs(h);
746         ok( !count, "Unexpected msg count: %ld\n", count );
747         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
748         count = get_pending_msgs(h);
749         ok( !count, "Unexpected msg count: %ld\n", count );
750         WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
751
752         if (get_msg(h))
753         {
754             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
755             ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
756             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
757             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
758             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] );
759         }
760         count = get_pending_msgs(h);
761         ok( !count, "Unexpected msg count: %ld\n", count );
762
763         memset( send_buf, 0, TEST_BUF_LEN );
764         memset( recv_buf, 0xde, TEST_BUF_LEN );
765         WriteFile( hPipeClt, send_buf, 2, &read, NULL );
766         count = get_pending_msgs(h);
767         ok( !count, "Unexpected msg count: %ld\n", count );
768         ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
769         count = get_pending_msgs(h);
770         ok( count == 1, "Unexpected msg count: %ld\n", count );
771         if (get_msg(h))
772         {
773             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
774             ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
775             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
776             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
777             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] );
778         }
779
780         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
781         CloseHandle( hPipeSrv );
782         count = get_pending_msgs(h);
783         ok( count == 1, "Unexpected msg count: %ld\n", count );
784         if (get_msg(h))
785         {
786             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
787             ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
788             /* wine sends wrong status here */
789             todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
790             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
791         }
792     }
793
794     CloseHandle( hPipeClt );
795 }
796
797 static void test_file_basic_information(void)
798 {
799     IO_STATUS_BLOCK io;
800     FILE_BASIC_INFORMATION fbi;
801     HANDLE h;
802     int res;
803     int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
804
805     if (!(h = create_temp_file(0))) return;
806
807     /* Check default first */
808     memset(&fbi, 0, sizeof(fbi));
809     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
810     ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
811     ok ( (fbi.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
812          "attribute %x not expected\n", fbi.FileAttributes );
813
814     /* Then SYSTEM */
815     /* Clear fbi to avoid setting times */
816     memset(&fbi, 0, sizeof(fbi));
817     fbi.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
818     res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
819     ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
820
821     memset(&fbi, 0, sizeof(fbi));
822     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
823     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
824     todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_SYSTEM, "attribute %x not FILE_ATTRIBUTE_SYSTEM\n", fbi.FileAttributes );
825
826     /* Then HIDDEN */
827     memset(&fbi, 0, sizeof(fbi));
828     fbi.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
829     res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
830     ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
831
832     memset(&fbi, 0, sizeof(fbi));
833     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
834     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
835     todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_HIDDEN, "attribute %x not FILE_ATTRIBUTE_HIDDEN\n", fbi.FileAttributes );
836
837     /* Check NORMAL last of all (to make sure we can clear attributes) */
838     memset(&fbi, 0, sizeof(fbi));
839     fbi.FileAttributes = FILE_ATTRIBUTE_NORMAL;
840     res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
841     ok ( res == STATUS_SUCCESS, "can't set normal attribute\n");
842
843     memset(&fbi, 0, sizeof(fbi));
844     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
845     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
846     todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_NORMAL, "attribute %x not 0\n", fbi.FileAttributes );
847
848     CloseHandle( h );
849 }
850
851 static void test_file_all_information(void)
852 {
853     IO_STATUS_BLOCK io;
854     /* FileAllInformation, like FileNameInformation, has a variable-length pathname
855      * buffer at the end.  Vista objects with STATUS_BUFFER_OVERFLOW if you
856      * don't leave enough room there.
857      */
858     struct {
859       FILE_ALL_INFORMATION fai;
860       WCHAR buf[256];
861     } fai_buf;
862     HANDLE h;
863     int res;
864     int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
865
866     if (!(h = create_temp_file(0))) return;
867
868     /* Check default first */
869     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
870     ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
871     ok ( (fai_buf.fai.BasicInformation.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
872          "attribute %x not expected\n", fai_buf.fai.BasicInformation.FileAttributes );
873
874     /* Then SYSTEM */
875     /* Clear fbi to avoid setting times */
876     memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
877     fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
878     res = pNtSetInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
879     ok ( res == STATUS_INVALID_INFO_CLASS || res == STATUS_NOT_IMPLEMENTED, "shouldn't be able to set FileAllInformation, res %x\n", res);
880     res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
881     ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
882
883     memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
884     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
885     ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
886     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 );
887
888     /* Then HIDDEN */
889     memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
890     fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
891     res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
892     ok ( res == STATUS_SUCCESS, "can't set system attribute\n");
893
894     memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
895     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
896     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
897     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 );
898
899     /* Check NORMAL last of all (to make sure we can clear attributes) */
900     memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
901     fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_NORMAL;
902     res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
903     ok ( res == STATUS_SUCCESS, "can't set normal attribute\n");
904
905     memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
906     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
907     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
908     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 );
909
910     CloseHandle( h );
911 }
912
913 static void test_file_both_information(void)
914 {
915     IO_STATUS_BLOCK io;
916     FILE_BOTH_DIR_INFORMATION fbi;
917     HANDLE h;
918     int res;
919
920     if (!(h = create_temp_file(0))) return;
921
922     memset(&fbi, 0, sizeof(fbi));
923     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBothDirectoryInformation);
924     ok ( res == STATUS_INVALID_INFO_CLASS || res == STATUS_NOT_IMPLEMENTED, "shouldn't be able to query FileBothDirectoryInformation, res %x\n", res);
925
926     CloseHandle( h );
927 }
928
929 static void test_iocompletion(void)
930 {
931     HANDLE h = INVALID_HANDLE_VALUE;
932     NTSTATUS res;
933
934     res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
935
936     ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
937     ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
938
939     if ( h && h != INVALID_HANDLE_VALUE)
940     {
941         test_iocp_setcompletion(h);
942         test_iocp_fileio(h);
943         pNtClose(h);
944     }
945 }
946
947 static void test_file_name_information(void)
948 {
949     WCHAR *file_name, *volume_prefix, *expected;
950     FILE_NAME_INFORMATION *info;
951     ULONG old_redir = 1, tmp;
952     UINT file_name_size;
953     IO_STATUS_BLOCK io;
954     UINT info_size;
955     HRESULT hr;
956     HANDLE h;
957     UINT len;
958
959     /* GetVolumePathName is not present before w2k */
960     if (!pGetVolumePathNameW) {
961         win_skip("GetVolumePathNameW not found\n");
962         return;
963     }
964
965     file_name_size = GetSystemDirectoryW( NULL, 0 );
966     file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
967     volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
968     expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
969
970     len = GetSystemDirectoryW( file_name, file_name_size );
971     ok(len == file_name_size - 1,
972             "GetSystemDirectoryW returned %u, expected %u.\n",
973             len, file_name_size - 1);
974
975     len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
976     ok(len, "GetVolumePathNameW failed.\n");
977
978     len = lstrlenW( volume_prefix );
979     if (len && volume_prefix[len - 1] == '\\') --len;
980     memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
981     expected[file_name_size - len - 1] = '\0';
982
983     /* A bit more than we actually need, but it keeps the calculation simple. */
984     info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
985     info = HeapAlloc( GetProcessHeap(), 0, info_size );
986
987     if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( TRUE, &old_redir );
988     h = CreateFileW( file_name, GENERIC_READ,
989             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
990             NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
991     if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( old_redir, &tmp );
992     ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
993
994     hr = pNtQueryInformationFile( h, &io, info, sizeof(*info) - 1, FileNameInformation );
995     ok(hr == STATUS_INFO_LENGTH_MISMATCH, "NtQueryInformationFile returned %#x.\n", hr);
996
997     memset( info, 0xcc, info_size );
998     hr = pNtQueryInformationFile( h, &io, info, sizeof(*info), FileNameInformation );
999     ok(hr == STATUS_BUFFER_OVERFLOW, "NtQueryInformationFile returned %#x, expected %#x.\n",
1000             hr, STATUS_BUFFER_OVERFLOW);
1001     ok(U(io).Status == STATUS_BUFFER_OVERFLOW, "io.Status is %#x, expected %#x.\n",
1002             U(io).Status, STATUS_BUFFER_OVERFLOW);
1003     ok(info->FileNameLength == lstrlenW( expected ) * sizeof(WCHAR), "info->FileNameLength is %u, expected %u.\n",
1004             info->FileNameLength, lstrlenW( expected ) * sizeof(WCHAR));
1005     ok(info->FileName[2] == 0xcccc, "info->FileName[2] is %#x, expected 0xcccc.\n", info->FileName[2]);
1006     ok(CharLowerW((LPWSTR)(UINT_PTR)info->FileName[1]) == CharLowerW((LPWSTR)(UINT_PTR)expected[1]),
1007             "info->FileName[1] is %p, expected %p.\n",
1008             CharLowerW((LPWSTR)(UINT_PTR)info->FileName[1]), CharLowerW((LPWSTR)(UINT_PTR)expected[1]));
1009     ok(io.Information == sizeof(*info), "io.Information is %lu, expected %u.\n", io.Information, sizeof(*info));
1010
1011     memset( info, 0xcc, info_size );
1012     hr = pNtQueryInformationFile( h, &io, info, info_size, FileNameInformation );
1013     ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1014     ok(U(io).Status == STATUS_SUCCESS, "io.Status is %#x, expected %#x.\n", U(io).Status, STATUS_SUCCESS);
1015     ok(info->FileNameLength == lstrlenW( expected ) * sizeof(WCHAR), "info->FileNameLength is %u, expected %u.\n",
1016             info->FileNameLength, lstrlenW( expected ) * sizeof(WCHAR));
1017     ok(info->FileName[info->FileNameLength / sizeof(WCHAR)] == 0xcccc, "info->FileName[%u] is %#x, expected 0xcccc.\n",
1018             info->FileNameLength / sizeof(WCHAR), info->FileName[info->FileNameLength / sizeof(WCHAR)]);
1019     info->FileName[info->FileNameLength / sizeof(WCHAR)] = '\0';
1020     ok(!lstrcmpiW( info->FileName, expected ), "info->FileName is %s, expected %s.\n",
1021             wine_dbgstr_w( info->FileName ), wine_dbgstr_w( expected ));
1022     ok(io.Information == FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + info->FileNameLength,
1023             "io.Information is %lu, expected %u.\n",
1024             io.Information, FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + info->FileNameLength);
1025
1026     CloseHandle( h );
1027     HeapFree( GetProcessHeap(), 0, info );
1028     HeapFree( GetProcessHeap(), 0, expected );
1029     HeapFree( GetProcessHeap(), 0, volume_prefix );
1030
1031     if (old_redir || !pGetSystemWow64DirectoryW || !(file_name_size = pGetSystemWow64DirectoryW( NULL, 0 )))
1032     {
1033         skip("Not running on WoW64, skipping test.\n");
1034         HeapFree( GetProcessHeap(), 0, file_name );
1035         return;
1036     }
1037
1038     h = CreateFileW( file_name, GENERIC_READ,
1039             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1040             NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1041     ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1042     HeapFree( GetProcessHeap(), 0, file_name );
1043
1044     file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1045     volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1046     expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*expected) );
1047
1048     len = pGetSystemWow64DirectoryW( file_name, file_name_size );
1049     ok(len == file_name_size - 1,
1050             "GetSystemWow64DirectoryW returned %u, expected %u.\n",
1051             len, file_name_size - 1);
1052
1053     len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1054     ok(len, "GetVolumePathNameW failed.\n");
1055
1056     len = lstrlenW( volume_prefix );
1057     if (len && volume_prefix[len - 1] == '\\') --len;
1058     memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1059     expected[file_name_size - len - 1] = '\0';
1060
1061     info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1062     info = HeapAlloc( GetProcessHeap(), 0, info_size );
1063
1064     memset( info, 0xcc, info_size );
1065     hr = pNtQueryInformationFile( h, &io, info, info_size, FileNameInformation );
1066     ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1067     info->FileName[info->FileNameLength / sizeof(WCHAR)] = '\0';
1068     ok(!lstrcmpiW( info->FileName, expected ), "info->FileName is %s, expected %s.\n",
1069             wine_dbgstr_w( info->FileName ), wine_dbgstr_w( expected ));
1070
1071     CloseHandle( h );
1072     HeapFree( GetProcessHeap(), 0, info );
1073     HeapFree( GetProcessHeap(), 0, expected );
1074     HeapFree( GetProcessHeap(), 0, volume_prefix );
1075     HeapFree( GetProcessHeap(), 0, file_name );
1076 }
1077
1078 START_TEST(file)
1079 {
1080     HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
1081     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1082     if (!hntdll)
1083     {
1084         skip("not running on NT, skipping test\n");
1085         return;
1086     }
1087
1088     pGetVolumePathNameW = (void *)GetProcAddress(hkernel32, "GetVolumePathNameW");
1089     pGetSystemWow64DirectoryW = (void *)GetProcAddress(hkernel32, "GetSystemWow64DirectoryW");
1090
1091     pRtlFreeUnicodeString   = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
1092     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
1093     pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
1094     pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll, "RtlWow64EnableFsRedirectionEx");
1095     pNtCreateMailslotFile   = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
1096     pNtDeleteFile           = (void *)GetProcAddress(hntdll, "NtDeleteFile");
1097     pNtReadFile             = (void *)GetProcAddress(hntdll, "NtReadFile");
1098     pNtWriteFile            = (void *)GetProcAddress(hntdll, "NtWriteFile");
1099     pNtCancelIoFile         = (void *)GetProcAddress(hntdll, "NtCancelIoFile");
1100     pNtCancelIoFileEx       = (void *)GetProcAddress(hntdll, "NtCancelIoFileEx");
1101     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
1102     pNtCreateIoCompletion   = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
1103     pNtOpenIoCompletion     = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
1104     pNtQueryIoCompletion    = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
1105     pNtRemoveIoCompletion   = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
1106     pNtSetIoCompletion      = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
1107     pNtSetInformationFile   = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
1108     pNtQueryInformationFile = (void *)GetProcAddress(hntdll, "NtQueryInformationFile");
1109
1110     delete_file_test();
1111     read_file_test();
1112     nt_mailslot_test();
1113     test_iocompletion();
1114     test_file_basic_information();
1115     test_file_all_information();
1116     test_file_both_information();
1117     test_file_name_information();
1118 }