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