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