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
30 #include "wine/test.h"
35 static HANDLE (WINAPI
*pFindFirstFileExA
)(LPCSTR
,FINDEX_INFO_LEVELS
,LPVOID
,FINDEX_SEARCH_OPS
,LPVOID
,DWORD
);
36 static BOOL (WINAPI
*pReplaceFileA
)(LPCSTR
, LPCSTR
, LPCSTR
, DWORD
, LPVOID
, LPVOID
);
37 static BOOL (WINAPI
*pReplaceFileW
)(LPCWSTR
, LPCWSTR
, LPCWSTR
, DWORD
, LPVOID
, LPVOID
);
38 static UINT (WINAPI
*pGetSystemWindowsDirectoryA
)(LPSTR
, UINT
);
40 /* keep filename and filenameW the same */
41 static const char filename
[] = "testfile.xxx";
42 static const WCHAR filenameW
[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
43 static const char sillytext
[] =
44 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
45 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
46 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
55 static void InitFunctionPointers(void)
57 HMODULE hkernel32
= GetModuleHandleA("kernel32");
59 pFindFirstFileExA
=(void*)GetProcAddress(hkernel32
, "FindFirstFileExA");
60 pReplaceFileA
=(void*)GetProcAddress(hkernel32
, "ReplaceFileA");
61 pReplaceFileW
=(void*)GetProcAddress(hkernel32
, "ReplaceFileW");
62 pGetSystemWindowsDirectoryA
=(void*)GetProcAddress(hkernel32
, "GetSystemWindowsDirectoryA");
65 static void test__hread( void )
74 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
); /* be sure to remove stale files */
75 DeleteFileA( filename
);
76 filehandle
= _lcreat( filename
, 0 );
77 if (filehandle
== HFILE_ERROR
)
79 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
83 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
85 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
87 filehandle
= _lopen( filename
, OF_READ
);
89 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError( ) );
91 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
93 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
95 for (bytes_wanted
= 0; bytes_wanted
< lstrlenA( sillytext
); bytes_wanted
++)
97 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
98 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
99 for (i
= 0; i
< bytes_wanted
; i
++)
101 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
105 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
107 ret
= DeleteFileA( filename
);
108 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
112 static void test__hwrite( void )
121 HLOCAL memory_object
;
125 filehandle
= _lcreat( filename
, 0 );
126 if (filehandle
== HFILE_ERROR
)
128 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
132 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
134 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
136 filehandle
= _lopen( filename
, OF_READ
);
138 bytes_read
= _hread( filehandle
, buffer
, 1);
140 ok( 0 == bytes_read
, "file read size error\n" );
142 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
144 filehandle
= _lopen( filename
, OF_READWRITE
);
148 srand( (unsigned)time( NULL
) );
149 for (blocks
= 0; blocks
< 100; blocks
++)
151 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
154 checksum
[0] = checksum
[0] + buffer
[i
];
156 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
157 bytes_written
= bytes_written
+ sizeof( buffer
);
160 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
163 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
165 memory_object
= LocalAlloc( LPTR
, bytes_written
);
167 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)\n" );
169 contents
= LocalLock( memory_object
);
171 filehandle
= _lopen( filename
, OF_READ
);
173 contents
= LocalLock( memory_object
);
175 ok( NULL
!= contents
, "LocalLock whines\n" );
177 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
183 checksum
[0] = checksum
[0] + contents
[i
];
186 while (i
< bytes_written
- 1);
188 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
190 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
192 ret
= DeleteFileA( filename
);
193 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
197 static void test__lclose( void )
202 filehandle
= _lcreat( filename
, 0 );
203 if (filehandle
== HFILE_ERROR
)
205 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
209 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
211 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
213 ret
= DeleteFileA( filename
);
214 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
218 static void test__lcreat( void )
222 WIN32_FIND_DATAA search_results
;
223 char slashname
[] = "testfi/";
228 filehandle
= _lcreat( filename
, 0 );
229 if (filehandle
== HFILE_ERROR
)
231 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
235 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
237 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
239 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
241 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
243 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
245 ret
= DeleteFileA(filename
);
246 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError());
248 filehandle
= _lcreat( filename
, 1 ); /* readonly */
249 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
251 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
253 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
255 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
257 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
259 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
261 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
263 filehandle
= _lcreat( filename
, 2 );
264 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
266 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
268 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
270 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
272 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
274 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
276 ret
= DeleteFileA( filename
);
277 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
279 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
280 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
282 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
284 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
286 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
288 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
290 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
292 ret
= DeleteFileA( filename
);
293 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
295 filehandle
=_lcreat (slashname
, 0); /* illegal name */
296 if (HFILE_ERROR
==filehandle
) {
298 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
299 "creating file \"%s\" failed with error %d\n", slashname
, err
);
300 } else { /* only NT succeeds */
302 find
=FindFirstFileA (slashname
, &search_results
);
303 if (INVALID_HANDLE_VALUE
!=find
)
305 ret
= FindClose (find
);
306 ok (0 != ret
, "FindClose complains (%d)\n", GetLastError ());
307 slashname
[strlen(slashname
)-1]=0;
308 ok (!strcmp (slashname
, search_results
.cFileName
),
309 "found unexpected name \"%s\"\n", search_results
.cFileName
);
310 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
311 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
312 search_results
.dwFileAttributes
);
314 ret
= DeleteFileA( slashname
);
315 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
318 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
319 if (HFILE_ERROR
==filehandle
)
320 ok (0, "couldn't create volume label \"%s\"\n", filename
);
323 find
=FindFirstFileA (filename
, &search_results
);
324 if (INVALID_HANDLE_VALUE
==find
)
325 ok (0, "file \"%s\" not found\n", filename
);
327 ret
= FindClose(find
);
328 ok ( 0 != ret
, "FindClose complains (%d)\n", GetLastError ());
329 ok (!strcmp (filename
, search_results
.cFileName
),
330 "found unexpected name \"%s\"\n", search_results
.cFileName
);
331 search_results
.dwFileAttributes
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
332 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
333 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
334 search_results
.dwFileAttributes
);
336 ret
= DeleteFileA( filename
);
337 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
342 static void test__llseek( void )
350 filehandle
= _lcreat( filename
, 0 );
351 if (filehandle
== HFILE_ERROR
)
353 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
357 for (i
= 0; i
< 400; i
++)
359 ok( _hwrite( filehandle
, sillytext
, strlen( sillytext
) ) != -1, "_hwrite complains\n" );
361 ok( _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
) != -1, "should be able to seek\n" );
362 ok( _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
) != -1, "should be able to seek\n" );
364 bytes_read
= _hread( filehandle
, buffer
, 1);
365 ok( 1 == bytes_read
, "file read size error\n" );
366 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
367 ok( _llseek( filehandle
, -400 * (LONG
)strlen( sillytext
), FILE_END
) != -1, "should be able to seek\n" );
369 bytes_read
= _hread( filehandle
, buffer
, 1);
370 ok( 1 == bytes_read
, "file read size error\n" );
371 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
372 ok( _llseek( filehandle
, 1000000, FILE_END
) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
373 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
375 ret
= DeleteFileA( filename
);
376 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
380 static void test__llopen( void )
387 filehandle
= _lcreat( filename
, 0 );
388 if (filehandle
== HFILE_ERROR
)
390 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
394 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
395 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
397 filehandle
= _lopen( filename
, OF_READ
);
398 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
399 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
400 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
401 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
403 filehandle
= _lopen( filename
, OF_READWRITE
);
404 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
405 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
406 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
407 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
409 filehandle
= _lopen( filename
, OF_WRITE
);
410 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
411 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
412 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
414 ret
= DeleteFileA( filename
);
415 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
416 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
420 static void test__lread( void )
429 filehandle
= _lcreat( filename
, 0 );
430 if (filehandle
== HFILE_ERROR
)
432 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
436 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
438 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
440 filehandle
= _lopen( filename
, OF_READ
);
442 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError());
444 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
446 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
448 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
450 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
451 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
452 for (i
= 0; i
< bytes_wanted
; i
++)
454 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
458 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
460 ret
= DeleteFileA( filename
);
461 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
465 static void test__lwrite( void )
474 HLOCAL memory_object
;
478 filehandle
= _lcreat( filename
, 0 );
479 if (filehandle
== HFILE_ERROR
)
481 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
485 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
487 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
489 filehandle
= _lopen( filename
, OF_READ
);
491 bytes_read
= _hread( filehandle
, buffer
, 1);
493 ok( 0 == bytes_read
, "file read size error\n" );
495 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
497 filehandle
= _lopen( filename
, OF_READWRITE
);
501 srand( (unsigned)time( NULL
) );
502 for (blocks
= 0; blocks
< 100; blocks
++)
504 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
507 checksum
[0] = checksum
[0] + buffer
[i
];
509 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
510 bytes_written
= bytes_written
+ sizeof( buffer
);
513 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
516 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
518 memory_object
= LocalAlloc( LPTR
, bytes_written
);
520 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
522 contents
= LocalLock( memory_object
);
524 filehandle
= _lopen( filename
, OF_READ
);
526 contents
= LocalLock( memory_object
);
528 ok( NULL
!= contents
, "LocalLock whines\n" );
530 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
536 checksum
[0] += contents
[i
];
539 while (i
< bytes_written
- 1);
541 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
543 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
545 ret
= DeleteFileA( filename
);
546 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
549 static void test_CopyFileA(void)
551 char temp_path
[MAX_PATH
];
552 char source
[MAX_PATH
], dest
[MAX_PATH
];
553 static const char prefix
[] = "pfx";
560 ret
= GetTempPathA(MAX_PATH
, temp_path
);
561 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
562 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
564 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
565 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
567 ret
= MoveFileA(source
, source
);
568 todo_wine
ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
570 /* make the source have not zero size */
571 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
572 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
573 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
574 ok( retok
&& ret
== sizeof(prefix
),
575 "WriteFile error %d\n", GetLastError());
576 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
577 /* get the file time and change it to prove the difference */
578 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
579 ok( ret
, "GetFileTime error %d\n", GetLastError());
580 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
581 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
582 ok( ret
, "SetFileTime error %d\n", GetLastError());
583 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
586 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
587 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
589 SetLastError(0xdeadbeef);
590 ret
= CopyFileA(source
, dest
, TRUE
);
591 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
592 "CopyFileA: unexpected error %d\n", GetLastError());
594 ret
= CopyFileA(source
, dest
, FALSE
);
595 ok(ret
, "CopyFileA: error %d\n", GetLastError());
597 /* make sure that destination has correct size */
598 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
599 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
600 ret
= GetFileSize(hfile
, NULL
);
601 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
603 /* make sure that destination has the same filetime */
604 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
605 ok( ret
, "GetFileTime error %d\n", GetLastError());
606 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
608 SetLastError(0xdeadbeef);
609 ret
= CopyFileA(source
, dest
, FALSE
);
610 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
611 "CopyFileA: ret = %d, unexpected error %d\n", ret
, GetLastError());
613 /* make sure that destination still has correct size */
614 ret
= GetFileSize(hfile
, NULL
);
615 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
616 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
617 ok( retok
&& ret
== sizeof(prefix
),
618 "ReadFile: error %d\n", GetLastError());
619 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
622 ret
= DeleteFileA(source
);
623 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
624 ret
= DeleteFileA(dest
);
625 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
628 static void test_CopyFileW(void)
630 WCHAR temp_path
[MAX_PATH
];
631 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
632 static const WCHAR prefix
[] = {'p','f','x',0};
635 ret
= GetTempPathW(MAX_PATH
, temp_path
);
636 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
638 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
639 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
641 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
642 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
644 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
645 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
647 ret
= CopyFileW(source
, dest
, TRUE
);
648 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
649 "CopyFileW: unexpected error %d\n", GetLastError());
651 ret
= CopyFileW(source
, dest
, FALSE
);
652 ok(ret
, "CopyFileW: error %d\n", GetLastError());
654 ret
= DeleteFileW(source
);
655 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
656 ret
= DeleteFileW(dest
);
657 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
660 static void test_CreateFileA(void)
663 char temp_path
[MAX_PATH
];
664 char filename
[MAX_PATH
];
665 static const char prefix
[] = "pfx";
668 ret
= GetTempPathA(MAX_PATH
, temp_path
);
669 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
670 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
672 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
673 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
675 SetLastError(0xdeadbeef);
676 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
677 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
678 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
679 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
681 SetLastError(0xdeadbeef);
682 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
683 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
684 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
685 "hFile %p, last error %u\n", hFile
, GetLastError());
689 SetLastError(0xdeadbeef);
690 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
691 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
692 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
693 "hFile %p, last error %u\n", hFile
, GetLastError());
697 ret
= DeleteFileA(filename
);
698 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
700 SetLastError(0xdeadbeef);
701 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
702 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
703 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
704 "hFile %p, last error %u\n", hFile
, GetLastError());
708 ret
= DeleteFileA(filename
);
709 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
712 static void test_CreateFileW(void)
715 WCHAR temp_path
[MAX_PATH
];
716 WCHAR filename
[MAX_PATH
];
717 static const WCHAR emptyW
[]={'\0'};
718 static const WCHAR prefix
[] = {'p','f','x',0};
719 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
722 ret
= GetTempPathW(MAX_PATH
, temp_path
);
723 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
725 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
726 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
728 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
729 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
731 SetLastError(0xdeadbeef);
732 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
733 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
734 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
735 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
737 SetLastError(0xdeadbeef);
738 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
739 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
740 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
741 "hFile %p, last error %u\n", hFile
, GetLastError());
745 SetLastError(0xdeadbeef);
746 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
747 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
748 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
749 "hFile %p, last error %u\n", hFile
, GetLastError());
753 ret
= DeleteFileW(filename
);
754 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
756 SetLastError(0xdeadbeef);
757 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
758 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
759 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
760 "hFile %p, last error %u\n", hFile
, GetLastError());
764 ret
= DeleteFileW(filename
);
765 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
769 /* this crashes on NT4.0 */
770 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
771 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
772 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
773 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile
,GetLastError());
776 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
777 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
778 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
779 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile
,GetLastError());
781 /* test the result of opening a nonexistent driver name */
782 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
783 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
784 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
785 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile
,GetLastError());
787 ret
= CreateDirectoryW(filename
, NULL
);
788 ok(ret
== TRUE
, "couldn't create temporary directory\n");
789 hFile
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
790 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
791 ok(hFile
!= INVALID_HANDLE_VALUE
,
792 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
794 ret
= RemoveDirectoryW(filename
);
795 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
798 static void test_GetTempFileNameA(void)
802 char expected
[MAX_PATH
+ 10];
803 char windowsdir
[MAX_PATH
+ 10];
804 char windowsdrive
[3];
806 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
807 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
808 ok(result
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
810 /* If the Windows directory is the root directory, it ends in backslash, not else. */
811 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
813 strcat(windowsdir
, "\\");
816 windowsdrive
[0] = windowsdir
[0];
817 windowsdrive
[1] = windowsdir
[1];
818 windowsdrive
[2] = '\0';
820 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
821 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
822 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
823 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
824 windowsdrive
[0], out
);
826 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
827 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
829 strcat(expected
, windowsdir
);
830 strcat(expected
, "abc2.tmp");
831 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
835 static void test_DeleteFileA( void )
839 ret
= DeleteFileA(NULL
);
840 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
841 GetLastError() == ERROR_PATH_NOT_FOUND
),
842 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
844 ret
= DeleteFileA("");
845 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
846 GetLastError() == ERROR_BAD_PATHNAME
),
847 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
849 ret
= DeleteFileA("nul");
850 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
851 GetLastError() == ERROR_INVALID_PARAMETER
||
852 GetLastError() == ERROR_ACCESS_DENIED
||
853 GetLastError() == ERROR_INVALID_FUNCTION
),
854 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret
,GetLastError());
857 static void test_DeleteFileW( void )
860 WCHAR pathW
[MAX_PATH
];
861 WCHAR pathsubW
[MAX_PATH
];
862 static const WCHAR dirW
[] = {'d','e','l','e','t','e','f','i','l','e',0};
863 static const WCHAR subdirW
[] = {'\\','s','u','b',0};
864 static const WCHAR emptyW
[]={'\0'};
866 ret
= DeleteFileW(NULL
);
867 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
869 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
870 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
872 ret
= DeleteFileW(emptyW
);
873 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
874 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
876 /* test DeleteFile on empty directory */
877 ret
= GetTempPathW(MAX_PATH
, pathW
);
878 if (ret
+ sizeof(dirW
)/sizeof(WCHAR
)-1 + sizeof(subdirW
)/sizeof(WCHAR
)-1 >= MAX_PATH
)
880 ok(0, "MAX_PATH exceeded in constructing paths\n");
883 lstrcatW(pathW
, dirW
);
884 lstrcpyW(pathsubW
, pathW
);
885 lstrcatW(pathsubW
, subdirW
);
886 ret
= CreateDirectoryW(pathW
, NULL
);
887 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
888 ret
= DeleteFileW(pathW
);
889 ok(ret
== FALSE
, "DeleteFile should fail for empty directories\n");
890 ret
= RemoveDirectoryW(pathW
);
891 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
893 /* test DeleteFile on non-empty directory */
894 ret
= CreateDirectoryW(pathW
, NULL
);
895 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
896 ret
= CreateDirectoryW(pathsubW
, NULL
);
897 ok(ret
== TRUE
, "couldn't create directory deletefile\\sub\n");
898 ret
= DeleteFileW(pathW
);
899 ok(ret
== FALSE
, "DeleteFile should fail for non-empty directories\n");
900 ret
= RemoveDirectoryW(pathsubW
);
901 ok(ret
== TRUE
, "expected to remove directory deletefile\\sub\n");
902 ret
= RemoveDirectoryW(pathW
);
903 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
906 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
908 static void test_MoveFileA(void)
910 char tempdir
[MAX_PATH
];
911 char source
[MAX_PATH
], dest
[MAX_PATH
];
912 static const char prefix
[] = "pfx";
915 ret
= GetTempPathA(MAX_PATH
, tempdir
);
916 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
917 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
919 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
920 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
922 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
923 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
925 ret
= MoveFileA(source
, dest
);
926 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
927 "MoveFileA: unexpected error %d\n", GetLastError());
929 ret
= DeleteFileA(dest
);
930 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
932 ret
= MoveFileA(source
, dest
);
933 ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
935 lstrcatA(tempdir
, "Remove Me");
936 ret
= CreateDirectoryA(tempdir
, NULL
);
937 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
939 lstrcpyA(source
, dest
);
940 lstrcpyA(dest
, tempdir
);
941 lstrcatA(dest
, "\\wild?.*");
942 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
943 ret
= MoveFileA(source
, dest
);
944 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
945 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
946 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
947 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
948 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
951 char temppath
[MAX_PATH
];
954 lstrcpyA(temppath
, tempdir
);
955 lstrcatA(temppath
, "\\*.*");
956 hFind
= FindFirstFileA(temppath
, &fd
);
957 if (INVALID_HANDLE_VALUE
!= hFind
)
962 lpName
= fd
.cAlternateFileName
;
964 lpName
= fd
.cFileName
;
965 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
967 while (FindNextFileA(hFind
, &fd
));
971 ret
= DeleteFileA(source
);
972 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
973 ret
= DeleteFileA(dest
);
974 ok(!ret
, "DeleteFileA: error %d\n", GetLastError());
975 ret
= RemoveDirectoryA(tempdir
);
976 ok(ret
, "DeleteDirectoryA: error %d\n", GetLastError());
979 static void test_MoveFileW(void)
981 WCHAR temp_path
[MAX_PATH
];
982 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
983 static const WCHAR prefix
[] = {'p','f','x',0};
986 ret
= GetTempPathW(MAX_PATH
, temp_path
);
987 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
989 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
990 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
992 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
993 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
995 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
996 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
998 ret
= MoveFileW(source
, dest
);
999 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
1000 "CopyFileW: unexpected error %d\n", GetLastError());
1002 ret
= DeleteFileW(source
);
1003 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1004 ret
= DeleteFileW(dest
);
1005 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1008 #define PATTERN_OFFSET 0x10
1010 static void test_offset_in_overlapped_structure(void)
1016 BYTE buf
[256], pattern
[] = "TeSt";
1018 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
1021 ret
=GetTempPathA(MAX_PATH
, temp_path
);
1022 ok( ret
, "GetTempPathA error %d\n", GetLastError());
1023 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
1024 ok( ret
, "GetTempFileNameA error %d\n", GetLastError());
1026 /*** Write File *****************************************************/
1028 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
1029 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1031 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
1032 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
1033 ok( ret
, "WriteFile error %d\n", GetLastError());
1034 ok(done
== sizeof(buf
), "expected number of bytes written %u\n", done
);
1036 memset(&ov
, 0, sizeof(ov
));
1037 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1038 S(U(ov
)).OffsetHigh
= 0;
1039 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1040 /* Win 9x does not support the overlapped I/O on files */
1041 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1042 ok(rc
, "WriteFile error %d\n", GetLastError());
1043 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1044 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1045 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1047 S(U(ov
)).Offset
= sizeof(buf
) * 2;
1048 S(U(ov
)).OffsetHigh
= 0;
1049 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1050 ok( ret
, "WriteFile error %d\n", GetLastError());
1051 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1052 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1053 ok(offset
== sizeof(buf
) * 2 + sizeof(pattern
), "wrong file offset %d\n", offset
);
1058 /*** Read File *****************************************************/
1060 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1061 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1063 memset(buf
, 0, sizeof(buf
));
1064 memset(&ov
, 0, sizeof(ov
));
1065 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1066 S(U(ov
)).OffsetHigh
= 0;
1067 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
1068 /* Win 9x does not support the overlapped I/O on files */
1069 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1070 ok(rc
, "ReadFile error %d\n", GetLastError());
1071 ok(done
== sizeof(pattern
), "expected number of bytes read %u\n", done
);
1072 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1073 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1074 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
1079 ret
= DeleteFileA(temp_fname
);
1080 ok( ret
, "DeleteFileA error %d\n", GetLastError());
1083 static void test_LockFile(void)
1087 OVERLAPPED overlapped
;
1088 int limited_LockFile
;
1089 int limited_UnLockFile
;
1092 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1093 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1094 CREATE_ALWAYS
, 0, 0 );
1095 if (handle
== INVALID_HANDLE_VALUE
)
1097 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1100 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
1102 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1103 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1105 limited_UnLockFile
= 0;
1106 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1108 limited_UnLockFile
= 1;
1111 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1112 /* overlapping locks must fail */
1113 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1114 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1115 /* non-overlapping locks must succeed */
1116 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1118 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1119 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1120 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1121 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1123 S(U(overlapped
)).Offset
= 100;
1124 S(U(overlapped
)).OffsetHigh
= 0;
1125 overlapped
.hEvent
= 0;
1127 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1128 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1130 /* LockFileEx is probably OK, test it more. */
1131 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1132 "LockFileEx 100,100 failed\n" );
1135 /* overlapping shared locks are OK */
1136 S(U(overlapped
)).Offset
= 150;
1137 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1139 /* but exclusive is not */
1140 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1141 0, 50, 0, &overlapped
),
1142 "LockFileEx exclusive 150,50 succeeded\n" );
1143 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1144 { /* UnLockFile is capable. */
1145 S(U(overlapped
)).Offset
= 100;
1146 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1147 "UnlockFileEx 150,100 again succeeded\n" );
1150 ret
= LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 );
1153 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1154 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1155 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1158 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong LockFile error %u\n", GetLastError() );
1160 /* wrap-around lock should not do anything */
1161 /* (but still succeeds on NT4 so we don't check result) */
1162 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1164 limited_LockFile
= 0;
1165 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1167 limited_LockFile
= 1;
1170 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1172 /* zero-byte lock */
1173 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1174 limited_LockFile
|| ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1175 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1176 limited_LockFile
|| ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1178 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1179 !ok( UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1181 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1183 CloseHandle( handle
);
1184 DeleteFileA( filename
);
1187 static inline int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
, BOOL is_win9x
)
1191 if (!access1
) sharing1
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1192 if (!access2
) sharing2
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1197 if (!access1
) access1
= GENERIC_READ
;
1200 if (!access2
) access2
= GENERIC_READ
;
1203 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1204 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1205 if ((access1
& DELETE
) && !(sharing2
& FILE_SHARE_DELETE
)) return 0;
1206 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1207 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1208 if ((access2
& DELETE
) && !(sharing1
& FILE_SHARE_DELETE
)) return 0;
1212 static void test_file_sharing(void)
1214 static const DWORD access_modes
[] =
1215 { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
,
1216 DELETE
, GENERIC_READ
|DELETE
, GENERIC_WRITE
|DELETE
, GENERIC_READ
|GENERIC_WRITE
|DELETE
};
1217 static const DWORD sharing_modes
[] =
1218 { 0, FILE_SHARE_READ
,
1219 FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1220 FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_DELETE
,
1221 FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
};
1225 BOOL is_win9x
= FALSE
;
1227 /* make sure the file exists */
1228 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1229 if (h
== INVALID_HANDLE_VALUE
)
1231 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError());
1234 is_win9x
= GetFileAttributesW(filenameW
) == INVALID_FILE_ATTRIBUTES
;
1237 for (a1
= 0; a1
< sizeof(access_modes
)/sizeof(access_modes
[0]); a1
++)
1239 for (s1
= 0; s1
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s1
++)
1241 /* Win9x doesn't support FILE_SHARE_DELETE */
1242 if (is_win9x
&& (sharing_modes
[s1
] & FILE_SHARE_DELETE
))
1245 SetLastError(0xdeadbeef);
1246 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1247 NULL
, OPEN_EXISTING
, 0, 0 );
1248 if (h
== INVALID_HANDLE_VALUE
)
1250 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1253 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1255 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1257 /* Win9x doesn't support FILE_SHARE_DELETE */
1258 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1261 SetLastError(0xdeadbeef);
1262 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1263 NULL
, OPEN_EXISTING
, 0, 0 );
1265 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1266 access_modes
[a2
], sharing_modes
[s2
], is_win9x
))
1268 ret
= GetLastError();
1270 ok( h2
!= INVALID_HANDLE_VALUE
,
1271 "open failed for modes %x/%x/%x/%x\n",
1272 access_modes
[a1
], sharing_modes
[s1
],
1273 access_modes
[a2
], sharing_modes
[s2
] );
1274 ok( ret
== 0xdeadbeef /* Win9x */ ||
1276 "wrong error code %d\n", ret
);
1282 ret
= GetLastError();
1284 ok( h2
== INVALID_HANDLE_VALUE
,
1285 "open succeeded for modes %x/%x/%x/%x\n",
1286 access_modes
[a1
], sharing_modes
[s1
],
1287 access_modes
[a2
], sharing_modes
[s2
] );
1288 ok( ret
== ERROR_SHARING_VIOLATION
,
1289 "wrong error code %d\n", ret
);
1297 SetLastError(0xdeadbeef);
1298 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1299 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1301 SetLastError(0xdeadbeef);
1302 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1303 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1304 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %d\n", GetLastError() );
1306 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1307 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1312 DeleteFileA( filename
);
1315 static char get_windows_drive(void)
1317 char windowsdir
[MAX_PATH
];
1318 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1319 return windowsdir
[0];
1322 static void test_FindFirstFileA(void)
1325 WIN32_FIND_DATAA data
;
1327 char buffer
[5] = "C:\\";
1329 char nonexistent
[MAX_PATH
];
1331 /* try FindFirstFileA on "C:\" */
1332 buffer
[0] = get_windows_drive();
1334 SetLastError( 0xdeadbeaf );
1335 handle
= FindFirstFileA(buffer
, &data
);
1336 err
= GetLastError();
1337 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should fail\n" );
1338 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1340 /* try FindFirstFileA on "C:\*" */
1341 strcpy(buffer2
, buffer
);
1342 strcat(buffer2
, "*");
1343 handle
= FindFirstFileA(buffer2
, &data
);
1344 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1345 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1346 "FindFirstFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1347 if (FindNextFileA( handle
, &data
))
1348 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1349 "FindNextFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1350 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1352 /* try FindFirstFileA on windows dir */
1353 GetWindowsDirectory( buffer2
, sizeof(buffer2
) );
1354 strcat(buffer2
, "\\*");
1355 handle
= FindFirstFileA(buffer2
, &data
);
1356 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1357 ok( !strcmp( data
.cFileName
, "." ), "FindFirstFile should return '.' first\n" );
1358 ok( FindNextFileA( handle
, &data
), "FindNextFile failed\n" );
1359 ok( !strcmp( data
.cFileName
, ".." ), "FindNextFile should return '..' as second entry\n" );
1360 while (FindNextFileA( handle
, &data
))
1361 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1362 "FindNextFile shouldn't return '%s'\n", data
.cFileName
);
1363 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1365 /* try FindFirstFileA on "C:\foo\" */
1366 SetLastError( 0xdeadbeaf );
1367 GetTempFileNameA( buffer
, "foo", 0, nonexistent
);
1368 DeleteFileA( nonexistent
);
1369 strcpy(buffer2
, nonexistent
);
1370 strcat(buffer2
, "\\");
1371 handle
= FindFirstFileA(buffer2
, &data
);
1372 err
= GetLastError();
1373 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1375 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1378 /* try FindFirstFileA on "C:\foo\bar.txt" */
1379 SetLastError( 0xdeadbeaf );
1380 strcpy(buffer2
, nonexistent
);
1381 strcat(buffer2
, "\\bar.txt");
1382 handle
= FindFirstFileA(buffer2
, &data
);
1383 err
= GetLastError();
1384 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1385 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1387 /* try FindFirstFileA on "C:\foo\*.*" */
1388 SetLastError( 0xdeadbeaf );
1389 strcpy(buffer2
, nonexistent
);
1390 strcat(buffer2
, "\\*.*");
1391 handle
= FindFirstFileA(buffer2
, &data
);
1392 err
= GetLastError();
1393 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1394 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1396 /* try FindFirstFileA on "foo\bar.txt" */
1397 SetLastError( 0xdeadbeaf );
1398 strcpy(buffer2
, nonexistent
+ 3);
1399 strcat(buffer2
, "\\bar.txt");
1400 handle
= FindFirstFileA(buffer2
, &data
);
1401 err
= GetLastError();
1402 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1403 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1405 /* try FindFirstFileA on "c:\nul" */
1406 SetLastError( 0xdeadbeaf );
1407 strcpy(buffer2
, buffer
);
1408 strcat(buffer2
, "nul");
1409 handle
= FindFirstFileA(buffer2
, &data
);
1410 err
= GetLastError();
1411 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1412 ok( 0 == lstrcmpiA(data
.cFileName
, "nul"), "wrong name %s\n", data
.cFileName
);
1413 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
1414 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
1415 "wrong attributes %x\n", data
.dwFileAttributes
);
1416 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
1418 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1419 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1421 SetLastError( 0xdeadbeaf );
1422 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1423 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1424 ok( FindClose( handle
), "failed to close handle\n" );
1426 /* try FindFirstFileA on "lpt1" */
1427 SetLastError( 0xdeadbeaf );
1428 strcpy(buffer2
, "lpt1");
1429 handle
= FindFirstFileA(buffer2
, &data
);
1430 err
= GetLastError();
1431 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1432 ok( 0 == lstrcmpiA(data
.cFileName
, "lpt1"), "wrong name %s\n", data
.cFileName
);
1433 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
1434 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
1435 "wrong attributes %x\n", data
.dwFileAttributes
);
1436 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
1438 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1439 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1441 SetLastError( 0xdeadbeaf );
1442 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1443 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1444 ok( FindClose( handle
), "failed to close handle\n" );
1446 /* try FindFirstFileA on "c:\nul\*" */
1447 SetLastError( 0xdeadbeaf );
1448 strcpy(buffer2
, buffer
);
1449 strcat(buffer2
, "nul\\*");
1450 handle
= FindFirstFileA(buffer2
, &data
);
1451 err
= GetLastError();
1452 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1453 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1455 /* try FindFirstFileA on "c:\nul*" */
1456 SetLastError( 0xdeadbeaf );
1457 strcpy(buffer2
, buffer
);
1458 strcat(buffer2
, "nul*");
1459 handle
= FindFirstFileA(buffer2
, &data
);
1460 err
= GetLastError();
1461 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1462 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1464 /* try FindFirstFileA on "c:\foo\bar\nul" */
1465 SetLastError( 0xdeadbeaf );
1466 strcpy(buffer2
, buffer
);
1467 strcat(buffer2
, "foo\\bar\\nul");
1468 handle
= FindFirstFileA(buffer2
, &data
);
1469 err
= GetLastError();
1470 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1471 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1473 /* try FindFirstFileA on "c:\foo\nul\bar" */
1474 SetLastError( 0xdeadbeaf );
1475 strcpy(buffer2
, buffer
);
1476 strcat(buffer2
, "foo\\nul\\bar");
1477 handle
= FindFirstFileA(buffer2
, &data
);
1478 err
= GetLastError();
1479 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1480 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1483 static void test_FindNextFileA(void)
1486 WIN32_FIND_DATAA search_results
;
1488 char buffer
[5] = "C:\\*";
1490 buffer
[0] = get_windows_drive();
1491 handle
= FindFirstFileA(buffer
,&search_results
);
1492 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
1493 while (FindNextFile(handle
, &search_results
))
1495 /* get to the end of the files */
1497 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1498 err
= GetLastError();
1499 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
1502 static void test_FindFirstFileExA(void)
1504 WIN32_FIND_DATAA search_results
;
1507 if (!pFindFirstFileExA
)
1509 win_skip("FindFirstFileExA() is missing\n");
1513 CreateDirectoryA("test-dir", NULL
);
1514 _lclose(_lcreat("test-dir\\file1", 0));
1515 _lclose(_lcreat("test-dir\\file2", 0));
1516 CreateDirectoryA("test-dir\\dir1", NULL
);
1517 /* FindExLimitToDirectories is ignored */
1518 SetLastError(0xdeadbeef);
1519 handle
= pFindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, FindExSearchLimitToDirectories
, NULL
, 0);
1520 if (handle
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1522 win_skip("FindFirstFileExA is not implemented\n");
1525 ok(handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile failed (err=%u)\n", GetLastError());
1526 ok(strcmp(search_results
.cFileName
, ".") == 0, "First entry should be '.', is %s\n", search_results
.cFileName
);
1528 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1530 ok(FindNextFile(handle
, &search_results
), "Fetching second file failed\n");
1531 ok(strcmp(search_results
.cFileName
, "..") == 0, "Second entry should be '..' is %s\n", search_results
.cFileName
);
1533 ok(FindNextFile(handle
, &search_results
), "Fetching third file failed\n");
1534 ok(CHECK_NAME(search_results
.cFileName
), "Invalid third entry - %s\n", search_results
.cFileName
);
1536 ok(FindNextFile(handle
, &search_results
), "Fetching fourth file failed\n");
1537 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
1539 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
1540 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
1544 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should failed\n");
1546 FindClose( handle
);
1549 DeleteFileA("test-dir\\file1");
1550 DeleteFileA("test-dir\\file2");
1551 RemoveDirectoryA("test-dir\\dir1");
1552 RemoveDirectoryA("test-dir");
1555 static int test_Mapfile_createtemp(HANDLE
*handle
)
1557 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1558 DeleteFile(filename
);
1559 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1560 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1561 if (*handle
!= INVALID_HANDLE_VALUE
) {
1569 static void test_MapFile(void)
1574 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1576 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1577 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1579 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1581 /* We have to close file before we try new stuff with mapping again.
1582 Else we would always succeed on XP or block descriptors on 95. */
1583 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1584 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1585 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1586 ok( CloseHandle( handle
), "can't close file handle\n");
1589 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1591 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1592 ok( hmap
== NULL
, "mapped zero size file\n");
1593 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1595 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
1596 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1597 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1599 CloseHandle( hmap
);
1601 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
1602 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1603 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1605 CloseHandle( hmap
);
1607 /* On XP you can now map again, on Win 95 you cannot. */
1609 ok( CloseHandle( handle
), "can't close file handle\n");
1610 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1613 static void test_GetFileType(void)
1616 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1617 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1618 type
= GetFileType(h
);
1619 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
1621 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1622 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1623 type
= GetFileType(h
);
1624 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
1626 DeleteFileA( filename
);
1629 static int completion_count
;
1631 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
1633 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1634 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
1638 static void test_async_file_errors(void)
1640 char szFile
[MAX_PATH
];
1641 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
1643 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
1645 S(U(ovl
)).Offset
= 0;
1646 S(U(ovl
)).OffsetHigh
= 0;
1648 completion_count
= 0;
1650 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
1651 strcat(szFile
, "\\win.ini");
1652 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1653 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1654 if (hFile
== INVALID_HANDLE_VALUE
) /* win9x doesn't like FILE_SHARE_DELETE */
1655 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1656 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1657 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA(%s ...) failed\n", szFile
);
1662 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
1664 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
1665 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1668 if (!GetOverlappedResult(hFile
, &ovl
, &count
, FALSE
))
1670 S(U(ovl
)).Offset
+= count
;
1671 /* i/o completion routine only called if ReadFileEx returned success.
1672 * we only care about violations of this rule so undo what should have
1676 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
1677 /*printf("Error = %ld\n", GetLastError());*/
1678 HeapFree(GetProcessHeap(), 0, lpBuffer
);
1681 static void test_read_write(void)
1683 DWORD bytes
, ret
, old_prot
;
1685 char temp_path
[MAX_PATH
];
1686 char filename
[MAX_PATH
];
1688 static const char prefix
[] = "pfx";
1690 ret
= GetTempPathA(MAX_PATH
, temp_path
);
1691 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
1692 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1694 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
1695 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1697 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1698 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1699 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
1701 SetLastError(12345678);
1703 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
1704 ok(ret
&& GetLastError() == 12345678,
1705 "ret = %d, error %d\n", ret
, GetLastError());
1706 ok(!bytes
, "bytes = %d\n", bytes
);
1708 SetLastError(12345678);
1710 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
1711 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
1712 (ret
&& GetLastError() == 12345678), /* Win9x */
1713 "ret = %d, error %d\n", ret
, GetLastError());
1714 ok(!bytes
|| /* Win2k */
1715 bytes
== 10, /* Win9x */
1716 "bytes = %d\n", bytes
);
1718 /* make sure the file contains data */
1719 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
1720 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
1722 SetLastError(12345678);
1724 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
1725 ok(ret
&& GetLastError() == 12345678,
1726 "ret = %d, error %d\n", ret
, GetLastError());
1727 ok(!bytes
, "bytes = %d\n", bytes
);
1729 SetLastError(12345678);
1731 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
1732 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
1733 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
1734 "ret = %d, error %d\n", ret
, GetLastError());
1735 ok(!bytes
, "bytes = %d\n", bytes
);
1737 /* test passing protected memory as buffer */
1739 mem
= VirtualAlloc( NULL
, 0x4000, MEM_COMMIT
, PAGE_READWRITE
);
1740 ok( mem
!= NULL
, "failed to allocate virtual mem error %u\n", GetLastError() );
1742 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1743 ok( ret
, "WriteFile failed error %u\n", GetLastError() );
1744 ok( bytes
== 0x4000, "only wrote %x bytes\n", bytes
);
1746 ret
= VirtualProtect( mem
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
1747 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1749 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1750 ok( !ret
, "WriteFile succeeded\n" );
1751 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
1752 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1753 "wrong error %u\n", GetLastError() );
1754 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
1756 ret
= WriteFile( (HANDLE
)0xdead, mem
, 0x4000, &bytes
, NULL
);
1757 ok( !ret
, "WriteFile succeeded\n" );
1758 ok( GetLastError() == ERROR_INVALID_HANDLE
|| /* handle is checked before buffer on NT */
1759 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1760 "wrong error %u\n", GetLastError() );
1761 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
1763 ret
= VirtualProtect( mem
, 0x2000, PAGE_NOACCESS
, &old_prot
);
1764 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1766 ret
= WriteFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1767 ok( !ret
, "WriteFile succeeded\n" );
1768 ok( GetLastError() == ERROR_INVALID_USER_BUFFER
||
1769 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1770 "wrong error %u\n", GetLastError() );
1771 ok( bytes
== 0, "wrote %x bytes\n", bytes
);
1773 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
1775 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1776 ok( !ret
, "ReadFile succeeded\n" );
1777 ok( GetLastError() == ERROR_NOACCESS
||
1778 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1779 "wrong error %u\n", GetLastError() );
1780 ok( bytes
== 0, "read %x bytes\n", bytes
);
1782 ret
= VirtualProtect( mem
, 0x2000, PAGE_READONLY
, &old_prot
);
1783 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1785 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1786 ok( !ret
, "ReadFile succeeded\n" );
1787 ok( GetLastError() == ERROR_NOACCESS
||
1788 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1789 "wrong error %u\n", GetLastError() );
1790 ok( bytes
== 0, "read %x bytes\n", bytes
);
1792 ret
= VirtualProtect( mem
, 0x2000, PAGE_READWRITE
, &old_prot
);
1793 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1795 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1796 ok( !ret
, "ReadFile succeeded\n" );
1797 ok( GetLastError() == ERROR_NOACCESS
||
1798 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1799 "wrong error %u\n", GetLastError() );
1800 ok( bytes
== 0, "read %x bytes\n", bytes
);
1802 SetFilePointer( hFile
, 0x1234, NULL
, FILE_BEGIN
);
1803 SetEndOfFile( hFile
);
1804 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
1806 ret
= ReadFile( hFile
, mem
, 0x4000, &bytes
, NULL
);
1807 ok( !ret
, "ReadFile succeeded\n" );
1808 ok( GetLastError() == ERROR_NOACCESS
||
1809 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1810 "wrong error %u\n", GetLastError() );
1811 ok( bytes
== 0, "read %x bytes\n", bytes
);
1813 ret
= ReadFile( hFile
, mem
, 0x2000, &bytes
, NULL
);
1814 ok( ret
, "ReadFile failed error %u\n", GetLastError() );
1815 ok( bytes
== 0x1234, "read %x bytes\n", bytes
);
1817 ret
= ReadFile( hFile
, NULL
, 1, &bytes
, NULL
);
1818 ok( !ret
, "ReadFile succeeded\n" );
1819 ok( GetLastError() == ERROR_NOACCESS
||
1820 GetLastError() == ERROR_INVALID_PARAMETER
, /* win9x */
1821 "wrong error %u\n", GetLastError() );
1822 ok( bytes
== 0, "read %x bytes\n", bytes
);
1824 VirtualFree( mem
, 0, MEM_FREE
);
1826 ret
= CloseHandle(hFile
);
1827 ok( ret
, "CloseHandle: error %d\n", GetLastError());
1828 ret
= DeleteFileA(filename
);
1829 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
1832 static void test_OpenFile(void)
1839 static const char file
[] = "regedit.exe";
1840 static const char foo
[] = ".\\foo-bar-foo.baz";
1841 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
1842 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1843 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1844 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1845 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1846 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1847 char buff
[MAX_PATH
];
1848 char buff_long
[4*MAX_PATH
];
1849 char filled_0xA5
[OFS_MAXPATHNAME
];
1853 /* Check for existing file */
1854 if (!pGetSystemWindowsDirectoryA
)
1855 length
= GetWindowsDirectoryA(buff
, MAX_PATH
);
1857 length
= pGetSystemWindowsDirectoryA(buff
, MAX_PATH
);
1859 if (length
+ sizeof(file
) < MAX_PATH
)
1861 p
= buff
+ strlen(buff
);
1862 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
1864 memset(&ofs
, 0xA5, sizeof(ofs
));
1865 SetLastError(0xfaceabee);
1867 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
1868 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
1869 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1870 "GetLastError() returns %d\n", GetLastError() );
1871 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1872 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1873 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1874 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1875 ofs
.szPathName
, buff
);
1878 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
1879 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
1881 /* Check for nonexistent file */
1882 if (length
+ sizeof(foo
) < MAX_PATH
)
1884 p
= buff
+ strlen(buff
);
1885 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
1886 strcpy( p
, foo
+ 2 );
1887 memset(&ofs
, 0xA5, sizeof(ofs
));
1888 SetLastError(0xfaceabee);
1890 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
1891 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1892 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
1894 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1895 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1896 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1897 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1898 ofs
.szPathName
, buff
);
1901 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
1902 length
+= lstrlenA(foo_too_long
+ 1);
1904 /* Check for nonexistent file with too long filename */
1905 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
1907 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
1908 memset(&ofs
, 0xA5, sizeof(ofs
));
1909 SetLastError(0xfaceabee);
1911 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
1912 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1913 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
1914 "GetLastError() returns %d\n", GetLastError() );
1916 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1917 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
1918 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1919 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1920 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1924 length
= GetCurrentDirectoryA(MAX_PATH
, buff
) + sizeof(filename
);
1926 if (length
>= MAX_PATH
)
1928 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
1931 p
= buff
+ strlen(buff
);
1932 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
1933 strcpy( p
, filename
);
1935 memset(&ofs
, 0xA5, sizeof(ofs
));
1936 SetLastError(0xfaceabee);
1937 /* Create an empty file */
1938 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
1939 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
1940 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1941 "GetLastError() returns %d\n", GetLastError() );
1942 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1943 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1944 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1945 ret
= _lclose(hFile
);
1946 ok( !ret
, "_lclose() returns %d\n", ret
);
1947 retval
= GetFileAttributesA(filename
);
1948 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
1950 memset(&ofs
, 0xA5, sizeof(ofs
));
1951 SetLastError(0xfaceabee);
1952 /* Check various opening options: */
1953 /* for reading only, */
1954 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
1955 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
1956 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1957 "GetLastError() returns %d\n", GetLastError() );
1958 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1959 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1960 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1961 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1962 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1963 ret
= _lclose(hFile
);
1964 ok( !ret
, "_lclose() returns %d\n", ret
);
1966 memset(&ofs
, 0xA5, sizeof(ofs
));
1967 SetLastError(0xfaceabee);
1968 /* for writing only, */
1969 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
1970 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
1971 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1972 "GetLastError() returns %d\n", GetLastError() );
1973 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1974 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1975 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1976 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1977 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1978 ret
= _lclose(hFile
);
1979 ok( !ret
, "_lclose() returns %d\n", ret
);
1981 memset(&ofs
, 0xA5, sizeof(ofs
));
1982 SetLastError(0xfaceabee);
1983 /* for reading and writing, */
1984 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
1985 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
1986 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1987 "GetLastError() returns %d\n", GetLastError() );
1988 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1989 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1990 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1991 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1992 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1993 ret
= _lclose(hFile
);
1994 ok( !ret
, "_lclose() returns %d\n", ret
);
1996 memset(&ofs
, 0xA5, sizeof(ofs
));
1997 SetLastError(0xfaceabee);
1998 /* for checking file presence. */
1999 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
2000 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
2001 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2002 "GetLastError() returns %d\n", GetLastError() );
2003 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2004 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2005 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2006 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2007 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2009 memset(&ofs
, 0xA5, sizeof(ofs
));
2010 SetLastError(0xfaceabee);
2011 /* Delete the file and make sure it doesn't exist anymore */
2012 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
2013 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
2014 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
2015 "GetLastError() returns %d\n", GetLastError() );
2016 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
2017 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
2018 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
2019 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
2020 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
2022 retval
= GetFileAttributesA(filename
);
2023 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
2026 static void test_overlapped(void)
2031 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2032 if (0) /* tested: WinXP */
2034 GetOverlappedResult(0, NULL
, &result
, FALSE
);
2035 GetOverlappedResult(0, &ov
, NULL
, FALSE
);
2036 GetOverlappedResult(0, NULL
, NULL
, FALSE
);
2039 memset( &ov
, 0, sizeof ov
);
2041 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2043 ok( result
== 0, "wrong result %u\n", result
);
2045 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2049 ov
.InternalHigh
= 0xabcd;
2050 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2052 ok( result
== 0xabcd, "wrong result %u\n", result
);
2054 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2056 SetLastError( 0xb00 );
2058 ov
.Internal
= STATUS_INVALID_HANDLE
;
2059 ov
.InternalHigh
= 0xabcd;
2060 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2061 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
2062 ok( r
== FALSE
, "should return false\n");
2063 ok( result
== 0xabcd || result
== 0 /* win9x */, "wrong result %u\n", result
);
2065 SetLastError( 0xb00 );
2067 ov
.Internal
= STATUS_PENDING
;
2068 ov
.InternalHigh
= 0xabcd;
2069 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2070 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2071 "wrong error %u\n", GetLastError() );
2072 ok( r
== FALSE
, "should return false\n");
2073 ok( result
== 0, "wrong result %u\n", result
);
2075 SetLastError( 0xb00 );
2076 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
2077 ov
.Internal
= STATUS_PENDING
;
2078 ov
.InternalHigh
= 0xabcd;
2079 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2080 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2081 "wrong error %u\n", GetLastError() );
2082 ok( r
== FALSE
, "should return false\n");
2084 ResetEvent( ov
.hEvent
);
2086 SetLastError( 0xb00 );
2087 ov
.Internal
= STATUS_PENDING
;
2088 ov
.InternalHigh
= 0;
2089 r
= GetOverlappedResult(0, &ov
, &result
, 0);
2090 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
2091 "wrong error %u\n", GetLastError() );
2092 ok( r
== FALSE
, "should return false\n");
2094 r
= CloseHandle( ov
.hEvent
);
2095 ok( r
== TRUE
, "close handle failed\n");
2098 static void test_RemoveDirectory(void)
2101 char directory
[] = "removeme";
2103 rc
= CreateDirectory(directory
, NULL
);
2104 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
2106 rc
= SetCurrentDirectory(directory
);
2107 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2109 rc
= RemoveDirectory(".");
2112 rc
= SetCurrentDirectory("..");
2113 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2115 rc
= RemoveDirectory(directory
);
2116 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2120 static BOOL
check_file_time( const FILETIME
*ft1
, const FILETIME
*ft2
, UINT tolerance
)
2122 ULONGLONG t1
= ((ULONGLONG
)ft1
->dwHighDateTime
<< 32) | ft1
->dwLowDateTime
;
2123 ULONGLONG t2
= ((ULONGLONG
)ft2
->dwHighDateTime
<< 32) | ft2
->dwLowDateTime
;
2124 return abs(t1
- t2
) <= tolerance
;
2127 static void test_ReplaceFileA(void)
2129 char replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2130 HANDLE hReplacedFile
, hReplacementFile
, hBackupFile
;
2131 static const char replacedData
[] = "file-to-replace";
2132 static const char replacementData
[] = "new-file";
2133 static const char backupData
[] = "backup-file";
2134 FILETIME ftReplaced
, ftReplacement
, ftBackup
;
2135 static const char prefix
[] = "pfx";
2136 char temp_path
[MAX_PATH
];
2138 BOOL retok
, removeBackup
= FALSE
;
2142 win_skip("ReplaceFileA() is missing\n");
2146 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2147 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2148 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2150 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replaced
);
2151 ok(ret
!= 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2153 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2154 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2156 ret
= GetTempFileNameA(temp_path
, prefix
, 0, backup
);
2157 ok(ret
!= 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2159 /* place predictable data in the file to be replaced */
2160 hReplacedFile
= CreateFileA(replaced
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2161 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2162 "failed to open replaced file\n");
2163 retok
= WriteFile(hReplacedFile
, replacedData
, sizeof(replacedData
), &ret
, NULL
);
2164 ok( retok
&& ret
== sizeof(replacedData
),
2165 "WriteFile error (replaced) %d\n", GetLastError());
2166 ok(GetFileSize(hReplacedFile
, NULL
) == sizeof(replacedData
),
2167 "replaced file has wrong size\n");
2168 /* place predictable data in the file to be the replacement */
2169 hReplacementFile
= CreateFileA(replacement
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2170 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
,
2171 "failed to open replacement file\n");
2172 retok
= WriteFile(hReplacementFile
, replacementData
, sizeof(replacementData
), &ret
, NULL
);
2173 ok( retok
&& ret
== sizeof(replacementData
),
2174 "WriteFile error (replacement) %d\n", GetLastError());
2175 ok(GetFileSize(hReplacementFile
, NULL
) == sizeof(replacementData
),
2176 "replacement file has wrong size\n");
2177 /* place predictable data in the backup file (to be over-written) */
2178 hBackupFile
= CreateFileA(backup
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2179 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2180 "failed to open backup file\n");
2181 retok
= WriteFile(hBackupFile
, backupData
, sizeof(backupData
), &ret
, NULL
);
2182 ok( retok
&& ret
== sizeof(backupData
),
2183 "WriteFile error (replacement) %d\n", GetLastError());
2184 ok(GetFileSize(hBackupFile
, NULL
) == sizeof(backupData
),
2185 "backup file has wrong size\n");
2186 /* change the filetime on the "replaced" file to ensure that it changes */
2187 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2188 ok( ret
, "GetFileTime error (replaced) %d\n", GetLastError());
2189 ftReplaced
.dwLowDateTime
-= 600000000; /* 60 second */
2190 ret
= SetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2191 ok( ret
, "SetFileTime error (replaced) %d\n", GetLastError());
2192 GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
); /* get the actual time back */
2193 CloseHandle(hReplacedFile
);
2194 /* change the filetime on the backup to ensure that it changes */
2195 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2196 ok( ret
, "GetFileTime error (backup) %d\n", GetLastError());
2197 ftBackup
.dwLowDateTime
-= 1200000000; /* 120 second */
2198 ret
= SetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2199 ok( ret
, "SetFileTime error (backup) %d\n", GetLastError());
2200 GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
); /* get the actual time back */
2201 CloseHandle(hBackupFile
);
2202 /* get the filetime on the replacement file to perform checks */
2203 ret
= GetFileTime(hReplacementFile
, NULL
, NULL
, &ftReplacement
);
2204 ok( ret
, "GetFileTime error (replacement) %d\n", GetLastError());
2205 CloseHandle(hReplacementFile
);
2207 /* perform replacement w/ backup
2208 * TODO: flags are not implemented
2210 SetLastError(0xdeadbeef);
2211 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2212 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2213 /* make sure that the backup has the size of the old "replaced" file */
2214 hBackupFile
= CreateFileA(backup
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2215 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2216 "failed to open backup file\n");
2217 ret
= GetFileSize(hBackupFile
, NULL
);
2218 ok(ret
== sizeof(replacedData
),
2219 "backup file has wrong size %d\n", ret
);
2220 /* make sure that the "replaced" file has the size of the replacement file */
2221 hReplacedFile
= CreateFileA(replaced
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2222 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2223 "failed to open replaced file: %d\n", GetLastError());
2224 if (hReplacedFile
!= INVALID_HANDLE_VALUE
)
2226 ret
= GetFileSize(hReplacedFile
, NULL
);
2227 ok(ret
== sizeof(replacementData
),
2228 "replaced file has wrong size %d\n", ret
);
2229 /* make sure that the replacement file no-longer exists */
2230 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2231 ok(hReplacementFile
== INVALID_HANDLE_VALUE
,
2232 "unexpected error, replacement file should not exist %d\n", GetLastError());
2233 /* make sure that the backup has the old "replaced" filetime */
2234 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2235 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2236 ok(check_file_time(&ftBackup
, &ftReplaced
, 20000000), "backup file has wrong filetime\n");
2237 CloseHandle(hBackupFile
);
2238 /* make sure that the "replaced" has the old replacement filetime */
2239 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2240 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2241 ok(check_file_time(&ftReplaced
, &ftReplacement
, 20000000),
2242 "replaced file has wrong filetime %x%08x / %x%08x\n",
2243 ftReplaced
.dwHighDateTime
, ftReplaced
.dwLowDateTime
,
2244 ftReplacement
.dwHighDateTime
, ftReplacement
.dwLowDateTime
);
2245 CloseHandle(hReplacedFile
);
2248 skip("couldn't open replacement file, skipping tests\n");
2250 /* re-create replacement file for pass w/o backup (blank) */
2251 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2252 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2253 /* perform replacement w/o backup
2254 * TODO: flags are not implemented
2256 SetLastError(0xdeadbeef);
2257 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2258 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2259 "ReplaceFileA: unexpected error %d\n", GetLastError());
2261 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2262 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2263 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2264 ret
= DeleteFileA(backup
);
2265 ok(ret
, "DeleteFileA: error (backup) %d\n", GetLastError());
2266 /* perform replacement w/ backup (no pre-existing backup)
2267 * TODO: flags are not implemented
2269 SetLastError(0xdeadbeef);
2270 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2271 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2272 "ReplaceFileA: unexpected error %d\n", GetLastError());
2274 removeBackup
= TRUE
;
2276 /* re-create replacement file for pass w/ no permissions to "replaced" */
2277 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2278 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2279 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_READONLY
);
2280 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2281 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2282 /* perform replacement w/ backup (no permission to "replaced")
2283 * TODO: flags are not implemented
2285 SetLastError(0xdeadbeef);
2286 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2287 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2288 /* make sure that the replacement file still exists */
2289 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2290 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
||
2291 broken(GetLastError() == ERROR_FILE_NOT_FOUND
), /* win2k */
2292 "unexpected error, replacement file should still exist %d\n", GetLastError());
2293 CloseHandle(hReplacementFile
);
2294 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_NORMAL
);
2295 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2296 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2298 /* replacement file still exists, make pass w/o "replaced" */
2299 ret
= DeleteFileA(replaced
);
2300 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2301 "DeleteFileA: error (replaced) %d\n", GetLastError());
2302 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2303 * TODO: flags are not implemented
2305 SetLastError(0xdeadbeef);
2306 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2307 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2308 GetLastError() == ERROR_ACCESS_DENIED
),
2309 "ReplaceFileA: unexpected error %d\n", GetLastError());
2311 /* perform replacement w/o existing "replacement" file
2312 * TODO: flags are not implemented
2314 SetLastError(0xdeadbeef);
2315 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2316 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2317 GetLastError() == ERROR_ACCESS_DENIED
),
2318 "ReplaceFileA: unexpected error %d\n", GetLastError());
2321 * if the first round (w/ backup) worked then as long as there is no
2322 * failure then there is no need to check this round (w/ backup is the
2323 * more complete case)
2326 /* delete temporary files, replacement and replaced are already deleted */
2329 ret
= DeleteFileA(backup
);
2331 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2332 "DeleteFileA: error (backup) %d\n", GetLastError());
2337 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2338 * need to be as thorough.
2340 static void test_ReplaceFileW(void)
2342 WCHAR replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2343 static const WCHAR prefix
[] = {'p','f','x',0};
2344 WCHAR temp_path
[MAX_PATH
];
2346 BOOL removeBackup
= FALSE
;
2350 win_skip("ReplaceFileW() is missing\n");
2354 ret
= GetTempPathW(MAX_PATH
, temp_path
);
2355 if (ret
==0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2357 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
2358 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2360 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replaced
);
2361 ok(ret
!= 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2363 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2364 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2366 ret
= GetTempFileNameW(temp_path
, prefix
, 0, backup
);
2367 ok(ret
!= 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2369 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2370 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2372 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2373 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2374 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2375 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2376 "ReplaceFileW: error %d\n", GetLastError());
2378 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2379 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2380 ret
= DeleteFileW(backup
);
2381 ok(ret
, "DeleteFileW: error (backup) %d\n", GetLastError());
2382 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2383 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2384 "ReplaceFileW: error %d\n", GetLastError());
2386 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2387 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2388 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_READONLY
);
2389 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2390 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2392 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2393 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
,
2394 "ReplaceFileW: unexpected error %d\n", GetLastError());
2395 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_NORMAL
);
2396 ok(ret
|| GetLastError() == ERROR_ACCESS_DENIED
,
2397 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2399 removeBackup
= TRUE
;
2401 ret
= DeleteFileW(replaced
);
2402 ok(ret
, "DeleteFileW: error (replaced) %d\n", GetLastError());
2403 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2404 ok(!ret
, "ReplaceFileW: error %d\n", GetLastError());
2406 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2407 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
2408 GetLastError() == ERROR_ACCESS_DENIED
),
2409 "ReplaceFileW: unexpected error %d\n", GetLastError());
2413 ret
= DeleteFileW(backup
);
2415 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2416 "DeleteFileW: error (backup) %d\n", GetLastError());
2422 InitFunctionPointers();
2432 test_GetTempFileNameA();
2441 test_FindFirstFileA();
2442 test_FindNextFileA();
2443 test_FindFirstFileExA();
2445 test_file_sharing();
2446 test_offset_in_overlapped_structure();
2449 test_async_file_errors();
2453 test_RemoveDirectory();
2454 test_ReplaceFileA();
2455 test_ReplaceFileW();