crypt32: Make sure we show Unicode characters (Dutch translation).
[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
38 #ifndef IO_COMPLETION_ALL_ACCESS
39 #define IO_COMPLETION_ALL_ACCESS 0x001F0003
40 #endif
41
42 static NTSTATUS (WINAPI *pRtlFreeUnicodeString)( PUNICODE_STRING );
43 static VOID     (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
44 static BOOL     (WINAPI *pRtlDosPathNameToNtPathName_U)( LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR* );
45 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
46                                        ULONG, ULONG, ULONG, PLARGE_INTEGER );
47 static NTSTATUS (WINAPI *pNtDeleteFile)(POBJECT_ATTRIBUTES ObjectAttributes);
48 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
49                                       PIO_APC_ROUTINE apc, void* apc_user,
50                                       PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
51                                       PLARGE_INTEGER offset, PULONG key);
52 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
53                                        PIO_APC_ROUTINE apc, void* apc_user,
54                                        PIO_STATUS_BLOCK io_status,
55                                        const void* buffer, ULONG length,
56                                        PLARGE_INTEGER offset, PULONG key);
57 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
58
59 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
60 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
61 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
62 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
63 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
64 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
65
66 static inline BOOL is_signaled( HANDLE obj )
67 {
68     return WaitForSingleObject( obj, 0 ) == 0;
69 }
70
71 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
72 #define TEST_BUF_LEN 3
73
74 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
75 {
76     *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
77                             1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
78     ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
79
80     *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
81     ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
82
83     return TRUE;
84 }
85
86 static HANDLE create_temp_file( ULONG flags )
87 {
88     char buffer[MAX_PATH];
89     HANDLE handle;
90
91     GetTempFileNameA( ".", "foo", 0, buffer );
92     handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
93                          flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
94     ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
95     return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
96 }
97
98 #define CVALUE_FIRST 0xfffabbcc
99 #define CKEY_FIRST 0x1030341
100 #define CKEY_SECOND 0x132E46
101
102 ULONG_PTR completionKey;
103 IO_STATUS_BLOCK ioSb;
104 ULONG_PTR completionValue;
105
106 static long get_pending_msgs(HANDLE h)
107 {
108     NTSTATUS res;
109     ULONG a, req;
110
111     res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, &a, sizeof(a), &req );
112     ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
113     if (res != STATUS_SUCCESS) return -1;
114     ok( req == sizeof(a), "Unexpected response size: %x\n", req );
115     return a;
116 }
117
118 static BOOL get_msg(HANDLE h)
119 {
120     LARGE_INTEGER timeout = {{-10000000*3}};
121     DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
122     ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
123     if (res != STATUS_SUCCESS)
124     {
125         completionKey = completionValue = 0;
126         memset(&ioSb, 0, sizeof(ioSb));
127         return FALSE;
128     }
129     return TRUE;
130 }
131
132
133 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
134 {
135     int *count = arg;
136
137     trace( "apc called block %p iosb.status %x iosb.info %lu\n",
138            iosb, U(*iosb).Status, iosb->Information );
139     (*count)++;
140     ok( !reserved, "reserved is not 0: %x\n", reserved );
141 }
142
143 static void delete_file_test(void)
144 {
145     NTSTATUS ret;
146     OBJECT_ATTRIBUTES attr;
147     UNICODE_STRING nameW;
148     WCHAR pathW[MAX_PATH];
149     WCHAR pathsubW[MAX_PATH];
150     static const WCHAR testdirW[] = {'n','t','d','e','l','e','t','e','f','i','l','e',0};
151     static const WCHAR subdirW[]  = {'\\','s','u','b',0};
152
153     ret = GetTempPathW(MAX_PATH, pathW);
154     if (!ret)
155     {
156         ok(0, "couldn't get temp dir\n");
157         return;
158     }
159     if (ret + sizeof(testdirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
160     {
161         ok(0, "MAX_PATH exceeded in constructing paths\n");
162         return;
163     }
164
165     lstrcatW(pathW, testdirW);
166     lstrcpyW(pathsubW, pathW);
167     lstrcatW(pathsubW, subdirW);
168
169     ret = CreateDirectoryW(pathW, NULL);
170     ok(ret == TRUE, "couldn't create directory ntdeletefile\n");
171     if (!pRtlDosPathNameToNtPathName_U(pathW, &nameW, NULL, NULL))
172     {
173         ok(0,"RtlDosPathNametoNtPathName_U failed\n");
174         return;
175     }
176
177     attr.Length = sizeof(attr);
178     attr.RootDirectory = 0;
179     attr.Attributes = OBJ_CASE_INSENSITIVE;
180     attr.ObjectName = &nameW;
181     attr.SecurityDescriptor = NULL;
182     attr.SecurityQualityOfService = NULL;
183
184     /* test NtDeleteFile on an empty directory */
185     ret = pNtDeleteFile(&attr);
186     ok(ret == STATUS_SUCCESS, "NtDeleteFile should succeed in removing an empty directory\n");
187     ret = RemoveDirectoryW(pathW);
188     ok(ret == FALSE, "expected to fail removing directory, NtDeleteFile should have removed it\n");
189
190     /* test NtDeleteFile on a non-empty directory */
191     ret = CreateDirectoryW(pathW, NULL);
192     ok(ret == TRUE, "couldn't create directory ntdeletefile ?!\n");
193     ret = CreateDirectoryW(pathsubW, NULL);
194     ok(ret == TRUE, "couldn't create directory subdir\n");
195     ret = pNtDeleteFile(&attr);
196     ok(ret == STATUS_SUCCESS, "expected NtDeleteFile to ret STATUS_SUCCESS\n");
197     ret = RemoveDirectoryW(pathsubW);
198     ok(ret == TRUE, "expected to remove directory ntdeletefile\\sub\n");
199     ret = RemoveDirectoryW(pathW);
200     ok(ret == TRUE, "expected to remove directory ntdeletefile, NtDeleteFile failed.\n");
201
202     pRtlFreeUnicodeString( &nameW );
203 }
204
205 static void read_file_test(void)
206 {
207     const char text[] = "foobar";
208     HANDLE handle, read, write;
209     NTSTATUS status;
210     IO_STATUS_BLOCK iosb;
211     DWORD written;
212     int apc_count = 0;
213     char buffer[128];
214     LARGE_INTEGER offset;
215     HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
216
217     buffer[0] = 1;
218
219     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
220
221     /* try read with no data */
222     U(iosb).Status = 0xdeadbabe;
223     iosb.Information = 0xdeadbeef;
224     ok( is_signaled( read ), "read handle is not signaled\n" );
225     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
226     ok( status == STATUS_PENDING, "wrong status %x\n", status );
227     ok( !is_signaled( read ), "read handle is signaled\n" );
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     /* iosb updated here by async i/o */
234     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
235     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
236     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
237     ok( !is_signaled( read ), "read handle is signaled\n" );
238     ok( is_signaled( event ), "event is not signaled\n" );
239     ok( !apc_count, "apc was called\n" );
240     apc_count = 0;
241     SleepEx( 1, FALSE ); /* non-alertable sleep */
242     ok( !apc_count, "apc was called\n" );
243     SleepEx( 1, TRUE ); /* alertable sleep */
244     ok( apc_count == 1, "apc not called\n" );
245
246     /* with no event, the pipe handle itself gets signaled */
247     apc_count = 0;
248     U(iosb).Status = 0xdeadbabe;
249     iosb.Information = 0xdeadbeef;
250     ok( !is_signaled( read ), "read handle is not signaled\n" );
251     status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
252     ok( status == STATUS_PENDING, "wrong status %x\n", status );
253     ok( !is_signaled( read ), "read handle is signaled\n" );
254     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
255     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
256     ok( !apc_count, "apc was called\n" );
257     WriteFile( write, buffer, 1, &written, NULL );
258     /* iosb updated here by async i/o */
259     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
260     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
261     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
262     ok( is_signaled( read ), "read handle is signaled\n" );
263     ok( !apc_count, "apc was called\n" );
264     apc_count = 0;
265     SleepEx( 1, FALSE ); /* non-alertable sleep */
266     ok( !apc_count, "apc was called\n" );
267     SleepEx( 1, TRUE ); /* alertable sleep */
268     ok( apc_count == 1, "apc not called\n" );
269
270     /* now read with data ready */
271     apc_count = 0;
272     U(iosb).Status = 0xdeadbabe;
273     iosb.Information = 0xdeadbeef;
274     ResetEvent( event );
275     WriteFile( write, buffer, 1, &written, NULL );
276     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
277     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
278     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
279     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
280     ok( is_signaled( event ), "event is not signaled\n" );
281     ok( !apc_count, "apc was called\n" );
282     SleepEx( 1, FALSE ); /* non-alertable sleep */
283     ok( !apc_count, "apc was called\n" );
284     SleepEx( 1, TRUE ); /* alertable sleep */
285     ok( apc_count == 1, "apc not called\n" );
286
287     /* try read with no data */
288     apc_count = 0;
289     U(iosb).Status = 0xdeadbabe;
290     iosb.Information = 0xdeadbeef;
291     ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
292     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
293     ok( status == STATUS_PENDING, "wrong status %x\n", status );
294     ok( !is_signaled( event ), "event is signaled\n" );
295     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
296     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
297     ok( !apc_count, "apc was called\n" );
298     WriteFile( write, buffer, 1, &written, NULL );
299     /* partial read is good enough */
300     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
301     ok( is_signaled( event ), "event is signaled\n" );
302     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
303     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
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     /* read from disconnected pipe */
309     apc_count = 0;
310     U(iosb).Status = 0xdeadbabe;
311     iosb.Information = 0xdeadbeef;
312     CloseHandle( write );
313     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
314     ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
315     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
316     ok( iosb.Information == 0xdeadbeef, "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, "apc was called\n" );
321     CloseHandle( read );
322
323     /* read from closed handle */
324     apc_count = 0;
325     U(iosb).Status = 0xdeadbabe;
326     iosb.Information = 0xdeadbeef;
327     SetEvent( event );
328     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
329     ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
330     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
331     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
332     ok( is_signaled( event ), "event is signaled\n" );  /* not reset on invalid handle */
333     ok( !apc_count, "apc was called\n" );
334     SleepEx( 1, TRUE ); /* alertable sleep */
335     ok( !apc_count, "apc was called\n" );
336
337     /* disconnect while async read is in progress */
338     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
339     apc_count = 0;
340     U(iosb).Status = 0xdeadbabe;
341     iosb.Information = 0xdeadbeef;
342     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
343     ok( status == STATUS_PENDING, "wrong status %x\n", status );
344     ok( !is_signaled( event ), "event is signaled\n" );
345     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
346     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
347     ok( !apc_count, "apc was called\n" );
348     CloseHandle( write );
349     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
350     ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
351     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
352     ok( is_signaled( event ), "event is signaled\n" );
353     ok( !apc_count, "apc was called\n" );
354     SleepEx( 1, TRUE ); /* alertable sleep */
355     ok( apc_count == 1, "apc was not called\n" );
356     CloseHandle( read );
357
358     /* now try a real file */
359     if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
360     apc_count = 0;
361     U(iosb).Status = 0xdeadbabe;
362     iosb.Information = 0xdeadbeef;
363     offset.QuadPart = 0;
364     ResetEvent( event );
365     pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
366     ok( status == STATUS_PENDING, "wrong status %x\n", status );
367     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
368     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
369     ok( is_signaled( event ), "event is signaled\n" );
370     ok( !apc_count, "apc was called\n" );
371     SleepEx( 1, TRUE ); /* alertable sleep */
372     ok( apc_count == 1, "apc was not called\n" );
373
374     apc_count = 0;
375     U(iosb).Status = 0xdeadbabe;
376     iosb.Information = 0xdeadbeef;
377     offset.QuadPart = 0;
378     ResetEvent( event );
379     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
380     ok( status == STATUS_SUCCESS ||
381         status == STATUS_PENDING, /* vista */
382         "wrong status %x\n", status );
383     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
384     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
385     ok( is_signaled( event ), "event is signaled\n" );
386     ok( !apc_count, "apc was called\n" );
387     SleepEx( 1, TRUE ); /* alertable sleep */
388     ok( apc_count == 1, "apc was not called\n" );
389
390     /* read beyond eof */
391     apc_count = 0;
392     U(iosb).Status = 0xdeadbabe;
393     iosb.Information = 0xdeadbeef;
394     offset.QuadPart = strlen(text) + 2;
395     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
396     if (status == STATUS_PENDING)  /* vista */
397     {
398         ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
399         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
400         ok( is_signaled( event ), "event is signaled\n" );
401         ok( !apc_count, "apc was called\n" );
402         SleepEx( 1, TRUE ); /* alertable sleep */
403         ok( apc_count == 1, "apc was not called\n" );
404     }
405     else
406     {
407         ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
408         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
409         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
410         ok( !is_signaled( event ), "event is signaled\n" );
411         ok( !apc_count, "apc was called\n" );
412         SleepEx( 1, TRUE ); /* alertable sleep */
413         ok( !apc_count, "apc was called\n" );
414     }
415     CloseHandle( handle );
416
417     /* now a non-overlapped file */
418     if (!(handle = create_temp_file(0))) return;
419     apc_count = 0;
420     U(iosb).Status = 0xdeadbabe;
421     iosb.Information = 0xdeadbeef;
422     offset.QuadPart = 0;
423     pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
424     ok( status == STATUS_END_OF_FILE ||
425         status == STATUS_PENDING,  /* vista */
426         "wrong status %x\n", status );
427     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
428     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
429     ok( is_signaled( event ), "event is signaled\n" );
430     ok( !apc_count, "apc was called\n" );
431     SleepEx( 1, TRUE ); /* alertable sleep */
432     ok( apc_count == 1, "apc was not called\n" );
433
434     apc_count = 0;
435     U(iosb).Status = 0xdeadbabe;
436     iosb.Information = 0xdeadbeef;
437     offset.QuadPart = 0;
438     ResetEvent( event );
439     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
440     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
441     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
442     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
443     ok( is_signaled( event ), "event is signaled\n" );
444     ok( !apc_count, "apc was called\n" );
445     SleepEx( 1, TRUE ); /* alertable sleep */
446     todo_wine ok( !apc_count, "apc was called\n" );
447
448     /* read beyond eof */
449     apc_count = 0;
450     U(iosb).Status = 0xdeadbabe;
451     iosb.Information = 0xdeadbeef;
452     offset.QuadPart = strlen(text) + 2;
453     ResetEvent( event );
454     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
455     ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
456     todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
457     todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
458     todo_wine ok( is_signaled( event ), "event is not signaled\n" );
459     ok( !apc_count, "apc was called\n" );
460     SleepEx( 1, TRUE ); /* alertable sleep */
461     ok( !apc_count, "apc was called\n" );
462
463     CloseHandle( handle );
464
465     CloseHandle( event );
466 }
467
468 static void nt_mailslot_test(void)
469 {
470     HANDLE hslot;
471     ACCESS_MASK DesiredAccess;
472     OBJECT_ATTRIBUTES attr;
473
474     ULONG CreateOptions;
475     ULONG MailslotQuota;
476     ULONG MaxMessageSize;
477     LARGE_INTEGER TimeOut;
478     IO_STATUS_BLOCK IoStatusBlock;
479     NTSTATUS rc;
480     UNICODE_STRING str;
481     WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
482                         'R',':','\\','F','R','E','D','\0' };
483
484     TimeOut.QuadPart = -1;
485
486     pRtlInitUnicodeString(&str, buffer1);
487     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
488     CreateOptions = MailslotQuota = MaxMessageSize = 0;
489     DesiredAccess = GENERIC_READ;
490
491     /*
492      * Check for NULL pointer handling
493      */
494     rc = pNtCreateMailslotFile(NULL, DesiredAccess,
495          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
496          &TimeOut);
497     ok( rc == STATUS_ACCESS_VIOLATION ||
498         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
499         "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
500
501     /*
502      * Test to see if the Timeout can be NULL
503      */
504     hslot = (HANDLE)0xdeadbeef;
505     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
506          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
507          NULL);
508     ok( rc == STATUS_SUCCESS ||
509         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
510         "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
511     ok( hslot != 0, "Handle is invalid\n");
512
513     if  ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
514
515     /*
516      * Test that the length field is checked properly
517      */
518     attr.Length = 0;
519     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
520          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
521          &TimeOut);
522     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
523
524     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
525
526     attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
527     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
528          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
529          &TimeOut);
530     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
531
532     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
533
534     /*
535      * Test handling of a NULL unicode string in ObjectName
536      */
537     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
538     attr.ObjectName = NULL;
539     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
540          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
541          &TimeOut);
542     ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
543         rc == STATUS_INVALID_PARAMETER,
544         "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
545
546     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
547
548     /*
549      * Test a valid call
550      */
551     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
552     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
553          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
554          &TimeOut);
555     ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
556     ok( hslot != 0, "Handle is invalid\n");
557
558     rc = pNtClose(hslot);
559     ok( rc == STATUS_SUCCESS, "NtClose failed\n");
560 }
561
562 static void test_iocp_setcompletion(HANDLE h)
563 {
564     NTSTATUS res;
565     long count;
566
567     res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
568     ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
569
570     count = get_pending_msgs(h);
571     ok( count == 1, "Unexpected msg count: %ld\n", count );
572
573     if (get_msg(h))
574     {
575         ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
576         ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
577         ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
578         ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
579     }
580
581     count = get_pending_msgs(h);
582     ok( !count, "Unexpected msg count: %ld\n", count );
583 }
584
585 static void test_iocp_fileio(HANDLE h)
586 {
587     static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
588
589     IO_STATUS_BLOCK iosb;
590     FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
591     HANDLE hPipeSrv, hPipeClt;
592     NTSTATUS res;
593
594     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
595     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
596     if (hPipeSrv != INVALID_HANDLE_VALUE )
597     {
598         hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
599         ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
600         if (hPipeClt != INVALID_HANDLE_VALUE)
601         {
602             res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
603             ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
604             CloseHandle(hPipeClt);
605         }
606         CloseHandle( hPipeSrv );
607     }
608
609     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
610     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
611     if (hPipeSrv == INVALID_HANDLE_VALUE )
612         return;
613
614     hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
615     ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
616     if (hPipeClt != INVALID_HANDLE_VALUE)
617     {
618         OVERLAPPED o = {0,};
619         BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
620         DWORD read;
621         long count;
622
623         NTSTATUS res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
624         ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
625         ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
626
627         memset( send_buf, 0, TEST_BUF_LEN );
628         memset( recv_buf, 0xde, TEST_BUF_LEN );
629         count = get_pending_msgs(h);
630         ok( !count, "Unexpected msg count: %ld\n", count );
631         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
632         count = get_pending_msgs(h);
633         ok( !count, "Unexpected msg count: %ld\n", count );
634         WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
635
636         if (get_msg(h))
637         {
638             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
639             ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
640             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
641             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
642             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] );
643         }
644         count = get_pending_msgs(h);
645         ok( !count, "Unexpected msg count: %ld\n", count );
646
647         memset( send_buf, 0, TEST_BUF_LEN );
648         memset( recv_buf, 0xde, TEST_BUF_LEN );
649         WriteFile( hPipeClt, send_buf, 2, &read, NULL );
650         count = get_pending_msgs(h);
651         ok( !count, "Unexpected msg count: %ld\n", count );
652         ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
653         count = get_pending_msgs(h);
654         ok( count == 1, "Unexpected msg count: %ld\n", count );
655         if (get_msg(h))
656         {
657             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
658             ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
659             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
660             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
661             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] );
662         }
663
664         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
665         CloseHandle( hPipeSrv );
666         count = get_pending_msgs(h);
667         ok( count == 1, "Unexpected msg count: %ld\n", count );
668         if (get_msg(h))
669         {
670             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
671             ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
672             /* wine sends wrong status here */
673             todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
674             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
675         }
676     }
677
678     CloseHandle( hPipeClt );
679 }
680
681 static void test_iocompletion(void)
682 {
683     HANDLE h = INVALID_HANDLE_VALUE;
684     NTSTATUS res;
685
686     res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
687
688     ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
689     ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
690
691     if ( h && h != INVALID_HANDLE_VALUE)
692     {
693         test_iocp_setcompletion(h);
694         test_iocp_fileio(h);
695         pNtClose(h);
696     }
697 }
698
699 START_TEST(file)
700 {
701     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
702     if (!hntdll)
703     {
704         skip("not running on NT, skipping test\n");
705         return;
706     }
707
708     pRtlFreeUnicodeString   = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
709     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
710     pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
711     pNtCreateMailslotFile   = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
712     pNtDeleteFile           = (void *)GetProcAddress(hntdll, "NtDeleteFile");
713     pNtReadFile             = (void *)GetProcAddress(hntdll, "NtReadFile");
714     pNtWriteFile            = (void *)GetProcAddress(hntdll, "NtWriteFile");
715     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
716     pNtCreateIoCompletion   = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
717     pNtOpenIoCompletion     = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
718     pNtQueryIoCompletion    = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
719     pNtRemoveIoCompletion   = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
720     pNtSetIoCompletion      = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
721     pNtSetInformationFile   = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
722
723     delete_file_test();
724     read_file_test();
725     nt_mailslot_test();
726     test_iocompletion();
727 }