2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 * Copyright (c) 2008 Jeff Zaroyko
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/test.h"
32 static HANDLE (WINAPI
*pFindFirstFileExA
)(LPCSTR
,FINDEX_INFO_LEVELS
,LPVOID
,FINDEX_SEARCH_OPS
,LPVOID
,DWORD
);
33 static BOOL (WINAPI
*pReplaceFileA
)(LPCSTR
, LPCSTR
, LPCSTR
, DWORD
, LPVOID
, LPVOID
);
34 static BOOL (WINAPI
*pReplaceFileW
)(LPCWSTR
, LPCWSTR
, LPCWSTR
, DWORD
, LPVOID
, LPVOID
);
36 /* keep filename and filenameW the same */
37 static const char filename
[] = "testfile.xxx";
38 static const WCHAR filenameW
[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
39 static const char sillytext
[] =
40 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
41 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
42 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
51 static void InitFunctionPointers(void)
53 HMODULE hkernel32
= GetModuleHandleA("kernel32");
55 pFindFirstFileExA
=(void*)GetProcAddress(hkernel32
, "FindFirstFileExA");
56 pReplaceFileA
=(void*)GetProcAddress(hkernel32
, "ReplaceFileA");
57 pReplaceFileW
=(void*)GetProcAddress(hkernel32
, "ReplaceFileW");
60 static void test__hread( void )
69 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
); /* be sure to remove stale files */
70 DeleteFileA( filename
);
71 filehandle
= _lcreat( filename
, 0 );
72 if (filehandle
== HFILE_ERROR
)
74 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
78 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
80 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
82 filehandle
= _lopen( filename
, OF_READ
);
84 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError( ) );
86 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
88 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
90 for (bytes_wanted
= 0; bytes_wanted
< lstrlenA( sillytext
); bytes_wanted
++)
92 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
93 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
94 for (i
= 0; i
< bytes_wanted
; i
++)
96 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
100 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
102 ret
= DeleteFileA( filename
);
103 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
107 static void test__hwrite( void )
116 HLOCAL memory_object
;
120 filehandle
= _lcreat( filename
, 0 );
121 if (filehandle
== HFILE_ERROR
)
123 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
127 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
129 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
131 filehandle
= _lopen( filename
, OF_READ
);
133 bytes_read
= _hread( filehandle
, buffer
, 1);
135 ok( 0 == bytes_read
, "file read size error\n" );
137 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
139 filehandle
= _lopen( filename
, OF_READWRITE
);
143 srand( (unsigned)time( NULL
) );
144 for (blocks
= 0; blocks
< 100; blocks
++)
146 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
149 checksum
[0] = checksum
[0] + buffer
[i
];
151 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
152 bytes_written
= bytes_written
+ sizeof( buffer
);
155 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
158 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
160 memory_object
= LocalAlloc( LPTR
, bytes_written
);
162 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)\n" );
164 contents
= LocalLock( memory_object
);
166 filehandle
= _lopen( filename
, OF_READ
);
168 contents
= LocalLock( memory_object
);
170 ok( NULL
!= contents
, "LocalLock whines\n" );
172 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
178 checksum
[0] = checksum
[0] + contents
[i
];
181 while (i
< bytes_written
- 1);
183 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
185 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
187 ret
= DeleteFileA( filename
);
188 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
192 static void test__lclose( void )
197 filehandle
= _lcreat( filename
, 0 );
198 if (filehandle
== HFILE_ERROR
)
200 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
204 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
206 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
208 ret
= DeleteFileA( filename
);
209 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
213 static void test__lcreat( void )
217 WIN32_FIND_DATAA search_results
;
218 char slashname
[] = "testfi/";
223 filehandle
= _lcreat( filename
, 0 );
224 if (filehandle
== HFILE_ERROR
)
226 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
230 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
232 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
234 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
236 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
238 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
240 ret
= DeleteFileA(filename
);
241 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError());
243 filehandle
= _lcreat( filename
, 1 ); /* readonly */
244 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
246 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
248 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
250 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
252 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
254 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
256 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
258 filehandle
= _lcreat( filename
, 2 );
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 complains\n" );
263 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
265 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
267 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
269 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
271 ret
= DeleteFileA( filename
);
272 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
274 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
275 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
277 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
279 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
281 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
283 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
285 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
287 ret
= DeleteFileA( filename
);
288 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
290 filehandle
=_lcreat (slashname
, 0); /* illegal name */
291 if (HFILE_ERROR
==filehandle
) {
293 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
294 "creating file \"%s\" failed with error %d\n", slashname
, err
);
295 } else { /* only NT succeeds */
297 find
=FindFirstFileA (slashname
, &search_results
);
298 if (INVALID_HANDLE_VALUE
!=find
)
300 ret
= FindClose (find
);
301 ok (0 != ret
, "FindClose complains (%d)\n", GetLastError ());
302 slashname
[strlen(slashname
)-1]=0;
303 ok (!strcmp (slashname
, search_results
.cFileName
),
304 "found unexpected name \"%s\"\n", search_results
.cFileName
);
305 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
306 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
307 search_results
.dwFileAttributes
);
309 ret
= DeleteFileA( slashname
);
310 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
313 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
314 if (HFILE_ERROR
==filehandle
)
315 ok (0, "couldn't create volume label \"%s\"\n", filename
);
318 find
=FindFirstFileA (filename
, &search_results
);
319 if (INVALID_HANDLE_VALUE
==find
)
320 ok (0, "file \"%s\" not found\n", filename
);
322 ret
= FindClose(find
);
323 ok ( 0 != ret
, "FindClose complains (%d)\n", GetLastError ());
324 ok (!strcmp (filename
, search_results
.cFileName
),
325 "found unexpected name \"%s\"\n", search_results
.cFileName
);
326 search_results
.dwFileAttributes
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
327 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
328 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
329 search_results
.dwFileAttributes
);
331 ret
= DeleteFileA( filename
);
332 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
337 static void test__llseek( void )
345 filehandle
= _lcreat( filename
, 0 );
346 if (filehandle
== HFILE_ERROR
)
348 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
352 for (i
= 0; i
< 400; i
++)
354 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
356 ok( HFILE_ERROR
!= _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
), "should be able to seek\n" );
357 ok( HFILE_ERROR
!= _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
), "should be able to seek\n" );
359 bytes_read
= _hread( filehandle
, buffer
, 1);
360 ok( 1 == bytes_read
, "file read size error\n" );
361 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
362 ok( HFILE_ERROR
!= _llseek( filehandle
, -400 * strlen( sillytext
), FILE_END
), "should be able to seek\n" );
364 bytes_read
= _hread( filehandle
, buffer
, 1);
365 ok( 1 == bytes_read
, "file read size error\n" );
366 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
367 ok( HFILE_ERROR
!= _llseek( filehandle
, 1000000, FILE_END
), "should be able to seek past file; poor, poor Windows programmers\n" );
368 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
370 ret
= DeleteFileA( filename
);
371 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
375 static void test__llopen( void )
382 filehandle
= _lcreat( filename
, 0 );
383 if (filehandle
== HFILE_ERROR
)
385 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
389 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
390 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
392 filehandle
= _lopen( filename
, OF_READ
);
393 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
394 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
395 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
396 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
398 filehandle
= _lopen( filename
, OF_READWRITE
);
399 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
400 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
401 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
402 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
404 filehandle
= _lopen( filename
, OF_WRITE
);
405 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
406 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
407 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
409 ret
= DeleteFileA( filename
);
410 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
411 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
415 static void test__lread( void )
424 filehandle
= _lcreat( filename
, 0 );
425 if (filehandle
== HFILE_ERROR
)
427 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
431 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
433 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
435 filehandle
= _lopen( filename
, OF_READ
);
437 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError());
439 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
441 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
443 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
445 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
446 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
447 for (i
= 0; i
< bytes_wanted
; i
++)
449 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
453 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
455 ret
= DeleteFileA( filename
);
456 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
460 static void test__lwrite( void )
469 HLOCAL memory_object
;
473 filehandle
= _lcreat( filename
, 0 );
474 if (filehandle
== HFILE_ERROR
)
476 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
480 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
482 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
484 filehandle
= _lopen( filename
, OF_READ
);
486 bytes_read
= _hread( filehandle
, buffer
, 1);
488 ok( 0 == bytes_read
, "file read size error\n" );
490 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
492 filehandle
= _lopen( filename
, OF_READWRITE
);
496 srand( (unsigned)time( NULL
) );
497 for (blocks
= 0; blocks
< 100; blocks
++)
499 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
502 checksum
[0] = checksum
[0] + buffer
[i
];
504 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
505 bytes_written
= bytes_written
+ sizeof( buffer
);
508 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
511 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
513 memory_object
= LocalAlloc( LPTR
, bytes_written
);
515 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
517 contents
= LocalLock( memory_object
);
519 filehandle
= _lopen( filename
, OF_READ
);
521 contents
= LocalLock( memory_object
);
523 ok( NULL
!= contents
, "LocalLock whines\n" );
525 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
531 checksum
[0] += contents
[i
];
534 while (i
< bytes_written
- 1);
536 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
538 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
540 ret
= DeleteFileA( filename
);
541 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
544 static void test_CopyFileA(void)
546 char temp_path
[MAX_PATH
];
547 char source
[MAX_PATH
], dest
[MAX_PATH
];
548 static const char prefix
[] = "pfx";
555 ret
= GetTempPathA(MAX_PATH
, temp_path
);
556 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
557 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
559 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
560 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
562 /* make the source have not zero size */
563 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
564 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
565 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
566 ok( retok
&& ret
== sizeof(prefix
),
567 "WriteFile error %d\n", GetLastError());
568 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
569 /* get the file time and change it to prove the difference */
570 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
571 ok( ret
, "GetFileTime error %d\n", GetLastError());
572 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
573 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
574 ok( ret
, "SetFileTime error %d\n", GetLastError());
575 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
578 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
579 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
581 SetLastError(0xdeadbeef);
582 ret
= CopyFileA(source
, dest
, TRUE
);
583 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
584 "CopyFileA: unexpected error %d\n", GetLastError());
586 ret
= CopyFileA(source
, dest
, FALSE
);
587 ok(ret
, "CopyFileA: error %d\n", GetLastError());
589 /* make sure that destination has correct size */
590 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
591 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
592 ret
= GetFileSize(hfile
, NULL
);
593 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
595 /* make sure that destination has the same filetime */
596 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
597 ok( ret
, "GetFileTime error %d\n", GetLastError());
598 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
600 SetLastError(0xdeadbeef);
601 ret
= CopyFileA(source
, dest
, FALSE
);
602 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
603 "CopyFileA: ret = %d, unexpected error %d\n", ret
, GetLastError());
605 /* make sure that destination still has correct size */
606 ret
= GetFileSize(hfile
, NULL
);
607 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
608 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
609 ok( retok
&& ret
== sizeof(prefix
),
610 "ReadFile: error %d\n", GetLastError());
611 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
614 ret
= DeleteFileA(source
);
615 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
616 ret
= DeleteFileA(dest
);
617 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
620 static void test_CopyFileW(void)
622 WCHAR temp_path
[MAX_PATH
];
623 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
624 static const WCHAR prefix
[] = {'p','f','x',0};
627 ret
= GetTempPathW(MAX_PATH
, temp_path
);
628 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
630 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
631 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
633 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
634 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
636 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
637 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
639 ret
= CopyFileW(source
, dest
, TRUE
);
640 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
641 "CopyFileW: unexpected error %d\n", GetLastError());
643 ret
= CopyFileW(source
, dest
, FALSE
);
644 ok(ret
, "CopyFileW: error %d\n", GetLastError());
646 ret
= DeleteFileW(source
);
647 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
648 ret
= DeleteFileW(dest
);
649 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
652 static void test_CreateFileA(void)
655 char temp_path
[MAX_PATH
];
656 char filename
[MAX_PATH
];
657 static const char prefix
[] = "pfx";
660 ret
= GetTempPathA(MAX_PATH
, temp_path
);
661 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
662 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
664 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
665 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
667 SetLastError(0xdeadbeef);
668 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
669 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
670 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
671 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
673 SetLastError(0xdeadbeef);
674 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
675 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
676 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
677 "hFile %p, last error %u\n", hFile
, GetLastError());
681 SetLastError(0xdeadbeef);
682 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
683 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
684 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
685 "hFile %p, last error %u\n", hFile
, GetLastError());
689 ret
= DeleteFileA(filename
);
690 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
692 SetLastError(0xdeadbeef);
693 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
694 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
695 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
696 "hFile %p, last error %u\n", hFile
, GetLastError());
700 ret
= DeleteFileA(filename
);
701 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
704 static void test_CreateFileW(void)
707 WCHAR temp_path
[MAX_PATH
];
708 WCHAR filename
[MAX_PATH
];
709 static const WCHAR emptyW
[]={'\0'};
710 static const WCHAR prefix
[] = {'p','f','x',0};
711 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
714 ret
= GetTempPathW(MAX_PATH
, temp_path
);
715 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
717 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
718 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
720 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
721 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
723 SetLastError(0xdeadbeef);
724 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
725 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
726 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
727 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
729 SetLastError(0xdeadbeef);
730 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
731 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
732 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
733 "hFile %p, last error %u\n", hFile
, GetLastError());
737 SetLastError(0xdeadbeef);
738 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
739 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
740 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
741 "hFile %p, last error %u\n", hFile
, GetLastError());
745 ret
= DeleteFileW(filename
);
746 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
748 SetLastError(0xdeadbeef);
749 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
750 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
751 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
752 "hFile %p, last error %u\n", hFile
, GetLastError());
756 ret
= DeleteFileW(filename
);
757 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
761 /* this crashes on NT4.0 */
762 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
763 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
764 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
765 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile
,GetLastError());
768 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
769 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
770 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
771 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile
,GetLastError());
773 /* test the result of opening a nonexistent driver name */
774 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
775 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
776 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
777 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile
,GetLastError());
779 ret
= CreateDirectoryW(filename
, NULL
);
780 ok(ret
== TRUE
, "couldn't create temporary directory\n");
781 hFile
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
782 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
783 ok(hFile
!= INVALID_HANDLE_VALUE
,
784 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
786 ret
= RemoveDirectoryW(filename
);
787 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
790 static void test_GetTempFileNameA(void)
794 char expected
[MAX_PATH
+ 10];
795 char windowsdir
[MAX_PATH
+ 10];
796 char windowsdrive
[3];
798 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
799 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
800 ok(result
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
802 /* If the Windows directory is the root directory, it ends in backslash, not else. */
803 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
805 strcat(windowsdir
, "\\");
808 windowsdrive
[0] = windowsdir
[0];
809 windowsdrive
[1] = windowsdir
[1];
810 windowsdrive
[2] = '\0';
812 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
813 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
814 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
815 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
816 windowsdrive
[0], out
);
818 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
819 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
821 strcat(expected
, windowsdir
);
822 strcat(expected
, "abc2.tmp");
823 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
827 static void test_DeleteFileA( void )
831 ret
= DeleteFileA(NULL
);
832 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
833 GetLastError() == ERROR_PATH_NOT_FOUND
),
834 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
836 ret
= DeleteFileA("");
837 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
838 GetLastError() == ERROR_BAD_PATHNAME
),
839 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
841 ret
= DeleteFileA("nul");
842 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
843 GetLastError() == ERROR_INVALID_PARAMETER
||
844 GetLastError() == ERROR_ACCESS_DENIED
||
845 GetLastError() == ERROR_INVALID_FUNCTION
),
846 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret
,GetLastError());
849 static void test_DeleteFileW( void )
852 WCHAR pathW
[MAX_PATH
];
853 WCHAR pathsubW
[MAX_PATH
];
854 static const WCHAR dirW
[] = {'d','e','l','e','t','e','f','i','l','e',0};
855 static const WCHAR subdirW
[] = {'\\','s','u','b',0};
856 static const WCHAR emptyW
[]={'\0'};
858 ret
= DeleteFileW(NULL
);
859 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
861 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
862 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
864 ret
= DeleteFileW(emptyW
);
865 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
866 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
868 /* test DeleteFile on empty directory */
869 ret
= GetTempPathW(MAX_PATH
, pathW
);
870 if (ret
+ sizeof(dirW
)/sizeof(WCHAR
)-1 + sizeof(subdirW
)/sizeof(WCHAR
)-1 >= MAX_PATH
)
872 ok(0, "MAX_PATH exceeded in constructing paths\n");
875 lstrcatW(pathW
, dirW
);
876 lstrcpyW(pathsubW
, pathW
);
877 lstrcatW(pathsubW
, subdirW
);
878 ret
= CreateDirectoryW(pathW
, NULL
);
879 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
880 ret
= DeleteFileW(pathW
);
881 ok(ret
== FALSE
, "DeleteFile should fail for empty directories\n");
882 ret
= RemoveDirectoryW(pathW
);
883 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
885 /* test DeleteFile on non-empty directory */
886 ret
= CreateDirectoryW(pathW
, NULL
);
887 ok(ret
== TRUE
, "couldn't create directory deletefile\n");
888 ret
= CreateDirectoryW(pathsubW
, NULL
);
889 ok(ret
== TRUE
, "couldn't create directory deletefile\\sub\n");
890 ret
= DeleteFileW(pathW
);
891 ok(ret
== FALSE
, "DeleteFile should fail for non-empty directories\n");
892 ret
= RemoveDirectoryW(pathsubW
);
893 ok(ret
== TRUE
, "expected to remove directory deletefile\\sub\n");
894 ret
= RemoveDirectoryW(pathW
);
895 ok(ret
== TRUE
, "expected to remove directory deletefile\n");
898 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
900 static void test_MoveFileA(void)
902 char tempdir
[MAX_PATH
];
903 char source
[MAX_PATH
], dest
[MAX_PATH
];
904 static const char prefix
[] = "pfx";
907 ret
= GetTempPathA(MAX_PATH
, tempdir
);
908 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
909 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
911 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
912 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
914 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
915 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
917 ret
= MoveFileA(source
, dest
);
918 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
919 "MoveFileA: unexpected error %d\n", GetLastError());
921 ret
= DeleteFileA(dest
);
922 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
924 ret
= MoveFileA(source
, dest
);
925 ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
927 lstrcatA(tempdir
, "Remove Me");
928 ret
= CreateDirectoryA(tempdir
, NULL
);
929 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
931 lstrcpyA(source
, dest
);
932 lstrcpyA(dest
, tempdir
);
933 lstrcatA(dest
, "\\wild?.*");
934 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
935 ret
= MoveFileA(source
, dest
);
936 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
937 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
938 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
939 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
940 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
943 char temppath
[MAX_PATH
];
946 lstrcpyA(temppath
, tempdir
);
947 lstrcatA(temppath
, "\\*.*");
948 hFind
= FindFirstFileA(temppath
, &fd
);
949 if (INVALID_HANDLE_VALUE
!= hFind
)
954 lpName
= fd
.cAlternateFileName
;
956 lpName
= fd
.cFileName
;
957 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
959 while (FindNextFileA(hFind
, &fd
));
963 ret
= DeleteFileA(source
);
964 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
965 ret
= DeleteFileA(dest
);
966 ok(!ret
, "DeleteFileA: error %d\n", GetLastError());
967 ret
= RemoveDirectoryA(tempdir
);
968 ok(ret
, "DeleteDirectoryA: error %d\n", GetLastError());
971 static void test_MoveFileW(void)
973 WCHAR temp_path
[MAX_PATH
];
974 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
975 static const WCHAR prefix
[] = {'p','f','x',0};
978 ret
= GetTempPathW(MAX_PATH
, temp_path
);
979 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
981 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
982 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
984 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
985 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
987 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
988 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
990 ret
= MoveFileW(source
, dest
);
991 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
992 "CopyFileW: unexpected error %d\n", GetLastError());
994 ret
= DeleteFileW(source
);
995 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
996 ret
= DeleteFileW(dest
);
997 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
1000 #define PATTERN_OFFSET 0x10
1002 static void test_offset_in_overlapped_structure(void)
1008 BYTE buf
[256], pattern
[] = "TeSt";
1010 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
1013 ret
=GetTempPathA(MAX_PATH
, temp_path
);
1014 ok( ret
, "GetTempPathA error %d\n", GetLastError());
1015 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
1016 ok( ret
, "GetTempFileNameA error %d\n", GetLastError());
1018 /*** Write File *****************************************************/
1020 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
1021 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1023 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
1024 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
1025 ok( ret
, "WriteFile error %d\n", GetLastError());
1026 ok(done
== sizeof(buf
), "expected number of bytes written %u\n", done
);
1028 memset(&ov
, 0, sizeof(ov
));
1029 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1030 S(U(ov
)).OffsetHigh
= 0;
1031 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1032 /* Win 9x does not support the overlapped I/O on files */
1033 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1034 ok(rc
, "WriteFile error %d\n", GetLastError());
1035 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1036 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1037 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1039 S(U(ov
)).Offset
= sizeof(buf
) * 2;
1040 S(U(ov
)).OffsetHigh
= 0;
1041 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
1042 ok( ret
, "WriteFile error %d\n", GetLastError());
1043 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
1044 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1045 ok(offset
== sizeof(buf
) * 2 + sizeof(pattern
), "wrong file offset %d\n", offset
);
1050 /*** Read File *****************************************************/
1052 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1053 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1055 memset(buf
, 0, sizeof(buf
));
1056 memset(&ov
, 0, sizeof(ov
));
1057 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1058 S(U(ov
)).OffsetHigh
= 0;
1059 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
1060 /* Win 9x does not support the overlapped I/O on files */
1061 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1062 ok(rc
, "ReadFile error %d\n", GetLastError());
1063 ok(done
== sizeof(pattern
), "expected number of bytes read %u\n", done
);
1064 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1065 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1066 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
1071 ret
= DeleteFileA(temp_fname
);
1072 ok( ret
, "DeleteFileA error %d\n", GetLastError());
1075 static void test_LockFile(void)
1079 OVERLAPPED overlapped
;
1080 int limited_LockFile
;
1081 int limited_UnLockFile
;
1084 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1085 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1086 CREATE_ALWAYS
, 0, 0 );
1087 if (handle
== INVALID_HANDLE_VALUE
)
1089 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1092 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
1094 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1095 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1097 limited_UnLockFile
= 0;
1098 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1100 limited_UnLockFile
= 1;
1103 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1104 /* overlapping locks must fail */
1105 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1106 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1107 /* non-overlapping locks must succeed */
1108 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1110 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1111 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1112 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1113 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1115 S(U(overlapped
)).Offset
= 100;
1116 S(U(overlapped
)).OffsetHigh
= 0;
1117 overlapped
.hEvent
= 0;
1119 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1120 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1122 /* LockFileEx is probably OK, test it more. */
1123 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1124 "LockFileEx 100,100 failed\n" );
1127 /* overlapping shared locks are OK */
1128 S(U(overlapped
)).Offset
= 150;
1129 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1131 /* but exclusive is not */
1132 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1133 0, 50, 0, &overlapped
),
1134 "LockFileEx exclusive 150,50 succeeded\n" );
1135 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1136 { /* UnLockFile is capable. */
1137 S(U(overlapped
)).Offset
= 100;
1138 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1139 "UnlockFileEx 150,100 again succeeded\n" );
1142 ret
= LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 );
1145 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1146 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1147 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1150 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong LockFile error %u\n", GetLastError() );
1152 /* wrap-around lock should not do anything */
1153 /* (but still succeeds on NT4 so we don't check result) */
1154 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1156 limited_LockFile
= 0;
1157 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1159 limited_LockFile
= 1;
1162 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1164 /* zero-byte lock */
1165 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1166 limited_LockFile
|| ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1167 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1168 limited_LockFile
|| ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1170 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1171 !ok( UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1173 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1175 CloseHandle( handle
);
1176 DeleteFileA( filename
);
1179 static inline int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
, BOOL is_win9x
)
1183 if (!access1
) sharing1
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1184 if (!access2
) sharing2
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1189 if (!access1
) access1
= GENERIC_READ
;
1192 if (!access2
) access2
= GENERIC_READ
;
1195 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1196 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1197 if ((access1
& DELETE
) && !(sharing2
& FILE_SHARE_DELETE
)) return 0;
1198 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1199 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1200 if ((access2
& DELETE
) && !(sharing1
& FILE_SHARE_DELETE
)) return 0;
1204 static void test_file_sharing(void)
1206 static const DWORD access_modes
[] =
1207 { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
,
1208 DELETE
, GENERIC_READ
|DELETE
, GENERIC_WRITE
|DELETE
, GENERIC_READ
|GENERIC_WRITE
|DELETE
};
1209 static const DWORD sharing_modes
[] =
1210 { 0, FILE_SHARE_READ
,
1211 FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1212 FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_DELETE
,
1213 FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
};
1217 BOOL is_win9x
= FALSE
;
1219 /* make sure the file exists */
1220 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1221 if (h
== INVALID_HANDLE_VALUE
)
1223 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError());
1226 is_win9x
= GetFileAttributesW(filenameW
) == INVALID_FILE_ATTRIBUTES
;
1229 for (a1
= 0; a1
< sizeof(access_modes
)/sizeof(access_modes
[0]); a1
++)
1231 for (s1
= 0; s1
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s1
++)
1233 /* Win9x doesn't support FILE_SHARE_DELETE */
1234 if (is_win9x
&& (sharing_modes
[s1
] & FILE_SHARE_DELETE
))
1237 SetLastError(0xdeadbeef);
1238 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1239 NULL
, OPEN_EXISTING
, 0, 0 );
1240 if (h
== INVALID_HANDLE_VALUE
)
1242 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1245 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1247 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1249 /* Win9x doesn't support FILE_SHARE_DELETE */
1250 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1253 SetLastError(0xdeadbeef);
1254 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1255 NULL
, OPEN_EXISTING
, 0, 0 );
1257 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1258 access_modes
[a2
], sharing_modes
[s2
], is_win9x
))
1260 ret
= GetLastError();
1262 ok( h2
!= INVALID_HANDLE_VALUE
,
1263 "open failed for modes %x/%x/%x/%x\n",
1264 access_modes
[a1
], sharing_modes
[s1
],
1265 access_modes
[a2
], sharing_modes
[s2
] );
1266 ok( ret
== 0xdeadbeef /* Win9x */ ||
1268 "wrong error code %d\n", ret
);
1274 ret
= GetLastError();
1276 ok( h2
== INVALID_HANDLE_VALUE
,
1277 "open succeeded for modes %x/%x/%x/%x\n",
1278 access_modes
[a1
], sharing_modes
[s1
],
1279 access_modes
[a2
], sharing_modes
[s2
] );
1280 ok( ret
== ERROR_SHARING_VIOLATION
,
1281 "wrong error code %d\n", ret
);
1289 SetLastError(0xdeadbeef);
1290 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1291 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1293 SetLastError(0xdeadbeef);
1294 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1295 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1296 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %d\n", GetLastError() );
1298 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1299 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1304 DeleteFileA( filename
);
1307 static char get_windows_drive(void)
1309 char windowsdir
[MAX_PATH
];
1310 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1311 return windowsdir
[0];
1314 static void test_FindFirstFileA(void)
1317 WIN32_FIND_DATAA data
;
1319 char buffer
[5] = "C:\\";
1322 /* try FindFirstFileA on "C:\" */
1323 buffer
[0] = get_windows_drive();
1325 SetLastError( 0xdeadbeaf );
1326 handle
= FindFirstFileA(buffer
, &data
);
1327 err
= GetLastError();
1328 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should fail\n" );
1329 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1331 /* try FindFirstFileA on "C:\*" */
1332 strcpy(buffer2
, buffer
);
1333 strcat(buffer2
, "*");
1334 handle
= FindFirstFileA(buffer2
, &data
);
1335 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1336 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1337 "FindFirstFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1338 if (FindNextFileA( handle
, &data
))
1339 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1340 "FindNextFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1341 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1343 /* try FindFirstFileA on windows dir */
1344 GetWindowsDirectory( buffer2
, sizeof(buffer2
) );
1345 strcat(buffer2
, "\\*");
1346 handle
= FindFirstFileA(buffer2
, &data
);
1347 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1348 ok( !strcmp( data
.cFileName
, "." ), "FindFirstFile should return '.' first\n" );
1349 ok( FindNextFileA( handle
, &data
), "FindNextFile failed\n" );
1350 ok( !strcmp( data
.cFileName
, ".." ), "FindNextFile should return '..' as second entry\n" );
1351 while (FindNextFileA( handle
, &data
))
1352 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1353 "FindNextFile shouldn't return '%s'\n", data
.cFileName
);
1354 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1356 /* try FindFirstFileA on "C:\foo\" */
1357 SetLastError( 0xdeadbeaf );
1358 strcpy(buffer2
, buffer
);
1359 strcat(buffer2
, "foo\\");
1360 handle
= FindFirstFileA(buffer2
, &data
);
1361 err
= GetLastError();
1362 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1364 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1367 /* try FindFirstFileA on "C:\foo\bar.txt" */
1368 SetLastError( 0xdeadbeaf );
1369 strcpy(buffer2
, buffer
);
1370 strcat(buffer2
, "foo\\bar.txt");
1371 handle
= FindFirstFileA(buffer2
, &data
);
1372 err
= GetLastError();
1373 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1374 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1376 /* try FindFirstFileA on "C:\foo\*.*" */
1377 SetLastError( 0xdeadbeaf );
1378 strcpy(buffer2
, buffer
);
1379 strcat(buffer2
, "foo\\*.*");
1380 handle
= FindFirstFileA(buffer2
, &data
);
1381 err
= GetLastError();
1382 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1383 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1385 /* try FindFirstFileA on "foo\bar.txt" */
1386 SetLastError( 0xdeadbeaf );
1387 strcpy(buffer2
, "foo\\bar.txt");
1388 handle
= FindFirstFileA(buffer2
, &data
);
1389 err
= GetLastError();
1390 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1391 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1393 /* try FindFirstFileA on "c:\nul" */
1394 SetLastError( 0xdeadbeaf );
1395 strcpy(buffer2
, buffer
);
1396 strcat(buffer2
, "nul");
1397 handle
= FindFirstFileA(buffer2
, &data
);
1398 err
= GetLastError();
1399 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1400 ok( 0 == lstrcmpiA(data
.cFileName
, "nul"), "wrong name %s\n", data
.cFileName
);
1401 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
1402 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
1403 "wrong attributes %x\n", data
.dwFileAttributes
);
1404 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
1406 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1407 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1409 SetLastError( 0xdeadbeaf );
1410 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1411 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1412 ok( FindClose( handle
), "failed to close handle\n" );
1414 /* try FindFirstFileA on "lpt1" */
1415 SetLastError( 0xdeadbeaf );
1416 strcpy(buffer2
, "lpt1");
1417 handle
= FindFirstFileA(buffer2
, &data
);
1418 err
= GetLastError();
1419 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1420 ok( 0 == lstrcmpiA(data
.cFileName
, "lpt1"), "wrong name %s\n", data
.cFileName
);
1421 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
||
1422 FILE_ATTRIBUTE_DEVICE
== data
.dwFileAttributes
/* Win9x */,
1423 "wrong attributes %x\n", data
.dwFileAttributes
);
1424 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_ARCHIVE
)
1426 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1427 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1429 SetLastError( 0xdeadbeaf );
1430 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1431 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1432 ok( FindClose( handle
), "failed to close handle\n" );
1434 /* try FindFirstFileA on "c:\nul\*" */
1435 SetLastError( 0xdeadbeaf );
1436 strcpy(buffer2
, buffer
);
1437 strcat(buffer2
, "nul\\*");
1438 handle
= FindFirstFileA(buffer2
, &data
);
1439 err
= GetLastError();
1440 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1441 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1443 /* try FindFirstFileA on "c:\nul*" */
1444 SetLastError( 0xdeadbeaf );
1445 strcpy(buffer2
, buffer
);
1446 strcat(buffer2
, "nul*");
1447 handle
= FindFirstFileA(buffer2
, &data
);
1448 err
= GetLastError();
1449 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1450 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1452 /* try FindFirstFileA on "c:\foo\bar\nul" */
1453 SetLastError( 0xdeadbeaf );
1454 strcpy(buffer2
, buffer
);
1455 strcat(buffer2
, "foo\\bar\\nul");
1456 handle
= FindFirstFileA(buffer2
, &data
);
1457 err
= GetLastError();
1458 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1459 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1461 /* try FindFirstFileA on "c:\foo\nul\bar" */
1462 SetLastError( 0xdeadbeaf );
1463 strcpy(buffer2
, buffer
);
1464 strcat(buffer2
, "foo\\nul\\bar");
1465 handle
= FindFirstFileA(buffer2
, &data
);
1466 err
= GetLastError();
1467 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1468 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1471 static void test_FindNextFileA(void)
1474 WIN32_FIND_DATAA search_results
;
1476 char buffer
[5] = "C:\\*";
1478 buffer
[0] = get_windows_drive();
1479 handle
= FindFirstFileA(buffer
,&search_results
);
1480 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
1481 while (FindNextFile(handle
, &search_results
))
1483 /* get to the end of the files */
1485 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1486 err
= GetLastError();
1487 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
1490 static void test_FindFirstFileExA(void)
1492 WIN32_FIND_DATAA search_results
;
1495 if (!pFindFirstFileExA
)
1497 skip("FindFirstFileExA() is missing\n");
1501 CreateDirectoryA("test-dir", NULL
);
1502 _lclose(_lcreat("test-dir\\file1", 0));
1503 _lclose(_lcreat("test-dir\\file2", 0));
1504 CreateDirectoryA("test-dir\\dir1", NULL
);
1505 /* FindExLimitToDirectories is ignored */
1506 SetLastError(0xdeadbeef);
1507 handle
= pFindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, FindExSearchLimitToDirectories
, NULL
, 0);
1508 if (handle
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
1510 skip("FindFirstFileExA is not implemented\n");
1513 ok(handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile failed (err=%u)\n", GetLastError());
1514 ok(strcmp(search_results
.cFileName
, ".") == 0, "First entry should be '.', is %s\n", search_results
.cFileName
);
1516 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1518 ok(FindNextFile(handle
, &search_results
), "Fetching second file failed\n");
1519 ok(strcmp(search_results
.cFileName
, "..") == 0, "Second entry should be '..' is %s\n", search_results
.cFileName
);
1521 ok(FindNextFile(handle
, &search_results
), "Fetching third file failed\n");
1522 ok(CHECK_NAME(search_results
.cFileName
), "Invalid third entry - %s\n", search_results
.cFileName
);
1524 ok(FindNextFile(handle
, &search_results
), "Fetching fourth file failed\n");
1525 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
1527 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
1528 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
1532 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should failed\n");
1534 FindClose( handle
);
1537 DeleteFileA("test-dir\\file1");
1538 DeleteFileA("test-dir\\file2");
1539 RemoveDirectoryA("test-dir\\dir1");
1540 RemoveDirectoryA("test-dir");
1543 static int test_Mapfile_createtemp(HANDLE
*handle
)
1545 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1546 DeleteFile(filename
);
1547 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1548 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1549 if (*handle
!= INVALID_HANDLE_VALUE
) {
1557 static void test_MapFile(void)
1562 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1564 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1565 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1567 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1569 /* We have to close file before we try new stuff with mapping again.
1570 Else we would always succeed on XP or block descriptors on 95. */
1571 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1572 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1573 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1574 ok( CloseHandle( handle
), "can't close file handle\n");
1577 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1579 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1580 ok( hmap
== NULL
, "mapped zero size file\n");
1581 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1583 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
1584 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1585 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1587 CloseHandle( hmap
);
1589 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
1590 ok( hmap
== NULL
|| broken(hmap
!= NULL
) /* NT4 */, "mapping should fail\n");
1591 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1593 CloseHandle( hmap
);
1595 /* On XP you can now map again, on Win 95 you cannot. */
1597 ok( CloseHandle( handle
), "can't close file handle\n");
1598 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1601 static void test_GetFileType(void)
1604 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1605 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1606 type
= GetFileType(h
);
1607 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
1609 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1610 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1611 type
= GetFileType(h
);
1612 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
1614 DeleteFileA( filename
);
1617 static int completion_count
;
1619 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
1621 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1622 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
1626 static void test_async_file_errors(void)
1628 char szFile
[MAX_PATH
];
1629 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
1631 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
1633 S(U(ovl
)).Offset
= 0;
1634 S(U(ovl
)).OffsetHigh
= 0;
1636 completion_count
= 0;
1638 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
1639 strcat(szFile
, "\\win.ini");
1640 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1641 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1642 if (hFile
== INVALID_HANDLE_VALUE
) /* win9x doesn't like FILE_SHARE_DELETE */
1643 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1644 NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1645 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA(%s ...) failed\n", szFile
);
1650 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
1652 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
1653 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1656 if (!GetOverlappedResult(hFile
, &ovl
, &count
, FALSE
))
1658 S(U(ovl
)).Offset
+= count
;
1659 /* i/o completion routine only called if ReadFileEx returned success.
1660 * we only care about violations of this rule so undo what should have
1664 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
1665 /*printf("Error = %ld\n", GetLastError());*/
1666 HeapFree(GetProcessHeap(), 0, lpBuffer
);
1669 static void test_read_write(void)
1673 char temp_path
[MAX_PATH
];
1674 char filename
[MAX_PATH
];
1675 static const char prefix
[] = "pfx";
1677 ret
= GetTempPathA(MAX_PATH
, temp_path
);
1678 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
1679 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1681 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
1682 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1684 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1685 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1686 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
1688 SetLastError(12345678);
1690 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
1691 ok(ret
&& GetLastError() == 12345678,
1692 "ret = %d, error %d\n", ret
, GetLastError());
1693 ok(!bytes
, "bytes = %d\n", bytes
);
1695 SetLastError(12345678);
1697 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
1698 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
1699 (ret
&& GetLastError() == 12345678), /* Win9x */
1700 "ret = %d, error %d\n", ret
, GetLastError());
1701 ok(!bytes
|| /* Win2k */
1702 bytes
== 10, /* Win9x */
1703 "bytes = %d\n", bytes
);
1705 /* make sure the file contains data */
1706 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
1707 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
1709 SetLastError(12345678);
1711 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
1712 ok(ret
&& GetLastError() == 12345678,
1713 "ret = %d, error %d\n", ret
, GetLastError());
1714 ok(!bytes
, "bytes = %d\n", bytes
);
1716 SetLastError(12345678);
1718 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
1719 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
1720 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
1721 "ret = %d, error %d\n", ret
, GetLastError());
1722 ok(!bytes
, "bytes = %d\n", bytes
);
1724 ret
= CloseHandle(hFile
);
1725 ok( ret
, "CloseHandle: error %d\n", GetLastError());
1726 ret
= DeleteFileA(filename
);
1727 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
1730 static void test_OpenFile(void)
1737 static const char file
[] = "regedit.exe";
1738 static const char foo
[] = ".\\foo-bar-foo.baz";
1739 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
1740 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1741 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1742 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1743 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1744 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1745 char buff
[MAX_PATH
];
1746 char buff_long
[4*MAX_PATH
];
1747 char filled_0xA5
[OFS_MAXPATHNAME
];
1751 /* Check for existing file */
1752 length
= GetWindowsDirectoryA(buff
, MAX_PATH
);
1754 if (length
+ sizeof(file
) < MAX_PATH
)
1756 p
= buff
+ strlen(buff
);
1757 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
1759 memset(&ofs
, 0xA5, sizeof(ofs
));
1760 SetLastError(0xfaceabee);
1762 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
1763 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
1764 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1765 "GetLastError() returns %d\n", GetLastError() );
1766 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1767 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1768 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1769 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1770 ofs
.szPathName
, buff
);
1773 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
1774 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
1776 /* Check for nonexistent file */
1777 if (length
+ sizeof(foo
) < MAX_PATH
)
1779 p
= buff
+ strlen(buff
);
1780 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
1781 strcpy( p
, foo
+ 2 );
1782 memset(&ofs
, 0xA5, sizeof(ofs
));
1783 SetLastError(0xfaceabee);
1785 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
1786 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1787 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
1789 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1790 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1791 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1792 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1793 ofs
.szPathName
, buff
);
1796 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
1797 length
+= lstrlenA(foo_too_long
+ 1);
1799 /* Check for nonexistent file with too long filename */
1800 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
1802 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
1803 memset(&ofs
, 0xA5, sizeof(ofs
));
1804 SetLastError(0xfaceabee);
1806 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
1807 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1808 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
1809 "GetLastError() returns %d\n", GetLastError() );
1811 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1812 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
1813 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1814 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1815 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1819 length
= GetCurrentDirectoryA(MAX_PATH
, buff
) + sizeof(filename
);
1821 if (length
>= MAX_PATH
)
1823 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
1826 p
= buff
+ strlen(buff
);
1827 if (p
> buff
&& p
[-1] != '\\') *p
++ = '\\';
1828 strcpy( p
, filename
);
1830 memset(&ofs
, 0xA5, sizeof(ofs
));
1831 SetLastError(0xfaceabee);
1832 /* Create an empty file */
1833 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
1834 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
1835 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1836 "GetLastError() returns %d\n", GetLastError() );
1837 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1838 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1839 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1840 ret
= CloseHandle((HANDLE
)hFile
);
1841 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1842 retval
= GetFileAttributesA(filename
);
1843 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
1845 memset(&ofs
, 0xA5, sizeof(ofs
));
1846 SetLastError(0xfaceabee);
1847 /* Check various opening options: */
1848 /* for reading only, */
1849 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
1850 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
1851 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1852 "GetLastError() returns %d\n", GetLastError() );
1853 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1854 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1855 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1856 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1857 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1858 ret
= CloseHandle((HANDLE
)hFile
);
1859 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1861 memset(&ofs
, 0xA5, sizeof(ofs
));
1862 SetLastError(0xfaceabee);
1863 /* for writing only, */
1864 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
1865 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
1866 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1867 "GetLastError() returns %d\n", GetLastError() );
1868 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1869 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1870 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1871 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1872 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1873 ret
= CloseHandle((HANDLE
)hFile
);
1874 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1876 memset(&ofs
, 0xA5, sizeof(ofs
));
1877 SetLastError(0xfaceabee);
1878 /* for reading and writing, */
1879 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
1880 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
1881 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1882 "GetLastError() returns %d\n", GetLastError() );
1883 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1884 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1885 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1886 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1887 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1888 ret
= CloseHandle((HANDLE
)hFile
);
1889 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1891 memset(&ofs
, 0xA5, sizeof(ofs
));
1892 SetLastError(0xfaceabee);
1893 /* for checking file presence. */
1894 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
1895 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
1896 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1897 "GetLastError() returns %d\n", GetLastError() );
1898 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1899 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1900 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1901 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1902 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1904 memset(&ofs
, 0xA5, sizeof(ofs
));
1905 SetLastError(0xfaceabee);
1906 /* Delete the file and make sure it doesn't exist anymore */
1907 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
1908 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
1909 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1910 "GetLastError() returns %d\n", GetLastError() );
1911 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1912 ok( ofs
.nErrCode
== ERROR_SUCCESS
|| broken(ofs
.nErrCode
!= ERROR_SUCCESS
) /* win9x */,
1913 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1914 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1915 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1917 retval
= GetFileAttributesA(filename
);
1918 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
1921 static void test_overlapped(void)
1926 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1928 memset( &ov
, 0, sizeof ov
);
1930 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1932 ok( result
== 0, "wrong result %u\n", result
);
1934 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
1938 ov
.InternalHigh
= 0xabcd;
1939 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1941 ok( result
== 0xabcd, "wrong result %u\n", result
);
1943 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
1945 SetLastError( 0xb00 );
1947 ov
.Internal
= STATUS_INVALID_HANDLE
;
1948 ov
.InternalHigh
= 0xabcd;
1949 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1950 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
1951 ok( r
== FALSE
, "should return false\n");
1952 ok( result
== 0xabcd || result
== 0 /* win9x */, "wrong result %u\n", result
);
1954 SetLastError( 0xb00 );
1956 ov
.Internal
= STATUS_PENDING
;
1957 ov
.InternalHigh
= 0xabcd;
1958 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1959 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
1960 "wrong error %u\n", GetLastError() );
1961 ok( r
== FALSE
, "should return false\n");
1962 ok( result
== 0, "wrong result %u\n", result
);
1964 SetLastError( 0xb00 );
1965 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
1966 ov
.Internal
= STATUS_PENDING
;
1967 ov
.InternalHigh
= 0xabcd;
1968 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1969 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
1970 "wrong error %u\n", GetLastError() );
1971 ok( r
== FALSE
, "should return false\n");
1973 ResetEvent( ov
.hEvent
);
1975 SetLastError( 0xb00 );
1976 ov
.Internal
= STATUS_PENDING
;
1977 ov
.InternalHigh
= 0;
1978 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1979 ok( GetLastError() == ERROR_IO_INCOMPLETE
|| GetLastError() == ERROR_INVALID_HANDLE
/* win9x */,
1980 "wrong error %u\n", GetLastError() );
1981 ok( r
== FALSE
, "should return false\n");
1983 r
= CloseHandle( ov
.hEvent
);
1984 ok( r
== TRUE
, "close handle failed\n");
1987 static void test_RemoveDirectory(void)
1990 char directory
[] = "removeme";
1992 rc
= CreateDirectory(directory
, NULL
);
1993 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
1995 rc
= SetCurrentDirectory(directory
);
1996 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1998 rc
= RemoveDirectory(".");
2001 rc
= SetCurrentDirectory("..");
2002 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2004 rc
= RemoveDirectory(directory
);
2005 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2009 static BOOL
check_file_time( const FILETIME
*ft1
, const FILETIME
*ft2
, UINT tolerance
)
2011 ULONGLONG t1
= ((ULONGLONG
)ft1
->dwHighDateTime
<< 32) | ft1
->dwLowDateTime
;
2012 ULONGLONG t2
= ((ULONGLONG
)ft2
->dwHighDateTime
<< 32) | ft2
->dwLowDateTime
;
2013 return abs(t1
- t2
) <= tolerance
;
2016 static void test_ReplaceFileA(void)
2018 char replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2019 HANDLE hReplacedFile
, hReplacementFile
, hBackupFile
;
2020 static const char replacedData
[] = "file-to-replace";
2021 static const char replacementData
[] = "new-file";
2022 static const char backupData
[] = "backup-file";
2023 FILETIME ftReplaced
, ftReplacement
, ftBackup
;
2024 static const char prefix
[] = "pfx";
2025 char temp_path
[MAX_PATH
];
2031 skip("ReplaceFileA() is missing\n");
2035 ret
= GetTempPathA(MAX_PATH
, temp_path
);
2036 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
2037 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2039 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replaced
);
2040 ok(ret
!= 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2042 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2043 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2045 ret
= GetTempFileNameA(temp_path
, prefix
, 0, backup
);
2046 ok(ret
!= 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2048 /* place predictable data in the file to be replaced */
2049 hReplacedFile
= CreateFileA(replaced
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2050 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2051 "failed to open replaced file\n");
2052 retok
= WriteFile(hReplacedFile
, replacedData
, sizeof(replacedData
), &ret
, NULL
);
2053 ok( retok
&& ret
== sizeof(replacedData
),
2054 "WriteFile error (replaced) %d\n", GetLastError());
2055 ok(GetFileSize(hReplacedFile
, NULL
) == sizeof(replacedData
),
2056 "replaced file has wrong size\n");
2057 /* place predictable data in the file to be the replacement */
2058 hReplacementFile
= CreateFileA(replacement
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2059 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
,
2060 "failed to open replacement file\n");
2061 retok
= WriteFile(hReplacementFile
, replacementData
, sizeof(replacementData
), &ret
, NULL
);
2062 ok( retok
&& ret
== sizeof(replacementData
),
2063 "WriteFile error (replacement) %d\n", GetLastError());
2064 ok(GetFileSize(hReplacementFile
, NULL
) == sizeof(replacementData
),
2065 "replacement file has wrong size\n");
2066 /* place predictable data in the backup file (to be over-written) */
2067 hBackupFile
= CreateFileA(backup
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
2068 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2069 "failed to open backup file\n");
2070 retok
= WriteFile(hBackupFile
, backupData
, sizeof(backupData
), &ret
, NULL
);
2071 ok( retok
&& ret
== sizeof(backupData
),
2072 "WriteFile error (replacement) %d\n", GetLastError());
2073 ok(GetFileSize(hBackupFile
, NULL
) == sizeof(backupData
),
2074 "backup file has wrong size\n");
2075 /* change the filetime on the "replaced" file to ensure that it changes */
2076 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2077 ok( ret
, "GetFileTime error (replaced) %d\n", GetLastError());
2078 ftReplaced
.dwLowDateTime
-= 600000000; /* 60 second */
2079 ret
= SetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2080 ok( ret
, "SetFileTime error (replaced) %d\n", GetLastError());
2081 GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
); /* get the actual time back */
2082 CloseHandle(hReplacedFile
);
2083 /* change the filetime on the backup to ensure that it changes */
2084 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2085 ok( ret
, "GetFileTime error (backup) %d\n", GetLastError());
2086 ftBackup
.dwLowDateTime
-= 1200000000; /* 120 second */
2087 ret
= SetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2088 ok( ret
, "SetFileTime error (backup) %d\n", GetLastError());
2089 GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
); /* get the actual time back */
2090 CloseHandle(hBackupFile
);
2091 /* get the filetime on the replacement file to perform checks */
2092 ret
= GetFileTime(hReplacementFile
, NULL
, NULL
, &ftReplacement
);
2093 ok( ret
, "GetFileTime error (replacement) %d\n", GetLastError());
2094 CloseHandle(hReplacementFile
);
2096 /* perform replacement w/ backup
2097 * TODO: flags are not implemented
2099 SetLastError(0xdeadbeef);
2100 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2101 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2102 /* make sure that the backup has the size of the old "replaced" file */
2103 hBackupFile
= CreateFileA(backup
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2104 ok(hBackupFile
!= INVALID_HANDLE_VALUE
,
2105 "failed to open backup file\n");
2106 ret
= GetFileSize(hBackupFile
, NULL
);
2107 ok(ret
== sizeof(replacedData
),
2108 "backup file has wrong size %d\n", ret
);
2109 /* make sure that the "replaced" file has the size of the replacement file */
2110 hReplacedFile
= CreateFileA(replaced
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2111 ok(hReplacedFile
!= INVALID_HANDLE_VALUE
,
2112 "failed to open replaced file\n");
2113 ret
= GetFileSize(hReplacedFile
, NULL
);
2114 ok(ret
== sizeof(replacementData
),
2115 "replaced file has wrong size %d\n", ret
);
2116 /* make sure that the replacement file no-longer exists */
2117 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2118 ok(hReplacementFile
== INVALID_HANDLE_VALUE
,
2119 "unexpected error, replacement file should not exist %d\n", GetLastError());
2120 /* make sure that the backup has the old "replaced" filetime */
2121 ret
= GetFileTime(hBackupFile
, NULL
, NULL
, &ftBackup
);
2122 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2123 ok(check_file_time(&ftBackup
, &ftReplaced
, 20000000), "backup file has wrong filetime\n");
2124 CloseHandle(hBackupFile
);
2125 /* make sure that the "replaced" has the old replacement filetime */
2126 ret
= GetFileTime(hReplacedFile
, NULL
, NULL
, &ftReplaced
);
2127 ok( ret
, "GetFileTime error (backup %d\n", GetLastError());
2128 ok(check_file_time(&ftReplaced
, &ftReplacement
, 20000000),
2129 "replaced file has wrong filetime %x%08x / %x%08x\n",
2130 ftReplaced
.dwHighDateTime
, ftReplaced
.dwLowDateTime
,
2131 ftReplacement
.dwHighDateTime
, ftReplacement
.dwLowDateTime
);
2132 CloseHandle(hReplacedFile
);
2134 /* re-create replacement file for pass w/o backup (blank) */
2135 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2136 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2137 /* perform replacement w/o backup
2138 * TODO: flags are not implemented
2140 SetLastError(0xdeadbeef);
2141 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2142 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2144 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2145 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2146 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2147 ret
= DeleteFileA(backup
);
2148 ok(ret
, "DeleteFileA: error (backup) %d\n", GetLastError());
2149 /* perform replacement w/ backup (no pre-existing backup)
2150 * TODO: flags are not implemented
2152 SetLastError(0xdeadbeef);
2153 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2154 ok(ret
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2156 /* re-create replacement file for pass w/ no permissions to "replaced" */
2157 ret
= GetTempFileNameA(temp_path
, prefix
, 0, replacement
);
2158 ok(ret
!= 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2159 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_READONLY
);
2160 ok(ret
, "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2161 /* perform replacement w/ backup (no permission to "replaced")
2162 * TODO: flags are not implemented
2164 SetLastError(0xdeadbeef);
2165 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2166 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
, "ReplaceFileA: unexpected error %d\n", GetLastError());
2167 /* make sure that the replacement file still exists */
2168 hReplacementFile
= CreateFileA(replacement
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
2169 ok(hReplacementFile
!= INVALID_HANDLE_VALUE
||
2170 broken(GetLastError() == ERROR_FILE_NOT_FOUND
), /* win2k */
2171 "unexpected error, replacement file should still exist %d\n", GetLastError());
2172 CloseHandle(hReplacementFile
);
2173 ret
= SetFileAttributesA(replaced
, FILE_ATTRIBUTE_NORMAL
);
2174 ok(ret
, "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2176 /* replacement file still exists, make pass w/o "replaced" */
2177 ret
= DeleteFileA(replaced
);
2178 ok(ret
, "DeleteFileA: error (replaced) %d\n", GetLastError());
2179 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2180 * TODO: flags are not implemented
2182 SetLastError(0xdeadbeef);
2183 ret
= pReplaceFileA(replaced
, replacement
, backup
, 0, 0, 0);
2184 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
2185 "ReplaceFileA: unexpected error %d\n", GetLastError());
2187 /* perform replacement w/o existing "replacement" file
2188 * TODO: flags are not implemented
2190 SetLastError(0xdeadbeef);
2191 ret
= pReplaceFileA(replaced
, replacement
, NULL
, 0, 0, 0);
2192 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
2193 "ReplaceFileA: unexpected error %d\n", GetLastError());
2196 * if the first round (w/ backup) worked then as long as there is no
2197 * failure then there is no need to check this round (w/ backup is the
2198 * more complete case)
2201 /* delete temporary files, replacement and replaced are already deleted */
2202 ret
= DeleteFileA(backup
);
2204 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2205 "DeleteFileA: error (backup) %d\n", GetLastError());
2209 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2210 * need to be as thorough.
2212 static void test_ReplaceFileW(void)
2214 WCHAR replaced
[MAX_PATH
], replacement
[MAX_PATH
], backup
[MAX_PATH
];
2215 static const WCHAR prefix
[] = {'p','f','x',0};
2216 WCHAR temp_path
[MAX_PATH
];
2221 skip("ReplaceFileW() is missing\n");
2225 ret
= GetTempPathW(MAX_PATH
, temp_path
);
2226 if (ret
==0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
2228 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
2229 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
2231 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replaced
);
2232 ok(ret
!= 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2234 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2235 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2237 ret
= GetTempFileNameW(temp_path
, prefix
, 0, backup
);
2238 ok(ret
!= 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2240 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2241 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2243 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2244 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2245 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2246 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2248 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2249 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2250 ret
= DeleteFileW(backup
);
2251 ok(ret
, "DeleteFileW: error (backup) %d\n", GetLastError());
2252 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2253 ok(ret
, "ReplaceFileW: error %d\n", GetLastError());
2255 ret
= GetTempFileNameW(temp_path
, prefix
, 0, replacement
);
2256 ok(ret
!= 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2257 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_READONLY
);
2258 ok(ret
, "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2260 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2261 ok(ret
!= ERROR_UNABLE_TO_REMOVE_REPLACED
,
2262 "ReplaceFileW: unexpected error %d\n", GetLastError());
2263 ret
= SetFileAttributesW(replaced
, FILE_ATTRIBUTE_NORMAL
);
2264 ok(ret
, "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2266 ret
= DeleteFileW(replaced
);
2267 ok(ret
, "DeleteFileW: error (replaced) %d\n", GetLastError());
2268 ret
= pReplaceFileW(replaced
, replacement
, backup
, 0, 0, 0);
2269 ok(!ret
, "ReplaceFileW: error %d\n", GetLastError());
2271 ret
= pReplaceFileW(replaced
, replacement
, NULL
, 0, 0, 0);
2272 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
2273 "ReplaceFileW: unexpected error %d\n", GetLastError());
2275 ret
= DeleteFileW(backup
);
2277 broken(GetLastError() == ERROR_ACCESS_DENIED
), /* win2k */
2278 "DeleteFileW: error (backup) %d\n", GetLastError());
2283 InitFunctionPointers();
2293 test_GetTempFileNameA();
2302 test_FindFirstFileA();
2303 test_FindNextFileA();
2304 test_FindFirstFileExA();
2306 test_file_sharing();
2307 test_offset_in_overlapped_structure();
2310 test_async_file_errors();
2314 test_RemoveDirectory();
2315 test_ReplaceFileA();
2316 test_ReplaceFileW();