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
27 /* ReplaceFile requires Windows 2000 or newer */
28 #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);
39 /* keep filename and filenameW the same */
40 static const char filename[] = "testfile.xxx";
41 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
42 static const char sillytext[] =
43 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
44 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
54 static void InitFunctionPointers(void)
56 HMODULE hkernel32 = GetModuleHandleA("kernel32");
58 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
59 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
60 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
63 static void test__hread( void )
72 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
73 DeleteFileA( filename );
74 filehandle = _lcreat( filename, 0 );
75 if (filehandle == HFILE_ERROR)
77 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
81 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
83 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
85 filehandle = _lopen( filename, OF_READ );
87 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
89 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
91 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
93 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
95 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
96 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
97 for (i = 0; i < bytes_wanted; i++)
99 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
103 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
105 ret = DeleteFileA( filename );
106 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
110 static void test__hwrite( void )
119 HLOCAL memory_object;
123 filehandle = _lcreat( filename, 0 );
124 if (filehandle == HFILE_ERROR)
126 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
130 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
132 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
134 filehandle = _lopen( filename, OF_READ );
136 bytes_read = _hread( filehandle, buffer, 1);
138 ok( 0 == bytes_read, "file read size error\n" );
140 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
142 filehandle = _lopen( filename, OF_READWRITE );
146 srand( (unsigned)time( NULL ) );
147 for (blocks = 0; blocks < 100; blocks++)
149 for (i = 0; i < (long)sizeof( buffer ); i++)
152 checksum[0] = checksum[0] + buffer[i];
154 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
155 bytes_written = bytes_written + sizeof( buffer );
158 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
161 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
163 memory_object = LocalAlloc( LPTR, bytes_written );
165 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
167 contents = LocalLock( memory_object );
169 filehandle = _lopen( filename, OF_READ );
171 contents = LocalLock( memory_object );
173 ok( NULL != contents, "LocalLock whines\n" );
175 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
181 checksum[0] = checksum[0] + contents[i];
184 while (i < bytes_written - 1);
186 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
188 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
190 ret = DeleteFileA( filename );
191 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
195 static void test__lclose( void )
200 filehandle = _lcreat( filename, 0 );
201 if (filehandle == HFILE_ERROR)
203 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
207 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
209 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
211 ret = DeleteFileA( filename );
212 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
216 static void test__lcreat( void )
220 WIN32_FIND_DATAA search_results;
221 char slashname[] = "testfi/";
226 filehandle = _lcreat( filename, 0 );
227 if (filehandle == HFILE_ERROR)
229 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
233 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
235 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
237 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
239 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
241 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
243 ret = DeleteFileA(filename);
244 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
246 filehandle = _lcreat( filename, 1 ); /* readonly */
247 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
249 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
251 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
253 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
255 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
257 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
259 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
261 filehandle = _lcreat( filename, 2 );
262 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
264 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
266 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
268 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
270 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
272 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
274 ret = DeleteFileA( filename );
275 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
277 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
278 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
280 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
282 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
284 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
286 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
288 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
290 ret = DeleteFileA( filename );
291 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
293 filehandle=_lcreat (slashname, 0); /* illegal name */
294 if (HFILE_ERROR==filehandle) {
296 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
297 "creating file \"%s\" failed with error %d\n", slashname, err);
298 } else { /* only NT succeeds */
300 find=FindFirstFileA (slashname, &search_results);
301 if (INVALID_HANDLE_VALUE!=find)
303 ret = FindClose (find);
304 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
305 slashname[strlen(slashname)-1]=0;
306 ok (!strcmp (slashname, search_results.cFileName),
307 "found unexpected name \"%s\"\n", search_results.cFileName);
308 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
309 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
310 search_results.dwFileAttributes);
312 ret = DeleteFileA( slashname );
313 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
316 filehandle=_lcreat (filename, 8); /* illegal attribute */
317 if (HFILE_ERROR==filehandle)
318 ok (0, "couldn't create volume label \"%s\"\n", filename);
321 find=FindFirstFileA (filename, &search_results);
322 if (INVALID_HANDLE_VALUE==find)
323 ok (0, "file \"%s\" not found\n", filename);
325 ret = FindClose(find);
326 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
327 ok (!strcmp (filename, search_results.cFileName),
328 "found unexpected name \"%s\"\n", search_results.cFileName);
329 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
330 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
331 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
332 search_results.dwFileAttributes);
334 ret = DeleteFileA( filename );
335 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
340 static void test__llseek( void )
348 filehandle = _lcreat( filename, 0 );
349 if (filehandle == HFILE_ERROR)
351 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
355 for (i = 0; i < 400; i++)
357 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
359 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
360 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
362 bytes_read = _hread( filehandle, buffer, 1);
363 ok( 1 == bytes_read, "file read size error\n" );
364 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
365 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
367 bytes_read = _hread( filehandle, buffer, 1);
368 ok( 1 == bytes_read, "file read size error\n" );
369 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
370 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
371 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
373 ret = DeleteFileA( filename );
374 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
378 static void test__llopen( void )
385 filehandle = _lcreat( filename, 0 );
386 if (filehandle == HFILE_ERROR)
388 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
392 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
393 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
395 filehandle = _lopen( filename, OF_READ );
396 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
397 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
398 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
399 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
401 filehandle = _lopen( filename, OF_READWRITE );
402 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
403 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
404 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
405 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
407 filehandle = _lopen( filename, OF_WRITE );
408 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
409 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
410 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
412 ret = DeleteFileA( filename );
413 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
414 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
418 static void test__lread( void )
427 filehandle = _lcreat( filename, 0 );
428 if (filehandle == HFILE_ERROR)
430 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
434 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
436 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
438 filehandle = _lopen( filename, OF_READ );
440 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
442 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
444 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
446 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
448 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
449 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
450 for (i = 0; i < bytes_wanted; i++)
452 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
456 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
458 ret = DeleteFileA( filename );
459 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
463 static void test__lwrite( void )
472 HLOCAL memory_object;
476 filehandle = _lcreat( filename, 0 );
477 if (filehandle == HFILE_ERROR)
479 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
483 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
485 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
487 filehandle = _lopen( filename, OF_READ );
489 bytes_read = _hread( filehandle, buffer, 1);
491 ok( 0 == bytes_read, "file read size error\n" );
493 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
495 filehandle = _lopen( filename, OF_READWRITE );
499 srand( (unsigned)time( NULL ) );
500 for (blocks = 0; blocks < 100; blocks++)
502 for (i = 0; i < (long)sizeof( buffer ); i++)
505 checksum[0] = checksum[0] + buffer[i];
507 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
508 bytes_written = bytes_written + sizeof( buffer );
511 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
514 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
516 memory_object = LocalAlloc( LPTR, bytes_written );
518 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
520 contents = LocalLock( memory_object );
522 filehandle = _lopen( filename, OF_READ );
524 contents = LocalLock( memory_object );
526 ok( NULL != contents, "LocalLock whines\n" );
528 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
534 checksum[0] += contents[i];
537 while (i < bytes_written - 1);
539 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
541 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
543 ret = DeleteFileA( filename );
544 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
547 static void test_CopyFileA(void)
549 char temp_path[MAX_PATH];
550 char source[MAX_PATH], dest[MAX_PATH];
551 static const char prefix[] = "pfx";
558 ret = GetTempPathA(MAX_PATH, temp_path);
559 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
560 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
562 ret = GetTempFileNameA(temp_path, prefix, 0, source);
563 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
565 /* make the source have not zero size */
566 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
567 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
568 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
569 ok( retok && ret == sizeof(prefix),
570 "WriteFile error %d\n", GetLastError());
571 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
572 /* get the file time and change it to prove the difference */
573 ret = GetFileTime(hfile, NULL, NULL, &ft1);
574 ok( ret, "GetFileTime error %d\n", GetLastError());
575 ft1.dwLowDateTime -= 600000000; /* 60 second */
576 ret = SetFileTime(hfile, NULL, NULL, &ft1);
577 ok( ret, "SetFileTime error %d\n", GetLastError());
578 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
581 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
582 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
584 SetLastError(0xdeadbeef);
585 ret = CopyFileA(source, dest, TRUE);
586 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
587 "CopyFileA: unexpected error %d\n", GetLastError());
589 ret = CopyFileA(source, dest, FALSE);
590 ok(ret, "CopyFileA: error %d\n", GetLastError());
592 /* make sure that destination has correct size */
593 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
594 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
595 ret = GetFileSize(hfile, NULL);
596 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
598 /* make sure that destination has the same filetime */
599 ret = GetFileTime(hfile, NULL, NULL, &ft2);
600 ok( ret, "GetFileTime error %d\n", GetLastError());
601 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
603 SetLastError(0xdeadbeef);
604 ret = CopyFileA(source, dest, FALSE);
605 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
606 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
608 /* make sure that destination still has correct size */
609 ret = GetFileSize(hfile, NULL);
610 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
611 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
612 ok( retok && ret == sizeof(prefix),
613 "ReadFile: error %d\n", GetLastError());
614 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
617 ret = DeleteFileA(source);
618 ok(ret, "DeleteFileA: error %d\n", GetLastError());
619 ret = DeleteFileA(dest);
620 ok(ret, "DeleteFileA: error %d\n", GetLastError());
623 static void test_CopyFileW(void)
625 WCHAR temp_path[MAX_PATH];
626 WCHAR source[MAX_PATH], dest[MAX_PATH];
627 static const WCHAR prefix[] = {'p','f','x',0};
630 ret = GetTempPathW(MAX_PATH, temp_path);
631 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
633 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
634 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
636 ret = GetTempFileNameW(temp_path, prefix, 0, source);
637 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
639 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
640 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
642 ret = CopyFileW(source, dest, TRUE);
643 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
644 "CopyFileW: unexpected error %d\n", GetLastError());
646 ret = CopyFileW(source, dest, FALSE);
647 ok(ret, "CopyFileW: error %d\n", GetLastError());
649 ret = DeleteFileW(source);
650 ok(ret, "DeleteFileW: error %d\n", GetLastError());
651 ret = DeleteFileW(dest);
652 ok(ret, "DeleteFileW: error %d\n", GetLastError());
655 static void test_CreateFileA(void)
658 char temp_path[MAX_PATH];
659 char filename[MAX_PATH];
660 static const char prefix[] = "pfx";
663 ret = GetTempPathA(MAX_PATH, temp_path);
664 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
665 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
667 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
668 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
670 SetLastError(0xdeadbeef);
671 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
672 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
673 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
674 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
676 SetLastError(0xdeadbeef);
677 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
678 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
679 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
680 "hFile %p, last error %u\n", hFile, GetLastError());
684 SetLastError(0xdeadbeef);
685 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
686 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
687 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
688 "hFile %p, last error %u\n", hFile, GetLastError());
692 ret = DeleteFileA(filename);
693 ok(ret, "DeleteFileA: error %d\n", GetLastError());
695 SetLastError(0xdeadbeef);
696 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
697 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
698 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
699 "hFile %p, last error %u\n", hFile, GetLastError());
703 ret = DeleteFileA(filename);
704 ok(ret, "DeleteFileA: error %d\n", GetLastError());
707 static void test_CreateFileW(void)
710 WCHAR temp_path[MAX_PATH];
711 WCHAR filename[MAX_PATH];
712 static const WCHAR emptyW[]={'\0'};
713 static const WCHAR prefix[] = {'p','f','x',0};
714 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
717 ret = GetTempPathW(MAX_PATH, temp_path);
718 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
720 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
721 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
723 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
724 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
726 SetLastError(0xdeadbeef);
727 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
728 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
729 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
730 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
732 SetLastError(0xdeadbeef);
733 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
734 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
735 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
736 "hFile %p, last error %u\n", hFile, GetLastError());
740 SetLastError(0xdeadbeef);
741 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
742 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
743 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
744 "hFile %p, last error %u\n", hFile, GetLastError());
748 ret = DeleteFileW(filename);
749 ok(ret, "DeleteFileW: error %d\n", GetLastError());
751 SetLastError(0xdeadbeef);
752 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
753 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
754 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
755 "hFile %p, last error %u\n", hFile, GetLastError());
759 ret = DeleteFileW(filename);
760 ok(ret, "DeleteFileW: error %d\n", GetLastError());
764 /* this crashes on NT4.0 */
765 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
766 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
767 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
768 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
771 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
772 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
773 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
774 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
776 /* test the result of opening a nonexistent driver name */
777 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
778 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
779 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
780 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
783 static void test_GetTempFileNameA(void)
787 char expected[MAX_PATH + 10];
788 char windowsdir[MAX_PATH + 10];
789 char windowsdrive[3];
791 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
792 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
793 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
795 /* If the Windows directory is the root directory, it ends in backslash, not else. */
796 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
798 strcat(windowsdir, "\\");
801 windowsdrive[0] = windowsdir[0];
802 windowsdrive[1] = windowsdir[1];
803 windowsdrive[2] = '\0';
805 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
806 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
807 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
808 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
809 windowsdrive[0], out);
811 result = GetTempFileNameA(windowsdir, "abc", 2, out);
812 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
814 strcat(expected, windowsdir);
815 strcat(expected, "abc2.tmp");
816 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
820 static void test_DeleteFileA( void )
824 ret = DeleteFileA(NULL);
825 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
826 GetLastError() == ERROR_PATH_NOT_FOUND),
827 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
829 ret = DeleteFileA("");
830 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
831 GetLastError() == ERROR_BAD_PATHNAME),
832 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
834 ret = DeleteFileA("nul");
835 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
836 GetLastError() == ERROR_INVALID_PARAMETER ||
837 GetLastError() == ERROR_ACCESS_DENIED ||
838 GetLastError() == ERROR_INVALID_FUNCTION),
839 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
842 static void test_DeleteFileW( void )
845 WCHAR pathW[MAX_PATH];
846 WCHAR pathsubW[MAX_PATH];
847 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
848 static const WCHAR subdirW[] = {'\\','s','u','b',0};
849 static const WCHAR emptyW[]={'\0'};
851 ret = DeleteFileW(NULL);
852 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
854 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
855 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
857 ret = DeleteFileW(emptyW);
858 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
859 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
861 /* test DeleteFile on empty directory */
862 ret = GetTempPathW(MAX_PATH, pathW);
863 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
865 ok(0, "MAX_PATH exceeded in constructing paths\n");
868 lstrcatW(pathW, dirW);
869 lstrcpyW(pathsubW, pathW);
870 lstrcatW(pathsubW, subdirW);
871 ret = CreateDirectoryW(pathW, NULL);
872 ok(ret == TRUE, "couldn't create directory deletefile\n");
873 ret = DeleteFileW(pathW);
874 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
875 ret = RemoveDirectoryW(pathW);
876 ok(ret == TRUE, "expected to remove directory deletefile\n");
878 /* test DeleteFile on non-empty directory */
879 ret = CreateDirectoryW(pathW, NULL);
880 ok(ret == TRUE, "couldn't create directory deletefile\n");
881 ret = CreateDirectoryW(pathsubW, NULL);
882 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
883 ret = DeleteFileW(pathW);
884 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
885 ret = RemoveDirectoryW(pathsubW);
886 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
887 ret = RemoveDirectoryW(pathW);
888 ok(ret == TRUE, "expected to remove directory deletefile\n");
891 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
893 static void test_MoveFileA(void)
895 char tempdir[MAX_PATH];
896 char source[MAX_PATH], dest[MAX_PATH];
897 static const char prefix[] = "pfx";
900 ret = GetTempPathA(MAX_PATH, tempdir);
901 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
902 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
904 ret = GetTempFileNameA(tempdir, prefix, 0, source);
905 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
907 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
908 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
910 ret = MoveFileA(source, dest);
911 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
912 "MoveFileA: unexpected error %d\n", GetLastError());
914 ret = DeleteFileA(dest);
915 ok(ret, "DeleteFileA: error %d\n", GetLastError());
917 ret = MoveFileA(source, dest);
918 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
920 lstrcatA(tempdir, "Remove Me");
921 ret = CreateDirectoryA(tempdir, NULL);
922 ok(ret == TRUE, "CreateDirectoryA failed\n");
924 lstrcpyA(source, dest);
925 lstrcpyA(dest, tempdir);
926 lstrcatA(dest, "\\wild?.*");
927 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
928 ret = MoveFileA(source, dest);
929 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
930 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
931 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
932 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
933 if (ret || (GetLastError() != ERROR_INVALID_NAME))
936 char temppath[MAX_PATH];
939 lstrcpyA(temppath, tempdir);
940 lstrcatA(temppath, "\\*.*");
941 hFind = FindFirstFileA(temppath, &fd);
942 if (INVALID_HANDLE_VALUE != hFind)
947 lpName = fd.cAlternateFileName;
949 lpName = fd.cFileName;
950 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
952 while (FindNextFileA(hFind, &fd));
956 ret = DeleteFileA(source);
957 ok(ret, "DeleteFileA: error %d\n", GetLastError());
958 ret = DeleteFileA(dest);
959 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
960 ret = RemoveDirectoryA(tempdir);
961 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
964 static void test_MoveFileW(void)
966 WCHAR temp_path[MAX_PATH];
967 WCHAR source[MAX_PATH], dest[MAX_PATH];
968 static const WCHAR prefix[] = {'p','f','x',0};
971 ret = GetTempPathW(MAX_PATH, temp_path);
972 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
974 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
975 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
977 ret = GetTempFileNameW(temp_path, prefix, 0, source);
978 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
980 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
981 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
983 ret = MoveFileW(source, dest);
984 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
985 "CopyFileW: unexpected error %d\n", GetLastError());
987 ret = DeleteFileW(source);
988 ok(ret, "DeleteFileW: error %d\n", GetLastError());
989 ret = DeleteFileW(dest);
990 ok(ret, "DeleteFileW: error %d\n", GetLastError());
993 #define PATTERN_OFFSET 0x10
995 static void test_offset_in_overlapped_structure(void)
1001 BYTE buf[256], pattern[] = "TeSt";
1003 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
1006 ret =GetTempPathA(MAX_PATH, temp_path);
1007 ok( ret, "GetTempPathA error %d\n", GetLastError());
1008 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
1009 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
1011 /*** Write File *****************************************************/
1013 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1014 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1016 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
1017 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
1018 ok( ret, "WriteFile error %d\n", GetLastError());
1019 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
1021 memset(&ov, 0, sizeof(ov));
1022 S(U(ov)).Offset = PATTERN_OFFSET;
1023 S(U(ov)).OffsetHigh = 0;
1024 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1025 /* Win 9x does not support the overlapped I/O on files */
1026 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1027 ok(rc, "WriteFile error %d\n", GetLastError());
1028 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1029 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1030 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1032 S(U(ov)).Offset = sizeof(buf) * 2;
1033 S(U(ov)).OffsetHigh = 0;
1034 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1035 ok( ret, "WriteFile error %d\n", GetLastError());
1036 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1037 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1038 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1043 /*** Read File *****************************************************/
1045 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1046 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1048 memset(buf, 0, sizeof(buf));
1049 memset(&ov, 0, sizeof(ov));
1050 S(U(ov)).Offset = PATTERN_OFFSET;
1051 S(U(ov)).OffsetHigh = 0;
1052 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1053 /* Win 9x does not support the overlapped I/O on files */
1054 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1055 ok(rc, "ReadFile error %d\n", GetLastError());
1056 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1057 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1058 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1059 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1064 ret = DeleteFileA(temp_fname);
1065 ok( ret, "DeleteFileA error %d\n", GetLastError());
1068 static void test_LockFile(void)
1072 OVERLAPPED overlapped;
1073 int limited_LockFile;
1074 int limited_UnLockFile;
1076 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1077 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1078 CREATE_ALWAYS, 0, 0 );
1079 if (handle == INVALID_HANDLE_VALUE)
1081 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1084 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1086 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1087 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1089 limited_UnLockFile = 0;
1090 if (UnlockFile( handle, 0, 0, 0, 0 ))
1092 limited_UnLockFile = 1;
1095 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1096 /* overlapping locks must fail */
1097 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1098 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1099 /* non-overlapping locks must succeed */
1100 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1102 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1103 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1104 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1105 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1107 S(U(overlapped)).Offset = 100;
1108 S(U(overlapped)).OffsetHigh = 0;
1109 overlapped.hEvent = 0;
1111 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1112 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1114 /* LockFileEx is probably OK, test it more. */
1115 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1116 "LockFileEx 100,100 failed\n" );
1119 /* overlapping shared locks are OK */
1120 S(U(overlapped)).Offset = 150;
1121 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1123 /* but exclusive is not */
1124 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1125 0, 50, 0, &overlapped ),
1126 "LockFileEx exclusive 150,50 succeeded\n" );
1127 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1128 { /* UnLockFile is capable. */
1129 S(U(overlapped)).Offset = 100;
1130 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1131 "UnlockFileEx 150,100 again succeeded\n" );
1134 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1135 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1136 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1137 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1139 /* wrap-around lock should not do anything */
1140 /* (but still succeeds on NT4 so we don't check result) */
1141 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1143 limited_LockFile = 0;
1144 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1146 limited_LockFile = 1;
1149 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1151 /* zero-byte lock */
1152 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1153 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1154 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1155 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1157 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1158 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1160 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1162 CloseHandle( handle );
1163 DeleteFileA( filename );
1166 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1170 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1171 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1176 if (!access1) access1 = GENERIC_READ;
1179 if (!access2) access2 = GENERIC_READ;
1182 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1183 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1184 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1185 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1186 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1187 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1191 static void test_file_sharing(void)
1193 static const DWORD access_modes[] =
1194 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1195 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1196 static const DWORD sharing_modes[] =
1197 { 0, FILE_SHARE_READ,
1198 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1199 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1200 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1204 BOOL is_win9x = FALSE;
1206 /* make sure the file exists */
1207 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1208 if (h == INVALID_HANDLE_VALUE)
1210 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1213 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1216 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1218 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1220 /* Win9x doesn't support FILE_SHARE_DELETE */
1221 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1224 SetLastError(0xdeadbeef);
1225 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1226 NULL, OPEN_EXISTING, 0, 0 );
1227 if (h == INVALID_HANDLE_VALUE)
1229 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1232 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1234 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1236 /* Win9x doesn't support FILE_SHARE_DELETE */
1237 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1240 SetLastError(0xdeadbeef);
1241 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1242 NULL, OPEN_EXISTING, 0, 0 );
1244 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1245 access_modes[a2], sharing_modes[s2], is_win9x ))
1247 ret = GetLastError();
1249 ok( h2 != INVALID_HANDLE_VALUE,
1250 "open failed for modes %x/%x/%x/%x\n",
1251 access_modes[a1], sharing_modes[s1],
1252 access_modes[a2], sharing_modes[s2] );
1253 ok( ret == 0xdeadbeef /* Win9x */ ||
1255 "wrong error code %d\n", ret );
1261 ret = GetLastError();
1263 ok( h2 == INVALID_HANDLE_VALUE,
1264 "open succeeded for modes %x/%x/%x/%x\n",
1265 access_modes[a1], sharing_modes[s1],
1266 access_modes[a2], sharing_modes[s2] );
1267 ok( ret == ERROR_SHARING_VIOLATION,
1268 "wrong error code %d\n", ret );
1276 SetLastError(0xdeadbeef);
1277 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1278 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1280 SetLastError(0xdeadbeef);
1281 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1282 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1283 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1285 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1286 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1291 DeleteFileA( filename );
1294 static char get_windows_drive(void)
1296 char windowsdir[MAX_PATH];
1297 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1298 return windowsdir[0];
1301 static void test_FindFirstFileA(void)
1304 WIN32_FIND_DATAA data;
1306 char buffer[5] = "C:\\";
1309 /* try FindFirstFileA on "C:\" */
1310 buffer[0] = get_windows_drive();
1312 SetLastError( 0xdeadbeaf );
1313 handle = FindFirstFileA(buffer, &data);
1314 err = GetLastError();
1315 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1316 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1318 /* try FindFirstFileA on "C:\*" */
1319 strcpy(buffer2, buffer);
1320 strcat(buffer2, "*");
1321 handle = FindFirstFileA(buffer2, &data);
1322 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1323 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1324 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1325 if (FindNextFileA( handle, &data ))
1326 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1327 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1328 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1330 /* try FindFirstFileA on windows dir */
1331 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1332 strcat(buffer2, "\\*");
1333 handle = FindFirstFileA(buffer2, &data);
1334 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1335 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1336 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1337 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1338 while (FindNextFileA( handle, &data ))
1339 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1340 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1341 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1343 /* try FindFirstFileA on "C:\foo\" */
1344 SetLastError( 0xdeadbeaf );
1345 strcpy(buffer2, buffer);
1346 strcat(buffer2, "foo\\");
1347 handle = FindFirstFileA(buffer2, &data);
1348 err = GetLastError();
1349 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1351 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1354 /* try FindFirstFileA on "C:\foo\bar.txt" */
1355 SetLastError( 0xdeadbeaf );
1356 strcpy(buffer2, buffer);
1357 strcat(buffer2, "foo\\bar.txt");
1358 handle = FindFirstFileA(buffer2, &data);
1359 err = GetLastError();
1360 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1361 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1363 /* try FindFirstFileA on "C:\foo\*.*" */
1364 SetLastError( 0xdeadbeaf );
1365 strcpy(buffer2, buffer);
1366 strcat(buffer2, "foo\\*.*");
1367 handle = FindFirstFileA(buffer2, &data);
1368 err = GetLastError();
1369 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1370 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1372 /* try FindFirstFileA on "foo\bar.txt" */
1373 SetLastError( 0xdeadbeaf );
1374 strcpy(buffer2, "foo\\bar.txt");
1375 handle = FindFirstFileA(buffer2, &data);
1376 err = GetLastError();
1377 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1378 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1380 /* try FindFirstFileA on "c:\nul" */
1381 SetLastError( 0xdeadbeaf );
1382 strcpy(buffer2, buffer);
1383 strcat(buffer2, "nul");
1384 handle = FindFirstFileA(buffer2, &data);
1385 err = GetLastError();
1386 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1387 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1388 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1389 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1390 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1391 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1392 "wrong attributes %x\n", data.dwFileAttributes );
1393 SetLastError( 0xdeadbeaf );
1394 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1395 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1396 ok( FindClose( handle ), "failed to close handle\n" );
1398 /* try FindFirstFileA on "lpt1" */
1399 SetLastError( 0xdeadbeaf );
1400 strcpy(buffer2, "lpt1");
1401 handle = FindFirstFileA(buffer2, &data);
1402 err = GetLastError();
1403 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1404 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1405 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1406 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1407 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1408 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1409 "wrong attributes %x\n", data.dwFileAttributes );
1410 SetLastError( 0xdeadbeaf );
1411 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1412 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1413 ok( FindClose( handle ), "failed to close handle\n" );
1415 /* try FindFirstFileA on "c:\nul\*" */
1416 SetLastError( 0xdeadbeaf );
1417 strcpy(buffer2, buffer);
1418 strcat(buffer2, "nul\\*");
1419 handle = FindFirstFileA(buffer2, &data);
1420 err = GetLastError();
1421 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1422 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1424 /* try FindFirstFileA on "c:\nul*" */
1425 SetLastError( 0xdeadbeaf );
1426 strcpy(buffer2, buffer);
1427 strcat(buffer2, "nul*");
1428 handle = FindFirstFileA(buffer2, &data);
1429 err = GetLastError();
1430 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1431 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1433 /* try FindFirstFileA on "c:\foo\bar\nul" */
1434 SetLastError( 0xdeadbeaf );
1435 strcpy(buffer2, buffer);
1436 strcat(buffer2, "foo\\bar\\nul");
1437 handle = FindFirstFileA(buffer2, &data);
1438 err = GetLastError();
1439 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1440 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1442 /* try FindFirstFileA on "c:\foo\nul\bar" */
1443 SetLastError( 0xdeadbeaf );
1444 strcpy(buffer2, buffer);
1445 strcat(buffer2, "foo\\nul\\bar");
1446 handle = FindFirstFileA(buffer2, &data);
1447 err = GetLastError();
1448 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1449 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1452 static void test_FindNextFileA(void)
1455 WIN32_FIND_DATAA search_results;
1457 char buffer[5] = "C:\\*";
1459 buffer[0] = get_windows_drive();
1460 handle = FindFirstFileA(buffer,&search_results);
1461 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1462 while (FindNextFile(handle, &search_results))
1464 /* get to the end of the files */
1466 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1467 err = GetLastError();
1468 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1471 static void test_FindFirstFileExA(void)
1473 WIN32_FIND_DATAA search_results;
1476 if (!pFindFirstFileExA)
1478 skip("FindFirstFileExA() is missing\n");
1482 CreateDirectoryA("test-dir", NULL);
1483 _lclose(_lcreat("test-dir\\file1", 0));
1484 _lclose(_lcreat("test-dir\\file2", 0));
1485 CreateDirectoryA("test-dir\\dir1", NULL);
1486 /* FindExLimitToDirectories is ignored */
1487 SetLastError(0xdeadbeef);
1488 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, FindExSearchLimitToDirectories, NULL, 0);
1489 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1491 skip("FindFirstFileExA is not implemented\n");
1494 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1495 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1497 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1499 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1500 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1502 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1503 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
1505 ok(FindNextFile(handle, &search_results), "Fetching fourth file failed\n");
1506 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1508 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1509 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1513 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should failed\n");
1515 FindClose( handle );
1518 DeleteFileA("test-dir\\file1");
1519 DeleteFileA("test-dir\\file2");
1520 RemoveDirectoryA("test-dir\\dir1");
1521 RemoveDirectoryA("test-dir");
1524 static int test_Mapfile_createtemp(HANDLE *handle)
1526 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1527 DeleteFile(filename);
1528 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1529 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1530 if (*handle != INVALID_HANDLE_VALUE) {
1538 static void test_MapFile(void)
1543 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1545 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1546 ok( hmap != NULL, "mapping should work, I named it!\n" );
1548 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1550 /* We have to close file before we try new stuff with mapping again.
1551 Else we would always succeed on XP or block descriptors on 95. */
1552 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1553 ok( hmap != NULL, "We should still be able to map!\n" );
1554 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1555 ok( CloseHandle( handle ), "can't close file handle\n");
1558 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1560 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1561 ok( hmap == NULL, "mapped zero size file\n");
1562 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1564 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1565 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
1566 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1568 CloseHandle( hmap );
1570 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1571 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
1572 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1574 CloseHandle( hmap );
1576 /* On XP you can now map again, on Win 95 you cannot. */
1578 ok( CloseHandle( handle ), "can't close file handle\n");
1579 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1582 static void test_GetFileType(void)
1585 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1586 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1587 type = GetFileType(h);
1588 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1590 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1591 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1592 type = GetFileType(h);
1593 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1595 DeleteFileA( filename );
1598 static int completion_count;
1600 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1602 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1603 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1607 static void test_async_file_errors(void)
1609 char szFile[MAX_PATH];
1610 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1612 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1614 S(U(ovl)).Offset = 0;
1615 S(U(ovl)).OffsetHigh = 0;
1617 completion_count = 0;
1619 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1620 strcat(szFile, "\\win.ini");
1621 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1622 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1623 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
1624 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1625 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1626 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
1631 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1633 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1634 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1637 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
1639 S(U(ovl)).Offset += count;
1640 /* i/o completion routine only called if ReadFileEx returned success.
1641 * we only care about violations of this rule so undo what should have
1645 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1646 /*printf("Error = %ld\n", GetLastError());*/
1647 HeapFree(GetProcessHeap(), 0, lpBuffer);
1650 static void test_read_write(void)
1654 char temp_path[MAX_PATH];
1655 char filename[MAX_PATH];
1656 static const char prefix[] = "pfx";
1658 ret = GetTempPathA(MAX_PATH, temp_path);
1659 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1660 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1662 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1663 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1665 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1666 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1667 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1669 SetLastError(12345678);
1671 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1672 ok(ret && GetLastError() == 12345678,
1673 "ret = %d, error %d\n", ret, GetLastError());
1674 ok(!bytes, "bytes = %d\n", bytes);
1676 SetLastError(12345678);
1678 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1679 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1680 (ret && GetLastError() == 12345678), /* Win9x */
1681 "ret = %d, error %d\n", ret, GetLastError());
1682 ok(!bytes || /* Win2k */
1683 bytes == 10, /* Win9x */
1684 "bytes = %d\n", bytes);
1686 /* make sure the file contains data */
1687 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1688 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1690 SetLastError(12345678);
1692 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1693 ok(ret && GetLastError() == 12345678,
1694 "ret = %d, error %d\n", ret, GetLastError());
1695 ok(!bytes, "bytes = %d\n", bytes);
1697 SetLastError(12345678);
1699 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1700 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1701 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1702 "ret = %d, error %d\n", ret, GetLastError());
1703 ok(!bytes, "bytes = %d\n", bytes);
1705 ret = CloseHandle(hFile);
1706 ok( ret, "CloseHandle: error %d\n", GetLastError());
1707 ret = DeleteFileA(filename);
1708 ok( ret, "DeleteFileA: error %d\n", GetLastError());
1711 static void test_OpenFile(void)
1718 static const char *file = "\\regedit.exe";
1719 static const char *foo = ".\\foo-bar-foo.baz";
1720 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1721 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1722 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1723 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1724 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1725 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1726 static const char *backslash = "\\";
1727 char buff[MAX_PATH];
1728 char buff_long[4*MAX_PATH];
1729 char filled_0xA5[OFS_MAXPATHNAME];
1732 /* Check for existing file */
1733 length = GetWindowsDirectoryA(buff, MAX_PATH);
1735 if (length + lstrlen(file) < MAX_PATH)
1737 lstrcatA(buff, file);
1738 memset(&ofs, 0xA5, sizeof(ofs));
1739 SetLastError(0xfaceabee);
1741 hFile = OpenFile(buff, &ofs, OF_EXIST);
1742 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1743 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1744 "GetLastError() returns %d\n", GetLastError() );
1745 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1746 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1747 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1748 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1749 ofs.szPathName, buff );
1752 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1753 length = GetCurrentDirectoryA(MAX_PATH, buff);
1755 /* Check for nonexistent file */
1756 if (length + lstrlenA(foo + 1) < MAX_PATH)
1758 lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1759 memset(&ofs, 0xA5, sizeof(ofs));
1760 SetLastError(0xfaceabee);
1762 hFile = OpenFile(foo, &ofs, OF_EXIST);
1763 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1764 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1766 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1767 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1768 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1769 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1770 ofs.szPathName, buff );
1773 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1774 length += lstrlenA(foo_too_long + 1);
1776 /* Check for nonexistent file with too long filename */
1777 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
1779 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1780 memset(&ofs, 0xA5, sizeof(ofs));
1781 SetLastError(0xfaceabee);
1783 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1784 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1785 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
1786 "GetLastError() returns %d\n", GetLastError() );
1788 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1789 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1790 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1791 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1792 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1796 length = GetCurrentDirectoryA(MAX_PATH, buff);
1797 length += lstrlenA(backslash);
1798 length += lstrlenA(filename);
1800 if (length >= MAX_PATH)
1802 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
1805 lstrcatA(buff, backslash);
1806 lstrcatA(buff, filename);
1808 memset(&ofs, 0xA5, sizeof(ofs));
1809 SetLastError(0xfaceabee);
1810 /* Create an empty file */
1811 hFile = OpenFile(filename, &ofs, OF_CREATE);
1812 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1813 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1814 "GetLastError() returns %d\n", GetLastError() );
1815 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1816 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1817 ret = CloseHandle((HANDLE)hFile);
1818 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1819 retval = GetFileAttributesA(filename);
1820 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1822 memset(&ofs, 0xA5, sizeof(ofs));
1823 SetLastError(0xfaceabee);
1824 /* Check various opening options: */
1825 /* for reading only, */
1826 hFile = OpenFile(filename, &ofs, OF_READ);
1827 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
1828 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1829 "GetLastError() returns %d\n", GetLastError() );
1830 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1831 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1832 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1833 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1834 ret = CloseHandle((HANDLE)hFile);
1835 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1837 memset(&ofs, 0xA5, sizeof(ofs));
1838 SetLastError(0xfaceabee);
1839 /* for writing only, */
1840 hFile = OpenFile(filename, &ofs, OF_WRITE);
1841 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
1842 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1843 "GetLastError() returns %d\n", GetLastError() );
1844 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1845 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1846 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1847 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1848 ret = CloseHandle((HANDLE)hFile);
1849 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1851 memset(&ofs, 0xA5, sizeof(ofs));
1852 SetLastError(0xfaceabee);
1853 /* for reading and writing, */
1854 hFile = OpenFile(filename, &ofs, OF_READWRITE);
1855 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1856 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1857 "GetLastError() returns %d\n", GetLastError() );
1858 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1859 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1860 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1861 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1862 ret = CloseHandle((HANDLE)hFile);
1863 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1865 memset(&ofs, 0xA5, sizeof(ofs));
1866 SetLastError(0xfaceabee);
1867 /* for checking file presence. */
1868 hFile = OpenFile(filename, &ofs, OF_EXIST);
1869 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1870 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1871 "GetLastError() returns %d\n", GetLastError() );
1872 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1873 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1874 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1875 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1877 memset(&ofs, 0xA5, sizeof(ofs));
1878 SetLastError(0xfaceabee);
1879 /* Delete the file and make sure it doesn't exist anymore */
1880 hFile = OpenFile(filename, &ofs, OF_DELETE);
1881 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1882 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1883 "GetLastError() returns %d\n", GetLastError() );
1884 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1885 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1886 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1887 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1889 retval = GetFileAttributesA(filename);
1890 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1893 static void test_overlapped(void)
1898 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1900 memset( &ov, 0, sizeof ov );
1902 r = GetOverlappedResult(0, &ov, &result, 0);
1903 ok( r == TRUE, "should return true\n");
1904 ok( result == 0, "wrong result %u\n", result );
1908 ov.InternalHigh = 0xabcd;
1909 r = GetOverlappedResult(0, &ov, &result, 0);
1910 ok( r == TRUE, "should return true\n");
1911 ok( result == 0xabcd, "wrong result %u\n", result );
1913 SetLastError( 0xb00 );
1915 ov.Internal = STATUS_INVALID_HANDLE;
1916 ov.InternalHigh = 0xabcd;
1917 r = GetOverlappedResult(0, &ov, &result, 0);
1918 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1919 ok( r == FALSE, "should return false\n");
1920 ok( result == 0xabcd, "wrong result %u\n", result );
1922 SetLastError( 0xb00 );
1924 ov.Internal = STATUS_PENDING;
1925 ov.InternalHigh = 0xabcd;
1926 r = GetOverlappedResult(0, &ov, &result, 0);
1927 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1928 ok( r == FALSE, "should return false\n");
1929 ok( result == 0, "wrong result %u\n", result );
1931 SetLastError( 0xb00 );
1932 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
1933 ov.Internal = STATUS_PENDING;
1934 ov.InternalHigh = 0xabcd;
1935 r = GetOverlappedResult(0, &ov, &result, 0);
1936 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1937 ok( r == FALSE, "should return false\n");
1939 ResetEvent( ov.hEvent );
1941 SetLastError( 0xb00 );
1942 ov.Internal = STATUS_PENDING;
1943 ov.InternalHigh = 0;
1944 r = GetOverlappedResult(0, &ov, &result, 0);
1945 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1946 ok( r == FALSE, "should return false\n");
1948 r = CloseHandle( ov.hEvent );
1949 ok( r == TRUE, "close handle failed\n");
1952 static void test_RemoveDirectory(void)
1955 char directory[] = "removeme";
1957 rc = CreateDirectory(directory, NULL);
1958 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
1960 rc = SetCurrentDirectory(directory);
1961 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1963 rc = RemoveDirectory(".");
1965 ok( !rc, "RemoveDirectory unexpectedly worked\n" );
1968 rc = SetCurrentDirectory("..");
1969 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1971 rc = RemoveDirectory(directory);
1973 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1977 static void test_ReplaceFileA(void)
1979 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
1980 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
1981 static const char replacedData[] = "file-to-replace";
1982 static const char replacementData[] = "new-file";
1983 static const char backupData[] = "backup-file";
1984 FILETIME ftReplaced, ftReplacement, ftBackup;
1985 static const char prefix[] = "pfx";
1986 char temp_path[MAX_PATH];
1992 skip("ReplaceFileA() is missing\n");
1996 ret = GetTempPathA(MAX_PATH, temp_path);
1997 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1998 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2000 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
2001 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2003 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2004 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2006 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
2007 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2009 /* place predictable data in the file to be replaced */
2010 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2011 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2012 "failed to open replaced file\n");
2013 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
2014 ok( retok && ret == sizeof(replacedData),
2015 "WriteFile error (replaced) %d\n", GetLastError());
2016 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
2017 "replaced file has wrong size\n");
2018 /* place predictable data in the file to be the replacement */
2019 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2020 ok(hReplacementFile != INVALID_HANDLE_VALUE,
2021 "failed to open replacement file\n");
2022 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
2023 ok( retok && ret == sizeof(replacementData),
2024 "WriteFile error (replacement) %d\n", GetLastError());
2025 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
2026 "replacement file has wrong size\n");
2027 /* place predictable data in the backup file (to be over-written) */
2028 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2029 ok(hBackupFile != INVALID_HANDLE_VALUE,
2030 "failed to open backup file\n");
2031 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
2032 ok( retok && ret == sizeof(backupData),
2033 "WriteFile error (replacement) %d\n", GetLastError());
2034 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
2035 "backup file has wrong size\n");
2036 /* change the filetime on the "replaced" file to ensure that it changes */
2037 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2038 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
2039 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
2040 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2041 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
2042 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
2043 CloseHandle(hReplacedFile);
2044 /* change the filetime on the backup to ensure that it changes */
2045 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2046 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
2047 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
2048 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2049 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
2050 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
2051 CloseHandle(hBackupFile);
2052 /* get the filetime on the replacement file to perform checks */
2053 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
2054 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
2055 CloseHandle(hReplacementFile);
2057 /* perform replacement w/ backup
2058 * TODO: flags are not implemented
2060 SetLastError(0xdeadbeef);
2061 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2062 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2063 /* make sure that the backup has the size of the old "replaced" file */
2064 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2065 ok(hBackupFile != INVALID_HANDLE_VALUE,
2066 "failed to open backup file\n");
2067 ret = GetFileSize(hBackupFile, NULL);
2068 ok(ret == sizeof(replacedData),
2069 "backup file has wrong size %d\n", ret);
2070 /* make sure that the "replaced" file has the size of the replacement file */
2071 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2072 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2073 "failed to open replaced file\n");
2074 ret = GetFileSize(hReplacedFile, NULL);
2075 ok(ret == sizeof(replacementData),
2076 "replaced file has wrong size %d\n", ret);
2077 /* make sure that the replacement file no-longer exists */
2078 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2079 ok(hReplacementFile == INVALID_HANDLE_VALUE,
2080 "unexpected error, replacement file should not exist %d\n", GetLastError());
2081 /* make sure that the backup has the old "replaced" filetime */
2082 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2083 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2084 ok(CompareFileTime(&ftBackup, &ftReplaced) == 0,
2085 "backup file has wrong filetime\n");
2086 CloseHandle(hBackupFile);
2087 /* make sure that the "replaced" has the old replacement filetime */
2088 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2089 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2090 ok(CompareFileTime(&ftReplaced, &ftReplacement) == 0,
2091 "replaced file has wrong filetime %x%08x / %x%08x\n",
2092 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
2093 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
2094 CloseHandle(hReplacedFile);
2096 /* re-create replacement file for pass w/o backup (blank) */
2097 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2098 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2099 /* perform replacement w/o backup
2100 * TODO: flags are not implemented
2102 SetLastError(0xdeadbeef);
2103 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2104 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2106 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2107 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2108 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2109 ret = DeleteFileA(backup);
2110 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2111 /* perform replacement w/ backup (no pre-existing backup)
2112 * TODO: flags are not implemented
2114 SetLastError(0xdeadbeef);
2115 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2116 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2118 /* re-create replacement file for pass w/ no permissions to "replaced" */
2119 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2120 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2121 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2122 ok(ret, "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2123 /* perform replacement w/ backup (no permission to "replaced")
2124 * TODO: flags are not implemented
2126 SetLastError(0xdeadbeef);
2127 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2128 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2129 /* make sure that the replacement file still exists */
2130 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2131 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
2132 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
2133 "unexpected error, replacement file should still exist %d\n", GetLastError());
2134 CloseHandle(hReplacementFile);
2135 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2136 ok(ret, "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2138 /* replacement file still exists, make pass w/o "replaced" */
2139 ret = DeleteFileA(replaced);
2140 ok(ret, "DeleteFileA: error (replaced) %d\n", GetLastError());
2141 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2142 * TODO: flags are not implemented
2144 SetLastError(0xdeadbeef);
2145 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2146 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2147 "ReplaceFileA: unexpected error %d\n", GetLastError());
2149 /* perform replacement w/o existing "replacement" file
2150 * TODO: flags are not implemented
2152 SetLastError(0xdeadbeef);
2153 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2154 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2155 "ReplaceFileA: unexpected error %d\n", GetLastError());
2158 * if the first round (w/ backup) worked then as long as there is no
2159 * failure then there is no need to check this round (w/ backup is the
2160 * more complete case)
2163 /* delete temporary files, replacement and replaced are already deleted */
2164 ret = DeleteFileA(backup);
2166 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2167 "DeleteFileA: error (backup) %d\n", GetLastError());
2171 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2172 * need to be as thorough.
2174 static void test_ReplaceFileW(void)
2176 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2177 static const WCHAR prefix[] = {'p','f','x',0};
2178 WCHAR temp_path[MAX_PATH];
2183 skip("ReplaceFileW() is missing\n");
2187 ret = GetTempPathW(MAX_PATH, temp_path);
2188 if (ret==0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2190 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2191 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2193 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
2194 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2196 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2197 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2199 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
2200 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2202 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2203 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2205 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2206 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2207 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2208 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2210 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2211 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2212 ret = DeleteFileW(backup);
2213 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
2214 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2215 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2217 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2218 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2219 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
2220 ok(ret, "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2222 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2223 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
2224 "ReplaceFileW: unexpected error %d\n", GetLastError());
2225 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
2226 ok(ret, "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2228 ret = DeleteFileW(replaced);
2229 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
2230 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2231 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
2233 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2234 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2235 "ReplaceFileW: unexpected error %d\n", GetLastError());
2237 ret = DeleteFileW(backup);
2239 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2240 "DeleteFileW: error (backup) %d\n", GetLastError());
2245 InitFunctionPointers();
2255 test_GetTempFileNameA();
2264 test_FindFirstFileA();
2265 test_FindNextFileA();
2266 test_FindFirstFileExA();
2268 test_file_sharing();
2269 test_offset_in_overlapped_structure();
2272 test_async_file_errors();
2276 test_RemoveDirectory();
2277 test_ReplaceFileA();
2278 test_ReplaceFileW();