server: Store I/O completion information in async structure.
[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  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  * We use function pointers here as there is no import library for NTDLL on
22  * windows.
23  */
24
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #include "ntstatus.h"
29 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
30  * definition errors when we get to winnt.h
31  */
32 #define WIN32_NO_STATUS
33
34 #include "wine/test.h"
35 #include "winternl.h"
36
37 #ifndef IO_COMPLETION_ALL_ACCESS
38 #define IO_COMPLETION_ALL_ACCESS 0x001F0003
39 #endif
40
41 static VOID     (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
42 static VOID     (WINAPI *pRtlFreeUnicodeString)(PUNICODE_STRING);
43 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
44                                        ULONG, ULONG, ULONG, PLARGE_INTEGER );
45 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
46                                       PIO_APC_ROUTINE apc, void* apc_user,
47                                       PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
48                                       PLARGE_INTEGER offset, PULONG key);
49 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
50                                        PIO_APC_ROUTINE apc, void* apc_user,
51                                        PIO_STATUS_BLOCK io_status,
52                                        const void* buffer, ULONG length,
53                                        PLARGE_INTEGER offset, PULONG key);
54 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
55
56 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
57 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
58 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
59 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
60 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
61 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
62
63 static inline BOOL is_signaled( HANDLE obj )
64 {
65     return WaitForSingleObject( obj, 0 ) == 0;
66 }
67
68 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
69
70 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
71 {
72     *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
73                             1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
74     ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
75
76     *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
77     ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
78
79     return TRUE;
80 }
81
82 static HANDLE create_temp_file( ULONG flags )
83 {
84     char buffer[MAX_PATH];
85     HANDLE handle;
86
87     GetTempFileNameA( ".", "foo", 0, buffer );
88     handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
89                          flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
90     ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
91     return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
92 }
93
94 #define CVALUE_FIRST 0xfffabbcc
95 #define CKEY_FIRST 0x1030341
96 #define CKEY_SECOND 0x132E46
97
98 ULONG_PTR completionKey;
99 IO_STATUS_BLOCK ioSb;
100 ULONG_PTR completionValue;
101
102 static long get_pending_msgs(HANDLE h)
103 {
104     NTSTATUS res;
105     ULONG a, req;
106
107     res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, (PVOID)&a, sizeof(a), &req );
108     ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
109     if (res != STATUS_SUCCESS) return -1;
110     ok( req == sizeof(a), "Unexpected response size: %x\n", req );
111     return a;
112 }
113
114 static BOOL get_msg(HANDLE h)
115 {
116     LARGE_INTEGER timeout = {{-10000000*3}};
117     DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
118     ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
119     if (res != STATUS_SUCCESS)
120     {
121         completionKey = completionValue = 0;
122         memset(&ioSb, 0, sizeof(ioSb));
123         return FALSE;
124     }
125     return TRUE;
126 }
127
128
129 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
130 {
131     int *count = arg;
132
133     trace( "apc called block %p iosb.status %x iosb.info %lu\n",
134            iosb, U(*iosb).Status, iosb->Information );
135     (*count)++;
136     ok( !reserved, "reserved is not 0: %x\n", reserved );
137 }
138
139 static void read_file_test(void)
140 {
141     const char text[] = "foobar";
142     HANDLE handle, read, write;
143     NTSTATUS status;
144     IO_STATUS_BLOCK iosb;
145     DWORD written;
146     int apc_count = 0;
147     char buffer[128];
148     LARGE_INTEGER offset;
149     HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
150
151     buffer[0] = 1;
152
153     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
154
155     /* try read with no data */
156     U(iosb).Status = 0xdeadbabe;
157     iosb.Information = 0xdeadbeef;
158     ok( is_signaled( read ), "read handle is not signaled\n" );
159     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
160     ok( status == STATUS_PENDING, "wrong status %x\n", status );
161     ok( !is_signaled( read ), "read handle is signaled\n" );
162     ok( !is_signaled( event ), "event is signaled\n" );
163     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
164     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
165     ok( !apc_count, "apc was called\n" );
166     WriteFile( write, buffer, 1, &written, NULL );
167     /* iosb updated here by async i/o */
168     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
169     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
170     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
171     ok( !is_signaled( read ), "read handle is signaled\n" );
172     ok( is_signaled( event ), "event is not signaled\n" );
173     ok( !apc_count, "apc was called\n" );
174     apc_count = 0;
175     SleepEx( 1, FALSE ); /* non-alertable sleep */
176     ok( !apc_count, "apc was called\n" );
177     SleepEx( 1, TRUE ); /* alertable sleep */
178     ok( apc_count == 1, "apc not called\n" );
179
180     /* with no event, the pipe handle itself gets signaled */
181     apc_count = 0;
182     U(iosb).Status = 0xdeadbabe;
183     iosb.Information = 0xdeadbeef;
184     ok( !is_signaled( read ), "read handle is not signaled\n" );
185     status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
186     ok( status == STATUS_PENDING, "wrong status %x\n", status );
187     ok( !is_signaled( read ), "read handle is signaled\n" );
188     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
189     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
190     ok( !apc_count, "apc was called\n" );
191     WriteFile( write, buffer, 1, &written, NULL );
192     /* iosb updated here by async i/o */
193     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
194     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
195     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
196     ok( is_signaled( read ), "read handle is signaled\n" );
197     ok( !apc_count, "apc was called\n" );
198     apc_count = 0;
199     SleepEx( 1, FALSE ); /* non-alertable sleep */
200     ok( !apc_count, "apc was called\n" );
201     SleepEx( 1, TRUE ); /* alertable sleep */
202     ok( apc_count == 1, "apc not called\n" );
203
204     /* now read with data ready */
205     apc_count = 0;
206     U(iosb).Status = 0xdeadbabe;
207     iosb.Information = 0xdeadbeef;
208     ResetEvent( event );
209     WriteFile( write, buffer, 1, &written, NULL );
210     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
211     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
212     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
213     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
214     ok( is_signaled( event ), "event is not signaled\n" );
215     ok( !apc_count, "apc was called\n" );
216     SleepEx( 1, FALSE ); /* non-alertable sleep */
217     ok( !apc_count, "apc was called\n" );
218     SleepEx( 1, TRUE ); /* alertable sleep */
219     ok( apc_count == 1, "apc not called\n" );
220
221     /* try read with no data */
222     apc_count = 0;
223     U(iosb).Status = 0xdeadbabe;
224     iosb.Information = 0xdeadbeef;
225     ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
226     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
227     ok( status == STATUS_PENDING, "wrong status %x\n", status );
228     ok( !is_signaled( event ), "event is signaled\n" );
229     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
230     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
231     ok( !apc_count, "apc was called\n" );
232     WriteFile( write, buffer, 1, &written, NULL );
233     /* partial read is good enough */
234     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
235     ok( is_signaled( event ), "event is signaled\n" );
236     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
237     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
238     ok( !apc_count, "apc was called\n" );
239     SleepEx( 1, TRUE ); /* alertable sleep */
240     ok( apc_count == 1, "apc was not called\n" );
241
242     /* read from disconnected pipe */
243     apc_count = 0;
244     U(iosb).Status = 0xdeadbabe;
245     iosb.Information = 0xdeadbeef;
246     CloseHandle( write );
247     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
248     ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
249     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
250     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
251     ok( !is_signaled( event ), "event is signaled\n" );
252     ok( !apc_count, "apc was called\n" );
253     SleepEx( 1, TRUE ); /* alertable sleep */
254     ok( !apc_count, "apc was called\n" );
255     CloseHandle( read );
256
257     /* read from closed handle */
258     apc_count = 0;
259     U(iosb).Status = 0xdeadbabe;
260     iosb.Information = 0xdeadbeef;
261     SetEvent( event );
262     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
263     ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
264     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
265     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
266     ok( is_signaled( event ), "event is signaled\n" );  /* not reset on invalid handle */
267     ok( !apc_count, "apc was called\n" );
268     SleepEx( 1, TRUE ); /* alertable sleep */
269     ok( !apc_count, "apc was called\n" );
270
271     /* disconnect while async read is in progress */
272     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
273     apc_count = 0;
274     U(iosb).Status = 0xdeadbabe;
275     iosb.Information = 0xdeadbeef;
276     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
277     ok( status == STATUS_PENDING, "wrong status %x\n", status );
278     ok( !is_signaled( event ), "event is signaled\n" );
279     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
280     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
281     ok( !apc_count, "apc was called\n" );
282     CloseHandle( write );
283     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
284     ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
285     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
286     ok( is_signaled( event ), "event is signaled\n" );
287     ok( !apc_count, "apc was called\n" );
288     SleepEx( 1, TRUE ); /* alertable sleep */
289     ok( apc_count == 1, "apc was not called\n" );
290     CloseHandle( read );
291
292     /* now try a real file */
293     if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
294     apc_count = 0;
295     U(iosb).Status = 0xdeadbabe;
296     iosb.Information = 0xdeadbeef;
297     offset.QuadPart = 0;
298     ResetEvent( event );
299     pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
300     ok( status == STATUS_PENDING, "wrong status %x\n", status );
301     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
302     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
303     ok( is_signaled( event ), "event is signaled\n" );
304     ok( !apc_count, "apc was called\n" );
305     SleepEx( 1, TRUE ); /* alertable sleep */
306     ok( apc_count == 1, "apc was not called\n" );
307
308     apc_count = 0;
309     U(iosb).Status = 0xdeadbabe;
310     iosb.Information = 0xdeadbeef;
311     offset.QuadPart = 0;
312     ResetEvent( event );
313     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
314     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
315     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
316     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
317     ok( is_signaled( event ), "event is signaled\n" );
318     ok( !apc_count, "apc was called\n" );
319     SleepEx( 1, TRUE ); /* alertable sleep */
320     ok( apc_count == 1, "apc was not called\n" );
321
322     /* read beyond eof */
323     apc_count = 0;
324     U(iosb).Status = 0xdeadbabe;
325     iosb.Information = 0xdeadbeef;
326     offset.QuadPart = strlen(text) + 2;
327     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
328     ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
329     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
330     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
331     ok( !is_signaled( event ), "event is signaled\n" );
332     ok( !apc_count, "apc was called\n" );
333     SleepEx( 1, TRUE ); /* alertable sleep */
334     ok( !apc_count, "apc was called\n" );
335     CloseHandle( handle );
336
337     /* now a non-overlapped file */
338     if (!(handle = create_temp_file(0))) return;
339     apc_count = 0;
340     U(iosb).Status = 0xdeadbabe;
341     iosb.Information = 0xdeadbeef;
342     offset.QuadPart = 0;
343     pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
344     ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
345     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
346     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
347     ok( is_signaled( event ), "event is signaled\n" );
348     ok( !apc_count, "apc was called\n" );
349     SleepEx( 1, TRUE ); /* alertable sleep */
350     ok( apc_count == 1, "apc was not called\n" );
351
352     apc_count = 0;
353     U(iosb).Status = 0xdeadbabe;
354     iosb.Information = 0xdeadbeef;
355     offset.QuadPart = 0;
356     ResetEvent( event );
357     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
358     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
359     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
360     ok( iosb.Information == strlen(text), "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     todo_wine ok( !apc_count, "apc was called\n" );
365
366     /* read beyond eof */
367     apc_count = 0;
368     U(iosb).Status = 0xdeadbabe;
369     iosb.Information = 0xdeadbeef;
370     offset.QuadPart = strlen(text) + 2;
371     ResetEvent( event );
372     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
373     ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
374     todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
375     todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
376     todo_wine ok( is_signaled( event ), "event is not signaled\n" );
377     ok( !apc_count, "apc was called\n" );
378     SleepEx( 1, TRUE ); /* alertable sleep */
379     ok( !apc_count, "apc was called\n" );
380
381     CloseHandle( handle );
382
383     CloseHandle( event );
384 }
385
386 static void nt_mailslot_test(void)
387 {
388     HANDLE hslot;
389     ACCESS_MASK DesiredAccess;
390     OBJECT_ATTRIBUTES attr;
391
392     ULONG CreateOptions;
393     ULONG MailslotQuota;
394     ULONG MaxMessageSize;
395     LARGE_INTEGER TimeOut;
396     IO_STATUS_BLOCK IoStatusBlock;
397     NTSTATUS rc;
398     UNICODE_STRING str;
399     WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
400                         'R',':','\\','F','R','E','D','\0' };
401
402     TimeOut.QuadPart = -1;
403
404     pRtlInitUnicodeString(&str, buffer1);
405     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
406     DesiredAccess = CreateOptions = MailslotQuota = MaxMessageSize = 0;
407
408     /*
409      * Check for NULL pointer handling
410      */
411     rc = pNtCreateMailslotFile(NULL, DesiredAccess,
412          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
413          &TimeOut);
414     ok( rc == STATUS_ACCESS_VIOLATION, "rc = %x not c0000005 STATUS_ACCESS_VIOLATION\n", rc);
415
416     /*
417      * Test to see if the Timeout can be NULL
418      */
419     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
420          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
421          NULL);
422     ok( rc == STATUS_SUCCESS, "rc = %x not STATUS_SUCCESS\n", rc);
423     ok( hslot != 0, "Handle is invalid\n");
424
425     if  ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
426
427     /*
428      * Test that the length field is checked properly
429      */
430     attr.Length = 0;
431     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
432          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
433          &TimeOut);
434     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
435
436     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
437
438     attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
439     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
440          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
441          &TimeOut);
442     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
443
444     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
445
446     /*
447      * Test handling of a NULL unicode string in ObjectName
448      */
449     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
450     attr.ObjectName = NULL;
451     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
452          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
453          &TimeOut);
454     ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD, "rc = %x not c000003b STATUS_OBJECT_PATH_SYNTAX_BAD\n", rc);
455
456     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
457
458     /*
459      * Test a valid call
460      */
461     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
462     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
463          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
464          &TimeOut);
465     ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x %u\n", rc, GetLastError());
466     ok( hslot != 0, "Handle is invalid\n");
467
468     rc = pNtClose(hslot);
469     ok( rc == STATUS_SUCCESS, "NtClose failed\n");
470
471     pRtlFreeUnicodeString(&str);
472 }
473
474 static void test_iocp_setcompletion(HANDLE h)
475 {
476     NTSTATUS res;
477     long count;
478
479     res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
480     ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
481
482     count = get_pending_msgs(h);
483     ok( count == 1, "Unexpected msg count: %ld\n", count );
484
485     if (get_msg(h))
486     {
487         ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
488         ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
489         ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
490         ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
491     }
492
493     count = get_pending_msgs(h);
494     ok( !count, "Unexpected msg count: %ld\n", count );
495 }
496
497 static void test_iocp_fileio(HANDLE h)
498 {
499     static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
500
501     IO_STATUS_BLOCK iosb;
502     FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
503     HANDLE hPipeSrv, hPipeClt;
504     NTSTATUS res;
505
506     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
507     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
508     if (hPipeSrv != INVALID_HANDLE_VALUE )
509     {
510         hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
511         ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
512         if (hPipeClt != INVALID_HANDLE_VALUE)
513         {
514             res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
515             ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
516             CloseHandle(hPipeClt);
517         }
518         CloseHandle( hPipeSrv );
519     }
520
521     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
522     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
523     if (hPipeSrv == INVALID_HANDLE_VALUE )
524         return;
525
526     hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
527     ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
528     if (hPipeClt != INVALID_HANDLE_VALUE)
529     {
530         OVERLAPPED o = {0,};
531         BYTE buf[3];
532         DWORD read;
533         long count;
534
535         NTSTATUS res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
536         ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
537         ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
538
539         count = get_pending_msgs(h);
540         ok( !count, "Unexpected msg count: %ld\n", count );
541         ReadFile( hPipeSrv, buf, 3, &read, &o);
542         count = get_pending_msgs(h);
543         ok( !count, "Unexpected msg count: %ld\n", count );
544         WriteFile( hPipeClt, buf, 3, &read, NULL );
545
546         if (get_msg(h))
547         {
548             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
549             ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
550             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
551             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
552         }
553         count = get_pending_msgs(h);
554         ok( !count, "Unexpected msg count: %ld\n", count );
555
556         WriteFile( hPipeClt, buf, 2, &read, NULL );
557         count = get_pending_msgs(h);
558         ok( !count, "Unexpected msg count: %ld\n", count );
559         ReadFile( hPipeSrv, buf, 2, &read, &o);
560         count = get_pending_msgs(h);
561         ok( count == 1, "Unexpected msg count: %ld\n", count );
562         if (get_msg(h))
563         {
564             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
565             ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
566             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
567             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
568         }
569
570         ReadFile( hPipeSrv, buf, sizeof(buf), &read, &o);
571         CloseHandle( hPipeSrv );
572         count = get_pending_msgs(h);
573         ok( count == 1, "Unexpected msg count: %ld\n", count );
574         if (get_msg(h))
575         {
576             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
577             ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
578             /* wine sends wrong status here */
579             todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
580             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
581         }
582     }
583
584     CloseHandle( hPipeClt );
585 }
586
587 static void test_iocompletion(void)
588 {
589     HANDLE h = INVALID_HANDLE_VALUE;
590     NTSTATUS res;
591
592     res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
593
594     ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
595     ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
596
597     if ( h && h != INVALID_HANDLE_VALUE)
598     {
599         test_iocp_setcompletion(h);
600         test_iocp_fileio(h);
601         pNtClose(h);
602     }
603 }
604
605 START_TEST(file)
606 {
607     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
608     if (!hntdll)
609     {
610         skip("not running on NT, skipping test\n");
611         return;
612     }
613
614     pRtlFreeUnicodeString   = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
615     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
616     pNtCreateMailslotFile   = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
617     pNtReadFile             = (void *)GetProcAddress(hntdll, "NtReadFile");
618     pNtWriteFile            = (void *)GetProcAddress(hntdll, "NtWriteFile");
619     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
620     pNtCreateIoCompletion   = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
621     pNtOpenIoCompletion     = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
622     pNtQueryIoCompletion    = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
623     pNtRemoveIoCompletion   = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
624     pNtSetIoCompletion      = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
625     pNtSetInformationFile   = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
626
627     read_file_test();
628     nt_mailslot_test();
629     test_iocompletion();
630 }