2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 * Copyright (c) 2008 Jeff Zaroyko
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* ReplaceFile requires Windows 2000 or newer */
24 #define _WIN32_WINNT 0x0500
31 #include "wine/test.h"
36 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
37 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
38 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
39 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
40 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
42 /* keep filename and filenameW the same */
43 static const char filename[] = "testfile.xxx";
44 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
45 static const char sillytext[] =
46 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
53 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
54 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
55 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
58 const char *file; /* file string to test */
59 const DWORD err; /* Win NT and further error code */
60 const LONG err2; /* Win 9x & ME error code or -1 */
61 const DWORD options; /* option flag to use for open */
62 const BOOL todo_flag; /* todo_wine indicator */
65 static void InitFunctionPointers(void)
67 HMODULE hkernel32 = GetModuleHandleA("kernel32");
69 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
70 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
71 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
72 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
73 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
76 static void test__hread( void )
85 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
86 DeleteFileA( filename );
87 filehandle = _lcreat( filename, 0 );
88 if (filehandle == HFILE_ERROR)
90 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
94 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
96 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
98 filehandle = _lopen( filename, OF_READ );
100 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
102 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
104 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
106 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
108 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
109 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
110 for (i = 0; i < bytes_wanted; i++)
112 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
116 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
118 ret = DeleteFileA( filename );
119 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
123 static void test__hwrite( void )
132 HLOCAL memory_object;
136 filehandle = _lcreat( filename, 0 );
137 if (filehandle == HFILE_ERROR)
139 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
143 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
145 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
147 filehandle = _lopen( filename, OF_READ );
149 bytes_read = _hread( filehandle, buffer, 1);
151 ok( 0 == bytes_read, "file read size error\n" );
153 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
155 filehandle = _lopen( filename, OF_READWRITE );
159 srand( (unsigned)time( NULL ) );
160 for (blocks = 0; blocks < 100; blocks++)
162 for (i = 0; i < (LONG)sizeof( buffer ); i++)
165 checksum[0] = checksum[0] + buffer[i];
167 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
168 bytes_written = bytes_written + sizeof( buffer );
171 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
174 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
176 memory_object = LocalAlloc( LPTR, bytes_written );
178 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
180 contents = LocalLock( memory_object );
182 filehandle = _lopen( filename, OF_READ );
184 contents = LocalLock( memory_object );
186 ok( NULL != contents, "LocalLock whines\n" );
188 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
194 checksum[0] = checksum[0] + contents[i];
197 while (i < bytes_written - 1);
199 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
201 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
203 ret = DeleteFileA( filename );
204 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
208 static void test__lclose( void )
213 filehandle = _lcreat( filename, 0 );
214 if (filehandle == HFILE_ERROR)
216 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
220 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
222 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
224 ret = DeleteFileA( filename );
225 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
229 static void test__lcreat( void )
233 WIN32_FIND_DATAA search_results;
234 char slashname[] = "testfi/";
239 filehandle = _lcreat( filename, 0 );
240 if (filehandle == HFILE_ERROR)
242 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
246 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
248 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
250 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
252 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
254 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
256 ret = DeleteFileA(filename);
257 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
259 filehandle = _lcreat( filename, 1 ); /* readonly */
260 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
262 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
264 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
266 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
268 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
270 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
272 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
274 filehandle = _lcreat( filename, 2 );
275 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
277 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
279 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
281 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
283 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
285 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
287 ret = DeleteFileA( filename );
288 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
290 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
291 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
293 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
295 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
297 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
299 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
301 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
303 ret = DeleteFileA( filename );
304 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
306 filehandle=_lcreat (slashname, 0); /* illegal name */
307 if (HFILE_ERROR==filehandle) {
309 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
310 "creating file \"%s\" failed with error %d\n", slashname, err);
311 } else { /* only NT succeeds */
313 find=FindFirstFileA (slashname, &search_results);
314 if (INVALID_HANDLE_VALUE!=find)
316 ret = FindClose (find);
317 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
318 slashname[strlen(slashname)-1]=0;
319 ok (!strcmp (slashname, search_results.cFileName),
320 "found unexpected name \"%s\"\n", search_results.cFileName);
321 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
322 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
323 search_results.dwFileAttributes);
325 ret = DeleteFileA( slashname );
326 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
329 filehandle=_lcreat (filename, 8); /* illegal attribute */
330 if (HFILE_ERROR==filehandle)
331 ok (0, "couldn't create volume label \"%s\"\n", filename);
334 find=FindFirstFileA (filename, &search_results);
335 if (INVALID_HANDLE_VALUE==find)
336 ok (0, "file \"%s\" not found\n", filename);
338 ret = FindClose(find);
339 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
340 ok (!strcmp (filename, search_results.cFileName),
341 "found unexpected name \"%s\"\n", search_results.cFileName);
342 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
343 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
344 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
345 search_results.dwFileAttributes);
347 ret = DeleteFileA( filename );
348 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
353 static void test__llseek( void )
361 filehandle = _lcreat( filename, 0 );
362 if (filehandle == HFILE_ERROR)
364 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
368 for (i = 0; i < 400; i++)
370 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
372 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
373 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
375 bytes_read = _hread( filehandle, buffer, 1);
376 ok( 1 == bytes_read, "file read size error\n" );
377 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
378 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
380 bytes_read = _hread( filehandle, buffer, 1);
381 ok( 1 == bytes_read, "file read size error\n" );
382 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
383 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
384 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
386 ret = DeleteFileA( filename );
387 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
391 static void test__llopen( void )
398 filehandle = _lcreat( filename, 0 );
399 if (filehandle == HFILE_ERROR)
401 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
405 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
406 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
408 filehandle = _lopen( filename, OF_READ );
409 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
410 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
411 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
412 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
414 filehandle = _lopen( filename, OF_READWRITE );
415 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
416 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
417 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
418 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
420 filehandle = _lopen( filename, OF_WRITE );
421 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
422 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
423 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
425 ret = DeleteFileA( filename );
426 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
427 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
431 static void test__lread( void )
440 filehandle = _lcreat( filename, 0 );
441 if (filehandle == HFILE_ERROR)
443 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
447 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
449 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
451 filehandle = _lopen( filename, OF_READ );
453 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
455 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
457 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
459 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
461 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
462 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
463 for (i = 0; i < bytes_wanted; i++)
465 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
469 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
471 ret = DeleteFileA( filename );
472 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
476 static void test__lwrite( void )
485 HLOCAL memory_object;
489 filehandle = _lcreat( filename, 0 );
490 if (filehandle == HFILE_ERROR)
492 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
496 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
498 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
500 filehandle = _lopen( filename, OF_READ );
502 bytes_read = _hread( filehandle, buffer, 1);
504 ok( 0 == bytes_read, "file read size error\n" );
506 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
508 filehandle = _lopen( filename, OF_READWRITE );
512 srand( (unsigned)time( NULL ) );
513 for (blocks = 0; blocks < 100; blocks++)
515 for (i = 0; i < (INT)sizeof( buffer ); i++)
518 checksum[0] = checksum[0] + buffer[i];
520 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
521 bytes_written = bytes_written + sizeof( buffer );
524 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
527 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
529 memory_object = LocalAlloc( LPTR, bytes_written );
531 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
533 contents = LocalLock( memory_object );
535 filehandle = _lopen( filename, OF_READ );
537 contents = LocalLock( memory_object );
539 ok( NULL != contents, "LocalLock whines\n" );
541 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
547 checksum[0] += contents[i];
550 while (i < bytes_written - 1);
552 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
554 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
556 ret = DeleteFileA( filename );
557 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
560 static void test_CopyFileA(void)
562 char temp_path[MAX_PATH];
563 char source[MAX_PATH], dest[MAX_PATH];
564 static const char prefix[] = "pfx";
572 ret = GetTempPathA(MAX_PATH, temp_path);
573 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
574 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
576 ret = GetTempFileNameA(temp_path, prefix, 0, source);
577 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
579 ret = MoveFileA(source, source);
580 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
582 /* make the source have not zero size */
583 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
584 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
585 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
586 ok( retok && ret == sizeof(prefix),
587 "WriteFile error %d\n", GetLastError());
588 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
589 /* get the file time and change it to prove the difference */
590 ret = GetFileTime(hfile, NULL, NULL, &ft1);
591 ok( ret, "GetFileTime error %d\n", GetLastError());
592 ft1.dwLowDateTime -= 600000000; /* 60 second */
593 ret = SetFileTime(hfile, NULL, NULL, &ft1);
594 ok( ret, "SetFileTime error %d\n", GetLastError());
595 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
598 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
599 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
601 SetLastError(0xdeadbeef);
602 ret = CopyFileA(source, dest, TRUE);
603 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
604 "CopyFileA: unexpected error %d\n", GetLastError());
606 ret = CopyFileA(source, dest, FALSE);
607 ok(ret, "CopyFileA: error %d\n", GetLastError());
609 /* make sure that destination has correct size */
610 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
611 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
612 ret = GetFileSize(hfile, NULL);
613 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
615 /* make sure that destination has the same filetime */
616 ret = GetFileTime(hfile, NULL, NULL, &ft2);
617 ok( ret, "GetFileTime error %d\n", GetLastError());
618 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
620 SetLastError(0xdeadbeef);
621 ret = CopyFileA(source, dest, FALSE);
622 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
623 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
625 /* make sure that destination still has correct size */
626 ret = GetFileSize(hfile, NULL);
627 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
628 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
629 ok( retok && ret == sizeof(prefix),
630 "ReadFile: error %d\n", GetLastError());
631 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
633 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
634 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
635 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
637 ret = CopyFileA(source, dest, FALSE);
638 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
639 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
641 CloseHandle(hmapfile);
644 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
645 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
647 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
648 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
649 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
651 ret = CopyFileA(source, dest, FALSE);
653 ok(!ret && GetLastError() == ERROR_USER_MAPPED_FILE,
654 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
657 CloseHandle(hmapfile);
660 ret = DeleteFileA(source);
661 ok(ret, "DeleteFileA: error %d\n", GetLastError());
662 ret = DeleteFileA(dest);
663 ok(ret, "DeleteFileA: error %d\n", GetLastError());
666 static void test_CopyFileW(void)
668 WCHAR temp_path[MAX_PATH];
669 WCHAR source[MAX_PATH], dest[MAX_PATH];
670 static const WCHAR prefix[] = {'p','f','x',0};
673 ret = GetTempPathW(MAX_PATH, temp_path);
674 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
676 win_skip("GetTempPathW is not available\n");
679 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
680 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
682 ret = GetTempFileNameW(temp_path, prefix, 0, source);
683 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
685 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
686 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
688 ret = CopyFileW(source, dest, TRUE);
689 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
690 "CopyFileW: unexpected error %d\n", GetLastError());
692 ret = CopyFileW(source, dest, FALSE);
693 ok(ret, "CopyFileW: error %d\n", GetLastError());
695 ret = DeleteFileW(source);
696 ok(ret, "DeleteFileW: error %d\n", GetLastError());
697 ret = DeleteFileW(dest);
698 ok(ret, "DeleteFileW: error %d\n", GetLastError());
703 * Debugging routine to dump a buffer in a hexdump-like fashion.
705 static void dumpmem(unsigned char* mem, int len) {
710 for (x=0; x<len; x+=16) {
711 ln += sprintf(buf+ln, "%04x: ",x);
712 for (y=0; y<16; y++) {
714 ln += sprintf(buf+ln, " ");
716 ln += sprintf(buf+ln, "%02hhx ",mem[x+y]);
719 ln += sprintf(buf+ln, "- ");
720 for (y=0; y<16; y++) {
722 if (mem[x+y]<32 || mem[x+y]>127) {
723 ln += sprintf(buf+ln, ".");
725 ln += sprintf(buf+ln, "%c",mem[x+y]);
729 sprintf(buf+ln, "\n");
735 static void test_CreateFileA(void)
738 char temp_path[MAX_PATH], dirname[MAX_PATH];
739 char filename[MAX_PATH];
740 static const char prefix[] = "pfx";
741 char windowsdir[MAX_PATH];
742 char Volume_1[MAX_PATH];
743 unsigned char buffer[512];
744 char directory[] = "removeme";
745 static const char nt_drive[] = "\\\\?\\A:";
747 struct test_list p[] = {
748 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
749 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
750 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
751 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
752 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
753 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
754 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
755 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
756 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
757 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
758 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
759 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
760 {NULL, 0, -1, 0, FALSE}
762 BY_HANDLE_FILE_INFORMATION Finfo;
764 ret = GetTempPathA(MAX_PATH, temp_path);
765 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
766 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
768 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
769 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
771 SetLastError(0xdeadbeef);
772 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
773 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
774 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
775 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
777 SetLastError(0xdeadbeef);
778 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
779 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
780 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
781 "hFile %p, last error %u\n", hFile, GetLastError());
785 SetLastError(0xdeadbeef);
786 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
787 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
788 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
789 "hFile %p, last error %u\n", hFile, GetLastError());
793 ret = DeleteFileA(filename);
794 ok(ret, "DeleteFileA: error %d\n", GetLastError());
796 SetLastError(0xdeadbeef);
797 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
798 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
799 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
800 "hFile %p, last error %u\n", hFile, GetLastError());
804 ret = DeleteFileA(filename);
805 ok(ret, "DeleteFileA: error %d\n", GetLastError());
807 /* get windows drive letter */
808 ret = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
809 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
810 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
812 /* test error return codes from CreateFile for some cases */
813 ret = GetTempPathA(MAX_PATH, temp_path);
814 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
815 strcpy(dirname, temp_path);
816 strcat(dirname, directory);
817 ret = CreateDirectory(dirname, NULL);
818 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
819 /* set current drive & directory to known location */
820 SetCurrentDirectoryA( temp_path );
825 /* update the drive id in the table entry with the current one */
826 if (p[i].file[1] == ':')
828 strcpy(filename, p[i].file);
829 filename[0] = windowsdir[0];
831 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
833 strcpy(filename, p[i].file);
834 filename[4] = windowsdir[0];
838 /* prefix the table entry with the current temp directory */
839 strcpy(filename, temp_path);
840 strcat(filename, p[i].file);
842 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
843 FILE_SHARE_READ | FILE_SHARE_WRITE,
845 p[i].options, NULL );
846 /* if we get ACCESS_DENIED when we do not expect it, assume
847 * no access to the volume
849 if (hFile == INVALID_HANDLE_VALUE &&
850 GetLastError() == ERROR_ACCESS_DENIED &&
851 p[i].err != ERROR_ACCESS_DENIED)
854 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
856 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
858 /* otherwise validate results with expectations */
859 else if (p[i].todo_flag)
861 (hFile == INVALID_HANDLE_VALUE &&
862 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
863 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
864 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
865 filename, hFile, GetLastError(), p[i].err);
868 (hFile == INVALID_HANDLE_VALUE &&
869 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
870 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
871 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
872 filename, hFile, GetLastError(), p[i].err);
873 if (hFile != INVALID_HANDLE_VALUE)
874 CloseHandle( hFile );
877 ret = RemoveDirectoryA(dirname);
878 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
881 /* test opening directory as a directory */
882 hFile = CreateFileA( temp_path, GENERIC_READ,
886 FILE_FLAG_BACKUP_SEMANTICS, NULL );
887 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
889 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
890 "CreateFileA did not work, last error %u on volume <%s>\n",
891 GetLastError(), temp_path );
893 if (hFile != INVALID_HANDLE_VALUE)
895 ret = GetFileInformationByHandle( hFile, &Finfo );
898 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
899 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
900 temp_path, Finfo.dwFileAttributes);
902 CloseHandle( hFile );
906 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
909 /* *** Test opening volumes/devices using drive letter *** */
911 /* test using drive letter in non-rewrite format without trailing \ */
912 /* this should work */
913 strcpy(filename, nt_drive);
914 filename[4] = windowsdir[0];
915 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
916 FILE_SHARE_READ | FILE_SHARE_WRITE,
918 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
919 if (hFile != INVALID_HANDLE_VALUE ||
920 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
922 /* if we have adm rights to volume, then try rest of tests */
923 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
924 filename, GetLastError());
925 if (hFile != INVALID_HANDLE_VALUE)
927 /* if we opened the volume/device, try to read it. Since it */
928 /* opened, we should be able to read it. We don't care about*/
929 /* what the data is at this time. */
931 ret = ReadFile( hFile, buffer, len, &len, NULL );
932 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
933 GetLastError(), ret, filename);
936 trace("buffer is\n");
939 CloseHandle( hFile );
942 /* test using drive letter with trailing \ and in non-rewrite */
943 /* this should not work */
944 strcpy(filename, nt_drive);
945 filename[4] = windowsdir[0];
946 strcat( filename, "\\" );
947 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
948 FILE_SHARE_READ | FILE_SHARE_WRITE,
950 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
952 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
953 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
954 filename, GetLastError());
955 if (hFile != INVALID_HANDLE_VALUE)
956 CloseHandle( hFile );
958 /* test using temp path with trailing \ and in non-rewrite as dir */
959 /* this should work */
960 strcpy(filename, nt_drive);
962 strcat( filename, temp_path );
963 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
964 FILE_SHARE_READ | FILE_SHARE_WRITE,
966 FILE_FLAG_BACKUP_SEMANTICS, NULL );
967 ok(hFile != INVALID_HANDLE_VALUE,
968 "CreateFileA should have worked on %s, but got %u\n",
969 filename, GetLastError());
970 if (hFile != INVALID_HANDLE_VALUE)
971 CloseHandle( hFile );
973 /* test using drive letter without trailing \ and in device ns */
974 /* this should work */
975 strcpy(filename, nt_drive);
976 filename[4] = windowsdir[0];
978 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
979 FILE_SHARE_READ | FILE_SHARE_WRITE,
981 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
982 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
983 filename, GetLastError());
984 if (hFile != INVALID_HANDLE_VALUE)
985 CloseHandle( hFile );
987 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
988 else if (GetLastError() == ERROR_BAD_NETPATH)
989 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
991 skip("Do not have authority to access volumes. Tests skipped\n");
994 /* *** Test opening volumes/devices using GUID *** */
996 if (pGetVolumeNameForVolumeMountPointA)
998 strcpy(filename, "c:\\");
999 filename[0] = windowsdir[0];
1000 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
1001 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
1004 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
1006 /* test the result of opening a unique volume name (GUID)
1007 * with the trailing \
1008 * this should error out
1010 strcpy(filename, Volume_1);
1011 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1012 FILE_SHARE_READ | FILE_SHARE_WRITE,
1013 NULL, OPEN_EXISTING,
1014 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1016 ok(hFile == INVALID_HANDLE_VALUE,
1017 "CreateFileA should not have opened %s, hFile %p\n",
1020 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1021 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1022 filename, GetLastError());
1023 if (hFile != INVALID_HANDLE_VALUE)
1024 CloseHandle( hFile );
1026 /* test the result of opening a unique volume name (GUID)
1027 * with the temp path string as dir
1030 strcpy(filename, Volume_1);
1031 strcat(filename, temp_path+3);
1032 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1033 FILE_SHARE_READ | FILE_SHARE_WRITE,
1034 NULL, OPEN_EXISTING,
1035 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1037 ok(hFile != INVALID_HANDLE_VALUE,
1038 "CreateFileA should have opened %s, but got %u\n",
1039 filename, GetLastError());
1040 if (hFile != INVALID_HANDLE_VALUE)
1041 CloseHandle( hFile );
1043 /* test the result of opening a unique volume name (GUID)
1044 * without the trailing \ and in device namespace
1047 strcpy(filename, Volume_1);
1050 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1051 FILE_SHARE_READ | FILE_SHARE_WRITE,
1052 NULL, OPEN_EXISTING,
1053 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1054 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1056 /* if we have adm rights to volume, then try rest of tests */
1057 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1058 filename, GetLastError());
1059 if (hFile != INVALID_HANDLE_VALUE)
1061 /* if we opened the volume/device, try to read it. Since it */
1062 /* opened, we should be able to read it. We don't care about*/
1063 /* what the data is at this time. */
1065 ret = ReadFile( hFile, buffer, len, &len, NULL );
1066 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1067 GetLastError(), ret, filename);
1070 trace("buffer is\n");
1071 dumpmem(buffer, 64);
1073 CloseHandle( hFile );
1077 skip("Do not have authority to access volumes. Tests skipped\n");
1080 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1083 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1086 static void test_CreateFileW(void)
1089 WCHAR temp_path[MAX_PATH];
1090 WCHAR filename[MAX_PATH];
1091 static const WCHAR emptyW[]={'\0'};
1092 static const WCHAR prefix[] = {'p','f','x',0};
1093 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1096 ret = GetTempPathW(MAX_PATH, temp_path);
1097 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1099 win_skip("GetTempPathW is not available\n");
1102 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1103 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1105 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1106 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1108 SetLastError(0xdeadbeef);
1109 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1110 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1111 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1112 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1114 SetLastError(0xdeadbeef);
1115 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1116 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1117 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1118 "hFile %p, last error %u\n", hFile, GetLastError());
1122 SetLastError(0xdeadbeef);
1123 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1124 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1125 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1126 "hFile %p, last error %u\n", hFile, GetLastError());
1130 ret = DeleteFileW(filename);
1131 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1133 SetLastError(0xdeadbeef);
1134 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1135 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1136 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1137 "hFile %p, last error %u\n", hFile, GetLastError());
1141 ret = DeleteFileW(filename);
1142 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1146 /* this crashes on NT4.0 */
1147 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1148 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1149 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1150 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1153 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1154 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1155 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1156 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1158 /* test the result of opening a nonexistent driver name */
1159 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1160 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1161 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1162 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1164 ret = CreateDirectoryW(filename, NULL);
1165 ok(ret == TRUE, "couldn't create temporary directory\n");
1166 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1167 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1168 ok(hFile != INVALID_HANDLE_VALUE,
1169 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1171 ret = RemoveDirectoryW(filename);
1172 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1175 static void test_GetTempFileNameA(void)
1179 char expected[MAX_PATH + 10];
1180 char windowsdir[MAX_PATH + 10];
1181 char windowsdrive[3];
1183 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1184 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1185 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1187 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1188 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1190 strcat(windowsdir, "\\");
1193 windowsdrive[0] = windowsdir[0];
1194 windowsdrive[1] = windowsdir[1];
1195 windowsdrive[2] = '\0';
1197 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1198 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1199 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1200 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1201 windowsdrive[0], out);
1203 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1204 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1206 strcat(expected, windowsdir);
1207 strcat(expected, "abc2.tmp");
1208 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1212 static void test_DeleteFileA( void )
1216 ret = DeleteFileA(NULL);
1217 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1218 GetLastError() == ERROR_PATH_NOT_FOUND),
1219 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1221 ret = DeleteFileA("");
1222 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1223 GetLastError() == ERROR_BAD_PATHNAME),
1224 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1226 ret = DeleteFileA("nul");
1227 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1228 GetLastError() == ERROR_INVALID_PARAMETER ||
1229 GetLastError() == ERROR_ACCESS_DENIED ||
1230 GetLastError() == ERROR_INVALID_FUNCTION),
1231 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1234 static void test_DeleteFileW( void )
1237 WCHAR pathW[MAX_PATH];
1238 WCHAR pathsubW[MAX_PATH];
1239 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1240 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1241 static const WCHAR emptyW[]={'\0'};
1243 ret = DeleteFileW(NULL);
1244 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1246 win_skip("DeleteFileW is not available\n");
1249 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1250 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1252 ret = DeleteFileW(emptyW);
1253 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1254 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1256 /* test DeleteFile on empty directory */
1257 ret = GetTempPathW(MAX_PATH, pathW);
1258 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1260 ok(0, "MAX_PATH exceeded in constructing paths\n");
1263 lstrcatW(pathW, dirW);
1264 lstrcpyW(pathsubW, pathW);
1265 lstrcatW(pathsubW, subdirW);
1266 ret = CreateDirectoryW(pathW, NULL);
1267 ok(ret == TRUE, "couldn't create directory deletefile\n");
1268 ret = DeleteFileW(pathW);
1269 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1270 ret = RemoveDirectoryW(pathW);
1271 ok(ret == TRUE, "expected to remove directory deletefile\n");
1273 /* test DeleteFile on non-empty directory */
1274 ret = CreateDirectoryW(pathW, NULL);
1275 ok(ret == TRUE, "couldn't create directory deletefile\n");
1276 ret = CreateDirectoryW(pathsubW, NULL);
1277 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1278 ret = DeleteFileW(pathW);
1279 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1280 ret = RemoveDirectoryW(pathsubW);
1281 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1282 ret = RemoveDirectoryW(pathW);
1283 ok(ret == TRUE, "expected to remove directory deletefile\n");
1286 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1288 static void test_MoveFileA(void)
1290 char tempdir[MAX_PATH];
1291 char source[MAX_PATH], dest[MAX_PATH];
1292 static const char prefix[] = "pfx";
1298 ret = GetTempPathA(MAX_PATH, tempdir);
1299 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1300 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1302 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1303 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1305 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1306 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1308 ret = MoveFileA(source, dest);
1309 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1310 "MoveFileA: unexpected error %d\n", GetLastError());
1312 ret = DeleteFileA(dest);
1313 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1315 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1316 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1318 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1319 ok( retok && ret == sizeof(prefix),
1320 "WriteFile error %d\n", GetLastError());
1322 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1323 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1325 ret = MoveFileA(source, dest);
1327 ok(!ret, "MoveFileA: expected failure\n");
1328 ok(GetLastError() == ERROR_SHARING_VIOLATION, "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1331 CloseHandle(hmapfile);
1334 /* if MoveFile succeeded, move back to dest */
1335 if (ret) MoveFile(dest, source);
1337 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1338 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1340 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1341 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1343 ret = MoveFileA(source, dest);
1345 ok(!ret, "MoveFileA: expected failure\n");
1346 ok(GetLastError() == ERROR_SHARING_VIOLATION, "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1349 CloseHandle(hmapfile);
1352 /* if MoveFile succeeded, move back to dest */
1353 if (ret) MoveFile(dest, source);
1355 ret = MoveFileA(source, dest);
1356 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1358 lstrcatA(tempdir, "Remove Me");
1359 ret = CreateDirectoryA(tempdir, NULL);
1360 ok(ret == TRUE, "CreateDirectoryA failed\n");
1362 lstrcpyA(source, dest);
1363 lstrcpyA(dest, tempdir);
1364 lstrcatA(dest, "\\wild?.*");
1365 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1366 ret = MoveFileA(source, dest);
1367 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1368 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1369 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1370 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1371 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1373 WIN32_FIND_DATAA fd;
1374 char temppath[MAX_PATH];
1377 lstrcpyA(temppath, tempdir);
1378 lstrcatA(temppath, "\\*.*");
1379 hFind = FindFirstFileA(temppath, &fd);
1380 if (INVALID_HANDLE_VALUE != hFind)
1385 lpName = fd.cAlternateFileName;
1387 lpName = fd.cFileName;
1388 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1390 while (FindNextFileA(hFind, &fd));
1394 ret = DeleteFileA(source);
1395 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1396 ret = DeleteFileA(dest);
1397 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1398 ret = RemoveDirectoryA(tempdir);
1399 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1402 static void test_MoveFileW(void)
1404 WCHAR temp_path[MAX_PATH];
1405 WCHAR source[MAX_PATH], dest[MAX_PATH];
1406 static const WCHAR prefix[] = {'p','f','x',0};
1409 ret = GetTempPathW(MAX_PATH, temp_path);
1410 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1412 win_skip("GetTempPathW is not available\n");
1415 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1416 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1418 ret = GetTempFileNameW(temp_path, prefix, 0, source);
1419 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1421 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
1422 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1424 ret = MoveFileW(source, dest);
1425 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1426 "CopyFileW: unexpected error %d\n", GetLastError());
1428 ret = DeleteFileW(source);
1429 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1430 ret = DeleteFileW(dest);
1431 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1434 #define PATTERN_OFFSET 0x10
1436 static void test_offset_in_overlapped_structure(void)
1442 BYTE buf[256], pattern[] = "TeSt";
1444 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
1447 ret =GetTempPathA(MAX_PATH, temp_path);
1448 ok( ret, "GetTempPathA error %d\n", GetLastError());
1449 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
1450 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
1452 /*** Write File *****************************************************/
1454 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1455 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1457 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
1458 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
1459 ok( ret, "WriteFile error %d\n", GetLastError());
1460 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
1462 memset(&ov, 0, sizeof(ov));
1463 S(U(ov)).Offset = PATTERN_OFFSET;
1464 S(U(ov)).OffsetHigh = 0;
1465 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1466 /* Win 9x does not support the overlapped I/O on files */
1467 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1468 ok(rc, "WriteFile error %d\n", GetLastError());
1469 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1470 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1471 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1473 S(U(ov)).Offset = sizeof(buf) * 2;
1474 S(U(ov)).OffsetHigh = 0;
1475 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1476 ok( ret, "WriteFile error %d\n", GetLastError());
1477 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1478 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1479 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1484 /*** Read File *****************************************************/
1486 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1487 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1489 memset(buf, 0, sizeof(buf));
1490 memset(&ov, 0, sizeof(ov));
1491 S(U(ov)).Offset = PATTERN_OFFSET;
1492 S(U(ov)).OffsetHigh = 0;
1493 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1494 /* Win 9x does not support the overlapped I/O on files */
1495 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1496 ok(rc, "ReadFile error %d\n", GetLastError());
1497 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1498 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1499 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1500 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1505 ret = DeleteFileA(temp_fname);
1506 ok( ret, "DeleteFileA error %d\n", GetLastError());
1509 static void test_LockFile(void)
1513 OVERLAPPED overlapped;
1514 int limited_LockFile;
1515 int limited_UnLockFile;
1518 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1519 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1520 CREATE_ALWAYS, 0, 0 );
1521 if (handle == INVALID_HANDLE_VALUE)
1523 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1526 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1528 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1529 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1531 limited_UnLockFile = 0;
1532 if (UnlockFile( handle, 0, 0, 0, 0 ))
1534 limited_UnLockFile = 1;
1537 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1538 /* overlapping locks must fail */
1539 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1540 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1541 /* non-overlapping locks must succeed */
1542 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1544 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1545 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1546 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1547 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1549 S(U(overlapped)).Offset = 100;
1550 S(U(overlapped)).OffsetHigh = 0;
1551 overlapped.hEvent = 0;
1553 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1554 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1556 /* LockFileEx is probably OK, test it more. */
1557 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1558 "LockFileEx 100,100 failed\n" );
1561 /* overlapping shared locks are OK */
1562 S(U(overlapped)).Offset = 150;
1563 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1565 /* but exclusive is not */
1566 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1567 0, 50, 0, &overlapped ),
1568 "LockFileEx exclusive 150,50 succeeded\n" );
1569 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1570 { /* UnLockFile is capable. */
1571 S(U(overlapped)).Offset = 100;
1572 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1573 "UnlockFileEx 150,100 again succeeded\n" );
1576 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
1579 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1580 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1581 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1584 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
1586 /* wrap-around lock should not do anything */
1587 /* (but still succeeds on NT4 so we don't check result) */
1588 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1590 limited_LockFile = 0;
1591 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1593 limited_LockFile = 1;
1596 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1598 /* zero-byte lock */
1599 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1600 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1601 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1602 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1604 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1605 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1607 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1609 CloseHandle( handle );
1610 DeleteFileA( filename );
1613 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1617 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1618 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1623 if (!access1) access1 = GENERIC_READ;
1626 if (!access2) access2 = GENERIC_READ;
1629 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1630 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1631 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1632 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1633 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1634 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1638 static void test_file_sharing(void)
1640 static const DWORD access_modes[] =
1641 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1642 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1643 static const DWORD sharing_modes[] =
1644 { 0, FILE_SHARE_READ,
1645 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1646 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1647 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1651 BOOL is_win9x = FALSE;
1653 /* make sure the file exists */
1654 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1655 if (h == INVALID_HANDLE_VALUE)
1657 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1660 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1663 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1665 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1667 /* Win9x doesn't support FILE_SHARE_DELETE */
1668 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1671 SetLastError(0xdeadbeef);
1672 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1673 NULL, OPEN_EXISTING, 0, 0 );
1674 if (h == INVALID_HANDLE_VALUE)
1676 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1679 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1681 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1683 /* Win9x doesn't support FILE_SHARE_DELETE */
1684 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1687 SetLastError(0xdeadbeef);
1688 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1689 NULL, OPEN_EXISTING, 0, 0 );
1691 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1692 access_modes[a2], sharing_modes[s2], is_win9x ))
1694 ret = GetLastError();
1696 ok( h2 != INVALID_HANDLE_VALUE,
1697 "open failed for modes %x/%x/%x/%x\n",
1698 access_modes[a1], sharing_modes[s1],
1699 access_modes[a2], sharing_modes[s2] );
1700 ok( ret == 0xdeadbeef /* Win9x */ ||
1702 "wrong error code %d\n", ret );
1708 ret = GetLastError();
1710 ok( h2 == INVALID_HANDLE_VALUE,
1711 "open succeeded for modes %x/%x/%x/%x\n",
1712 access_modes[a1], sharing_modes[s1],
1713 access_modes[a2], sharing_modes[s2] );
1714 ok( ret == ERROR_SHARING_VIOLATION,
1715 "wrong error code %d\n", ret );
1723 SetLastError(0xdeadbeef);
1724 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1725 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1727 SetLastError(0xdeadbeef);
1728 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1729 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1730 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1732 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1733 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1738 DeleteFileA( filename );
1741 static char get_windows_drive(void)
1743 char windowsdir[MAX_PATH];
1744 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1745 return windowsdir[0];
1748 static void test_FindFirstFileA(void)
1751 WIN32_FIND_DATAA data;
1753 char buffer[5] = "C:\\";
1755 char nonexistent[MAX_PATH];
1757 /* try FindFirstFileA on "C:\" */
1758 buffer[0] = get_windows_drive();
1760 SetLastError( 0xdeadbeaf );
1761 handle = FindFirstFileA(buffer, &data);
1762 err = GetLastError();
1763 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1764 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1766 /* try FindFirstFileA on "C:\*" */
1767 strcpy(buffer2, buffer);
1768 strcat(buffer2, "*");
1769 handle = FindFirstFileA(buffer2, &data);
1770 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1771 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1772 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1773 if (FindNextFileA( handle, &data ))
1774 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1775 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1776 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1778 /* try FindFirstFileA on windows dir */
1779 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1780 strcat(buffer2, "\\*");
1781 handle = FindFirstFileA(buffer2, &data);
1782 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1783 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1784 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1785 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1786 while (FindNextFileA( handle, &data ))
1787 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1788 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1789 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1791 /* try FindFirstFileA on "C:\foo\" */
1792 SetLastError( 0xdeadbeaf );
1793 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
1796 GetTempPathA( sizeof(tmp), tmp );
1797 GetTempFileNameA( tmp, "foo", 0, nonexistent );
1799 DeleteFileA( nonexistent );
1800 strcpy(buffer2, nonexistent);
1801 strcat(buffer2, "\\");
1802 handle = FindFirstFileA(buffer2, &data);
1803 err = GetLastError();
1804 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1806 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1809 /* try FindFirstFileA on "C:\foo\bar.txt" */
1810 SetLastError( 0xdeadbeaf );
1811 strcpy(buffer2, nonexistent);
1812 strcat(buffer2, "\\bar.txt");
1813 handle = FindFirstFileA(buffer2, &data);
1814 err = GetLastError();
1815 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1816 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1818 /* try FindFirstFileA on "C:\foo\*.*" */
1819 SetLastError( 0xdeadbeaf );
1820 strcpy(buffer2, nonexistent);
1821 strcat(buffer2, "\\*.*");
1822 handle = FindFirstFileA(buffer2, &data);
1823 err = GetLastError();
1824 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1825 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1827 /* try FindFirstFileA on "foo\bar.txt" */
1828 SetLastError( 0xdeadbeaf );
1829 strcpy(buffer2, nonexistent + 3);
1830 strcat(buffer2, "\\bar.txt");
1831 handle = FindFirstFileA(buffer2, &data);
1832 err = GetLastError();
1833 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1834 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1836 /* try FindFirstFileA on "c:\nul" */
1837 SetLastError( 0xdeadbeaf );
1838 strcpy(buffer2, buffer);
1839 strcat(buffer2, "nul");
1840 handle = FindFirstFileA(buffer2, &data);
1841 err = GetLastError();
1842 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1843 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1844 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1845 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1846 "wrong attributes %x\n", data.dwFileAttributes );
1847 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
1849 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1850 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1852 SetLastError( 0xdeadbeaf );
1853 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1854 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1855 ok( FindClose( handle ), "failed to close handle\n" );
1857 /* try FindFirstFileA on "lpt1" */
1858 SetLastError( 0xdeadbeaf );
1859 strcpy(buffer2, "lpt1");
1860 handle = FindFirstFileA(buffer2, &data);
1861 err = GetLastError();
1862 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1863 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1864 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1865 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1866 "wrong attributes %x\n", data.dwFileAttributes );
1867 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
1869 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1870 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1872 SetLastError( 0xdeadbeaf );
1873 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1874 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1875 ok( FindClose( handle ), "failed to close handle\n" );
1877 /* try FindFirstFileA on "c:\nul\*" */
1878 SetLastError( 0xdeadbeaf );
1879 strcpy(buffer2, buffer);
1880 strcat(buffer2, "nul\\*");
1881 handle = FindFirstFileA(buffer2, &data);
1882 err = GetLastError();
1883 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1884 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1886 /* try FindFirstFileA on "c:\nul*" */
1887 SetLastError( 0xdeadbeaf );
1888 strcpy(buffer2, buffer);
1889 strcat(buffer2, "nul*");
1890 handle = FindFirstFileA(buffer2, &data);
1891 err = GetLastError();
1892 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1893 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1895 /* try FindFirstFileA on "c:\foo\bar\nul" */
1896 SetLastError( 0xdeadbeaf );
1897 strcpy(buffer2, buffer);
1898 strcat(buffer2, "foo\\bar\\nul");
1899 handle = FindFirstFileA(buffer2, &data);
1900 err = GetLastError();
1901 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1902 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1904 /* try FindFirstFileA on "c:\foo\nul\bar" */
1905 SetLastError( 0xdeadbeaf );
1906 strcpy(buffer2, buffer);
1907 strcat(buffer2, "foo\\nul\\bar");
1908 handle = FindFirstFileA(buffer2, &data);
1909 err = GetLastError();
1910 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1911 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1914 static void test_FindNextFileA(void)
1917 WIN32_FIND_DATAA search_results;
1919 char buffer[5] = "C:\\*";
1921 buffer[0] = get_windows_drive();
1922 handle = FindFirstFileA(buffer,&search_results);
1923 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1924 while (FindNextFile(handle, &search_results))
1926 /* get to the end of the files */
1928 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1929 err = GetLastError();
1930 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1933 static void test_FindFirstFileExA(FINDEX_SEARCH_OPS search_ops)
1935 WIN32_FIND_DATAA search_results;
1939 if (!pFindFirstFileExA)
1941 win_skip("FindFirstFileExA() is missing\n");
1945 CreateDirectoryA("test-dir", NULL);
1946 _lclose(_lcreat("test-dir\\file1", 0));
1947 _lclose(_lcreat("test-dir\\file2", 0));
1948 CreateDirectoryA("test-dir\\dir1", NULL);
1949 SetLastError(0xdeadbeef);
1950 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, search_ops, NULL, 0);
1951 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1953 win_skip("FindFirstFileExA is not implemented\n");
1956 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1957 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1959 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1961 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1962 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1964 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1965 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
1967 SetLastError(0xdeadbeef);
1968 ret = FindNextFile(handle, &search_results);
1969 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
1971 skip("File system supports directory filtering\n");
1972 /* Results from the previous call are not cleared */
1973 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
1974 FindClose( handle );
1978 ok(ret, "Fetching fourth file failed\n");
1979 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1981 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1982 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1986 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
1988 FindClose( handle );
1991 DeleteFileA("test-dir\\file1");
1992 DeleteFileA("test-dir\\file2");
1993 RemoveDirectoryA("test-dir\\dir1");
1994 RemoveDirectoryA("test-dir");
1997 static int test_Mapfile_createtemp(HANDLE *handle)
1999 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
2000 DeleteFile(filename);
2001 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
2002 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2003 if (*handle != INVALID_HANDLE_VALUE) {
2011 static void test_MapFile(void)
2016 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2018 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
2019 ok( hmap != NULL, "mapping should work, I named it!\n" );
2021 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2023 /* We have to close file before we try new stuff with mapping again.
2024 Else we would always succeed on XP or block descriptors on 95. */
2025 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2026 ok( hmap != NULL, "We should still be able to map!\n" );
2027 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2028 ok( CloseHandle( handle ), "can't close file handle\n");
2031 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2033 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2034 ok( hmap == NULL, "mapped zero size file\n");
2035 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
2037 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
2038 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2039 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2041 CloseHandle( hmap );
2043 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
2044 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2045 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2047 CloseHandle( hmap );
2049 /* On XP you can now map again, on Win 95 you cannot. */
2051 ok( CloseHandle( handle ), "can't close file handle\n");
2052 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
2055 static void test_GetFileType(void)
2058 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2059 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
2060 type = GetFileType(h);
2061 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
2063 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2064 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
2065 type = GetFileType(h);
2066 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
2068 DeleteFileA( filename );
2071 static int completion_count;
2073 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2075 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2076 ReleaseSemaphore(ovl->hEvent, 1, NULL);
2080 static void test_async_file_errors(void)
2082 char szFile[MAX_PATH];
2083 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
2085 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
2087 S(U(ovl)).Offset = 0;
2088 S(U(ovl)).OffsetHigh = 0;
2090 completion_count = 0;
2092 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
2093 strcat(szFile, "\\win.ini");
2094 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2095 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2096 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
2097 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2098 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2099 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2104 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2106 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2107 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2110 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2112 S(U(ovl)).Offset += count;
2113 /* i/o completion routine only called if ReadFileEx returned success.
2114 * we only care about violations of this rule so undo what should have
2118 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2119 /*printf("Error = %ld\n", GetLastError());*/
2120 HeapFree(GetProcessHeap(), 0, lpBuffer);
2123 static void test_read_write(void)
2125 DWORD bytes, ret, old_prot;
2127 char temp_path[MAX_PATH];
2128 char filename[MAX_PATH];
2130 static const char prefix[] = "pfx";
2132 ret = GetTempPathA(MAX_PATH, temp_path);
2133 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2134 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2136 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2137 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2139 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2140 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2141 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2143 SetLastError(12345678);
2145 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2146 ok(ret && GetLastError() == 12345678,
2147 "ret = %d, error %d\n", ret, GetLastError());
2148 ok(!bytes, "bytes = %d\n", bytes);
2150 SetLastError(12345678);
2152 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2153 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2154 (ret && GetLastError() == 12345678), /* Win9x */
2155 "ret = %d, error %d\n", ret, GetLastError());
2156 ok(!bytes || /* Win2k */
2157 bytes == 10, /* Win9x */
2158 "bytes = %d\n", bytes);
2160 /* make sure the file contains data */
2161 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2162 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
2164 SetLastError(12345678);
2166 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
2167 ok(ret && GetLastError() == 12345678,
2168 "ret = %d, error %d\n", ret, GetLastError());
2169 ok(!bytes, "bytes = %d\n", bytes);
2171 SetLastError(12345678);
2173 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
2174 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
2175 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
2176 "ret = %d, error %d\n", ret, GetLastError());
2177 ok(!bytes, "bytes = %d\n", bytes);
2179 /* test passing protected memory as buffer */
2181 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
2182 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
2184 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2185 ok( ret, "WriteFile failed error %u\n", GetLastError() );
2186 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
2188 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
2189 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2191 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2192 ok( !ret, "WriteFile succeeded\n" );
2193 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2194 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2195 "wrong error %u\n", GetLastError() );
2196 ok( bytes == 0, "wrote %x bytes\n", bytes );
2198 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
2199 ok( !ret, "WriteFile succeeded\n" );
2200 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
2201 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2202 "wrong error %u\n", GetLastError() );
2203 ok( bytes == 0, "wrote %x bytes\n", bytes );
2205 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
2206 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2208 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2209 ok( !ret, "WriteFile succeeded\n" );
2210 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2211 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2212 "wrong error %u\n", GetLastError() );
2213 ok( bytes == 0, "wrote %x bytes\n", bytes );
2215 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2217 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2218 ok( !ret, "ReadFile succeeded\n" );
2219 ok( GetLastError() == ERROR_NOACCESS ||
2220 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2221 "wrong error %u\n", GetLastError() );
2222 ok( bytes == 0, "read %x bytes\n", bytes );
2224 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
2225 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2227 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2228 ok( !ret, "ReadFile succeeded\n" );
2229 ok( GetLastError() == ERROR_NOACCESS ||
2230 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2231 "wrong error %u\n", GetLastError() );
2232 ok( bytes == 0, "read %x bytes\n", bytes );
2234 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
2235 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2237 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2238 ok( !ret, "ReadFile succeeded\n" );
2239 ok( GetLastError() == ERROR_NOACCESS ||
2240 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2241 "wrong error %u\n", GetLastError() );
2242 ok( bytes == 0, "read %x bytes\n", bytes );
2244 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
2245 SetEndOfFile( hFile );
2246 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2248 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2249 ok( !ret, "ReadFile succeeded\n" );
2250 ok( GetLastError() == ERROR_NOACCESS ||
2251 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2252 "wrong error %u\n", GetLastError() );
2253 ok( bytes == 0, "read %x bytes\n", bytes );
2255 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
2256 ok( ret, "ReadFile failed error %u\n", GetLastError() );
2257 ok( bytes == 0x1234, "read %x bytes\n", bytes );
2259 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
2260 ok( !ret, "ReadFile succeeded\n" );
2261 ok( GetLastError() == ERROR_NOACCESS ||
2262 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2263 "wrong error %u\n", GetLastError() );
2264 ok( bytes == 0, "read %x bytes\n", bytes );
2266 VirtualFree( mem, 0, MEM_FREE );
2268 ret = CloseHandle(hFile);
2269 ok( ret, "CloseHandle: error %d\n", GetLastError());
2270 ret = DeleteFileA(filename);
2271 ok( ret, "DeleteFileA: error %d\n", GetLastError());
2274 static void test_OpenFile(void)
2281 static const char file[] = "regedit.exe";
2282 static const char foo[] = ".\\foo-bar-foo.baz";
2283 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
2284 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2285 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2286 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2287 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2288 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2289 char buff[MAX_PATH];
2290 char buff_long[4*MAX_PATH];
2291 char filled_0xA5[OFS_MAXPATHNAME];
2295 /* Check for existing file */
2296 if (!pGetSystemWindowsDirectoryA)
2297 length = GetWindowsDirectoryA(buff, MAX_PATH);
2299 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
2301 if (length + sizeof(file) < MAX_PATH)
2303 p = buff + strlen(buff);
2304 if (p > buff && p[-1] != '\\') *p++ = '\\';
2306 memset(&ofs, 0xA5, sizeof(ofs));
2307 SetLastError(0xfaceabee);
2309 hFile = OpenFile(buff, &ofs, OF_EXIST);
2310 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
2311 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2312 "GetLastError() returns %d\n", GetLastError() );
2313 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2314 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2315 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2316 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2317 ofs.szPathName, buff );
2320 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
2321 length = GetCurrentDirectoryA(MAX_PATH, buff);
2323 /* Check for nonexistent file */
2324 if (length + sizeof(foo) < MAX_PATH)
2326 p = buff + strlen(buff);
2327 if (p > buff && p[-1] != '\\') *p++ = '\\';
2328 strcpy( p, foo + 2 );
2329 memset(&ofs, 0xA5, sizeof(ofs));
2330 SetLastError(0xfaceabee);
2332 hFile = OpenFile(foo, &ofs, OF_EXIST);
2333 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2334 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
2336 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2337 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2338 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2339 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2340 ofs.szPathName, buff );
2343 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
2344 length += lstrlenA(foo_too_long + 1);
2346 /* Check for nonexistent file with too long filename */
2347 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
2349 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
2350 memset(&ofs, 0xA5, sizeof(ofs));
2351 SetLastError(0xfaceabee);
2353 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
2354 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2355 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
2356 "GetLastError() returns %d\n", GetLastError() );
2358 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2359 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
2360 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2361 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2362 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2366 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
2368 if (length >= MAX_PATH)
2370 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
2373 p = buff + strlen(buff);
2374 if (p > buff && p[-1] != '\\') *p++ = '\\';
2375 strcpy( p, filename );
2377 memset(&ofs, 0xA5, sizeof(ofs));
2378 SetLastError(0xfaceabee);
2379 /* Create an empty file */
2380 hFile = OpenFile(filename, &ofs, OF_CREATE);
2381 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
2382 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2383 "GetLastError() returns %d\n", GetLastError() );
2384 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2385 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2386 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2387 ret = _lclose(hFile);
2388 ok( !ret, "_lclose() returns %d\n", ret );
2389 retval = GetFileAttributesA(filename);
2390 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
2392 memset(&ofs, 0xA5, sizeof(ofs));
2393 SetLastError(0xfaceabee);
2394 /* Check various opening options: */
2395 /* for reading only, */
2396 hFile = OpenFile(filename, &ofs, OF_READ);
2397 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
2398 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2399 "GetLastError() returns %d\n", GetLastError() );
2400 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2401 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2402 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2403 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2404 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2405 ret = _lclose(hFile);
2406 ok( !ret, "_lclose() returns %d\n", ret );
2408 memset(&ofs, 0xA5, sizeof(ofs));
2409 SetLastError(0xfaceabee);
2410 /* for writing only, */
2411 hFile = OpenFile(filename, &ofs, OF_WRITE);
2412 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
2413 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2414 "GetLastError() returns %d\n", GetLastError() );
2415 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2416 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2417 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2418 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2419 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2420 ret = _lclose(hFile);
2421 ok( !ret, "_lclose() returns %d\n", ret );
2423 memset(&ofs, 0xA5, sizeof(ofs));
2424 SetLastError(0xfaceabee);
2425 /* for reading and writing, */
2426 hFile = OpenFile(filename, &ofs, OF_READWRITE);
2427 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
2428 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2429 "GetLastError() returns %d\n", GetLastError() );
2430 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2431 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2432 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2433 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2434 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2435 ret = _lclose(hFile);
2436 ok( !ret, "_lclose() returns %d\n", ret );
2438 memset(&ofs, 0xA5, sizeof(ofs));
2439 SetLastError(0xfaceabee);
2440 /* for checking file presence. */
2441 hFile = OpenFile(filename, &ofs, OF_EXIST);
2442 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
2443 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2444 "GetLastError() returns %d\n", GetLastError() );
2445 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2446 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2447 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2448 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2449 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2451 memset(&ofs, 0xA5, sizeof(ofs));
2452 SetLastError(0xfaceabee);
2453 /* Delete the file and make sure it doesn't exist anymore */
2454 hFile = OpenFile(filename, &ofs, OF_DELETE);
2455 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
2456 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2457 "GetLastError() returns %d\n", GetLastError() );
2458 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2459 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2460 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2461 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2462 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2464 retval = GetFileAttributesA(filename);
2465 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
2468 static void test_overlapped(void)
2473 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2474 if (0) /* tested: WinXP */
2476 GetOverlappedResult(0, NULL, &result, FALSE);
2477 GetOverlappedResult(0, &ov, NULL, FALSE);
2478 GetOverlappedResult(0, NULL, NULL, FALSE);
2481 memset( &ov, 0, sizeof ov );
2483 r = GetOverlappedResult(0, &ov, &result, 0);
2485 ok( result == 0, "wrong result %u\n", result );
2487 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2491 ov.InternalHigh = 0xabcd;
2492 r = GetOverlappedResult(0, &ov, &result, 0);
2494 ok( result == 0xabcd, "wrong result %u\n", result );
2496 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2498 SetLastError( 0xb00 );
2500 ov.Internal = STATUS_INVALID_HANDLE;
2501 ov.InternalHigh = 0xabcd;
2502 r = GetOverlappedResult(0, &ov, &result, 0);
2503 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2504 ok( r == FALSE, "should return false\n");
2505 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
2507 SetLastError( 0xb00 );
2509 ov.Internal = STATUS_PENDING;
2510 ov.InternalHigh = 0xabcd;
2511 r = GetOverlappedResult(0, &ov, &result, 0);
2512 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2513 "wrong error %u\n", GetLastError() );
2514 ok( r == FALSE, "should return false\n");
2515 ok( result == 0, "wrong result %u\n", result );
2517 SetLastError( 0xb00 );
2518 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
2519 ov.Internal = STATUS_PENDING;
2520 ov.InternalHigh = 0xabcd;
2521 r = GetOverlappedResult(0, &ov, &result, 0);
2522 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2523 "wrong error %u\n", GetLastError() );
2524 ok( r == FALSE, "should return false\n");
2526 ResetEvent( ov.hEvent );
2528 SetLastError( 0xb00 );
2529 ov.Internal = STATUS_PENDING;
2530 ov.InternalHigh = 0;
2531 r = GetOverlappedResult(0, &ov, &result, 0);
2532 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2533 "wrong error %u\n", GetLastError() );
2534 ok( r == FALSE, "should return false\n");
2536 r = CloseHandle( ov.hEvent );
2537 ok( r == TRUE, "close handle failed\n");
2540 static void test_RemoveDirectory(void)
2543 char directory[] = "removeme";
2545 rc = CreateDirectory(directory, NULL);
2546 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
2548 rc = SetCurrentDirectory(directory);
2549 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2551 rc = RemoveDirectory(".");
2554 rc = SetCurrentDirectory("..");
2555 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2557 rc = RemoveDirectory(directory);
2558 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2562 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
2564 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
2565 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
2566 return abs(t1 - t2) <= tolerance;
2569 static void test_ReplaceFileA(void)
2571 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2572 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
2573 static const char replacedData[] = "file-to-replace";
2574 static const char replacementData[] = "new-file";
2575 static const char backupData[] = "backup-file";
2576 FILETIME ftReplaced, ftReplacement, ftBackup;
2577 static const char prefix[] = "pfx";
2578 char temp_path[MAX_PATH];
2580 BOOL retok, removeBackup = FALSE;
2584 win_skip("ReplaceFileA() is missing\n");
2588 ret = GetTempPathA(MAX_PATH, temp_path);
2589 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2590 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2592 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
2593 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2595 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2596 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2598 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
2599 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2601 /* place predictable data in the file to be replaced */
2602 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2603 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2604 "failed to open replaced file\n");
2605 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
2606 ok( retok && ret == sizeof(replacedData),
2607 "WriteFile error (replaced) %d\n", GetLastError());
2608 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
2609 "replaced file has wrong size\n");
2610 /* place predictable data in the file to be the replacement */
2611 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2612 ok(hReplacementFile != INVALID_HANDLE_VALUE,
2613 "failed to open replacement file\n");
2614 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
2615 ok( retok && ret == sizeof(replacementData),
2616 "WriteFile error (replacement) %d\n", GetLastError());
2617 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
2618 "replacement file has wrong size\n");
2619 /* place predictable data in the backup file (to be over-written) */
2620 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2621 ok(hBackupFile != INVALID_HANDLE_VALUE,
2622 "failed to open backup file\n");
2623 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
2624 ok( retok && ret == sizeof(backupData),
2625 "WriteFile error (replacement) %d\n", GetLastError());
2626 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
2627 "backup file has wrong size\n");
2628 /* change the filetime on the "replaced" file to ensure that it changes */
2629 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2630 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
2631 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
2632 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2633 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
2634 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
2635 CloseHandle(hReplacedFile);
2636 /* change the filetime on the backup to ensure that it changes */
2637 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2638 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
2639 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
2640 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2641 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
2642 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
2643 CloseHandle(hBackupFile);
2644 /* get the filetime on the replacement file to perform checks */
2645 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
2646 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
2647 CloseHandle(hReplacementFile);
2649 /* perform replacement w/ backup
2650 * TODO: flags are not implemented
2652 SetLastError(0xdeadbeef);
2653 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2654 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2655 /* make sure that the backup has the size of the old "replaced" file */
2656 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2657 ok(hBackupFile != INVALID_HANDLE_VALUE,
2658 "failed to open backup file\n");
2659 ret = GetFileSize(hBackupFile, NULL);
2660 ok(ret == sizeof(replacedData),
2661 "backup file has wrong size %d\n", ret);
2662 /* make sure that the "replaced" file has the size of the replacement file */
2663 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2664 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2665 "failed to open replaced file: %d\n", GetLastError());
2666 if (hReplacedFile != INVALID_HANDLE_VALUE)
2668 ret = GetFileSize(hReplacedFile, NULL);
2669 ok(ret == sizeof(replacementData),
2670 "replaced file has wrong size %d\n", ret);
2671 /* make sure that the replacement file no-longer exists */
2672 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2673 ok(hReplacementFile == INVALID_HANDLE_VALUE,
2674 "unexpected error, replacement file should not exist %d\n", GetLastError());
2675 /* make sure that the backup has the old "replaced" filetime */
2676 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2677 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2678 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
2679 CloseHandle(hBackupFile);
2680 /* make sure that the "replaced" has the old replacement filetime */
2681 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2682 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2683 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
2684 "replaced file has wrong filetime %x%08x / %x%08x\n",
2685 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
2686 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
2687 CloseHandle(hReplacedFile);
2690 skip("couldn't open replacement file, skipping tests\n");
2692 /* re-create replacement file for pass w/o backup (blank) */
2693 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2694 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2695 /* perform replacement w/o backup
2696 * TODO: flags are not implemented
2698 SetLastError(0xdeadbeef);
2699 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2700 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2701 "ReplaceFileA: unexpected error %d\n", GetLastError());
2703 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2704 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2705 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2706 ret = DeleteFileA(backup);
2707 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2708 /* perform replacement w/ backup (no pre-existing backup)
2709 * TODO: flags are not implemented
2711 SetLastError(0xdeadbeef);
2712 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2713 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2714 "ReplaceFileA: unexpected error %d\n", GetLastError());
2716 removeBackup = TRUE;
2718 /* re-create replacement file for pass w/ no permissions to "replaced" */
2719 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2720 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2721 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2722 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2723 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2724 /* perform replacement w/ backup (no permission to "replaced")
2725 * TODO: flags are not implemented
2727 SetLastError(0xdeadbeef);
2728 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2729 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2730 /* make sure that the replacement file still exists */
2731 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2732 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
2733 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
2734 "unexpected error, replacement file should still exist %d\n", GetLastError());
2735 CloseHandle(hReplacementFile);
2736 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2737 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2738 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2740 /* replacement file still exists, make pass w/o "replaced" */
2741 ret = DeleteFileA(replaced);
2742 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2743 "DeleteFileA: error (replaced) %d\n", GetLastError());
2744 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2745 * TODO: flags are not implemented
2747 SetLastError(0xdeadbeef);
2748 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2749 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2750 GetLastError() == ERROR_ACCESS_DENIED),
2751 "ReplaceFileA: unexpected error %d\n", GetLastError());
2753 /* perform replacement w/o existing "replacement" file
2754 * TODO: flags are not implemented
2756 SetLastError(0xdeadbeef);
2757 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2758 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2759 GetLastError() == ERROR_ACCESS_DENIED),
2760 "ReplaceFileA: unexpected error %d\n", GetLastError());
2763 * if the first round (w/ backup) worked then as long as there is no
2764 * failure then there is no need to check this round (w/ backup is the
2765 * more complete case)
2768 /* delete temporary files, replacement and replaced are already deleted */
2771 ret = DeleteFileA(backup);
2773 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2774 "DeleteFileA: error (backup) %d\n", GetLastError());
2779 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2780 * need to be as thorough.
2782 static void test_ReplaceFileW(void)
2784 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2785 static const WCHAR prefix[] = {'p','f','x',0};
2786 WCHAR temp_path[MAX_PATH];
2788 BOOL removeBackup = FALSE;
2792 win_skip("ReplaceFileW() is missing\n");
2796 ret = GetTempPathW(MAX_PATH, temp_path);
2797 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2799 win_skip("GetTempPathW is not available\n");
2802 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2803 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2805 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
2806 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2808 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2809 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2811 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
2812 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2814 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2815 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2817 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2818 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2819 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2820 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2821 "ReplaceFileW: error %d\n", GetLastError());
2823 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2824 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2825 ret = DeleteFileW(backup);
2826 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
2827 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2828 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2829 "ReplaceFileW: error %d\n", GetLastError());
2831 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2832 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2833 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
2834 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2835 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2837 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2838 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
2839 "ReplaceFileW: unexpected error %d\n", GetLastError());
2840 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
2841 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2842 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2844 removeBackup = TRUE;
2846 ret = DeleteFileW(replaced);
2847 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
2848 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2849 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
2851 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2852 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2853 GetLastError() == ERROR_ACCESS_DENIED),
2854 "ReplaceFileW: unexpected error %d\n", GetLastError());
2858 ret = DeleteFileW(backup);
2860 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2861 "DeleteFileW: error (backup) %d\n", GetLastError());
2867 InitFunctionPointers();
2877 test_GetTempFileNameA();
2886 test_FindFirstFileA();
2887 test_FindNextFileA();
2888 test_FindFirstFileExA(0);
2889 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
2890 test_FindFirstFileExA(FindExSearchLimitToDirectories);
2892 test_file_sharing();
2893 test_offset_in_overlapped_structure();
2896 test_async_file_errors();
2900 test_RemoveDirectory();
2901 test_ReplaceFileA();
2902 test_ReplaceFileW();