kernel32: Return a dummy value in GetSystemPreferredUILanguages.
[wine.git] / dlls / kernel32 / tests / file.c
blob58ddddf68235bc3cf2b67a6818fb9daf6155fc4b
1 /*
2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 * Copyright (c) 2008 Jeff Zaroyko
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* ReplaceFile requires Windows 2000 or newer */
24 #define _WIN32_WINNT 0x0500
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <stdio.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "wine/test.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winternl.h"
38 #include "winnls.h"
39 #include "fileapi.h"
41 #ifdef WINE_NO_UNICODE_MACROS
42 #undef DeleteFile /* needed for FILE_DISPOSITION_INFO */
43 #endif
45 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
46 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
47 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
48 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
49 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
50 static DWORD (WINAPI *pQueueUserAPC)(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
51 static BOOL (WINAPI *pGetFileInformationByHandleEx)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, LPVOID, DWORD);
52 static HANDLE (WINAPI *pOpenFileById)(HANDLE, LPFILE_ID_DESCRIPTOR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD);
53 static BOOL (WINAPI *pSetFileValidData)(HANDLE, LONGLONG);
54 static HRESULT (WINAPI *pCopyFile2)(PCWSTR,PCWSTR,COPYFILE2_EXTENDED_PARAMETERS*);
55 static HANDLE (WINAPI *pCreateFile2)(LPCWSTR, DWORD, DWORD, DWORD, CREATEFILE2_EXTENDED_PARAMETERS*);
56 static DWORD (WINAPI *pGetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, DWORD);
57 static DWORD (WINAPI *pGetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, DWORD);
58 static NTSTATUS (WINAPI *pNtCreateFile)(PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK,
59 PLARGE_INTEGER, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG);
60 static BOOL (WINAPI *pRtlDosPathNameToNtPathName_U)(LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR*);
61 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PCANSI_STRING, BOOLEAN);
62 static BOOL (WINAPI *pSetFileInformationByHandle)(HANDLE, FILE_INFO_BY_HANDLE_CLASS, void*, DWORD);
64 static const char filename[] = "testfile.xxx";
65 static const char sillytext[] =
66 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
67 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
68 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
69 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
70 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
71 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
72 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
73 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
74 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
75 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
77 struct test_list {
78 const char *file; /* file string to test */
79 const DWORD err; /* Win NT and further error code */
80 const LONG err2; /* Win 9x & ME error code or -1 */
81 const DWORD options; /* option flag to use for open */
82 const BOOL todo_flag; /* todo_wine indicator */
83 } ;
85 static void InitFunctionPointers(void)
87 HMODULE hntdll = GetModuleHandleA("ntdll");
88 HMODULE hkernel32 = GetModuleHandleA("kernel32");
90 pNtCreateFile = (void *)GetProcAddress(hntdll, "NtCreateFile");
91 pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
92 pRtlAnsiStringToUnicodeString = (void *)GetProcAddress(hntdll, "RtlAnsiStringToUnicodeString");
94 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
95 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
96 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
97 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
98 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
99 pQueueUserAPC = (void *) GetProcAddress(hkernel32, "QueueUserAPC");
100 pGetFileInformationByHandleEx = (void *) GetProcAddress(hkernel32, "GetFileInformationByHandleEx");
101 pOpenFileById = (void *) GetProcAddress(hkernel32, "OpenFileById");
102 pSetFileValidData = (void *) GetProcAddress(hkernel32, "SetFileValidData");
103 pCopyFile2 = (void *) GetProcAddress(hkernel32, "CopyFile2");
104 pCreateFile2 = (void *) GetProcAddress(hkernel32, "CreateFile2");
105 pGetFinalPathNameByHandleA = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleA");
106 pGetFinalPathNameByHandleW = (void *) GetProcAddress(hkernel32, "GetFinalPathNameByHandleW");
107 pSetFileInformationByHandle = (void *) GetProcAddress(hkernel32, "SetFileInformationByHandle");
110 static void test__hread( void )
112 HFILE filehandle;
113 char buffer[10000];
114 LONG bytes_read;
115 LONG bytes_wanted;
116 LONG i;
117 BOOL ret;
119 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
120 DeleteFileA( filename );
121 filehandle = _lcreat( filename, 0 );
122 if (filehandle == HFILE_ERROR)
124 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
125 return;
128 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
130 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
132 filehandle = _lopen( filename, OF_READ );
134 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
136 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
138 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
140 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
142 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
143 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
144 for (i = 0; i < bytes_wanted; i++)
146 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
150 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
152 ret = DeleteFileA( filename );
153 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
157 static void test__hwrite( void )
159 HFILE filehandle;
160 char buffer[10000];
161 LONG bytes_read;
162 LONG bytes_written;
163 ULONG blocks;
164 LONG i;
165 char *contents;
166 HLOCAL memory_object;
167 char checksum[1];
168 BOOL ret;
170 filehandle = _lcreat( filename, 0 );
171 if (filehandle == HFILE_ERROR)
173 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
174 return;
177 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
179 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
181 filehandle = _lopen( filename, OF_READ );
183 bytes_read = _hread( filehandle, buffer, 1);
185 ok( 0 == bytes_read, "file read size error\n" );
187 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
189 filehandle = _lopen( filename, OF_READWRITE );
191 bytes_written = 0;
192 checksum[0] = '\0';
193 srand( (unsigned)time( NULL ) );
194 for (blocks = 0; blocks < 100; blocks++)
196 for (i = 0; i < (LONG)sizeof( buffer ); i++)
198 buffer[i] = rand( );
199 checksum[0] = checksum[0] + buffer[i];
201 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
202 bytes_written = bytes_written + sizeof( buffer );
205 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
206 bytes_written++;
208 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
210 memory_object = LocalAlloc( LPTR, bytes_written );
212 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
214 contents = LocalLock( memory_object );
215 ok( NULL != contents, "LocalLock whines\n" );
217 filehandle = _lopen( filename, OF_READ );
219 contents = LocalLock( memory_object );
220 ok( NULL != contents, "LocalLock whines\n" );
222 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
224 checksum[0] = '\0';
225 i = 0;
228 checksum[0] = checksum[0] + contents[i];
229 i++;
231 while (i < bytes_written - 1);
233 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
235 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
237 ret = DeleteFileA( filename );
238 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
240 LocalFree( contents );
244 static void test__lclose( void )
246 HFILE filehandle;
247 BOOL ret;
249 filehandle = _lcreat( filename, 0 );
250 if (filehandle == HFILE_ERROR)
252 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
253 return;
256 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
258 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
260 ret = DeleteFileA( filename );
261 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
264 /* helper function for test__lcreat */
265 static void get_nt_pathW( const char *name, UNICODE_STRING *nameW )
267 UNICODE_STRING strW;
268 ANSI_STRING str;
269 NTSTATUS status;
270 BOOLEAN ret;
271 RtlInitAnsiString( &str, name );
273 status = pRtlAnsiStringToUnicodeString( &strW, &str, TRUE );
274 ok( !status, "RtlAnsiStringToUnicodeString failed with %08x\n", status );
276 ret = pRtlDosPathNameToNtPathName_U( strW.Buffer, nameW, NULL, NULL );
277 ok( ret, "RtlDosPathNameToNtPathName_U failed\n" );
279 RtlFreeUnicodeString( &strW );
282 static void test__lcreat( void )
284 UNICODE_STRING filenameW;
285 OBJECT_ATTRIBUTES attr;
286 IO_STATUS_BLOCK io;
287 HFILE filehandle;
288 char buffer[10000];
289 WIN32_FIND_DATAA search_results;
290 char slashname[] = "testfi/";
291 int err;
292 HANDLE find, file;
293 NTSTATUS status;
294 BOOL ret;
296 filehandle = _lcreat( filename, 0 );
297 if (filehandle == HFILE_ERROR)
299 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
300 return;
303 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
305 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
307 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
309 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
311 find = FindFirstFileA( filename, &search_results );
312 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
313 FindClose( find );
315 ret = DeleteFileA(filename);
316 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
318 filehandle = _lcreat( filename, 1 ); /* readonly */
319 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
321 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
323 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
325 find = FindFirstFileA( filename, &search_results );
326 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
327 FindClose( find );
329 SetLastError( 0xdeadbeef );
330 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
331 ok( GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError() );
333 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
335 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
337 filehandle = _lcreat( filename, 1 ); /* readonly */
338 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError() );
339 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen(sillytext) ),
340 "_hwrite shouldn't be able to write never the less\n" );
341 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
343 find = FindFirstFileA( filename, &search_results );
344 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
345 FindClose( find );
347 get_nt_pathW( filename, &filenameW );
348 attr.Length = sizeof(attr);
349 attr.RootDirectory = 0;
350 attr.Attributes = OBJ_CASE_INSENSITIVE;
351 attr.ObjectName = &filenameW;
352 attr.SecurityDescriptor = NULL;
353 attr.SecurityQualityOfService = NULL;
355 status = NtCreateFile( &file, GENERIC_READ | GENERIC_WRITE | DELETE, &attr, &io, NULL, 0,
356 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
357 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
358 ok( status == STATUS_ACCESS_DENIED, "expected STATUS_ACCESS_DENIED, got %08x\n", status );
359 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
361 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
362 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
363 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
364 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
366 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
367 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
368 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_DIRECTORY_FILE, NULL, 0 );
369 ok( status == STATUS_NOT_A_DIRECTORY, "expected STATUS_NOT_A_DIRECTORY, got %08x\n", status );
371 status = NtCreateFile( &file, DELETE, &attr, &io, NULL, 0,
372 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
373 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0 );
374 todo_wine
375 ok( status == STATUS_CANNOT_DELETE, "expected STATUS_CANNOT_DELETE, got %08x\n", status );
376 if (!status) CloseHandle( file );
378 RtlFreeUnicodeString( &filenameW );
380 todo_wine
381 ok( GetFileAttributesA( filename ) != INVALID_FILE_ATTRIBUTES, "file was deleted\n" );
382 todo_wine
383 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
384 todo_wine
385 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file\n" );
387 filehandle = _lcreat( filename, 2 );
388 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
390 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
392 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
394 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
396 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
398 find = FindFirstFileA( filename, &search_results );
399 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
400 FindClose( find );
402 ret = DeleteFileA( filename );
403 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
405 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
406 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
408 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
410 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
412 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
414 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
416 find = FindFirstFileA( filename, &search_results );
417 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
418 FindClose( find );
420 ret = DeleteFileA( filename );
421 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
423 filehandle=_lcreat (slashname, 0); /* illegal name */
424 if (HFILE_ERROR==filehandle) {
425 err=GetLastError ();
426 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
427 "creating file \"%s\" failed with error %d\n", slashname, err);
428 } else { /* only NT succeeds */
429 _lclose(filehandle);
430 find=FindFirstFileA (slashname, &search_results);
431 if (INVALID_HANDLE_VALUE!=find)
433 ret = FindClose (find);
434 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
435 slashname[strlen(slashname)-1]=0;
436 ok (!strcmp (slashname, search_results.cFileName),
437 "found unexpected name \"%s\"\n", search_results.cFileName);
438 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
439 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
440 search_results.dwFileAttributes);
442 ret = DeleteFileA( slashname );
443 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
446 filehandle=_lcreat (filename, 8); /* illegal attribute */
447 if (HFILE_ERROR==filehandle)
448 ok (0, "couldn't create volume label \"%s\"\n", filename);
449 else {
450 _lclose(filehandle);
451 find=FindFirstFileA (filename, &search_results);
452 if (INVALID_HANDLE_VALUE==find)
453 ok (0, "file \"%s\" not found\n", filename);
454 else {
455 ret = FindClose(find);
456 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
457 ok (!strcmp (filename, search_results.cFileName),
458 "found unexpected name \"%s\"\n", search_results.cFileName);
459 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
460 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
461 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
462 search_results.dwFileAttributes);
464 ret = DeleteFileA( filename );
465 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
470 static void test__llseek( void )
472 INT i;
473 HFILE filehandle;
474 char buffer[1];
475 LONG bytes_read;
476 BOOL ret;
478 filehandle = _lcreat( filename, 0 );
479 if (filehandle == HFILE_ERROR)
481 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
482 return;
485 for (i = 0; i < 400; i++)
487 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
489 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
490 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
492 bytes_read = _hread( filehandle, buffer, 1);
493 ok( 1 == bytes_read, "file read size error\n" );
494 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
495 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
497 bytes_read = _hread( filehandle, buffer, 1);
498 ok( 1 == bytes_read, "file read size error\n" );
499 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
500 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
501 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
503 ret = DeleteFileA( filename );
504 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
508 static void test__llopen( void )
510 HFILE filehandle;
511 UINT bytes_read;
512 char buffer[10000];
513 BOOL ret;
515 filehandle = _lcreat( filename, 0 );
516 if (filehandle == HFILE_ERROR)
518 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
519 return;
522 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
523 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
525 filehandle = _lopen( filename, OF_READ );
526 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
527 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
528 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
529 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
531 filehandle = _lopen( filename, OF_READWRITE );
532 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
533 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
534 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
535 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
537 filehandle = _lopen( filename, OF_WRITE );
538 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
539 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
540 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
542 ret = DeleteFileA( filename );
543 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
544 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
548 static void test__lread( void )
550 HFILE filehandle;
551 char buffer[10000];
552 UINT bytes_read;
553 UINT bytes_wanted;
554 UINT i;
555 BOOL ret;
557 filehandle = _lcreat( filename, 0 );
558 if (filehandle == HFILE_ERROR)
560 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
561 return;
564 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
566 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
568 filehandle = _lopen( filename, OF_READ );
570 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
572 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
574 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
576 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
578 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
579 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
580 for (i = 0; i < bytes_wanted; i++)
582 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
586 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
588 ret = DeleteFileA( filename );
589 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
593 static void test__lwrite( void )
595 HFILE filehandle;
596 char buffer[10000];
597 UINT bytes_read;
598 UINT bytes_written;
599 UINT blocks;
600 INT i;
601 char *contents;
602 HLOCAL memory_object;
603 char checksum[1];
604 BOOL ret;
606 filehandle = _lcreat( filename, 0 );
607 if (filehandle == HFILE_ERROR)
609 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
610 return;
613 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
615 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
617 filehandle = _lopen( filename, OF_READ );
619 bytes_read = _hread( filehandle, buffer, 1);
621 ok( 0 == bytes_read, "file read size error\n" );
623 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
625 filehandle = _lopen( filename, OF_READWRITE );
627 bytes_written = 0;
628 checksum[0] = '\0';
629 srand( (unsigned)time( NULL ) );
630 for (blocks = 0; blocks < 100; blocks++)
632 for (i = 0; i < (INT)sizeof( buffer ); i++)
634 buffer[i] = rand( );
635 checksum[0] = checksum[0] + buffer[i];
637 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
638 bytes_written = bytes_written + sizeof( buffer );
641 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
642 bytes_written++;
644 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
646 memory_object = LocalAlloc( LPTR, bytes_written );
648 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
650 contents = LocalLock( memory_object );
651 ok( NULL != contents, "LocalLock whines\n" );
653 filehandle = _lopen( filename, OF_READ );
655 contents = LocalLock( memory_object );
656 ok( NULL != contents, "LocalLock whines\n" );
658 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
660 checksum[0] = '\0';
661 i = 0;
664 checksum[0] += contents[i];
665 i++;
667 while (i < bytes_written - 1);
669 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
671 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
673 ret = DeleteFileA( filename );
674 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
676 LocalFree( contents );
679 static void test_CopyFileA(void)
681 char temp_path[MAX_PATH];
682 char source[MAX_PATH], dest[MAX_PATH];
683 static const char prefix[] = "pfx";
684 HANDLE hfile;
685 HANDLE hmapfile;
686 FILETIME ft1, ft2;
687 char buf[10];
688 DWORD ret;
689 BOOL retok;
691 ret = GetTempPathA(MAX_PATH, temp_path);
692 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
693 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
695 ret = GetTempFileNameA(temp_path, prefix, 0, source);
696 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
698 ret = MoveFileA(source, source);
699 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
701 /* copying a file to itself must fail */
702 retok = CopyFileA(source, source, FALSE);
703 ok( !retok && (GetLastError() == ERROR_SHARING_VIOLATION || broken(GetLastError() == ERROR_FILE_EXISTS) /* Win 9x */),
704 "copying a file to itself didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
706 /* make the source have not zero size */
707 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
708 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
709 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
710 ok( retok && ret == sizeof(prefix),
711 "WriteFile error %d\n", GetLastError());
712 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
713 /* get the file time and change it to prove the difference */
714 ret = GetFileTime(hfile, NULL, NULL, &ft1);
715 ok( ret, "GetFileTime error %d\n", GetLastError());
716 ft1.dwLowDateTime -= 600000000; /* 60 second */
717 ret = SetFileTime(hfile, NULL, NULL, &ft1);
718 ok( ret, "SetFileTime error %d\n", GetLastError());
719 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
720 CloseHandle(hfile);
722 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
723 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
725 SetLastError(0xdeadbeef);
726 ret = CopyFileA(source, dest, TRUE);
727 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
728 "CopyFileA: unexpected error %d\n", GetLastError());
730 ret = CopyFileA(source, dest, FALSE);
731 ok(ret, "CopyFileA: error %d\n", GetLastError());
733 /* copying from a read-locked source fails */
734 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
735 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
736 retok = CopyFileA(source, dest, FALSE);
737 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
738 "copying from a read-locked file succeeded when it shouldn't have\n");
739 /* in addition, the source is opened before the destination */
740 retok = CopyFileA("25f99d3b-4ba4-4f66-88f5-2906886993cc", dest, FALSE);
741 ok(!retok && GetLastError() == ERROR_FILE_NOT_FOUND,
742 "copying from a file that doesn't exist failed in an unexpected way (ret=%d, err=%d)\n", retok, GetLastError());
743 CloseHandle(hfile);
745 /* copying from a r+w opened, r shared source succeeds */
746 hfile = CreateFileA(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
747 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
748 retok = CopyFileA(source, dest, FALSE);
749 ok(retok,
750 "copying from an r+w opened and r shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
751 CloseHandle(hfile);
753 /* copying from a delete-locked source mostly succeeds */
754 hfile = CreateFileA(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
755 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
756 retok = CopyFileA(source, dest, FALSE);
757 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* NT, 2000, XP */,
758 "copying from a delete-locked file failed (ret=%d, err=%d)\n", retok, GetLastError());
759 CloseHandle(hfile);
761 /* copying to a write-locked destination fails */
762 hfile = CreateFileA(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
763 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
764 retok = CopyFileA(source, dest, FALSE);
765 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
766 "copying to a write-locked file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
767 CloseHandle(hfile);
769 /* copying to a r+w opened, w shared destination mostly succeeds */
770 hfile = CreateFileA(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
771 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
772 retok = CopyFileA(source, dest, FALSE);
773 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
774 "copying to a r+w opened and w shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
775 CloseHandle(hfile);
777 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
778 hfile = CreateFileA(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
779 ok(hfile != INVALID_HANDLE_VALUE || broken(GetLastError() == ERROR_INVALID_PARAMETER) /* Win 9x */,
780 "failed to open destination file, error %d\n", GetLastError());
781 if (hfile != INVALID_HANDLE_VALUE)
783 retok = CopyFileA(source, dest, FALSE);
784 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
785 "copying to a delete-locked shared file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
786 CloseHandle(hfile);
789 /* copy to a file that's opened the way Wine opens the source */
790 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
791 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
792 retok = CopyFileA(source, dest, FALSE);
793 ok(retok || broken(GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
794 "copying to a file opened the way Wine opens the source failed (ret=%d, err=%d)\n", retok, GetLastError());
795 CloseHandle(hfile);
797 /* make sure that destination has correct size */
798 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
799 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
800 ret = GetFileSize(hfile, NULL);
801 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
803 /* make sure that destination has the same filetime */
804 ret = GetFileTime(hfile, NULL, NULL, &ft2);
805 ok( ret, "GetFileTime error %d\n", GetLastError());
806 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
808 SetLastError(0xdeadbeef);
809 ret = CopyFileA(source, dest, FALSE);
810 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
811 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
813 /* make sure that destination still has correct size */
814 ret = GetFileSize(hfile, NULL);
815 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
816 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
817 ok( retok && ret == sizeof(prefix),
818 "ReadFile: error %d\n", GetLastError());
819 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
821 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
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 && GetLastError() == ERROR_SHARING_VIOLATION,
827 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
829 CloseHandle(hmapfile);
830 CloseHandle(hfile);
832 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
833 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
835 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
836 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
837 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
839 ret = CopyFileA(source, dest, FALSE);
840 ok(!ret, "CopyFileA: expected failure\n");
841 ok(GetLastError() == ERROR_USER_MAPPED_FILE ||
842 broken(GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x */
843 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
845 CloseHandle(hmapfile);
846 CloseHandle(hfile);
848 ret = DeleteFileA(source);
849 ok(ret, "DeleteFileA: error %d\n", GetLastError());
850 ret = DeleteFileA(dest);
851 ok(ret, "DeleteFileA: error %d\n", GetLastError());
854 static void test_CopyFileW(void)
856 WCHAR temp_path[MAX_PATH];
857 WCHAR source[MAX_PATH], dest[MAX_PATH];
858 static const WCHAR prefix[] = {'p','f','x',0};
859 DWORD ret;
861 ret = GetTempPathW(MAX_PATH, temp_path);
862 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
864 win_skip("GetTempPathW is not available\n");
865 return;
867 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
868 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
870 ret = GetTempFileNameW(temp_path, prefix, 0, source);
871 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
873 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
874 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
876 ret = CopyFileW(source, dest, TRUE);
877 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
878 "CopyFileW: unexpected error %d\n", GetLastError());
880 ret = CopyFileW(source, dest, FALSE);
881 ok(ret, "CopyFileW: error %d\n", GetLastError());
883 ret = DeleteFileW(source);
884 ok(ret, "DeleteFileW: error %d\n", GetLastError());
885 ret = DeleteFileW(dest);
886 ok(ret, "DeleteFileW: error %d\n", GetLastError());
889 static void test_CopyFile2(void)
891 static const WCHAR doesntexistW[] = {'d','o','e','s','n','t','e','x','i','s','t',0};
892 static const WCHAR prefix[] = {'p','f','x',0};
893 WCHAR source[MAX_PATH], dest[MAX_PATH], temp_path[MAX_PATH];
894 COPYFILE2_EXTENDED_PARAMETERS params;
895 HANDLE hfile, hmapfile;
896 FILETIME ft1, ft2;
897 DWORD ret, len;
898 char buf[10];
899 HRESULT hr;
901 if (!pCopyFile2)
903 skip("CopyFile2 is not available\n");
904 return;
907 ret = GetTempPathW(MAX_PATH, temp_path);
908 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
909 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
911 ret = GetTempFileNameW(temp_path, prefix, 0, source);
912 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
914 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
915 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
917 /* fail if exists */
918 memset(&params, 0, sizeof(params));
919 params.dwSize = sizeof(params);
920 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
922 SetLastError(0xdeadbeef);
923 hr = pCopyFile2(source, dest, &params);
924 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
925 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
927 /* don't fail if exists */
928 params.dwSize = sizeof(params);
929 params.dwCopyFlags = 0;
931 hr = pCopyFile2(source, dest, &params);
932 ok(hr == S_OK, "CopyFile2: error 0x%08x\n", hr);
934 /* copying a file to itself must fail */
935 params.dwSize = sizeof(params);
936 params.dwCopyFlags = 0;
938 SetLastError(0xdeadbeef);
939 hr = pCopyFile2(source, source, &params);
940 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: copying a file to itself didn't fail, 0x%08x\n", hr);
941 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
943 /* make the source have not zero size */
944 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
945 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
946 ret = WriteFile(hfile, prefix, sizeof(prefix), &len, NULL );
947 ok(ret && len == sizeof(prefix), "WriteFile error %d\n", GetLastError());
948 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
950 /* get the file time and change it to prove the difference */
951 ret = GetFileTime(hfile, NULL, NULL, &ft1);
952 ok(ret, "GetFileTime error %d\n", GetLastError());
953 ft1.dwLowDateTime -= 600000000; /* 60 second */
954 ret = SetFileTime(hfile, NULL, NULL, &ft1);
955 ok(ret, "SetFileTime error %d\n", GetLastError());
956 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
957 CloseHandle(hfile);
959 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
960 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
962 params.dwSize = sizeof(params);
963 params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
965 SetLastError(0xdeadbeef);
966 hr = pCopyFile2(source, dest, &params);
967 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "CopyFile2: unexpected error 0x%08x\n", hr);
968 ok(GetLastError() == ERROR_FILE_EXISTS, "CopyFile2: last error %d\n", GetLastError());
970 params.dwSize = sizeof(params);
971 params.dwCopyFlags = 0;
972 hr = pCopyFile2(source, dest, &params);
973 ok(ret, "CopyFile2: error 0x%08x\n", hr);
975 /* copying from a read-locked source fails */
976 hfile = CreateFileW(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
977 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
979 params.dwSize = sizeof(params);
980 params.dwCopyFlags = 0;
981 SetLastError(0xdeadbeef);
982 hr = pCopyFile2(source, dest, &params);
983 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
984 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
986 /* in addition, the source is opened before the destination */
987 params.dwSize = sizeof(params);
988 params.dwCopyFlags = 0;
989 SetLastError(0xdeadbeef);
990 hr = pCopyFile2(doesntexistW, dest, &params);
991 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "got 0x%08x\n", hr);
992 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "CopyFile2: last error %d\n", GetLastError());
993 CloseHandle(hfile);
995 /* copying from a r+w opened, r shared source succeeds */
996 hfile = CreateFileW(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
997 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
999 params.dwSize = sizeof(params);
1000 params.dwCopyFlags = 0;
1001 hr = pCopyFile2(source, dest, &params);
1002 ok(hr == S_OK, "failed 0x%08x\n", hr);
1003 CloseHandle(hfile);
1005 /* copying from a delete-locked source mostly succeeds */
1006 hfile = CreateFileW(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1007 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
1009 params.dwSize = sizeof(params);
1010 params.dwCopyFlags = 0;
1011 hr = pCopyFile2(source, dest, &params);
1012 ok(hr == S_OK, "failed 0x%08x\n", hr);
1013 CloseHandle(hfile);
1015 /* copying to a write-locked destination fails */
1016 hfile = CreateFileW(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1017 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1019 params.dwSize = sizeof(params);
1020 params.dwCopyFlags = 0;
1021 SetLastError(0xdeadbeef);
1022 hr = pCopyFile2(source, dest, FALSE);
1023 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1024 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1025 CloseHandle(hfile);
1027 /* copying to a r+w opened, w shared destination mostly succeeds */
1028 hfile = CreateFileW(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1029 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1031 params.dwSize = sizeof(params);
1032 params.dwCopyFlags = 0;
1033 hr = pCopyFile2(source, dest, FALSE);
1034 ok(hr == S_OK, "got 0x%08x\n", hr);
1035 CloseHandle(hfile);
1037 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
1038 hfile = CreateFileW(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
1039 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1041 params.dwSize = sizeof(params);
1042 params.dwCopyFlags = 0;
1043 SetLastError(0xdeadbeef);
1044 hr = pCopyFile2(source, dest, &params);
1045 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1046 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1047 CloseHandle(hfile);
1049 /* copy to a file that's opened the way Wine opens the source */
1050 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1051 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1053 params.dwSize = sizeof(params);
1054 params.dwCopyFlags = 0;
1055 hr = pCopyFile2(source, dest, &params);
1056 ok(hr == S_OK, "got 0x%08x\n", hr);
1057 CloseHandle(hfile);
1059 /* make sure that destination has correct size */
1060 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1061 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1062 ret = GetFileSize(hfile, NULL);
1063 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1065 /* make sure that destination has the same filetime */
1066 ret = GetFileTime(hfile, NULL, NULL, &ft2);
1067 ok(ret, "GetFileTime error %d\n", GetLastError());
1068 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
1070 params.dwSize = sizeof(params);
1071 params.dwCopyFlags = 0;
1072 SetLastError(0xdeadbeef);
1073 hr = pCopyFile2(source, dest, &params);
1074 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1075 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1077 /* make sure that destination still has correct size */
1078 ret = GetFileSize(hfile, NULL);
1079 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
1080 ret = ReadFile(hfile, buf, sizeof(buf), &len, NULL);
1081 ok(ret && len == sizeof(prefix), "ReadFile: error %d\n", GetLastError());
1082 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
1084 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
1085 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1086 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1088 params.dwSize = sizeof(params);
1089 params.dwCopyFlags = 0;
1090 SetLastError(0xdeadbeef);
1091 hr = pCopyFile2(source, dest, &params);
1092 ok(hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), "CopyFile2: unexpected error 0x%08x\n", hr);
1093 ok(GetLastError() == ERROR_SHARING_VIOLATION, "CopyFile2: last error %d\n", GetLastError());
1095 CloseHandle(hmapfile);
1096 CloseHandle(hfile);
1098 hfile = CreateFileW(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1099 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
1101 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
1102 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1103 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1105 params.dwSize = sizeof(params);
1106 params.dwCopyFlags = 0;
1107 hr = pCopyFile2(source, dest, &params);
1108 ok(hr == HRESULT_FROM_WIN32(ERROR_USER_MAPPED_FILE), "CopyFile2: unexpected error 0x%08x\n", hr);
1109 ok(GetLastError() == ERROR_USER_MAPPED_FILE, "CopyFile2: last error %d\n", GetLastError());
1111 CloseHandle(hmapfile);
1112 CloseHandle(hfile);
1114 DeleteFileW(source);
1115 DeleteFileW(dest);
1118 static DWORD WINAPI copy_progress_cb(LARGE_INTEGER total_size, LARGE_INTEGER total_transferred,
1119 LARGE_INTEGER stream_size, LARGE_INTEGER stream_transferred,
1120 DWORD stream, DWORD reason, HANDLE source, HANDLE dest, LPVOID userdata)
1122 ok(reason == CALLBACK_STREAM_SWITCH, "expected CALLBACK_STREAM_SWITCH, got %u\n", reason);
1123 CloseHandle(userdata);
1124 return PROGRESS_CANCEL;
1127 static void test_CopyFileEx(void)
1129 char temp_path[MAX_PATH];
1130 char source[MAX_PATH], dest[MAX_PATH];
1131 static const char prefix[] = "pfx";
1132 HANDLE hfile;
1133 DWORD ret;
1134 BOOL retok;
1136 ret = GetTempPathA(MAX_PATH, temp_path);
1137 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1138 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1140 ret = GetTempFileNameA(temp_path, prefix, 0, source);
1141 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1143 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
1144 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1146 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1147 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1148 SetLastError(0xdeadbeef);
1149 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1150 todo_wine
1151 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1152 todo_wine
1153 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1154 ok(GetFileAttributesA(dest) != INVALID_FILE_ATTRIBUTES, "file was deleted\n");
1156 hfile = CreateFileA(dest, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1157 NULL, OPEN_EXISTING, 0, 0);
1158 todo_wine
1159 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
1160 SetLastError(0xdeadbeef);
1161 retok = CopyFileExA(source, dest, copy_progress_cb, hfile, NULL, 0);
1162 todo_wine
1163 ok(!retok, "CopyFileExA unexpectedly succeeded\n");
1164 todo_wine
1165 ok(GetLastError() == ERROR_REQUEST_ABORTED, "expected ERROR_REQUEST_ABORTED, got %d\n", GetLastError());
1166 todo_wine
1167 ok(GetFileAttributesA(dest) == INVALID_FILE_ATTRIBUTES, "file was not deleted\n");
1169 ret = DeleteFileA(source);
1170 ok(ret, "DeleteFileA failed with error %d\n", GetLastError());
1171 ret = DeleteFileA(dest);
1172 ok(!ret, "DeleteFileA unexpectedly succeeded\n");
1176 * Debugging routine to dump a buffer in a hexdump-like fashion.
1178 static void dumpmem(unsigned char *mem, int len)
1180 int x = 0;
1181 char hex[49], *p;
1182 char txt[17], *c;
1184 while (x < len)
1186 p = hex;
1187 c = txt;
1188 do {
1189 p += sprintf(p, "%02x ", mem[x]);
1190 *c++ = (mem[x] >= 32 && mem[x] <= 127) ? mem[x] : '.';
1191 } while (++x % 16 && x < len);
1192 *c = '\0';
1193 trace("%04x: %-48s- %s\n", x, hex, txt);
1197 static void test_CreateFileA(void)
1199 HANDLE hFile;
1200 char temp_path[MAX_PATH], dirname[MAX_PATH];
1201 char filename[MAX_PATH];
1202 static const char prefix[] = "pfx";
1203 char windowsdir[MAX_PATH];
1204 char Volume_1[MAX_PATH];
1205 unsigned char buffer[512];
1206 char directory[] = "removeme";
1207 static const char nt_drive[] = "\\\\?\\A:";
1208 DWORD i, ret, len;
1209 struct test_list p[] = {
1210 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
1211 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
1212 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
1213 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
1214 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
1215 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
1216 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
1217 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
1218 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
1219 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
1220 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
1221 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
1222 {NULL, 0, -1, 0, FALSE}
1224 BY_HANDLE_FILE_INFORMATION Finfo;
1226 ret = GetTempPathA(MAX_PATH, temp_path);
1227 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1228 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1230 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1231 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1233 SetLastError(0xdeadbeef);
1234 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
1235 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1236 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1237 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1239 SetLastError(0xdeadbeef);
1240 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1241 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1242 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1243 "hFile %p, last error %u\n", hFile, GetLastError());
1245 CloseHandle(hFile);
1247 SetLastError(0xdeadbeef);
1248 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1249 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1250 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1251 "hFile %p, last error %u\n", hFile, GetLastError());
1253 CloseHandle(hFile);
1255 ret = DeleteFileA(filename);
1256 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1258 SetLastError(0xdeadbeef);
1259 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1260 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1261 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1262 "hFile %p, last error %u\n", hFile, GetLastError());
1264 CloseHandle(hFile);
1266 ret = DeleteFileA(filename);
1267 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1269 SetLastError(0xdeadbeef);
1270 hFile = CreateFileA("c:\\*.*", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1271 ok(hFile == INVALID_HANDLE_VALUE, "hFile should have been INVALID_HANDLE_VALUE\n");
1272 ok(GetLastError() == ERROR_INVALID_NAME ||
1273 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
1274 "LastError should have been ERROR_INVALID_NAME or ERROR_FILE_NOT_FOUND but got %u\n", GetLastError());
1276 /* get windows drive letter */
1277 ret = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1278 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1279 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1281 /* test error return codes from CreateFile for some cases */
1282 ret = GetTempPathA(MAX_PATH, temp_path);
1283 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1284 strcpy(dirname, temp_path);
1285 strcat(dirname, directory);
1286 ret = CreateDirectoryA(dirname, NULL);
1287 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
1288 /* set current drive & directory to known location */
1289 SetCurrentDirectoryA( temp_path );
1290 i = 0;
1291 while (p[i].file)
1293 filename[0] = 0;
1294 /* update the drive id in the table entry with the current one */
1295 if (p[i].file[1] == ':')
1297 strcpy(filename, p[i].file);
1298 filename[0] = windowsdir[0];
1300 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
1302 strcpy(filename, p[i].file);
1303 filename[4] = windowsdir[0];
1305 else
1307 /* prefix the table entry with the current temp directory */
1308 strcpy(filename, temp_path);
1309 strcat(filename, p[i].file);
1311 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1312 FILE_SHARE_READ | FILE_SHARE_WRITE,
1313 NULL, OPEN_EXISTING,
1314 p[i].options, NULL );
1315 /* if we get ACCESS_DENIED when we do not expect it, assume
1316 * no access to the volume
1318 if (hFile == INVALID_HANDLE_VALUE &&
1319 GetLastError() == ERROR_ACCESS_DENIED &&
1320 p[i].err != ERROR_ACCESS_DENIED)
1322 if (p[i].todo_flag)
1323 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
1324 else
1325 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
1327 /* otherwise validate results with expectations */
1328 else if (p[i].todo_flag)
1329 todo_wine ok(
1330 (hFile == INVALID_HANDLE_VALUE &&
1331 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
1332 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
1333 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
1334 filename, hFile, GetLastError(), p[i].err);
1335 else
1337 (hFile == INVALID_HANDLE_VALUE &&
1338 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
1339 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
1340 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
1341 filename, hFile, GetLastError(), p[i].err);
1342 if (hFile != INVALID_HANDLE_VALUE)
1343 CloseHandle( hFile );
1344 i++;
1346 ret = RemoveDirectoryA(dirname);
1347 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
1350 /* test opening directory as a directory */
1351 hFile = CreateFileA( temp_path, GENERIC_READ,
1352 FILE_SHARE_READ,
1353 NULL,
1354 OPEN_EXISTING,
1355 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1356 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
1358 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
1359 "CreateFileA did not work, last error %u on volume <%s>\n",
1360 GetLastError(), temp_path );
1362 if (hFile != INVALID_HANDLE_VALUE)
1364 ret = GetFileInformationByHandle( hFile, &Finfo );
1365 if (ret)
1367 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
1368 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
1369 temp_path, Finfo.dwFileAttributes);
1371 CloseHandle( hFile );
1374 else
1375 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
1378 /* *** Test opening volumes/devices using drive letter *** */
1380 /* test using drive letter in non-rewrite format without trailing \ */
1381 /* this should work */
1382 strcpy(filename, nt_drive);
1383 filename[4] = windowsdir[0];
1384 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1385 FILE_SHARE_READ | FILE_SHARE_WRITE,
1386 NULL, OPEN_EXISTING,
1387 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1388 if (hFile != INVALID_HANDLE_VALUE ||
1389 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
1391 /* if we have adm rights to volume, then try rest of tests */
1392 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1393 filename, GetLastError());
1394 if (hFile != INVALID_HANDLE_VALUE)
1396 /* if we opened the volume/device, try to read it. Since it */
1397 /* opened, we should be able to read it. We don't care about*/
1398 /* what the data is at this time. */
1399 len = 512;
1400 ret = ReadFile( hFile, buffer, len, &len, NULL );
1401 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1402 GetLastError(), ret, filename);
1403 if (ret)
1405 trace("buffer is\n");
1406 dumpmem(buffer, 64);
1408 CloseHandle( hFile );
1411 /* test using drive letter with trailing \ and in non-rewrite */
1412 /* this should not work */
1413 strcpy(filename, nt_drive);
1414 filename[4] = windowsdir[0];
1415 strcat( filename, "\\" );
1416 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1417 FILE_SHARE_READ | FILE_SHARE_WRITE,
1418 NULL, OPEN_EXISTING,
1419 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1420 todo_wine
1421 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1422 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1423 filename, GetLastError());
1424 if (hFile != INVALID_HANDLE_VALUE)
1425 CloseHandle( hFile );
1427 /* test using temp path with trailing \ and in non-rewrite as dir */
1428 /* this should work */
1429 strcpy(filename, nt_drive);
1430 filename[4] = 0;
1431 strcat( filename, temp_path );
1432 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1433 FILE_SHARE_READ | FILE_SHARE_WRITE,
1434 NULL, OPEN_EXISTING,
1435 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1436 ok(hFile != INVALID_HANDLE_VALUE,
1437 "CreateFileA should have worked on %s, but got %u\n",
1438 filename, GetLastError());
1439 if (hFile != INVALID_HANDLE_VALUE)
1440 CloseHandle( hFile );
1442 /* test using drive letter without trailing \ and in device ns */
1443 /* this should work */
1444 strcpy(filename, nt_drive);
1445 filename[4] = windowsdir[0];
1446 filename[2] = '.';
1447 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1448 FILE_SHARE_READ | FILE_SHARE_WRITE,
1449 NULL, OPEN_EXISTING,
1450 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1451 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1452 filename, GetLastError());
1453 if (hFile != INVALID_HANDLE_VALUE)
1454 CloseHandle( hFile );
1456 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
1457 else if (GetLastError() == ERROR_BAD_NETPATH)
1458 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
1459 else
1460 skip("Do not have authority to access volumes. Tests skipped\n");
1463 /* *** Test opening volumes/devices using GUID *** */
1465 if (pGetVolumeNameForVolumeMountPointA)
1467 strcpy(filename, "c:\\");
1468 filename[0] = windowsdir[0];
1469 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
1470 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
1471 if (ret)
1473 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
1475 /* test the result of opening a unique volume name (GUID)
1476 * with the trailing \
1477 * this should error out
1479 strcpy(filename, Volume_1);
1480 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1481 FILE_SHARE_READ | FILE_SHARE_WRITE,
1482 NULL, OPEN_EXISTING,
1483 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1484 todo_wine
1485 ok(hFile == INVALID_HANDLE_VALUE,
1486 "CreateFileA should not have opened %s, hFile %p\n",
1487 filename, hFile);
1488 todo_wine
1489 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1490 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1491 filename, GetLastError());
1492 if (hFile != INVALID_HANDLE_VALUE)
1493 CloseHandle( hFile );
1495 /* test the result of opening a unique volume name (GUID)
1496 * with the temp path string as dir
1497 * this should work
1499 strcpy(filename, Volume_1);
1500 strcat(filename, temp_path+3);
1501 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1502 FILE_SHARE_READ | FILE_SHARE_WRITE,
1503 NULL, OPEN_EXISTING,
1504 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1505 todo_wine
1506 ok(hFile != INVALID_HANDLE_VALUE,
1507 "CreateFileA should have opened %s, but got %u\n",
1508 filename, GetLastError());
1509 if (hFile != INVALID_HANDLE_VALUE)
1510 CloseHandle( hFile );
1512 /* test the result of opening a unique volume name (GUID)
1513 * without the trailing \ and in device namespace
1514 * this should work
1516 strcpy(filename, Volume_1);
1517 filename[2] = '.';
1518 filename[48] = 0;
1519 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1520 FILE_SHARE_READ | FILE_SHARE_WRITE,
1521 NULL, OPEN_EXISTING,
1522 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1523 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1525 /* if we have adm rights to volume, then try rest of tests */
1526 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1527 filename, GetLastError());
1528 if (hFile != INVALID_HANDLE_VALUE)
1530 /* if we opened the volume/device, try to read it. Since it */
1531 /* opened, we should be able to read it. We don't care about*/
1532 /* what the data is at this time. */
1533 len = 512;
1534 ret = ReadFile( hFile, buffer, len, &len, NULL );
1535 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1536 GetLastError(), ret, filename);
1537 if (ret)
1539 trace("buffer is\n");
1540 dumpmem(buffer, 64);
1542 CloseHandle( hFile );
1545 else
1546 skip("Do not have authority to access volumes. Tests skipped\n");
1548 else
1549 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1551 else
1552 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1555 static void test_CreateFileW(void)
1557 HANDLE hFile;
1558 WCHAR temp_path[MAX_PATH];
1559 WCHAR filename[MAX_PATH];
1560 static const WCHAR emptyW[]={'\0'};
1561 static const WCHAR prefix[] = {'p','f','x',0};
1562 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1563 DWORD ret;
1565 ret = GetTempPathW(MAX_PATH, temp_path);
1566 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1568 win_skip("GetTempPathW is not available\n");
1569 return;
1571 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1572 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1574 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1575 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1577 SetLastError(0xdeadbeef);
1578 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1579 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1580 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1581 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1583 SetLastError(0xdeadbeef);
1584 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1585 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1586 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1587 "hFile %p, last error %u\n", hFile, GetLastError());
1589 CloseHandle(hFile);
1591 SetLastError(0xdeadbeef);
1592 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1593 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1594 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1595 "hFile %p, last error %u\n", hFile, GetLastError());
1597 CloseHandle(hFile);
1599 ret = DeleteFileW(filename);
1600 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1602 SetLastError(0xdeadbeef);
1603 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1604 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1605 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1606 "hFile %p, last error %u\n", hFile, GetLastError());
1608 CloseHandle(hFile);
1610 ret = DeleteFileW(filename);
1611 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1613 if (0)
1615 /* this crashes on NT4.0 */
1616 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1617 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1618 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1619 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1622 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1623 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1624 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1625 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1627 /* test the result of opening a nonexistent driver name */
1628 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1629 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1630 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1631 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1633 ret = CreateDirectoryW(filename, NULL);
1634 ok(ret == TRUE, "couldn't create temporary directory\n");
1635 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1636 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1637 ok(hFile != INVALID_HANDLE_VALUE,
1638 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1639 CloseHandle(hFile);
1640 ret = RemoveDirectoryW(filename);
1641 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1644 static void test_CreateFile2(void)
1646 HANDLE hFile;
1647 WCHAR temp_path[MAX_PATH];
1648 WCHAR filename[MAX_PATH];
1649 CREATEFILE2_EXTENDED_PARAMETERS exparams;
1650 static const WCHAR emptyW[]={'\0'};
1651 static const WCHAR prefix[] = {'p','f','x',0};
1652 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1653 DWORD ret;
1655 if (!pCreateFile2)
1657 win_skip("CreateFile2 is missing\n");
1658 return;
1661 ret = GetTempPathW(MAX_PATH, temp_path);
1662 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1663 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1665 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1666 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1668 SetLastError(0xdeadbeef);
1669 exparams.dwSize = sizeof(exparams);
1670 exparams.dwFileAttributes = FILE_FLAG_RANDOM_ACCESS;
1671 exparams.dwFileFlags = 0;
1672 exparams.dwSecurityQosFlags = 0;
1673 exparams.lpSecurityAttributes = NULL;
1674 exparams.hTemplateFile = 0;
1675 hFile = pCreateFile2(filename, GENERIC_READ, 0, CREATE_NEW, &exparams);
1676 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1677 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1679 SetLastError(0xdeadbeef);
1680 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, CREATE_ALWAYS, &exparams);
1681 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1682 "hFile %p, last error %u\n", hFile, GetLastError());
1683 CloseHandle(hFile);
1685 SetLastError(0xdeadbeef);
1686 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1687 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1688 "hFile %p, last error %u\n", hFile, GetLastError());
1689 CloseHandle(hFile);
1691 ret = DeleteFileW(filename);
1692 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1694 SetLastError(0xdeadbeef);
1695 hFile = pCreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, &exparams);
1696 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1697 "hFile %p, last error %u\n", hFile, GetLastError());
1698 CloseHandle(hFile);
1700 ret = DeleteFileW(filename);
1701 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1703 hFile = pCreateFile2(emptyW, GENERIC_READ, 0, CREATE_NEW, &exparams);
1704 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1705 "CreateFile2(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1707 /* test the result of opening a nonexistent driver name */
1708 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
1709 hFile = pCreateFile2(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &exparams);
1710 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1711 "CreateFile2 on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1713 ret = CreateDirectoryW(filename, NULL);
1714 ok(ret == TRUE, "couldn't create temporary directory\n");
1715 exparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS;
1716 hFile = pCreateFile2(filename, GENERIC_READ | GENERIC_WRITE, 0, OPEN_ALWAYS, &exparams);
1717 todo_wine
1718 ok(hFile == INVALID_HANDLE_VALUE,
1719 "expected CreateFile2 to fail on existing directory, error: %d\n", GetLastError());
1720 CloseHandle(hFile);
1721 ret = RemoveDirectoryW(filename);
1722 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1725 static void test_GetTempFileNameA(void)
1727 UINT result;
1728 char out[MAX_PATH];
1729 char expected[MAX_PATH + 10];
1730 char windowsdir[MAX_PATH + 10];
1731 char windowsdrive[3];
1733 result = GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
1734 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1735 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1737 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1738 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1740 strcat(windowsdir, "\\");
1743 windowsdrive[0] = windowsdir[0];
1744 windowsdrive[1] = windowsdir[1];
1745 windowsdrive[2] = '\0';
1747 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1748 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1749 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1750 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1751 windowsdrive[0], out);
1753 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1754 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1755 expected[0] = '\0';
1756 strcat(expected, windowsdir);
1757 strcat(expected, "abc2.tmp");
1758 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1759 out, expected);
1762 static void test_DeleteFileA( void )
1764 BOOL ret;
1765 char temp_path[MAX_PATH], temp_file[MAX_PATH];
1766 HANDLE hfile;
1768 ret = DeleteFileA(NULL);
1769 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1770 GetLastError() == ERROR_PATH_NOT_FOUND),
1771 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1773 ret = DeleteFileA("");
1774 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1775 GetLastError() == ERROR_BAD_PATHNAME),
1776 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1778 ret = DeleteFileA("nul");
1779 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1780 GetLastError() == ERROR_INVALID_PARAMETER ||
1781 GetLastError() == ERROR_ACCESS_DENIED ||
1782 GetLastError() == ERROR_INVALID_FUNCTION),
1783 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1785 ret = DeleteFileA("nonexist.txt");
1786 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "DeleteFileA(\"nonexist.txt\") returned ret=%d error=%d\n",ret,GetLastError());
1788 GetTempPathA(MAX_PATH, temp_path);
1789 GetTempFileNameA(temp_path, "tst", 0, temp_file);
1791 SetLastError(0xdeadbeef);
1792 hfile = CreateFileA(temp_file, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1793 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1795 SetLastError(0xdeadbeef);
1796 ret = DeleteFileA(temp_file);
1797 ok(ret, "DeleteFile error %d\n", GetLastError());
1799 SetLastError(0xdeadbeef);
1800 ret = CloseHandle(hfile);
1801 ok(ret, "CloseHandle error %d\n", GetLastError());
1802 ret = DeleteFileA(temp_file);
1803 ok(!ret, "DeleteFile should fail\n");
1805 SetLastError(0xdeadbeef);
1806 ret = CreateDirectoryA("testdir", NULL);
1807 ok(ret, "CreateDirectory failed, got err %d\n", GetLastError());
1808 ret = DeleteFileA("testdir");
1809 ok(!ret && GetLastError() == ERROR_ACCESS_DENIED,
1810 "Expected ERROR_ACCESS_DENIED, got error %d\n", GetLastError());
1811 ret = RemoveDirectoryA("testdir");
1812 ok(ret, "Remove a directory failed, got error %d\n", GetLastError());
1815 static void test_DeleteFileW( void )
1817 BOOL ret;
1818 WCHAR pathW[MAX_PATH];
1819 WCHAR pathsubW[MAX_PATH];
1820 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1821 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1822 static const WCHAR emptyW[]={'\0'};
1824 ret = DeleteFileW(NULL);
1825 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1827 win_skip("DeleteFileW is not available\n");
1828 return;
1830 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1831 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1833 ret = DeleteFileW(emptyW);
1834 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1835 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1837 /* test DeleteFile on empty directory */
1838 ret = GetTempPathW(MAX_PATH, pathW);
1839 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1841 ok(0, "MAX_PATH exceeded in constructing paths\n");
1842 return;
1844 lstrcatW(pathW, dirW);
1845 lstrcpyW(pathsubW, pathW);
1846 lstrcatW(pathsubW, subdirW);
1847 ret = CreateDirectoryW(pathW, NULL);
1848 ok(ret == TRUE, "couldn't create directory deletefile\n");
1849 ret = DeleteFileW(pathW);
1850 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1851 ret = RemoveDirectoryW(pathW);
1852 ok(ret == TRUE, "expected to remove directory deletefile\n");
1854 /* test DeleteFile on non-empty directory */
1855 ret = CreateDirectoryW(pathW, NULL);
1856 ok(ret == TRUE, "couldn't create directory deletefile\n");
1857 ret = CreateDirectoryW(pathsubW, NULL);
1858 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1859 ret = DeleteFileW(pathW);
1860 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1861 ret = RemoveDirectoryW(pathsubW);
1862 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1863 ret = RemoveDirectoryW(pathW);
1864 ok(ret == TRUE, "expected to remove directory deletefile\n");
1867 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1869 static void test_MoveFileA(void)
1871 char tempdir[MAX_PATH];
1872 char source[MAX_PATH], dest[MAX_PATH];
1873 static const char prefix[] = "pfx";
1874 HANDLE hfile;
1875 HANDLE hmapfile;
1876 DWORD ret;
1877 BOOL retok;
1879 ret = GetTempPathA(MAX_PATH, tempdir);
1880 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1881 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1883 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1884 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1886 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1887 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1889 ret = MoveFileA(source, dest);
1890 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1891 "MoveFileA: unexpected error %d\n", GetLastError());
1893 ret = DeleteFileA(dest);
1894 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1896 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1897 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1899 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1900 ok( retok && ret == sizeof(prefix),
1901 "WriteFile error %d\n", GetLastError());
1903 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1904 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1906 ret = MoveFileA(source, dest);
1907 todo_wine {
1908 ok(!ret, "MoveFileA: expected failure\n");
1909 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1910 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1911 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1914 CloseHandle(hmapfile);
1915 CloseHandle(hfile);
1917 /* if MoveFile succeeded, move back to dest */
1918 if (ret) MoveFileA(dest, source);
1920 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1921 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1923 hmapfile = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1924 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1926 ret = MoveFileA(source, dest);
1927 todo_wine {
1928 ok(!ret, "MoveFileA: expected failure\n");
1929 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1930 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1931 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1934 CloseHandle(hmapfile);
1935 CloseHandle(hfile);
1937 /* if MoveFile succeeded, move back to dest */
1938 if (ret) MoveFileA(dest, source);
1940 ret = MoveFileA(source, dest);
1941 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1943 lstrcatA(tempdir, "Remove Me");
1944 ret = CreateDirectoryA(tempdir, NULL);
1945 ok(ret == TRUE, "CreateDirectoryA failed\n");
1947 lstrcpyA(source, dest);
1948 lstrcpyA(dest, tempdir);
1949 lstrcatA(dest, "\\wild?.*");
1950 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1951 ret = MoveFileA(source, dest);
1952 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1953 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1954 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1955 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1956 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1958 WIN32_FIND_DATAA fd;
1959 char temppath[MAX_PATH];
1960 HANDLE hFind;
1962 lstrcpyA(temppath, tempdir);
1963 lstrcatA(temppath, "\\*.*");
1964 hFind = FindFirstFileA(temppath, &fd);
1965 if (INVALID_HANDLE_VALUE != hFind)
1967 LPSTR lpName;
1970 lpName = fd.cAlternateFileName;
1971 if (!lpName[0])
1972 lpName = fd.cFileName;
1973 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1975 while (FindNextFileA(hFind, &fd));
1976 FindClose(hFind);
1979 ret = DeleteFileA(source);
1980 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1981 ret = DeleteFileA(dest);
1982 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1983 ret = RemoveDirectoryA(tempdir);
1984 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1987 static void test_MoveFileW(void)
1989 WCHAR temp_path[MAX_PATH];
1990 WCHAR source[MAX_PATH], dest[MAX_PATH];
1991 static const WCHAR prefix[] = {'p','f','x',0};
1992 DWORD ret;
1994 ret = GetTempPathW(MAX_PATH, temp_path);
1995 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1997 win_skip("GetTempPathW is not available\n");
1998 return;
2000 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2001 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2003 ret = GetTempFileNameW(temp_path, prefix, 0, source);
2004 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
2006 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
2007 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
2009 ret = MoveFileW(source, dest);
2010 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
2011 "CopyFileW: unexpected error %d\n", GetLastError());
2013 ret = DeleteFileW(source);
2014 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2015 ret = DeleteFileW(dest);
2016 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2019 #define PATTERN_OFFSET 0x10
2021 static void test_offset_in_overlapped_structure(void)
2023 HANDLE hFile;
2024 OVERLAPPED ov;
2025 DWORD done, offset;
2026 BOOL rc;
2027 BYTE buf[256], pattern[] = "TeSt";
2028 UINT i;
2029 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
2030 BOOL ret;
2032 ret =GetTempPathA(MAX_PATH, temp_path);
2033 ok( ret, "GetTempPathA error %d\n", GetLastError());
2034 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
2035 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
2037 /*** Write File *****************************************************/
2039 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
2040 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2042 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
2043 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
2044 ok( ret, "WriteFile error %d\n", GetLastError());
2045 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
2047 memset(&ov, 0, sizeof(ov));
2048 S(U(ov)).Offset = PATTERN_OFFSET;
2049 S(U(ov)).OffsetHigh = 0;
2050 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2051 /* Win 9x does not support the overlapped I/O on files */
2052 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2053 ok(rc, "WriteFile error %d\n", GetLastError());
2054 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2055 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2056 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2058 S(U(ov)).Offset = sizeof(buf) * 2;
2059 S(U(ov)).OffsetHigh = 0;
2060 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
2061 ok( ret, "WriteFile error %d\n", GetLastError());
2062 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
2063 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2064 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
2067 CloseHandle(hFile);
2069 /*** Read File *****************************************************/
2071 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
2072 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
2074 memset(buf, 0, sizeof(buf));
2075 memset(&ov, 0, sizeof(ov));
2076 S(U(ov)).Offset = PATTERN_OFFSET;
2077 S(U(ov)).OffsetHigh = 0;
2078 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
2079 /* Win 9x does not support the overlapped I/O on files */
2080 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
2081 ok(rc, "ReadFile error %d\n", GetLastError());
2082 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
2083 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
2084 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
2085 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
2088 CloseHandle(hFile);
2090 ret = DeleteFileA(temp_fname);
2091 ok( ret, "DeleteFileA error %d\n", GetLastError());
2094 static void test_LockFile(void)
2096 HANDLE handle, handle2;
2097 DWORD written;
2098 OVERLAPPED overlapped;
2099 int limited_LockFile;
2100 int limited_UnLockFile;
2101 BOOL ret;
2103 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2104 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2105 CREATE_ALWAYS, 0, 0 );
2106 if (handle == INVALID_HANDLE_VALUE)
2108 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2109 return;
2111 handle2 = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
2112 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
2113 OPEN_EXISTING, 0, 0 );
2114 if (handle2 == INVALID_HANDLE_VALUE)
2116 ok( 0, "couldn't open file \"%s\" (err=%d)\n", filename, GetLastError() );
2117 goto cleanup;
2119 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
2121 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
2122 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
2124 limited_UnLockFile = 0;
2125 if (UnlockFile( handle, 0, 0, 0, 0 ))
2127 limited_UnLockFile = 1;
2130 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
2131 /* overlapping locks must fail */
2132 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
2133 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
2134 /* non-overlapping locks must succeed */
2135 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
2137 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
2138 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
2139 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
2140 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
2142 S(U(overlapped)).Offset = 100;
2143 S(U(overlapped)).OffsetHigh = 0;
2144 overlapped.hEvent = 0;
2146 /* Test for broken LockFileEx a la Windows 95 OSR2. */
2147 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
2149 /* LockFileEx is probably OK, test it more. */
2150 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
2151 "LockFileEx 100,100 failed\n" );
2154 /* overlapping shared locks are OK */
2155 S(U(overlapped)).Offset = 150;
2156 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
2158 /* but exclusive is not */
2159 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
2160 0, 50, 0, &overlapped ),
2161 "LockFileEx exclusive 150,50 succeeded\n" );
2162 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
2163 { /* UnLockFile is capable. */
2164 S(U(overlapped)).Offset = 100;
2165 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
2166 "UnlockFileEx 150,100 again succeeded\n" );
2169 /* shared lock can overlap exclusive if handles are equal */
2170 S(U(overlapped)).Offset = 300;
2171 ok( LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0, &overlapped ),
2172 "LockFileEx exclusive 300,100 failed\n" );
2173 ok( !LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2174 "LockFileEx handle2 300,100 succeeded\n" );
2175 ret = LockFileEx( handle, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped );
2176 ok( ret, "LockFileEx 300,100 failed\n" );
2177 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2178 /* exclusive lock is removed first */
2179 ok( LockFileEx( handle2, LOCKFILE_FAIL_IMMEDIATELY, 0, 100, 0, &overlapped ),
2180 "LockFileEx handle2 300,100 failed\n" );
2181 ok( UnlockFileEx( handle2, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2182 if (ret)
2183 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 300,100 failed\n" );
2185 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
2186 if (ret)
2188 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
2189 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
2190 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
2192 else /* win9x */
2193 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
2195 /* wrap-around lock should not do anything */
2196 /* (but still succeeds on NT4 so we don't check result) */
2197 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
2199 limited_LockFile = 0;
2200 if (!LockFile( handle, ~0, ~0, 1, 0 ))
2202 limited_LockFile = 1;
2205 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
2207 /* zero-byte lock */
2208 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
2209 if (!limited_LockFile) ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
2210 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
2211 if (!limited_LockFile) ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
2213 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
2214 ok( !UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 succeeded\n" );
2216 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
2218 CloseHandle( handle2 );
2219 cleanup:
2220 CloseHandle( handle );
2221 DeleteFileA( filename );
2224 static BOOL create_fake_dll( LPCSTR filename )
2226 IMAGE_DOS_HEADER *dos;
2227 IMAGE_NT_HEADERS *nt;
2228 IMAGE_SECTION_HEADER *sec;
2229 BYTE *buffer;
2230 DWORD lfanew = sizeof(*dos);
2231 DWORD size = lfanew + sizeof(*nt) + sizeof(*sec);
2232 DWORD written;
2233 BOOL ret;
2235 HANDLE file = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2236 if (file == INVALID_HANDLE_VALUE) return FALSE;
2238 buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
2240 dos = (IMAGE_DOS_HEADER *)buffer;
2241 dos->e_magic = IMAGE_DOS_SIGNATURE;
2242 dos->e_cblp = sizeof(*dos);
2243 dos->e_cp = 1;
2244 dos->e_cparhdr = lfanew / 16;
2245 dos->e_minalloc = 0;
2246 dos->e_maxalloc = 0xffff;
2247 dos->e_ss = 0x0000;
2248 dos->e_sp = 0x00b8;
2249 dos->e_lfarlc = lfanew;
2250 dos->e_lfanew = lfanew;
2252 nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
2253 nt->Signature = IMAGE_NT_SIGNATURE;
2254 #if defined __i386__
2255 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
2256 #elif defined __x86_64__
2257 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
2258 #elif defined __powerpc__
2259 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
2260 #elif defined __arm__
2261 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT;
2262 #elif defined __aarch64__
2263 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
2264 #else
2265 # error You must specify the machine type
2266 #endif
2267 nt->FileHeader.NumberOfSections = 1;
2268 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2269 nt->FileHeader.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_EXECUTABLE_IMAGE;
2270 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
2271 nt->OptionalHeader.MajorLinkerVersion = 1;
2272 nt->OptionalHeader.MinorLinkerVersion = 0;
2273 nt->OptionalHeader.ImageBase = 0x10000000;
2274 nt->OptionalHeader.SectionAlignment = 0x1000;
2275 nt->OptionalHeader.FileAlignment = 0x1000;
2276 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
2277 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
2278 nt->OptionalHeader.MajorImageVersion = 1;
2279 nt->OptionalHeader.MinorImageVersion = 0;
2280 nt->OptionalHeader.MajorSubsystemVersion = 4;
2281 nt->OptionalHeader.MinorSubsystemVersion = 0;
2282 nt->OptionalHeader.SizeOfImage = 0x2000;
2283 nt->OptionalHeader.SizeOfHeaders = size;
2284 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
2285 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
2287 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
2288 memcpy( sec->Name, ".rodata", sizeof(".rodata") );
2289 sec->Misc.VirtualSize = 0x1000;
2290 sec->VirtualAddress = 0x1000;
2291 sec->SizeOfRawData = 0;
2292 sec->PointerToRawData = 0;
2293 sec->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
2295 ret = WriteFile( file, buffer, size, &written, NULL ) && written == size;
2296 HeapFree( GetProcessHeap(), 0, buffer );
2297 CloseHandle( file );
2298 return ret;
2301 static unsigned int map_file_access( unsigned int access )
2303 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2304 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2305 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2306 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2307 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2310 static BOOL is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
2312 access1 = map_file_access( access1 );
2313 access2 = map_file_access( access2 );
2314 access1 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2315 access2 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
2317 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2318 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
2320 if ((access1 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing2 & FILE_SHARE_READ)) return FALSE;
2321 if ((access1 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2322 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return FALSE;
2323 if ((access2 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing1 & FILE_SHARE_READ)) return FALSE;
2324 if ((access2 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing1 & FILE_SHARE_WRITE)) return FALSE;
2325 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return FALSE;
2326 return TRUE;
2329 static BOOL is_sharing_map_compatible( DWORD map_access, DWORD access2, DWORD sharing2 )
2331 if ((map_access == PAGE_READWRITE || map_access == PAGE_EXECUTE_READWRITE) &&
2332 !(sharing2 & FILE_SHARE_WRITE)) return FALSE;
2333 access2 = map_file_access( access2 );
2334 if ((map_access & SEC_IMAGE) && (access2 & FILE_WRITE_DATA)) return FALSE;
2335 return TRUE;
2338 static void test_file_sharing(void)
2340 static const DWORD access_modes[] =
2341 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
2342 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE,
2343 GENERIC_EXECUTE, GENERIC_EXECUTE | DELETE,
2344 FILE_READ_DATA, FILE_WRITE_DATA, FILE_APPEND_DATA, FILE_READ_EA, FILE_WRITE_EA,
2345 FILE_READ_DATA | FILE_EXECUTE, FILE_WRITE_DATA | FILE_EXECUTE, FILE_APPEND_DATA | FILE_EXECUTE,
2346 FILE_READ_EA | FILE_EXECUTE, FILE_WRITE_EA | FILE_EXECUTE, FILE_EXECUTE,
2347 FILE_DELETE_CHILD, FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES };
2348 static const DWORD sharing_modes[] =
2349 { 0, FILE_SHARE_READ,
2350 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2351 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
2352 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
2353 static const DWORD mapping_modes[] =
2354 { PAGE_READONLY, PAGE_WRITECOPY, PAGE_READWRITE, SEC_IMAGE | PAGE_WRITECOPY };
2355 int a1, s1, a2, s2;
2356 int ret;
2357 HANDLE h, h2;
2359 /* make sure the file exists */
2360 if (!create_fake_dll( filename ))
2362 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
2363 return;
2366 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
2368 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
2370 SetLastError(0xdeadbeef);
2371 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
2372 NULL, OPEN_EXISTING, 0, 0 );
2373 if (h == INVALID_HANDLE_VALUE)
2375 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2376 return;
2378 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
2380 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
2382 SetLastError(0xdeadbeef);
2383 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
2384 NULL, OPEN_EXISTING, 0, 0 );
2385 ret = GetLastError();
2386 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
2387 access_modes[a2], sharing_modes[s2] ))
2389 ok( h2 != INVALID_HANDLE_VALUE,
2390 "open failed for modes %x/%x/%x/%x\n",
2391 access_modes[a1], sharing_modes[s1],
2392 access_modes[a2], sharing_modes[s2] );
2393 ok( ret == 0, "wrong error code %d\n", ret );
2395 else
2397 ok( h2 == INVALID_HANDLE_VALUE,
2398 "open succeeded for modes %x/%x/%x/%x\n",
2399 access_modes[a1], sharing_modes[s1],
2400 access_modes[a2], sharing_modes[s2] );
2401 ok( ret == ERROR_SHARING_VIOLATION,
2402 "wrong error code %d\n", ret );
2404 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2407 CloseHandle( h );
2411 for (a1 = 0; a1 < sizeof(mapping_modes)/sizeof(mapping_modes[0]); a1++)
2413 HANDLE m;
2415 create_fake_dll( filename );
2416 SetLastError(0xdeadbeef);
2417 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2418 if (h == INVALID_HANDLE_VALUE)
2420 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
2421 return;
2423 m = CreateFileMappingA( h, NULL, mapping_modes[a1], 0, 0, NULL );
2424 ok( m != 0, "failed to create mapping %x err %u\n", mapping_modes[a1], GetLastError() );
2425 CloseHandle( h );
2426 if (!m) continue;
2428 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
2430 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
2432 SetLastError(0xdeadbeef);
2433 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
2434 NULL, OPEN_EXISTING, 0, 0 );
2436 ret = GetLastError();
2437 if (h2 == INVALID_HANDLE_VALUE)
2439 ok( !is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]),
2440 "open failed for modes map %x/%x/%x\n",
2441 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
2442 ok( ret == ERROR_SHARING_VIOLATION,
2443 "wrong error code %d\n", ret );
2445 else
2447 if (!is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]))
2448 ok( broken(1), /* no checking on nt4 */
2449 "open succeeded for modes map %x/%x/%x\n",
2450 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
2451 ok( ret == 0xdeadbeef /* Win9x */ ||
2452 ret == 0, /* XP */
2453 "wrong error code %d\n", ret );
2454 CloseHandle( h2 );
2459 /* try CREATE_ALWAYS over an existing mapping */
2460 SetLastError(0xdeadbeef);
2461 h2 = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
2462 NULL, CREATE_ALWAYS, 0, 0 );
2463 ret = GetLastError();
2464 if (mapping_modes[a1] & SEC_IMAGE)
2466 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
2467 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
2469 else
2471 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
2472 ok( ret == ERROR_USER_MAPPED_FILE, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
2474 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2476 /* try DELETE_ON_CLOSE over an existing mapping */
2477 SetLastError(0xdeadbeef);
2478 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2479 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 );
2480 ret = GetLastError();
2481 if (mapping_modes[a1] & SEC_IMAGE)
2483 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
2484 ok( ret == ERROR_ACCESS_DENIED, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
2486 else
2488 ok( h2 != INVALID_HANDLE_VALUE, "open failed for map %x err %u\n", mapping_modes[a1], ret );
2490 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
2492 CloseHandle( m );
2495 SetLastError(0xdeadbeef);
2496 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
2497 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2499 SetLastError(0xdeadbeef);
2500 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
2501 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
2502 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
2504 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
2505 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
2507 CloseHandle(h);
2508 CloseHandle(h2);
2510 DeleteFileA( filename );
2513 static char get_windows_drive(void)
2515 char windowsdir[MAX_PATH];
2516 GetWindowsDirectoryA(windowsdir, sizeof(windowsdir));
2517 return windowsdir[0];
2520 static void test_FindFirstFileA(void)
2522 HANDLE handle;
2523 WIN32_FIND_DATAA data;
2524 int err;
2525 char buffer[5] = "C:\\";
2526 char buffer2[100];
2527 char nonexistent[MAX_PATH];
2529 /* try FindFirstFileA on "C:\" */
2530 buffer[0] = get_windows_drive();
2532 SetLastError( 0xdeadbeaf );
2533 handle = FindFirstFileA(buffer, &data);
2534 err = GetLastError();
2535 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
2536 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2538 /* try FindFirstFileA on "C:\*" */
2539 strcpy(buffer2, buffer);
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, "." ) && strcmp( data.cFileName, ".." ),
2544 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
2545 if (FindNextFileA( handle, &data ))
2546 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2547 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
2548 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2550 /* try FindFirstFileA on windows dir */
2551 GetWindowsDirectoryA( buffer2, sizeof(buffer2) );
2552 strcat(buffer2, "\\*");
2553 handle = FindFirstFileA(buffer2, &data);
2554 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2555 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
2556 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
2557 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
2558 while (FindNextFileA( handle, &data ))
2559 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2560 "FindNextFile shouldn't return '%s'\n", data.cFileName );
2561 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2563 /* try FindFirstFileA on "C:\foo\" */
2564 SetLastError( 0xdeadbeaf );
2565 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
2567 char tmp[MAX_PATH];
2568 GetTempPathA( sizeof(tmp), tmp );
2569 GetTempFileNameA( tmp, "foo", 0, nonexistent );
2571 DeleteFileA( nonexistent );
2572 strcpy(buffer2, nonexistent);
2573 strcat(buffer2, "\\");
2574 handle = FindFirstFileA(buffer2, &data);
2575 err = GetLastError();
2576 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2577 todo_wine {
2578 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2581 /* try FindFirstFileA without trailing backslash */
2582 SetLastError( 0xdeadbeaf );
2583 strcpy(buffer2, nonexistent);
2584 handle = FindFirstFileA(buffer2, &data);
2585 err = GetLastError();
2586 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2587 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2589 /* try FindFirstFileA on "C:\foo\bar.txt" */
2590 SetLastError( 0xdeadbeaf );
2591 strcpy(buffer2, nonexistent);
2592 strcat(buffer2, "\\bar.txt");
2593 handle = FindFirstFileA(buffer2, &data);
2594 err = GetLastError();
2595 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2596 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2598 /* try FindFirstFileA on "C:\foo\*.*" */
2599 SetLastError( 0xdeadbeaf );
2600 strcpy(buffer2, nonexistent);
2601 strcat(buffer2, "\\*.*");
2602 handle = FindFirstFileA(buffer2, &data);
2603 err = GetLastError();
2604 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2605 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2607 /* try FindFirstFileA on "foo\bar.txt" */
2608 SetLastError( 0xdeadbeaf );
2609 strcpy(buffer2, nonexistent + 3);
2610 strcat(buffer2, "\\bar.txt");
2611 handle = FindFirstFileA(buffer2, &data);
2612 err = GetLastError();
2613 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2614 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2616 /* try FindFirstFileA on "c:\nul" */
2617 SetLastError( 0xdeadbeaf );
2618 strcpy(buffer2, buffer);
2619 strcat(buffer2, "nul");
2620 handle = FindFirstFileA(buffer2, &data);
2621 err = GetLastError();
2622 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2623 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
2624 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2625 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2626 "wrong attributes %x\n", data.dwFileAttributes );
2627 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2629 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2630 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2632 SetLastError( 0xdeadbeaf );
2633 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2634 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2635 ok( FindClose( handle ), "failed to close handle\n" );
2637 /* try FindFirstFileA on "lpt1" */
2638 SetLastError( 0xdeadbeaf );
2639 strcpy(buffer2, "lpt1");
2640 handle = FindFirstFileA(buffer2, &data);
2641 err = GetLastError();
2642 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2643 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
2644 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2645 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2646 "wrong attributes %x\n", data.dwFileAttributes );
2647 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2649 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2650 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2652 SetLastError( 0xdeadbeaf );
2653 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2654 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2655 ok( FindClose( handle ), "failed to close handle\n" );
2657 /* try FindFirstFileA on "c:\nul\*" */
2658 SetLastError( 0xdeadbeaf );
2659 strcpy(buffer2, buffer);
2660 strcat(buffer2, "nul\\*");
2661 handle = FindFirstFileA(buffer2, &data);
2662 err = GetLastError();
2663 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2664 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2666 /* try FindFirstFileA on "c:\nul*" */
2667 SetLastError( 0xdeadbeaf );
2668 strcpy(buffer2, buffer);
2669 strcat(buffer2, "nul*");
2670 handle = FindFirstFileA(buffer2, &data);
2671 err = GetLastError();
2672 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2673 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2675 /* try FindFirstFileA on "c:\foo\bar\nul" */
2676 SetLastError( 0xdeadbeaf );
2677 strcpy(buffer2, buffer);
2678 strcat(buffer2, "foo\\bar\\nul");
2679 handle = FindFirstFileA(buffer2, &data);
2680 err = GetLastError();
2681 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2682 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2684 /* try FindFirstFileA on "c:\foo\nul\bar" */
2685 SetLastError( 0xdeadbeaf );
2686 strcpy(buffer2, buffer);
2687 strcat(buffer2, "foo\\nul\\bar");
2688 handle = FindFirstFileA(buffer2, &data);
2689 err = GetLastError();
2690 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should fail\n", buffer2 );
2691 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2694 static void test_FindNextFileA(void)
2696 HANDLE handle;
2697 WIN32_FIND_DATAA search_results;
2698 int err;
2699 char buffer[5] = "C:\\*";
2701 buffer[0] = get_windows_drive();
2702 handle = FindFirstFileA(buffer,&search_results);
2703 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
2704 while (FindNextFileA(handle, &search_results))
2706 /* get to the end of the files */
2708 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
2709 err = GetLastError();
2710 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
2713 static void test_FindFirstFileExA(FINDEX_INFO_LEVELS level, FINDEX_SEARCH_OPS search_ops, DWORD flags)
2715 WIN32_FIND_DATAA search_results;
2716 HANDLE handle;
2717 BOOL ret;
2719 if (!pFindFirstFileExA)
2721 win_skip("FindFirstFileExA() is missing\n");
2722 return;
2725 trace("Running FindFirstFileExA tests with level=%d, search_ops=%d, flags=%u\n",
2726 level, search_ops, flags);
2728 CreateDirectoryA("test-dir", NULL);
2729 _lclose(_lcreat("test-dir\\file1", 0));
2730 _lclose(_lcreat("test-dir\\file2", 0));
2731 CreateDirectoryA("test-dir\\dir1", NULL);
2732 SetLastError(0xdeadbeef);
2733 handle = pFindFirstFileExA("test-dir\\*", level, &search_results, search_ops, NULL, flags);
2734 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2736 win_skip("FindFirstFileExA is not implemented\n");
2737 goto cleanup;
2739 if ((flags & FIND_FIRST_EX_LARGE_FETCH) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2741 win_skip("FindFirstFileExA flag FIND_FIRST_EX_LARGE_FETCH not supported, skipping test\n");
2742 goto cleanup;
2744 if ((level == FindExInfoBasic) && handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
2746 win_skip("FindFirstFileExA level FindExInfoBasic not supported, skipping test\n");
2747 goto cleanup;
2750 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2751 #define CHECK_LEVEL(fn) (level != FindExInfoBasic || !(fn)[0])
2753 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
2754 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
2755 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2757 ok(FindNextFileA(handle, &search_results), "Fetching second file failed\n");
2758 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
2759 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2761 ok(FindNextFileA(handle, &search_results), "Fetching third file failed\n");
2762 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
2763 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2765 SetLastError(0xdeadbeef);
2766 ret = FindNextFileA(handle, &search_results);
2767 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
2769 skip("File system supports directory filtering\n");
2770 /* Results from the previous call are not cleared */
2771 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
2772 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2775 else
2777 ok(ret, "Fetching fourth file failed\n");
2778 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
2779 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2781 ok(FindNextFileA(handle, &search_results), "Fetching fifth file failed\n");
2782 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
2783 ok(CHECK_LEVEL(search_results.cAlternateFileName), "FindFirstFile unexpectedly returned an alternate filename\n");
2785 ok(FindNextFileA(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
2788 #undef CHECK_NAME
2789 #undef CHECK_LEVEL
2791 FindClose( handle );
2793 /* Most Windows systems seem to ignore the FIND_FIRST_EX_CASE_SENSITIVE flag. Unofficial documentation
2794 * suggests that there are registry keys and that it might depend on the used filesystem. */
2795 SetLastError(0xdeadbeef);
2796 handle = pFindFirstFileExA("TEST-DIR\\*", level, &search_results, search_ops, NULL, flags);
2797 if (flags & FIND_FIRST_EX_CASE_SENSITIVE)
2799 ok(handle != INVALID_HANDLE_VALUE || GetLastError() == ERROR_PATH_NOT_FOUND,
2800 "Unexpected error %x, expected valid handle or ERROR_PATH_NOT_FOUND\n", GetLastError());
2801 trace("FindFirstFileExA flag FIND_FIRST_EX_CASE_SENSITIVE is %signored\n",
2802 (handle == INVALID_HANDLE_VALUE) ? "not " : "");
2804 else
2805 ok(handle != INVALID_HANDLE_VALUE, "Unexpected error %x, expected valid handle\n", GetLastError());
2806 if (handle != INVALID_HANDLE_VALUE)
2807 FindClose( handle );
2809 cleanup:
2810 DeleteFileA("test-dir\\file1");
2811 DeleteFileA("test-dir\\file2");
2812 RemoveDirectoryA("test-dir\\dir1");
2813 RemoveDirectoryA("test-dir");
2816 static int test_Mapfile_createtemp(HANDLE *handle)
2818 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
2819 DeleteFileA(filename);
2820 *handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
2821 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2822 if (*handle != INVALID_HANDLE_VALUE) {
2824 return 1;
2827 return 0;
2830 static void test_MapFile(void)
2832 HANDLE handle;
2833 HANDLE hmap;
2835 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2837 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
2838 ok( hmap != NULL, "mapping should work, I named it!\n" );
2840 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2842 /* We have to close file before we try new stuff with mapping again.
2843 Else we would always succeed on XP or block descriptors on 95. */
2844 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2845 ok( hmap != NULL, "We should still be able to map!\n" );
2846 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2847 ok( CloseHandle( handle ), "can't close file handle\n");
2848 handle = NULL;
2850 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2852 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2853 ok( hmap == NULL, "mapped zero size file\n");
2854 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
2856 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
2857 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2858 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2859 if ( hmap )
2860 CloseHandle( hmap );
2862 hmap = CreateFileMappingA( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
2863 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2864 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2865 if ( hmap )
2866 CloseHandle( hmap );
2868 /* On XP you can now map again, on Win 95 you cannot. */
2870 ok( CloseHandle( handle ), "can't close file handle\n");
2871 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
2874 static void test_GetFileType(void)
2876 DWORD type, type2;
2877 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2878 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
2879 type = GetFileType(h);
2880 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
2881 CloseHandle( h );
2882 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2883 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
2884 type = GetFileType(h);
2885 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
2886 CloseHandle( h );
2887 DeleteFileA( filename );
2888 h = GetStdHandle( STD_OUTPUT_HANDLE );
2889 ok( h != INVALID_HANDLE_VALUE, "GetStdHandle failed\n" );
2890 type = GetFileType( (HANDLE)STD_OUTPUT_HANDLE );
2891 type2 = GetFileType( h );
2892 ok(type == type2, "expected type %d for STD_OUTPUT_HANDLE got %d\n", type2, type);
2895 static int completion_count;
2897 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2899 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2900 ReleaseSemaphore(ovl->hEvent, 1, NULL);
2901 completion_count++;
2904 static void test_async_file_errors(void)
2906 char szFile[MAX_PATH];
2907 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
2908 HANDLE hFile;
2909 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
2910 OVERLAPPED ovl;
2911 S(U(ovl)).Offset = 0;
2912 S(U(ovl)).OffsetHigh = 0;
2913 ovl.hEvent = hSem;
2914 completion_count = 0;
2915 szFile[0] = '\0';
2916 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
2917 strcat(szFile, "\\win.ini");
2918 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2919 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2920 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
2921 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2922 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2923 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2924 while (TRUE)
2926 BOOL res;
2927 DWORD count;
2928 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2930 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2931 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2932 if (!res)
2933 break;
2934 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2935 break;
2936 S(U(ovl)).Offset += count;
2937 /* i/o completion routine only called if ReadFileEx returned success.
2938 * we only care about violations of this rule so undo what should have
2939 * been done */
2940 completion_count--;
2942 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2943 /*printf("Error = %ld\n", GetLastError());*/
2944 HeapFree(GetProcessHeap(), 0, lpBuffer);
2947 static BOOL user_apc_ran;
2948 static void CALLBACK user_apc(ULONG_PTR param)
2950 user_apc_ran = TRUE;
2953 static void test_read_write(void)
2955 DWORD bytes, ret, old_prot;
2956 HANDLE hFile;
2957 char temp_path[MAX_PATH];
2958 char filename[MAX_PATH];
2959 char *mem;
2960 static const char prefix[] = "pfx";
2962 ret = GetTempPathA(MAX_PATH, temp_path);
2963 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2964 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2966 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2967 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2969 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2970 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2971 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2973 user_apc_ran = FALSE;
2974 if (pQueueUserAPC) {
2975 trace("Queueing an user APC\n"); /* verify the file is non alerable */
2976 ret = pQueueUserAPC(&user_apc, GetCurrentThread(), 0);
2977 ok(ret, "QueueUserAPC failed: %d\n", GetLastError());
2980 SetLastError(12345678);
2981 bytes = 12345678;
2982 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2983 ok(ret && GetLastError() == 12345678,
2984 "ret = %d, error %d\n", ret, GetLastError());
2985 ok(!bytes, "bytes = %d\n", bytes);
2987 SetLastError(12345678);
2988 bytes = 12345678;
2989 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2990 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2991 (ret && GetLastError() == 12345678), /* Win9x */
2992 "ret = %d, error %d\n", ret, GetLastError());
2993 ok(!bytes || /* Win2k */
2994 bytes == 10, /* Win9x */
2995 "bytes = %d\n", bytes);
2997 /* make sure the file contains data */
2998 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2999 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
3001 SetLastError(12345678);
3002 bytes = 12345678;
3003 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
3004 ok(ret && GetLastError() == 12345678,
3005 "ret = %d, error %d\n", ret, GetLastError());
3006 ok(!bytes, "bytes = %d\n", bytes);
3008 SetLastError(12345678);
3009 bytes = 12345678;
3010 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
3011 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
3012 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
3013 "ret = %d, error %d\n", ret, GetLastError());
3014 ok(!bytes, "bytes = %d\n", bytes);
3016 ok(user_apc_ran == FALSE, "UserAPC ran, file using alertable io mode\n");
3017 if (pQueueUserAPC)
3018 SleepEx(0, TRUE); /* get rid of apc */
3020 /* test passing protected memory as buffer */
3022 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
3023 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
3025 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3026 ok( ret, "WriteFile failed error %u\n", GetLastError() );
3027 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
3029 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
3030 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3032 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3033 ok( !ret, "WriteFile succeeded\n" );
3034 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3035 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3036 "wrong error %u\n", GetLastError() );
3037 ok( bytes == 0, "wrote %x bytes\n", bytes );
3039 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
3040 ok( !ret, "WriteFile succeeded\n" );
3041 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
3042 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3043 "wrong error %u\n", GetLastError() );
3044 ok( bytes == 0, "wrote %x bytes\n", bytes );
3046 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
3047 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3049 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
3050 ok( !ret, "WriteFile succeeded\n" );
3051 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
3052 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3053 "wrong error %u\n", GetLastError() );
3054 ok( bytes == 0, "wrote %x bytes\n", bytes );
3056 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3058 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3059 ok( !ret, "ReadFile succeeded\n" );
3060 ok( GetLastError() == ERROR_NOACCESS ||
3061 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3062 "wrong error %u\n", GetLastError() );
3063 ok( bytes == 0, "read %x bytes\n", bytes );
3065 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
3066 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3068 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3069 ok( !ret, "ReadFile succeeded\n" );
3070 ok( GetLastError() == ERROR_NOACCESS ||
3071 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3072 "wrong error %u\n", GetLastError() );
3073 ok( bytes == 0, "read %x bytes\n", bytes );
3075 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
3076 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
3078 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3079 ok( !ret, "ReadFile succeeded\n" );
3080 ok( GetLastError() == ERROR_NOACCESS ||
3081 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3082 "wrong error %u\n", GetLastError() );
3083 ok( bytes == 0, "read %x bytes\n", bytes );
3085 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
3086 SetEndOfFile( hFile );
3087 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
3089 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
3090 ok( !ret, "ReadFile succeeded\n" );
3091 ok( GetLastError() == ERROR_NOACCESS ||
3092 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3093 "wrong error %u\n", GetLastError() );
3094 ok( bytes == 0, "read %x bytes\n", bytes );
3096 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
3097 ok( ret, "ReadFile failed error %u\n", GetLastError() );
3098 ok( bytes == 0x1234, "read %x bytes\n", bytes );
3100 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
3101 ok( !ret, "ReadFile succeeded\n" );
3102 ok( GetLastError() == ERROR_NOACCESS ||
3103 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
3104 "wrong error %u\n", GetLastError() );
3105 ok( bytes == 0, "read %x bytes\n", bytes );
3107 VirtualFree( mem, 0, MEM_RELEASE );
3109 ret = CloseHandle(hFile);
3110 ok( ret, "CloseHandle: error %d\n", GetLastError());
3111 ret = DeleteFileA(filename);
3112 ok( ret, "DeleteFileA: error %d\n", GetLastError());
3115 static void test_OpenFile(void)
3117 HFILE hFile;
3118 OFSTRUCT ofs;
3119 BOOL ret;
3120 DWORD retval;
3122 static const char file[] = "regedit.exe";
3123 static const char foo[] = ".\\foo-bar-foo.baz";
3124 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
3125 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3126 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3127 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3128 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
3129 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
3130 char buff[MAX_PATH];
3131 char buff_long[4*MAX_PATH];
3132 char filled_0xA5[OFS_MAXPATHNAME];
3133 char *p;
3134 UINT length;
3136 /* Check for existing file */
3137 if (!pGetSystemWindowsDirectoryA)
3138 length = GetWindowsDirectoryA(buff, MAX_PATH);
3139 else
3140 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
3142 if (length + sizeof(file) < MAX_PATH)
3144 p = buff + strlen(buff);
3145 if (p > buff && p[-1] != '\\') *p++ = '\\';
3146 strcpy( p, file );
3147 memset(&ofs, 0xA5, sizeof(ofs));
3148 SetLastError(0xfaceabee);
3150 hFile = OpenFile(buff, &ofs, OF_EXIST);
3151 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
3152 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3153 "GetLastError() returns %d\n", GetLastError() );
3154 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3155 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3156 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3157 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3158 ofs.szPathName, buff );
3161 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
3162 length = GetCurrentDirectoryA(MAX_PATH, buff);
3164 /* Check for nonexistent file */
3165 if (length + sizeof(foo) < MAX_PATH)
3167 p = buff + strlen(buff);
3168 if (p > buff && p[-1] != '\\') *p++ = '\\';
3169 strcpy( p, foo + 2 );
3170 memset(&ofs, 0xA5, sizeof(ofs));
3171 SetLastError(0xfaceabee);
3173 hFile = OpenFile(foo, &ofs, OF_EXIST);
3174 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3175 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
3176 todo_wine
3177 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3178 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3179 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3180 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
3181 ofs.szPathName, buff );
3184 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
3185 length += lstrlenA(foo_too_long + 1);
3187 /* Check for nonexistent file with too long filename */
3188 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
3190 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
3191 memset(&ofs, 0xA5, sizeof(ofs));
3192 SetLastError(0xfaceabee);
3194 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
3195 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
3196 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
3197 "GetLastError() returns %d\n", GetLastError() );
3198 todo_wine
3199 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3200 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
3201 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3202 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
3203 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
3204 ofs.szPathName );
3207 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
3209 if (length >= MAX_PATH)
3211 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
3212 return;
3214 p = buff + strlen(buff);
3215 if (p > buff && p[-1] != '\\') *p++ = '\\';
3216 strcpy( p, filename );
3218 memset(&ofs, 0xA5, sizeof(ofs));
3219 SetLastError(0xfaceabee);
3220 /* Create an empty file */
3221 hFile = OpenFile(filename, &ofs, OF_CREATE);
3222 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
3223 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3224 "GetLastError() returns %d\n", GetLastError() );
3225 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3226 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3227 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3228 ret = _lclose(hFile);
3229 ok( !ret, "_lclose() returns %d\n", ret );
3230 retval = GetFileAttributesA(filename);
3231 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
3233 memset(&ofs, 0xA5, sizeof(ofs));
3234 SetLastError(0xfaceabee);
3235 /* Check various opening options: */
3236 /* for reading only, */
3237 hFile = OpenFile(filename, &ofs, OF_READ);
3238 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
3239 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3240 "GetLastError() returns %d\n", GetLastError() );
3241 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3242 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3243 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3244 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3245 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3246 ret = _lclose(hFile);
3247 ok( !ret, "_lclose() returns %d\n", ret );
3249 memset(&ofs, 0xA5, sizeof(ofs));
3250 SetLastError(0xfaceabee);
3251 /* for writing only, */
3252 hFile = OpenFile(filename, &ofs, OF_WRITE);
3253 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
3254 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3255 "GetLastError() returns %d\n", GetLastError() );
3256 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3257 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3258 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3259 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3260 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3261 ret = _lclose(hFile);
3262 ok( !ret, "_lclose() returns %d\n", ret );
3264 memset(&ofs, 0xA5, sizeof(ofs));
3265 SetLastError(0xfaceabee);
3266 /* for reading and writing, */
3267 hFile = OpenFile(filename, &ofs, OF_READWRITE);
3268 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
3269 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3270 "GetLastError() returns %d\n", GetLastError() );
3271 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3272 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3273 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3274 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3275 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3276 ret = _lclose(hFile);
3277 ok( !ret, "_lclose() returns %d\n", ret );
3279 memset(&ofs, 0xA5, sizeof(ofs));
3280 SetLastError(0xfaceabee);
3281 /* for checking file presence. */
3282 hFile = OpenFile(filename, &ofs, OF_EXIST);
3283 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
3284 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3285 "GetLastError() returns %d\n", GetLastError() );
3286 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3287 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3288 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3289 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3290 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3292 memset(&ofs, 0xA5, sizeof(ofs));
3293 SetLastError(0xfaceabee);
3294 /* Delete the file and make sure it doesn't exist anymore */
3295 hFile = OpenFile(filename, &ofs, OF_DELETE);
3296 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
3297 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
3298 "GetLastError() returns %d\n", GetLastError() );
3299 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
3300 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
3301 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
3302 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
3303 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
3305 retval = GetFileAttributesA(filename);
3306 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
3309 static void test_overlapped(void)
3311 OVERLAPPED ov;
3312 DWORD r, result;
3314 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
3315 if (0) /* tested: WinXP */
3317 GetOverlappedResult(0, NULL, &result, FALSE);
3318 GetOverlappedResult(0, &ov, NULL, FALSE);
3319 GetOverlappedResult(0, NULL, NULL, FALSE);
3322 memset( &ov, 0, sizeof ov );
3323 result = 1;
3324 r = GetOverlappedResult(0, &ov, &result, 0);
3325 if (r)
3326 ok( result == 0, "wrong result %u\n", result );
3327 else /* win9x */
3328 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3330 result = 0;
3331 ov.Internal = 0;
3332 ov.InternalHigh = 0xabcd;
3333 r = GetOverlappedResult(0, &ov, &result, 0);
3334 if (r)
3335 ok( result == 0xabcd, "wrong result %u\n", result );
3336 else /* win9x */
3337 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3339 SetLastError( 0xb00 );
3340 result = 0;
3341 ov.Internal = STATUS_INVALID_HANDLE;
3342 ov.InternalHigh = 0xabcd;
3343 r = GetOverlappedResult(0, &ov, &result, 0);
3344 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
3345 ok( r == FALSE, "should return false\n");
3346 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
3348 SetLastError( 0xb00 );
3349 result = 0;
3350 ov.Internal = STATUS_PENDING;
3351 ov.InternalHigh = 0xabcd;
3352 r = GetOverlappedResult(0, &ov, &result, 0);
3353 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3354 "wrong error %u\n", GetLastError() );
3355 ok( r == FALSE, "should return false\n");
3356 ok( result == 0, "wrong result %u\n", result );
3358 SetLastError( 0xb00 );
3359 ov.hEvent = CreateEventW( NULL, 1, 1, NULL );
3360 ov.Internal = STATUS_PENDING;
3361 ov.InternalHigh = 0xabcd;
3362 r = GetOverlappedResult(0, &ov, &result, 0);
3363 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3364 "wrong error %u\n", GetLastError() );
3365 ok( r == FALSE, "should return false\n");
3367 ResetEvent( ov.hEvent );
3369 SetLastError( 0xb00 );
3370 ov.Internal = STATUS_PENDING;
3371 ov.InternalHigh = 0;
3372 r = GetOverlappedResult(0, &ov, &result, 0);
3373 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
3374 "wrong error %u\n", GetLastError() );
3375 ok( r == FALSE, "should return false\n");
3377 r = CloseHandle( ov.hEvent );
3378 ok( r == TRUE, "close handle failed\n");
3381 static void test_RemoveDirectory(void)
3383 int rc;
3384 char directory[] = "removeme";
3386 rc = CreateDirectoryA(directory, NULL);
3387 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
3389 rc = SetCurrentDirectoryA(directory);
3390 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3392 rc = RemoveDirectoryA(".");
3393 if (!rc)
3395 rc = SetCurrentDirectoryA("..");
3396 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
3398 rc = RemoveDirectoryA(directory);
3399 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
3403 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
3405 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
3406 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
3407 return abs(t1 - t2) <= tolerance;
3410 static void test_ReplaceFileA(void)
3412 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3413 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
3414 static const char replacedData[] = "file-to-replace";
3415 static const char replacementData[] = "new-file";
3416 static const char backupData[] = "backup-file";
3417 FILETIME ftReplaced, ftReplacement, ftBackup;
3418 static const char prefix[] = "pfx";
3419 char temp_path[MAX_PATH];
3420 DWORD ret;
3421 BOOL retok, removeBackup = FALSE;
3423 if (!pReplaceFileA)
3425 win_skip("ReplaceFileA() is missing\n");
3426 return;
3429 ret = GetTempPathA(MAX_PATH, temp_path);
3430 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
3431 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3433 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
3434 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
3436 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3437 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3439 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
3440 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
3442 /* place predictable data in the file to be replaced */
3443 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3444 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3445 "failed to open replaced file\n");
3446 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
3447 ok( retok && ret == sizeof(replacedData),
3448 "WriteFile error (replaced) %d\n", GetLastError());
3449 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
3450 "replaced file has wrong size\n");
3451 /* place predictable data in the file to be the replacement */
3452 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3453 ok(hReplacementFile != INVALID_HANDLE_VALUE,
3454 "failed to open replacement file\n");
3455 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
3456 ok( retok && ret == sizeof(replacementData),
3457 "WriteFile error (replacement) %d\n", GetLastError());
3458 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
3459 "replacement file has wrong size\n");
3460 /* place predictable data in the backup file (to be over-written) */
3461 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
3462 ok(hBackupFile != INVALID_HANDLE_VALUE,
3463 "failed to open backup file\n");
3464 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
3465 ok( retok && ret == sizeof(backupData),
3466 "WriteFile error (replacement) %d\n", GetLastError());
3467 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
3468 "backup file has wrong size\n");
3469 /* change the filetime on the "replaced" file to ensure that it changes */
3470 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3471 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
3472 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
3473 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3474 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
3475 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
3476 CloseHandle(hReplacedFile);
3477 /* change the filetime on the backup to ensure that it changes */
3478 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3479 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
3480 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
3481 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3482 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
3483 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
3484 CloseHandle(hBackupFile);
3485 /* get the filetime on the replacement file to perform checks */
3486 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
3487 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
3488 CloseHandle(hReplacementFile);
3490 /* perform replacement w/ backup
3491 * TODO: flags are not implemented
3493 SetLastError(0xdeadbeef);
3494 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3495 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
3496 /* make sure that the backup has the size of the old "replaced" file */
3497 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3498 ok(hBackupFile != INVALID_HANDLE_VALUE,
3499 "failed to open backup file\n");
3500 ret = GetFileSize(hBackupFile, NULL);
3501 ok(ret == sizeof(replacedData),
3502 "backup file has wrong size %d\n", ret);
3503 /* make sure that the "replaced" file has the size of the replacement file */
3504 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3505 ok(hReplacedFile != INVALID_HANDLE_VALUE,
3506 "failed to open replaced file: %d\n", GetLastError());
3507 if (hReplacedFile != INVALID_HANDLE_VALUE)
3509 ret = GetFileSize(hReplacedFile, NULL);
3510 ok(ret == sizeof(replacementData),
3511 "replaced file has wrong size %d\n", ret);
3512 /* make sure that the replacement file no-longer exists */
3513 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3514 ok(hReplacementFile == INVALID_HANDLE_VALUE,
3515 "unexpected error, replacement file should not exist %d\n", GetLastError());
3516 /* make sure that the backup has the old "replaced" filetime */
3517 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
3518 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3519 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
3520 CloseHandle(hBackupFile);
3521 /* make sure that the "replaced" has the old replacement filetime */
3522 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
3523 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
3524 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
3525 "replaced file has wrong filetime %x%08x / %x%08x\n",
3526 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
3527 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
3528 CloseHandle(hReplacedFile);
3530 else
3531 skip("couldn't open replacement file, skipping tests\n");
3533 /* re-create replacement file for pass w/o backup (blank) */
3534 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3535 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3536 /* perform replacement w/o backup
3537 * TODO: flags are not implemented
3539 SetLastError(0xdeadbeef);
3540 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3541 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3542 "ReplaceFileA: unexpected error %d\n", GetLastError());
3544 /* re-create replacement file for pass w/ backup (backup-file not existing) */
3545 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3546 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3547 ret = DeleteFileA(backup);
3548 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
3549 /* perform replacement w/ backup (no pre-existing backup)
3550 * TODO: flags are not implemented
3552 SetLastError(0xdeadbeef);
3553 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3554 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3555 "ReplaceFileA: unexpected error %d\n", GetLastError());
3556 if (ret)
3557 removeBackup = TRUE;
3559 /* re-create replacement file for pass w/ no permissions to "replaced" */
3560 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
3561 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
3562 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
3563 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3564 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
3565 /* perform replacement w/ backup (no permission to "replaced")
3566 * TODO: flags are not implemented
3568 SetLastError(0xdeadbeef);
3569 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3570 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
3571 /* make sure that the replacement file still exists */
3572 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
3573 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
3574 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
3575 "unexpected error, replacement file should still exist %d\n", GetLastError());
3576 CloseHandle(hReplacementFile);
3577 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
3578 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3579 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
3581 /* replacement file still exists, make pass w/o "replaced" */
3582 ret = DeleteFileA(replaced);
3583 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3584 "DeleteFileA: error (replaced) %d\n", GetLastError());
3585 /* perform replacement w/ backup (no pre-existing backup or "replaced")
3586 * TODO: flags are not implemented
3588 SetLastError(0xdeadbeef);
3589 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3590 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3591 GetLastError() == ERROR_ACCESS_DENIED),
3592 "ReplaceFileA: unexpected error %d\n", GetLastError());
3594 /* perform replacement w/o existing "replacement" file
3595 * TODO: flags are not implemented
3597 SetLastError(0xdeadbeef);
3598 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3599 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3600 GetLastError() == ERROR_ACCESS_DENIED),
3601 "ReplaceFileA: unexpected error %d\n", GetLastError());
3602 DeleteFileA( replacement );
3605 * if the first round (w/ backup) worked then as long as there is no
3606 * failure then there is no need to check this round (w/ backup is the
3607 * more complete case)
3610 /* delete temporary files, replacement and replaced are already deleted */
3611 if (removeBackup)
3613 ret = DeleteFileA(backup);
3614 ok(ret ||
3615 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3616 "DeleteFileA: error (backup) %d\n", GetLastError());
3621 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
3622 * need to be as thorough.
3624 static void test_ReplaceFileW(void)
3626 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3627 static const WCHAR prefix[] = {'p','f','x',0};
3628 WCHAR temp_path[MAX_PATH];
3629 DWORD ret;
3630 BOOL removeBackup = FALSE;
3632 if (!pReplaceFileW)
3634 win_skip("ReplaceFileW() is missing\n");
3635 return;
3638 ret = GetTempPathW(MAX_PATH, temp_path);
3639 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3641 win_skip("GetTempPathW is not available\n");
3642 return;
3644 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
3645 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3647 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
3648 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
3650 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3651 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3653 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
3654 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
3656 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3657 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
3659 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3660 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3661 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3662 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3663 "ReplaceFileW: error %d\n", GetLastError());
3665 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3666 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3667 ret = DeleteFileW(backup);
3668 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
3669 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3670 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3671 "ReplaceFileW: error %d\n", GetLastError());
3673 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3674 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3675 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
3676 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3677 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3679 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3680 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
3681 "ReplaceFileW: unexpected error %d\n", GetLastError());
3682 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
3683 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3684 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3685 if (ret)
3686 removeBackup = TRUE;
3688 ret = DeleteFileW(replaced);
3689 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
3690 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3691 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
3693 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3694 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3695 GetLastError() == ERROR_ACCESS_DENIED),
3696 "ReplaceFileW: unexpected error %d\n", GetLastError());
3697 DeleteFileW( replacement );
3699 if (removeBackup)
3701 ret = DeleteFileW(backup);
3702 ok(ret ||
3703 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3704 "DeleteFileW: error (backup) %d\n", GetLastError());
3708 static void test_CreateFile(void)
3710 static const struct test_data
3712 DWORD disposition, access, error, clean_up;
3713 } td[] =
3715 /* 0 */ { 0, 0, ERROR_INVALID_PARAMETER, 0 },
3716 /* 1 */ { 0, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3717 /* 2 */ { 0, GENERIC_READ|GENERIC_WRITE, ERROR_INVALID_PARAMETER, 0 },
3718 /* 3 */ { CREATE_NEW, 0, ERROR_FILE_EXISTS, 1 },
3719 /* 4 */ { CREATE_NEW, 0, 0, 1 },
3720 /* 5 */ { CREATE_NEW, GENERIC_READ, 0, 1 },
3721 /* 6 */ { CREATE_NEW, GENERIC_WRITE, 0, 1 },
3722 /* 7 */ { CREATE_NEW, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3723 /* 8 */ { CREATE_ALWAYS, 0, 0, 0 },
3724 /* 9 */ { CREATE_ALWAYS, GENERIC_READ, 0, 0 },
3725 /* 10*/ { CREATE_ALWAYS, GENERIC_WRITE, 0, 0 },
3726 /* 11*/ { CREATE_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3727 /* 12*/ { OPEN_EXISTING, 0, ERROR_FILE_NOT_FOUND, 0 },
3728 /* 13*/ { CREATE_ALWAYS, 0, 0, 0 },
3729 /* 14*/ { OPEN_EXISTING, 0, 0, 0 },
3730 /* 15*/ { OPEN_EXISTING, GENERIC_READ, 0, 0 },
3731 /* 16*/ { OPEN_EXISTING, GENERIC_WRITE, 0, 0 },
3732 /* 17*/ { OPEN_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 1 },
3733 /* 18*/ { OPEN_ALWAYS, 0, 0, 0 },
3734 /* 19*/ { OPEN_ALWAYS, GENERIC_READ, 0, 0 },
3735 /* 20*/ { OPEN_ALWAYS, GENERIC_WRITE, 0, 0 },
3736 /* 21*/ { OPEN_ALWAYS, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3737 /* 22*/ { TRUNCATE_EXISTING, 0, ERROR_INVALID_PARAMETER, 0 },
3738 /* 23*/ { TRUNCATE_EXISTING, GENERIC_READ, ERROR_INVALID_PARAMETER, 0 },
3739 /* 24*/ { TRUNCATE_EXISTING, GENERIC_WRITE, 0, 0 },
3740 /* 25*/ { TRUNCATE_EXISTING, GENERIC_READ|GENERIC_WRITE, 0, 0 },
3741 /* 26*/ { TRUNCATE_EXISTING, FILE_WRITE_DATA, ERROR_INVALID_PARAMETER, 0 }
3743 char temp_path[MAX_PATH];
3744 char file_name[MAX_PATH];
3745 DWORD i, ret, written;
3746 HANDLE hfile;
3748 GetTempPathA(MAX_PATH, temp_path);
3749 GetTempFileNameA(temp_path, "tmp", 0, file_name);
3751 i = strlen(temp_path);
3752 if (i && temp_path[i - 1] == '\\') temp_path[i - 1] = 0;
3754 for (i = 0; i <= 5; i++)
3756 SetLastError(0xdeadbeef);
3757 hfile = CreateFileA(temp_path, GENERIC_READ, 0, NULL, i, 0, 0);
3758 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
3759 if (i == 0 || i == 5)
3761 /* FIXME: remove once Wine is fixed */
3762 if (i == 5) todo_wine
3763 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3764 else
3765 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3767 else
3769 /* FIXME: remove once Wine is fixed */
3770 if (i == 1) todo_wine
3771 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3772 else
3773 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3776 SetLastError(0xdeadbeef);
3777 hfile = CreateFileA(temp_path, GENERIC_WRITE, 0, NULL, i, 0, 0);
3778 ok(hfile == INVALID_HANDLE_VALUE, "CreateFile should fail\n");
3779 if (i == 0)
3780 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3781 else
3783 /* FIXME: remove once Wine is fixed */
3784 if (i == 1) todo_wine
3785 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3786 else
3787 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3791 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3793 SetLastError(0xdeadbeef);
3794 hfile = CreateFileA(file_name, td[i].access, 0, NULL, td[i].disposition, 0, 0);
3795 if (!td[i].error)
3797 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
3798 written = 0xdeadbeef;
3799 SetLastError(0xdeadbeef);
3800 ret = WriteFile(hfile, &td[i].error, sizeof(td[i].error), &written, NULL);
3801 if (td[i].access & GENERIC_WRITE)
3802 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
3803 else
3805 ok(!ret, "%d: WriteFile should fail\n", i);
3806 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3808 CloseHandle(hfile);
3810 else
3812 /* FIXME: remove the condition below once Wine is fixed */
3813 if (td[i].disposition == TRUNCATE_EXISTING && !(td[i].access & GENERIC_WRITE))
3815 todo_wine
3817 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
3818 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
3820 CloseHandle(hfile);
3822 else
3824 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
3825 ok(GetLastError() == td[i].error, "%d: expected %d, got %d\n", i, td[i].error, GetLastError());
3829 if (td[i].clean_up) DeleteFileA(file_name);
3832 DeleteFileA(file_name);
3835 static void test_GetFileInformationByHandleEx(void)
3837 int i;
3838 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[1024], *strPtr;
3839 BOOL ret;
3840 DWORD ret2, written;
3841 HANDLE directory, file;
3842 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
3843 FILE_BASIC_INFO *basicInfo;
3844 FILE_STANDARD_INFO *standardInfo;
3845 FILE_NAME_INFO *nameInfo;
3846 LARGE_INTEGER prevWrite;
3847 FILE_IO_PRIORITY_HINT_INFO priohintinfo;
3848 FILE_ALLOCATION_INFO allocinfo;
3849 FILE_DISPOSITION_INFO dispinfo;
3850 FILE_END_OF_FILE_INFO eofinfo;
3851 FILE_RENAME_INFO renameinfo;
3853 struct {
3854 FILE_INFO_BY_HANDLE_CLASS handleClass;
3855 void *ptr;
3856 DWORD size;
3857 DWORD errorCode;
3858 } checks[] = {
3859 {0xdeadbeef, NULL, 0, ERROR_INVALID_PARAMETER},
3860 {FileIdBothDirectoryInfo, NULL, 0, ERROR_BAD_LENGTH},
3861 {FileIdBothDirectoryInfo, NULL, sizeof(buffer), ERROR_NOACCESS},
3862 {FileIdBothDirectoryInfo, buffer, 0, ERROR_BAD_LENGTH}};
3864 if (!pGetFileInformationByHandleEx)
3866 win_skip("GetFileInformationByHandleEx is missing.\n");
3867 return;
3870 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
3871 ok(ret2, "GetFileInformationByHandleEx: GetTempPathA failed, got error %u.\n", GetLastError());
3873 /* ensure the existence of a file in the temp folder */
3874 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
3875 ok(ret2, "GetFileInformationByHandleEx: GetTempFileNameA failed, got error %u.\n", GetLastError());
3876 ret2 = GetFileAttributesA(tempFileName);
3877 ok(ret2 != INVALID_FILE_ATTRIBUTES, "GetFileInformationByHandleEx: "
3878 "GetFileAttributesA failed to find the temp file, got error %u.\n", GetLastError());
3880 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3881 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
3882 ok(directory != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp folder, "
3883 "got error %u.\n", GetLastError());
3885 for (i = 0; i < sizeof(checks) / sizeof(checks[0]); i += 1)
3887 SetLastError(0xdeadbeef);
3888 ret = pGetFileInformationByHandleEx(directory, checks[i].handleClass, checks[i].ptr, checks[i].size);
3889 ok(!ret && GetLastError() == checks[i].errorCode, "GetFileInformationByHandleEx: expected error %u, "
3890 "got %u.\n", checks[i].errorCode, GetLastError());
3893 while (TRUE)
3895 memset(buffer, 0xff, sizeof(buffer));
3896 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
3897 if (!ret && GetLastError() == ERROR_NO_MORE_FILES)
3898 break;
3899 ok(ret, "GetFileInformationByHandleEx: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
3900 if (!ret)
3901 break;
3902 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
3903 while (TRUE)
3905 ok(bothDirInfo->FileAttributes != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file attributes.\n");
3906 ok(bothDirInfo->FileId.u.LowPart != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file id.\n");
3907 ok(bothDirInfo->FileNameLength != 0xffffffff, "GetFileInformationByHandleEx: returned invalid file name length.\n");
3908 if (!bothDirInfo->NextEntryOffset)
3909 break;
3910 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
3914 CloseHandle(directory);
3916 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3917 NULL, OPEN_EXISTING, 0, NULL);
3918 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
3919 "got error %u.\n", GetLastError());
3921 /* Test FileBasicInfo; make sure the write time changes when a file is updated */
3922 memset(buffer, 0xff, sizeof(buffer));
3923 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
3924 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
3925 basicInfo = (FILE_BASIC_INFO *)buffer;
3926 prevWrite = basicInfo->LastWriteTime;
3927 CloseHandle(file);
3929 Sleep(30); /* Make sure a new write time is different from the previous */
3931 /* Write something to the file, to make sure the write time has changed */
3932 file = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3933 NULL, OPEN_EXISTING, 0, NULL);
3934 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
3935 "got error %u.\n", GetLastError());
3936 ret = WriteFile(file, tempFileName, strlen(tempFileName), &written, NULL);
3937 ok(ret, "GetFileInformationByHandleEx: Write failed\n");
3938 CloseHandle(file);
3940 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3941 NULL, OPEN_EXISTING, 0, NULL);
3942 ok(file != INVALID_HANDLE_VALUE, "GetFileInformationByHandleEx: failed to open the temp file, "
3943 "got error %u.\n", GetLastError());
3945 memset(buffer, 0xff, sizeof(buffer));
3946 ret = pGetFileInformationByHandleEx(file, FileBasicInfo, buffer, sizeof(buffer));
3947 ok(ret, "GetFileInformationByHandleEx: failed to get FileBasicInfo, %u\n", GetLastError());
3948 basicInfo = (FILE_BASIC_INFO *)buffer;
3949 /* Could also check that the creation time didn't change - on windows
3950 * it doesn't, but on wine, it does change even if it shouldn't. */
3951 ok(basicInfo->LastWriteTime.QuadPart != prevWrite.QuadPart,
3952 "GetFileInformationByHandleEx: last write time didn't change\n");
3954 /* Test FileStandardInfo, check some basic parameters */
3955 memset(buffer, 0xff, sizeof(buffer));
3956 ret = pGetFileInformationByHandleEx(file, FileStandardInfo, buffer, sizeof(buffer));
3957 ok(ret, "GetFileInformationByHandleEx: failed to get FileStandardInfo, %u\n", GetLastError());
3958 standardInfo = (FILE_STANDARD_INFO *)buffer;
3959 ok(standardInfo->NumberOfLinks == 1, "GetFileInformationByHandleEx: Unexpected number of links\n");
3960 ok(standardInfo->DeletePending == FALSE, "GetFileInformationByHandleEx: Unexpected pending delete\n");
3961 ok(standardInfo->Directory == FALSE, "GetFileInformationByHandleEx: Incorrect directory flag\n");
3963 /* Test FileNameInfo */
3964 memset(buffer, 0xff, sizeof(buffer));
3965 ret = pGetFileInformationByHandleEx(file, FileNameInfo, buffer, sizeof(buffer));
3966 ok(ret, "GetFileInformationByHandleEx: failed to get FileNameInfo, %u\n", GetLastError());
3967 nameInfo = (FILE_NAME_INFO *)buffer;
3968 strPtr = strchr(tempFileName, '\\');
3969 ok(strPtr != NULL, "GetFileInformationByHandleEx: Temp filename didn't contain backslash\n");
3970 ok(nameInfo->FileNameLength == strlen(strPtr) * 2,
3971 "GetFileInformationByHandleEx: Incorrect file name length\n");
3972 for (i = 0; i < nameInfo->FileNameLength/2; i++)
3973 ok(strPtr[i] == nameInfo->FileName[i], "Incorrect filename char %d: %c vs %c\n",
3974 i, strPtr[i], nameInfo->FileName[i]);
3976 /* invalid classes */
3977 SetLastError(0xdeadbeef);
3978 ret = pGetFileInformationByHandleEx(file, FileEndOfFileInfo, &eofinfo, sizeof(eofinfo));
3979 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3981 SetLastError(0xdeadbeef);
3982 ret = pGetFileInformationByHandleEx(file, FileIoPriorityHintInfo, &priohintinfo, sizeof(priohintinfo));
3983 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3985 SetLastError(0xdeadbeef);
3986 ret = pGetFileInformationByHandleEx(file, FileAllocationInfo, &allocinfo, sizeof(allocinfo));
3987 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3989 SetLastError(0xdeadbeef);
3990 ret = pGetFileInformationByHandleEx(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
3991 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3993 SetLastError(0xdeadbeef);
3994 ret = pGetFileInformationByHandleEx(file, FileRenameInfo, &renameinfo, sizeof(renameinfo));
3995 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
3997 CloseHandle(file);
3998 DeleteFileA(tempFileName);
4001 static void test_OpenFileById(void)
4003 char tempPath[MAX_PATH], tempFileName[MAX_PATH], buffer[256], tickCount[256];
4004 WCHAR tempFileNameW[MAX_PATH];
4005 BOOL ret, found;
4006 DWORD ret2, count, tempFileNameLen;
4007 HANDLE directory, handle, tempFile;
4008 FILE_ID_BOTH_DIR_INFO *bothDirInfo;
4009 FILE_ID_DESCRIPTOR fileIdDescr;
4011 if (!pGetFileInformationByHandleEx || !pOpenFileById)
4013 win_skip("GetFileInformationByHandleEx or OpenFileById is missing.\n");
4014 return;
4017 ret2 = GetTempPathA(sizeof(tempPath), tempPath);
4018 ok(ret2, "OpenFileById: GetTempPath failed, got error %u.\n", GetLastError());
4020 /* ensure the existence of a file in the temp folder */
4021 ret2 = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4022 ok(ret2, "OpenFileById: GetTempFileNameA failed, got error %u.\n", GetLastError());
4023 ret2 = GetFileAttributesA(tempFileName);
4024 ok(ret2 != INVALID_FILE_ATTRIBUTES,
4025 "OpenFileById: GetFileAttributesA failed to find the temp file, got error %u\n", GetLastError());
4027 ret2 = MultiByteToWideChar(CP_ACP, 0, tempFileName + strlen(tempPath), -1, tempFileNameW, sizeof(tempFileNameW)/sizeof(tempFileNameW[0]));
4028 ok(ret2, "OpenFileById: MultiByteToWideChar failed to convert tempFileName, got error %u.\n", GetLastError());
4029 tempFileNameLen = ret2 - 1;
4031 tempFile = CreateFileA(tempFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4032 ok(tempFile != INVALID_HANDLE_VALUE, "OpenFileById: failed to create a temp file, "
4033 "got error %u.\n", GetLastError());
4034 ret2 = sprintf(tickCount, "%u", GetTickCount());
4035 ret = WriteFile(tempFile, tickCount, ret2, &count, NULL);
4036 ok(ret, "OpenFileById: WriteFile failed, got error %u.\n", GetLastError());
4037 CloseHandle(tempFile);
4039 directory = CreateFileA(tempPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4040 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
4041 ok(directory != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder, "
4042 "got error %u.\n", GetLastError());
4044 /* get info about the temp folder itself */
4045 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4046 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4047 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4048 ok(bothDirInfo->FileNameLength == sizeof(WCHAR) && bothDirInfo->FileName[0] == '.',
4049 "OpenFileById: failed to return the temp folder at the first entry, got error %u.\n", GetLastError());
4051 /* open the temp folder itself */
4052 fileIdDescr.dwSize = sizeof(fileIdDescr);
4053 fileIdDescr.Type = FileIdType;
4054 U(fileIdDescr).FileId = bothDirInfo->FileId;
4055 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4056 todo_wine
4057 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the temp folder itself, got error %u.\n", GetLastError());
4058 CloseHandle(handle);
4060 /* find the temp file in the temp folder */
4061 found = FALSE;
4062 while (!found)
4064 ret = pGetFileInformationByHandleEx(directory, FileIdBothDirectoryInfo, buffer, sizeof(buffer));
4065 ok(ret, "OpenFileById: failed to query for FileIdBothDirectoryInfo, got error %u.\n", GetLastError());
4066 if (!ret)
4067 break;
4068 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)buffer;
4069 while (TRUE)
4071 if (tempFileNameLen == bothDirInfo->FileNameLength / sizeof(WCHAR) &&
4072 memcmp(tempFileNameW, bothDirInfo->FileName, bothDirInfo->FileNameLength) == 0)
4074 found = TRUE;
4075 break;
4077 if (!bothDirInfo->NextEntryOffset)
4078 break;
4079 bothDirInfo = (FILE_ID_BOTH_DIR_INFO *)(((char *)bothDirInfo) + bothDirInfo->NextEntryOffset);
4082 ok(found, "OpenFileById: failed to find the temp file in the temp folder.\n");
4084 SetLastError(0xdeadbeef);
4085 handle = pOpenFileById(directory, NULL, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4086 ok(handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER,
4087 "OpenFileById: expected ERROR_INVALID_PARAMETER, got error %u.\n", GetLastError());
4089 fileIdDescr.dwSize = sizeof(fileIdDescr);
4090 fileIdDescr.Type = FileIdType;
4091 U(fileIdDescr).FileId = bothDirInfo->FileId;
4092 handle = pOpenFileById(directory, &fileIdDescr, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, 0);
4093 ok(handle != INVALID_HANDLE_VALUE, "OpenFileById: failed to open the file, got error %u.\n", GetLastError());
4095 ret = ReadFile(handle, buffer, sizeof(buffer), &count, NULL);
4096 buffer[count] = 0;
4097 ok(ret, "OpenFileById: ReadFile failed, got error %u.\n", GetLastError());
4098 ok(strcmp(tickCount, buffer) == 0, "OpenFileById: invalid contents of the temp file.\n");
4100 CloseHandle(handle);
4101 CloseHandle(directory);
4102 DeleteFileA(tempFileName);
4105 static void test_SetFileValidData(void)
4107 BOOL ret;
4108 HANDLE handle;
4109 DWORD error, count;
4110 char path[MAX_PATH], filename[MAX_PATH];
4111 TOKEN_PRIVILEGES privs;
4112 HANDLE token = NULL;
4114 if (!pSetFileValidData)
4116 win_skip("SetFileValidData is missing\n");
4117 return;
4119 GetTempPathA(sizeof(path), path);
4120 GetTempFileNameA(path, "tst", 0, filename);
4121 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4122 WriteFile(handle, "test", sizeof("test") - 1, &count, NULL);
4123 CloseHandle(handle);
4125 SetLastError(0xdeadbeef);
4126 ret = pSetFileValidData(INVALID_HANDLE_VALUE, 0);
4127 error = GetLastError();
4128 ok(!ret, "SetFileValidData succeeded\n");
4129 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4131 SetLastError(0xdeadbeef);
4132 ret = pSetFileValidData(INVALID_HANDLE_VALUE, -1);
4133 error = GetLastError();
4134 ok(!ret, "SetFileValidData succeeded\n");
4135 ok(error == ERROR_INVALID_HANDLE, "got %u\n", error);
4137 /* file opened for reading */
4138 handle = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
4140 SetLastError(0xdeadbeef);
4141 ret = pSetFileValidData(handle, 0);
4142 ok(!ret, "SetFileValidData succeeded\n");
4143 error = GetLastError();
4144 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4146 SetLastError(0xdeadbeef);
4147 ret = pSetFileValidData(handle, -1);
4148 error = GetLastError();
4149 ok(!ret, "SetFileValidData succeeded\n");
4150 ok(error == ERROR_ACCESS_DENIED, "got %u\n", error);
4151 CloseHandle(handle);
4153 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4155 SetLastError(0xdeadbeef);
4156 ret = pSetFileValidData(handle, 0);
4157 error = GetLastError();
4158 ok(!ret, "SetFileValidData succeeded\n");
4159 todo_wine ok(error == ERROR_PRIVILEGE_NOT_HELD, "got %u\n", error);
4160 CloseHandle(handle);
4162 privs.PrivilegeCount = 1;
4163 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
4165 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token) ||
4166 !LookupPrivilegeValueA(NULL, SE_MANAGE_VOLUME_NAME, &privs.Privileges[0].Luid) ||
4167 !AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL) ||
4168 GetLastError() == ERROR_NOT_ALL_ASSIGNED)
4170 win_skip("cannot enable SE_MANAGE_VOLUME_NAME privilege\n");
4171 CloseHandle(token);
4172 DeleteFileA(filename);
4173 return;
4175 handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
4177 SetLastError(0xdeadbeef);
4178 ret = pSetFileValidData(handle, 0);
4179 error = GetLastError();
4180 ok(!ret, "SetFileValidData succeeded\n");
4181 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4183 SetLastError(0xdeadbeef);
4184 ret = pSetFileValidData(handle, -1);
4185 error = GetLastError();
4186 ok(!ret, "SetFileValidData succeeded\n");
4187 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4189 SetLastError(0xdeadbeef);
4190 ret = pSetFileValidData(handle, 2);
4191 error = GetLastError();
4192 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4193 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4195 ret = pSetFileValidData(handle, 4);
4196 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4198 SetLastError(0xdeadbeef);
4199 ret = pSetFileValidData(handle, 8);
4200 error = GetLastError();
4201 ok(!ret, "SetFileValidData succeeded\n");
4202 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4204 count = SetFilePointer(handle, 1024, NULL, FILE_END);
4205 ok(count != INVALID_SET_FILE_POINTER, "SetFilePointer failed %u\n", GetLastError());
4206 ret = SetEndOfFile(handle);
4207 ok(ret, "SetEndOfFile failed %u\n", GetLastError());
4209 SetLastError(0xdeadbeef);
4210 ret = pSetFileValidData(handle, 2);
4211 error = GetLastError();
4212 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4213 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4215 ret = pSetFileValidData(handle, 4);
4216 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4218 ret = pSetFileValidData(handle, 8);
4219 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4221 ret = pSetFileValidData(handle, 4);
4222 error = GetLastError();
4223 todo_wine ok(!ret, "SetFileValidData succeeded\n");
4224 todo_wine ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4226 ret = pSetFileValidData(handle, 1024);
4227 ok(ret, "SetFileValidData failed %u\n", GetLastError());
4229 ret = pSetFileValidData(handle, 2048);
4230 error = GetLastError();
4231 ok(!ret, "SetFileValidData succeeded\n");
4232 ok(error == ERROR_INVALID_PARAMETER, "got %u\n", error);
4234 privs.Privileges[0].Attributes = 0;
4235 AdjustTokenPrivileges(token, FALSE, &privs, sizeof(privs), NULL, NULL);
4237 CloseHandle(token);
4238 CloseHandle(handle);
4239 DeleteFileA(filename);
4242 static void test_WriteFileGather(void)
4244 char temp_path[MAX_PATH], filename[MAX_PATH];
4245 HANDLE hfile, hiocp1, hiocp2;
4246 DWORD ret, size;
4247 ULONG_PTR key;
4248 FILE_SEGMENT_ELEMENT fse[2];
4249 OVERLAPPED ovl, *povl = NULL;
4250 SYSTEM_INFO si;
4251 LPVOID buf = NULL;
4253 ret = GetTempPathA( MAX_PATH, temp_path );
4254 ok( ret != 0, "GetTempPathA error %d\n", GetLastError() );
4255 ok( ret < MAX_PATH, "temp path should fit into MAX_PATH\n" );
4256 ret = GetTempFileNameA( temp_path, "wfg", 0, filename );
4257 ok( ret != 0, "GetTempFileNameA error %d\n", GetLastError() );
4259 hfile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
4260 FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL, 0 );
4261 ok( hfile != INVALID_HANDLE_VALUE, "CreateFile failed err %u\n", GetLastError() );
4262 if (hfile == INVALID_HANDLE_VALUE) return;
4264 hiocp1 = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 999, 0 );
4265 hiocp2 = CreateIoCompletionPort( hfile, hiocp1, 999, 0 );
4266 ok( hiocp2 != 0, "CreateIoCompletionPort failed err %u\n", GetLastError() );
4268 GetSystemInfo( &si );
4269 buf = VirtualAlloc( NULL, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE );
4270 ok( buf != NULL, "VirtualAlloc failed err %u\n", GetLastError() );
4272 memset( &ovl, 0, sizeof(ovl) );
4273 memset( fse, 0, sizeof(fse) );
4274 fse[0].Buffer = buf;
4275 if (!WriteFileGather( hfile, fse, si.dwPageSize, NULL, &ovl ))
4276 ok( GetLastError() == ERROR_IO_PENDING, "WriteFileGather failed err %u\n", GetLastError() );
4278 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4279 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4280 ok( povl == &ovl, "wrong ovl %p\n", povl );
4282 memset( &ovl, 0, sizeof(ovl) );
4283 memset( fse, 0, sizeof(fse) );
4284 fse[0].Buffer = buf;
4285 if (!ReadFileScatter( hfile, fse, si.dwPageSize, NULL, &ovl ))
4286 ok( GetLastError() == ERROR_IO_PENDING, "ReadFileScatter failed err %u\n", GetLastError() );
4288 ret = GetQueuedCompletionStatus( hiocp2, &size, &key, &povl, 1000 );
4289 ok( ret, "GetQueuedCompletionStatus failed err %u\n", GetLastError());
4290 ok( povl == &ovl, "wrong ovl %p\n", povl );
4292 CloseHandle( hfile );
4293 CloseHandle( hiocp1 );
4294 CloseHandle( hiocp2 );
4295 VirtualFree( buf, 0, MEM_RELEASE );
4296 DeleteFileA( filename );
4299 static unsigned file_map_access(unsigned access)
4301 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
4302 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
4303 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
4304 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
4305 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
4308 static BOOL is_access_compatible(unsigned obj_access, unsigned desired_access)
4310 obj_access = file_map_access(obj_access);
4311 desired_access = file_map_access(desired_access);
4312 return (obj_access & desired_access) == desired_access;
4315 static void test_file_access(void)
4317 static const struct
4319 unsigned access, create_error, write_error, read_error;
4320 } td[] =
4322 { GENERIC_READ | GENERIC_WRITE, 0, 0, 0 },
4323 { GENERIC_WRITE, 0, 0, ERROR_ACCESS_DENIED },
4324 { GENERIC_READ, 0, ERROR_ACCESS_DENIED, 0 },
4325 { FILE_READ_DATA | FILE_WRITE_DATA, 0, 0, 0 },
4326 { FILE_WRITE_DATA, 0, 0, ERROR_ACCESS_DENIED },
4327 { FILE_READ_DATA, 0, ERROR_ACCESS_DENIED, 0 },
4328 { FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4329 { FILE_READ_DATA | FILE_APPEND_DATA, 0, 0, 0 },
4330 { FILE_WRITE_DATA | FILE_APPEND_DATA, 0, 0, ERROR_ACCESS_DENIED },
4331 { 0, 0, ERROR_ACCESS_DENIED, ERROR_ACCESS_DENIED },
4333 char path[MAX_PATH], fname[MAX_PATH];
4334 unsigned char buf[16];
4335 HANDLE hfile, hdup;
4336 DWORD i, j, ret, bytes;
4338 GetTempPathA(MAX_PATH, path);
4339 GetTempFileNameA(path, "foo", 0, fname);
4341 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
4343 SetLastError(0xdeadbeef);
4344 hfile = CreateFileA(fname, td[i].access, 0, NULL, CREATE_ALWAYS,
4345 FILE_FLAG_DELETE_ON_CLOSE, 0);
4346 if (td[i].create_error)
4348 ok(hfile == INVALID_HANDLE_VALUE, "%d: CreateFile should fail\n", i);
4349 ok(td[i].create_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].create_error, GetLastError());
4350 continue;
4352 else
4353 ok(hfile != INVALID_HANDLE_VALUE, "%d: CreateFile error %d\n", i, GetLastError());
4355 for (j = 0; j < sizeof(td)/sizeof(td[0]); j++)
4357 SetLastError(0xdeadbeef);
4358 ret = DuplicateHandle(GetCurrentProcess(), hfile, GetCurrentProcess(), &hdup,
4359 td[j].access, 0, 0);
4360 if (is_access_compatible(td[i].access, td[j].access))
4361 ok(ret, "DuplicateHandle(%#x => %#x) error %d\n", td[i].access, td[j].access, GetLastError());
4362 else
4364 /* FIXME: Remove once Wine is fixed */
4365 if ((td[j].access & (GENERIC_READ | GENERIC_WRITE)) ||
4366 (!(td[i].access & (GENERIC_WRITE | FILE_WRITE_DATA)) && (td[j].access & FILE_WRITE_DATA)) ||
4367 (!(td[i].access & (GENERIC_READ | FILE_READ_DATA)) && (td[j].access & FILE_READ_DATA)) ||
4368 (!(td[i].access & (GENERIC_WRITE)) && (td[j].access & FILE_APPEND_DATA)))
4370 todo_wine
4371 ok(!ret, "DuplicateHandle(%#x => %#x) should fail\n", td[i].access, td[j].access);
4372 todo_wine
4373 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4375 else
4377 ok(!ret, "DuplicateHandle(%#x => %#x) should fail\n", td[i].access, td[j].access);
4378 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
4381 if (ret) CloseHandle(hdup);
4384 SetLastError(0xdeadbeef);
4385 bytes = 0xdeadbeef;
4386 ret = WriteFile(hfile, "\x5e\xa7", 2, &bytes, NULL);
4387 if (td[i].write_error)
4389 ok(!ret, "%d: WriteFile should fail\n", i);
4390 ok(td[i].write_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].write_error, GetLastError());
4391 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4393 else
4395 ok(ret, "%d: WriteFile error %d\n", i, GetLastError());
4396 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4399 SetLastError(0xdeadbeef);
4400 ret = SetFilePointer(hfile, 0, NULL, FILE_BEGIN);
4401 ok(ret != INVALID_SET_FILE_POINTER, "SetFilePointer error %d\n", GetLastError());
4403 SetLastError(0xdeadbeef);
4404 bytes = 0xdeadbeef;
4405 ret = ReadFile(hfile, buf, sizeof(buf), &bytes, NULL);
4406 if (td[i].read_error)
4408 ok(!ret, "%d: ReadFile should fail\n", i);
4409 ok(td[i].read_error == GetLastError(), "%d: expected %d, got %d\n", i, td[i].read_error, GetLastError());
4410 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4412 else
4414 ok(ret, "%d: ReadFile error %d\n", i, GetLastError());
4415 if (td[i].write_error)
4416 ok(bytes == 0, "%d: expected 0, got %u\n", i, bytes);
4417 else
4419 ok(bytes == 2, "%d: expected 2, got %u\n", i, bytes);
4420 ok(buf[0] == 0x5e && buf[1] == 0xa7, "%d: expected 5ea7, got %02x%02x\n", i, buf[0], buf[1]);
4424 CloseHandle(hfile);
4428 static void test_GetFinalPathNameByHandleA(void)
4430 static char prefix[] = "GetFinalPathNameByHandleA";
4431 static char dos_prefix[] = "\\\\?\\";
4432 char temp_path[MAX_PATH], test_path[MAX_PATH];
4433 char long_path[MAX_PATH], result_path[MAX_PATH];
4434 char dos_path[MAX_PATH + sizeof(dos_prefix)];
4435 HANDLE file;
4436 DWORD count;
4437 UINT ret;
4439 if (!pGetFinalPathNameByHandleA)
4441 skip("GetFinalPathNameByHandleA is missing\n");
4442 return;
4445 /* Test calling with INVALID_HANDLE_VALUE */
4446 SetLastError(0xdeadbeaf);
4447 count = pGetFinalPathNameByHandleA(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4448 ok(count == 0, "Expected length 0, got %u\n", count);
4449 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4451 count = GetTempPathA(MAX_PATH, temp_path);
4452 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4453 ret = GetTempFileNameA(temp_path, prefix, 0, test_path);
4454 ok(ret != 0, "GetTempFileNameA error %u\n", GetLastError());
4455 ret = GetLongPathNameA(test_path, long_path, MAX_PATH);
4456 ok(ret != 0, "GetLongPathNameA error %u\n", GetLastError());
4457 strcpy(dos_path, dos_prefix);
4458 strcat(dos_path, long_path);
4460 file = CreateFileA(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4461 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4462 ok(file != INVALID_HANDLE_VALUE, "CreateFileA error %u\n", GetLastError());
4464 /* Test VOLUME_NAME_DOS with sufficient buffer size */
4465 memset(result_path, 0x11, sizeof(result_path));
4466 count = pGetFinalPathNameByHandleA(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4467 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4468 ok(lstrcmpiA(dos_path, result_path) == 0, "Expected %s, got %s\n", dos_path, result_path);
4470 /* Test VOLUME_NAME_DOS with insufficient buffer size */
4471 memset(result_path, 0x11, sizeof(result_path));
4472 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-2, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4473 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4474 ok(result_path[0] == 0x11, "Result path was modified\n");
4476 memset(result_path, 0x11, sizeof(result_path));
4477 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4478 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4479 ok(result_path[0] == 0x11, "Result path was modified\n");
4481 memset(result_path, 0x11, sizeof(result_path));
4482 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4483 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4484 ok(result_path[0] == 0x11, "Result path was modified\n");
4486 memset(result_path, 0x11, sizeof(result_path));
4487 count = pGetFinalPathNameByHandleA(file, result_path, strlen(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4488 ok(count == strlen(dos_path), "Expected length %u, got %u\n", (DWORD)strlen(dos_path), count);
4489 ok(result_path[0] != 0x11, "Result path was not modified\n");
4490 ok(!result_path[strlen(dos_path)], "Expected nullterminated string\n");
4491 ok(result_path[strlen(dos_path)+1] == 0x11, "Buffer overflow\n");
4493 CloseHandle(file);
4496 static void test_GetFinalPathNameByHandleW(void)
4498 static WCHAR prefix[] = {'G','e','t','F','i','n','a','l','P','a','t','h',
4499 'N','a','m','e','B','y','H','a','n','d','l','e','W','\0'};
4500 static WCHAR dos_prefix[] = {'\\','\\','?','\\','\0'};
4501 WCHAR temp_path[MAX_PATH], test_path[MAX_PATH];
4502 WCHAR long_path[MAX_PATH], result_path[MAX_PATH];
4503 WCHAR dos_path[MAX_PATH + sizeof(dos_prefix)];
4504 WCHAR drive_part[MAX_PATH];
4505 WCHAR *file_part;
4506 WCHAR volume_path[MAX_PATH + 50];
4507 WCHAR nt_path[2 * MAX_PATH];
4508 BOOL success;
4509 HANDLE file;
4510 DWORD count;
4511 UINT ret;
4513 if (!pGetFinalPathNameByHandleW)
4515 skip("GetFinalPathNameByHandleW is missing\n");
4516 return;
4519 /* Test calling with INVALID_HANDLE_VALUE */
4520 SetLastError(0xdeadbeaf);
4521 count = pGetFinalPathNameByHandleW(INVALID_HANDLE_VALUE, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4522 ok(count == 0, "Expected length 0, got %u\n", count);
4523 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %u\n", GetLastError());
4525 count = GetTempPathW(MAX_PATH, temp_path);
4526 ok(count, "Failed to get temp path, error %u\n", GetLastError());
4527 ret = GetTempFileNameW(temp_path, prefix, 0, test_path);
4528 ok(ret != 0, "GetTempFileNameW error %u\n", GetLastError());
4529 ret = GetLongPathNameW(test_path, long_path, MAX_PATH);
4530 ok(ret != 0, "GetLongPathNameW error %u\n", GetLastError());
4531 lstrcpyW(dos_path, dos_prefix);
4532 lstrcatW(dos_path, long_path);
4534 file = CreateFileW(test_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4535 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0);
4536 ok(file != INVALID_HANDLE_VALUE, "CreateFileW error %u\n", GetLastError());
4538 /* Test VOLUME_NAME_DOS with sufficient buffer size */
4539 memset(result_path, 0x11, sizeof(result_path));
4540 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4541 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
4542 ok(lstrcmpiW(dos_path, result_path) == 0, "Expected %s, got %s\n", wine_dbgstr_w(dos_path), wine_dbgstr_w(result_path));
4544 /* Test VOLUME_NAME_DOS with insufficient buffer size */
4545 memset(result_path, 0x11, sizeof(result_path));
4546 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4547 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4548 ok(result_path[0] == 0x1111, "Result path was modified\n");
4550 memset(result_path, 0x11, sizeof(result_path));
4551 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4552 ok(count == lstrlenW(dos_path) + 1, "Expected length %u, got %u\n", lstrlenW(dos_path) + 1, count);
4553 ok(result_path[0] == 0x1111, "Result path was modified\n");
4555 memset(result_path, 0x11, sizeof(result_path));
4556 count = pGetFinalPathNameByHandleW(file, result_path, lstrlenW(dos_path)+1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
4557 ok(count == lstrlenW(dos_path), "Expected length %u, got %u\n", lstrlenW(dos_path), count);
4558 ok(result_path[0] != 0x1111, "Result path was not modified\n");
4559 ok(!result_path[lstrlenW(dos_path)], "Expected nullterminated string\n");
4560 ok(result_path[lstrlenW(dos_path)+1] == 0x1111, "Buffer overflow\n");
4562 success = GetVolumePathNameW(long_path, drive_part, MAX_PATH);
4563 ok(success, "GetVolumePathNameW error %u\n", GetLastError());
4564 success = GetVolumeNameForVolumeMountPointW(drive_part, volume_path, sizeof(volume_path) / sizeof(WCHAR));
4565 ok(success, "GetVolumeNameForVolumeMountPointW error %u\n", GetLastError());
4567 /* Test for VOLUME_NAME_GUID */
4568 lstrcatW(volume_path, long_path + lstrlenW(drive_part));
4569 memset(result_path, 0x11, sizeof(result_path));
4570 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_GUID);
4571 ok(count == lstrlenW(volume_path), "Expected length %u, got %u\n", lstrlenW(volume_path), count);
4572 ok(lstrcmpiW(volume_path, result_path) == 0, "Expected %s, got %s\n",
4573 wine_dbgstr_w(volume_path), wine_dbgstr_w(result_path));
4575 /* Test for VOLUME_NAME_NONE */
4576 file_part = long_path + lstrlenW(drive_part) - 1;
4577 memset(result_path, 0x11, sizeof(result_path));
4578 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NONE);
4579 ok(count == lstrlenW(file_part), "Expected length %u, got %u\n", lstrlenW(file_part), count);
4580 ok(lstrcmpiW(file_part, result_path) == 0, "Expected %s, got %s\n",
4581 wine_dbgstr_w(file_part), wine_dbgstr_w(result_path));
4583 drive_part[lstrlenW(drive_part)-1] = 0;
4584 success = QueryDosDeviceW(drive_part, nt_path, sizeof(nt_path) / sizeof(WCHAR));
4585 ok(success, "QueryDosDeviceW error %u\n", GetLastError());
4587 /* Test for VOLUME_NAME_NT */
4588 lstrcatW(nt_path, file_part);
4589 memset(result_path, 0x11, sizeof(result_path));
4590 count = pGetFinalPathNameByHandleW(file, result_path, MAX_PATH, FILE_NAME_NORMALIZED | VOLUME_NAME_NT);
4591 ok(count == lstrlenW(nt_path), "Expected length %u, got %u\n", lstrlenW(nt_path), count);
4592 ok(lstrcmpiW(nt_path, result_path) == 0, "Expected %s, got %s\n",
4593 wine_dbgstr_w(nt_path), wine_dbgstr_w(result_path));
4595 CloseHandle(file);
4598 static void test_SetFileInformationByHandle(void)
4600 FILE_ATTRIBUTE_TAG_INFO fileattrinfo = { 0 };
4601 FILE_REMOTE_PROTOCOL_INFO protinfo = { 0 };
4602 FILE_STANDARD_INFO stdinfo = { };
4603 FILE_COMPRESSION_INFO compressinfo;
4604 FILE_DISPOSITION_INFO dispinfo;
4605 char tempFileName[MAX_PATH];
4606 char tempPath[MAX_PATH];
4607 HANDLE file;
4608 BOOL ret;
4610 if (!pSetFileInformationByHandle)
4612 win_skip("SetFileInformationByHandle is not supported\n");
4613 return;
4616 ret = GetTempPathA(sizeof(tempPath), tempPath);
4617 ok(ret, "GetTempPathA failed, got error %u.\n", GetLastError());
4619 /* ensure the existence of a file in the temp folder */
4620 ret = GetTempFileNameA(tempPath, "abc", 0, tempFileName);
4621 ok(ret, "GetTempFileNameA failed, got error %u.\n", GetLastError());
4623 file = CreateFileA(tempFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4624 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
4625 ok(file != INVALID_HANDLE_VALUE, "failed to open the temp file, error %u.\n", GetLastError());
4627 /* invalid classes */
4628 SetLastError(0xdeadbeef);
4629 ret = pSetFileInformationByHandle(file, FileStandardInfo, &stdinfo, sizeof(stdinfo));
4630 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4632 memset(&compressinfo, 0, sizeof(compressinfo));
4633 SetLastError(0xdeadbeef);
4634 ret = pSetFileInformationByHandle(file, FileCompressionInfo, &compressinfo, sizeof(compressinfo));
4635 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4637 SetLastError(0xdeadbeef);
4638 ret = pSetFileInformationByHandle(file, FileAttributeTagInfo, &fileattrinfo, sizeof(fileattrinfo));
4639 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4641 memset(&protinfo, 0, sizeof(protinfo));
4642 protinfo.StructureVersion = 1;
4643 protinfo.StructureSize = sizeof(protinfo);
4644 SetLastError(0xdeadbeef);
4645 ret = pSetFileInformationByHandle(file, FileRemoteProtocolInfo, &protinfo, sizeof(protinfo));
4646 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "got %d, error %d\n", ret, GetLastError());
4648 /* test FileDispositionInfo, additional details already covered by ntdll tests */
4649 SetLastError(0xdeadbeef);
4650 ret = pSetFileInformationByHandle(file, FileDispositionInfo, &dispinfo, 0);
4651 todo_wine
4652 ok(!ret && GetLastError() == ERROR_BAD_LENGTH, "got %d, error %d\n", ret, GetLastError());
4654 dispinfo.DeleteFile = TRUE;
4655 ret = pSetFileInformationByHandle(file, FileDispositionInfo, &dispinfo, sizeof(dispinfo));
4656 ok(ret, "setting FileDispositionInfo failed, error %d\n", GetLastError());
4658 CloseHandle(file);
4661 static void test_GetFileAttributesExW(void)
4663 static const WCHAR path1[] = {'\\','\\','?','\\',0};
4664 static const WCHAR path2[] = {'\\','?','?','\\',0};
4665 static const WCHAR path3[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\',0};
4666 WIN32_FILE_ATTRIBUTE_DATA info;
4667 BOOL ret;
4669 SetLastError(0xdeadbeef);
4670 ret = GetFileAttributesExW(path1, GetFileExInfoStandard, &info);
4671 ok(!ret, "GetFileAttributesExW succeeded\n");
4672 ok(GetLastError() == ERROR_INVALID_NAME, "Expected error ERROR_INVALID_NAME, got %u\n", GetLastError());
4674 SetLastError(0xdeadbeef);
4675 ret = GetFileAttributesExW(path2, GetFileExInfoStandard, &info);
4676 ok(!ret, "GetFileAttributesExW succeeded\n");
4677 ok(GetLastError() == ERROR_INVALID_NAME, "Expected error ERROR_INVALID_NAME, got %u\n", GetLastError());
4679 SetLastError(0xdeadbeef);
4680 ret = GetFileAttributesExW(path3, GetFileExInfoStandard, &info);
4681 ok(!ret, "GetFileAttributesExW succeeded\n");
4682 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
4685 START_TEST(file)
4687 InitFunctionPointers();
4689 test__hread( );
4690 test__hwrite( );
4691 test__lclose( );
4692 test__lcreat( );
4693 test__llseek( );
4694 test__llopen( );
4695 test__lread( );
4696 test__lwrite( );
4697 test_GetTempFileNameA();
4698 test_CopyFileA();
4699 test_CopyFileW();
4700 test_CopyFile2();
4701 test_CopyFileEx();
4702 test_CreateFile();
4703 test_CreateFileA();
4704 test_CreateFileW();
4705 test_CreateFile2();
4706 test_DeleteFileA();
4707 test_DeleteFileW();
4708 test_MoveFileA();
4709 test_MoveFileW();
4710 test_FindFirstFileA();
4711 test_FindNextFileA();
4712 test_FindFirstFileExA(FindExInfoStandard, 0, 0);
4713 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_CASE_SENSITIVE);
4714 test_FindFirstFileExA(FindExInfoStandard, 0, FIND_FIRST_EX_LARGE_FETCH);
4715 test_FindFirstFileExA(FindExInfoBasic, 0, 0);
4716 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
4717 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, 0);
4718 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_CASE_SENSITIVE);
4719 test_FindFirstFileExA(FindExInfoStandard, FindExSearchLimitToDirectories, FIND_FIRST_EX_LARGE_FETCH);
4720 test_FindFirstFileExA(FindExInfoBasic, FindExSearchLimitToDirectories, 0);
4721 test_LockFile();
4722 test_file_sharing();
4723 test_offset_in_overlapped_structure();
4724 test_MapFile();
4725 test_GetFileType();
4726 test_async_file_errors();
4727 test_read_write();
4728 test_OpenFile();
4729 test_overlapped();
4730 test_RemoveDirectory();
4731 test_ReplaceFileA();
4732 test_ReplaceFileW();
4733 test_GetFileInformationByHandleEx();
4734 test_OpenFileById();
4735 test_SetFileValidData();
4736 test_WriteFileGather();
4737 test_file_access();
4738 test_GetFinalPathNameByHandleA();
4739 test_GetFinalPathNameByHandleW();
4740 test_SetFileInformationByHandle();
4741 test_GetFileAttributesExW();