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
< (long)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(void)
1865 WIN32_FIND_DATAA search_results
;
1868 if (!pFindFirstFileExA
)
1870 win_skip("FindFirstFileExA() is missing\n");
1874 CreateDirectoryA("test-dir", NULL
);
1875 _lclose(_lcreat("test-dir\\file1", 0));
1876 _lclose(_lcreat("test-dir\\file2", 0));
1877 CreateDirectoryA("test-dir\\dir1", NULL
);
1878 /* FindExLimitToDirectories is ignored */
1879 SetLastError(0xdeadbeef);
1880 handle
= pFindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, FindExSearchLimitToDirectories
, 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 ok(FindNextFile(handle
, &search_results
), "Fetching fourth file failed\n");
1898 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
1900 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
1901 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
1905 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should failed\n");
1907 FindClose( handle
);
1910 DeleteFileA("test-dir\\file1");
1911 DeleteFileA("test-dir\\file2");
1912 RemoveDirectoryA("test-dir\\dir1");
1913 RemoveDirectoryA("test-dir");
1916 static int test_Mapfile_createtemp(HANDLE
*handle
)
1918 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1919 DeleteFile(filename
);
1920 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1921 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1922 if (*handle
!= INVALID_HANDLE_VALUE
) {
1930 static void test_MapFile(void)
1935 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1937 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1938 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1940 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1942 /* We have to close file before we try new stuff with mapping again.
1943 Else we would always succeed on XP or block descriptors on 95. */
1944 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1945 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1946 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1947 ok( CloseHandle( handle
), "can't close file handle\n");
1950 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1952 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1953 ok( hmap
== NULL
, "mapped zero size file\n");
1954 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1956 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
1957 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1958 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1960 CloseHandle( hmap
);
1962 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
1963 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1964 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1966 CloseHandle( hmap
);
1968 /* On XP you can now map again, on Win 95 you cannot. */
1970 ok( CloseHandle( handle
), "can't close file handle\n");
1971 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1974 static void test_GetFileType(void)
1977 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1978 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1979 type
= GetFileType(h
);
1980 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
1982 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1983 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1984 type
= GetFileType(h
);
1985 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
1987 DeleteFileA( filename
);
1990 static int completion_count
;
1992 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
1994 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1995 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
1999 static void test_async_file_errors(void)
2001 char szFile
[MAX_PATH
];
2002 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
2004 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
2006 S(U(ovl
)).Offset
= 0;
2007 S(U(ovl
)).OffsetHigh
= 0;
2009 completion_count
= 0;
2011 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
2012 strcat(szFile
, "\\win.ini");
2013 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2014 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
2015 if (hFile
== INVALID_HANDLE_VALUE
) /* win9x doesn't like FILE_SHARE_DELETE */
2016 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2017 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
2018 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA(%s ...) failed\n", szFile
);
2023 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
2025 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
2026 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2029 if (!GetOverlappedResult(hFile
, &ovl
, &count
, FALSE
))
2031 S(U(ovl
)).Offset
+= count
;
2032 /* i/o completion routine only called if ReadFileEx returned success.
2033 * we only care about violations of this rule so undo what should have
2037 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
2038 /*printf("Error = %ld\n", GetLastError());*/
2039 HeapFree(GetProcessHeap(), 0, lpBuffer
);
2042 static void test_read_write(void)
2044 DWORD bytes
, ret
, old_prot
;
2046 char temp_path
[MAX_PATH
];
2047 char filename
[MAX_PATH
];
2049 static const char prefix
[] = "pfx";
2051 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2052 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2053 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2055 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
2056 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
2058 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
2059 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
2060 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
2062 SetLastError(12345678);
2064 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
2065 ok(ret
&& GetLastError() == 12345678,
2066 "ret = %d, error %d\n", ret
, GetLastError());
2067 ok(!bytes
, "bytes = %d\n", bytes
);
2069 SetLastError(12345678);
2071 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
2072 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
2073 (ret
&& GetLastError() == 12345678), /* Win9x */
2074 "ret = %d, error %d\n", ret
, GetLastError());
2075 ok(!bytes
|| /* Win2k */
2076 bytes
== 10, /* Win9x */
2077 "bytes = %d\n", bytes
);
2079 /* make sure the file contains data */
2080 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
2081 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
2083 SetLastError(12345678);
2085 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
2086 ok(ret
&& GetLastError() == 12345678,
2087 "ret = %d, error %d\n", ret
, GetLastError());
2088 ok(!bytes
, "bytes = %d\n", bytes
);
2090 SetLastError(12345678);
2092 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
2093 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
2094 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
2095 "ret = %d, error %d\n", ret
, GetLastError());
2096 ok(!bytes
, "bytes = %d\n", bytes
);
2098 /* test passing protected memory as buffer */
2100 mem
= VirtualAlloc( NULL
, 0x4000, MEM_COMMIT
, PAGE_READWRITE
);
2101 ok( mem
!= NULL
, "failed to allocate virtual mem error %u\n", GetLastError() );
2103 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2104 ok( ret
, "WriteFile failed error %u\n", GetLastError() );
2105 ok( bytes
== 0x4000, "only wrote %x bytes\n", bytes
);
2107 ret
= VirtualProtect( mem
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
2108 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2110 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2111 ok( !ret
, "WriteFile succeeded\n" );
2112 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
2113 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2114 "wrong error %u\n", GetLastError() );
2115 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2117 ret
= WriteFile( (HANDLE
)0xdead, mem
, 0x4000, &bytes
, NULL
);
2118 ok( !ret
, "WriteFile succeeded\n" );
2119 ok( GetLastError() == ERROR_INVALID_HANDLE
|| /* handle is checked before buffer on NT */
2120 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2121 "wrong error %u\n", GetLastError() );
2122 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2124 ret
= VirtualProtect( mem
, 0x2000, PAGE_NOACCESS
, &old_prot
);
2125 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2127 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2128 ok( !ret
, "WriteFile succeeded\n" );
2129 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
2130 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2131 "wrong error %u\n", GetLastError() );
2132 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
2134 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
2136 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2137 ok( !ret
, "ReadFile succeeded\n" );
2138 ok( GetLastError() == ERROR_NOACCESS
||
2139 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2140 "wrong error %u\n", GetLastError() );
2141 ok( bytes
== 0, "read %x bytes\n", bytes
);
2143 ret
= VirtualProtect( mem
, 0x2000, PAGE_READONLY
, &old_prot
);
2144 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2146 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2147 ok( !ret
, "ReadFile succeeded\n" );
2148 ok( GetLastError() == ERROR_NOACCESS
||
2149 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2150 "wrong error %u\n", GetLastError() );
2151 ok( bytes
== 0, "read %x bytes\n", bytes
);
2153 ret
= VirtualProtect( mem
, 0x2000, PAGE_READWRITE
, &old_prot
);
2154 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
2156 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
2157 ok( !ret
, "ReadFile succeeded\n" );
2158 ok( GetLastError() == ERROR_NOACCESS
||
2159 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
2160 "wrong error %u\n", GetLastError() );
2161 ok( bytes
== 0, "read %x bytes\n", bytes
);
2163 SetFilePointer( hFile
, 0x1234, NULL
, FILE_BEGIN
);
2164 SetEndOfFile( hFile
);
2165 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
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 ret
= ReadFile( hFile
, mem
, 0x2000, &bytes
, NULL
);
2175 ok( ret
, "ReadFile failed error %u\n", GetLastError() );
2176 ok( bytes
== 0x1234, "read %x bytes\n", bytes
);
2178 ret
= ReadFile( hFile
, NULL
, 1, &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 VirtualFree( mem
, 0, MEM_FREE
);
2187 ret
= CloseHandle(hFile
);
2188 ok( ret
, "CloseHandle: error %d\n", GetLastError());
2189 ret
= DeleteFileA(filename
);
2190 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
2193 static void test_OpenFile(void)
2200 static const char file
[] = "regedit.exe";
2201 static const char foo
[] = ".\\foo-bar-foo.baz";
2202 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
2203 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2204 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2205 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2206 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2207 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2208 char buff
[MAX_PATH
];
2209 char buff_long
[4*MAX_PATH
];
2210 char filled_0xA5
[OFS_MAXPATHNAME
];
2214 /* Check for existing file */
2215 if (!pGetSystemWindowsDirectoryA
)
2216 length
= GetWindowsDirectoryA(buff
, MAX_PATH
);
2218 length
= pGetSystemWindowsDirectoryA(buff
, MAX_PATH
);
2220 if (length
+ sizeof(file
) < MAX_PATH
)
2222 p
= buff
+ strlen(buff
);
2223 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2225 memset(&ofs
, 0xA5, sizeof(ofs
));
2226 SetLastError(0xfaceabee);
2228 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
2229 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
2230 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2231 "GetLastError() returns %d\n", GetLastError() );
2232 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2233 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2234 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2235 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2236 ofs
.szPathName
, buff
);
2239 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
2240 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
2242 /* Check for nonexistent file */
2243 if (length
+ sizeof(foo
) < MAX_PATH
)
2245 p
= buff
+ strlen(buff
);
2246 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2247 strcpy( p
, foo
+ 2 );
2248 memset(&ofs
, 0xA5, sizeof(ofs
));
2249 SetLastError(0xfaceabee);
2251 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
2252 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
2253 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
2255 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2256 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2257 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
2258 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2259 ofs
.szPathName
, buff
);
2262 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
2263 length
+= lstrlenA(foo_too_long
+ 1);
2265 /* Check for nonexistent file with too long filename */
2266 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
2268 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
2269 memset(&ofs
, 0xA5, sizeof(ofs
));
2270 SetLastError(0xfaceabee);
2272 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
2273 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
2274 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
2275 "GetLastError() returns %d\n", GetLastError() );
2277 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2278 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
2279 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2280 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
2281 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2285 length
= GetCurrentDirectoryA(MAX_PATH
, buff
) + sizeof(filename
);
2287 if (length
>= MAX_PATH
)
2289 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
2292 p
= buff
+ strlen(buff
);
2293 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
2294 strcpy( p
, filename
);
2296 memset(&ofs
, 0xA5, sizeof(ofs
));
2297 SetLastError(0xfaceabee);
2298 /* Create an empty file */
2299 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
2300 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
2301 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2302 "GetLastError() returns %d\n", GetLastError() );
2303 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2304 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2305 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2306 ret
= _lclose(hFile
);
2307 ok( !ret
, "_lclose() returns %d\n", ret
);
2308 retval
= GetFileAttributesA(filename
);
2309 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
2311 memset(&ofs
, 0xA5, sizeof(ofs
));
2312 SetLastError(0xfaceabee);
2313 /* Check various opening options: */
2314 /* for reading only, */
2315 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
2316 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
2317 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2318 "GetLastError() returns %d\n", GetLastError() );
2319 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2320 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2321 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2322 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2323 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2324 ret
= _lclose(hFile
);
2325 ok( !ret
, "_lclose() returns %d\n", ret
);
2327 memset(&ofs
, 0xA5, sizeof(ofs
));
2328 SetLastError(0xfaceabee);
2329 /* for writing only, */
2330 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
2331 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
2332 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2333 "GetLastError() returns %d\n", GetLastError() );
2334 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2335 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2336 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2337 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2338 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2339 ret
= _lclose(hFile
);
2340 ok( !ret
, "_lclose() returns %d\n", ret
);
2342 memset(&ofs
, 0xA5, sizeof(ofs
));
2343 SetLastError(0xfaceabee);
2344 /* for reading and writing, */
2345 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
2346 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
2347 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2348 "GetLastError() returns %d\n", GetLastError() );
2349 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2350 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2351 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2352 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2353 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2354 ret
= _lclose(hFile
);
2355 ok( !ret
, "_lclose() returns %d\n", ret
);
2357 memset(&ofs
, 0xA5, sizeof(ofs
));
2358 SetLastError(0xfaceabee);
2359 /* for checking file presence. */
2360 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
2361 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
2362 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2363 "GetLastError() returns %d\n", GetLastError() );
2364 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2365 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2366 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2367 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2368 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2370 memset(&ofs
, 0xA5, sizeof(ofs
));
2371 SetLastError(0xfaceabee);
2372 /* Delete the file and make sure it doesn't exist anymore */
2373 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
2374 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
2375 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2376 "GetLastError() returns %d\n", GetLastError() );
2377 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2378 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2379 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2380 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2381 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2383 retval
= GetFileAttributesA(filename
);
2384 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
2387 static void test_overlapped(void)
2392 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2393 if (0) /* tested: WinXP */
2395 GetOverlappedResult(0, NULL
, &result
, FALSE
);
2396 GetOverlappedResult(0, &ov
, NULL
, FALSE
);
2397 GetOverlappedResult(0, NULL
, NULL
, FALSE
);
2400 memset( &ov
, 0, sizeof ov
);
2402 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2404 ok( result
== 0, "wrong result %u\n", result
);
2406 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2410 ov
.InternalHigh
= 0xabcd;
2411 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2413 ok( result
== 0xabcd, "wrong result %u\n", result
);
2415 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2417 SetLastError( 0xb00 );
2419 ov
.Internal
= STATUS_INVALID_HANDLE
;
2420 ov
.InternalHigh
= 0xabcd;
2421 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2422 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2423 ok( r
== FALSE
, "should return false\n");
2424 ok( result
== 0xabcd || result
== 0 /* win9x */, "wrong result %u\n", result
);
2426 SetLastError( 0xb00 );
2428 ov
.Internal
= STATUS_PENDING
;
2429 ov
.InternalHigh
= 0xabcd;
2430 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2431 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2432 "wrong error %u\n", GetLastError() );
2433 ok( r
== FALSE
, "should return false\n");
2434 ok( result
== 0, "wrong result %u\n", result
);
2436 SetLastError( 0xb00 );
2437 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
2438 ov
.Internal
= STATUS_PENDING
;
2439 ov
.InternalHigh
= 0xabcd;
2440 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2441 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2442 "wrong error %u\n", GetLastError() );
2443 ok( r
== FALSE
, "should return false\n");
2445 ResetEvent( ov
.hEvent
);
2447 SetLastError( 0xb00 );
2448 ov
.Internal
= STATUS_PENDING
;
2449 ov
.InternalHigh
= 0;
2450 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2451 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2452 "wrong error %u\n", GetLastError() );
2453 ok( r
== FALSE
, "should return false\n");
2455 r
= CloseHandle( ov
.hEvent
);
2456 ok( r
== TRUE
, "close handle failed\n");
2459 static void test_RemoveDirectory(void)
2462 char directory
[] = "removeme";
2464 rc
= CreateDirectory(directory
, NULL
);
2465 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
2467 rc
= SetCurrentDirectory(directory
);
2468 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2470 rc
= RemoveDirectory(".");
2473 rc
= SetCurrentDirectory("..");
2474 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2476 rc
= RemoveDirectory(directory
);
2477 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2481 static BOOL
check_file_time( const FILETIME
*ft1
, const FILETIME
*ft2
, UINT tolerance
)
2483 ULONGLONG t1
= ((ULONGLONG
)ft1
->dwHighDateTime
<< 32) | ft1
->dwLowDateTime
;
2484 ULONGLONG t2
= ((ULONGLONG
)ft2
->dwHighDateTime
<< 32) | ft2
->dwLowDateTime
;
2485 return abs(t1
- t2
) <= tolerance
;
2488 static void test_ReplaceFileA(void)
2490 char replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2491 HANDLE hReplacedFile
, hReplacementFile
, hBackupFile
;
2492 static const char replacedData
[] = "file-to-replace";
2493 static const char replacementData
[] = "new-file";
2494 static const char backupData
[] = "backup-file";
2495 FILETIME ftReplaced
, ftReplacement
, ftBackup
;
2496 static const char prefix
[] = "pfx";
2497 char temp_path
[MAX_PATH
];
2499 BOOL retok
, removeBackup
= FALSE
;
2503 win_skip("ReplaceFileA() is missing\n");
2507 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2508 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2509 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2511 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replaced
);
2512 ok(ret
!= 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2514 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2515 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2517 ret
= GetTempFileNameA(temp_path
, prefix
, 0, backup
);
2518 ok(ret
!= 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2520 /* place predictable data in the file to be replaced */
2521 hReplacedFile
= CreateFileA(replaced
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2522 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2523 "failed to open replaced file\n");
2524 retok
= WriteFile(hReplacedFile
, replacedData
, sizeof(replacedData
), &ret
, NULL
);
2525 ok( retok
&& ret
== sizeof(replacedData
),
2526 "WriteFile error (replaced) %d\n", GetLastError());
2527 ok(GetFileSize(hReplacedFile
, NULL
) == sizeof(replacedData
),
2528 "replaced file has wrong size\n");
2529 /* place predictable data in the file to be the replacement */
2530 hReplacementFile
= CreateFileA(replacement
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2531 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
,
2532 "failed to open replacement file\n");
2533 retok
= WriteFile(hReplacementFile
, replacementData
, sizeof(replacementData
), &ret
, NULL
);
2534 ok( retok
&& ret
== sizeof(replacementData
),
2535 "WriteFile error (replacement) %d\n", GetLastError());
2536 ok(GetFileSize(hReplacementFile
, NULL
) == sizeof(replacementData
),
2537 "replacement file has wrong size\n");
2538 /* place predictable data in the backup file (to be over-written) */
2539 hBackupFile
= CreateFileA(backup
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2540 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2541 "failed to open backup file\n");
2542 retok
= WriteFile(hBackupFile
, backupData
, sizeof(backupData
), &ret
, NULL
);
2543 ok( retok
&& ret
== sizeof(backupData
),
2544 "WriteFile error (replacement) %d\n", GetLastError());
2545 ok(GetFileSize(hBackupFile
, NULL
) == sizeof(backupData
),
2546 "backup file has wrong size\n");
2547 /* change the filetime on the "replaced" file to ensure that it changes */
2548 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2549 ok( ret
, "GetFileTime error (replaced) %d\n", GetLastError());
2550 ftReplaced
.dwLowDateTime
-= 600000000; /* 60 second */
2551 ret
= SetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2552 ok( ret
, "SetFileTime error (replaced) %d\n", GetLastError());
2553 GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
); /* get the actual time back */
2554 CloseHandle(hReplacedFile
);
2555 /* change the filetime on the backup to ensure that it changes */
2556 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2557 ok( ret
, "GetFileTime error (backup) %d\n", GetLastError());
2558 ftBackup
.dwLowDateTime
-= 1200000000; /* 120 second */
2559 ret
= SetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2560 ok( ret
, "SetFileTime error (backup) %d\n", GetLastError());
2561 GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
); /* get the actual time back */
2562 CloseHandle(hBackupFile
);
2563 /* get the filetime on the replacement file to perform checks */
2564 ret
= GetFileTime(hReplacementFile
, NULL
, NULL
, &ftReplacement
);
2565 ok( ret
, "GetFileTime error (replacement) %d\n", GetLastError());
2566 CloseHandle(hReplacementFile
);
2568 /* perform replacement w/ backup
2569 * TODO: flags are not implemented
2571 SetLastError(0xdeadbeef);
2572 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2573 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2574 /* make sure that the backup has the size of the old "replaced" file */
2575 hBackupFile
= CreateFileA(backup
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2576 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2577 "failed to open backup file\n");
2578 ret
= GetFileSize(hBackupFile
, NULL
);
2579 ok(ret
== sizeof(replacedData
),
2580 "backup file has wrong size %d\n", ret
);
2581 /* make sure that the "replaced" file has the size of the replacement file */
2582 hReplacedFile
= CreateFileA(replaced
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2583 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2584 "failed to open replaced file: %d\n", GetLastError());
2585 if (hReplacedFile
!= INVALID_HANDLE_VALUE
)
2587 ret
= GetFileSize(hReplacedFile
, NULL
);
2588 ok(ret
== sizeof(replacementData
),
2589 "replaced file has wrong size %d\n", ret
);
2590 /* make sure that the replacement file no-longer exists */
2591 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2592 ok(hReplacementFile
== INVALID_HANDLE_VALUE
,
2593 "unexpected error, replacement file should not exist %d\n", GetLastError());
2594 /* make sure that the backup has the old "replaced" filetime */
2595 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2596 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2597 ok(check_file_time(&ftBackup
, &ftReplaced
, 20000000), "backup file has wrong filetime\n");
2598 CloseHandle(hBackupFile
);
2599 /* make sure that the "replaced" has the old replacement filetime */
2600 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2601 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2602 ok(check_file_time(&ftReplaced
, &ftReplacement
, 20000000),
2603 "replaced file has wrong filetime %x%08x / %x%08x\n",
2604 ftReplaced
.dwHighDateTime
, ftReplaced
.dwLowDateTime
,
2605 ftReplacement
.dwHighDateTime
, ftReplacement
.dwLowDateTime
);
2606 CloseHandle(hReplacedFile
);
2609 skip("couldn't open replacement file, skipping tests\n");
2611 /* re-create replacement file for pass w/o backup (blank) */
2612 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2613 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2614 /* perform replacement w/o backup
2615 * TODO: flags are not implemented
2617 SetLastError(0xdeadbeef);
2618 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2619 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2620 "ReplaceFileA: unexpected error %d\n", GetLastError());
2622 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2623 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2624 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2625 ret
= DeleteFileA(backup
);
2626 ok(ret
, "DeleteFileA: error (backup) %d\n", GetLastError());
2627 /* perform replacement w/ backup (no pre-existing backup)
2628 * TODO: flags are not implemented
2630 SetLastError(0xdeadbeef);
2631 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2632 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2633 "ReplaceFileA: unexpected error %d\n", GetLastError());
2635 removeBackup
= TRUE
;
2637 /* re-create replacement file for pass w/ no permissions to "replaced" */
2638 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2639 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2640 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_READONLY
);
2641 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2642 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2643 /* perform replacement w/ backup (no permission to "replaced")
2644 * TODO: flags are not implemented
2646 SetLastError(0xdeadbeef);
2647 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2648 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2649 /* make sure that the replacement file still exists */
2650 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2651 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
||
2652 broken(GetLastError() == ERROR_FILE_NOT_FOUND
), /* win2k */
2653 "unexpected error, replacement file should still exist %d\n", GetLastError());
2654 CloseHandle(hReplacementFile
);
2655 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_NORMAL
);
2656 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2657 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2659 /* replacement file still exists, make pass w/o "replaced" */
2660 ret
= DeleteFileA(replaced
);
2661 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2662 "DeleteFileA: error (replaced) %d\n", GetLastError());
2663 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2664 * TODO: flags are not implemented
2666 SetLastError(0xdeadbeef);
2667 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2668 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2669 GetLastError() == ERROR_ACCESS_DENIED
),
2670 "ReplaceFileA: unexpected error %d\n", GetLastError());
2672 /* perform replacement w/o existing "replacement" file
2673 * TODO: flags are not implemented
2675 SetLastError(0xdeadbeef);
2676 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2677 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2678 GetLastError() == ERROR_ACCESS_DENIED
),
2679 "ReplaceFileA: unexpected error %d\n", GetLastError());
2682 * if the first round (w/ backup) worked then as long as there is no
2683 * failure then there is no need to check this round (w/ backup is the
2684 * more complete case)
2687 /* delete temporary files, replacement and replaced are already deleted */
2690 ret
= DeleteFileA(backup
);
2692 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2693 "DeleteFileA: error (backup) %d\n", GetLastError());
2698 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2699 * need to be as thorough.
2701 static void test_ReplaceFileW(void)
2703 WCHAR replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2704 static const WCHAR prefix
[] = {'p','f','x',0};
2705 WCHAR temp_path
[MAX_PATH
];
2707 BOOL removeBackup
= FALSE
;
2711 win_skip("ReplaceFileW() is missing\n");
2715 ret
= GetTempPathW(MAX_PATH
, temp_path
);
2716 if (ret
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2718 win_skip("GetTempPathW is not available\n");
2721 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
2722 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2724 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replaced
);
2725 ok(ret
!= 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2727 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2728 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2730 ret
= GetTempFileNameW(temp_path
, prefix
, 0, backup
);
2731 ok(ret
!= 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2733 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2734 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2736 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2737 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2738 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2739 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2740 "ReplaceFileW: error %d\n", GetLastError());
2742 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2743 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2744 ret
= DeleteFileW(backup
);
2745 ok(ret
, "DeleteFileW: error (backup) %d\n", GetLastError());
2746 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2747 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2748 "ReplaceFileW: error %d\n", GetLastError());
2750 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2751 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2752 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_READONLY
);
2753 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2754 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2756 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2757 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
,
2758 "ReplaceFileW: unexpected error %d\n", GetLastError());
2759 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_NORMAL
);
2760 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2761 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2763 removeBackup
= TRUE
;
2765 ret
= DeleteFileW(replaced
);
2766 ok(ret
, "DeleteFileW: error (replaced) %d\n", GetLastError());
2767 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2768 ok(!ret
, "ReplaceFileW: error %d\n", GetLastError());
2770 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2771 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2772 GetLastError() == ERROR_ACCESS_DENIED
),
2773 "ReplaceFileW: unexpected error %d\n", GetLastError());
2777 ret
= DeleteFileW(backup
);
2779 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2780 "DeleteFileW: error (backup) %d\n", GetLastError());
2786 InitFunctionPointers();
2796 test_GetTempFileNameA();
2805 test_FindFirstFileA();
2806 test_FindNextFileA();
2807 test_FindFirstFileExA();
2809 test_file_sharing();
2810 test_offset_in_overlapped_structure();
2813 test_async_file_errors();
2817 test_RemoveDirectory();
2818 test_ReplaceFileA();
2819 test_ReplaceFileW();