2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 /* ReplaceFile requires Windows 2000 or newer */
27 #define _WIN32_WINNT 0x0500
29 #include "wine/test.h"
34 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
35 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
36 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
38 /* keep filename and filenameW the same */
39 static const char filename[] = "testfile.xxx";
40 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
41 static const char sillytext[] =
42 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
43 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
53 static void InitFunctionPointers(void)
55 HMODULE hkernel32 = GetModuleHandleA("kernel32");
57 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
58 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
59 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
62 static void test__hread( void )
71 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
72 DeleteFileA( filename );
73 filehandle = _lcreat( filename, 0 );
74 if (filehandle == HFILE_ERROR)
76 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
80 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
82 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
84 filehandle = _lopen( filename, OF_READ );
86 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
88 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
90 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
92 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
94 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
95 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
96 for (i = 0; i < bytes_wanted; i++)
98 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
102 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
104 ret = DeleteFileA( filename );
105 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
109 static void test__hwrite( void )
118 HLOCAL memory_object;
122 filehandle = _lcreat( filename, 0 );
123 if (filehandle == HFILE_ERROR)
125 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
129 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
131 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
133 filehandle = _lopen( filename, OF_READ );
135 bytes_read = _hread( filehandle, buffer, 1);
137 ok( 0 == bytes_read, "file read size error\n" );
139 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
141 filehandle = _lopen( filename, OF_READWRITE );
145 srand( (unsigned)time( NULL ) );
146 for (blocks = 0; blocks < 100; blocks++)
148 for (i = 0; i < (long)sizeof( buffer ); i++)
151 checksum[0] = checksum[0] + buffer[i];
153 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
154 bytes_written = bytes_written + sizeof( buffer );
157 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
160 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
162 memory_object = LocalAlloc( LPTR, bytes_written );
164 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
166 contents = LocalLock( memory_object );
168 filehandle = _lopen( filename, OF_READ );
170 contents = LocalLock( memory_object );
172 ok( NULL != contents, "LocalLock whines\n" );
174 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
180 checksum[0] = checksum[0] + contents[i];
183 while (i < bytes_written - 1);
185 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
187 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
189 ret = DeleteFileA( filename );
190 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
194 static void test__lclose( void )
199 filehandle = _lcreat( filename, 0 );
200 if (filehandle == HFILE_ERROR)
202 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
206 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
208 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
210 ret = DeleteFileA( filename );
211 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
215 static void test__lcreat( void )
219 WIN32_FIND_DATAA search_results;
220 char slashname[] = "testfi/";
225 filehandle = _lcreat( filename, 0 );
226 if (filehandle == HFILE_ERROR)
228 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
232 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
234 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
236 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
238 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
240 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
242 ret = DeleteFileA(filename);
243 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
245 filehandle = _lcreat( filename, 1 ); /* readonly */
246 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
248 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
250 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
252 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
254 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
256 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
258 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
260 filehandle = _lcreat( filename, 2 );
261 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
263 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
265 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
267 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
269 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
271 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
273 ret = DeleteFileA( filename );
274 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
276 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
277 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
279 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
281 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
283 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
285 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
287 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
289 ret = DeleteFileA( filename );
290 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
292 filehandle=_lcreat (slashname, 0); /* illegal name */
293 if (HFILE_ERROR==filehandle) {
295 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
296 "creating file \"%s\" failed with error %d\n", slashname, err);
297 } else { /* only NT succeeds */
299 find=FindFirstFileA (slashname, &search_results);
300 if (INVALID_HANDLE_VALUE!=find)
302 ret = FindClose (find);
303 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
304 slashname[strlen(slashname)-1]=0;
305 ok (!strcmp (slashname, search_results.cFileName),
306 "found unexpected name \"%s\"\n", search_results.cFileName);
307 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
308 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
309 search_results.dwFileAttributes);
311 ret = DeleteFileA( slashname );
312 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
315 filehandle=_lcreat (filename, 8); /* illegal attribute */
316 if (HFILE_ERROR==filehandle)
317 ok (0, "couldn't create volume label \"%s\"\n", filename);
320 find=FindFirstFileA (filename, &search_results);
321 if (INVALID_HANDLE_VALUE==find)
322 ok (0, "file \"%s\" not found\n", filename);
324 ret = FindClose(find);
325 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
326 ok (!strcmp (filename, search_results.cFileName),
327 "found unexpected name \"%s\"\n", search_results.cFileName);
328 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
329 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
330 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
331 search_results.dwFileAttributes);
333 ret = DeleteFileA( filename );
334 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
339 static void test__llseek( void )
347 filehandle = _lcreat( filename, 0 );
348 if (filehandle == HFILE_ERROR)
350 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
354 for (i = 0; i < 400; i++)
356 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
358 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
359 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
361 bytes_read = _hread( filehandle, buffer, 1);
362 ok( 1 == bytes_read, "file read size error\n" );
363 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
364 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
366 bytes_read = _hread( filehandle, buffer, 1);
367 ok( 1 == bytes_read, "file read size error\n" );
368 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
369 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
370 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
372 ret = DeleteFileA( filename );
373 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
377 static void test__llopen( void )
384 filehandle = _lcreat( filename, 0 );
385 if (filehandle == HFILE_ERROR)
387 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
391 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
392 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
394 filehandle = _lopen( filename, OF_READ );
395 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
396 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
397 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
398 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
400 filehandle = _lopen( filename, OF_READWRITE );
401 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
402 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
403 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
404 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
406 filehandle = _lopen( filename, OF_WRITE );
407 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
408 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
409 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
411 ret = DeleteFileA( filename );
412 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
413 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
417 static void test__lread( void )
426 filehandle = _lcreat( filename, 0 );
427 if (filehandle == HFILE_ERROR)
429 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
433 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
435 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
437 filehandle = _lopen( filename, OF_READ );
439 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
441 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
443 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
445 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
447 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
448 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
449 for (i = 0; i < bytes_wanted; i++)
451 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
455 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
457 ret = DeleteFileA( filename );
458 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
462 static void test__lwrite( void )
471 HLOCAL memory_object;
475 filehandle = _lcreat( filename, 0 );
476 if (filehandle == HFILE_ERROR)
478 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
482 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
484 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
486 filehandle = _lopen( filename, OF_READ );
488 bytes_read = _hread( filehandle, buffer, 1);
490 ok( 0 == bytes_read, "file read size error\n" );
492 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
494 filehandle = _lopen( filename, OF_READWRITE );
498 srand( (unsigned)time( NULL ) );
499 for (blocks = 0; blocks < 100; blocks++)
501 for (i = 0; i < (long)sizeof( buffer ); i++)
504 checksum[0] = checksum[0] + buffer[i];
506 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
507 bytes_written = bytes_written + sizeof( buffer );
510 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
513 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
515 memory_object = LocalAlloc( LPTR, bytes_written );
517 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
519 contents = LocalLock( memory_object );
521 filehandle = _lopen( filename, OF_READ );
523 contents = LocalLock( memory_object );
525 ok( NULL != contents, "LocalLock whines\n" );
527 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
533 checksum[0] += contents[i];
536 while (i < bytes_written - 1);
538 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
540 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
542 ret = DeleteFileA( filename );
543 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
546 static void test_CopyFileA(void)
548 char temp_path[MAX_PATH];
549 char source[MAX_PATH], dest[MAX_PATH];
550 static const char prefix[] = "pfx";
557 ret = GetTempPathA(MAX_PATH, temp_path);
558 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
559 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
561 ret = GetTempFileNameA(temp_path, prefix, 0, source);
562 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
564 /* make the source have not zero size */
565 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
566 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
567 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
568 ok( retok && ret == sizeof(prefix),
569 "WriteFile error %d\n", GetLastError());
570 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
571 /* get the file time and change it to prove the difference */
572 ret = GetFileTime(hfile, NULL, NULL, &ft1);
573 ok( ret, "GetFileTime error %d\n", GetLastError());
574 ft1.dwLowDateTime -= 600000000; /* 60 second */
575 ret = SetFileTime(hfile, NULL, NULL, &ft1);
576 ok( ret, "SetFileTime error %d\n", GetLastError());
577 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
580 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
581 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
583 SetLastError(0xdeadbeef);
584 ret = CopyFileA(source, dest, TRUE);
585 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
586 "CopyFileA: unexpected error %d\n", GetLastError());
588 ret = CopyFileA(source, dest, FALSE);
589 ok(ret, "CopyFileA: error %d\n", GetLastError());
591 /* make sure that destination has correct size */
592 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
593 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
594 ret = GetFileSize(hfile, NULL);
595 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
597 /* make sure that destination has the same filetime */
598 ret = GetFileTime(hfile, NULL, NULL, &ft2);
599 ok( ret, "GetFileTime error %d\n", GetLastError());
600 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
602 SetLastError(0xdeadbeef);
603 ret = CopyFileA(source, dest, FALSE);
604 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
605 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
607 /* make sure that destination still has correct size */
608 ret = GetFileSize(hfile, NULL);
609 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
610 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
611 ok( retok && ret == sizeof(prefix),
612 "ReadFile: error %d\n", GetLastError());
613 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
616 ret = DeleteFileA(source);
617 ok(ret, "DeleteFileA: error %d\n", GetLastError());
618 ret = DeleteFileA(dest);
619 ok(ret, "DeleteFileA: error %d\n", GetLastError());
622 static void test_CopyFileW(void)
624 WCHAR temp_path[MAX_PATH];
625 WCHAR source[MAX_PATH], dest[MAX_PATH];
626 static const WCHAR prefix[] = {'p','f','x',0};
629 ret = GetTempPathW(MAX_PATH, temp_path);
630 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
632 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
633 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
635 ret = GetTempFileNameW(temp_path, prefix, 0, source);
636 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
638 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
639 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
641 ret = CopyFileW(source, dest, TRUE);
642 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
643 "CopyFileW: unexpected error %d\n", GetLastError());
645 ret = CopyFileW(source, dest, FALSE);
646 ok(ret, "CopyFileW: error %d\n", GetLastError());
648 ret = DeleteFileW(source);
649 ok(ret, "DeleteFileW: error %d\n", GetLastError());
650 ret = DeleteFileW(dest);
651 ok(ret, "DeleteFileW: error %d\n", GetLastError());
654 static void test_CreateFileA(void)
657 char temp_path[MAX_PATH];
658 char filename[MAX_PATH];
659 static const char prefix[] = "pfx";
662 ret = GetTempPathA(MAX_PATH, temp_path);
663 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
664 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
666 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
667 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
669 SetLastError(0xdeadbeef);
670 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
671 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
672 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
673 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
675 SetLastError(0xdeadbeef);
676 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
677 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
678 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
679 "hFile %p, last error %u\n", hFile, GetLastError());
683 SetLastError(0xdeadbeef);
684 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
685 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
686 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
687 "hFile %p, last error %u\n", hFile, GetLastError());
691 ret = DeleteFileA(filename);
692 ok(ret, "DeleteFileA: error %d\n", GetLastError());
694 SetLastError(0xdeadbeef);
695 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
696 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
697 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
698 "hFile %p, last error %u\n", hFile, GetLastError());
702 ret = DeleteFileA(filename);
703 ok(ret, "DeleteFileA: error %d\n", GetLastError());
706 static void test_CreateFileW(void)
709 WCHAR temp_path[MAX_PATH];
710 WCHAR filename[MAX_PATH];
711 static const WCHAR emptyW[]={'\0'};
712 static const WCHAR prefix[] = {'p','f','x',0};
713 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
716 ret = GetTempPathW(MAX_PATH, temp_path);
717 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
719 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
720 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
722 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
723 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
725 SetLastError(0xdeadbeef);
726 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
727 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
728 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
729 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
731 SetLastError(0xdeadbeef);
732 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
733 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
734 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
735 "hFile %p, last error %u\n", hFile, GetLastError());
739 SetLastError(0xdeadbeef);
740 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
741 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
742 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
743 "hFile %p, last error %u\n", hFile, GetLastError());
747 ret = DeleteFileW(filename);
748 ok(ret, "DeleteFileW: error %d\n", GetLastError());
750 SetLastError(0xdeadbeef);
751 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
752 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
753 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
754 "hFile %p, last error %u\n", hFile, GetLastError());
758 ret = DeleteFileW(filename);
759 ok(ret, "DeleteFileW: error %d\n", GetLastError());
763 /* this crashes on NT4.0 */
764 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
765 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
766 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
767 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
770 hFile = CreateFileW(emptyW, 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(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
775 /* test the result of opening a nonexistent driver name */
776 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
777 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
778 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
779 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
782 static void test_GetTempFileNameA(void)
786 char expected[MAX_PATH + 10];
787 char windowsdir[MAX_PATH + 10];
788 char windowsdrive[3];
790 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
791 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
792 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
794 /* If the Windows directory is the root directory, it ends in backslash, not else. */
795 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
797 strcat(windowsdir, "\\");
800 windowsdrive[0] = windowsdir[0];
801 windowsdrive[1] = windowsdir[1];
802 windowsdrive[2] = '\0';
804 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
805 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
806 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
807 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
808 windowsdrive[0], out);
810 result = GetTempFileNameA(windowsdir, "abc", 2, out);
811 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
813 strcat(expected, windowsdir);
814 strcat(expected, "abc2.tmp");
815 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
819 static void test_DeleteFileA( void )
823 ret = DeleteFileA(NULL);
824 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
825 GetLastError() == ERROR_PATH_NOT_FOUND),
826 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
828 ret = DeleteFileA("");
829 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
830 GetLastError() == ERROR_BAD_PATHNAME),
831 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
833 ret = DeleteFileA("nul");
834 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
835 GetLastError() == ERROR_INVALID_PARAMETER ||
836 GetLastError() == ERROR_ACCESS_DENIED ||
837 GetLastError() == ERROR_INVALID_FUNCTION),
838 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
841 static void test_DeleteFileW( void )
844 static const WCHAR emptyW[]={'\0'};
846 ret = DeleteFileW(NULL);
847 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
849 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
850 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
852 ret = DeleteFileW(emptyW);
853 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
854 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
857 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
859 static void test_MoveFileA(void)
861 char tempdir[MAX_PATH];
862 char source[MAX_PATH], dest[MAX_PATH];
863 static const char prefix[] = "pfx";
866 ret = GetTempPathA(MAX_PATH, tempdir);
867 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
868 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
870 ret = GetTempFileNameA(tempdir, prefix, 0, source);
871 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
873 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
874 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
876 ret = MoveFileA(source, dest);
877 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
878 "MoveFileA: unexpected error %d\n", GetLastError());
880 ret = DeleteFileA(dest);
881 ok(ret, "DeleteFileA: error %d\n", GetLastError());
883 ret = MoveFileA(source, dest);
884 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
886 lstrcatA(tempdir, "Remove Me");
887 ret = CreateDirectoryA(tempdir, NULL);
888 ok(ret == TRUE, "CreateDirectoryA failed\n");
890 lstrcpyA(source, dest);
891 lstrcpyA(dest, tempdir);
892 lstrcatA(dest, "\\wild?.*");
893 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
894 ret = MoveFileA(source, dest);
895 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
896 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
897 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
898 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
899 if (ret || (GetLastError() != ERROR_INVALID_NAME))
902 char temppath[MAX_PATH];
905 lstrcpyA(temppath, tempdir);
906 lstrcatA(temppath, "\\*.*");
907 hFind = FindFirstFileA(temppath, &fd);
908 if (INVALID_HANDLE_VALUE != hFind)
913 lpName = fd.cAlternateFileName;
915 lpName = fd.cFileName;
916 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
918 while (FindNextFileA(hFind, &fd));
922 ret = DeleteFileA(source);
923 ok(ret, "DeleteFileA: error %d\n", GetLastError());
924 ret = DeleteFileA(dest);
925 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
926 ret = RemoveDirectoryA(tempdir);
927 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
930 static void test_MoveFileW(void)
932 WCHAR temp_path[MAX_PATH];
933 WCHAR source[MAX_PATH], dest[MAX_PATH];
934 static const WCHAR prefix[] = {'p','f','x',0};
937 ret = GetTempPathW(MAX_PATH, temp_path);
938 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
940 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
941 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
943 ret = GetTempFileNameW(temp_path, prefix, 0, source);
944 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
946 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
947 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
949 ret = MoveFileW(source, dest);
950 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
951 "CopyFileW: unexpected error %d\n", GetLastError());
953 ret = DeleteFileW(source);
954 ok(ret, "DeleteFileW: error %d\n", GetLastError());
955 ret = DeleteFileW(dest);
956 ok(ret, "DeleteFileW: error %d\n", GetLastError());
959 #define PATTERN_OFFSET 0x10
961 static void test_offset_in_overlapped_structure(void)
967 BYTE buf[256], pattern[] = "TeSt";
969 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
972 ret =GetTempPathA(MAX_PATH, temp_path);
973 ok( ret, "GetTempPathA error %d\n", GetLastError());
974 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
975 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
977 /*** Write File *****************************************************/
979 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
980 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
982 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
983 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
984 ok( ret, "WriteFile error %d\n", GetLastError());
985 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
987 memset(&ov, 0, sizeof(ov));
988 S(U(ov)).Offset = PATTERN_OFFSET;
989 S(U(ov)).OffsetHigh = 0;
990 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
991 /* Win 9x does not support the overlapped I/O on files */
992 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
993 ok(rc, "WriteFile error %d\n", GetLastError());
994 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
995 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
996 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
998 S(U(ov)).Offset = sizeof(buf) * 2;
999 S(U(ov)).OffsetHigh = 0;
1000 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1001 ok( ret, "WriteFile error %d\n", GetLastError());
1002 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1003 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1004 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1009 /*** Read File *****************************************************/
1011 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1012 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1014 memset(buf, 0, sizeof(buf));
1015 memset(&ov, 0, sizeof(ov));
1016 S(U(ov)).Offset = PATTERN_OFFSET;
1017 S(U(ov)).OffsetHigh = 0;
1018 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1019 /* Win 9x does not support the overlapped I/O on files */
1020 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1021 ok(rc, "ReadFile error %d\n", GetLastError());
1022 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1023 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1024 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1025 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1030 ret = DeleteFileA(temp_fname);
1031 ok( ret, "DeleteFileA error %d\n", GetLastError());
1034 static void test_LockFile(void)
1038 OVERLAPPED overlapped;
1039 int limited_LockFile;
1040 int limited_UnLockFile;
1042 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1043 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1044 CREATE_ALWAYS, 0, 0 );
1045 if (handle == INVALID_HANDLE_VALUE)
1047 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1050 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1052 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1053 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1055 limited_UnLockFile = 0;
1056 if (UnlockFile( handle, 0, 0, 0, 0 ))
1058 limited_UnLockFile = 1;
1061 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1062 /* overlapping locks must fail */
1063 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1064 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1065 /* non-overlapping locks must succeed */
1066 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1068 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1069 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1070 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1071 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1073 S(U(overlapped)).Offset = 100;
1074 S(U(overlapped)).OffsetHigh = 0;
1075 overlapped.hEvent = 0;
1077 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1078 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1080 /* LockFileEx is probably OK, test it more. */
1081 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1082 "LockFileEx 100,100 failed\n" );
1085 /* overlapping shared locks are OK */
1086 S(U(overlapped)).Offset = 150;
1087 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1089 /* but exclusive is not */
1090 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1091 0, 50, 0, &overlapped ),
1092 "LockFileEx exclusive 150,50 succeeded\n" );
1093 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1094 { /* UnLockFile is capable. */
1095 S(U(overlapped)).Offset = 100;
1096 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1097 "UnlockFileEx 150,100 again succeeded\n" );
1100 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1101 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1102 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1103 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1105 /* wrap-around lock should not do anything */
1106 /* (but still succeeds on NT4 so we don't check result) */
1107 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1109 limited_LockFile = 0;
1110 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1112 limited_LockFile = 1;
1115 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1117 /* zero-byte lock */
1118 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1119 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1120 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1121 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1123 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1124 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1126 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1128 CloseHandle( handle );
1129 DeleteFileA( filename );
1132 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1136 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1137 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1142 if (!access1) access1 = GENERIC_READ;
1145 if (!access2) access2 = GENERIC_READ;
1148 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1149 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1150 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1151 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1152 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1153 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1157 static void test_file_sharing(void)
1159 static const DWORD access_modes[] =
1160 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1161 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1162 static const DWORD sharing_modes[] =
1163 { 0, FILE_SHARE_READ,
1164 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1165 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1166 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1170 BOOL is_win9x = FALSE;
1172 /* make sure the file exists */
1173 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1174 if (h == INVALID_HANDLE_VALUE)
1176 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1179 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1182 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1184 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1186 /* Win9x doesn't support FILE_SHARE_DELETE */
1187 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1190 SetLastError(0xdeadbeef);
1191 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1192 NULL, OPEN_EXISTING, 0, 0 );
1193 if (h == INVALID_HANDLE_VALUE)
1195 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1198 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1200 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1202 /* Win9x doesn't support FILE_SHARE_DELETE */
1203 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1206 SetLastError(0xdeadbeef);
1207 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1208 NULL, OPEN_EXISTING, 0, 0 );
1210 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1211 access_modes[a2], sharing_modes[s2], is_win9x ))
1213 ret = GetLastError();
1215 ok( h2 != INVALID_HANDLE_VALUE,
1216 "open failed for modes %x/%x/%x/%x\n",
1217 access_modes[a1], sharing_modes[s1],
1218 access_modes[a2], sharing_modes[s2] );
1219 ok( ret == 0xdeadbeef /* Win9x */ ||
1221 "wrong error code %d\n", ret );
1227 ret = GetLastError();
1229 ok( h2 == INVALID_HANDLE_VALUE,
1230 "open succeeded for modes %x/%x/%x/%x\n",
1231 access_modes[a1], sharing_modes[s1],
1232 access_modes[a2], sharing_modes[s2] );
1233 ok( ret == ERROR_SHARING_VIOLATION,
1234 "wrong error code %d\n", ret );
1242 SetLastError(0xdeadbeef);
1243 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1244 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1246 SetLastError(0xdeadbeef);
1247 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1248 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1249 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1251 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1252 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1257 DeleteFileA( filename );
1260 static char get_windows_drive(void)
1262 char windowsdir[MAX_PATH];
1263 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1264 return windowsdir[0];
1267 static void test_FindFirstFileA(void)
1270 WIN32_FIND_DATAA data;
1272 char buffer[5] = "C:\\";
1275 /* try FindFirstFileA on "C:\" */
1276 buffer[0] = get_windows_drive();
1278 SetLastError( 0xdeadbeaf );
1279 handle = FindFirstFileA(buffer, &data);
1280 err = GetLastError();
1281 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1282 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1284 /* try FindFirstFileA on "C:\*" */
1285 strcpy(buffer2, buffer);
1286 strcat(buffer2, "*");
1287 handle = FindFirstFileA(buffer2, &data);
1288 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1289 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1290 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1291 if (FindNextFileA( handle, &data ))
1292 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1293 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1294 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1296 /* try FindFirstFileA on windows dir */
1297 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1298 strcat(buffer2, "\\*");
1299 handle = FindFirstFileA(buffer2, &data);
1300 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1301 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1302 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1303 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1304 while (FindNextFileA( handle, &data ))
1305 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1306 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1307 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1309 /* try FindFirstFileA on "C:\foo\" */
1310 SetLastError( 0xdeadbeaf );
1311 strcpy(buffer2, buffer);
1312 strcat(buffer2, "foo\\");
1313 handle = FindFirstFileA(buffer2, &data);
1314 err = GetLastError();
1315 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1317 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1320 /* try FindFirstFileA on "C:\foo\bar.txt" */
1321 SetLastError( 0xdeadbeaf );
1322 strcpy(buffer2, buffer);
1323 strcat(buffer2, "foo\\bar.txt");
1324 handle = FindFirstFileA(buffer2, &data);
1325 err = GetLastError();
1326 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1327 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1329 /* try FindFirstFileA on "C:\foo\*.*" */
1330 SetLastError( 0xdeadbeaf );
1331 strcpy(buffer2, buffer);
1332 strcat(buffer2, "foo\\*.*");
1333 handle = FindFirstFileA(buffer2, &data);
1334 err = GetLastError();
1335 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1336 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1338 /* try FindFirstFileA on "foo\bar.txt" */
1339 SetLastError( 0xdeadbeaf );
1340 strcpy(buffer2, "foo\\bar.txt");
1341 handle = FindFirstFileA(buffer2, &data);
1342 err = GetLastError();
1343 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1344 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1346 /* try FindFirstFileA on "c:\nul" */
1347 SetLastError( 0xdeadbeaf );
1348 strcpy(buffer2, buffer);
1349 strcat(buffer2, "nul");
1350 handle = FindFirstFileA(buffer2, &data);
1351 err = GetLastError();
1352 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1353 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1354 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1355 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1356 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1357 SetLastError( 0xdeadbeaf );
1358 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1359 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1360 ok( FindClose( handle ), "failed to close handle\n" );
1362 /* try FindFirstFileA on "lpt1" */
1363 SetLastError( 0xdeadbeaf );
1364 strcpy(buffer2, "lpt1");
1365 handle = FindFirstFileA(buffer2, &data);
1366 err = GetLastError();
1367 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1368 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1369 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1370 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1371 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1372 SetLastError( 0xdeadbeaf );
1373 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1374 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1375 ok( FindClose( handle ), "failed to close handle\n" );
1377 /* try FindFirstFileA on "c:\nul\*" */
1378 SetLastError( 0xdeadbeaf );
1379 strcpy(buffer2, buffer);
1380 strcat(buffer2, "nul\\*");
1381 handle = FindFirstFileA(buffer2, &data);
1382 err = GetLastError();
1383 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1384 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1386 /* try FindFirstFileA on "c:\nul*" */
1387 SetLastError( 0xdeadbeaf );
1388 strcpy(buffer2, buffer);
1389 strcat(buffer2, "nul*");
1390 handle = FindFirstFileA(buffer2, &data);
1391 err = GetLastError();
1392 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1393 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1395 /* try FindFirstFileA on "c:\foo\bar\nul" */
1396 SetLastError( 0xdeadbeaf );
1397 strcpy(buffer2, buffer);
1398 strcat(buffer2, "foo\\bar\\nul");
1399 handle = FindFirstFileA(buffer2, &data);
1400 err = GetLastError();
1401 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1402 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1404 /* try FindFirstFileA on "c:\foo\nul\bar" */
1405 SetLastError( 0xdeadbeaf );
1406 strcpy(buffer2, buffer);
1407 strcat(buffer2, "foo\\nul\\bar");
1408 handle = FindFirstFileA(buffer2, &data);
1409 err = GetLastError();
1410 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1411 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1414 static void test_FindNextFileA(void)
1417 WIN32_FIND_DATAA search_results;
1419 char buffer[5] = "C:\\*";
1421 buffer[0] = get_windows_drive();
1422 handle = FindFirstFileA(buffer,&search_results);
1423 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1424 while (FindNextFile(handle, &search_results))
1426 /* get to the end of the files */
1428 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1429 err = GetLastError();
1430 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1433 static void test_FindFirstFileExA(void)
1435 WIN32_FIND_DATAA search_results;
1438 if (!pFindFirstFileExA)
1440 skip("FindFirstFileExA() is missing\n");
1444 CreateDirectoryA("test-dir", NULL);
1445 _lclose(_lcreat("test-dir\\file1", 0));
1446 _lclose(_lcreat("test-dir\\file2", 0));
1447 CreateDirectoryA("test-dir\\dir1", NULL);
1448 /* FindExLimitToDirectories is ignored */
1449 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, FindExSearchLimitToDirectories, NULL, 0);
1450 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1451 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1453 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1455 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1456 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1458 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1459 ok(CHECK_NAME(search_results.cFileName), "Invalid thrid entry - %s\n", search_results.cFileName);
1461 ok(FindNextFile(handle, &search_results), "Fetching fourth file failed\n");
1462 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1464 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1465 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1469 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should failed\n");
1470 DeleteFileA("test-dir\\file1");
1471 DeleteFileA("test-dir\\file2");
1472 RemoveDirectoryA("test-dir\\dir1");
1473 RemoveDirectoryA("test-dir");
1476 static int test_Mapfile_createtemp(HANDLE *handle)
1478 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1479 DeleteFile(filename);
1480 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1481 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1482 if (*handle != INVALID_HANDLE_VALUE) {
1490 static void test_MapFile(void)
1495 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1497 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1498 ok( hmap != NULL, "mapping should work, I named it!\n" );
1500 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1502 /* We have to close file before we try new stuff with mapping again.
1503 Else we would always succeed on XP or block descriptors on 95. */
1504 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1505 ok( hmap != NULL, "We should still be able to map!\n" );
1506 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1507 ok( CloseHandle( handle ), "can't close file handle\n");
1510 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1512 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1513 ok( hmap == NULL, "mapped zero size file\n");
1514 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1516 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1517 ok( hmap == NULL, "mapping should fail\n");
1518 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1520 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1521 ok( hmap == NULL, "mapping should fail\n");
1522 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1524 /* On XP you can now map again, on Win 95 you cannot. */
1526 ok( CloseHandle( handle ), "can't close file handle\n");
1527 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1530 static void test_GetFileType(void)
1533 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1534 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1535 type = GetFileType(h);
1536 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1538 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1539 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1540 type = GetFileType(h);
1541 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1543 DeleteFileA( filename );
1546 static int completion_count;
1548 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1550 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1551 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1555 static void test_async_file_errors(void)
1557 char szFile[MAX_PATH];
1558 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1560 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1562 S(U(ovl)).Offset = 0;
1563 S(U(ovl)).OffsetHigh = 0;
1565 completion_count = 0;
1567 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1568 strcat(szFile, "\\win.ini");
1569 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1570 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1574 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1576 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1577 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1580 S(U(ovl)).Offset += 4096;
1581 /* i/o completion routine only called if ReadFileEx returned success.
1582 * we only care about violations of this rule so undo what should have
1586 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1587 /*printf("Error = %ld\n", GetLastError());*/
1590 static void test_read_write(void)
1594 char temp_path[MAX_PATH];
1595 char filename[MAX_PATH];
1596 static const char prefix[] = "pfx";
1598 ret = GetTempPathA(MAX_PATH, temp_path);
1599 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1600 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1602 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1603 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1605 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1606 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1607 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1609 SetLastError(12345678);
1611 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1612 ok(ret && GetLastError() == 12345678,
1613 "ret = %d, error %d\n", ret, GetLastError());
1614 ok(!bytes, "bytes = %d\n", bytes);
1616 SetLastError(12345678);
1618 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1619 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1620 (ret && GetLastError() == 12345678), /* Win9x */
1621 "ret = %d, error %d\n", ret, GetLastError());
1622 ok(!bytes || /* Win2k */
1623 bytes == 10, /* Win9x */
1624 "bytes = %d\n", bytes);
1626 /* make sure the file contains data */
1627 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1628 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1630 SetLastError(12345678);
1632 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1633 ok(ret && GetLastError() == 12345678,
1634 "ret = %d, error %d\n", ret, GetLastError());
1635 ok(!bytes, "bytes = %d\n", bytes);
1637 SetLastError(12345678);
1639 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1640 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1641 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1642 "ret = %d, error %d\n", ret, GetLastError());
1643 ok(!bytes, "bytes = %d\n", bytes);
1645 ret = CloseHandle(hFile);
1646 ok( ret, "CloseHandle: error %d\n", GetLastError());
1647 ret = DeleteFileA(filename);
1648 ok( ret, "DeleteFileA: error %d\n", GetLastError());
1651 static void test_OpenFile(void)
1658 static const char *file = "\\regsvr32.exe";
1659 static const char *foo = ".\\foo-bar-foo.baz";
1660 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1661 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1662 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1663 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1664 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1665 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1666 static const char *backslash = "\\";
1667 char buff[MAX_PATH];
1668 char buff_long[4*MAX_PATH];
1669 char filled_0xA5[OFS_MAXPATHNAME];
1672 /* Check for existing file */
1673 length = GetSystemDirectoryA(buff, MAX_PATH);
1675 if (length + lstrlen(file) < MAX_PATH)
1677 lstrcatA(buff, file);
1678 memset(&ofs, 0xA5, sizeof(ofs));
1679 SetLastError(0xfaceabee);
1681 hFile = OpenFile(buff, &ofs, OF_EXIST);
1682 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1683 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1684 "GetLastError() returns %d\n", GetLastError() );
1685 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1686 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1687 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1688 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1689 ofs.szPathName, buff );
1692 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1693 length = GetCurrentDirectoryA(MAX_PATH, buff);
1695 /* Check for nonexistent file */
1696 if (length + lstrlenA(foo + 1) < MAX_PATH)
1698 lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1699 memset(&ofs, 0xA5, sizeof(ofs));
1700 SetLastError(0xfaceabee);
1702 hFile = OpenFile(foo, &ofs, OF_EXIST);
1703 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1704 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1706 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1707 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1708 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1709 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1710 ofs.szPathName, buff );
1713 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1714 length += lstrlenA(foo_too_long + 1);
1716 /* Check for nonexistent file with too long filename */
1717 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
1719 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1720 memset(&ofs, 0xA5, sizeof(ofs));
1721 SetLastError(0xfaceabee);
1723 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1724 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1725 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
1726 "GetLastError() returns %d\n", GetLastError() );
1728 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1729 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1730 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1731 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1732 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1736 length = GetCurrentDirectoryA(MAX_PATH, buff);
1737 length += lstrlenA(backslash);
1738 length += lstrlenA(filename);
1740 if (length >= MAX_PATH)
1742 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
1745 lstrcatA(buff, backslash);
1746 lstrcatA(buff, filename);
1748 memset(&ofs, 0xA5, sizeof(ofs));
1749 SetLastError(0xfaceabee);
1750 /* Create an empty file */
1751 hFile = OpenFile(filename, &ofs, OF_CREATE);
1752 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1753 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1754 "GetLastError() returns %d\n", GetLastError() );
1755 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1756 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1757 ret = CloseHandle((HANDLE)hFile);
1758 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1759 retval = GetFileAttributesA(filename);
1760 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1762 memset(&ofs, 0xA5, sizeof(ofs));
1763 SetLastError(0xfaceabee);
1764 /* Check various opening options: */
1765 /* for reading only, */
1766 hFile = OpenFile(filename, &ofs, OF_READ);
1767 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
1768 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1769 "GetLastError() returns %d\n", GetLastError() );
1770 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1771 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1772 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1773 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1774 ret = CloseHandle((HANDLE)hFile);
1775 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1777 memset(&ofs, 0xA5, sizeof(ofs));
1778 SetLastError(0xfaceabee);
1779 /* for writing only, */
1780 hFile = OpenFile(filename, &ofs, OF_WRITE);
1781 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
1782 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1783 "GetLastError() returns %d\n", GetLastError() );
1784 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1785 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1786 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1787 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1788 ret = CloseHandle((HANDLE)hFile);
1789 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1791 memset(&ofs, 0xA5, sizeof(ofs));
1792 SetLastError(0xfaceabee);
1793 /* for reading and writing, */
1794 hFile = OpenFile(filename, &ofs, OF_READWRITE);
1795 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1796 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1797 "GetLastError() returns %d\n", GetLastError() );
1798 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1799 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1800 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1801 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1802 ret = CloseHandle((HANDLE)hFile);
1803 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1805 memset(&ofs, 0xA5, sizeof(ofs));
1806 SetLastError(0xfaceabee);
1807 /* for checking file presence. */
1808 hFile = OpenFile(filename, &ofs, OF_EXIST);
1809 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1810 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1811 "GetLastError() returns %d\n", GetLastError() );
1812 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1813 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1814 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1815 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1817 memset(&ofs, 0xA5, sizeof(ofs));
1818 SetLastError(0xfaceabee);
1819 /* Delete the file and make sure it doesn't exist anymore */
1820 hFile = OpenFile(filename, &ofs, OF_DELETE);
1821 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1822 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1823 "GetLastError() returns %d\n", GetLastError() );
1824 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1825 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1826 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1827 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1829 retval = GetFileAttributesA(filename);
1830 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1833 static void test_overlapped(void)
1838 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1840 memset( &ov, 0, sizeof ov );
1842 r = GetOverlappedResult(0, &ov, &result, 0);
1843 ok( r == TRUE, "should return false\n");
1844 ok( result == 0, "wrong result %u\n", result );
1848 ov.InternalHigh = 0xabcd;
1849 r = GetOverlappedResult(0, &ov, &result, 0);
1850 ok( r == TRUE, "should return false\n");
1851 ok( result == 0xabcd, "wrong result %u\n", result );
1853 SetLastError( 0xb00 );
1855 ov.Internal = STATUS_INVALID_HANDLE;
1856 ov.InternalHigh = 0xabcd;
1857 r = GetOverlappedResult(0, &ov, &result, 0);
1858 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1859 ok( r == FALSE, "should return false\n");
1860 ok( result == 0xabcd, "wrong result %u\n", result );
1862 SetLastError( 0xb00 );
1864 ov.Internal = STATUS_PENDING;
1865 ov.InternalHigh = 0xabcd;
1866 r = GetOverlappedResult(0, &ov, &result, 0);
1867 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1868 ok( r == FALSE, "should return false\n");
1869 ok( result == 0, "wrong result %u\n", result );
1871 SetLastError( 0xb00 );
1872 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
1873 ov.Internal = STATUS_PENDING;
1874 ov.InternalHigh = 0xabcd;
1875 r = GetOverlappedResult(0, &ov, &result, 0);
1876 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1877 ok( r == FALSE, "should return false\n");
1879 ResetEvent( ov.hEvent );
1881 SetLastError( 0xb00 );
1882 ov.Internal = STATUS_PENDING;
1883 ov.InternalHigh = 0;
1884 r = GetOverlappedResult(0, &ov, &result, 0);
1885 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1886 ok( r == FALSE, "should return false\n");
1888 r = CloseHandle( ov.hEvent );
1889 ok( r == TRUE, "close handle failed\n");
1892 static void test_RemoveDirectory(void)
1895 char directory[] = "removeme";
1897 rc = CreateDirectory(directory, NULL);
1898 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
1900 rc = SetCurrentDirectory(directory);
1901 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1903 rc = RemoveDirectory(".");
1905 ok( !rc, "RemoveDirectory unexpectedly worked\n" );
1908 rc = SetCurrentDirectory("..");
1909 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1911 rc = RemoveDirectory(directory);
1913 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1917 static void test_ReplaceFileA(void)
1919 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
1920 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
1921 static const char replacedData[] = "file-to-replace";
1922 static const char replacementData[] = "new-file";
1923 static const char backupData[] = "backup-file";
1924 FILETIME ftReplaced, ftReplacement, ftBackup;
1925 static const char prefix[] = "pfx";
1926 char temp_path[MAX_PATH];
1932 skip("ReplaceFileA() is missing\n");
1936 ret = GetTempPathA(MAX_PATH, temp_path);
1937 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1938 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1940 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
1941 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
1943 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
1944 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
1946 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
1947 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
1949 /* place predictable data in the file to be replaced */
1950 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1951 ok(hReplacedFile != INVALID_HANDLE_VALUE,
1952 "failed to open replaced file\n");
1953 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
1954 ok( retok && ret == sizeof(replacedData),
1955 "WriteFile error (replaced) %d\n", GetLastError());
1956 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
1957 "replaced file has wrong size\n");
1958 /* place predictable data in the file to be the replacement */
1959 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1960 ok(hReplacementFile != INVALID_HANDLE_VALUE,
1961 "failed to open replacement file\n");
1962 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
1963 ok( retok && ret == sizeof(replacementData),
1964 "WriteFile error (replacement) %d\n", GetLastError());
1965 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
1966 "replacement file has wrong size\n");
1967 /* place predictable data in the backup file (to be over-written) */
1968 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1969 ok(hBackupFile != INVALID_HANDLE_VALUE,
1970 "failed to open backup file\n");
1971 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
1972 ok( retok && ret == sizeof(backupData),
1973 "WriteFile error (replacement) %d\n", GetLastError());
1974 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
1975 "backup file has wrong size\n");
1976 /* change the filetime on the "replaced" file to ensure that it changes */
1977 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
1978 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
1979 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
1980 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
1981 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
1982 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
1983 CloseHandle(hReplacedFile);
1984 /* change the filetime on the backup to ensure that it changes */
1985 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
1986 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
1987 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
1988 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
1989 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
1990 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
1991 CloseHandle(hBackupFile);
1992 /* get the filetime on the replacement file to perform checks */
1993 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
1994 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
1995 CloseHandle(hReplacementFile);
1997 /* perform replacement w/ backup
1998 * TODO: flags are not implemented
2000 SetLastError(0xdeadbeef);
2001 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2002 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2003 /* make sure that the backup has the size of the old "replaced" file */
2004 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2005 ok(hBackupFile != INVALID_HANDLE_VALUE,
2006 "failed to open backup file\n");
2007 ret = GetFileSize(hBackupFile, NULL);
2008 ok(ret == sizeof(replacedData),
2009 "backup file has wrong size %d\n", ret);
2010 /* make sure that the "replaced" file has the size of the replacement file */
2011 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2012 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2013 "failed to open replaced file\n");
2014 ret = GetFileSize(hReplacedFile, NULL);
2015 ok(ret == sizeof(replacementData),
2016 "replaced file has wrong size %d\n", ret);
2017 /* make sure that the replacement file no-longer exists */
2018 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2019 ok(hReplacementFile == INVALID_HANDLE_VALUE,
2020 "unexpected error, replacement file should not exist %d\n", GetLastError());
2021 /* make sure that the backup has the old "replaced" filetime */
2022 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2023 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2024 ok(CompareFileTime(&ftBackup, &ftReplaced) == 0,
2025 "backup file has wrong filetime\n");
2026 CloseHandle(hBackupFile);
2027 /* make sure that the "replaced" has the old replacement filetime */
2028 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2029 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2030 ok(CompareFileTime(&ftReplaced, &ftReplacement) == 0,
2031 "replaced file has wrong filetime\n");
2032 CloseHandle(hReplacedFile);
2034 /* re-create replacement file for pass w/o backup (blank) */
2035 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2036 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2037 /* perform replacement w/o backup
2038 * TODO: flags are not implemented
2040 SetLastError(0xdeadbeef);
2041 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2042 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2044 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2045 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2046 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2047 ret = DeleteFileA(backup);
2048 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2049 /* perform replacement w/ backup (no pre-existing backup)
2050 * TODO: flags are not implemented
2052 SetLastError(0xdeadbeef);
2053 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2054 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2056 /* re-create replacement file for pass w/ no permissions to "replaced" */
2057 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2058 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2059 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2060 ok(ret, "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2061 /* perform replacement w/ backup (no permission to "replaced")
2062 * TODO: flags are not implemented
2064 SetLastError(0xdeadbeef);
2065 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2066 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2067 /* make sure that the replacement file still exists */
2068 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2069 ok(hReplacementFile != INVALID_HANDLE_VALUE,
2070 "unexpected error, replacement file should still exist %d\n", GetLastError());
2071 CloseHandle(hReplacementFile);
2072 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2073 ok(ret, "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2075 /* replacement file still exists, make pass w/o "replaced" */
2076 ret = DeleteFileA(replaced);
2077 ok(ret, "DeleteFileA: error (replaced) %d\n", GetLastError());
2078 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2079 * TODO: flags are not implemented
2081 SetLastError(0xdeadbeef);
2082 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2083 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2084 "ReplaceFileA: unexpected error %d\n", GetLastError());
2086 /* perform replacement w/o existing "replacement" file
2087 * TODO: flags are not implemented
2089 SetLastError(0xdeadbeef);
2090 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2091 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2092 "ReplaceFileA: unexpected error %d\n", GetLastError());
2095 * if the first round (w/ backup) worked then as long as there is no
2096 * failure then there is no need to check this round (w/ backup is the
2097 * more complete case)
2100 /* delete temporary files, replacement and replaced are already deleted */
2101 ret = DeleteFileA(backup);
2102 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2106 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2107 * need to be as thorough.
2109 static void test_ReplaceFileW(void)
2111 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2112 static const WCHAR prefix[] = {'p','f','x',0};
2113 WCHAR temp_path[MAX_PATH];
2118 skip("ReplaceFileW() is missing\n");
2122 ret = GetTempPathW(MAX_PATH, temp_path);
2123 if (ret==0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2125 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2126 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2128 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
2129 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2131 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2132 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2134 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
2135 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2137 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2138 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2140 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2141 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2142 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2143 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2145 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2146 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2147 ret = DeleteFileW(backup);
2148 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
2149 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2150 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2152 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2153 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2154 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
2155 ok(ret, "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2157 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2158 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
2159 "ReplaceFileW: unexpected error %d\n", GetLastError());
2160 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
2161 ok(ret, "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2163 ret = DeleteFileW(replaced);
2164 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
2165 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2166 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
2168 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2169 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2170 "ReplaceFileW: unexpected error %d\n", GetLastError());
2172 ret = DeleteFileW(backup);
2173 ok(ret, "DeleteFileW: error %d\n", GetLastError());
2178 InitFunctionPointers();
2188 test_GetTempFileNameA();
2197 test_FindFirstFileA();
2198 test_FindNextFileA();
2199 test_FindFirstFileExA();
2201 test_file_sharing();
2202 test_offset_in_overlapped_structure();
2205 test_async_file_errors();
2209 test_RemoveDirectory();
2210 test_ReplaceFileA();
2211 test_ReplaceFileW();