ntdll: Don't require full-page reads in NtReadFileScatter.
[wine.git] / dlls / kernel32 / tests / file.c
blob410c3f059124922f491cfaf195a39eba21b38b0f
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 /* ReplaceFile requires Windows 2000 or newer */
24 #define _WIN32_WINNT 0x0500
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <stdio.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "wine/test.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winternl.h"
38 #include "winnls.h"
39 #include "fileapi.h"
41 #ifdef WINE_NO_UNICODE_MACROS
42 #undef DeleteFile /* needed for FILE_DISPOSITION_INFO */
43 #endif
45 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
46 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
47 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
48 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
49 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
50 static DWORD (WINAPI *pQueueUserAPC)(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
51 static BOOL (WINAPI *pGetFileInformationByHandleEx)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, LPVOID, DWORD);
52 static HANDLE (WINAPI *pOpenFileById)(HANDLE, LPFILE_ID_DESCRIPTOR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD);
53 static BOOL (WINAPI *pSetFileValidData)(HANDLE, LONGLONG);
54 static HRESULT (WINAPI *pCopyFile2)(PCWSTR,PCWSTR,COPYFILE2_EXTENDED_PARAMETERS*);
55 static HANDLE (WINAPI *pCreateFile2)(LPCWSTR, DWORD, DWORD, DWORD, CREATEFILE2_EXTENDED_PARAMETERS*);
56 static DWORD (WINAPI *pGetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, DWORD);
57 static DWORD (WINAPI *pGetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, DWORD);
58 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
59 PLARGE_INTEGER, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG);
60 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)(LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR*);
61 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PCANSI_STRING, BOOLEAN);
62 static BOOL (WINAPI *pSetFileInformationByHandle)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, void*, DWORD);
64 static const char filename[] = "testfile.xxx";
65 static const char sillytext[] =
66 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
75 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
77 struct test_list {
78 const char *file; /* file string to test */
79 const DWORD err; /* Win NT and further error code */
80 const LONG err2; /* Win 9x & ME error code or -1 */
81 const DWORD options; /* option flag to use for open */
82 const BOOL todo_flag; /* todo_wine indicator */
83 } ;
85 static void InitFunctionPointers(void)
87 HMODULE hntdll = GetModuleHandleA("ntdll");
88 HMODULE hkernel32 = GetModuleHandleA("kernel32");
90 pNtCreateFile = (void *)GetProcAddress(hntdll, "NtCreateFile");
91 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
92 pRtlAnsiStringToUnicodeString = (void *)GetProcAddress(hntdll, "RtlAnsiStringToUnicodeString");
94 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
95 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
96 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
97 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
98 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
99 pQueueUserAPC = (void *) GetProcAddress(hkernel32, "QueueUserAPC");
100 pGetFileInformationByHandleEx = (void *) GetProcAddress(hkernel32, "GetFileInformationByHandleEx");
101 pOpenFileById = (void *) GetProcAddress(hkernel32, "OpenFileById");
102 pSetFileValidData = (void *) GetProcAddress(hkernel32, "SetFileValidData");
103 pCopyFile2 = (void *) GetProcAddress(hkernel32, "CopyFile2");
104 pCreateFile2 = (void *) GetProcAddress(hkernel32, "CreateFile2");
105 pGetFinalPathNameByHandleA = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleA");
106 pGetFinalPathNameByHandleW = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleW");
107 pSetFileInformationByHandle = (void *) GetProcAddress(hkernel32, "SetFileInformationByHandle");
110 static void test__hread( void )
112 HFILE filehandle;
113 char buffer[10000];
114 LONG bytes_read;
115 LONG bytes_wanted;
116 LONG i;
117 BOOL ret;
119 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
120 DeleteFileA( filename );
121 filehandle = _lcreat( filename, 0 );
122 if (filehandle == HFILE_ERROR)
124 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
125 return;
128 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
130 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
132 filehandle = _lopen( filename, OF_READ );
134 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
136 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
138 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
140 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
142 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
143 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
144 for (i = 0; i < bytes_wanted; i++)
146 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
150 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
152 ret = DeleteFileA( filename );
153 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
157 static void test__hwrite( void )
159 HFILE filehandle;
160 char buffer[10000];
161 LONG bytes_read;
162 LONG bytes_written;
163 ULONG blocks;
164 LONG i;
165 char *contents;
166 HLOCAL memory_object;
167 char checksum[1];
168 BOOL ret;
170 filehandle = _lcreat( filename, 0 );
171 if (filehandle == HFILE_ERROR)
173 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
174 return;
177 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
179 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
181 filehandle = _lopen( filename, OF_READ );
183 bytes_read = _hread( filehandle, buffer, 1);
185 ok( 0 == bytes_read, "file read size error\n" );
187 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
189 filehandle = _lopen( filename, OF_READWRITE );
191 bytes_written = 0;
192 checksum[0] = '\0';
193 srand( (unsigned)time( NULL ) );
194 for (blocks = 0; blocks < 100; blocks++)
196 for (i = 0; i < (LONG)sizeof( buffer ); i++)
198 buffer[i] = rand( );
199 checksum[0] = checksum[0] + buffer[i];
201 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
202 bytes_written = bytes_written + sizeof( buffer );
205 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
206 bytes_written++;
208 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
210 memory_object = LocalAlloc( LPTR, bytes_written );
212 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
214 contents = LocalLock( memory_object );
215 ok( NULL != contents, "LocalLock whines\n" );
217 filehandle = _lopen( filename, OF_READ );
219 contents = LocalLock( memory_object );
220 ok( NULL != contents, "LocalLock whines\n" );
222 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
224 checksum[0] = '\0';
225 i = 0;
228 checksum[0] = checksum[0] + contents[i];
229 i++;
231 while (i < bytes_written - 1);
233 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
235 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
237 ret = DeleteFileA( filename );
238 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
240 LocalFree( contents );
244 static void test__lclose( void )
246 HFILE filehandle;
247 BOOL ret;
249 filehandle = _lcreat( filename, 0 );
250 if (filehandle == HFILE_ERROR)
252 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
253 return;
256 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
258 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
260 ret = DeleteFileA( filename );
261 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
264 /* helper function for test__lcreat */
265 static void get_nt_pathW( const char *name, UNICODE_STRING *nameW )
267 UNICODE_STRING strW;
268 ANSI_STRING str;
269 NTSTATUS status;
270 BOOLEAN ret;
271 RtlInitAnsiString( &str, name );
273 status = pRtlAnsiStringToUnicodeString( &strW, &str, TRUE );
274 ok( !status, "RtlAnsiStringToUnicodeString failed with %08x\n", status );
276 ret = pRtlDosPathNameToNtPathName_U( strW.Buffer, nameW, NULL, NULL );
277 ok( ret, "RtlDosPathNameToNtPathName_U failed\n" );
279 RtlFreeUnicodeString( &strW );
282 static void test__lcreat( void )
284 UNICODE_STRING filenameW;
285 OBJECT_ATTRIBUTES attr;
286 IO_STATUS_BLOCK io;
287 HFILE filehandle;
288 char buffer[10000];
289 WIN32_FIND_DATAA search_results;
290 char slashname[] = "testfi/";
291 int err;
292 HANDLE find, file;
293 NTSTATUS status;
294 BOOL ret;
296 filehandle = _lcreat( filename, 0 );
297 if (filehandle == HFILE_ERROR)
299 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
300 return;
303 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
305 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
307 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
309 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
311 find = FindFirstFileA( filename, &search_results );
312 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
313 FindClose( find );
315 ret = DeleteFileA(filename);
316 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
318 filehandle = _lcreat( filename, 1 ); /* readonly */
319 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
321 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
323 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
325 find = FindFirstFileA( filename, &search_results );
326 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
327 FindClose( find );
329 SetLastError( 0xdeadbeef );
330 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
331 ok( GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError() );
333 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
335 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
337 filehandle = _lcreat( filename, 1 ); /* readonly */
338 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError() );
339 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen(sillytext) ),
340 "_hwrite shouldn't be able to write never the less\n" );
341 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
343 find = FindFirstFileA( filename, &search_results );
344 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
345 FindClose( find );
347 get_nt_pathW( filename, &filenameW );
348 attr.Length = sizeof(attr);
349 attr.RootDirectory = 0;
350 attr.Attributes = OBJ_CASE_INSENSITIVE;
351 attr.ObjectName = &filenameW;
352 attr.SecurityDescriptor = NULL;
353 attr.SecurityQualityOfService = NULL;
355 status = NtCreateFile( &file, GENERIC_READ | GENERIC_WRITE | DELETE, &attr, &io, NULL, 0,
356 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
357 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
358 ok( status == STATUS_ACCESS_DENIED, "expected STATUS_ACCESS_DENIED, got %08x\n", status );
359 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
361 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
362 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
363 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
364 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
366 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
367 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
368 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_DIRECTORY_FILE, NULL, 0 );
369 ok( status == STATUS_NOT_A_DIRECTORY, "expected STATUS_NOT_A_DIRECTORY, got %08x\n", status );
371 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
372 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
373 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
374 todo_wine
375 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
376 if (!status) CloseHandle( file );
378 RtlFreeUnicodeString( &filenameW );
380 todo_wine
381 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
382 todo_wine
383 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
384 todo_wine
385 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file\n" );
387 filehandle = _lcreat( filename, 2 );
388 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
390 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
392 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
394 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
396 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
398 find = FindFirstFileA( filename, &search_results );
399 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
400 FindClose( find );
402 ret = DeleteFileA( filename );
403 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
405 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
406 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
408 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
410 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
412 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
414 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
416 find = FindFirstFileA( filename, &search_results );
417 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
418 FindClose( find );
420 ret = DeleteFileA( filename );
421 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
423 filehandle=_lcreat (slashname, 0); /* illegal name */
424 if (HFILE_ERROR==filehandle) {
425 err=GetLastError ();
426 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
427 "creating file \"%s\" failed with error %d\n", slashname, err);
428 } else { /* only NT succeeds */
429 _lclose(filehandle);
430 find=FindFirstFileA (slashname, &search_results);
431 if (INVALID_HANDLE_VALUE!=find)
433 ret = FindClose (find);
434 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
435 slashname[strlen(slashname)-1]=0;
436 ok (!strcmp (slashname, search_results.cFileName),
437 "found unexpected name \"%s\"\n", search_results.cFileName);
438 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
439 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
440 search_results.dwFileAttributes);
442 ret = DeleteFileA( slashname );
443 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
446 filehandle=_lcreat (filename, 8); /* illegal attribute */
447 if (HFILE_ERROR==filehandle)
448 ok (0, "couldn't create volume label \"%s\"\n", filename);
449 else {
450 _lclose(filehandle);
451 find=FindFirstFileA (filename, &search_results);
452 if (INVALID_HANDLE_VALUE==find)
453 ok (0, "file \"%s\" not found\n", filename);
454 else {
455 ret = FindClose(find);
456 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
457 ok (!strcmp (filename, search_results.cFileName),
458 "found unexpected name \"%s\"\n", search_results.cFileName);
459 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
460 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_COMPRESSED;
461 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
462 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
463 search_results.dwFileAttributes);
465 ret = DeleteFileA( filename );
466 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
471 static void test__llseek( void )
473 INT i;
474 HFILE filehandle;
475 char buffer[1];
476 LONG bytes_read;
477 BOOL ret;
479 filehandle = _lcreat( filename, 0 );
480 if (filehandle == HFILE_ERROR)
482 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
483 return;
486 for (i = 0; i < 400; i++)
488 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
490 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
491 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
493 bytes_read = _hread( filehandle, buffer, 1);
494 ok( 1 == bytes_read, "file read size error\n" );
495 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
496 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
498 bytes_read = _hread( filehandle, buffer, 1);
499 ok( 1 == bytes_read, "file read size error\n" );
500 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
501 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
502 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
504 ret = DeleteFileA( filename );
505 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
509 static void test__llopen( void )
511 HFILE filehandle;
512 UINT bytes_read;
513 char buffer[10000];
514 BOOL ret;
516 filehandle = _lcreat( filename, 0 );
517 if (filehandle == HFILE_ERROR)
519 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
520 return;
523 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
524 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
526 filehandle = _lopen( filename, OF_READ );
527 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
528 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
529 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
530 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
532 filehandle = _lopen( filename, OF_READWRITE );
533 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
534 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
535 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
536 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
538 filehandle = _lopen( filename, OF_WRITE );
539 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
540 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
541 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
543 ret = DeleteFileA( filename );
544 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
545 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
549 static void test__lread( void )
551 HFILE filehandle;
552 char buffer[10000];
553 UINT bytes_read;
554 UINT bytes_wanted;
555 UINT i;
556 BOOL ret;
558 filehandle = _lcreat( filename, 0 );
559 if (filehandle == HFILE_ERROR)
561 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
562 return;
565 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
567 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
569 filehandle = _lopen( filename, OF_READ );
571 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
573 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
575 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
577 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
579 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
580 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
581 for (i = 0; i < bytes_wanted; i++)
583 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
587 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
589 ret = DeleteFileA( filename );
590 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
594 static void test__lwrite( void )
596 HFILE filehandle;
597 char buffer[10000];
598 UINT bytes_read;
599 UINT bytes_written;
600 UINT blocks;
601 INT i;
602 char *contents;
603 HLOCAL memory_object;
604 char checksum[1];
605 BOOL ret;
607 filehandle = _lcreat( filename, 0 );
608 if (filehandle == HFILE_ERROR)
610 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
611 return;
614 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
616 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
618 filehandle = _lopen( filename, OF_READ );
620 bytes_read = _hread( filehandle, buffer, 1);
622 ok( 0 == bytes_read, "file read size error\n" );
624 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
626 filehandle = _lopen( filename, OF_READWRITE );
628 bytes_written = 0;
629 checksum[0] = '\0';
630 srand( (unsigned)time( NULL ) );
631 for (blocks = 0; blocks < 100; blocks++)
633 for (i = 0; i < (INT)sizeof( buffer ); i++)
635 buffer[i] = rand( );
636 checksum[0] = checksum[0] + buffer[i];
638 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
639 bytes_written = bytes_written + sizeof( buffer );
642 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
643 bytes_written++;
645 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
647 memory_object = LocalAlloc( LPTR, bytes_written );
649 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
651 contents = LocalLock( memory_object );
652 ok( NULL != contents, "LocalLock whines\n" );
654 filehandle = _lopen( filename, OF_READ );
656 contents = LocalLock( memory_object );
657 ok( NULL != contents, "LocalLock whines\n" );
659 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
661 checksum[0] = '\0';
662 i = 0;
665 checksum[0] += contents[i];
666 i++;
668 while (i < bytes_written - 1);
670 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
672 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
674 ret = DeleteFileA( filename );
675 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
677 LocalFree( contents );
680 static void test_CopyFileA(void)
682 char temp_path[MAX_PATH];
683 char source[MAX_PATH], dest[MAX_PATH];
684 static const char prefix[] = "pfx";
685 HANDLE hfile;
686 HANDLE hmapfile;
687 FILETIME ft1, ft2;
688 char buf[10];
689 DWORD ret;
690 BOOL retok;
692 ret = GetTempPathA(MAX_PATH, temp_path);
693 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
694 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
696 ret = GetTempFileNameA(temp_path, prefix, 0, source);
697 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
699 ret = MoveFileA(source, source);
700 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
702 /* copying a file to itself must fail */
703 retok = CopyFileA(source, source, FALSE);
704 ok( !retok && (GetLastError() == ERROR_SHARING_VIOLATION || broken(GetLastError() == ERROR_FILE_EXISTS) /* Win 9x */),
705 "copying a file to itself didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
707 /* make the source have not zero size */
708 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
709 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
710 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
711 ok( retok && ret == sizeof(prefix),
712 "WriteFile error %d\n", GetLastError());
713 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
714 /* get the file time and change it to prove the difference */
715 ret = GetFileTime(hfile, NULL, NULL, &ft1);
716 ok( ret, "GetFileTime error %d\n", GetLastError());
717 ft1.dwLowDateTime -= 600000000; /* 60 second */
718 ret = SetFileTime(hfile, NULL, NULL, &ft1);
719 ok( ret, "SetFileTime error %d\n", GetLastError());
720 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
721 CloseHandle(hfile);
723 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
724 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
726 SetLastError(0xdeadbeef);
727 ret = CopyFileA(source, dest, TRUE);
728 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
729 "CopyFileA: unexpected error %d\n", GetLastError());
731 ret = CopyFileA(source, dest, FALSE);
732 ok(ret, "CopyFileA: error %d\n", GetLastError());
734 /* copying from a read-locked source fails */
735 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
736 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
737 retok = CopyFileA(source, dest, FALSE);
738 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
739 "copying from a read-locked file succeeded when it shouldn't have\n");
740 /* in addition, the source is opened before the destination */
741 retok = CopyFileA("25f99d3b-4ba4-4f66-88f5-2906886993cc", dest, FALSE);
742 ok(!retok && GetLastError() == ERROR_FILE_NOT_FOUND,
743 "copying from a file that doesn't exist failed in an unexpected way (ret=%d, err=%d)\n", retok, GetLastError());
744 CloseHandle(hfile);
746 /* copying from a r+w opened, r shared source succeeds */
747 hfile = CreateFileA(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
748 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
749 retok = CopyFileA(source, dest, FALSE);
750 ok(retok,
751 "copying from an r+w opened and r shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
752 CloseHandle(hfile);
754 /* copying from a delete-locked source mostly succeeds */
755 hfile = CreateFileA(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
756 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
757 retok = CopyFileA(source, dest, FALSE);
758 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* NT, 2000, XP */,
759 "copying from a delete-locked file failed (ret=%d, err=%d)\n", retok, GetLastError());
760 CloseHandle(hfile);
762 /* copying to a write-locked destination fails */
763 hfile = CreateFileA(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
764 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
765 retok = CopyFileA(source, dest, FALSE);
766 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
767 "copying to a write-locked file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
768 CloseHandle(hfile);
770 /* copying to a r+w opened, w shared destination mostly succeeds */
771 hfile = CreateFileA(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
772 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
773 retok = CopyFileA(source, dest, FALSE);
774 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
775 "copying to a r+w opened and w shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
776 CloseHandle(hfile);
778 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
779 hfile = CreateFileA(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
780 ok(hfile != INVALID_HANDLE_VALUE || broken(GetLastError() == ERROR_INVALID_PARAMETER) /* Win 9x */,
781 "failed to open destination file, error %d\n", GetLastError());
782 if (hfile != INVALID_HANDLE_VALUE)
784 retok = CopyFileA(source, dest, FALSE);
785 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
786 "copying to a delete-locked shared file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
787 CloseHandle(hfile);
790 /* copy to a file that's opened the way Wine opens the source */
791 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
792 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
793 retok = CopyFileA(source, dest, FALSE);
794 ok(retok || broken(GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
795 "copying to a file opened the way Wine opens the source failed (ret=%d, err=%d)\n", retok, GetLastError());
796 CloseHandle(hfile);
798 /* make sure that destination has correct size */
799 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
800 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
801 ret = GetFileSize(hfile, NULL);
802 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
804 /* make sure that destination has the same filetime */
805 ret = GetFileTime(hfile, NULL, NULL, &ft2);
806 ok( ret, "GetFileTime error %d\n", GetLastError());
807 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
809 SetLastError(0xdeadbeef);
810 ret = CopyFileA(source, dest, FALSE);
811 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
812 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
814 /* make sure that destination still has correct size */
815 ret = GetFileSize(hfile, NULL);
816 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
817 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
818 ok( retok && ret == sizeof(prefix),
819 "ReadFile: error %d\n", GetLastError());
820 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
822 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
823 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
824 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
826 ret = CopyFileA(source, dest, FALSE);
827 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
828 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
830 CloseHandle(hmapfile);
831 CloseHandle(hfile);
833 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
834 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
836 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
837 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
838 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
840 ret = CopyFileA(source, dest, FALSE);
841 ok(!ret, "CopyFileA: expected failure\n");
842 ok(GetLastError() == ERROR_USER_MAPPED_FILE ||
843 broken(GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x */
844 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
846 CloseHandle(hmapfile);
847 CloseHandle(hfile);
849 ret = DeleteFileA(source);
850 ok(ret, "DeleteFileA: error %d\n", GetLastError());
851 ret = DeleteFileA(dest);
852 ok(ret, "DeleteFileA: error %d\n", GetLastError());
855 static void test_CopyFileW(void)
857 WCHAR temp_path[MAX_PATH];
858 WCHAR source[MAX_PATH], dest[MAX_PATH];
859 static const WCHAR prefix[] = {'p','f','x',0};
860 DWORD ret;
862 ret = GetTempPathW(MAX_PATH, temp_path);
863 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
865 win_skip("GetTempPathW is not available\n");
866 return;
868 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
869 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
871 ret = GetTempFileNameW(temp_path, prefix, 0, source);
872 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
874 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
875 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
877 ret = CopyFileW(source, dest, TRUE);
878 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
879 "CopyFileW: unexpected error %d\n", GetLastError());
881 ret = CopyFileW(source, dest, FALSE);
882 ok(ret, "CopyFileW: error %d\n", GetLastError());
884 ret = DeleteFileW(source);
885 ok(ret, "DeleteFileW: error %d\n", GetLastError());
886 ret = DeleteFileW(dest);
887 ok(ret, "DeleteFileW: error %d\n", GetLastError());
890 static void test_CopyFile2(void)
892 static const WCHAR doesntexistW[] = {'d','o','e','s','n','t','e','x','i','s','t',0};
893 static const WCHAR prefix[] = {'p','f','x',0};
894 WCHAR source[MAX_PATH], dest[MAX_PATH], temp_path[MAX_PATH];
895 COPYFILE2_EXTENDED_PARAMETERS params;
896 HANDLE hfile, hmapfile;
897 FILETIME ft1, ft2;
898 DWORD ret, len;
899 char buf[10];
900 HRESULT hr;
902 if (!pCopyFile2)
904 skip("CopyFile2 is not available\n");
905 return;
908 ret = GetTempPathW(MAX_PATH, temp_path);
909 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
910 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
912 ret = GetTempFileNameW(temp_path, prefix, 0, source);
913 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
915 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
916 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
918 /* fail if exists */
919 memset(&params, 0, sizeof(params));
920 params.dwSize = sizeof(params);
921 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
923 SetLastError(0xdeadbeef);
924 hr = pCopyFile2(source, dest, &params);
925 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
926 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
928 /* don't fail if exists */
929 params.dwSize = sizeof(params);
930 params.dwCopyFlags = 0;
932 hr = pCopyFile2(source, dest, &params);
933 ok(hr == S_OK, "CopyFile2: error 0x%08x\n", hr);
935 /* copying a file to itself must fail */
936 params.dwSize = sizeof(params);
937 params.dwCopyFlags = 0;
939 SetLastError(0xdeadbeef);
940 hr = pCopyFile2(source, source, &params);
941 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: copying a file to itself didn't fail, 0x%08x\n", hr);
942 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
944 /* make the source have not zero size */
945 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
946 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
947 ret = WriteFile(hfile, prefix, sizeof(prefix), &len, NULL );
948 ok(ret && len == sizeof(prefix), "WriteFile error %d\n", GetLastError());
949 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
951 /* get the file time and change it to prove the difference */
952 ret = GetFileTime(hfile, NULL, NULL, &ft1);
953 ok(ret, "GetFileTime error %d\n", GetLastError());
954 ft1.dwLowDateTime -= 600000000; /* 60 second */
955 ret = SetFileTime(hfile, NULL, NULL, &ft1);
956 ok(ret, "SetFileTime error %d\n", GetLastError());
957 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
958 CloseHandle(hfile);
960 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
961 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
963 params.dwSize = sizeof(params);
964 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
966 SetLastError(0xdeadbeef);
967 hr = pCopyFile2(source, dest, &params);
968 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
969 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
971 params.dwSize = sizeof(params);
972 params.dwCopyFlags = 0;
973 hr = pCopyFile2(source, dest, &params);
974 ok(ret, "CopyFile2: error 0x%08x\n", hr);
976 /* copying from a read-locked source fails */
977 hfile = CreateFileW(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
978 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
980 params.dwSize = sizeof(params);
981 params.dwCopyFlags = 0;
982 SetLastError(0xdeadbeef);
983 hr = pCopyFile2(source, dest, &params);
984 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
985 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
987 /* in addition, the source is opened before the destination */
988 params.dwSize = sizeof(params);
989 params.dwCopyFlags = 0;
990 SetLastError(0xdeadbeef);
991 hr = pCopyFile2(doesntexistW, dest, &params);
992 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "got 0x%08x\n", hr);
993 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "CopyFile2: last error %d\n", GetLastError());
994 CloseHandle(hfile);
996 /* copying from a r+w opened, r shared source succeeds */
997 hfile = CreateFileW(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
998 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
1000 params.dwSize = sizeof(params);
1001 params.dwCopyFlags = 0;
1002 hr = pCopyFile2(source, dest, &params);
1003 ok(hr == S_OK, "failed 0x%08x\n", hr);
1004 CloseHandle(hfile);
1006 /* copying from a delete-locked source mostly succeeds */
1007 hfile = CreateFileW(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1008 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
1010 params.dwSize = sizeof(params);
1011 params.dwCopyFlags = 0;
1012 hr = pCopyFile2(source, dest, &params);
1013 ok(hr == S_OK, "failed 0x%08x\n", hr);
1014 CloseHandle(hfile);
1016 /* copying to a write-locked destination fails */
1017 hfile = CreateFileW(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1018 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1020 params.dwSize = sizeof(params);
1021 params.dwCopyFlags = 0;
1022 SetLastError(0xdeadbeef);
1023 hr = pCopyFile2(source, dest, FALSE);
1024 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1025 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1026 CloseHandle(hfile);
1028 /* copying to a r+w opened, w shared destination mostly succeeds */
1029 hfile = CreateFileW(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1030 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1032 params.dwSize = sizeof(params);
1033 params.dwCopyFlags = 0;
1034 hr = pCopyFile2(source, dest, FALSE);
1035 ok(hr == S_OK, "got 0x%08x\n", hr);
1036 CloseHandle(hfile);
1038 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
1039 hfile = CreateFileW(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
1040 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1042 params.dwSize = sizeof(params);
1043 params.dwCopyFlags = 0;
1044 SetLastError(0xdeadbeef);
1045 hr = pCopyFile2(source, dest, &params);
1046 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1047 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1048 CloseHandle(hfile);
1050 /* copy to a file that's opened the way Wine opens the source */
1051 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1052 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1054 params.dwSize = sizeof(params);
1055 params.dwCopyFlags = 0;
1056 hr = pCopyFile2(source, dest, &params);
1057 ok(hr == S_OK, "got 0x%08x\n", hr);
1058 CloseHandle(hfile);
1060 /* make sure that destination has correct size */
1061 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1062 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1063 ret = GetFileSize(hfile, NULL);
1064 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1066 /* make sure that destination has the same filetime */
1067 ret = GetFileTime(hfile, NULL, NULL, &ft2);
1068 ok(ret, "GetFileTime error %d\n", GetLastError());
1069 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
1071 params.dwSize = sizeof(params);
1072 params.dwCopyFlags = 0;
1073 SetLastError(0xdeadbeef);
1074 hr = pCopyFile2(source, dest, &params);
1075 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1076 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1078 /* make sure that destination still has correct size */
1079 ret = GetFileSize(hfile, NULL);
1080 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1081 ret = ReadFile(hfile, buf, sizeof(buf), &len, NULL);
1082 ok(ret && len == sizeof(prefix), "ReadFile: error %d\n", GetLastError());
1083 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
1085 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
1086 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1087 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1089 params.dwSize = sizeof(params);
1090 params.dwCopyFlags = 0;
1091 SetLastError(0xdeadbeef);
1092 hr = pCopyFile2(source, dest, &params);
1093 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1094 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1096 CloseHandle(hmapfile);
1097 CloseHandle(hfile);
1099 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1100 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1102 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
1103 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1104 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1106 params.dwSize = sizeof(params);
1107 params.dwCopyFlags = 0;
1108 hr = pCopyFile2(source, dest, &params);
1109 ok(hr == HRESULT_FROM_WIN32(ERROR_USER_MAPPED_FILE), "CopyFile2: unexpected error 0x%08x\n", hr);
1110 ok(GetLastError() == ERROR_USER_MAPPED_FILE, "CopyFile2: last error %d\n", GetLastError());
1112 CloseHandle(hmapfile);
1113 CloseHandle(hfile);
1115 DeleteFileW(source);
1116 DeleteFileW(dest);
1119 static DWORD WINAPI copy_progress_cb(LARGE_INTEGER total_size, LARGE_INTEGER total_transferred,
1120 LARGE_INTEGER stream_size, LARGE_INTEGER stream_transferred,
1121 DWORD stream, DWORD reason, HANDLE source, HANDLE dest, LPVOID userdata)
1123 ok(reason == CALLBACK_STREAM_SWITCH, "expected CALLBACK_STREAM_SWITCH, got %u\n", reason);
1124 CloseHandle(userdata);
1125 return PROGRESS_CANCEL;
1128 static void test_CopyFileEx(void)
1130 char temp_path[MAX_PATH];
1131 char source[MAX_PATH], dest[MAX_PATH];
1132 static const char prefix[] = "pfx";
1133 HANDLE hfile;
1134 DWORD ret;
1135 BOOL retok;
1137 ret = GetTempPathA(MAX_PATH, temp_path);
1138 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1139 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1141 ret = GetTempFileNameA(temp_path, prefix, 0, source);
1142 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1144 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
1145 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1147 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1148 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1149 SetLastError(0xdeadbeef);
1150 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1151 todo_wine
1152 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1153 todo_wine
1154 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1155 ok(GetFileAttributesA(dest) != INVALID_FILE_ATTRIBUTES, "file was deleted\n");
1157 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1158 NULL, OPEN_EXISTING, 0, 0);
1159 todo_wine
1160 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1161 SetLastError(0xdeadbeef);
1162 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1163 todo_wine
1164 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1165 todo_wine
1166 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1167 todo_wine
1168 ok(GetFileAttributesA(dest) == INVALID_FILE_ATTRIBUTES, "file was not deleted\n");
1170 ret = DeleteFileA(source);
1171 ok(ret, "DeleteFileA failed with error %d\n", GetLastError());
1172 ret = DeleteFileA(dest);
1173 ok(!ret, "DeleteFileA unexpectedly succeeded\n");
1177 * Debugging routine to dump a buffer in a hexdump-like fashion.
1179 static void dumpmem(unsigned char *mem, int len)
1181 int x = 0;
1182 char hex[49], *p;
1183 char txt[17], *c;
1185 while (x < len)
1187 p = hex;
1188 c = txt;
1189 do {
1190 p += sprintf(p, "%02x ", mem[x]);
1191 *c++ = (mem[x] >= 32 && mem[x] <= 127) ? mem[x] : '.';
1192 } while (++x % 16 && x < len);
1193 *c = '\0';
1194 trace("%04x: %-48s- %s\n", x, hex, txt);
1198 static void test_CreateFileA(void)
1200 HANDLE hFile;
1201 char temp_path[MAX_PATH], dirname[MAX_PATH];
1202 char filename[MAX_PATH];
1203 static const char prefix[] = "pfx";
1204 char windowsdir[MAX_PATH];
1205 char Volume_1[MAX_PATH];
1206 unsigned char buffer[512];
1207 char directory[] = "removeme";
1208 static const char nt_drive[] = "\\\\?\\A:";
1209 DWORD i, ret, len;
1210 static const struct test_list p[] =
1212 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
1213 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
1214 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
1215 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
1216 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
1217 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
1218 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
1219 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
1220 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
1221 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
1222 {"c:c:\\windows", ERROR_INVALID_NAME, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* invalid path */
1223 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
1224 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
1225 {NULL, 0, -1, 0, FALSE}
1227 BY_HANDLE_FILE_INFORMATION Finfo;
1228 WCHAR curdir[MAX_PATH];
1230 ret = GetTempPathA(MAX_PATH, temp_path);
1231 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1232 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1234 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1235 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1237 SetLastError(0xdeadbeef);
1238 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
1239 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1240 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1241 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1243 SetLastError(0xdeadbeef);
1244 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1245 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1246 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1247 "hFile %p, last error %u\n", hFile, GetLastError());
1249 CloseHandle(hFile);
1251 SetLastError(0xdeadbeef);
1252 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1253 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1254 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1255 "hFile %p, last error %u\n", hFile, GetLastError());
1257 CloseHandle(hFile);
1259 ret = DeleteFileA(filename);
1260 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1262 SetLastError(0xdeadbeef);
1263 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1264 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1265 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1266 "hFile %p, last error %u\n", hFile, GetLastError());
1268 CloseHandle(hFile);
1270 ret = DeleteFileA(filename);
1271 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1273 SetLastError(0xdeadbeef);
1274 hFile = CreateFileA("c:\\*.*", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1275 ok(hFile == INVALID_HANDLE_VALUE, "hFile should have been INVALID_HANDLE_VALUE\n");
1276 ok(GetLastError() == ERROR_INVALID_NAME ||
1277 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
1278 "LastError should have been ERROR_INVALID_NAME or ERROR_FILE_NOT_FOUND but got %u\n", GetLastError());
1280 /* get windows drive letter */
1281 ret = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1282 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1283 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1285 /* test error return codes from CreateFile for some cases */
1286 ret = GetTempPathA(MAX_PATH, temp_path);
1287 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1288 strcpy(dirname, temp_path);
1289 strcat(dirname, directory);
1290 ret = CreateDirectoryA(dirname, NULL);
1291 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
1292 /* set current drive & directory to known location */
1293 GetCurrentDirectoryW( MAX_PATH, curdir);
1294 SetCurrentDirectoryA( temp_path );
1295 i = 0;
1296 while (p[i].file)
1298 filename[0] = 0;
1299 /* update the drive id in the table entry with the current one */
1300 if (p[i].file[1] == ':')
1302 strcpy(filename, p[i].file);
1303 filename[0] = windowsdir[0];
1305 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
1307 strcpy(filename, p[i].file);
1308 filename[4] = windowsdir[0];
1310 else
1312 /* prefix the table entry with the current temp directory */
1313 strcpy(filename, temp_path);
1314 strcat(filename, p[i].file);
1316 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1317 FILE_SHARE_READ | FILE_SHARE_WRITE,
1318 NULL, OPEN_EXISTING,
1319 p[i].options, NULL );
1320 /* if we get ACCESS_DENIED when we do not expect it, assume
1321 * no access to the volume
1323 if (hFile == INVALID_HANDLE_VALUE &&
1324 GetLastError() == ERROR_ACCESS_DENIED &&
1325 p[i].err != ERROR_ACCESS_DENIED)
1327 if (p[i].todo_flag)
1328 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
1329 else
1330 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
1332 /* otherwise validate results with expectations */
1333 else
1335 todo_wine_if (p[i].todo_flag)
1336 ok((hFile == INVALID_HANDLE_VALUE &&
1337 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
1338 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
1339 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
1340 filename, hFile, GetLastError(), p[i].err);
1342 if (hFile != INVALID_HANDLE_VALUE)
1343 CloseHandle( hFile );
1344 i++;
1346 ret = RemoveDirectoryA(dirname);
1347 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
1348 SetCurrentDirectoryW(curdir);
1350 /* test opening directory as a directory */
1351 hFile = CreateFileA( temp_path, GENERIC_READ,
1352 FILE_SHARE_READ,
1353 NULL,
1354 OPEN_EXISTING,
1355 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1356 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
1358 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
1359 "CreateFileA did not work, last error %u on volume <%s>\n",
1360 GetLastError(), temp_path );
1362 if (hFile != INVALID_HANDLE_VALUE)
1364 ret = GetFileInformationByHandle( hFile, &Finfo );
1365 if (ret)
1367 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
1368 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
1369 temp_path, Finfo.dwFileAttributes);
1371 CloseHandle( hFile );
1374 else
1375 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
1378 /* *** Test opening volumes/devices using drive letter *** */
1380 /* test using drive letter in non-rewrite format without trailing \ */
1381 /* this should work */
1382 strcpy(filename, nt_drive);
1383 filename[4] = windowsdir[0];
1384 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1385 FILE_SHARE_READ | FILE_SHARE_WRITE,
1386 NULL, OPEN_EXISTING,
1387 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1388 if (hFile != INVALID_HANDLE_VALUE ||
1389 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
1391 /* if we have adm rights to volume, then try rest of tests */
1392 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1393 filename, GetLastError());
1394 if (hFile != INVALID_HANDLE_VALUE)
1396 /* if we opened the volume/device, try to read it. Since it */
1397 /* opened, we should be able to read it. We don't care about*/
1398 /* what the data is at this time. */
1399 len = 512;
1400 ret = ReadFile( hFile, buffer, len, &len, NULL );
1401 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1402 GetLastError(), ret, filename);
1403 if (ret)
1405 trace("buffer is\n");
1406 dumpmem(buffer, 64);
1408 CloseHandle( hFile );
1411 /* test using drive letter with trailing \ and in non-rewrite */
1412 /* this should not work */
1413 strcpy(filename, nt_drive);
1414 filename[4] = windowsdir[0];
1415 strcat( filename, "\\" );
1416 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1417 FILE_SHARE_READ | FILE_SHARE_WRITE,
1418 NULL, OPEN_EXISTING,
1419 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1420 todo_wine
1421 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1422 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1423 filename, GetLastError());
1424 if (hFile != INVALID_HANDLE_VALUE)
1425 CloseHandle( hFile );
1427 /* test using temp path with trailing \ and in non-rewrite as dir */
1428 /* this should work */
1429 strcpy(filename, nt_drive);
1430 filename[4] = 0;
1431 strcat( filename, temp_path );
1432 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1433 FILE_SHARE_READ | FILE_SHARE_WRITE,
1434 NULL, OPEN_EXISTING,
1435 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1436 ok(hFile != INVALID_HANDLE_VALUE,
1437 "CreateFileA should have worked on %s, but got %u\n",
1438 filename, GetLastError());
1439 if (hFile != INVALID_HANDLE_VALUE)
1440 CloseHandle( hFile );
1442 /* test using drive letter without trailing \ and in device ns */
1443 /* this should work */
1444 strcpy(filename, nt_drive);
1445 filename[4] = windowsdir[0];
1446 filename[2] = '.';
1447 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1448 FILE_SHARE_READ | FILE_SHARE_WRITE,
1449 NULL, OPEN_EXISTING,
1450 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1451 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1452 filename, GetLastError());
1453 if (hFile != INVALID_HANDLE_VALUE)
1454 CloseHandle( hFile );
1456 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
1457 else if (GetLastError() == ERROR_BAD_NETPATH)
1458 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
1459 else
1460 skip("Do not have authority to access volumes. Tests skipped\n");
1463 /* *** Test opening volumes/devices using GUID *** */
1465 if (pGetVolumeNameForVolumeMountPointA)
1467 strcpy(filename, "c:\\");
1468 filename[0] = windowsdir[0];
1469 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
1470 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
1471 if (ret)
1473 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
1475 /* test the result of opening a unique volume name (GUID)
1476 * with the trailing \
1477 * this should error out
1479 strcpy(filename, Volume_1);
1480 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1481 FILE_SHARE_READ | FILE_SHARE_WRITE,
1482 NULL, OPEN_EXISTING,
1483 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1484 todo_wine
1485 ok(hFile == INVALID_HANDLE_VALUE,
1486 "CreateFileA should not have opened %s, hFile %p\n",
1487 filename, hFile);
1488 todo_wine
1489 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1490 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1491 filename, GetLastError());
1492 if (hFile != INVALID_HANDLE_VALUE)
1493 CloseHandle( hFile );
1495 /* test the result of opening a unique volume name (GUID)
1496 * with the temp path string as dir
1497 * this should work
1499 strcpy(filename, Volume_1);
1500 strcat(filename, temp_path+3);
1501 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1502 FILE_SHARE_READ | FILE_SHARE_WRITE,
1503 NULL, OPEN_EXISTING,
1504 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1505 todo_wine
1506 ok(hFile != INVALID_HANDLE_VALUE,
1507 "CreateFileA should have opened %s, but got %u\n",
1508 filename, GetLastError());
1509 if (hFile != INVALID_HANDLE_VALUE)
1510 CloseHandle( hFile );
1512 /* test the result of opening a unique volume name (GUID)
1513 * without the trailing \ and in device namespace
1514 * this should work
1516 strcpy(filename, Volume_1);
1517 filename[2] = '.';
1518 filename[48] = 0;
1519 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1520 FILE_SHARE_READ | FILE_SHARE_WRITE,
1521 NULL, OPEN_EXISTING,
1522 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1523 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1525 /* if we have adm rights to volume, then try rest of tests */
1526 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1527 filename, GetLastError());
1528 if (hFile != INVALID_HANDLE_VALUE)
1530 /* if we opened the volume/device, try to read it. Since it */
1531 /* opened, we should be able to read it. We don't care about*/
1532 /* what the data is at this time. */
1533 len = 512;
1534 ret = ReadFile( hFile, buffer, len, &len, NULL );
1535 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1536 GetLastError(), ret, filename);
1537 if (ret)
1539 trace("buffer is\n");
1540 dumpmem(buffer, 64);
1542 CloseHandle( hFile );
1545 else
1546 skip("Do not have authority to access volumes. Tests skipped\n");
1548 else
1549 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1551 else
1552 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1555 static void test_CreateFileW(void)
1557 HANDLE hFile;
1558 WCHAR temp_path[MAX_PATH];
1559 WCHAR filename[MAX_PATH];
1560 static const WCHAR emptyW[]={'\0'};
1561 static const WCHAR prefix[] = {'p','f','x',0};
1562 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1563 DWORD ret;
1565 ret = GetTempPathW(MAX_PATH, temp_path);
1566 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1568 win_skip("GetTempPathW is not available\n");
1569 return;
1571 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1572 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1574 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1575 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1577 SetLastError(0xdeadbeef);
1578 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1579 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1580 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1581 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1583 SetLastError(0xdeadbeef);
1584 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1585 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1586 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1587 "hFile %p, last error %u\n", hFile, GetLastError());
1589 CloseHandle(hFile);
1591 SetLastError(0xdeadbeef);
1592 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1593 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1594 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1595 "hFile %p, last error %u\n", hFile, GetLastError());
1597 CloseHandle(hFile);
1599 ret = DeleteFileW(filename);
1600 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1602 SetLastError(0xdeadbeef);
1603 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1604 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1605 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1606 "hFile %p, last error %u\n", hFile, GetLastError());
1608 CloseHandle(hFile);
1610 ret = DeleteFileW(filename);
1611 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1613 if (0)
1615 /* this crashes on NT4.0 */
1616 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1617 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1618 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1619 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1622 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1623 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1624 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1625 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1627 /* test the result of opening a nonexistent driver name */
1628 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1629 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1630 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1631 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1633 ret = CreateDirectoryW(filename, NULL);
1634 ok(ret == TRUE, "couldn't create temporary directory\n");
1635 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1636 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1637 ok(hFile != INVALID_HANDLE_VALUE,
1638 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1639 CloseHandle(hFile);
1640 ret = RemoveDirectoryW(filename);
1641 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1644 static void test_CreateFile2(void)
1646 HANDLE hFile;
1647 WCHAR temp_path[MAX_PATH];
1648 WCHAR filename[MAX_PATH];
1649 CREATEFILE2_EXTENDED_PARAMETERS exparams;
1650 static const WCHAR emptyW[]={'\0'};
1651 static const WCHAR prefix[] = {'p','f','x',0};
1652 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1653 DWORD ret;
1655 if (!pCreateFile2)
1657 win_skip("CreateFile2 is missing\n");
1658 return;
1661 ret = GetTempPathW(MAX_PATH, temp_path);
1662 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1663 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1665 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1666 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1668 SetLastError(0xdeadbeef);
1669 exparams.dwSize = sizeof(exparams);
1670 exparams.dwFileAttributes = FILE_FLAG_RANDOM_ACCESS;
1671 exparams.dwFileFlags = 0;
1672 exparams.dwSecurityQosFlags = 0;
1673 exparams.lpSecurityAttributes = NULL;
1674 exparams.hTemplateFile = 0;
1675 hFile = pCreateFile2(filename, GENERIC_READ, 0, CREATE_NEW, &exparams);
1676 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1677 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1679 SetLastError(0xdeadbeef);
1680 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, CREATE_ALWAYS, &exparams);
1681 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1682 "hFile %p, last error %u\n", hFile, GetLastError());
1683 CloseHandle(hFile);
1685 SetLastError(0xdeadbeef);
1686 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1687 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1688 "hFile %p, last error %u\n", hFile, GetLastError());
1689 CloseHandle(hFile);
1691 ret = DeleteFileW(filename);
1692 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1694 SetLastError(0xdeadbeef);
1695 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1696 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1697 "hFile %p, last error %u\n", hFile, GetLastError());
1698 CloseHandle(hFile);
1700 ret = DeleteFileW(filename);
1701 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1703 hFile = pCreateFile2(emptyW, GENERIC_READ, 0, CREATE_NEW, &exparams);
1704 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1705 "CreateFile2(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1707 /* test the result of opening a nonexistent driver name */
1708 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1709 hFile = pCreateFile2(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &exparams);
1710 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1711 "CreateFile2 on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1713 ret = CreateDirectoryW(filename, NULL);
1714 ok(ret == TRUE, "couldn't create temporary directory\n");
1715 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS;
1716 hFile = pCreateFile2(filename, GENERIC_READ | GENERIC_WRITE, 0, OPEN_ALWAYS, &exparams);
1717 todo_wine
1718 ok(hFile == INVALID_HANDLE_VALUE,
1719 "expected CreateFile2 to fail on existing directory, error: %d\n", GetLastError());
1720 CloseHandle(hFile);
1721 ret = RemoveDirectoryW(filename);
1722 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1725 static void test_GetTempFileNameA(void)
1727 UINT result;
1728 char out[MAX_PATH];
1729 char expected[MAX_PATH + 10];
1730 char windowsdir[MAX_PATH + 10];
1731 char windowsdrive[3];
1733 result = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1734 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1735 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1737 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1738 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1740 strcat(windowsdir, "\\");
1743 windowsdrive[0] = windowsdir[0];
1744 windowsdrive[1] = windowsdir[1];
1745 windowsdrive[2] = '\0';
1747 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1748 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1749 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1750 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1751 windowsdrive[0], out);
1753 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1754 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1755 expected[0] = '\0';
1756 strcat(expected, windowsdir);
1757 strcat(expected, "abc2.tmp");
1758 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1759 out, expected);
1762 static void test_DeleteFileA( void )
1764 BOOL ret;
1765 char temp_path[MAX_PATH], temp_file[MAX_PATH];
1766 HANDLE hfile;
1768 ret = DeleteFileA(NULL);
1769 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1770 GetLastError() == ERROR_PATH_NOT_FOUND),
1771 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1773 ret = DeleteFileA("");
1774 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1775 GetLastError() == ERROR_BAD_PATHNAME),
1776 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1778 ret = DeleteFileA("nul");
1779 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1780 GetLastError() == ERROR_INVALID_PARAMETER ||
1781 GetLastError() == ERROR_ACCESS_DENIED ||
1782 GetLastError() == ERROR_INVALID_FUNCTION),
1783 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1785 ret = DeleteFileA("nonexist.txt");
1786 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "DeleteFileA(\"nonexist.txt\") returned ret=%d error=%d\n",ret,GetLastError());
1788 GetTempPathA(MAX_PATH, temp_path);
1789 GetTempFileNameA(temp_path, "tst", 0, temp_file);
1791 SetLastError(0xdeadbeef);
1792 hfile = CreateFileA(temp_file, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1793 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1795 SetLastError(0xdeadbeef);
1796 ret = DeleteFileA(temp_file);
1797 ok(ret, "DeleteFile error %d\n", GetLastError());
1799 SetLastError(0xdeadbeef);
1800 ret = CloseHandle(hfile);
1801 ok(ret, "CloseHandle error %d\n", GetLastError());
1802 ret = DeleteFileA(temp_file);
1803 ok(!ret, "DeleteFile should fail\n");
1805 SetLastError(0xdeadbeef);
1806 ret = CreateDirectoryA("testdir", NULL);
1807 ok(ret, "CreateDirectory failed, got err %d\n", GetLastError());
1808 ret = DeleteFileA("testdir");
1809 ok(!ret && GetLastError() == ERROR_ACCESS_DENIED,
1810 "Expected ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
1811 ret = RemoveDirectoryA("testdir");
1812 ok(ret, "Remove a directory failed, got error %d\n", GetLastError());
1815 static void test_DeleteFileW( void )
1817 BOOL ret;
1818 WCHAR pathW[MAX_PATH];
1819 WCHAR pathsubW[MAX_PATH];
1820 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1821 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1822 static const WCHAR emptyW[]={'\0'};
1824 ret = DeleteFileW(NULL);
1825 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1827 win_skip("DeleteFileW is not available\n");
1828 return;
1830 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1831 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1833 ret = DeleteFileW(emptyW);
1834 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1835 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1837 /* test DeleteFile on empty directory */
1838 ret = GetTempPathW(MAX_PATH, pathW);
1839 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1841 ok(0, "MAX_PATH exceeded in constructing paths\n");
1842 return;
1844 lstrcatW(pathW, dirW);
1845 lstrcpyW(pathsubW, pathW);
1846 lstrcatW(pathsubW, subdirW);
1847 ret = CreateDirectoryW(pathW, NULL);
1848 ok(ret == TRUE, "couldn't create directory deletefile\n");
1849 ret = DeleteFileW(pathW);
1850 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1851 ret = RemoveDirectoryW(pathW);
1852 ok(ret == TRUE, "expected to remove directory deletefile\n");
1854 /* test DeleteFile on non-empty directory */
1855 ret = CreateDirectoryW(pathW, NULL);
1856 ok(ret == TRUE, "couldn't create directory deletefile\n");
1857 ret = CreateDirectoryW(pathsubW, NULL);
1858 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1859 ret = DeleteFileW(pathW);
1860 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1861 ret = RemoveDirectoryW(pathsubW);
1862 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1863 ret = RemoveDirectoryW(pathW);
1864 ok(ret == TRUE, "expected to remove directory deletefile\n");
1867 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1869 static void test_MoveFileA(void)
1871 char tempdir[MAX_PATH];
1872 char source[MAX_PATH], dest[MAX_PATH];
1873 static const char prefix[] = "pfx";
1874 HANDLE hfile;
1875 HANDLE hmapfile;
1876 DWORD ret;
1877 BOOL retok;
1879 ret = GetTempPathA(MAX_PATH, tempdir);
1880 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1881 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1883 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1884 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1886 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1887 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1889 ret = MoveFileA(source, dest);
1890 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1891 "MoveFileA: unexpected error %d\n", GetLastError());
1893 ret = DeleteFileA(dest);
1894 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1896 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1897 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1899 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1900 ok( retok && ret == sizeof(prefix),
1901 "WriteFile error %d\n", GetLastError());
1903 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1904 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1906 ret = MoveFileA(source, dest);
1907 todo_wine {
1908 ok(!ret, "MoveFileA: expected failure\n");
1909 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1910 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1911 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1914 CloseHandle(hmapfile);
1915 CloseHandle(hfile);
1917 /* if MoveFile succeeded, move back to dest */
1918 if (ret) MoveFileA(dest, source);
1920 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1921 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1923 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1924 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1926 ret = MoveFileA(source, dest);
1927 todo_wine {
1928 ok(!ret, "MoveFileA: expected failure\n");
1929 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1930 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1931 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1934 CloseHandle(hmapfile);
1935 CloseHandle(hfile);
1937 /* if MoveFile succeeded, move back to dest */
1938 if (ret) MoveFileA(dest, source);
1940 ret = MoveFileA(source, dest);
1941 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1943 lstrcatA(tempdir, "Remove Me");
1944 ret = CreateDirectoryA(tempdir, NULL);
1945 ok(ret == TRUE, "CreateDirectoryA failed\n");
1947 lstrcpyA(source, dest);
1948 lstrcpyA(dest, tempdir);
1949 lstrcatA(dest, "\\wild?.*");
1950 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1951 ret = MoveFileA(source, dest);
1952 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1953 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1954 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1955 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1956 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1958 WIN32_FIND_DATAA fd;
1959 char temppath[MAX_PATH];
1960 HANDLE hFind;
1962 lstrcpyA(temppath, tempdir);
1963 lstrcatA(temppath, "\\*.*");
1964 hFind = FindFirstFileA(temppath, &fd);
1965 if (INVALID_HANDLE_VALUE != hFind)
1967 LPSTR lpName;
1970 lpName = fd.cAlternateFileName;
1971 if (!lpName[0])
1972 lpName = fd.cFileName;
1973 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1975 while (FindNextFileA(hFind, &fd));
1976 FindClose(hFind);
1979 ret = DeleteFileA(source);
1980 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1981 ret = DeleteFileA(dest);
1982 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1983 ret = RemoveDirectoryA(tempdir);
1984 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1987 static void test_MoveFileW(void)
1989 WCHAR temp_path[MAX_PATH];
1990 WCHAR source[MAX_PATH], dest[MAX_PATH];
1991 static const WCHAR prefix[] = {'p','f','x',0};
1992 DWORD ret;
1994 ret = GetTempPathW(MAX_PATH, temp_path);
1995 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1997 win_skip("GetTempPathW is not available\n");
1998 return;
2000 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2001 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2003 ret = GetTempFileNameW(temp_path, prefix, 0, source);
2004 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
2006 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
2007 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
2009 ret = MoveFileW(source, dest);
2010 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
2011 "CopyFileW: unexpected error %d\n", GetLastError());
2013 ret = DeleteFileW(source);
2014 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2015 ret = DeleteFileW(dest);
2016 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2019 #define PATTERN_OFFSET 0x10
2021 static void test_offset_in_overlapped_structure(void)
2023 HANDLE hFile;
2024 OVERLAPPED ov;
2025 DWORD done, offset;
2026 BOOL rc;
2027 BYTE buf[256], pattern[] = "TeSt";
2028 UINT i;
2029 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
2030 BOOL ret;
2032 ret =GetTempPathA(MAX_PATH, temp_path);
2033 ok( ret, "GetTempPathA error %d\n", GetLastError());
2034 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
2035 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
2037 /*** Write File *****************************************************/
2039 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
2040 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2042 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
2043 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
2044 ok( ret, "WriteFile error %d\n", GetLastError());
2045 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
2047 memset(&ov, 0, sizeof(ov));
2048 S(U(ov)).Offset = PATTERN_OFFSET;
2049 S(U(ov)).OffsetHigh = 0;
2050 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2051 /* Win 9x does not support the overlapped I/O on files */
2052 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2053 ok(rc, "WriteFile error %d\n", GetLastError());
2054 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2055 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2056 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2058 S(U(ov)).Offset = sizeof(buf) * 2;
2059 S(U(ov)).OffsetHigh = 0;
2060 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2061 ok( ret, "WriteFile error %d\n", GetLastError());
2062 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2063 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2064 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
2067 CloseHandle(hFile);
2069 /*** Read File *****************************************************/
2071 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
2072 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2074 memset(buf, 0, sizeof(buf));
2075 memset(&ov, 0, sizeof(ov));
2076 S(U(ov)).Offset = PATTERN_OFFSET;
2077 S(U(ov)).OffsetHigh = 0;
2078 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
2079 /* Win 9x does not support the overlapped I/O on files */
2080 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2081 ok(rc, "ReadFile error %d\n", GetLastError());
2082 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
2083 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2084 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2085 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
2088 CloseHandle(hFile);
2090 ret = DeleteFileA(temp_fname);
2091 ok( ret, "DeleteFileA error %d\n", GetLastError());
2094 static void test_LockFile(void)
2096 HANDLE handle, handle2;
2097 DWORD written;
2098 OVERLAPPED overlapped;
2099 int limited_LockFile;
2100 int limited_UnLockFile;
2101 BOOL ret;
2103 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2104 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2105 CREATE_ALWAYS, 0, 0 );
2106 if (handle == INVALID_HANDLE_VALUE)
2108 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2109 return;
2111 handle2 = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2112 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2113 OPEN_EXISTING, 0, 0 );
2114 if (handle2 == INVALID_HANDLE_VALUE)
2116 ok( 0, "couldn't open file \"%s\" (err=%d)\n", filename, GetLastError() );
2117 goto cleanup;
2119 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
2121 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
2122 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
2124 limited_UnLockFile = 0;
2125 if (UnlockFile( handle, 0, 0, 0, 0 ))
2127 limited_UnLockFile = 1;
2130 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
2131 /* overlapping locks must fail */
2132 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
2133 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
2134 /* non-overlapping locks must succeed */
2135 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
2137 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
2138 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
2139 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
2140 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
2142 S(U(overlapped)).Offset = 100;
2143 S(U(overlapped)).OffsetHigh = 0;
2144 overlapped.hEvent = 0;
2146 /* Test for broken LockFileEx a la Windows 95 OSR2. */
2147 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
2149 /* LockFileEx is probably OK, test it more. */
2150 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
2151 "LockFileEx 100,100 failed\n" );
2154 /* overlapping shared locks are OK */
2155 S(U(overlapped)).Offset = 150;
2156 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
2158 /* but exclusive is not */
2159 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
2160 0, 50, 0, &overlapped ),
2161 "LockFileEx exclusive 150,50 succeeded\n" );
2162 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
2163 { /* UnLockFile is capable. */
2164 S(U(overlapped)).Offset = 100;
2165 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
2166 "UnlockFileEx 150,100 again succeeded\n" );
2169 /* shared lock can overlap exclusive if handles are equal */
2170 S(U(overlapped)).Offset = 300;
2171 ok( LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0, &overlapped ),
2172 "LockFileEx exclusive 300,100 failed\n" );
2173 ok( !LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2174 "LockFileEx handle2 300,100 succeeded\n" );
2175 ret = LockFileEx( handle, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped );
2176 ok( ret, "LockFileEx 300,100 failed\n" );
2177 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2178 /* exclusive lock is removed first */
2179 ok( LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2180 "LockFileEx handle2 300,100 failed\n" );
2181 ok( UnlockFileEx( handle2, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2182 if (ret)
2183 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2185 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
2186 if (ret)
2188 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
2189 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
2190 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
2192 else /* win9x */
2193 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
2195 /* wrap-around lock should not do anything */
2196 /* (but still succeeds on NT4 so we don't check result) */
2197 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
2199 limited_LockFile = 0;
2200 if (!LockFile( handle, ~0, ~0, 1, 0 ))
2202 limited_LockFile = 1;
2205 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
2207 /* zero-byte lock */
2208 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
2209 if (!limited_LockFile) ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
2210 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
2211 if (!limited_LockFile) ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
2213 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
2214 ok( !UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 succeeded\n" );
2216 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
2218 CloseHandle( handle2 );
2219 cleanup:
2220 CloseHandle( handle );
2221 DeleteFileA( filename );
2224 static BOOL create_fake_dll( LPCSTR filename )
2226 IMAGE_DOS_HEADER *dos;
2227 IMAGE_NT_HEADERS *nt;
2228 IMAGE_SECTION_HEADER *sec;
2229 BYTE *buffer;
2230 DWORD lfanew = sizeof(*dos);
2231 DWORD size = lfanew + sizeof(*nt) + sizeof(*sec);
2232 DWORD written;
2233 BOOL ret;
2235 HANDLE file = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2236 if (file == INVALID_HANDLE_VALUE) return FALSE;
2238 buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
2240 dos = (IMAGE_DOS_HEADER *)buffer;
2241 dos->e_magic = IMAGE_DOS_SIGNATURE;
2242 dos->e_cblp = sizeof(*dos);
2243 dos->e_cp = 1;
2244 dos->e_cparhdr = lfanew / 16;
2245 dos->e_minalloc = 0;
2246 dos->e_maxalloc = 0xffff;
2247 dos->e_ss = 0x0000;
2248 dos->e_sp = 0x00b8;
2249 dos->e_lfarlc = lfanew;
2250 dos->e_lfanew = lfanew;
2252 nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
2253 nt->Signature = IMAGE_NT_SIGNATURE;
2254 #if defined __i386__
2255 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
2256 #elif defined __x86_64__
2257 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
2258 #elif defined __powerpc__
2259 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
2260 #elif defined __arm__
2261 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT;
2262 #elif defined __aarch64__
2263 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
2264 #else
2265 # error You must specify the machine type
2266 #endif
2267 nt->FileHeader.NumberOfSections = 1;
2268 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2269 nt->FileHeader.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_EXECUTABLE_IMAGE;
2270 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
2271 nt->OptionalHeader.MajorLinkerVersion = 1;
2272 nt->OptionalHeader.MinorLinkerVersion = 0;
2273 nt->OptionalHeader.ImageBase = 0x10000000;
2274 nt->OptionalHeader.SectionAlignment = 0x1000;
2275 nt->OptionalHeader.FileAlignment = 0x1000;
2276 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
2277 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
2278 nt->OptionalHeader.MajorImageVersion = 1;
2279 nt->OptionalHeader.MinorImageVersion = 0;
2280 nt->OptionalHeader.MajorSubsystemVersion = 4;
2281 nt->OptionalHeader.MinorSubsystemVersion = 0;
2282 nt->OptionalHeader.SizeOfImage = 0x2000;
2283 nt->OptionalHeader.SizeOfHeaders = size;
2284 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
2285 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
2287 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
2288 memcpy( sec->Name, ".rodata", sizeof(".rodata") );
2289 sec->Misc.VirtualSize = 0x1000;
2290 sec->VirtualAddress = 0x1000;
2291 sec->SizeOfRawData = 0;
2292 sec->PointerToRawData = 0;
2293 sec->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
2295 ret = WriteFile( file, buffer, size, &written, NULL ) && written == size;
2296 HeapFree( GetProcessHeap(), 0, buffer );
2297 CloseHandle( file );
2298 return ret;
2301 static unsigned int map_file_access( unsigned int access )
2303 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2304 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2305 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2306 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2307 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2310 static BOOL is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
2312 access1 = map_file_access( access1 );
2313 access2 = map_file_access( access2 );
2314 access1 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2315 access2 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2317 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2318 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2320 if ((access1 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing2 & FILE_SHARE_READ)) return FALSE;
2321 if ((access1 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2322 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return FALSE;
2323 if ((access2 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing1 & FILE_SHARE_READ)) return FALSE;
2324 if ((access2 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing1 & FILE_SHARE_WRITE)) return FALSE;
2325 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return FALSE;
2326 return TRUE;
2329 static BOOL is_sharing_map_compatible( DWORD map_access, DWORD access2, DWORD sharing2 )
2331 if ((map_access == PAGE_READWRITE || map_access == PAGE_EXECUTE_READWRITE) &&
2332 !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2333 access2 = map_file_access( access2 );
2334 if ((map_access & SEC_IMAGE) && (access2 & FILE_WRITE_DATA)) return FALSE;
2335 return TRUE;
2338 static void test_file_sharing(void)
2340 struct mode { DWORD dw; const char* str; };
2341 #define M(x) {x, # x}
2342 static const struct mode access_modes[] =
2343 { M(0), M(GENERIC_READ), M(GENERIC_WRITE), M(GENERIC_READ|GENERIC_WRITE),
2344 M(DELETE), M(GENERIC_READ|DELETE), M(GENERIC_WRITE|DELETE), M(GENERIC_READ|GENERIC_WRITE|DELETE),
2345 M(GENERIC_EXECUTE), M(GENERIC_EXECUTE | DELETE),
2346 M(FILE_READ_DATA), M(FILE_WRITE_DATA), M(FILE_APPEND_DATA), M(FILE_READ_EA), M(FILE_WRITE_EA),
2347 M(FILE_READ_DATA | FILE_EXECUTE), M(FILE_WRITE_DATA | FILE_EXECUTE), M(FILE_APPEND_DATA | FILE_EXECUTE),
2348 M(FILE_READ_EA | FILE_EXECUTE), M(FILE_WRITE_EA | FILE_EXECUTE), M(FILE_EXECUTE),
2349 M(FILE_DELETE_CHILD), M(FILE_READ_ATTRIBUTES), M(FILE_WRITE_ATTRIBUTES) };
2350 static const struct mode sharing_modes[] =
2351 { M(0), M(FILE_SHARE_READ),
2352 M(FILE_SHARE_WRITE), M(FILE_SHARE_READ|FILE_SHARE_WRITE),
2353 M(FILE_SHARE_DELETE), M(FILE_SHARE_READ|FILE_SHARE_DELETE),
2354 M(FILE_SHARE_WRITE|FILE_SHARE_DELETE), M(FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE) };
2355 static const struct mode mapping_modes[] =
2356 { M(PAGE_READONLY), M(PAGE_WRITECOPY), M(PAGE_READWRITE), M(SEC_IMAGE | PAGE_WRITECOPY) };
2357 #undef M
2358 int a1, s1, a2, s2;
2359 int ret;
2360 HANDLE h, h2;
2362 /* make sure the file exists */
2363 if (!create_fake_dll( filename ))
2365 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
2366 return;
2369 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
2371 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
2373 SetLastError(0xdeadbeef);
2374 h = CreateFileA( filename, access_modes[a1].dw, sharing_modes[s1].dw,
2375 NULL, OPEN_EXISTING, 0, 0 );
2376 if (h == INVALID_HANDLE_VALUE)
2378 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2379 return;
2381 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
2383 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
2385 SetLastError(0xdeadbeef);
2386 h2 = CreateFileA( filename, access_modes[a2].dw, sharing_modes[s2].dw,
2387 NULL, OPEN_EXISTING, 0, 0 );
2388 ret = GetLastError();
2389 if (is_sharing_compatible( access_modes[a1].dw, sharing_modes[s1].dw,
2390 access_modes[a2].dw, sharing_modes[s2].dw ))
2392 ok( h2 != INVALID_HANDLE_VALUE,
2393 "open failed for modes %s / %s / %s / %s\n",
2394 access_modes[a1].str, sharing_modes[s1].str,
2395 access_modes[a2].str, sharing_modes[s2].str );
2396 ok( ret == 0, "wrong error code %d\n", ret );
2398 else
2400 ok( h2 == INVALID_HANDLE_VALUE,
2401 "open succeeded for modes %s / %s / %s / %s\n",
2402 access_modes[a1].str, sharing_modes[s1].str,
2403 access_modes[a2].str, sharing_modes[s2].str );
2404 ok( ret == ERROR_SHARING_VIOLATION,
2405 "wrong error code %d\n", ret );
2407 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2410 CloseHandle( h );
2414 for (a1 = 0; a1 < sizeof(mapping_modes)/sizeof(mapping_modes[0]); a1++)
2416 HANDLE m;
2418 create_fake_dll( filename );
2419 SetLastError(0xdeadbeef);
2420 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2421 if (h == INVALID_HANDLE_VALUE)
2423 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2424 return;
2426 m = CreateFileMappingA( h, NULL, mapping_modes[a1].dw, 0, 0, NULL );
2427 ok( m != 0, "failed to create mapping %s err %u\n", mapping_modes[a1].str, GetLastError() );
2428 CloseHandle( h );
2429 if (!m) continue;
2431 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
2433 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
2435 SetLastError(0xdeadbeef);
2436 h2 = CreateFileA( filename, access_modes[a2].dw, sharing_modes[s2].dw,
2437 NULL, OPEN_EXISTING, 0, 0 );
2439 ret = GetLastError();
2440 if (h2 == INVALID_HANDLE_VALUE)
2442 ok( !is_sharing_map_compatible(mapping_modes[a1].dw, access_modes[a2].dw, sharing_modes[s2].dw),
2443 "open failed for modes map %s / %s / %s\n",
2444 mapping_modes[a1].str, access_modes[a2].str, sharing_modes[s2].str );
2445 ok( ret == ERROR_SHARING_VIOLATION,
2446 "wrong error code %d\n", ret );
2448 else
2450 if (!is_sharing_map_compatible(mapping_modes[a1].dw, access_modes[a2].dw, sharing_modes[s2].dw))
2451 ok( broken(1), /* no checking on nt4 */
2452 "open succeeded for modes map %s / %s / %s\n",
2453 mapping_modes[a1].str, access_modes[a2].str, sharing_modes[s2].str );
2454 ok( ret == 0xdeadbeef /* Win9x */ ||
2455 ret == 0, /* XP */
2456 "wrong error code %d\n", ret );
2457 CloseHandle( h2 );
2462 /* try CREATE_ALWAYS over an existing mapping */
2463 SetLastError(0xdeadbeef);
2464 h2 = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
2465 NULL, CREATE_ALWAYS, 0, 0 );
2466 ret = GetLastError();
2467 if (mapping_modes[a1].dw & SEC_IMAGE)
2469 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %s\n", mapping_modes[a1].str );
2470 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %s\n", ret, mapping_modes[a1].str );
2472 else
2474 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %s\n", mapping_modes[a1].str );
2475 ok( ret == ERROR_USER_MAPPED_FILE, "wrong error code %d for %s\n", ret, mapping_modes[a1].str );
2477 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2479 /* try DELETE_ON_CLOSE over an existing mapping */
2480 SetLastError(0xdeadbeef);
2481 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2482 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 );
2483 ret = GetLastError();
2484 if (mapping_modes[a1].dw & SEC_IMAGE)
2486 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %s\n", mapping_modes[a1].str );
2487 ok( ret == ERROR_ACCESS_DENIED, "wrong error code %d for %s\n", ret, mapping_modes[a1].str );
2489 else
2491 ok( h2 != INVALID_HANDLE_VALUE, "open failed for map %s err %u\n", mapping_modes[a1].str, ret );
2493 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2495 CloseHandle( m );
2498 SetLastError(0xdeadbeef);
2499 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
2500 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2502 SetLastError(0xdeadbeef);
2503 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
2504 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
2505 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
2507 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
2508 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2510 CloseHandle(h);
2511 CloseHandle(h2);
2513 DeleteFileA( filename );
2516 static char get_windows_drive(void)
2518 char windowsdir[MAX_PATH];
2519 GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
2520 return windowsdir[0];
2523 static void test_FindFirstFileA(void)
2525 HANDLE handle;
2526 WIN32_FIND_DATAA data;
2527 int err;
2528 char buffer[5] = "C:\\";
2529 char buffer2[100];
2530 char nonexistent[MAX_PATH];
2532 /* try FindFirstFileA on "C:\" */
2533 buffer[0] = get_windows_drive();
2535 SetLastError( 0xdeadbeaf );
2536 handle = FindFirstFileA(buffer, &data);
2537 err = GetLastError();
2538 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
2539 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2541 /* try FindFirstFileA on "C:\*" */
2542 strcpy(buffer2, buffer);
2543 strcat(buffer2, "*");
2544 handle = FindFirstFileA(buffer2, &data);
2545 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2546 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2547 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
2548 if (FindNextFileA( handle, &data ))
2549 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2550 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
2551 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2553 /* try FindFirstFileA on windows dir */
2554 GetWindowsDirectoryA( buffer2, sizeof(buffer2) );
2555 strcat(buffer2, "\\*");
2556 handle = FindFirstFileA(buffer2, &data);
2557 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2558 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
2559 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
2560 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
2561 while (FindNextFileA( handle, &data ))
2562 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2563 "FindNextFile shouldn't return '%s'\n", data.cFileName );
2564 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2566 /* try FindFirstFileA on "C:\foo\" */
2567 SetLastError( 0xdeadbeaf );
2568 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
2570 char tmp[MAX_PATH];
2571 GetTempPathA( sizeof(tmp), tmp );
2572 GetTempFileNameA( tmp, "foo", 0, nonexistent );
2574 DeleteFileA( nonexistent );
2575 strcpy(buffer2, nonexistent);
2576 strcat(buffer2, "\\");
2577 handle = FindFirstFileA(buffer2, &data);
2578 err = GetLastError();
2579 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2580 todo_wine {
2581 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2584 /* try FindFirstFileA without trailing backslash */
2585 SetLastError( 0xdeadbeaf );
2586 strcpy(buffer2, nonexistent);
2587 handle = FindFirstFileA(buffer2, &data);
2588 err = GetLastError();
2589 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2590 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2592 /* try FindFirstFileA on "C:\foo\bar.txt" */
2593 SetLastError( 0xdeadbeaf );
2594 strcpy(buffer2, nonexistent);
2595 strcat(buffer2, "\\bar.txt");
2596 handle = FindFirstFileA(buffer2, &data);
2597 err = GetLastError();
2598 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2599 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2601 /* try FindFirstFileA on "C:\foo\*.*" */
2602 SetLastError( 0xdeadbeaf );
2603 strcpy(buffer2, nonexistent);
2604 strcat(buffer2, "\\*.*");
2605 handle = FindFirstFileA(buffer2, &data);
2606 err = GetLastError();
2607 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2608 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2610 /* try FindFirstFileA on "foo\bar.txt" */
2611 SetLastError( 0xdeadbeaf );
2612 strcpy(buffer2, nonexistent + 3);
2613 strcat(buffer2, "\\bar.txt");
2614 handle = FindFirstFileA(buffer2, &data);
2615 err = GetLastError();
2616 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2617 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2619 /* try FindFirstFileA on "c:\nul" */
2620 SetLastError( 0xdeadbeaf );
2621 strcpy(buffer2, buffer);
2622 strcat(buffer2, "nul");
2623 handle = FindFirstFileA(buffer2, &data);
2624 err = GetLastError();
2625 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2626 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
2627 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2628 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2629 "wrong attributes %x\n", data.dwFileAttributes );
2630 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2632 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2633 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2635 SetLastError( 0xdeadbeaf );
2636 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2637 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2638 ok( FindClose( handle ), "failed to close handle\n" );
2640 /* try FindFirstFileA on "lpt1" */
2641 SetLastError( 0xdeadbeaf );
2642 strcpy(buffer2, "lpt1");
2643 handle = FindFirstFileA(buffer2, &data);
2644 err = GetLastError();
2645 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2646 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
2647 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2648 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2649 "wrong attributes %x\n", data.dwFileAttributes );
2650 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2652 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2653 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2655 SetLastError( 0xdeadbeaf );
2656 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2657 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2658 ok( FindClose( handle ), "failed to close handle\n" );
2660 /* try FindFirstFileA on "c:\nul\*" */
2661 SetLastError( 0xdeadbeaf );
2662 strcpy(buffer2, buffer);
2663 strcat(buffer2, "nul\\*");
2664 handle = FindFirstFileA(buffer2, &data);
2665 err = GetLastError();
2666 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2667 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2669 /* try FindFirstFileA on "c:\nul*" */
2670 SetLastError( 0xdeadbeaf );
2671 strcpy(buffer2, buffer);
2672 strcat(buffer2, "nul*");
2673 handle = FindFirstFileA(buffer2, &data);
2674 err = GetLastError();
2675 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2676 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2678 /* try FindFirstFileA on "c:\foo\bar\nul" */
2679 SetLastError( 0xdeadbeaf );
2680 strcpy(buffer2, buffer);
2681 strcat(buffer2, "foo\\bar\\nul");
2682 handle = FindFirstFileA(buffer2, &data);
2683 err = GetLastError();
2684 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2685 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2687 /* try FindFirstFileA on "c:\foo\nul\bar" */
2688 SetLastError( 0xdeadbeaf );
2689 strcpy(buffer2, buffer);
2690 strcat(buffer2, "foo\\nul\\bar");
2691 handle = FindFirstFileA(buffer2, &data);
2692 err = GetLastError();
2693 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2694 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2697 static void test_FindNextFileA(void)
2699 HANDLE handle;
2700 WIN32_FIND_DATAA search_results;
2701 int err;
2702 char buffer[5] = "C:\\*";
2704 buffer[0] = get_windows_drive();
2705 handle = FindFirstFileA(buffer,&search_results);
2706 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
2707 while (FindNextFileA(handle, &search_results))
2709 /* get to the end of the files */
2711 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
2712 err = GetLastError();
2713 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
2716 static void test_FindFirstFileExA(FINDEX_INFO_LEVELS level, FINDEX_SEARCH_OPS search_ops, DWORD flags)
2718 WIN32_FIND_DATAA search_results;
2719 HANDLE handle;
2720 BOOL ret;
2722 if (!pFindFirstFileExA)
2724 win_skip("FindFirstFileExA() is missing\n");
2725 return;
2728 trace("Running FindFirstFileExA tests with level=%d, search_ops=%d, flags=%u\n",
2729 level, search_ops, flags);
2731 CreateDirectoryA("test-dir", NULL);
2732 _lclose(_lcreat("test-dir\\file1", 0));
2733 _lclose(_lcreat("test-dir\\file2", 0));
2734 CreateDirectoryA("test-dir\\dir1", NULL);
2735 SetLastError(0xdeadbeef);
2736 handle = pFindFirstFileExA("test-dir\\*", level, &search_results, search_ops, NULL, flags);
2737 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2739 win_skip("FindFirstFileExA is not implemented\n");
2740 goto cleanup;
2742 if ((flags & FIND_FIRST_EX_LARGE_FETCH) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2744 win_skip("FindFirstFileExA flag FIND_FIRST_EX_LARGE_FETCH not supported, skipping test\n");
2745 goto cleanup;
2747 if ((level == FindExInfoBasic) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2749 win_skip("FindFirstFileExA level FindExInfoBasic not supported, skipping test\n");
2750 goto cleanup;
2753 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2754 #define CHECK_LEVEL(fn) (level != FindExInfoBasic || !(fn)[0])
2756 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
2757 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
2758 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2760 ok(FindNextFileA(handle, &search_results), "Fetching second file failed\n");
2761 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
2762 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2764 ok(FindNextFileA(handle, &search_results), "Fetching third file failed\n");
2765 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
2766 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2768 SetLastError(0xdeadbeef);
2769 ret = FindNextFileA(handle, &search_results);
2770 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
2772 skip("File system supports directory filtering\n");
2773 /* Results from the previous call are not cleared */
2774 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
2775 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2778 else
2780 ok(ret, "Fetching fourth file failed\n");
2781 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
2782 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2784 ok(FindNextFileA(handle, &search_results), "Fetching fifth file failed\n");
2785 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
2786 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2788 ok(FindNextFileA(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
2791 #undef CHECK_NAME
2792 #undef CHECK_LEVEL
2794 FindClose( handle );
2796 /* Most Windows systems seem to ignore the FIND_FIRST_EX_CASE_SENSITIVE flag. Unofficial documentation
2797 * suggests that there are registry keys and that it might depend on the used filesystem. */
2798 SetLastError(0xdeadbeef);
2799 handle = pFindFirstFileExA("TEST-DIR\\*", level, &search_results, search_ops, NULL, flags);
2800 if (flags & FIND_FIRST_EX_CASE_SENSITIVE)
2802 ok(handle != INVALID_HANDLE_VALUE || GetLastError() == ERROR_PATH_NOT_FOUND,
2803 "Unexpected error %x, expected valid handle or ERROR_PATH_NOT_FOUND\n", GetLastError());
2804 trace("FindFirstFileExA flag FIND_FIRST_EX_CASE_SENSITIVE is %signored\n",
2805 (handle == INVALID_HANDLE_VALUE) ? "not " : "");
2807 else
2808 ok(handle != INVALID_HANDLE_VALUE, "Unexpected error %x, expected valid handle\n", GetLastError());
2809 if (handle != INVALID_HANDLE_VALUE)
2810 FindClose( handle );
2812 cleanup:
2813 DeleteFileA("test-dir\\file1");
2814 DeleteFileA("test-dir\\file2");
2815 RemoveDirectoryA("test-dir\\dir1");
2816 RemoveDirectoryA("test-dir");
2819 static void test_FindFirstFile_wildcards(void)
2821 WIN32_FIND_DATAA find_data;
2822 HANDLE handle;
2823 int i;
2824 static const char* files[] = {
2825 "..a", "..a.a", ".a", ".a..a", ".a.a", ".aaa",
2826 "a", "a..a", "a.a", "a.a.a", "aa", "aaa", "aaaa"
2828 static const struct {
2829 int todo;
2830 const char *pattern, *result;
2831 } tests[] = {
2832 {0, "*.*.*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2833 {0, "*.*.", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2834 {0, ".*.*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa'"},
2835 {0, "*.*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2836 {0, ".*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa'"},
2837 {1, "*.", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2838 {0, "*", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2839 {1, "*..*", ", '.', '..', '..a', '..a.a', '.a..a', 'a..a'"},
2840 {1, "*..", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2841 {1, ".*.", ", '.', '..', '.a', '.aaa'"},
2842 {0, "..*", ", '.', '..', '..a', '..a.a'"},
2843 {0, "**", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2844 {0, "**.", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2845 {0, "*. ", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2846 {1, "* .", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2847 {0, "* . ", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2848 {0, "*.. ", ", '.', '..', '..a', '..a.a', '.a', '.a..a', '.a.a', '.aaa', 'a', 'a..a', 'a.a', 'a.a.a', 'aa', 'aaa', 'aaaa'"},
2849 {1, "*. .", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2850 {1, "* ..", ", '.', '..', 'a', '.a', '..a', 'aa', 'aaa', 'aaaa', '.aaa'"},
2851 {1, " *..", ", '.aaa'"},
2852 {0, "..* ", ", '.', '..', '..a', '..a.a'"},
2853 {1, "?", ", '.', '..', 'a'"},
2854 {1, "?.", ", '.', '..', 'a'"},
2855 {1, "?. ", ", '.', '..', 'a'"},
2856 {1, "??.", ", '.', '..', 'a', 'aa'"},
2857 {1, "??. ", ", '.', '..', 'a', 'aa'"},
2858 {1, "???.", ", '.', '..', 'a', 'aa', 'aaa'"},
2859 {1, "?.??.", ", '.', '..', '.a', 'a', 'a.a'"}
2862 CreateDirectoryA("test-dir", NULL);
2863 SetCurrentDirectoryA("test-dir");
2864 for (i = 0; i < sizeof(files) / sizeof(files[0]); ++i)
2865 _lclose(_lcreat(files[i], 0));
2867 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
2869 char correct[512];
2870 char incorrect[512];
2871 char missing[512];
2873 strcpy(missing, tests[i].result);
2874 correct[0] = incorrect[0] = 0;
2876 handle = FindFirstFileA(tests[i].pattern, &find_data);
2877 if (handle) do {
2878 char* ptr;
2879 char quoted[16];
2881 sprintf( quoted, ", '%.10s'", find_data.cFileName );
2883 if ((ptr = strstr(missing, quoted)))
2885 int len = strlen(quoted);
2886 while ((ptr[0] = ptr[len]) != 0)
2887 ++ptr;
2888 strcat(correct, quoted);
2890 else
2891 strcat(incorrect, quoted);
2892 } while (FindNextFileA(handle, &find_data));
2893 FindClose(handle);
2895 todo_wine_if (tests[i].todo)
2896 ok(missing[0] == 0 && incorrect[0] == 0,
2897 "FindFirstFile with '%s' found correctly %s, found incorrectly %s, and missed %s\n",
2898 tests[i].pattern,
2899 correct[0] ? correct+2 : "none",
2900 incorrect[0] ? incorrect+2 : "none",
2901 missing[0] ? missing+2 : "none");
2904 for (i = 0; i < sizeof(files) / sizeof(files[0]); ++i)
2905 DeleteFileA(files[i]);
2906 SetCurrentDirectoryA("..");
2907 RemoveDirectoryA("test-dir");
2910 static int test_Mapfile_createtemp(HANDLE *handle)
2912 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
2913 DeleteFileA(filename);
2914 *handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
2915 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2916 if (*handle != INVALID_HANDLE_VALUE) {
2918 return 1;
2921 return 0;
2924 static void test_MapFile(void)
2926 HANDLE handle;
2927 HANDLE hmap;
2929 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2931 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
2932 ok( hmap != NULL, "mapping should work, I named it!\n" );
2934 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2936 /* We have to close file before we try new stuff with mapping again.
2937 Else we would always succeed on XP or block descriptors on 95. */
2938 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2939 ok( hmap != NULL, "We should still be able to map!\n" );
2940 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2941 ok( CloseHandle( handle ), "can't close file handle\n");
2942 handle = NULL;
2944 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2946 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2947 ok( hmap == NULL, "mapped zero size file\n");
2948 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
2950 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
2951 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2952 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2953 if ( hmap )
2954 CloseHandle( hmap );
2956 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
2957 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2958 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2959 if ( hmap )
2960 CloseHandle( hmap );
2962 /* On XP you can now map again, on Win 95 you cannot. */
2964 ok( CloseHandle( handle ), "can't close file handle\n");
2965 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
2968 static void test_GetFileType(void)
2970 DWORD type, type2;
2971 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2972 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
2973 type = GetFileType(h);
2974 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
2975 CloseHandle( h );
2976 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2977 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
2978 type = GetFileType(h);
2979 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
2980 CloseHandle( h );
2981 DeleteFileA( filename );
2982 h = GetStdHandle( STD_OUTPUT_HANDLE );
2983 ok( h != INVALID_HANDLE_VALUE, "GetStdHandle failed\n" );
2984 type = GetFileType( (HANDLE)STD_OUTPUT_HANDLE );
2985 type2 = GetFileType( h );
2986 ok(type == type2, "expected type %d for STD_OUTPUT_HANDLE got %d\n", type2, type);
2989 static int completion_count;
2991 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2993 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2994 ReleaseSemaphore(ovl->hEvent, 1, NULL);
2995 completion_count++;
2998 static void test_async_file_errors(void)
3000 char szFile[MAX_PATH];
3001 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
3002 HANDLE hFile;
3003 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
3004 OVERLAPPED ovl;
3005 S(U(ovl)).Offset = 0;
3006 S(U(ovl)).OffsetHigh = 0;
3007 ovl.hEvent = hSem;
3008 completion_count = 0;
3009 szFile[0] = '\0';
3010 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
3011 strcat(szFile, "\\win.ini");
3012 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3013 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
3014 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
3015 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
3016 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
3017 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
3018 while (TRUE)
3020 BOOL res;
3021 DWORD count;
3022 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
3024 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
3025 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
3026 if (!res)
3027 break;
3028 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
3029 break;
3030 S(U(ovl)).Offset += count;
3031 /* i/o completion routine only called if ReadFileEx returned success.
3032 * we only care about violations of this rule so undo what should have
3033 * been done */
3034 completion_count--;
3036 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
3037 /*printf("Error = %ld\n", GetLastError());*/
3038 HeapFree(GetProcessHeap(), 0, lpBuffer);
3041 static BOOL user_apc_ran;
3042 static void CALLBACK user_apc(ULONG_PTR param)
3044 user_apc_ran = TRUE;
3047 static void test_read_write(void)
3049 DWORD bytes, ret, old_prot;
3050 HANDLE hFile;
3051 char temp_path[MAX_PATH];
3052 char filename[MAX_PATH];
3053 char *mem;
3054 static const char prefix[] = "pfx";
3056 ret = GetTempPathA(MAX_PATH, temp_path);
3057 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
3058 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3060 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
3061 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
3063 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
3064 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
3065 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
3067 user_apc_ran = FALSE;
3068 if (pQueueUserAPC) {
3069 trace("Queueing an user APC\n"); /* verify the file is non alerable */
3070 ret = pQueueUserAPC(&user_apc, GetCurrentThread(), 0);
3071 ok(ret, "QueueUserAPC failed: %d\n", GetLastError());
3074 SetLastError(12345678);
3075 bytes = 12345678;
3076 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
3077 ok(ret && GetLastError() == 12345678,
3078 "ret = %d, error %d\n", ret, GetLastError());
3079 ok(!bytes, "bytes = %d\n", bytes);
3081 SetLastError(12345678);
3082 bytes = 12345678;
3083 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
3084 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
3085 (ret && GetLastError() == 12345678), /* Win9x */
3086 "ret = %d, error %d\n", ret, GetLastError());
3087 ok(!bytes || /* Win2k */
3088 bytes == 10, /* Win9x */
3089 "bytes = %d\n", bytes);
3091 /* make sure the file contains data */
3092 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
3093 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
3095 SetLastError(12345678);
3096 bytes = 12345678;
3097 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
3098 ok(ret && GetLastError() == 12345678,
3099 "ret = %d, error %d\n", ret, GetLastError());
3100 ok(!bytes, "bytes = %d\n", bytes);
3102 SetLastError(12345678);
3103 bytes = 12345678;
3104 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
3105 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
3106 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
3107 "ret = %d, error %d\n", ret, GetLastError());
3108 ok(!bytes, "bytes = %d\n", bytes);
3110 ok(user_apc_ran == FALSE, "UserAPC ran, file using alertable io mode\n");
3111 if (pQueueUserAPC)
3112 SleepEx(0, TRUE); /* get rid of apc */
3114 /* test passing protected memory as buffer */
3116 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
3117 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
3119 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3120 ok( ret, "WriteFile failed error %u\n", GetLastError() );
3121 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
3123 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
3124 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3126 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3127 ok( !ret, "WriteFile succeeded\n" );
3128 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3129 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3130 "wrong error %u\n", GetLastError() );
3131 ok( bytes == 0, "wrote %x bytes\n", bytes );
3133 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
3134 ok( !ret, "WriteFile succeeded\n" );
3135 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
3136 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3137 "wrong error %u\n", GetLastError() );
3138 ok( bytes == 0, "wrote %x bytes\n", bytes );
3140 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
3141 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3143 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3144 ok( !ret, "WriteFile succeeded\n" );
3145 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3146 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3147 "wrong error %u\n", GetLastError() );
3148 ok( bytes == 0, "wrote %x bytes\n", bytes );
3150 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3152 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3153 ok( !ret, "ReadFile succeeded\n" );
3154 ok( GetLastError() == ERROR_NOACCESS ||
3155 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3156 "wrong error %u\n", GetLastError() );
3157 ok( bytes == 0, "read %x bytes\n", bytes );
3159 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
3160 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3162 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3163 ok( !ret, "ReadFile succeeded\n" );
3164 ok( GetLastError() == ERROR_NOACCESS ||
3165 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3166 "wrong error %u\n", GetLastError() );
3167 ok( bytes == 0, "read %x bytes\n", bytes );
3169 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
3170 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3172 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3173 ok( !ret, "ReadFile succeeded\n" );
3174 ok( GetLastError() == ERROR_NOACCESS ||
3175 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3176 "wrong error %u\n", GetLastError() );
3177 ok( bytes == 0, "read %x bytes\n", bytes );
3179 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
3180 SetEndOfFile( hFile );
3181 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3183 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3184 ok( !ret, "ReadFile succeeded\n" );
3185 ok( GetLastError() == ERROR_NOACCESS ||
3186 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3187 "wrong error %u\n", GetLastError() );
3188 ok( bytes == 0, "read %x bytes\n", bytes );
3190 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
3191 ok( ret, "ReadFile failed error %u\n", GetLastError() );
3192 ok( bytes == 0x1234, "read %x bytes\n", bytes );
3194 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
3195 ok( !ret, "ReadFile succeeded\n" );
3196 ok( GetLastError() == ERROR_NOACCESS ||
3197 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3198 "wrong error %u\n", GetLastError() );
3199 ok( bytes == 0, "read %x bytes\n", bytes );
3201 VirtualFree( mem, 0, MEM_RELEASE );
3203 ret = CloseHandle(hFile);
3204 ok( ret, "CloseHandle: error %d\n", GetLastError());
3205 ret = DeleteFileA(filename);
3206 ok( ret, "DeleteFileA: error %d\n", GetLastError());
3209 static void test_OpenFile(void)
3211 HFILE hFile;
3212 OFSTRUCT ofs;
3213 BOOL ret;
3214 DWORD retval;
3216 static const char file[] = "regedit.exe";
3217 static const char foo[] = ".\\foo-bar-foo.baz";
3218 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
3219 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3220 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3221 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3222 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3223 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
3224 char buff[MAX_PATH];
3225 char buff_long[4*MAX_PATH];
3226 char filled_0xA5[OFS_MAXPATHNAME];
3227 char *p;
3228 UINT length;
3230 /* Check for existing file */
3231 if (!pGetSystemWindowsDirectoryA)
3232 length = GetWindowsDirectoryA(buff, MAX_PATH);
3233 else
3234 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
3236 if (length + sizeof(file) < MAX_PATH)
3238 p = buff + strlen(buff);
3239 if (p > buff && p[-1] != '\\') *p++ = '\\';
3240 strcpy( p, file );
3241 memset(&ofs, 0xA5, sizeof(ofs));
3242 SetLastError(0xfaceabee);
3244 hFile = OpenFile(buff, &ofs, OF_EXIST);
3245 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
3246 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3247 "GetLastError() returns %d\n", GetLastError() );
3248 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3249 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3250 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3251 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3252 ofs.szPathName, buff );
3255 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
3256 length = GetCurrentDirectoryA(MAX_PATH, buff);
3258 /* Check for nonexistent file */
3259 if (length + sizeof(foo) < MAX_PATH)
3261 p = buff + strlen(buff);
3262 if (p > buff && p[-1] != '\\') *p++ = '\\';
3263 strcpy( p, foo + 2 );
3264 memset(&ofs, 0xA5, sizeof(ofs));
3265 SetLastError(0xfaceabee);
3267 hFile = OpenFile(foo, &ofs, OF_EXIST);
3268 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3269 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
3270 todo_wine
3271 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3272 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3273 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3274 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3275 ofs.szPathName, buff );
3278 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
3279 length += lstrlenA(foo_too_long + 1);
3281 /* Check for nonexistent file with too long filename */
3282 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
3284 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
3285 memset(&ofs, 0xA5, sizeof(ofs));
3286 SetLastError(0xfaceabee);
3288 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
3289 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3290 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
3291 "GetLastError() returns %d\n", GetLastError() );
3292 todo_wine
3293 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3294 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
3295 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3296 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3297 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
3298 ofs.szPathName );
3301 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
3303 if (length >= MAX_PATH)
3305 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
3306 return;
3308 p = buff + strlen(buff);
3309 if (p > buff && p[-1] != '\\') *p++ = '\\';
3310 strcpy( p, filename );
3312 memset(&ofs, 0xA5, sizeof(ofs));
3313 SetLastError(0xfaceabee);
3314 /* Create an empty file */
3315 hFile = OpenFile(filename, &ofs, OF_CREATE);
3316 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
3317 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3318 "GetLastError() returns %d\n", GetLastError() );
3319 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3320 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3321 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3322 ret = _lclose(hFile);
3323 ok( !ret, "_lclose() returns %d\n", ret );
3324 retval = GetFileAttributesA(filename);
3325 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
3327 memset(&ofs, 0xA5, sizeof(ofs));
3328 SetLastError(0xfaceabee);
3329 /* Check various opening options: */
3330 /* for reading only, */
3331 hFile = OpenFile(filename, &ofs, OF_READ);
3332 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
3333 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3334 "GetLastError() returns %d\n", GetLastError() );
3335 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3336 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3337 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3338 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3339 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3340 ret = _lclose(hFile);
3341 ok( !ret, "_lclose() returns %d\n", ret );
3343 memset(&ofs, 0xA5, sizeof(ofs));
3344 SetLastError(0xfaceabee);
3345 /* for writing only, */
3346 hFile = OpenFile(filename, &ofs, OF_WRITE);
3347 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
3348 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3349 "GetLastError() returns %d\n", GetLastError() );
3350 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3351 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3352 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3353 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3354 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3355 ret = _lclose(hFile);
3356 ok( !ret, "_lclose() returns %d\n", ret );
3358 memset(&ofs, 0xA5, sizeof(ofs));
3359 SetLastError(0xfaceabee);
3360 /* for reading and writing, */
3361 hFile = OpenFile(filename, &ofs, OF_READWRITE);
3362 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
3363 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3364 "GetLastError() returns %d\n", GetLastError() );
3365 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3366 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3367 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3368 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3369 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3370 ret = _lclose(hFile);
3371 ok( !ret, "_lclose() returns %d\n", ret );
3373 memset(&ofs, 0xA5, sizeof(ofs));
3374 SetLastError(0xfaceabee);
3375 /* for checking file presence. */
3376 hFile = OpenFile(filename, &ofs, OF_EXIST);
3377 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
3378 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3379 "GetLastError() returns %d\n", GetLastError() );
3380 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3381 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3382 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3383 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3384 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3386 memset(&ofs, 0xA5, sizeof(ofs));
3387 SetLastError(0xfaceabee);
3388 /* Delete the file and make sure it doesn't exist anymore */
3389 hFile = OpenFile(filename, &ofs, OF_DELETE);
3390 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
3391 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3392 "GetLastError() returns %d\n", GetLastError() );
3393 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3394 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3395 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3396 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3397 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3399 retval = GetFileAttributesA(filename);
3400 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
3403 static void test_overlapped(void)
3405 OVERLAPPED ov;
3406 DWORD r, result;
3408 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
3409 if (0) /* tested: WinXP */
3411 GetOverlappedResult(0, NULL, &result, FALSE);
3412 GetOverlappedResult(0, &ov, NULL, FALSE);
3413 GetOverlappedResult(0, NULL, NULL, FALSE);
3416 memset( &ov, 0, sizeof ov );
3417 result = 1;
3418 r = GetOverlappedResult(0, &ov, &result, 0);
3419 if (r)
3420 ok( result == 0, "wrong result %u\n", result );
3421 else /* win9x */
3422 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3424 result = 0;
3425 ov.Internal = 0;
3426 ov.InternalHigh = 0xabcd;
3427 r = GetOverlappedResult(0, &ov, &result, 0);
3428 if (r)
3429 ok( result == 0xabcd, "wrong result %u\n", result );
3430 else /* win9x */
3431 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3433 SetLastError( 0xb00 );
3434 result = 0;
3435 ov.Internal = STATUS_INVALID_HANDLE;
3436 ov.InternalHigh = 0xabcd;
3437 r = GetOverlappedResult(0, &ov, &result, 0);
3438 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3439 ok( r == FALSE, "should return false\n");
3440 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
3442 SetLastError( 0xb00 );
3443 result = 0;
3444 ov.Internal = STATUS_PENDING;
3445 ov.InternalHigh = 0xabcd;
3446 r = GetOverlappedResult(0, &ov, &result, 0);
3447 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3448 "wrong error %u\n", GetLastError() );
3449 ok( r == FALSE, "should return false\n");
3450 ok( result == 0, "wrong result %u\n", result );
3452 SetLastError( 0xb00 );
3453 ov.hEvent = CreateEventW( NULL, 1, 1, NULL );
3454 ov.Internal = STATUS_PENDING;
3455 ov.InternalHigh = 0xabcd;
3456 r = GetOverlappedResult(0, &ov, &result, 0);
3457 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3458 "wrong error %u\n", GetLastError() );
3459 ok( r == FALSE, "should return false\n");
3461 r = GetOverlappedResult( 0, &ov, &result, TRUE );
3462 ok( r == TRUE, "should return TRUE\n" );
3463 ok( result == 0xabcd, "wrong result %u\n", result );
3464 ok( ov.Internal == STATUS_PENDING, "expected STATUS_PENDING, got %08lx\n", ov.Internal );
3466 ResetEvent( ov.hEvent );
3468 SetLastError( 0xb00 );
3469 ov.Internal = STATUS_PENDING;
3470 ov.InternalHigh = 0;
3471 r = GetOverlappedResult(0, &ov, &result, 0);
3472 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3473 "wrong error %u\n", GetLastError() );
3474 ok( r == FALSE, "should return false\n");
3476 r = CloseHandle( ov.hEvent );
3477 ok( r == TRUE, "close handle failed\n");
3480 static void test_RemoveDirectory(void)
3482 int rc;
3483 char directory[] = "removeme";
3485 rc = CreateDirectoryA(directory, NULL);
3486 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
3488 rc = SetCurrentDirectoryA(directory);
3489 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3491 rc = RemoveDirectoryA(".");
3492 if (!rc)
3494 rc = SetCurrentDirectoryA("..");
3495 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3497 rc = RemoveDirectoryA(directory);
3498 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
3502 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
3504 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
3505 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
3506 return abs(t1 - t2) <= tolerance;
3509 static void test_ReplaceFileA(void)
3511 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3512 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
3513 static const char replacedData[] = "file-to-replace";
3514 static const char replacementData[] = "new-file";
3515 static const char backupData[] = "backup-file";
3516 FILETIME ftReplaced, ftReplacement, ftBackup;
3517 static const char prefix[] = "pfx";
3518 char temp_path[MAX_PATH];
3519 DWORD ret;
3520 BOOL retok, removeBackup = FALSE;
3522 if (!pReplaceFileA)
3524 win_skip("ReplaceFileA() is missing\n");
3525 return;
3528 ret = GetTempPathA(MAX_PATH, temp_path);
3529 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
3530 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3532 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
3533 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
3535 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3536 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3538 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
3539 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
3541 /* place predictable data in the file to be replaced */
3542 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3543 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3544 "failed to open replaced file\n");
3545 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
3546 ok( retok && ret == sizeof(replacedData),
3547 "WriteFile error (replaced) %d\n", GetLastError());
3548 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
3549 "replaced file has wrong size\n");
3550 /* place predictable data in the file to be the replacement */
3551 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3552 ok(hReplacementFile != INVALID_HANDLE_VALUE,
3553 "failed to open replacement file\n");
3554 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
3555 ok( retok && ret == sizeof(replacementData),
3556 "WriteFile error (replacement) %d\n", GetLastError());
3557 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
3558 "replacement file has wrong size\n");
3559 /* place predictable data in the backup file (to be over-written) */
3560 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3561 ok(hBackupFile != INVALID_HANDLE_VALUE,
3562 "failed to open backup file\n");
3563 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
3564 ok( retok && ret == sizeof(backupData),
3565 "WriteFile error (replacement) %d\n", GetLastError());
3566 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
3567 "backup file has wrong size\n");
3568 /* change the filetime on the "replaced" file to ensure that it changes */
3569 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3570 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
3571 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
3572 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3573 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
3574 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
3575 CloseHandle(hReplacedFile);
3576 /* change the filetime on the backup to ensure that it changes */
3577 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3578 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
3579 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
3580 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3581 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
3582 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
3583 CloseHandle(hBackupFile);
3584 /* get the filetime on the replacement file to perform checks */
3585 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
3586 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
3587 CloseHandle(hReplacementFile);
3589 /* perform replacement w/ backup
3590 * TODO: flags are not implemented
3592 SetLastError(0xdeadbeef);
3593 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3594 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
3595 /* make sure that the backup has the size of the old "replaced" file */
3596 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3597 ok(hBackupFile != INVALID_HANDLE_VALUE,
3598 "failed to open backup file\n");
3599 ret = GetFileSize(hBackupFile, NULL);
3600 ok(ret == sizeof(replacedData),
3601 "backup file has wrong size %d\n", ret);
3602 /* make sure that the "replaced" file has the size of the replacement file */
3603 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3604 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3605 "failed to open replaced file: %d\n", GetLastError());
3606 if (hReplacedFile != INVALID_HANDLE_VALUE)
3608 ret = GetFileSize(hReplacedFile, NULL);
3609 ok(ret == sizeof(replacementData),
3610 "replaced file has wrong size %d\n", ret);
3611 /* make sure that the replacement file no-longer exists */
3612 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3613 ok(hReplacementFile == INVALID_HANDLE_VALUE,
3614 "unexpected error, replacement file should not exist %d\n", GetLastError());
3615 /* make sure that the backup has the old "replaced" filetime */
3616 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3617 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3618 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
3619 CloseHandle(hBackupFile);
3620 /* make sure that the "replaced" has the old replacement filetime */
3621 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3622 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3623 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
3624 "replaced file has wrong filetime %x%08x / %x%08x\n",
3625 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
3626 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
3627 CloseHandle(hReplacedFile);
3629 else
3630 skip("couldn't open replacement file, skipping tests\n");
3632 /* re-create replacement file for pass w/o backup (blank) */
3633 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3634 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3635 /* perform replacement w/o backup
3636 * TODO: flags are not implemented
3638 SetLastError(0xdeadbeef);
3639 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3640 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3641 "ReplaceFileA: unexpected error %d\n", GetLastError());
3643 /* re-create replacement file for pass w/ backup (backup-file not existing) */
3644 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3645 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3646 ret = DeleteFileA(backup);
3647 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
3648 /* perform replacement w/ backup (no pre-existing backup)
3649 * TODO: flags are not implemented
3651 SetLastError(0xdeadbeef);
3652 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3653 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3654 "ReplaceFileA: unexpected error %d\n", GetLastError());
3655 if (ret)
3656 removeBackup = TRUE;
3658 /* re-create replacement file for pass w/ no permissions to "replaced" */
3659 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3660 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3661 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
3662 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3663 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
3664 /* perform replacement w/ backup (no permission to "replaced")
3665 * TODO: flags are not implemented
3667 SetLastError(0xdeadbeef);
3668 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3669 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
3670 /* make sure that the replacement file still exists */
3671 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3672 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
3673 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
3674 "unexpected error, replacement file should still exist %d\n", GetLastError());
3675 CloseHandle(hReplacementFile);
3676 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
3677 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3678 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
3680 /* replacement file still exists, make pass w/o "replaced" */
3681 ret = DeleteFileA(replaced);
3682 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3683 "DeleteFileA: error (replaced) %d\n", GetLastError());
3684 /* perform replacement w/ backup (no pre-existing backup or "replaced")
3685 * TODO: flags are not implemented
3687 SetLastError(0xdeadbeef);
3688 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3689 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3690 GetLastError() == ERROR_ACCESS_DENIED),
3691 "ReplaceFileA: unexpected error %d\n", GetLastError());
3693 /* perform replacement w/o existing "replacement" file
3694 * TODO: flags are not implemented
3696 SetLastError(0xdeadbeef);
3697 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3698 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3699 GetLastError() == ERROR_ACCESS_DENIED),
3700 "ReplaceFileA: unexpected error %d\n", GetLastError());
3701 DeleteFileA( replacement );
3704 * if the first round (w/ backup) worked then as long as there is no
3705 * failure then there is no need to check this round (w/ backup is the
3706 * more complete case)
3709 /* delete temporary files, replacement and replaced are already deleted */
3710 if (removeBackup)
3712 ret = DeleteFileA(backup);
3713 ok(ret ||
3714 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3715 "DeleteFileA: error (backup) %d\n", GetLastError());
3720 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
3721 * need to be as thorough.
3723 static void test_ReplaceFileW(void)
3725 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3726 static const WCHAR prefix[] = {'p','f','x',0};
3727 WCHAR temp_path[MAX_PATH];
3728 DWORD ret;
3729 BOOL removeBackup = FALSE;
3731 if (!pReplaceFileW)
3733 win_skip("ReplaceFileW() is missing\n");
3734 return;
3737 ret = GetTempPathW(MAX_PATH, temp_path);
3738 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3740 win_skip("GetTempPathW is not available\n");
3741 return;
3743 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
3744 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3746 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
3747 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
3749 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3750 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3752 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
3753 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
3755 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3756 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
3758 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3759 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3760 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3761 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3762 "ReplaceFileW: error %d\n", GetLastError());
3764 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3765 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3766 ret = DeleteFileW(backup);
3767 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
3768 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3769 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3770 "ReplaceFileW: error %d\n", GetLastError());
3772 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3773 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3774 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
3775 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3776 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3778 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3779 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
3780 "ReplaceFileW: unexpected error %d\n", GetLastError());
3781 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
3782 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3783 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3784 if (ret)
3785 removeBackup = TRUE;
3787 ret = DeleteFileW(replaced);
3788 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
3789 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3790 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
3792 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3793 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3794 GetLastError() == ERROR_ACCESS_DENIED),
3795 "ReplaceFileW: unexpected error %d\n", GetLastError());
3796 DeleteFileW( replacement );
3798 if (removeBackup)
3800 ret = DeleteFileW(backup);
3801 ok(ret ||
3802 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3803 "DeleteFileW: error (backup) %d\n", GetLastError());
3807 static void test_CreateFile(void)
3809 static const struct test_data
3811 DWORD disposition, access, error, clean_up;
3812 } td[] =
3814 /* 0 */ { 0, 0, ERROR_INVALID_PARAMETER, 0 },
3815 /* 1 */ { 0, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3816 /* 2 */ { 0, GENERIC_READ|GENERIC_WRITE, ERROR_INVALID_PARAMETER, 0 },
3817 /* 3 */ { CREATE_NEW, 0, ERROR_FILE_EXISTS, 1 },
3818 /* 4 */ { CREATE_NEW, 0, 0, 1 },
3819 /* 5 */ { CREATE_NEW, GENERIC_READ, 0, 1 },
3820 /* 6 */ { CREATE_NEW, GENERIC_WRITE, 0, 1 },
3821 /* 7 */ { CREATE_NEW, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3822 /* 8 */ { CREATE_ALWAYS, 0, 0, 0 },
3823 /* 9 */ { CREATE_ALWAYS, GENERIC_READ, 0, 0 },
3824 /* 10*/ { CREATE_ALWAYS, GENERIC_WRITE, 0, 0 },
3825 /* 11*/ { CREATE_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3826 /* 12*/ { OPEN_EXISTING, 0, ERROR_FILE_NOT_FOUND, 0 },
3827 /* 13*/ { CREATE_ALWAYS, 0, 0, 0 },
3828 /* 14*/ { OPEN_EXISTING, 0, 0, 0 },
3829 /* 15*/ { OPEN_EXISTING, GENERIC_READ, 0, 0 },
3830 /* 16*/ { OPEN_EXISTING, GENERIC_WRITE, 0, 0 },
3831 /* 17*/ { OPEN_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3832 /* 18*/ { OPEN_ALWAYS, 0, 0, 0 },
3833 /* 19*/ { OPEN_ALWAYS, GENERIC_READ, 0, 0 },
3834 /* 20*/ { OPEN_ALWAYS, GENERIC_WRITE, 0, 0 },
3835 /* 21*/ { OPEN_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3836 /* 22*/ { TRUNCATE_EXISTING, 0, ERROR_INVALID_PARAMETER, 0 },
3837 /* 23*/ { TRUNCATE_EXISTING, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3838 /* 24*/ { TRUNCATE_EXISTING, GENERIC_WRITE, 0, 0 },
3839 /* 25*/ { TRUNCATE_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3840 /* 26*/ { TRUNCATE_EXISTING, FILE_WRITE_DATA, ERROR_INVALID_PARAMETER, 0 }
3842 char temp_path[MAX_PATH];
3843 char file_name[MAX_PATH];
3844 DWORD i, ret, written;
3845 HANDLE hfile;
3847 GetTempPathA(MAX_PATH, temp_path);
3848 GetTempFileNameA(temp_path, "tmp", 0, file_name);
3850 i = strlen(temp_path);
3851 if (i && temp_path[i - 1] == '\\') temp_path[i - 1] = 0;
3853 for (i = 0; i <= 5; i++)
3855 SetLastError(0xdeadbeef);
3856 hfile = CreateFileA(temp_path, GENERIC_READ, 0, NULL, i, 0, 0);
3857 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
3858 if (i == 0 || i == 5)
3860 /* FIXME: remove once Wine is fixed */
3861 todo_wine_if (i == 5)
3862 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3864 else
3866 /* FIXME: remove once Wine is fixed */
3867 todo_wine_if (i == 1)
3868 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3871 SetLastError(0xdeadbeef);
3872 hfile = CreateFileA(temp_path, GENERIC_WRITE, 0, NULL, i, 0, 0);
3873 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
3874 if (i == 0)
3875 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3876 else
3878 /* FIXME: remove once Wine is fixed */
3879 todo_wine_if (i == 1)
3880 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3884 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3886 SetLastError(0xdeadbeef);
3887 hfile = CreateFileA(file_name, td[i].access, 0, NULL, td[i].disposition, 0, 0);
3888 if (!td[i].error)
3890 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
3891 written = 0xdeadbeef;
3892 SetLastError(0xdeadbeef);
3893 ret = WriteFile(hfile, &td[i].error, sizeof(td[i].error), &written, NULL);
3894 if (td[i].access & GENERIC_WRITE)
3895 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
3896 else
3898 ok(!ret, "%d: WriteFile should fail\n", i);
3899 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3901 SetLastError(0xdeadbeef);
3902 ret = SetFileTime(hfile, NULL, NULL, NULL);
3903 if (td[i].access & GENERIC_WRITE) /* actually FILE_WRITE_ATTRIBUTES */
3904 ok(ret, "%d: SetFileTime error %d\n", i, GetLastError());
3905 else
3907 todo_wine
3909 ok(!ret, "%d: SetFileTime should fail\n", i);
3910 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3913 CloseHandle(hfile);
3915 else
3917 /* FIXME: remove the condition below once Wine is fixed */
3918 if (td[i].disposition == TRUNCATE_EXISTING && !(td[i].access & GENERIC_WRITE))
3920 todo_wine
3922 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
3923 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
3925 CloseHandle(hfile);
3927 else
3929 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
3930 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
3934 if (td[i].clean_up) DeleteFileA(file_name);
3937 DeleteFileA(file_name);
3940 static void test_GetFileInformationByHandleEx(void)
3942 int i;
3943 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[1024], *strPtr;
3944 BOOL ret;
3945 DWORD ret2, written;
3946 HANDLE directory, file;
3947 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
3948 FILE_BASIC_INFO *basicInfo;
3949 FILE_STANDARD_INFO *standardInfo;
3950 FILE_NAME_INFO *nameInfo;
3951 LARGE_INTEGER prevWrite;
3952 FILE_IO_PRIORITY_HINT_INFO priohintinfo;
3953 FILE_ALLOCATION_INFO allocinfo;
3954 FILE_DISPOSITION_INFO dispinfo;
3955 FILE_END_OF_FILE_INFO eofinfo;
3956 FILE_RENAME_INFO renameinfo;
3958 struct {
3959 FILE_INFO_BY_HANDLE_CLASS handleClass;
3960 void *ptr;
3961 DWORD size;
3962 DWORD errorCode;
3963 } checks[] = {
3964 {0xdeadbeef, NULL, 0, ERROR_INVALID_PARAMETER},
3965 {FileIdBothDirectoryInfo, NULL, 0, ERROR_BAD_LENGTH},
3966 {FileIdBothDirectoryInfo, NULL, sizeof(buffer), ERROR_NOACCESS},
3967 {FileIdBothDirectoryInfo, buffer, 0, ERROR_BAD_LENGTH}};
3969 if (!pGetFileInformationByHandleEx)
3971 win_skip("GetFileInformationByHandleEx is missing.\n");
3972 return;
3975 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
3976 ok(ret2, "GetFileInformationByHandleEx: GetTempPathA failed, got error %u.\n", GetLastError());
3978 /* ensure the existence of a file in the temp folder */
3979 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
3980 ok(ret2, "GetFileInformationByHandleEx: GetTempFileNameA failed, got error %u.\n", GetLastError());
3981 ret2 = GetFileAttributesA(tempFileName);
3982 ok(ret2 != INVALID_FILE_ATTRIBUTES, "GetFileInformationByHandleEx: "
3983 "GetFileAttributesA failed to find the temp file, got error %u.\n", GetLastError());
3985 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3986 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
3987 ok(directory != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp folder, "
3988 "got error %u.\n", GetLastError());
3990 for (i = 0; i < sizeof(checks) / sizeof(checks[0]); i += 1)
3992 SetLastError(0xdeadbeef);
3993 ret = pGetFileInformationByHandleEx(directory, checks[i].handleClass, checks[i].ptr, checks[i].size);
3994 ok(!ret && GetLastError() == checks[i].errorCode, "GetFileInformationByHandleEx: expected error %u, "
3995 "got %u.\n", checks[i].errorCode, GetLastError());
3998 while (TRUE)
4000 memset(buffer, 0xff, sizeof(buffer));
4001 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4002 if (!ret && GetLastError() == ERROR_NO_MORE_FILES)
4003 break;
4004 ok(ret, "GetFileInformationByHandleEx: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4005 if (!ret)
4006 break;
4007 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4008 while (TRUE)
4010 ok(bothDirInfo->FileAttributes != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file attributes.\n");
4011 ok(bothDirInfo->FileId.u.LowPart != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file id.\n");
4012 ok(bothDirInfo->FileNameLength != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file name length.\n");
4013 if (!bothDirInfo->NextEntryOffset)
4014 break;
4015 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
4019 CloseHandle(directory);
4021 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4022 NULL, OPEN_EXISTING, 0, NULL);
4023 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
4024 "got error %u.\n", GetLastError());
4026 /* Test FileBasicInfo; make sure the write time changes when a file is updated */
4027 memset(buffer, 0xff, sizeof(buffer));
4028 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
4029 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
4030 basicInfo = (FILE_BASIC_INFO *)buffer;
4031 prevWrite = basicInfo->LastWriteTime;
4032 CloseHandle(file);
4034 Sleep(30); /* Make sure a new write time is different from the previous */
4036 /* Write something to the file, to make sure the write time has changed */
4037 file = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4038 NULL, OPEN_EXISTING, 0, NULL);
4039 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
4040 "got error %u.\n", GetLastError());
4041 ret = WriteFile(file, tempFileName, strlen(tempFileName), &written, NULL);
4042 ok(ret, "GetFileInformationByHandleEx: Write failed\n");
4043 CloseHandle(file);
4045 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4046 NULL, OPEN_EXISTING, 0, NULL);
4047 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
4048 "got error %u.\n", GetLastError());
4050 memset(buffer, 0xff, sizeof(buffer));
4051 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
4052 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
4053 basicInfo = (FILE_BASIC_INFO *)buffer;
4054 /* Could also check that the creation time didn't change - on windows
4055 * it doesn't, but on wine, it does change even if it shouldn't. */
4056 ok(basicInfo->LastWriteTime.QuadPart != prevWrite.QuadPart,
4057 "GetFileInformationByHandleEx: last write time didn't change\n");
4059 /* Test FileStandardInfo, check some basic parameters */
4060 memset(buffer, 0xff, sizeof(buffer));
4061 ret = pGetFileInformationByHandleEx(file, FileStandardInfo, buffer, sizeof(buffer));
4062 ok(ret, "GetFileInformationByHandleEx: failed to get FileStandardInfo, %u\n", GetLastError());
4063 standardInfo = (FILE_STANDARD_INFO *)buffer;
4064 ok(standardInfo->NumberOfLinks == 1, "GetFileInformationByHandleEx: Unexpected number of links\n");
4065 ok(standardInfo->DeletePending == FALSE, "GetFileInformationByHandleEx: Unexpected pending delete\n");
4066 ok(standardInfo->Directory == FALSE, "GetFileInformationByHandleEx: Incorrect directory flag\n");
4068 /* Test FileNameInfo */
4069 memset(buffer, 0xff, sizeof(buffer));
4070 ret = pGetFileInformationByHandleEx(file, FileNameInfo, buffer, sizeof(buffer));
4071 ok(ret, "GetFileInformationByHandleEx: failed to get FileNameInfo, %u\n", GetLastError());
4072 nameInfo = (FILE_NAME_INFO *)buffer;
4073 strPtr = strchr(tempFileName, '\\');
4074 ok(strPtr != NULL, "GetFileInformationByHandleEx: Temp filename didn't contain backslash\n");
4075 ok(nameInfo->FileNameLength == strlen(strPtr) * 2,
4076 "GetFileInformationByHandleEx: Incorrect file name length\n");
4077 for (i = 0; i < nameInfo->FileNameLength/2; i++)
4078 ok(strPtr[i] == nameInfo->FileName[i], "Incorrect filename char %d: %c vs %c\n",
4079 i, strPtr[i], nameInfo->FileName[i]);
4081 /* invalid classes */
4082 SetLastError(0xdeadbeef);
4083 ret = pGetFileInformationByHandleEx(file, FileEndOfFileInfo, &eofinfo, sizeof(eofinfo));
4084 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4086 SetLastError(0xdeadbeef);
4087 ret = pGetFileInformationByHandleEx(file, FileIoPriorityHintInfo, &priohintinfo, sizeof(priohintinfo));
4088 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4090 SetLastError(0xdeadbeef);
4091 ret = pGetFileInformationByHandleEx(file, FileAllocationInfo, &allocinfo, sizeof(allocinfo));
4092 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4094 SetLastError(0xdeadbeef);
4095 ret = pGetFileInformationByHandleEx(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
4096 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4098 SetLastError(0xdeadbeef);
4099 ret = pGetFileInformationByHandleEx(file, FileRenameInfo, &renameinfo, sizeof(renameinfo));
4100 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4102 CloseHandle(file);
4103 DeleteFileA(tempFileName);
4106 static void test_OpenFileById(void)
4108 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[256], tickCount[256];
4109 WCHAR tempFileNameW[MAX_PATH];
4110 BOOL ret, found;
4111 DWORD ret2, count, tempFileNameLen;
4112 HANDLE directory, handle, tempFile;
4113 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
4114 FILE_ID_DESCRIPTOR fileIdDescr;
4116 if (!pGetFileInformationByHandleEx || !pOpenFileById)
4118 win_skip("GetFileInformationByHandleEx or OpenFileById is missing.\n");
4119 return;
4122 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
4123 ok(ret2, "OpenFileById: GetTempPath failed, got error %u.\n", GetLastError());
4125 /* ensure the existence of a file in the temp folder */
4126 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4127 ok(ret2, "OpenFileById: GetTempFileNameA failed, got error %u.\n", GetLastError());
4128 ret2 = GetFileAttributesA(tempFileName);
4129 ok(ret2 != INVALID_FILE_ATTRIBUTES,
4130 "OpenFileById: GetFileAttributesA failed to find the temp file, got error %u\n", GetLastError());
4132 ret2 = MultiByteToWideChar(CP_ACP, 0, tempFileName + strlen(tempPath), -1, tempFileNameW, sizeof(tempFileNameW)/sizeof(tempFileNameW[0]));
4133 ok(ret2, "OpenFileById: MultiByteToWideChar failed to convert tempFileName, got error %u.\n", GetLastError());
4134 tempFileNameLen = ret2 - 1;
4136 tempFile = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4137 ok(tempFile != INVALID_HANDLE_VALUE, "OpenFileById: failed to create a temp file, "
4138 "got error %u.\n", GetLastError());
4139 ret2 = sprintf(tickCount, "%u", GetTickCount());
4140 ret = WriteFile(tempFile, tickCount, ret2, &count, NULL);
4141 ok(ret, "OpenFileById: WriteFile failed, got error %u.\n", GetLastError());
4142 CloseHandle(tempFile);
4144 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4145 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
4146 ok(directory != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder, "
4147 "got error %u.\n", GetLastError());
4149 /* get info about the temp folder itself */
4150 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4151 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4152 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4153 ok(bothDirInfo->FileNameLength == sizeof(WCHAR) && bothDirInfo->FileName[0] == '.',
4154 "OpenFileById: failed to return the temp folder at the first entry, got error %u.\n", GetLastError());
4156 /* open the temp folder itself */
4157 fileIdDescr.dwSize = sizeof(fileIdDescr);
4158 fileIdDescr.Type = FileIdType;
4159 U(fileIdDescr).FileId = bothDirInfo->FileId;
4160 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4161 todo_wine
4162 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder itself, got error %u.\n", GetLastError());
4163 CloseHandle(handle);
4165 /* find the temp file in the temp folder */
4166 found = FALSE;
4167 while (!found)
4169 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4170 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4171 if (!ret)
4172 break;
4173 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4174 while (TRUE)
4176 if (tempFileNameLen == bothDirInfo->FileNameLength / sizeof(WCHAR) &&
4177 memcmp(tempFileNameW, bothDirInfo->FileName, bothDirInfo->FileNameLength) == 0)
4179 found = TRUE;
4180 break;
4182 if (!bothDirInfo->NextEntryOffset)
4183 break;
4184 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
4187 ok(found, "OpenFileById: failed to find the temp file in the temp folder.\n");
4189 SetLastError(0xdeadbeef);
4190 handle = pOpenFileById(directory, NULL, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4191 ok(handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER,
4192 "OpenFileById: expected ERROR_INVALID_PARAMETER, got error %u.\n", GetLastError());
4194 fileIdDescr.dwSize = sizeof(fileIdDescr);
4195 fileIdDescr.Type = FileIdType;
4196 U(fileIdDescr).FileId = bothDirInfo->FileId;
4197 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4198 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the file, got error %u.\n", GetLastError());
4200 ret = ReadFile(handle, buffer, sizeof(buffer), &count, NULL);
4201 buffer[count] = 0;
4202 ok(ret, "OpenFileById: ReadFile failed, got error %u.\n", GetLastError());
4203 ok(strcmp(tickCount, buffer) == 0, "OpenFileById: invalid contents of the temp file.\n");
4205 CloseHandle(handle);
4206 CloseHandle(directory);
4207 DeleteFileA(tempFileName);
4210 static void test_SetFileValidData(void)
4212 BOOL ret;
4213 HANDLE handle;
4214 DWORD error, count;
4215 char path[MAX_PATH], filename[MAX_PATH];
4216 TOKEN_PRIVILEGES privs;
4217 HANDLE token = NULL;
4219 if (!pSetFileValidData)
4221 win_skip("SetFileValidData is missing\n");
4222 return;
4224 GetTempPathA(sizeof(path), path);
4225 GetTempFileNameA(path, "tst", 0, filename);
4226 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4227 WriteFile(handle, "test", sizeof("test") - 1, &count, NULL);
4228 CloseHandle(handle);
4230 SetLastError(0xdeadbeef);
4231 ret = pSetFileValidData(INVALID_HANDLE_VALUE, 0);
4232 error = GetLastError();
4233 ok(!ret, "SetFileValidData succeeded\n");
4234 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4236 SetLastError(0xdeadbeef);
4237 ret = pSetFileValidData(INVALID_HANDLE_VALUE, -1);
4238 error = GetLastError();
4239 ok(!ret, "SetFileValidData succeeded\n");
4240 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4242 /* file opened for reading */
4243 handle = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
4245 SetLastError(0xdeadbeef);
4246 ret = pSetFileValidData(handle, 0);
4247 ok(!ret, "SetFileValidData succeeded\n");
4248 error = GetLastError();
4249 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4251 SetLastError(0xdeadbeef);
4252 ret = pSetFileValidData(handle, -1);
4253 error = GetLastError();
4254 ok(!ret, "SetFileValidData succeeded\n");
4255 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4256 CloseHandle(handle);
4258 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4260 SetLastError(0xdeadbeef);
4261 ret = pSetFileValidData(handle, 0);
4262 error = GetLastError();
4263 ok(!ret, "SetFileValidData succeeded\n");
4264 todo_wine ok(error == ERROR_PRIVILEGE_NOT_HELD, "got %u\n", error);
4265 CloseHandle(handle);
4267 privs.PrivilegeCount = 1;
4268 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
4270 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token) ||
4271 !LookupPrivilegeValueA(NULL, SE_MANAGE_VOLUME_NAME, &privs.Privileges[0].Luid) ||
4272 !AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL) ||
4273 GetLastError() == ERROR_NOT_ALL_ASSIGNED)
4275 win_skip("cannot enable SE_MANAGE_VOLUME_NAME privilege\n");
4276 CloseHandle(token);
4277 DeleteFileA(filename);
4278 return;
4280 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4282 SetLastError(0xdeadbeef);
4283 ret = pSetFileValidData(handle, 0);
4284 error = GetLastError();
4285 ok(!ret, "SetFileValidData succeeded\n");
4286 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4288 SetLastError(0xdeadbeef);
4289 ret = pSetFileValidData(handle, -1);
4290 error = GetLastError();
4291 ok(!ret, "SetFileValidData succeeded\n");
4292 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4294 SetLastError(0xdeadbeef);
4295 ret = pSetFileValidData(handle, 2);
4296 error = GetLastError();
4297 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4298 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4300 ret = pSetFileValidData(handle, 4);
4301 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4303 SetLastError(0xdeadbeef);
4304 ret = pSetFileValidData(handle, 8);
4305 error = GetLastError();
4306 ok(!ret, "SetFileValidData succeeded\n");
4307 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4309 count = SetFilePointer(handle, 1024, NULL, FILE_END);
4310 ok(count != INVALID_SET_FILE_POINTER, "SetFilePointer failed %u\n", GetLastError());
4311 ret = SetEndOfFile(handle);
4312 ok(ret, "SetEndOfFile failed %u\n", GetLastError());
4314 SetLastError(0xdeadbeef);
4315 ret = pSetFileValidData(handle, 2);
4316 error = GetLastError();
4317 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4318 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4320 ret = pSetFileValidData(handle, 4);
4321 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4323 ret = pSetFileValidData(handle, 8);
4324 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4326 ret = pSetFileValidData(handle, 4);
4327 error = GetLastError();
4328 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4329 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4331 ret = pSetFileValidData(handle, 1024);
4332 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4334 ret = pSetFileValidData(handle, 2048);
4335 error = GetLastError();
4336 ok(!ret, "SetFileValidData succeeded\n");
4337 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4339 privs.Privileges[0].Attributes = 0;
4340 AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL);
4342 CloseHandle(token);
4343 CloseHandle(handle);
4344 DeleteFileA(filename);
4347 static void test_WriteFileGather(void)
4349 char temp_path[MAX_PATH], filename[MAX_PATH];
4350 HANDLE hfile, hiocp1, hiocp2, evt;
4351 DWORD ret, size, tx;
4352 ULONG_PTR key;
4353 FILE_SEGMENT_ELEMENT fse[2];
4354 OVERLAPPED ovl, *povl = NULL;
4355 SYSTEM_INFO si;
4356 char *wbuf = NULL, *rbuf1, *rbuf2;
4357 BOOL br;
4359 evt = CreateEventW( NULL, TRUE, FALSE, NULL );
4361 ret = GetTempPathA( MAX_PATH, temp_path );
4362 ok( ret != 0, "GetTempPathA error %d\n", GetLastError() );
4363 ok( ret < MAX_PATH, "temp path should fit into MAX_PATH\n" );
4364 ret = GetTempFileNameA( temp_path, "wfg", 0, filename );
4365 ok( ret != 0, "GetTempFileNameA error %d\n", GetLastError() );
4367 hfile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
4368 FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL, 0 );
4369 ok( hfile != INVALID_HANDLE_VALUE, "CreateFile failed err %u\n", GetLastError() );
4370 if (hfile == INVALID_HANDLE_VALUE) return;
4372 hiocp1 = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 999, 0 );
4373 hiocp2 = CreateIoCompletionPort( hfile, hiocp1, 999, 0 );
4374 ok( hiocp2 != 0, "CreateIoCompletionPort failed err %u\n", GetLastError() );
4376 GetSystemInfo( &si );
4377 wbuf = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4378 ok( wbuf != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4380 rbuf1 = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4381 ok( rbuf1 != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4383 rbuf2 = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4384 ok( rbuf2 != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4386 memset( &ovl, 0, sizeof(ovl) );
4387 ovl.hEvent = evt;
4388 memset( fse, 0, sizeof(fse) );
4389 fse[0].Buffer = wbuf;
4390 memset( wbuf, 0x42, si.dwPageSize );
4391 SetLastError( 0xdeadbeef );
4392 if (!WriteFileGather( hfile, fse, si.dwPageSize, NULL, &ovl ))
4393 ok( GetLastError() == ERROR_IO_PENDING, "WriteFileGather failed err %u\n", GetLastError() );
4395 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4396 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4397 ok( povl == &ovl, "wrong ovl %p\n", povl );
4399 tx = 0;
4400 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4401 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4402 ok( tx == si.dwPageSize, "got unexpected bytes transferred: %u\n", tx );
4404 ResetEvent( evt );
4406 /* read exact size */
4407 memset( &ovl, 0, sizeof(ovl) );
4408 ovl.hEvent = evt;
4409 memset( fse, 0, sizeof(fse) );
4410 fse[0].Buffer = rbuf1;
4411 memset( rbuf1, 0, si.dwPageSize );
4412 SetLastError( 0xdeadbeef );
4413 br = ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl );
4414 ok( br == FALSE, "ReadFileScatter should be asynchronous\n" );
4415 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4417 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4418 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4419 ok( povl == &ovl, "wrong ovl %p\n", povl );
4421 tx = 0;
4422 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4423 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4424 ok( tx == si.dwPageSize, "got unexpected bytes transferred: %u\n", tx );
4426 ok( memcmp( rbuf1, wbuf, si.dwPageSize ) == 0,
4427 "data was not read into buffer\n" );
4429 ResetEvent( evt );
4431 /* start read at EOF */
4432 memset( &ovl, 0, sizeof(ovl) );
4433 ovl.hEvent = evt;
4434 S(U(ovl)).OffsetHigh = 0;
4435 S(U(ovl)).Offset = si.dwPageSize;
4436 memset( fse, 0, sizeof(fse) );
4437 fse[0].Buffer = rbuf1;
4438 SetLastError( 0xdeadbeef );
4439 br = ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl );
4440 ok( br == FALSE, "ReadFileScatter should have failed\n" );
4441 ok( GetLastError() == ERROR_HANDLE_EOF ||
4442 GetLastError() == ERROR_IO_PENDING, "ReadFileScatter gave wrong error %u\n", GetLastError() );
4443 if (GetLastError() == ERROR_IO_PENDING)
4445 SetLastError( 0xdeadbeef );
4446 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4447 ok( !ret, "GetQueuedCompletionStatus should have returned failure\n" );
4448 ok( GetLastError() == ERROR_HANDLE_EOF, "Got wrong error: %u\n", GetLastError() );
4449 ok( povl == &ovl, "wrong ovl %p\n", povl );
4451 SetLastError( 0xdeadbeef );
4452 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4453 ok( br == FALSE, "GetOverlappedResult should have failed\n" );
4454 ok( GetLastError() == ERROR_HANDLE_EOF, "Got wrong error: %u\n", GetLastError() );
4456 else
4458 SetLastError( 0xdeadbeef );
4459 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 100 );
4460 ok( !ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError() );
4461 ok( GetLastError() == WAIT_TIMEOUT, "GetQueuedCompletionStatus gave wrong error %u\n", GetLastError() );
4462 ok( povl == NULL, "wrong ovl %p\n", povl );
4465 ResetEvent( evt );
4467 /* read past EOF */
4468 memset( &ovl, 0, sizeof(ovl) );
4469 ovl.hEvent = evt;
4470 memset( fse, 0, sizeof(fse) );
4471 fse[0].Buffer = rbuf1;
4472 fse[1].Buffer = rbuf2;
4473 memset( rbuf1, 0, si.dwPageSize );
4474 memset( rbuf2, 0x17, si.dwPageSize );
4475 SetLastError( 0xdeadbeef );
4476 br = ReadFileScatter( hfile, fse, si.dwPageSize * 2, NULL, &ovl );
4477 ok( br == FALSE, "ReadFileScatter should be asynchronous\n" );
4478 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4480 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4481 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError() );
4482 ok( povl == &ovl, "wrong ovl %p\n", povl );
4484 tx = 0;
4485 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4486 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4487 ok( tx == si.dwPageSize, "got unexpected bytes transferred: %u\n", tx );
4489 ok( memcmp( rbuf1, wbuf, si.dwPageSize ) == 0,
4490 "data was not read into buffer\n" );
4491 memset( rbuf1, 0x17, si.dwPageSize );
4492 ok( memcmp( rbuf2, rbuf1, si.dwPageSize ) == 0,
4493 "data should not have been read into buffer\n" );
4495 ResetEvent( evt );
4497 /* partial page read */
4498 memset( &ovl, 0, sizeof(ovl) );
4499 ovl.hEvent = evt;
4500 memset( fse, 0, sizeof(fse) );
4501 fse[0].Buffer = rbuf1;
4502 memset( rbuf1, 0, si.dwPageSize );
4503 SetLastError( 0xdeadbeef );
4504 br = ReadFileScatter( hfile, fse, si.dwPageSize / 2, NULL, &ovl );
4505 ok( br == FALSE, "ReadFileScatter should be asynchronous\n" );
4506 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4508 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4509 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError() );
4510 ok( povl == &ovl, "wrong ovl %p\n", povl );
4512 tx = 0;
4513 br = GetOverlappedResult( hfile, &ovl, &tx, TRUE );
4514 ok( br == TRUE, "GetOverlappedResult failed: %u\n", GetLastError() );
4515 ok( tx == si.dwPageSize / 2, "got unexpected bytes transferred: %u\n", tx );
4517 ok( memcmp( rbuf1, wbuf, si.dwPageSize / 2 ) == 0,
4518 "invalid data was read into buffer\n" );
4519 memset( rbuf2, 0, si.dwPageSize );
4520 ok( memcmp( rbuf1 + si.dwPageSize / 2, rbuf2, si.dwPageSize - si.dwPageSize / 2 ) == 0,
4521 "invalid data was read into buffer\n" );
4523 CloseHandle( hfile );
4524 CloseHandle( hiocp1 );
4525 CloseHandle( hiocp2 );
4527 /* file handle must be overlapped */
4528 hfile = CreateFileA( filename, GENERIC_READ, 0, 0, OPEN_EXISTING,
4529 FILE_FLAG_NO_BUFFERING | FILE_ATTRIBUTE_NORMAL, 0 );
4530 ok( hfile != INVALID_HANDLE_VALUE, "CreateFile failed err %u\n", GetLastError() );
4532 memset( &ovl, 0, sizeof(ovl) );
4533 memset( fse, 0, sizeof(fse) );
4534 fse[0].Buffer = rbuf1;
4535 memset( rbuf1, 0, si.dwPageSize );
4536 SetLastError( 0xdeadbeef );
4537 br = ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl );
4538 ok( br == FALSE, "ReadFileScatter should fail\n" );
4539 ok( GetLastError() == ERROR_INVALID_PARAMETER, "ReadFileScatter failed err %u\n", GetLastError() );
4541 VirtualFree( wbuf, 0, MEM_RELEASE );
4542 VirtualFree( rbuf1, 0, MEM_RELEASE );
4543 VirtualFree( rbuf2, 0, MEM_RELEASE );
4544 CloseHandle( evt );
4545 DeleteFileA( filename );
4548 static unsigned file_map_access(unsigned access)
4550 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
4551 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
4552 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
4553 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
4554 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
4557 static BOOL is_access_compatible(unsigned obj_access, unsigned desired_access)
4559 obj_access = file_map_access(obj_access);
4560 desired_access = file_map_access(desired_access);
4561 return (obj_access & desired_access) == desired_access;
4564 static void test_file_access(void)
4566 static const struct
4568 unsigned access, create_error, write_error, read_error;
4569 } td[] =
4571 { GENERIC_READ | GENERIC_WRITE, 0, 0, 0 },
4572 { GENERIC_WRITE, 0, 0, ERROR_ACCESS_DENIED },
4573 { GENERIC_READ, 0, ERROR_ACCESS_DENIED, 0 },
4574 { FILE_READ_DATA | FILE_WRITE_DATA, 0, 0, 0 },
4575 { FILE_WRITE_DATA, 0, 0, ERROR_ACCESS_DENIED },
4576 { FILE_READ_DATA, 0, ERROR_ACCESS_DENIED, 0 },
4577 { FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4578 { FILE_READ_DATA | FILE_APPEND_DATA, 0, 0, 0 },
4579 { FILE_WRITE_DATA | FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4580 { 0, 0, ERROR_ACCESS_DENIED, ERROR_ACCESS_DENIED },
4582 char path[MAX_PATH], fname[MAX_PATH];
4583 unsigned char buf[16];
4584 HANDLE hfile, hdup;
4585 DWORD i, j, ret, bytes;
4587 GetTempPathA(MAX_PATH, path);
4588 GetTempFileNameA(path, "foo", 0, fname);
4590 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
4592 SetLastError(0xdeadbeef);
4593 hfile = CreateFileA(fname, td[i].access, 0, NULL, CREATE_ALWAYS,
4594 FILE_FLAG_DELETE_ON_CLOSE, 0);
4595 if (td[i].create_error)
4597 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
4598 ok(td[i].create_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].create_error, GetLastError());
4599 continue;
4601 else
4602 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
4604 for (j = 0; j < sizeof(td)/sizeof(td[0]); j++)
4606 SetLastError(0xdeadbeef);
4607 ret = DuplicateHandle(GetCurrentProcess(), hfile, GetCurrentProcess(), &hdup,
4608 td[j].access, 0, 0);
4609 if (is_access_compatible(td[i].access, td[j].access))
4610 ok(ret, "DuplicateHandle(%#x => %#x) error %d\n", td[i].access, td[j].access, GetLastError());
4611 else
4613 /* FIXME: Remove once Wine is fixed */
4614 todo_wine_if((td[j].access & (GENERIC_READ | GENERIC_WRITE) ||
4615 (!(td[i].access & (GENERIC_WRITE | FILE_WRITE_DATA)) && (td[j].access & FILE_WRITE_DATA)) ||
4616 (!(td[i].access & (GENERIC_READ | FILE_READ_DATA)) && (td[j].access & FILE_READ_DATA)) ||
4617 (!(td[i].access & (GENERIC_WRITE)) && (td[j].access & FILE_APPEND_DATA))))
4619 ok(!ret, "DuplicateHandle(%#x => %#x) should fail\n", td[i].access, td[j].access);
4620 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4623 if (ret) CloseHandle(hdup);
4626 SetLastError(0xdeadbeef);
4627 bytes = 0xdeadbeef;
4628 ret = WriteFile(hfile, "\x5e\xa7", 2, &bytes, NULL);
4629 if (td[i].write_error)
4631 ok(!ret, "%d: WriteFile should fail\n", i);
4632 ok(td[i].write_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].write_error, GetLastError());
4633 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4635 else
4637 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
4638 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4641 SetLastError(0xdeadbeef);
4642 ret = SetFilePointer(hfile, 0, NULL, FILE_BEGIN);
4643 ok(ret != INVALID_SET_FILE_POINTER, "SetFilePointer error %d\n", GetLastError());
4645 SetLastError(0xdeadbeef);
4646 bytes = 0xdeadbeef;
4647 ret = ReadFile(hfile, buf, sizeof(buf), &bytes, NULL);
4648 if (td[i].read_error)
4650 ok(!ret, "%d: ReadFile should fail\n", i);
4651 ok(td[i].read_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].read_error, GetLastError());
4652 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4654 else
4656 ok(ret, "%d: ReadFile error %d\n", i, GetLastError());
4657 if (td[i].write_error)
4658 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4659 else
4661 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4662 ok(buf[0] == 0x5e && buf[1] == 0xa7, "%d: expected 5ea7, got %02x%02x\n", i, buf[0], buf[1]);
4666 CloseHandle(hfile);
4670 static void test_GetFinalPathNameByHandleA(void)
4672 static char prefix[] = "GetFinalPathNameByHandleA";
4673 static char dos_prefix[] = "\\\\?\\";
4674 char temp_path[MAX_PATH], test_path[MAX_PATH];
4675 char long_path[MAX_PATH], result_path[MAX_PATH];
4676 char dos_path[MAX_PATH + sizeof(dos_prefix)];
4677 HANDLE file;
4678 DWORD count;
4679 UINT ret;
4681 if (!pGetFinalPathNameByHandleA)
4683 skip("GetFinalPathNameByHandleA is missing\n");
4684 return;
4687 /* Test calling with INVALID_HANDLE_VALUE */
4688 SetLastError(0xdeadbeaf);
4689 count = pGetFinalPathNameByHandleA(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4690 ok(count == 0, "Expected length 0, got %u\n", count);
4691 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4693 count = GetTempPathA(MAX_PATH, temp_path);
4694 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4695 ret = GetTempFileNameA(temp_path, prefix, 0, test_path);
4696 ok(ret != 0, "GetTempFileNameA error %u\n", GetLastError());
4697 ret = GetLongPathNameA(test_path, long_path, MAX_PATH);
4698 ok(ret != 0, "GetLongPathNameA error %u\n", GetLastError());
4699 strcpy(dos_path, dos_prefix);
4700 strcat(dos_path, long_path);
4702 count = pGetFinalPathNameByHandleA(INVALID_HANDLE_VALUE, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4703 ok(count == 0, "Expected length 0, got %u\n", count);
4704 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4706 file = CreateFileA(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4707 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4708 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error %u\n", GetLastError());
4710 if (0) {
4711 /* Windows crashes on NULL path */
4712 count = pGetFinalPathNameByHandleA(file, NULL, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4713 ok(count == 0, "Expected length 0, got %u\n", count);
4714 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4717 /* Test 0-length path */
4718 count = pGetFinalPathNameByHandleA(file, result_path, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4719 ok(count == strlen(dos_path), "Expected length %u, got %u\n", lstrlenA(dos_path), count);
4721 /* Test 0 and NULL path */
4722 count = pGetFinalPathNameByHandleA(file, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4723 ok(count == strlen(dos_path), "Expected length %u, got %u\n", lstrlenA(dos_path), count);
4725 /* Test VOLUME_NAME_DOS with sufficient buffer size */
4726 memset(result_path, 0x11, sizeof(result_path));
4727 count = pGetFinalPathNameByHandleA(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4728 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4729 ok(lstrcmpiA(dos_path, result_path) == 0, "Expected %s, got %s\n", dos_path, result_path);
4731 /* Test VOLUME_NAME_DOS with insufficient buffer size */
4732 memset(result_path, 0x11, sizeof(result_path));
4733 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-2, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4734 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4735 ok(result_path[0] == 0x11, "Result path was modified\n");
4737 memset(result_path, 0x11, sizeof(result_path));
4738 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4739 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4740 ok(result_path[0] == 0x11, "Result path was modified\n");
4742 memset(result_path, 0x11, sizeof(result_path));
4743 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4744 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4745 ok(result_path[0] == 0x11, "Result path was modified\n");
4747 memset(result_path, 0x11, sizeof(result_path));
4748 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4749 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4750 ok(result_path[0] != 0x11, "Result path was not modified\n");
4751 ok(!result_path[strlen(dos_path)], "Expected nullterminated string\n");
4752 ok(result_path[strlen(dos_path)+1] == 0x11, "Buffer overflow\n");
4754 CloseHandle(file);
4757 static void test_GetFinalPathNameByHandleW(void)
4759 static WCHAR prefix[] = {'G','e','t','F','i','n','a','l','P','a','t','h',
4760 'N','a','m','e','B','y','H','a','n','d','l','e','W','\0'};
4761 static WCHAR dos_prefix[] = {'\\','\\','?','\\','\0'};
4762 WCHAR temp_path[MAX_PATH], test_path[MAX_PATH];
4763 WCHAR long_path[MAX_PATH], result_path[MAX_PATH];
4764 WCHAR dos_path[MAX_PATH + sizeof(dos_prefix)];
4765 WCHAR drive_part[MAX_PATH];
4766 WCHAR *file_part;
4767 WCHAR volume_path[MAX_PATH + 50];
4768 WCHAR nt_path[2 * MAX_PATH];
4769 BOOL success;
4770 HANDLE file;
4771 DWORD count;
4772 UINT ret;
4774 if (!pGetFinalPathNameByHandleW)
4776 skip("GetFinalPathNameByHandleW is missing\n");
4777 return;
4780 /* Test calling with INVALID_HANDLE_VALUE */
4781 SetLastError(0xdeadbeaf);
4782 count = pGetFinalPathNameByHandleW(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4783 ok(count == 0, "Expected length 0, got %u\n", count);
4784 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4786 count = pGetFinalPathNameByHandleW(INVALID_HANDLE_VALUE, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4787 ok(count == 0, "Expected length 0, got %u\n", count);
4788 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4790 count = GetTempPathW(MAX_PATH, temp_path);
4791 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4792 ret = GetTempFileNameW(temp_path, prefix, 0, test_path);
4793 ok(ret != 0, "GetTempFileNameW error %u\n", GetLastError());
4794 ret = GetLongPathNameW(test_path, long_path, MAX_PATH);
4795 ok(ret != 0, "GetLongPathNameW error %u\n", GetLastError());
4796 lstrcpyW(dos_path, dos_prefix);
4797 lstrcatW(dos_path, long_path);
4799 file = CreateFileW(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4800 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4801 ok(file != INVALID_HANDLE_VALUE, "CreateFileW error %u\n", GetLastError());
4803 if (0) {
4804 /* Windows crashes on NULL path */
4805 count = pGetFinalPathNameByHandleW(file, NULL, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4806 ok(count == 0, "Expected length 0, got %u\n", count);
4807 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4810 /* Test 0-length path */
4811 count = pGetFinalPathNameByHandleW(file, result_path, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4812 ok(count == lstrlenW(dos_path) + 1 ||
4813 broken(count == lstrlenW(dos_path) + 2), "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4815 /* Test 0 and NULL path */
4816 count = pGetFinalPathNameByHandleW(file, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4817 ok(count == lstrlenW(dos_path) + 1 ||
4818 broken(count == lstrlenW(dos_path) + 2), "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4820 /* Test VOLUME_NAME_DOS with sufficient buffer size */
4821 memset(result_path, 0x11, sizeof(result_path));
4822 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4823 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
4824 ok(lstrcmpiW(dos_path, result_path) == 0, "Expected %s, got %s\n", wine_dbgstr_w(dos_path), wine_dbgstr_w(result_path));
4826 /* Test VOLUME_NAME_DOS with insufficient buffer size */
4827 memset(result_path, 0x11, sizeof(result_path));
4828 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4829 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4830 ok(result_path[0] == 0x1111, "Result path was modified\n");
4832 memset(result_path, 0x11, sizeof(result_path));
4833 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4834 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4835 ok(result_path[0] == 0x1111, "Result path was modified\n");
4837 memset(result_path, 0x11, sizeof(result_path));
4838 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4839 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
4840 ok(result_path[0] != 0x1111, "Result path was not modified\n");
4841 ok(!result_path[lstrlenW(dos_path)], "Expected nullterminated string\n");
4842 ok(result_path[lstrlenW(dos_path)+1] == 0x1111, "Buffer overflow\n");
4844 success = GetVolumePathNameW(long_path, drive_part, MAX_PATH);
4845 ok(success, "GetVolumePathNameW error %u\n", GetLastError());
4846 success = GetVolumeNameForVolumeMountPointW(drive_part, volume_path, sizeof(volume_path) / sizeof(WCHAR));
4847 ok(success, "GetVolumeNameForVolumeMountPointW error %u\n", GetLastError());
4849 /* Test for VOLUME_NAME_GUID */
4850 lstrcatW(volume_path, long_path + lstrlenW(drive_part));
4851 memset(result_path, 0x11, sizeof(result_path));
4852 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_GUID);
4853 ok(count == lstrlenW(volume_path), "Expected length %u, got %u\n", lstrlenW(volume_path), count);
4854 ok(lstrcmpiW(volume_path, result_path) == 0, "Expected %s, got %s\n",
4855 wine_dbgstr_w(volume_path), wine_dbgstr_w(result_path));
4857 /* Test for VOLUME_NAME_NONE */
4858 file_part = long_path + lstrlenW(drive_part) - 1;
4859 memset(result_path, 0x11, sizeof(result_path));
4860 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NONE);
4861 ok(count == lstrlenW(file_part), "Expected length %u, got %u\n", lstrlenW(file_part), count);
4862 ok(lstrcmpiW(file_part, result_path) == 0, "Expected %s, got %s\n",
4863 wine_dbgstr_w(file_part), wine_dbgstr_w(result_path));
4865 drive_part[lstrlenW(drive_part)-1] = 0;
4866 success = QueryDosDeviceW(drive_part, nt_path, sizeof(nt_path) / sizeof(WCHAR));
4867 ok(success, "QueryDosDeviceW error %u\n", GetLastError());
4869 /* Test for VOLUME_NAME_NT */
4870 lstrcatW(nt_path, file_part);
4871 memset(result_path, 0x11, sizeof(result_path));
4872 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NT);
4873 ok(count == lstrlenW(nt_path), "Expected length %u, got %u\n", lstrlenW(nt_path), count);
4874 ok(lstrcmpiW(nt_path, result_path) == 0, "Expected %s, got %s\n",
4875 wine_dbgstr_w(nt_path), wine_dbgstr_w(result_path));
4877 CloseHandle(file);
4880 static void test_SetFileInformationByHandle(void)
4882 FILE_ATTRIBUTE_TAG_INFO fileattrinfo = { 0 };
4883 FILE_REMOTE_PROTOCOL_INFO protinfo = { 0 };
4884 FILE_STANDARD_INFO stdinfo = { };
4885 FILE_COMPRESSION_INFO compressinfo;
4886 FILE_DISPOSITION_INFO dispinfo;
4887 char tempFileName[MAX_PATH];
4888 char tempPath[MAX_PATH];
4889 HANDLE file;
4890 BOOL ret;
4892 if (!pSetFileInformationByHandle)
4894 win_skip("SetFileInformationByHandle is not supported\n");
4895 return;
4898 ret = GetTempPathA(sizeof(tempPath), tempPath);
4899 ok(ret, "GetTempPathA failed, got error %u.\n", GetLastError());
4901 /* ensure the existence of a file in the temp folder */
4902 ret = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4903 ok(ret, "GetTempFileNameA failed, got error %u.\n", GetLastError());
4905 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4906 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
4907 ok(file != INVALID_HANDLE_VALUE, "failed to open the temp file, error %u.\n", GetLastError());
4909 /* invalid classes */
4910 SetLastError(0xdeadbeef);
4911 ret = pSetFileInformationByHandle(file, FileStandardInfo, &stdinfo, sizeof(stdinfo));
4912 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4914 memset(&compressinfo, 0, sizeof(compressinfo));
4915 SetLastError(0xdeadbeef);
4916 ret = pSetFileInformationByHandle(file, FileCompressionInfo, &compressinfo, sizeof(compressinfo));
4917 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4919 SetLastError(0xdeadbeef);
4920 ret = pSetFileInformationByHandle(file, FileAttributeTagInfo, &fileattrinfo, sizeof(fileattrinfo));
4921 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4923 memset(&protinfo, 0, sizeof(protinfo));
4924 protinfo.StructureVersion = 1;
4925 protinfo.StructureSize = sizeof(protinfo);
4926 SetLastError(0xdeadbeef);
4927 ret = pSetFileInformationByHandle(file, FileRemoteProtocolInfo, &protinfo, sizeof(protinfo));
4928 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4930 /* test FileDispositionInfo, additional details already covered by ntdll tests */
4931 SetLastError(0xdeadbeef);
4932 ret = pSetFileInformationByHandle(file, FileDispositionInfo, &dispinfo, 0);
4933 todo_wine
4934 ok(!ret && GetLastError() == ERROR_BAD_LENGTH, "got %d, error %d\n", ret, GetLastError());
4936 dispinfo.DeleteFile = TRUE;
4937 ret = pSetFileInformationByHandle(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
4938 ok(ret, "setting FileDispositionInfo failed, error %d\n", GetLastError());
4940 CloseHandle(file);
4943 static void test_GetFileAttributesExW(void)
4945 static const WCHAR path1[] = {'\\','\\','?','\\',0};
4946 static const WCHAR path2[] = {'\\','?','?','\\',0};
4947 static const WCHAR path3[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
4948 WIN32_FILE_ATTRIBUTE_DATA info;
4949 BOOL ret;
4951 SetLastError(0xdeadbeef);
4952 ret = GetFileAttributesExW(path1, GetFileExInfoStandard, &info);
4953 ok(!ret, "GetFileAttributesExW succeeded\n");
4954 ok(GetLastError() == ERROR_INVALID_NAME, "Expected error ERROR_INVALID_NAME, got %u\n", GetLastError());
4956 SetLastError(0xdeadbeef);
4957 ret = GetFileAttributesExW(path2, GetFileExInfoStandard, &info);
4958 ok(!ret, "GetFileAttributesExW succeeded\n");
4959 ok(GetLastError() == ERROR_INVALID_NAME, "Expected error ERROR_INVALID_NAME, got %u\n", GetLastError());
4961 SetLastError(0xdeadbeef);
4962 ret = GetFileAttributesExW(path3, GetFileExInfoStandard, &info);
4963 ok(!ret, "GetFileAttributesExW succeeded\n");
4964 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
4967 START_TEST(file)
4969 InitFunctionPointers();
4971 test__hread( );
4972 test__hwrite( );
4973 test__lclose( );
4974 test__lcreat( );
4975 test__llseek( );
4976 test__llopen( );
4977 test__lread( );
4978 test__lwrite( );
4979 test_GetTempFileNameA();
4980 test_CopyFileA();
4981 test_CopyFileW();
4982 test_CopyFile2();
4983 test_CopyFileEx();
4984 test_CreateFile();
4985 test_CreateFileA();
4986 test_CreateFileW();
4987 test_CreateFile2();
4988 test_DeleteFileA();
4989 test_DeleteFileW();
4990 test_MoveFileA();
4991 test_MoveFileW();
4992 test_FindFirstFileA();
4993 test_FindNextFileA();
4994 test_FindFirstFile_wildcards();
4995 test_FindFirstFileExA(FindExInfoStandard, 0, 0);
4996 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_CASE_SENSITIVE);
4997 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_LARGE_FETCH);
4998 test_FindFirstFileExA(FindExInfoBasic, 0, 0);
4999 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
5000 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, 0);
5001 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_CASE_SENSITIVE);
5002 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_LARGE_FETCH);
5003 test_FindFirstFileExA(FindExInfoBasic, FindExSearchLimitToDirectories, 0);
5004 test_LockFile();
5005 test_file_sharing();
5006 test_offset_in_overlapped_structure();
5007 test_MapFile();
5008 test_GetFileType();
5009 test_async_file_errors();
5010 test_read_write();
5011 test_OpenFile();
5012 test_overlapped();
5013 test_RemoveDirectory();
5014 test_ReplaceFileA();
5015 test_ReplaceFileW();
5016 test_GetFileInformationByHandleEx();
5017 test_OpenFileById();
5018 test_SetFileValidData();
5019 test_WriteFileGather();
5020 test_file_access();
5021 test_GetFinalPathNameByHandleA();
5022 test_GetFinalPathNameByHandleW();
5023 test_SetFileInformationByHandle();
5024 test_GetFileAttributesExW();