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
31 #include "wine/test.h"
36 static HANDLE (WINAPI
*pFindFirstFileExA
)(LPCSTR
,FINDEX_INFO_LEVELS
,LPVOID
,FINDEX_SEARCH_OPS
,LPVOID
,DWORD
);
37 static BOOL (WINAPI
*pReplaceFileA
)(LPCSTR
, LPCSTR
, LPCSTR
, DWORD
, LPVOID
, LPVOID
);
38 static BOOL (WINAPI
*pReplaceFileW
)(LPCWSTR
, LPCWSTR
, LPCWSTR
, DWORD
, LPVOID
, LPVOID
);
39 static UINT (WINAPI
*pGetSystemWindowsDirectoryA
)(LPSTR
, UINT
);
40 static BOOL (WINAPI
*pGetVolumeNameForVolumeMountPointA
)(LPCSTR
, LPSTR
, DWORD
);
42 /* keep filename and filenameW the same */
43 static const char filename
[] = "testfile.xxx";
44 static const WCHAR filenameW
[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
45 static const char sillytext
[] =
46 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
53 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
54 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
55 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
58 const char *file
; /* file string to test */
59 const DWORD err
; /* Win NT and further error code */
60 const LONG err2
; /* Win 9x & ME error code or -1 */
61 const DWORD options
; /* option flag to use for open */
62 const BOOL todo_flag
; /* todo_wine indicator */
65 static void InitFunctionPointers(void)
67 HMODULE hkernel32
= GetModuleHandleA("kernel32");
69 pFindFirstFileExA
=(void*)GetProcAddress(hkernel32
, "FindFirstFileExA");
70 pReplaceFileA
=(void*)GetProcAddress(hkernel32
, "ReplaceFileA");
71 pReplaceFileW
=(void*)GetProcAddress(hkernel32
, "ReplaceFileW");
72 pGetSystemWindowsDirectoryA
=(void*)GetProcAddress(hkernel32
, "GetSystemWindowsDirectoryA");
73 pGetVolumeNameForVolumeMountPointA
= (void *) GetProcAddress(hkernel32
, "GetVolumeNameForVolumeMountPointA");
76 static void test__hread( void )
85 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
); /* be sure to remove stale files */
86 DeleteFileA( filename
);
87 filehandle
= _lcreat( filename
, 0 );
88 if (filehandle
== HFILE_ERROR
)
90 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
94 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
96 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
98 filehandle
= _lopen( filename
, OF_READ
);
100 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError( ) );
102 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
104 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
106 for (bytes_wanted
= 0; bytes_wanted
< lstrlenA( sillytext
); bytes_wanted
++)
108 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
109 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
110 for (i
= 0; i
< bytes_wanted
; i
++)
112 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
116 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
118 ret
= DeleteFileA( filename
);
119 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
123 static void test__hwrite( void )
132 HLOCAL memory_object
;
136 filehandle
= _lcreat( filename
, 0 );
137 if (filehandle
== HFILE_ERROR
)
139 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
143 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
145 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
147 filehandle
= _lopen( filename
, OF_READ
);
149 bytes_read
= _hread( filehandle
, buffer
, 1);
151 ok( 0 == bytes_read
, "file read size error\n" );
153 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
155 filehandle
= _lopen( filename
, OF_READWRITE
);
159 srand( (unsigned)time( NULL
) );
160 for (blocks
= 0; blocks
< 100; blocks
++)
162 for (i
= 0; i
< (LONG
)sizeof( buffer
); i
++)
165 checksum
[0] = checksum
[0] + buffer
[i
];
167 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
168 bytes_written
= bytes_written
+ sizeof( buffer
);
171 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
174 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
176 memory_object
= LocalAlloc( LPTR
, bytes_written
);
178 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)\n" );
180 contents
= LocalLock( memory_object
);
182 filehandle
= _lopen( filename
, OF_READ
);
184 contents
= LocalLock( memory_object
);
186 ok( NULL
!= contents
, "LocalLock whines\n" );
188 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
194 checksum
[0] = checksum
[0] + contents
[i
];
197 while (i
< bytes_written
- 1);
199 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
201 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
203 ret
= DeleteFileA( filename
);
204 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
206 LocalFree( contents
);
210 static void test__lclose( void )
215 filehandle
= _lcreat( filename
, 0 );
216 if (filehandle
== HFILE_ERROR
)
218 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
222 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
224 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
226 ret
= DeleteFileA( filename
);
227 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
231 static void test__lcreat( void )
235 WIN32_FIND_DATAA search_results
;
236 char slashname
[] = "testfi/";
241 filehandle
= _lcreat( filename
, 0 );
242 if (filehandle
== HFILE_ERROR
)
244 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
248 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
250 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
252 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
254 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
256 find
= FindFirstFileA( filename
, &search_results
);
257 ok( INVALID_HANDLE_VALUE
!= find
, "should be able to find file\n" );
260 ret
= DeleteFileA(filename
);
261 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError());
263 filehandle
= _lcreat( filename
, 1 ); /* readonly */
264 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
266 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
268 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
270 find
= FindFirstFileA( filename
, &search_results
);
271 ok( INVALID_HANDLE_VALUE
!= find
, "should be able to find file\n" );
274 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
276 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
278 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
280 filehandle
= _lcreat( filename
, 2 );
281 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
283 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
285 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
287 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
289 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
291 find
= FindFirstFileA( filename
, &search_results
);
292 ok( INVALID_HANDLE_VALUE
!= find
, "should STILL be able to find file\n" );
295 ret
= DeleteFileA( filename
);
296 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
298 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
299 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
301 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
303 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
305 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
307 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
309 find
= FindFirstFileA( filename
, &search_results
);
310 ok( INVALID_HANDLE_VALUE
!= find
, "should STILL be able to find file\n" );
313 ret
= DeleteFileA( filename
);
314 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
316 filehandle
=_lcreat (slashname
, 0); /* illegal name */
317 if (HFILE_ERROR
==filehandle
) {
319 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
320 "creating file \"%s\" failed with error %d\n", slashname
, err
);
321 } else { /* only NT succeeds */
323 find
=FindFirstFileA (slashname
, &search_results
);
324 if (INVALID_HANDLE_VALUE
!=find
)
326 ret
= FindClose (find
);
327 ok (0 != ret
, "FindClose complains (%d)\n", GetLastError ());
328 slashname
[strlen(slashname
)-1]=0;
329 ok (!strcmp (slashname
, search_results
.cFileName
),
330 "found unexpected name \"%s\"\n", search_results
.cFileName
);
331 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
332 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
333 search_results
.dwFileAttributes
);
335 ret
= DeleteFileA( slashname
);
336 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
339 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
340 if (HFILE_ERROR
==filehandle
)
341 ok (0, "couldn't create volume label \"%s\"\n", filename
);
344 find
=FindFirstFileA (filename
, &search_results
);
345 if (INVALID_HANDLE_VALUE
==find
)
346 ok (0, "file \"%s\" not found\n", filename
);
348 ret
= FindClose(find
);
349 ok ( 0 != ret
, "FindClose complains (%d)\n", GetLastError ());
350 ok (!strcmp (filename
, search_results
.cFileName
),
351 "found unexpected name \"%s\"\n", search_results
.cFileName
);
352 search_results
.dwFileAttributes
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
353 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
354 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
355 search_results
.dwFileAttributes
);
357 ret
= DeleteFileA( filename
);
358 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
363 static void test__llseek( void )
371 filehandle
= _lcreat( filename
, 0 );
372 if (filehandle
== HFILE_ERROR
)
374 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
378 for (i
= 0; i
< 400; i
++)
380 ok( _hwrite( filehandle
, sillytext
, strlen( sillytext
) ) != -1, "_hwrite complains\n" );
382 ok( _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
) != -1, "should be able to seek\n" );
383 ok( _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
) != -1, "should be able to seek\n" );
385 bytes_read
= _hread( filehandle
, buffer
, 1);
386 ok( 1 == bytes_read
, "file read size error\n" );
387 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
388 ok( _llseek( filehandle
, -400 * (LONG
)strlen( sillytext
), FILE_END
) != -1, "should be able to seek\n" );
390 bytes_read
= _hread( filehandle
, buffer
, 1);
391 ok( 1 == bytes_read
, "file read size error\n" );
392 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
393 ok( _llseek( filehandle
, 1000000, FILE_END
) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
394 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
396 ret
= DeleteFileA( filename
);
397 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
401 static void test__llopen( void )
408 filehandle
= _lcreat( filename
, 0 );
409 if (filehandle
== HFILE_ERROR
)
411 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
415 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
416 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
418 filehandle
= _lopen( filename
, OF_READ
);
419 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
420 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
421 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
422 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
424 filehandle
= _lopen( filename
, OF_READWRITE
);
425 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
426 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
427 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
428 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
430 filehandle
= _lopen( filename
, OF_WRITE
);
431 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
432 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
433 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
435 ret
= DeleteFileA( filename
);
436 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
437 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
441 static void test__lread( void )
450 filehandle
= _lcreat( filename
, 0 );
451 if (filehandle
== HFILE_ERROR
)
453 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
457 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
459 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
461 filehandle
= _lopen( filename
, OF_READ
);
463 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError());
465 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
467 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
469 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
471 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
472 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
473 for (i
= 0; i
< bytes_wanted
; i
++)
475 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
479 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
481 ret
= DeleteFileA( filename
);
482 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
486 static void test__lwrite( void )
495 HLOCAL memory_object
;
499 filehandle
= _lcreat( filename
, 0 );
500 if (filehandle
== HFILE_ERROR
)
502 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
506 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
508 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
510 filehandle
= _lopen( filename
, OF_READ
);
512 bytes_read
= _hread( filehandle
, buffer
, 1);
514 ok( 0 == bytes_read
, "file read size error\n" );
516 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
518 filehandle
= _lopen( filename
, OF_READWRITE
);
522 srand( (unsigned)time( NULL
) );
523 for (blocks
= 0; blocks
< 100; blocks
++)
525 for (i
= 0; i
< (INT
)sizeof( buffer
); i
++)
528 checksum
[0] = checksum
[0] + buffer
[i
];
530 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
531 bytes_written
= bytes_written
+ sizeof( buffer
);
534 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
537 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
539 memory_object
= LocalAlloc( LPTR
, bytes_written
);
541 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
543 contents
= LocalLock( memory_object
);
545 filehandle
= _lopen( filename
, OF_READ
);
547 contents
= LocalLock( memory_object
);
549 ok( NULL
!= contents
, "LocalLock whines\n" );
551 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
557 checksum
[0] += contents
[i
];
560 while (i
< bytes_written
- 1);
562 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
564 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
566 ret
= DeleteFileA( filename
);
567 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
569 LocalFree( contents
);
572 static void test_CopyFileA(void)
574 char temp_path
[MAX_PATH
];
575 char source
[MAX_PATH
], dest
[MAX_PATH
];
576 static const char prefix
[] = "pfx";
584 ret
= GetTempPathA(MAX_PATH
, temp_path
);
585 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
586 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
588 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
589 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
591 ret
= MoveFileA(source
, source
);
592 todo_wine
ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
594 /* make the source have not zero size */
595 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
596 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
597 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
598 ok( retok
&& ret
== sizeof(prefix
),
599 "WriteFile error %d\n", GetLastError());
600 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
601 /* get the file time and change it to prove the difference */
602 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
603 ok( ret
, "GetFileTime error %d\n", GetLastError());
604 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
605 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
606 ok( ret
, "SetFileTime error %d\n", GetLastError());
607 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
610 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
611 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
613 SetLastError(0xdeadbeef);
614 ret
= CopyFileA(source
, dest
, TRUE
);
615 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
616 "CopyFileA: unexpected error %d\n", GetLastError());
618 ret
= CopyFileA(source
, dest
, FALSE
);
619 ok(ret
, "CopyFileA: error %d\n", GetLastError());
621 /* make sure that destination has correct size */
622 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
623 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
624 ret
= GetFileSize(hfile
, NULL
);
625 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
627 /* make sure that destination has the same filetime */
628 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
629 ok( ret
, "GetFileTime error %d\n", GetLastError());
630 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
632 SetLastError(0xdeadbeef);
633 ret
= CopyFileA(source
, dest
, FALSE
);
634 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
635 "CopyFileA: ret = %d, unexpected error %d\n", ret
, GetLastError());
637 /* make sure that destination still has correct size */
638 ret
= GetFileSize(hfile
, NULL
);
639 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
640 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
641 ok( retok
&& ret
== sizeof(prefix
),
642 "ReadFile: error %d\n", GetLastError());
643 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
645 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
646 hmapfile
= CreateFileMapping(hfile
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 0, NULL
);
647 ok(hmapfile
!= NULL
, "CreateFileMapping: error %d\n", GetLastError());
649 ret
= CopyFileA(source
, dest
, FALSE
);
650 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
651 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
653 CloseHandle(hmapfile
);
656 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
657 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
659 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
660 hmapfile
= CreateFileMapping(hfile
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 0, NULL
);
661 ok(hmapfile
!= NULL
, "CreateFileMapping: error %d\n", GetLastError());
663 ret
= CopyFileA(source
, dest
, FALSE
);
664 ok(!ret
, "CopyFileA: expected failure\n");
665 ok(GetLastError() == ERROR_USER_MAPPED_FILE
||
666 broken(GetLastError() == ERROR_SHARING_VIOLATION
), /* Win9x */
667 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
669 CloseHandle(hmapfile
);
672 ret
= DeleteFileA(source
);
673 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
674 ret
= DeleteFileA(dest
);
675 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
678 static void test_CopyFileW(void)
680 WCHAR temp_path
[MAX_PATH
];
681 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
682 static const WCHAR prefix
[] = {'p','f','x',0};
685 ret
= GetTempPathW(MAX_PATH
, temp_path
);
686 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
688 win_skip("GetTempPathW is not available\n");
691 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
692 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
694 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
695 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
697 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
698 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
700 ret
= CopyFileW(source
, dest
, TRUE
);
701 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
702 "CopyFileW: unexpected error %d\n", GetLastError());
704 ret
= CopyFileW(source
, dest
, FALSE
);
705 ok(ret
, "CopyFileW: error %d\n", GetLastError());
707 ret
= DeleteFileW(source
);
708 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
709 ret
= DeleteFileW(dest
);
710 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
715 * Debugging routine to dump a buffer in a hexdump-like fashion.
717 static void dumpmem(unsigned char *mem
, int len
)
728 p
+= sprintf(p
, "%02hhx ", mem
[x
]);
729 *c
++ = (mem
[x
] >= 32 && mem
[x
] <= 127) ? mem
[x
] : '.';
730 } while (++x
% 16 && x
< len
);
732 trace("%04x: %-48s- %s\n", x
, hex
, txt
);
736 static void test_CreateFileA(void)
739 char temp_path
[MAX_PATH
], dirname
[MAX_PATH
];
740 char filename
[MAX_PATH
];
741 static const char prefix
[] = "pfx";
742 char windowsdir
[MAX_PATH
];
743 char Volume_1
[MAX_PATH
];
744 unsigned char buffer
[512];
745 char directory
[] = "removeme";
746 static const char nt_drive
[] = "\\\\?\\A:";
748 struct test_list p
[] = {
749 {"", ERROR_PATH_NOT_FOUND
, -1, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* dir as file w \ */
750 {"", ERROR_SUCCESS
, ERROR_PATH_NOT_FOUND
, FILE_FLAG_BACKUP_SEMANTICS
, FALSE
}, /* dir as dir w \ */
751 {"a", ERROR_FILE_NOT_FOUND
, -1, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* non-exist file */
752 {"a\\", ERROR_FILE_NOT_FOUND
, ERROR_PATH_NOT_FOUND
, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* non-exist dir */
753 {"removeme", ERROR_ACCESS_DENIED
, -1, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* exist dir w/o \ */
754 {"removeme\\", ERROR_PATH_NOT_FOUND
, -1, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* exst dir w \ */
755 {"c:", ERROR_ACCESS_DENIED
, ERROR_PATH_NOT_FOUND
, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* device in file namespace */
756 {"c:", ERROR_SUCCESS
, ERROR_PATH_NOT_FOUND
, FILE_FLAG_BACKUP_SEMANTICS
, FALSE
}, /* device in file namespace as dir */
757 {"c:\\", ERROR_PATH_NOT_FOUND
, ERROR_ACCESS_DENIED
, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* root dir w \ */
758 {"c:\\", ERROR_SUCCESS
, ERROR_ACCESS_DENIED
, FILE_FLAG_BACKUP_SEMANTICS
, FALSE
}, /* root dir w \ as dir */
759 {"\\\\?\\c:", ERROR_SUCCESS
, ERROR_BAD_NETPATH
, FILE_ATTRIBUTE_NORMAL
,FALSE
}, /* dev namespace drive */
760 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND
, ERROR_BAD_NETPATH
, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* dev namespace drive w \ */
761 {NULL
, 0, -1, 0, FALSE
}
763 BY_HANDLE_FILE_INFORMATION Finfo
;
765 ret
= GetTempPathA(MAX_PATH
, temp_path
);
766 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
767 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
769 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
770 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
772 SetLastError(0xdeadbeef);
773 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
774 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
775 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
776 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
778 SetLastError(0xdeadbeef);
779 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
780 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
781 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
782 "hFile %p, last error %u\n", hFile
, GetLastError());
786 SetLastError(0xdeadbeef);
787 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
788 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
789 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
790 "hFile %p, last error %u\n", hFile
, GetLastError());
794 ret
= DeleteFileA(filename
);
795 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
797 SetLastError(0xdeadbeef);
798 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
799 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
800 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
801 "hFile %p, last error %u\n", hFile
, GetLastError());
805 ret
= DeleteFileA(filename
);
806 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
808 /* get windows drive letter */
809 ret
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
810 ok(ret
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
811 ok(ret
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
813 /* test error return codes from CreateFile for some cases */
814 ret
= GetTempPathA(MAX_PATH
, temp_path
);
815 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
816 strcpy(dirname
, temp_path
);
817 strcat(dirname
, directory
);
818 ret
= CreateDirectory(dirname
, NULL
);
819 ok( ret
, "Createdirectory failed, gle=%d\n", GetLastError() );
820 /* set current drive & directory to known location */
821 SetCurrentDirectoryA( temp_path
);
826 /* update the drive id in the table entry with the current one */
827 if (p
[i
].file
[1] == ':')
829 strcpy(filename
, p
[i
].file
);
830 filename
[0] = windowsdir
[0];
832 else if (p
[i
].file
[0] == '\\' && p
[i
].file
[5] == ':')
834 strcpy(filename
, p
[i
].file
);
835 filename
[4] = windowsdir
[0];
839 /* prefix the table entry with the current temp directory */
840 strcpy(filename
, temp_path
);
841 strcat(filename
, p
[i
].file
);
843 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
844 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
846 p
[i
].options
, NULL
);
847 /* if we get ACCESS_DENIED when we do not expect it, assume
848 * no access to the volume
850 if (hFile
== INVALID_HANDLE_VALUE
&&
851 GetLastError() == ERROR_ACCESS_DENIED
&&
852 p
[i
].err
!= ERROR_ACCESS_DENIED
)
855 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename
, GetLastError(), p
[i
].err
);
857 skip("Do not have authority to access volumes. Test for %s skipped\n", filename
);
859 /* otherwise validate results with expectations */
860 else if (p
[i
].todo_flag
)
862 (hFile
== INVALID_HANDLE_VALUE
&&
863 (p
[i
].err
== GetLastError() || p
[i
].err2
== GetLastError())) ||
864 (hFile
!= INVALID_HANDLE_VALUE
&& p
[i
].err
== ERROR_SUCCESS
),
865 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
866 filename
, hFile
, GetLastError(), p
[i
].err
);
869 (hFile
== INVALID_HANDLE_VALUE
&&
870 (p
[i
].err
== GetLastError() || p
[i
].err2
== GetLastError())) ||
871 (hFile
!= INVALID_HANDLE_VALUE
&& p
[i
].err
== ERROR_SUCCESS
),
872 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
873 filename
, hFile
, GetLastError(), p
[i
].err
);
874 if (hFile
!= INVALID_HANDLE_VALUE
)
875 CloseHandle( hFile
);
878 ret
= RemoveDirectoryA(dirname
);
879 ok(ret
, "RemoveDirectoryA: error %d\n", GetLastError());
882 /* test opening directory as a directory */
883 hFile
= CreateFileA( temp_path
, GENERIC_READ
,
887 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
888 if (hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() != ERROR_PATH_NOT_FOUND
)
890 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_SUCCESS
,
891 "CreateFileA did not work, last error %u on volume <%s>\n",
892 GetLastError(), temp_path
);
894 if (hFile
!= INVALID_HANDLE_VALUE
)
896 ret
= GetFileInformationByHandle( hFile
, &Finfo
);
899 ok(Finfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
,
900 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
901 temp_path
, Finfo
.dwFileAttributes
);
903 CloseHandle( hFile
);
907 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path
);
910 /* *** Test opening volumes/devices using drive letter *** */
912 /* test using drive letter in non-rewrite format without trailing \ */
913 /* this should work */
914 strcpy(filename
, nt_drive
);
915 filename
[4] = windowsdir
[0];
916 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
917 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
919 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
920 if (hFile
!= INVALID_HANDLE_VALUE
||
921 (GetLastError() != ERROR_ACCESS_DENIED
&& GetLastError() != ERROR_BAD_NETPATH
))
923 /* if we have adm rights to volume, then try rest of tests */
924 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA did not open %s, last error=%u\n",
925 filename
, GetLastError());
926 if (hFile
!= INVALID_HANDLE_VALUE
)
928 /* if we opened the volume/device, try to read it. Since it */
929 /* opened, we should be able to read it. We don't care about*/
930 /* what the data is at this time. */
932 ret
= ReadFile( hFile
, buffer
, len
, &len
, NULL
);
933 todo_wine
ok(ret
, "Failed to read volume, last error %u, %u, for %s\n",
934 GetLastError(), ret
, filename
);
937 trace("buffer is\n");
940 CloseHandle( hFile
);
943 /* test using drive letter with trailing \ and in non-rewrite */
944 /* this should not work */
945 strcpy(filename
, nt_drive
);
946 filename
[4] = windowsdir
[0];
947 strcat( filename
, "\\" );
948 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
949 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
951 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
953 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
954 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
955 filename
, GetLastError());
956 if (hFile
!= INVALID_HANDLE_VALUE
)
957 CloseHandle( hFile
);
959 /* test using temp path with trailing \ and in non-rewrite as dir */
960 /* this should work */
961 strcpy(filename
, nt_drive
);
963 strcat( filename
, temp_path
);
964 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
965 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
967 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
968 ok(hFile
!= INVALID_HANDLE_VALUE
,
969 "CreateFileA should have worked on %s, but got %u\n",
970 filename
, GetLastError());
971 if (hFile
!= INVALID_HANDLE_VALUE
)
972 CloseHandle( hFile
);
974 /* test using drive letter without trailing \ and in device ns */
975 /* this should work */
976 strcpy(filename
, nt_drive
);
977 filename
[4] = windowsdir
[0];
979 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
980 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
982 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
983 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA did not open %s, last error=%u\n",
984 filename
, GetLastError());
985 if (hFile
!= INVALID_HANDLE_VALUE
)
986 CloseHandle( hFile
);
988 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
989 else if (GetLastError() == ERROR_BAD_NETPATH
)
990 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
992 skip("Do not have authority to access volumes. Tests skipped\n");
995 /* *** Test opening volumes/devices using GUID *** */
997 if (pGetVolumeNameForVolumeMountPointA
)
999 strcpy(filename
, "c:\\");
1000 filename
[0] = windowsdir
[0];
1001 ret
= pGetVolumeNameForVolumeMountPointA( filename
, Volume_1
, MAX_PATH
);
1002 ok(ret
, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename
, GetLastError());
1005 ok(strlen(Volume_1
) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1
);
1007 /* test the result of opening a unique volume name (GUID)
1008 * with the trailing \
1009 * this should error out
1011 strcpy(filename
, Volume_1
);
1012 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1013 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1014 NULL
, OPEN_EXISTING
,
1015 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
1017 ok(hFile
== INVALID_HANDLE_VALUE
,
1018 "CreateFileA should not have opened %s, hFile %p\n",
1021 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1022 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1023 filename
, GetLastError());
1024 if (hFile
!= INVALID_HANDLE_VALUE
)
1025 CloseHandle( hFile
);
1027 /* test the result of opening a unique volume name (GUID)
1028 * with the temp path string as dir
1031 strcpy(filename
, Volume_1
);
1032 strcat(filename
, temp_path
+3);
1033 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1034 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1035 NULL
, OPEN_EXISTING
,
1036 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
1038 ok(hFile
!= INVALID_HANDLE_VALUE
,
1039 "CreateFileA should have opened %s, but got %u\n",
1040 filename
, GetLastError());
1041 if (hFile
!= INVALID_HANDLE_VALUE
)
1042 CloseHandle( hFile
);
1044 /* test the result of opening a unique volume name (GUID)
1045 * without the trailing \ and in device namespace
1048 strcpy(filename
, Volume_1
);
1051 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1052 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1053 NULL
, OPEN_EXISTING
,
1054 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
1055 if (hFile
!= INVALID_HANDLE_VALUE
|| GetLastError() != ERROR_ACCESS_DENIED
)
1057 /* if we have adm rights to volume, then try rest of tests */
1058 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA did not open %s, last error=%u\n",
1059 filename
, GetLastError());
1060 if (hFile
!= INVALID_HANDLE_VALUE
)
1062 /* if we opened the volume/device, try to read it. Since it */
1063 /* opened, we should be able to read it. We don't care about*/
1064 /* what the data is at this time. */
1066 ret
= ReadFile( hFile
, buffer
, len
, &len
, NULL
);
1067 todo_wine
ok(ret
, "Failed to read volume, last error %u, %u, for %s\n",
1068 GetLastError(), ret
, filename
);
1071 trace("buffer is\n");
1072 dumpmem(buffer
, 64);
1074 CloseHandle( hFile
);
1078 skip("Do not have authority to access volumes. Tests skipped\n");
1081 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1084 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1087 static void test_CreateFileW(void)
1090 WCHAR temp_path
[MAX_PATH
];
1091 WCHAR filename
[MAX_PATH
];
1092 static const WCHAR emptyW
[]={'\0'};
1093 static const WCHAR prefix
[] = {'p','f','x',0};
1094 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1097 ret
= GetTempPathW(MAX_PATH
, temp_path
);
1098 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1100 win_skip("GetTempPathW is not available\n");
1103 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
1104 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1106 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
1107 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
1109 SetLastError(0xdeadbeef);
1110 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
1111 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
1112 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
1113 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1115 SetLastError(0xdeadbeef);
1116 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1117 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1118 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1119 "hFile %p, last error %u\n", hFile
, GetLastError());
1123 SetLastError(0xdeadbeef);
1124 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1125 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1126 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1127 "hFile %p, last error %u\n", hFile
, GetLastError());
1131 ret
= DeleteFileW(filename
);
1132 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1134 SetLastError(0xdeadbeef);
1135 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1136 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1137 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
1138 "hFile %p, last error %u\n", hFile
, GetLastError());
1142 ret
= DeleteFileW(filename
);
1143 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1147 /* this crashes on NT4.0 */
1148 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
1149 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
1150 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1151 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile
,GetLastError());
1154 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
1155 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
1156 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1157 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile
,GetLastError());
1159 /* test the result of opening a nonexistent driver name */
1160 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1161 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1162 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
1163 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile
,GetLastError());
1165 ret
= CreateDirectoryW(filename
, NULL
);
1166 ok(ret
== TRUE
, "couldn't create temporary directory\n");
1167 hFile
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1168 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
1169 ok(hFile
!= INVALID_HANDLE_VALUE
,
1170 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1172 ret
= RemoveDirectoryW(filename
);
1173 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1176 static void test_GetTempFileNameA(void)
1180 char expected
[MAX_PATH
+ 10];
1181 char windowsdir
[MAX_PATH
+ 10];
1182 char windowsdrive
[3];
1184 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1185 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
1186 ok(result
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
1188 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1189 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
1191 strcat(windowsdir
, "\\");
1194 windowsdrive
[0] = windowsdir
[0];
1195 windowsdrive
[1] = windowsdir
[1];
1196 windowsdrive
[2] = '\0';
1198 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
1199 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
1200 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
1201 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1202 windowsdrive
[0], out
);
1204 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
1205 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
1207 strcat(expected
, windowsdir
);
1208 strcat(expected
, "abc2.tmp");
1209 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1213 static void test_DeleteFileA( void )
1217 ret
= DeleteFileA(NULL
);
1218 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
1219 GetLastError() == ERROR_PATH_NOT_FOUND
),
1220 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
1222 ret
= DeleteFileA("");
1223 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
1224 GetLastError() == ERROR_BAD_PATHNAME
),
1225 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
1227 ret
= DeleteFileA("nul");
1228 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
1229 GetLastError() == ERROR_INVALID_PARAMETER
||
1230 GetLastError() == ERROR_ACCESS_DENIED
||
1231 GetLastError() == ERROR_INVALID_FUNCTION
),
1232 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret
,GetLastError());
1235 static void test_DeleteFileW( void )
1238 WCHAR pathW
[MAX_PATH
];
1239 WCHAR pathsubW
[MAX_PATH
];
1240 static const WCHAR dirW
[] = {'d','e','l','e','t','e','f','i','l','e',0};
1241 static const WCHAR subdirW
[] = {'\\','s','u','b',0};
1242 static const WCHAR emptyW
[]={'\0'};
1244 ret
= DeleteFileW(NULL
);
1245 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1247 win_skip("DeleteFileW is not available\n");
1250 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1251 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
1253 ret
= DeleteFileW(emptyW
);
1254 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1255 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
1257 /* test DeleteFile on empty directory */
1258 ret
= GetTempPathW(MAX_PATH
, pathW
);
1259 if (ret
+ sizeof(dirW
)/sizeof(WCHAR
)-1 + sizeof(subdirW
)/sizeof(WCHAR
)-1 >= MAX_PATH
)
1261 ok(0, "MAX_PATH exceeded in constructing paths\n");
1264 lstrcatW(pathW
, dirW
);
1265 lstrcpyW(pathsubW
, pathW
);
1266 lstrcatW(pathsubW
, subdirW
);
1267 ret
= CreateDirectoryW(pathW
, NULL
);
1268 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
1269 ret
= DeleteFileW(pathW
);
1270 ok(ret
== FALSE
, "DeleteFile should fail for empty directories\n");
1271 ret
= RemoveDirectoryW(pathW
);
1272 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
1274 /* test DeleteFile on non-empty directory */
1275 ret
= CreateDirectoryW(pathW
, NULL
);
1276 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
1277 ret
= CreateDirectoryW(pathsubW
, NULL
);
1278 ok(ret
== TRUE
, "couldn't create directory deletefile\\sub\n");
1279 ret
= DeleteFileW(pathW
);
1280 ok(ret
== FALSE
, "DeleteFile should fail for non-empty directories\n");
1281 ret
= RemoveDirectoryW(pathsubW
);
1282 ok(ret
== TRUE
, "expected to remove directory deletefile\\sub\n");
1283 ret
= RemoveDirectoryW(pathW
);
1284 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
1287 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1289 static void test_MoveFileA(void)
1291 char tempdir
[MAX_PATH
];
1292 char source
[MAX_PATH
], dest
[MAX_PATH
];
1293 static const char prefix
[] = "pfx";
1299 ret
= GetTempPathA(MAX_PATH
, tempdir
);
1300 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
1301 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1303 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
1304 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1306 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
1307 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1309 ret
= MoveFileA(source
, dest
);
1310 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1311 "MoveFileA: unexpected error %d\n", GetLastError());
1313 ret
= DeleteFileA(dest
);
1314 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
1316 hfile
= CreateFileA(source
, GENERIC_READ
| GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0);
1317 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
1319 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
1320 ok( retok
&& ret
== sizeof(prefix
),
1321 "WriteFile error %d\n", GetLastError());
1323 hmapfile
= CreateFileMapping(hfile
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 0, NULL
);
1324 ok(hmapfile
!= NULL
, "CreateFileMapping: error %d\n", GetLastError());
1326 ret
= MoveFileA(source
, dest
);
1328 ok(!ret
, "MoveFileA: expected failure\n");
1329 ok(GetLastError() == ERROR_SHARING_VIOLATION
||
1330 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* Win9x and WinMe */
1331 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1334 CloseHandle(hmapfile
);
1337 /* if MoveFile succeeded, move back to dest */
1338 if (ret
) MoveFile(dest
, source
);
1340 hfile
= CreateFileA(source
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
1341 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
1343 hmapfile
= CreateFileMapping(hfile
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 0, NULL
);
1344 ok(hmapfile
!= NULL
, "CreateFileMapping: error %d\n", GetLastError());
1346 ret
= MoveFileA(source
, dest
);
1348 ok(!ret
, "MoveFileA: expected failure\n");
1349 ok(GetLastError() == ERROR_SHARING_VIOLATION
||
1350 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* Win9x and WinMe */
1351 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1354 CloseHandle(hmapfile
);
1357 /* if MoveFile succeeded, move back to dest */
1358 if (ret
) MoveFile(dest
, source
);
1360 ret
= MoveFileA(source
, dest
);
1361 ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
1363 lstrcatA(tempdir
, "Remove Me");
1364 ret
= CreateDirectoryA(tempdir
, NULL
);
1365 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
1367 lstrcpyA(source
, dest
);
1368 lstrcpyA(dest
, tempdir
);
1369 lstrcatA(dest
, "\\wild?.*");
1370 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1371 ret
= MoveFileA(source
, dest
);
1372 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
1373 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
1374 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
1375 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1376 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
1378 WIN32_FIND_DATAA fd
;
1379 char temppath
[MAX_PATH
];
1382 lstrcpyA(temppath
, tempdir
);
1383 lstrcatA(temppath
, "\\*.*");
1384 hFind
= FindFirstFileA(temppath
, &fd
);
1385 if (INVALID_HANDLE_VALUE
!= hFind
)
1390 lpName
= fd
.cAlternateFileName
;
1392 lpName
= fd
.cFileName
;
1393 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
1395 while (FindNextFileA(hFind
, &fd
));
1399 ret
= DeleteFileA(source
);
1400 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
1401 ret
= DeleteFileA(dest
);
1402 ok(!ret
, "DeleteFileA: error %d\n", GetLastError());
1403 ret
= RemoveDirectoryA(tempdir
);
1404 ok(ret
, "DeleteDirectoryA: error %d\n", GetLastError());
1407 static void test_MoveFileW(void)
1409 WCHAR temp_path
[MAX_PATH
];
1410 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
1411 static const WCHAR prefix
[] = {'p','f','x',0};
1414 ret
= GetTempPathW(MAX_PATH
, temp_path
);
1415 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1417 win_skip("GetTempPathW is not available\n");
1420 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
1421 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1423 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
1424 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
1426 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
1427 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
1429 ret
= MoveFileW(source
, dest
);
1430 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1431 "CopyFileW: unexpected error %d\n", GetLastError());
1433 ret
= DeleteFileW(source
);
1434 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1435 ret
= DeleteFileW(dest
);
1436 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1439 #define PATTERN_OFFSET 0x10
1441 static void test_offset_in_overlapped_structure(void)
1447 BYTE buf
[256], pattern
[] = "TeSt";
1449 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
1452 ret
=GetTempPathA(MAX_PATH
, temp_path
);
1453 ok( ret
, "GetTempPathA error %d\n", GetLastError());
1454 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
1455 ok( ret
, "GetTempFileNameA error %d\n", GetLastError());
1457 /*** Write File *****************************************************/
1459 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
1460 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1462 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
1463 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
1464 ok( ret
, "WriteFile error %d\n", GetLastError());
1465 ok(done
== sizeof(buf
), "expected number of bytes written %u\n", done
);
1467 memset(&ov
, 0, sizeof(ov
));
1468 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1469 S(U(ov
)).OffsetHigh
= 0;
1470 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1471 /* Win 9x does not support the overlapped I/O on files */
1472 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1473 ok(rc
, "WriteFile error %d\n", GetLastError());
1474 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1475 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1476 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1478 S(U(ov
)).Offset
= sizeof(buf
) * 2;
1479 S(U(ov
)).OffsetHigh
= 0;
1480 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1481 ok( ret
, "WriteFile error %d\n", GetLastError());
1482 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1483 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1484 ok(offset
== sizeof(buf
) * 2 + sizeof(pattern
), "wrong file offset %d\n", offset
);
1489 /*** Read File *****************************************************/
1491 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1492 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1494 memset(buf
, 0, sizeof(buf
));
1495 memset(&ov
, 0, sizeof(ov
));
1496 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1497 S(U(ov
)).OffsetHigh
= 0;
1498 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
1499 /* Win 9x does not support the overlapped I/O on files */
1500 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1501 ok(rc
, "ReadFile error %d\n", GetLastError());
1502 ok(done
== sizeof(pattern
), "expected number of bytes read %u\n", done
);
1503 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1504 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1505 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
1510 ret
= DeleteFileA(temp_fname
);
1511 ok( ret
, "DeleteFileA error %d\n", GetLastError());
1514 static void test_LockFile(void)
1518 OVERLAPPED overlapped
;
1519 int limited_LockFile
;
1520 int limited_UnLockFile
;
1523 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1524 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1525 CREATE_ALWAYS
, 0, 0 );
1526 if (handle
== INVALID_HANDLE_VALUE
)
1528 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1531 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
1533 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1534 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1536 limited_UnLockFile
= 0;
1537 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1539 limited_UnLockFile
= 1;
1542 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1543 /* overlapping locks must fail */
1544 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1545 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1546 /* non-overlapping locks must succeed */
1547 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1549 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1550 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1551 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1552 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1554 S(U(overlapped
)).Offset
= 100;
1555 S(U(overlapped
)).OffsetHigh
= 0;
1556 overlapped
.hEvent
= 0;
1558 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1559 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1561 /* LockFileEx is probably OK, test it more. */
1562 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1563 "LockFileEx 100,100 failed\n" );
1566 /* overlapping shared locks are OK */
1567 S(U(overlapped
)).Offset
= 150;
1568 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1570 /* but exclusive is not */
1571 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1572 0, 50, 0, &overlapped
),
1573 "LockFileEx exclusive 150,50 succeeded\n" );
1574 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1575 { /* UnLockFile is capable. */
1576 S(U(overlapped
)).Offset
= 100;
1577 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1578 "UnlockFileEx 150,100 again succeeded\n" );
1581 ret
= LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 );
1584 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1585 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1586 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1589 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong LockFile error %u\n", GetLastError() );
1591 /* wrap-around lock should not do anything */
1592 /* (but still succeeds on NT4 so we don't check result) */
1593 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1595 limited_LockFile
= 0;
1596 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1598 limited_LockFile
= 1;
1601 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1603 /* zero-byte lock */
1604 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1605 if (!limited_LockFile
) ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1606 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1607 if (!limited_LockFile
) ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1609 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1610 ok( !UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 succeeded\n" );
1612 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1614 CloseHandle( handle
);
1615 DeleteFileA( filename
);
1618 static BOOL
create_fake_dll( LPCSTR filename
)
1620 IMAGE_DOS_HEADER
*dos
;
1621 IMAGE_NT_HEADERS
*nt
;
1622 IMAGE_SECTION_HEADER
*sec
;
1624 DWORD lfanew
= sizeof(*dos
);
1625 DWORD size
= lfanew
+ sizeof(*nt
) + sizeof(*sec
);
1629 HANDLE file
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1630 if (file
== INVALID_HANDLE_VALUE
) return FALSE
;
1632 buffer
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
1634 dos
= (IMAGE_DOS_HEADER
*)buffer
;
1635 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
1636 dos
->e_cblp
= sizeof(*dos
);
1638 dos
->e_cparhdr
= lfanew
/ 16;
1639 dos
->e_minalloc
= 0;
1640 dos
->e_maxalloc
= 0xffff;
1643 dos
->e_lfarlc
= lfanew
;
1644 dos
->e_lfanew
= lfanew
;
1646 nt
= (IMAGE_NT_HEADERS
*)(buffer
+ lfanew
);
1647 nt
->Signature
= IMAGE_NT_SIGNATURE
;
1648 #if defined __i386__
1649 nt
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
1650 #elif defined __x86_64__
1651 nt
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_AMD64
;
1652 #elif defined __powerpc__
1653 nt
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_POWERPC
;
1655 # error You must specify the machine type
1657 nt
->FileHeader
.NumberOfSections
= 1;
1658 nt
->FileHeader
.SizeOfOptionalHeader
= IMAGE_SIZEOF_NT_OPTIONAL_HEADER
;
1659 nt
->FileHeader
.Characteristics
= IMAGE_FILE_DLL
| IMAGE_FILE_EXECUTABLE_IMAGE
;
1660 nt
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
1661 nt
->OptionalHeader
.MajorLinkerVersion
= 1;
1662 nt
->OptionalHeader
.MinorLinkerVersion
= 0;
1663 nt
->OptionalHeader
.ImageBase
= 0x10000000;
1664 nt
->OptionalHeader
.SectionAlignment
= 0x1000;
1665 nt
->OptionalHeader
.FileAlignment
= 0x1000;
1666 nt
->OptionalHeader
.MajorOperatingSystemVersion
= 1;
1667 nt
->OptionalHeader
.MinorOperatingSystemVersion
= 0;
1668 nt
->OptionalHeader
.MajorImageVersion
= 1;
1669 nt
->OptionalHeader
.MinorImageVersion
= 0;
1670 nt
->OptionalHeader
.MajorSubsystemVersion
= 4;
1671 nt
->OptionalHeader
.MinorSubsystemVersion
= 0;
1672 nt
->OptionalHeader
.SizeOfImage
= 0x2000;
1673 nt
->OptionalHeader
.SizeOfHeaders
= size
;
1674 nt
->OptionalHeader
.Subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
1675 nt
->OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
1677 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
1678 memcpy( sec
->Name
, ".rodata", sizeof(".rodata") );
1679 sec
->Misc
.VirtualSize
= 0x1000;
1680 sec
->VirtualAddress
= 0x1000;
1681 sec
->SizeOfRawData
= 0;
1682 sec
->PointerToRawData
= 0;
1683 sec
->Characteristics
= IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
;
1685 ret
= WriteFile( file
, buffer
, size
, &written
, NULL
) && written
== size
;
1686 HeapFree( GetProcessHeap(), 0, buffer
);
1687 CloseHandle( file
);
1691 static int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
, BOOL is_win9x
)
1695 if (!access1
) sharing1
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1696 if (!access2
) sharing2
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1701 if (!access1
) access1
= GENERIC_READ
;
1704 if (!access2
) access2
= GENERIC_READ
;
1707 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1708 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1709 if ((access1
& DELETE
) && !(sharing2
& FILE_SHARE_DELETE
)) return 0;
1710 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1711 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1712 if ((access2
& DELETE
) && !(sharing1
& FILE_SHARE_DELETE
)) return 0;
1716 static int is_sharing_map_compatible( DWORD map_access
, DWORD access2
, DWORD sharing2
)
1718 if ((map_access
== PAGE_READWRITE
|| map_access
== PAGE_EXECUTE_READWRITE
) &&
1719 !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1720 if ((map_access
& SEC_IMAGE
) && (access2
& GENERIC_WRITE
)) return 0;
1724 static void test_file_sharing(void)
1726 static const DWORD access_modes
[] =
1727 { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
,
1728 DELETE
, GENERIC_READ
|DELETE
, GENERIC_WRITE
|DELETE
, GENERIC_READ
|GENERIC_WRITE
|DELETE
};
1729 static const DWORD sharing_modes
[] =
1730 { 0, FILE_SHARE_READ
,
1731 FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1732 FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_DELETE
,
1733 FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
};
1734 static const DWORD mapping_modes
[] =
1735 { PAGE_READONLY
, PAGE_WRITECOPY
, PAGE_READWRITE
, SEC_IMAGE
| PAGE_WRITECOPY
};
1739 BOOL is_win9x
= FALSE
;
1741 /* make sure the file exists */
1742 if (!create_fake_dll( filename
))
1744 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError());
1747 is_win9x
= GetFileAttributesW(filenameW
) == INVALID_FILE_ATTRIBUTES
;
1749 for (a1
= 0; a1
< sizeof(access_modes
)/sizeof(access_modes
[0]); a1
++)
1751 for (s1
= 0; s1
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s1
++)
1753 /* Win9x doesn't support FILE_SHARE_DELETE */
1754 if (is_win9x
&& (sharing_modes
[s1
] & FILE_SHARE_DELETE
))
1757 SetLastError(0xdeadbeef);
1758 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1759 NULL
, OPEN_EXISTING
, 0, 0 );
1760 if (h
== INVALID_HANDLE_VALUE
)
1762 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1765 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1767 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1769 /* Win9x doesn't support FILE_SHARE_DELETE */
1770 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1773 SetLastError(0xdeadbeef);
1774 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1775 NULL
, OPEN_EXISTING
, 0, 0 );
1776 ret
= GetLastError();
1777 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1778 access_modes
[a2
], sharing_modes
[s2
], is_win9x
))
1780 ok( h2
!= INVALID_HANDLE_VALUE
,
1781 "open failed for modes %x/%x/%x/%x\n",
1782 access_modes
[a1
], sharing_modes
[s1
],
1783 access_modes
[a2
], sharing_modes
[s2
] );
1784 ok( ret
== 0xdeadbeef /* Win9x */ ||
1786 "wrong error code %d\n", ret
);
1790 ok( h2
== INVALID_HANDLE_VALUE
,
1791 "open succeeded for modes %x/%x/%x/%x\n",
1792 access_modes
[a1
], sharing_modes
[s1
],
1793 access_modes
[a2
], sharing_modes
[s2
] );
1794 ok( ret
== ERROR_SHARING_VIOLATION
,
1795 "wrong error code %d\n", ret
);
1797 if (h2
!= INVALID_HANDLE_VALUE
) CloseHandle( h2
);
1804 for (a1
= 0; a1
< sizeof(mapping_modes
)/sizeof(mapping_modes
[0]); a1
++)
1808 create_fake_dll( filename
);
1809 SetLastError(0xdeadbeef);
1810 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1811 if (h
== INVALID_HANDLE_VALUE
)
1813 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1816 m
= CreateFileMappingA( h
, NULL
, mapping_modes
[a1
], 0, 0, NULL
);
1817 ok( m
!= 0, "failed to create mapping %x err %u\n", mapping_modes
[a1
], GetLastError() );
1821 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1823 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1825 /* Win9x doesn't support FILE_SHARE_DELETE */
1826 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1829 SetLastError(0xdeadbeef);
1830 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1831 NULL
, OPEN_EXISTING
, 0, 0 );
1833 ret
= GetLastError();
1834 if (h2
== INVALID_HANDLE_VALUE
)
1836 if (is_sharing_map_compatible(mapping_modes
[a1
], access_modes
[a2
], sharing_modes
[s2
]))
1837 ok( is_win9x
, /* there's no sharing at all with a mapping on win9x */
1838 "open failed for modes map %x/%x/%x\n",
1839 mapping_modes
[a1
], access_modes
[a2
], sharing_modes
[s2
] );
1840 ok( ret
== ERROR_SHARING_VIOLATION
,
1841 "wrong error code %d\n", ret
);
1845 if (!is_sharing_map_compatible(mapping_modes
[a1
], access_modes
[a2
], sharing_modes
[s2
]))
1846 ok( broken(1), /* no checking on nt4 */
1847 "open succeeded for modes map %x/%x/%x\n",
1848 mapping_modes
[a1
], access_modes
[a2
], sharing_modes
[s2
] );
1849 ok( ret
== 0xdeadbeef /* Win9x */ ||
1851 "wrong error code %d\n", ret
);
1857 /* try CREATE_ALWAYS over an existing mapping */
1858 SetLastError(0xdeadbeef);
1859 h2
= CreateFileA( filename
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1860 NULL
, CREATE_ALWAYS
, 0, 0 );
1861 ret
= GetLastError();
1862 if ((mapping_modes
[a1
] & SEC_IMAGE
) || is_win9x
)
1864 ok( h2
== INVALID_HANDLE_VALUE
, "create succeeded for map %x\n", mapping_modes
[a1
] );
1865 ok( ret
== ERROR_SHARING_VIOLATION
, "wrong error code %d for %x\n", ret
, mapping_modes
[a1
] );
1869 ok( h2
== INVALID_HANDLE_VALUE
, "create succeeded for map %x\n", mapping_modes
[a1
] );
1870 ok( ret
== ERROR_USER_MAPPED_FILE
, "wrong error code %d for %x\n", ret
, mapping_modes
[a1
] );
1872 if (h2
!= INVALID_HANDLE_VALUE
) CloseHandle( h2
);
1874 /* try DELETE_ON_CLOSE over an existing mapping */
1875 SetLastError(0xdeadbeef);
1876 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1877 NULL
, OPEN_EXISTING
, FILE_FLAG_DELETE_ON_CLOSE
, 0 );
1878 ret
= GetLastError();
1881 ok( h2
== INVALID_HANDLE_VALUE
, "create succeeded for map %x\n", mapping_modes
[a1
] );
1882 ok( ret
== ERROR_SHARING_VIOLATION
, "wrong error code %d for %x\n", ret
, mapping_modes
[a1
] );
1884 else if (mapping_modes
[a1
] & SEC_IMAGE
)
1886 ok( h2
== INVALID_HANDLE_VALUE
, "create succeeded for map %x\n", mapping_modes
[a1
] );
1887 ok( ret
== ERROR_ACCESS_DENIED
, "wrong error code %d for %x\n", ret
, mapping_modes
[a1
] );
1891 ok( h2
!= INVALID_HANDLE_VALUE
, "open failed for map %x err %u\n", mapping_modes
[a1
], ret
);
1893 if (h2
!= INVALID_HANDLE_VALUE
) CloseHandle( h2
);
1898 SetLastError(0xdeadbeef);
1899 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1900 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1902 SetLastError(0xdeadbeef);
1903 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1904 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1905 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %d\n", GetLastError() );
1907 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1908 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1913 DeleteFileA( filename
);
1916 static char get_windows_drive(void)
1918 char windowsdir
[MAX_PATH
];
1919 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1920 return windowsdir
[0];
1923 static void test_FindFirstFileA(void)
1926 WIN32_FIND_DATAA data
;
1928 char buffer
[5] = "C:\\";
1930 char nonexistent
[MAX_PATH
];
1932 /* try FindFirstFileA on "C:\" */
1933 buffer
[0] = get_windows_drive();
1935 SetLastError( 0xdeadbeaf );
1936 handle
= FindFirstFileA(buffer
, &data
);
1937 err
= GetLastError();
1938 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should fail\n" );
1939 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1941 /* try FindFirstFileA on "C:\*" */
1942 strcpy(buffer2
, buffer
);
1943 strcat(buffer2
, "*");
1944 handle
= FindFirstFileA(buffer2
, &data
);
1945 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1946 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1947 "FindFirstFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1948 if (FindNextFileA( handle
, &data
))
1949 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1950 "FindNextFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1951 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1953 /* try FindFirstFileA on windows dir */
1954 GetWindowsDirectory( buffer2
, sizeof(buffer2
) );
1955 strcat(buffer2
, "\\*");
1956 handle
= FindFirstFileA(buffer2
, &data
);
1957 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1958 ok( !strcmp( data
.cFileName
, "." ), "FindFirstFile should return '.' first\n" );
1959 ok( FindNextFileA( handle
, &data
), "FindNextFile failed\n" );
1960 ok( !strcmp( data
.cFileName
, ".." ), "FindNextFile should return '..' as second entry\n" );
1961 while (FindNextFileA( handle
, &data
))
1962 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1963 "FindNextFile shouldn't return '%s'\n", data
.cFileName
);
1964 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1966 /* try FindFirstFileA on "C:\foo\" */
1967 SetLastError( 0xdeadbeaf );
1968 if (!GetTempFileNameA( buffer
, "foo", 0, nonexistent
) && GetLastError() == ERROR_ACCESS_DENIED
)
1971 GetTempPathA( sizeof(tmp
), tmp
);
1972 GetTempFileNameA( tmp
, "foo", 0, nonexistent
);
1974 DeleteFileA( nonexistent
);
1975 strcpy(buffer2
, nonexistent
);
1976 strcat(buffer2
, "\\");
1977 handle
= FindFirstFileA(buffer2
, &data
);
1978 err
= GetLastError();
1979 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1981 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1984 /* try FindFirstFileA on "C:\foo\bar.txt" */
1985 SetLastError( 0xdeadbeaf );
1986 strcpy(buffer2
, nonexistent
);
1987 strcat(buffer2
, "\\bar.txt");
1988 handle
= FindFirstFileA(buffer2
, &data
);
1989 err
= GetLastError();
1990 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1991 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1993 /* try FindFirstFileA on "C:\foo\*.*" */
1994 SetLastError( 0xdeadbeaf );
1995 strcpy(buffer2
, nonexistent
);
1996 strcat(buffer2
, "\\*.*");
1997 handle
= FindFirstFileA(buffer2
, &data
);
1998 err
= GetLastError();
1999 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
2000 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
2002 /* try FindFirstFileA on "foo\bar.txt" */
2003 SetLastError( 0xdeadbeaf );
2004 strcpy(buffer2
, nonexistent
+ 3);
2005 strcat(buffer2
, "\\bar.txt");
2006 handle
= FindFirstFileA(buffer2
, &data
);
2007 err
= GetLastError();
2008 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
2009 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
2011 /* try FindFirstFileA on "c:\nul" */
2012 SetLastError( 0xdeadbeaf );
2013 strcpy(buffer2
, buffer
);
2014 strcat(buffer2
, "nul");
2015 handle
= FindFirstFileA(buffer2
, &data
);
2016 err
= GetLastError();
2017 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
2018 ok( 0 == lstrcmpiA(data
.cFileName
, "nul"), "wrong name %s\n", data
.cFileName
);
2019 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
2020 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
2021 "wrong attributes %x\n", data
.dwFileAttributes
);
2022 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
2024 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
2025 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
2027 SetLastError( 0xdeadbeaf );
2028 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
2029 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
2030 ok( FindClose( handle
), "failed to close handle\n" );
2032 /* try FindFirstFileA on "lpt1" */
2033 SetLastError( 0xdeadbeaf );
2034 strcpy(buffer2
, "lpt1");
2035 handle
= FindFirstFileA(buffer2
, &data
);
2036 err
= GetLastError();
2037 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
2038 ok( 0 == lstrcmpiA(data
.cFileName
, "lpt1"), "wrong name %s\n", data
.cFileName
);
2039 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
2040 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
2041 "wrong attributes %x\n", data
.dwFileAttributes
);
2042 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
2044 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
2045 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
2047 SetLastError( 0xdeadbeaf );
2048 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
2049 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
2050 ok( FindClose( handle
), "failed to close handle\n" );
2052 /* try FindFirstFileA on "c:\nul\*" */
2053 SetLastError( 0xdeadbeaf );
2054 strcpy(buffer2
, buffer
);
2055 strcat(buffer2
, "nul\\*");
2056 handle
= FindFirstFileA(buffer2
, &data
);
2057 err
= GetLastError();
2058 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
2059 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
2061 /* try FindFirstFileA on "c:\nul*" */
2062 SetLastError( 0xdeadbeaf );
2063 strcpy(buffer2
, buffer
);
2064 strcat(buffer2
, "nul*");
2065 handle
= FindFirstFileA(buffer2
, &data
);
2066 err
= GetLastError();
2067 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
2068 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
2070 /* try FindFirstFileA on "c:\foo\bar\nul" */
2071 SetLastError( 0xdeadbeaf );
2072 strcpy(buffer2
, buffer
);
2073 strcat(buffer2
, "foo\\bar\\nul");
2074 handle
= FindFirstFileA(buffer2
, &data
);
2075 err
= GetLastError();
2076 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
2077 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
2079 /* try FindFirstFileA on "c:\foo\nul\bar" */
2080 SetLastError( 0xdeadbeaf );
2081 strcpy(buffer2
, buffer
);
2082 strcat(buffer2
, "foo\\nul\\bar");
2083 handle
= FindFirstFileA(buffer2
, &data
);
2084 err
= GetLastError();
2085 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
2086 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
2089 static void test_FindNextFileA(void)
2092 WIN32_FIND_DATAA search_results
;
2094 char buffer
[5] = "C:\\*";
2096 buffer
[0] = get_windows_drive();
2097 handle
= FindFirstFileA(buffer
,&search_results
);
2098 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
2099 while (FindNextFile(handle
, &search_results
))
2101 /* get to the end of the files */
2103 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
2104 err
= GetLastError();
2105 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
2108 static void test_FindFirstFileExA(FINDEX_SEARCH_OPS search_ops
)
2110 WIN32_FIND_DATAA search_results
;
2114 if (!pFindFirstFileExA
)
2116 win_skip("FindFirstFileExA() is missing\n");
2120 CreateDirectoryA("test-dir", NULL
);
2121 _lclose(_lcreat("test-dir\\file1", 0));
2122 _lclose(_lcreat("test-dir\\file2", 0));
2123 CreateDirectoryA("test-dir\\dir1", NULL
);
2124 SetLastError(0xdeadbeef);
2125 handle
= pFindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, search_ops
, NULL
, 0);
2126 if (handle
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2128 win_skip("FindFirstFileExA is not implemented\n");
2131 ok(handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile failed (err=%u)\n", GetLastError());
2132 ok(strcmp(search_results
.cFileName
, ".") == 0, "First entry should be '.', is %s\n", search_results
.cFileName
);
2134 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2136 ok(FindNextFile(handle
, &search_results
), "Fetching second file failed\n");
2137 ok(strcmp(search_results
.cFileName
, "..") == 0, "Second entry should be '..' is %s\n", search_results
.cFileName
);
2139 ok(FindNextFile(handle
, &search_results
), "Fetching third file failed\n");
2140 ok(CHECK_NAME(search_results
.cFileName
), "Invalid third entry - %s\n", search_results
.cFileName
);
2142 SetLastError(0xdeadbeef);
2143 ret
= FindNextFile(handle
, &search_results
);
2144 if (!ret
&& (GetLastError() == ERROR_NO_MORE_FILES
) && (search_ops
== FindExSearchLimitToDirectories
))
2146 skip("File system supports directory filtering\n");
2147 /* Results from the previous call are not cleared */
2148 ok(strcmp(search_results
.cFileName
, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results
.cFileName
);
2149 FindClose( handle
);
2153 ok(ret
, "Fetching fourth file failed\n");
2154 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
2156 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
2157 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
2161 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should fail\n");
2163 FindClose( handle
);
2166 DeleteFileA("test-dir\\file1");
2167 DeleteFileA("test-dir\\file2");
2168 RemoveDirectoryA("test-dir\\dir1");
2169 RemoveDirectoryA("test-dir");
2172 static int test_Mapfile_createtemp(HANDLE
*handle
)
2174 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
2175 DeleteFile(filename
);
2176 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
2177 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2178 if (*handle
!= INVALID_HANDLE_VALUE
) {
2186 static void test_MapFile(void)
2191 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
2193 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
2194 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
2196 ok( CloseHandle( hmap
), "can't close mapping handle\n");
2198 /* We have to close file before we try new stuff with mapping again.
2199 Else we would always succeed on XP or block descriptors on 95. */
2200 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
2201 ok( hmap
!= NULL
, "We should still be able to map!\n" );
2202 ok( CloseHandle( hmap
), "can't close mapping handle\n");
2203 ok( CloseHandle( handle
), "can't close file handle\n");
2206 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
2208 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
2209 ok( hmap
== NULL
, "mapped zero size file\n");
2210 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
2212 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
2213 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
2214 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2216 CloseHandle( hmap
);
2218 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
2219 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
2220 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2222 CloseHandle( hmap
);
2224 /* On XP you can now map again, on Win 95 you cannot. */
2226 ok( CloseHandle( handle
), "can't close file handle\n");
2227 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
2230 static void test_GetFileType(void)
2233 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
2234 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
2235 type
= GetFileType(h
);
2236 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
2238 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2239 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
2240 type
= GetFileType(h
);
2241 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
2243 DeleteFileA( filename
);
2246 static int completion_count
;
2248 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
2250 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2251 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
2255 static void test_async_file_errors(void)
2257 char szFile
[MAX_PATH
];
2258 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
2260 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
2262 S(U(ovl
)).Offset
= 0;
2263 S(U(ovl
)).OffsetHigh
= 0;
2265 completion_count
= 0;
2267 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
2268 strcat(szFile
, "\\win.ini");
2269 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2270 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
2271 if (hFile
== INVALID_HANDLE_VALUE
) /* win9x doesn't like FILE_SHARE_DELETE */
2272 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2273 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
2274 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA(%s ...) failed\n", szFile
);
2279 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
2281 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
2282 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2285 if (!GetOverlappedResult(hFile
, &ovl
, &count
, FALSE
))
2287 S(U(ovl
)).Offset
+= count
;
2288 /* i/o completion routine only called if ReadFileEx returned success.
2289 * we only care about violations of this rule so undo what should have
2293 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
2294 /*printf("Error = %ld\n", GetLastError());*/
2295 HeapFree(GetProcessHeap(), 0, lpBuffer
);
2298 static void test_read_write(void)
2300 DWORD bytes
, ret
, old_prot
;
2302 char temp_path
[MAX_PATH
];
2303 char filename
[MAX_PATH
];
2305 static const char prefix
[] = "pfx";
2307 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2308 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2309 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2311 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
2312 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
2314 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
2315 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
2316 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
2318 SetLastError(12345678);
2320 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
2321 ok(ret
&& GetLastError() == 12345678,
2322 "ret = %d, error %d\n", ret
, GetLastError());
2323 ok(!bytes
, "bytes = %d\n", bytes
);
2325 SetLastError(12345678);
2327 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
2328 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
2329 (ret
&& GetLastError() == 12345678), /* Win9x */
2330 "ret = %d, error %d\n", ret
, GetLastError());
2331 ok(!bytes
|| /* Win2k */
2332 bytes
== 10, /* Win9x */
2333 "bytes = %d\n", bytes
);
2335 /* make sure the file contains data */
2336 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
2337 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
2339 SetLastError(12345678);
2341 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
2342 ok(ret
&& GetLastError() == 12345678,
2343 "ret = %d, error %d\n", ret
, GetLastError());
2344 ok(!bytes
, "bytes = %d\n", bytes
);
2346 SetLastError(12345678);
2348 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
2349 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
2350 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
2351 "ret = %d, error %d\n", ret
, GetLastError());
2352 ok(!bytes
, "bytes = %d\n", bytes
);
2354 /* test passing protected memory as buffer */
2356 mem
= VirtualAlloc( NULL
, 0x4000, MEM_COMMIT
, PAGE_READWRITE
);
2357 ok( mem
!= NULL
, "failed to allocate virtual mem error %u\n", GetLastError() );
2359 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2360 ok( ret
, "WriteFile failed error %u\n", GetLastError() );
2361 ok( bytes
== 0x4000, "only wrote %x bytes\n", bytes
);
2363 ret
= VirtualProtect( mem
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
2364 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2366 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2367 ok( !ret
, "WriteFile succeeded\n" );
2368 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
2369 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2370 "wrong error %u\n", GetLastError() );
2371 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2373 ret
= WriteFile( (HANDLE
)0xdead, mem
, 0x4000, &bytes
, NULL
);
2374 ok( !ret
, "WriteFile succeeded\n" );
2375 ok( GetLastError() == ERROR_INVALID_HANDLE
|| /* handle is checked before buffer on NT */
2376 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2377 "wrong error %u\n", GetLastError() );
2378 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2380 ret
= VirtualProtect( mem
, 0x2000, PAGE_NOACCESS
, &old_prot
);
2381 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2383 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2384 ok( !ret
, "WriteFile succeeded\n" );
2385 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
2386 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2387 "wrong error %u\n", GetLastError() );
2388 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2390 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
2392 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2393 ok( !ret
, "ReadFile succeeded\n" );
2394 ok( GetLastError() == ERROR_NOACCESS
||
2395 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2396 "wrong error %u\n", GetLastError() );
2397 ok( bytes
== 0, "read %x bytes\n", bytes
);
2399 ret
= VirtualProtect( mem
, 0x2000, PAGE_READONLY
, &old_prot
);
2400 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2402 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2403 ok( !ret
, "ReadFile succeeded\n" );
2404 ok( GetLastError() == ERROR_NOACCESS
||
2405 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2406 "wrong error %u\n", GetLastError() );
2407 ok( bytes
== 0, "read %x bytes\n", bytes
);
2409 ret
= VirtualProtect( mem
, 0x2000, PAGE_READWRITE
, &old_prot
);
2410 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2412 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2413 ok( !ret
, "ReadFile succeeded\n" );
2414 ok( GetLastError() == ERROR_NOACCESS
||
2415 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2416 "wrong error %u\n", GetLastError() );
2417 ok( bytes
== 0, "read %x bytes\n", bytes
);
2419 SetFilePointer( hFile
, 0x1234, NULL
, FILE_BEGIN
);
2420 SetEndOfFile( hFile
);
2421 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
2423 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2424 ok( !ret
, "ReadFile succeeded\n" );
2425 ok( GetLastError() == ERROR_NOACCESS
||
2426 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2427 "wrong error %u\n", GetLastError() );
2428 ok( bytes
== 0, "read %x bytes\n", bytes
);
2430 ret
= ReadFile( hFile
, mem
, 0x2000, &bytes
, NULL
);
2431 ok( ret
, "ReadFile failed error %u\n", GetLastError() );
2432 ok( bytes
== 0x1234, "read %x bytes\n", bytes
);
2434 ret
= ReadFile( hFile
, NULL
, 1, &bytes
, NULL
);
2435 ok( !ret
, "ReadFile succeeded\n" );
2436 ok( GetLastError() == ERROR_NOACCESS
||
2437 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2438 "wrong error %u\n", GetLastError() );
2439 ok( bytes
== 0, "read %x bytes\n", bytes
);
2441 VirtualFree( mem
, 0, MEM_FREE
);
2443 ret
= CloseHandle(hFile
);
2444 ok( ret
, "CloseHandle: error %d\n", GetLastError());
2445 ret
= DeleteFileA(filename
);
2446 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
2449 static void test_OpenFile(void)
2456 static const char file
[] = "regedit.exe";
2457 static const char foo
[] = ".\\foo-bar-foo.baz";
2458 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
2459 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2460 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2461 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2462 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2463 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2464 char buff
[MAX_PATH
];
2465 char buff_long
[4*MAX_PATH
];
2466 char filled_0xA5
[OFS_MAXPATHNAME
];
2470 /* Check for existing file */
2471 if (!pGetSystemWindowsDirectoryA
)
2472 length
= GetWindowsDirectoryA(buff
, MAX_PATH
);
2474 length
= pGetSystemWindowsDirectoryA(buff
, MAX_PATH
);
2476 if (length
+ sizeof(file
) < MAX_PATH
)
2478 p
= buff
+ strlen(buff
);
2479 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2481 memset(&ofs
, 0xA5, sizeof(ofs
));
2482 SetLastError(0xfaceabee);
2484 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
2485 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
2486 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2487 "GetLastError() returns %d\n", GetLastError() );
2488 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2489 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2490 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2491 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2492 ofs
.szPathName
, buff
);
2495 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
2496 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
2498 /* Check for nonexistent file */
2499 if (length
+ sizeof(foo
) < MAX_PATH
)
2501 p
= buff
+ strlen(buff
);
2502 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2503 strcpy( p
, foo
+ 2 );
2504 memset(&ofs
, 0xA5, sizeof(ofs
));
2505 SetLastError(0xfaceabee);
2507 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
2508 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
2509 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
2511 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2512 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2513 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
2514 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2515 ofs
.szPathName
, buff
);
2518 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
2519 length
+= lstrlenA(foo_too_long
+ 1);
2521 /* Check for nonexistent file with too long filename */
2522 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
2524 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
2525 memset(&ofs
, 0xA5, sizeof(ofs
));
2526 SetLastError(0xfaceabee);
2528 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
2529 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
2530 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
2531 "GetLastError() returns %d\n", GetLastError() );
2533 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2534 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
2535 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2536 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
2537 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2541 length
= GetCurrentDirectoryA(MAX_PATH
, buff
) + sizeof(filename
);
2543 if (length
>= MAX_PATH
)
2545 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
2548 p
= buff
+ strlen(buff
);
2549 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2550 strcpy( p
, filename
);
2552 memset(&ofs
, 0xA5, sizeof(ofs
));
2553 SetLastError(0xfaceabee);
2554 /* Create an empty file */
2555 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
2556 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
2557 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2558 "GetLastError() returns %d\n", GetLastError() );
2559 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2560 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2561 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2562 ret
= _lclose(hFile
);
2563 ok( !ret
, "_lclose() returns %d\n", ret
);
2564 retval
= GetFileAttributesA(filename
);
2565 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
2567 memset(&ofs
, 0xA5, sizeof(ofs
));
2568 SetLastError(0xfaceabee);
2569 /* Check various opening options: */
2570 /* for reading only, */
2571 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
2572 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
2573 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2574 "GetLastError() returns %d\n", GetLastError() );
2575 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2576 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2577 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2578 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2579 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2580 ret
= _lclose(hFile
);
2581 ok( !ret
, "_lclose() returns %d\n", ret
);
2583 memset(&ofs
, 0xA5, sizeof(ofs
));
2584 SetLastError(0xfaceabee);
2585 /* for writing only, */
2586 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
2587 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
2588 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2589 "GetLastError() returns %d\n", GetLastError() );
2590 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2591 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2592 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2593 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2594 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2595 ret
= _lclose(hFile
);
2596 ok( !ret
, "_lclose() returns %d\n", ret
);
2598 memset(&ofs
, 0xA5, sizeof(ofs
));
2599 SetLastError(0xfaceabee);
2600 /* for reading and writing, */
2601 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
2602 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
2603 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2604 "GetLastError() returns %d\n", GetLastError() );
2605 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2606 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2607 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2608 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2609 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2610 ret
= _lclose(hFile
);
2611 ok( !ret
, "_lclose() returns %d\n", ret
);
2613 memset(&ofs
, 0xA5, sizeof(ofs
));
2614 SetLastError(0xfaceabee);
2615 /* for checking file presence. */
2616 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
2617 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
2618 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2619 "GetLastError() returns %d\n", GetLastError() );
2620 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2621 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2622 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2623 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2624 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2626 memset(&ofs
, 0xA5, sizeof(ofs
));
2627 SetLastError(0xfaceabee);
2628 /* Delete the file and make sure it doesn't exist anymore */
2629 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
2630 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
2631 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2632 "GetLastError() returns %d\n", GetLastError() );
2633 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2634 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2635 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2636 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2637 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2639 retval
= GetFileAttributesA(filename
);
2640 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
2643 static void test_overlapped(void)
2648 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2649 if (0) /* tested: WinXP */
2651 GetOverlappedResult(0, NULL
, &result
, FALSE
);
2652 GetOverlappedResult(0, &ov
, NULL
, FALSE
);
2653 GetOverlappedResult(0, NULL
, NULL
, FALSE
);
2656 memset( &ov
, 0, sizeof ov
);
2658 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2660 ok( result
== 0, "wrong result %u\n", result
);
2662 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2666 ov
.InternalHigh
= 0xabcd;
2667 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2669 ok( result
== 0xabcd, "wrong result %u\n", result
);
2671 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2673 SetLastError( 0xb00 );
2675 ov
.Internal
= STATUS_INVALID_HANDLE
;
2676 ov
.InternalHigh
= 0xabcd;
2677 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2678 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2679 ok( r
== FALSE
, "should return false\n");
2680 ok( result
== 0xabcd || result
== 0 /* win9x */, "wrong result %u\n", result
);
2682 SetLastError( 0xb00 );
2684 ov
.Internal
= STATUS_PENDING
;
2685 ov
.InternalHigh
= 0xabcd;
2686 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2687 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2688 "wrong error %u\n", GetLastError() );
2689 ok( r
== FALSE
, "should return false\n");
2690 ok( result
== 0, "wrong result %u\n", result
);
2692 SetLastError( 0xb00 );
2693 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
2694 ov
.Internal
= STATUS_PENDING
;
2695 ov
.InternalHigh
= 0xabcd;
2696 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2697 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2698 "wrong error %u\n", GetLastError() );
2699 ok( r
== FALSE
, "should return false\n");
2701 ResetEvent( ov
.hEvent
);
2703 SetLastError( 0xb00 );
2704 ov
.Internal
= STATUS_PENDING
;
2705 ov
.InternalHigh
= 0;
2706 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2707 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2708 "wrong error %u\n", GetLastError() );
2709 ok( r
== FALSE
, "should return false\n");
2711 r
= CloseHandle( ov
.hEvent
);
2712 ok( r
== TRUE
, "close handle failed\n");
2715 static void test_RemoveDirectory(void)
2718 char directory
[] = "removeme";
2720 rc
= CreateDirectory(directory
, NULL
);
2721 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
2723 rc
= SetCurrentDirectory(directory
);
2724 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2726 rc
= RemoveDirectory(".");
2729 rc
= SetCurrentDirectory("..");
2730 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2732 rc
= RemoveDirectory(directory
);
2733 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2737 static BOOL
check_file_time( const FILETIME
*ft1
, const FILETIME
*ft2
, UINT tolerance
)
2739 ULONGLONG t1
= ((ULONGLONG
)ft1
->dwHighDateTime
<< 32) | ft1
->dwLowDateTime
;
2740 ULONGLONG t2
= ((ULONGLONG
)ft2
->dwHighDateTime
<< 32) | ft2
->dwLowDateTime
;
2741 return abs(t1
- t2
) <= tolerance
;
2744 static void test_ReplaceFileA(void)
2746 char replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2747 HANDLE hReplacedFile
, hReplacementFile
, hBackupFile
;
2748 static const char replacedData
[] = "file-to-replace";
2749 static const char replacementData
[] = "new-file";
2750 static const char backupData
[] = "backup-file";
2751 FILETIME ftReplaced
, ftReplacement
, ftBackup
;
2752 static const char prefix
[] = "pfx";
2753 char temp_path
[MAX_PATH
];
2755 BOOL retok
, removeBackup
= FALSE
;
2759 win_skip("ReplaceFileA() is missing\n");
2763 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2764 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2765 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2767 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replaced
);
2768 ok(ret
!= 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2770 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2771 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2773 ret
= GetTempFileNameA(temp_path
, prefix
, 0, backup
);
2774 ok(ret
!= 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2776 /* place predictable data in the file to be replaced */
2777 hReplacedFile
= CreateFileA(replaced
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2778 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2779 "failed to open replaced file\n");
2780 retok
= WriteFile(hReplacedFile
, replacedData
, sizeof(replacedData
), &ret
, NULL
);
2781 ok( retok
&& ret
== sizeof(replacedData
),
2782 "WriteFile error (replaced) %d\n", GetLastError());
2783 ok(GetFileSize(hReplacedFile
, NULL
) == sizeof(replacedData
),
2784 "replaced file has wrong size\n");
2785 /* place predictable data in the file to be the replacement */
2786 hReplacementFile
= CreateFileA(replacement
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2787 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
,
2788 "failed to open replacement file\n");
2789 retok
= WriteFile(hReplacementFile
, replacementData
, sizeof(replacementData
), &ret
, NULL
);
2790 ok( retok
&& ret
== sizeof(replacementData
),
2791 "WriteFile error (replacement) %d\n", GetLastError());
2792 ok(GetFileSize(hReplacementFile
, NULL
) == sizeof(replacementData
),
2793 "replacement file has wrong size\n");
2794 /* place predictable data in the backup file (to be over-written) */
2795 hBackupFile
= CreateFileA(backup
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2796 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2797 "failed to open backup file\n");
2798 retok
= WriteFile(hBackupFile
, backupData
, sizeof(backupData
), &ret
, NULL
);
2799 ok( retok
&& ret
== sizeof(backupData
),
2800 "WriteFile error (replacement) %d\n", GetLastError());
2801 ok(GetFileSize(hBackupFile
, NULL
) == sizeof(backupData
),
2802 "backup file has wrong size\n");
2803 /* change the filetime on the "replaced" file to ensure that it changes */
2804 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2805 ok( ret
, "GetFileTime error (replaced) %d\n", GetLastError());
2806 ftReplaced
.dwLowDateTime
-= 600000000; /* 60 second */
2807 ret
= SetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2808 ok( ret
, "SetFileTime error (replaced) %d\n", GetLastError());
2809 GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
); /* get the actual time back */
2810 CloseHandle(hReplacedFile
);
2811 /* change the filetime on the backup to ensure that it changes */
2812 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2813 ok( ret
, "GetFileTime error (backup) %d\n", GetLastError());
2814 ftBackup
.dwLowDateTime
-= 1200000000; /* 120 second */
2815 ret
= SetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2816 ok( ret
, "SetFileTime error (backup) %d\n", GetLastError());
2817 GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
); /* get the actual time back */
2818 CloseHandle(hBackupFile
);
2819 /* get the filetime on the replacement file to perform checks */
2820 ret
= GetFileTime(hReplacementFile
, NULL
, NULL
, &ftReplacement
);
2821 ok( ret
, "GetFileTime error (replacement) %d\n", GetLastError());
2822 CloseHandle(hReplacementFile
);
2824 /* perform replacement w/ backup
2825 * TODO: flags are not implemented
2827 SetLastError(0xdeadbeef);
2828 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2829 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2830 /* make sure that the backup has the size of the old "replaced" file */
2831 hBackupFile
= CreateFileA(backup
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2832 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2833 "failed to open backup file\n");
2834 ret
= GetFileSize(hBackupFile
, NULL
);
2835 ok(ret
== sizeof(replacedData
),
2836 "backup file has wrong size %d\n", ret
);
2837 /* make sure that the "replaced" file has the size of the replacement file */
2838 hReplacedFile
= CreateFileA(replaced
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2839 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2840 "failed to open replaced file: %d\n", GetLastError());
2841 if (hReplacedFile
!= INVALID_HANDLE_VALUE
)
2843 ret
= GetFileSize(hReplacedFile
, NULL
);
2844 ok(ret
== sizeof(replacementData
),
2845 "replaced file has wrong size %d\n", ret
);
2846 /* make sure that the replacement file no-longer exists */
2847 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2848 ok(hReplacementFile
== INVALID_HANDLE_VALUE
,
2849 "unexpected error, replacement file should not exist %d\n", GetLastError());
2850 /* make sure that the backup has the old "replaced" filetime */
2851 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2852 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2853 ok(check_file_time(&ftBackup
, &ftReplaced
, 20000000), "backup file has wrong filetime\n");
2854 CloseHandle(hBackupFile
);
2855 /* make sure that the "replaced" has the old replacement filetime */
2856 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2857 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2858 ok(check_file_time(&ftReplaced
, &ftReplacement
, 20000000),
2859 "replaced file has wrong filetime %x%08x / %x%08x\n",
2860 ftReplaced
.dwHighDateTime
, ftReplaced
.dwLowDateTime
,
2861 ftReplacement
.dwHighDateTime
, ftReplacement
.dwLowDateTime
);
2862 CloseHandle(hReplacedFile
);
2865 skip("couldn't open replacement file, skipping tests\n");
2867 /* re-create replacement file for pass w/o backup (blank) */
2868 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2869 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2870 /* perform replacement w/o backup
2871 * TODO: flags are not implemented
2873 SetLastError(0xdeadbeef);
2874 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2875 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2876 "ReplaceFileA: unexpected error %d\n", GetLastError());
2878 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2879 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2880 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2881 ret
= DeleteFileA(backup
);
2882 ok(ret
, "DeleteFileA: error (backup) %d\n", GetLastError());
2883 /* perform replacement w/ backup (no pre-existing backup)
2884 * TODO: flags are not implemented
2886 SetLastError(0xdeadbeef);
2887 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2888 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2889 "ReplaceFileA: unexpected error %d\n", GetLastError());
2891 removeBackup
= TRUE
;
2893 /* re-create replacement file for pass w/ no permissions to "replaced" */
2894 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2895 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2896 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_READONLY
);
2897 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2898 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2899 /* perform replacement w/ backup (no permission to "replaced")
2900 * TODO: flags are not implemented
2902 SetLastError(0xdeadbeef);
2903 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2904 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2905 /* make sure that the replacement file still exists */
2906 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2907 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
||
2908 broken(GetLastError() == ERROR_FILE_NOT_FOUND
), /* win2k */
2909 "unexpected error, replacement file should still exist %d\n", GetLastError());
2910 CloseHandle(hReplacementFile
);
2911 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_NORMAL
);
2912 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2913 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2915 /* replacement file still exists, make pass w/o "replaced" */
2916 ret
= DeleteFileA(replaced
);
2917 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2918 "DeleteFileA: error (replaced) %d\n", GetLastError());
2919 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2920 * TODO: flags are not implemented
2922 SetLastError(0xdeadbeef);
2923 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2924 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2925 GetLastError() == ERROR_ACCESS_DENIED
),
2926 "ReplaceFileA: unexpected error %d\n", GetLastError());
2928 /* perform replacement w/o existing "replacement" file
2929 * TODO: flags are not implemented
2931 SetLastError(0xdeadbeef);
2932 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2933 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2934 GetLastError() == ERROR_ACCESS_DENIED
),
2935 "ReplaceFileA: unexpected error %d\n", GetLastError());
2938 * if the first round (w/ backup) worked then as long as there is no
2939 * failure then there is no need to check this round (w/ backup is the
2940 * more complete case)
2943 /* delete temporary files, replacement and replaced are already deleted */
2946 ret
= DeleteFileA(backup
);
2948 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2949 "DeleteFileA: error (backup) %d\n", GetLastError());
2954 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2955 * need to be as thorough.
2957 static void test_ReplaceFileW(void)
2959 WCHAR replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2960 static const WCHAR prefix
[] = {'p','f','x',0};
2961 WCHAR temp_path
[MAX_PATH
];
2963 BOOL removeBackup
= FALSE
;
2967 win_skip("ReplaceFileW() is missing\n");
2971 ret
= GetTempPathW(MAX_PATH
, temp_path
);
2972 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2974 win_skip("GetTempPathW is not available\n");
2977 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
2978 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2980 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replaced
);
2981 ok(ret
!= 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2983 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2984 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2986 ret
= GetTempFileNameW(temp_path
, prefix
, 0, backup
);
2987 ok(ret
!= 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2989 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2990 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2992 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2993 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2994 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2995 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2996 "ReplaceFileW: error %d\n", GetLastError());
2998 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2999 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3000 ret
= DeleteFileW(backup
);
3001 ok(ret
, "DeleteFileW: error (backup) %d\n", GetLastError());
3002 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
3003 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
3004 "ReplaceFileW: error %d\n", GetLastError());
3006 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
3007 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3008 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_READONLY
);
3009 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
3010 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3012 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
3013 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
,
3014 "ReplaceFileW: unexpected error %d\n", GetLastError());
3015 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_NORMAL
);
3016 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
3017 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3019 removeBackup
= TRUE
;
3021 ret
= DeleteFileW(replaced
);
3022 ok(ret
, "DeleteFileW: error (replaced) %d\n", GetLastError());
3023 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
3024 ok(!ret
, "ReplaceFileW: error %d\n", GetLastError());
3026 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
3027 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
3028 GetLastError() == ERROR_ACCESS_DENIED
),
3029 "ReplaceFileW: unexpected error %d\n", GetLastError());
3033 ret
= DeleteFileW(backup
);
3035 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
3036 "DeleteFileW: error (backup) %d\n", GetLastError());
3042 InitFunctionPointers();
3052 test_GetTempFileNameA();
3061 test_FindFirstFileA();
3062 test_FindNextFileA();
3063 test_FindFirstFileExA(0);
3064 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
3065 test_FindFirstFileExA(FindExSearchLimitToDirectories
);
3067 test_file_sharing();
3068 test_offset_in_overlapped_structure();
3071 test_async_file_errors();
3075 test_RemoveDirectory();
3076 test_ReplaceFileA();
3077 test_ReplaceFileW();