winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / kernel32 / tests / file.c
blob5deed96abf3d5143a67156dc6fe1aa5a327f886a
1 /*
2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 * Copyright (c) 2008 Jeff Zaroyko
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <stdio.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "wine/test.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winternl.h"
35 #include "winnls.h"
36 #include "fileapi.h"
38 #undef DeleteFile /* needed for FILE_DISPOSITION_INFO */
40 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
41 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
42 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
43 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
44 static BOOL (WINAPI *pGetFileInformationByHandleEx)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, LPVOID, DWORD);
45 static HANDLE (WINAPI *pOpenFileById)(HANDLE, LPFILE_ID_DESCRIPTOR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD);
46 static BOOL (WINAPI *pSetFileValidData)(HANDLE, LONGLONG);
47 static HRESULT (WINAPI *pCopyFile2)(PCWSTR,PCWSTR,COPYFILE2_EXTENDED_PARAMETERS*);
48 static HANDLE (WINAPI *pCreateFile2)(LPCWSTR, DWORD, DWORD, DWORD, CREATEFILE2_EXTENDED_PARAMETERS*);
49 static DWORD (WINAPI *pGetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, DWORD);
50 static DWORD (WINAPI *pGetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, DWORD);
51 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
52 PLARGE_INTEGER, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG);
53 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)(LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR*);
54 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PCANSI_STRING, BOOLEAN);
55 static BOOL (WINAPI *pSetFileInformationByHandle)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, void*, DWORD);
56 static BOOL (WINAPI *pGetQueuedCompletionStatusEx)(HANDLE, OVERLAPPED_ENTRY*, ULONG, ULONG*, DWORD, BOOL);
57 static HANDLE (WINAPI *pReOpenFile)(HANDLE, DWORD, DWORD, DWORD);
58 static void (WINAPI *pRtlInitAnsiString)(PANSI_STRING,PCSZ);
59 static void (WINAPI *pRtlFreeUnicodeString)(PUNICODE_STRING);
60 static BOOL (WINAPI *pSetFileCompletionNotificationModes)(HANDLE, UCHAR);
61 static HANDLE (WINAPI *pFindFirstStreamW)(LPCWSTR filename, STREAM_INFO_LEVELS infolevel, void *data, DWORD flags);
63 static char filename[MAX_PATH];
64 static const char sillytext[] =
65 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
66 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
67 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
68 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
69 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
70 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
71 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
72 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
73 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
74 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
76 struct test_list {
77 const char *file; /* file string to test */
78 const DWORD err; /* Win NT and further error code */
79 const LONG err2; /* Win 9x & ME error code or -1 */
80 const DWORD options; /* option flag to use for open */
81 const BOOL todo_flag; /* todo_wine indicator */
82 } ;
84 static void InitFunctionPointers(void)
86 HMODULE hntdll = GetModuleHandleA("ntdll");
87 HMODULE hkernel32 = GetModuleHandleA("kernel32");
89 pNtCreateFile = (void *)GetProcAddress(hntdll, "NtCreateFile");
90 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
91 pRtlAnsiStringToUnicodeString = (void *)GetProcAddress(hntdll, "RtlAnsiStringToUnicodeString");
92 pRtlInitAnsiString = (void *)GetProcAddress(hntdll, "RtlInitAnsiString");
93 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
95 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
96 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
97 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
98 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
99 pGetFileInformationByHandleEx = (void *) GetProcAddress(hkernel32, "GetFileInformationByHandleEx");
100 pOpenFileById = (void *) GetProcAddress(hkernel32, "OpenFileById");
101 pSetFileValidData = (void *) GetProcAddress(hkernel32, "SetFileValidData");
102 pCopyFile2 = (void *) GetProcAddress(hkernel32, "CopyFile2");
103 pCreateFile2 = (void *) GetProcAddress(hkernel32, "CreateFile2");
104 pGetFinalPathNameByHandleA = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleA");
105 pGetFinalPathNameByHandleW = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleW");
106 pSetFileInformationByHandle = (void *) GetProcAddress(hkernel32, "SetFileInformationByHandle");
107 pGetQueuedCompletionStatusEx = (void *) GetProcAddress(hkernel32, "GetQueuedCompletionStatusEx");
108 pReOpenFile = (void *) GetProcAddress(hkernel32, "ReOpenFile");
109 pSetFileCompletionNotificationModes = (void *)GetProcAddress(hkernel32, "SetFileCompletionNotificationModes");
110 pFindFirstStreamW = (void *)GetProcAddress(hkernel32, "FindFirstStreamW");
113 static void create_file( const char *path )
115 FILE *f = fopen( path, "wb" );
116 fputs( path, f );
117 fclose( f );
120 static void test__hread( void )
122 HFILE filehandle;
123 char buffer[10000];
124 LONG bytes_read;
125 LONG bytes_wanted;
126 LONG i;
127 BOOL ret;
129 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
130 DeleteFileA( filename );
131 filehandle = _lcreat( filename, 0 );
132 if (filehandle == HFILE_ERROR)
134 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
135 return;
138 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
140 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
142 filehandle = _lopen( filename, OF_READ );
144 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
146 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
148 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
150 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
152 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
153 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
154 for (i = 0; i < bytes_wanted; i++)
156 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
160 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
162 ret = DeleteFileA( filename );
163 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
167 static void test__hwrite( void )
169 HFILE filehandle;
170 char buffer[10000];
171 LONG bytes_read;
172 LONG bytes_written;
173 ULONG blocks;
174 LONG i;
175 char *contents;
176 HLOCAL memory_object;
177 char checksum[1];
178 BOOL ret;
180 filehandle = _lcreat( filename, 0 );
181 if (filehandle == HFILE_ERROR)
183 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
184 return;
187 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
189 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
191 filehandle = _lopen( filename, OF_READ );
193 bytes_read = _hread( filehandle, buffer, 1);
195 ok( 0 == bytes_read, "file read size error\n" );
197 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
199 filehandle = _lopen( filename, OF_READWRITE );
201 bytes_written = 0;
202 checksum[0] = '\0';
203 srand( (unsigned)time( NULL ) );
204 for (blocks = 0; blocks < 100; blocks++)
206 for (i = 0; i < (LONG)sizeof( buffer ); i++)
208 buffer[i] = rand( );
209 checksum[0] = checksum[0] + buffer[i];
211 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
212 bytes_written = bytes_written + sizeof( buffer );
215 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
216 bytes_written++;
218 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
220 memory_object = LocalAlloc( LPTR, bytes_written );
222 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
224 contents = LocalLock( memory_object );
225 ok( NULL != contents, "LocalLock whines\n" );
227 filehandle = _lopen( filename, OF_READ );
229 contents = LocalLock( memory_object );
230 ok( NULL != contents, "LocalLock whines\n" );
232 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
234 checksum[0] = '\0';
235 i = 0;
238 checksum[0] = checksum[0] + contents[i];
239 i++;
241 while (i < bytes_written - 1);
243 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
245 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
247 ret = DeleteFileA( filename );
248 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
250 LocalFree( contents );
254 static void test__lclose( void )
256 HFILE filehandle;
257 BOOL ret;
259 filehandle = _lcreat( filename, 0 );
260 if (filehandle == HFILE_ERROR)
262 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
263 return;
266 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
268 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
270 ret = DeleteFileA( filename );
271 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
274 /* helper function for test__lcreat */
275 static void get_nt_pathW( const char *name, UNICODE_STRING *nameW )
277 UNICODE_STRING strW;
278 ANSI_STRING str;
279 NTSTATUS status;
280 BOOLEAN ret;
282 pRtlInitAnsiString( &str, name );
284 status = pRtlAnsiStringToUnicodeString( &strW, &str, TRUE );
285 ok( !status, "RtlAnsiStringToUnicodeString failed with %08x\n", status );
287 ret = pRtlDosPathNameToNtPathName_U( strW.Buffer, nameW, NULL, NULL );
288 ok( ret, "RtlDosPathNameToNtPathName_U failed\n" );
290 pRtlFreeUnicodeString( &strW );
293 static void test__lcreat( void )
295 UNICODE_STRING filenameW;
296 OBJECT_ATTRIBUTES attr;
297 IO_STATUS_BLOCK io;
298 HFILE filehandle;
299 char buffer[10000];
300 WIN32_FIND_DATAA search_results;
301 char slashname[] = "testfi/";
302 int err;
303 HANDLE find, file;
304 NTSTATUS status;
305 BOOL ret;
307 filehandle = _lcreat( filename, 0 );
308 if (filehandle == HFILE_ERROR)
310 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
311 return;
314 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
316 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
318 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
320 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
322 find = FindFirstFileA( filename, &search_results );
323 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
324 FindClose( find );
326 ret = DeleteFileA(filename);
327 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
329 filehandle = _lcreat( filename, 1 ); /* readonly */
330 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
332 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
334 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
336 find = FindFirstFileA( filename, &search_results );
337 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
338 FindClose( find );
340 SetLastError( 0xdeadbeef );
341 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
342 ok( GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError() );
344 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
346 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
348 filehandle = _lcreat( filename, 1 ); /* readonly */
349 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError() );
350 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen(sillytext) ),
351 "_hwrite shouldn't be able to write never the less\n" );
352 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
354 find = FindFirstFileA( filename, &search_results );
355 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
356 FindClose( find );
358 get_nt_pathW( filename, &filenameW );
359 attr.Length = sizeof(attr);
360 attr.RootDirectory = 0;
361 attr.Attributes = OBJ_CASE_INSENSITIVE;
362 attr.ObjectName = &filenameW;
363 attr.SecurityDescriptor = NULL;
364 attr.SecurityQualityOfService = NULL;
366 status = pNtCreateFile( &file, GENERIC_READ | GENERIC_WRITE | DELETE, &attr, &io, NULL, 0,
367 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
368 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
369 ok( status == STATUS_ACCESS_DENIED, "expected STATUS_ACCESS_DENIED, got %08x\n", status );
370 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
372 status = pNtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
373 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
374 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
375 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
377 status = pNtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
378 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
379 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_DIRECTORY_FILE, NULL, 0 );
380 ok( status == STATUS_NOT_A_DIRECTORY, "expected STATUS_NOT_A_DIRECTORY, got %08x\n", status );
382 status = pNtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
383 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
384 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
385 todo_wine
386 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
387 if (!status) CloseHandle( file );
389 pRtlFreeUnicodeString( &filenameW );
391 todo_wine
392 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
393 todo_wine
394 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
395 todo_wine
396 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file\n" );
398 filehandle = _lcreat( filename, 2 );
399 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
401 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
403 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
405 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
407 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
409 find = FindFirstFileA( filename, &search_results );
410 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
411 FindClose( find );
413 ret = DeleteFileA( filename );
414 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
416 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
417 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
419 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
421 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
423 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
425 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
427 find = FindFirstFileA( filename, &search_results );
428 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
429 FindClose( find );
431 ret = DeleteFileA( filename );
432 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
434 filehandle=_lcreat (slashname, 0); /* illegal name */
435 if (HFILE_ERROR==filehandle) {
436 err=GetLastError ();
437 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
438 "creating file \"%s\" failed with error %d\n", slashname, err);
439 } else { /* only NT succeeds */
440 _lclose(filehandle);
441 find=FindFirstFileA (slashname, &search_results);
442 if (INVALID_HANDLE_VALUE!=find)
444 ret = FindClose (find);
445 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
446 slashname[strlen(slashname)-1]=0;
447 ok (!strcmp (slashname, search_results.cFileName),
448 "found unexpected name \"%s\"\n", search_results.cFileName);
449 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
450 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
451 search_results.dwFileAttributes);
453 ret = DeleteFileA( slashname );
454 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
457 filehandle=_lcreat (filename, 8); /* illegal attribute */
458 if (HFILE_ERROR==filehandle)
459 ok (0, "couldn't create volume label \"%s\"\n", filename);
460 else {
461 _lclose(filehandle);
462 find=FindFirstFileA (filename, &search_results);
463 if (INVALID_HANDLE_VALUE==find)
464 ok (0, "file \"%s\" not found\n", filename);
465 else {
466 const char *name = strrchr(filename, '\\');
468 if (name) name++;
469 else name = filename;
471 ret = FindClose(find);
472 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
473 ok (!strcmp (name, search_results.cFileName),
474 "expected \"%s\", got \"%s\"\n", name, search_results.cFileName);
475 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
476 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_COMPRESSED;
477 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
478 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
479 search_results.dwFileAttributes);
481 ret = DeleteFileA( filename );
482 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
487 static void test__llseek( void )
489 INT i;
490 HFILE filehandle;
491 char buffer[1];
492 LONG bytes_read;
493 BOOL ret;
495 filehandle = _lcreat( filename, 0 );
496 if (filehandle == HFILE_ERROR)
498 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
499 return;
502 for (i = 0; i < 400; i++)
504 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
506 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
507 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
509 bytes_read = _hread( filehandle, buffer, 1);
510 ok( 1 == bytes_read, "file read size error\n" );
511 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
512 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
514 bytes_read = _hread( filehandle, buffer, 1);
515 ok( 1 == bytes_read, "file read size error\n" );
516 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
517 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
518 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
520 ret = DeleteFileA( filename );
521 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
525 static void test__llopen( void )
527 HFILE filehandle;
528 UINT bytes_read;
529 char buffer[10000];
530 BOOL ret;
532 filehandle = _lcreat( filename, 0 );
533 if (filehandle == HFILE_ERROR)
535 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
536 return;
539 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
540 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
542 filehandle = _lopen( filename, OF_READ );
543 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
544 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
545 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
546 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
548 filehandle = _lopen( filename, OF_READWRITE );
549 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
550 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
551 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
552 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
554 filehandle = _lopen( filename, OF_WRITE );
555 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
556 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
557 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
559 ret = DeleteFileA( filename );
560 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
561 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
565 static void test__lread( void )
567 HFILE filehandle;
568 char buffer[10000];
569 UINT bytes_read;
570 UINT bytes_wanted;
571 UINT i;
572 BOOL ret;
574 filehandle = _lcreat( filename, 0 );
575 if (filehandle == HFILE_ERROR)
577 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
578 return;
581 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
583 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
585 filehandle = _lopen( filename, OF_READ );
587 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
589 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
591 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
593 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
595 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
596 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
597 for (i = 0; i < bytes_wanted; i++)
599 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
603 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
605 ret = DeleteFileA( filename );
606 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
610 static void test__lwrite( void )
612 HFILE filehandle;
613 char buffer[10000];
614 UINT bytes_read;
615 UINT bytes_written;
616 UINT blocks;
617 INT i;
618 char *contents;
619 HLOCAL memory_object;
620 char checksum[1];
621 BOOL ret;
623 filehandle = _lcreat( filename, 0 );
624 if (filehandle == HFILE_ERROR)
626 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
627 return;
630 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
632 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
634 filehandle = _lopen( filename, OF_READ );
636 bytes_read = _hread( filehandle, buffer, 1);
638 ok( 0 == bytes_read, "file read size error\n" );
640 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
642 filehandle = _lopen( filename, OF_READWRITE );
644 bytes_written = 0;
645 checksum[0] = '\0';
646 srand( (unsigned)time( NULL ) );
647 for (blocks = 0; blocks < 100; blocks++)
649 for (i = 0; i < (INT)sizeof( buffer ); i++)
651 buffer[i] = rand( );
652 checksum[0] = checksum[0] + buffer[i];
654 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
655 bytes_written = bytes_written + sizeof( buffer );
658 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
659 bytes_written++;
661 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
663 memory_object = LocalAlloc( LPTR, bytes_written );
665 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
667 contents = LocalLock( memory_object );
668 ok( NULL != contents, "LocalLock whines\n" );
670 filehandle = _lopen( filename, OF_READ );
672 contents = LocalLock( memory_object );
673 ok( NULL != contents, "LocalLock whines\n" );
675 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
677 checksum[0] = '\0';
678 i = 0;
681 checksum[0] += contents[i];
682 i++;
684 while (i < bytes_written - 1);
686 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
688 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
690 ret = DeleteFileA( filename );
691 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
693 LocalFree( contents );
696 static void test_CopyFileA(void)
698 char temp_path[MAX_PATH];
699 char source[MAX_PATH], dest[MAX_PATH];
700 static const char prefix[] = "pfx";
701 HANDLE hfile;
702 HANDLE hmapfile;
703 FILETIME ft1, ft2;
704 char buf[10];
705 DWORD ret;
706 BOOL retok;
708 ret = GetTempPathA(MAX_PATH, temp_path);
709 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
710 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
712 ret = GetTempFileNameA(temp_path, prefix, 0, source);
713 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
715 /* copying a file to itself must fail */
716 retok = CopyFileA(source, source, FALSE);
717 ok( !retok && (GetLastError() == ERROR_SHARING_VIOLATION || broken(GetLastError() == ERROR_FILE_EXISTS) /* Win 9x */),
718 "copying a file to itself didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
720 /* make the source have not zero size */
721 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
722 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
723 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
724 ok( retok && ret == sizeof(prefix),
725 "WriteFile error %d\n", GetLastError());
726 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
727 /* get the file time and change it to prove the difference */
728 ret = GetFileTime(hfile, NULL, NULL, &ft1);
729 ok( ret, "GetFileTime error %d\n", GetLastError());
730 ft1.dwLowDateTime -= 600000000; /* 60 second */
731 ret = SetFileTime(hfile, NULL, NULL, &ft1);
732 ok( ret, "SetFileTime error %d\n", GetLastError());
733 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
734 CloseHandle(hfile);
736 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
737 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
739 SetLastError(0xdeadbeef);
740 ret = CopyFileA(source, dest, TRUE);
741 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
742 "CopyFileA: unexpected error %d\n", GetLastError());
744 ret = CopyFileA(source, dest, FALSE);
745 ok(ret, "CopyFileA: error %d\n", GetLastError());
747 /* NULL checks */
748 retok = CopyFileA(NULL, dest, TRUE);
749 ok(!retok && GetLastError() == ERROR_PATH_NOT_FOUND,
750 "CopyFileA: ret = %d, unexpected error %d\n", retok, GetLastError());
751 retok = CopyFileA(source, NULL, TRUE);
752 ok(!retok && GetLastError() == ERROR_PATH_NOT_FOUND,
753 "CopyFileA: ret = %d, unexpected error %d\n", retok, GetLastError());
755 /* copying from a read-locked source fails */
756 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
757 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
758 retok = CopyFileA(source, dest, FALSE);
759 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
760 "copying from a read-locked file succeeded when it shouldn't have\n");
761 /* in addition, the source is opened before the destination */
762 retok = CopyFileA("25f99d3b-4ba4-4f66-88f5-2906886993cc", dest, FALSE);
763 ok(!retok && GetLastError() == ERROR_FILE_NOT_FOUND,
764 "copying from a file that doesn't exist failed in an unexpected way (ret=%d, err=%d)\n", retok, GetLastError());
765 CloseHandle(hfile);
767 /* copying from a r+w opened, r shared source succeeds */
768 hfile = CreateFileA(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
769 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
770 retok = CopyFileA(source, dest, FALSE);
771 ok(retok,
772 "copying from an r+w opened and r shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
773 CloseHandle(hfile);
775 /* copying from a delete-locked source mostly succeeds */
776 hfile = CreateFileA(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
777 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
778 retok = CopyFileA(source, dest, FALSE);
779 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* NT, 2000, XP */,
780 "copying from a delete-locked file failed (ret=%d, err=%d)\n", retok, GetLastError());
781 CloseHandle(hfile);
783 /* copying to a write-locked destination fails */
784 hfile = CreateFileA(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
785 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
786 retok = CopyFileA(source, dest, FALSE);
787 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
788 "copying to a write-locked file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
789 CloseHandle(hfile);
791 /* copying to a r+w opened, w shared destination mostly succeeds */
792 hfile = CreateFileA(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
793 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
794 retok = CopyFileA(source, dest, FALSE);
795 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
796 "copying to a r+w opened and w shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
797 CloseHandle(hfile);
799 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
800 hfile = CreateFileA(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
801 ok(hfile != INVALID_HANDLE_VALUE || broken(GetLastError() == ERROR_INVALID_PARAMETER) /* Win 9x */,
802 "failed to open destination file, error %d\n", GetLastError());
803 if (hfile != INVALID_HANDLE_VALUE)
805 retok = CopyFileA(source, dest, FALSE);
806 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
807 "copying to a delete-locked shared file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
808 CloseHandle(hfile);
811 /* copy to a file that's opened the way Wine opens the source */
812 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
813 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
814 retok = CopyFileA(source, dest, FALSE);
815 ok(retok || broken(GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
816 "copying to a file opened the way Wine opens the source failed (ret=%d, err=%d)\n", retok, GetLastError());
817 CloseHandle(hfile);
819 /* make sure that destination has correct size */
820 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
821 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
822 ret = GetFileSize(hfile, NULL);
823 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
825 /* make sure that destination has the same filetime */
826 ret = GetFileTime(hfile, NULL, NULL, &ft2);
827 ok( ret, "GetFileTime error %d\n", GetLastError());
828 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
830 SetLastError(0xdeadbeef);
831 ret = CopyFileA(source, dest, FALSE);
832 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
833 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
835 /* make sure that destination still has correct size */
836 ret = GetFileSize(hfile, NULL);
837 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
838 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
839 ok( retok && ret == sizeof(prefix),
840 "ReadFile: error %d\n", GetLastError());
841 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
843 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
844 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
845 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
847 ret = CopyFileA(source, dest, FALSE);
848 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
849 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
851 CloseHandle(hmapfile);
852 CloseHandle(hfile);
854 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
855 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
857 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
858 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
859 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
861 ret = CopyFileA(source, dest, FALSE);
862 ok(!ret, "CopyFileA: expected failure\n");
863 ok(GetLastError() == ERROR_USER_MAPPED_FILE ||
864 broken(GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x */
865 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
867 CloseHandle(hmapfile);
868 CloseHandle(hfile);
870 ret = DeleteFileA(source);
871 ok(ret, "DeleteFileA: error %d\n", GetLastError());
872 ret = DeleteFileA(dest);
873 ok(ret, "DeleteFileA: error %d\n", GetLastError());
876 static void test_CopyFileW(void)
878 WCHAR temp_path[MAX_PATH];
879 WCHAR source[MAX_PATH], dest[MAX_PATH];
880 static const WCHAR prefix[] = {'p','f','x',0};
881 DWORD ret;
883 ret = GetTempPathW(MAX_PATH, temp_path);
884 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
886 win_skip("GetTempPathW is not available\n");
887 return;
889 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
890 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
892 ret = GetTempFileNameW(temp_path, prefix, 0, source);
893 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
895 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
896 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
898 ret = CopyFileW(source, dest, TRUE);
899 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
900 "CopyFileW: unexpected error %d\n", GetLastError());
902 ret = CopyFileW(source, dest, FALSE);
903 ok(ret, "CopyFileW: error %d\n", GetLastError());
905 ret = DeleteFileW(source);
906 ok(ret, "DeleteFileW: error %d\n", GetLastError());
907 ret = DeleteFileW(dest);
908 ok(ret, "DeleteFileW: error %d\n", GetLastError());
911 static void test_CopyFile2(void)
913 static const WCHAR doesntexistW[] = {'d','o','e','s','n','t','e','x','i','s','t',0};
914 static const WCHAR prefix[] = {'p','f','x',0};
915 WCHAR source[MAX_PATH], dest[MAX_PATH], temp_path[MAX_PATH];
916 COPYFILE2_EXTENDED_PARAMETERS params;
917 HANDLE hfile, hmapfile;
918 FILETIME ft1, ft2;
919 DWORD ret, len;
920 char buf[10];
921 HRESULT hr;
923 if (!pCopyFile2)
925 skip("CopyFile2 is not available\n");
926 return;
929 ret = GetTempPathW(MAX_PATH, temp_path);
930 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
931 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
933 ret = GetTempFileNameW(temp_path, prefix, 0, source);
934 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
936 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
937 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
939 /* fail if exists */
940 memset(&params, 0, sizeof(params));
941 params.dwSize = sizeof(params);
942 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
944 SetLastError(0xdeadbeef);
945 hr = pCopyFile2(source, dest, &params);
946 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
947 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
949 /* don't fail if exists */
950 params.dwSize = sizeof(params);
951 params.dwCopyFlags = 0;
953 hr = pCopyFile2(source, dest, &params);
954 ok(hr == S_OK, "CopyFile2: error 0x%08x\n", hr);
956 /* copying a file to itself must fail */
957 params.dwSize = sizeof(params);
958 params.dwCopyFlags = 0;
960 SetLastError(0xdeadbeef);
961 hr = pCopyFile2(source, source, &params);
962 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: copying a file to itself didn't fail, 0x%08x\n", hr);
963 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
965 /* make the source have not zero size */
966 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
967 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
968 ret = WriteFile(hfile, prefix, sizeof(prefix), &len, NULL );
969 ok(ret && len == sizeof(prefix), "WriteFile error %d\n", GetLastError());
970 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
972 /* get the file time and change it to prove the difference */
973 ret = GetFileTime(hfile, NULL, NULL, &ft1);
974 ok(ret, "GetFileTime error %d\n", GetLastError());
975 ft1.dwLowDateTime -= 600000000; /* 60 second */
976 ret = SetFileTime(hfile, NULL, NULL, &ft1);
977 ok(ret, "SetFileTime error %d\n", GetLastError());
978 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
979 CloseHandle(hfile);
981 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
982 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
984 params.dwSize = sizeof(params);
985 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
987 SetLastError(0xdeadbeef);
988 hr = pCopyFile2(source, dest, &params);
989 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
990 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
992 params.dwSize = sizeof(params);
993 params.dwCopyFlags = 0;
994 hr = pCopyFile2(source, dest, &params);
995 ok(ret, "CopyFile2: error 0x%08x\n", hr);
997 /* copying from a read-locked source fails */
998 hfile = CreateFileW(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
999 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
1001 params.dwSize = sizeof(params);
1002 params.dwCopyFlags = 0;
1003 SetLastError(0xdeadbeef);
1004 hr = pCopyFile2(source, dest, &params);
1005 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1006 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1008 /* in addition, the source is opened before the destination */
1009 params.dwSize = sizeof(params);
1010 params.dwCopyFlags = 0;
1011 SetLastError(0xdeadbeef);
1012 hr = pCopyFile2(doesntexistW, dest, &params);
1013 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "got 0x%08x\n", hr);
1014 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "CopyFile2: last error %d\n", GetLastError());
1015 CloseHandle(hfile);
1017 /* copying from a r+w opened, r shared source succeeds */
1018 hfile = CreateFileW(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1019 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
1021 params.dwSize = sizeof(params);
1022 params.dwCopyFlags = 0;
1023 hr = pCopyFile2(source, dest, &params);
1024 ok(hr == S_OK, "failed 0x%08x\n", hr);
1025 CloseHandle(hfile);
1027 /* copying from a delete-locked source mostly succeeds */
1028 hfile = CreateFileW(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1029 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
1031 params.dwSize = sizeof(params);
1032 params.dwCopyFlags = 0;
1033 hr = pCopyFile2(source, dest, &params);
1034 ok(hr == S_OK, "failed 0x%08x\n", hr);
1035 CloseHandle(hfile);
1037 /* copying to a write-locked destination fails */
1038 hfile = CreateFileW(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1039 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1041 params.dwSize = sizeof(params);
1042 params.dwCopyFlags = 0;
1043 SetLastError(0xdeadbeef);
1044 hr = pCopyFile2(source, dest, FALSE);
1045 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1046 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1047 CloseHandle(hfile);
1049 /* copying to a r+w opened, w shared destination mostly succeeds */
1050 hfile = CreateFileW(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1051 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1053 params.dwSize = sizeof(params);
1054 params.dwCopyFlags = 0;
1055 hr = pCopyFile2(source, dest, FALSE);
1056 ok(hr == S_OK, "got 0x%08x\n", hr);
1057 CloseHandle(hfile);
1059 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
1060 hfile = CreateFileW(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
1061 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1063 params.dwSize = sizeof(params);
1064 params.dwCopyFlags = 0;
1065 SetLastError(0xdeadbeef);
1066 hr = pCopyFile2(source, dest, &params);
1067 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1068 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1069 CloseHandle(hfile);
1071 /* copy to a file that's opened the way Wine opens the source */
1072 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1073 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1075 params.dwSize = sizeof(params);
1076 params.dwCopyFlags = 0;
1077 hr = pCopyFile2(source, dest, &params);
1078 ok(hr == S_OK, "got 0x%08x\n", hr);
1079 CloseHandle(hfile);
1081 /* make sure that destination has correct size */
1082 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1083 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1084 ret = GetFileSize(hfile, NULL);
1085 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1087 /* make sure that destination has the same filetime */
1088 ret = GetFileTime(hfile, NULL, NULL, &ft2);
1089 ok(ret, "GetFileTime error %d\n", GetLastError());
1090 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
1092 params.dwSize = sizeof(params);
1093 params.dwCopyFlags = 0;
1094 SetLastError(0xdeadbeef);
1095 hr = pCopyFile2(source, dest, &params);
1096 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1097 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1099 /* make sure that destination still has correct size */
1100 ret = GetFileSize(hfile, NULL);
1101 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1102 ret = ReadFile(hfile, buf, sizeof(buf), &len, NULL);
1103 ok(ret && len == sizeof(prefix), "ReadFile: error %d\n", GetLastError());
1104 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
1106 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
1107 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1108 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1110 params.dwSize = sizeof(params);
1111 params.dwCopyFlags = 0;
1112 SetLastError(0xdeadbeef);
1113 hr = pCopyFile2(source, dest, &params);
1114 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1115 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1117 CloseHandle(hmapfile);
1118 CloseHandle(hfile);
1120 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1121 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1123 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
1124 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1125 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1127 params.dwSize = sizeof(params);
1128 params.dwCopyFlags = 0;
1129 hr = pCopyFile2(source, dest, &params);
1130 ok(hr == HRESULT_FROM_WIN32(ERROR_USER_MAPPED_FILE), "CopyFile2: unexpected error 0x%08x\n", hr);
1131 ok(GetLastError() == ERROR_USER_MAPPED_FILE, "CopyFile2: last error %d\n", GetLastError());
1133 CloseHandle(hmapfile);
1134 CloseHandle(hfile);
1136 DeleteFileW(source);
1137 DeleteFileW(dest);
1140 static DWORD WINAPI copy_progress_cb(LARGE_INTEGER total_size, LARGE_INTEGER total_transferred,
1141 LARGE_INTEGER stream_size, LARGE_INTEGER stream_transferred,
1142 DWORD stream, DWORD reason, HANDLE source, HANDLE dest, LPVOID userdata)
1144 ok(reason == CALLBACK_STREAM_SWITCH, "expected CALLBACK_STREAM_SWITCH, got %u\n", reason);
1145 CloseHandle(userdata);
1146 return PROGRESS_CANCEL;
1149 static void test_CopyFileEx(void)
1151 char temp_path[MAX_PATH];
1152 char source[MAX_PATH], dest[MAX_PATH];
1153 static const char prefix[] = "pfx";
1154 HANDLE hfile;
1155 DWORD ret;
1156 BOOL retok;
1158 ret = GetTempPathA(MAX_PATH, temp_path);
1159 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1160 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1162 ret = GetTempFileNameA(temp_path, prefix, 0, source);
1163 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1165 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
1166 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1168 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1169 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1170 SetLastError(0xdeadbeef);
1171 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1172 todo_wine
1173 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1174 todo_wine
1175 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1176 ok(GetFileAttributesA(dest) != INVALID_FILE_ATTRIBUTES, "file was deleted\n");
1178 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1179 NULL, OPEN_EXISTING, 0, 0);
1180 todo_wine
1181 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1182 SetLastError(0xdeadbeef);
1183 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1184 todo_wine
1185 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1186 todo_wine
1187 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1188 todo_wine
1189 ok(GetFileAttributesA(dest) == INVALID_FILE_ATTRIBUTES, "file was not deleted\n");
1191 retok = CopyFileExA(source, NULL, copy_progress_cb, hfile, NULL, 0);
1192 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1193 ok(GetLastError() == ERROR_PATH_NOT_FOUND, "expected ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
1194 retok = CopyFileExA(NULL, dest, copy_progress_cb, hfile, NULL, 0);
1195 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1196 ok(GetLastError() == ERROR_PATH_NOT_FOUND, "expected ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
1198 ret = DeleteFileA(source);
1199 ok(ret, "DeleteFileA failed with error %d\n", GetLastError());
1200 ret = DeleteFileA(dest);
1201 ok(!ret, "DeleteFileA unexpectedly succeeded\n");
1205 * Debugging routine to dump a buffer in a hexdump-like fashion.
1207 static void dumpmem(unsigned char *mem, int len)
1209 int x = 0;
1210 char hex[49], *p;
1211 char txt[17], *c;
1213 while (x < len)
1215 p = hex;
1216 c = txt;
1217 do {
1218 p += sprintf(p, "%02x ", mem[x]);
1219 *c++ = (mem[x] >= 32 && mem[x] <= 127) ? mem[x] : '.';
1220 } while (++x % 16 && x < len);
1221 *c = '\0';
1222 trace("%04x: %-48s- %s\n", x, hex, txt);
1226 static void test_CreateFileA(void)
1228 HANDLE hFile;
1229 char temp_path[MAX_PATH], dirname[MAX_PATH];
1230 char filename[MAX_PATH];
1231 static const char prefix[] = "pfx";
1232 char windowsdir[MAX_PATH];
1233 char Volume_1[MAX_PATH];
1234 unsigned char buffer[512];
1235 char directory[] = "removeme";
1236 static const char nt_drive[] = "\\\\?\\A:";
1237 DWORD i, ret, len;
1238 static const struct test_list p[] =
1240 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
1241 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
1242 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
1243 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
1244 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
1245 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
1246 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
1247 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
1248 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
1249 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
1250 {"c:c:\\windows", ERROR_INVALID_NAME, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* invalid path */
1251 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
1252 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
1253 {NULL, 0, -1, 0, FALSE}
1255 BY_HANDLE_FILE_INFORMATION Finfo;
1256 WCHAR curdir[MAX_PATH];
1258 ret = GetTempPathA(MAX_PATH, temp_path);
1259 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1260 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1262 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1263 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1265 SetLastError(0xdeadbeef);
1266 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
1267 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1268 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1269 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1271 SetLastError(0xdeadbeef);
1272 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1273 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1274 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1275 "hFile %p, last error %u\n", hFile, GetLastError());
1277 CloseHandle(hFile);
1279 SetLastError(0xdeadbeef);
1280 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1281 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1282 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1283 "hFile %p, last error %u\n", hFile, GetLastError());
1285 CloseHandle(hFile);
1287 ret = DeleteFileA(filename);
1288 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1290 SetLastError(0xdeadbeef);
1291 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1292 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1293 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1294 "hFile %p, last error %u\n", hFile, GetLastError());
1296 CloseHandle(hFile);
1298 ret = DeleteFileA(filename);
1299 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1301 SetLastError(0xdeadbeef);
1302 hFile = CreateFileA("c:\\*.*", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1303 ok(hFile == INVALID_HANDLE_VALUE, "hFile should have been INVALID_HANDLE_VALUE\n");
1304 ok(GetLastError() == ERROR_INVALID_NAME ||
1305 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
1306 "LastError should have been ERROR_INVALID_NAME or ERROR_FILE_NOT_FOUND but got %u\n", GetLastError());
1308 /* get windows drive letter */
1309 ret = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1310 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1311 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1313 /* test error return codes from CreateFile for some cases */
1314 ret = GetTempPathA(MAX_PATH, temp_path);
1315 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1316 strcpy(dirname, temp_path);
1317 strcat(dirname, directory);
1318 ret = CreateDirectoryA(dirname, NULL);
1319 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
1320 /* set current drive & directory to known location */
1321 GetCurrentDirectoryW( MAX_PATH, curdir);
1322 SetCurrentDirectoryA( temp_path );
1323 i = 0;
1324 while (p[i].file)
1326 filename[0] = 0;
1327 /* update the drive id in the table entry with the current one */
1328 if (p[i].file[1] == ':')
1330 strcpy(filename, p[i].file);
1331 filename[0] = windowsdir[0];
1333 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
1335 strcpy(filename, p[i].file);
1336 filename[4] = windowsdir[0];
1338 else
1340 /* prefix the table entry with the current temp directory */
1341 strcpy(filename, temp_path);
1342 strcat(filename, p[i].file);
1344 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1345 FILE_SHARE_READ | FILE_SHARE_WRITE,
1346 NULL, OPEN_EXISTING,
1347 p[i].options, NULL );
1348 /* if we get ACCESS_DENIED when we do not expect it, assume
1349 * no access to the volume
1351 if (hFile == INVALID_HANDLE_VALUE &&
1352 GetLastError() == ERROR_ACCESS_DENIED &&
1353 p[i].err != ERROR_ACCESS_DENIED)
1355 if (p[i].todo_flag)
1356 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
1357 else
1358 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
1360 /* otherwise validate results with expectations */
1361 else
1363 todo_wine_if (p[i].todo_flag)
1364 ok((hFile == INVALID_HANDLE_VALUE &&
1365 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
1366 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
1367 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
1368 filename, hFile, GetLastError(), p[i].err);
1370 if (hFile != INVALID_HANDLE_VALUE)
1371 CloseHandle( hFile );
1372 i++;
1374 ret = RemoveDirectoryA(dirname);
1375 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
1376 SetCurrentDirectoryW(curdir);
1378 /* test opening directory as a directory */
1379 hFile = CreateFileA( temp_path, GENERIC_READ,
1380 FILE_SHARE_READ,
1381 NULL,
1382 OPEN_EXISTING,
1383 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1384 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
1386 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
1387 "CreateFileA did not work, last error %u on volume <%s>\n",
1388 GetLastError(), temp_path );
1390 if (hFile != INVALID_HANDLE_VALUE)
1392 ret = GetFileInformationByHandle( hFile, &Finfo );
1393 if (ret)
1395 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
1396 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
1397 temp_path, Finfo.dwFileAttributes);
1399 CloseHandle( hFile );
1402 else
1403 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
1406 /* *** Test opening volumes/devices using drive letter *** */
1408 /* test using drive letter in non-rewrite format without trailing \ */
1409 /* this should work */
1410 strcpy(filename, nt_drive);
1411 filename[4] = windowsdir[0];
1412 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1413 FILE_SHARE_READ | FILE_SHARE_WRITE,
1414 NULL, OPEN_EXISTING,
1415 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1416 if (hFile != INVALID_HANDLE_VALUE ||
1417 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
1419 /* if we have adm rights to volume, then try rest of tests */
1420 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1421 filename, GetLastError());
1422 if (hFile != INVALID_HANDLE_VALUE)
1424 /* if we opened the volume/device, try to read it. Since it */
1425 /* opened, we should be able to read it. We don't care about*/
1426 /* what the data is at this time. */
1427 len = 512;
1428 ret = ReadFile( hFile, buffer, len, &len, NULL );
1429 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1430 GetLastError(), ret, filename);
1431 if (ret)
1433 trace("buffer is\n");
1434 dumpmem(buffer, 64);
1436 CloseHandle( hFile );
1439 /* test using drive letter with trailing \ and in non-rewrite */
1440 /* this should not work */
1441 strcpy(filename, nt_drive);
1442 filename[4] = windowsdir[0];
1443 strcat( filename, "\\" );
1444 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1445 FILE_SHARE_READ | FILE_SHARE_WRITE,
1446 NULL, OPEN_EXISTING,
1447 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1448 todo_wine
1449 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1450 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1451 filename, GetLastError());
1452 if (hFile != INVALID_HANDLE_VALUE)
1453 CloseHandle( hFile );
1455 /* test using temp path with trailing \ and in non-rewrite as dir */
1456 /* this should work */
1457 strcpy(filename, nt_drive);
1458 filename[4] = 0;
1459 strcat( filename, temp_path );
1460 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1461 FILE_SHARE_READ | FILE_SHARE_WRITE,
1462 NULL, OPEN_EXISTING,
1463 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1464 ok(hFile != INVALID_HANDLE_VALUE,
1465 "CreateFileA should have worked on %s, but got %u\n",
1466 filename, GetLastError());
1467 if (hFile != INVALID_HANDLE_VALUE)
1468 CloseHandle( hFile );
1470 /* test using drive letter without trailing \ and in device ns */
1471 /* this should work */
1472 strcpy(filename, nt_drive);
1473 filename[4] = windowsdir[0];
1474 filename[2] = '.';
1475 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1476 FILE_SHARE_READ | FILE_SHARE_WRITE,
1477 NULL, OPEN_EXISTING,
1478 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1479 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1480 filename, GetLastError());
1481 if (hFile != INVALID_HANDLE_VALUE)
1482 CloseHandle( hFile );
1484 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
1485 else if (GetLastError() == ERROR_BAD_NETPATH)
1486 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
1487 else
1488 skip("Do not have authority to access volumes. Tests skipped\n");
1491 /* *** Test opening volumes/devices using GUID *** */
1493 if (pGetVolumeNameForVolumeMountPointA)
1495 strcpy(filename, "c:\\");
1496 filename[0] = windowsdir[0];
1497 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
1498 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
1499 if (ret)
1501 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
1503 /* test the result of opening a unique volume name (GUID)
1504 * with the trailing \
1505 * this should error out
1507 strcpy(filename, Volume_1);
1508 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1509 FILE_SHARE_READ | FILE_SHARE_WRITE,
1510 NULL, OPEN_EXISTING,
1511 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1512 todo_wine
1513 ok(hFile == INVALID_HANDLE_VALUE,
1514 "CreateFileA should not have opened %s, hFile %p\n",
1515 filename, hFile);
1516 todo_wine
1517 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1518 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1519 filename, GetLastError());
1520 if (hFile != INVALID_HANDLE_VALUE)
1521 CloseHandle( hFile );
1523 /* test the result of opening a unique volume name (GUID)
1524 * with the temp path string as dir
1525 * this should work
1527 strcpy(filename, Volume_1);
1528 strcat(filename, temp_path+3);
1529 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1530 FILE_SHARE_READ | FILE_SHARE_WRITE,
1531 NULL, OPEN_EXISTING,
1532 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1533 todo_wine
1534 ok(hFile != INVALID_HANDLE_VALUE,
1535 "CreateFileA should have opened %s, but got %u\n",
1536 filename, GetLastError());
1537 if (hFile != INVALID_HANDLE_VALUE)
1538 CloseHandle( hFile );
1540 /* test the result of opening a unique volume name (GUID)
1541 * without the trailing \ and in device namespace
1542 * this should work
1544 strcpy(filename, Volume_1);
1545 filename[2] = '.';
1546 filename[48] = 0;
1547 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1548 FILE_SHARE_READ | FILE_SHARE_WRITE,
1549 NULL, OPEN_EXISTING,
1550 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1551 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1553 /* if we have adm rights to volume, then try rest of tests */
1554 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1555 filename, GetLastError());
1556 if (hFile != INVALID_HANDLE_VALUE)
1558 /* if we opened the volume/device, try to read it. Since it */
1559 /* opened, we should be able to read it. We don't care about*/
1560 /* what the data is at this time. */
1561 len = 512;
1562 ret = ReadFile( hFile, buffer, len, &len, NULL );
1563 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1564 GetLastError(), ret, filename);
1565 if (ret)
1567 trace("buffer is\n");
1568 dumpmem(buffer, 64);
1570 CloseHandle( hFile );
1573 else
1574 skip("Do not have authority to access volumes. Tests skipped\n");
1576 else
1577 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1579 else
1580 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1583 static void test_CreateFileW(void)
1585 HANDLE hFile;
1586 WCHAR temp_path[MAX_PATH];
1587 WCHAR filename[MAX_PATH];
1588 static const WCHAR emptyW[]={'\0'};
1589 static const WCHAR prefix[] = {'p','f','x',0};
1590 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1591 DWORD ret;
1593 ret = GetTempPathW(MAX_PATH, temp_path);
1594 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1596 win_skip("GetTempPathW is not available\n");
1597 return;
1599 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1600 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1602 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1603 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1605 SetLastError(0xdeadbeef);
1606 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1607 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1608 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1609 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1611 SetLastError(0xdeadbeef);
1612 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1613 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1614 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1615 "hFile %p, last error %u\n", hFile, GetLastError());
1617 CloseHandle(hFile);
1619 SetLastError(0xdeadbeef);
1620 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1621 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1622 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1623 "hFile %p, last error %u\n", hFile, GetLastError());
1625 CloseHandle(hFile);
1627 ret = DeleteFileW(filename);
1628 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1630 SetLastError(0xdeadbeef);
1631 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1632 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1633 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1634 "hFile %p, last error %u\n", hFile, GetLastError());
1636 CloseHandle(hFile);
1638 ret = DeleteFileW(filename);
1639 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1641 if (0)
1643 /* this crashes on NT4.0 */
1644 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1645 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1646 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1647 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1650 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1651 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1652 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1653 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1655 /* test the result of opening a nonexistent driver name */
1656 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1657 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1658 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1659 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1661 ret = CreateDirectoryW(filename, NULL);
1662 ok(ret == TRUE, "couldn't create temporary directory\n");
1663 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1664 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1665 ok(hFile != INVALID_HANDLE_VALUE,
1666 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1667 CloseHandle(hFile);
1668 ret = RemoveDirectoryW(filename);
1669 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1672 static void test_CreateFile2(void)
1674 HANDLE hFile, iocp;
1675 WCHAR temp_path[MAX_PATH];
1676 WCHAR filename[MAX_PATH];
1677 CREATEFILE2_EXTENDED_PARAMETERS exparams;
1678 static const WCHAR emptyW[]={'\0'};
1679 static const WCHAR prefix[] = {'p','f','x',0};
1680 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1681 DWORD i, ret;
1683 if (!pCreateFile2)
1685 win_skip("CreateFile2 is missing\n");
1686 return;
1689 ret = GetTempPathW(MAX_PATH, temp_path);
1690 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1691 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1693 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1694 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1696 SetLastError(0xdeadbeef);
1697 exparams.dwSize = sizeof(exparams);
1698 exparams.dwFileAttributes = 0;
1699 exparams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS;
1700 exparams.dwSecurityQosFlags = 0;
1701 exparams.lpSecurityAttributes = NULL;
1702 exparams.hTemplateFile = 0;
1703 hFile = pCreateFile2(filename, GENERIC_READ, 0, CREATE_NEW, &exparams);
1704 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1705 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1707 SetLastError(0xdeadbeef);
1708 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, CREATE_ALWAYS, &exparams);
1709 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1710 "hFile %p, last error %u\n", hFile, GetLastError());
1711 CloseHandle(hFile);
1713 SetLastError(0xdeadbeef);
1714 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1715 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1716 "hFile %p, last error %u\n", hFile, GetLastError());
1717 CloseHandle(hFile);
1719 ret = DeleteFileW(filename);
1720 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1722 SetLastError(0xdeadbeef);
1723 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1724 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1725 "hFile %p, last error %u\n", hFile, GetLastError());
1726 CloseHandle(hFile);
1728 ret = DeleteFileW(filename);
1729 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1731 hFile = pCreateFile2(emptyW, GENERIC_READ, 0, CREATE_NEW, &exparams);
1732 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1733 "CreateFile2(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1735 /* test the result of opening a nonexistent driver name */
1736 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1737 hFile = pCreateFile2(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &exparams);
1738 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1739 "CreateFile2 on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1741 ret = CreateDirectoryW(filename, NULL);
1742 ok(ret == TRUE, "couldn't create temporary directory\n");
1743 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1744 exparams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
1745 SetLastError(0xdeadbeef);
1746 hFile = pCreateFile2(filename, GENERIC_READ | GENERIC_WRITE, 0, OPEN_ALWAYS, &exparams);
1747 ok(hFile != INVALID_HANDLE_VALUE,
1748 "CreateFile2 failed with FILE_FLAG_BACKUP_SEMANTICS on existing directory, error: %d\n", GetLastError());
1749 CloseHandle(hFile);
1750 ret = RemoveDirectoryW(filename);
1751 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1753 for (i = 0; i < 2; ++i)
1755 memset(&exparams, 0, sizeof(exparams));
1756 exparams.dwSize = sizeof(exparams);
1757 if (i == 0)
1759 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1760 exparams.dwFileFlags = FILE_FLAG_OVERLAPPED | FILE_FLAG_DELETE_ON_CLOSE;
1762 else
1764 exparams.dwFileFlags = FILE_ATTRIBUTE_NORMAL;
1765 exparams.dwFileAttributes = FILE_FLAG_OVERLAPPED | FILE_FLAG_DELETE_ON_CLOSE;
1768 SetLastError(0xdeadbeef);
1769 hFile = pCreateFile2(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, &exparams);
1770 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0, "%d: hFile %p, last error %u\n", i, hFile, GetLastError());
1772 iocp = CreateIoCompletionPort(hFile, NULL, 0, 2);
1773 if (i == 1) ok(iocp == NULL && GetLastError() == ERROR_INVALID_PARAMETER, "%d: CreateIoCompletionPort returned %p, error %u\n", i, iocp, GetLastError());
1774 else ok(iocp != INVALID_HANDLE_VALUE && GetLastError() == 0, "%d: CreateIoCompletionPort returned %p, error %u\n", i, iocp, GetLastError());
1776 CloseHandle(iocp);
1777 CloseHandle(hFile);
1779 ret = DeleteFileW(filename);
1780 if (i == 1) ok(ret, "%d: unexpected DeleteFileW failure, error %u\n", i, GetLastError());
1781 else ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "%d: unexpected DeleteFileW result, ret %d error %u\n", i, ret, GetLastError());
1785 static void test_GetTempFileNameA(void)
1787 UINT result;
1788 char out[MAX_PATH];
1789 char expected[MAX_PATH + 10];
1790 char windowsdir[MAX_PATH + 10];
1791 char windowsdrive[3];
1793 result = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1794 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1795 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1797 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1798 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1800 strcat(windowsdir, "\\");
1803 windowsdrive[0] = windowsdir[0];
1804 windowsdrive[1] = windowsdir[1];
1805 windowsdrive[2] = '\0';
1807 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1808 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1809 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1810 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1811 windowsdrive[0], out);
1813 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1814 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1815 expected[0] = '\0';
1816 strcat(expected, windowsdir);
1817 strcat(expected, "abc2.tmp");
1818 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1819 out, expected);
1822 static void test_DeleteFileA( void )
1824 BOOL ret;
1825 char temp_path[MAX_PATH], temp_file[MAX_PATH];
1826 HANDLE hfile, mapping;
1827 char **argv;
1829 ret = DeleteFileA(NULL);
1830 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1831 GetLastError() == ERROR_PATH_NOT_FOUND),
1832 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1834 ret = DeleteFileA("");
1835 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1836 GetLastError() == ERROR_BAD_PATHNAME),
1837 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1839 ret = DeleteFileA("nul");
1840 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1841 GetLastError() == ERROR_INVALID_PARAMETER ||
1842 GetLastError() == ERROR_ACCESS_DENIED ||
1843 GetLastError() == ERROR_INVALID_FUNCTION),
1844 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1846 ret = DeleteFileA("nonexist.txt");
1847 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "DeleteFileA(\"nonexist.txt\") returned ret=%d error=%d\n",ret,GetLastError());
1849 GetTempPathA(MAX_PATH, temp_path);
1850 GetTempFileNameA(temp_path, "tst", 0, temp_file);
1852 SetLastError(0xdeadbeef);
1853 hfile = CreateFileA(temp_file, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1854 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1856 SetLastError(0xdeadbeef);
1857 ret = DeleteFileA(temp_file);
1858 ok(ret, "DeleteFile error %d\n", GetLastError());
1860 SetLastError(0xdeadbeef);
1861 ret = CloseHandle(hfile);
1862 ok(ret, "CloseHandle error %d\n", GetLastError());
1863 ret = DeleteFileA(temp_file);
1864 ok(!ret, "DeleteFile should fail\n");
1866 SetLastError(0xdeadbeef);
1867 ret = CreateDirectoryA("testdir", NULL);
1868 ok(ret, "CreateDirectory failed, got err %d\n", GetLastError());
1869 ret = DeleteFileA("testdir");
1870 ok(!ret && GetLastError() == ERROR_ACCESS_DENIED,
1871 "Expected ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
1872 ret = RemoveDirectoryA("testdir");
1873 ok(ret, "Remove a directory failed, got error %d\n", GetLastError());
1875 winetest_get_mainargs(&argv);
1877 ret = CopyFileA(argv[0], temp_file, FALSE);
1878 ok(ret, "got error %u\n", GetLastError());
1879 hfile = CreateFileA(temp_file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
1880 ok(hfile != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
1882 mapping = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL);
1883 ok(!!mapping, "got error %u\n", GetLastError());
1885 SetLastError(0xdeadbeef);
1886 ret = DeleteFileA(temp_file);
1887 ok(!ret, "expected failure\n");
1888 ok(GetLastError() == ERROR_ACCESS_DENIED, "got error %u\n", GetLastError());
1890 CloseHandle(mapping);
1892 ret = DeleteFileA(temp_file);
1893 ok(ret, "got error %u\n", GetLastError());
1895 CloseHandle(hfile);
1898 static void test_DeleteFileW( void )
1900 BOOL ret;
1901 WCHAR pathW[MAX_PATH];
1902 WCHAR pathsubW[MAX_PATH];
1903 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1904 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1905 static const WCHAR emptyW[]={'\0'};
1907 ret = DeleteFileW(NULL);
1908 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1910 win_skip("DeleteFileW is not available\n");
1911 return;
1913 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1914 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1916 ret = DeleteFileW(emptyW);
1917 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1918 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1920 /* test DeleteFile on empty directory */
1921 ret = GetTempPathW(MAX_PATH, pathW);
1922 if (ret + ARRAY_SIZE(dirW)-1 + ARRAY_SIZE(subdirW)-1 >= MAX_PATH)
1924 ok(0, "MAX_PATH exceeded in constructing paths\n");
1925 return;
1927 lstrcatW(pathW, dirW);
1928 lstrcpyW(pathsubW, pathW);
1929 lstrcatW(pathsubW, subdirW);
1930 ret = CreateDirectoryW(pathW, NULL);
1931 ok(ret == TRUE, "couldn't create directory deletefile\n");
1932 ret = DeleteFileW(pathW);
1933 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1934 ret = RemoveDirectoryW(pathW);
1935 ok(ret == TRUE, "expected to remove directory deletefile\n");
1937 /* test DeleteFile on non-empty directory */
1938 ret = CreateDirectoryW(pathW, NULL);
1939 ok(ret == TRUE, "couldn't create directory deletefile\n");
1940 ret = CreateDirectoryW(pathsubW, NULL);
1941 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1942 ret = DeleteFileW(pathW);
1943 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1944 ret = RemoveDirectoryW(pathsubW);
1945 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1946 ret = RemoveDirectoryW(pathW);
1947 ok(ret == TRUE, "expected to remove directory deletefile\n");
1950 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1952 static void test_MoveFileA(void)
1954 char tempdir[MAX_PATH];
1955 char source[MAX_PATH], dest[MAX_PATH];
1956 static const char prefix[] = "pfx";
1957 HANDLE hfile;
1958 HANDLE hmapfile;
1959 DWORD ret;
1960 BOOL retok;
1962 ret = GetTempPathA(MAX_PATH, tempdir);
1963 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1964 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1966 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1967 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1969 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1970 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1972 ret = MoveFileA(source, source);
1973 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1975 ret = MoveFileA(source, dest);
1976 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1977 "MoveFileA: unexpected error %d\n", GetLastError());
1979 ret = DeleteFileA(dest);
1980 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1982 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1983 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1985 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1986 ok( retok && ret == sizeof(prefix),
1987 "WriteFile error %d\n", GetLastError());
1989 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1990 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1992 ret = MoveFileA(source, dest);
1993 ok(!ret, "MoveFileA: expected failure\n");
1994 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1995 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1996 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1998 CloseHandle(hmapfile);
1999 CloseHandle(hfile);
2001 /* if MoveFile succeeded, move back to dest */
2002 if (ret) MoveFileA(dest, source);
2004 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2005 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
2007 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
2008 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
2010 ret = MoveFileA(source, dest);
2011 ok(!ret, "MoveFileA: expected failure\n");
2012 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
2013 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
2014 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
2016 CloseHandle(hmapfile);
2017 CloseHandle(hfile);
2019 /* if MoveFile succeeded, move back to dest */
2020 if (ret) MoveFileA(dest, source);
2022 ret = MoveFileA(source, dest);
2023 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
2025 lstrcatA(tempdir, "Remove Me");
2026 ret = CreateDirectoryA(tempdir, NULL);
2027 ok(ret == TRUE, "CreateDirectoryA failed\n");
2029 lstrcpyA(source, dest);
2030 lstrcpyA(dest, tempdir);
2031 lstrcatA(dest, "\\wild?.*");
2032 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
2033 ret = MoveFileA(source, dest);
2034 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
2035 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
2036 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
2037 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
2038 if (ret || (GetLastError() != ERROR_INVALID_NAME))
2040 WIN32_FIND_DATAA fd;
2041 char temppath[MAX_PATH];
2042 HANDLE hFind;
2044 lstrcpyA(temppath, tempdir);
2045 lstrcatA(temppath, "\\*.*");
2046 hFind = FindFirstFileA(temppath, &fd);
2047 if (INVALID_HANDLE_VALUE != hFind)
2049 LPSTR lpName;
2052 lpName = fd.cAlternateFileName;
2053 if (!lpName[0])
2054 lpName = fd.cFileName;
2055 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
2057 while (FindNextFileA(hFind, &fd));
2058 FindClose(hFind);
2061 ret = DeleteFileA(source);
2062 ok(ret, "DeleteFileA: error %d\n", GetLastError());
2063 ret = DeleteFileA(dest);
2064 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
2065 ret = RemoveDirectoryA(tempdir);
2066 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
2069 static void test_MoveFileW(void)
2071 WCHAR temp_path[MAX_PATH];
2072 WCHAR source[MAX_PATH], dest[MAX_PATH];
2073 static const WCHAR prefix[] = {'p','f','x',0};
2074 DWORD ret;
2076 ret = GetTempPathW(MAX_PATH, temp_path);
2077 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2079 win_skip("GetTempPathW is not available\n");
2080 return;
2082 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2083 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2085 ret = GetTempFileNameW(temp_path, prefix, 0, source);
2086 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
2088 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
2089 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
2091 ret = MoveFileW(source, dest);
2092 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
2093 "CopyFileW: unexpected error %d\n", GetLastError());
2095 ret = DeleteFileW(source);
2096 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2097 ret = DeleteFileW(dest);
2098 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2101 #define PATTERN_OFFSET 0x10
2103 static void test_offset_in_overlapped_structure(void)
2105 HANDLE hFile;
2106 OVERLAPPED ov;
2107 DWORD done, offset;
2108 BOOL rc;
2109 BYTE buf[256], pattern[] = "TeSt";
2110 UINT i;
2111 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
2112 BOOL ret;
2114 ret =GetTempPathA(MAX_PATH, temp_path);
2115 ok( ret, "GetTempPathA error %d\n", GetLastError());
2116 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
2117 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
2119 /*** Write File *****************************************************/
2121 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
2122 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2124 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
2125 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
2126 ok( ret, "WriteFile error %d\n", GetLastError());
2127 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
2129 memset(&ov, 0, sizeof(ov));
2130 S(U(ov)).Offset = PATTERN_OFFSET;
2131 S(U(ov)).OffsetHigh = 0;
2132 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2133 /* Win 9x does not support the overlapped I/O on files */
2134 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2135 ok(rc, "WriteFile error %d\n", GetLastError());
2136 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2137 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2138 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2140 S(U(ov)).Offset = sizeof(buf) * 2;
2141 S(U(ov)).OffsetHigh = 0;
2142 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2143 ok( ret, "WriteFile error %d\n", GetLastError());
2144 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2145 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2146 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
2149 CloseHandle(hFile);
2151 /*** Read File *****************************************************/
2153 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
2154 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2156 memset(buf, 0, sizeof(buf));
2157 memset(&ov, 0, sizeof(ov));
2158 S(U(ov)).Offset = PATTERN_OFFSET;
2159 S(U(ov)).OffsetHigh = 0;
2160 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
2161 /* Win 9x does not support the overlapped I/O on files */
2162 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2163 ok(rc, "ReadFile error %d\n", GetLastError());
2164 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
2165 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2166 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2167 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
2170 CloseHandle(hFile);
2172 ret = DeleteFileA(temp_fname);
2173 ok( ret, "DeleteFileA error %d\n", GetLastError());
2176 static void test_LockFile(void)
2178 HANDLE handle, handle2;
2179 DWORD written;
2180 OVERLAPPED overlapped;
2181 int limited_LockFile;
2182 int limited_UnLockFile;
2183 BOOL ret;
2185 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2186 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2187 CREATE_ALWAYS, 0, 0 );
2188 if (handle == INVALID_HANDLE_VALUE)
2190 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2191 return;
2193 handle2 = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2194 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2195 OPEN_EXISTING, 0, 0 );
2196 if (handle2 == INVALID_HANDLE_VALUE)
2198 ok( 0, "couldn't open file \"%s\" (err=%d)\n", filename, GetLastError() );
2199 goto cleanup;
2201 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
2203 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
2204 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
2206 limited_UnLockFile = 0;
2207 if (UnlockFile( handle, 0, 0, 0, 0 ))
2209 limited_UnLockFile = 1;
2212 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
2213 /* overlapping locks must fail */
2214 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
2215 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
2216 /* non-overlapping locks must succeed */
2217 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
2219 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
2220 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
2221 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
2222 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
2224 S(U(overlapped)).Offset = 100;
2225 S(U(overlapped)).OffsetHigh = 0;
2226 overlapped.hEvent = 0;
2228 /* Test for broken LockFileEx a la Windows 95 OSR2. */
2229 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
2231 /* LockFileEx is probably OK, test it more. */
2232 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
2233 "LockFileEx 100,100 failed\n" );
2236 /* overlapping shared locks are OK */
2237 S(U(overlapped)).Offset = 150;
2238 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
2240 /* but exclusive is not */
2241 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
2242 0, 50, 0, &overlapped ),
2243 "LockFileEx exclusive 150,50 succeeded\n" );
2244 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
2245 { /* UnLockFile is capable. */
2246 S(U(overlapped)).Offset = 100;
2247 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
2248 "UnlockFileEx 150,100 again succeeded\n" );
2251 /* shared lock can overlap exclusive if handles are equal */
2252 S(U(overlapped)).Offset = 300;
2253 ok( LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0, &overlapped ),
2254 "LockFileEx exclusive 300,100 failed\n" );
2255 ok( !LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2256 "LockFileEx handle2 300,100 succeeded\n" );
2257 ret = LockFileEx( handle, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped );
2258 ok( ret, "LockFileEx 300,100 failed\n" );
2259 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2260 /* exclusive lock is removed first */
2261 ok( LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2262 "LockFileEx handle2 300,100 failed\n" );
2263 ok( UnlockFileEx( handle2, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2264 if (ret)
2265 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2267 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
2268 if (ret)
2270 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
2271 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
2272 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
2274 else /* win9x */
2275 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
2277 /* wrap-around lock should not do anything */
2278 /* (but still succeeds on NT4 so we don't check result) */
2279 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
2281 limited_LockFile = 0;
2282 if (!LockFile( handle, ~0, ~0, 1, 0 ))
2284 limited_LockFile = 1;
2287 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
2289 /* zero-byte lock */
2290 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
2291 if (!limited_LockFile) ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
2292 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
2293 if (!limited_LockFile) ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
2295 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
2296 ok( !UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 succeeded\n" );
2298 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
2300 CloseHandle( handle2 );
2301 cleanup:
2302 CloseHandle( handle );
2303 DeleteFileA( filename );
2306 static BOOL create_fake_dll( LPCSTR filename )
2308 IMAGE_DOS_HEADER *dos;
2309 IMAGE_NT_HEADERS *nt;
2310 IMAGE_SECTION_HEADER *sec;
2311 BYTE *buffer;
2312 DWORD lfanew = sizeof(*dos);
2313 DWORD size = lfanew + sizeof(*nt) + sizeof(*sec);
2314 DWORD written;
2315 BOOL ret;
2317 HANDLE file = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2318 if (file == INVALID_HANDLE_VALUE) return FALSE;
2320 buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
2322 dos = (IMAGE_DOS_HEADER *)buffer;
2323 dos->e_magic = IMAGE_DOS_SIGNATURE;
2324 dos->e_cblp = sizeof(*dos);
2325 dos->e_cp = 1;
2326 dos->e_cparhdr = lfanew / 16;
2327 dos->e_minalloc = 0;
2328 dos->e_maxalloc = 0xffff;
2329 dos->e_ss = 0x0000;
2330 dos->e_sp = 0x00b8;
2331 dos->e_lfarlc = lfanew;
2332 dos->e_lfanew = lfanew;
2334 nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
2335 nt->Signature = IMAGE_NT_SIGNATURE;
2336 #if defined __i386__
2337 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
2338 #elif defined __x86_64__
2339 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
2340 #elif defined __arm__
2341 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT;
2342 #elif defined __aarch64__
2343 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
2344 #else
2345 # error You must specify the machine type
2346 #endif
2347 nt->FileHeader.NumberOfSections = 1;
2348 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2349 nt->FileHeader.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_EXECUTABLE_IMAGE;
2350 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
2351 nt->OptionalHeader.MajorLinkerVersion = 1;
2352 nt->OptionalHeader.MinorLinkerVersion = 0;
2353 nt->OptionalHeader.ImageBase = 0x10000000;
2354 nt->OptionalHeader.SectionAlignment = 0x1000;
2355 nt->OptionalHeader.FileAlignment = 0x1000;
2356 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
2357 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
2358 nt->OptionalHeader.MajorImageVersion = 1;
2359 nt->OptionalHeader.MinorImageVersion = 0;
2360 nt->OptionalHeader.MajorSubsystemVersion = 4;
2361 nt->OptionalHeader.MinorSubsystemVersion = 0;
2362 nt->OptionalHeader.SizeOfImage = 0x2000;
2363 nt->OptionalHeader.SizeOfHeaders = size;
2364 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
2365 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
2367 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
2368 memcpy( sec->Name, ".rodata", sizeof(".rodata") );
2369 sec->Misc.VirtualSize = 0x1000;
2370 sec->VirtualAddress = 0x1000;
2371 sec->SizeOfRawData = 0;
2372 sec->PointerToRawData = 0;
2373 sec->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
2375 ret = WriteFile( file, buffer, size, &written, NULL ) && written == size;
2376 HeapFree( GetProcessHeap(), 0, buffer );
2377 CloseHandle( file );
2378 return ret;
2381 static unsigned int map_file_access( unsigned int access )
2383 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2384 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2385 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2386 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2387 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2390 static BOOL is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
2392 access1 = map_file_access( access1 );
2393 access2 = map_file_access( access2 );
2394 access1 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2395 access2 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2397 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2398 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2400 if ((access1 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing2 & FILE_SHARE_READ)) return FALSE;
2401 if ((access1 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2402 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return FALSE;
2403 if ((access2 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing1 & FILE_SHARE_READ)) return FALSE;
2404 if ((access2 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing1 & FILE_SHARE_WRITE)) return FALSE;
2405 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return FALSE;
2406 return TRUE;
2409 static BOOL is_sharing_map_compatible( DWORD map_access, DWORD access2, DWORD sharing2 )
2411 if ((map_access == PAGE_READWRITE || map_access == PAGE_EXECUTE_READWRITE) &&
2412 !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2413 access2 = map_file_access( access2 );
2414 if ((map_access & SEC_IMAGE) && (access2 & FILE_WRITE_DATA)) return FALSE;
2415 return TRUE;
2418 static void test_file_sharing(void)
2420 struct mode { DWORD dw; const char* str; };
2421 #define M(x) {x, # x}
2422 static const struct mode access_modes[] =
2423 { M(0), M(GENERIC_READ), M(GENERIC_WRITE), M(GENERIC_READ|GENERIC_WRITE),
2424 M(DELETE), M(GENERIC_READ|DELETE), M(GENERIC_WRITE|DELETE), M(GENERIC_READ|GENERIC_WRITE|DELETE),
2425 M(GENERIC_EXECUTE), M(GENERIC_EXECUTE | DELETE),
2426 M(FILE_READ_DATA), M(FILE_WRITE_DATA), M(FILE_APPEND_DATA), M(FILE_READ_EA), M(FILE_WRITE_EA),
2427 M(FILE_READ_DATA | FILE_EXECUTE), M(FILE_WRITE_DATA | FILE_EXECUTE), M(FILE_APPEND_DATA | FILE_EXECUTE),
2428 M(FILE_READ_EA | FILE_EXECUTE), M(FILE_WRITE_EA | FILE_EXECUTE), M(FILE_EXECUTE),
2429 M(FILE_DELETE_CHILD), M(FILE_READ_ATTRIBUTES), M(FILE_WRITE_ATTRIBUTES) };
2430 static const struct mode sharing_modes[] =
2431 { M(0), M(FILE_SHARE_READ),
2432 M(FILE_SHARE_WRITE), M(FILE_SHARE_READ|FILE_SHARE_WRITE),
2433 M(FILE_SHARE_DELETE), M(FILE_SHARE_READ|FILE_SHARE_DELETE),
2434 M(FILE_SHARE_WRITE|FILE_SHARE_DELETE), M(FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE) };
2435 static const struct mode mapping_modes[] =
2436 { M(PAGE_READONLY), M(PAGE_WRITECOPY), M(PAGE_READWRITE), M(SEC_IMAGE | PAGE_WRITECOPY) };
2437 #undef M
2438 int a1, s1, a2, s2;
2439 int ret;
2440 HANDLE h, h2;
2442 /* make sure the file exists */
2443 if (!create_fake_dll( filename ))
2445 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
2446 return;
2449 for (a1 = 0; a1 < ARRAY_SIZE(access_modes); a1++)
2451 for (s1 = 0; s1 < ARRAY_SIZE(sharing_modes); s1++)
2453 SetLastError(0xdeadbeef);
2454 h = CreateFileA( filename, access_modes[a1].dw, sharing_modes[s1].dw,
2455 NULL, OPEN_EXISTING, 0, 0 );
2456 if (h == INVALID_HANDLE_VALUE)
2458 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2459 return;
2461 for (a2 = 0; a2 < ARRAY_SIZE(access_modes); a2++)
2463 for (s2 = 0; s2 < ARRAY_SIZE(sharing_modes); s2++)
2465 SetLastError(0xdeadbeef);
2466 h2 = CreateFileA( filename, access_modes[a2].dw, sharing_modes[s2].dw,
2467 NULL, OPEN_EXISTING, 0, 0 );
2468 ret = GetLastError();
2469 if (is_sharing_compatible( access_modes[a1].dw, sharing_modes[s1].dw,
2470 access_modes[a2].dw, sharing_modes[s2].dw ))
2472 ok( h2 != INVALID_HANDLE_VALUE,
2473 "open failed for modes %s / %s / %s / %s\n",
2474 access_modes[a1].str, sharing_modes[s1].str,
2475 access_modes[a2].str, sharing_modes[s2].str );
2476 ok( ret == 0, "wrong error code %d\n", ret );
2478 else
2480 ok( h2 == INVALID_HANDLE_VALUE,
2481 "open succeeded for modes %s / %s / %s / %s\n",
2482 access_modes[a1].str, sharing_modes[s1].str,
2483 access_modes[a2].str, sharing_modes[s2].str );
2484 ok( ret == ERROR_SHARING_VIOLATION,
2485 "wrong error code %d\n", ret );
2487 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2490 CloseHandle( h );
2494 for (a1 = 0; a1 < ARRAY_SIZE(mapping_modes); a1++)
2496 HANDLE m;
2498 create_fake_dll( filename );
2499 SetLastError(0xdeadbeef);
2500 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2501 if (h == INVALID_HANDLE_VALUE)
2503 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2504 return;
2506 m = CreateFileMappingA( h, NULL, mapping_modes[a1].dw, 0, 0, NULL );
2507 ok( m != 0, "failed to create mapping %s err %u\n", mapping_modes[a1].str, GetLastError() );
2508 CloseHandle( h );
2509 if (!m) continue;
2511 for (a2 = 0; a2 < ARRAY_SIZE(access_modes); a2++)
2513 for (s2 = 0; s2 < ARRAY_SIZE(sharing_modes); s2++)
2515 SetLastError(0xdeadbeef);
2516 h2 = CreateFileA( filename, access_modes[a2].dw, sharing_modes[s2].dw,
2517 NULL, OPEN_EXISTING, 0, 0 );
2519 ret = GetLastError();
2520 if (h2 == INVALID_HANDLE_VALUE)
2522 ok( !is_sharing_map_compatible(mapping_modes[a1].dw, access_modes[a2].dw, sharing_modes[s2].dw),
2523 "open failed for modes map %s / %s / %s\n",
2524 mapping_modes[a1].str, access_modes[a2].str, sharing_modes[s2].str );
2525 ok( ret == ERROR_SHARING_VIOLATION,
2526 "wrong error code %d\n", ret );
2528 else
2530 if (!is_sharing_map_compatible(mapping_modes[a1].dw, access_modes[a2].dw, sharing_modes[s2].dw))
2531 ok( broken(1), /* no checking on nt4 */
2532 "open succeeded for modes map %s / %s / %s\n",
2533 mapping_modes[a1].str, access_modes[a2].str, sharing_modes[s2].str );
2534 ok( ret == 0xdeadbeef /* Win9x */ ||
2535 ret == 0, /* XP */
2536 "wrong error code %d\n", ret );
2537 CloseHandle( h2 );
2542 /* try CREATE_ALWAYS over an existing mapping */
2543 SetLastError(0xdeadbeef);
2544 h2 = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
2545 NULL, CREATE_ALWAYS, 0, 0 );
2546 ret = GetLastError();
2547 if (mapping_modes[a1].dw & SEC_IMAGE)
2549 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %s\n", mapping_modes[a1].str );
2550 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %s\n", ret, mapping_modes[a1].str );
2552 else
2554 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %s\n", mapping_modes[a1].str );
2555 ok( ret == ERROR_USER_MAPPED_FILE, "wrong error code %d for %s\n", ret, mapping_modes[a1].str );
2557 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2559 /* try DELETE_ON_CLOSE over an existing mapping */
2560 SetLastError(0xdeadbeef);
2561 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2562 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 );
2563 ret = GetLastError();
2564 if (mapping_modes[a1].dw & SEC_IMAGE)
2566 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %s\n", mapping_modes[a1].str );
2567 ok( ret == ERROR_ACCESS_DENIED, "wrong error code %d for %s\n", ret, mapping_modes[a1].str );
2569 else
2571 ok( h2 != INVALID_HANDLE_VALUE, "open failed for map %s err %u\n", mapping_modes[a1].str, ret );
2573 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2575 CloseHandle( m );
2578 SetLastError(0xdeadbeef);
2579 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
2580 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2582 SetLastError(0xdeadbeef);
2583 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
2584 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
2585 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
2587 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
2588 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2590 CloseHandle(h);
2591 CloseHandle(h2);
2593 DeleteFileA( filename );
2596 static char get_windows_drive(void)
2598 char windowsdir[MAX_PATH];
2599 GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
2600 return windowsdir[0];
2603 static void test_FindFirstFileA(void)
2605 HANDLE handle;
2606 WIN32_FIND_DATAA data;
2607 int err;
2608 char buffer[5] = "C:\\";
2609 char buffer2[100];
2610 char nonexistent[MAX_PATH];
2612 /* try FindFirstFileA on "C:\" */
2613 buffer[0] = get_windows_drive();
2615 SetLastError( 0xdeadbeaf );
2616 handle = FindFirstFileA(buffer, &data);
2617 err = GetLastError();
2618 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
2619 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2621 /* try FindFirstFileA on "C:\*" */
2622 strcpy(buffer2, buffer);
2623 strcat(buffer2, "*");
2624 handle = FindFirstFileA(buffer2, &data);
2625 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2626 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2627 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
2628 if (FindNextFileA( handle, &data ))
2629 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2630 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
2631 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2633 /* try FindFirstFileA on windows dir */
2634 GetWindowsDirectoryA( buffer2, sizeof(buffer2) );
2635 strcat(buffer2, "\\*");
2636 handle = FindFirstFileA(buffer2, &data);
2637 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2638 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
2639 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
2640 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
2641 while (FindNextFileA( handle, &data ))
2642 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2643 "FindNextFile shouldn't return '%s'\n", data.cFileName );
2644 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2646 /* try FindFirstFileA on "C:\foo\" */
2647 SetLastError( 0xdeadbeaf );
2648 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
2650 char tmp[MAX_PATH];
2651 GetTempPathA( sizeof(tmp), tmp );
2652 GetTempFileNameA( tmp, "foo", 0, nonexistent );
2654 DeleteFileA( nonexistent );
2655 strcpy(buffer2, nonexistent);
2656 strcat(buffer2, "\\");
2657 handle = FindFirstFileA(buffer2, &data);
2658 err = GetLastError();
2659 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2660 todo_wine {
2661 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2664 /* try FindFirstFileA without trailing backslash */
2665 SetLastError( 0xdeadbeaf );
2666 strcpy(buffer2, nonexistent);
2667 handle = FindFirstFileA(buffer2, &data);
2668 err = GetLastError();
2669 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2670 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2672 /* try FindFirstFileA on "C:\foo\bar.txt" */
2673 SetLastError( 0xdeadbeaf );
2674 strcpy(buffer2, nonexistent);
2675 strcat(buffer2, "\\bar.txt");
2676 handle = FindFirstFileA(buffer2, &data);
2677 err = GetLastError();
2678 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2679 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2681 /* try FindFirstFileA on "C:\foo\*.*" */
2682 SetLastError( 0xdeadbeaf );
2683 strcpy(buffer2, nonexistent);
2684 strcat(buffer2, "\\*.*");
2685 handle = FindFirstFileA(buffer2, &data);
2686 err = GetLastError();
2687 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2688 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2690 /* try FindFirstFileA on "foo\bar.txt" */
2691 SetLastError( 0xdeadbeaf );
2692 strcpy(buffer2, nonexistent + 3);
2693 strcat(buffer2, "\\bar.txt");
2694 handle = FindFirstFileA(buffer2, &data);
2695 err = GetLastError();
2696 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2697 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2699 /* try FindFirstFileA on "c:\nul" */
2700 SetLastError( 0xdeadbeaf );
2701 strcpy(buffer2, buffer);
2702 strcat(buffer2, "nul");
2703 handle = FindFirstFileA(buffer2, &data);
2704 err = GetLastError();
2705 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2706 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
2707 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2708 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2709 "wrong attributes %x\n", data.dwFileAttributes );
2710 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2712 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2713 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2715 SetLastError( 0xdeadbeaf );
2716 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2717 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2718 ok( FindClose( handle ), "failed to close handle\n" );
2720 /* try FindFirstFileA on "lpt1" */
2721 SetLastError( 0xdeadbeaf );
2722 strcpy(buffer2, "lpt1");
2723 handle = FindFirstFileA(buffer2, &data);
2724 err = GetLastError();
2725 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2726 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
2727 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2728 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2729 "wrong attributes %x\n", data.dwFileAttributes );
2730 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2732 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2733 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2735 SetLastError( 0xdeadbeaf );
2736 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2737 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2738 ok( FindClose( handle ), "failed to close handle\n" );
2740 /* try FindFirstFileA on "c:\nul\*" */
2741 SetLastError( 0xdeadbeaf );
2742 strcpy(buffer2, buffer);
2743 strcat(buffer2, "nul\\*");
2744 handle = FindFirstFileA(buffer2, &data);
2745 err = GetLastError();
2746 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2747 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2749 /* try FindFirstFileA on "c:\nul*" */
2750 SetLastError( 0xdeadbeaf );
2751 strcpy(buffer2, buffer);
2752 strcat(buffer2, "nul*");
2753 handle = FindFirstFileA(buffer2, &data);
2754 err = GetLastError();
2755 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2756 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2758 /* try FindFirstFileA on "c:\foo\bar\nul" */
2759 SetLastError( 0xdeadbeaf );
2760 strcpy(buffer2, buffer);
2761 strcat(buffer2, "foo\\bar\\nul");
2762 handle = FindFirstFileA(buffer2, &data);
2763 err = GetLastError();
2764 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2765 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2767 /* try FindFirstFileA on "c:\foo\nul\bar" */
2768 SetLastError( 0xdeadbeaf );
2769 strcpy(buffer2, buffer);
2770 strcat(buffer2, "foo\\nul\\bar");
2771 handle = FindFirstFileA(buffer2, &data);
2772 err = GetLastError();
2773 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2774 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2777 static void test_FindNextFileA(void)
2779 HANDLE handle;
2780 WIN32_FIND_DATAA search_results;
2781 int err;
2782 char buffer[5] = "C:\\*";
2784 buffer[0] = get_windows_drive();
2785 handle = FindFirstFileA(buffer,&search_results);
2786 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
2787 while (FindNextFileA(handle, &search_results))
2789 /* get to the end of the files */
2791 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
2792 err = GetLastError();
2793 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
2796 static void test_FindFirstFileExA(FINDEX_INFO_LEVELS level, FINDEX_SEARCH_OPS search_ops, DWORD flags)
2798 WIN32_FIND_DATAA search_results;
2799 HANDLE handle;
2800 BOOL ret;
2802 if (!pFindFirstFileExA)
2804 win_skip("FindFirstFileExA() is missing\n");
2805 return;
2808 trace("Running FindFirstFileExA tests with level=%d, search_ops=%d, flags=%u\n",
2809 level, search_ops, flags);
2811 CreateDirectoryA("test-dir", NULL);
2812 _lclose(_lcreat("test-dir\\file1", 0));
2813 _lclose(_lcreat("test-dir\\file2", 0));
2814 CreateDirectoryA("test-dir\\dir1", NULL);
2815 SetLastError(0xdeadbeef);
2816 handle = pFindFirstFileExA("test-dir\\*", level, &search_results, search_ops, NULL, flags);
2817 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2819 win_skip("FindFirstFileExA is not implemented\n");
2820 goto cleanup;
2822 if ((flags & FIND_FIRST_EX_LARGE_FETCH) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2824 win_skip("FindFirstFileExA flag FIND_FIRST_EX_LARGE_FETCH not supported, skipping test\n");
2825 goto cleanup;
2827 if ((level == FindExInfoBasic) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2829 win_skip("FindFirstFileExA level FindExInfoBasic not supported, skipping test\n");
2830 goto cleanup;
2833 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2834 #define CHECK_LEVEL(fn) (level != FindExInfoBasic || !(fn)[0])
2836 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
2837 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
2838 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2840 ok(FindNextFileA(handle, &search_results), "Fetching second file failed\n");
2841 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
2842 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2844 ok(FindNextFileA(handle, &search_results), "Fetching third file failed\n");
2845 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
2846 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2848 SetLastError(0xdeadbeef);
2849 ret = FindNextFileA(handle, &search_results);
2850 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
2852 skip("File system supports directory filtering\n");
2853 /* Results from the previous call are not cleared */
2854 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
2855 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2858 else
2860 ok(ret, "Fetching fourth file failed\n");
2861 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
2862 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2864 ok(FindNextFileA(handle, &search_results), "Fetching fifth file failed\n");
2865 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
2866 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2868 ok(FindNextFileA(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
2871 #undef CHECK_NAME
2872 #undef CHECK_LEVEL
2874 FindClose( handle );
2876 /* Most Windows systems seem to ignore the FIND_FIRST_EX_CASE_SENSITIVE flag. Unofficial documentation
2877 * suggests that there are registry keys and that it might depend on the used filesystem. */
2878 SetLastError(0xdeadbeef);
2879 handle = pFindFirstFileExA("TEST-DIR\\*", level, &search_results, search_ops, NULL, flags);
2880 if (flags & FIND_FIRST_EX_CASE_SENSITIVE)
2882 ok(handle != INVALID_HANDLE_VALUE || GetLastError() == ERROR_PATH_NOT_FOUND,
2883 "Unexpected error %x, expected valid handle or ERROR_PATH_NOT_FOUND\n", GetLastError());
2884 trace("FindFirstFileExA flag FIND_FIRST_EX_CASE_SENSITIVE is %signored\n",
2885 (handle == INVALID_HANDLE_VALUE) ? "not " : "");
2887 else
2888 ok(handle != INVALID_HANDLE_VALUE, "Unexpected error %x, expected valid handle\n", GetLastError());
2889 if (handle != INVALID_HANDLE_VALUE)
2890 FindClose( handle );
2892 cleanup:
2893 DeleteFileA("test-dir\\file1");
2894 DeleteFileA("test-dir\\file2");
2895 RemoveDirectoryA("test-dir\\dir1");
2896 RemoveDirectoryA("test-dir");
2899 static void test_FindFirstFile_wildcards(void)
2901 WIN32_FIND_DATAA find_data;
2902 HANDLE handle;
2903 int i;
2904 static const char* files[] = {
2905 "..a", "..a.a", ".a", ".a..a", ".a.a", ".aaa",
2906 "a", "a..a", "a.a", "a.a.a", "aa", "aaa", "aaaa"
2908 static const struct {
2909 int todo;
2910 const char *pattern, *result;
2911 } tests[] = {
2912 {0, "*.*.*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2913 {0, "*.*.", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2914 {0, ".*.*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa'"},
2915 {0, "*.*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2916 {0, ".*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa'"},
2917 {1, "*.", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2918 {0, "*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2919 {1, "*..*", ", '.', '..', '..a', '..a.a', '.a..a', 'a..a'"},
2920 {1, "*..", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2921 {1, ".*.", ", '.', '..', '.a', '.aaa'"},
2922 {0, "..*", ", '.', '..', '..a', '..a.a'"},
2923 {0, "**", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2924 {0, "**.", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2925 {0, "*. ", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2926 {1, "* .", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2927 {0, "* . ", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2928 {0, "*.. ", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2929 {1, "*. .", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2930 {1, "* ..", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2931 {0, " *..", ""},
2932 {0, "..* ", ", '.', '..', '..a', '..a.a'"},
2934 {1, "<.<.<", ", '..a', '..a.a', '.a..a', '.a.a', 'a..a', 'a.a.a'"},
2935 {1, "<.<.", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a..a', 'a.a', 'a.a.a'"},
2936 {1, ".<.<", ", '..a', '..a.a', '.a..a', '.a.a'"},
2937 {1, "<.<", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a..a', 'a.a', 'a.a.a'"},
2938 {1, ".<", ", '.', '..', '.a', '.aaa'"},
2939 {1, "<.", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2940 {1, "<", ", '.', '..', '..a', '.a', '.aaa', 'a', 'aa', 'aaa', 'aaaa'"},
2941 {1, "<..<", ", '..a', '.a..a', 'a..a'"},
2942 {1, "<..", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2943 {1, ".<.", ", '.', '..', '.a', '.aaa'"},
2944 {1, "..<", ", '..a'"},
2945 {1, "<<", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2946 {1, "<<.", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2947 {1, "<. ", ", '.', '..', '..a', '.a', '.aaa', 'a', 'aa', 'aaa', 'aaaa'"},
2948 {1, "< .", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2949 {1, "< . ", ", '.', '..', '..a', '.a', '.aaa', 'a', 'aa', 'aaa', 'aaaa'"},
2950 {1, "<.. ", ", '.', '..', '..a', '.a', '.aaa', 'a', 'aa', 'aaa', 'aaaa'"},
2951 {1, "<. .", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2952 {1, "< ..", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2953 {0, " <..", ""},
2954 {1, "..< ", ", '..a'"},
2956 {1, "?", ", '.', '..', 'a'"},
2957 {1, "?.", ", '.', '..', 'a'"},
2958 {1, "?. ", ", '.', '..', 'a'"},
2959 {1, "??.", ", '.', '..', 'a', 'aa'"},
2960 {1, "??. ", ", '.', '..', 'a', 'aa'"},
2961 {1, "???.", ", '.', '..', 'a', 'aa', 'aaa'"},
2962 {1, "?.??.", ", '.', '..', '.a', 'a', 'a.a'"},
2964 {1, ">", ", '.', '..', 'a'"},
2965 {1, ">.", ", '.', '..', 'a'"},
2966 {1, ">. ", ", '.', '..', 'a'"},
2967 {1, ">>.", ", '.', '..', 'a', 'aa'"},
2968 {1, ">>. ", ", '.', '..', 'a', 'aa'"},
2969 {1, ">>>.", ", '.', '..', 'a', 'aa', 'aaa'"},
2970 {1, ">.>>.", ", '.', '..', '.a', 'a.a'"},
2973 CreateDirectoryA("test-dir", NULL);
2974 SetCurrentDirectoryA("test-dir");
2975 for (i = 0; i < ARRAY_SIZE(files); ++i)
2976 _lclose(_lcreat(files[i], 0));
2978 for (i = 0; i < ARRAY_SIZE(tests); ++i)
2980 char correct[512];
2981 char incorrect[512];
2982 char missing[512];
2984 strcpy(missing, tests[i].result);
2985 correct[0] = incorrect[0] = 0;
2987 handle = FindFirstFileA(tests[i].pattern, &find_data);
2988 if (handle != INVALID_HANDLE_VALUE)
2990 do {
2991 char *ptr;
2992 char quoted[16];
2994 sprintf(quoted, ", '%.10s'", find_data.cFileName);
2996 if ((ptr = strstr(missing, quoted)))
2998 int len = strlen(quoted);
2999 while ((ptr[0] = ptr[len]) != 0)
3000 ++ptr;
3001 strcat(correct, quoted);
3003 else
3004 strcat(incorrect, quoted);
3005 } while (FindNextFileA(handle, &find_data));
3006 FindClose(handle);
3009 todo_wine_if (tests[i].todo)
3010 ok(missing[0] == 0 && incorrect[0] == 0,
3011 "FindFirstFile with '%s' found correctly %s, found incorrectly %s, and missed %s\n",
3012 tests[i].pattern,
3013 correct[0] ? correct+2 : "none",
3014 incorrect[0] ? incorrect+2 : "none",
3015 missing[0] ? missing+2 : "none");
3018 for (i = 0; i < ARRAY_SIZE(files); ++i)
3019 DeleteFileA(files[i]);
3020 SetCurrentDirectoryA("..");
3021 RemoveDirectoryA("test-dir");
3024 static int test_Mapfile_createtemp(HANDLE *handle)
3026 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
3027 DeleteFileA(filename);
3028 *handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
3029 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3030 if (*handle != INVALID_HANDLE_VALUE) {
3032 return 1;
3035 return 0;
3038 static void test_MapFile(void)
3040 HANDLE handle;
3041 HANDLE hmap;
3043 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
3045 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
3046 ok( hmap != NULL, "mapping should work, I named it!\n" );
3048 ok( CloseHandle( hmap ), "can't close mapping handle\n");
3050 /* We have to close file before we try new stuff with mapping again.
3051 Else we would always succeed on XP or block descriptors on 95. */
3052 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
3053 ok( hmap != NULL, "We should still be able to map!\n" );
3054 ok( CloseHandle( hmap ), "can't close mapping handle\n");
3055 ok( CloseHandle( handle ), "can't close file handle\n");
3056 handle = NULL;
3058 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
3060 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
3061 ok( hmap == NULL, "mapped zero size file\n");
3062 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
3064 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
3065 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
3066 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
3067 if ( hmap )
3068 CloseHandle( hmap );
3070 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
3071 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
3072 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
3073 if ( hmap )
3074 CloseHandle( hmap );
3076 /* On XP you can now map again, on Win 95 you cannot. */
3078 ok( CloseHandle( handle ), "can't close file handle\n");
3079 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
3082 static void test_GetFileType(void)
3084 DWORD type, type2;
3085 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
3086 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
3087 type = GetFileType(h);
3088 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
3089 CloseHandle( h );
3090 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3091 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
3092 type = GetFileType(h);
3093 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
3094 CloseHandle( h );
3095 DeleteFileA( filename );
3096 h = GetStdHandle( STD_OUTPUT_HANDLE );
3097 ok( h != INVALID_HANDLE_VALUE, "GetStdHandle failed\n" );
3098 type = GetFileType( (HANDLE)STD_OUTPUT_HANDLE );
3099 type2 = GetFileType( h );
3100 ok(type == type2, "expected type %d for STD_OUTPUT_HANDLE got %d\n", type2, type);
3103 static int completion_count;
3105 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
3107 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
3108 ReleaseSemaphore(ovl->hEvent, 1, NULL);
3109 completion_count++;
3112 static void test_async_file_errors(void)
3114 char szFile[MAX_PATH];
3115 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
3116 HANDLE hFile;
3117 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
3118 OVERLAPPED ovl;
3119 S(U(ovl)).Offset = 0;
3120 S(U(ovl)).OffsetHigh = 0;
3121 ovl.hEvent = hSem;
3122 completion_count = 0;
3123 szFile[0] = '\0';
3124 GetWindowsDirectoryA(szFile, ARRAY_SIZE(szFile)-1-strlen("\\win.ini"));
3125 strcat(szFile, "\\win.ini");
3126 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3127 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
3128 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
3129 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
3130 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
3131 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
3132 while (TRUE)
3134 BOOL res;
3135 DWORD count;
3136 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
3138 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
3139 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
3140 if (!res)
3141 break;
3142 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
3143 break;
3144 S(U(ovl)).Offset += count;
3145 /* i/o completion routine only called if ReadFileEx returned success.
3146 * we only care about violations of this rule so undo what should have
3147 * been done */
3148 completion_count--;
3150 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
3151 /*printf("Error = %ld\n", GetLastError());*/
3152 HeapFree(GetProcessHeap(), 0, lpBuffer);
3155 static BOOL user_apc_ran;
3156 static void CALLBACK user_apc(ULONG_PTR param)
3158 user_apc_ran = TRUE;
3161 static void test_read_write(void)
3163 DWORD bytes, ret, old_prot;
3164 HANDLE hFile;
3165 char temp_path[MAX_PATH];
3166 char filename[MAX_PATH];
3167 char *mem;
3168 static const char prefix[] = "pfx";
3170 ret = GetTempPathA(MAX_PATH, temp_path);
3171 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
3172 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3174 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
3175 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
3177 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
3178 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
3179 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
3181 user_apc_ran = FALSE;
3182 ret = QueueUserAPC(&user_apc, GetCurrentThread(), 0);
3183 ok(ret, "QueueUserAPC failed: %d\n", GetLastError());
3185 SetLastError(12345678);
3186 bytes = 12345678;
3187 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
3188 ok(ret && GetLastError() == 12345678,
3189 "ret = %d, error %d\n", ret, GetLastError());
3190 ok(!bytes, "bytes = %d\n", bytes);
3192 SetLastError(12345678);
3193 bytes = 12345678;
3194 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
3195 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
3196 (ret && GetLastError() == 12345678), /* Win9x */
3197 "ret = %d, error %d\n", ret, GetLastError());
3198 ok(!bytes || /* Win2k */
3199 bytes == 10, /* Win9x */
3200 "bytes = %d\n", bytes);
3202 /* make sure the file contains data */
3203 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
3204 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
3206 SetLastError(12345678);
3207 bytes = 12345678;
3208 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
3209 ok(ret && GetLastError() == 12345678,
3210 "ret = %d, error %d\n", ret, GetLastError());
3211 ok(!bytes, "bytes = %d\n", bytes);
3213 SetLastError(12345678);
3214 bytes = 12345678;
3215 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
3216 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
3217 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
3218 "ret = %d, error %d\n", ret, GetLastError());
3219 ok(!bytes, "bytes = %d\n", bytes);
3221 ok(user_apc_ran == FALSE, "UserAPC ran, file using alertable io mode\n");
3222 SleepEx(0, TRUE); /* get rid of apc */
3224 /* test passing protected memory as buffer */
3226 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
3227 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
3229 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3230 ok( ret, "WriteFile failed error %u\n", GetLastError() );
3231 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
3233 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
3234 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3236 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3237 ok( !ret, "WriteFile succeeded\n" );
3238 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3239 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3240 "wrong error %u\n", GetLastError() );
3241 ok( bytes == 0, "wrote %x bytes\n", bytes );
3243 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
3244 ok( !ret, "WriteFile succeeded\n" );
3245 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
3246 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3247 "wrong error %u\n", GetLastError() );
3248 ok( bytes == 0, "wrote %x bytes\n", bytes );
3250 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
3251 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3253 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3254 ok( !ret, "WriteFile succeeded\n" );
3255 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3256 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3257 "wrong error %u\n", GetLastError() );
3258 ok( bytes == 0, "wrote %x bytes\n", bytes );
3260 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3262 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3263 ok( !ret, "ReadFile succeeded\n" );
3264 ok( GetLastError() == ERROR_NOACCESS ||
3265 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3266 "wrong error %u\n", GetLastError() );
3267 ok( bytes == 0, "read %x bytes\n", bytes );
3269 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
3270 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3272 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3273 ok( !ret, "ReadFile succeeded\n" );
3274 ok( GetLastError() == ERROR_NOACCESS ||
3275 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3276 "wrong error %u\n", GetLastError() );
3277 ok( bytes == 0, "read %x bytes\n", bytes );
3279 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
3280 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3282 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3283 ok( !ret, "ReadFile succeeded\n" );
3284 ok( GetLastError() == ERROR_NOACCESS ||
3285 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3286 "wrong error %u\n", GetLastError() );
3287 ok( bytes == 0, "read %x bytes\n", bytes );
3289 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
3290 SetEndOfFile( hFile );
3291 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3293 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3294 ok( !ret, "ReadFile succeeded\n" );
3295 ok( GetLastError() == ERROR_NOACCESS ||
3296 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3297 "wrong error %u\n", GetLastError() );
3298 ok( bytes == 0, "read %x bytes\n", bytes );
3300 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
3301 ok( ret, "ReadFile failed error %u\n", GetLastError() );
3302 ok( bytes == 0x1234, "read %x bytes\n", bytes );
3304 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
3305 ok( !ret, "ReadFile succeeded\n" );
3306 ok( GetLastError() == ERROR_NOACCESS ||
3307 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3308 "wrong error %u\n", GetLastError() );
3309 ok( bytes == 0, "read %x bytes\n", bytes );
3311 VirtualFree( mem, 0, MEM_RELEASE );
3313 ret = CloseHandle(hFile);
3314 ok( ret, "CloseHandle: error %d\n", GetLastError());
3315 ret = DeleteFileA(filename);
3316 ok( ret, "DeleteFileA: error %d\n", GetLastError());
3319 static void test_OpenFile(void)
3321 HFILE hFile;
3322 OFSTRUCT ofs;
3323 BOOL ret;
3324 DWORD retval;
3326 static const char file[] = "regedit.exe";
3327 static const char foo[] = ".\\foo-bar-foo.baz";
3328 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
3329 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3330 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3331 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3332 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3333 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
3334 char buff[MAX_PATH];
3335 char buff_long[4*MAX_PATH];
3336 char filled_0xA5[OFS_MAXPATHNAME];
3337 char *p;
3338 UINT length;
3340 /* Check for existing file */
3341 if (!pGetSystemWindowsDirectoryA)
3342 length = GetWindowsDirectoryA(buff, MAX_PATH);
3343 else
3344 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
3346 if (length + sizeof(file) < MAX_PATH)
3348 p = buff + strlen(buff);
3349 if (p > buff && p[-1] != '\\') *p++ = '\\';
3350 strcpy( p, file );
3351 memset(&ofs, 0xA5, sizeof(ofs));
3352 SetLastError(0xfaceabee);
3354 hFile = OpenFile(buff, &ofs, OF_EXIST);
3355 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
3356 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3357 "GetLastError() returns %d\n", GetLastError() );
3358 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3359 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3360 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3361 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3362 ofs.szPathName, buff );
3365 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
3366 length = GetCurrentDirectoryA(MAX_PATH, buff);
3368 /* Check for nonexistent file */
3369 if (length + sizeof(foo) < MAX_PATH)
3371 p = buff + strlen(buff);
3372 if (p > buff && p[-1] != '\\') *p++ = '\\';
3373 strcpy( p, foo + 2 );
3374 memset(&ofs, 0xA5, sizeof(ofs));
3375 SetLastError(0xfaceabee);
3377 hFile = OpenFile(foo, &ofs, OF_EXIST);
3378 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3379 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
3380 todo_wine
3381 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3382 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3383 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3384 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3385 ofs.szPathName, buff );
3388 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
3389 length += lstrlenA(foo_too_long + 1);
3391 /* Check for nonexistent file with too long filename */
3392 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
3394 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
3395 memset(&ofs, 0xA5, sizeof(ofs));
3396 SetLastError(0xfaceabee);
3398 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
3399 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3400 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
3401 "GetLastError() returns %d\n", GetLastError() );
3402 todo_wine
3403 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3404 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
3405 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3406 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3407 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
3408 ofs.szPathName );
3411 memset(&ofs, 0xA5, sizeof(ofs));
3412 SetLastError(0xfaceabee);
3413 /* Create an empty file */
3414 hFile = OpenFile(filename, &ofs, OF_CREATE);
3415 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
3416 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3417 "GetLastError() returns %d\n", GetLastError() );
3418 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3419 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3420 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3421 ret = _lclose(hFile);
3422 ok( !ret, "_lclose() returns %d\n", ret );
3423 retval = GetFileAttributesA(filename);
3424 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
3426 memset(&ofs, 0xA5, sizeof(ofs));
3427 SetLastError(0xfaceabee);
3428 /* Check various opening options: */
3429 /* for reading only, */
3430 hFile = OpenFile(filename, &ofs, OF_READ);
3431 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
3432 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3433 "GetLastError() returns %d\n", GetLastError() );
3434 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3435 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3436 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3437 ok( lstrcmpiA(ofs.szPathName, filename) == 0,
3438 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, filename );
3439 ret = _lclose(hFile);
3440 ok( !ret, "_lclose() returns %d\n", ret );
3442 memset(&ofs, 0xA5, sizeof(ofs));
3443 SetLastError(0xfaceabee);
3444 /* for writing only, */
3445 hFile = OpenFile(filename, &ofs, OF_WRITE);
3446 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
3447 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3448 "GetLastError() returns %d\n", GetLastError() );
3449 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3450 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3451 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3452 ok( lstrcmpiA(ofs.szPathName, filename) == 0,
3453 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, filename );
3454 ret = _lclose(hFile);
3455 ok( !ret, "_lclose() returns %d\n", ret );
3457 memset(&ofs, 0xA5, sizeof(ofs));
3458 SetLastError(0xfaceabee);
3459 /* for reading and writing, */
3460 hFile = OpenFile(filename, &ofs, OF_READWRITE);
3461 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
3462 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3463 "GetLastError() returns %d\n", GetLastError() );
3464 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3465 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3466 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3467 ok( lstrcmpiA(ofs.szPathName, filename) == 0,
3468 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, filename );
3469 ret = _lclose(hFile);
3470 ok( !ret, "_lclose() returns %d\n", ret );
3472 memset(&ofs, 0xA5, sizeof(ofs));
3473 SetLastError(0xfaceabee);
3474 /* for checking file presence. */
3475 hFile = OpenFile(filename, &ofs, OF_EXIST);
3476 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
3477 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3478 "GetLastError() returns %d\n", GetLastError() );
3479 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3480 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3481 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3482 ok( lstrcmpiA(ofs.szPathName, filename) == 0,
3483 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, filename );
3485 memset(&ofs, 0xA5, sizeof(ofs));
3486 SetLastError(0xfaceabee);
3487 /* Delete the file and make sure it doesn't exist anymore */
3488 hFile = OpenFile(filename, &ofs, OF_DELETE);
3489 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
3490 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3491 "GetLastError() returns %d\n", GetLastError() );
3492 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3493 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3494 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3495 ok( lstrcmpiA(ofs.szPathName, filename) == 0,
3496 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, filename );
3498 retval = GetFileAttributesA(filename);
3499 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
3502 static void test_overlapped(void)
3504 OVERLAPPED ov;
3505 DWORD r, result;
3507 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
3508 if (0) /* tested: WinXP */
3510 GetOverlappedResult(0, NULL, &result, FALSE);
3511 GetOverlappedResult(0, &ov, NULL, FALSE);
3512 GetOverlappedResult(0, NULL, NULL, FALSE);
3515 memset( &ov, 0, sizeof ov );
3516 result = 1;
3517 r = GetOverlappedResult(0, &ov, &result, 0);
3518 if (r)
3519 ok( result == 0, "wrong result %u\n", result );
3520 else /* win9x */
3521 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3523 result = 0;
3524 ov.Internal = 0;
3525 ov.InternalHigh = 0xabcd;
3526 r = GetOverlappedResult(0, &ov, &result, 0);
3527 if (r)
3528 ok( result == 0xabcd, "wrong result %u\n", result );
3529 else /* win9x */
3530 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3532 SetLastError( 0xb00 );
3533 result = 0;
3534 ov.Internal = STATUS_INVALID_HANDLE;
3535 ov.InternalHigh = 0xabcd;
3536 r = GetOverlappedResult(0, &ov, &result, 0);
3537 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3538 ok( r == FALSE, "should return false\n");
3539 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
3541 SetLastError( 0xb00 );
3542 result = 0;
3543 ov.Internal = STATUS_PENDING;
3544 ov.InternalHigh = 0xabcd;
3545 r = GetOverlappedResult(0, &ov, &result, 0);
3546 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3547 "wrong error %u\n", GetLastError() );
3548 ok( r == FALSE, "should return false\n");
3549 ok( result == 0, "wrong result %u\n", result );
3551 SetLastError( 0xb00 );
3552 ov.hEvent = CreateEventW( NULL, 1, 1, NULL );
3553 ov.Internal = STATUS_PENDING;
3554 ov.InternalHigh = 0xabcd;
3555 r = GetOverlappedResult(0, &ov, &result, 0);
3556 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3557 "wrong error %u\n", GetLastError() );
3558 ok( r == FALSE, "should return false\n");
3560 r = GetOverlappedResult( 0, &ov, &result, TRUE );
3561 ok( r == TRUE, "should return TRUE\n" );
3562 ok( result == 0xabcd, "wrong result %u\n", result );
3563 ok( ov.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %08lx\n", ov.Internal );
3565 ResetEvent( ov.hEvent );
3567 SetLastError( 0xb00 );
3568 ov.Internal = STATUS_PENDING;
3569 ov.InternalHigh = 0;
3570 r = GetOverlappedResult(0, &ov, &result, 0);
3571 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3572 "wrong error %u\n", GetLastError() );
3573 ok( r == FALSE, "should return false\n");
3575 r = CloseHandle( ov.hEvent );
3576 ok( r == TRUE, "close handle failed\n");
3579 static void test_RemoveDirectory(void)
3581 int rc;
3582 char directory[] = "removeme";
3584 rc = CreateDirectoryA(directory, NULL);
3585 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
3587 rc = SetCurrentDirectoryA(directory);
3588 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3590 rc = RemoveDirectoryA(".");
3591 if (!rc)
3593 rc = SetCurrentDirectoryA("..");
3594 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3596 rc = RemoveDirectoryA(directory);
3597 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
3601 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
3603 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
3604 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
3605 return (t1 > t2 ? t1 - t2 : t2 - t1) <= tolerance;
3608 static void test_ReplaceFileA(void)
3610 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3611 HANDLE hReplacedFile, hReplacementFile, hBackupFile, mapping;
3612 static const char replacedData[] = "file-to-replace";
3613 static const char replacementData[] = "new-file";
3614 static const char backupData[] = "backup-file";
3615 FILETIME ftReplaced, ftReplacement, ftBackup;
3616 static const char prefix[] = "pfx";
3617 char temp_path[MAX_PATH];
3618 DWORD ret;
3619 BOOL retok, removeBackup = FALSE;
3620 char **argv;
3622 ret = GetTempPathA(MAX_PATH, temp_path);
3623 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
3624 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3626 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
3627 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
3629 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3630 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3632 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
3633 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
3635 /* place predictable data in the file to be replaced */
3636 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3637 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3638 "failed to open replaced file\n");
3639 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
3640 ok( retok && ret == sizeof(replacedData),
3641 "WriteFile error (replaced) %d\n", GetLastError());
3642 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
3643 "replaced file has wrong size\n");
3644 /* place predictable data in the file to be the replacement */
3645 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3646 ok(hReplacementFile != INVALID_HANDLE_VALUE,
3647 "failed to open replacement file\n");
3648 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
3649 ok( retok && ret == sizeof(replacementData),
3650 "WriteFile error (replacement) %d\n", GetLastError());
3651 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
3652 "replacement file has wrong size\n");
3653 /* place predictable data in the backup file (to be over-written) */
3654 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3655 ok(hBackupFile != INVALID_HANDLE_VALUE,
3656 "failed to open backup file\n");
3657 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
3658 ok( retok && ret == sizeof(backupData),
3659 "WriteFile error (replacement) %d\n", GetLastError());
3660 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
3661 "backup file has wrong size\n");
3662 /* change the filetime on the "replaced" file to ensure that it changes */
3663 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3664 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
3665 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
3666 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3667 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
3668 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
3669 CloseHandle(hReplacedFile);
3670 /* change the filetime on the backup to ensure that it changes */
3671 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3672 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
3673 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
3674 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3675 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
3676 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
3677 CloseHandle(hBackupFile);
3678 /* get the filetime on the replacement file to perform checks */
3679 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
3680 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
3681 CloseHandle(hReplacementFile);
3683 /* perform replacement w/ backup
3684 * TODO: flags are not implemented
3686 SetLastError(0xdeadbeef);
3687 ret = ReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3688 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
3689 /* make sure that the backup has the size of the old "replaced" file */
3690 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3691 ok(hBackupFile != INVALID_HANDLE_VALUE,
3692 "failed to open backup file\n");
3693 ret = GetFileSize(hBackupFile, NULL);
3694 ok(ret == sizeof(replacedData),
3695 "backup file has wrong size %d\n", ret);
3696 /* make sure that the "replaced" file has the size of the replacement file */
3697 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3698 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3699 "failed to open replaced file: %d\n", GetLastError());
3700 if (hReplacedFile != INVALID_HANDLE_VALUE)
3702 ret = GetFileSize(hReplacedFile, NULL);
3703 ok(ret == sizeof(replacementData),
3704 "replaced file has wrong size %d\n", ret);
3705 /* make sure that the replacement file no-longer exists */
3706 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3707 ok(hReplacementFile == INVALID_HANDLE_VALUE,
3708 "unexpected error, replacement file should not exist %d\n", GetLastError());
3709 /* make sure that the backup has the old "replaced" filetime */
3710 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3711 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3712 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
3713 CloseHandle(hBackupFile);
3714 /* make sure that the "replaced" has the old replacement filetime */
3715 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3716 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3717 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
3718 "replaced file has wrong filetime %x%08x / %x%08x\n",
3719 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
3720 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
3721 CloseHandle(hReplacedFile);
3723 else
3724 skip("couldn't open replacement file, skipping tests\n");
3726 /* re-create replacement file for pass w/o backup (blank) */
3727 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3728 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3729 /* perform replacement w/o backup
3730 * TODO: flags are not implemented
3732 SetLastError(0xdeadbeef);
3733 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3734 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3735 "ReplaceFileA: unexpected error %d\n", GetLastError());
3737 /* re-create replacement file for pass w/ backup (backup-file not existing) */
3738 DeleteFileA(replacement);
3739 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3740 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3741 ret = DeleteFileA(backup);
3742 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
3743 /* perform replacement w/ backup (no pre-existing backup)
3744 * TODO: flags are not implemented
3746 SetLastError(0xdeadbeef);
3747 ret = ReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3748 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3749 "ReplaceFileA: unexpected error %d\n", GetLastError());
3750 if (ret)
3751 removeBackup = TRUE;
3753 /* re-create replacement file for pass w/ no permissions to "replaced" */
3754 DeleteFileA(replacement);
3755 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3756 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3757 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
3758 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3759 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
3760 /* perform replacement w/ backup (no permission to "replaced")
3761 * TODO: flags are not implemented
3763 SetLastError(0xdeadbeef);
3764 ret = ReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3765 ok(ret == 0 && GetLastError() == ERROR_ACCESS_DENIED, "ReplaceFileA: unexpected error %d\n", GetLastError());
3766 /* make sure that the replacement file still exists */
3767 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3768 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
3769 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
3770 "unexpected error, replacement file should still exist %d\n", GetLastError());
3771 CloseHandle(hReplacementFile);
3772 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
3773 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3774 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
3776 /* replacement readonly */
3777 DeleteFileA(replacement);
3778 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3779 ok(ret != 0, "GetTempFileNameA error (replacement) %#x\n", GetLastError());
3780 ret = SetFileAttributesA(replacement, FILE_ATTRIBUTE_READONLY);
3781 ok(ret, "SetFileAttributesA: error setting to readonly %#x\n", GetLastError());
3782 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3783 ok(GetLastError() == ERROR_ACCESS_DENIED, "ReplaceFileA: unexpected error %#x\n", GetLastError());
3784 ret = SetFileAttributesA(replacement, FILE_ATTRIBUTE_NORMAL);
3785 ok(ret, "SetFileAttributesA: error setting to normal %#x\n", GetLastError());
3787 /* re-create replacement file for pass w/ replaced opened with
3788 * the same permissions as an exe (Replicating an exe trying to
3789 * replace itself)
3791 DeleteFileA(replacement);
3792 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3793 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3795 /* make sure that the replaced file is opened like an exe*/
3796 hReplacedFile = CreateFileA(replaced, GENERIC_READ | SYNCHRONIZE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
3797 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3798 "unexpected error, replaced file should be able to be opened %d\n", GetLastError());
3799 /*Calling ReplaceFileA on an exe should succeed*/
3800 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3801 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
3802 CloseHandle(hReplacedFile);
3804 /* replace file while replacement is opened */
3805 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3806 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3807 hReplacementFile = CreateFileA(replacement, GENERIC_READ | SYNCHRONIZE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
3808 ok(hReplacementFile != INVALID_HANDLE_VALUE, "unexpected error, replacement file should be able to be opened %d\n",
3809 GetLastError());
3810 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3811 ok(!ret, "expect failure\n");
3812 ok(GetLastError() == ERROR_SHARING_VIOLATION, "expect ERROR_SHARING_VIOLATION, got %#x.\n", GetLastError());
3813 CloseHandle(hReplacementFile);
3815 /* replacement file still exists, make pass w/o "replaced" */
3816 ret = DeleteFileA(replaced);
3817 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3818 "DeleteFileA: error (replaced) %d\n", GetLastError());
3819 /* perform replacement w/ backup (no pre-existing backup or "replaced")
3820 * TODO: flags are not implemented
3822 SetLastError(0xdeadbeef);
3823 ret = ReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3824 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3825 GetLastError() == ERROR_ACCESS_DENIED),
3826 "ReplaceFileA: unexpected error %d\n", GetLastError());
3828 /* perform replacement w/o existing "replacement" file
3829 * TODO: flags are not implemented
3831 SetLastError(0xdeadbeef);
3832 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3833 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3834 GetLastError() == ERROR_ACCESS_DENIED),
3835 "ReplaceFileA: unexpected error %d\n", GetLastError());
3836 DeleteFileA( replacement );
3839 * if the first round (w/ backup) worked then as long as there is no
3840 * failure then there is no need to check this round (w/ backup is the
3841 * more complete case)
3844 /* delete temporary files, replacement and replaced are already deleted */
3845 if (removeBackup)
3847 ret = DeleteFileA(backup);
3848 ok(ret ||
3849 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3850 "DeleteFileA: error (backup) %d\n", GetLastError());
3853 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
3854 ok(ret, "got error %u\n", GetLastError());
3855 hReplacedFile = CreateFileA(replaced, 0, 0, NULL, OPEN_EXISTING, 0, 0);
3856 ok(hReplacedFile != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
3858 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3859 ok(ret, "got error %u\n", GetLastError());
3861 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3862 ok(ret, "got error %u\n", GetLastError());
3864 CloseHandle(hReplacedFile);
3865 ret = DeleteFileA(replaced);
3866 ok(ret, "got error %u\n", GetLastError());
3868 winetest_get_mainargs(&argv);
3870 ret = CopyFileA(argv[0], replaced, FALSE);
3871 ok(ret, "got error %u\n", GetLastError());
3872 hReplacedFile = CreateFileA(replaced, GENERIC_READ,
3873 FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
3874 ok(hReplacedFile != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
3876 mapping = CreateFileMappingA(hReplacedFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL);
3877 ok(!!mapping, "got error %u\n", GetLastError());
3879 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3880 ok(ret, "got error %u\n", GetLastError());
3882 ret = ReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3883 ok(ret, "got error %u\n", GetLastError());
3885 CloseHandle(mapping);
3886 CloseHandle(hReplacedFile);
3887 ret = DeleteFileA(replaced);
3888 ok(ret, "got error %u\n", GetLastError());
3892 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
3893 * need to be as thorough.
3895 static void test_ReplaceFileW(void)
3897 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3898 static const WCHAR prefix[] = {'p','f','x',0};
3899 WCHAR temp_path[MAX_PATH];
3900 DWORD ret;
3901 BOOL removeBackup = FALSE;
3903 if (!pReplaceFileW)
3905 win_skip("ReplaceFileW() is missing\n");
3906 return;
3909 ret = GetTempPathW(MAX_PATH, temp_path);
3910 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3912 win_skip("GetTempPathW is not available\n");
3913 return;
3915 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
3916 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3918 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
3919 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
3921 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3922 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3924 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
3925 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
3927 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3928 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
3930 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3931 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3932 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3933 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3934 "ReplaceFileW: error %d\n", GetLastError());
3936 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3937 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3938 ret = DeleteFileW(backup);
3939 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
3940 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3941 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3942 "ReplaceFileW: error %d\n", GetLastError());
3944 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3945 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3946 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
3947 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3948 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3950 SetLastError(0xdeadbeef);
3951 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3952 ok(!ret, "expected failure\n");
3953 ok(GetLastError() == ERROR_ACCESS_DENIED, "got error %u\n", GetLastError());
3954 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
3955 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3956 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3957 if (ret)
3958 removeBackup = TRUE;
3960 ret = DeleteFileW(replaced);
3961 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
3962 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3963 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
3965 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3966 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3967 GetLastError() == ERROR_ACCESS_DENIED),
3968 "ReplaceFileW: unexpected error %d\n", GetLastError());
3969 DeleteFileW( replacement );
3971 if (removeBackup)
3973 ret = DeleteFileW(backup);
3974 ok(ret ||
3975 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3976 "DeleteFileW: error (backup) %d\n", GetLastError());
3980 static void test_CreateFile(void)
3982 static const struct test_data
3984 DWORD disposition, access, error, clean_up;
3985 } td[] =
3987 /* 0 */ { 0, 0, ERROR_INVALID_PARAMETER, 0 },
3988 /* 1 */ { 0, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3989 /* 2 */ { 0, GENERIC_READ|GENERIC_WRITE, ERROR_INVALID_PARAMETER, 0 },
3990 /* 3 */ { CREATE_NEW, 0, ERROR_FILE_EXISTS, 1 },
3991 /* 4 */ { CREATE_NEW, 0, 0, 1 },
3992 /* 5 */ { CREATE_NEW, GENERIC_READ, 0, 1 },
3993 /* 6 */ { CREATE_NEW, GENERIC_WRITE, 0, 1 },
3994 /* 7 */ { CREATE_NEW, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3995 /* 8 */ { CREATE_ALWAYS, 0, 0, 0 },
3996 /* 9 */ { CREATE_ALWAYS, GENERIC_READ, 0, 0 },
3997 /* 10*/ { CREATE_ALWAYS, GENERIC_WRITE, 0, 0 },
3998 /* 11*/ { CREATE_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3999 /* 12*/ { OPEN_EXISTING, 0, ERROR_FILE_NOT_FOUND, 0 },
4000 /* 13*/ { CREATE_ALWAYS, 0, 0, 0 },
4001 /* 14*/ { OPEN_EXISTING, 0, 0, 0 },
4002 /* 15*/ { OPEN_EXISTING, GENERIC_READ, 0, 0 },
4003 /* 16*/ { OPEN_EXISTING, GENERIC_WRITE, 0, 0 },
4004 /* 17*/ { OPEN_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 1 },
4005 /* 18*/ { OPEN_ALWAYS, 0, 0, 0 },
4006 /* 19*/ { OPEN_ALWAYS, GENERIC_READ, 0, 0 },
4007 /* 20*/ { OPEN_ALWAYS, GENERIC_WRITE, 0, 0 },
4008 /* 21*/ { OPEN_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 0 },
4009 /* 22*/ { TRUNCATE_EXISTING, 0, ERROR_INVALID_PARAMETER, 0 },
4010 /* 23*/ { TRUNCATE_EXISTING, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
4011 /* 24*/ { TRUNCATE_EXISTING, GENERIC_WRITE, 0, 0 },
4012 /* 25*/ { TRUNCATE_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 0 },
4013 /* 26*/ { TRUNCATE_EXISTING, FILE_WRITE_DATA, ERROR_INVALID_PARAMETER, 0 }
4015 char temp_path[MAX_PATH];
4016 char file_name[MAX_PATH];
4017 DWORD i, ret, written;
4018 HANDLE hfile;
4020 GetTempPathA(MAX_PATH, temp_path);
4021 GetTempFileNameA(temp_path, "tmp", 0, file_name);
4023 i = strlen(temp_path);
4024 if (i && temp_path[i - 1] == '\\') temp_path[i - 1] = 0;
4026 for (i = 0; i <= 5; i++)
4028 SetLastError(0xdeadbeef);
4029 hfile = CreateFileA(temp_path, GENERIC_READ, 0, NULL, i, 0, 0);
4030 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
4031 if (i == 0 || i == 5)
4033 /* FIXME: remove once Wine is fixed */
4034 todo_wine_if (i == 5)
4035 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
4037 else
4039 /* FIXME: remove once Wine is fixed */
4040 todo_wine_if (i == 1)
4041 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
4044 SetLastError(0xdeadbeef);
4045 hfile = CreateFileA(temp_path, GENERIC_WRITE, 0, NULL, i, 0, 0);
4046 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
4047 if (i == 0)
4048 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
4049 else
4051 /* FIXME: remove once Wine is fixed */
4052 todo_wine_if (i == 1)
4053 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
4057 for (i = 0; i < ARRAY_SIZE(td); i++)
4059 SetLastError(0xdeadbeef);
4060 hfile = CreateFileA(file_name, td[i].access, 0, NULL, td[i].disposition, 0, 0);
4061 if (!td[i].error)
4063 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
4064 written = 0xdeadbeef;
4065 SetLastError(0xdeadbeef);
4066 ret = WriteFile(hfile, &td[i].error, sizeof(td[i].error), &written, NULL);
4067 if (td[i].access & GENERIC_WRITE)
4068 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
4069 else
4071 ok(!ret, "%d: WriteFile should fail\n", i);
4072 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
4074 SetLastError(0xdeadbeef);
4075 ret = SetFileTime(hfile, NULL, NULL, NULL);
4076 if (td[i].access & GENERIC_WRITE) /* actually FILE_WRITE_ATTRIBUTES */
4077 ok(ret, "%d: SetFileTime error %d\n", i, GetLastError());
4078 else
4080 todo_wine
4082 ok(!ret, "%d: SetFileTime should fail\n", i);
4083 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
4086 CloseHandle(hfile);
4088 else
4090 /* FIXME: remove the condition below once Wine is fixed */
4091 if (td[i].disposition == TRUNCATE_EXISTING && !(td[i].access & GENERIC_WRITE))
4093 todo_wine
4095 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
4096 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
4098 CloseHandle(hfile);
4100 else
4102 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
4103 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
4107 if (td[i].clean_up) DeleteFileA(file_name);
4110 DeleteFileA(file_name);
4113 static void test_GetFileInformationByHandleEx(void)
4115 int i;
4116 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[1024], *strPtr;
4117 BOOL ret;
4118 DWORD ret2, written;
4119 HANDLE directory, file;
4120 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
4121 FILE_BASIC_INFO *basicInfo;
4122 FILE_STANDARD_INFO *standardInfo;
4123 FILE_NAME_INFO *nameInfo;
4124 LARGE_INTEGER prevWrite;
4125 FILE_IO_PRIORITY_HINT_INFO priohintinfo;
4126 FILE_ALLOCATION_INFO allocinfo;
4127 FILE_DISPOSITION_INFO dispinfo;
4128 FILE_END_OF_FILE_INFO eofinfo;
4129 FILE_RENAME_INFO renameinfo;
4131 struct {
4132 FILE_INFO_BY_HANDLE_CLASS handleClass;
4133 void *ptr;
4134 DWORD size;
4135 DWORD errorCode;
4136 } checks[] = {
4137 {0xdeadbeef, NULL, 0, ERROR_INVALID_PARAMETER},
4138 {FileIdBothDirectoryInfo, NULL, 0, ERROR_BAD_LENGTH},
4139 {FileIdBothDirectoryInfo, NULL, sizeof(buffer), ERROR_NOACCESS},
4140 {FileIdBothDirectoryInfo, buffer, 0, ERROR_BAD_LENGTH}};
4142 if (!pGetFileInformationByHandleEx)
4144 win_skip("GetFileInformationByHandleEx is missing.\n");
4145 return;
4148 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
4149 ok(ret2, "GetFileInformationByHandleEx: GetTempPathA failed, got error %u.\n", GetLastError());
4151 /* ensure the existence of a file in the temp folder */
4152 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4153 ok(ret2, "GetFileInformationByHandleEx: GetTempFileNameA failed, got error %u.\n", GetLastError());
4154 ret2 = GetFileAttributesA(tempFileName);
4155 ok(ret2 != INVALID_FILE_ATTRIBUTES, "GetFileInformationByHandleEx: "
4156 "GetFileAttributesA failed to find the temp file, got error %u.\n", GetLastError());
4158 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4159 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
4160 ok(directory != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp folder, "
4161 "got error %u.\n", GetLastError());
4163 for (i = 0; i < ARRAY_SIZE(checks); i += 1)
4165 SetLastError(0xdeadbeef);
4166 ret = pGetFileInformationByHandleEx(directory, checks[i].handleClass, checks[i].ptr, checks[i].size);
4167 ok(!ret && GetLastError() == checks[i].errorCode, "GetFileInformationByHandleEx: expected error %u, "
4168 "got %u.\n", checks[i].errorCode, GetLastError());
4171 while (TRUE)
4173 memset(buffer, 0xff, sizeof(buffer));
4174 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4175 if (!ret && GetLastError() == ERROR_NO_MORE_FILES)
4176 break;
4177 ok(ret, "GetFileInformationByHandleEx: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4178 if (!ret)
4179 break;
4180 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4181 while (TRUE)
4183 ok(bothDirInfo->FileAttributes != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file attributes.\n");
4184 ok(bothDirInfo->FileId.u.LowPart != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file id.\n");
4185 ok(bothDirInfo->FileNameLength != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file name length.\n");
4186 if (!bothDirInfo->NextEntryOffset)
4187 break;
4188 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
4192 CloseHandle(directory);
4194 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4195 NULL, OPEN_EXISTING, 0, NULL);
4196 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
4197 "got error %u.\n", GetLastError());
4199 /* Test FileBasicInfo; make sure the write time changes when a file is updated */
4200 memset(buffer, 0xff, sizeof(buffer));
4201 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
4202 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
4203 basicInfo = (FILE_BASIC_INFO *)buffer;
4204 prevWrite = basicInfo->LastWriteTime;
4205 CloseHandle(file);
4207 Sleep(30); /* Make sure a new write time is different from the previous */
4209 /* Write something to the file, to make sure the write time has changed */
4210 file = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4211 NULL, OPEN_EXISTING, 0, NULL);
4212 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
4213 "got error %u.\n", GetLastError());
4214 ret = WriteFile(file, tempFileName, strlen(tempFileName), &written, NULL);
4215 ok(ret, "GetFileInformationByHandleEx: Write failed\n");
4216 CloseHandle(file);
4218 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4219 NULL, OPEN_EXISTING, 0, NULL);
4220 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
4221 "got error %u.\n", GetLastError());
4223 memset(buffer, 0xff, sizeof(buffer));
4224 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
4225 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
4226 basicInfo = (FILE_BASIC_INFO *)buffer;
4227 /* Could also check that the creation time didn't change - on windows
4228 * it doesn't, but on wine, it does change even if it shouldn't. */
4229 ok(basicInfo->LastWriteTime.QuadPart != prevWrite.QuadPart,
4230 "GetFileInformationByHandleEx: last write time didn't change\n");
4232 /* Test FileStandardInfo, check some basic parameters */
4233 memset(buffer, 0xff, sizeof(buffer));
4234 ret = pGetFileInformationByHandleEx(file, FileStandardInfo, buffer, sizeof(buffer));
4235 ok(ret, "GetFileInformationByHandleEx: failed to get FileStandardInfo, %u\n", GetLastError());
4236 standardInfo = (FILE_STANDARD_INFO *)buffer;
4237 ok(standardInfo->NumberOfLinks == 1, "GetFileInformationByHandleEx: Unexpected number of links\n");
4238 ok(standardInfo->DeletePending == FALSE, "GetFileInformationByHandleEx: Unexpected pending delete\n");
4239 ok(standardInfo->Directory == FALSE, "GetFileInformationByHandleEx: Incorrect directory flag\n");
4241 /* Test FileNameInfo */
4242 memset(buffer, 0xff, sizeof(buffer));
4243 ret = pGetFileInformationByHandleEx(file, FileNameInfo, buffer, sizeof(buffer));
4244 ok(ret, "GetFileInformationByHandleEx: failed to get FileNameInfo, %u\n", GetLastError());
4245 nameInfo = (FILE_NAME_INFO *)buffer;
4246 strPtr = strchr(tempFileName, '\\');
4247 ok(strPtr != NULL, "GetFileInformationByHandleEx: Temp filename didn't contain backslash\n");
4248 ok(nameInfo->FileNameLength == strlen(strPtr) * 2,
4249 "GetFileInformationByHandleEx: Incorrect file name length\n");
4250 for (i = 0; i < nameInfo->FileNameLength/2; i++)
4251 ok(strPtr[i] == nameInfo->FileName[i], "Incorrect filename char %d: %c vs %c\n",
4252 i, strPtr[i], nameInfo->FileName[i]);
4254 /* invalid classes */
4255 SetLastError(0xdeadbeef);
4256 ret = pGetFileInformationByHandleEx(file, FileEndOfFileInfo, &eofinfo, sizeof(eofinfo));
4257 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4259 SetLastError(0xdeadbeef);
4260 ret = pGetFileInformationByHandleEx(file, FileIoPriorityHintInfo, &priohintinfo, sizeof(priohintinfo));
4261 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4263 SetLastError(0xdeadbeef);
4264 ret = pGetFileInformationByHandleEx(file, FileAllocationInfo, &allocinfo, sizeof(allocinfo));
4265 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4267 SetLastError(0xdeadbeef);
4268 ret = pGetFileInformationByHandleEx(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
4269 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4271 SetLastError(0xdeadbeef);
4272 ret = pGetFileInformationByHandleEx(file, FileRenameInfo, &renameinfo, sizeof(renameinfo));
4273 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4275 CloseHandle(file);
4276 DeleteFileA(tempFileName);
4279 static void test_OpenFileById(void)
4281 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[256], tickCount[256];
4282 WCHAR tempFileNameW[MAX_PATH];
4283 BOOL ret, found;
4284 DWORD ret2, count, tempFileNameLen;
4285 HANDLE directory, handle, tempFile;
4286 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
4287 FILE_ID_DESCRIPTOR fileIdDescr;
4289 if (!pGetFileInformationByHandleEx || !pOpenFileById)
4291 win_skip("GetFileInformationByHandleEx or OpenFileById is missing.\n");
4292 return;
4295 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
4296 ok(ret2, "OpenFileById: GetTempPath failed, got error %u.\n", GetLastError());
4298 /* ensure the existence of a file in the temp folder */
4299 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4300 ok(ret2, "OpenFileById: GetTempFileNameA failed, got error %u.\n", GetLastError());
4301 ret2 = GetFileAttributesA(tempFileName);
4302 ok(ret2 != INVALID_FILE_ATTRIBUTES,
4303 "OpenFileById: GetFileAttributesA failed to find the temp file, got error %u\n", GetLastError());
4305 ret2 = MultiByteToWideChar(CP_ACP, 0, tempFileName + strlen(tempPath), -1, tempFileNameW, ARRAY_SIZE(tempFileNameW));
4306 ok(ret2, "OpenFileById: MultiByteToWideChar failed to convert tempFileName, got error %u.\n", GetLastError());
4307 tempFileNameLen = ret2 - 1;
4309 tempFile = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4310 ok(tempFile != INVALID_HANDLE_VALUE, "OpenFileById: failed to create a temp file, "
4311 "got error %u.\n", GetLastError());
4312 ret2 = sprintf(tickCount, "%u", GetTickCount());
4313 ret = WriteFile(tempFile, tickCount, ret2, &count, NULL);
4314 ok(ret, "OpenFileById: WriteFile failed, got error %u.\n", GetLastError());
4315 CloseHandle(tempFile);
4317 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4318 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
4319 ok(directory != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder, "
4320 "got error %u.\n", GetLastError());
4322 /* get info about the temp folder itself */
4323 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4324 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4325 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4326 ok(bothDirInfo->FileNameLength == sizeof(WCHAR) && bothDirInfo->FileName[0] == '.',
4327 "OpenFileById: failed to return the temp folder at the first entry, got error %u.\n", GetLastError());
4329 /* open the temp folder itself */
4330 fileIdDescr.dwSize = sizeof(fileIdDescr);
4331 fileIdDescr.Type = FileIdType;
4332 U(fileIdDescr).FileId = bothDirInfo->FileId;
4333 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4334 todo_wine
4335 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder itself, got error %u.\n", GetLastError());
4336 CloseHandle(handle);
4338 /* find the temp file in the temp folder */
4339 found = FALSE;
4340 while (!found)
4342 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4343 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4344 if (!ret)
4345 break;
4346 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4347 while (TRUE)
4349 if (tempFileNameLen == bothDirInfo->FileNameLength / sizeof(WCHAR) &&
4350 memcmp(tempFileNameW, bothDirInfo->FileName, bothDirInfo->FileNameLength) == 0)
4352 found = TRUE;
4353 break;
4355 if (!bothDirInfo->NextEntryOffset)
4356 break;
4357 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
4360 ok(found, "OpenFileById: failed to find the temp file in the temp folder.\n");
4362 SetLastError(0xdeadbeef);
4363 handle = pOpenFileById(directory, NULL, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4364 ok(handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER,
4365 "OpenFileById: expected ERROR_INVALID_PARAMETER, got error %u.\n", GetLastError());
4367 fileIdDescr.dwSize = sizeof(fileIdDescr);
4368 fileIdDescr.Type = FileIdType;
4369 U(fileIdDescr).FileId = bothDirInfo->FileId;
4370 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4371 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the file, got error %u.\n", GetLastError());
4373 ret = ReadFile(handle, buffer, sizeof(buffer), &count, NULL);
4374 buffer[count] = 0;
4375 ok(ret, "OpenFileById: ReadFile failed, got error %u.\n", GetLastError());
4376 ok(strcmp(tickCount, buffer) == 0, "OpenFileById: invalid contents of the temp file.\n");
4378 CloseHandle(handle);
4379 CloseHandle(directory);
4380 DeleteFileA(tempFileName);
4383 static void test_SetFileValidData(void)
4385 BOOL ret;
4386 HANDLE handle;
4387 DWORD error, count;
4388 char path[MAX_PATH], filename[MAX_PATH];
4389 TOKEN_PRIVILEGES privs;
4390 HANDLE token = NULL;
4392 if (!pSetFileValidData)
4394 win_skip("SetFileValidData is missing\n");
4395 return;
4397 GetTempPathA(sizeof(path), path);
4398 GetTempFileNameA(path, "tst", 0, filename);
4399 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4400 WriteFile(handle, "test", sizeof("test") - 1, &count, NULL);
4401 CloseHandle(handle);
4403 SetLastError(0xdeadbeef);
4404 ret = pSetFileValidData(INVALID_HANDLE_VALUE, 0);
4405 error = GetLastError();
4406 ok(!ret, "SetFileValidData succeeded\n");
4407 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4409 SetLastError(0xdeadbeef);
4410 ret = pSetFileValidData(INVALID_HANDLE_VALUE, -1);
4411 error = GetLastError();
4412 ok(!ret, "SetFileValidData succeeded\n");
4413 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4415 /* file opened for reading */
4416 handle = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
4418 SetLastError(0xdeadbeef);
4419 ret = pSetFileValidData(handle, 0);
4420 ok(!ret, "SetFileValidData succeeded\n");
4421 error = GetLastError();
4422 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4424 SetLastError(0xdeadbeef);
4425 ret = pSetFileValidData(handle, -1);
4426 error = GetLastError();
4427 ok(!ret, "SetFileValidData succeeded\n");
4428 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4429 CloseHandle(handle);
4431 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4433 SetLastError(0xdeadbeef);
4434 ret = pSetFileValidData(handle, 0);
4435 error = GetLastError();
4436 ok(!ret, "SetFileValidData succeeded\n");
4437 todo_wine ok(error == ERROR_PRIVILEGE_NOT_HELD, "got %u\n", error);
4438 CloseHandle(handle);
4440 privs.PrivilegeCount = 1;
4441 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
4443 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token) ||
4444 !LookupPrivilegeValueA(NULL, SE_MANAGE_VOLUME_NAME, &privs.Privileges[0].Luid) ||
4445 !AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL) ||
4446 GetLastError() == ERROR_NOT_ALL_ASSIGNED)
4448 win_skip("cannot enable SE_MANAGE_VOLUME_NAME privilege\n");
4449 CloseHandle(token);
4450 DeleteFileA(filename);
4451 return;
4453 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4455 SetLastError(0xdeadbeef);
4456 ret = pSetFileValidData(handle, 0);
4457 error = GetLastError();
4458 ok(!ret, "SetFileValidData succeeded\n");
4459 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4461 SetLastError(0xdeadbeef);
4462 ret = pSetFileValidData(handle, -1);
4463 error = GetLastError();
4464 ok(!ret, "SetFileValidData succeeded\n");
4465 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4467 SetLastError(0xdeadbeef);
4468 ret = pSetFileValidData(handle, 2);
4469 error = GetLastError();
4470 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4471 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4473 ret = pSetFileValidData(handle, 4);
4474 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4476 SetLastError(0xdeadbeef);
4477 ret = pSetFileValidData(handle, 8);
4478 error = GetLastError();
4479 ok(!ret, "SetFileValidData succeeded\n");
4480 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4482 count = SetFilePointer(handle, 1024, NULL, FILE_END);
4483 ok(count != INVALID_SET_FILE_POINTER, "SetFilePointer failed %u\n", GetLastError());
4484 ret = SetEndOfFile(handle);
4485 ok(ret, "SetEndOfFile failed %u\n", GetLastError());
4487 SetLastError(0xdeadbeef);
4488 ret = pSetFileValidData(handle, 2);
4489 error = GetLastError();
4490 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4491 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4493 ret = pSetFileValidData(handle, 4);
4494 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4496 ret = pSetFileValidData(handle, 8);
4497 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4499 ret = pSetFileValidData(handle, 4);
4500 error = GetLastError();
4501 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4502 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4504 ret = pSetFileValidData(handle, 1024);
4505 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4507 ret = pSetFileValidData(handle, 2048);
4508 error = GetLastError();
4509 ok(!ret, "SetFileValidData succeeded\n");
4510 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4512 privs.Privileges[0].Attributes = 0;
4513 AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL);
4515 CloseHandle(token);
4516 CloseHandle(handle);
4517 DeleteFileA(filename);
4520 static void test_ReOpenFile(void)
4522 char path[MAX_PATH], filename[MAX_PATH], buffer[4];
4523 HANDLE file, new;
4524 unsigned int i;
4525 DWORD size;
4526 BOOL ret;
4528 static const DWORD invalid_attributes[] =
4530 FILE_ATTRIBUTE_ARCHIVE,
4531 FILE_ATTRIBUTE_ENCRYPTED,
4532 FILE_ATTRIBUTE_HIDDEN,
4533 FILE_ATTRIBUTE_NORMAL,
4534 FILE_ATTRIBUTE_OFFLINE,
4535 FILE_ATTRIBUTE_READONLY,
4536 FILE_ATTRIBUTE_SYSTEM,
4537 FILE_ATTRIBUTE_TEMPORARY,
4540 static const DWORD valid_attributes[] =
4542 FILE_FLAG_BACKUP_SEMANTICS,
4543 FILE_FLAG_NO_BUFFERING,
4544 FILE_FLAG_OVERLAPPED,
4545 FILE_FLAG_RANDOM_ACCESS,
4546 FILE_FLAG_SEQUENTIAL_SCAN,
4547 FILE_FLAG_WRITE_THROUGH,
4550 if (!pReOpenFile)
4552 win_skip("ReOpenFile() is not available\n");
4553 return;
4556 GetTempPathA(sizeof(path), path);
4557 GetTempFileNameA(path, "tst", 0, filename);
4559 file = CreateFileA(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
4560 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4561 ok(file != INVALID_HANDLE_VALUE, "failed to create file, error %u\n", GetLastError());
4562 ret = WriteFile(file, "foo", 4, &size, NULL);
4563 ok(ret, "failed to write file, error %u\n", GetLastError());
4565 for (i = 0; i < ARRAY_SIZE(invalid_attributes); ++i)
4567 SetLastError(0xdeadbeef);
4568 new = pReOpenFile(file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, invalid_attributes[i]);
4569 ok(new == INVALID_HANDLE_VALUE, "got %p\n", new);
4570 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %u\n", GetLastError());
4573 new = pReOpenFile(file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0);
4574 ok(new != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
4576 ret = ReadFile(new, buffer, sizeof(buffer), &size, NULL);
4577 ok(ret, "failed to read file, error %u\n", GetLastError());
4578 ok(size == 4, "got size %u\n", size);
4579 ok(!strcmp(buffer, "foo"), "got wrong data\n");
4580 CloseHandle(new);
4582 for (i = 0; i < ARRAY_SIZE(valid_attributes); ++i)
4584 new = pReOpenFile(file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, valid_attributes[i]);
4585 ok(new != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
4586 CloseHandle(new);
4589 SetLastError(0xdeadbeef);
4590 new = pReOpenFile(file, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0);
4591 ok(new == INVALID_HANDLE_VALUE, "got %p\n", new);
4592 ok(GetLastError() == ERROR_SHARING_VIOLATION, "got error %u\n", GetLastError());
4594 CloseHandle(file);
4595 ret = DeleteFileA(filename);
4596 ok(ret, "failed to delete file, error %u\n", GetLastError());
4598 file = CreateNamedPipeA("\\\\.\\pipe\\test_pipe", PIPE_ACCESS_DUPLEX, 0, 1, 1000, 1000, 1000, NULL);
4599 ok(file != INVALID_HANDLE_VALUE, "failed to create pipe, error %u\n", GetLastError());
4601 new = pReOpenFile(file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0);
4602 ok(new != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
4604 ret = WriteFile(file, "foo", 4, &size, NULL);
4605 ok(ret, "failed to write file, error %u\n", GetLastError());
4606 ret = ReadFile(new, buffer, sizeof(buffer), &size, NULL);
4607 ok(ret, "failed to read file, error %u\n", GetLastError());
4608 ok(size == 4, "got size %u\n", size);
4609 ok(!strcmp(buffer, "foo"), "got wrong data\n");
4611 CloseHandle(new);
4612 CloseHandle(file);
4615 static void test_WriteFileGather(void)
4617 char temp_path[MAX_PATH], filename[MAX_PATH];
4618 HANDLE hfile, hiocp1, hiocp2, evt;
4619 DWORD ret, size, tx;
4620 ULONG_PTR key;
4621 FILE_SEGMENT_ELEMENT fse[2];
4622 OVERLAPPED ovl, *povl = NULL;
4623 SYSTEM_INFO si;
4624 char *wbuf = NULL, *rbuf1, *rbuf2;
4625 BOOL br;
4627 evt = CreateEventW( NULL, TRUE, FALSE, NULL );
4629 ret = GetTempPathA( MAX_PATH, temp_path );
4630 ok( ret != 0, "GetTempPathA error %d\n", GetLastError() );
4631 ok( ret < MAX_PATH, "temp path should fit into MAX_PATH\n" );
4632 ret = GetTempFileNameA( temp_path, "wfg", 0, filename );
4633 ok( ret != 0, "GetTempFileNameA error %d\n", GetLastError() );
4635 hfile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
4636 FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL, 0 );
4637 ok( hfile != INVALID_HANDLE_VALUE, "CreateFile failed err %u\n", GetLastError() );
4638 if (hfile == INVALID_HANDLE_VALUE) return;
4640 hiocp1 = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 999, 0 );
4641 hiocp2 = CreateIoCompletionPort( hfile, hiocp1, 999, 0 );
4642 ok( hiocp2 != 0, "CreateIoCompletionPort failed err %u\n", GetLastError() );
4644 GetSystemInfo( &si );
4645 wbuf = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4646 ok( wbuf != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4648 rbuf1 = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4649 ok( rbuf1 != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4651 rbuf2 = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4652 ok( rbuf2 != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4654 memset( &ovl, 0, sizeof(ovl) );
4655 ovl.hEvent = evt;
4656 memset( fse, 0, sizeof(fse) );
4657 fse[0].Buffer = wbuf;
4658 memset( wbuf, 0x42, si.dwPageSize );
4659 SetLastError( 0xdeadbeef );
4660 if (!WriteFileGather( hfile, fse, si.dwPageSize, NULL, &ovl ))
4661 ok( GetLastError() == ERROR_IO_PENDING, "WriteFileGather failed err %u\n", GetLastError() );
4663 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4664 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4665 ok( povl == &ovl, "wrong ovl %p\n", povl );
4667 tx = 0;
4668 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4669 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4670 ok( tx == si.dwPageSize, "got unexpected bytes transferred: %u\n", tx );
4672 ResetEvent( evt );
4674 /* read exact size */
4675 memset( &ovl, 0, sizeof(ovl) );
4676 ovl.hEvent = evt;
4677 memset( fse, 0, sizeof(fse) );
4678 fse[0].Buffer = rbuf1;
4679 memset( rbuf1, 0, si.dwPageSize );
4680 SetLastError( 0xdeadbeef );
4681 br = ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl );
4682 ok( br == FALSE, "ReadFileScatter should be asynchronous\n" );
4683 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4685 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4686 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4687 ok( povl == &ovl, "wrong ovl %p\n", povl );
4689 tx = 0;
4690 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4691 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4692 ok( tx == si.dwPageSize, "got unexpected bytes transferred: %u\n", tx );
4694 ok( memcmp( rbuf1, wbuf, si.dwPageSize ) == 0,
4695 "data was not read into buffer\n" );
4697 ResetEvent( evt );
4699 /* start read at EOF */
4700 memset( &ovl, 0, sizeof(ovl) );
4701 ovl.hEvent = evt;
4702 S(U(ovl)).OffsetHigh = 0;
4703 S(U(ovl)).Offset = si.dwPageSize;
4704 memset( fse, 0, sizeof(fse) );
4705 fse[0].Buffer = rbuf1;
4706 SetLastError( 0xdeadbeef );
4707 br = ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl );
4708 ok( br == FALSE, "ReadFileScatter should have failed\n" );
4709 ok( GetLastError() == ERROR_HANDLE_EOF ||
4710 GetLastError() == ERROR_IO_PENDING, "ReadFileScatter gave wrong error %u\n", GetLastError() );
4711 if (GetLastError() == ERROR_IO_PENDING)
4713 SetLastError( 0xdeadbeef );
4714 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4715 ok( !ret, "GetQueuedCompletionStatus should have returned failure\n" );
4716 ok( GetLastError() == ERROR_HANDLE_EOF, "Got wrong error: %u\n", GetLastError() );
4717 ok( povl == &ovl, "wrong ovl %p\n", povl );
4719 SetLastError( 0xdeadbeef );
4720 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4721 ok( br == FALSE, "GetOverlappedResult should have failed\n" );
4722 ok( GetLastError() == ERROR_HANDLE_EOF, "Got wrong error: %u\n", GetLastError() );
4724 else
4726 SetLastError( 0xdeadbeef );
4727 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 100 );
4728 ok( !ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError() );
4729 ok( GetLastError() == WAIT_TIMEOUT, "GetQueuedCompletionStatus gave wrong error %u\n", GetLastError() );
4730 ok( povl == NULL, "wrong ovl %p\n", povl );
4733 ResetEvent( evt );
4735 /* read past EOF */
4736 memset( &ovl, 0, sizeof(ovl) );
4737 ovl.hEvent = evt;
4738 memset( fse, 0, sizeof(fse) );
4739 fse[0].Buffer = rbuf1;
4740 fse[1].Buffer = rbuf2;
4741 memset( rbuf1, 0, si.dwPageSize );
4742 memset( rbuf2, 0x17, si.dwPageSize );
4743 SetLastError( 0xdeadbeef );
4744 br = ReadFileScatter( hfile, fse, si.dwPageSize * 2, NULL, &ovl );
4745 ok( br == FALSE, "ReadFileScatter should be asynchronous\n" );
4746 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4748 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4749 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError() );
4750 ok( povl == &ovl, "wrong ovl %p\n", povl );
4752 tx = 0;
4753 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4754 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4755 ok( tx == si.dwPageSize, "got unexpected bytes transferred: %u\n", tx );
4757 ok( memcmp( rbuf1, wbuf, si.dwPageSize ) == 0,
4758 "data was not read into buffer\n" );
4759 memset( rbuf1, 0x17, si.dwPageSize );
4760 ok( memcmp( rbuf2, rbuf1, si.dwPageSize ) == 0,
4761 "data should not have been read into buffer\n" );
4763 ResetEvent( evt );
4765 /* partial page read */
4766 memset( &ovl, 0, sizeof(ovl) );
4767 ovl.hEvent = evt;
4768 memset( fse, 0, sizeof(fse) );
4769 fse[0].Buffer = rbuf1;
4770 memset( rbuf1, 0, si.dwPageSize );
4771 SetLastError( 0xdeadbeef );
4772 br = ReadFileScatter( hfile, fse, si.dwPageSize / 2, NULL, &ovl );
4773 ok( br == FALSE, "ReadFileScatter should be asynchronous\n" );
4774 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4776 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4777 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError() );
4778 ok( povl == &ovl, "wrong ovl %p\n", povl );
4780 tx = 0;
4781 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4782 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4783 ok( tx == si.dwPageSize / 2, "got unexpected bytes transferred: %u\n", tx );
4785 ok( memcmp( rbuf1, wbuf, si.dwPageSize / 2 ) == 0,
4786 "invalid data was read into buffer\n" );
4787 memset( rbuf2, 0, si.dwPageSize );
4788 ok( memcmp( rbuf1 + si.dwPageSize / 2, rbuf2, si.dwPageSize - si.dwPageSize / 2 ) == 0,
4789 "invalid data was read into buffer\n" );
4791 if (pSetFileCompletionNotificationModes)
4793 br = pSetFileCompletionNotificationModes(hfile, FILE_SKIP_COMPLETION_PORT_ON_SUCCESS);
4794 ok(br, "SetFileCompletionNotificationModes failed, error %u.\n", GetLastError());
4796 br = ReadFileScatter(hfile, fse, si.dwPageSize, NULL, &ovl);
4797 ok(br == FALSE, "ReadFileScatter should be asynchronous.\n");
4798 ok(GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed, error %u.\n", GetLastError());
4800 br = GetQueuedCompletionStatus(hiocp2, &size, &key, &povl, 1000);
4801 ok(br, "GetQueuedCompletionStatus failed, err %u.\n", GetLastError());
4802 ok(povl == &ovl, "Wrong ovl %p.\n", povl);
4804 br = GetOverlappedResult(hfile, &ovl, &tx, TRUE);
4805 ok(br, "GetOverlappedResult failed, err %u.\n", GetLastError());
4806 ok(tx == si.dwPageSize, "Got unexpected size %u.\n", tx);
4808 ResetEvent(evt);
4810 else
4811 win_skip("SetFileCompletionNotificationModes not available.\n");
4813 CloseHandle( hfile );
4814 CloseHandle( hiocp1 );
4815 CloseHandle( hiocp2 );
4817 /* file handle must be overlapped */
4818 hfile = CreateFileA( filename, GENERIC_READ, 0, 0, OPEN_EXISTING,
4819 FILE_FLAG_NO_BUFFERING | FILE_ATTRIBUTE_NORMAL, 0 );
4820 ok( hfile != INVALID_HANDLE_VALUE, "CreateFile failed err %u\n", GetLastError() );
4822 memset( &ovl, 0, sizeof(ovl) );
4823 memset( fse, 0, sizeof(fse) );
4824 fse[0].Buffer = rbuf1;
4825 memset( rbuf1, 0, si.dwPageSize );
4826 SetLastError( 0xdeadbeef );
4827 br = ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl );
4828 ok( br == FALSE, "ReadFileScatter should fail\n" );
4829 ok( GetLastError() == ERROR_INVALID_PARAMETER, "ReadFileScatter failed err %u\n", GetLastError() );
4831 VirtualFree( wbuf, 0, MEM_RELEASE );
4832 VirtualFree( rbuf1, 0, MEM_RELEASE );
4833 VirtualFree( rbuf2, 0, MEM_RELEASE );
4834 CloseHandle( evt );
4835 DeleteFileA( filename );
4838 static unsigned file_map_access(unsigned access)
4840 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
4841 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
4842 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
4843 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
4844 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
4847 static BOOL is_access_compatible(unsigned obj_access, unsigned desired_access)
4849 obj_access = file_map_access(obj_access);
4850 desired_access = file_map_access(desired_access);
4851 return (obj_access & desired_access) == desired_access;
4854 static void test_file_access(void)
4856 static const struct
4858 unsigned access, create_error, write_error, read_error;
4859 } td[] =
4861 { GENERIC_READ | GENERIC_WRITE, 0, 0, 0 },
4862 { GENERIC_WRITE, 0, 0, ERROR_ACCESS_DENIED },
4863 { GENERIC_READ, 0, ERROR_ACCESS_DENIED, 0 },
4864 { FILE_READ_DATA | FILE_WRITE_DATA, 0, 0, 0 },
4865 { FILE_WRITE_DATA, 0, 0, ERROR_ACCESS_DENIED },
4866 { FILE_READ_DATA, 0, ERROR_ACCESS_DENIED, 0 },
4867 { FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4868 { FILE_READ_DATA | FILE_APPEND_DATA, 0, 0, 0 },
4869 { FILE_WRITE_DATA | FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4870 { 0, 0, ERROR_ACCESS_DENIED, ERROR_ACCESS_DENIED },
4872 char path[MAX_PATH], fname[MAX_PATH];
4873 unsigned char buf[16];
4874 HANDLE hfile, hdup;
4875 DWORD i, j, ret, bytes;
4877 GetTempPathA(MAX_PATH, path);
4878 GetTempFileNameA(path, "foo", 0, fname);
4880 for (i = 0; i < ARRAY_SIZE(td); i++)
4882 SetLastError(0xdeadbeef);
4883 hfile = CreateFileA(fname, td[i].access, 0, NULL, CREATE_ALWAYS,
4884 FILE_FLAG_DELETE_ON_CLOSE, 0);
4885 if (td[i].create_error)
4887 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
4888 ok(td[i].create_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].create_error, GetLastError());
4889 continue;
4891 else
4892 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
4894 for (j = 0; j < ARRAY_SIZE(td); j++)
4896 SetLastError(0xdeadbeef);
4897 ret = DuplicateHandle(GetCurrentProcess(), hfile, GetCurrentProcess(), &hdup,
4898 td[j].access, 0, 0);
4899 if (is_access_compatible(td[i].access, td[j].access))
4900 ok(ret, "DuplicateHandle(%#x => %#x) error %d\n", td[i].access, td[j].access, GetLastError());
4901 else
4903 /* FIXME: Remove once Wine is fixed */
4904 todo_wine_if((td[j].access & (GENERIC_READ | GENERIC_WRITE) ||
4905 (!(td[i].access & (GENERIC_WRITE | FILE_WRITE_DATA)) && (td[j].access & FILE_WRITE_DATA)) ||
4906 (!(td[i].access & (GENERIC_READ | FILE_READ_DATA)) && (td[j].access & FILE_READ_DATA)) ||
4907 (!(td[i].access & (GENERIC_WRITE)) && (td[j].access & FILE_APPEND_DATA))))
4909 ok(!ret, "DuplicateHandle(%#x => %#x) should fail\n", td[i].access, td[j].access);
4910 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4913 if (ret) CloseHandle(hdup);
4916 SetLastError(0xdeadbeef);
4917 bytes = 0xdeadbeef;
4918 ret = WriteFile(hfile, "\x5e\xa7", 2, &bytes, NULL);
4919 if (td[i].write_error)
4921 ok(!ret, "%d: WriteFile should fail\n", i);
4922 ok(td[i].write_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].write_error, GetLastError());
4923 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4925 else
4927 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
4928 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4931 SetLastError(0xdeadbeef);
4932 ret = SetFilePointer(hfile, 0, NULL, FILE_BEGIN);
4933 ok(ret != INVALID_SET_FILE_POINTER, "SetFilePointer error %d\n", GetLastError());
4935 SetLastError(0xdeadbeef);
4936 bytes = 0xdeadbeef;
4937 ret = ReadFile(hfile, buf, sizeof(buf), &bytes, NULL);
4938 if (td[i].read_error)
4940 ok(!ret, "%d: ReadFile should fail\n", i);
4941 ok(td[i].read_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].read_error, GetLastError());
4942 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4944 else
4946 ok(ret, "%d: ReadFile error %d\n", i, GetLastError());
4947 if (td[i].write_error)
4948 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4949 else
4951 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4952 ok(buf[0] == 0x5e && buf[1] == 0xa7, "%d: expected 5ea7, got %02x%02x\n", i, buf[0], buf[1]);
4956 CloseHandle(hfile);
4960 static void test_GetFinalPathNameByHandleA(void)
4962 static char prefix[] = "GetFinalPathNameByHandleA";
4963 static char dos_prefix[] = "\\\\?\\";
4964 char temp_path[MAX_PATH], test_path[MAX_PATH];
4965 char long_path[MAX_PATH], result_path[MAX_PATH];
4966 char dos_path[MAX_PATH + sizeof(dos_prefix)];
4967 HANDLE file;
4968 DWORD count;
4969 UINT ret;
4971 if (!pGetFinalPathNameByHandleA)
4973 win_skip("GetFinalPathNameByHandleA is missing\n");
4974 return;
4977 /* Test calling with INVALID_HANDLE_VALUE */
4978 SetLastError(0xdeadbeaf);
4979 count = pGetFinalPathNameByHandleA(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4980 ok(count == 0, "Expected length 0, got %u\n", count);
4981 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4983 count = GetTempPathA(MAX_PATH, temp_path);
4984 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4985 ret = GetTempFileNameA(temp_path, prefix, 0, test_path);
4986 ok(ret != 0, "GetTempFileNameA error %u\n", GetLastError());
4987 ret = GetLongPathNameA(test_path, long_path, MAX_PATH);
4988 ok(ret != 0, "GetLongPathNameA error %u\n", GetLastError());
4989 strcpy(dos_path, dos_prefix);
4990 strcat(dos_path, long_path);
4992 count = pGetFinalPathNameByHandleA(INVALID_HANDLE_VALUE, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4993 ok(count == 0, "Expected length 0, got %u\n", count);
4994 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4996 file = CreateFileA(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4997 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4998 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error %u\n", GetLastError());
5000 if (0) {
5001 /* Windows crashes on NULL path */
5002 count = pGetFinalPathNameByHandleA(file, NULL, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5003 ok(count == 0, "Expected length 0, got %u\n", count);
5004 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
5007 /* Test 0-length path */
5008 count = pGetFinalPathNameByHandleA(file, result_path, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5009 ok(count == strlen(dos_path), "Expected length %u, got %u\n", lstrlenA(dos_path), count);
5011 /* Test 0 and NULL path */
5012 count = pGetFinalPathNameByHandleA(file, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5013 ok(count == strlen(dos_path), "Expected length %u, got %u\n", lstrlenA(dos_path), count);
5015 /* Test VOLUME_NAME_DOS with sufficient buffer size */
5016 memset(result_path, 0x11, sizeof(result_path));
5017 count = pGetFinalPathNameByHandleA(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5018 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
5019 ok(lstrcmpiA(dos_path, result_path) == 0, "Expected %s, got %s\n", dos_path, result_path);
5021 /* Test VOLUME_NAME_DOS with insufficient buffer size */
5022 memset(result_path, 0x11, sizeof(result_path));
5023 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-2, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5024 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
5025 ok(result_path[0] == 0x11, "Result path was modified\n");
5027 memset(result_path, 0x11, sizeof(result_path));
5028 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5029 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
5030 ok(result_path[0] == 0x11, "Result path was modified\n");
5032 memset(result_path, 0x11, sizeof(result_path));
5033 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5034 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
5035 ok(result_path[0] == 0x11, "Result path was modified\n");
5037 memset(result_path, 0x11, sizeof(result_path));
5038 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5039 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
5040 ok(result_path[0] != 0x11, "Result path was not modified\n");
5041 ok(!result_path[strlen(dos_path)], "Expected nullterminated string\n");
5042 ok(result_path[strlen(dos_path)+1] == 0x11, "Buffer overflow\n");
5044 CloseHandle(file);
5047 static void test_GetFinalPathNameByHandleW(void)
5049 static WCHAR prefix[] = {'G','e','t','F','i','n','a','l','P','a','t','h',
5050 'N','a','m','e','B','y','H','a','n','d','l','e','W','\0'};
5051 static WCHAR dos_prefix[] = {'\\','\\','?','\\','\0'};
5052 WCHAR temp_path[MAX_PATH], test_path[MAX_PATH];
5053 WCHAR long_path[MAX_PATH], result_path[MAX_PATH];
5054 WCHAR dos_path[MAX_PATH + sizeof(dos_prefix)];
5055 WCHAR drive_part[MAX_PATH];
5056 WCHAR *file_part;
5057 WCHAR volume_path[MAX_PATH + 50];
5058 WCHAR nt_path[2 * MAX_PATH];
5059 BOOL success;
5060 HANDLE file;
5061 DWORD count;
5062 UINT ret;
5064 if (!pGetFinalPathNameByHandleW)
5066 win_skip("GetFinalPathNameByHandleW is missing\n");
5067 return;
5070 /* Test calling with INVALID_HANDLE_VALUE */
5071 SetLastError(0xdeadbeaf);
5072 count = pGetFinalPathNameByHandleW(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5073 ok(count == 0, "Expected length 0, got %u\n", count);
5074 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
5076 count = pGetFinalPathNameByHandleW(INVALID_HANDLE_VALUE, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5077 ok(count == 0, "Expected length 0, got %u\n", count);
5078 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
5080 count = GetTempPathW(MAX_PATH, temp_path);
5081 ok(count, "Failed to get temp path, error %u\n", GetLastError());
5082 ret = GetTempFileNameW(temp_path, prefix, 0, test_path);
5083 ok(ret != 0, "GetTempFileNameW error %u\n", GetLastError());
5084 ret = GetLongPathNameW(test_path, long_path, MAX_PATH);
5085 ok(ret != 0, "GetLongPathNameW error %u\n", GetLastError());
5086 lstrcpyW(dos_path, dos_prefix);
5087 lstrcatW(dos_path, long_path);
5089 file = CreateFileW(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
5090 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
5091 ok(file != INVALID_HANDLE_VALUE, "CreateFileW error %u\n", GetLastError());
5093 if (0) {
5094 /* Windows crashes on NULL path */
5095 count = pGetFinalPathNameByHandleW(file, NULL, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5096 ok(count == 0, "Expected length 0, got %u\n", count);
5097 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
5100 /* Test 0-length path */
5101 count = pGetFinalPathNameByHandleW(file, result_path, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5102 ok(count == lstrlenW(dos_path) + 1 ||
5103 broken(count == lstrlenW(dos_path) + 2), "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
5105 /* Test 0 and NULL path */
5106 count = pGetFinalPathNameByHandleW(file, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5107 ok(count == lstrlenW(dos_path) + 1 ||
5108 broken(count == lstrlenW(dos_path) + 2), "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
5110 /* Test VOLUME_NAME_DOS with sufficient buffer size */
5111 memset(result_path, 0x11, sizeof(result_path));
5112 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5113 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
5114 ok(lstrcmpiW(dos_path, result_path) == 0, "Expected %s, got %s\n", wine_dbgstr_w(dos_path), wine_dbgstr_w(result_path));
5116 /* Test VOLUME_NAME_DOS with insufficient buffer size */
5117 memset(result_path, 0x11, sizeof(result_path));
5118 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5119 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
5120 ok(result_path[0] == 0x1111, "Result path was modified\n");
5122 memset(result_path, 0x11, sizeof(result_path));
5123 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5124 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
5125 ok(result_path[0] == 0x1111, "Result path was modified\n");
5127 memset(result_path, 0x11, sizeof(result_path));
5128 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
5129 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
5130 ok(result_path[0] != 0x1111, "Result path was not modified\n");
5131 ok(!result_path[lstrlenW(dos_path)], "Expected nullterminated string\n");
5132 ok(result_path[lstrlenW(dos_path)+1] == 0x1111, "Buffer overflow\n");
5134 success = GetVolumePathNameW(long_path, drive_part, MAX_PATH);
5135 ok(success, "GetVolumePathNameW error %u\n", GetLastError());
5136 success = GetVolumeNameForVolumeMountPointW(drive_part, volume_path, ARRAY_SIZE(volume_path));
5137 ok(success, "GetVolumeNameForVolumeMountPointW error %u\n", GetLastError());
5139 /* Test for VOLUME_NAME_GUID */
5140 lstrcatW(volume_path, long_path + lstrlenW(drive_part));
5141 memset(result_path, 0x11, sizeof(result_path));
5142 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_GUID);
5143 ok(count == lstrlenW(volume_path), "Expected length %u, got %u\n", lstrlenW(volume_path), count);
5144 ok(lstrcmpiW(volume_path, result_path) == 0, "Expected %s, got %s\n",
5145 wine_dbgstr_w(volume_path), wine_dbgstr_w(result_path));
5147 /* Test for VOLUME_NAME_NONE */
5148 file_part = long_path + lstrlenW(drive_part) - 1;
5149 memset(result_path, 0x11, sizeof(result_path));
5150 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NONE);
5151 ok(count == lstrlenW(file_part), "Expected length %u, got %u\n", lstrlenW(file_part), count);
5152 ok(lstrcmpiW(file_part, result_path) == 0, "Expected %s, got %s\n",
5153 wine_dbgstr_w(file_part), wine_dbgstr_w(result_path));
5155 drive_part[lstrlenW(drive_part)-1] = 0;
5156 success = QueryDosDeviceW(drive_part, nt_path, ARRAY_SIZE(nt_path));
5157 ok(success, "QueryDosDeviceW error %u\n", GetLastError());
5159 /* Test for VOLUME_NAME_NT */
5160 lstrcatW(nt_path, file_part);
5161 memset(result_path, 0x11, sizeof(result_path));
5162 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NT);
5163 ok(count == lstrlenW(nt_path), "Expected length %u, got %u\n", lstrlenW(nt_path), count);
5164 ok(lstrcmpiW(nt_path, result_path) == 0, "Expected %s, got %s\n",
5165 wine_dbgstr_w(nt_path), wine_dbgstr_w(result_path));
5167 CloseHandle(file);
5170 static void test_SetFileInformationByHandle(void)
5172 FILE_ATTRIBUTE_TAG_INFO fileattrinfo = { 0 };
5173 FILE_REMOTE_PROTOCOL_INFO protinfo = { 0 };
5174 FILE_STANDARD_INFO stdinfo = { {{0}},{{0}},0,FALSE,FALSE };
5175 FILE_COMPRESSION_INFO compressinfo;
5176 FILE_DISPOSITION_INFO dispinfo;
5177 DECLSPEC_ALIGN(8) FILE_IO_PRIORITY_HINT_INFO hintinfo;
5178 FILE_BASIC_INFO basicinfo = { {{0}} };
5179 char tempFileName[MAX_PATH];
5180 char tempPath[MAX_PATH];
5181 LARGE_INTEGER atime;
5182 HANDLE file;
5183 BOOL ret;
5185 if (!pSetFileInformationByHandle)
5187 win_skip("SetFileInformationByHandle is not supported\n");
5188 return;
5191 ret = GetTempPathA(sizeof(tempPath), tempPath);
5192 ok(ret, "GetTempPathA failed, got error %u.\n", GetLastError());
5194 /* ensure the existence of a file in the temp folder */
5195 ret = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
5196 ok(ret, "GetTempFileNameA failed, got error %u.\n", GetLastError());
5198 file = CreateFileA(tempFileName, GENERIC_READ | FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
5199 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
5200 ok(file != INVALID_HANDLE_VALUE, "failed to open the temp file, error %u.\n", GetLastError());
5202 /* invalid classes */
5203 SetLastError(0xdeadbeef);
5204 ret = pSetFileInformationByHandle(file, FileStandardInfo, &stdinfo, sizeof(stdinfo));
5205 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
5207 memset(&compressinfo, 0, sizeof(compressinfo));
5208 SetLastError(0xdeadbeef);
5209 ret = pSetFileInformationByHandle(file, FileCompressionInfo, &compressinfo, sizeof(compressinfo));
5210 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
5212 SetLastError(0xdeadbeef);
5213 ret = pSetFileInformationByHandle(file, FileAttributeTagInfo, &fileattrinfo, sizeof(fileattrinfo));
5214 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
5216 SetLastError(0xdeadbeef);
5217 hintinfo.PriorityHint = MaximumIoPriorityHintType;
5218 ret = pSetFileInformationByHandle(file, FileIoPriorityHintInfo, &hintinfo, sizeof(hintinfo));
5219 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
5221 hintinfo.PriorityHint = IoPriorityHintNormal;
5222 ret = pSetFileInformationByHandle(file, FileIoPriorityHintInfo, &hintinfo, sizeof(hintinfo));
5223 ok(ret, "setting FileIoPriorityHintInfo got %d, error %d\n", ret, GetLastError());
5225 hintinfo.PriorityHint = IoPriorityHintVeryLow;
5226 ret = pSetFileInformationByHandle(file, FileIoPriorityHintInfo, &hintinfo, sizeof(hintinfo));
5227 ok(ret, "setting FileIoPriorityHintInfo got %d, error %d\n", ret, GetLastError());
5229 SetLastError(0xdeadbeef);
5230 ret = pSetFileInformationByHandle(file, FileIoPriorityHintInfo, &hintinfo, sizeof(hintinfo) - 1);
5231 ok(!ret && GetLastError() == ERROR_BAD_LENGTH, "got %d, error %d\n", ret, GetLastError());
5233 SetLastError(0xdeadbeef);
5234 hintinfo.PriorityHint = IoPriorityHintVeryLow - 1;
5235 ret = pSetFileInformationByHandle(file, FileIoPriorityHintInfo, &hintinfo, sizeof(hintinfo));
5236 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
5238 memset(&protinfo, 0, sizeof(protinfo));
5239 protinfo.StructureVersion = 1;
5240 protinfo.StructureSize = sizeof(protinfo);
5241 SetLastError(0xdeadbeef);
5242 ret = pSetFileInformationByHandle(file, FileRemoteProtocolInfo, &protinfo, sizeof(protinfo));
5243 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
5245 /* test FileDispositionInfo, additional details already covered by ntdll tests */
5246 SetLastError(0xdeadbeef);
5247 ret = pSetFileInformationByHandle(file, FileDispositionInfo, &dispinfo, 0);
5248 todo_wine
5249 ok(!ret && GetLastError() == ERROR_BAD_LENGTH, "got %d, error %d\n", ret, GetLastError());
5251 SetLastError(0xdeadbeef);
5252 ret = pSetFileInformationByHandle(file, FileBasicInfo, &basicinfo, 0);
5253 todo_wine
5254 ok(!ret && GetLastError() == ERROR_BAD_LENGTH, "got %d, error %d\n", ret, GetLastError());
5256 memset(&basicinfo, 0, sizeof(basicinfo));
5257 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, &basicinfo, sizeof(basicinfo));
5258 ok(ret, "Failed to get basic info, error %d.\n", GetLastError());
5259 atime = basicinfo.LastAccessTime;
5261 basicinfo.LastAccessTime.QuadPart++;
5262 ret = pSetFileInformationByHandle(file, FileBasicInfo, &basicinfo, sizeof(basicinfo));
5263 ok(ret, "Failed to set basic info, error %d.\n", GetLastError());
5265 memset(&basicinfo, 0, sizeof(basicinfo));
5266 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, &basicinfo, sizeof(basicinfo));
5267 ok(ret, "Failed to get basic info, error %d.\n", GetLastError());
5268 ok(atime.QuadPart + 1 == basicinfo.LastAccessTime.QuadPart, "Unexpected access time.\n");
5270 memset(&basicinfo, 0, sizeof(basicinfo));
5271 basicinfo.LastAccessTime.QuadPart = -1;
5272 ret = pSetFileInformationByHandle(file, FileBasicInfo, &basicinfo, sizeof(basicinfo));
5273 ok(ret, "Failed to set basic info, error %d.\n", GetLastError());
5275 memset(&basicinfo, 0, sizeof(basicinfo));
5276 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, &basicinfo, sizeof(basicinfo));
5277 ok(ret, "Failed to get basic info, error %d.\n", GetLastError());
5278 ok(atime.QuadPart + 1 == basicinfo.LastAccessTime.QuadPart, "Unexpected access time.\n");
5280 dispinfo.DeleteFile = TRUE;
5281 ret = pSetFileInformationByHandle(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
5282 ok(ret, "setting FileDispositionInfo failed, error %d\n", GetLastError());
5284 CloseHandle(file);
5287 static void test_SetFileRenameInfo(void)
5289 WCHAR tempFileFrom[MAX_PATH], tempFileTo1[MAX_PATH], tempFileTo2[MAX_PATH];
5290 WCHAR tempPath[MAX_PATH];
5291 FILE_RENAME_INFORMATION *fri;
5292 HANDLE file;
5293 DWORD size;
5294 BOOL ret;
5296 if (!pSetFileInformationByHandle)
5298 win_skip("SetFileInformationByHandle is not supported\n");
5299 return;
5302 ret = GetTempPathW(MAX_PATH, tempPath);
5303 ok(ret, "GetTempPathW failed, got error %u.\n", GetLastError());
5305 ret = GetTempFileNameW(tempPath, L"abc", 0, tempFileFrom);
5306 ok(ret, "GetTempFileNameW failed, got error %u.\n", GetLastError());
5308 ret = GetTempFileNameW(tempPath, L"abc", 0, tempFileTo1);
5309 ok(ret, "GetTempFileNameW failed, got error %u.\n", GetLastError());
5311 ret = GetTempFileNameW(tempPath, L"abc", 1, tempFileTo2);
5312 ok(ret, "GetTempFileNameW failed, got error %u.\n", GetLastError());
5314 file = CreateFileW(tempFileFrom, GENERIC_READ | GENERIC_WRITE | DELETE, 0, 0, OPEN_EXISTING, 0, 0);
5315 ok(file != INVALID_HANDLE_VALUE, "failed to create temp file, error %u.\n", GetLastError());
5317 size = sizeof(FILE_RENAME_INFORMATION) + MAX_PATH;
5318 fri = HeapAlloc(GetProcessHeap(), 0, size);
5320 fri->ReplaceIfExists = FALSE;
5321 fri->RootDirectory = NULL;
5322 fri->FileNameLength = wcslen(tempFileTo1) * sizeof(WCHAR);
5323 memcpy(fri->FileName, tempFileTo1, fri->FileNameLength + sizeof(WCHAR));
5324 ret = pSetFileInformationByHandle(file, FileRenameInfo, fri, size);
5325 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS, "FileRenameInfo unexpected result %d\n", GetLastError());
5327 fri->ReplaceIfExists = TRUE;
5328 ret = pSetFileInformationByHandle(file, FileRenameInfo, fri, size);
5329 ok(ret, "FileRenameInfo failed, error %d\n", GetLastError());
5331 fri->ReplaceIfExists = FALSE;
5332 fri->FileNameLength = wcslen(tempFileTo2) * sizeof(WCHAR);
5333 memcpy(fri->FileName, tempFileTo2, fri->FileNameLength + sizeof(WCHAR));
5334 ret = pSetFileInformationByHandle(file, FileRenameInfo, fri, size);
5335 ok(ret, "FileRenameInfo failed, error %d\n", GetLastError());
5336 CloseHandle(file);
5338 file = CreateFileW(tempFileTo2, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
5339 ok(file != INVALID_HANDLE_VALUE, "file not renamed, error %d\n", GetLastError());
5341 fri->FileNameLength = wcslen(tempFileTo1) * sizeof(WCHAR);
5342 memcpy(fri->FileName, tempFileTo1, fri->FileNameLength + sizeof(WCHAR));
5343 ret = pSetFileInformationByHandle(file, FileRenameInfo, fri, size);
5344 todo_wine
5345 ok(!ret && GetLastError() == ERROR_ACCESS_DENIED, "FileRenameInfo unexpected result %d\n", GetLastError());
5346 CloseHandle(file);
5348 HeapFree(GetProcessHeap(), 0, fri);
5349 DeleteFileW(tempFileFrom);
5350 DeleteFileW(tempFileTo1);
5351 DeleteFileW(tempFileTo2);
5354 static void test_GetFileAttributesExW(void)
5356 static const WCHAR path1[] = {'\\','\\','?','\\',0};
5357 static const WCHAR path2[] = {'\\','?','?','\\',0};
5358 static const WCHAR path3[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
5359 WIN32_FILE_ATTRIBUTE_DATA info;
5360 BOOL ret;
5362 SetLastError(0xdeadbeef);
5363 ret = GetFileAttributesExW(path1, GetFileExInfoStandard, &info);
5364 ok(!ret, "GetFileAttributesExW succeeded\n");
5365 ok(GetLastError() == ERROR_INVALID_NAME, "Expected error ERROR_INVALID_NAME, got %u\n", GetLastError());
5367 SetLastError(0xdeadbeef);
5368 ret = GetFileAttributesExW(path2, GetFileExInfoStandard, &info);
5369 ok(!ret, "GetFileAttributesExW succeeded\n");
5370 ok(GetLastError() == ERROR_INVALID_NAME, "Expected error ERROR_INVALID_NAME, got %u\n", GetLastError());
5372 SetLastError(0xdeadbeef);
5373 ret = GetFileAttributesExW(path3, GetFileExInfoStandard, &info);
5374 ok(!ret, "GetFileAttributesExW succeeded\n");
5375 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
5378 static void test_post_completion(void)
5380 OVERLAPPED ovl, ovl2, *povl;
5381 OVERLAPPED_ENTRY entries[2];
5382 ULONG_PTR key;
5383 HANDLE port;
5384 ULONG count;
5385 DWORD size;
5386 BOOL ret;
5388 port = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 999, 0 );
5389 ok(port != NULL, "CreateIoCompletionPort failed: %u\n", GetLastError());
5391 ret = GetQueuedCompletionStatus( port, &size, &key, &povl, 0 );
5392 ok(!ret, "GetQueuedCompletionStatus succeeded\n");
5393 ok(GetLastError() == WAIT_TIMEOUT, "wrong error %u\n", GetLastError());
5395 ret = PostQueuedCompletionStatus( port, 123, 456, &ovl );
5396 ok(ret, "PostQueuedCompletionStatus failed: %u\n", GetLastError());
5398 ret = GetQueuedCompletionStatus( port, &size, &key, &povl, 0 );
5399 ok(ret, "GetQueuedCompletionStatus failed: %u\n", GetLastError());
5400 ok(size == 123, "wrong size %u\n", size);
5401 ok(key == 456, "wrong key %lu\n", key);
5402 ok(povl == &ovl, "wrong ovl %p\n", povl);
5404 ret = GetQueuedCompletionStatus( port, &size, &key, &povl, 0 );
5405 ok(!ret, "GetQueuedCompletionStatus succeeded\n");
5406 ok(GetLastError() == WAIT_TIMEOUT, "wrong error %u\n", GetLastError());
5408 if (!pGetQueuedCompletionStatusEx)
5410 win_skip("GetQueuedCompletionStatusEx not available\n");
5411 CloseHandle( port );
5412 return;
5415 count = 0xdeadbeef;
5416 ret = pGetQueuedCompletionStatusEx( port, entries, 2, &count, 0, FALSE );
5417 ok(!ret, "GetQueuedCompletionStatusEx succeeded\n");
5418 ok(GetLastError() == WAIT_TIMEOUT, "wrong error %u\n", GetLastError());
5419 ok(count == 1, "wrong count %u\n", count);
5421 ret = PostQueuedCompletionStatus( port, 123, 456, &ovl );
5422 ok(ret, "PostQueuedCompletionStatus failed: %u\n", GetLastError());
5424 count = 0xdeadbeef;
5425 memset( entries, 0xcc, sizeof(entries) );
5426 ret = pGetQueuedCompletionStatusEx( port, entries, 2, &count, 0, FALSE );
5427 ok(ret, "GetQueuedCompletionStatusEx failed\n");
5428 ok(count == 1, "wrong count %u\n", count);
5429 ok(entries[0].lpCompletionKey == 456, "wrong key %lu\n", entries[0].lpCompletionKey);
5430 ok(entries[0].lpOverlapped == &ovl, "wrong ovl %p\n", entries[0].lpOverlapped);
5431 ok(!(ULONG)entries[0].Internal, "wrong internal %#x\n", (ULONG)entries[0].Internal);
5432 ok(entries[0].dwNumberOfBytesTransferred == 123, "wrong size %u\n", entries[0].dwNumberOfBytesTransferred);
5434 ret = PostQueuedCompletionStatus( port, 123, 456, &ovl );
5435 ok(ret, "PostQueuedCompletionStatus failed: %u\n", GetLastError());
5437 ret = PostQueuedCompletionStatus( port, 654, 321, &ovl2 );
5438 ok(ret, "PostQueuedCompletionStatus failed: %u\n", GetLastError());
5440 count = 0xdeadbeef;
5441 memset( entries, 0xcc, sizeof(entries) );
5442 ret = pGetQueuedCompletionStatusEx( port, entries, 2, &count, 0, FALSE );
5443 ok(ret, "GetQueuedCompletionStatusEx failed\n");
5444 ok(count == 2, "wrong count %u\n", count);
5445 ok(entries[0].lpCompletionKey == 456, "wrong key %lu\n", entries[0].lpCompletionKey);
5446 ok(entries[0].lpOverlapped == &ovl, "wrong ovl %p\n", entries[0].lpOverlapped);
5447 ok(!(ULONG)entries[0].Internal, "wrong internal %#x\n", (ULONG)entries[0].Internal);
5448 ok(entries[0].dwNumberOfBytesTransferred == 123, "wrong size %u\n", entries[0].dwNumberOfBytesTransferred);
5449 ok(entries[1].lpCompletionKey == 321, "wrong key %lu\n", entries[1].lpCompletionKey);
5450 ok(entries[1].lpOverlapped == &ovl2, "wrong ovl %p\n", entries[1].lpOverlapped);
5451 ok(!(ULONG)entries[1].Internal, "wrong internal %#x\n", (ULONG)entries[1].Internal);
5452 ok(entries[1].dwNumberOfBytesTransferred == 654, "wrong size %u\n", entries[1].dwNumberOfBytesTransferred);
5454 user_apc_ran = FALSE;
5455 QueueUserAPC( user_apc, GetCurrentThread(), 0 );
5457 ret = pGetQueuedCompletionStatusEx( port, entries, 2, &count, 0, FALSE );
5458 ok(!ret, "GetQueuedCompletionStatusEx succeeded\n");
5459 ok(GetLastError() == WAIT_TIMEOUT, "wrong error %u\n", GetLastError());
5460 ok(count == 1, "wrong count %u\n", count);
5461 ok(!user_apc_ran, "user APC should not have run\n");
5463 ret = pGetQueuedCompletionStatusEx( port, entries, 2, &count, 0, TRUE );
5464 ok(!ret || broken(ret) /* Vista */, "GetQueuedCompletionStatusEx succeeded\n");
5465 if (!ret)
5466 ok(GetLastError() == WAIT_IO_COMPLETION, "wrong error %u\n", GetLastError());
5467 ok(count == 1, "wrong count %u\n", count);
5468 ok(user_apc_ran, "user APC should have run\n");
5470 user_apc_ran = FALSE;
5471 QueueUserAPC( user_apc, GetCurrentThread(), 0 );
5473 ret = PostQueuedCompletionStatus( port, 123, 456, &ovl );
5474 ok(ret, "PostQueuedCompletionStatus failed: %u\n", GetLastError());
5476 ret = pGetQueuedCompletionStatusEx( port, entries, 2, &count, 0, TRUE );
5477 ok(ret, "GetQueuedCompletionStatusEx failed\n");
5478 ok(count == 1, "wrong count %u\n", count);
5479 ok(!user_apc_ran, "user APC should not have run\n");
5481 SleepEx(0, TRUE);
5483 CloseHandle( port );
5486 #define TEST_OVERLAPPED_READ_SIZE 4096
5488 static void test_overlapped_read(void)
5490 DECLSPEC_ALIGN(TEST_OVERLAPPED_READ_SIZE) static unsigned char buffer[TEST_OVERLAPPED_READ_SIZE];
5491 static const char prefix[] = "pfx";
5492 char temp_path[MAX_PATH];
5493 char file_name[MAX_PATH];
5494 DWORD bytes_count;
5495 OVERLAPPED ov;
5496 HANDLE hfile;
5497 DWORD err;
5498 DWORD ret;
5500 ret = GetTempPathA(MAX_PATH, temp_path);
5501 ok(ret, "Unexpected error %u.\n", GetLastError());
5502 ret = GetTempFileNameA(temp_path, prefix, 0, file_name);
5503 ok(ret, "Unexpected error %u.\n", GetLastError());
5505 hfile = CreateFileA(file_name, GENERIC_WRITE, 0,
5506 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
5507 ok(hfile != INVALID_HANDLE_VALUE, "Failed to create file, GetLastError() %u.\n", GetLastError());
5508 memset(buffer, 0x55, sizeof(buffer));
5509 ret = WriteFile(hfile, buffer, TEST_OVERLAPPED_READ_SIZE, &bytes_count, NULL);
5510 ok(ret && bytes_count == TEST_OVERLAPPED_READ_SIZE,
5511 "Unexpected WriteFile result, ret %#x, bytes_count %u, GetLastError() %u.\n",
5512 ret, bytes_count, GetLastError());
5513 CloseHandle(hfile);
5515 hfile = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ,
5516 NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL);
5517 ok(hfile != INVALID_HANDLE_VALUE, "Failed to create file, GetLastError() %u.\n", GetLastError());
5519 memset(&ov, 0, sizeof(ov));
5521 bytes_count = 0xffffffff;
5522 ret = ReadFile(hfile, buffer, TEST_OVERLAPPED_READ_SIZE, &bytes_count, &ov);
5523 ok(!ret && GetLastError() == ERROR_IO_PENDING,
5524 "Unexpected ReadFile result, ret %#x, GetLastError() %u.\n", ret, GetLastError());
5525 ok(!bytes_count, "Unexpected read size %u.\n", bytes_count);
5526 ret = GetOverlappedResult(hfile, &ov, &bytes_count, TRUE);
5527 ok(ret, "Unexpected error %u.\n", GetLastError());
5528 ok(bytes_count == TEST_OVERLAPPED_READ_SIZE, "Unexpected read size %u.\n", bytes_count);
5530 S(U(ov)).Offset = bytes_count;
5531 ret = ReadFile(hfile, buffer, TEST_OVERLAPPED_READ_SIZE, &bytes_count, &ov);
5532 err = GetLastError();
5533 /* Win8+ return ERROR_IO_PENDING like stated in MSDN, while older ones
5534 * return ERROR_HANDLE_EOF right away. */
5535 ok(!ret && (err == ERROR_IO_PENDING || broken(err == ERROR_HANDLE_EOF)),
5536 "Unexpected ReadFile result, ret %#x, GetLastError() %u.\n", ret, GetLastError());
5537 if (err == ERROR_IO_PENDING)
5539 ret = GetOverlappedResult(hfile, &ov, &bytes_count, TRUE);
5540 ok(!ret && GetLastError() == ERROR_HANDLE_EOF, "Unexpected result %#x, GetLasttError() %u.\n",
5541 ret, GetLastError());
5543 ok(!bytes_count, "Unexpected read size %u.\n", bytes_count);
5545 CloseHandle(hfile);
5546 ret = DeleteFileA(file_name);
5547 ok(ret, "Unexpected error %u.\n", GetLastError());
5550 static void test_file_readonly_access(void)
5552 static const DWORD default_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
5553 static const CHAR prefix[] = "pfx";
5554 CHAR file_name[MAX_PATH], file_name2[MAX_PATH];
5555 CHAR temp_path[MAX_PATH];
5556 HANDLE handle;
5557 DWORD error;
5558 DWORD ret;
5560 /* Set up */
5561 ret = GetTempPathA(MAX_PATH, temp_path);
5562 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
5563 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
5565 ret = GetTempFileNameA(temp_path, prefix, 0, file_name);
5566 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
5567 ret = DeleteFileA(file_name);
5568 ok(ret, "expect success\n");
5570 ret = GetTempFileNameA(temp_path, prefix, 0, file_name2);
5571 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
5572 ret = DeleteFileA(file_name2);
5573 ok(ret, "expect success\n");
5575 handle = CreateFileA(file_name, 0, default_sharing, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);
5576 ok(handle != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
5577 CloseHandle(handle);
5579 /* CreateFile GENERIC_WRITE */
5580 SetLastError(0xdeadbeef);
5581 handle = CreateFileA(file_name, GENERIC_WRITE, default_sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5582 error = GetLastError();
5583 ok(handle == INVALID_HANDLE_VALUE, "expect failure\n");
5584 ok(error == ERROR_ACCESS_DENIED, "wrong error code: %#x\n", error);
5586 /* CreateFile DELETE without FILE_FLAG_DELETE_ON_CLOSE */
5587 handle = CreateFileA(file_name, DELETE, default_sharing, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
5588 ok(handle != INVALID_HANDLE_VALUE, "expect success\n");
5589 CloseHandle(handle);
5591 /* CreateFile DELETE with FILE_FLAG_DELETE_ON_CLOSE */
5592 SetLastError(0xdeadbeef);
5593 handle = CreateFileA(file_name, DELETE, default_sharing, NULL, OPEN_EXISTING,
5594 FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_NORMAL, 0);
5595 error = GetLastError();
5596 ok(handle == INVALID_HANDLE_VALUE, "expect failure\n");
5597 ok(error == ERROR_ACCESS_DENIED, "wrong error code: %#x\n", error);
5599 ret = MoveFileA(file_name, file_name2);
5600 ok(ret, "expect success\n");
5601 ret = MoveFileA(file_name2, file_name);
5602 ok(ret, "expect success\n");
5604 SetLastError(0xdeadbeef);
5605 ret = DeleteFileA(file_name);
5606 error = GetLastError();
5607 ok(!ret, "expect failure\n");
5608 ok(error == ERROR_ACCESS_DENIED, "wrong error code: %#x\n", error);
5610 ret = GetFileAttributesA(file_name);
5611 ok(ret & FILE_ATTRIBUTE_READONLY, "got wrong attribute: %#x.\n", ret);
5613 /* Clean up */
5614 SetFileAttributesA(file_name, FILE_ATTRIBUTE_NORMAL);
5615 ret = DeleteFileA(file_name);
5616 ok(ret, "DeleteFileA: error %d\n", GetLastError());
5619 static void test_find_file_stream(void)
5621 WCHAR path[] = {'C',':','\\','w','i','n','d','o','w','s',0};
5622 HANDLE handle;
5623 int error;
5624 WIN32_FIND_STREAM_DATA data;
5626 if (!pFindFirstStreamW)
5628 win_skip("FindFirstStreamW is missing\n");
5629 return;
5632 SetLastError(0xdeadbeef);
5633 handle = pFindFirstStreamW(path, FindStreamInfoStandard, &data, 0);
5634 error = GetLastError();
5635 ok(handle == INVALID_HANDLE_VALUE, "Expected INVALID_HANDLE_VALUE, got %p\n", handle);
5636 ok(error == ERROR_HANDLE_EOF, "Expected ERROR_HANDLE_EOF, got %d\n", error);
5639 static void test_SetFileTime(void)
5641 static const WCHAR prefix[] = {'p','f','x',0};
5642 WCHAR path[MAX_PATH], temp_path[MAX_PATH];
5643 FILETIME ft1, ft2;
5644 DWORD ret, len;
5645 HANDLE hfile;
5647 ret = GetTempPathW(MAX_PATH, temp_path);
5648 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
5649 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
5651 ret = GetTempFileNameW(temp_path, prefix, 0, path);
5652 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
5654 hfile = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
5655 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
5656 ret = WriteFile(hfile, prefix, sizeof(prefix), &len, NULL );
5657 ok(ret && len == sizeof(prefix), "WriteFile error %d\n", GetLastError());
5658 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
5660 ret = GetFileTime(hfile, NULL, NULL, &ft1);
5661 ok(ret, "GetFileTime error %d\n", GetLastError());
5662 ft2 = ft1;
5663 ft2.dwLowDateTime -= 600000000; /* 60 second */
5664 ret = SetFileTime(hfile, NULL, NULL, &ft2);
5665 ok(ret, "SetFileTime error %d\n", GetLastError());
5666 memset(&ft2, 0, sizeof(ft2));
5667 ret = GetFileTime(hfile, NULL, NULL, &ft2); /* get the actual time back */
5668 ok(ret, "GetFileTime error %d\n", GetLastError());
5669 ok(memcmp(&ft1, &ft2, sizeof(ft1)), "Unexpected write time.\n");
5671 memset(&ft1, 0xff, sizeof(ft1));
5672 ret = SetFileTime(hfile, NULL, NULL, &ft1);
5673 ok(ret, "SetFileTime error %d\n", GetLastError());
5674 memset(&ft1, 0, sizeof(ft1));
5675 ret = GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
5676 ok(ret, "GetFileTime error %d\n", GetLastError());
5677 ok(!memcmp(&ft1, &ft2, sizeof(ft1)), "Unexpected write time.\n");
5679 CloseHandle(hfile);
5682 static void test_hard_link(void)
5684 char cwd[MAX_PATH], temp_dir[MAX_PATH], name_buffer[200], buffer[20];
5685 FILE_NAME_INFORMATION *name_info = (FILE_NAME_INFORMATION *)name_buffer;
5686 IO_STATUS_BLOCK io;
5687 NTSTATUS status;
5688 HANDLE file;
5689 DWORD size;
5690 BOOL ret;
5692 GetCurrentDirectoryA( sizeof(cwd), cwd );
5693 GetTempPathA( sizeof(temp_dir), temp_dir );
5694 SetCurrentDirectoryA( temp_dir );
5696 ret = CreateDirectoryA( "winetest_dir1", NULL );
5697 ok(ret, "failed to create directory, error %u\n", GetLastError());
5698 ret = CreateDirectoryA( "winetest_dir2", NULL );
5699 ok(ret, "failed to create directory, error %u\n", GetLastError());
5700 create_file( "winetest_file1" );
5701 create_file( "winetest_file2" );
5703 ret = CreateHardLinkA( "winetest_file3", "winetest_file1", NULL );
5704 ok(ret, "got error %u\n", GetLastError());
5706 file = CreateFileA( "winetest_file3", FILE_READ_DATA, 0, NULL, OPEN_EXISTING, 0, NULL );
5707 ok(file != INVALID_HANDLE_VALUE, "got error %u\n", GetLastError());
5709 status = NtQueryInformationFile( file, &io, name_buffer, sizeof(name_buffer), FileNameInformation );
5710 ok(!status, "got status %#x\n", status);
5711 ok(!wcsncmp(name_info->FileName + (name_info->FileNameLength / sizeof(WCHAR)) - wcslen(L"\\winetest_file3"),
5712 L"\\winetest_file3", wcslen(L"\\winetest_file3")), "got name %s\n",
5713 debugstr_wn(name_info->FileName, name_info->FileNameLength / sizeof(WCHAR)));
5715 ret = ReadFile( file, buffer, sizeof(buffer), &size, NULL );
5716 ok(ret, "got error %u\n", GetLastError());
5717 ok(!memcmp( buffer, "winetest_file1", size ), "got file contents %s\n", debugstr_an( buffer, size ));
5719 CloseHandle( file );
5721 ret = DeleteFileA( "winetest_file3" );
5722 ok(ret, "failed to delete file, error %u\n", GetLastError());
5724 SetLastError(0xdeadbeef);
5725 ret = CreateHardLinkA( "winetest_file2", "winetest_file1", NULL );
5726 ok(!ret, "expected failure\n");
5727 ok(GetLastError() == ERROR_ALREADY_EXISTS, "got error %u\n", GetLastError());
5729 SetLastError(0xdeadbeef);
5730 ret = CreateHardLinkA( "winetest_file3", "winetest_dir1", NULL );
5731 ok(!ret, "expected failure\n");
5732 ok(GetLastError() == ERROR_ACCESS_DENIED, "got error %u\n", GetLastError());
5734 SetLastError(0xdeadbeef);
5735 ret = CreateHardLinkA( "winetest_dir2", "winetest_dir1", NULL );
5736 ok(!ret, "expected failure\n");
5737 ok(GetLastError() == ERROR_ACCESS_DENIED
5738 || GetLastError() == ERROR_ALREADY_EXISTS /* XP */, "got error %u\n", GetLastError());
5740 SetLastError(0xdeadbeef);
5741 ret = CreateHardLinkA( "winetest_dir1", "winetest_file1", NULL );
5742 ok(!ret, "expected failure\n");
5743 ok(GetLastError() == ERROR_ALREADY_EXISTS, "got error %u\n", GetLastError());
5745 ret = RemoveDirectoryA( "winetest_dir1" );
5746 ok(ret, "failed to remove directory, error %u\n", GetLastError());
5747 ret = RemoveDirectoryA( "winetest_dir2" );
5748 ok(ret, "failed to remove directory, error %u\n", GetLastError());
5749 ret = DeleteFileA( "winetest_file1" );
5750 ok(ret, "failed to delete file, error %u\n", GetLastError());
5751 ret = DeleteFileA( "winetest_file2" );
5752 ok(ret, "failed to delete file, error %u\n", GetLastError());
5753 SetCurrentDirectoryA( cwd );
5756 static void test_move_file(void)
5758 char cwd[MAX_PATH], temp_dir[MAX_PATH];
5759 HANDLE file;
5760 BOOL ret;
5762 GetCurrentDirectoryA( sizeof(cwd), cwd );
5763 GetTempPathA( sizeof(temp_dir), temp_dir );
5764 SetCurrentDirectoryA( temp_dir );
5766 ret = CreateDirectoryA( "winetest_dir1", NULL );
5767 ok(ret, "failed to create directory, error %u\n", GetLastError());
5768 ret = CreateDirectoryA( "winetest_dir2", NULL );
5769 ok(ret, "failed to create directory, error %u\n", GetLastError());
5770 create_file( "winetest_file1" );
5771 create_file( "winetest_file2" );
5773 ret = MoveFileA( "winetest_file1", "winetest_file3" );
5774 ok(ret, "failed to move file, error %u\n", GetLastError());
5775 ret = GetFileAttributesA( "winetest_file1" );
5776 ok(ret == INVALID_FILE_ATTRIBUTES, "got %#x\n", ret);
5777 ret = GetFileAttributesA( "winetest_file3" );
5778 ok(ret != INVALID_FILE_ATTRIBUTES, "got %#x\n", ret);
5780 SetLastError(0xdeadbeef);
5781 ret = MoveFileA( "winetest_file3", "winetest_file2" );
5782 ok(!ret, "expected failure\n");
5783 ok(GetLastError() == ERROR_ALREADY_EXISTS, "got error %u\n", GetLastError());
5785 SetLastError(0xdeadbeef);
5786 ret = MoveFileA( "winetest_file1", "winetest_file4" );
5787 ok(!ret, "expected failure\n");
5788 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "got error %u\n", GetLastError());
5790 ret = MoveFileA( "winetest_dir1", "winetest_dir3" );
5791 ok(ret, "failed to move file, error %u\n", GetLastError());
5793 SetLastError(0xdeadbeef);
5794 ret = MoveFileA( "winetest_dir3", "winetest_dir2" );
5795 ok(!ret, "expected failure\n");
5796 ok(GetLastError() == ERROR_ALREADY_EXISTS, "got error %u\n", GetLastError());
5798 file = CreateFileA( "winetest_file3", DELETE, 0, NULL, OPEN_EXISTING, 0, 0 );
5799 ok(file != INVALID_HANDLE_VALUE, "failed to open file, error %u\n", GetLastError());
5800 ret = MoveFileA( "winetest_file3", "winetest_file1" );
5801 ok(!ret, "expected failure\n");
5802 ok(GetLastError() == ERROR_SHARING_VIOLATION, "got error %u\n", GetLastError());
5803 CloseHandle( file );
5805 file = CreateFileA( "winetest_file3", 0, 0, NULL, OPEN_EXISTING, 0, 0 );
5806 ok(file != INVALID_HANDLE_VALUE, "failed to open file, error %u\n", GetLastError());
5807 ret = MoveFileA( "winetest_file3", "winetest_file1" );
5808 ok(ret, "failed to move file, error %u\n", GetLastError());
5809 ret = GetFileAttributesA( "winetest_file1" );
5810 ok(ret != INVALID_FILE_ATTRIBUTES, "got %#x\n", ret);
5811 ret = GetFileAttributesA( "winetest_file3" );
5812 ok(ret == INVALID_FILE_ATTRIBUTES, "got %#x\n", ret);
5813 CloseHandle( file );
5815 ret = MoveFileExA( "winetest_file1", "winetest_file2", MOVEFILE_REPLACE_EXISTING );
5816 ok(ret, "failed to move file, error %u\n", GetLastError());
5818 file = CreateFileA( "winetest_file1", GENERIC_ALL,
5819 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_NEW, 0, 0 );
5820 ok(file != INVALID_HANDLE_VALUE, "failed to open file, error %u\n", GetLastError());
5821 SetLastError(0xdeadbeef);
5822 ret = MoveFileExA( "winetest_file2", "winetest_file1", MOVEFILE_REPLACE_EXISTING );
5823 ok(!ret, "expected failure\n");
5824 ok(GetLastError() == ERROR_ACCESS_DENIED, "got error %u\n", GetLastError());
5825 CloseHandle( file );
5827 SetLastError(0xdeadbeef);
5828 ret = MoveFileExA( "winetest_file2", "winetest_dir2", MOVEFILE_REPLACE_EXISTING );
5829 ok(!ret, "expected failure\n");
5830 ok(GetLastError() == ERROR_ACCESS_DENIED, "got error %u\n", GetLastError());
5832 SetLastError(0xdeadbeef);
5833 ret = MoveFileExA( "winetest_dir3", "winetest_dir2", MOVEFILE_REPLACE_EXISTING );
5834 ok(!ret, "expected failure\n");
5835 ok(GetLastError() == ERROR_ACCESS_DENIED, "got error %u\n", GetLastError());
5837 ret = MoveFileExA( "winetest_dir2", "winetest_file2", MOVEFILE_REPLACE_EXISTING );
5838 ok(ret, "failed to move file, error %u\n", GetLastError());
5840 ret = RemoveDirectoryA( "winetest_dir3" );
5841 ok(ret, "failed to remove directory, error %u\n", GetLastError());
5842 ret = RemoveDirectoryA( "winetest_file2" );
5843 ok(ret, "failed to remove directory, error %u\n", GetLastError());
5844 ret = DeleteFileA( "winetest_file1" );
5845 ok(ret, "failed to delete file, error %u\n", GetLastError());
5846 SetCurrentDirectoryA( cwd );
5849 START_TEST(file)
5851 char temp_path[MAX_PATH];
5852 DWORD ret;
5854 InitFunctionPointers();
5856 ret = GetTempPathA(MAX_PATH, temp_path);
5857 ok(ret != 0, "GetTempPath error %u\n", GetLastError());
5858 ret = GetTempFileNameA(temp_path, "tmp", 0, filename);
5859 ok(ret != 0, "GetTempFileName error %u\n", GetLastError());
5860 ret = DeleteFileA(filename);
5861 ok(ret != 0, "DeleteFile error %u\n", GetLastError());
5863 test__hread( );
5864 test__hwrite( );
5865 test__lclose( );
5866 test__lcreat( );
5867 test__llseek( );
5868 test__llopen( );
5869 test__lread( );
5870 test__lwrite( );
5871 test_GetTempFileNameA();
5872 test_CopyFileA();
5873 test_CopyFileW();
5874 test_CopyFile2();
5875 test_CopyFileEx();
5876 test_CreateFile();
5877 test_CreateFileA();
5878 test_CreateFileW();
5879 test_CreateFile2();
5880 test_DeleteFileA();
5881 test_DeleteFileW();
5882 test_MoveFileA();
5883 test_MoveFileW();
5884 test_FindFirstFileA();
5885 test_FindNextFileA();
5886 test_FindFirstFile_wildcards();
5887 test_FindFirstFileExA(FindExInfoStandard, 0, 0);
5888 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_CASE_SENSITIVE);
5889 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_LARGE_FETCH);
5890 test_FindFirstFileExA(FindExInfoBasic, 0, 0);
5891 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
5892 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, 0);
5893 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_CASE_SENSITIVE);
5894 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_LARGE_FETCH);
5895 test_FindFirstFileExA(FindExInfoBasic, FindExSearchLimitToDirectories, 0);
5896 test_LockFile();
5897 test_file_sharing();
5898 test_offset_in_overlapped_structure();
5899 test_MapFile();
5900 test_GetFileType();
5901 test_async_file_errors();
5902 test_read_write();
5903 test_OpenFile();
5904 test_overlapped();
5905 test_RemoveDirectory();
5906 test_ReplaceFileA();
5907 test_ReplaceFileW();
5908 test_GetFileInformationByHandleEx();
5909 test_OpenFileById();
5910 test_SetFileValidData();
5911 test_WriteFileGather();
5912 test_file_access();
5913 test_GetFinalPathNameByHandleA();
5914 test_GetFinalPathNameByHandleW();
5915 test_SetFileInformationByHandle();
5916 test_SetFileRenameInfo();
5917 test_GetFileAttributesExW();
5918 test_post_completion();
5919 test_overlapped_read();
5920 test_file_readonly_access();
5921 test_find_file_stream();
5922 test_SetFileTime();
5923 test_ReOpenFile();
5924 test_hard_link();
5925 test_move_file();