windowscodecs: Handle TIFF's with RowsPerStrip greater than Height.
[wine] / dlls / ntdll / tests / file.c
1 /* Unit test suite for Ntdll file functions
2  *
3  * Copyright 2007 Jeff Latimer
4  * Copyright 2007 Andrey Turkin
5  * Copyright 2008 Jeff Zaroyko
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES
22  * We use function pointers here as there is no import library for NTDLL on
23  * windows.
24  */
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include "ntstatus.h"
30 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
31  * definition errors when we get to winnt.h
32  */
33 #define WIN32_NO_STATUS
34
35 #include "wine/test.h"
36 #include "winternl.h"
37 #include "winuser.h"
38
39 #ifndef IO_COMPLETION_ALL_ACCESS
40 #define IO_COMPLETION_ALL_ACCESS 0x001F0003
41 #endif
42
43 static BOOL     (WINAPI * pGetVolumePathNameW)(LPCWSTR, LPWSTR, DWORD);
44 static UINT     (WINAPI *pGetSystemWow64DirectoryW)( LPWSTR, UINT );
45
46 static VOID     (WINAPI *pRtlFreeUnicodeString)( PUNICODE_STRING );
47 static VOID     (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
48 static BOOL     (WINAPI *pRtlDosPathNameToNtPathName_U)( LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR* );
49 static NTSTATUS (WINAPI *pRtlWow64EnableFsRedirectionEx)( ULONG, ULONG * );
50
51 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
52                                        ULONG, ULONG, ULONG, PLARGE_INTEGER );
53 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,PLARGE_INTEGER,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG);
54 static NTSTATUS (WINAPI *pNtOpenFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,ULONG,ULONG);
55 static NTSTATUS (WINAPI *pNtDeleteFile)(POBJECT_ATTRIBUTES ObjectAttributes);
56 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
57                                       PIO_APC_ROUTINE apc, void* apc_user,
58                                       PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
59                                       PLARGE_INTEGER offset, PULONG key);
60 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
61                                        PIO_APC_ROUTINE apc, void* apc_user,
62                                        PIO_STATUS_BLOCK io_status,
63                                        const void* buffer, ULONG length,
64                                        PLARGE_INTEGER offset, PULONG key);
65 static NTSTATUS (WINAPI *pNtCancelIoFile)(HANDLE hFile, PIO_STATUS_BLOCK io_status);
66 static NTSTATUS (WINAPI *pNtCancelIoFileEx)(HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status);
67 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
68
69 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
70 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
71 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
72 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
73 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, ULONG);
74 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
75 static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
76 static NTSTATUS (WINAPI *pNtQueryDirectoryFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,
77                                                 PVOID,ULONG,FILE_INFORMATION_CLASS,BOOLEAN,PUNICODE_STRING,BOOLEAN);
78
79 static inline BOOL is_signaled( HANDLE obj )
80 {
81     return WaitForSingleObject( obj, 0 ) == 0;
82 }
83
84 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
85 #define TEST_BUF_LEN 3
86
87 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
88 {
89     *read = CreateNamedPipe(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
90                             1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
91     ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
92
93     *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
94     ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
95
96     return TRUE;
97 }
98
99 static HANDLE create_temp_file( ULONG flags )
100 {
101     char buffer[MAX_PATH];
102     HANDLE handle;
103
104     GetTempFileNameA( ".", "foo", 0, buffer );
105     handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
106                          flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
107     ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
108     return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
109 }
110
111 #define CVALUE_FIRST 0xfffabbcc
112 #define CKEY_FIRST 0x1030341
113 #define CKEY_SECOND 0x132E46
114
115 ULONG_PTR completionKey;
116 IO_STATUS_BLOCK ioSb;
117 ULONG_PTR completionValue;
118
119 static ULONG get_pending_msgs(HANDLE h)
120 {
121     NTSTATUS res;
122     ULONG a, req;
123
124     res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, &a, sizeof(a), &req );
125     ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
126     if (res != STATUS_SUCCESS) return -1;
127     ok( req == sizeof(a), "Unexpected response size: %x\n", req );
128     return a;
129 }
130
131 static BOOL get_msg(HANDLE h)
132 {
133     LARGE_INTEGER timeout = {{-10000000*3}};
134     DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
135     ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
136     if (res != STATUS_SUCCESS)
137     {
138         completionKey = completionValue = 0;
139         memset(&ioSb, 0, sizeof(ioSb));
140         return FALSE;
141     }
142     return TRUE;
143 }
144
145
146 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
147 {
148     int *count = arg;
149
150     trace( "apc called block %p iosb.status %x iosb.info %lu\n",
151            iosb, U(*iosb).Status, iosb->Information );
152     (*count)++;
153     ok( !reserved, "reserved is not 0: %x\n", reserved );
154 }
155
156 static void create_file_test(void)
157 {
158     static const WCHAR systemrootW[] = {'\\','S','y','s','t','e','m','R','o','o','t',
159                                         '\\','f','a','i','l','i','n','g',0};
160     NTSTATUS status;
161     HANDLE dir;
162     WCHAR path[MAX_PATH];
163     OBJECT_ATTRIBUTES attr;
164     IO_STATUS_BLOCK io;
165     UNICODE_STRING nameW;
166
167     GetCurrentDirectoryW( MAX_PATH, path );
168     pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
169     attr.Length = sizeof(attr);
170     attr.RootDirectory = 0;
171     attr.ObjectName = &nameW;
172     attr.Attributes = OBJ_CASE_INSENSITIVE;
173     attr.SecurityDescriptor = NULL;
174     attr.SecurityQualityOfService = NULL;
175
176     /* try various open modes and options on directories */
177     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
178                             FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0 );
179     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
180     CloseHandle( dir );
181
182     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
183                             FILE_CREATE, FILE_DIRECTORY_FILE, NULL, 0 );
184     ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
185         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
186
187     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
188                             FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0 );
189     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
190     CloseHandle( dir );
191
192     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
193                             FILE_SUPERSEDE, FILE_DIRECTORY_FILE, NULL, 0 );
194     ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
195
196     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
197                             FILE_OVERWRITE, FILE_DIRECTORY_FILE, NULL, 0 );
198     ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
199
200     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
201                             FILE_OVERWRITE_IF, FILE_DIRECTORY_FILE, NULL, 0 );
202     ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
203
204     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
205                             FILE_OPEN, 0, NULL, 0 );
206     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
207     CloseHandle( dir );
208
209     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
210                             FILE_CREATE, 0, NULL, 0 );
211     ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
212         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
213
214     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
215                             FILE_OPEN_IF, 0, NULL, 0 );
216     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
217     CloseHandle( dir );
218
219     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
220                             FILE_SUPERSEDE, 0, NULL, 0 );
221     ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
222         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
223
224     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
225                             FILE_OVERWRITE, 0, NULL, 0 );
226     ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
227         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
228
229     status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
230                             FILE_OVERWRITE_IF, 0, NULL, 0 );
231     ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
232         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
233
234     pRtlFreeUnicodeString( &nameW );
235
236     pRtlInitUnicodeString( &nameW, systemrootW );
237     attr.Length = sizeof(attr);
238     attr.RootDirectory = NULL;
239     attr.ObjectName = &nameW;
240     attr.Attributes = OBJ_CASE_INSENSITIVE;
241     attr.SecurityDescriptor = NULL;
242     attr.SecurityQualityOfService = NULL;
243     dir = NULL;
244     status = pNtCreateFile( &dir, FILE_APPEND_DATA, &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
245                             FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
246     todo_wine
247     ok( status == STATUS_INVALID_PARAMETER,
248         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
249 }
250
251 static void open_file_test(void)
252 {
253     NTSTATUS status;
254     HANDLE dir, root, handle;
255     WCHAR path[MAX_PATH];
256     BYTE data[1024];
257     OBJECT_ATTRIBUTES attr;
258     IO_STATUS_BLOCK io;
259     UNICODE_STRING nameW;
260     UINT i, len;
261     BOOL restart = TRUE;
262
263     len = GetWindowsDirectoryW( path, MAX_PATH );
264     pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
265     attr.Length = sizeof(attr);
266     attr.RootDirectory = 0;
267     attr.ObjectName = &nameW;
268     attr.Attributes = OBJ_CASE_INSENSITIVE;
269     attr.SecurityDescriptor = NULL;
270     attr.SecurityQualityOfService = NULL;
271     status = pNtOpenFile( &dir, SYNCHRONIZE|FILE_LIST_DIRECTORY, &attr, &io,
272                           FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT );
273     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
274     pRtlFreeUnicodeString( &nameW );
275
276     path[3] = 0;  /* root of the drive */
277     pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
278     status = pNtOpenFile( &root, GENERIC_READ, &attr, &io,
279                           FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
280     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
281     pRtlFreeUnicodeString( &nameW );
282
283     /* test opening system dir with RootDirectory set to windows dir */
284     GetSystemDirectoryW( path, MAX_PATH );
285     while (path[len] == '\\') len++;
286     nameW.Buffer = path + len;
287     nameW.Length = lstrlenW(path + len) * sizeof(WCHAR);
288     attr.RootDirectory = dir;
289     status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
290                           FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
291     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
292     CloseHandle( handle );
293
294     /* try uppercase name */
295     for (i = len; path[i]; i++) if (path[i] >= 'a' && path[i] <= 'z') path[i] -= 'a' - 'A';
296     status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
297                           FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
298     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
299     CloseHandle( handle );
300
301     /* try with leading backslash */
302     nameW.Buffer--;
303     nameW.Length += sizeof(WCHAR);
304     status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
305                           FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
306     ok( status == STATUS_INVALID_PARAMETER ||
307         status == STATUS_OBJECT_NAME_INVALID ||
308         status == STATUS_OBJECT_PATH_SYNTAX_BAD,
309         "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
310     if (!status) CloseHandle( handle );
311
312     /* try with empty name */
313     nameW.Length = 0;
314     status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
315                           FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
316     ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
317     CloseHandle( handle );
318
319     /* try open by file id */
320
321     while (!pNtQueryDirectoryFile( dir, NULL, NULL, NULL, &io, data, sizeof(data),
322                                    FileIdBothDirectoryInformation, TRUE, NULL, restart ))
323     {
324         FILE_ID_BOTH_DIRECTORY_INFORMATION *info = (FILE_ID_BOTH_DIRECTORY_INFORMATION *)data;
325
326         restart = FALSE;
327
328         if (!info->FileId.QuadPart) continue;
329
330         nameW.Buffer = (WCHAR *)&info->FileId;
331         nameW.Length = sizeof(info->FileId);
332         info->FileName[info->FileNameLength/sizeof(WCHAR)] = 0;
333         attr.RootDirectory = dir;
334         /* We skip 'open' files by not specifying FILE_SHARE_WRITE */
335         status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
336                               FILE_SHARE_READ,
337                               FILE_OPEN_BY_FILE_ID |
338                               ((info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FILE_DIRECTORY_FILE : 0) );
339         ok( status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED || status == STATUS_NOT_IMPLEMENTED || status == STATUS_SHARING_VIOLATION,
340             "open %s failed %x\n", wine_dbgstr_w(info->FileName), status );
341         if (status == STATUS_NOT_IMPLEMENTED)
342         {
343             win_skip( "FILE_OPEN_BY_FILE_ID not supported\n" );
344             break;
345         }
346         if (status == STATUS_SHARING_VIOLATION)
347             trace( "%s is currently open\n", wine_dbgstr_w(info->FileName) );
348         if (!status)
349         {
350             BYTE buf[sizeof(FILE_ALL_INFORMATION) + MAX_PATH * sizeof(WCHAR)];
351
352             if (!pNtQueryInformationFile( handle, &io, buf, sizeof(buf), FileAllInformation ))
353             {
354                 FILE_ALL_INFORMATION *fai = (FILE_ALL_INFORMATION *)buf;
355
356                 /* check that it's the same file/directory */
357
358                 /* don't check the size for directories */
359                 if (!(info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY))
360                     ok( info->EndOfFile.QuadPart == fai->StandardInformation.EndOfFile.QuadPart,
361                         "mismatched file size for %s\n", wine_dbgstr_w(info->FileName));
362
363                 ok( info->CreationTime.QuadPart == fai->BasicInformation.CreationTime.QuadPart,
364                     "mismatched creation time for %s\n", wine_dbgstr_w(info->FileName));
365             }
366             CloseHandle( handle );
367
368             /* try same thing from drive root */
369             attr.RootDirectory = root;
370             status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
371                                   FILE_SHARE_READ|FILE_SHARE_WRITE,
372                                   FILE_OPEN_BY_FILE_ID |
373                                   ((info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FILE_DIRECTORY_FILE : 0) );
374             ok( status == STATUS_SUCCESS || status == STATUS_NOT_IMPLEMENTED,
375                 "open %s failed %x\n", wine_dbgstr_w(info->FileName), status );
376             if (!status) CloseHandle( handle );
377         }
378     }
379
380     CloseHandle( dir );
381     CloseHandle( root );
382 }
383
384 static void delete_file_test(void)
385 {
386     NTSTATUS ret;
387     OBJECT_ATTRIBUTES attr;
388     UNICODE_STRING nameW;
389     WCHAR pathW[MAX_PATH];
390     WCHAR pathsubW[MAX_PATH];
391     static const WCHAR testdirW[] = {'n','t','d','e','l','e','t','e','f','i','l','e',0};
392     static const WCHAR subdirW[]  = {'\\','s','u','b',0};
393
394     ret = GetTempPathW(MAX_PATH, pathW);
395     if (!ret)
396     {
397         ok(0, "couldn't get temp dir\n");
398         return;
399     }
400     if (ret + sizeof(testdirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
401     {
402         ok(0, "MAX_PATH exceeded in constructing paths\n");
403         return;
404     }
405
406     lstrcatW(pathW, testdirW);
407     lstrcpyW(pathsubW, pathW);
408     lstrcatW(pathsubW, subdirW);
409
410     ret = CreateDirectoryW(pathW, NULL);
411     ok(ret == TRUE, "couldn't create directory ntdeletefile\n");
412     if (!pRtlDosPathNameToNtPathName_U(pathW, &nameW, NULL, NULL))
413     {
414         ok(0,"RtlDosPathNametoNtPathName_U failed\n");
415         return;
416     }
417
418     attr.Length = sizeof(attr);
419     attr.RootDirectory = 0;
420     attr.Attributes = OBJ_CASE_INSENSITIVE;
421     attr.ObjectName = &nameW;
422     attr.SecurityDescriptor = NULL;
423     attr.SecurityQualityOfService = NULL;
424
425     /* test NtDeleteFile on an empty directory */
426     ret = pNtDeleteFile(&attr);
427     ok(ret == STATUS_SUCCESS, "NtDeleteFile should succeed in removing an empty directory\n");
428     ret = RemoveDirectoryW(pathW);
429     ok(ret == FALSE, "expected to fail removing directory, NtDeleteFile should have removed it\n");
430
431     /* test NtDeleteFile on a non-empty directory */
432     ret = CreateDirectoryW(pathW, NULL);
433     ok(ret == TRUE, "couldn't create directory ntdeletefile ?!\n");
434     ret = CreateDirectoryW(pathsubW, NULL);
435     ok(ret == TRUE, "couldn't create directory subdir\n");
436     ret = pNtDeleteFile(&attr);
437     ok(ret == STATUS_SUCCESS, "expected NtDeleteFile to ret STATUS_SUCCESS\n");
438     ret = RemoveDirectoryW(pathsubW);
439     ok(ret == TRUE, "expected to remove directory ntdeletefile\\sub\n");
440     ret = RemoveDirectoryW(pathW);
441     ok(ret == TRUE, "expected to remove directory ntdeletefile, NtDeleteFile failed.\n");
442
443     pRtlFreeUnicodeString( &nameW );
444 }
445
446 static void read_file_test(void)
447 {
448     const char text[] = "foobar";
449     HANDLE handle, read, write;
450     NTSTATUS status;
451     IO_STATUS_BLOCK iosb, iosb2;
452     DWORD written;
453     int apc_count = 0;
454     char buffer[128];
455     LARGE_INTEGER offset;
456     HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
457
458     buffer[0] = 1;
459
460     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
461
462     /* try read with no data */
463     U(iosb).Status = 0xdeadbabe;
464     iosb.Information = 0xdeadbeef;
465     ok( is_signaled( read ), "read handle is not signaled\n" );
466     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
467     ok( status == STATUS_PENDING, "wrong status %x\n", status );
468     ok( !is_signaled( read ), "read handle is signaled\n" );
469     ok( !is_signaled( event ), "event is signaled\n" );
470     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
471     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
472     ok( !apc_count, "apc was called\n" );
473     WriteFile( write, buffer, 1, &written, NULL );
474     /* iosb updated here by async i/o */
475     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
476     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
477     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
478     ok( !is_signaled( read ), "read handle is signaled\n" );
479     ok( is_signaled( event ), "event is not signaled\n" );
480     ok( !apc_count, "apc was called\n" );
481     apc_count = 0;
482     SleepEx( 1, FALSE ); /* non-alertable sleep */
483     ok( !apc_count, "apc was called\n" );
484     SleepEx( 1, TRUE ); /* alertable sleep */
485     ok( apc_count == 1, "apc not called\n" );
486
487     /* with no event, the pipe handle itself gets signaled */
488     apc_count = 0;
489     U(iosb).Status = 0xdeadbabe;
490     iosb.Information = 0xdeadbeef;
491     ok( !is_signaled( read ), "read handle is not signaled\n" );
492     status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
493     ok( status == STATUS_PENDING, "wrong status %x\n", status );
494     ok( !is_signaled( read ), "read handle is signaled\n" );
495     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
496     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
497     ok( !apc_count, "apc was called\n" );
498     WriteFile( write, buffer, 1, &written, NULL );
499     /* iosb updated here by async i/o */
500     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
501     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
502     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
503     ok( is_signaled( read ), "read handle is signaled\n" );
504     ok( !apc_count, "apc was called\n" );
505     apc_count = 0;
506     SleepEx( 1, FALSE ); /* non-alertable sleep */
507     ok( !apc_count, "apc was called\n" );
508     SleepEx( 1, TRUE ); /* alertable sleep */
509     ok( apc_count == 1, "apc not called\n" );
510
511     /* now read with data ready */
512     apc_count = 0;
513     U(iosb).Status = 0xdeadbabe;
514     iosb.Information = 0xdeadbeef;
515     ResetEvent( event );
516     WriteFile( write, buffer, 1, &written, NULL );
517     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
518     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
519     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
520     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
521     ok( is_signaled( event ), "event is not signaled\n" );
522     ok( !apc_count, "apc was called\n" );
523     SleepEx( 1, FALSE ); /* non-alertable sleep */
524     ok( !apc_count, "apc was called\n" );
525     SleepEx( 1, TRUE ); /* alertable sleep */
526     ok( apc_count == 1, "apc not called\n" );
527
528     /* try read with no data */
529     apc_count = 0;
530     U(iosb).Status = 0xdeadbabe;
531     iosb.Information = 0xdeadbeef;
532     ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
533     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
534     ok( status == STATUS_PENDING, "wrong status %x\n", status );
535     ok( !is_signaled( event ), "event is signaled\n" );
536     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
537     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
538     ok( !apc_count, "apc was called\n" );
539     WriteFile( write, buffer, 1, &written, NULL );
540     /* partial read is good enough */
541     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
542     ok( is_signaled( event ), "event is signaled\n" );
543     ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
544     ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
545     ok( !apc_count, "apc was called\n" );
546     SleepEx( 1, TRUE ); /* alertable sleep */
547     ok( apc_count == 1, "apc was not called\n" );
548
549     /* read from disconnected pipe */
550     apc_count = 0;
551     U(iosb).Status = 0xdeadbabe;
552     iosb.Information = 0xdeadbeef;
553     CloseHandle( write );
554     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
555     ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
556     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
557     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
558     ok( !is_signaled( event ), "event is signaled\n" );
559     ok( !apc_count, "apc was called\n" );
560     SleepEx( 1, TRUE ); /* alertable sleep */
561     ok( !apc_count, "apc was called\n" );
562     CloseHandle( read );
563
564     /* read from closed handle */
565     apc_count = 0;
566     U(iosb).Status = 0xdeadbabe;
567     iosb.Information = 0xdeadbeef;
568     SetEvent( event );
569     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
570     ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
571     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
572     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
573     ok( is_signaled( event ), "event is signaled\n" );  /* not reset on invalid handle */
574     ok( !apc_count, "apc was called\n" );
575     SleepEx( 1, TRUE ); /* alertable sleep */
576     ok( !apc_count, "apc was called\n" );
577
578     /* disconnect while async read is in progress */
579     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
580     apc_count = 0;
581     U(iosb).Status = 0xdeadbabe;
582     iosb.Information = 0xdeadbeef;
583     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
584     ok( status == STATUS_PENDING, "wrong status %x\n", status );
585     ok( !is_signaled( event ), "event is signaled\n" );
586     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
587     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
588     ok( !apc_count, "apc was called\n" );
589     CloseHandle( write );
590     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
591     ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
592     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
593     ok( is_signaled( event ), "event is signaled\n" );
594     ok( !apc_count, "apc was called\n" );
595     SleepEx( 1, TRUE ); /* alertable sleep */
596     ok( apc_count == 1, "apc was not called\n" );
597     CloseHandle( read );
598
599     if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
600     ok(DuplicateHandle(GetCurrentProcess(), read, GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS),
601         "Failed to duplicate handle: %d\n", GetLastError());
602
603     apc_count = 0;
604     U(iosb).Status = 0xdeadbabe;
605     iosb.Information = 0xdeadbeef;
606     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
607     ok( status == STATUS_PENDING, "wrong status %x\n", status );
608     ok( !is_signaled( event ), "event is signaled\n" );
609     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
610     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
611     ok( !apc_count, "apc was called\n" );
612     /* Cancel by other handle */
613     status = pNtCancelIoFile( read, &iosb2 );
614     ok(status == STATUS_SUCCESS, "failed to cancel by different handle: %x\n", status);
615     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
616     ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
617     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
618     ok( is_signaled( event ), "event is signaled\n" );
619     todo_wine ok( !apc_count, "apc was called\n" );
620     SleepEx( 1, TRUE ); /* alertable sleep */
621     ok( apc_count == 1, "apc was not called\n" );
622
623     apc_count = 0;
624     U(iosb).Status = 0xdeadbabe;
625     iosb.Information = 0xdeadbeef;
626     status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
627     ok( status == STATUS_PENDING, "wrong status %x\n", status );
628     ok( !is_signaled( event ), "event is signaled\n" );
629     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
630     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
631     ok( !apc_count, "apc was called\n" );
632     /* Close queued handle */
633     CloseHandle( read );
634     SleepEx( 1, TRUE ); /* alertable sleep */
635     ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
636     ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
637     status = pNtCancelIoFile( read, &iosb2 );
638     ok(status == STATUS_INVALID_HANDLE, "cancelled by closed handle?\n");
639     status = pNtCancelIoFile( handle, &iosb2 );
640     ok(status == STATUS_SUCCESS, "failed to cancel: %x\n", status);
641     Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
642     ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
643     ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
644     ok( is_signaled( event ), "event is signaled\n" );
645     todo_wine ok( !apc_count, "apc was called\n" );
646     SleepEx( 1, TRUE ); /* alertable sleep */
647     ok( apc_count == 1, "apc was not called\n" );
648     CloseHandle( handle );
649     CloseHandle( write );
650
651     if (pNtCancelIoFileEx)
652     {
653         /* Basic Cancel Ex */
654         if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
655
656         apc_count = 0;
657         U(iosb).Status = 0xdeadbabe;
658         iosb.Information = 0xdeadbeef;
659         status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
660         ok( status == STATUS_PENDING, "wrong status %x\n", status );
661         ok( !is_signaled( event ), "event is signaled\n" );
662         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
663         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
664         ok( !apc_count, "apc was called\n" );
665         status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
666         ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
667         Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
668         ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
669         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
670         ok( is_signaled( event ), "event is signaled\n" );
671         todo_wine ok( !apc_count, "apc was called\n" );
672         SleepEx( 1, TRUE ); /* alertable sleep */
673         ok( apc_count == 1, "apc was not called\n" );
674
675         /* Duplicate iosb */
676         apc_count = 0;
677         U(iosb).Status = 0xdeadbabe;
678         iosb.Information = 0xdeadbeef;
679         status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
680         ok( status == STATUS_PENDING, "wrong status %x\n", status );
681         ok( !is_signaled( event ), "event is signaled\n" );
682         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
683         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
684         ok( !apc_count, "apc was called\n" );
685         status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
686         ok( status == STATUS_PENDING, "wrong status %x\n", status );
687         ok( !is_signaled( event ), "event is signaled\n" );
688         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
689         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
690         ok( !apc_count, "apc was called\n" );
691         status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
692         ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
693         Sleep(1);  /* FIXME: needed for wine to run the i/o apc  */
694         ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
695         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
696         ok( is_signaled( event ), "event is signaled\n" );
697         todo_wine ok( !apc_count, "apc was called\n" );
698         SleepEx( 1, TRUE ); /* alertable sleep */
699         ok( apc_count == 2, "apc was not called\n" );
700
701         CloseHandle( read );
702         CloseHandle( write );
703     }
704
705     /* now try a real file */
706     if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
707     apc_count = 0;
708     U(iosb).Status = 0xdeadbabe;
709     iosb.Information = 0xdeadbeef;
710     offset.QuadPart = 0;
711     ResetEvent( event );
712     status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
713     ok( status == STATUS_SUCCESS || status == STATUS_PENDING, "wrong status %x\n", status );
714     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
715     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
716     ok( is_signaled( event ), "event is signaled\n" );
717     ok( !apc_count, "apc was called\n" );
718     SleepEx( 1, TRUE ); /* alertable sleep */
719     ok( apc_count == 1, "apc was not called\n" );
720
721     apc_count = 0;
722     U(iosb).Status = 0xdeadbabe;
723     iosb.Information = 0xdeadbeef;
724     offset.QuadPart = 0;
725     ResetEvent( event );
726     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
727     ok( status == STATUS_SUCCESS ||
728         status == STATUS_PENDING, /* vista */
729         "wrong status %x\n", status );
730     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
731     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
732     ok( is_signaled( event ), "event is signaled\n" );
733     ok( !apc_count, "apc was called\n" );
734     SleepEx( 1, TRUE ); /* alertable sleep */
735     ok( apc_count == 1, "apc was not called\n" );
736
737     /* read beyond eof */
738     apc_count = 0;
739     U(iosb).Status = 0xdeadbabe;
740     iosb.Information = 0xdeadbeef;
741     offset.QuadPart = strlen(text) + 2;
742     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
743     if (status == STATUS_PENDING)  /* vista */
744     {
745         ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
746         ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
747         ok( is_signaled( event ), "event is signaled\n" );
748         ok( !apc_count, "apc was called\n" );
749         SleepEx( 1, TRUE ); /* alertable sleep */
750         ok( apc_count == 1, "apc was not called\n" );
751     }
752     else
753     {
754         ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
755         ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
756         ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
757         ok( !is_signaled( event ), "event is signaled\n" );
758         ok( !apc_count, "apc was called\n" );
759         SleepEx( 1, TRUE ); /* alertable sleep */
760         ok( !apc_count, "apc was called\n" );
761     }
762     CloseHandle( handle );
763
764     /* now a non-overlapped file */
765     if (!(handle = create_temp_file(0))) return;
766     apc_count = 0;
767     U(iosb).Status = 0xdeadbabe;
768     iosb.Information = 0xdeadbeef;
769     offset.QuadPart = 0;
770     status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
771     ok( status == STATUS_END_OF_FILE ||
772         status == STATUS_SUCCESS ||
773         status == STATUS_PENDING,  /* vista */
774         "wrong status %x\n", status );
775     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
776     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
777     ok( is_signaled( event ), "event is signaled\n" );
778     ok( !apc_count, "apc was called\n" );
779     SleepEx( 1, TRUE ); /* alertable sleep */
780     ok( apc_count == 1, "apc was not called\n" );
781
782     apc_count = 0;
783     U(iosb).Status = 0xdeadbabe;
784     iosb.Information = 0xdeadbeef;
785     offset.QuadPart = 0;
786     ResetEvent( event );
787     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
788     ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
789     ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
790     ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
791     ok( is_signaled( event ), "event is signaled\n" );
792     ok( !apc_count, "apc was called\n" );
793     SleepEx( 1, TRUE ); /* alertable sleep */
794     todo_wine ok( !apc_count, "apc was called\n" );
795
796     /* read beyond eof */
797     apc_count = 0;
798     U(iosb).Status = 0xdeadbabe;
799     iosb.Information = 0xdeadbeef;
800     offset.QuadPart = strlen(text) + 2;
801     ResetEvent( event );
802     status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
803     ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
804     todo_wine ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
805     todo_wine ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
806     todo_wine ok( is_signaled( event ), "event is not signaled\n" );
807     ok( !apc_count, "apc was called\n" );
808     SleepEx( 1, TRUE ); /* alertable sleep */
809     ok( !apc_count, "apc was called\n" );
810
811     CloseHandle( handle );
812
813     CloseHandle( event );
814 }
815
816 static void nt_mailslot_test(void)
817 {
818     HANDLE hslot;
819     ACCESS_MASK DesiredAccess;
820     OBJECT_ATTRIBUTES attr;
821
822     ULONG CreateOptions;
823     ULONG MailslotQuota;
824     ULONG MaxMessageSize;
825     LARGE_INTEGER TimeOut;
826     IO_STATUS_BLOCK IoStatusBlock;
827     NTSTATUS rc;
828     UNICODE_STRING str;
829     WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
830                         'R',':','\\','F','R','E','D','\0' };
831
832     TimeOut.QuadPart = -1;
833
834     pRtlInitUnicodeString(&str, buffer1);
835     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
836     CreateOptions = MailslotQuota = MaxMessageSize = 0;
837     DesiredAccess = GENERIC_READ;
838
839     /*
840      * Check for NULL pointer handling
841      */
842     rc = pNtCreateMailslotFile(NULL, DesiredAccess,
843          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
844          &TimeOut);
845     ok( rc == STATUS_ACCESS_VIOLATION ||
846         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
847         "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
848
849     /*
850      * Test to see if the Timeout can be NULL
851      */
852     hslot = (HANDLE)0xdeadbeef;
853     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
854          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
855          NULL);
856     ok( rc == STATUS_SUCCESS ||
857         rc == STATUS_INVALID_PARAMETER, /* win2k3 */
858         "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
859     ok( hslot != 0, "Handle is invalid\n");
860
861     if  ( rc == STATUS_SUCCESS ) rc = pNtClose(hslot);
862
863     /*
864      * Test that the length field is checked properly
865      */
866     attr.Length = 0;
867     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
868          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
869          &TimeOut);
870     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
871
872     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
873
874     attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
875     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
876          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
877          &TimeOut);
878     todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
879
880     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
881
882     /*
883      * Test handling of a NULL unicode string in ObjectName
884      */
885     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
886     attr.ObjectName = NULL;
887     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
888          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
889          &TimeOut);
890     ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
891         rc == STATUS_INVALID_PARAMETER,
892         "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
893
894     if  (rc == STATUS_SUCCESS) pNtClose(hslot);
895
896     /*
897      * Test a valid call
898      */
899     InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
900     rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
901          &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
902          &TimeOut);
903     ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
904     ok( hslot != 0, "Handle is invalid\n");
905
906     rc = pNtClose(hslot);
907     ok( rc == STATUS_SUCCESS, "NtClose failed\n");
908 }
909
910 static void test_iocp_setcompletion(HANDLE h)
911 {
912     NTSTATUS res;
913     ULONG count;
914
915     res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, 3 );
916     ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
917
918     count = get_pending_msgs(h);
919     ok( count == 1, "Unexpected msg count: %d\n", count );
920
921     if (get_msg(h))
922     {
923         ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
924         ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
925         ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
926         ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
927     }
928
929     count = get_pending_msgs(h);
930     ok( !count, "Unexpected msg count: %d\n", count );
931 }
932
933 static void test_iocp_fileio(HANDLE h)
934 {
935     static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
936
937     IO_STATUS_BLOCK iosb;
938     FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
939     HANDLE hPipeSrv, hPipeClt;
940     NTSTATUS res;
941
942     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
943     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
944     if (hPipeSrv != INVALID_HANDLE_VALUE )
945     {
946         hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
947         ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
948         if (hPipeClt != INVALID_HANDLE_VALUE)
949         {
950             U(iosb).Status = 0xdeadbeef;
951             res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
952             ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
953             ok( U(iosb).Status == STATUS_INVALID_PARAMETER /* 98 */ || U(iosb).Status == 0xdeadbeef /* NT4+ */,
954                 "Unexpected iosb.Status on non-overlapped handle: %x\n", U(iosb).Status );
955             CloseHandle(hPipeClt);
956         }
957         CloseHandle( hPipeSrv );
958     }
959
960     hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
961     ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
962     if (hPipeSrv == INVALID_HANDLE_VALUE )
963         return;
964
965     hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
966     ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
967     if (hPipeClt != INVALID_HANDLE_VALUE)
968     {
969         OVERLAPPED o = {0,};
970         BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
971         DWORD read;
972         long count;
973
974         U(iosb).Status = 0xdeadbeef;
975         res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
976         ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
977         ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
978
979         memset( send_buf, 0, TEST_BUF_LEN );
980         memset( recv_buf, 0xde, TEST_BUF_LEN );
981         count = get_pending_msgs(h);
982         ok( !count, "Unexpected msg count: %ld\n", count );
983         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
984         count = get_pending_msgs(h);
985         ok( !count, "Unexpected msg count: %ld\n", count );
986         WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
987
988         if (get_msg(h))
989         {
990             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
991             ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
992             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
993             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
994             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] );
995         }
996         count = get_pending_msgs(h);
997         ok( !count, "Unexpected msg count: %ld\n", count );
998
999         memset( send_buf, 0, TEST_BUF_LEN );
1000         memset( recv_buf, 0xde, TEST_BUF_LEN );
1001         WriteFile( hPipeClt, send_buf, 2, &read, NULL );
1002         count = get_pending_msgs(h);
1003         ok( !count, "Unexpected msg count: %ld\n", count );
1004         ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
1005         count = get_pending_msgs(h);
1006         ok( count == 1, "Unexpected msg count: %ld\n", count );
1007         if (get_msg(h))
1008         {
1009             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1010             ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1011             ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1012             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1013             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] );
1014         }
1015
1016         ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
1017         CloseHandle( hPipeSrv );
1018         count = get_pending_msgs(h);
1019         ok( count == 1, "Unexpected msg count: %ld\n", count );
1020         if (get_msg(h))
1021         {
1022             ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1023             ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1024             /* wine sends wrong status here */
1025             todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1026             ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1027         }
1028     }
1029
1030     CloseHandle( hPipeClt );
1031 }
1032
1033 static void test_file_basic_information(void)
1034 {
1035     IO_STATUS_BLOCK io;
1036     FILE_BASIC_INFORMATION fbi;
1037     HANDLE h;
1038     int res;
1039     int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
1040
1041     if (!(h = create_temp_file(0))) return;
1042
1043     /* Check default first */
1044     memset(&fbi, 0, sizeof(fbi));
1045     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1046     ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1047     ok ( (fbi.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
1048          "attribute %x not expected\n", fbi.FileAttributes );
1049
1050     /* Then SYSTEM */
1051     /* Clear fbi to avoid setting times */
1052     memset(&fbi, 0, sizeof(fbi));
1053     fbi.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
1054     U(io).Status = 0xdeadbeef;
1055     res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1056     ok ( res == STATUS_SUCCESS, "can't set system attribute, NtSetInformationFile returned %x\n", res );
1057     ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status is %x\n", U(io).Status );
1058
1059     memset(&fbi, 0, sizeof(fbi));
1060     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1061     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1062     todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_SYSTEM, "attribute %x not FILE_ATTRIBUTE_SYSTEM\n", fbi.FileAttributes );
1063
1064     /* Then HIDDEN */
1065     memset(&fbi, 0, sizeof(fbi));
1066     fbi.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1067     U(io).Status = 0xdeadbeef;
1068     res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1069     ok ( res == STATUS_SUCCESS, "can't set system attribute, NtSetInformationFile returned %x\n", res );
1070     ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status is %x\n", U(io).Status );
1071
1072     memset(&fbi, 0, sizeof(fbi));
1073     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1074     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1075     todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_HIDDEN, "attribute %x not FILE_ATTRIBUTE_HIDDEN\n", fbi.FileAttributes );
1076
1077     /* Check NORMAL last of all (to make sure we can clear attributes) */
1078     memset(&fbi, 0, sizeof(fbi));
1079     fbi.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1080     U(io).Status = 0xdeadbeef;
1081     res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1082     ok ( res == STATUS_SUCCESS, "can't set normal attribute, NtSetInformationFile returned %x\n", res );
1083     ok ( U(io).Status == STATUS_SUCCESS, "can't set normal attribute, io.Status is %x\n", U(io).Status );
1084
1085     memset(&fbi, 0, sizeof(fbi));
1086     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1087     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1088     todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_NORMAL, "attribute %x not 0\n", fbi.FileAttributes );
1089
1090     CloseHandle( h );
1091 }
1092
1093 static void test_file_all_information(void)
1094 {
1095     IO_STATUS_BLOCK io;
1096     /* FileAllInformation, like FileNameInformation, has a variable-length pathname
1097      * buffer at the end.  Vista objects with STATUS_BUFFER_OVERFLOW if you
1098      * don't leave enough room there.
1099      */
1100     struct {
1101       FILE_ALL_INFORMATION fai;
1102       WCHAR buf[256];
1103     } fai_buf;
1104     HANDLE h;
1105     int res;
1106     int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
1107
1108     if (!(h = create_temp_file(0))) return;
1109
1110     /* Check default first */
1111     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1112     ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1113     ok ( (fai_buf.fai.BasicInformation.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
1114          "attribute %x not expected\n", fai_buf.fai.BasicInformation.FileAttributes );
1115
1116     /* Then SYSTEM */
1117     /* Clear fbi to avoid setting times */
1118     memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1119     fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
1120     U(io).Status = 0xdeadbeef;
1121     res = pNtSetInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1122     ok ( res == STATUS_INVALID_INFO_CLASS || broken(res == STATUS_NOT_IMPLEMENTED), "shouldn't be able to set FileAllInformation, res %x\n", res);
1123     todo_wine ok ( U(io).Status == 0xdeadbeef, "shouldn't be able to set FileAllInformation, io.Status is %x\n", U(io).Status);
1124     U(io).Status = 0xdeadbeef;
1125     res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1126     ok ( res == STATUS_SUCCESS, "can't set system attribute, res: %x\n", res );
1127     ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status: %x\n", U(io).Status );
1128
1129     memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1130     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1131     ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1132     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 );
1133
1134     /* Then HIDDEN */
1135     memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1136     fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1137     U(io).Status = 0xdeadbeef;
1138     res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1139     ok ( res == STATUS_SUCCESS, "can't set system attribute, res: %x\n", res );
1140     ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status: %x\n", U(io).Status );
1141
1142     memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1143     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1144     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1145     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 );
1146
1147     /* Check NORMAL last of all (to make sure we can clear attributes) */
1148     memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1149     fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1150     U(io).Status = 0xdeadbeef;
1151     res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1152     ok ( res == STATUS_SUCCESS, "can't set system attribute, res: %x\n", res );
1153     ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status: %x\n", U(io).Status );
1154
1155     memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1156     res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1157     ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1158     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 );
1159
1160     CloseHandle( h );
1161 }
1162
1163 static void test_file_both_information(void)
1164 {
1165     IO_STATUS_BLOCK io;
1166     FILE_BOTH_DIR_INFORMATION fbi;
1167     HANDLE h;
1168     int res;
1169
1170     if (!(h = create_temp_file(0))) return;
1171
1172     memset(&fbi, 0, sizeof(fbi));
1173     res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBothDirectoryInformation);
1174     ok ( res == STATUS_INVALID_INFO_CLASS || res == STATUS_NOT_IMPLEMENTED, "shouldn't be able to query FileBothDirectoryInformation, res %x\n", res);
1175
1176     CloseHandle( h );
1177 }
1178
1179 static void test_iocompletion(void)
1180 {
1181     HANDLE h = INVALID_HANDLE_VALUE;
1182     NTSTATUS res;
1183
1184     res = pNtCreateIoCompletion( &h, IO_COMPLETION_ALL_ACCESS, NULL, 0);
1185
1186     ok( res == 0, "NtCreateIoCompletion anonymous failed: %x\n", res );
1187     ok( h && h != INVALID_HANDLE_VALUE, "Invalid handle returned\n" );
1188
1189     if ( h && h != INVALID_HANDLE_VALUE)
1190     {
1191         test_iocp_setcompletion(h);
1192         test_iocp_fileio(h);
1193         pNtClose(h);
1194     }
1195 }
1196
1197 static void test_file_name_information(void)
1198 {
1199     WCHAR *file_name, *volume_prefix, *expected;
1200     FILE_NAME_INFORMATION *info;
1201     ULONG old_redir = 1, tmp;
1202     UINT file_name_size;
1203     IO_STATUS_BLOCK io;
1204     UINT info_size;
1205     HRESULT hr;
1206     HANDLE h;
1207     UINT len;
1208
1209     /* GetVolumePathName is not present before w2k */
1210     if (!pGetVolumePathNameW) {
1211         win_skip("GetVolumePathNameW not found\n");
1212         return;
1213     }
1214
1215     file_name_size = GetSystemDirectoryW( NULL, 0 );
1216     file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1217     volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1218     expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1219
1220     len = GetSystemDirectoryW( file_name, file_name_size );
1221     ok(len == file_name_size - 1,
1222             "GetSystemDirectoryW returned %u, expected %u.\n",
1223             len, file_name_size - 1);
1224
1225     len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1226     ok(len, "GetVolumePathNameW failed.\n");
1227
1228     len = lstrlenW( volume_prefix );
1229     if (len && volume_prefix[len - 1] == '\\') --len;
1230     memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1231     expected[file_name_size - len - 1] = '\0';
1232
1233     /* A bit more than we actually need, but it keeps the calculation simple. */
1234     info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1235     info = HeapAlloc( GetProcessHeap(), 0, info_size );
1236
1237     if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( TRUE, &old_redir );
1238     h = CreateFileW( file_name, GENERIC_READ,
1239             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1240             NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1241     if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( old_redir, &tmp );
1242     ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1243
1244     hr = pNtQueryInformationFile( h, &io, info, sizeof(*info) - 1, FileNameInformation );
1245     ok(hr == STATUS_INFO_LENGTH_MISMATCH, "NtQueryInformationFile returned %#x.\n", hr);
1246
1247     memset( info, 0xcc, info_size );
1248     hr = pNtQueryInformationFile( h, &io, info, sizeof(*info), FileNameInformation );
1249     ok(hr == STATUS_BUFFER_OVERFLOW, "NtQueryInformationFile returned %#x, expected %#x.\n",
1250             hr, STATUS_BUFFER_OVERFLOW);
1251     ok(U(io).Status == STATUS_BUFFER_OVERFLOW, "io.Status is %#x, expected %#x.\n",
1252             U(io).Status, STATUS_BUFFER_OVERFLOW);
1253     ok(info->FileNameLength == lstrlenW( expected ) * sizeof(WCHAR), "info->FileNameLength is %u\n", info->FileNameLength);
1254     ok(info->FileName[2] == 0xcccc, "info->FileName[2] is %#x, expected 0xcccc.\n", info->FileName[2]);
1255     ok(CharLowerW((LPWSTR)(UINT_PTR)info->FileName[1]) == CharLowerW((LPWSTR)(UINT_PTR)expected[1]),
1256             "info->FileName[1] is %p, expected %p.\n",
1257             CharLowerW((LPWSTR)(UINT_PTR)info->FileName[1]), CharLowerW((LPWSTR)(UINT_PTR)expected[1]));
1258     ok(io.Information == sizeof(*info), "io.Information is %lu\n", io.Information);
1259
1260     memset( info, 0xcc, info_size );
1261     hr = pNtQueryInformationFile( h, &io, info, info_size, FileNameInformation );
1262     ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1263     ok(U(io).Status == STATUS_SUCCESS, "io.Status is %#x, expected %#x.\n", U(io).Status, STATUS_SUCCESS);
1264     ok(info->FileNameLength == lstrlenW( expected ) * sizeof(WCHAR), "info->FileNameLength is %u\n", info->FileNameLength);
1265     ok(info->FileName[info->FileNameLength / sizeof(WCHAR)] == 0xcccc, "info->FileName[len] is %#x, expected 0xcccc.\n",
1266        info->FileName[info->FileNameLength / sizeof(WCHAR)]);
1267     info->FileName[info->FileNameLength / sizeof(WCHAR)] = '\0';
1268     ok(!lstrcmpiW( info->FileName, expected ), "info->FileName is %s, expected %s.\n",
1269             wine_dbgstr_w( info->FileName ), wine_dbgstr_w( expected ));
1270     ok(io.Information == FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + info->FileNameLength,
1271             "io.Information is %lu, expected %u.\n",
1272             io.Information, FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + info->FileNameLength);
1273
1274     CloseHandle( h );
1275     HeapFree( GetProcessHeap(), 0, info );
1276     HeapFree( GetProcessHeap(), 0, expected );
1277     HeapFree( GetProcessHeap(), 0, volume_prefix );
1278
1279     if (old_redir || !pGetSystemWow64DirectoryW || !(file_name_size = pGetSystemWow64DirectoryW( NULL, 0 )))
1280     {
1281         skip("Not running on WoW64, skipping test.\n");
1282         HeapFree( GetProcessHeap(), 0, file_name );
1283         return;
1284     }
1285
1286     h = CreateFileW( file_name, GENERIC_READ,
1287             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1288             NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1289     ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1290     HeapFree( GetProcessHeap(), 0, file_name );
1291
1292     file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1293     volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1294     expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*expected) );
1295
1296     len = pGetSystemWow64DirectoryW( file_name, file_name_size );
1297     ok(len == file_name_size - 1,
1298             "GetSystemWow64DirectoryW returned %u, expected %u.\n",
1299             len, file_name_size - 1);
1300
1301     len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1302     ok(len, "GetVolumePathNameW failed.\n");
1303
1304     len = lstrlenW( volume_prefix );
1305     if (len && volume_prefix[len - 1] == '\\') --len;
1306     memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1307     expected[file_name_size - len - 1] = '\0';
1308
1309     info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1310     info = HeapAlloc( GetProcessHeap(), 0, info_size );
1311
1312     memset( info, 0xcc, info_size );
1313     hr = pNtQueryInformationFile( h, &io, info, info_size, FileNameInformation );
1314     ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1315     info->FileName[info->FileNameLength / sizeof(WCHAR)] = '\0';
1316     ok(!lstrcmpiW( info->FileName, expected ), "info->FileName is %s, expected %s.\n",
1317             wine_dbgstr_w( info->FileName ), wine_dbgstr_w( expected ));
1318
1319     CloseHandle( h );
1320     HeapFree( GetProcessHeap(), 0, info );
1321     HeapFree( GetProcessHeap(), 0, expected );
1322     HeapFree( GetProcessHeap(), 0, volume_prefix );
1323     HeapFree( GetProcessHeap(), 0, file_name );
1324 }
1325
1326 static void test_file_all_name_information(void)
1327 {
1328     WCHAR *file_name, *volume_prefix, *expected;
1329     FILE_ALL_INFORMATION *info;
1330     ULONG old_redir = 1, tmp;
1331     UINT file_name_size;
1332     IO_STATUS_BLOCK io;
1333     UINT info_size;
1334     HRESULT hr;
1335     HANDLE h;
1336     UINT len;
1337
1338     /* GetVolumePathName is not present before w2k */
1339     if (!pGetVolumePathNameW) {
1340         win_skip("GetVolumePathNameW not found\n");
1341         return;
1342     }
1343
1344     file_name_size = GetSystemDirectoryW( NULL, 0 );
1345     file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1346     volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1347     expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1348
1349     len = GetSystemDirectoryW( file_name, file_name_size );
1350     ok(len == file_name_size - 1,
1351             "GetSystemDirectoryW returned %u, expected %u.\n",
1352             len, file_name_size - 1);
1353
1354     len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1355     ok(len, "GetVolumePathNameW failed.\n");
1356
1357     len = lstrlenW( volume_prefix );
1358     if (len && volume_prefix[len - 1] == '\\') --len;
1359     memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1360     expected[file_name_size - len - 1] = '\0';
1361
1362     /* A bit more than we actually need, but it keeps the calculation simple. */
1363     info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1364     info = HeapAlloc( GetProcessHeap(), 0, info_size );
1365
1366     if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( TRUE, &old_redir );
1367     h = CreateFileW( file_name, GENERIC_READ,
1368             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1369             NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1370     if (pRtlWow64EnableFsRedirectionEx) pRtlWow64EnableFsRedirectionEx( old_redir, &tmp );
1371     ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1372
1373     hr = pNtQueryInformationFile( h, &io, info, sizeof(*info) - 1, FileAllInformation );
1374     ok(hr == STATUS_INFO_LENGTH_MISMATCH, "NtQueryInformationFile returned %#x, expected %#x.\n",
1375             hr, STATUS_INFO_LENGTH_MISMATCH);
1376
1377     memset( info, 0xcc, info_size );
1378     hr = pNtQueryInformationFile( h, &io, info, sizeof(*info), FileAllInformation );
1379     ok(hr == STATUS_BUFFER_OVERFLOW, "NtQueryInformationFile returned %#x, expected %#x.\n",
1380             hr, STATUS_BUFFER_OVERFLOW);
1381     ok(U(io).Status == STATUS_BUFFER_OVERFLOW, "io.Status is %#x, expected %#x.\n",
1382             U(io).Status, STATUS_BUFFER_OVERFLOW);
1383     ok(info->NameInformation.FileNameLength == lstrlenW( expected ) * sizeof(WCHAR),
1384        "info->NameInformation.FileNameLength is %u\n", info->NameInformation.FileNameLength );
1385     ok(info->NameInformation.FileName[2] == 0xcccc,
1386             "info->NameInformation.FileName[2] is %#x, expected 0xcccc.\n", info->NameInformation.FileName[2]);
1387     ok(CharLowerW((LPWSTR)(UINT_PTR)info->NameInformation.FileName[1]) == CharLowerW((LPWSTR)(UINT_PTR)expected[1]),
1388             "info->NameInformation.FileName[1] is %p, expected %p.\n",
1389             CharLowerW((LPWSTR)(UINT_PTR)info->NameInformation.FileName[1]), CharLowerW((LPWSTR)(UINT_PTR)expected[1]));
1390     ok(io.Information == sizeof(*info), "io.Information is %lu\n", io.Information);
1391
1392     memset( info, 0xcc, info_size );
1393     hr = pNtQueryInformationFile( h, &io, info, info_size, FileAllInformation );
1394     ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1395     ok(U(io).Status == STATUS_SUCCESS, "io.Status is %#x, expected %#x.\n", U(io).Status, STATUS_SUCCESS);
1396     ok(info->NameInformation.FileNameLength == lstrlenW( expected ) * sizeof(WCHAR),
1397        "info->NameInformation.FileNameLength is %u\n", info->NameInformation.FileNameLength );
1398     ok(info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)] == 0xcccc,
1399        "info->NameInformation.FileName[len] is %#x, expected 0xcccc.\n",
1400        info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)]);
1401     info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)] = '\0';
1402     ok(!lstrcmpiW( info->NameInformation.FileName, expected ),
1403             "info->NameInformation.FileName is %s, expected %s.\n",
1404             wine_dbgstr_w( info->NameInformation.FileName ), wine_dbgstr_w( expected ));
1405     ok(io.Information == FIELD_OFFSET(FILE_ALL_INFORMATION, NameInformation.FileName)
1406             + info->NameInformation.FileNameLength,
1407             "io.Information is %lu\n", io.Information );
1408
1409     CloseHandle( h );
1410     HeapFree( GetProcessHeap(), 0, info );
1411     HeapFree( GetProcessHeap(), 0, expected );
1412     HeapFree( GetProcessHeap(), 0, volume_prefix );
1413
1414     if (old_redir || !pGetSystemWow64DirectoryW || !(file_name_size = pGetSystemWow64DirectoryW( NULL, 0 )))
1415     {
1416         skip("Not running on WoW64, skipping test.\n");
1417         HeapFree( GetProcessHeap(), 0, file_name );
1418         return;
1419     }
1420
1421     h = CreateFileW( file_name, GENERIC_READ,
1422             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1423             NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1424     ok(h != INVALID_HANDLE_VALUE, "Failed to open file.\n");
1425     HeapFree( GetProcessHeap(), 0, file_name );
1426
1427     file_name = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*file_name) );
1428     volume_prefix = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*volume_prefix) );
1429     expected = HeapAlloc( GetProcessHeap(), 0, file_name_size * sizeof(*expected) );
1430
1431     len = pGetSystemWow64DirectoryW( file_name, file_name_size );
1432     ok(len == file_name_size - 1,
1433             "GetSystemWow64DirectoryW returned %u, expected %u.\n",
1434             len, file_name_size - 1);
1435
1436     len = pGetVolumePathNameW( file_name, volume_prefix, file_name_size );
1437     ok(len, "GetVolumePathNameW failed.\n");
1438
1439     len = lstrlenW( volume_prefix );
1440     if (len && volume_prefix[len - 1] == '\\') --len;
1441     memcpy( expected, file_name + len, (file_name_size - len - 1) * sizeof(WCHAR) );
1442     expected[file_name_size - len - 1] = '\0';
1443
1444     info_size = sizeof(*info) + (file_name_size * sizeof(WCHAR));
1445     info = HeapAlloc( GetProcessHeap(), 0, info_size );
1446
1447     memset( info, 0xcc, info_size );
1448     hr = pNtQueryInformationFile( h, &io, info, info_size, FileAllInformation );
1449     ok(hr == STATUS_SUCCESS, "NtQueryInformationFile returned %#x, expected %#x.\n", hr, STATUS_SUCCESS);
1450     info->NameInformation.FileName[info->NameInformation.FileNameLength / sizeof(WCHAR)] = '\0';
1451     ok(!lstrcmpiW( info->NameInformation.FileName, expected ), "info->NameInformation.FileName is %s, expected %s.\n",
1452             wine_dbgstr_w( info->NameInformation.FileName ), wine_dbgstr_w( expected ));
1453
1454     CloseHandle( h );
1455     HeapFree( GetProcessHeap(), 0, info );
1456     HeapFree( GetProcessHeap(), 0, expected );
1457     HeapFree( GetProcessHeap(), 0, volume_prefix );
1458     HeapFree( GetProcessHeap(), 0, file_name );
1459 }
1460
1461 START_TEST(file)
1462 {
1463     HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
1464     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1465     if (!hntdll)
1466     {
1467         skip("not running on NT, skipping test\n");
1468         return;
1469     }
1470
1471     pGetVolumePathNameW = (void *)GetProcAddress(hkernel32, "GetVolumePathNameW");
1472     pGetSystemWow64DirectoryW = (void *)GetProcAddress(hkernel32, "GetSystemWow64DirectoryW");
1473
1474     pRtlFreeUnicodeString   = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
1475     pRtlInitUnicodeString   = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
1476     pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
1477     pRtlWow64EnableFsRedirectionEx = (void *)GetProcAddress(hntdll, "RtlWow64EnableFsRedirectionEx");
1478     pNtCreateMailslotFile   = (void *)GetProcAddress(hntdll, "NtCreateMailslotFile");
1479     pNtCreateFile           = (void *)GetProcAddress(hntdll, "NtCreateFile");
1480     pNtOpenFile             = (void *)GetProcAddress(hntdll, "NtOpenFile");
1481     pNtDeleteFile           = (void *)GetProcAddress(hntdll, "NtDeleteFile");
1482     pNtReadFile             = (void *)GetProcAddress(hntdll, "NtReadFile");
1483     pNtWriteFile            = (void *)GetProcAddress(hntdll, "NtWriteFile");
1484     pNtCancelIoFile         = (void *)GetProcAddress(hntdll, "NtCancelIoFile");
1485     pNtCancelIoFileEx       = (void *)GetProcAddress(hntdll, "NtCancelIoFileEx");
1486     pNtClose                = (void *)GetProcAddress(hntdll, "NtClose");
1487     pNtCreateIoCompletion   = (void *)GetProcAddress(hntdll, "NtCreateIoCompletion");
1488     pNtOpenIoCompletion     = (void *)GetProcAddress(hntdll, "NtOpenIoCompletion");
1489     pNtQueryIoCompletion    = (void *)GetProcAddress(hntdll, "NtQueryIoCompletion");
1490     pNtRemoveIoCompletion   = (void *)GetProcAddress(hntdll, "NtRemoveIoCompletion");
1491     pNtSetIoCompletion      = (void *)GetProcAddress(hntdll, "NtSetIoCompletion");
1492     pNtSetInformationFile   = (void *)GetProcAddress(hntdll, "NtSetInformationFile");
1493     pNtQueryInformationFile = (void *)GetProcAddress(hntdll, "NtQueryInformationFile");
1494     pNtQueryDirectoryFile   = (void *)GetProcAddress(hntdll, "NtQueryDirectoryFile");
1495
1496     create_file_test();
1497     open_file_test();
1498     delete_file_test();
1499     read_file_test();
1500     nt_mailslot_test();
1501     test_iocompletion();
1502     test_file_basic_information();
1503     test_file_all_information();
1504     test_file_both_information();
1505     test_file_name_information();
1506     test_file_all_name_information();
1507 }