ntdll/tests: Free memory after use.
[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 NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
43                                        ULONG, ULONG, ULONG, PLARGE_INTEGER );
44 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
45                                       PIO_APC_ROUTINE apc, void* apc_user,
46                                       PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
47                                       PLARGE_INTEGER offset, PULONG key);
48 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
49                                        PIO_APC_ROUTINE apc, void* apc_user,
50                                        PIO_STATUS_BLOCK io_status,
51                                        const void* buffer, ULONG length,
52                                        PLARGE_INTEGER offset, PULONG key);
53 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
54
55 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
56 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
57 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
58 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
59 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
60 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
61
62 static inline BOOL is_signaled( HANDLE obj )
63 {
64     return WaitForSingleObject( obj, 0 ) == 0;
65 }
66
67 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
68 #define TEST_BUF_LEN 3
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     CreateOptions = MailslotQuota = MaxMessageSize = 0;
407     DesiredAccess = GENERIC_READ;
408
409     /*
410      * Check for NULL pointer handling
411      */
412     rc = pNtCreateMailslotFile(NULL, DesiredAccess,
413          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
414          &TimeOut);
415     ok( rc == STATUS_ACCESS_VIOLATION ||
416         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
417         "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
418
419     /*
420      * Test to see if the Timeout can be NULL
421      */
422     hslot = (HANDLE)0xdeadbeef;
423     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
424          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
425          NULL);
426     ok( rc == STATUS_SUCCESS ||
427         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
428         "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
429     ok( hslot != 0, "Handle is invalid\n");
430
431     if  ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
432
433     /*
434      * Test that the length field is checked properly
435      */
436     attr.Length = 0;
437     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
438          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
439          &TimeOut);
440     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
441
442     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
443
444     attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
445     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
446          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
447          &TimeOut);
448     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
449
450     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
451
452     /*
453      * Test handling of a NULL unicode string in ObjectName
454      */
455     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
456     attr.ObjectName = NULL;
457     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
458          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
459          &TimeOut);
460     ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
461         rc == STATUS_INVALID_PARAMETER,
462         "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
463
464     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
465
466     /*
467      * Test a valid call
468      */
469     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
470     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
471          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
472          &TimeOut);
473     ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
474     ok( hslot != 0, "Handle is invalid\n");
475
476     rc = pNtClose(hslot);
477     ok( rc == STATUS_SUCCESS, "NtClose failed\n");
478 }
479
480 static void test_iocp_setcompletion(HANDLE h)
481 {
482     NTSTATUS res;
483     long count;
484
485     res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
486     ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
487
488     count = get_pending_msgs(h);
489     ok( count == 1, "Unexpected msg count: %ld\n", count );
490
491     if (get_msg(h))
492     {
493         ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
494         ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
495         ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
496         ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
497     }
498
499     count = get_pending_msgs(h);
500     ok( !count, "Unexpected msg count: %ld\n", count );
501 }
502
503 static void test_iocp_fileio(HANDLE h)
504 {
505     static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
506
507     IO_STATUS_BLOCK iosb;
508     FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
509     HANDLE hPipeSrv, hPipeClt;
510     NTSTATUS res;
511
512     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
513     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
514     if (hPipeSrv != INVALID_HANDLE_VALUE )
515     {
516         hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
517         ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
518         if (hPipeClt != INVALID_HANDLE_VALUE)
519         {
520             res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
521             ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
522             CloseHandle(hPipeClt);
523         }
524         CloseHandle( hPipeSrv );
525     }
526
527     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
528     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
529     if (hPipeSrv == INVALID_HANDLE_VALUE )
530         return;
531
532     hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
533     ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
534     if (hPipeClt != INVALID_HANDLE_VALUE)
535     {
536         OVERLAPPED o = {0,};
537         BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
538         DWORD read;
539         long count;
540
541         NTSTATUS res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
542         ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
543         ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
544
545         memset( send_buf, 0, TEST_BUF_LEN );
546         memset( recv_buf, 0xde, TEST_BUF_LEN );
547         count = get_pending_msgs(h);
548         ok( !count, "Unexpected msg count: %ld\n", count );
549         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
550         count = get_pending_msgs(h);
551         ok( !count, "Unexpected msg count: %ld\n", count );
552         WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
553
554         if (get_msg(h))
555         {
556             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
557             ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
558             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
559             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
560             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] );
561         }
562         count = get_pending_msgs(h);
563         ok( !count, "Unexpected msg count: %ld\n", count );
564
565         memset( send_buf, 0, TEST_BUF_LEN );
566         memset( recv_buf, 0xde, TEST_BUF_LEN );
567         WriteFile( hPipeClt, send_buf, 2, &read, NULL );
568         count = get_pending_msgs(h);
569         ok( !count, "Unexpected msg count: %ld\n", count );
570         ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
571         count = get_pending_msgs(h);
572         ok( count == 1, "Unexpected msg count: %ld\n", count );
573         if (get_msg(h))
574         {
575             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
576             ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
577             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
578             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
579             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] );
580         }
581
582         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
583         CloseHandle( hPipeSrv );
584         count = get_pending_msgs(h);
585         ok( count == 1, "Unexpected msg count: %ld\n", count );
586         if (get_msg(h))
587         {
588             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
589             ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
590             /* wine sends wrong status here */
591             todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
592             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
593         }
594     }
595
596     CloseHandle( hPipeClt );
597 }
598
599 static void test_iocompletion(void)
600 {
601     HANDLE h = INVALID_HANDLE_VALUE;
602     NTSTATUS res;
603
604     res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
605
606     ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
607     ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
608
609     if ( h && h != INVALID_HANDLE_VALUE)
610     {
611         test_iocp_setcompletion(h);
612         test_iocp_fileio(h);
613         pNtClose(h);
614     }
615 }
616
617 START_TEST(file)
618 {
619     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
620     if (!hntdll)
621     {
622         skip("not running on NT, skipping test\n");
623         return;
624     }
625
626     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
627     pNtCreateMailslotFile   = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
628     pNtReadFile             = (void *)GetProcAddress(hntdll, "NtReadFile");
629     pNtWriteFile            = (void *)GetProcAddress(hntdll, "NtWriteFile");
630     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
631     pNtCreateIoCompletion   = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
632     pNtOpenIoCompletion     = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
633     pNtQueryIoCompletion    = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
634     pNtRemoveIoCompletion   = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
635     pNtSetIoCompletion      = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
636     pNtSetInformationFile   = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
637
638     read_file_test();
639     nt_mailslot_test();
640     test_iocompletion();
641 }