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( ) );
208 static void test__lclose( void )
213 filehandle
= _lcreat( filename
, 0 );
214 if (filehandle
== HFILE_ERROR
)
216 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
220 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
222 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
224 ret
= DeleteFileA( filename
);
225 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
229 static void test__lcreat( void )
233 WIN32_FIND_DATAA search_results
;
234 char slashname
[] = "testfi/";
239 filehandle
= _lcreat( filename
, 0 );
240 if (filehandle
== HFILE_ERROR
)
242 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
246 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
248 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
250 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
252 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
254 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
256 ret
= DeleteFileA(filename
);
257 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError());
259 filehandle
= _lcreat( filename
, 1 ); /* readonly */
260 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
262 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
264 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
266 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
268 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
270 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
272 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
274 filehandle
= _lcreat( filename
, 2 );
275 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
277 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
279 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
281 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
283 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
285 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
287 ret
= DeleteFileA( filename
);
288 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
290 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
291 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
293 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
295 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
297 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
299 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
301 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
303 ret
= DeleteFileA( filename
);
304 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
306 filehandle
=_lcreat (slashname
, 0); /* illegal name */
307 if (HFILE_ERROR
==filehandle
) {
309 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
310 "creating file \"%s\" failed with error %d\n", slashname
, err
);
311 } else { /* only NT succeeds */
313 find
=FindFirstFileA (slashname
, &search_results
);
314 if (INVALID_HANDLE_VALUE
!=find
)
316 ret
= FindClose (find
);
317 ok (0 != ret
, "FindClose complains (%d)\n", GetLastError ());
318 slashname
[strlen(slashname
)-1]=0;
319 ok (!strcmp (slashname
, search_results
.cFileName
),
320 "found unexpected name \"%s\"\n", search_results
.cFileName
);
321 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
322 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
323 search_results
.dwFileAttributes
);
325 ret
= DeleteFileA( slashname
);
326 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
329 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
330 if (HFILE_ERROR
==filehandle
)
331 ok (0, "couldn't create volume label \"%s\"\n", filename
);
334 find
=FindFirstFileA (filename
, &search_results
);
335 if (INVALID_HANDLE_VALUE
==find
)
336 ok (0, "file \"%s\" not found\n", filename
);
338 ret
= FindClose(find
);
339 ok ( 0 != ret
, "FindClose complains (%d)\n", GetLastError ());
340 ok (!strcmp (filename
, search_results
.cFileName
),
341 "found unexpected name \"%s\"\n", search_results
.cFileName
);
342 search_results
.dwFileAttributes
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
343 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
344 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
345 search_results
.dwFileAttributes
);
347 ret
= DeleteFileA( filename
);
348 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
353 static void test__llseek( void )
361 filehandle
= _lcreat( filename
, 0 );
362 if (filehandle
== HFILE_ERROR
)
364 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
368 for (i
= 0; i
< 400; i
++)
370 ok( _hwrite( filehandle
, sillytext
, strlen( sillytext
) ) != -1, "_hwrite complains\n" );
372 ok( _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
) != -1, "should be able to seek\n" );
373 ok( _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
) != -1, "should be able to seek\n" );
375 bytes_read
= _hread( filehandle
, buffer
, 1);
376 ok( 1 == bytes_read
, "file read size error\n" );
377 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
378 ok( _llseek( filehandle
, -400 * (LONG
)strlen( sillytext
), FILE_END
) != -1, "should be able to seek\n" );
380 bytes_read
= _hread( filehandle
, buffer
, 1);
381 ok( 1 == bytes_read
, "file read size error\n" );
382 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
383 ok( _llseek( filehandle
, 1000000, FILE_END
) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
384 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
386 ret
= DeleteFileA( filename
);
387 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
391 static void test__llopen( void )
398 filehandle
= _lcreat( filename
, 0 );
399 if (filehandle
== HFILE_ERROR
)
401 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
405 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
406 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
408 filehandle
= _lopen( filename
, OF_READ
);
409 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
410 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
411 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
412 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
414 filehandle
= _lopen( filename
, OF_READWRITE
);
415 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
416 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
417 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
418 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
420 filehandle
= _lopen( filename
, OF_WRITE
);
421 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
422 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
423 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
425 ret
= DeleteFileA( filename
);
426 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
427 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
431 static void test__lread( void )
440 filehandle
= _lcreat( filename
, 0 );
441 if (filehandle
== HFILE_ERROR
)
443 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
447 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
449 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
451 filehandle
= _lopen( filename
, OF_READ
);
453 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError());
455 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
457 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
459 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
461 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
462 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
463 for (i
= 0; i
< bytes_wanted
; i
++)
465 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
469 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
471 ret
= DeleteFileA( filename
);
472 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
476 static void test__lwrite( void )
485 HLOCAL memory_object
;
489 filehandle
= _lcreat( filename
, 0 );
490 if (filehandle
== HFILE_ERROR
)
492 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
496 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
498 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
500 filehandle
= _lopen( filename
, OF_READ
);
502 bytes_read
= _hread( filehandle
, buffer
, 1);
504 ok( 0 == bytes_read
, "file read size error\n" );
506 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
508 filehandle
= _lopen( filename
, OF_READWRITE
);
512 srand( (unsigned)time( NULL
) );
513 for (blocks
= 0; blocks
< 100; blocks
++)
515 for (i
= 0; i
< (INT
)sizeof( buffer
); i
++)
518 checksum
[0] = checksum
[0] + buffer
[i
];
520 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
521 bytes_written
= bytes_written
+ sizeof( buffer
);
524 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
527 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
529 memory_object
= LocalAlloc( LPTR
, bytes_written
);
531 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
533 contents
= LocalLock( memory_object
);
535 filehandle
= _lopen( filename
, OF_READ
);
537 contents
= LocalLock( memory_object
);
539 ok( NULL
!= contents
, "LocalLock whines\n" );
541 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
547 checksum
[0] += contents
[i
];
550 while (i
< bytes_written
- 1);
552 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
554 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
556 ret
= DeleteFileA( filename
);
557 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
560 static void test_CopyFileA(void)
562 char temp_path
[MAX_PATH
];
563 char source
[MAX_PATH
], dest
[MAX_PATH
];
564 static const char prefix
[] = "pfx";
571 ret
= GetTempPathA(MAX_PATH
, temp_path
);
572 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
573 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
575 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
576 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
578 ret
= MoveFileA(source
, source
);
579 todo_wine
ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
581 /* make the source have not zero size */
582 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
583 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
584 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
585 ok( retok
&& ret
== sizeof(prefix
),
586 "WriteFile error %d\n", GetLastError());
587 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
588 /* get the file time and change it to prove the difference */
589 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
590 ok( ret
, "GetFileTime error %d\n", GetLastError());
591 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
592 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
593 ok( ret
, "SetFileTime error %d\n", GetLastError());
594 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
597 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
598 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
600 SetLastError(0xdeadbeef);
601 ret
= CopyFileA(source
, dest
, TRUE
);
602 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
603 "CopyFileA: unexpected error %d\n", GetLastError());
605 ret
= CopyFileA(source
, dest
, FALSE
);
606 ok(ret
, "CopyFileA: error %d\n", GetLastError());
608 /* make sure that destination has correct size */
609 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
610 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
611 ret
= GetFileSize(hfile
, NULL
);
612 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
614 /* make sure that destination has the same filetime */
615 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
616 ok( ret
, "GetFileTime error %d\n", GetLastError());
617 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
619 SetLastError(0xdeadbeef);
620 ret
= CopyFileA(source
, dest
, FALSE
);
621 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
622 "CopyFileA: ret = %d, unexpected error %d\n", ret
, GetLastError());
624 /* make sure that destination still has correct size */
625 ret
= GetFileSize(hfile
, NULL
);
626 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
627 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
628 ok( retok
&& ret
== sizeof(prefix
),
629 "ReadFile: error %d\n", GetLastError());
630 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
633 ret
= DeleteFileA(source
);
634 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
635 ret
= DeleteFileA(dest
);
636 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
639 static void test_CopyFileW(void)
641 WCHAR temp_path
[MAX_PATH
];
642 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
643 static const WCHAR prefix
[] = {'p','f','x',0};
646 ret
= GetTempPathW(MAX_PATH
, temp_path
);
647 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
649 win_skip("GetTempPathW is not available\n");
652 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
653 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
655 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
656 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
658 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
659 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
661 ret
= CopyFileW(source
, dest
, TRUE
);
662 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
663 "CopyFileW: unexpected error %d\n", GetLastError());
665 ret
= CopyFileW(source
, dest
, FALSE
);
666 ok(ret
, "CopyFileW: error %d\n", GetLastError());
668 ret
= DeleteFileW(source
);
669 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
670 ret
= DeleteFileW(dest
);
671 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
676 * Debugging routine to dump a buffer in a hexdump-like fashion.
678 static void dumpmem(unsigned char* mem
, int len
) {
683 for (x
=0; x
<len
; x
+=16) {
684 ln
+= sprintf(buf
+ln
, "%04x: ",x
);
685 for (y
=0; y
<16; y
++) {
687 ln
+= sprintf(buf
+ln
, " ");
689 ln
+= sprintf(buf
+ln
, "%02hhx ",mem
[x
+y
]);
692 ln
+= sprintf(buf
+ln
, "- ");
693 for (y
=0; y
<16; y
++) {
695 if (mem
[x
+y
]<32 || mem
[x
+y
]>127) {
696 ln
+= sprintf(buf
+ln
, ".");
698 ln
+= sprintf(buf
+ln
, "%c",mem
[x
+y
]);
702 sprintf(buf
+ln
, "\n");
708 static void test_CreateFileA(void)
711 char temp_path
[MAX_PATH
], dirname
[MAX_PATH
];
712 char filename
[MAX_PATH
];
713 static const char prefix
[] = "pfx";
714 char windowsdir
[MAX_PATH
];
715 char Volume_1
[MAX_PATH
];
716 unsigned char buffer
[512];
717 char directory
[] = "removeme";
718 static const char nt_drive
[] = "\\\\?\\A:";
720 struct test_list p
[] = {
721 {"", ERROR_PATH_NOT_FOUND
, -1, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* dir as file w \ */
722 {"", ERROR_SUCCESS
, ERROR_PATH_NOT_FOUND
, FILE_FLAG_BACKUP_SEMANTICS
, FALSE
}, /* dir as dir w \ */
723 {"a", ERROR_FILE_NOT_FOUND
, -1, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* non-exist file */
724 {"a\\", ERROR_FILE_NOT_FOUND
, ERROR_PATH_NOT_FOUND
, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* non-exist dir */
725 {"removeme", ERROR_ACCESS_DENIED
, -1, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* exist dir w/o \ */
726 {"removeme\\", ERROR_PATH_NOT_FOUND
, -1, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* exst dir w \ */
727 {"c:", ERROR_ACCESS_DENIED
, ERROR_PATH_NOT_FOUND
, FILE_ATTRIBUTE_NORMAL
, FALSE
}, /* device in file namespace */
728 {"c:", ERROR_SUCCESS
, ERROR_PATH_NOT_FOUND
, FILE_FLAG_BACKUP_SEMANTICS
, FALSE
}, /* device in file namespace as dir */
729 {"c:\\", ERROR_PATH_NOT_FOUND
, ERROR_ACCESS_DENIED
, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* root dir w \ */
730 {"c:\\", ERROR_SUCCESS
, ERROR_ACCESS_DENIED
, FILE_FLAG_BACKUP_SEMANTICS
, FALSE
}, /* root dir w \ as dir */
731 {"\\\\?\\c:", ERROR_SUCCESS
, ERROR_BAD_NETPATH
, FILE_ATTRIBUTE_NORMAL
,FALSE
}, /* dev namespace drive */
732 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND
, ERROR_BAD_NETPATH
, FILE_ATTRIBUTE_NORMAL
, TRUE
}, /* dev namespace drive w \ */
733 {NULL
, 0, -1, 0, FALSE
}
735 BY_HANDLE_FILE_INFORMATION Finfo
;
737 ret
= GetTempPathA(MAX_PATH
, temp_path
);
738 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
739 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
741 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
742 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
744 SetLastError(0xdeadbeef);
745 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
746 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
747 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
748 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
750 SetLastError(0xdeadbeef);
751 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
752 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
753 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
754 "hFile %p, last error %u\n", hFile
, GetLastError());
758 SetLastError(0xdeadbeef);
759 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
760 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
761 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
762 "hFile %p, last error %u\n", hFile
, GetLastError());
766 ret
= DeleteFileA(filename
);
767 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
769 SetLastError(0xdeadbeef);
770 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
771 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
772 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
773 "hFile %p, last error %u\n", hFile
, GetLastError());
777 ret
= DeleteFileA(filename
);
778 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
780 /* get windows drive letter */
781 ret
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
782 ok(ret
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
783 ok(ret
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
785 /* test error return codes from CreateFile for some cases */
786 ret
= GetTempPathA(MAX_PATH
, temp_path
);
787 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
788 strcpy(dirname
, temp_path
);
789 strcat(dirname
, directory
);
790 ret
= CreateDirectory(dirname
, NULL
);
791 ok( ret
, "Createdirectory failed, gle=%d\n", GetLastError() );
792 /* set current drive & directory to known location */
793 SetCurrentDirectoryA( temp_path
);
798 /* update the drive id in the table entry with the current one */
799 if (p
[i
].file
[1] == ':')
801 strcpy(filename
, p
[i
].file
);
802 filename
[0] = windowsdir
[0];
804 else if (p
[i
].file
[0] == '\\' && p
[i
].file
[5] == ':')
806 strcpy(filename
, p
[i
].file
);
807 filename
[4] = windowsdir
[0];
811 /* prefix the table entry with the current temp directory */
812 strcpy(filename
, temp_path
);
813 strcat(filename
, p
[i
].file
);
815 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
816 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
818 p
[i
].options
, NULL
);
819 /* if we get ACCESS_DENIED when we do not expect it, assume
820 * no access to the volume
822 if (hFile
== INVALID_HANDLE_VALUE
&&
823 GetLastError() == ERROR_ACCESS_DENIED
&&
824 p
[i
].err
!= ERROR_ACCESS_DENIED
)
827 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename
, GetLastError(), p
[i
].err
);
829 skip("Do not have authority to access volumes. Test for %s skipped\n", filename
);
831 /* otherwise validate results with expectations */
832 else if (p
[i
].todo_flag
)
834 (hFile
== INVALID_HANDLE_VALUE
&&
835 (p
[i
].err
== GetLastError() || p
[i
].err2
== GetLastError())) ||
836 (hFile
!= INVALID_HANDLE_VALUE
&& p
[i
].err
== ERROR_SUCCESS
),
837 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
838 filename
, hFile
, GetLastError(), p
[i
].err
);
841 (hFile
== INVALID_HANDLE_VALUE
&&
842 (p
[i
].err
== GetLastError() || p
[i
].err2
== GetLastError())) ||
843 (hFile
!= INVALID_HANDLE_VALUE
&& p
[i
].err
== ERROR_SUCCESS
),
844 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
845 filename
, hFile
, GetLastError(), p
[i
].err
);
846 if (hFile
!= INVALID_HANDLE_VALUE
)
847 CloseHandle( hFile
);
850 ret
= RemoveDirectoryA(dirname
);
851 ok(ret
, "RemoveDirectoryA: error %d\n", GetLastError());
854 /* test opening directory as a directory */
855 hFile
= CreateFileA( temp_path
, GENERIC_READ
,
859 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
860 if (hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() != ERROR_PATH_NOT_FOUND
)
862 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_SUCCESS
,
863 "CreateFileA did not work, last error %u on volume <%s>\n",
864 GetLastError(), temp_path
);
866 if (hFile
!= INVALID_HANDLE_VALUE
)
868 ret
= GetFileInformationByHandle( hFile
, &Finfo
);
871 ok(Finfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
,
872 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
873 temp_path
, Finfo
.dwFileAttributes
);
875 CloseHandle( hFile
);
879 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path
);
882 /* *** Test opening volumes/devices using drive letter *** */
884 /* test using drive letter in non-rewrite format without trailing \ */
885 /* this should work */
886 strcpy(filename
, nt_drive
);
887 filename
[4] = windowsdir
[0];
888 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
889 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
891 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
892 if (hFile
!= INVALID_HANDLE_VALUE
||
893 (GetLastError() != ERROR_ACCESS_DENIED
&& GetLastError() != ERROR_BAD_NETPATH
))
895 /* if we have adm rights to volume, then try rest of tests */
896 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA did not open %s, last error=%u\n",
897 filename
, GetLastError());
898 if (hFile
!= INVALID_HANDLE_VALUE
)
900 /* if we opened the volume/device, try to read it. Since it */
901 /* opened, we should be able to read it. We don't care about*/
902 /* what the data is at this time. */
904 ret
= ReadFile( hFile
, buffer
, len
, &len
, NULL
);
905 todo_wine
ok(ret
, "Failed to read volume, last error %u, %u, for %s\n",
906 GetLastError(), ret
, filename
);
909 trace("buffer is\n");
912 CloseHandle( hFile
);
915 /* test using drive letter with trailing \ and in non-rewrite */
916 /* this should not work */
917 strcpy(filename
, nt_drive
);
918 filename
[4] = windowsdir
[0];
919 strcat( filename
, "\\" );
920 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
921 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
923 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
925 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
926 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
927 filename
, GetLastError());
928 if (hFile
!= INVALID_HANDLE_VALUE
)
929 CloseHandle( hFile
);
931 /* test using temp path with trailing \ and in non-rewrite as dir */
932 /* this should work */
933 strcpy(filename
, nt_drive
);
935 strcat( filename
, temp_path
);
936 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
937 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
939 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
940 ok(hFile
!= INVALID_HANDLE_VALUE
,
941 "CreateFileA should have worked on %s, but got %u\n",
942 filename
, GetLastError());
943 if (hFile
!= INVALID_HANDLE_VALUE
)
944 CloseHandle( hFile
);
946 /* test using drive letter without trailing \ and in device ns */
947 /* this should work */
948 strcpy(filename
, nt_drive
);
949 filename
[4] = windowsdir
[0];
951 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
952 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
954 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
955 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA did not open %s, last error=%u\n",
956 filename
, GetLastError());
957 if (hFile
!= INVALID_HANDLE_VALUE
)
958 CloseHandle( hFile
);
960 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
961 else if (GetLastError() == ERROR_BAD_NETPATH
)
962 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
964 skip("Do not have authority to access volumes. Tests skipped\n");
967 /* *** Test opening volumes/devices using GUID *** */
969 if (pGetVolumeNameForVolumeMountPointA
)
971 strcpy(filename
, "c:\\");
972 filename
[0] = windowsdir
[0];
973 ret
= pGetVolumeNameForVolumeMountPointA( filename
, Volume_1
, MAX_PATH
);
974 ok(ret
, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename
, GetLastError());
977 ok(strlen(Volume_1
) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1
);
979 /* test the result of opening a unique volume name (GUID)
980 * with the trailing \
981 * this should error out
983 strcpy(filename
, Volume_1
);
984 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
985 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
987 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
989 ok(hFile
== INVALID_HANDLE_VALUE
,
990 "CreateFileA should not have opened %s, hFile %p\n",
993 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
994 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
995 filename
, GetLastError());
996 if (hFile
!= INVALID_HANDLE_VALUE
)
997 CloseHandle( hFile
);
999 /* test the result of opening a unique volume name (GUID)
1000 * with the temp path string as dir
1003 strcpy(filename
, Volume_1
);
1004 strcat(filename
, temp_path
+3);
1005 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1006 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1007 NULL
, OPEN_EXISTING
,
1008 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
1010 ok(hFile
!= INVALID_HANDLE_VALUE
,
1011 "CreateFileA should have opened %s, but got %u\n",
1012 filename
, GetLastError());
1013 if (hFile
!= INVALID_HANDLE_VALUE
)
1014 CloseHandle( hFile
);
1016 /* test the result of opening a unique volume name (GUID)
1017 * without the trailing \ and in device namespace
1020 strcpy(filename
, Volume_1
);
1023 hFile
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1024 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1025 NULL
, OPEN_EXISTING
,
1026 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
, NULL
);
1027 if (hFile
!= INVALID_HANDLE_VALUE
|| GetLastError() != ERROR_ACCESS_DENIED
)
1029 /* if we have adm rights to volume, then try rest of tests */
1030 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA did not open %s, last error=%u\n",
1031 filename
, GetLastError());
1032 if (hFile
!= INVALID_HANDLE_VALUE
)
1034 /* if we opened the volume/device, try to read it. Since it */
1035 /* opened, we should be able to read it. We don't care about*/
1036 /* what the data is at this time. */
1038 ret
= ReadFile( hFile
, buffer
, len
, &len
, NULL
);
1039 todo_wine
ok(ret
, "Failed to read volume, last error %u, %u, for %s\n",
1040 GetLastError(), ret
, filename
);
1043 trace("buffer is\n");
1044 dumpmem(buffer
, 64);
1046 CloseHandle( hFile
);
1050 skip("Do not have authority to access volumes. Tests skipped\n");
1053 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1056 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1059 static void test_CreateFileW(void)
1062 WCHAR temp_path
[MAX_PATH
];
1063 WCHAR filename
[MAX_PATH
];
1064 static const WCHAR emptyW
[]={'\0'};
1065 static const WCHAR prefix
[] = {'p','f','x',0};
1066 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1069 ret
= GetTempPathW(MAX_PATH
, temp_path
);
1070 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1072 win_skip("GetTempPathW is not available\n");
1075 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
1076 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1078 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
1079 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
1081 SetLastError(0xdeadbeef);
1082 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
1083 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
1084 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
1085 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1087 SetLastError(0xdeadbeef);
1088 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1089 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1090 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1091 "hFile %p, last error %u\n", hFile
, GetLastError());
1095 SetLastError(0xdeadbeef);
1096 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1097 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1098 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1099 "hFile %p, last error %u\n", hFile
, GetLastError());
1103 ret
= DeleteFileW(filename
);
1104 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1106 SetLastError(0xdeadbeef);
1107 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1108 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1109 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
1110 "hFile %p, last error %u\n", hFile
, GetLastError());
1114 ret
= DeleteFileW(filename
);
1115 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1119 /* this crashes on NT4.0 */
1120 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
1121 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
1122 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1123 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile
,GetLastError());
1126 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
1127 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
1128 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1129 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile
,GetLastError());
1131 /* test the result of opening a nonexistent driver name */
1132 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1133 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1134 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
1135 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile
,GetLastError());
1137 ret
= CreateDirectoryW(filename
, NULL
);
1138 ok(ret
== TRUE
, "couldn't create temporary directory\n");
1139 hFile
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1140 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
1141 ok(hFile
!= INVALID_HANDLE_VALUE
,
1142 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1144 ret
= RemoveDirectoryW(filename
);
1145 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1148 static void test_GetTempFileNameA(void)
1152 char expected
[MAX_PATH
+ 10];
1153 char windowsdir
[MAX_PATH
+ 10];
1154 char windowsdrive
[3];
1156 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1157 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
1158 ok(result
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
1160 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1161 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
1163 strcat(windowsdir
, "\\");
1166 windowsdrive
[0] = windowsdir
[0];
1167 windowsdrive
[1] = windowsdir
[1];
1168 windowsdrive
[2] = '\0';
1170 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
1171 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
1172 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
1173 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1174 windowsdrive
[0], out
);
1176 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
1177 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
1179 strcat(expected
, windowsdir
);
1180 strcat(expected
, "abc2.tmp");
1181 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1185 static void test_DeleteFileA( void )
1189 ret
= DeleteFileA(NULL
);
1190 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
1191 GetLastError() == ERROR_PATH_NOT_FOUND
),
1192 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
1194 ret
= DeleteFileA("");
1195 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
1196 GetLastError() == ERROR_BAD_PATHNAME
),
1197 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
1199 ret
= DeleteFileA("nul");
1200 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
1201 GetLastError() == ERROR_INVALID_PARAMETER
||
1202 GetLastError() == ERROR_ACCESS_DENIED
||
1203 GetLastError() == ERROR_INVALID_FUNCTION
),
1204 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret
,GetLastError());
1207 static void test_DeleteFileW( void )
1210 WCHAR pathW
[MAX_PATH
];
1211 WCHAR pathsubW
[MAX_PATH
];
1212 static const WCHAR dirW
[] = {'d','e','l','e','t','e','f','i','l','e',0};
1213 static const WCHAR subdirW
[] = {'\\','s','u','b',0};
1214 static const WCHAR emptyW
[]={'\0'};
1216 ret
= DeleteFileW(NULL
);
1217 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1219 win_skip("DeleteFileW is not available\n");
1222 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1223 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
1225 ret
= DeleteFileW(emptyW
);
1226 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
1227 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
1229 /* test DeleteFile on empty directory */
1230 ret
= GetTempPathW(MAX_PATH
, pathW
);
1231 if (ret
+ sizeof(dirW
)/sizeof(WCHAR
)-1 + sizeof(subdirW
)/sizeof(WCHAR
)-1 >= MAX_PATH
)
1233 ok(0, "MAX_PATH exceeded in constructing paths\n");
1236 lstrcatW(pathW
, dirW
);
1237 lstrcpyW(pathsubW
, pathW
);
1238 lstrcatW(pathsubW
, subdirW
);
1239 ret
= CreateDirectoryW(pathW
, NULL
);
1240 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
1241 ret
= DeleteFileW(pathW
);
1242 ok(ret
== FALSE
, "DeleteFile should fail for empty directories\n");
1243 ret
= RemoveDirectoryW(pathW
);
1244 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
1246 /* test DeleteFile on non-empty directory */
1247 ret
= CreateDirectoryW(pathW
, NULL
);
1248 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
1249 ret
= CreateDirectoryW(pathsubW
, NULL
);
1250 ok(ret
== TRUE
, "couldn't create directory deletefile\\sub\n");
1251 ret
= DeleteFileW(pathW
);
1252 ok(ret
== FALSE
, "DeleteFile should fail for non-empty directories\n");
1253 ret
= RemoveDirectoryW(pathsubW
);
1254 ok(ret
== TRUE
, "expected to remove directory deletefile\\sub\n");
1255 ret
= RemoveDirectoryW(pathW
);
1256 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
1259 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1261 static void test_MoveFileA(void)
1263 char tempdir
[MAX_PATH
];
1264 char source
[MAX_PATH
], dest
[MAX_PATH
];
1265 static const char prefix
[] = "pfx";
1268 ret
= GetTempPathA(MAX_PATH
, tempdir
);
1269 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
1270 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1272 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
1273 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1275 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
1276 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1278 ret
= MoveFileA(source
, dest
);
1279 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1280 "MoveFileA: unexpected error %d\n", GetLastError());
1282 ret
= DeleteFileA(dest
);
1283 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
1285 ret
= MoveFileA(source
, dest
);
1286 ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
1288 lstrcatA(tempdir
, "Remove Me");
1289 ret
= CreateDirectoryA(tempdir
, NULL
);
1290 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
1292 lstrcpyA(source
, dest
);
1293 lstrcpyA(dest
, tempdir
);
1294 lstrcatA(dest
, "\\wild?.*");
1295 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1296 ret
= MoveFileA(source
, dest
);
1297 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
1298 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
1299 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
1300 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1301 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
1303 WIN32_FIND_DATAA fd
;
1304 char temppath
[MAX_PATH
];
1307 lstrcpyA(temppath
, tempdir
);
1308 lstrcatA(temppath
, "\\*.*");
1309 hFind
= FindFirstFileA(temppath
, &fd
);
1310 if (INVALID_HANDLE_VALUE
!= hFind
)
1315 lpName
= fd
.cAlternateFileName
;
1317 lpName
= fd
.cFileName
;
1318 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
1320 while (FindNextFileA(hFind
, &fd
));
1324 ret
= DeleteFileA(source
);
1325 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
1326 ret
= DeleteFileA(dest
);
1327 ok(!ret
, "DeleteFileA: error %d\n", GetLastError());
1328 ret
= RemoveDirectoryA(tempdir
);
1329 ok(ret
, "DeleteDirectoryA: error %d\n", GetLastError());
1332 static void test_MoveFileW(void)
1334 WCHAR temp_path
[MAX_PATH
];
1335 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
1336 static const WCHAR prefix
[] = {'p','f','x',0};
1339 ret
= GetTempPathW(MAX_PATH
, temp_path
);
1340 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1342 win_skip("GetTempPathW is not available\n");
1345 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
1346 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1348 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
1349 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
1351 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
1352 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
1354 ret
= MoveFileW(source
, dest
);
1355 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1356 "CopyFileW: unexpected error %d\n", GetLastError());
1358 ret
= DeleteFileW(source
);
1359 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1360 ret
= DeleteFileW(dest
);
1361 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1364 #define PATTERN_OFFSET 0x10
1366 static void test_offset_in_overlapped_structure(void)
1372 BYTE buf
[256], pattern
[] = "TeSt";
1374 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
1377 ret
=GetTempPathA(MAX_PATH
, temp_path
);
1378 ok( ret
, "GetTempPathA error %d\n", GetLastError());
1379 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
1380 ok( ret
, "GetTempFileNameA error %d\n", GetLastError());
1382 /*** Write File *****************************************************/
1384 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
1385 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1387 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
1388 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
1389 ok( ret
, "WriteFile error %d\n", GetLastError());
1390 ok(done
== sizeof(buf
), "expected number of bytes written %u\n", done
);
1392 memset(&ov
, 0, sizeof(ov
));
1393 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1394 S(U(ov
)).OffsetHigh
= 0;
1395 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1396 /* Win 9x does not support the overlapped I/O on files */
1397 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1398 ok(rc
, "WriteFile error %d\n", GetLastError());
1399 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1400 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1401 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1403 S(U(ov
)).Offset
= sizeof(buf
) * 2;
1404 S(U(ov
)).OffsetHigh
= 0;
1405 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1406 ok( ret
, "WriteFile error %d\n", GetLastError());
1407 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1408 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1409 ok(offset
== sizeof(buf
) * 2 + sizeof(pattern
), "wrong file offset %d\n", offset
);
1414 /*** Read File *****************************************************/
1416 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1417 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1419 memset(buf
, 0, sizeof(buf
));
1420 memset(&ov
, 0, sizeof(ov
));
1421 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1422 S(U(ov
)).OffsetHigh
= 0;
1423 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
1424 /* Win 9x does not support the overlapped I/O on files */
1425 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1426 ok(rc
, "ReadFile error %d\n", GetLastError());
1427 ok(done
== sizeof(pattern
), "expected number of bytes read %u\n", done
);
1428 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1429 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1430 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
1435 ret
= DeleteFileA(temp_fname
);
1436 ok( ret
, "DeleteFileA error %d\n", GetLastError());
1439 static void test_LockFile(void)
1443 OVERLAPPED overlapped
;
1444 int limited_LockFile
;
1445 int limited_UnLockFile
;
1448 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1449 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1450 CREATE_ALWAYS
, 0, 0 );
1451 if (handle
== INVALID_HANDLE_VALUE
)
1453 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1456 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
1458 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1459 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1461 limited_UnLockFile
= 0;
1462 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1464 limited_UnLockFile
= 1;
1467 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1468 /* overlapping locks must fail */
1469 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1470 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1471 /* non-overlapping locks must succeed */
1472 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1474 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1475 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1476 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1477 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1479 S(U(overlapped
)).Offset
= 100;
1480 S(U(overlapped
)).OffsetHigh
= 0;
1481 overlapped
.hEvent
= 0;
1483 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1484 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1486 /* LockFileEx is probably OK, test it more. */
1487 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1488 "LockFileEx 100,100 failed\n" );
1491 /* overlapping shared locks are OK */
1492 S(U(overlapped
)).Offset
= 150;
1493 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1495 /* but exclusive is not */
1496 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1497 0, 50, 0, &overlapped
),
1498 "LockFileEx exclusive 150,50 succeeded\n" );
1499 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1500 { /* UnLockFile is capable. */
1501 S(U(overlapped
)).Offset
= 100;
1502 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1503 "UnlockFileEx 150,100 again succeeded\n" );
1506 ret
= LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 );
1509 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1510 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1511 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1514 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong LockFile error %u\n", GetLastError() );
1516 /* wrap-around lock should not do anything */
1517 /* (but still succeeds on NT4 so we don't check result) */
1518 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1520 limited_LockFile
= 0;
1521 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1523 limited_LockFile
= 1;
1526 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1528 /* zero-byte lock */
1529 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1530 limited_LockFile
|| ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1531 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1532 limited_LockFile
|| ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1534 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1535 !ok( UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1537 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1539 CloseHandle( handle
);
1540 DeleteFileA( filename
);
1543 static inline int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
, BOOL is_win9x
)
1547 if (!access1
) sharing1
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1548 if (!access2
) sharing2
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1553 if (!access1
) access1
= GENERIC_READ
;
1556 if (!access2
) access2
= GENERIC_READ
;
1559 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1560 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1561 if ((access1
& DELETE
) && !(sharing2
& FILE_SHARE_DELETE
)) return 0;
1562 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1563 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1564 if ((access2
& DELETE
) && !(sharing1
& FILE_SHARE_DELETE
)) return 0;
1568 static void test_file_sharing(void)
1570 static const DWORD access_modes
[] =
1571 { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
,
1572 DELETE
, GENERIC_READ
|DELETE
, GENERIC_WRITE
|DELETE
, GENERIC_READ
|GENERIC_WRITE
|DELETE
};
1573 static const DWORD sharing_modes
[] =
1574 { 0, FILE_SHARE_READ
,
1575 FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1576 FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_DELETE
,
1577 FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
};
1581 BOOL is_win9x
= FALSE
;
1583 /* make sure the file exists */
1584 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1585 if (h
== INVALID_HANDLE_VALUE
)
1587 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError());
1590 is_win9x
= GetFileAttributesW(filenameW
) == INVALID_FILE_ATTRIBUTES
;
1593 for (a1
= 0; a1
< sizeof(access_modes
)/sizeof(access_modes
[0]); a1
++)
1595 for (s1
= 0; s1
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s1
++)
1597 /* Win9x doesn't support FILE_SHARE_DELETE */
1598 if (is_win9x
&& (sharing_modes
[s1
] & FILE_SHARE_DELETE
))
1601 SetLastError(0xdeadbeef);
1602 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1603 NULL
, OPEN_EXISTING
, 0, 0 );
1604 if (h
== INVALID_HANDLE_VALUE
)
1606 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1609 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1611 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1613 /* Win9x doesn't support FILE_SHARE_DELETE */
1614 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1617 SetLastError(0xdeadbeef);
1618 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1619 NULL
, OPEN_EXISTING
, 0, 0 );
1621 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1622 access_modes
[a2
], sharing_modes
[s2
], is_win9x
))
1624 ret
= GetLastError();
1626 ok( h2
!= INVALID_HANDLE_VALUE
,
1627 "open failed for modes %x/%x/%x/%x\n",
1628 access_modes
[a1
], sharing_modes
[s1
],
1629 access_modes
[a2
], sharing_modes
[s2
] );
1630 ok( ret
== 0xdeadbeef /* Win9x */ ||
1632 "wrong error code %d\n", ret
);
1638 ret
= GetLastError();
1640 ok( h2
== INVALID_HANDLE_VALUE
,
1641 "open succeeded for modes %x/%x/%x/%x\n",
1642 access_modes
[a1
], sharing_modes
[s1
],
1643 access_modes
[a2
], sharing_modes
[s2
] );
1644 ok( ret
== ERROR_SHARING_VIOLATION
,
1645 "wrong error code %d\n", ret
);
1653 SetLastError(0xdeadbeef);
1654 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1655 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1657 SetLastError(0xdeadbeef);
1658 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1659 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1660 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %d\n", GetLastError() );
1662 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1663 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1668 DeleteFileA( filename
);
1671 static char get_windows_drive(void)
1673 char windowsdir
[MAX_PATH
];
1674 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1675 return windowsdir
[0];
1678 static void test_FindFirstFileA(void)
1681 WIN32_FIND_DATAA data
;
1683 char buffer
[5] = "C:\\";
1685 char nonexistent
[MAX_PATH
];
1687 /* try FindFirstFileA on "C:\" */
1688 buffer
[0] = get_windows_drive();
1690 SetLastError( 0xdeadbeaf );
1691 handle
= FindFirstFileA(buffer
, &data
);
1692 err
= GetLastError();
1693 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should fail\n" );
1694 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1696 /* try FindFirstFileA on "C:\*" */
1697 strcpy(buffer2
, buffer
);
1698 strcat(buffer2
, "*");
1699 handle
= FindFirstFileA(buffer2
, &data
);
1700 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1701 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1702 "FindFirstFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1703 if (FindNextFileA( handle
, &data
))
1704 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1705 "FindNextFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1706 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1708 /* try FindFirstFileA on windows dir */
1709 GetWindowsDirectory( buffer2
, sizeof(buffer2
) );
1710 strcat(buffer2
, "\\*");
1711 handle
= FindFirstFileA(buffer2
, &data
);
1712 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1713 ok( !strcmp( data
.cFileName
, "." ), "FindFirstFile should return '.' first\n" );
1714 ok( FindNextFileA( handle
, &data
), "FindNextFile failed\n" );
1715 ok( !strcmp( data
.cFileName
, ".." ), "FindNextFile should return '..' as second entry\n" );
1716 while (FindNextFileA( handle
, &data
))
1717 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1718 "FindNextFile shouldn't return '%s'\n", data
.cFileName
);
1719 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1721 /* try FindFirstFileA on "C:\foo\" */
1722 SetLastError( 0xdeadbeaf );
1723 if (!GetTempFileNameA( buffer
, "foo", 0, nonexistent
) && GetLastError() == ERROR_ACCESS_DENIED
)
1726 GetTempPathA( sizeof(tmp
), tmp
);
1727 GetTempFileNameA( tmp
, "foo", 0, nonexistent
);
1729 DeleteFileA( nonexistent
);
1730 strcpy(buffer2
, nonexistent
);
1731 strcat(buffer2
, "\\");
1732 handle
= FindFirstFileA(buffer2
, &data
);
1733 err
= GetLastError();
1734 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1736 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1739 /* try FindFirstFileA on "C:\foo\bar.txt" */
1740 SetLastError( 0xdeadbeaf );
1741 strcpy(buffer2
, nonexistent
);
1742 strcat(buffer2
, "\\bar.txt");
1743 handle
= FindFirstFileA(buffer2
, &data
);
1744 err
= GetLastError();
1745 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1746 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1748 /* try FindFirstFileA on "C:\foo\*.*" */
1749 SetLastError( 0xdeadbeaf );
1750 strcpy(buffer2
, nonexistent
);
1751 strcat(buffer2
, "\\*.*");
1752 handle
= FindFirstFileA(buffer2
, &data
);
1753 err
= GetLastError();
1754 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1755 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1757 /* try FindFirstFileA on "foo\bar.txt" */
1758 SetLastError( 0xdeadbeaf );
1759 strcpy(buffer2
, nonexistent
+ 3);
1760 strcat(buffer2
, "\\bar.txt");
1761 handle
= FindFirstFileA(buffer2
, &data
);
1762 err
= GetLastError();
1763 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1764 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1766 /* try FindFirstFileA on "c:\nul" */
1767 SetLastError( 0xdeadbeaf );
1768 strcpy(buffer2
, buffer
);
1769 strcat(buffer2
, "nul");
1770 handle
= FindFirstFileA(buffer2
, &data
);
1771 err
= GetLastError();
1772 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1773 ok( 0 == lstrcmpiA(data
.cFileName
, "nul"), "wrong name %s\n", data
.cFileName
);
1774 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
1775 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
1776 "wrong attributes %x\n", data
.dwFileAttributes
);
1777 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
1779 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1780 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1782 SetLastError( 0xdeadbeaf );
1783 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1784 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1785 ok( FindClose( handle
), "failed to close handle\n" );
1787 /* try FindFirstFileA on "lpt1" */
1788 SetLastError( 0xdeadbeaf );
1789 strcpy(buffer2
, "lpt1");
1790 handle
= FindFirstFileA(buffer2
, &data
);
1791 err
= GetLastError();
1792 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1793 ok( 0 == lstrcmpiA(data
.cFileName
, "lpt1"), "wrong name %s\n", data
.cFileName
);
1794 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
1795 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
1796 "wrong attributes %x\n", data
.dwFileAttributes
);
1797 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
1799 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1800 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1802 SetLastError( 0xdeadbeaf );
1803 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1804 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1805 ok( FindClose( handle
), "failed to close handle\n" );
1807 /* try FindFirstFileA on "c:\nul\*" */
1808 SetLastError( 0xdeadbeaf );
1809 strcpy(buffer2
, buffer
);
1810 strcat(buffer2
, "nul\\*");
1811 handle
= FindFirstFileA(buffer2
, &data
);
1812 err
= GetLastError();
1813 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1814 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1816 /* try FindFirstFileA on "c:\nul*" */
1817 SetLastError( 0xdeadbeaf );
1818 strcpy(buffer2
, buffer
);
1819 strcat(buffer2
, "nul*");
1820 handle
= FindFirstFileA(buffer2
, &data
);
1821 err
= GetLastError();
1822 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1823 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1825 /* try FindFirstFileA on "c:\foo\bar\nul" */
1826 SetLastError( 0xdeadbeaf );
1827 strcpy(buffer2
, buffer
);
1828 strcat(buffer2
, "foo\\bar\\nul");
1829 handle
= FindFirstFileA(buffer2
, &data
);
1830 err
= GetLastError();
1831 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1832 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1834 /* try FindFirstFileA on "c:\foo\nul\bar" */
1835 SetLastError( 0xdeadbeaf );
1836 strcpy(buffer2
, buffer
);
1837 strcat(buffer2
, "foo\\nul\\bar");
1838 handle
= FindFirstFileA(buffer2
, &data
);
1839 err
= GetLastError();
1840 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1841 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1844 static void test_FindNextFileA(void)
1847 WIN32_FIND_DATAA search_results
;
1849 char buffer
[5] = "C:\\*";
1851 buffer
[0] = get_windows_drive();
1852 handle
= FindFirstFileA(buffer
,&search_results
);
1853 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
1854 while (FindNextFile(handle
, &search_results
))
1856 /* get to the end of the files */
1858 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1859 err
= GetLastError();
1860 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
1863 static void test_FindFirstFileExA(FINDEX_SEARCH_OPS search_ops
)
1865 WIN32_FIND_DATAA search_results
;
1869 if (!pFindFirstFileExA
)
1871 win_skip("FindFirstFileExA() is missing\n");
1875 CreateDirectoryA("test-dir", NULL
);
1876 _lclose(_lcreat("test-dir\\file1", 0));
1877 _lclose(_lcreat("test-dir\\file2", 0));
1878 CreateDirectoryA("test-dir\\dir1", NULL
);
1879 SetLastError(0xdeadbeef);
1880 handle
= pFindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, search_ops
, NULL
, 0);
1881 if (handle
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1883 win_skip("FindFirstFileExA is not implemented\n");
1886 ok(handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile failed (err=%u)\n", GetLastError());
1887 ok(strcmp(search_results
.cFileName
, ".") == 0, "First entry should be '.', is %s\n", search_results
.cFileName
);
1889 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1891 ok(FindNextFile(handle
, &search_results
), "Fetching second file failed\n");
1892 ok(strcmp(search_results
.cFileName
, "..") == 0, "Second entry should be '..' is %s\n", search_results
.cFileName
);
1894 ok(FindNextFile(handle
, &search_results
), "Fetching third file failed\n");
1895 ok(CHECK_NAME(search_results
.cFileName
), "Invalid third entry - %s\n", search_results
.cFileName
);
1897 SetLastError(0xdeadbeef);
1898 ret
= FindNextFile(handle
, &search_results
);
1899 if (!ret
&& (GetLastError() == ERROR_NO_MORE_FILES
) && (search_ops
== FindExSearchLimitToDirectories
))
1901 skip("File system supports directory filtering\n");
1902 /* Results from the previous call are not cleared */
1903 ok(strcmp(search_results
.cFileName
, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results
.cFileName
);
1904 FindClose( handle
);
1908 ok(ret
, "Fetching fourth file failed\n");
1909 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
1911 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
1912 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
1916 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should fail\n");
1918 FindClose( handle
);
1921 DeleteFileA("test-dir\\file1");
1922 DeleteFileA("test-dir\\file2");
1923 RemoveDirectoryA("test-dir\\dir1");
1924 RemoveDirectoryA("test-dir");
1927 static int test_Mapfile_createtemp(HANDLE
*handle
)
1929 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1930 DeleteFile(filename
);
1931 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1932 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1933 if (*handle
!= INVALID_HANDLE_VALUE
) {
1941 static void test_MapFile(void)
1946 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1948 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1949 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1951 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1953 /* We have to close file before we try new stuff with mapping again.
1954 Else we would always succeed on XP or block descriptors on 95. */
1955 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1956 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1957 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1958 ok( CloseHandle( handle
), "can't close file handle\n");
1961 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1963 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1964 ok( hmap
== NULL
, "mapped zero size file\n");
1965 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1967 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
1968 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1969 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1971 CloseHandle( hmap
);
1973 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
1974 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1975 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1977 CloseHandle( hmap
);
1979 /* On XP you can now map again, on Win 95 you cannot. */
1981 ok( CloseHandle( handle
), "can't close file handle\n");
1982 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1985 static void test_GetFileType(void)
1988 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1989 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1990 type
= GetFileType(h
);
1991 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
1993 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1994 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1995 type
= GetFileType(h
);
1996 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
1998 DeleteFileA( filename
);
2001 static int completion_count
;
2003 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
2005 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2006 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
2010 static void test_async_file_errors(void)
2012 char szFile
[MAX_PATH
];
2013 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
2015 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
2017 S(U(ovl
)).Offset
= 0;
2018 S(U(ovl
)).OffsetHigh
= 0;
2020 completion_count
= 0;
2022 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
2023 strcat(szFile
, "\\win.ini");
2024 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2025 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
2026 if (hFile
== INVALID_HANDLE_VALUE
) /* win9x doesn't like FILE_SHARE_DELETE */
2027 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2028 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
2029 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA(%s ...) failed\n", szFile
);
2034 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
2036 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
2037 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2040 if (!GetOverlappedResult(hFile
, &ovl
, &count
, FALSE
))
2042 S(U(ovl
)).Offset
+= count
;
2043 /* i/o completion routine only called if ReadFileEx returned success.
2044 * we only care about violations of this rule so undo what should have
2048 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
2049 /*printf("Error = %ld\n", GetLastError());*/
2050 HeapFree(GetProcessHeap(), 0, lpBuffer
);
2053 static void test_read_write(void)
2055 DWORD bytes
, ret
, old_prot
;
2057 char temp_path
[MAX_PATH
];
2058 char filename
[MAX_PATH
];
2060 static const char prefix
[] = "pfx";
2062 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2063 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2064 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2066 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
2067 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
2069 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
2070 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
2071 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
2073 SetLastError(12345678);
2075 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
2076 ok(ret
&& GetLastError() == 12345678,
2077 "ret = %d, error %d\n", ret
, GetLastError());
2078 ok(!bytes
, "bytes = %d\n", bytes
);
2080 SetLastError(12345678);
2082 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
2083 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
2084 (ret
&& GetLastError() == 12345678), /* Win9x */
2085 "ret = %d, error %d\n", ret
, GetLastError());
2086 ok(!bytes
|| /* Win2k */
2087 bytes
== 10, /* Win9x */
2088 "bytes = %d\n", bytes
);
2090 /* make sure the file contains data */
2091 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
2092 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
2094 SetLastError(12345678);
2096 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
2097 ok(ret
&& GetLastError() == 12345678,
2098 "ret = %d, error %d\n", ret
, GetLastError());
2099 ok(!bytes
, "bytes = %d\n", bytes
);
2101 SetLastError(12345678);
2103 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
2104 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
2105 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
2106 "ret = %d, error %d\n", ret
, GetLastError());
2107 ok(!bytes
, "bytes = %d\n", bytes
);
2109 /* test passing protected memory as buffer */
2111 mem
= VirtualAlloc( NULL
, 0x4000, MEM_COMMIT
, PAGE_READWRITE
);
2112 ok( mem
!= NULL
, "failed to allocate virtual mem error %u\n", GetLastError() );
2114 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2115 ok( ret
, "WriteFile failed error %u\n", GetLastError() );
2116 ok( bytes
== 0x4000, "only wrote %x bytes\n", bytes
);
2118 ret
= VirtualProtect( mem
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
2119 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2121 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2122 ok( !ret
, "WriteFile succeeded\n" );
2123 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
2124 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2125 "wrong error %u\n", GetLastError() );
2126 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2128 ret
= WriteFile( (HANDLE
)0xdead, mem
, 0x4000, &bytes
, NULL
);
2129 ok( !ret
, "WriteFile succeeded\n" );
2130 ok( GetLastError() == ERROR_INVALID_HANDLE
|| /* handle is checked before buffer on NT */
2131 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2132 "wrong error %u\n", GetLastError() );
2133 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2135 ret
= VirtualProtect( mem
, 0x2000, PAGE_NOACCESS
, &old_prot
);
2136 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2138 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2139 ok( !ret
, "WriteFile succeeded\n" );
2140 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
2141 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2142 "wrong error %u\n", GetLastError() );
2143 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2145 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
2147 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2148 ok( !ret
, "ReadFile succeeded\n" );
2149 ok( GetLastError() == ERROR_NOACCESS
||
2150 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2151 "wrong error %u\n", GetLastError() );
2152 ok( bytes
== 0, "read %x bytes\n", bytes
);
2154 ret
= VirtualProtect( mem
, 0x2000, PAGE_READONLY
, &old_prot
);
2155 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2157 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2158 ok( !ret
, "ReadFile succeeded\n" );
2159 ok( GetLastError() == ERROR_NOACCESS
||
2160 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2161 "wrong error %u\n", GetLastError() );
2162 ok( bytes
== 0, "read %x bytes\n", bytes
);
2164 ret
= VirtualProtect( mem
, 0x2000, PAGE_READWRITE
, &old_prot
);
2165 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2167 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2168 ok( !ret
, "ReadFile succeeded\n" );
2169 ok( GetLastError() == ERROR_NOACCESS
||
2170 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2171 "wrong error %u\n", GetLastError() );
2172 ok( bytes
== 0, "read %x bytes\n", bytes
);
2174 SetFilePointer( hFile
, 0x1234, NULL
, FILE_BEGIN
);
2175 SetEndOfFile( hFile
);
2176 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
2178 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2179 ok( !ret
, "ReadFile succeeded\n" );
2180 ok( GetLastError() == ERROR_NOACCESS
||
2181 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2182 "wrong error %u\n", GetLastError() );
2183 ok( bytes
== 0, "read %x bytes\n", bytes
);
2185 ret
= ReadFile( hFile
, mem
, 0x2000, &bytes
, NULL
);
2186 ok( ret
, "ReadFile failed error %u\n", GetLastError() );
2187 ok( bytes
== 0x1234, "read %x bytes\n", bytes
);
2189 ret
= ReadFile( hFile
, NULL
, 1, &bytes
, NULL
);
2190 ok( !ret
, "ReadFile succeeded\n" );
2191 ok( GetLastError() == ERROR_NOACCESS
||
2192 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2193 "wrong error %u\n", GetLastError() );
2194 ok( bytes
== 0, "read %x bytes\n", bytes
);
2196 VirtualFree( mem
, 0, MEM_FREE
);
2198 ret
= CloseHandle(hFile
);
2199 ok( ret
, "CloseHandle: error %d\n", GetLastError());
2200 ret
= DeleteFileA(filename
);
2201 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
2204 static void test_OpenFile(void)
2211 static const char file
[] = "regedit.exe";
2212 static const char foo
[] = ".\\foo-bar-foo.baz";
2213 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
2214 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2215 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2216 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2217 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2218 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2219 char buff
[MAX_PATH
];
2220 char buff_long
[4*MAX_PATH
];
2221 char filled_0xA5
[OFS_MAXPATHNAME
];
2225 /* Check for existing file */
2226 if (!pGetSystemWindowsDirectoryA
)
2227 length
= GetWindowsDirectoryA(buff
, MAX_PATH
);
2229 length
= pGetSystemWindowsDirectoryA(buff
, MAX_PATH
);
2231 if (length
+ sizeof(file
) < MAX_PATH
)
2233 p
= buff
+ strlen(buff
);
2234 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2236 memset(&ofs
, 0xA5, sizeof(ofs
));
2237 SetLastError(0xfaceabee);
2239 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
2240 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
2241 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2242 "GetLastError() returns %d\n", GetLastError() );
2243 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2244 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2245 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2246 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2247 ofs
.szPathName
, buff
);
2250 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
2251 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
2253 /* Check for nonexistent file */
2254 if (length
+ sizeof(foo
) < MAX_PATH
)
2256 p
= buff
+ strlen(buff
);
2257 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2258 strcpy( p
, foo
+ 2 );
2259 memset(&ofs
, 0xA5, sizeof(ofs
));
2260 SetLastError(0xfaceabee);
2262 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
2263 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
2264 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
2266 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2267 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2268 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
2269 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2270 ofs
.szPathName
, buff
);
2273 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
2274 length
+= lstrlenA(foo_too_long
+ 1);
2276 /* Check for nonexistent file with too long filename */
2277 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
2279 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
2280 memset(&ofs
, 0xA5, sizeof(ofs
));
2281 SetLastError(0xfaceabee);
2283 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
2284 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
2285 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
2286 "GetLastError() returns %d\n", GetLastError() );
2288 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2289 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
2290 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2291 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
2292 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2296 length
= GetCurrentDirectoryA(MAX_PATH
, buff
) + sizeof(filename
);
2298 if (length
>= MAX_PATH
)
2300 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
2303 p
= buff
+ strlen(buff
);
2304 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2305 strcpy( p
, filename
);
2307 memset(&ofs
, 0xA5, sizeof(ofs
));
2308 SetLastError(0xfaceabee);
2309 /* Create an empty file */
2310 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
2311 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
2312 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2313 "GetLastError() returns %d\n", GetLastError() );
2314 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2315 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2316 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2317 ret
= _lclose(hFile
);
2318 ok( !ret
, "_lclose() returns %d\n", ret
);
2319 retval
= GetFileAttributesA(filename
);
2320 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
2322 memset(&ofs
, 0xA5, sizeof(ofs
));
2323 SetLastError(0xfaceabee);
2324 /* Check various opening options: */
2325 /* for reading only, */
2326 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
2327 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
2328 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2329 "GetLastError() returns %d\n", GetLastError() );
2330 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2331 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2332 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2333 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2334 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2335 ret
= _lclose(hFile
);
2336 ok( !ret
, "_lclose() returns %d\n", ret
);
2338 memset(&ofs
, 0xA5, sizeof(ofs
));
2339 SetLastError(0xfaceabee);
2340 /* for writing only, */
2341 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
2342 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
2343 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2344 "GetLastError() returns %d\n", GetLastError() );
2345 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2346 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2347 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2348 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2349 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2350 ret
= _lclose(hFile
);
2351 ok( !ret
, "_lclose() returns %d\n", ret
);
2353 memset(&ofs
, 0xA5, sizeof(ofs
));
2354 SetLastError(0xfaceabee);
2355 /* for reading and writing, */
2356 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
2357 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
2358 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2359 "GetLastError() returns %d\n", GetLastError() );
2360 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2361 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2362 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2363 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2364 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2365 ret
= _lclose(hFile
);
2366 ok( !ret
, "_lclose() returns %d\n", ret
);
2368 memset(&ofs
, 0xA5, sizeof(ofs
));
2369 SetLastError(0xfaceabee);
2370 /* for checking file presence. */
2371 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
2372 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
2373 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2374 "GetLastError() returns %d\n", GetLastError() );
2375 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2376 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2377 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2378 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2379 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2381 memset(&ofs
, 0xA5, sizeof(ofs
));
2382 SetLastError(0xfaceabee);
2383 /* Delete the file and make sure it doesn't exist anymore */
2384 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
2385 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
2386 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2387 "GetLastError() returns %d\n", GetLastError() );
2388 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2389 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2390 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2391 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2392 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2394 retval
= GetFileAttributesA(filename
);
2395 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
2398 static void test_overlapped(void)
2403 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2404 if (0) /* tested: WinXP */
2406 GetOverlappedResult(0, NULL
, &result
, FALSE
);
2407 GetOverlappedResult(0, &ov
, NULL
, FALSE
);
2408 GetOverlappedResult(0, NULL
, NULL
, FALSE
);
2411 memset( &ov
, 0, sizeof ov
);
2413 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2415 ok( result
== 0, "wrong result %u\n", result
);
2417 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2421 ov
.InternalHigh
= 0xabcd;
2422 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2424 ok( result
== 0xabcd, "wrong result %u\n", result
);
2426 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2428 SetLastError( 0xb00 );
2430 ov
.Internal
= STATUS_INVALID_HANDLE
;
2431 ov
.InternalHigh
= 0xabcd;
2432 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2433 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2434 ok( r
== FALSE
, "should return false\n");
2435 ok( result
== 0xabcd || result
== 0 /* win9x */, "wrong result %u\n", result
);
2437 SetLastError( 0xb00 );
2439 ov
.Internal
= STATUS_PENDING
;
2440 ov
.InternalHigh
= 0xabcd;
2441 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2442 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2443 "wrong error %u\n", GetLastError() );
2444 ok( r
== FALSE
, "should return false\n");
2445 ok( result
== 0, "wrong result %u\n", result
);
2447 SetLastError( 0xb00 );
2448 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
2449 ov
.Internal
= STATUS_PENDING
;
2450 ov
.InternalHigh
= 0xabcd;
2451 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2452 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2453 "wrong error %u\n", GetLastError() );
2454 ok( r
== FALSE
, "should return false\n");
2456 ResetEvent( ov
.hEvent
);
2458 SetLastError( 0xb00 );
2459 ov
.Internal
= STATUS_PENDING
;
2460 ov
.InternalHigh
= 0;
2461 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2462 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2463 "wrong error %u\n", GetLastError() );
2464 ok( r
== FALSE
, "should return false\n");
2466 r
= CloseHandle( ov
.hEvent
);
2467 ok( r
== TRUE
, "close handle failed\n");
2470 static void test_RemoveDirectory(void)
2473 char directory
[] = "removeme";
2475 rc
= CreateDirectory(directory
, NULL
);
2476 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
2478 rc
= SetCurrentDirectory(directory
);
2479 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2481 rc
= RemoveDirectory(".");
2484 rc
= SetCurrentDirectory("..");
2485 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2487 rc
= RemoveDirectory(directory
);
2488 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2492 static BOOL
check_file_time( const FILETIME
*ft1
, const FILETIME
*ft2
, UINT tolerance
)
2494 ULONGLONG t1
= ((ULONGLONG
)ft1
->dwHighDateTime
<< 32) | ft1
->dwLowDateTime
;
2495 ULONGLONG t2
= ((ULONGLONG
)ft2
->dwHighDateTime
<< 32) | ft2
->dwLowDateTime
;
2496 return abs(t1
- t2
) <= tolerance
;
2499 static void test_ReplaceFileA(void)
2501 char replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2502 HANDLE hReplacedFile
, hReplacementFile
, hBackupFile
;
2503 static const char replacedData
[] = "file-to-replace";
2504 static const char replacementData
[] = "new-file";
2505 static const char backupData
[] = "backup-file";
2506 FILETIME ftReplaced
, ftReplacement
, ftBackup
;
2507 static const char prefix
[] = "pfx";
2508 char temp_path
[MAX_PATH
];
2510 BOOL retok
, removeBackup
= FALSE
;
2514 win_skip("ReplaceFileA() is missing\n");
2518 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2519 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2520 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2522 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replaced
);
2523 ok(ret
!= 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2525 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2526 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2528 ret
= GetTempFileNameA(temp_path
, prefix
, 0, backup
);
2529 ok(ret
!= 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2531 /* place predictable data in the file to be replaced */
2532 hReplacedFile
= CreateFileA(replaced
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2533 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2534 "failed to open replaced file\n");
2535 retok
= WriteFile(hReplacedFile
, replacedData
, sizeof(replacedData
), &ret
, NULL
);
2536 ok( retok
&& ret
== sizeof(replacedData
),
2537 "WriteFile error (replaced) %d\n", GetLastError());
2538 ok(GetFileSize(hReplacedFile
, NULL
) == sizeof(replacedData
),
2539 "replaced file has wrong size\n");
2540 /* place predictable data in the file to be the replacement */
2541 hReplacementFile
= CreateFileA(replacement
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2542 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
,
2543 "failed to open replacement file\n");
2544 retok
= WriteFile(hReplacementFile
, replacementData
, sizeof(replacementData
), &ret
, NULL
);
2545 ok( retok
&& ret
== sizeof(replacementData
),
2546 "WriteFile error (replacement) %d\n", GetLastError());
2547 ok(GetFileSize(hReplacementFile
, NULL
) == sizeof(replacementData
),
2548 "replacement file has wrong size\n");
2549 /* place predictable data in the backup file (to be over-written) */
2550 hBackupFile
= CreateFileA(backup
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2551 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2552 "failed to open backup file\n");
2553 retok
= WriteFile(hBackupFile
, backupData
, sizeof(backupData
), &ret
, NULL
);
2554 ok( retok
&& ret
== sizeof(backupData
),
2555 "WriteFile error (replacement) %d\n", GetLastError());
2556 ok(GetFileSize(hBackupFile
, NULL
) == sizeof(backupData
),
2557 "backup file has wrong size\n");
2558 /* change the filetime on the "replaced" file to ensure that it changes */
2559 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2560 ok( ret
, "GetFileTime error (replaced) %d\n", GetLastError());
2561 ftReplaced
.dwLowDateTime
-= 600000000; /* 60 second */
2562 ret
= SetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2563 ok( ret
, "SetFileTime error (replaced) %d\n", GetLastError());
2564 GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
); /* get the actual time back */
2565 CloseHandle(hReplacedFile
);
2566 /* change the filetime on the backup to ensure that it changes */
2567 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2568 ok( ret
, "GetFileTime error (backup) %d\n", GetLastError());
2569 ftBackup
.dwLowDateTime
-= 1200000000; /* 120 second */
2570 ret
= SetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2571 ok( ret
, "SetFileTime error (backup) %d\n", GetLastError());
2572 GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
); /* get the actual time back */
2573 CloseHandle(hBackupFile
);
2574 /* get the filetime on the replacement file to perform checks */
2575 ret
= GetFileTime(hReplacementFile
, NULL
, NULL
, &ftReplacement
);
2576 ok( ret
, "GetFileTime error (replacement) %d\n", GetLastError());
2577 CloseHandle(hReplacementFile
);
2579 /* perform replacement w/ backup
2580 * TODO: flags are not implemented
2582 SetLastError(0xdeadbeef);
2583 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2584 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2585 /* make sure that the backup has the size of the old "replaced" file */
2586 hBackupFile
= CreateFileA(backup
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2587 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2588 "failed to open backup file\n");
2589 ret
= GetFileSize(hBackupFile
, NULL
);
2590 ok(ret
== sizeof(replacedData
),
2591 "backup file has wrong size %d\n", ret
);
2592 /* make sure that the "replaced" file has the size of the replacement file */
2593 hReplacedFile
= CreateFileA(replaced
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2594 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2595 "failed to open replaced file: %d\n", GetLastError());
2596 if (hReplacedFile
!= INVALID_HANDLE_VALUE
)
2598 ret
= GetFileSize(hReplacedFile
, NULL
);
2599 ok(ret
== sizeof(replacementData
),
2600 "replaced file has wrong size %d\n", ret
);
2601 /* make sure that the replacement file no-longer exists */
2602 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2603 ok(hReplacementFile
== INVALID_HANDLE_VALUE
,
2604 "unexpected error, replacement file should not exist %d\n", GetLastError());
2605 /* make sure that the backup has the old "replaced" filetime */
2606 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2607 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2608 ok(check_file_time(&ftBackup
, &ftReplaced
, 20000000), "backup file has wrong filetime\n");
2609 CloseHandle(hBackupFile
);
2610 /* make sure that the "replaced" has the old replacement filetime */
2611 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2612 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2613 ok(check_file_time(&ftReplaced
, &ftReplacement
, 20000000),
2614 "replaced file has wrong filetime %x%08x / %x%08x\n",
2615 ftReplaced
.dwHighDateTime
, ftReplaced
.dwLowDateTime
,
2616 ftReplacement
.dwHighDateTime
, ftReplacement
.dwLowDateTime
);
2617 CloseHandle(hReplacedFile
);
2620 skip("couldn't open replacement file, skipping tests\n");
2622 /* re-create replacement file for pass w/o backup (blank) */
2623 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2624 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2625 /* perform replacement w/o backup
2626 * TODO: flags are not implemented
2628 SetLastError(0xdeadbeef);
2629 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2630 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2631 "ReplaceFileA: unexpected error %d\n", GetLastError());
2633 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2634 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2635 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2636 ret
= DeleteFileA(backup
);
2637 ok(ret
, "DeleteFileA: error (backup) %d\n", GetLastError());
2638 /* perform replacement w/ backup (no pre-existing backup)
2639 * TODO: flags are not implemented
2641 SetLastError(0xdeadbeef);
2642 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2643 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2644 "ReplaceFileA: unexpected error %d\n", GetLastError());
2646 removeBackup
= TRUE
;
2648 /* re-create replacement file for pass w/ no permissions to "replaced" */
2649 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2650 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2651 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_READONLY
);
2652 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2653 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2654 /* perform replacement w/ backup (no permission to "replaced")
2655 * TODO: flags are not implemented
2657 SetLastError(0xdeadbeef);
2658 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2659 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2660 /* make sure that the replacement file still exists */
2661 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2662 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
||
2663 broken(GetLastError() == ERROR_FILE_NOT_FOUND
), /* win2k */
2664 "unexpected error, replacement file should still exist %d\n", GetLastError());
2665 CloseHandle(hReplacementFile
);
2666 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_NORMAL
);
2667 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2668 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2670 /* replacement file still exists, make pass w/o "replaced" */
2671 ret
= DeleteFileA(replaced
);
2672 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2673 "DeleteFileA: error (replaced) %d\n", GetLastError());
2674 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2675 * TODO: flags are not implemented
2677 SetLastError(0xdeadbeef);
2678 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2679 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2680 GetLastError() == ERROR_ACCESS_DENIED
),
2681 "ReplaceFileA: unexpected error %d\n", GetLastError());
2683 /* perform replacement w/o existing "replacement" file
2684 * TODO: flags are not implemented
2686 SetLastError(0xdeadbeef);
2687 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2688 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2689 GetLastError() == ERROR_ACCESS_DENIED
),
2690 "ReplaceFileA: unexpected error %d\n", GetLastError());
2693 * if the first round (w/ backup) worked then as long as there is no
2694 * failure then there is no need to check this round (w/ backup is the
2695 * more complete case)
2698 /* delete temporary files, replacement and replaced are already deleted */
2701 ret
= DeleteFileA(backup
);
2703 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2704 "DeleteFileA: error (backup) %d\n", GetLastError());
2709 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2710 * need to be as thorough.
2712 static void test_ReplaceFileW(void)
2714 WCHAR replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2715 static const WCHAR prefix
[] = {'p','f','x',0};
2716 WCHAR temp_path
[MAX_PATH
];
2718 BOOL removeBackup
= FALSE
;
2722 win_skip("ReplaceFileW() is missing\n");
2726 ret
= GetTempPathW(MAX_PATH
, temp_path
);
2727 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2729 win_skip("GetTempPathW is not available\n");
2732 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
2733 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2735 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replaced
);
2736 ok(ret
!= 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2738 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2739 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2741 ret
= GetTempFileNameW(temp_path
, prefix
, 0, backup
);
2742 ok(ret
!= 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2744 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2745 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2747 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2748 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2749 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2750 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2751 "ReplaceFileW: error %d\n", GetLastError());
2753 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2754 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2755 ret
= DeleteFileW(backup
);
2756 ok(ret
, "DeleteFileW: error (backup) %d\n", GetLastError());
2757 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2758 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2759 "ReplaceFileW: error %d\n", GetLastError());
2761 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2762 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2763 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_READONLY
);
2764 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2765 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2767 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2768 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
,
2769 "ReplaceFileW: unexpected error %d\n", GetLastError());
2770 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_NORMAL
);
2771 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2772 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2774 removeBackup
= TRUE
;
2776 ret
= DeleteFileW(replaced
);
2777 ok(ret
, "DeleteFileW: error (replaced) %d\n", GetLastError());
2778 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2779 ok(!ret
, "ReplaceFileW: error %d\n", GetLastError());
2781 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2782 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2783 GetLastError() == ERROR_ACCESS_DENIED
),
2784 "ReplaceFileW: unexpected error %d\n", GetLastError());
2788 ret
= DeleteFileW(backup
);
2790 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2791 "DeleteFileW: error (backup) %d\n", GetLastError());
2797 InitFunctionPointers();
2807 test_GetTempFileNameA();
2816 test_FindFirstFileA();
2817 test_FindNextFileA();
2818 test_FindFirstFileExA(0);
2819 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
2820 test_FindFirstFileExA(FindExSearchLimitToDirectories
);
2822 test_file_sharing();
2823 test_offset_in_overlapped_structure();
2826 test_async_file_errors();
2830 test_RemoveDirectory();
2831 test_ReplaceFileA();
2832 test_ReplaceFileW();