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 ";
64 static void InitFunctionPointers(void)
66 HMODULE hkernel32 = GetModuleHandleA("kernel32");
68 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
69 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
70 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
71 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
72 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
75 static void test__hread( void )
84 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
85 DeleteFileA( filename );
86 filehandle = _lcreat( filename, 0 );
87 if (filehandle == HFILE_ERROR)
89 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
93 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
95 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
97 filehandle = _lopen( filename, OF_READ );
99 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
101 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
103 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
105 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
107 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
108 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
109 for (i = 0; i < bytes_wanted; i++)
111 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
115 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
117 ret = DeleteFileA( filename );
118 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
122 static void test__hwrite( void )
131 HLOCAL memory_object;
135 filehandle = _lcreat( filename, 0 );
136 if (filehandle == HFILE_ERROR)
138 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
142 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
144 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
146 filehandle = _lopen( filename, OF_READ );
148 bytes_read = _hread( filehandle, buffer, 1);
150 ok( 0 == bytes_read, "file read size error\n" );
152 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
154 filehandle = _lopen( filename, OF_READWRITE );
158 srand( (unsigned)time( NULL ) );
159 for (blocks = 0; blocks < 100; blocks++)
161 for (i = 0; i < (long)sizeof( buffer ); i++)
164 checksum[0] = checksum[0] + buffer[i];
166 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
167 bytes_written = bytes_written + sizeof( buffer );
170 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
173 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
175 memory_object = LocalAlloc( LPTR, bytes_written );
177 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
179 contents = LocalLock( memory_object );
181 filehandle = _lopen( filename, OF_READ );
183 contents = LocalLock( memory_object );
185 ok( NULL != contents, "LocalLock whines\n" );
187 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
193 checksum[0] = checksum[0] + contents[i];
196 while (i < bytes_written - 1);
198 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
200 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
202 ret = DeleteFileA( filename );
203 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
207 static void test__lclose( void )
212 filehandle = _lcreat( filename, 0 );
213 if (filehandle == HFILE_ERROR)
215 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
219 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
221 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
223 ret = DeleteFileA( filename );
224 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
228 static void test__lcreat( void )
232 WIN32_FIND_DATAA search_results;
233 char slashname[] = "testfi/";
238 filehandle = _lcreat( filename, 0 );
239 if (filehandle == HFILE_ERROR)
241 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
245 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
247 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
249 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
251 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
253 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
255 ret = DeleteFileA(filename);
256 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
258 filehandle = _lcreat( filename, 1 ); /* readonly */
259 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
261 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
263 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
265 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
267 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
269 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
271 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
273 filehandle = _lcreat( filename, 2 );
274 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
276 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
278 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
280 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
282 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
284 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
286 ret = DeleteFileA( filename );
287 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
289 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
290 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
292 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
294 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
296 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
298 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
300 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
302 ret = DeleteFileA( filename );
303 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
305 filehandle=_lcreat (slashname, 0); /* illegal name */
306 if (HFILE_ERROR==filehandle) {
308 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
309 "creating file \"%s\" failed with error %d\n", slashname, err);
310 } else { /* only NT succeeds */
312 find=FindFirstFileA (slashname, &search_results);
313 if (INVALID_HANDLE_VALUE!=find)
315 ret = FindClose (find);
316 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
317 slashname[strlen(slashname)-1]=0;
318 ok (!strcmp (slashname, search_results.cFileName),
319 "found unexpected name \"%s\"\n", search_results.cFileName);
320 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
321 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
322 search_results.dwFileAttributes);
324 ret = DeleteFileA( slashname );
325 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
328 filehandle=_lcreat (filename, 8); /* illegal attribute */
329 if (HFILE_ERROR==filehandle)
330 ok (0, "couldn't create volume label \"%s\"\n", filename);
333 find=FindFirstFileA (filename, &search_results);
334 if (INVALID_HANDLE_VALUE==find)
335 ok (0, "file \"%s\" not found\n", filename);
337 ret = FindClose(find);
338 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
339 ok (!strcmp (filename, search_results.cFileName),
340 "found unexpected name \"%s\"\n", search_results.cFileName);
341 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
342 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
343 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
344 search_results.dwFileAttributes);
346 ret = DeleteFileA( filename );
347 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
352 static void test__llseek( void )
360 filehandle = _lcreat( filename, 0 );
361 if (filehandle == HFILE_ERROR)
363 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
367 for (i = 0; i < 400; i++)
369 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
371 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
372 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
374 bytes_read = _hread( filehandle, buffer, 1);
375 ok( 1 == bytes_read, "file read size error\n" );
376 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
377 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
379 bytes_read = _hread( filehandle, buffer, 1);
380 ok( 1 == bytes_read, "file read size error\n" );
381 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
382 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
383 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
385 ret = DeleteFileA( filename );
386 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
390 static void test__llopen( void )
397 filehandle = _lcreat( filename, 0 );
398 if (filehandle == HFILE_ERROR)
400 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
404 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
405 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
407 filehandle = _lopen( filename, OF_READ );
408 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
409 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
410 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
411 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
413 filehandle = _lopen( filename, OF_READWRITE );
414 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
415 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
416 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
417 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
419 filehandle = _lopen( filename, OF_WRITE );
420 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
421 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
422 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
424 ret = DeleteFileA( filename );
425 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
426 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
430 static void test__lread( void )
439 filehandle = _lcreat( filename, 0 );
440 if (filehandle == HFILE_ERROR)
442 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
446 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
448 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
450 filehandle = _lopen( filename, OF_READ );
452 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
454 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
456 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
458 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
460 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
461 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
462 for (i = 0; i < bytes_wanted; i++)
464 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
468 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
470 ret = DeleteFileA( filename );
471 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
475 static void test__lwrite( void )
484 HLOCAL memory_object;
488 filehandle = _lcreat( filename, 0 );
489 if (filehandle == HFILE_ERROR)
491 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
495 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
497 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
499 filehandle = _lopen( filename, OF_READ );
501 bytes_read = _hread( filehandle, buffer, 1);
503 ok( 0 == bytes_read, "file read size error\n" );
505 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
507 filehandle = _lopen( filename, OF_READWRITE );
511 srand( (unsigned)time( NULL ) );
512 for (blocks = 0; blocks < 100; blocks++)
514 for (i = 0; i < (long)sizeof( buffer ); i++)
517 checksum[0] = checksum[0] + buffer[i];
519 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
520 bytes_written = bytes_written + sizeof( buffer );
523 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
526 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
528 memory_object = LocalAlloc( LPTR, bytes_written );
530 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
532 contents = LocalLock( memory_object );
534 filehandle = _lopen( filename, OF_READ );
536 contents = LocalLock( memory_object );
538 ok( NULL != contents, "LocalLock whines\n" );
540 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
546 checksum[0] += contents[i];
549 while (i < bytes_written - 1);
551 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
553 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
555 ret = DeleteFileA( filename );
556 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
559 static void test_CopyFileA(void)
561 char temp_path[MAX_PATH];
562 char source[MAX_PATH], dest[MAX_PATH];
563 static const char prefix[] = "pfx";
570 ret = GetTempPathA(MAX_PATH, temp_path);
571 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
572 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
574 ret = GetTempFileNameA(temp_path, prefix, 0, source);
575 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
577 ret = MoveFileA(source, source);
578 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
580 /* make the source have not zero size */
581 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
582 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
583 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
584 ok( retok && ret == sizeof(prefix),
585 "WriteFile error %d\n", GetLastError());
586 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
587 /* get the file time and change it to prove the difference */
588 ret = GetFileTime(hfile, NULL, NULL, &ft1);
589 ok( ret, "GetFileTime error %d\n", GetLastError());
590 ft1.dwLowDateTime -= 600000000; /* 60 second */
591 ret = SetFileTime(hfile, NULL, NULL, &ft1);
592 ok( ret, "SetFileTime error %d\n", GetLastError());
593 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
596 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
597 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
599 SetLastError(0xdeadbeef);
600 ret = CopyFileA(source, dest, TRUE);
601 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
602 "CopyFileA: unexpected error %d\n", GetLastError());
604 ret = CopyFileA(source, dest, FALSE);
605 ok(ret, "CopyFileA: error %d\n", GetLastError());
607 /* make sure that destination has correct size */
608 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
609 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
610 ret = GetFileSize(hfile, NULL);
611 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
613 /* make sure that destination has the same filetime */
614 ret = GetFileTime(hfile, NULL, NULL, &ft2);
615 ok( ret, "GetFileTime error %d\n", GetLastError());
616 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
618 SetLastError(0xdeadbeef);
619 ret = CopyFileA(source, dest, FALSE);
620 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
621 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
623 /* make sure that destination still has correct size */
624 ret = GetFileSize(hfile, NULL);
625 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
626 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
627 ok( retok && ret == sizeof(prefix),
628 "ReadFile: error %d\n", GetLastError());
629 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
632 ret = DeleteFileA(source);
633 ok(ret, "DeleteFileA: error %d\n", GetLastError());
634 ret = DeleteFileA(dest);
635 ok(ret, "DeleteFileA: error %d\n", GetLastError());
638 static void test_CopyFileW(void)
640 WCHAR temp_path[MAX_PATH];
641 WCHAR source[MAX_PATH], dest[MAX_PATH];
642 static const WCHAR prefix[] = {'p','f','x',0};
645 ret = GetTempPathW(MAX_PATH, temp_path);
646 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
648 win_skip("GetTempPathW is not available\n");
651 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
652 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
654 ret = GetTempFileNameW(temp_path, prefix, 0, source);
655 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
657 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
658 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
660 ret = CopyFileW(source, dest, TRUE);
661 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
662 "CopyFileW: unexpected error %d\n", GetLastError());
664 ret = CopyFileW(source, dest, FALSE);
665 ok(ret, "CopyFileW: error %d\n", GetLastError());
667 ret = DeleteFileW(source);
668 ok(ret, "DeleteFileW: error %d\n", GetLastError());
669 ret = DeleteFileW(dest);
670 ok(ret, "DeleteFileW: error %d\n", GetLastError());
675 * Debugging routine to dump a buffer in a hexdump-like fashion.
677 static void dumpmem(unsigned char* mem, int len) {
682 for (x=0; x<len; x+=16) {
683 ln += sprintf(buf+ln, "%04x: ",x);
684 for (y=0; y<16; y++) {
686 ln += sprintf(buf+ln, " ");
688 ln += sprintf(buf+ln, "%02hhx ",mem[x+y]);
691 ln += sprintf(buf+ln, "- ");
692 for (y=0; y<16; y++) {
694 if (mem[x+y]<32 || mem[x+y]>127) {
695 ln += sprintf(buf+ln, ".");
697 ln += sprintf(buf+ln, "%c",mem[x+y]);
701 sprintf(buf+ln, "\n");
707 static void test_CreateFileA(void)
710 char temp_path[MAX_PATH], dirname[MAX_PATH];
711 char filename[MAX_PATH];
712 static const char prefix[] = "pfx";
713 char windowsdir[MAX_PATH];
714 char Volume_1[MAX_PATH];
715 unsigned char buffer[512];
716 char directory[] = "removeme";
717 static const char nt_drive[] = "\\\\?\\A:";
719 struct test_list p[] = {
720 {"", ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
721 {"", ERROR_SUCCESS, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
722 {"a", ERROR_FILE_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
723 {"a\\", ERROR_FILE_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
724 {"removeme", ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
725 {"removeme\\", ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
726 {"c:", ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
727 {"c:", ERROR_SUCCESS, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
728 {"c:\\", ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
729 {"c:\\", ERROR_SUCCESS, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
730 {"\\\\?\\c:", ERROR_SUCCESS, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
731 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
734 BY_HANDLE_FILE_INFORMATION Finfo;
736 ret = GetTempPathA(MAX_PATH, temp_path);
737 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
738 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
740 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
741 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
743 SetLastError(0xdeadbeef);
744 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
745 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
746 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
747 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
749 SetLastError(0xdeadbeef);
750 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
751 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
752 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
753 "hFile %p, last error %u\n", hFile, GetLastError());
757 SetLastError(0xdeadbeef);
758 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
759 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
760 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
761 "hFile %p, last error %u\n", hFile, GetLastError());
765 ret = DeleteFileA(filename);
766 ok(ret, "DeleteFileA: error %d\n", GetLastError());
768 SetLastError(0xdeadbeef);
769 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
770 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
771 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
772 "hFile %p, last error %u\n", hFile, GetLastError());
776 ret = DeleteFileA(filename);
777 ok(ret, "DeleteFileA: error %d\n", GetLastError());
779 /* get windows drive letter */
780 ret = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
781 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
782 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
784 /* test error return codes from CreateFile for some cases */
785 ret = GetTempPathA(MAX_PATH, temp_path);
786 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
787 strcpy(dirname, temp_path);
788 strcat(dirname, directory);
789 ret = CreateDirectory(dirname, NULL);
790 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
791 /* set current drive & directory to known location */
792 SetCurrentDirectoryA( temp_path );
797 /* update the drive id in the table entry with the current one */
798 if (p[i].file[1] == ':')
800 strcpy(filename, p[i].file);
801 filename[0] = windowsdir[0];
803 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
805 strcpy(filename, p[i].file);
806 filename[4] = windowsdir[0];
810 /* prefix the table entry with the current temp directory */
811 strcpy(filename, temp_path);
812 strcat(filename, p[i].file);
814 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
815 FILE_SHARE_READ | FILE_SHARE_WRITE,
817 p[i].options, NULL );
818 /* if we get ACCESS_DENIED when we do not expect it, assume
819 * no access to the volume
821 if (hFile == INVALID_HANDLE_VALUE &&
822 GetLastError() == ERROR_ACCESS_DENIED &&
823 p[i].err != ERROR_ACCESS_DENIED)
826 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
828 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
830 /* otherwise validate results with expectations */
831 else if (p[i].todo_flag)
832 todo_wine ok((hFile == INVALID_HANDLE_VALUE && p[i].err == GetLastError()) ||
833 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
834 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
835 filename, hFile, GetLastError(), p[i].err);
837 ok((hFile == INVALID_HANDLE_VALUE && p[i].err == GetLastError()) ||
838 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
839 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
840 filename, hFile, GetLastError(), p[i].err);
841 if (hFile != INVALID_HANDLE_VALUE)
842 CloseHandle( hFile );
845 ret = RemoveDirectoryA(dirname);
846 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
849 /* test opening directory as a directory */
850 hFile = CreateFileA( temp_path, GENERIC_READ,
854 FILE_FLAG_BACKUP_SEMANTICS, NULL );
855 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
856 "CreateFileA did not work, last error %u on volume <%s>\n",
857 GetLastError(), temp_path );
859 if (hFile != INVALID_HANDLE_VALUE)
861 ret = GetFileInformationByHandle( hFile, &Finfo );
864 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
865 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
866 temp_path, Finfo.dwFileAttributes);
868 CloseHandle( hFile );
872 /* *** Test opening volumes/devices using drive letter *** */
874 /* test using drive letter in non-rewrite format without trailing \ */
875 /* this should work */
876 strcpy(filename, nt_drive);
877 filename[4] = windowsdir[0];
878 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
879 FILE_SHARE_READ | FILE_SHARE_WRITE,
881 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
882 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
884 /* if we have adm rights to volume, then try rest of tests */
885 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
886 filename, GetLastError());
887 if (hFile != INVALID_HANDLE_VALUE)
889 /* if we opened the volume/device, try to read it. Since it */
890 /* opened, we should be able to read it. We don't care about*/
891 /* what the data is at this time. */
893 ret = ReadFile( hFile, buffer, len, &len, NULL );
894 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
895 GetLastError(), ret, filename);
898 trace("buffer is\n");
901 CloseHandle( hFile );
904 /* test using drive letter with trailing \ and in non-rewrite */
905 /* this should not work */
906 strcpy(filename, nt_drive);
907 filename[4] = windowsdir[0];
908 strcat( filename, "\\" );
909 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
910 FILE_SHARE_READ | FILE_SHARE_WRITE,
912 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
914 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
915 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
916 filename, GetLastError());
917 if (hFile != INVALID_HANDLE_VALUE)
918 CloseHandle( hFile );
920 /* test using temp path with trailing \ and in non-rewrite as dir */
921 /* this should work */
922 strcpy(filename, nt_drive);
924 strcat( filename, temp_path );
925 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
926 FILE_SHARE_READ | FILE_SHARE_WRITE,
928 FILE_FLAG_BACKUP_SEMANTICS, NULL );
929 ok(hFile != INVALID_HANDLE_VALUE,
930 "CreateFileA should have worked on %s, but got %u\n",
931 filename, GetLastError());
932 if (hFile != INVALID_HANDLE_VALUE)
933 CloseHandle( hFile );
935 /* test using drive letter without trailing \ and in device ns */
936 /* this should work */
937 strcpy(filename, nt_drive);
938 filename[4] = windowsdir[0];
940 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
941 FILE_SHARE_READ | FILE_SHARE_WRITE,
943 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
944 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
945 filename, GetLastError());
946 if (hFile != INVALID_HANDLE_VALUE)
947 CloseHandle( hFile );
950 skip("Do not have authority to access volumes. Tests skipped\n");
953 /* *** Test opening volumes/devices using GUID *** */
955 if (pGetVolumeNameForVolumeMountPointA)
957 strcpy(filename, "c:\\");
958 filename[0] = windowsdir[0];
959 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
960 ok(ret == TRUE, "GetVolumeNameForVolumeMountPointA failed\n");
961 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name %s\n", Volume_1);
963 /* test the result of opening a unique volume name (GUID) */
964 /* with the trailing \ */
965 /* this should error out */
966 strcpy(filename, Volume_1);
967 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
968 FILE_SHARE_READ | FILE_SHARE_WRITE,
970 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
972 ok(hFile == INVALID_HANDLE_VALUE,
973 "CreateFileA should not have opened %s, hFile %p\n",
976 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
977 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
978 filename, GetLastError());
979 if (hFile != INVALID_HANDLE_VALUE)
980 CloseHandle( hFile );
982 /* test the result of opening a unique volume name (GUID) */
983 /* with the temp path string as dir */
984 /* this should work */
985 strcpy(filename, Volume_1);
986 strcat(filename, temp_path+3);
987 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
988 FILE_SHARE_READ | FILE_SHARE_WRITE,
990 FILE_FLAG_BACKUP_SEMANTICS, NULL );
992 ok(hFile != INVALID_HANDLE_VALUE,
993 "CreateFileA should have opened %s, but got %u\n",
994 filename, GetLastError());
995 if (hFile != INVALID_HANDLE_VALUE)
996 CloseHandle( hFile );
998 /* test the result of opening a unique volume name (GUID) */
999 /* without the trailing \ and in device namespace */
1000 /* this should work */
1001 strcpy(filename, Volume_1);
1004 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1005 FILE_SHARE_READ | FILE_SHARE_WRITE,
1006 NULL, OPEN_EXISTING,
1007 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1008 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1010 /* if we have adm rights to volume, then try rest of tests */
1011 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1012 filename, GetLastError());
1013 if (hFile != INVALID_HANDLE_VALUE)
1015 /* if we opened the volume/device, try to read it. Since it */
1016 /* opened, we should be able to read it. We don't care about*/
1017 /* what the data is at this time. */
1019 ret = ReadFile( hFile, buffer, len, &len, NULL );
1020 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1021 GetLastError(), ret, filename);
1024 trace("buffer is\n");
1025 dumpmem(buffer, 64);
1027 CloseHandle( hFile );
1031 skip("Do not have authority to access volumes. Tests skipped\n");
1034 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1037 static void test_CreateFileW(void)
1040 WCHAR temp_path[MAX_PATH];
1041 WCHAR filename[MAX_PATH];
1042 static const WCHAR emptyW[]={'\0'};
1043 static const WCHAR prefix[] = {'p','f','x',0};
1044 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1047 ret = GetTempPathW(MAX_PATH, temp_path);
1048 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1050 win_skip("GetTempPathW is not available\n");
1053 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1054 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1056 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1057 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1059 SetLastError(0xdeadbeef);
1060 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1061 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1062 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1063 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1065 SetLastError(0xdeadbeef);
1066 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1067 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1068 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1069 "hFile %p, last error %u\n", hFile, GetLastError());
1073 SetLastError(0xdeadbeef);
1074 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1075 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1076 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1077 "hFile %p, last error %u\n", hFile, GetLastError());
1081 ret = DeleteFileW(filename);
1082 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1084 SetLastError(0xdeadbeef);
1085 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1086 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1087 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1088 "hFile %p, last error %u\n", hFile, GetLastError());
1092 ret = DeleteFileW(filename);
1093 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1097 /* this crashes on NT4.0 */
1098 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1099 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1100 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1101 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1104 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1105 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1106 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1107 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1109 /* test the result of opening a nonexistent driver name */
1110 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1111 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1112 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1113 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1115 ret = CreateDirectoryW(filename, NULL);
1116 ok(ret == TRUE, "couldn't create temporary directory\n");
1117 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1118 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1119 ok(hFile != INVALID_HANDLE_VALUE,
1120 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1122 ret = RemoveDirectoryW(filename);
1123 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1126 static void test_GetTempFileNameA(void)
1130 char expected[MAX_PATH + 10];
1131 char windowsdir[MAX_PATH + 10];
1132 char windowsdrive[3];
1134 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1135 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1136 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1138 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1139 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1141 strcat(windowsdir, "\\");
1144 windowsdrive[0] = windowsdir[0];
1145 windowsdrive[1] = windowsdir[1];
1146 windowsdrive[2] = '\0';
1148 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1149 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1150 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1151 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1152 windowsdrive[0], out);
1154 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1155 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1157 strcat(expected, windowsdir);
1158 strcat(expected, "abc2.tmp");
1159 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1163 static void test_DeleteFileA( void )
1167 ret = DeleteFileA(NULL);
1168 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1169 GetLastError() == ERROR_PATH_NOT_FOUND),
1170 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1172 ret = DeleteFileA("");
1173 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1174 GetLastError() == ERROR_BAD_PATHNAME),
1175 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1177 ret = DeleteFileA("nul");
1178 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1179 GetLastError() == ERROR_INVALID_PARAMETER ||
1180 GetLastError() == ERROR_ACCESS_DENIED ||
1181 GetLastError() == ERROR_INVALID_FUNCTION),
1182 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1185 static void test_DeleteFileW( void )
1188 WCHAR pathW[MAX_PATH];
1189 WCHAR pathsubW[MAX_PATH];
1190 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1191 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1192 static const WCHAR emptyW[]={'\0'};
1194 ret = DeleteFileW(NULL);
1195 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1197 win_skip("DeleteFileW is not available\n");
1200 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1201 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1203 ret = DeleteFileW(emptyW);
1204 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1205 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1207 /* test DeleteFile on empty directory */
1208 ret = GetTempPathW(MAX_PATH, pathW);
1209 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1211 ok(0, "MAX_PATH exceeded in constructing paths\n");
1214 lstrcatW(pathW, dirW);
1215 lstrcpyW(pathsubW, pathW);
1216 lstrcatW(pathsubW, subdirW);
1217 ret = CreateDirectoryW(pathW, NULL);
1218 ok(ret == TRUE, "couldn't create directory deletefile\n");
1219 ret = DeleteFileW(pathW);
1220 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1221 ret = RemoveDirectoryW(pathW);
1222 ok(ret == TRUE, "expected to remove directory deletefile\n");
1224 /* test DeleteFile on non-empty directory */
1225 ret = CreateDirectoryW(pathW, NULL);
1226 ok(ret == TRUE, "couldn't create directory deletefile\n");
1227 ret = CreateDirectoryW(pathsubW, NULL);
1228 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1229 ret = DeleteFileW(pathW);
1230 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1231 ret = RemoveDirectoryW(pathsubW);
1232 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1233 ret = RemoveDirectoryW(pathW);
1234 ok(ret == TRUE, "expected to remove directory deletefile\n");
1237 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1239 static void test_MoveFileA(void)
1241 char tempdir[MAX_PATH];
1242 char source[MAX_PATH], dest[MAX_PATH];
1243 static const char prefix[] = "pfx";
1246 ret = GetTempPathA(MAX_PATH, tempdir);
1247 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1248 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1250 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1251 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1253 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1254 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1256 ret = MoveFileA(source, dest);
1257 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1258 "MoveFileA: unexpected error %d\n", GetLastError());
1260 ret = DeleteFileA(dest);
1261 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1263 ret = MoveFileA(source, dest);
1264 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1266 lstrcatA(tempdir, "Remove Me");
1267 ret = CreateDirectoryA(tempdir, NULL);
1268 ok(ret == TRUE, "CreateDirectoryA failed\n");
1270 lstrcpyA(source, dest);
1271 lstrcpyA(dest, tempdir);
1272 lstrcatA(dest, "\\wild?.*");
1273 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1274 ret = MoveFileA(source, dest);
1275 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1276 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1277 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1278 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1279 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1281 WIN32_FIND_DATAA fd;
1282 char temppath[MAX_PATH];
1285 lstrcpyA(temppath, tempdir);
1286 lstrcatA(temppath, "\\*.*");
1287 hFind = FindFirstFileA(temppath, &fd);
1288 if (INVALID_HANDLE_VALUE != hFind)
1293 lpName = fd.cAlternateFileName;
1295 lpName = fd.cFileName;
1296 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1298 while (FindNextFileA(hFind, &fd));
1302 ret = DeleteFileA(source);
1303 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1304 ret = DeleteFileA(dest);
1305 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1306 ret = RemoveDirectoryA(tempdir);
1307 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1310 static void test_MoveFileW(void)
1312 WCHAR temp_path[MAX_PATH];
1313 WCHAR source[MAX_PATH], dest[MAX_PATH];
1314 static const WCHAR prefix[] = {'p','f','x',0};
1317 ret = GetTempPathW(MAX_PATH, temp_path);
1318 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1320 win_skip("GetTempPathW is not available\n");
1323 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1324 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1326 ret = GetTempFileNameW(temp_path, prefix, 0, source);
1327 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1329 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
1330 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1332 ret = MoveFileW(source, dest);
1333 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1334 "CopyFileW: unexpected error %d\n", GetLastError());
1336 ret = DeleteFileW(source);
1337 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1338 ret = DeleteFileW(dest);
1339 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1342 #define PATTERN_OFFSET 0x10
1344 static void test_offset_in_overlapped_structure(void)
1350 BYTE buf[256], pattern[] = "TeSt";
1352 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
1355 ret =GetTempPathA(MAX_PATH, temp_path);
1356 ok( ret, "GetTempPathA error %d\n", GetLastError());
1357 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
1358 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
1360 /*** Write File *****************************************************/
1362 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1363 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1365 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
1366 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
1367 ok( ret, "WriteFile error %d\n", GetLastError());
1368 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
1370 memset(&ov, 0, sizeof(ov));
1371 S(U(ov)).Offset = PATTERN_OFFSET;
1372 S(U(ov)).OffsetHigh = 0;
1373 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1374 /* Win 9x does not support the overlapped I/O on files */
1375 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1376 ok(rc, "WriteFile error %d\n", GetLastError());
1377 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1378 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1379 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1381 S(U(ov)).Offset = sizeof(buf) * 2;
1382 S(U(ov)).OffsetHigh = 0;
1383 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1384 ok( ret, "WriteFile error %d\n", GetLastError());
1385 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1386 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1387 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1392 /*** Read File *****************************************************/
1394 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1395 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1397 memset(buf, 0, sizeof(buf));
1398 memset(&ov, 0, sizeof(ov));
1399 S(U(ov)).Offset = PATTERN_OFFSET;
1400 S(U(ov)).OffsetHigh = 0;
1401 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1402 /* Win 9x does not support the overlapped I/O on files */
1403 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1404 ok(rc, "ReadFile error %d\n", GetLastError());
1405 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1406 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1407 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1408 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1413 ret = DeleteFileA(temp_fname);
1414 ok( ret, "DeleteFileA error %d\n", GetLastError());
1417 static void test_LockFile(void)
1421 OVERLAPPED overlapped;
1422 int limited_LockFile;
1423 int limited_UnLockFile;
1426 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1427 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1428 CREATE_ALWAYS, 0, 0 );
1429 if (handle == INVALID_HANDLE_VALUE)
1431 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1434 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1436 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1437 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1439 limited_UnLockFile = 0;
1440 if (UnlockFile( handle, 0, 0, 0, 0 ))
1442 limited_UnLockFile = 1;
1445 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1446 /* overlapping locks must fail */
1447 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1448 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1449 /* non-overlapping locks must succeed */
1450 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1452 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1453 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1454 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1455 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1457 S(U(overlapped)).Offset = 100;
1458 S(U(overlapped)).OffsetHigh = 0;
1459 overlapped.hEvent = 0;
1461 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1462 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1464 /* LockFileEx is probably OK, test it more. */
1465 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1466 "LockFileEx 100,100 failed\n" );
1469 /* overlapping shared locks are OK */
1470 S(U(overlapped)).Offset = 150;
1471 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1473 /* but exclusive is not */
1474 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1475 0, 50, 0, &overlapped ),
1476 "LockFileEx exclusive 150,50 succeeded\n" );
1477 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1478 { /* UnLockFile is capable. */
1479 S(U(overlapped)).Offset = 100;
1480 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1481 "UnlockFileEx 150,100 again succeeded\n" );
1484 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
1487 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1488 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1489 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1492 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
1494 /* wrap-around lock should not do anything */
1495 /* (but still succeeds on NT4 so we don't check result) */
1496 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1498 limited_LockFile = 0;
1499 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1501 limited_LockFile = 1;
1504 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1506 /* zero-byte lock */
1507 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1508 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1509 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1510 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1512 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1513 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1515 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1517 CloseHandle( handle );
1518 DeleteFileA( filename );
1521 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1525 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1526 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1531 if (!access1) access1 = GENERIC_READ;
1534 if (!access2) access2 = GENERIC_READ;
1537 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1538 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1539 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1540 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1541 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1542 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1546 static void test_file_sharing(void)
1548 static const DWORD access_modes[] =
1549 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1550 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1551 static const DWORD sharing_modes[] =
1552 { 0, FILE_SHARE_READ,
1553 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1554 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1555 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1559 BOOL is_win9x = FALSE;
1561 /* make sure the file exists */
1562 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1563 if (h == INVALID_HANDLE_VALUE)
1565 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1568 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1571 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1573 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1575 /* Win9x doesn't support FILE_SHARE_DELETE */
1576 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1579 SetLastError(0xdeadbeef);
1580 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1581 NULL, OPEN_EXISTING, 0, 0 );
1582 if (h == INVALID_HANDLE_VALUE)
1584 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1587 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1589 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1591 /* Win9x doesn't support FILE_SHARE_DELETE */
1592 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1595 SetLastError(0xdeadbeef);
1596 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1597 NULL, OPEN_EXISTING, 0, 0 );
1599 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1600 access_modes[a2], sharing_modes[s2], is_win9x ))
1602 ret = GetLastError();
1604 ok( h2 != INVALID_HANDLE_VALUE,
1605 "open failed for modes %x/%x/%x/%x\n",
1606 access_modes[a1], sharing_modes[s1],
1607 access_modes[a2], sharing_modes[s2] );
1608 ok( ret == 0xdeadbeef /* Win9x */ ||
1610 "wrong error code %d\n", ret );
1616 ret = GetLastError();
1618 ok( h2 == INVALID_HANDLE_VALUE,
1619 "open succeeded for modes %x/%x/%x/%x\n",
1620 access_modes[a1], sharing_modes[s1],
1621 access_modes[a2], sharing_modes[s2] );
1622 ok( ret == ERROR_SHARING_VIOLATION,
1623 "wrong error code %d\n", ret );
1631 SetLastError(0xdeadbeef);
1632 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1633 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1635 SetLastError(0xdeadbeef);
1636 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1637 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1638 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1640 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1641 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1646 DeleteFileA( filename );
1649 static char get_windows_drive(void)
1651 char windowsdir[MAX_PATH];
1652 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1653 return windowsdir[0];
1656 static void test_FindFirstFileA(void)
1659 WIN32_FIND_DATAA data;
1661 char buffer[5] = "C:\\";
1663 char nonexistent[MAX_PATH];
1665 /* try FindFirstFileA on "C:\" */
1666 buffer[0] = get_windows_drive();
1668 SetLastError( 0xdeadbeaf );
1669 handle = FindFirstFileA(buffer, &data);
1670 err = GetLastError();
1671 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1672 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1674 /* try FindFirstFileA on "C:\*" */
1675 strcpy(buffer2, buffer);
1676 strcat(buffer2, "*");
1677 handle = FindFirstFileA(buffer2, &data);
1678 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1679 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1680 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1681 if (FindNextFileA( handle, &data ))
1682 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1683 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1684 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1686 /* try FindFirstFileA on windows dir */
1687 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1688 strcat(buffer2, "\\*");
1689 handle = FindFirstFileA(buffer2, &data);
1690 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1691 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1692 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1693 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1694 while (FindNextFileA( handle, &data ))
1695 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1696 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1697 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1699 /* try FindFirstFileA on "C:\foo\" */
1700 SetLastError( 0xdeadbeaf );
1701 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
1704 GetTempPathA( sizeof(tmp), tmp );
1705 GetTempFileNameA( tmp, "foo", 0, nonexistent );
1707 DeleteFileA( nonexistent );
1708 strcpy(buffer2, nonexistent);
1709 strcat(buffer2, "\\");
1710 handle = FindFirstFileA(buffer2, &data);
1711 err = GetLastError();
1712 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1714 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1717 /* try FindFirstFileA on "C:\foo\bar.txt" */
1718 SetLastError( 0xdeadbeaf );
1719 strcpy(buffer2, nonexistent);
1720 strcat(buffer2, "\\bar.txt");
1721 handle = FindFirstFileA(buffer2, &data);
1722 err = GetLastError();
1723 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1724 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1726 /* try FindFirstFileA on "C:\foo\*.*" */
1727 SetLastError( 0xdeadbeaf );
1728 strcpy(buffer2, nonexistent);
1729 strcat(buffer2, "\\*.*");
1730 handle = FindFirstFileA(buffer2, &data);
1731 err = GetLastError();
1732 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1733 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1735 /* try FindFirstFileA on "foo\bar.txt" */
1736 SetLastError( 0xdeadbeaf );
1737 strcpy(buffer2, nonexistent + 3);
1738 strcat(buffer2, "\\bar.txt");
1739 handle = FindFirstFileA(buffer2, &data);
1740 err = GetLastError();
1741 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1742 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1744 /* try FindFirstFileA on "c:\nul" */
1745 SetLastError( 0xdeadbeaf );
1746 strcpy(buffer2, buffer);
1747 strcat(buffer2, "nul");
1748 handle = FindFirstFileA(buffer2, &data);
1749 err = GetLastError();
1750 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1751 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1752 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1753 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1754 "wrong attributes %x\n", data.dwFileAttributes );
1755 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
1757 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1758 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1760 SetLastError( 0xdeadbeaf );
1761 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1762 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1763 ok( FindClose( handle ), "failed to close handle\n" );
1765 /* try FindFirstFileA on "lpt1" */
1766 SetLastError( 0xdeadbeaf );
1767 strcpy(buffer2, "lpt1");
1768 handle = FindFirstFileA(buffer2, &data);
1769 err = GetLastError();
1770 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1771 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1772 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1773 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1774 "wrong attributes %x\n", data.dwFileAttributes );
1775 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
1777 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1778 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1780 SetLastError( 0xdeadbeaf );
1781 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1782 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1783 ok( FindClose( handle ), "failed to close handle\n" );
1785 /* try FindFirstFileA on "c:\nul\*" */
1786 SetLastError( 0xdeadbeaf );
1787 strcpy(buffer2, buffer);
1788 strcat(buffer2, "nul\\*");
1789 handle = FindFirstFileA(buffer2, &data);
1790 err = GetLastError();
1791 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1792 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1794 /* try FindFirstFileA on "c:\nul*" */
1795 SetLastError( 0xdeadbeaf );
1796 strcpy(buffer2, buffer);
1797 strcat(buffer2, "nul*");
1798 handle = FindFirstFileA(buffer2, &data);
1799 err = GetLastError();
1800 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1801 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1803 /* try FindFirstFileA on "c:\foo\bar\nul" */
1804 SetLastError( 0xdeadbeaf );
1805 strcpy(buffer2, buffer);
1806 strcat(buffer2, "foo\\bar\\nul");
1807 handle = FindFirstFileA(buffer2, &data);
1808 err = GetLastError();
1809 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1810 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1812 /* try FindFirstFileA on "c:\foo\nul\bar" */
1813 SetLastError( 0xdeadbeaf );
1814 strcpy(buffer2, buffer);
1815 strcat(buffer2, "foo\\nul\\bar");
1816 handle = FindFirstFileA(buffer2, &data);
1817 err = GetLastError();
1818 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1819 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1822 static void test_FindNextFileA(void)
1825 WIN32_FIND_DATAA search_results;
1827 char buffer[5] = "C:\\*";
1829 buffer[0] = get_windows_drive();
1830 handle = FindFirstFileA(buffer,&search_results);
1831 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1832 while (FindNextFile(handle, &search_results))
1834 /* get to the end of the files */
1836 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1837 err = GetLastError();
1838 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1841 static void test_FindFirstFileExA(void)
1843 WIN32_FIND_DATAA search_results;
1846 if (!pFindFirstFileExA)
1848 win_skip("FindFirstFileExA() is missing\n");
1852 CreateDirectoryA("test-dir", NULL);
1853 _lclose(_lcreat("test-dir\\file1", 0));
1854 _lclose(_lcreat("test-dir\\file2", 0));
1855 CreateDirectoryA("test-dir\\dir1", NULL);
1856 /* FindExLimitToDirectories is ignored */
1857 SetLastError(0xdeadbeef);
1858 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, FindExSearchLimitToDirectories, NULL, 0);
1859 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1861 win_skip("FindFirstFileExA is not implemented\n");
1864 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1865 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1867 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1869 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1870 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1872 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1873 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
1875 ok(FindNextFile(handle, &search_results), "Fetching fourth file failed\n");
1876 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1878 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1879 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1883 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should failed\n");
1885 FindClose( handle );
1888 DeleteFileA("test-dir\\file1");
1889 DeleteFileA("test-dir\\file2");
1890 RemoveDirectoryA("test-dir\\dir1");
1891 RemoveDirectoryA("test-dir");
1894 static int test_Mapfile_createtemp(HANDLE *handle)
1896 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1897 DeleteFile(filename);
1898 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1899 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1900 if (*handle != INVALID_HANDLE_VALUE) {
1908 static void test_MapFile(void)
1913 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1915 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1916 ok( hmap != NULL, "mapping should work, I named it!\n" );
1918 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1920 /* We have to close file before we try new stuff with mapping again.
1921 Else we would always succeed on XP or block descriptors on 95. */
1922 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1923 ok( hmap != NULL, "We should still be able to map!\n" );
1924 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1925 ok( CloseHandle( handle ), "can't close file handle\n");
1928 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1930 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1931 ok( hmap == NULL, "mapped zero size file\n");
1932 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1934 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1935 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
1936 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1938 CloseHandle( hmap );
1940 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1941 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
1942 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1944 CloseHandle( hmap );
1946 /* On XP you can now map again, on Win 95 you cannot. */
1948 ok( CloseHandle( handle ), "can't close file handle\n");
1949 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1952 static void test_GetFileType(void)
1955 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1956 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1957 type = GetFileType(h);
1958 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1960 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1961 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1962 type = GetFileType(h);
1963 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1965 DeleteFileA( filename );
1968 static int completion_count;
1970 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1972 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1973 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1977 static void test_async_file_errors(void)
1979 char szFile[MAX_PATH];
1980 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1982 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1984 S(U(ovl)).Offset = 0;
1985 S(U(ovl)).OffsetHigh = 0;
1987 completion_count = 0;
1989 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1990 strcat(szFile, "\\win.ini");
1991 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1992 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1993 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
1994 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1995 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1996 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2001 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2003 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2004 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2007 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2009 S(U(ovl)).Offset += count;
2010 /* i/o completion routine only called if ReadFileEx returned success.
2011 * we only care about violations of this rule so undo what should have
2015 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2016 /*printf("Error = %ld\n", GetLastError());*/
2017 HeapFree(GetProcessHeap(), 0, lpBuffer);
2020 static void test_read_write(void)
2022 DWORD bytes, ret, old_prot;
2024 char temp_path[MAX_PATH];
2025 char filename[MAX_PATH];
2027 static const char prefix[] = "pfx";
2029 ret = GetTempPathA(MAX_PATH, temp_path);
2030 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2031 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2033 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2034 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2036 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2037 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2038 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2040 SetLastError(12345678);
2042 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2043 ok(ret && GetLastError() == 12345678,
2044 "ret = %d, error %d\n", ret, GetLastError());
2045 ok(!bytes, "bytes = %d\n", bytes);
2047 SetLastError(12345678);
2049 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2050 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2051 (ret && GetLastError() == 12345678), /* Win9x */
2052 "ret = %d, error %d\n", ret, GetLastError());
2053 ok(!bytes || /* Win2k */
2054 bytes == 10, /* Win9x */
2055 "bytes = %d\n", bytes);
2057 /* make sure the file contains data */
2058 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2059 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
2061 SetLastError(12345678);
2063 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
2064 ok(ret && GetLastError() == 12345678,
2065 "ret = %d, error %d\n", ret, GetLastError());
2066 ok(!bytes, "bytes = %d\n", bytes);
2068 SetLastError(12345678);
2070 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
2071 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
2072 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
2073 "ret = %d, error %d\n", ret, GetLastError());
2074 ok(!bytes, "bytes = %d\n", bytes);
2076 /* test passing protected memory as buffer */
2078 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
2079 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
2081 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2082 ok( ret, "WriteFile failed error %u\n", GetLastError() );
2083 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
2085 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
2086 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2088 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2089 ok( !ret, "WriteFile succeeded\n" );
2090 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2091 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2092 "wrong error %u\n", GetLastError() );
2093 ok( bytes == 0, "wrote %x bytes\n", bytes );
2095 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
2096 ok( !ret, "WriteFile succeeded\n" );
2097 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
2098 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2099 "wrong error %u\n", GetLastError() );
2100 ok( bytes == 0, "wrote %x bytes\n", bytes );
2102 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
2103 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2105 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2106 ok( !ret, "WriteFile succeeded\n" );
2107 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2108 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2109 "wrong error %u\n", GetLastError() );
2110 ok( bytes == 0, "wrote %x bytes\n", bytes );
2112 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2114 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2115 ok( !ret, "ReadFile succeeded\n" );
2116 ok( GetLastError() == ERROR_NOACCESS ||
2117 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2118 "wrong error %u\n", GetLastError() );
2119 ok( bytes == 0, "read %x bytes\n", bytes );
2121 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
2122 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2124 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2125 ok( !ret, "ReadFile succeeded\n" );
2126 ok( GetLastError() == ERROR_NOACCESS ||
2127 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2128 "wrong error %u\n", GetLastError() );
2129 ok( bytes == 0, "read %x bytes\n", bytes );
2131 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
2132 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2134 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2135 ok( !ret, "ReadFile succeeded\n" );
2136 ok( GetLastError() == ERROR_NOACCESS ||
2137 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2138 "wrong error %u\n", GetLastError() );
2139 ok( bytes == 0, "read %x bytes\n", bytes );
2141 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
2142 SetEndOfFile( hFile );
2143 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2145 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2146 ok( !ret, "ReadFile succeeded\n" );
2147 ok( GetLastError() == ERROR_NOACCESS ||
2148 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2149 "wrong error %u\n", GetLastError() );
2150 ok( bytes == 0, "read %x bytes\n", bytes );
2152 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
2153 ok( ret, "ReadFile failed error %u\n", GetLastError() );
2154 ok( bytes == 0x1234, "read %x bytes\n", bytes );
2156 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
2157 ok( !ret, "ReadFile succeeded\n" );
2158 ok( GetLastError() == ERROR_NOACCESS ||
2159 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2160 "wrong error %u\n", GetLastError() );
2161 ok( bytes == 0, "read %x bytes\n", bytes );
2163 VirtualFree( mem, 0, MEM_FREE );
2165 ret = CloseHandle(hFile);
2166 ok( ret, "CloseHandle: error %d\n", GetLastError());
2167 ret = DeleteFileA(filename);
2168 ok( ret, "DeleteFileA: error %d\n", GetLastError());
2171 static void test_OpenFile(void)
2178 static const char file[] = "regedit.exe";
2179 static const char foo[] = ".\\foo-bar-foo.baz";
2180 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
2181 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2182 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2183 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2184 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2185 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2186 char buff[MAX_PATH];
2187 char buff_long[4*MAX_PATH];
2188 char filled_0xA5[OFS_MAXPATHNAME];
2192 /* Check for existing file */
2193 if (!pGetSystemWindowsDirectoryA)
2194 length = GetWindowsDirectoryA(buff, MAX_PATH);
2196 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
2198 if (length + sizeof(file) < MAX_PATH)
2200 p = buff + strlen(buff);
2201 if (p > buff && p[-1] != '\\') *p++ = '\\';
2203 memset(&ofs, 0xA5, sizeof(ofs));
2204 SetLastError(0xfaceabee);
2206 hFile = OpenFile(buff, &ofs, OF_EXIST);
2207 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
2208 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2209 "GetLastError() returns %d\n", GetLastError() );
2210 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2211 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2212 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2213 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2214 ofs.szPathName, buff );
2217 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
2218 length = GetCurrentDirectoryA(MAX_PATH, buff);
2220 /* Check for nonexistent file */
2221 if (length + sizeof(foo) < MAX_PATH)
2223 p = buff + strlen(buff);
2224 if (p > buff && p[-1] != '\\') *p++ = '\\';
2225 strcpy( p, foo + 2 );
2226 memset(&ofs, 0xA5, sizeof(ofs));
2227 SetLastError(0xfaceabee);
2229 hFile = OpenFile(foo, &ofs, OF_EXIST);
2230 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2231 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
2233 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2234 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2235 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2236 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2237 ofs.szPathName, buff );
2240 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
2241 length += lstrlenA(foo_too_long + 1);
2243 /* Check for nonexistent file with too long filename */
2244 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
2246 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
2247 memset(&ofs, 0xA5, sizeof(ofs));
2248 SetLastError(0xfaceabee);
2250 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
2251 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2252 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
2253 "GetLastError() returns %d\n", GetLastError() );
2255 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2256 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
2257 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2258 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2259 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2263 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
2265 if (length >= MAX_PATH)
2267 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
2270 p = buff + strlen(buff);
2271 if (p > buff && p[-1] != '\\') *p++ = '\\';
2272 strcpy( p, filename );
2274 memset(&ofs, 0xA5, sizeof(ofs));
2275 SetLastError(0xfaceabee);
2276 /* Create an empty file */
2277 hFile = OpenFile(filename, &ofs, OF_CREATE);
2278 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
2279 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2280 "GetLastError() returns %d\n", GetLastError() );
2281 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2282 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2283 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2284 ret = _lclose(hFile);
2285 ok( !ret, "_lclose() returns %d\n", ret );
2286 retval = GetFileAttributesA(filename);
2287 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
2289 memset(&ofs, 0xA5, sizeof(ofs));
2290 SetLastError(0xfaceabee);
2291 /* Check various opening options: */
2292 /* for reading only, */
2293 hFile = OpenFile(filename, &ofs, OF_READ);
2294 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
2295 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2296 "GetLastError() returns %d\n", GetLastError() );
2297 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2298 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2299 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2300 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2301 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2302 ret = _lclose(hFile);
2303 ok( !ret, "_lclose() returns %d\n", ret );
2305 memset(&ofs, 0xA5, sizeof(ofs));
2306 SetLastError(0xfaceabee);
2307 /* for writing only, */
2308 hFile = OpenFile(filename, &ofs, OF_WRITE);
2309 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
2310 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2311 "GetLastError() returns %d\n", GetLastError() );
2312 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2313 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2314 "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'\n", ofs.szPathName, buff );
2317 ret = _lclose(hFile);
2318 ok( !ret, "_lclose() returns %d\n", ret );
2320 memset(&ofs, 0xA5, sizeof(ofs));
2321 SetLastError(0xfaceabee);
2322 /* for reading and writing, */
2323 hFile = OpenFile(filename, &ofs, OF_READWRITE);
2324 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
2325 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2326 "GetLastError() returns %d\n", GetLastError() );
2327 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2328 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2329 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2330 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2331 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2332 ret = _lclose(hFile);
2333 ok( !ret, "_lclose() returns %d\n", ret );
2335 memset(&ofs, 0xA5, sizeof(ofs));
2336 SetLastError(0xfaceabee);
2337 /* for checking file presence. */
2338 hFile = OpenFile(filename, &ofs, OF_EXIST);
2339 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
2340 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2341 "GetLastError() returns %d\n", GetLastError() );
2342 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2343 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2344 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2345 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2346 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2348 memset(&ofs, 0xA5, sizeof(ofs));
2349 SetLastError(0xfaceabee);
2350 /* Delete the file and make sure it doesn't exist anymore */
2351 hFile = OpenFile(filename, &ofs, OF_DELETE);
2352 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
2353 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2354 "GetLastError() returns %d\n", GetLastError() );
2355 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2356 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2357 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2358 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2359 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2361 retval = GetFileAttributesA(filename);
2362 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
2365 static void test_overlapped(void)
2370 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2371 if (0) /* tested: WinXP */
2373 GetOverlappedResult(0, NULL, &result, FALSE);
2374 GetOverlappedResult(0, &ov, NULL, FALSE);
2375 GetOverlappedResult(0, NULL, NULL, FALSE);
2378 memset( &ov, 0, sizeof ov );
2380 r = GetOverlappedResult(0, &ov, &result, 0);
2382 ok( result == 0, "wrong result %u\n", result );
2384 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2388 ov.InternalHigh = 0xabcd;
2389 r = GetOverlappedResult(0, &ov, &result, 0);
2391 ok( result == 0xabcd, "wrong result %u\n", result );
2393 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2395 SetLastError( 0xb00 );
2397 ov.Internal = STATUS_INVALID_HANDLE;
2398 ov.InternalHigh = 0xabcd;
2399 r = GetOverlappedResult(0, &ov, &result, 0);
2400 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2401 ok( r == FALSE, "should return false\n");
2402 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
2404 SetLastError( 0xb00 );
2406 ov.Internal = STATUS_PENDING;
2407 ov.InternalHigh = 0xabcd;
2408 r = GetOverlappedResult(0, &ov, &result, 0);
2409 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2410 "wrong error %u\n", GetLastError() );
2411 ok( r == FALSE, "should return false\n");
2412 ok( result == 0, "wrong result %u\n", result );
2414 SetLastError( 0xb00 );
2415 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
2416 ov.Internal = STATUS_PENDING;
2417 ov.InternalHigh = 0xabcd;
2418 r = GetOverlappedResult(0, &ov, &result, 0);
2419 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2420 "wrong error %u\n", GetLastError() );
2421 ok( r == FALSE, "should return false\n");
2423 ResetEvent( ov.hEvent );
2425 SetLastError( 0xb00 );
2426 ov.Internal = STATUS_PENDING;
2427 ov.InternalHigh = 0;
2428 r = GetOverlappedResult(0, &ov, &result, 0);
2429 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2430 "wrong error %u\n", GetLastError() );
2431 ok( r == FALSE, "should return false\n");
2433 r = CloseHandle( ov.hEvent );
2434 ok( r == TRUE, "close handle failed\n");
2437 static void test_RemoveDirectory(void)
2440 char directory[] = "removeme";
2442 rc = CreateDirectory(directory, NULL);
2443 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
2445 rc = SetCurrentDirectory(directory);
2446 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2448 rc = RemoveDirectory(".");
2451 rc = SetCurrentDirectory("..");
2452 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2454 rc = RemoveDirectory(directory);
2455 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2459 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
2461 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
2462 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
2463 return abs(t1 - t2) <= tolerance;
2466 static void test_ReplaceFileA(void)
2468 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2469 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
2470 static const char replacedData[] = "file-to-replace";
2471 static const char replacementData[] = "new-file";
2472 static const char backupData[] = "backup-file";
2473 FILETIME ftReplaced, ftReplacement, ftBackup;
2474 static const char prefix[] = "pfx";
2475 char temp_path[MAX_PATH];
2477 BOOL retok, removeBackup = FALSE;
2481 win_skip("ReplaceFileA() is missing\n");
2485 ret = GetTempPathA(MAX_PATH, temp_path);
2486 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2487 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2489 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
2490 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2492 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2493 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2495 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
2496 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2498 /* place predictable data in the file to be replaced */
2499 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2500 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2501 "failed to open replaced file\n");
2502 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
2503 ok( retok && ret == sizeof(replacedData),
2504 "WriteFile error (replaced) %d\n", GetLastError());
2505 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
2506 "replaced file has wrong size\n");
2507 /* place predictable data in the file to be the replacement */
2508 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2509 ok(hReplacementFile != INVALID_HANDLE_VALUE,
2510 "failed to open replacement file\n");
2511 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
2512 ok( retok && ret == sizeof(replacementData),
2513 "WriteFile error (replacement) %d\n", GetLastError());
2514 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
2515 "replacement file has wrong size\n");
2516 /* place predictable data in the backup file (to be over-written) */
2517 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2518 ok(hBackupFile != INVALID_HANDLE_VALUE,
2519 "failed to open backup file\n");
2520 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
2521 ok( retok && ret == sizeof(backupData),
2522 "WriteFile error (replacement) %d\n", GetLastError());
2523 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
2524 "backup file has wrong size\n");
2525 /* change the filetime on the "replaced" file to ensure that it changes */
2526 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2527 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
2528 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
2529 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2530 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
2531 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
2532 CloseHandle(hReplacedFile);
2533 /* change the filetime on the backup to ensure that it changes */
2534 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2535 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
2536 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
2537 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2538 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
2539 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
2540 CloseHandle(hBackupFile);
2541 /* get the filetime on the replacement file to perform checks */
2542 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
2543 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
2544 CloseHandle(hReplacementFile);
2546 /* perform replacement w/ backup
2547 * TODO: flags are not implemented
2549 SetLastError(0xdeadbeef);
2550 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2551 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2552 /* make sure that the backup has the size of the old "replaced" file */
2553 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2554 ok(hBackupFile != INVALID_HANDLE_VALUE,
2555 "failed to open backup file\n");
2556 ret = GetFileSize(hBackupFile, NULL);
2557 ok(ret == sizeof(replacedData),
2558 "backup file has wrong size %d\n", ret);
2559 /* make sure that the "replaced" file has the size of the replacement file */
2560 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2561 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2562 "failed to open replaced file: %d\n", GetLastError());
2563 if (hReplacedFile != INVALID_HANDLE_VALUE)
2565 ret = GetFileSize(hReplacedFile, NULL);
2566 ok(ret == sizeof(replacementData),
2567 "replaced file has wrong size %d\n", ret);
2568 /* make sure that the replacement file no-longer exists */
2569 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2570 ok(hReplacementFile == INVALID_HANDLE_VALUE,
2571 "unexpected error, replacement file should not exist %d\n", GetLastError());
2572 /* make sure that the backup has the old "replaced" filetime */
2573 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2574 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2575 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
2576 CloseHandle(hBackupFile);
2577 /* make sure that the "replaced" has the old replacement filetime */
2578 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2579 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2580 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
2581 "replaced file has wrong filetime %x%08x / %x%08x\n",
2582 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
2583 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
2584 CloseHandle(hReplacedFile);
2587 skip("couldn't open replacement file, skipping tests\n");
2589 /* re-create replacement file for pass w/o backup (blank) */
2590 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2591 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2592 /* perform replacement w/o backup
2593 * TODO: flags are not implemented
2595 SetLastError(0xdeadbeef);
2596 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2597 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2598 "ReplaceFileA: unexpected error %d\n", GetLastError());
2600 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2601 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2602 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2603 ret = DeleteFileA(backup);
2604 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2605 /* perform replacement w/ backup (no pre-existing backup)
2606 * TODO: flags are not implemented
2608 SetLastError(0xdeadbeef);
2609 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2610 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2611 "ReplaceFileA: unexpected error %d\n", GetLastError());
2613 removeBackup = TRUE;
2615 /* re-create replacement file for pass w/ no permissions to "replaced" */
2616 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2617 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2618 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2619 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2620 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2621 /* perform replacement w/ backup (no permission to "replaced")
2622 * TODO: flags are not implemented
2624 SetLastError(0xdeadbeef);
2625 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2626 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2627 /* make sure that the replacement file still exists */
2628 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2629 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
2630 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
2631 "unexpected error, replacement file should still exist %d\n", GetLastError());
2632 CloseHandle(hReplacementFile);
2633 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2634 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2635 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2637 /* replacement file still exists, make pass w/o "replaced" */
2638 ret = DeleteFileA(replaced);
2639 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2640 "DeleteFileA: error (replaced) %d\n", GetLastError());
2641 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2642 * TODO: flags are not implemented
2644 SetLastError(0xdeadbeef);
2645 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2646 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2647 GetLastError() == ERROR_ACCESS_DENIED),
2648 "ReplaceFileA: unexpected error %d\n", GetLastError());
2650 /* perform replacement w/o existing "replacement" file
2651 * TODO: flags are not implemented
2653 SetLastError(0xdeadbeef);
2654 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2655 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2656 GetLastError() == ERROR_ACCESS_DENIED),
2657 "ReplaceFileA: unexpected error %d\n", GetLastError());
2660 * if the first round (w/ backup) worked then as long as there is no
2661 * failure then there is no need to check this round (w/ backup is the
2662 * more complete case)
2665 /* delete temporary files, replacement and replaced are already deleted */
2668 ret = DeleteFileA(backup);
2670 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2671 "DeleteFileA: error (backup) %d\n", GetLastError());
2676 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2677 * need to be as thorough.
2679 static void test_ReplaceFileW(void)
2681 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2682 static const WCHAR prefix[] = {'p','f','x',0};
2683 WCHAR temp_path[MAX_PATH];
2685 BOOL removeBackup = FALSE;
2689 win_skip("ReplaceFileW() is missing\n");
2693 ret = GetTempPathW(MAX_PATH, temp_path);
2694 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2696 win_skip("GetTempPathW is not available\n");
2699 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2700 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2702 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
2703 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2705 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2706 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2708 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
2709 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2711 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2712 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2714 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2715 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2716 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2717 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2718 "ReplaceFileW: error %d\n", GetLastError());
2720 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2721 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2722 ret = DeleteFileW(backup);
2723 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
2724 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2725 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2726 "ReplaceFileW: error %d\n", GetLastError());
2728 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2729 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2730 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
2731 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2732 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2734 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2735 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
2736 "ReplaceFileW: unexpected error %d\n", GetLastError());
2737 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
2738 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2739 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2741 removeBackup = TRUE;
2743 ret = DeleteFileW(replaced);
2744 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
2745 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2746 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
2748 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2749 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2750 GetLastError() == ERROR_ACCESS_DENIED),
2751 "ReplaceFileW: unexpected error %d\n", GetLastError());
2755 ret = DeleteFileW(backup);
2757 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2758 "DeleteFileW: error (backup) %d\n", GetLastError());
2764 InitFunctionPointers();
2774 test_GetTempFileNameA();
2783 test_FindFirstFileA();
2784 test_FindNextFileA();
2785 test_FindFirstFileExA();
2787 test_file_sharing();
2788 test_offset_in_overlapped_structure();
2791 test_async_file_errors();
2795 test_RemoveDirectory();
2796 test_ReplaceFileA();
2797 test_ReplaceFileW();