kernel32: Improve stub for SetFileInformationByHandle.
[wine.git] / dlls / kernel32 / tests / file.c
blobfb4cd9b26d12c034a9699ed08c9d2d00a5f9ea02
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 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
42 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
43 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
44 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
45 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
46 static DWORD (WINAPI *pQueueUserAPC)(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
47 static BOOL (WINAPI *pGetFileInformationByHandleEx)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, LPVOID, DWORD);
48 static HANDLE (WINAPI *pOpenFileById)(HANDLE, LPFILE_ID_DESCRIPTOR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD);
49 static BOOL (WINAPI *pSetFileValidData)(HANDLE, LONGLONG);
50 static HRESULT (WINAPI *pCopyFile2)(PCWSTR,PCWSTR,COPYFILE2_EXTENDED_PARAMETERS*);
51 static HANDLE (WINAPI *pCreateFile2)(LPCWSTR, DWORD, DWORD, DWORD, CREATEFILE2_EXTENDED_PARAMETERS*);
52 static DWORD (WINAPI *pGetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, DWORD);
53 static DWORD (WINAPI *pGetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, DWORD);
54 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
55 PLARGE_INTEGER, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG);
56 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)(LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR*);
57 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PCANSI_STRING, BOOLEAN);
58 static BOOL (WINAPI *pSetFileInformationByHandle)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, void*, DWORD);
60 static const char filename[] = "testfile.xxx";
61 static const char sillytext[] =
62 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
63 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
64 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
65 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
66 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
67 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
68 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
69 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
70 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
71 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
73 struct test_list {
74 const char *file; /* file string to test */
75 const DWORD err; /* Win NT and further error code */
76 const LONG err2; /* Win 9x & ME error code or -1 */
77 const DWORD options; /* option flag to use for open */
78 const BOOL todo_flag; /* todo_wine indicator */
79 } ;
81 static void InitFunctionPointers(void)
83 HMODULE hntdll = GetModuleHandleA("ntdll");
84 HMODULE hkernel32 = GetModuleHandleA("kernel32");
86 pNtCreateFile = (void *)GetProcAddress(hntdll, "NtCreateFile");
87 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
88 pRtlAnsiStringToUnicodeString = (void *)GetProcAddress(hntdll, "RtlAnsiStringToUnicodeString");
90 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
91 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
92 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
93 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
94 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
95 pQueueUserAPC = (void *) GetProcAddress(hkernel32, "QueueUserAPC");
96 pGetFileInformationByHandleEx = (void *) GetProcAddress(hkernel32, "GetFileInformationByHandleEx");
97 pOpenFileById = (void *) GetProcAddress(hkernel32, "OpenFileById");
98 pSetFileValidData = (void *) GetProcAddress(hkernel32, "SetFileValidData");
99 pCopyFile2 = (void *) GetProcAddress(hkernel32, "CopyFile2");
100 pCreateFile2 = (void *) GetProcAddress(hkernel32, "CreateFile2");
101 pGetFinalPathNameByHandleA = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleA");
102 pGetFinalPathNameByHandleW = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleW");
103 pSetFileInformationByHandle = (void *) GetProcAddress(hkernel32, "SetFileInformationByHandle");
106 static void test__hread( void )
108 HFILE filehandle;
109 char buffer[10000];
110 LONG bytes_read;
111 LONG bytes_wanted;
112 LONG i;
113 BOOL ret;
115 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
116 DeleteFileA( filename );
117 filehandle = _lcreat( filename, 0 );
118 if (filehandle == HFILE_ERROR)
120 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
121 return;
124 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
126 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
128 filehandle = _lopen( filename, OF_READ );
130 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
132 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
134 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
136 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
138 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
139 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
140 for (i = 0; i < bytes_wanted; i++)
142 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
146 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
148 ret = DeleteFileA( filename );
149 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
153 static void test__hwrite( void )
155 HFILE filehandle;
156 char buffer[10000];
157 LONG bytes_read;
158 LONG bytes_written;
159 ULONG blocks;
160 LONG i;
161 char *contents;
162 HLOCAL memory_object;
163 char checksum[1];
164 BOOL ret;
166 filehandle = _lcreat( filename, 0 );
167 if (filehandle == HFILE_ERROR)
169 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
170 return;
173 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
175 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
177 filehandle = _lopen( filename, OF_READ );
179 bytes_read = _hread( filehandle, buffer, 1);
181 ok( 0 == bytes_read, "file read size error\n" );
183 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
185 filehandle = _lopen( filename, OF_READWRITE );
187 bytes_written = 0;
188 checksum[0] = '\0';
189 srand( (unsigned)time( NULL ) );
190 for (blocks = 0; blocks < 100; blocks++)
192 for (i = 0; i < (LONG)sizeof( buffer ); i++)
194 buffer[i] = rand( );
195 checksum[0] = checksum[0] + buffer[i];
197 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
198 bytes_written = bytes_written + sizeof( buffer );
201 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
202 bytes_written++;
204 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
206 memory_object = LocalAlloc( LPTR, bytes_written );
208 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
210 contents = LocalLock( memory_object );
211 ok( NULL != contents, "LocalLock whines\n" );
213 filehandle = _lopen( filename, OF_READ );
215 contents = LocalLock( memory_object );
216 ok( NULL != contents, "LocalLock whines\n" );
218 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
220 checksum[0] = '\0';
221 i = 0;
224 checksum[0] = checksum[0] + contents[i];
225 i++;
227 while (i < bytes_written - 1);
229 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
231 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
233 ret = DeleteFileA( filename );
234 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
236 LocalFree( contents );
240 static void test__lclose( void )
242 HFILE filehandle;
243 BOOL ret;
245 filehandle = _lcreat( filename, 0 );
246 if (filehandle == HFILE_ERROR)
248 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
249 return;
252 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
254 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
256 ret = DeleteFileA( filename );
257 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
260 /* helper function for test__lcreat */
261 static void get_nt_pathW( const char *name, UNICODE_STRING *nameW )
263 UNICODE_STRING strW;
264 ANSI_STRING str;
265 NTSTATUS status;
266 BOOLEAN ret;
267 RtlInitAnsiString( &str, name );
269 status = pRtlAnsiStringToUnicodeString( &strW, &str, TRUE );
270 ok( !status, "RtlAnsiStringToUnicodeString failed with %08x\n", status );
272 ret = pRtlDosPathNameToNtPathName_U( strW.Buffer, nameW, NULL, NULL );
273 ok( ret, "RtlDosPathNameToNtPathName_U failed\n" );
275 RtlFreeUnicodeString( &strW );
278 static void test__lcreat( void )
280 UNICODE_STRING filenameW;
281 OBJECT_ATTRIBUTES attr;
282 IO_STATUS_BLOCK io;
283 HFILE filehandle;
284 char buffer[10000];
285 WIN32_FIND_DATAA search_results;
286 char slashname[] = "testfi/";
287 int err;
288 HANDLE find, file;
289 NTSTATUS status;
290 BOOL ret;
292 filehandle = _lcreat( filename, 0 );
293 if (filehandle == HFILE_ERROR)
295 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
296 return;
299 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
301 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
303 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
305 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
307 find = FindFirstFileA( filename, &search_results );
308 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
309 FindClose( find );
311 ret = DeleteFileA(filename);
312 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
314 filehandle = _lcreat( filename, 1 ); /* readonly */
315 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
317 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
319 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
321 find = FindFirstFileA( filename, &search_results );
322 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
323 FindClose( find );
325 SetLastError( 0xdeadbeef );
326 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
327 ok( GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError() );
329 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
331 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
333 filehandle = _lcreat( filename, 1 ); /* readonly */
334 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError() );
335 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen(sillytext) ),
336 "_hwrite shouldn't be able to write never the less\n" );
337 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
339 find = FindFirstFileA( filename, &search_results );
340 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
341 FindClose( find );
343 get_nt_pathW( filename, &filenameW );
344 attr.Length = sizeof(attr);
345 attr.RootDirectory = 0;
346 attr.Attributes = OBJ_CASE_INSENSITIVE;
347 attr.ObjectName = &filenameW;
348 attr.SecurityDescriptor = NULL;
349 attr.SecurityQualityOfService = NULL;
351 status = NtCreateFile( &file, GENERIC_READ | GENERIC_WRITE | DELETE, &attr, &io, NULL, 0,
352 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
353 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
354 ok( status == STATUS_ACCESS_DENIED, "expected STATUS_ACCESS_DENIED, got %08x\n", status );
355 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
357 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
358 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
359 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
360 todo_wine
361 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
362 if (!status) CloseHandle( file );
364 RtlFreeUnicodeString( &filenameW );
366 todo_wine
367 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
368 todo_wine
369 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
370 todo_wine
371 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file\n" );
373 filehandle = _lcreat( filename, 2 );
374 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
376 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
378 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
380 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
382 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
384 find = FindFirstFileA( filename, &search_results );
385 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
386 FindClose( find );
388 ret = DeleteFileA( filename );
389 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
391 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
392 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
394 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
396 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
398 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
400 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
402 find = FindFirstFileA( filename, &search_results );
403 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
404 FindClose( find );
406 ret = DeleteFileA( filename );
407 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
409 filehandle=_lcreat (slashname, 0); /* illegal name */
410 if (HFILE_ERROR==filehandle) {
411 err=GetLastError ();
412 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
413 "creating file \"%s\" failed with error %d\n", slashname, err);
414 } else { /* only NT succeeds */
415 _lclose(filehandle);
416 find=FindFirstFileA (slashname, &search_results);
417 if (INVALID_HANDLE_VALUE!=find)
419 ret = FindClose (find);
420 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
421 slashname[strlen(slashname)-1]=0;
422 ok (!strcmp (slashname, search_results.cFileName),
423 "found unexpected name \"%s\"\n", search_results.cFileName);
424 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
425 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
426 search_results.dwFileAttributes);
428 ret = DeleteFileA( slashname );
429 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
432 filehandle=_lcreat (filename, 8); /* illegal attribute */
433 if (HFILE_ERROR==filehandle)
434 ok (0, "couldn't create volume label \"%s\"\n", filename);
435 else {
436 _lclose(filehandle);
437 find=FindFirstFileA (filename, &search_results);
438 if (INVALID_HANDLE_VALUE==find)
439 ok (0, "file \"%s\" not found\n", filename);
440 else {
441 ret = FindClose(find);
442 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
443 ok (!strcmp (filename, search_results.cFileName),
444 "found unexpected name \"%s\"\n", search_results.cFileName);
445 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
446 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
447 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
448 search_results.dwFileAttributes);
450 ret = DeleteFileA( filename );
451 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
456 static void test__llseek( void )
458 INT i;
459 HFILE filehandle;
460 char buffer[1];
461 LONG bytes_read;
462 BOOL ret;
464 filehandle = _lcreat( filename, 0 );
465 if (filehandle == HFILE_ERROR)
467 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
468 return;
471 for (i = 0; i < 400; i++)
473 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
475 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
476 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
478 bytes_read = _hread( filehandle, buffer, 1);
479 ok( 1 == bytes_read, "file read size error\n" );
480 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
481 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
483 bytes_read = _hread( filehandle, buffer, 1);
484 ok( 1 == bytes_read, "file read size error\n" );
485 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
486 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
487 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
489 ret = DeleteFileA( filename );
490 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
494 static void test__llopen( void )
496 HFILE filehandle;
497 UINT bytes_read;
498 char buffer[10000];
499 BOOL ret;
501 filehandle = _lcreat( filename, 0 );
502 if (filehandle == HFILE_ERROR)
504 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
505 return;
508 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
509 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
511 filehandle = _lopen( filename, OF_READ );
512 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
513 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
514 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
515 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
517 filehandle = _lopen( filename, OF_READWRITE );
518 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
519 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
520 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
521 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
523 filehandle = _lopen( filename, OF_WRITE );
524 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
525 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
526 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
528 ret = DeleteFileA( filename );
529 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
530 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
534 static void test__lread( void )
536 HFILE filehandle;
537 char buffer[10000];
538 UINT bytes_read;
539 UINT bytes_wanted;
540 UINT i;
541 BOOL ret;
543 filehandle = _lcreat( filename, 0 );
544 if (filehandle == HFILE_ERROR)
546 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
547 return;
550 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
552 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
554 filehandle = _lopen( filename, OF_READ );
556 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
558 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
560 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
562 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
564 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
565 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
566 for (i = 0; i < bytes_wanted; i++)
568 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
572 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
574 ret = DeleteFileA( filename );
575 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
579 static void test__lwrite( void )
581 HFILE filehandle;
582 char buffer[10000];
583 UINT bytes_read;
584 UINT bytes_written;
585 UINT blocks;
586 INT i;
587 char *contents;
588 HLOCAL memory_object;
589 char checksum[1];
590 BOOL ret;
592 filehandle = _lcreat( filename, 0 );
593 if (filehandle == HFILE_ERROR)
595 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
596 return;
599 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
601 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
603 filehandle = _lopen( filename, OF_READ );
605 bytes_read = _hread( filehandle, buffer, 1);
607 ok( 0 == bytes_read, "file read size error\n" );
609 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
611 filehandle = _lopen( filename, OF_READWRITE );
613 bytes_written = 0;
614 checksum[0] = '\0';
615 srand( (unsigned)time( NULL ) );
616 for (blocks = 0; blocks < 100; blocks++)
618 for (i = 0; i < (INT)sizeof( buffer ); i++)
620 buffer[i] = rand( );
621 checksum[0] = checksum[0] + buffer[i];
623 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
624 bytes_written = bytes_written + sizeof( buffer );
627 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
628 bytes_written++;
630 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
632 memory_object = LocalAlloc( LPTR, bytes_written );
634 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
636 contents = LocalLock( memory_object );
637 ok( NULL != contents, "LocalLock whines\n" );
639 filehandle = _lopen( filename, OF_READ );
641 contents = LocalLock( memory_object );
642 ok( NULL != contents, "LocalLock whines\n" );
644 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
646 checksum[0] = '\0';
647 i = 0;
650 checksum[0] += contents[i];
651 i++;
653 while (i < bytes_written - 1);
655 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
657 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
659 ret = DeleteFileA( filename );
660 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
662 LocalFree( contents );
665 static void test_CopyFileA(void)
667 char temp_path[MAX_PATH];
668 char source[MAX_PATH], dest[MAX_PATH];
669 static const char prefix[] = "pfx";
670 HANDLE hfile;
671 HANDLE hmapfile;
672 FILETIME ft1, ft2;
673 char buf[10];
674 DWORD ret;
675 BOOL retok;
677 ret = GetTempPathA(MAX_PATH, temp_path);
678 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
679 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
681 ret = GetTempFileNameA(temp_path, prefix, 0, source);
682 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
684 ret = MoveFileA(source, source);
685 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
687 /* copying a file to itself must fail */
688 retok = CopyFileA(source, source, FALSE);
689 ok( !retok && (GetLastError() == ERROR_SHARING_VIOLATION || broken(GetLastError() == ERROR_FILE_EXISTS) /* Win 9x */),
690 "copying a file to itself didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
692 /* make the source have not zero size */
693 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
694 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
695 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
696 ok( retok && ret == sizeof(prefix),
697 "WriteFile error %d\n", GetLastError());
698 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
699 /* get the file time and change it to prove the difference */
700 ret = GetFileTime(hfile, NULL, NULL, &ft1);
701 ok( ret, "GetFileTime error %d\n", GetLastError());
702 ft1.dwLowDateTime -= 600000000; /* 60 second */
703 ret = SetFileTime(hfile, NULL, NULL, &ft1);
704 ok( ret, "SetFileTime error %d\n", GetLastError());
705 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
706 CloseHandle(hfile);
708 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
709 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
711 SetLastError(0xdeadbeef);
712 ret = CopyFileA(source, dest, TRUE);
713 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
714 "CopyFileA: unexpected error %d\n", GetLastError());
716 ret = CopyFileA(source, dest, FALSE);
717 ok(ret, "CopyFileA: error %d\n", GetLastError());
719 /* copying from a read-locked source fails */
720 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
721 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
722 retok = CopyFileA(source, dest, FALSE);
723 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
724 "copying from a read-locked file succeeded when it shouldn't have\n");
725 /* in addition, the source is opened before the destination */
726 retok = CopyFileA("25f99d3b-4ba4-4f66-88f5-2906886993cc", dest, FALSE);
727 ok(!retok && GetLastError() == ERROR_FILE_NOT_FOUND,
728 "copying from a file that doesn't exist failed in an unexpected way (ret=%d, err=%d)\n", retok, GetLastError());
729 CloseHandle(hfile);
731 /* copying from a r+w opened, r shared source succeeds */
732 hfile = CreateFileA(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
733 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
734 retok = CopyFileA(source, dest, FALSE);
735 ok(retok,
736 "copying from an r+w opened and r shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
737 CloseHandle(hfile);
739 /* copying from a delete-locked source mostly succeeds */
740 hfile = CreateFileA(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
741 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
742 retok = CopyFileA(source, dest, FALSE);
743 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* NT, 2000, XP */,
744 "copying from a delete-locked file failed (ret=%d, err=%d)\n", retok, GetLastError());
745 CloseHandle(hfile);
747 /* copying to a write-locked destination fails */
748 hfile = CreateFileA(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
749 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
750 retok = CopyFileA(source, dest, FALSE);
751 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
752 "copying to a write-locked file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
753 CloseHandle(hfile);
755 /* copying to a r+w opened, w shared destination mostly succeeds */
756 hfile = CreateFileA(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
757 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
758 retok = CopyFileA(source, dest, FALSE);
759 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
760 "copying to a r+w opened and w shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
761 CloseHandle(hfile);
763 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
764 hfile = CreateFileA(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
765 ok(hfile != INVALID_HANDLE_VALUE || broken(GetLastError() == ERROR_INVALID_PARAMETER) /* Win 9x */,
766 "failed to open destination file, error %d\n", GetLastError());
767 if (hfile != INVALID_HANDLE_VALUE)
769 retok = CopyFileA(source, dest, FALSE);
770 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
771 "copying to a delete-locked shared file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
772 CloseHandle(hfile);
775 /* copy to a file that's opened the way Wine opens the source */
776 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
777 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
778 retok = CopyFileA(source, dest, FALSE);
779 ok(retok || broken(GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
780 "copying to a file opened the way Wine opens the source failed (ret=%d, err=%d)\n", retok, GetLastError());
781 CloseHandle(hfile);
783 /* make sure that destination has correct size */
784 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
785 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
786 ret = GetFileSize(hfile, NULL);
787 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
789 /* make sure that destination has the same filetime */
790 ret = GetFileTime(hfile, NULL, NULL, &ft2);
791 ok( ret, "GetFileTime error %d\n", GetLastError());
792 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
794 SetLastError(0xdeadbeef);
795 ret = CopyFileA(source, dest, FALSE);
796 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
797 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
799 /* make sure that destination still has correct size */
800 ret = GetFileSize(hfile, NULL);
801 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
802 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
803 ok( retok && ret == sizeof(prefix),
804 "ReadFile: error %d\n", GetLastError());
805 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
807 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
808 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
809 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
811 ret = CopyFileA(source, dest, FALSE);
812 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
813 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
815 CloseHandle(hmapfile);
816 CloseHandle(hfile);
818 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
819 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
821 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
822 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
823 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
825 ret = CopyFileA(source, dest, FALSE);
826 ok(!ret, "CopyFileA: expected failure\n");
827 ok(GetLastError() == ERROR_USER_MAPPED_FILE ||
828 broken(GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x */
829 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
831 CloseHandle(hmapfile);
832 CloseHandle(hfile);
834 ret = DeleteFileA(source);
835 ok(ret, "DeleteFileA: error %d\n", GetLastError());
836 ret = DeleteFileA(dest);
837 ok(ret, "DeleteFileA: error %d\n", GetLastError());
840 static void test_CopyFileW(void)
842 WCHAR temp_path[MAX_PATH];
843 WCHAR source[MAX_PATH], dest[MAX_PATH];
844 static const WCHAR prefix[] = {'p','f','x',0};
845 DWORD ret;
847 ret = GetTempPathW(MAX_PATH, temp_path);
848 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
850 win_skip("GetTempPathW is not available\n");
851 return;
853 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
854 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
856 ret = GetTempFileNameW(temp_path, prefix, 0, source);
857 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
859 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
860 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
862 ret = CopyFileW(source, dest, TRUE);
863 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
864 "CopyFileW: unexpected error %d\n", GetLastError());
866 ret = CopyFileW(source, dest, FALSE);
867 ok(ret, "CopyFileW: error %d\n", GetLastError());
869 ret = DeleteFileW(source);
870 ok(ret, "DeleteFileW: error %d\n", GetLastError());
871 ret = DeleteFileW(dest);
872 ok(ret, "DeleteFileW: error %d\n", GetLastError());
875 static void test_CopyFile2(void)
877 static const WCHAR doesntexistW[] = {'d','o','e','s','n','t','e','x','i','s','t',0};
878 static const WCHAR prefix[] = {'p','f','x',0};
879 WCHAR source[MAX_PATH], dest[MAX_PATH], temp_path[MAX_PATH];
880 COPYFILE2_EXTENDED_PARAMETERS params;
881 HANDLE hfile, hmapfile;
882 FILETIME ft1, ft2;
883 DWORD ret, len;
884 char buf[10];
885 HRESULT hr;
887 if (!pCopyFile2)
889 skip("CopyFile2 is not available\n");
890 return;
893 ret = GetTempPathW(MAX_PATH, temp_path);
894 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
895 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
897 ret = GetTempFileNameW(temp_path, prefix, 0, source);
898 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
900 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
901 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
903 /* fail if exists */
904 memset(&params, 0, sizeof(params));
905 params.dwSize = sizeof(params);
906 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
908 SetLastError(0xdeadbeef);
909 hr = pCopyFile2(source, dest, &params);
910 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
911 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
913 /* don't fail if exists */
914 params.dwSize = sizeof(params);
915 params.dwCopyFlags = 0;
917 hr = pCopyFile2(source, dest, &params);
918 ok(hr == S_OK, "CopyFile2: error 0x%08x\n", hr);
920 /* copying a file to itself must fail */
921 params.dwSize = sizeof(params);
922 params.dwCopyFlags = 0;
924 SetLastError(0xdeadbeef);
925 hr = pCopyFile2(source, source, &params);
926 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: copying a file to itself didn't fail, 0x%08x\n", hr);
927 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
929 /* make the source have not zero size */
930 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
931 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
932 ret = WriteFile(hfile, prefix, sizeof(prefix), &len, NULL );
933 ok(ret && len == sizeof(prefix), "WriteFile error %d\n", GetLastError());
934 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
936 /* get the file time and change it to prove the difference */
937 ret = GetFileTime(hfile, NULL, NULL, &ft1);
938 ok(ret, "GetFileTime error %d\n", GetLastError());
939 ft1.dwLowDateTime -= 600000000; /* 60 second */
940 ret = SetFileTime(hfile, NULL, NULL, &ft1);
941 ok(ret, "SetFileTime error %d\n", GetLastError());
942 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
943 CloseHandle(hfile);
945 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
946 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
948 params.dwSize = sizeof(params);
949 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
951 SetLastError(0xdeadbeef);
952 hr = pCopyFile2(source, dest, &params);
953 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
954 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
956 params.dwSize = sizeof(params);
957 params.dwCopyFlags = 0;
958 hr = pCopyFile2(source, dest, &params);
959 ok(ret, "CopyFile2: error 0x%08x\n", hr);
961 /* copying from a read-locked source fails */
962 hfile = CreateFileW(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
963 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
965 params.dwSize = sizeof(params);
966 params.dwCopyFlags = 0;
967 SetLastError(0xdeadbeef);
968 hr = pCopyFile2(source, dest, &params);
969 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
970 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
972 /* in addition, the source is opened before the destination */
973 params.dwSize = sizeof(params);
974 params.dwCopyFlags = 0;
975 SetLastError(0xdeadbeef);
976 hr = pCopyFile2(doesntexistW, dest, &params);
977 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "got 0x%08x\n", hr);
978 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "CopyFile2: last error %d\n", GetLastError());
979 CloseHandle(hfile);
981 /* copying from a r+w opened, r shared source succeeds */
982 hfile = CreateFileW(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
983 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
985 params.dwSize = sizeof(params);
986 params.dwCopyFlags = 0;
987 hr = pCopyFile2(source, dest, &params);
988 ok(hr == S_OK, "failed 0x%08x\n", hr);
989 CloseHandle(hfile);
991 /* copying from a delete-locked source mostly succeeds */
992 hfile = CreateFileW(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
993 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
995 params.dwSize = sizeof(params);
996 params.dwCopyFlags = 0;
997 hr = pCopyFile2(source, dest, &params);
998 ok(hr == S_OK, "failed 0x%08x\n", hr);
999 CloseHandle(hfile);
1001 /* copying to a write-locked destination fails */
1002 hfile = CreateFileW(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1003 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1005 params.dwSize = sizeof(params);
1006 params.dwCopyFlags = 0;
1007 SetLastError(0xdeadbeef);
1008 hr = pCopyFile2(source, dest, FALSE);
1009 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1010 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1011 CloseHandle(hfile);
1013 /* copying to a r+w opened, w shared destination mostly succeeds */
1014 hfile = CreateFileW(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1015 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1017 params.dwSize = sizeof(params);
1018 params.dwCopyFlags = 0;
1019 hr = pCopyFile2(source, dest, FALSE);
1020 ok(hr == S_OK, "got 0x%08x\n", hr);
1021 CloseHandle(hfile);
1023 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
1024 hfile = CreateFileW(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
1025 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1027 params.dwSize = sizeof(params);
1028 params.dwCopyFlags = 0;
1029 SetLastError(0xdeadbeef);
1030 hr = pCopyFile2(source, dest, &params);
1031 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1032 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1033 CloseHandle(hfile);
1035 /* copy to a file that's opened the way Wine opens the source */
1036 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1037 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1039 params.dwSize = sizeof(params);
1040 params.dwCopyFlags = 0;
1041 hr = pCopyFile2(source, dest, &params);
1042 ok(hr == S_OK, "got 0x%08x\n", hr);
1043 CloseHandle(hfile);
1045 /* make sure that destination has correct size */
1046 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1047 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1048 ret = GetFileSize(hfile, NULL);
1049 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1051 /* make sure that destination has the same filetime */
1052 ret = GetFileTime(hfile, NULL, NULL, &ft2);
1053 ok(ret, "GetFileTime error %d\n", GetLastError());
1054 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
1056 params.dwSize = sizeof(params);
1057 params.dwCopyFlags = 0;
1058 SetLastError(0xdeadbeef);
1059 hr = pCopyFile2(source, dest, &params);
1060 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1061 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1063 /* make sure that destination still has correct size */
1064 ret = GetFileSize(hfile, NULL);
1065 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1066 ret = ReadFile(hfile, buf, sizeof(buf), &len, NULL);
1067 ok(ret && len == sizeof(prefix), "ReadFile: error %d\n", GetLastError());
1068 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
1070 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
1071 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1072 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1074 params.dwSize = sizeof(params);
1075 params.dwCopyFlags = 0;
1076 SetLastError(0xdeadbeef);
1077 hr = pCopyFile2(source, dest, &params);
1078 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1079 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1081 CloseHandle(hmapfile);
1082 CloseHandle(hfile);
1084 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1085 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1087 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
1088 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1089 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1091 params.dwSize = sizeof(params);
1092 params.dwCopyFlags = 0;
1093 hr = pCopyFile2(source, dest, &params);
1094 ok(hr == HRESULT_FROM_WIN32(ERROR_USER_MAPPED_FILE), "CopyFile2: unexpected error 0x%08x\n", hr);
1095 ok(GetLastError() == ERROR_USER_MAPPED_FILE, "CopyFile2: last error %d\n", GetLastError());
1097 CloseHandle(hmapfile);
1098 CloseHandle(hfile);
1100 DeleteFileW(source);
1101 DeleteFileW(dest);
1104 static DWORD WINAPI copy_progress_cb(LARGE_INTEGER total_size, LARGE_INTEGER total_transferred,
1105 LARGE_INTEGER stream_size, LARGE_INTEGER stream_transferred,
1106 DWORD stream, DWORD reason, HANDLE source, HANDLE dest, LPVOID userdata)
1108 ok(reason == CALLBACK_STREAM_SWITCH, "expected CALLBACK_STREAM_SWITCH, got %u\n", reason);
1109 CloseHandle(userdata);
1110 return PROGRESS_CANCEL;
1113 static void test_CopyFileEx(void)
1115 char temp_path[MAX_PATH];
1116 char source[MAX_PATH], dest[MAX_PATH];
1117 static const char prefix[] = "pfx";
1118 HANDLE hfile;
1119 DWORD ret;
1120 BOOL retok;
1122 ret = GetTempPathA(MAX_PATH, temp_path);
1123 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1124 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1126 ret = GetTempFileNameA(temp_path, prefix, 0, source);
1127 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1129 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
1130 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1132 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1133 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1134 SetLastError(0xdeadbeef);
1135 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1136 todo_wine
1137 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1138 todo_wine
1139 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1140 ok(GetFileAttributesA(dest) != INVALID_FILE_ATTRIBUTES, "file was deleted\n");
1142 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1143 NULL, OPEN_EXISTING, 0, 0);
1144 todo_wine
1145 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1146 SetLastError(0xdeadbeef);
1147 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1148 todo_wine
1149 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1150 todo_wine
1151 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1152 todo_wine
1153 ok(GetFileAttributesA(dest) == INVALID_FILE_ATTRIBUTES, "file was not deleted\n");
1155 ret = DeleteFileA(source);
1156 ok(ret, "DeleteFileA failed with error %d\n", GetLastError());
1157 ret = DeleteFileA(dest);
1158 ok(!ret, "DeleteFileA unexpectedly succeeded\n");
1162 * Debugging routine to dump a buffer in a hexdump-like fashion.
1164 static void dumpmem(unsigned char *mem, int len)
1166 int x = 0;
1167 char hex[49], *p;
1168 char txt[17], *c;
1170 while (x < len)
1172 p = hex;
1173 c = txt;
1174 do {
1175 p += sprintf(p, "%02x ", mem[x]);
1176 *c++ = (mem[x] >= 32 && mem[x] <= 127) ? mem[x] : '.';
1177 } while (++x % 16 && x < len);
1178 *c = '\0';
1179 trace("%04x: %-48s- %s\n", x, hex, txt);
1183 static void test_CreateFileA(void)
1185 HANDLE hFile;
1186 char temp_path[MAX_PATH], dirname[MAX_PATH];
1187 char filename[MAX_PATH];
1188 static const char prefix[] = "pfx";
1189 char windowsdir[MAX_PATH];
1190 char Volume_1[MAX_PATH];
1191 unsigned char buffer[512];
1192 char directory[] = "removeme";
1193 static const char nt_drive[] = "\\\\?\\A:";
1194 DWORD i, ret, len;
1195 struct test_list p[] = {
1196 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
1197 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
1198 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
1199 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
1200 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
1201 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
1202 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
1203 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
1204 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
1205 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
1206 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
1207 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
1208 {NULL, 0, -1, 0, FALSE}
1210 BY_HANDLE_FILE_INFORMATION Finfo;
1212 ret = GetTempPathA(MAX_PATH, temp_path);
1213 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1214 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1216 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1217 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1219 SetLastError(0xdeadbeef);
1220 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
1221 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1222 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1223 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1225 SetLastError(0xdeadbeef);
1226 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1227 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1228 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1229 "hFile %p, last error %u\n", hFile, GetLastError());
1231 CloseHandle(hFile);
1233 SetLastError(0xdeadbeef);
1234 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1235 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1236 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1237 "hFile %p, last error %u\n", hFile, GetLastError());
1239 CloseHandle(hFile);
1241 ret = DeleteFileA(filename);
1242 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1244 SetLastError(0xdeadbeef);
1245 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1246 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1247 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1248 "hFile %p, last error %u\n", hFile, GetLastError());
1250 CloseHandle(hFile);
1252 ret = DeleteFileA(filename);
1253 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1255 SetLastError(0xdeadbeef);
1256 hFile = CreateFileA("c:\\*.*", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1257 ok(hFile == INVALID_HANDLE_VALUE, "hFile should have been INVALID_HANDLE_VALUE\n");
1258 ok(GetLastError() == ERROR_INVALID_NAME ||
1259 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
1260 "LastError should have been ERROR_INVALID_NAME or ERROR_FILE_NOT_FOUND but got %u\n", GetLastError());
1262 /* get windows drive letter */
1263 ret = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1264 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1265 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1267 /* test error return codes from CreateFile for some cases */
1268 ret = GetTempPathA(MAX_PATH, temp_path);
1269 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1270 strcpy(dirname, temp_path);
1271 strcat(dirname, directory);
1272 ret = CreateDirectoryA(dirname, NULL);
1273 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
1274 /* set current drive & directory to known location */
1275 SetCurrentDirectoryA( temp_path );
1276 i = 0;
1277 while (p[i].file)
1279 filename[0] = 0;
1280 /* update the drive id in the table entry with the current one */
1281 if (p[i].file[1] == ':')
1283 strcpy(filename, p[i].file);
1284 filename[0] = windowsdir[0];
1286 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
1288 strcpy(filename, p[i].file);
1289 filename[4] = windowsdir[0];
1291 else
1293 /* prefix the table entry with the current temp directory */
1294 strcpy(filename, temp_path);
1295 strcat(filename, p[i].file);
1297 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1298 FILE_SHARE_READ | FILE_SHARE_WRITE,
1299 NULL, OPEN_EXISTING,
1300 p[i].options, NULL );
1301 /* if we get ACCESS_DENIED when we do not expect it, assume
1302 * no access to the volume
1304 if (hFile == INVALID_HANDLE_VALUE &&
1305 GetLastError() == ERROR_ACCESS_DENIED &&
1306 p[i].err != ERROR_ACCESS_DENIED)
1308 if (p[i].todo_flag)
1309 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
1310 else
1311 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
1313 /* otherwise validate results with expectations */
1314 else if (p[i].todo_flag)
1315 todo_wine ok(
1316 (hFile == INVALID_HANDLE_VALUE &&
1317 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
1318 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
1319 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
1320 filename, hFile, GetLastError(), p[i].err);
1321 else
1323 (hFile == INVALID_HANDLE_VALUE &&
1324 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
1325 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
1326 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
1327 filename, hFile, GetLastError(), p[i].err);
1328 if (hFile != INVALID_HANDLE_VALUE)
1329 CloseHandle( hFile );
1330 i++;
1332 ret = RemoveDirectoryA(dirname);
1333 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
1336 /* test opening directory as a directory */
1337 hFile = CreateFileA( temp_path, GENERIC_READ,
1338 FILE_SHARE_READ,
1339 NULL,
1340 OPEN_EXISTING,
1341 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1342 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
1344 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
1345 "CreateFileA did not work, last error %u on volume <%s>\n",
1346 GetLastError(), temp_path );
1348 if (hFile != INVALID_HANDLE_VALUE)
1350 ret = GetFileInformationByHandle( hFile, &Finfo );
1351 if (ret)
1353 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
1354 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
1355 temp_path, Finfo.dwFileAttributes);
1357 CloseHandle( hFile );
1360 else
1361 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
1364 /* *** Test opening volumes/devices using drive letter *** */
1366 /* test using drive letter in non-rewrite format without trailing \ */
1367 /* this should work */
1368 strcpy(filename, nt_drive);
1369 filename[4] = windowsdir[0];
1370 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1371 FILE_SHARE_READ | FILE_SHARE_WRITE,
1372 NULL, OPEN_EXISTING,
1373 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1374 if (hFile != INVALID_HANDLE_VALUE ||
1375 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
1377 /* if we have adm rights to volume, then try rest of tests */
1378 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1379 filename, GetLastError());
1380 if (hFile != INVALID_HANDLE_VALUE)
1382 /* if we opened the volume/device, try to read it. Since it */
1383 /* opened, we should be able to read it. We don't care about*/
1384 /* what the data is at this time. */
1385 len = 512;
1386 ret = ReadFile( hFile, buffer, len, &len, NULL );
1387 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1388 GetLastError(), ret, filename);
1389 if (ret)
1391 trace("buffer is\n");
1392 dumpmem(buffer, 64);
1394 CloseHandle( hFile );
1397 /* test using drive letter with trailing \ and in non-rewrite */
1398 /* this should not work */
1399 strcpy(filename, nt_drive);
1400 filename[4] = windowsdir[0];
1401 strcat( filename, "\\" );
1402 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1403 FILE_SHARE_READ | FILE_SHARE_WRITE,
1404 NULL, OPEN_EXISTING,
1405 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1406 todo_wine
1407 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1408 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1409 filename, GetLastError());
1410 if (hFile != INVALID_HANDLE_VALUE)
1411 CloseHandle( hFile );
1413 /* test using temp path with trailing \ and in non-rewrite as dir */
1414 /* this should work */
1415 strcpy(filename, nt_drive);
1416 filename[4] = 0;
1417 strcat( filename, temp_path );
1418 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1419 FILE_SHARE_READ | FILE_SHARE_WRITE,
1420 NULL, OPEN_EXISTING,
1421 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1422 ok(hFile != INVALID_HANDLE_VALUE,
1423 "CreateFileA should have worked on %s, but got %u\n",
1424 filename, GetLastError());
1425 if (hFile != INVALID_HANDLE_VALUE)
1426 CloseHandle( hFile );
1428 /* test using drive letter without trailing \ and in device ns */
1429 /* this should work */
1430 strcpy(filename, nt_drive);
1431 filename[4] = windowsdir[0];
1432 filename[2] = '.';
1433 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1434 FILE_SHARE_READ | FILE_SHARE_WRITE,
1435 NULL, OPEN_EXISTING,
1436 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1437 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1438 filename, GetLastError());
1439 if (hFile != INVALID_HANDLE_VALUE)
1440 CloseHandle( hFile );
1442 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
1443 else if (GetLastError() == ERROR_BAD_NETPATH)
1444 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
1445 else
1446 skip("Do not have authority to access volumes. Tests skipped\n");
1449 /* *** Test opening volumes/devices using GUID *** */
1451 if (pGetVolumeNameForVolumeMountPointA)
1453 strcpy(filename, "c:\\");
1454 filename[0] = windowsdir[0];
1455 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
1456 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
1457 if (ret)
1459 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
1461 /* test the result of opening a unique volume name (GUID)
1462 * with the trailing \
1463 * this should error out
1465 strcpy(filename, Volume_1);
1466 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1467 FILE_SHARE_READ | FILE_SHARE_WRITE,
1468 NULL, OPEN_EXISTING,
1469 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1470 todo_wine
1471 ok(hFile == INVALID_HANDLE_VALUE,
1472 "CreateFileA should not have opened %s, hFile %p\n",
1473 filename, hFile);
1474 todo_wine
1475 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1476 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1477 filename, GetLastError());
1478 if (hFile != INVALID_HANDLE_VALUE)
1479 CloseHandle( hFile );
1481 /* test the result of opening a unique volume name (GUID)
1482 * with the temp path string as dir
1483 * this should work
1485 strcpy(filename, Volume_1);
1486 strcat(filename, temp_path+3);
1487 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1488 FILE_SHARE_READ | FILE_SHARE_WRITE,
1489 NULL, OPEN_EXISTING,
1490 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1491 todo_wine
1492 ok(hFile != INVALID_HANDLE_VALUE,
1493 "CreateFileA should have opened %s, but got %u\n",
1494 filename, GetLastError());
1495 if (hFile != INVALID_HANDLE_VALUE)
1496 CloseHandle( hFile );
1498 /* test the result of opening a unique volume name (GUID)
1499 * without the trailing \ and in device namespace
1500 * this should work
1502 strcpy(filename, Volume_1);
1503 filename[2] = '.';
1504 filename[48] = 0;
1505 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1506 FILE_SHARE_READ | FILE_SHARE_WRITE,
1507 NULL, OPEN_EXISTING,
1508 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1509 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1511 /* if we have adm rights to volume, then try rest of tests */
1512 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1513 filename, GetLastError());
1514 if (hFile != INVALID_HANDLE_VALUE)
1516 /* if we opened the volume/device, try to read it. Since it */
1517 /* opened, we should be able to read it. We don't care about*/
1518 /* what the data is at this time. */
1519 len = 512;
1520 ret = ReadFile( hFile, buffer, len, &len, NULL );
1521 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1522 GetLastError(), ret, filename);
1523 if (ret)
1525 trace("buffer is\n");
1526 dumpmem(buffer, 64);
1528 CloseHandle( hFile );
1531 else
1532 skip("Do not have authority to access volumes. Tests skipped\n");
1534 else
1535 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1537 else
1538 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1541 static void test_CreateFileW(void)
1543 HANDLE hFile;
1544 WCHAR temp_path[MAX_PATH];
1545 WCHAR filename[MAX_PATH];
1546 static const WCHAR emptyW[]={'\0'};
1547 static const WCHAR prefix[] = {'p','f','x',0};
1548 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1549 DWORD ret;
1551 ret = GetTempPathW(MAX_PATH, temp_path);
1552 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1554 win_skip("GetTempPathW is not available\n");
1555 return;
1557 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1558 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1560 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1561 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1563 SetLastError(0xdeadbeef);
1564 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1565 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1566 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1567 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1569 SetLastError(0xdeadbeef);
1570 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1571 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1572 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1573 "hFile %p, last error %u\n", hFile, GetLastError());
1575 CloseHandle(hFile);
1577 SetLastError(0xdeadbeef);
1578 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1579 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1580 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1581 "hFile %p, last error %u\n", hFile, GetLastError());
1583 CloseHandle(hFile);
1585 ret = DeleteFileW(filename);
1586 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1588 SetLastError(0xdeadbeef);
1589 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1590 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1591 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1592 "hFile %p, last error %u\n", hFile, GetLastError());
1594 CloseHandle(hFile);
1596 ret = DeleteFileW(filename);
1597 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1599 if (0)
1601 /* this crashes on NT4.0 */
1602 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1603 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1604 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1605 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1608 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1609 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1610 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1611 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1613 /* test the result of opening a nonexistent driver name */
1614 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1615 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1616 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1617 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1619 ret = CreateDirectoryW(filename, NULL);
1620 ok(ret == TRUE, "couldn't create temporary directory\n");
1621 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1622 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1623 ok(hFile != INVALID_HANDLE_VALUE,
1624 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1625 CloseHandle(hFile);
1626 ret = RemoveDirectoryW(filename);
1627 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1630 static void test_CreateFile2(void)
1632 HANDLE hFile;
1633 WCHAR temp_path[MAX_PATH];
1634 WCHAR filename[MAX_PATH];
1635 CREATEFILE2_EXTENDED_PARAMETERS exparams;
1636 static const WCHAR emptyW[]={'\0'};
1637 static const WCHAR prefix[] = {'p','f','x',0};
1638 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1639 DWORD ret;
1641 if (!pCreateFile2)
1643 win_skip("CreateFile2 is missing\n");
1644 return;
1647 ret = GetTempPathW(MAX_PATH, temp_path);
1648 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1649 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1651 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1652 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1654 SetLastError(0xdeadbeef);
1655 exparams.dwSize = sizeof(exparams);
1656 exparams.dwFileAttributes = FILE_FLAG_RANDOM_ACCESS;
1657 exparams.dwFileFlags = 0;
1658 exparams.dwSecurityQosFlags = 0;
1659 exparams.lpSecurityAttributes = NULL;
1660 exparams.hTemplateFile = 0;
1661 hFile = pCreateFile2(filename, GENERIC_READ, 0, CREATE_NEW, &exparams);
1662 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1663 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1665 SetLastError(0xdeadbeef);
1666 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, CREATE_ALWAYS, &exparams);
1667 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1668 "hFile %p, last error %u\n", hFile, GetLastError());
1669 CloseHandle(hFile);
1671 SetLastError(0xdeadbeef);
1672 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1673 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1674 "hFile %p, last error %u\n", hFile, GetLastError());
1675 CloseHandle(hFile);
1677 ret = DeleteFileW(filename);
1678 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1680 SetLastError(0xdeadbeef);
1681 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1682 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1683 "hFile %p, last error %u\n", hFile, GetLastError());
1684 CloseHandle(hFile);
1686 ret = DeleteFileW(filename);
1687 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1689 hFile = pCreateFile2(emptyW, GENERIC_READ, 0, CREATE_NEW, &exparams);
1690 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1691 "CreateFile2(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1693 /* test the result of opening a nonexistent driver name */
1694 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1695 hFile = pCreateFile2(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &exparams);
1696 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1697 "CreateFile2 on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1699 ret = CreateDirectoryW(filename, NULL);
1700 ok(ret == TRUE, "couldn't create temporary directory\n");
1701 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS;
1702 hFile = pCreateFile2(filename, GENERIC_READ | GENERIC_WRITE, 0, OPEN_ALWAYS, &exparams);
1703 todo_wine
1704 ok(hFile == INVALID_HANDLE_VALUE,
1705 "expected CreateFile2 to fail on existing directory, error: %d\n", GetLastError());
1706 CloseHandle(hFile);
1707 ret = RemoveDirectoryW(filename);
1708 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1711 static void test_GetTempFileNameA(void)
1713 UINT result;
1714 char out[MAX_PATH];
1715 char expected[MAX_PATH + 10];
1716 char windowsdir[MAX_PATH + 10];
1717 char windowsdrive[3];
1719 result = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1720 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1721 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1723 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1724 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1726 strcat(windowsdir, "\\");
1729 windowsdrive[0] = windowsdir[0];
1730 windowsdrive[1] = windowsdir[1];
1731 windowsdrive[2] = '\0';
1733 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1734 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1735 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1736 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1737 windowsdrive[0], out);
1739 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1740 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1741 expected[0] = '\0';
1742 strcat(expected, windowsdir);
1743 strcat(expected, "abc2.tmp");
1744 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1745 out, expected);
1748 static void test_DeleteFileA( void )
1750 BOOL ret;
1751 char temp_path[MAX_PATH], temp_file[MAX_PATH];
1752 HANDLE hfile;
1754 ret = DeleteFileA(NULL);
1755 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1756 GetLastError() == ERROR_PATH_NOT_FOUND),
1757 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1759 ret = DeleteFileA("");
1760 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1761 GetLastError() == ERROR_BAD_PATHNAME),
1762 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1764 ret = DeleteFileA("nul");
1765 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1766 GetLastError() == ERROR_INVALID_PARAMETER ||
1767 GetLastError() == ERROR_ACCESS_DENIED ||
1768 GetLastError() == ERROR_INVALID_FUNCTION),
1769 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1771 ret = DeleteFileA("nonexist.txt");
1772 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "DeleteFileA(\"nonexist.txt\") returned ret=%d error=%d\n",ret,GetLastError());
1774 GetTempPathA(MAX_PATH, temp_path);
1775 GetTempFileNameA(temp_path, "tst", 0, temp_file);
1777 SetLastError(0xdeadbeef);
1778 hfile = CreateFileA(temp_file, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1779 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1781 SetLastError(0xdeadbeef);
1782 ret = DeleteFileA(temp_file);
1783 todo_wine
1784 ok(ret, "DeleteFile error %d\n", GetLastError());
1786 SetLastError(0xdeadbeef);
1787 ret = CloseHandle(hfile);
1788 ok(ret, "CloseHandle error %d\n", GetLastError());
1789 ret = DeleteFileA(temp_file);
1790 todo_wine
1791 ok(!ret, "DeleteFile should fail\n");
1793 SetLastError(0xdeadbeef);
1794 ret = CreateDirectoryA("testdir", NULL);
1795 ok(ret, "CreateDirectory failed, got err %d\n", GetLastError());
1796 ret = DeleteFileA("testdir");
1797 ok(!ret && GetLastError() == ERROR_ACCESS_DENIED,
1798 "Expected ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
1799 ret = RemoveDirectoryA("testdir");
1800 ok(ret, "Remove a directory failed, got error %d\n", GetLastError());
1803 static void test_DeleteFileW( void )
1805 BOOL ret;
1806 WCHAR pathW[MAX_PATH];
1807 WCHAR pathsubW[MAX_PATH];
1808 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1809 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1810 static const WCHAR emptyW[]={'\0'};
1812 ret = DeleteFileW(NULL);
1813 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1815 win_skip("DeleteFileW is not available\n");
1816 return;
1818 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1819 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1821 ret = DeleteFileW(emptyW);
1822 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1823 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1825 /* test DeleteFile on empty directory */
1826 ret = GetTempPathW(MAX_PATH, pathW);
1827 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1829 ok(0, "MAX_PATH exceeded in constructing paths\n");
1830 return;
1832 lstrcatW(pathW, dirW);
1833 lstrcpyW(pathsubW, pathW);
1834 lstrcatW(pathsubW, subdirW);
1835 ret = CreateDirectoryW(pathW, NULL);
1836 ok(ret == TRUE, "couldn't create directory deletefile\n");
1837 ret = DeleteFileW(pathW);
1838 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1839 ret = RemoveDirectoryW(pathW);
1840 ok(ret == TRUE, "expected to remove directory deletefile\n");
1842 /* test DeleteFile on non-empty directory */
1843 ret = CreateDirectoryW(pathW, NULL);
1844 ok(ret == TRUE, "couldn't create directory deletefile\n");
1845 ret = CreateDirectoryW(pathsubW, NULL);
1846 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1847 ret = DeleteFileW(pathW);
1848 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1849 ret = RemoveDirectoryW(pathsubW);
1850 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1851 ret = RemoveDirectoryW(pathW);
1852 ok(ret == TRUE, "expected to remove directory deletefile\n");
1855 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1857 static void test_MoveFileA(void)
1859 char tempdir[MAX_PATH];
1860 char source[MAX_PATH], dest[MAX_PATH];
1861 static const char prefix[] = "pfx";
1862 HANDLE hfile;
1863 HANDLE hmapfile;
1864 DWORD ret;
1865 BOOL retok;
1867 ret = GetTempPathA(MAX_PATH, tempdir);
1868 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1869 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1871 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1872 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1874 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1875 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1877 ret = MoveFileA(source, dest);
1878 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1879 "MoveFileA: unexpected error %d\n", GetLastError());
1881 ret = DeleteFileA(dest);
1882 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1884 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1885 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1887 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1888 ok( retok && ret == sizeof(prefix),
1889 "WriteFile error %d\n", GetLastError());
1891 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1892 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1894 ret = MoveFileA(source, dest);
1895 todo_wine {
1896 ok(!ret, "MoveFileA: expected failure\n");
1897 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1898 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1899 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1902 CloseHandle(hmapfile);
1903 CloseHandle(hfile);
1905 /* if MoveFile succeeded, move back to dest */
1906 if (ret) MoveFileA(dest, source);
1908 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1909 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1911 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1912 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1914 ret = MoveFileA(source, dest);
1915 todo_wine {
1916 ok(!ret, "MoveFileA: expected failure\n");
1917 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1918 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1919 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1922 CloseHandle(hmapfile);
1923 CloseHandle(hfile);
1925 /* if MoveFile succeeded, move back to dest */
1926 if (ret) MoveFileA(dest, source);
1928 ret = MoveFileA(source, dest);
1929 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1931 lstrcatA(tempdir, "Remove Me");
1932 ret = CreateDirectoryA(tempdir, NULL);
1933 ok(ret == TRUE, "CreateDirectoryA failed\n");
1935 lstrcpyA(source, dest);
1936 lstrcpyA(dest, tempdir);
1937 lstrcatA(dest, "\\wild?.*");
1938 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1939 ret = MoveFileA(source, dest);
1940 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1941 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1942 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1943 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1944 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1946 WIN32_FIND_DATAA fd;
1947 char temppath[MAX_PATH];
1948 HANDLE hFind;
1950 lstrcpyA(temppath, tempdir);
1951 lstrcatA(temppath, "\\*.*");
1952 hFind = FindFirstFileA(temppath, &fd);
1953 if (INVALID_HANDLE_VALUE != hFind)
1955 LPSTR lpName;
1958 lpName = fd.cAlternateFileName;
1959 if (!lpName[0])
1960 lpName = fd.cFileName;
1961 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1963 while (FindNextFileA(hFind, &fd));
1964 FindClose(hFind);
1967 ret = DeleteFileA(source);
1968 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1969 ret = DeleteFileA(dest);
1970 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1971 ret = RemoveDirectoryA(tempdir);
1972 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1975 static void test_MoveFileW(void)
1977 WCHAR temp_path[MAX_PATH];
1978 WCHAR source[MAX_PATH], dest[MAX_PATH];
1979 static const WCHAR prefix[] = {'p','f','x',0};
1980 DWORD ret;
1982 ret = GetTempPathW(MAX_PATH, temp_path);
1983 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1985 win_skip("GetTempPathW is not available\n");
1986 return;
1988 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1989 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1991 ret = GetTempFileNameW(temp_path, prefix, 0, source);
1992 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1994 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
1995 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1997 ret = MoveFileW(source, dest);
1998 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1999 "CopyFileW: unexpected error %d\n", GetLastError());
2001 ret = DeleteFileW(source);
2002 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2003 ret = DeleteFileW(dest);
2004 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2007 #define PATTERN_OFFSET 0x10
2009 static void test_offset_in_overlapped_structure(void)
2011 HANDLE hFile;
2012 OVERLAPPED ov;
2013 DWORD done, offset;
2014 BOOL rc;
2015 BYTE buf[256], pattern[] = "TeSt";
2016 UINT i;
2017 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
2018 BOOL ret;
2020 ret =GetTempPathA(MAX_PATH, temp_path);
2021 ok( ret, "GetTempPathA error %d\n", GetLastError());
2022 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
2023 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
2025 /*** Write File *****************************************************/
2027 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
2028 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2030 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
2031 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
2032 ok( ret, "WriteFile error %d\n", GetLastError());
2033 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
2035 memset(&ov, 0, sizeof(ov));
2036 S(U(ov)).Offset = PATTERN_OFFSET;
2037 S(U(ov)).OffsetHigh = 0;
2038 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2039 /* Win 9x does not support the overlapped I/O on files */
2040 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2041 ok(rc, "WriteFile error %d\n", GetLastError());
2042 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2043 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2044 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2046 S(U(ov)).Offset = sizeof(buf) * 2;
2047 S(U(ov)).OffsetHigh = 0;
2048 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2049 ok( ret, "WriteFile error %d\n", GetLastError());
2050 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2051 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2052 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
2055 CloseHandle(hFile);
2057 /*** Read File *****************************************************/
2059 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
2060 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2062 memset(buf, 0, sizeof(buf));
2063 memset(&ov, 0, sizeof(ov));
2064 S(U(ov)).Offset = PATTERN_OFFSET;
2065 S(U(ov)).OffsetHigh = 0;
2066 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
2067 /* Win 9x does not support the overlapped I/O on files */
2068 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2069 ok(rc, "ReadFile error %d\n", GetLastError());
2070 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
2071 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2072 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2073 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
2076 CloseHandle(hFile);
2078 ret = DeleteFileA(temp_fname);
2079 ok( ret, "DeleteFileA error %d\n", GetLastError());
2082 static void test_LockFile(void)
2084 HANDLE handle, handle2;
2085 DWORD written;
2086 OVERLAPPED overlapped;
2087 int limited_LockFile;
2088 int limited_UnLockFile;
2089 BOOL ret;
2091 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2092 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2093 CREATE_ALWAYS, 0, 0 );
2094 if (handle == INVALID_HANDLE_VALUE)
2096 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2097 return;
2099 handle2 = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2100 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2101 OPEN_EXISTING, 0, 0 );
2102 if (handle2 == INVALID_HANDLE_VALUE)
2104 ok( 0, "couldn't open file \"%s\" (err=%d)\n", filename, GetLastError() );
2105 goto cleanup;
2107 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
2109 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
2110 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
2112 limited_UnLockFile = 0;
2113 if (UnlockFile( handle, 0, 0, 0, 0 ))
2115 limited_UnLockFile = 1;
2118 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
2119 /* overlapping locks must fail */
2120 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
2121 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
2122 /* non-overlapping locks must succeed */
2123 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
2125 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
2126 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
2127 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
2128 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
2130 S(U(overlapped)).Offset = 100;
2131 S(U(overlapped)).OffsetHigh = 0;
2132 overlapped.hEvent = 0;
2134 /* Test for broken LockFileEx a la Windows 95 OSR2. */
2135 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
2137 /* LockFileEx is probably OK, test it more. */
2138 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
2139 "LockFileEx 100,100 failed\n" );
2142 /* overlapping shared locks are OK */
2143 S(U(overlapped)).Offset = 150;
2144 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
2146 /* but exclusive is not */
2147 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
2148 0, 50, 0, &overlapped ),
2149 "LockFileEx exclusive 150,50 succeeded\n" );
2150 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
2151 { /* UnLockFile is capable. */
2152 S(U(overlapped)).Offset = 100;
2153 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
2154 "UnlockFileEx 150,100 again succeeded\n" );
2157 /* shared lock can overlap exclusive if handles are equal */
2158 S(U(overlapped)).Offset = 300;
2159 ok( LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0, &overlapped ),
2160 "LockFileEx exclusive 300,100 failed\n" );
2161 ok( !LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2162 "LockFileEx handle2 300,100 succeeded\n" );
2163 ret = LockFileEx( handle, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped );
2164 ok( ret, "LockFileEx 300,100 failed\n" );
2165 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2166 /* exclusive lock is removed first */
2167 ok( LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2168 "LockFileEx handle2 300,100 failed\n" );
2169 ok( UnlockFileEx( handle2, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2170 if (ret)
2171 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2173 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
2174 if (ret)
2176 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
2177 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
2178 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
2180 else /* win9x */
2181 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
2183 /* wrap-around lock should not do anything */
2184 /* (but still succeeds on NT4 so we don't check result) */
2185 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
2187 limited_LockFile = 0;
2188 if (!LockFile( handle, ~0, ~0, 1, 0 ))
2190 limited_LockFile = 1;
2193 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
2195 /* zero-byte lock */
2196 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
2197 if (!limited_LockFile) ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
2198 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
2199 if (!limited_LockFile) ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
2201 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
2202 ok( !UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 succeeded\n" );
2204 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
2206 CloseHandle( handle2 );
2207 cleanup:
2208 CloseHandle( handle );
2209 DeleteFileA( filename );
2212 static BOOL create_fake_dll( LPCSTR filename )
2214 IMAGE_DOS_HEADER *dos;
2215 IMAGE_NT_HEADERS *nt;
2216 IMAGE_SECTION_HEADER *sec;
2217 BYTE *buffer;
2218 DWORD lfanew = sizeof(*dos);
2219 DWORD size = lfanew + sizeof(*nt) + sizeof(*sec);
2220 DWORD written;
2221 BOOL ret;
2223 HANDLE file = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2224 if (file == INVALID_HANDLE_VALUE) return FALSE;
2226 buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
2228 dos = (IMAGE_DOS_HEADER *)buffer;
2229 dos->e_magic = IMAGE_DOS_SIGNATURE;
2230 dos->e_cblp = sizeof(*dos);
2231 dos->e_cp = 1;
2232 dos->e_cparhdr = lfanew / 16;
2233 dos->e_minalloc = 0;
2234 dos->e_maxalloc = 0xffff;
2235 dos->e_ss = 0x0000;
2236 dos->e_sp = 0x00b8;
2237 dos->e_lfarlc = lfanew;
2238 dos->e_lfanew = lfanew;
2240 nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
2241 nt->Signature = IMAGE_NT_SIGNATURE;
2242 #if defined __i386__
2243 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
2244 #elif defined __x86_64__
2245 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
2246 #elif defined __powerpc__
2247 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
2248 #elif defined __arm__
2249 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT;
2250 #elif defined __aarch64__
2251 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
2252 #else
2253 # error You must specify the machine type
2254 #endif
2255 nt->FileHeader.NumberOfSections = 1;
2256 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2257 nt->FileHeader.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_EXECUTABLE_IMAGE;
2258 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
2259 nt->OptionalHeader.MajorLinkerVersion = 1;
2260 nt->OptionalHeader.MinorLinkerVersion = 0;
2261 nt->OptionalHeader.ImageBase = 0x10000000;
2262 nt->OptionalHeader.SectionAlignment = 0x1000;
2263 nt->OptionalHeader.FileAlignment = 0x1000;
2264 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
2265 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
2266 nt->OptionalHeader.MajorImageVersion = 1;
2267 nt->OptionalHeader.MinorImageVersion = 0;
2268 nt->OptionalHeader.MajorSubsystemVersion = 4;
2269 nt->OptionalHeader.MinorSubsystemVersion = 0;
2270 nt->OptionalHeader.SizeOfImage = 0x2000;
2271 nt->OptionalHeader.SizeOfHeaders = size;
2272 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
2273 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
2275 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
2276 memcpy( sec->Name, ".rodata", sizeof(".rodata") );
2277 sec->Misc.VirtualSize = 0x1000;
2278 sec->VirtualAddress = 0x1000;
2279 sec->SizeOfRawData = 0;
2280 sec->PointerToRawData = 0;
2281 sec->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
2283 ret = WriteFile( file, buffer, size, &written, NULL ) && written == size;
2284 HeapFree( GetProcessHeap(), 0, buffer );
2285 CloseHandle( file );
2286 return ret;
2289 static unsigned int map_file_access( unsigned int access )
2291 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2292 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2293 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2294 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2295 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2298 static BOOL is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
2300 access1 = map_file_access( access1 );
2301 access2 = map_file_access( access2 );
2302 access1 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2303 access2 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2305 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2306 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2308 if ((access1 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing2 & FILE_SHARE_READ)) return FALSE;
2309 if ((access1 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2310 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return FALSE;
2311 if ((access2 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing1 & FILE_SHARE_READ)) return FALSE;
2312 if ((access2 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing1 & FILE_SHARE_WRITE)) return FALSE;
2313 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return FALSE;
2314 return TRUE;
2317 static BOOL is_sharing_map_compatible( DWORD map_access, DWORD access2, DWORD sharing2 )
2319 if ((map_access == PAGE_READWRITE || map_access == PAGE_EXECUTE_READWRITE) &&
2320 !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2321 access2 = map_file_access( access2 );
2322 if ((map_access & SEC_IMAGE) && (access2 & FILE_WRITE_DATA)) return FALSE;
2323 return TRUE;
2326 static void test_file_sharing(void)
2328 static const DWORD access_modes[] =
2329 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
2330 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE,
2331 GENERIC_EXECUTE, GENERIC_EXECUTE | DELETE,
2332 FILE_READ_DATA, FILE_WRITE_DATA, FILE_APPEND_DATA, FILE_READ_EA, FILE_WRITE_EA,
2333 FILE_READ_DATA | FILE_EXECUTE, FILE_WRITE_DATA | FILE_EXECUTE, FILE_APPEND_DATA | FILE_EXECUTE,
2334 FILE_READ_EA | FILE_EXECUTE, FILE_WRITE_EA | FILE_EXECUTE, FILE_EXECUTE,
2335 FILE_DELETE_CHILD, FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES };
2336 static const DWORD sharing_modes[] =
2337 { 0, FILE_SHARE_READ,
2338 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2339 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
2340 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
2341 static const DWORD mapping_modes[] =
2342 { PAGE_READONLY, PAGE_WRITECOPY, PAGE_READWRITE, SEC_IMAGE | PAGE_WRITECOPY };
2343 int a1, s1, a2, s2;
2344 int ret;
2345 HANDLE h, h2;
2347 /* make sure the file exists */
2348 if (!create_fake_dll( filename ))
2350 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
2351 return;
2354 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
2356 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
2358 SetLastError(0xdeadbeef);
2359 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
2360 NULL, OPEN_EXISTING, 0, 0 );
2361 if (h == INVALID_HANDLE_VALUE)
2363 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2364 return;
2366 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
2368 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
2370 SetLastError(0xdeadbeef);
2371 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
2372 NULL, OPEN_EXISTING, 0, 0 );
2373 ret = GetLastError();
2374 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
2375 access_modes[a2], sharing_modes[s2] ))
2377 ok( h2 != INVALID_HANDLE_VALUE,
2378 "open failed for modes %x/%x/%x/%x\n",
2379 access_modes[a1], sharing_modes[s1],
2380 access_modes[a2], sharing_modes[s2] );
2381 ok( ret == 0, "wrong error code %d\n", ret );
2383 else
2385 ok( h2 == INVALID_HANDLE_VALUE,
2386 "open succeeded for modes %x/%x/%x/%x\n",
2387 access_modes[a1], sharing_modes[s1],
2388 access_modes[a2], sharing_modes[s2] );
2389 ok( ret == ERROR_SHARING_VIOLATION,
2390 "wrong error code %d\n", ret );
2392 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2395 CloseHandle( h );
2399 for (a1 = 0; a1 < sizeof(mapping_modes)/sizeof(mapping_modes[0]); a1++)
2401 HANDLE m;
2403 create_fake_dll( filename );
2404 SetLastError(0xdeadbeef);
2405 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2406 if (h == INVALID_HANDLE_VALUE)
2408 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2409 return;
2411 m = CreateFileMappingA( h, NULL, mapping_modes[a1], 0, 0, NULL );
2412 ok( m != 0, "failed to create mapping %x err %u\n", mapping_modes[a1], GetLastError() );
2413 CloseHandle( h );
2414 if (!m) continue;
2416 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
2418 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
2420 SetLastError(0xdeadbeef);
2421 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
2422 NULL, OPEN_EXISTING, 0, 0 );
2424 ret = GetLastError();
2425 if (h2 == INVALID_HANDLE_VALUE)
2427 ok( !is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]),
2428 "open failed for modes map %x/%x/%x\n",
2429 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
2430 ok( ret == ERROR_SHARING_VIOLATION,
2431 "wrong error code %d\n", ret );
2433 else
2435 if (!is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]))
2436 ok( broken(1), /* no checking on nt4 */
2437 "open succeeded for modes map %x/%x/%x\n",
2438 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
2439 ok( ret == 0xdeadbeef /* Win9x */ ||
2440 ret == 0, /* XP */
2441 "wrong error code %d\n", ret );
2442 CloseHandle( h2 );
2447 /* try CREATE_ALWAYS over an existing mapping */
2448 SetLastError(0xdeadbeef);
2449 h2 = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
2450 NULL, CREATE_ALWAYS, 0, 0 );
2451 ret = GetLastError();
2452 if (mapping_modes[a1] & SEC_IMAGE)
2454 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
2455 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
2457 else
2459 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
2460 ok( ret == ERROR_USER_MAPPED_FILE, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
2462 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2464 /* try DELETE_ON_CLOSE over an existing mapping */
2465 SetLastError(0xdeadbeef);
2466 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2467 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 );
2468 ret = GetLastError();
2469 if (mapping_modes[a1] & SEC_IMAGE)
2471 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
2472 ok( ret == ERROR_ACCESS_DENIED, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
2474 else
2476 ok( h2 != INVALID_HANDLE_VALUE, "open failed for map %x err %u\n", mapping_modes[a1], ret );
2478 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2480 CloseHandle( m );
2483 SetLastError(0xdeadbeef);
2484 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
2485 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2487 SetLastError(0xdeadbeef);
2488 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
2489 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
2490 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
2492 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
2493 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2495 CloseHandle(h);
2496 CloseHandle(h2);
2498 DeleteFileA( filename );
2501 static char get_windows_drive(void)
2503 char windowsdir[MAX_PATH];
2504 GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
2505 return windowsdir[0];
2508 static void test_FindFirstFileA(void)
2510 HANDLE handle;
2511 WIN32_FIND_DATAA data;
2512 int err;
2513 char buffer[5] = "C:\\";
2514 char buffer2[100];
2515 char nonexistent[MAX_PATH];
2517 /* try FindFirstFileA on "C:\" */
2518 buffer[0] = get_windows_drive();
2520 SetLastError( 0xdeadbeaf );
2521 handle = FindFirstFileA(buffer, &data);
2522 err = GetLastError();
2523 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
2524 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2526 /* try FindFirstFileA on "C:\*" */
2527 strcpy(buffer2, buffer);
2528 strcat(buffer2, "*");
2529 handle = FindFirstFileA(buffer2, &data);
2530 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2531 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2532 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
2533 if (FindNextFileA( handle, &data ))
2534 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2535 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
2536 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2538 /* try FindFirstFileA on windows dir */
2539 GetWindowsDirectoryA( buffer2, sizeof(buffer2) );
2540 strcat(buffer2, "\\*");
2541 handle = FindFirstFileA(buffer2, &data);
2542 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2543 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
2544 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
2545 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
2546 while (FindNextFileA( handle, &data ))
2547 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2548 "FindNextFile shouldn't return '%s'\n", data.cFileName );
2549 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2551 /* try FindFirstFileA on "C:\foo\" */
2552 SetLastError( 0xdeadbeaf );
2553 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
2555 char tmp[MAX_PATH];
2556 GetTempPathA( sizeof(tmp), tmp );
2557 GetTempFileNameA( tmp, "foo", 0, nonexistent );
2559 DeleteFileA( nonexistent );
2560 strcpy(buffer2, nonexistent);
2561 strcat(buffer2, "\\");
2562 handle = FindFirstFileA(buffer2, &data);
2563 err = GetLastError();
2564 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2565 todo_wine {
2566 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2569 /* try FindFirstFileA without trailing backslash */
2570 SetLastError( 0xdeadbeaf );
2571 strcpy(buffer2, nonexistent);
2572 handle = FindFirstFileA(buffer2, &data);
2573 err = GetLastError();
2574 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2575 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2577 /* try FindFirstFileA on "C:\foo\bar.txt" */
2578 SetLastError( 0xdeadbeaf );
2579 strcpy(buffer2, nonexistent);
2580 strcat(buffer2, "\\bar.txt");
2581 handle = FindFirstFileA(buffer2, &data);
2582 err = GetLastError();
2583 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2584 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2586 /* try FindFirstFileA on "C:\foo\*.*" */
2587 SetLastError( 0xdeadbeaf );
2588 strcpy(buffer2, nonexistent);
2589 strcat(buffer2, "\\*.*");
2590 handle = FindFirstFileA(buffer2, &data);
2591 err = GetLastError();
2592 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2593 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2595 /* try FindFirstFileA on "foo\bar.txt" */
2596 SetLastError( 0xdeadbeaf );
2597 strcpy(buffer2, nonexistent + 3);
2598 strcat(buffer2, "\\bar.txt");
2599 handle = FindFirstFileA(buffer2, &data);
2600 err = GetLastError();
2601 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2602 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2604 /* try FindFirstFileA on "c:\nul" */
2605 SetLastError( 0xdeadbeaf );
2606 strcpy(buffer2, buffer);
2607 strcat(buffer2, "nul");
2608 handle = FindFirstFileA(buffer2, &data);
2609 err = GetLastError();
2610 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2611 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
2612 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2613 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2614 "wrong attributes %x\n", data.dwFileAttributes );
2615 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2617 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2618 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2620 SetLastError( 0xdeadbeaf );
2621 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2622 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2623 ok( FindClose( handle ), "failed to close handle\n" );
2625 /* try FindFirstFileA on "lpt1" */
2626 SetLastError( 0xdeadbeaf );
2627 strcpy(buffer2, "lpt1");
2628 handle = FindFirstFileA(buffer2, &data);
2629 err = GetLastError();
2630 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2631 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
2632 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2633 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2634 "wrong attributes %x\n", data.dwFileAttributes );
2635 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2637 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2638 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2640 SetLastError( 0xdeadbeaf );
2641 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2642 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2643 ok( FindClose( handle ), "failed to close handle\n" );
2645 /* try FindFirstFileA on "c:\nul\*" */
2646 SetLastError( 0xdeadbeaf );
2647 strcpy(buffer2, buffer);
2648 strcat(buffer2, "nul\\*");
2649 handle = FindFirstFileA(buffer2, &data);
2650 err = GetLastError();
2651 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2652 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2654 /* try FindFirstFileA on "c:\nul*" */
2655 SetLastError( 0xdeadbeaf );
2656 strcpy(buffer2, buffer);
2657 strcat(buffer2, "nul*");
2658 handle = FindFirstFileA(buffer2, &data);
2659 err = GetLastError();
2660 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2661 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2663 /* try FindFirstFileA on "c:\foo\bar\nul" */
2664 SetLastError( 0xdeadbeaf );
2665 strcpy(buffer2, buffer);
2666 strcat(buffer2, "foo\\bar\\nul");
2667 handle = FindFirstFileA(buffer2, &data);
2668 err = GetLastError();
2669 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2670 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2672 /* try FindFirstFileA on "c:\foo\nul\bar" */
2673 SetLastError( 0xdeadbeaf );
2674 strcpy(buffer2, buffer);
2675 strcat(buffer2, "foo\\nul\\bar");
2676 handle = FindFirstFileA(buffer2, &data);
2677 err = GetLastError();
2678 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2679 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2682 static void test_FindNextFileA(void)
2684 HANDLE handle;
2685 WIN32_FIND_DATAA search_results;
2686 int err;
2687 char buffer[5] = "C:\\*";
2689 buffer[0] = get_windows_drive();
2690 handle = FindFirstFileA(buffer,&search_results);
2691 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
2692 while (FindNextFileA(handle, &search_results))
2694 /* get to the end of the files */
2696 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
2697 err = GetLastError();
2698 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
2701 static void test_FindFirstFileExA(FINDEX_INFO_LEVELS level, FINDEX_SEARCH_OPS search_ops, DWORD flags)
2703 WIN32_FIND_DATAA search_results;
2704 HANDLE handle;
2705 BOOL ret;
2707 if (!pFindFirstFileExA)
2709 win_skip("FindFirstFileExA() is missing\n");
2710 return;
2713 trace("Running FindFirstFileExA tests with level=%d, search_ops=%d, flags=%u\n",
2714 level, search_ops, flags);
2716 CreateDirectoryA("test-dir", NULL);
2717 _lclose(_lcreat("test-dir\\file1", 0));
2718 _lclose(_lcreat("test-dir\\file2", 0));
2719 CreateDirectoryA("test-dir\\dir1", NULL);
2720 SetLastError(0xdeadbeef);
2721 handle = pFindFirstFileExA("test-dir\\*", level, &search_results, search_ops, NULL, flags);
2722 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2724 win_skip("FindFirstFileExA is not implemented\n");
2725 goto cleanup;
2727 if ((flags & FIND_FIRST_EX_LARGE_FETCH) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2729 win_skip("FindFirstFileExA flag FIND_FIRST_EX_LARGE_FETCH not supported, skipping test\n");
2730 goto cleanup;
2732 if ((level == FindExInfoBasic) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2734 win_skip("FindFirstFileExA level FindExInfoBasic not supported, skipping test\n");
2735 goto cleanup;
2738 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2739 #define CHECK_LEVEL(fn) (level != FindExInfoBasic || !(fn)[0])
2741 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
2742 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
2743 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2745 ok(FindNextFileA(handle, &search_results), "Fetching second file failed\n");
2746 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
2747 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2749 ok(FindNextFileA(handle, &search_results), "Fetching third file failed\n");
2750 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
2751 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2753 SetLastError(0xdeadbeef);
2754 ret = FindNextFileA(handle, &search_results);
2755 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
2757 skip("File system supports directory filtering\n");
2758 /* Results from the previous call are not cleared */
2759 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
2760 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2763 else
2765 ok(ret, "Fetching fourth file failed\n");
2766 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
2767 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2769 ok(FindNextFileA(handle, &search_results), "Fetching fifth file failed\n");
2770 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
2771 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2773 ok(FindNextFileA(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
2776 #undef CHECK_NAME
2777 #undef CHECK_LEVEL
2779 FindClose( handle );
2781 /* Most Windows systems seem to ignore the FIND_FIRST_EX_CASE_SENSITIVE flag. Unofficial documentation
2782 * suggests that there are registry keys and that it might depend on the used filesystem. */
2783 SetLastError(0xdeadbeef);
2784 handle = pFindFirstFileExA("TEST-DIR\\*", level, &search_results, search_ops, NULL, flags);
2785 if (flags & FIND_FIRST_EX_CASE_SENSITIVE)
2787 ok(handle != INVALID_HANDLE_VALUE || GetLastError() == ERROR_PATH_NOT_FOUND,
2788 "Unexpected error %x, expected valid handle or ERROR_PATH_NOT_FOUND\n", GetLastError());
2789 trace("FindFirstFileExA flag FIND_FIRST_EX_CASE_SENSITIVE is %signored\n",
2790 (handle == INVALID_HANDLE_VALUE) ? "not " : "");
2792 else
2793 ok(handle != INVALID_HANDLE_VALUE, "Unexpected error %x, expected valid handle\n", GetLastError());
2794 if (handle != INVALID_HANDLE_VALUE)
2795 FindClose( handle );
2797 cleanup:
2798 DeleteFileA("test-dir\\file1");
2799 DeleteFileA("test-dir\\file2");
2800 RemoveDirectoryA("test-dir\\dir1");
2801 RemoveDirectoryA("test-dir");
2804 static int test_Mapfile_createtemp(HANDLE *handle)
2806 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
2807 DeleteFileA(filename);
2808 *handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
2809 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2810 if (*handle != INVALID_HANDLE_VALUE) {
2812 return 1;
2815 return 0;
2818 static void test_MapFile(void)
2820 HANDLE handle;
2821 HANDLE hmap;
2823 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2825 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
2826 ok( hmap != NULL, "mapping should work, I named it!\n" );
2828 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2830 /* We have to close file before we try new stuff with mapping again.
2831 Else we would always succeed on XP or block descriptors on 95. */
2832 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2833 ok( hmap != NULL, "We should still be able to map!\n" );
2834 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2835 ok( CloseHandle( handle ), "can't close file handle\n");
2836 handle = NULL;
2838 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2840 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2841 ok( hmap == NULL, "mapped zero size file\n");
2842 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
2844 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
2845 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2846 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2847 if ( hmap )
2848 CloseHandle( hmap );
2850 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
2851 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2852 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2853 if ( hmap )
2854 CloseHandle( hmap );
2856 /* On XP you can now map again, on Win 95 you cannot. */
2858 ok( CloseHandle( handle ), "can't close file handle\n");
2859 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
2862 static void test_GetFileType(void)
2864 DWORD type, type2;
2865 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2866 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
2867 type = GetFileType(h);
2868 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
2869 CloseHandle( h );
2870 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2871 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
2872 type = GetFileType(h);
2873 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
2874 CloseHandle( h );
2875 DeleteFileA( filename );
2876 h = GetStdHandle( STD_OUTPUT_HANDLE );
2877 ok( h != INVALID_HANDLE_VALUE, "GetStdHandle failed\n" );
2878 type = GetFileType( (HANDLE)STD_OUTPUT_HANDLE );
2879 type2 = GetFileType( h );
2880 ok(type == type2, "expected type %d for STD_OUTPUT_HANDLE got %d\n", type2, type);
2883 static int completion_count;
2885 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2887 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2888 ReleaseSemaphore(ovl->hEvent, 1, NULL);
2889 completion_count++;
2892 static void test_async_file_errors(void)
2894 char szFile[MAX_PATH];
2895 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
2896 HANDLE hFile;
2897 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
2898 OVERLAPPED ovl;
2899 S(U(ovl)).Offset = 0;
2900 S(U(ovl)).OffsetHigh = 0;
2901 ovl.hEvent = hSem;
2902 completion_count = 0;
2903 szFile[0] = '\0';
2904 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
2905 strcat(szFile, "\\win.ini");
2906 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2907 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2908 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
2909 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2910 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2911 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2912 while (TRUE)
2914 BOOL res;
2915 DWORD count;
2916 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2918 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2919 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2920 if (!res)
2921 break;
2922 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2923 break;
2924 S(U(ovl)).Offset += count;
2925 /* i/o completion routine only called if ReadFileEx returned success.
2926 * we only care about violations of this rule so undo what should have
2927 * been done */
2928 completion_count--;
2930 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2931 /*printf("Error = %ld\n", GetLastError());*/
2932 HeapFree(GetProcessHeap(), 0, lpBuffer);
2935 static BOOL user_apc_ran;
2936 static void CALLBACK user_apc(ULONG_PTR param)
2938 user_apc_ran = TRUE;
2941 static void test_read_write(void)
2943 DWORD bytes, ret, old_prot;
2944 HANDLE hFile;
2945 char temp_path[MAX_PATH];
2946 char filename[MAX_PATH];
2947 char *mem;
2948 static const char prefix[] = "pfx";
2950 ret = GetTempPathA(MAX_PATH, temp_path);
2951 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2952 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2954 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2955 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2957 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2958 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2959 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2961 user_apc_ran = FALSE;
2962 if (pQueueUserAPC) {
2963 trace("Queueing an user APC\n"); /* verify the file is non alerable */
2964 ret = pQueueUserAPC(&user_apc, GetCurrentThread(), 0);
2965 ok(ret, "QueueUserAPC failed: %d\n", GetLastError());
2968 SetLastError(12345678);
2969 bytes = 12345678;
2970 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2971 ok(ret && GetLastError() == 12345678,
2972 "ret = %d, error %d\n", ret, GetLastError());
2973 ok(!bytes, "bytes = %d\n", bytes);
2975 SetLastError(12345678);
2976 bytes = 12345678;
2977 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2978 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2979 (ret && GetLastError() == 12345678), /* Win9x */
2980 "ret = %d, error %d\n", ret, GetLastError());
2981 ok(!bytes || /* Win2k */
2982 bytes == 10, /* Win9x */
2983 "bytes = %d\n", bytes);
2985 /* make sure the file contains data */
2986 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2987 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
2989 SetLastError(12345678);
2990 bytes = 12345678;
2991 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
2992 ok(ret && GetLastError() == 12345678,
2993 "ret = %d, error %d\n", ret, GetLastError());
2994 ok(!bytes, "bytes = %d\n", bytes);
2996 SetLastError(12345678);
2997 bytes = 12345678;
2998 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
2999 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
3000 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
3001 "ret = %d, error %d\n", ret, GetLastError());
3002 ok(!bytes, "bytes = %d\n", bytes);
3004 ok(user_apc_ran == FALSE, "UserAPC ran, file using alertable io mode\n");
3005 if (pQueueUserAPC)
3006 SleepEx(0, TRUE); /* get rid of apc */
3008 /* test passing protected memory as buffer */
3010 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
3011 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
3013 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3014 ok( ret, "WriteFile failed error %u\n", GetLastError() );
3015 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
3017 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
3018 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3020 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3021 ok( !ret, "WriteFile succeeded\n" );
3022 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3023 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3024 "wrong error %u\n", GetLastError() );
3025 ok( bytes == 0, "wrote %x bytes\n", bytes );
3027 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
3028 ok( !ret, "WriteFile succeeded\n" );
3029 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
3030 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3031 "wrong error %u\n", GetLastError() );
3032 ok( bytes == 0, "wrote %x bytes\n", bytes );
3034 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
3035 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3037 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3038 ok( !ret, "WriteFile succeeded\n" );
3039 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3040 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3041 "wrong error %u\n", GetLastError() );
3042 ok( bytes == 0, "wrote %x bytes\n", bytes );
3044 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3046 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3047 ok( !ret, "ReadFile succeeded\n" );
3048 ok( GetLastError() == ERROR_NOACCESS ||
3049 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3050 "wrong error %u\n", GetLastError() );
3051 ok( bytes == 0, "read %x bytes\n", bytes );
3053 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
3054 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3056 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3057 ok( !ret, "ReadFile succeeded\n" );
3058 ok( GetLastError() == ERROR_NOACCESS ||
3059 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3060 "wrong error %u\n", GetLastError() );
3061 ok( bytes == 0, "read %x bytes\n", bytes );
3063 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
3064 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3066 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3067 ok( !ret, "ReadFile succeeded\n" );
3068 ok( GetLastError() == ERROR_NOACCESS ||
3069 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3070 "wrong error %u\n", GetLastError() );
3071 ok( bytes == 0, "read %x bytes\n", bytes );
3073 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
3074 SetEndOfFile( hFile );
3075 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3077 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3078 ok( !ret, "ReadFile succeeded\n" );
3079 ok( GetLastError() == ERROR_NOACCESS ||
3080 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3081 "wrong error %u\n", GetLastError() );
3082 ok( bytes == 0, "read %x bytes\n", bytes );
3084 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
3085 ok( ret, "ReadFile failed error %u\n", GetLastError() );
3086 ok( bytes == 0x1234, "read %x bytes\n", bytes );
3088 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
3089 ok( !ret, "ReadFile succeeded\n" );
3090 ok( GetLastError() == ERROR_NOACCESS ||
3091 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3092 "wrong error %u\n", GetLastError() );
3093 ok( bytes == 0, "read %x bytes\n", bytes );
3095 VirtualFree( mem, 0, MEM_FREE );
3097 ret = CloseHandle(hFile);
3098 ok( ret, "CloseHandle: error %d\n", GetLastError());
3099 ret = DeleteFileA(filename);
3100 ok( ret, "DeleteFileA: error %d\n", GetLastError());
3103 static void test_OpenFile(void)
3105 HFILE hFile;
3106 OFSTRUCT ofs;
3107 BOOL ret;
3108 DWORD retval;
3110 static const char file[] = "regedit.exe";
3111 static const char foo[] = ".\\foo-bar-foo.baz";
3112 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
3113 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3114 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3115 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3116 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3117 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
3118 char buff[MAX_PATH];
3119 char buff_long[4*MAX_PATH];
3120 char filled_0xA5[OFS_MAXPATHNAME];
3121 char *p;
3122 UINT length;
3124 /* Check for existing file */
3125 if (!pGetSystemWindowsDirectoryA)
3126 length = GetWindowsDirectoryA(buff, MAX_PATH);
3127 else
3128 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
3130 if (length + sizeof(file) < MAX_PATH)
3132 p = buff + strlen(buff);
3133 if (p > buff && p[-1] != '\\') *p++ = '\\';
3134 strcpy( p, file );
3135 memset(&ofs, 0xA5, sizeof(ofs));
3136 SetLastError(0xfaceabee);
3138 hFile = OpenFile(buff, &ofs, OF_EXIST);
3139 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
3140 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3141 "GetLastError() returns %d\n", GetLastError() );
3142 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3143 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3144 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3145 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3146 ofs.szPathName, buff );
3149 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
3150 length = GetCurrentDirectoryA(MAX_PATH, buff);
3152 /* Check for nonexistent file */
3153 if (length + sizeof(foo) < MAX_PATH)
3155 p = buff + strlen(buff);
3156 if (p > buff && p[-1] != '\\') *p++ = '\\';
3157 strcpy( p, foo + 2 );
3158 memset(&ofs, 0xA5, sizeof(ofs));
3159 SetLastError(0xfaceabee);
3161 hFile = OpenFile(foo, &ofs, OF_EXIST);
3162 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3163 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
3164 todo_wine
3165 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3166 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3167 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3168 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3169 ofs.szPathName, buff );
3172 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
3173 length += lstrlenA(foo_too_long + 1);
3175 /* Check for nonexistent file with too long filename */
3176 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
3178 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
3179 memset(&ofs, 0xA5, sizeof(ofs));
3180 SetLastError(0xfaceabee);
3182 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
3183 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3184 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
3185 "GetLastError() returns %d\n", GetLastError() );
3186 todo_wine
3187 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3188 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
3189 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3190 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3191 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
3192 ofs.szPathName );
3195 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
3197 if (length >= MAX_PATH)
3199 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
3200 return;
3202 p = buff + strlen(buff);
3203 if (p > buff && p[-1] != '\\') *p++ = '\\';
3204 strcpy( p, filename );
3206 memset(&ofs, 0xA5, sizeof(ofs));
3207 SetLastError(0xfaceabee);
3208 /* Create an empty file */
3209 hFile = OpenFile(filename, &ofs, OF_CREATE);
3210 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
3211 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3212 "GetLastError() returns %d\n", GetLastError() );
3213 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3214 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3215 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3216 ret = _lclose(hFile);
3217 ok( !ret, "_lclose() returns %d\n", ret );
3218 retval = GetFileAttributesA(filename);
3219 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
3221 memset(&ofs, 0xA5, sizeof(ofs));
3222 SetLastError(0xfaceabee);
3223 /* Check various opening options: */
3224 /* for reading only, */
3225 hFile = OpenFile(filename, &ofs, OF_READ);
3226 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
3227 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3228 "GetLastError() returns %d\n", GetLastError() );
3229 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3230 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3231 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3232 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3233 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3234 ret = _lclose(hFile);
3235 ok( !ret, "_lclose() returns %d\n", ret );
3237 memset(&ofs, 0xA5, sizeof(ofs));
3238 SetLastError(0xfaceabee);
3239 /* for writing only, */
3240 hFile = OpenFile(filename, &ofs, OF_WRITE);
3241 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
3242 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3243 "GetLastError() returns %d\n", GetLastError() );
3244 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3245 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3246 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3247 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3248 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3249 ret = _lclose(hFile);
3250 ok( !ret, "_lclose() returns %d\n", ret );
3252 memset(&ofs, 0xA5, sizeof(ofs));
3253 SetLastError(0xfaceabee);
3254 /* for reading and writing, */
3255 hFile = OpenFile(filename, &ofs, OF_READWRITE);
3256 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
3257 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3258 "GetLastError() returns %d\n", GetLastError() );
3259 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3260 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3261 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3262 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3263 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3264 ret = _lclose(hFile);
3265 ok( !ret, "_lclose() returns %d\n", ret );
3267 memset(&ofs, 0xA5, sizeof(ofs));
3268 SetLastError(0xfaceabee);
3269 /* for checking file presence. */
3270 hFile = OpenFile(filename, &ofs, OF_EXIST);
3271 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
3272 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3273 "GetLastError() returns %d\n", GetLastError() );
3274 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3275 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3276 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3277 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3278 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3280 memset(&ofs, 0xA5, sizeof(ofs));
3281 SetLastError(0xfaceabee);
3282 /* Delete the file and make sure it doesn't exist anymore */
3283 hFile = OpenFile(filename, &ofs, OF_DELETE);
3284 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
3285 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3286 "GetLastError() returns %d\n", GetLastError() );
3287 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3288 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3289 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3290 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3291 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3293 retval = GetFileAttributesA(filename);
3294 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
3297 static void test_overlapped(void)
3299 OVERLAPPED ov;
3300 DWORD r, result;
3302 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
3303 if (0) /* tested: WinXP */
3305 GetOverlappedResult(0, NULL, &result, FALSE);
3306 GetOverlappedResult(0, &ov, NULL, FALSE);
3307 GetOverlappedResult(0, NULL, NULL, FALSE);
3310 memset( &ov, 0, sizeof ov );
3311 result = 1;
3312 r = GetOverlappedResult(0, &ov, &result, 0);
3313 if (r)
3314 ok( result == 0, "wrong result %u\n", result );
3315 else /* win9x */
3316 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3318 result = 0;
3319 ov.Internal = 0;
3320 ov.InternalHigh = 0xabcd;
3321 r = GetOverlappedResult(0, &ov, &result, 0);
3322 if (r)
3323 ok( result == 0xabcd, "wrong result %u\n", result );
3324 else /* win9x */
3325 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3327 SetLastError( 0xb00 );
3328 result = 0;
3329 ov.Internal = STATUS_INVALID_HANDLE;
3330 ov.InternalHigh = 0xabcd;
3331 r = GetOverlappedResult(0, &ov, &result, 0);
3332 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3333 ok( r == FALSE, "should return false\n");
3334 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
3336 SetLastError( 0xb00 );
3337 result = 0;
3338 ov.Internal = STATUS_PENDING;
3339 ov.InternalHigh = 0xabcd;
3340 r = GetOverlappedResult(0, &ov, &result, 0);
3341 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3342 "wrong error %u\n", GetLastError() );
3343 ok( r == FALSE, "should return false\n");
3344 ok( result == 0, "wrong result %u\n", result );
3346 SetLastError( 0xb00 );
3347 ov.hEvent = CreateEventW( NULL, 1, 1, NULL );
3348 ov.Internal = STATUS_PENDING;
3349 ov.InternalHigh = 0xabcd;
3350 r = GetOverlappedResult(0, &ov, &result, 0);
3351 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3352 "wrong error %u\n", GetLastError() );
3353 ok( r == FALSE, "should return false\n");
3355 ResetEvent( ov.hEvent );
3357 SetLastError( 0xb00 );
3358 ov.Internal = STATUS_PENDING;
3359 ov.InternalHigh = 0;
3360 r = GetOverlappedResult(0, &ov, &result, 0);
3361 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3362 "wrong error %u\n", GetLastError() );
3363 ok( r == FALSE, "should return false\n");
3365 r = CloseHandle( ov.hEvent );
3366 ok( r == TRUE, "close handle failed\n");
3369 static void test_RemoveDirectory(void)
3371 int rc;
3372 char directory[] = "removeme";
3374 rc = CreateDirectoryA(directory, NULL);
3375 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
3377 rc = SetCurrentDirectoryA(directory);
3378 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3380 rc = RemoveDirectoryA(".");
3381 if (!rc)
3383 rc = SetCurrentDirectoryA("..");
3384 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3386 rc = RemoveDirectoryA(directory);
3387 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
3391 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
3393 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
3394 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
3395 return abs(t1 - t2) <= tolerance;
3398 static void test_ReplaceFileA(void)
3400 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3401 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
3402 static const char replacedData[] = "file-to-replace";
3403 static const char replacementData[] = "new-file";
3404 static const char backupData[] = "backup-file";
3405 FILETIME ftReplaced, ftReplacement, ftBackup;
3406 static const char prefix[] = "pfx";
3407 char temp_path[MAX_PATH];
3408 DWORD ret;
3409 BOOL retok, removeBackup = FALSE;
3411 if (!pReplaceFileA)
3413 win_skip("ReplaceFileA() is missing\n");
3414 return;
3417 ret = GetTempPathA(MAX_PATH, temp_path);
3418 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
3419 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3421 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
3422 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
3424 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3425 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3427 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
3428 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
3430 /* place predictable data in the file to be replaced */
3431 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3432 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3433 "failed to open replaced file\n");
3434 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
3435 ok( retok && ret == sizeof(replacedData),
3436 "WriteFile error (replaced) %d\n", GetLastError());
3437 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
3438 "replaced file has wrong size\n");
3439 /* place predictable data in the file to be the replacement */
3440 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3441 ok(hReplacementFile != INVALID_HANDLE_VALUE,
3442 "failed to open replacement file\n");
3443 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
3444 ok( retok && ret == sizeof(replacementData),
3445 "WriteFile error (replacement) %d\n", GetLastError());
3446 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
3447 "replacement file has wrong size\n");
3448 /* place predictable data in the backup file (to be over-written) */
3449 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3450 ok(hBackupFile != INVALID_HANDLE_VALUE,
3451 "failed to open backup file\n");
3452 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
3453 ok( retok && ret == sizeof(backupData),
3454 "WriteFile error (replacement) %d\n", GetLastError());
3455 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
3456 "backup file has wrong size\n");
3457 /* change the filetime on the "replaced" file to ensure that it changes */
3458 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3459 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
3460 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
3461 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3462 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
3463 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
3464 CloseHandle(hReplacedFile);
3465 /* change the filetime on the backup to ensure that it changes */
3466 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3467 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
3468 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
3469 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3470 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
3471 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
3472 CloseHandle(hBackupFile);
3473 /* get the filetime on the replacement file to perform checks */
3474 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
3475 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
3476 CloseHandle(hReplacementFile);
3478 /* perform replacement w/ backup
3479 * TODO: flags are not implemented
3481 SetLastError(0xdeadbeef);
3482 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3483 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
3484 /* make sure that the backup has the size of the old "replaced" file */
3485 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3486 ok(hBackupFile != INVALID_HANDLE_VALUE,
3487 "failed to open backup file\n");
3488 ret = GetFileSize(hBackupFile, NULL);
3489 ok(ret == sizeof(replacedData),
3490 "backup file has wrong size %d\n", ret);
3491 /* make sure that the "replaced" file has the size of the replacement file */
3492 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3493 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3494 "failed to open replaced file: %d\n", GetLastError());
3495 if (hReplacedFile != INVALID_HANDLE_VALUE)
3497 ret = GetFileSize(hReplacedFile, NULL);
3498 ok(ret == sizeof(replacementData),
3499 "replaced file has wrong size %d\n", ret);
3500 /* make sure that the replacement file no-longer exists */
3501 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3502 ok(hReplacementFile == INVALID_HANDLE_VALUE,
3503 "unexpected error, replacement file should not exist %d\n", GetLastError());
3504 /* make sure that the backup has the old "replaced" filetime */
3505 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3506 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3507 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
3508 CloseHandle(hBackupFile);
3509 /* make sure that the "replaced" has the old replacement filetime */
3510 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3511 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3512 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
3513 "replaced file has wrong filetime %x%08x / %x%08x\n",
3514 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
3515 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
3516 CloseHandle(hReplacedFile);
3518 else
3519 skip("couldn't open replacement file, skipping tests\n");
3521 /* re-create replacement file for pass w/o backup (blank) */
3522 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3523 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3524 /* perform replacement w/o backup
3525 * TODO: flags are not implemented
3527 SetLastError(0xdeadbeef);
3528 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3529 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3530 "ReplaceFileA: unexpected error %d\n", GetLastError());
3532 /* re-create replacement file for pass w/ backup (backup-file not existing) */
3533 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3534 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3535 ret = DeleteFileA(backup);
3536 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
3537 /* perform replacement w/ backup (no pre-existing backup)
3538 * TODO: flags are not implemented
3540 SetLastError(0xdeadbeef);
3541 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3542 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3543 "ReplaceFileA: unexpected error %d\n", GetLastError());
3544 if (ret)
3545 removeBackup = TRUE;
3547 /* re-create replacement file for pass w/ no permissions to "replaced" */
3548 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3549 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3550 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
3551 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3552 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
3553 /* perform replacement w/ backup (no permission to "replaced")
3554 * TODO: flags are not implemented
3556 SetLastError(0xdeadbeef);
3557 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3558 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
3559 /* make sure that the replacement file still exists */
3560 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3561 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
3562 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
3563 "unexpected error, replacement file should still exist %d\n", GetLastError());
3564 CloseHandle(hReplacementFile);
3565 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
3566 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3567 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
3569 /* replacement file still exists, make pass w/o "replaced" */
3570 ret = DeleteFileA(replaced);
3571 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3572 "DeleteFileA: error (replaced) %d\n", GetLastError());
3573 /* perform replacement w/ backup (no pre-existing backup or "replaced")
3574 * TODO: flags are not implemented
3576 SetLastError(0xdeadbeef);
3577 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3578 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3579 GetLastError() == ERROR_ACCESS_DENIED),
3580 "ReplaceFileA: unexpected error %d\n", GetLastError());
3582 /* perform replacement w/o existing "replacement" file
3583 * TODO: flags are not implemented
3585 SetLastError(0xdeadbeef);
3586 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3587 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3588 GetLastError() == ERROR_ACCESS_DENIED),
3589 "ReplaceFileA: unexpected error %d\n", GetLastError());
3590 DeleteFileA( replacement );
3593 * if the first round (w/ backup) worked then as long as there is no
3594 * failure then there is no need to check this round (w/ backup is the
3595 * more complete case)
3598 /* delete temporary files, replacement and replaced are already deleted */
3599 if (removeBackup)
3601 ret = DeleteFileA(backup);
3602 ok(ret ||
3603 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3604 "DeleteFileA: error (backup) %d\n", GetLastError());
3609 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
3610 * need to be as thorough.
3612 static void test_ReplaceFileW(void)
3614 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3615 static const WCHAR prefix[] = {'p','f','x',0};
3616 WCHAR temp_path[MAX_PATH];
3617 DWORD ret;
3618 BOOL removeBackup = FALSE;
3620 if (!pReplaceFileW)
3622 win_skip("ReplaceFileW() is missing\n");
3623 return;
3626 ret = GetTempPathW(MAX_PATH, temp_path);
3627 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3629 win_skip("GetTempPathW is not available\n");
3630 return;
3632 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
3633 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3635 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
3636 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
3638 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3639 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3641 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
3642 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
3644 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3645 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
3647 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3648 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3649 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3650 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3651 "ReplaceFileW: error %d\n", GetLastError());
3653 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3654 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3655 ret = DeleteFileW(backup);
3656 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
3657 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3658 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3659 "ReplaceFileW: error %d\n", GetLastError());
3661 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3662 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3663 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
3664 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3665 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3667 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3668 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
3669 "ReplaceFileW: unexpected error %d\n", GetLastError());
3670 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
3671 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3672 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3673 if (ret)
3674 removeBackup = TRUE;
3676 ret = DeleteFileW(replaced);
3677 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
3678 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3679 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
3681 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3682 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3683 GetLastError() == ERROR_ACCESS_DENIED),
3684 "ReplaceFileW: unexpected error %d\n", GetLastError());
3685 DeleteFileW( replacement );
3687 if (removeBackup)
3689 ret = DeleteFileW(backup);
3690 ok(ret ||
3691 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3692 "DeleteFileW: error (backup) %d\n", GetLastError());
3696 static void test_CreateFile(void)
3698 static const struct test_data
3700 DWORD disposition, access, error, clean_up;
3701 } td[] =
3703 /* 0 */ { 0, 0, ERROR_INVALID_PARAMETER, 0 },
3704 /* 1 */ { 0, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3705 /* 2 */ { 0, GENERIC_READ|GENERIC_WRITE, ERROR_INVALID_PARAMETER, 0 },
3706 /* 3 */ { CREATE_NEW, 0, ERROR_FILE_EXISTS, 1 },
3707 /* 4 */ { CREATE_NEW, 0, 0, 1 },
3708 /* 5 */ { CREATE_NEW, GENERIC_READ, 0, 1 },
3709 /* 6 */ { CREATE_NEW, GENERIC_WRITE, 0, 1 },
3710 /* 7 */ { CREATE_NEW, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3711 /* 8 */ { CREATE_ALWAYS, 0, 0, 0 },
3712 /* 9 */ { CREATE_ALWAYS, GENERIC_READ, 0, 0 },
3713 /* 10*/ { CREATE_ALWAYS, GENERIC_WRITE, 0, 0 },
3714 /* 11*/ { CREATE_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3715 /* 12*/ { OPEN_EXISTING, 0, ERROR_FILE_NOT_FOUND, 0 },
3716 /* 13*/ { CREATE_ALWAYS, 0, 0, 0 },
3717 /* 14*/ { OPEN_EXISTING, 0, 0, 0 },
3718 /* 15*/ { OPEN_EXISTING, GENERIC_READ, 0, 0 },
3719 /* 16*/ { OPEN_EXISTING, GENERIC_WRITE, 0, 0 },
3720 /* 17*/ { OPEN_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3721 /* 18*/ { OPEN_ALWAYS, 0, 0, 0 },
3722 /* 19*/ { OPEN_ALWAYS, GENERIC_READ, 0, 0 },
3723 /* 20*/ { OPEN_ALWAYS, GENERIC_WRITE, 0, 0 },
3724 /* 21*/ { OPEN_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3725 /* 22*/ { TRUNCATE_EXISTING, 0, ERROR_INVALID_PARAMETER, 0 },
3726 /* 23*/ { TRUNCATE_EXISTING, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3727 /* 24*/ { TRUNCATE_EXISTING, GENERIC_WRITE, 0, 0 },
3728 /* 25*/ { TRUNCATE_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3729 /* 26*/ { TRUNCATE_EXISTING, FILE_WRITE_DATA, ERROR_INVALID_PARAMETER, 0 }
3731 char temp_path[MAX_PATH];
3732 char file_name[MAX_PATH];
3733 DWORD i, ret, written;
3734 HANDLE hfile;
3736 GetTempPathA(MAX_PATH, temp_path);
3737 GetTempFileNameA(temp_path, "tmp", 0, file_name);
3739 i = strlen(temp_path);
3740 if (i && temp_path[i - 1] == '\\') temp_path[i - 1] = 0;
3742 for (i = 0; i <= 5; i++)
3744 SetLastError(0xdeadbeef);
3745 hfile = CreateFileA(temp_path, GENERIC_READ, 0, NULL, i, 0, 0);
3746 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
3747 if (i == 0 || i == 5)
3749 /* FIXME: remove once Wine is fixed */
3750 if (i == 5) todo_wine
3751 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3752 else
3753 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3755 else
3757 /* FIXME: remove once Wine is fixed */
3758 if (i == 1) todo_wine
3759 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3760 else
3761 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3764 SetLastError(0xdeadbeef);
3765 hfile = CreateFileA(temp_path, GENERIC_WRITE, 0, NULL, i, 0, 0);
3766 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
3767 if (i == 0)
3768 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3769 else
3771 /* FIXME: remove once Wine is fixed */
3772 if (i == 1) todo_wine
3773 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3774 else
3775 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3779 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3781 SetLastError(0xdeadbeef);
3782 hfile = CreateFileA(file_name, td[i].access, 0, NULL, td[i].disposition, 0, 0);
3783 if (!td[i].error)
3785 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
3786 written = 0xdeadbeef;
3787 SetLastError(0xdeadbeef);
3788 ret = WriteFile(hfile, &td[i].error, sizeof(td[i].error), &written, NULL);
3789 if (td[i].access & GENERIC_WRITE)
3790 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
3791 else
3793 ok(!ret, "%d: WriteFile should fail\n", i);
3794 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3796 CloseHandle(hfile);
3798 else
3800 /* FIXME: remove the condition below once Wine is fixed */
3801 if (td[i].disposition == TRUNCATE_EXISTING && !(td[i].access & GENERIC_WRITE))
3803 todo_wine
3805 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
3806 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
3808 CloseHandle(hfile);
3810 else
3812 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
3813 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
3817 if (td[i].clean_up) DeleteFileA(file_name);
3820 DeleteFileA(file_name);
3823 static void test_GetFileInformationByHandleEx(void)
3825 int i;
3826 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[1024], *strPtr;
3827 BOOL ret;
3828 DWORD ret2, written;
3829 HANDLE directory, file;
3830 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
3831 FILE_BASIC_INFO *basicInfo;
3832 FILE_STANDARD_INFO *standardInfo;
3833 FILE_NAME_INFO *nameInfo;
3834 LARGE_INTEGER prevWrite;
3835 FILE_IO_PRIORITY_HINT_INFO priohintinfo;
3836 FILE_ALLOCATION_INFO allocinfo;
3837 FILE_DISPOSITION_INFO dispinfo;
3838 FILE_END_OF_FILE_INFO eofinfo;
3839 FILE_RENAME_INFO renameinfo;
3841 struct {
3842 FILE_INFO_BY_HANDLE_CLASS handleClass;
3843 void *ptr;
3844 DWORD size;
3845 DWORD errorCode;
3846 } checks[] = {
3847 {0xdeadbeef, NULL, 0, ERROR_INVALID_PARAMETER},
3848 {FileIdBothDirectoryInfo, NULL, 0, ERROR_BAD_LENGTH},
3849 {FileIdBothDirectoryInfo, NULL, sizeof(buffer), ERROR_NOACCESS},
3850 {FileIdBothDirectoryInfo, buffer, 0, ERROR_BAD_LENGTH}};
3852 if (!pGetFileInformationByHandleEx)
3854 win_skip("GetFileInformationByHandleEx is missing.\n");
3855 return;
3858 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
3859 ok(ret2, "GetFileInformationByHandleEx: GetTempPathA failed, got error %u.\n", GetLastError());
3861 /* ensure the existence of a file in the temp folder */
3862 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
3863 ok(ret2, "GetFileInformationByHandleEx: GetTempFileNameA failed, got error %u.\n", GetLastError());
3864 ret2 = GetFileAttributesA(tempFileName);
3865 ok(ret2 != INVALID_FILE_ATTRIBUTES, "GetFileInformationByHandleEx: "
3866 "GetFileAttributesA failed to find the temp file, got error %u.\n", GetLastError());
3868 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3869 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
3870 ok(directory != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp folder, "
3871 "got error %u.\n", GetLastError());
3873 for (i = 0; i < sizeof(checks) / sizeof(checks[0]); i += 1)
3875 SetLastError(0xdeadbeef);
3876 ret = pGetFileInformationByHandleEx(directory, checks[i].handleClass, checks[i].ptr, checks[i].size);
3877 ok(!ret && GetLastError() == checks[i].errorCode, "GetFileInformationByHandleEx: expected error %u, "
3878 "got %u.\n", checks[i].errorCode, GetLastError());
3881 while (TRUE)
3883 memset(buffer, 0xff, sizeof(buffer));
3884 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
3885 if (!ret && GetLastError() == ERROR_NO_MORE_FILES)
3886 break;
3887 ok(ret, "GetFileInformationByHandleEx: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
3888 if (!ret)
3889 break;
3890 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
3891 while (TRUE)
3893 ok(bothDirInfo->FileAttributes != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file attributes.\n");
3894 ok(bothDirInfo->FileId.u.LowPart != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file id.\n");
3895 ok(bothDirInfo->FileNameLength != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file name length.\n");
3896 if (!bothDirInfo->NextEntryOffset)
3897 break;
3898 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
3902 CloseHandle(directory);
3904 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3905 NULL, OPEN_EXISTING, 0, NULL);
3906 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
3907 "got error %u.\n", GetLastError());
3909 /* Test FileBasicInfo; make sure the write time changes when a file is updated */
3910 memset(buffer, 0xff, sizeof(buffer));
3911 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
3912 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
3913 basicInfo = (FILE_BASIC_INFO *)buffer;
3914 prevWrite = basicInfo->LastWriteTime;
3915 CloseHandle(file);
3917 Sleep(30); /* Make sure a new write time is different from the previous */
3919 /* Write something to the file, to make sure the write time has changed */
3920 file = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3921 NULL, OPEN_EXISTING, 0, NULL);
3922 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
3923 "got error %u.\n", GetLastError());
3924 ret = WriteFile(file, tempFileName, strlen(tempFileName), &written, NULL);
3925 ok(ret, "GetFileInformationByHandleEx: Write failed\n");
3926 CloseHandle(file);
3928 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3929 NULL, OPEN_EXISTING, 0, NULL);
3930 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
3931 "got error %u.\n", GetLastError());
3933 memset(buffer, 0xff, sizeof(buffer));
3934 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
3935 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
3936 basicInfo = (FILE_BASIC_INFO *)buffer;
3937 /* Could also check that the creation time didn't change - on windows
3938 * it doesn't, but on wine, it does change even if it shouldn't. */
3939 ok(basicInfo->LastWriteTime.QuadPart != prevWrite.QuadPart,
3940 "GetFileInformationByHandleEx: last write time didn't change\n");
3942 /* Test FileStandardInfo, check some basic parameters */
3943 memset(buffer, 0xff, sizeof(buffer));
3944 ret = pGetFileInformationByHandleEx(file, FileStandardInfo, buffer, sizeof(buffer));
3945 ok(ret, "GetFileInformationByHandleEx: failed to get FileStandardInfo, %u\n", GetLastError());
3946 standardInfo = (FILE_STANDARD_INFO *)buffer;
3947 ok(standardInfo->NumberOfLinks == 1, "GetFileInformationByHandleEx: Unexpcted number of links\n");
3948 ok(standardInfo->DeletePending == FALSE, "GetFileInformationByHandleEx: Unexpcted pending delete\n");
3949 ok(standardInfo->Directory == FALSE, "GetFileInformationByHandleEx: Incorrect directory flag\n");
3951 /* Test FileNameInfo */
3952 memset(buffer, 0xff, sizeof(buffer));
3953 ret = pGetFileInformationByHandleEx(file, FileNameInfo, buffer, sizeof(buffer));
3954 ok(ret, "GetFileInformationByHandleEx: failed to get FileNameInfo, %u\n", GetLastError());
3955 nameInfo = (FILE_NAME_INFO *)buffer;
3956 strPtr = strchr(tempFileName, '\\');
3957 ok(strPtr != NULL, "GetFileInformationByHandleEx: Temp filename didn't contain backslash\n");
3958 ok(nameInfo->FileNameLength == strlen(strPtr) * 2,
3959 "GetFileInformationByHandleEx: Incorrect file name length\n");
3960 for (i = 0; i < nameInfo->FileNameLength/2; i++)
3961 ok(strPtr[i] == nameInfo->FileName[i], "Incorrect filename char %d: %c vs %c\n",
3962 i, strPtr[i], nameInfo->FileName[i]);
3964 /* invalid classes */
3965 SetLastError(0xdeadbeef);
3966 ret = pGetFileInformationByHandleEx(file, FileEndOfFileInfo, &eofinfo, sizeof(eofinfo));
3967 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3969 SetLastError(0xdeadbeef);
3970 ret = pGetFileInformationByHandleEx(file, FileIoPriorityHintInfo, &priohintinfo, sizeof(priohintinfo));
3971 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3973 SetLastError(0xdeadbeef);
3974 ret = pGetFileInformationByHandleEx(file, FileAllocationInfo, &allocinfo, sizeof(allocinfo));
3975 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3977 SetLastError(0xdeadbeef);
3978 ret = pGetFileInformationByHandleEx(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
3979 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3981 SetLastError(0xdeadbeef);
3982 ret = pGetFileInformationByHandleEx(file, FileRenameInfo, &renameinfo, sizeof(renameinfo));
3983 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3985 CloseHandle(file);
3986 DeleteFileA(tempFileName);
3989 static void test_OpenFileById(void)
3991 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[256], tickCount[256];
3992 WCHAR tempFileNameW[MAX_PATH];
3993 BOOL ret, found;
3994 DWORD ret2, count, tempFileNameLen;
3995 HANDLE directory, handle, tempFile;
3996 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
3997 FILE_ID_DESCRIPTOR fileIdDescr;
3999 if (!pGetFileInformationByHandleEx || !pOpenFileById)
4001 win_skip("GetFileInformationByHandleEx or OpenFileById is missing.\n");
4002 return;
4005 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
4006 ok(ret2, "OpenFileById: GetTempPath failed, got error %u.\n", GetLastError());
4008 /* ensure the existence of a file in the temp folder */
4009 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4010 ok(ret2, "OpenFileById: GetTempFileNameA failed, got error %u.\n", GetLastError());
4011 ret2 = GetFileAttributesA(tempFileName);
4012 ok(ret2 != INVALID_FILE_ATTRIBUTES,
4013 "OpenFileById: GetFileAttributesA failed to find the temp file, got error %u\n", GetLastError());
4015 ret2 = MultiByteToWideChar(CP_ACP, 0, tempFileName + strlen(tempPath), -1, tempFileNameW, sizeof(tempFileNameW)/sizeof(tempFileNameW[0]));
4016 ok(ret2, "OpenFileById: MultiByteToWideChar failed to convert tempFileName, got error %u.\n", GetLastError());
4017 tempFileNameLen = ret2 - 1;
4019 tempFile = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4020 ok(tempFile != INVALID_HANDLE_VALUE, "OpenFileById: failed to create a temp file, "
4021 "got error %u.\n", GetLastError());
4022 ret2 = sprintf(tickCount, "%u", GetTickCount());
4023 ret = WriteFile(tempFile, tickCount, ret2, &count, NULL);
4024 ok(ret, "OpenFileById: WriteFile failed, got error %u.\n", GetLastError());
4025 CloseHandle(tempFile);
4027 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4028 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
4029 ok(directory != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder, "
4030 "got error %u.\n", GetLastError());
4032 /* get info about the temp folder itself */
4033 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4034 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4035 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4036 ok(bothDirInfo->FileNameLength == sizeof(WCHAR) && bothDirInfo->FileName[0] == '.',
4037 "OpenFileById: failed to return the temp folder at the first entry, got error %u.\n", GetLastError());
4039 /* open the temp folder itself */
4040 fileIdDescr.dwSize = sizeof(fileIdDescr);
4041 fileIdDescr.Type = FileIdType;
4042 U(fileIdDescr).FileId = bothDirInfo->FileId;
4043 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4044 todo_wine
4045 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder itself, got error %u.\n", GetLastError());
4046 CloseHandle(handle);
4048 /* find the temp file in the temp folder */
4049 found = FALSE;
4050 while (!found)
4052 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4053 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4054 if (!ret)
4055 break;
4056 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4057 while (TRUE)
4059 if (tempFileNameLen == bothDirInfo->FileNameLength / sizeof(WCHAR) &&
4060 memcmp(tempFileNameW, bothDirInfo->FileName, bothDirInfo->FileNameLength) == 0)
4062 found = TRUE;
4063 break;
4065 if (!bothDirInfo->NextEntryOffset)
4066 break;
4067 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
4070 ok(found, "OpenFileById: failed to find the temp file in the temp folder.\n");
4072 SetLastError(0xdeadbeef);
4073 handle = pOpenFileById(directory, NULL, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4074 ok(handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER,
4075 "OpenFileById: expected ERROR_INVALID_PARAMETER, got error %u.\n", GetLastError());
4077 fileIdDescr.dwSize = sizeof(fileIdDescr);
4078 fileIdDescr.Type = FileIdType;
4079 U(fileIdDescr).FileId = bothDirInfo->FileId;
4080 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4081 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the file, got error %u.\n", GetLastError());
4083 ret = ReadFile(handle, buffer, sizeof(buffer), &count, NULL);
4084 buffer[count] = 0;
4085 ok(ret, "OpenFileById: ReadFile failed, got error %u.\n", GetLastError());
4086 ok(strcmp(tickCount, buffer) == 0, "OpenFileById: invalid contents of the temp file.\n");
4088 CloseHandle(handle);
4089 CloseHandle(directory);
4090 DeleteFileA(tempFileName);
4093 static void test_SetFileValidData(void)
4095 BOOL ret;
4096 HANDLE handle;
4097 DWORD error, count;
4098 char path[MAX_PATH], filename[MAX_PATH];
4099 TOKEN_PRIVILEGES privs;
4100 HANDLE token = NULL;
4102 if (!pSetFileValidData)
4104 win_skip("SetFileValidData is missing\n");
4105 return;
4107 GetTempPathA(sizeof(path), path);
4108 GetTempFileNameA(path, "tst", 0, filename);
4109 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4110 WriteFile(handle, "test", sizeof("test") - 1, &count, NULL);
4111 CloseHandle(handle);
4113 SetLastError(0xdeadbeef);
4114 ret = pSetFileValidData(INVALID_HANDLE_VALUE, 0);
4115 error = GetLastError();
4116 ok(!ret, "SetFileValidData succeeded\n");
4117 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4119 SetLastError(0xdeadbeef);
4120 ret = pSetFileValidData(INVALID_HANDLE_VALUE, -1);
4121 error = GetLastError();
4122 ok(!ret, "SetFileValidData succeeded\n");
4123 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4125 /* file opened for reading */
4126 handle = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
4128 SetLastError(0xdeadbeef);
4129 ret = pSetFileValidData(handle, 0);
4130 ok(!ret, "SetFileValidData succeeded\n");
4131 error = GetLastError();
4132 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4134 SetLastError(0xdeadbeef);
4135 ret = pSetFileValidData(handle, -1);
4136 error = GetLastError();
4137 ok(!ret, "SetFileValidData succeeded\n");
4138 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4139 CloseHandle(handle);
4141 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4143 SetLastError(0xdeadbeef);
4144 ret = pSetFileValidData(handle, 0);
4145 error = GetLastError();
4146 ok(!ret, "SetFileValidData succeeded\n");
4147 todo_wine ok(error == ERROR_PRIVILEGE_NOT_HELD, "got %u\n", error);
4148 CloseHandle(handle);
4150 privs.PrivilegeCount = 1;
4151 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
4153 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token) ||
4154 !LookupPrivilegeValueA(NULL, SE_MANAGE_VOLUME_NAME, &privs.Privileges[0].Luid) ||
4155 !AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL) ||
4156 GetLastError() == ERROR_NOT_ALL_ASSIGNED)
4158 win_skip("cannot enable SE_MANAGE_VOLUME_NAME privilege\n");
4159 CloseHandle(token);
4160 DeleteFileA(filename);
4161 return;
4163 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4165 SetLastError(0xdeadbeef);
4166 ret = pSetFileValidData(handle, 0);
4167 error = GetLastError();
4168 ok(!ret, "SetFileValidData succeeded\n");
4169 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4171 SetLastError(0xdeadbeef);
4172 ret = pSetFileValidData(handle, -1);
4173 error = GetLastError();
4174 ok(!ret, "SetFileValidData succeeded\n");
4175 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4177 SetLastError(0xdeadbeef);
4178 ret = pSetFileValidData(handle, 2);
4179 error = GetLastError();
4180 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4181 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4183 ret = pSetFileValidData(handle, 4);
4184 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4186 SetLastError(0xdeadbeef);
4187 ret = pSetFileValidData(handle, 8);
4188 error = GetLastError();
4189 ok(!ret, "SetFileValidData succeeded\n");
4190 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4192 count = SetFilePointer(handle, 1024, NULL, FILE_END);
4193 ok(count != INVALID_SET_FILE_POINTER, "SetFilePointer failed %u\n", GetLastError());
4194 ret = SetEndOfFile(handle);
4195 ok(ret, "SetEndOfFile failed %u\n", GetLastError());
4197 SetLastError(0xdeadbeef);
4198 ret = pSetFileValidData(handle, 2);
4199 error = GetLastError();
4200 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4201 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4203 ret = pSetFileValidData(handle, 4);
4204 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4206 ret = pSetFileValidData(handle, 8);
4207 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4209 ret = pSetFileValidData(handle, 4);
4210 error = GetLastError();
4211 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4212 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4214 ret = pSetFileValidData(handle, 1024);
4215 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4217 ret = pSetFileValidData(handle, 2048);
4218 error = GetLastError();
4219 ok(!ret, "SetFileValidData succeeded\n");
4220 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4222 privs.Privileges[0].Attributes = 0;
4223 AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL);
4225 CloseHandle(token);
4226 CloseHandle(handle);
4227 DeleteFileA(filename);
4230 static void test_WriteFileGather(void)
4232 char temp_path[MAX_PATH], filename[MAX_PATH];
4233 HANDLE hfile, hiocp1, hiocp2;
4234 DWORD ret, size;
4235 ULONG_PTR key;
4236 FILE_SEGMENT_ELEMENT fse[2];
4237 OVERLAPPED ovl, *povl = NULL;
4238 SYSTEM_INFO si;
4239 LPVOID buf = NULL;
4241 ret = GetTempPathA( MAX_PATH, temp_path );
4242 ok( ret != 0, "GetTempPathA error %d\n", GetLastError() );
4243 ok( ret < MAX_PATH, "temp path should fit into MAX_PATH\n" );
4244 ret = GetTempFileNameA( temp_path, "wfg", 0, filename );
4245 ok( ret != 0, "GetTempFileNameA error %d\n", GetLastError() );
4247 hfile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
4248 FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL, 0 );
4249 ok( hfile != INVALID_HANDLE_VALUE, "CreateFile failed err %u\n", GetLastError() );
4250 if (hfile == INVALID_HANDLE_VALUE) return;
4252 hiocp1 = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 999, 0 );
4253 hiocp2 = CreateIoCompletionPort( hfile, hiocp1, 999, 0 );
4254 ok( hiocp2 != 0, "CreateIoCompletionPort failed err %u\n", GetLastError() );
4256 GetSystemInfo( &si );
4257 buf = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4258 ok( buf != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4260 memset( &ovl, 0, sizeof(ovl) );
4261 memset( fse, 0, sizeof(fse) );
4262 fse[0].Buffer = buf;
4263 if (!WriteFileGather( hfile, fse, si.dwPageSize, NULL, &ovl ))
4264 ok( GetLastError() == ERROR_IO_PENDING, "WriteFileGather failed err %u\n", GetLastError() );
4266 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4267 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4268 ok( povl == &ovl, "wrong ovl %p\n", povl );
4270 memset( &ovl, 0, sizeof(ovl) );
4271 memset( fse, 0, sizeof(fse) );
4272 fse[0].Buffer = buf;
4273 if (!ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl ))
4274 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4276 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4277 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4278 ok( povl == &ovl, "wrong ovl %p\n", povl );
4280 CloseHandle( hfile );
4281 CloseHandle( hiocp1 );
4282 CloseHandle( hiocp2 );
4283 VirtualFree( buf, 0, MEM_RELEASE );
4284 DeleteFileA( filename );
4287 static unsigned file_map_access(unsigned access)
4289 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
4290 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
4291 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
4292 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
4293 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
4296 static BOOL is_access_compatible(unsigned obj_access, unsigned desired_access)
4298 obj_access = file_map_access(obj_access);
4299 desired_access = file_map_access(desired_access);
4300 return (obj_access & desired_access) == desired_access;
4303 static void test_file_access(void)
4305 static const struct
4307 unsigned access, create_error, write_error, read_error;
4308 } td[] =
4310 { GENERIC_READ | GENERIC_WRITE, 0, 0, 0 },
4311 { GENERIC_WRITE, 0, 0, ERROR_ACCESS_DENIED },
4312 { GENERIC_READ, 0, ERROR_ACCESS_DENIED, 0 },
4313 { FILE_READ_DATA | FILE_WRITE_DATA, 0, 0, 0 },
4314 { FILE_WRITE_DATA, 0, 0, ERROR_ACCESS_DENIED },
4315 { FILE_READ_DATA, 0, ERROR_ACCESS_DENIED, 0 },
4316 { FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4317 { FILE_READ_DATA | FILE_APPEND_DATA, 0, 0, 0 },
4318 { FILE_WRITE_DATA | FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4319 { 0, 0, ERROR_ACCESS_DENIED, ERROR_ACCESS_DENIED },
4321 char path[MAX_PATH], fname[MAX_PATH];
4322 unsigned char buf[16];
4323 HANDLE hfile, hdup;
4324 DWORD i, j, ret, bytes;
4326 GetTempPathA(MAX_PATH, path);
4327 GetTempFileNameA(path, "foo", 0, fname);
4329 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
4331 SetLastError(0xdeadbeef);
4332 hfile = CreateFileA(fname, td[i].access, 0, NULL, CREATE_ALWAYS,
4333 FILE_FLAG_DELETE_ON_CLOSE, 0);
4334 if (td[i].create_error)
4336 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
4337 ok(td[i].create_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].create_error, GetLastError());
4338 continue;
4340 else
4341 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
4343 for (j = 0; j < sizeof(td)/sizeof(td[0]); j++)
4345 SetLastError(0xdeadbeef);
4346 ret = DuplicateHandle(GetCurrentProcess(), hfile, GetCurrentProcess(), &hdup,
4347 td[j].access, 0, 0);
4348 if (is_access_compatible(td[i].access, td[j].access))
4349 ok(ret, "DuplicateHandle(%#x => %#x) error %d\n", td[i].access, td[j].access, GetLastError());
4350 else
4352 /* FIXME: Remove once Wine is fixed */
4353 if ((td[j].access & (GENERIC_READ | GENERIC_WRITE)) ||
4354 (!(td[i].access & (GENERIC_WRITE | FILE_WRITE_DATA)) && (td[j].access & FILE_WRITE_DATA)) ||
4355 (!(td[i].access & (GENERIC_READ | FILE_READ_DATA)) && (td[j].access & FILE_READ_DATA)) ||
4356 (!(td[i].access & (GENERIC_WRITE)) && (td[j].access & FILE_APPEND_DATA)))
4358 todo_wine
4359 ok(!ret, "DuplicateHandle(%#x => %#x) should fail\n", td[i].access, td[j].access);
4360 todo_wine
4361 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4363 else
4365 ok(!ret, "DuplicateHandle(%#x => %#x) should fail\n", td[i].access, td[j].access);
4366 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4369 if (ret) CloseHandle(hdup);
4372 SetLastError(0xdeadbeef);
4373 bytes = 0xdeadbeef;
4374 ret = WriteFile(hfile, "\x5e\xa7", 2, &bytes, NULL);
4375 if (td[i].write_error)
4377 ok(!ret, "%d: WriteFile should fail\n", i);
4378 ok(td[i].write_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].write_error, GetLastError());
4379 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4381 else
4383 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
4384 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4387 SetLastError(0xdeadbeef);
4388 ret = SetFilePointer(hfile, 0, NULL, FILE_BEGIN);
4389 ok(ret != INVALID_SET_FILE_POINTER, "SetFilePointer error %d\n", GetLastError());
4391 SetLastError(0xdeadbeef);
4392 bytes = 0xdeadbeef;
4393 ret = ReadFile(hfile, buf, sizeof(buf), &bytes, NULL);
4394 if (td[i].read_error)
4396 ok(!ret, "%d: ReadFile should fail\n", i);
4397 ok(td[i].read_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].read_error, GetLastError());
4398 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4400 else
4402 ok(ret, "%d: ReadFile error %d\n", i, GetLastError());
4403 if (td[i].write_error)
4404 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4405 else
4407 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4408 ok(buf[0] == 0x5e && buf[1] == 0xa7, "%d: expected 5ea7, got %02x%02x\n", i, buf[0], buf[1]);
4412 CloseHandle(hfile);
4416 static void test_GetFinalPathNameByHandleA(void)
4418 static char prefix[] = "GetFinalPathNameByHandleA";
4419 static char dos_prefix[] = "\\\\?\\";
4420 char temp_path[MAX_PATH], test_path[MAX_PATH];
4421 char long_path[MAX_PATH], result_path[MAX_PATH];
4422 char dos_path[MAX_PATH + sizeof(dos_prefix)];
4423 HANDLE file;
4424 DWORD count;
4425 UINT ret;
4427 if (!pGetFinalPathNameByHandleA)
4429 skip("GetFinalPathNameByHandleA is missing\n");
4430 return;
4433 /* Test calling with INVALID_HANDLE_VALUE */
4434 SetLastError(0xdeadbeaf);
4435 count = pGetFinalPathNameByHandleA(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4436 ok(count == 0, "Expected length 0, got %u\n", count);
4437 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4439 count = GetTempPathA(MAX_PATH, temp_path);
4440 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4441 ret = GetTempFileNameA(temp_path, prefix, 0, test_path);
4442 ok(ret != 0, "GetTempFileNameA error %u\n", GetLastError());
4443 ret = GetLongPathNameA(test_path, long_path, MAX_PATH);
4444 ok(ret != 0, "GetLongPathNameA error %u\n", GetLastError());
4445 strcpy(dos_path, dos_prefix);
4446 strcat(dos_path, long_path);
4448 file = CreateFileA(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4449 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4450 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error %u\n", GetLastError());
4452 /* Test VOLUME_NAME_DOS with sufficient buffer size */
4453 memset(result_path, 0x11, sizeof(result_path));
4454 count = pGetFinalPathNameByHandleA(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4455 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4456 ok(lstrcmpiA(dos_path, result_path) == 0, "Expected %s, got %s\n", dos_path, result_path);
4458 /* Test VOLUME_NAME_DOS with insufficient buffer size */
4459 memset(result_path, 0x11, sizeof(result_path));
4460 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-2, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4461 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4462 ok(result_path[0] == 0x11, "Result path was modified\n");
4464 memset(result_path, 0x11, sizeof(result_path));
4465 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4466 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4467 ok(result_path[0] == 0x11, "Result path was modified\n");
4469 memset(result_path, 0x11, sizeof(result_path));
4470 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4471 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4472 ok(result_path[0] == 0x11, "Result path was modified\n");
4474 memset(result_path, 0x11, sizeof(result_path));
4475 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4476 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4477 ok(result_path[0] != 0x11, "Result path was not modified\n");
4478 ok(!result_path[strlen(dos_path)], "Expected nullterminated string\n");
4479 ok(result_path[strlen(dos_path)+1] == 0x11, "Buffer overflow\n");
4481 CloseHandle(file);
4484 static void test_GetFinalPathNameByHandleW(void)
4486 static WCHAR prefix[] = {'G','e','t','F','i','n','a','l','P','a','t','h',
4487 'N','a','m','e','B','y','H','a','n','d','l','e','W','\0'};
4488 static WCHAR dos_prefix[] = {'\\','\\','?','\\','\0'};
4489 WCHAR temp_path[MAX_PATH], test_path[MAX_PATH];
4490 WCHAR long_path[MAX_PATH], result_path[MAX_PATH];
4491 WCHAR dos_path[MAX_PATH + sizeof(dos_prefix)];
4492 WCHAR drive_part[MAX_PATH];
4493 WCHAR *file_part;
4494 WCHAR volume_path[MAX_PATH + 50];
4495 WCHAR nt_path[2 * MAX_PATH];
4496 BOOL success;
4497 HANDLE file;
4498 DWORD count;
4499 UINT ret;
4501 if (!pGetFinalPathNameByHandleW)
4503 skip("GetFinalPathNameByHandleW is missing\n");
4504 return;
4507 /* Test calling with INVALID_HANDLE_VALUE */
4508 SetLastError(0xdeadbeaf);
4509 count = pGetFinalPathNameByHandleW(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4510 ok(count == 0, "Expected length 0, got %u\n", count);
4511 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4513 count = GetTempPathW(MAX_PATH, temp_path);
4514 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4515 ret = GetTempFileNameW(temp_path, prefix, 0, test_path);
4516 ok(ret != 0, "GetTempFileNameW error %u\n", GetLastError());
4517 ret = GetLongPathNameW(test_path, long_path, MAX_PATH);
4518 ok(ret != 0, "GetLongPathNameW error %u\n", GetLastError());
4519 lstrcpyW(dos_path, dos_prefix);
4520 lstrcatW(dos_path, long_path);
4522 file = CreateFileW(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4523 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4524 ok(file != INVALID_HANDLE_VALUE, "CreateFileW error %u\n", GetLastError());
4526 /* Test VOLUME_NAME_DOS with sufficient buffer size */
4527 memset(result_path, 0x11, sizeof(result_path));
4528 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4529 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
4530 ok(lstrcmpiW(dos_path, result_path) == 0, "Expected %s, got %s\n", wine_dbgstr_w(dos_path), wine_dbgstr_w(result_path));
4532 /* Test VOLUME_NAME_DOS with insufficient buffer size */
4533 memset(result_path, 0x11, sizeof(result_path));
4534 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4535 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4536 ok(result_path[0] == 0x1111, "Result path was modified\n");
4538 memset(result_path, 0x11, sizeof(result_path));
4539 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4540 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4541 ok(result_path[0] == 0x1111, "Result path was modified\n");
4543 memset(result_path, 0x11, sizeof(result_path));
4544 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4545 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
4546 ok(result_path[0] != 0x1111, "Result path was not modified\n");
4547 ok(!result_path[lstrlenW(dos_path)], "Expected nullterminated string\n");
4548 ok(result_path[lstrlenW(dos_path)+1] == 0x1111, "Buffer overflow\n");
4550 success = GetVolumePathNameW(long_path, drive_part, MAX_PATH);
4551 ok(success, "GetVolumePathNameW error %u\n", GetLastError());
4552 success = GetVolumeNameForVolumeMountPointW(drive_part, volume_path, sizeof(volume_path) / sizeof(WCHAR));
4553 ok(success, "GetVolumeNameForVolumeMountPointW error %u\n", GetLastError());
4555 /* Test for VOLUME_NAME_GUID */
4556 lstrcatW(volume_path, long_path + lstrlenW(drive_part));
4557 memset(result_path, 0x11, sizeof(result_path));
4558 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_GUID);
4559 ok(count == lstrlenW(volume_path), "Expected length %u, got %u\n", lstrlenW(volume_path), count);
4560 ok(lstrcmpiW(volume_path, result_path) == 0, "Expected %s, got %s\n",
4561 wine_dbgstr_w(volume_path), wine_dbgstr_w(result_path));
4563 /* Test for VOLUME_NAME_NONE */
4564 file_part = long_path + lstrlenW(drive_part) - 1;
4565 memset(result_path, 0x11, sizeof(result_path));
4566 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NONE);
4567 ok(count == lstrlenW(file_part), "Expected length %u, got %u\n", lstrlenW(file_part), count);
4568 ok(lstrcmpiW(file_part, result_path) == 0, "Expected %s, got %s\n",
4569 wine_dbgstr_w(file_part), wine_dbgstr_w(result_path));
4571 drive_part[lstrlenW(drive_part)-1] = 0;
4572 success = QueryDosDeviceW(drive_part, nt_path, sizeof(nt_path) / sizeof(WCHAR));
4573 ok(success, "QueryDosDeviceW error %u\n", GetLastError());
4575 /* Test for VOLUME_NAME_NT */
4576 lstrcatW(nt_path, file_part);
4577 memset(result_path, 0x11, sizeof(result_path));
4578 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NT);
4579 ok(count == lstrlenW(nt_path), "Expected length %u, got %u\n", lstrlenW(nt_path), count);
4580 ok(lstrcmpiW(nt_path, result_path) == 0, "Expected %s, got %s\n",
4581 wine_dbgstr_w(nt_path), wine_dbgstr_w(result_path));
4583 CloseHandle(file);
4586 static void test_SetFileInformationByHandle(void)
4588 FILE_ATTRIBUTE_TAG_INFO fileattrinfo = { 0 };
4589 FILE_REMOTE_PROTOCOL_INFO protinfo = { 0 };
4590 FILE_STANDARD_INFO stdinfo = { };
4591 FILE_COMPRESSION_INFO compressinfo;
4592 char tempFileName[MAX_PATH];
4593 char tempPath[MAX_PATH];
4594 HANDLE file;
4595 BOOL ret;
4597 if (!pSetFileInformationByHandle)
4599 win_skip("SetFileInformationByHandle is not supported\n");
4600 return;
4603 ret = GetTempPathA(sizeof(tempPath), tempPath);
4604 ok(ret, "GetTempPathA failed, got error %u.\n", GetLastError());
4606 /* ensure the existence of a file in the temp folder */
4607 ret = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4608 ok(ret, "GetTempFileNameA failed, got error %u.\n", GetLastError());
4610 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4611 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
4612 ok(file != INVALID_HANDLE_VALUE, "failed to open the temp file, error %u.\n", GetLastError());
4614 /* invalid classes */
4615 SetLastError(0xdeadbeef);
4616 ret = pSetFileInformationByHandle(file, FileStandardInfo, &stdinfo, sizeof(stdinfo));
4617 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4619 memset(&compressinfo, 0, sizeof(compressinfo));
4620 SetLastError(0xdeadbeef);
4621 ret = pSetFileInformationByHandle(file, FileCompressionInfo, &compressinfo, sizeof(compressinfo));
4622 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4624 SetLastError(0xdeadbeef);
4625 ret = pSetFileInformationByHandle(file, FileAttributeTagInfo, &fileattrinfo, sizeof(fileattrinfo));
4626 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4628 memset(&protinfo, 0, sizeof(protinfo));
4629 protinfo.StructureVersion = 1;
4630 protinfo.StructureSize = sizeof(protinfo);
4631 SetLastError(0xdeadbeef);
4632 ret = pSetFileInformationByHandle(file, FileRemoteProtocolInfo, &protinfo, sizeof(protinfo));
4633 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4635 CloseHandle(file);
4638 START_TEST(file)
4640 InitFunctionPointers();
4642 test__hread( );
4643 test__hwrite( );
4644 test__lclose( );
4645 test__lcreat( );
4646 test__llseek( );
4647 test__llopen( );
4648 test__lread( );
4649 test__lwrite( );
4650 test_GetTempFileNameA();
4651 test_CopyFileA();
4652 test_CopyFileW();
4653 test_CopyFile2();
4654 test_CopyFileEx();
4655 test_CreateFile();
4656 test_CreateFileA();
4657 test_CreateFileW();
4658 test_CreateFile2();
4659 test_DeleteFileA();
4660 test_DeleteFileW();
4661 test_MoveFileA();
4662 test_MoveFileW();
4663 test_FindFirstFileA();
4664 test_FindNextFileA();
4665 test_FindFirstFileExA(FindExInfoStandard, 0, 0);
4666 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_CASE_SENSITIVE);
4667 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_LARGE_FETCH);
4668 test_FindFirstFileExA(FindExInfoBasic, 0, 0);
4669 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
4670 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, 0);
4671 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_CASE_SENSITIVE);
4672 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_LARGE_FETCH);
4673 test_FindFirstFileExA(FindExInfoBasic, FindExSearchLimitToDirectories, 0);
4674 test_LockFile();
4675 test_file_sharing();
4676 test_offset_in_overlapped_structure();
4677 test_MapFile();
4678 test_GetFileType();
4679 test_async_file_errors();
4680 test_read_write();
4681 test_OpenFile();
4682 test_overlapped();
4683 test_RemoveDirectory();
4684 test_ReplaceFileA();
4685 test_ReplaceFileW();
4686 test_GetFileInformationByHandleEx();
4687 test_OpenFileById();
4688 test_SetFileValidData();
4689 test_WriteFileGather();
4690 test_file_access();
4691 test_GetFinalPathNameByHandleA();
4692 test_GetFinalPathNameByHandleW();
4693 test_SetFileInformationByHandle();