[NTDLL_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / winetests / ntdll / 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 * Copyright 2011 Dmitry Timoshkov
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 * NOTES
23 * We use function pointers here as there is no import library for NTDLL on
24 * windows.
25 */
26
27 #include <stdio.h>
28 #include <stdarg.h>
29
30 #include "ntstatus.h"
31 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro
32 * definition errors when we get to winnt.h
33 */
34 #define WIN32_NO_STATUS
35
36 #include "wine/test.h"
37 #include "wine/winternl.h"
38 #include "winuser.h"
39 #include "wine/winioctl.h"
40
41 /* FIXME */
42 typedef struct _REPARSE_DATA_BUFFER {
43 ULONG ReparseTag;
44 USHORT ReparseDataLength;
45 USHORT Reserved;
46 _ANONYMOUS_UNION union {
47 struct {
48 USHORT SubstituteNameOffset;
49 USHORT SubstituteNameLength;
50 USHORT PrintNameOffset;
51 USHORT PrintNameLength;
52 ULONG Flags;
53 WCHAR PathBuffer[1];
54 } SymbolicLinkReparseBuffer;
55 struct {
56 USHORT SubstituteNameOffset;
57 USHORT SubstituteNameLength;
58 USHORT PrintNameOffset;
59 USHORT PrintNameLength;
60 WCHAR PathBuffer[1];
61 } MountPointReparseBuffer;
62 struct {
63 UCHAR DataBuffer[1];
64 } GenericReparseBuffer;
65 } DUMMYUNIONNAME;
66 } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
67
68 #ifndef IO_COMPLETION_ALL_ACCESS
69 #define IO_COMPLETION_ALL_ACCESS 0x001F0003
70 #endif
71
72 static BOOL (WINAPI * pGetVolumePathNameW)(LPCWSTR, LPWSTR, DWORD);
73 static UINT (WINAPI *pGetSystemWow64DirectoryW)( LPWSTR, UINT );
74
75 static VOID (WINAPI *pRtlFreeUnicodeString)( PUNICODE_STRING );
76 static VOID (WINAPI *pRtlInitUnicodeString)( PUNICODE_STRING, LPCWSTR );
77 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)( LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR* );
78 static NTSTATUS (WINAPI *pRtlWow64EnableFsRedirectionEx)( ULONG, ULONG * );
79
80 static NTSTATUS (WINAPI *pNtCreateMailslotFile)( PHANDLE, ULONG, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
81 ULONG, ULONG, ULONG, PLARGE_INTEGER );
82 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,PLARGE_INTEGER,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG);
83 static NTSTATUS (WINAPI *pNtOpenFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,ULONG,ULONG);
84 static NTSTATUS (WINAPI *pNtDeleteFile)(POBJECT_ATTRIBUTES ObjectAttributes);
85 static NTSTATUS (WINAPI *pNtReadFile)(HANDLE hFile, HANDLE hEvent,
86 PIO_APC_ROUTINE apc, void* apc_user,
87 PIO_STATUS_BLOCK io_status, void* buffer, ULONG length,
88 PLARGE_INTEGER offset, PULONG key);
89 static NTSTATUS (WINAPI *pNtWriteFile)(HANDLE hFile, HANDLE hEvent,
90 PIO_APC_ROUTINE apc, void* apc_user,
91 PIO_STATUS_BLOCK io_status,
92 const void* buffer, ULONG length,
93 PLARGE_INTEGER offset, PULONG key);
94 static NTSTATUS (WINAPI *pNtCancelIoFile)(HANDLE hFile, PIO_STATUS_BLOCK io_status);
95 static NTSTATUS (WINAPI *pNtCancelIoFileEx)(HANDLE hFile, PIO_STATUS_BLOCK iosb, PIO_STATUS_BLOCK io_status);
96 static NTSTATUS (WINAPI *pNtClose)( PHANDLE );
97
98 static NTSTATUS (WINAPI *pNtCreateIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, ULONG);
99 static NTSTATUS (WINAPI *pNtOpenIoCompletion)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
100 static NTSTATUS (WINAPI *pNtQueryIoCompletion)(HANDLE, IO_COMPLETION_INFORMATION_CLASS, PVOID, ULONG, PULONG);
101 static NTSTATUS (WINAPI *pNtRemoveIoCompletion)(HANDLE, PULONG_PTR, PULONG_PTR, PIO_STATUS_BLOCK, PLARGE_INTEGER);
102 static NTSTATUS (WINAPI *pNtSetIoCompletion)(HANDLE, ULONG_PTR, ULONG_PTR, NTSTATUS, SIZE_T);
103 static NTSTATUS (WINAPI *pNtSetInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
104 static NTSTATUS (WINAPI *pNtQueryInformationFile)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS);
105 static NTSTATUS (WINAPI *pNtQueryDirectoryFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,
106 PVOID,ULONG,FILE_INFORMATION_CLASS,BOOLEAN,PUNICODE_STRING,BOOLEAN);
107 static NTSTATUS (WINAPI *pNtQueryVolumeInformationFile)(HANDLE,PIO_STATUS_BLOCK,PVOID,ULONG,FS_INFORMATION_CLASS);
108 static NTSTATUS (WINAPI *pNtQueryFullAttributesFile)(const OBJECT_ATTRIBUTES*, FILE_NETWORK_OPEN_INFORMATION*);
109 static NTSTATUS (WINAPI *pNtQueryEaFile)(HANDLE,PIO_STATUS_BLOCK,PVOID,ULONG,BOOLEAN,PVOID,ULONG,PULONG,BOOLEAN);
110
111 static inline BOOL is_signaled( HANDLE obj )
112 {
113 return WaitForSingleObject( obj, 0 ) == WAIT_OBJECT_0;
114 }
115
116 static const char* debugstr_longlong(ULONGLONG ll)
117 {
118 static char str[17];
119 if (sizeof(ll) > sizeof(unsigned long) && ll >> 32)
120 sprintf(str, "%lx%08lx", (unsigned long)(ll >> 32), (unsigned long)ll);
121 else
122 sprintf(str, "%lx", (unsigned long)ll);
123 return str;
124 }
125
126 #define PIPENAME "\\\\.\\pipe\\ntdll_tests_file.c"
127 #define TEST_BUF_LEN 3
128
129 static BOOL create_pipe( HANDLE *read, HANDLE *write, ULONG flags, ULONG size )
130 {
131 *read = CreateNamedPipeA(PIPENAME, PIPE_ACCESS_INBOUND | flags, PIPE_TYPE_BYTE | PIPE_WAIT,
132 1, size, size, NMPWAIT_USE_DEFAULT_WAIT, NULL);
133 ok(*read != INVALID_HANDLE_VALUE, "CreateNamedPipe failed\n");
134
135 *write = CreateFileA(PIPENAME, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
136 ok(*write != INVALID_HANDLE_VALUE, "CreateFile failed (%d)\n", GetLastError());
137
138 return TRUE;
139 }
140
141 static HANDLE create_temp_file( ULONG flags )
142 {
143 char path[MAX_PATH], buffer[MAX_PATH];
144 HANDLE handle;
145
146 GetTempPathA( MAX_PATH, path );
147 GetTempFileNameA( path, "foo", 0, buffer );
148 handle = CreateFileA(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
149 flags | FILE_FLAG_DELETE_ON_CLOSE, 0);
150 ok( handle != INVALID_HANDLE_VALUE, "failed to create temp file\n" );
151 return (handle == INVALID_HANDLE_VALUE) ? 0 : handle;
152 }
153
154 #define CVALUE_FIRST 0xfffabbcc
155 #define CKEY_FIRST 0x1030341
156 #define CKEY_SECOND 0x132E46
157
158 static ULONG_PTR completionKey;
159 static IO_STATUS_BLOCK ioSb;
160 static ULONG_PTR completionValue;
161
162 static ULONG get_pending_msgs(HANDLE h)
163 {
164 NTSTATUS res;
165 ULONG a, req;
166
167 res = pNtQueryIoCompletion( h, IoCompletionBasicInformation, &a, sizeof(a), &req );
168 ok( res == STATUS_SUCCESS, "NtQueryIoCompletion failed: %x\n", res );
169 if (res != STATUS_SUCCESS) return -1;
170 ok( req == sizeof(a), "Unexpected response size: %x\n", req );
171 return a;
172 }
173
174 static BOOL get_msg(HANDLE h)
175 {
176 LARGE_INTEGER timeout = {{-10000000*3}};
177 DWORD res = pNtRemoveIoCompletion( h, &completionKey, &completionValue, &ioSb, &timeout);
178 ok( res == STATUS_SUCCESS, "NtRemoveIoCompletion failed: %x\n", res );
179 if (res != STATUS_SUCCESS)
180 {
181 completionKey = completionValue = 0;
182 memset(&ioSb, 0, sizeof(ioSb));
183 return FALSE;
184 }
185 return TRUE;
186 }
187
188
189 static void WINAPI apc( void *arg, IO_STATUS_BLOCK *iosb, ULONG reserved )
190 {
191 int *count = arg;
192
193 trace( "apc called block %p iosb.status %x iosb.info %lu\n",
194 iosb, U(*iosb).Status, iosb->Information );
195 (*count)++;
196 ok( !reserved, "reserved is not 0: %x\n", reserved );
197 }
198
199 static void create_file_test(void)
200 {
201 static const WCHAR systemrootW[] = {'\\','S','y','s','t','e','m','R','o','o','t',
202 '\\','f','a','i','l','i','n','g',0};
203 static const WCHAR questionmarkInvalidNameW[] = {'a','f','i','l','e','?',0};
204 static const WCHAR pipeInvalidNameW[] = {'a','|','b',0};
205 static const WCHAR pathInvalidNtW[] = {'\\','\\','?','\\',0};
206 static const WCHAR pathInvalidNt2W[] = {'\\','?','?','\\',0};
207 static const WCHAR pathInvalidDosW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
208 static const char testdata[] = "Hello World";
209 FILE_NETWORK_OPEN_INFORMATION info;
210 NTSTATUS status;
211 HANDLE dir, file;
212 WCHAR path[MAX_PATH];
213 OBJECT_ATTRIBUTES attr;
214 IO_STATUS_BLOCK io;
215 UNICODE_STRING nameW;
216 LARGE_INTEGER offset;
217 char buf[32];
218 DWORD ret;
219
220 GetCurrentDirectoryW( MAX_PATH, path );
221 pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
222 attr.Length = sizeof(attr);
223 attr.RootDirectory = 0;
224 attr.ObjectName = &nameW;
225 attr.Attributes = OBJ_CASE_INSENSITIVE;
226 attr.SecurityDescriptor = NULL;
227 attr.SecurityQualityOfService = NULL;
228
229 /* try various open modes and options on directories */
230 status = pNtCreateFile( &dir, GENERIC_READ|GENERIC_WRITE, &attr, &io, NULL, 0,
231 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, FILE_DIRECTORY_FILE, NULL, 0 );
232 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
233
234 U(io).Status = 0xdeadbeef;
235 offset.QuadPart = 0;
236 status = pNtReadFile( dir, NULL, NULL, NULL, &io, buf, sizeof(buf), &offset, NULL );
237 ok( status == STATUS_INVALID_DEVICE_REQUEST || status == STATUS_PENDING, "NtReadFile error %08x\n", status );
238 if (status == STATUS_PENDING)
239 {
240 ret = WaitForSingleObject( dir, 1000 );
241 ok( ret == WAIT_OBJECT_0, "WaitForSingleObject error %u\n", ret );
242 ok( U(io).Status == STATUS_INVALID_DEVICE_REQUEST,
243 "expected STATUS_INVALID_DEVICE_REQUEST, got %08x\n", U(io).Status );
244 }
245
246 U(io).Status = 0xdeadbeef;
247 offset.QuadPart = 0;
248 status = pNtWriteFile( dir, NULL, NULL, NULL, &io, testdata, sizeof(testdata), &offset, NULL);
249 todo_wine
250 ok( status == STATUS_INVALID_DEVICE_REQUEST || status == STATUS_PENDING, "NtWriteFile error %08x\n", status );
251 if (status == STATUS_PENDING)
252 {
253 ret = WaitForSingleObject( dir, 1000 );
254 ok( ret == WAIT_OBJECT_0, "WaitForSingleObject error %u\n", ret );
255 ok( U(io).Status == STATUS_INVALID_DEVICE_REQUEST,
256 "expected STATUS_INVALID_DEVICE_REQUEST, got %08x\n", U(io).Status );
257 }
258
259 CloseHandle( dir );
260
261 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
262 FILE_CREATE, FILE_DIRECTORY_FILE, NULL, 0 );
263 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
264 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
265
266 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
267 FILE_OPEN_IF, FILE_DIRECTORY_FILE, NULL, 0 );
268 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
269 CloseHandle( dir );
270
271 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
272 FILE_SUPERSEDE, FILE_DIRECTORY_FILE, NULL, 0 );
273 ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
274
275 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
276 FILE_OVERWRITE, FILE_DIRECTORY_FILE, NULL, 0 );
277 ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
278
279 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
280 FILE_OVERWRITE_IF, FILE_DIRECTORY_FILE, NULL, 0 );
281 ok( status == STATUS_INVALID_PARAMETER, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
282
283 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
284 FILE_OPEN, 0, NULL, 0 );
285 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
286 CloseHandle( dir );
287
288 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
289 FILE_CREATE, 0, NULL, 0 );
290 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
291 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
292
293 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
294 FILE_OPEN_IF, 0, NULL, 0 );
295 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
296 CloseHandle( dir );
297
298 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
299 FILE_SUPERSEDE, 0, NULL, 0 );
300 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
301 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
302
303 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
304 FILE_OVERWRITE, 0, NULL, 0 );
305 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
306 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
307
308 status = pNtCreateFile( &dir, GENERIC_READ, &attr, &io, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
309 FILE_OVERWRITE_IF, 0, NULL, 0 );
310 ok( status == STATUS_OBJECT_NAME_COLLISION || status == STATUS_ACCESS_DENIED,
311 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
312
313 pRtlFreeUnicodeString( &nameW );
314
315 pRtlInitUnicodeString( &nameW, systemrootW );
316 attr.Length = sizeof(attr);
317 attr.RootDirectory = NULL;
318 attr.ObjectName = &nameW;
319 attr.Attributes = OBJ_CASE_INSENSITIVE;
320 attr.SecurityDescriptor = NULL;
321 attr.SecurityQualityOfService = NULL;
322 dir = NULL;
323 status = pNtCreateFile( &dir, FILE_APPEND_DATA, &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, 0,
324 FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
325 todo_wine
326 ok( status == STATUS_INVALID_PARAMETER,
327 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
328
329 /* Invalid chars in file/dirnames */
330 pRtlDosPathNameToNtPathName_U(questionmarkInvalidNameW, &nameW, NULL, NULL);
331 attr.ObjectName = &nameW;
332 status = pNtCreateFile(&dir, GENERIC_READ|SYNCHRONIZE, &attr, &io, NULL, 0,
333 FILE_SHARE_READ, FILE_CREATE,
334 FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
335 ok(status == STATUS_OBJECT_NAME_INVALID,
336 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status);
337
338 status = pNtCreateFile(&file, GENERIC_WRITE|SYNCHRONIZE, &attr, &io, NULL, 0,
339 0, FILE_CREATE,
340 FILE_NON_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
341 ok(status == STATUS_OBJECT_NAME_INVALID,
342 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status);
343 pRtlFreeUnicodeString(&nameW);
344
345 pRtlDosPathNameToNtPathName_U(pipeInvalidNameW, &nameW, NULL, NULL);
346 attr.ObjectName = &nameW;
347 status = pNtCreateFile(&dir, GENERIC_READ|SYNCHRONIZE, &attr, &io, NULL, 0,
348 FILE_SHARE_READ, FILE_CREATE,
349 FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
350 ok(status == STATUS_OBJECT_NAME_INVALID,
351 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status);
352
353 status = pNtCreateFile(&file, GENERIC_WRITE|SYNCHRONIZE, &attr, &io, NULL, 0,
354 0, FILE_CREATE,
355 FILE_NON_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
356 ok(status == STATUS_OBJECT_NAME_INVALID,
357 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status);
358 pRtlFreeUnicodeString(&nameW);
359
360 pRtlInitUnicodeString( &nameW, pathInvalidNtW );
361 status = pNtCreateFile( &dir, GENERIC_READ|SYNCHRONIZE, &attr, &io, NULL, 0,
362 FILE_SHARE_READ, FILE_CREATE,
363 FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
364 ok( status == STATUS_OBJECT_NAME_INVALID,
365 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
366
367 status = pNtQueryFullAttributesFile( &attr, &info );
368 todo_wine ok( status == STATUS_OBJECT_NAME_INVALID,
369 "query %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
370
371 pRtlInitUnicodeString( &nameW, pathInvalidNt2W );
372 status = pNtCreateFile( &dir, GENERIC_READ|SYNCHRONIZE, &attr, &io, NULL, 0,
373 FILE_SHARE_READ, FILE_CREATE,
374 FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
375 ok( status == STATUS_OBJECT_NAME_INVALID,
376 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
377
378 status = pNtQueryFullAttributesFile( &attr, &info );
379 ok( status == STATUS_OBJECT_NAME_INVALID,
380 "query %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
381
382 pRtlInitUnicodeString( &nameW, pathInvalidDosW );
383 status = pNtCreateFile( &dir, GENERIC_READ|SYNCHRONIZE, &attr, &io, NULL, 0,
384 FILE_SHARE_READ, FILE_CREATE,
385 FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
386 ok( status == STATUS_OBJECT_NAME_INVALID,
387 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
388
389 status = pNtQueryFullAttributesFile( &attr, &info );
390 ok( status == STATUS_OBJECT_NAME_INVALID,
391 "query %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
392 }
393
394 static void open_file_test(void)
395 {
396 static const char testdata[] = "Hello World";
397 static WCHAR fooW[] = {'f','o','o',0};
398 NTSTATUS status;
399 HANDLE dir, root, handle, file;
400 WCHAR path[MAX_PATH], tmpfile[MAX_PATH];
401 BYTE data[1024];
402 OBJECT_ATTRIBUTES attr;
403 IO_STATUS_BLOCK io;
404 UNICODE_STRING nameW;
405 UINT i, len;
406 BOOL ret, restart = TRUE;
407 DWORD numbytes;
408
409 len = GetWindowsDirectoryW( path, MAX_PATH );
410 pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
411 attr.Length = sizeof(attr);
412 attr.RootDirectory = 0;
413 attr.ObjectName = &nameW;
414 attr.Attributes = OBJ_CASE_INSENSITIVE;
415 attr.SecurityDescriptor = NULL;
416 attr.SecurityQualityOfService = NULL;
417 status = pNtOpenFile( &dir, SYNCHRONIZE|FILE_LIST_DIRECTORY, &attr, &io,
418 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_NONALERT );
419 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
420 pRtlFreeUnicodeString( &nameW );
421
422 path[3] = 0; /* root of the drive */
423 pRtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL );
424 status = pNtOpenFile( &root, GENERIC_READ, &attr, &io,
425 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
426 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
427 pRtlFreeUnicodeString( &nameW );
428
429 /* test opening system dir with RootDirectory set to windows dir */
430 GetSystemDirectoryW( path, MAX_PATH );
431 while (path[len] == '\\') len++;
432 nameW.Buffer = path + len;
433 nameW.Length = lstrlenW(path + len) * sizeof(WCHAR);
434 attr.RootDirectory = dir;
435 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
436 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
437 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
438 CloseHandle( handle );
439
440 /* try uppercase name */
441 for (i = len; path[i]; i++) if (path[i] >= 'a' && path[i] <= 'z') path[i] -= 'a' - 'A';
442 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
443 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
444 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
445 CloseHandle( handle );
446
447 /* try with leading backslash */
448 nameW.Buffer--;
449 nameW.Length += sizeof(WCHAR);
450 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
451 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
452 ok( status == STATUS_INVALID_PARAMETER ||
453 status == STATUS_OBJECT_NAME_INVALID ||
454 status == STATUS_OBJECT_PATH_SYNTAX_BAD,
455 "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
456 if (!status) CloseHandle( handle );
457
458 /* try with empty name */
459 nameW.Length = 0;
460 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
461 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_DIRECTORY_FILE );
462 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
463 CloseHandle( handle );
464
465 /* try open by file id */
466
467 while (!pNtQueryDirectoryFile( dir, NULL, NULL, NULL, &io, data, sizeof(data),
468 FileIdBothDirectoryInformation, TRUE, NULL, restart ))
469 {
470 FILE_ID_BOTH_DIRECTORY_INFORMATION *info = (FILE_ID_BOTH_DIRECTORY_INFORMATION *)data;
471
472 restart = FALSE;
473
474 if (!info->FileId.QuadPart) continue;
475
476 nameW.Buffer = (WCHAR *)&info->FileId;
477 nameW.Length = sizeof(info->FileId);
478 info->FileName[info->FileNameLength/sizeof(WCHAR)] = 0;
479 attr.RootDirectory = dir;
480 /* We skip 'open' files by not specifying FILE_SHARE_WRITE */
481 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
482 FILE_SHARE_READ,
483 FILE_OPEN_BY_FILE_ID |
484 ((info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FILE_DIRECTORY_FILE : 0) );
485 ok( status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED || status == STATUS_NOT_IMPLEMENTED || status == STATUS_SHARING_VIOLATION,
486 "open %s failed %x\n", wine_dbgstr_w(info->FileName), status );
487 if (status == STATUS_NOT_IMPLEMENTED)
488 {
489 win_skip( "FILE_OPEN_BY_FILE_ID not supported\n" );
490 break;
491 }
492 if (status == STATUS_SHARING_VIOLATION)
493 trace( "%s is currently open\n", wine_dbgstr_w(info->FileName) );
494 if (!status)
495 {
496 BYTE buf[sizeof(FILE_ALL_INFORMATION) + MAX_PATH * sizeof(WCHAR)];
497
498 if (!pNtQueryInformationFile( handle, &io, buf, sizeof(buf), FileAllInformation ))
499 {
500 FILE_ALL_INFORMATION *fai = (FILE_ALL_INFORMATION *)buf;
501
502 /* check that it's the same file/directory */
503
504 /* don't check the size for directories */
505 if (!(info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY))
506 ok( info->EndOfFile.QuadPart == fai->StandardInformation.EndOfFile.QuadPart,
507 "mismatched file size for %s\n", wine_dbgstr_w(info->FileName));
508
509 ok( info->CreationTime.QuadPart == fai->BasicInformation.CreationTime.QuadPart,
510 "mismatched creation time for %s\n", wine_dbgstr_w(info->FileName));
511 }
512 CloseHandle( handle );
513
514 /* try same thing from drive root */
515 attr.RootDirectory = root;
516 status = pNtOpenFile( &handle, GENERIC_READ, &attr, &io,
517 FILE_SHARE_READ|FILE_SHARE_WRITE,
518 FILE_OPEN_BY_FILE_ID |
519 ((info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FILE_DIRECTORY_FILE : 0) );
520 ok( status == STATUS_SUCCESS || status == STATUS_NOT_IMPLEMENTED,
521 "open %s failed %x\n", wine_dbgstr_w(info->FileName), status );
522 if (!status) CloseHandle( handle );
523 }
524 }
525
526 CloseHandle( dir );
527 CloseHandle( root );
528
529 GetTempPathW( MAX_PATH, path );
530 GetTempFileNameW( path, fooW, 0, tmpfile );
531 pRtlDosPathNameToNtPathName_U( tmpfile, &nameW, NULL, NULL );
532
533 file = CreateFileW( tmpfile, FILE_WRITE_DATA, 0, NULL, CREATE_ALWAYS, 0, 0 );
534 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError() );
535 numbytes = 0xdeadbeef;
536 ret = WriteFile( file, testdata, sizeof(testdata) - 1, &numbytes, NULL );
537 ok( ret, "WriteFile failed with error %u\n", GetLastError() );
538 ok( numbytes == sizeof(testdata) - 1, "failed to write all data\n" );
539 CloseHandle( file );
540
541 attr.Length = sizeof(attr);
542 attr.RootDirectory = 0;
543 attr.ObjectName = &nameW;
544 attr.Attributes = OBJ_CASE_INSENSITIVE;
545 attr.SecurityDescriptor = NULL;
546 attr.SecurityQualityOfService = NULL;
547 status = pNtOpenFile( &file, SYNCHRONIZE|FILE_LIST_DIRECTORY, &attr, &io,
548 FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT );
549 ok( !status, "open %s failed %x\n", wine_dbgstr_w(nameW.Buffer), status );
550 pRtlFreeUnicodeString( &nameW );
551
552 numbytes = 0xdeadbeef;
553 memset( data, 0, sizeof(data) );
554 ret = ReadFile( file, data, sizeof(data), &numbytes, NULL );
555 ok( ret, "ReadFile failed with error %u\n", GetLastError() );
556 ok( numbytes == sizeof(testdata) - 1, "failed to read all data\n" );
557 ok( !memcmp( data, testdata, sizeof(testdata) - 1 ), "testdata doesn't match\n" );
558
559 nameW.Length = sizeof(fooW) - sizeof(WCHAR);
560 nameW.Buffer = fooW;
561 attr.RootDirectory = file;
562 attr.ObjectName = &nameW;
563 status = pNtOpenFile( &root, SYNCHRONIZE|FILE_LIST_DIRECTORY, &attr, &io,
564 FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT );
565 ok( status == STATUS_OBJECT_PATH_NOT_FOUND,
566 "expected STATUS_OBJECT_PATH_NOT_FOUND, got %08x\n", status );
567
568 nameW.Length = 0;
569 nameW.Buffer = NULL;
570 attr.RootDirectory = file;
571 attr.ObjectName = &nameW;
572 status = pNtOpenFile( &root, SYNCHRONIZE|FILE_LIST_DIRECTORY, &attr, &io,
573 FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT );
574 ok( !status, "open %s failed %x\n", wine_dbgstr_w(tmpfile), status );
575
576 numbytes = SetFilePointer( file, 0, 0, FILE_CURRENT );
577 ok( numbytes == sizeof(testdata) - 1, "SetFilePointer returned %u\n", numbytes );
578 numbytes = SetFilePointer( root, 0, 0, FILE_CURRENT );
579 ok( numbytes == 0, "SetFilePointer returned %u\n", numbytes );
580
581 numbytes = 0xdeadbeef;
582 memset( data, 0, sizeof(data) );
583 ret = ReadFile( root, data, sizeof(data), &numbytes, NULL );
584 ok( ret, "ReadFile failed with error %u\n", GetLastError() );
585 ok( numbytes == sizeof(testdata) - 1, "failed to read all data\n" );
586 ok( !memcmp( data, testdata, sizeof(testdata) - 1 ), "testdata doesn't match\n" );
587
588 numbytes = SetFilePointer( file, 0, 0, FILE_CURRENT );
589 ok( numbytes == sizeof(testdata) - 1, "SetFilePointer returned %u\n", numbytes );
590 numbytes = SetFilePointer( root, 0, 0, FILE_CURRENT );
591 ok( numbytes == sizeof(testdata) - 1, "SetFilePointer returned %u\n", numbytes );
592
593 CloseHandle( file );
594 CloseHandle( root );
595 DeleteFileW( tmpfile );
596 }
597
598 static void delete_file_test(void)
599 {
600 NTSTATUS ret;
601 OBJECT_ATTRIBUTES attr;
602 UNICODE_STRING nameW;
603 WCHAR pathW[MAX_PATH];
604 WCHAR pathsubW[MAX_PATH];
605 static const WCHAR testdirW[] = {'n','t','d','e','l','e','t','e','f','i','l','e',0};
606 static const WCHAR subdirW[] = {'\\','s','u','b',0};
607
608 ret = GetTempPathW(MAX_PATH, pathW);
609 if (!ret)
610 {
611 ok(0, "couldn't get temp dir\n");
612 return;
613 }
614 if (ret + sizeof(testdirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
615 {
616 ok(0, "MAX_PATH exceeded in constructing paths\n");
617 return;
618 }
619
620 lstrcatW(pathW, testdirW);
621 lstrcpyW(pathsubW, pathW);
622 lstrcatW(pathsubW, subdirW);
623
624 ret = CreateDirectoryW(pathW, NULL);
625 ok(ret == TRUE, "couldn't create directory ntdeletefile\n");
626 if (!pRtlDosPathNameToNtPathName_U(pathW, &nameW, NULL, NULL))
627 {
628 ok(0,"RtlDosPathNametoNtPathName_U failed\n");
629 return;
630 }
631
632 attr.Length = sizeof(attr);
633 attr.RootDirectory = 0;
634 attr.Attributes = OBJ_CASE_INSENSITIVE;
635 attr.ObjectName = &nameW;
636 attr.SecurityDescriptor = NULL;
637 attr.SecurityQualityOfService = NULL;
638
639 /* test NtDeleteFile on an empty directory */
640 ret = pNtDeleteFile(&attr);
641 ok(ret == STATUS_SUCCESS, "NtDeleteFile should succeed in removing an empty directory\n");
642 ret = RemoveDirectoryW(pathW);
643 ok(ret == FALSE, "expected to fail removing directory, NtDeleteFile should have removed it\n");
644
645 /* test NtDeleteFile on a non-empty directory */
646 ret = CreateDirectoryW(pathW, NULL);
647 ok(ret == TRUE, "couldn't create directory ntdeletefile ?!\n");
648 ret = CreateDirectoryW(pathsubW, NULL);
649 ok(ret == TRUE, "couldn't create directory subdir\n");
650 ret = pNtDeleteFile(&attr);
651 ok(ret == STATUS_SUCCESS, "expected NtDeleteFile to ret STATUS_SUCCESS\n");
652 ret = RemoveDirectoryW(pathsubW);
653 ok(ret == TRUE, "expected to remove directory ntdeletefile\\sub\n");
654 ret = RemoveDirectoryW(pathW);
655 ok(ret == TRUE, "expected to remove directory ntdeletefile, NtDeleteFile failed.\n");
656
657 pRtlFreeUnicodeString( &nameW );
658 }
659
660 static void read_file_test(void)
661 {
662 const char text[] = "foobar";
663 HANDLE handle, read, write;
664 NTSTATUS status;
665 IO_STATUS_BLOCK iosb, iosb2;
666 DWORD written;
667 int apc_count = 0;
668 char buffer[128];
669 LARGE_INTEGER offset;
670 HANDLE event = CreateEventA( NULL, TRUE, FALSE, NULL );
671 BOOL ret;
672
673 buffer[0] = 1;
674
675 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
676
677 /* try read with no data */
678 U(iosb).Status = 0xdeadbabe;
679 iosb.Information = 0xdeadbeef;
680 ok( is_signaled( read ), "read handle is not signaled\n" );
681 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
682 ok( status == STATUS_PENDING, "wrong status %x\n", status );
683 ok( !is_signaled( read ), "read handle is signaled\n" );
684 ok( !is_signaled( event ), "event is signaled\n" );
685 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
686 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
687 ok( !apc_count, "apc was called\n" );
688 ret = WriteFile( write, buffer, 1, &written, NULL );
689 ok(ret && written == 1, "WriteFile error %d\n", GetLastError());
690 /* iosb updated here by async i/o */
691 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
692 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
693 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
694 ok( !is_signaled( read ), "read handle is signaled\n" );
695 ok( is_signaled( event ), "event is not signaled\n" );
696 ok( !apc_count, "apc was called\n" );
697 apc_count = 0;
698 SleepEx( 1, FALSE ); /* non-alertable sleep */
699 ok( !apc_count, "apc was called\n" );
700 SleepEx( 1, TRUE ); /* alertable sleep */
701 ok( apc_count == 1, "apc not called\n" );
702
703 /* with no event, the pipe handle itself gets signaled */
704 apc_count = 0;
705 U(iosb).Status = 0xdeadbabe;
706 iosb.Information = 0xdeadbeef;
707 ok( !is_signaled( read ), "read handle is not signaled\n" );
708 status = pNtReadFile( read, 0, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
709 ok( status == STATUS_PENDING, "wrong status %x\n", status );
710 ok( !is_signaled( read ), "read handle is signaled\n" );
711 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
712 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
713 ok( !apc_count, "apc was called\n" );
714 ret = WriteFile( write, buffer, 1, &written, NULL );
715 ok(ret && written == 1, "WriteFile error %d\n", GetLastError());
716 /* iosb updated here by async i/o */
717 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
718 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
719 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
720 ok( is_signaled( read ), "read handle is signaled\n" );
721 ok( !apc_count, "apc was called\n" );
722 apc_count = 0;
723 SleepEx( 1, FALSE ); /* non-alertable sleep */
724 ok( !apc_count, "apc was called\n" );
725 SleepEx( 1, TRUE ); /* alertable sleep */
726 ok( apc_count == 1, "apc not called\n" );
727
728 /* now read with data ready */
729 apc_count = 0;
730 U(iosb).Status = 0xdeadbabe;
731 iosb.Information = 0xdeadbeef;
732 ResetEvent( event );
733 ret = WriteFile( write, buffer, 1, &written, NULL );
734 ok(ret && written == 1, "WriteFile error %d\n", GetLastError());
735 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
736 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
737 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
738 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
739 ok( is_signaled( event ), "event is not signaled\n" );
740 ok( !apc_count, "apc was called\n" );
741 SleepEx( 1, FALSE ); /* non-alertable sleep */
742 ok( !apc_count, "apc was called\n" );
743 SleepEx( 1, TRUE ); /* alertable sleep */
744 ok( apc_count == 1, "apc not called\n" );
745
746 /* try read with no data */
747 apc_count = 0;
748 U(iosb).Status = 0xdeadbabe;
749 iosb.Information = 0xdeadbeef;
750 ok( is_signaled( event ), "event is not signaled\n" ); /* check that read resets the event */
751 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
752 ok( status == STATUS_PENDING, "wrong status %x\n", status );
753 ok( !is_signaled( event ), "event is signaled\n" );
754 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
755 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
756 ok( !apc_count, "apc was called\n" );
757 ret = WriteFile( write, buffer, 1, &written, NULL );
758 ok(ret && written == 1, "WriteFile error %d\n", GetLastError());
759 /* partial read is good enough */
760 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
761 ok( is_signaled( event ), "event is signaled\n" );
762 ok( U(iosb).Status == 0, "wrong status %x\n", U(iosb).Status );
763 ok( iosb.Information == 1, "wrong info %lu\n", iosb.Information );
764 ok( !apc_count, "apc was called\n" );
765 SleepEx( 1, TRUE ); /* alertable sleep */
766 ok( apc_count == 1, "apc was not called\n" );
767
768 /* read from disconnected pipe */
769 apc_count = 0;
770 U(iosb).Status = 0xdeadbabe;
771 iosb.Information = 0xdeadbeef;
772 CloseHandle( write );
773 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
774 ok( status == STATUS_PIPE_BROKEN, "wrong status %x\n", status );
775 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
776 ok( iosb.Information == 0xdeadbeef, "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, "apc was called\n" );
781 CloseHandle( read );
782
783 /* read from closed handle */
784 apc_count = 0;
785 U(iosb).Status = 0xdeadbabe;
786 iosb.Information = 0xdeadbeef;
787 SetEvent( event );
788 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 1, NULL, NULL );
789 ok( status == STATUS_INVALID_HANDLE, "wrong status %x\n", status );
790 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
791 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
792 ok( is_signaled( event ), "event is signaled\n" ); /* not reset on invalid handle */
793 ok( !apc_count, "apc was called\n" );
794 SleepEx( 1, TRUE ); /* alertable sleep */
795 ok( !apc_count, "apc was called\n" );
796
797 /* disconnect while async read is in progress */
798 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
799 apc_count = 0;
800 U(iosb).Status = 0xdeadbabe;
801 iosb.Information = 0xdeadbeef;
802 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
803 ok( status == STATUS_PENDING, "wrong status %x\n", status );
804 ok( !is_signaled( event ), "event is signaled\n" );
805 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
806 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
807 ok( !apc_count, "apc was called\n" );
808 CloseHandle( write );
809 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
810 ok( U(iosb).Status == STATUS_PIPE_BROKEN, "wrong status %x\n", U(iosb).Status );
811 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
812 ok( is_signaled( event ), "event is signaled\n" );
813 ok( !apc_count, "apc was called\n" );
814 SleepEx( 1, TRUE ); /* alertable sleep */
815 ok( apc_count == 1, "apc was not called\n" );
816 CloseHandle( read );
817
818 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
819 ret = DuplicateHandle(GetCurrentProcess(), read, GetCurrentProcess(), &handle, 0, TRUE, DUPLICATE_SAME_ACCESS);
820 ok(ret, "Failed to duplicate handle: %d\n", GetLastError());
821
822 apc_count = 0;
823 U(iosb).Status = 0xdeadbabe;
824 iosb.Information = 0xdeadbeef;
825 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
826 ok( status == STATUS_PENDING, "wrong status %x\n", status );
827 ok( !is_signaled( event ), "event is signaled\n" );
828 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
829 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
830 ok( !apc_count, "apc was called\n" );
831 /* Cancel by other handle */
832 status = pNtCancelIoFile( read, &iosb2 );
833 ok(status == STATUS_SUCCESS, "failed to cancel by different handle: %x\n", status);
834 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
835 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
836 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
837 ok( is_signaled( event ), "event is signaled\n" );
838 ok( !apc_count, "apc was called\n" );
839 SleepEx( 1, TRUE ); /* alertable sleep */
840 ok( apc_count == 1, "apc was not called\n" );
841
842 apc_count = 0;
843 U(iosb).Status = 0xdeadbabe;
844 iosb.Information = 0xdeadbeef;
845 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
846 ok( status == STATUS_PENDING, "wrong status %x\n", status );
847 ok( !is_signaled( event ), "event is signaled\n" );
848 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
849 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
850 ok( !apc_count, "apc was called\n" );
851 /* Close queued handle */
852 CloseHandle( read );
853 SleepEx( 1, TRUE ); /* alertable sleep */
854 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
855 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
856 status = pNtCancelIoFile( read, &iosb2 );
857 ok(status == STATUS_INVALID_HANDLE, "cancelled by closed handle?\n");
858 status = pNtCancelIoFile( handle, &iosb2 );
859 ok(status == STATUS_SUCCESS, "failed to cancel: %x\n", status);
860 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
861 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
862 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
863 ok( is_signaled( event ), "event is signaled\n" );
864 ok( !apc_count, "apc was called\n" );
865 SleepEx( 1, TRUE ); /* alertable sleep */
866 ok( apc_count == 1, "apc was not called\n" );
867 CloseHandle( handle );
868 CloseHandle( write );
869
870 if (pNtCancelIoFileEx)
871 {
872 /* Basic Cancel Ex */
873 if (!create_pipe( &read, &write, FILE_FLAG_OVERLAPPED, 4096 )) return;
874
875 apc_count = 0;
876 U(iosb).Status = 0xdeadbabe;
877 iosb.Information = 0xdeadbeef;
878 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
879 ok( status == STATUS_PENDING, "wrong status %x\n", status );
880 ok( !is_signaled( event ), "event is signaled\n" );
881 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
882 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
883 ok( !apc_count, "apc was called\n" );
884 status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
885 ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
886 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
887 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
888 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
889 ok( is_signaled( event ), "event is signaled\n" );
890 ok( !apc_count, "apc was called\n" );
891 SleepEx( 1, TRUE ); /* alertable sleep */
892 ok( apc_count == 1, "apc was not called\n" );
893
894 /* Duplicate iosb */
895 apc_count = 0;
896 U(iosb).Status = 0xdeadbabe;
897 iosb.Information = 0xdeadbeef;
898 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
899 ok( status == STATUS_PENDING, "wrong status %x\n", status );
900 ok( !is_signaled( event ), "event is signaled\n" );
901 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
902 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
903 ok( !apc_count, "apc was called\n" );
904 status = pNtReadFile( read, event, apc, &apc_count, &iosb, buffer, 2, NULL, NULL );
905 ok( status == STATUS_PENDING, "wrong status %x\n", status );
906 ok( !is_signaled( event ), "event is signaled\n" );
907 ok( U(iosb).Status == 0xdeadbabe, "wrong status %x\n", U(iosb).Status );
908 ok( iosb.Information == 0xdeadbeef, "wrong info %lu\n", iosb.Information );
909 ok( !apc_count, "apc was called\n" );
910 status = pNtCancelIoFileEx( read, &iosb, &iosb2 );
911 ok(status == STATUS_SUCCESS, "Failed to cancel I/O\n");
912 Sleep(1); /* FIXME: needed for wine to run the i/o apc */
913 ok( U(iosb).Status == STATUS_CANCELLED, "wrong status %x\n", U(iosb).Status );
914 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
915 ok( is_signaled( event ), "event is signaled\n" );
916 ok( !apc_count, "apc was called\n" );
917 SleepEx( 1, TRUE ); /* alertable sleep */
918 ok( apc_count == 2, "apc was not called\n" );
919
920 CloseHandle( read );
921 CloseHandle( write );
922 }
923
924 /* now try a real file */
925 if (!(handle = create_temp_file( FILE_FLAG_OVERLAPPED ))) return;
926 apc_count = 0;
927 U(iosb).Status = 0xdeadbabe;
928 iosb.Information = 0xdeadbeef;
929 offset.QuadPart = 0;
930 ResetEvent( event );
931 status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
932 ok( status == STATUS_SUCCESS || status == STATUS_PENDING, "wrong status %x\n", status );
933 if (status == STATUS_PENDING) WaitForSingleObject( event, 1000 );
934 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
935 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
936 ok( is_signaled( event ), "event is signaled\n" );
937 ok( !apc_count, "apc was called\n" );
938 SleepEx( 1, TRUE ); /* alertable sleep */
939 ok( apc_count == 1, "apc was not called\n" );
940
941 apc_count = 0;
942 U(iosb).Status = 0xdeadbabe;
943 iosb.Information = 0xdeadbeef;
944 offset.QuadPart = 0;
945 ResetEvent( event );
946 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
947 ok( status == STATUS_SUCCESS ||
948 status == STATUS_PENDING, /* vista */
949 "wrong status %x\n", status );
950 if (status == STATUS_PENDING) WaitForSingleObject( event, 1000 );
951 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
952 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
953 ok( is_signaled( event ), "event is signaled\n" );
954 ok( !apc_count, "apc was called\n" );
955 SleepEx( 1, TRUE ); /* alertable sleep */
956 ok( apc_count == 1, "apc was not called\n" );
957
958 /* read beyond eof */
959 apc_count = 0;
960 U(iosb).Status = 0xdeadbabe;
961 iosb.Information = 0xdeadbeef;
962 offset.QuadPart = strlen(text) + 2;
963 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
964 ok(status == STATUS_PENDING || status == STATUS_END_OF_FILE /* before Vista */, "expected STATUS_PENDING or STATUS_END_OF_FILE, got %#x\n", status);
965 if (status == STATUS_PENDING) /* vista */
966 {
967 WaitForSingleObject( event, 1000 );
968 ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
969 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
970 ok( is_signaled( event ), "event is signaled\n" );
971 ok( !apc_count, "apc was called\n" );
972 SleepEx( 1, TRUE ); /* alertable sleep */
973 ok( apc_count == 1, "apc was not called\n" );
974 }
975 CloseHandle( handle );
976
977 /* now a non-overlapped file */
978 if (!(handle = create_temp_file(0))) return;
979 apc_count = 0;
980 U(iosb).Status = 0xdeadbabe;
981 iosb.Information = 0xdeadbeef;
982 offset.QuadPart = 0;
983 status = pNtWriteFile( handle, event, apc, &apc_count, &iosb, text, strlen(text), &offset, NULL );
984 ok( status == STATUS_END_OF_FILE ||
985 status == STATUS_SUCCESS ||
986 status == STATUS_PENDING, /* vista */
987 "wrong status %x\n", status );
988 if (status == STATUS_PENDING) WaitForSingleObject( event, 1000 );
989 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
990 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
991 ok( is_signaled( event ), "event is signaled\n" );
992 ok( !apc_count, "apc was called\n" );
993 SleepEx( 1, TRUE ); /* alertable sleep */
994 ok( apc_count == 1, "apc was not called\n" );
995
996 apc_count = 0;
997 U(iosb).Status = 0xdeadbabe;
998 iosb.Information = 0xdeadbeef;
999 offset.QuadPart = 0;
1000 ResetEvent( event );
1001 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, strlen(text) + 10, &offset, NULL );
1002 ok( status == STATUS_SUCCESS, "wrong status %x\n", status );
1003 ok( U(iosb).Status == STATUS_SUCCESS, "wrong status %x\n", U(iosb).Status );
1004 ok( iosb.Information == strlen(text), "wrong info %lu\n", iosb.Information );
1005 ok( is_signaled( event ), "event is signaled\n" );
1006 ok( !apc_count, "apc was called\n" );
1007 SleepEx( 1, TRUE ); /* alertable sleep */
1008 todo_wine ok( !apc_count, "apc was called\n" );
1009
1010 /* read beyond eof */
1011 apc_count = 0;
1012 U(iosb).Status = 0xdeadbabe;
1013 iosb.Information = 0xdeadbeef;
1014 offset.QuadPart = strlen(text) + 2;
1015 ResetEvent( event );
1016 status = pNtReadFile( handle, event, apc, &apc_count, &iosb, buffer, 2, &offset, NULL );
1017 ok( status == STATUS_END_OF_FILE, "wrong status %x\n", status );
1018 ok( U(iosb).Status == STATUS_END_OF_FILE, "wrong status %x\n", U(iosb).Status );
1019 ok( iosb.Information == 0, "wrong info %lu\n", iosb.Information );
1020 ok( is_signaled( event ), "event is not signaled\n" );
1021 ok( !apc_count, "apc was called\n" );
1022 SleepEx( 1, TRUE ); /* alertable sleep */
1023 ok( !apc_count, "apc was called\n" );
1024
1025 CloseHandle( handle );
1026
1027 CloseHandle( event );
1028 }
1029
1030 static void append_file_test(void)
1031 {
1032 static const char text[6] = "foobar";
1033 HANDLE handle;
1034 NTSTATUS status;
1035 IO_STATUS_BLOCK iosb;
1036 LARGE_INTEGER offset;
1037 char path[MAX_PATH], buffer[MAX_PATH], buf[16];
1038 DWORD ret;
1039
1040 GetTempPathA( MAX_PATH, path );
1041 GetTempFileNameA( path, "foo", 0, buffer );
1042
1043 handle = CreateFileA(buffer, FILE_WRITE_DATA, 0, NULL, CREATE_ALWAYS, 0, 0);
1044 ok(handle != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1045
1046 U(iosb).Status = -1;
1047 iosb.Information = -1;
1048 status = pNtWriteFile(handle, NULL, NULL, NULL, &iosb, text, 2, NULL, NULL);
1049 ok(status == STATUS_SUCCESS, "NtWriteFile error %#x\n", status);
1050 ok(U(iosb).Status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#x\n", U(iosb).Status);
1051 ok(iosb.Information == 2, "expected 2, got %lu\n", iosb.Information);
1052
1053 CloseHandle(handle);
1054
1055 /* It is possible to open a file with only FILE_APPEND_DATA access flags.
1056 It matches the O_WRONLY|O_APPEND open() posix behavior */
1057 handle = CreateFileA(buffer, FILE_APPEND_DATA, 0, NULL, OPEN_EXISTING, 0, 0);
1058 ok(handle != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1059
1060 U(iosb).Status = -1;
1061 iosb.Information = -1;
1062 offset.QuadPart = 1;
1063 status = pNtWriteFile(handle, NULL, NULL, NULL, &iosb, text + 2, 2, &offset, NULL);
1064 ok(status == STATUS_SUCCESS, "NtWriteFile error %#x\n", status);
1065 ok(U(iosb).Status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#x\n", U(iosb).Status);
1066 ok(iosb.Information == 2, "expected 2, got %lu\n", iosb.Information);
1067
1068 ret = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
1069 ok(ret == 4, "expected 4, got %u\n", ret);
1070
1071 U(iosb).Status = -1;
1072 iosb.Information = -1;
1073 offset.QuadPart = 3;
1074 status = pNtWriteFile(handle, NULL, NULL, NULL, &iosb, text + 4, 2, &offset, NULL);
1075 ok(status == STATUS_SUCCESS, "NtWriteFile error %#x\n", status);
1076 ok(U(iosb).Status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#x\n", U(iosb).Status);
1077 ok(iosb.Information == 2, "expected 2, got %lu\n", iosb.Information);
1078
1079 ret = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
1080 ok(ret == 6, "expected 6, got %u\n", ret);
1081
1082 CloseHandle(handle);
1083
1084 handle = CreateFileA(buffer, FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA, 0, NULL, OPEN_EXISTING, 0, 0);
1085 ok(handle != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1086
1087 memset(buf, 0, sizeof(buf));
1088 U(iosb).Status = -1;
1089 iosb.Information = -1;
1090 offset.QuadPart = 0;
1091 status = pNtReadFile(handle, 0, NULL, NULL, &iosb, buf, sizeof(buf), &offset, NULL);
1092 ok(status == STATUS_SUCCESS, "NtReadFile error %#x\n", status);
1093 ok(U(iosb).Status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#x\n", U(iosb).Status);
1094 ok(iosb.Information == 6, "expected 6, got %lu\n", iosb.Information);
1095 buf[6] = 0;
1096 ok(memcmp(buf, text, 6) == 0, "wrong file contents: %s\n", buf);
1097
1098 U(iosb).Status = -1;
1099 iosb.Information = -1;
1100 offset.QuadPart = 0;
1101 status = pNtWriteFile(handle, NULL, NULL, NULL, &iosb, text + 3, 3, &offset, NULL);
1102 ok(status == STATUS_SUCCESS, "NtWriteFile error %#x\n", status);
1103 ok(U(iosb).Status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#x\n", U(iosb).Status);
1104 ok(iosb.Information == 3, "expected 3, got %lu\n", iosb.Information);
1105
1106 memset(buf, 0, sizeof(buf));
1107 U(iosb).Status = -1;
1108 iosb.Information = -1;
1109 offset.QuadPart = 0;
1110 status = pNtReadFile(handle, 0, NULL, NULL, &iosb, buf, sizeof(buf), &offset, NULL);
1111 ok(status == STATUS_SUCCESS, "NtReadFile error %#x\n", status);
1112 ok(U(iosb).Status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %#x\n", U(iosb).Status);
1113 ok(iosb.Information == 6, "expected 6, got %lu\n", iosb.Information);
1114 buf[6] = 0;
1115 ok(memcmp(buf, "barbar", 6) == 0, "wrong file contents: %s\n", buf);
1116
1117 CloseHandle(handle);
1118 DeleteFileA(buffer);
1119 }
1120
1121 static void nt_mailslot_test(void)
1122 {
1123 HANDLE hslot;
1124 ACCESS_MASK DesiredAccess;
1125 OBJECT_ATTRIBUTES attr;
1126
1127 ULONG CreateOptions;
1128 ULONG MailslotQuota;
1129 ULONG MaxMessageSize;
1130 LARGE_INTEGER TimeOut;
1131 IO_STATUS_BLOCK IoStatusBlock;
1132 NTSTATUS rc;
1133 UNICODE_STRING str;
1134 WCHAR buffer1[] = { '\\','?','?','\\','M','A','I','L','S','L','O','T','\\',
1135 'R',':','\\','F','R','E','D','\0' };
1136
1137 TimeOut.QuadPart = -1;
1138
1139 pRtlInitUnicodeString(&str, buffer1);
1140 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
1141 CreateOptions = MailslotQuota = MaxMessageSize = 0;
1142 DesiredAccess = GENERIC_READ;
1143
1144 /*
1145 * Check for NULL pointer handling
1146 */
1147 rc = pNtCreateMailslotFile(NULL, DesiredAccess,
1148 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
1149 &TimeOut);
1150 ok( rc == STATUS_ACCESS_VIOLATION ||
1151 rc == STATUS_INVALID_PARAMETER, /* win2k3 */
1152 "rc = %x not STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER\n", rc);
1153
1154 /*
1155 * Test to see if the Timeout can be NULL
1156 */
1157 hslot = (HANDLE)0xdeadbeef;
1158 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
1159 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
1160 NULL);
1161 ok( rc == STATUS_SUCCESS ||
1162 rc == STATUS_INVALID_PARAMETER, /* win2k3 */
1163 "rc = %x not STATUS_SUCCESS or STATUS_INVALID_PARAMETER\n", rc);
1164 ok( hslot != 0, "Handle is invalid\n");
1165
1166 if ( rc == STATUS_SUCCESS ) pNtClose(hslot);
1167
1168 /*
1169 * Test that the length field is checked properly
1170 */
1171 attr.Length = 0;
1172 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
1173 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
1174 &TimeOut);
1175 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
1176
1177 if (rc == STATUS_SUCCESS) pNtClose(hslot);
1178
1179 attr.Length = sizeof(OBJECT_ATTRIBUTES)+1;
1180 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
1181 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
1182 &TimeOut);
1183 todo_wine ok( rc == STATUS_INVALID_PARAMETER, "rc = %x not c000000d STATUS_INVALID_PARAMETER\n", rc);
1184
1185 if (rc == STATUS_SUCCESS) pNtClose(hslot);
1186
1187 /*
1188 * Test handling of a NULL unicode string in ObjectName
1189 */
1190 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
1191 attr.ObjectName = NULL;
1192 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
1193 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
1194 &TimeOut);
1195 ok( rc == STATUS_OBJECT_PATH_SYNTAX_BAD ||
1196 rc == STATUS_INVALID_PARAMETER,
1197 "rc = %x not STATUS_OBJECT_PATH_SYNTAX_BAD or STATUS_INVALID_PARAMETER\n", rc);
1198
1199 if (rc == STATUS_SUCCESS) pNtClose(hslot);
1200
1201 /*
1202 * Test a valid call
1203 */
1204 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, 0, NULL);
1205 rc = pNtCreateMailslotFile(&hslot, DesiredAccess,
1206 &attr, &IoStatusBlock, CreateOptions, MailslotQuota, MaxMessageSize,
1207 &TimeOut);
1208 ok( rc == STATUS_SUCCESS, "Create MailslotFile failed rc = %x\n", rc);
1209 ok( hslot != 0, "Handle is invalid\n");
1210
1211 rc = pNtClose(hslot);
1212 ok( rc == STATUS_SUCCESS, "NtClose failed\n");
1213 }
1214
1215 static void test_iocp_setcompletion(HANDLE h)
1216 {
1217 NTSTATUS res;
1218 ULONG count;
1219 SIZE_T size = 3;
1220
1221 if (sizeof(size) > 4) size |= (ULONGLONG)0x12345678 << 32;
1222
1223 res = pNtSetIoCompletion( h, CKEY_FIRST, CVALUE_FIRST, STATUS_INVALID_DEVICE_REQUEST, size );
1224 ok( res == STATUS_SUCCESS, "NtSetIoCompletion failed: %x\n", res );
1225
1226 count = get_pending_msgs(h);
1227 ok( count == 1, "Unexpected msg count: %d\n", count );
1228
1229 if (get_msg(h))
1230 {
1231 ok( completionKey == CKEY_FIRST, "Invalid completion key: %lx\n", completionKey );
1232 ok( ioSb.Information == size, "Invalid ioSb.Information: %lu\n", ioSb.Information );
1233 ok( U(ioSb).Status == STATUS_INVALID_DEVICE_REQUEST, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1234 ok( completionValue == CVALUE_FIRST, "Invalid completion value: %lx\n", completionValue );
1235 }
1236
1237 count = get_pending_msgs(h);
1238 ok( !count, "Unexpected msg count: %d\n", count );
1239 }
1240
1241 static void test_iocp_fileio(HANDLE h)
1242 {
1243 static const char pipe_name[] = "\\\\.\\pipe\\iocompletiontestnamedpipe";
1244
1245 IO_STATUS_BLOCK iosb;
1246 FILE_COMPLETION_INFORMATION fci = {h, CKEY_SECOND};
1247 HANDLE hPipeSrv, hPipeClt;
1248 NTSTATUS res;
1249
1250 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
1251 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
1252 if (hPipeSrv != INVALID_HANDLE_VALUE )
1253 {
1254 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
1255 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
1256 if (hPipeClt != INVALID_HANDLE_VALUE)
1257 {
1258 U(iosb).Status = 0xdeadbeef;
1259 res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
1260 ok( res == STATUS_INVALID_PARAMETER, "Unexpected NtSetInformationFile on non-overlapped handle: %x\n", res );
1261 ok( U(iosb).Status == STATUS_INVALID_PARAMETER /* 98 */ || U(iosb).Status == 0xdeadbeef /* NT4+ */,
1262 "Unexpected iosb.Status on non-overlapped handle: %x\n", U(iosb).Status );
1263 CloseHandle(hPipeClt);
1264 }
1265 CloseHandle( hPipeSrv );
1266 }
1267
1268 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
1269 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
1270 if (hPipeSrv == INVALID_HANDLE_VALUE )
1271 return;
1272
1273 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
1274 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
1275 if (hPipeClt != INVALID_HANDLE_VALUE)
1276 {
1277 OVERLAPPED o = {0,};
1278 BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
1279 DWORD read;
1280 long count;
1281
1282 U(iosb).Status = 0xdeadbeef;
1283 res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
1284 ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
1285 ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
1286
1287 memset( send_buf, 0, TEST_BUF_LEN );
1288 memset( recv_buf, 0xde, TEST_BUF_LEN );
1289 count = get_pending_msgs(h);
1290 ok( !count, "Unexpected msg count: %ld\n", count );
1291 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
1292 count = get_pending_msgs(h);
1293 ok( !count, "Unexpected msg count: %ld\n", count );
1294 WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
1295
1296 if (get_msg(h))
1297 {
1298 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1299 ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1300 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1301 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1302 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] );
1303 }
1304 count = get_pending_msgs(h);
1305 ok( !count, "Unexpected msg count: %ld\n", count );
1306
1307 memset( send_buf, 0, TEST_BUF_LEN );
1308 memset( recv_buf, 0xde, TEST_BUF_LEN );
1309 WriteFile( hPipeClt, send_buf, 2, &read, NULL );
1310 count = get_pending_msgs(h);
1311 ok( !count, "Unexpected msg count: %ld\n", count );
1312 ReadFile( hPipeSrv, recv_buf, 2, &read, &o);
1313 count = get_pending_msgs(h);
1314 ok( count == 1, "Unexpected msg count: %ld\n", count );
1315 if (get_msg(h))
1316 {
1317 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1318 ok( ioSb.Information == 2, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1319 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1320 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1321 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] );
1322 }
1323
1324 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
1325 CloseHandle( hPipeSrv );
1326 count = get_pending_msgs(h);
1327 ok( count == 1, "Unexpected msg count: %ld\n", count );
1328 if (get_msg(h))
1329 {
1330 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1331 ok( ioSb.Information == 0, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1332 /* wine sends wrong status here */
1333 todo_wine ok( U(ioSb).Status == STATUS_PIPE_BROKEN, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1334 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1335 }
1336 }
1337
1338 CloseHandle( hPipeClt );
1339
1340 /* test associating a completion port with a handle after an async is queued */
1341 hPipeSrv = CreateNamedPipeA( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 4, 1024, 1024, 1000, NULL );
1342 ok( hPipeSrv != INVALID_HANDLE_VALUE, "Cannot create named pipe\n" );
1343 if (hPipeSrv == INVALID_HANDLE_VALUE )
1344 return;
1345 hPipeClt = CreateFileA( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, NULL );
1346 ok( hPipeClt != INVALID_HANDLE_VALUE, "Cannot connect to pipe\n" );
1347 if (hPipeClt != INVALID_HANDLE_VALUE)
1348 {
1349 OVERLAPPED o = {0,};
1350 BYTE send_buf[TEST_BUF_LEN], recv_buf[TEST_BUF_LEN];
1351 DWORD read;
1352 long count;
1353
1354 memset( send_buf, 0, TEST_BUF_LEN );
1355 memset( recv_buf, 0xde, TEST_BUF_LEN );
1356 count = get_pending_msgs(h);
1357 ok( !count, "Unexpected msg count: %ld\n", count );
1358 ReadFile( hPipeSrv, recv_buf, TEST_BUF_LEN, &read, &o);
1359
1360 U(iosb).Status = 0xdeadbeef;
1361 res = pNtSetInformationFile( hPipeSrv, &iosb, &fci, sizeof(fci), FileCompletionInformation );
1362 ok( res == STATUS_SUCCESS, "NtSetInformationFile failed: %x\n", res );
1363 ok( U(iosb).Status == STATUS_SUCCESS, "iosb.Status invalid: %x\n", U(iosb).Status );
1364 count = get_pending_msgs(h);
1365 ok( !count, "Unexpected msg count: %ld\n", count );
1366
1367 WriteFile( hPipeClt, send_buf, TEST_BUF_LEN, &read, NULL );
1368
1369 if (get_msg(h))
1370 {
1371 ok( completionKey == CKEY_SECOND, "Invalid completion key: %lx\n", completionKey );
1372 ok( ioSb.Information == 3, "Invalid ioSb.Information: %ld\n", ioSb.Information );
1373 ok( U(ioSb).Status == STATUS_SUCCESS, "Invalid ioSb.Status: %x\n", U(ioSb).Status);
1374 ok( completionValue == (ULONG_PTR)&o, "Invalid completion value: %lx\n", completionValue );
1375 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] );
1376 }
1377 count = get_pending_msgs(h);
1378 ok( !count, "Unexpected msg count: %ld\n", count );
1379 }
1380
1381 CloseHandle( hPipeSrv );
1382 CloseHandle( hPipeClt );
1383 }
1384
1385 static void test_file_full_size_information(void)
1386 {
1387 IO_STATUS_BLOCK io;
1388 FILE_FS_FULL_SIZE_INFORMATION ffsi;
1389 FILE_FS_SIZE_INFORMATION fsi;
1390 HANDLE h;
1391 NTSTATUS res;
1392
1393 if(!(h = create_temp_file(0))) return ;
1394
1395 memset(&ffsi,0,sizeof(ffsi));
1396 memset(&fsi,0,sizeof(fsi));
1397
1398 /* Assume No Quota Settings configured on Wine Testbot */
1399 res = pNtQueryVolumeInformationFile(h, &io, &ffsi, sizeof ffsi, FileFsFullSizeInformation);
1400 ok(res == STATUS_SUCCESS, "cannot get attributes, res %x\n", res);
1401 res = pNtQueryVolumeInformationFile(h, &io, &fsi, sizeof fsi, FileFsSizeInformation);
1402 ok(res == STATUS_SUCCESS, "cannot get attributes, res %x\n", res);
1403
1404 /* Test for FileFsSizeInformation */
1405 ok(fsi.TotalAllocationUnits.QuadPart > 0,
1406 "[fsi] TotalAllocationUnits expected positive, got 0x%s\n",
1407 debugstr_longlong(fsi.TotalAllocationUnits.QuadPart));
1408 ok(fsi.AvailableAllocationUnits.QuadPart > 0,
1409 "[fsi] AvailableAllocationUnits expected positive, got 0x%s\n",
1410 debugstr_longlong(fsi.AvailableAllocationUnits.QuadPart));
1411
1412 /* Assume file system is NTFS */
1413 ok(fsi.BytesPerSector == 512, "[fsi] BytesPerSector expected 512, got %d\n",fsi.BytesPerSector);
1414 ok(fsi.SectorsPerAllocationUnit == 8, "[fsi] SectorsPerAllocationUnit expected 8, got %d\n",fsi.SectorsPerAllocationUnit);
1415
1416 ok(ffsi.TotalAllocationUnits.QuadPart > 0,
1417 "[ffsi] TotalAllocationUnits expected positive, got negative value 0x%s\n",
1418 debugstr_longlong(ffsi.TotalAllocationUnits.QuadPart));
1419 ok(ffsi.CallerAvailableAllocationUnits.QuadPart > 0,
1420 "[ffsi] CallerAvailableAllocationUnits expected positive, got negative value 0x%s\n",
1421 debugstr_longlong(ffsi.CallerAvailableAllocationUnits.QuadPart));
1422 ok(ffsi.ActualAvailableAllocationUnits.QuadPart > 0,
1423 "[ffsi] ActualAvailableAllocationUnits expected positive, got negative value 0x%s\n",
1424 debugstr_longlong(ffsi.ActualAvailableAllocationUnits.QuadPart));
1425 ok(ffsi.TotalAllocationUnits.QuadPart == fsi.TotalAllocationUnits.QuadPart,
1426 "[ffsi] TotalAllocationUnits error fsi:0x%s, ffsi:0x%s\n",
1427 debugstr_longlong(fsi.TotalAllocationUnits.QuadPart),
1428 debugstr_longlong(ffsi.TotalAllocationUnits.QuadPart));
1429 ok(ffsi.CallerAvailableAllocationUnits.QuadPart == fsi.AvailableAllocationUnits.QuadPart,
1430 "[ffsi] CallerAvailableAllocationUnits error fsi:0x%s, ffsi: 0x%s\n",
1431 debugstr_longlong(fsi.AvailableAllocationUnits.QuadPart),
1432 debugstr_longlong(ffsi.CallerAvailableAllocationUnits.QuadPart));
1433
1434 /* Assume file system is NTFS */
1435 ok(ffsi.BytesPerSector == 512, "[ffsi] BytesPerSector expected 512, got %d\n",ffsi.BytesPerSector);
1436 ok(ffsi.SectorsPerAllocationUnit == 8, "[ffsi] SectorsPerAllocationUnit expected 8, got %d\n",ffsi.SectorsPerAllocationUnit);
1437
1438 CloseHandle( h );
1439 }
1440
1441 static void test_file_basic_information(void)
1442 {
1443 IO_STATUS_BLOCK io;
1444 FILE_BASIC_INFORMATION fbi;
1445 HANDLE h;
1446 int res;
1447 int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
1448
1449 if (!(h = create_temp_file(0))) return;
1450
1451 /* Check default first */
1452 memset(&fbi, 0, sizeof(fbi));
1453 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1454 ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1455 ok ( (fbi.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
1456 "attribute %x not expected\n", fbi.FileAttributes );
1457
1458 /* Then SYSTEM */
1459 /* Clear fbi to avoid setting times */
1460 memset(&fbi, 0, sizeof(fbi));
1461 fbi.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
1462 U(io).Status = 0xdeadbeef;
1463 res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1464 ok ( res == STATUS_SUCCESS, "can't set system attribute, NtSetInformationFile returned %x\n", res );
1465 ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status is %x\n", U(io).Status );
1466
1467 memset(&fbi, 0, sizeof(fbi));
1468 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1469 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1470 ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_SYSTEM, "attribute %x not FILE_ATTRIBUTE_SYSTEM (ok in old linux without xattr)\n", fbi.FileAttributes );
1471
1472 /* Then HIDDEN */
1473 memset(&fbi, 0, sizeof(fbi));
1474 fbi.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1475 U(io).Status = 0xdeadbeef;
1476 res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1477 ok ( res == STATUS_SUCCESS, "can't set system attribute, NtSetInformationFile returned %x\n", res );
1478 ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status is %x\n", U(io).Status );
1479
1480 memset(&fbi, 0, sizeof(fbi));
1481 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1482 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1483 ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_HIDDEN, "attribute %x not FILE_ATTRIBUTE_HIDDEN (ok in old linux without xattr)\n", fbi.FileAttributes );
1484
1485 /* Check NORMAL last of all (to make sure we can clear attributes) */
1486 memset(&fbi, 0, sizeof(fbi));
1487 fbi.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1488 U(io).Status = 0xdeadbeef;
1489 res = pNtSetInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1490 ok ( res == STATUS_SUCCESS, "can't set normal attribute, NtSetInformationFile returned %x\n", res );
1491 ok ( U(io).Status == STATUS_SUCCESS, "can't set normal attribute, io.Status is %x\n", U(io).Status );
1492
1493 memset(&fbi, 0, sizeof(fbi));
1494 res = pNtQueryInformationFile(h, &io, &fbi, sizeof fbi, FileBasicInformation);
1495 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1496 todo_wine ok ( (fbi.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_NORMAL, "attribute %x not 0\n", fbi.FileAttributes );
1497
1498 CloseHandle( h );
1499 }
1500
1501 static void test_file_all_information(void)
1502 {
1503 IO_STATUS_BLOCK io;
1504 /* FileAllInformation, like FileNameInformation, has a variable-length pathname
1505 * buffer at the end. Vista objects with STATUS_BUFFER_OVERFLOW if you
1506 * don't leave enough room there.
1507 */
1508 struct {
1509 FILE_ALL_INFORMATION fai;
1510 WCHAR buf[256];
1511 } fai_buf;
1512 HANDLE h;
1513 int res;
1514 int attrib_mask = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL;
1515
1516 if (!(h = create_temp_file(0))) return;
1517
1518 /* Check default first */
1519 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1520 ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1521 ok ( (fai_buf.fai.BasicInformation.FileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE,
1522 "attribute %x not expected\n", fai_buf.fai.BasicInformation.FileAttributes );
1523
1524 /* Then SYSTEM */
1525 /* Clear fbi to avoid setting times */
1526 memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1527 fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_SYSTEM;
1528 U(io).Status = 0xdeadbeef;
1529 res = pNtSetInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1530 ok ( res == STATUS_INVALID_INFO_CLASS || broken(res == STATUS_NOT_IMPLEMENTED), "shouldn't be able to set FileAllInformation, res %x\n", res);
1531 todo_wine ok ( U(io).Status == 0xdeadbeef, "shouldn't be able to set FileAllInformation, io.Status is %x\n", U(io).Status);
1532 U(io).Status = 0xdeadbeef;
1533 res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1534 ok ( res == STATUS_SUCCESS, "can't set system attribute, res: %x\n", res );
1535 ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status: %x\n", U(io).Status );
1536
1537 memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1538 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1539 ok ( res == STATUS_SUCCESS, "can't get attributes, res %x\n", res);
1540 ok ( (fai_buf.fai.BasicInformation.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_SYSTEM, "attribute %x not FILE_ATTRIBUTE_SYSTEM (ok in old linux without xattr)\n", fai_buf.fai.BasicInformation.FileAttributes );
1541
1542 /* Then HIDDEN */
1543 memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1544 fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
1545 U(io).Status = 0xdeadbeef;
1546 res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1547 ok ( res == STATUS_SUCCESS, "can't set system attribute, res: %x\n", res );
1548 ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status: %x\n", U(io).Status );
1549
1550 memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1551 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1552 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1553 ok ( (fai_buf.fai.BasicInformation.FileAttributes & attrib_mask) == FILE_ATTRIBUTE_HIDDEN, "attribute %x not FILE_ATTRIBUTE_HIDDEN (ok in old linux without xattr)\n", fai_buf.fai.BasicInformation.FileAttributes );
1554
1555 /* Check NORMAL last of all (to make sure we can clear attributes) */
1556 memset(&fai_buf.fai.BasicInformation, 0, sizeof(fai_buf.fai.BasicInformation));
1557 fai_buf.fai.BasicInformation.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1558 U(io).Status = 0xdeadbeef;
1559 res = pNtSetInformationFile(h, &io, &fai_buf.fai.BasicInformation, sizeof fai_buf.fai.BasicInformation, FileBasicInformation);
1560 ok ( res == STATUS_SUCCESS, "can't set system attribute, res: %x\n", res );
1561 ok ( U(io).Status == STATUS_SUCCESS, "can't set system attribute, io.Status: %x\n", U(io).Status );
1562
1563 memset(&fai_buf.fai, 0, sizeof(fai_buf.fai));
1564 res = pNtQueryInformationFile(h, &io, &fai_buf.fai, sizeof fai_buf, FileAllInformation);
1565 ok ( res == STATUS_SUCCESS, "can't get attributes\n");
1566 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 );
1567
1568 CloseHandle( h );
1569 }
1570
1571 static void delete_object( WCHAR *path )
1572 {
1573 BOOL ret = DeleteFileW( path );
1574 ok( ret || GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_ACCESS_DENIED,
1575 "DeleteFileW failed with %u\n", GetLastError() );
1576 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
1577 {
1578 ret = RemoveDirectoryW( path );
1579 ok( ret, "RemoveDirectoryW failed with %u\n", GetLastError() );
1580 }
1581 }
1582
1583 static void test_file_rename_information(void)
1584 {
1585 static const WCHAR foo_txtW[] = {'\\','f','o','o','.','t','x','t',0};
1586 static const WCHAR fooW[] = {'f','o','o',0};
1587 WCHAR tmp_path[MAX_PATH], oldpath[MAX_PATH + 16], newpath[MAX_PATH + 16], *filename, *p;
1588 FILE_RENAME_INFORMATION *fri;
1589 FILE_NAME_INFORMATION *fni;
1590 BOOL success, fileDeleted;
1591 UNICODE_STRING name_str;
1592 HANDLE handle, handle2;
1593 IO_STATUS_BLOCK io;
1594 NTSTATUS res;
1595
1596 GetTempPathW( MAX_PATH, tmp_path );
1597
1598 /* oldpath is a file, newpath doesn't exist */
1599 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1600 ok( res != 0, "failed to create temp file\n" );
1601 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1602 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1603
1604 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1605 ok( res != 0, "failed to create temp file\n" );
1606 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1607 DeleteFileW( newpath );
1608 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1609 fri->Replace = FALSE;
1610 fri->RootDir = NULL;
1611 fri->FileNameLength = name_str.Length;
1612 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1613 pRtlFreeUnicodeString( &name_str );
1614
1615 U(io).Status = 0xdeadbeef;
1616 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1617 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
1618 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
1619 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1620 ok( fileDeleted, "file should not exist\n" );
1621 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1622 ok( !fileDeleted, "file should exist\n" );
1623
1624 fni = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR) );
1625 res = pNtQueryInformationFile( handle, &io, fni, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), FileNameInformation );
1626 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
1627 fni->FileName[ fni->FileNameLength / sizeof(WCHAR) ] = 0;
1628 ok( !lstrcmpW(fni->FileName, newpath + 2), "FileName expected %s, got %s\n",
1629 wine_dbgstr_w(newpath + 2), wine_dbgstr_w(fni->FileName) );
1630 HeapFree( GetProcessHeap(), 0, fni );
1631
1632 CloseHandle( handle );
1633 HeapFree( GetProcessHeap(), 0, fri );
1634 delete_object( oldpath );
1635 delete_object( newpath );
1636
1637 /* oldpath is a file, newpath is a file, Replace = FALSE */
1638 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1639 ok( res != 0, "failed to create temp file\n" );
1640 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1641 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1642
1643 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1644 ok( res != 0, "failed to create temp file\n" );
1645 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1646 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1647 fri->Replace = FALSE;
1648 fri->RootDir = NULL;
1649 fri->FileNameLength = name_str.Length;
1650 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1651 pRtlFreeUnicodeString( &name_str );
1652
1653 U(io).Status = 0xdeadbeef;
1654 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1655 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1656 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
1657 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1658 ok( !fileDeleted, "file should exist\n" );
1659 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1660 ok( !fileDeleted, "file should exist\n" );
1661
1662 CloseHandle( handle );
1663 HeapFree( GetProcessHeap(), 0, fri );
1664 delete_object( oldpath );
1665 delete_object( newpath );
1666
1667 /* oldpath is a file, newpath is a file, Replace = TRUE */
1668 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1669 ok( res != 0, "failed to create temp file\n" );
1670 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1671 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1672
1673 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1674 ok( res != 0, "failed to create temp file\n" );
1675 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1676 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1677 fri->Replace = TRUE;
1678 fri->RootDir = NULL;
1679 fri->FileNameLength = name_str.Length;
1680 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1681 pRtlFreeUnicodeString( &name_str );
1682
1683 U(io).Status = 0xdeadbeef;
1684 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1685 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
1686 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
1687 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1688 ok( fileDeleted, "file should not exist\n" );
1689 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1690 ok( !fileDeleted, "file should exist\n" );
1691
1692 CloseHandle( handle );
1693 HeapFree( GetProcessHeap(), 0, fri );
1694 delete_object( oldpath );
1695 delete_object( newpath );
1696
1697 /* oldpath is a file, newpath is a file, Replace = FALSE, target file opened */
1698 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1699 ok( res != 0, "failed to create temp file\n" );
1700 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1701 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1702
1703 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1704 ok( res != 0, "failed to create temp file\n" );
1705 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1706 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1707
1708 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1709 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1710 fri->Replace = FALSE;
1711 fri->RootDir = NULL;
1712 fri->FileNameLength = name_str.Length;
1713 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1714 pRtlFreeUnicodeString( &name_str );
1715
1716 U(io).Status = 0xdeadbeef;
1717 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1718 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1719 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
1720 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1721 ok( !fileDeleted, "file should exist\n" );
1722 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1723 ok( !fileDeleted, "file should exist\n" );
1724
1725 CloseHandle( handle );
1726 CloseHandle( handle2 );
1727 HeapFree( GetProcessHeap(), 0, fri );
1728 delete_object( oldpath );
1729 delete_object( newpath );
1730
1731 /* oldpath is a file, newpath is a file, Replace = TRUE, target file opened */
1732 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1733 ok( res != 0, "failed to create temp file\n" );
1734 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1735 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1736
1737 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1738 ok( res != 0, "failed to create temp file\n" );
1739 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1740 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1741
1742 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1743 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1744 fri->Replace = TRUE;
1745 fri->RootDir = NULL;
1746 fri->FileNameLength = name_str.Length;
1747 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1748 pRtlFreeUnicodeString( &name_str );
1749
1750 U(io).Status = 0xdeadbeef;
1751 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1752 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1753 ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
1754 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1755 ok( !fileDeleted, "file should exist\n" );
1756 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1757 ok( !fileDeleted, "file should exist\n" );
1758
1759 CloseHandle( handle );
1760 CloseHandle( handle2 );
1761 HeapFree( GetProcessHeap(), 0, fri );
1762 delete_object( oldpath );
1763 delete_object( newpath );
1764
1765 /* oldpath is a directory, newpath doesn't exist, Replace = FALSE */
1766 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1767 ok( res != 0, "failed to create temp file\n" );
1768 DeleteFileW( oldpath );
1769 success = CreateDirectoryW( oldpath, NULL );
1770 ok( success != 0, "failed to create temp directory\n" );
1771 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1772 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1773
1774 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1775 ok( res != 0, "failed to create temp file\n" );
1776 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1777 DeleteFileW( newpath );
1778 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1779 fri->Replace = FALSE;
1780 fri->RootDir = NULL;
1781 fri->FileNameLength = name_str.Length;
1782 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1783 pRtlFreeUnicodeString( &name_str );
1784
1785 U(io).Status = 0xdeadbeef;
1786 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1787 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
1788 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
1789 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1790 ok( fileDeleted, "file should not exist\n" );
1791 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1792 ok( !fileDeleted, "file should exist\n" );
1793
1794 fni = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR) );
1795 res = pNtQueryInformationFile( handle, &io, fni, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), FileNameInformation );
1796 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
1797 fni->FileName[ fni->FileNameLength / sizeof(WCHAR) ] = 0;
1798 ok( !lstrcmpW(fni->FileName, newpath + 2), "FileName expected %s, got %s\n",
1799 wine_dbgstr_w(newpath + 2), wine_dbgstr_w(fni->FileName) );
1800 HeapFree( GetProcessHeap(), 0, fni );
1801
1802 CloseHandle( handle );
1803 HeapFree( GetProcessHeap(), 0, fri );
1804 delete_object( oldpath );
1805 delete_object( newpath );
1806
1807 /* oldpath is a directory (but child object opened), newpath doesn't exist, Replace = FALSE */
1808 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1809 ok( res != 0, "failed to create temp file\n" );
1810 DeleteFileW( oldpath );
1811 success = CreateDirectoryW( oldpath, NULL );
1812 ok( success != 0, "failed to create temp directory\n" );
1813 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1814 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1815
1816 lstrcpyW( newpath, oldpath );
1817 lstrcatW( newpath, foo_txtW );
1818 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0 );
1819 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1820
1821 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1822 ok( res != 0, "failed to create temp file\n" );
1823 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1824 DeleteFileW( newpath );
1825 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1826 fri->Replace = FALSE;
1827 fri->RootDir = NULL;
1828 fri->FileNameLength = name_str.Length;
1829 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1830 pRtlFreeUnicodeString( &name_str );
1831
1832 U(io).Status = 0xdeadbeef;
1833 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1834 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1835 todo_wine ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
1836 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1837 todo_wine ok( !fileDeleted, "file should exist\n" );
1838 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1839 todo_wine ok( fileDeleted, "file should not exist\n" );
1840
1841 CloseHandle( handle );
1842 CloseHandle( handle2 );
1843 HeapFree( GetProcessHeap(), 0, fri );
1844 delete_object( oldpath );
1845 if (res == STATUS_SUCCESS) /* remove when Wine is fixed */
1846 {
1847 lstrcpyW( oldpath, newpath );
1848 lstrcatW( oldpath, foo_txtW );
1849 delete_object( oldpath );
1850 }
1851 delete_object( newpath );
1852
1853 /* oldpath is a directory, newpath is a file, Replace = FALSE */
1854 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1855 ok( res != 0, "failed to create temp file\n" );
1856 DeleteFileW( oldpath );
1857 success = CreateDirectoryW( oldpath, NULL );
1858 ok( success != 0, "failed to create temp directory\n" );
1859 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1860 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1861
1862 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1863 ok( res != 0, "failed to create temp file\n" );
1864 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1865 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1866 fri->Replace = FALSE;
1867 fri->RootDir = NULL;
1868 fri->FileNameLength = name_str.Length;
1869 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1870 pRtlFreeUnicodeString( &name_str );
1871
1872 U(io).Status = 0xdeadbeef;
1873 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1874 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1875 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
1876 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1877 ok( !fileDeleted, "file should exist\n" );
1878 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1879 ok( !fileDeleted, "file should exist\n" );
1880
1881 CloseHandle( handle );
1882 HeapFree( GetProcessHeap(), 0, fri );
1883 delete_object( oldpath );
1884 delete_object( newpath );
1885
1886 /* oldpath is a directory, newpath is a file, Replace = FALSE, target file opened */
1887 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1888 ok( res != 0, "failed to create temp file\n" );
1889 DeleteFileW( oldpath );
1890 success = CreateDirectoryW( oldpath, NULL );
1891 ok( success != 0, "failed to create temp directory\n" );
1892 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1893 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1894
1895 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1896 ok( res != 0, "failed to create temp file\n" );
1897 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1898 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1899
1900 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1901 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1902 fri->Replace = FALSE;
1903 fri->RootDir = NULL;
1904 fri->FileNameLength = name_str.Length;
1905 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1906 pRtlFreeUnicodeString( &name_str );
1907
1908 U(io).Status = 0xdeadbeef;
1909 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1910 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1911 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
1912 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1913 ok( !fileDeleted, "file should exist\n" );
1914 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1915 ok( !fileDeleted, "file should exist\n" );
1916
1917 CloseHandle( handle );
1918 CloseHandle( handle2 );
1919 HeapFree( GetProcessHeap(), 0, fri );
1920 delete_object( oldpath );
1921 delete_object( newpath );
1922
1923 /* oldpath is a directory, newpath is a file, Replace = TRUE */
1924 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1925 ok( res != 0, "failed to create temp file\n" );
1926 DeleteFileW( oldpath );
1927 success = CreateDirectoryW( oldpath, NULL );
1928 ok( success != 0, "failed to create temp directory\n" );
1929 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1930 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1931
1932 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1933 ok( res != 0, "failed to create temp file\n" );
1934 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1935 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1936 fri->Replace = TRUE;
1937 fri->RootDir = NULL;
1938 fri->FileNameLength = name_str.Length;
1939 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1940 pRtlFreeUnicodeString( &name_str );
1941
1942 U(io).Status = 0xdeadbeef;
1943 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1944 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
1945 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
1946 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1947 ok( fileDeleted, "file should not exist\n" );
1948 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1949 ok( !fileDeleted, "file should exist\n" );
1950
1951 CloseHandle( handle );
1952 HeapFree( GetProcessHeap(), 0, fri );
1953 delete_object( oldpath );
1954 delete_object( newpath );
1955
1956 /* oldpath is a directory, newpath is a file, Replace = TRUE, target file opened */
1957 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1958 ok( res != 0, "failed to create temp file\n" );
1959 DeleteFileW( oldpath );
1960 success = CreateDirectoryW( oldpath, NULL );
1961 ok( success != 0, "failed to create temp directory\n" );
1962 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
1963 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1964
1965 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
1966 ok( res != 0, "failed to create temp file\n" );
1967 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1968 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
1969
1970 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
1971 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
1972 fri->Replace = TRUE;
1973 fri->RootDir = NULL;
1974 fri->FileNameLength = name_str.Length;
1975 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
1976 pRtlFreeUnicodeString( &name_str );
1977
1978 U(io).Status = 0xdeadbeef;
1979 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
1980 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
1981 ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
1982 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1983 ok( !fileDeleted, "file should exist\n" );
1984 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
1985 ok( !fileDeleted, "file should exist\n" );
1986
1987 CloseHandle( handle );
1988 CloseHandle( handle2 );
1989 HeapFree( GetProcessHeap(), 0, fri );
1990 delete_object( oldpath );
1991 delete_object( newpath );
1992
1993 /* oldpath is a directory, newpath is a directory, Replace = FALSE */
1994 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
1995 ok( res != 0, "failed to create temp file\n" );
1996 DeleteFileW( oldpath );
1997 success = CreateDirectoryW( oldpath, NULL );
1998 ok( success != 0, "failed to create temp directory\n" );
1999 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
2000 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2001
2002 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2003 ok( res != 0, "failed to create temp file\n" );
2004 DeleteFileW( newpath );
2005 success = CreateDirectoryW( newpath, NULL );
2006 ok( success != 0, "failed to create temp directory\n" );
2007 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2008 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
2009 fri->Replace = FALSE;
2010 fri->RootDir = NULL;
2011 fri->FileNameLength = name_str.Length;
2012 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
2013 pRtlFreeUnicodeString( &name_str );
2014
2015 U(io).Status = 0xdeadbeef;
2016 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
2017 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2018 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
2019 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2020 ok( !fileDeleted, "file should exist\n" );
2021 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2022 ok( !fileDeleted, "file should exist\n" );
2023
2024 CloseHandle( handle );
2025 HeapFree( GetProcessHeap(), 0, fri );
2026 delete_object( oldpath );
2027 delete_object( newpath );
2028
2029 /* oldpath is a directory, newpath is a directory, Replace = TRUE */
2030 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2031 ok( res != 0, "failed to create temp file\n" );
2032 DeleteFileW( oldpath );
2033 success = CreateDirectoryW( oldpath, NULL );
2034 ok( success != 0, "failed to create temp directory\n" );
2035 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
2036 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2037
2038 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2039 ok( res != 0, "failed to create temp file\n" );
2040 DeleteFileW( newpath );
2041 success = CreateDirectoryW( newpath, NULL );
2042 ok( success != 0, "failed to create temp directory\n" );
2043 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2044 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
2045 fri->Replace = TRUE;
2046 fri->RootDir = NULL;
2047 fri->FileNameLength = name_str.Length;
2048 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
2049 pRtlFreeUnicodeString( &name_str );
2050
2051 U(io).Status = 0xdeadbeef;
2052 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
2053 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2054 ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
2055 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2056 ok( !fileDeleted, "file should exist\n" );
2057 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2058 ok( !fileDeleted, "file should exist\n" );
2059
2060 CloseHandle( handle );
2061 HeapFree( GetProcessHeap(), 0, fri );
2062 delete_object( oldpath );
2063 delete_object( newpath );
2064
2065 /* oldpath is a directory, newpath is a directory, Replace = TRUE, target file opened */
2066 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2067 ok( res != 0, "failed to create temp file\n" );
2068 DeleteFileW( oldpath );
2069 success = CreateDirectoryW( oldpath, NULL );
2070 ok( success != 0, "failed to create temp directory\n" );
2071 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
2072 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2073
2074 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2075 ok( res != 0, "failed to create temp file\n" );
2076 DeleteFileW( newpath );
2077 success = CreateDirectoryW( newpath, NULL );
2078 ok( success != 0, "failed to create temp directory\n" );
2079 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
2080 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2081
2082 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2083 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
2084 fri->Replace = TRUE;
2085 fri->RootDir = NULL;
2086 fri->FileNameLength = name_str.Length;
2087 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
2088 pRtlFreeUnicodeString( &name_str );
2089
2090 U(io).Status = 0xdeadbeef;
2091 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
2092 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2093 ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
2094 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2095 ok( !fileDeleted, "file should exist\n" );
2096 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2097 ok( !fileDeleted, "file should exist\n" );
2098
2099 CloseHandle( handle );
2100 CloseHandle( handle2 );
2101 HeapFree( GetProcessHeap(), 0, fri );
2102 delete_object( oldpath );
2103 delete_object( newpath );
2104
2105 /* oldpath is a file, newpath is a directory, Replace = FALSE */
2106 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2107 ok( res != 0, "failed to create temp file\n" );
2108 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2109 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2110
2111 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2112 ok( res != 0, "failed to create temp file\n" );
2113 DeleteFileW( newpath );
2114 success = CreateDirectoryW( newpath, NULL );
2115 ok( success != 0, "failed to create temp directory\n" );
2116 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2117 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
2118 fri->Replace = FALSE;
2119 fri->RootDir = NULL;
2120 fri->FileNameLength = name_str.Length;
2121 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
2122 pRtlFreeUnicodeString( &name_str );
2123
2124 U(io).Status = 0xdeadbeef;
2125 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
2126 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2127 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
2128 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2129 ok( !fileDeleted, "file should exist\n" );
2130 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2131 ok( !fileDeleted, "file should exist\n" );
2132
2133 CloseHandle( handle );
2134 HeapFree( GetProcessHeap(), 0, fri );
2135 delete_object( oldpath );
2136 delete_object( newpath );
2137
2138 /* oldpath is a file, newpath is a directory, Replace = TRUE */
2139 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2140 ok( res != 0, "failed to create temp file\n" );
2141 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2142 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2143
2144 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2145 ok( res != 0, "failed to create temp file\n" );
2146 DeleteFileW( newpath );
2147 success = CreateDirectoryW( newpath, NULL );
2148 ok( success != 0, "failed to create temp directory\n" );
2149 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2150 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + name_str.Length );
2151 fri->Replace = TRUE;
2152 fri->RootDir = NULL;
2153 fri->FileNameLength = name_str.Length;
2154 memcpy( fri->FileName, name_str.Buffer, name_str.Length );
2155 pRtlFreeUnicodeString( &name_str );
2156
2157 U(io).Status = 0xdeadbeef;
2158 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
2159 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2160 ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
2161 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2162 ok( !fileDeleted, "file should exist\n" );
2163 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2164 ok( !fileDeleted, "file should exist\n" );
2165
2166 CloseHandle( handle );
2167 HeapFree( GetProcessHeap(), 0, fri );
2168 delete_object( oldpath );
2169 delete_object( newpath );
2170
2171 /* oldpath is a file, newpath doesn't exist, test with RootDir != NULL */
2172 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2173 ok( res != 0, "failed to create temp file\n" );
2174 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2175 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2176
2177 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2178 ok( res != 0, "failed to create temp file\n" );
2179 DeleteFileW( newpath );
2180 for (filename = newpath, p = newpath; *p; p++)
2181 if (*p == '\\') filename = p + 1;
2182 handle2 = CreateFileW( tmp_path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
2183 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2184
2185 fri = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_RENAME_INFORMATION) + lstrlenW(filename) * sizeof(WCHAR) );
2186 fri->Replace = FALSE;
2187 fri->RootDir = handle2;
2188 fri->FileNameLength = lstrlenW(filename) * sizeof(WCHAR);
2189 memcpy( fri->FileName, filename, fri->FileNameLength );
2190
2191 U(io).Status = 0xdeadbeef;
2192 res = pNtSetInformationFile( handle, &io, fri, sizeof(FILE_RENAME_INFORMATION) + fri->FileNameLength, FileRenameInformation );
2193 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
2194 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
2195 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2196 ok( fileDeleted, "file should not exist\n" );
2197 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2198 ok( !fileDeleted, "file should exist\n" );
2199
2200 fni = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR) );
2201 res = pNtQueryInformationFile( handle, &io, fni, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), FileNameInformation );
2202 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
2203 fni->FileName[ fni->FileNameLength / sizeof(WCHAR) ] = 0;
2204 ok( !lstrcmpW(fni->FileName, newpath + 2), "FileName expected %s, got %s\n",
2205 wine_dbgstr_w(newpath + 2), wine_dbgstr_w(fni->FileName) );
2206 HeapFree( GetProcessHeap(), 0, fni );
2207
2208 CloseHandle( handle );
2209 CloseHandle( handle2 );
2210 HeapFree( GetProcessHeap(), 0, fri );
2211 delete_object( oldpath );
2212 delete_object( newpath );
2213 }
2214
2215 static void test_file_link_information(void)
2216 {
2217 static const WCHAR foo_txtW[] = {'\\','f','o','o','.','t','x','t',0};
2218 static const WCHAR fooW[] = {'f','o','o',0};
2219 WCHAR tmp_path[MAX_PATH], oldpath[MAX_PATH + 16], newpath[MAX_PATH + 16], *filename, *p;
2220 FILE_LINK_INFORMATION *fli;
2221 FILE_NAME_INFORMATION *fni;
2222 BOOL success, fileDeleted;
2223 UNICODE_STRING name_str;
2224 HANDLE handle, handle2;
2225 IO_STATUS_BLOCK io;
2226 NTSTATUS res;
2227
2228 GetTempPathW( MAX_PATH, tmp_path );
2229
2230 /* oldpath is a file, newpath doesn't exist */
2231 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2232 ok( res != 0, "failed to create temp file\n" );
2233 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2234 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2235
2236 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2237 ok( res != 0, "failed to create temp file\n" );
2238 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2239 DeleteFileW( newpath );
2240 fli = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_LINK_INFORMATION) + name_str.Length );
2241 fli->ReplaceIfExists = FALSE;
2242 fli->RootDirectory = NULL;
2243 fli->FileNameLength = name_str.Length;
2244 memcpy( fli->FileName, name_str.Buffer, name_str.Length );
2245 pRtlFreeUnicodeString( &name_str );
2246
2247 U(io).Status = 0xdeadbeef;
2248 res = pNtSetInformationFile( handle, &io, fli, sizeof(FILE_LINK_INFORMATION) + fli->FileNameLength, FileLinkInformation );
2249 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
2250 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
2251 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2252 ok( !fileDeleted, "file should exist\n" );
2253 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2254 ok( !fileDeleted, "file should exist\n" );
2255
2256 fni = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR) );
2257 res = pNtQueryInformationFile( handle, &io, fni, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), FileNameInformation );
2258 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
2259 fni->FileName[ fni->FileNameLength / sizeof(WCHAR) ] = 0;
2260 ok( !lstrcmpW(fni->FileName, oldpath + 2), "FileName expected %s, got %s\n",
2261 wine_dbgstr_w(oldpath + 2), wine_dbgstr_w(fni->FileName) );
2262 HeapFree( GetProcessHeap(), 0, fni );
2263
2264 CloseHandle( handle );
2265 HeapFree( GetProcessHeap(), 0, fli );
2266 delete_object( oldpath );
2267 delete_object( newpath );
2268
2269 /* oldpath is a file, newpath is a file, ReplaceIfExists = FALSE */
2270 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2271 ok( res != 0, "failed to create temp file\n" );
2272 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2273 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2274
2275 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2276 ok( res != 0, "failed to create temp file\n" );
2277 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2278 fli = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_LINK_INFORMATION) + name_str.Length );
2279 fli->ReplaceIfExists = FALSE;
2280 fli->RootDirectory = NULL;
2281 fli->FileNameLength = name_str.Length;
2282 memcpy( fli->FileName, name_str.Buffer, name_str.Length );
2283 pRtlFreeUnicodeString( &name_str );
2284
2285 U(io).Status = 0xdeadbeef;
2286 res = pNtSetInformationFile( handle, &io, fli, sizeof(FILE_LINK_INFORMATION) + fli->FileNameLength, FileLinkInformation );
2287 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2288 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
2289 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2290 ok( !fileDeleted, "file should exist\n" );
2291 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2292 ok( !fileDeleted, "file should exist\n" );
2293
2294 CloseHandle( handle );
2295 HeapFree( GetProcessHeap(), 0, fli );
2296 delete_object( oldpath );
2297 delete_object( newpath );
2298
2299 /* oldpath is a file, newpath is a file, ReplaceIfExists = TRUE */
2300 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2301 ok( res != 0, "failed to create temp file\n" );
2302 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2303 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2304
2305 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2306 ok( res != 0, "failed to create temp file\n" );
2307 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2308 fli = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_LINK_INFORMATION) + name_str.Length );
2309 fli->ReplaceIfExists = TRUE;
2310 fli->RootDirectory = NULL;
2311 fli->FileNameLength = name_str.Length;
2312 memcpy( fli->FileName, name_str.Buffer, name_str.Length );
2313 pRtlFreeUnicodeString( &name_str );
2314
2315 U(io).Status = 0xdeadbeef;
2316 res = pNtSetInformationFile( handle, &io, fli, sizeof(FILE_LINK_INFORMATION) + fli->FileNameLength, FileLinkInformation );
2317 ok( U(io).Status == STATUS_SUCCESS, "io.Status expected STATUS_SUCCESS, got %x\n", U(io).Status );
2318 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
2319 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2320 ok( !fileDeleted, "file should exist\n" );
2321 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2322 ok( !fileDeleted, "file should exist\n" );
2323
2324 CloseHandle( handle );
2325 HeapFree( GetProcessHeap(), 0, fli );
2326 delete_object( oldpath );
2327 delete_object( newpath );
2328
2329 /* oldpath is a file, newpath is a file, ReplaceIfExists = FALSE, target file opened */
2330 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2331 ok( res != 0, "failed to create temp file\n" );
2332 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2333 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2334
2335 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2336 ok( res != 0, "failed to create temp file\n" );
2337 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2338 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2339
2340 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2341 fli = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_LINK_INFORMATION) + name_str.Length );
2342 fli->ReplaceIfExists = FALSE;
2343 fli->RootDirectory = NULL;
2344 fli->FileNameLength = name_str.Length;
2345 memcpy( fli->FileName, name_str.Buffer, name_str.Length );
2346 pRtlFreeUnicodeString( &name_str );
2347
2348 U(io).Status = 0xdeadbeef;
2349 res = pNtSetInformationFile( handle, &io, fli, sizeof(FILE_LINK_INFORMATION) + fli->FileNameLength, FileLinkInformation );
2350 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2351 ok( res == STATUS_OBJECT_NAME_COLLISION, "res expected STATUS_OBJECT_NAME_COLLISION, got %x\n", res );
2352 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2353 ok( !fileDeleted, "file should exist\n" );
2354 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2355 ok( !fileDeleted, "file should exist\n" );
2356
2357 CloseHandle( handle );
2358 CloseHandle( handle2 );
2359 HeapFree( GetProcessHeap(), 0, fli );
2360 delete_object( oldpath );
2361 delete_object( newpath );
2362
2363 /* oldpath is a file, newpath is a file, ReplaceIfExists = TRUE, target file opened */
2364 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2365 ok( res != 0, "failed to create temp file\n" );
2366 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2367 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2368
2369 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2370 ok( res != 0, "failed to create temp file\n" );
2371 handle2 = CreateFileW( newpath, GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2372 ok( handle2 != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2373
2374 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2375 fli = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_LINK_INFORMATION) + name_str.Length );
2376 fli->ReplaceIfExists = TRUE;
2377 fli->RootDirectory = NULL;
2378 fli->FileNameLength = name_str.Length;
2379 memcpy( fli->FileName, name_str.Buffer, name_str.Length );
2380 pRtlFreeUnicodeString( &name_str );
2381
2382 U(io).Status = 0xdeadbeef;
2383 res = pNtSetInformationFile( handle, &io, fli, sizeof(FILE_LINK_INFORMATION) + fli->FileNameLength, FileLinkInformation );
2384 todo_wine ok( U(io).Status == 0xdeadbeef, "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2385 ok( res == STATUS_ACCESS_DENIED, "res expected STATUS_ACCESS_DENIED, got %x\n", res );
2386 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2387 ok( !fileDeleted, "file should exist\n" );
2388 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2389 ok( !fileDeleted, "file should exist\n" );
2390
2391 CloseHandle( handle );
2392 CloseHandle( handle2 );
2393 HeapFree( GetProcessHeap(), 0, fli );
2394 delete_object( oldpath );
2395 delete_object( newpath );
2396
2397 /* oldpath is a directory, newpath doesn't exist, ReplaceIfExists = FALSE */
2398 res = GetTempFileNameW( tmp_path, fooW, 0, oldpath );
2399 ok( res != 0, "failed to create temp file\n" );
2400 DeleteFileW( oldpath );
2401 success = CreateDirectoryW( oldpath, NULL );
2402 ok( success != 0, "failed to create temp directory\n" );
2403 handle = CreateFileW( oldpath, GENERIC_WRITE | DELETE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 );
2404 ok( handle != INVALID_HANDLE_VALUE, "CreateFileW failed\n" );
2405
2406 res = GetTempFileNameW( tmp_path, fooW, 0, newpath );
2407 ok( res != 0, "failed to create temp file\n" );
2408 pRtlDosPathNameToNtPathName_U( newpath, &name_str, NULL, NULL );
2409 DeleteFileW( newpath );
2410 fli = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_LINK_INFORMATION) + name_str.Length );
2411 fli->ReplaceIfExists = FALSE;
2412 fli->RootDirectory = NULL;
2413 fli->FileNameLength = name_str.Length;
2414 memcpy( fli->FileName, name_str.Buffer, name_str.Length );
2415 pRtlFreeUnicodeString( &name_str );
2416
2417 U(io).Status = 0xdeadbeef;
2418 res = pNtSetInformationFile( handle, &io, fli, sizeof(FILE_LINK_INFORMATION) + fli->FileNameLength, FileLinkInformation );
2419 todo_wine ok( U(io).Status == 0xdeadbeef , "io.Status expected 0xdeadbeef, got %x\n", U(io).Status );
2420 ok( res == STATUS_FILE_IS_A_DIRECTORY, "res expected STATUS_FILE_IS_A_DIRECTORY, got %x\n", res );
2421 fileDeleted = GetFileAttributesW( oldpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2422 ok( !fileDeleted, "file should exist\n" );
2423 fileDeleted = GetFileAttributesW( newpath ) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND;
2424 ok( fileDeleted, "file should not exist\n" );
2425
2426 fni = HeapAlloc( GetProcessHeap(), 0, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR) );
2427 res = pNtQueryInformationFile( handle, &io, fni, sizeof(FILE_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), FileNameInformation );
2428 ok( res == STATUS_SUCCESS, "res expected STATUS_SUCCESS, got %x\n", res );
2429 fni->FileName[ fni->FileNameLength / sizeof(WCHAR) ] = 0;
2430 ok( !lstrcmpW(fni