kernel32/tests: Simplify dumpmem function.
[wine/multimedia.git] / dlls / kernel32 / tests / file.c
blob90fdee284f6aef1d0b7f10334a3ed6bd09d51ce6
1 /*
2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 * Copyright (c) 2008 Jeff Zaroyko
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* ReplaceFile requires Windows 2000 or newer */
24 #define _WIN32_WINNT 0x0500
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <stdio.h>
31 #include "wine/test.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
36 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
37 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
38 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
39 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
40 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
42 /* keep filename and filenameW the same */
43 static const char filename[] = "testfile.xxx";
44 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
45 static const char sillytext[] =
46 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
53 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
54 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
55 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
57 struct test_list {
58 const char *file; /* file string to test */
59 const DWORD err; /* Win NT and further error code */
60 const LONG err2; /* Win 9x & ME error code or -1 */
61 const DWORD options; /* option flag to use for open */
62 const BOOL todo_flag; /* todo_wine indicator */
63 } ;
65 static void InitFunctionPointers(void)
67 HMODULE hkernel32 = GetModuleHandleA("kernel32");
69 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
70 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
71 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
72 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
73 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
76 static void test__hread( void )
78 HFILE filehandle;
79 char buffer[10000];
80 LONG bytes_read;
81 LONG bytes_wanted;
82 LONG i;
83 BOOL ret;
85 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
86 DeleteFileA( filename );
87 filehandle = _lcreat( filename, 0 );
88 if (filehandle == HFILE_ERROR)
90 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
91 return;
94 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
96 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
98 filehandle = _lopen( filename, OF_READ );
100 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
102 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
104 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
106 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
108 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
109 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
110 for (i = 0; i < bytes_wanted; i++)
112 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
116 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
118 ret = DeleteFileA( filename );
119 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
123 static void test__hwrite( void )
125 HFILE filehandle;
126 char buffer[10000];
127 LONG bytes_read;
128 LONG bytes_written;
129 ULONG blocks;
130 LONG i;
131 char *contents;
132 HLOCAL memory_object;
133 char checksum[1];
134 BOOL ret;
136 filehandle = _lcreat( filename, 0 );
137 if (filehandle == HFILE_ERROR)
139 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
140 return;
143 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
145 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
147 filehandle = _lopen( filename, OF_READ );
149 bytes_read = _hread( filehandle, buffer, 1);
151 ok( 0 == bytes_read, "file read size error\n" );
153 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
155 filehandle = _lopen( filename, OF_READWRITE );
157 bytes_written = 0;
158 checksum[0] = '\0';
159 srand( (unsigned)time( NULL ) );
160 for (blocks = 0; blocks < 100; blocks++)
162 for (i = 0; i < (LONG)sizeof( buffer ); i++)
164 buffer[i] = rand( );
165 checksum[0] = checksum[0] + buffer[i];
167 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
168 bytes_written = bytes_written + sizeof( buffer );
171 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
172 bytes_written++;
174 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
176 memory_object = LocalAlloc( LPTR, bytes_written );
178 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
180 contents = LocalLock( memory_object );
182 filehandle = _lopen( filename, OF_READ );
184 contents = LocalLock( memory_object );
186 ok( NULL != contents, "LocalLock whines\n" );
188 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
190 checksum[0] = '\0';
191 i = 0;
194 checksum[0] = checksum[0] + contents[i];
195 i++;
197 while (i < bytes_written - 1);
199 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
201 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
203 ret = DeleteFileA( filename );
204 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
208 static void test__lclose( void )
210 HFILE filehandle;
211 BOOL ret;
213 filehandle = _lcreat( filename, 0 );
214 if (filehandle == HFILE_ERROR)
216 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
217 return;
220 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
222 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
224 ret = DeleteFileA( filename );
225 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
229 static void test__lcreat( void )
231 HFILE filehandle;
232 char buffer[10000];
233 WIN32_FIND_DATAA search_results;
234 char slashname[] = "testfi/";
235 int err;
236 HANDLE find;
237 BOOL ret;
239 filehandle = _lcreat( filename, 0 );
240 if (filehandle == HFILE_ERROR)
242 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
243 return;
246 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
248 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
250 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
252 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
254 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
256 ret = DeleteFileA(filename);
257 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
259 filehandle = _lcreat( filename, 1 ); /* readonly */
260 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
262 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
264 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
266 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
268 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
270 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
272 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
274 filehandle = _lcreat( filename, 2 );
275 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
277 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
279 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
281 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
283 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
285 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
287 ret = DeleteFileA( filename );
288 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
290 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
291 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
293 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
295 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
297 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
299 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
301 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
303 ret = DeleteFileA( filename );
304 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
306 filehandle=_lcreat (slashname, 0); /* illegal name */
307 if (HFILE_ERROR==filehandle) {
308 err=GetLastError ();
309 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
310 "creating file \"%s\" failed with error %d\n", slashname, err);
311 } else { /* only NT succeeds */
312 _lclose(filehandle);
313 find=FindFirstFileA (slashname, &search_results);
314 if (INVALID_HANDLE_VALUE!=find)
316 ret = FindClose (find);
317 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
318 slashname[strlen(slashname)-1]=0;
319 ok (!strcmp (slashname, search_results.cFileName),
320 "found unexpected name \"%s\"\n", search_results.cFileName);
321 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
322 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
323 search_results.dwFileAttributes);
325 ret = DeleteFileA( slashname );
326 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
329 filehandle=_lcreat (filename, 8); /* illegal attribute */
330 if (HFILE_ERROR==filehandle)
331 ok (0, "couldn't create volume label \"%s\"\n", filename);
332 else {
333 _lclose(filehandle);
334 find=FindFirstFileA (filename, &search_results);
335 if (INVALID_HANDLE_VALUE==find)
336 ok (0, "file \"%s\" not found\n", filename);
337 else {
338 ret = FindClose(find);
339 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
340 ok (!strcmp (filename, search_results.cFileName),
341 "found unexpected name \"%s\"\n", search_results.cFileName);
342 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
343 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
344 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
345 search_results.dwFileAttributes);
347 ret = DeleteFileA( filename );
348 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
353 static void test__llseek( void )
355 INT i;
356 HFILE filehandle;
357 char buffer[1];
358 LONG bytes_read;
359 BOOL ret;
361 filehandle = _lcreat( filename, 0 );
362 if (filehandle == HFILE_ERROR)
364 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
365 return;
368 for (i = 0; i < 400; i++)
370 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
372 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
373 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
375 bytes_read = _hread( filehandle, buffer, 1);
376 ok( 1 == bytes_read, "file read size error\n" );
377 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
378 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
380 bytes_read = _hread( filehandle, buffer, 1);
381 ok( 1 == bytes_read, "file read size error\n" );
382 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
383 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
384 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
386 ret = DeleteFileA( filename );
387 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
391 static void test__llopen( void )
393 HFILE filehandle;
394 UINT bytes_read;
395 char buffer[10000];
396 BOOL ret;
398 filehandle = _lcreat( filename, 0 );
399 if (filehandle == HFILE_ERROR)
401 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
402 return;
405 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
406 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
408 filehandle = _lopen( filename, OF_READ );
409 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
410 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
411 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
412 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
414 filehandle = _lopen( filename, OF_READWRITE );
415 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
416 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
417 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
418 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
420 filehandle = _lopen( filename, OF_WRITE );
421 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
422 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
423 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
425 ret = DeleteFileA( filename );
426 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
427 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
431 static void test__lread( void )
433 HFILE filehandle;
434 char buffer[10000];
435 UINT bytes_read;
436 UINT bytes_wanted;
437 UINT i;
438 BOOL ret;
440 filehandle = _lcreat( filename, 0 );
441 if (filehandle == HFILE_ERROR)
443 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
444 return;
447 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
449 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
451 filehandle = _lopen( filename, OF_READ );
453 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
455 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
457 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
459 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
461 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
462 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
463 for (i = 0; i < bytes_wanted; i++)
465 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
469 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
471 ret = DeleteFileA( filename );
472 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
476 static void test__lwrite( void )
478 HFILE filehandle;
479 char buffer[10000];
480 UINT bytes_read;
481 UINT bytes_written;
482 UINT blocks;
483 INT i;
484 char *contents;
485 HLOCAL memory_object;
486 char checksum[1];
487 BOOL ret;
489 filehandle = _lcreat( filename, 0 );
490 if (filehandle == HFILE_ERROR)
492 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
493 return;
496 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
498 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
500 filehandle = _lopen( filename, OF_READ );
502 bytes_read = _hread( filehandle, buffer, 1);
504 ok( 0 == bytes_read, "file read size error\n" );
506 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
508 filehandle = _lopen( filename, OF_READWRITE );
510 bytes_written = 0;
511 checksum[0] = '\0';
512 srand( (unsigned)time( NULL ) );
513 for (blocks = 0; blocks < 100; blocks++)
515 for (i = 0; i < (INT)sizeof( buffer ); i++)
517 buffer[i] = rand( );
518 checksum[0] = checksum[0] + buffer[i];
520 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
521 bytes_written = bytes_written + sizeof( buffer );
524 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
525 bytes_written++;
527 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
529 memory_object = LocalAlloc( LPTR, bytes_written );
531 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
533 contents = LocalLock( memory_object );
535 filehandle = _lopen( filename, OF_READ );
537 contents = LocalLock( memory_object );
539 ok( NULL != contents, "LocalLock whines\n" );
541 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
543 checksum[0] = '\0';
544 i = 0;
547 checksum[0] += contents[i];
548 i++;
550 while (i < bytes_written - 1);
552 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
554 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
556 ret = DeleteFileA( filename );
557 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
560 static void test_CopyFileA(void)
562 char temp_path[MAX_PATH];
563 char source[MAX_PATH], dest[MAX_PATH];
564 static const char prefix[] = "pfx";
565 HANDLE hfile;
566 HANDLE hmapfile;
567 FILETIME ft1, ft2;
568 char buf[10];
569 DWORD ret;
570 BOOL retok;
572 ret = GetTempPathA(MAX_PATH, temp_path);
573 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
574 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
576 ret = GetTempFileNameA(temp_path, prefix, 0, source);
577 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
579 ret = MoveFileA(source, source);
580 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
582 /* make the source have not zero size */
583 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
584 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
585 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
586 ok( retok && ret == sizeof(prefix),
587 "WriteFile error %d\n", GetLastError());
588 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
589 /* get the file time and change it to prove the difference */
590 ret = GetFileTime(hfile, NULL, NULL, &ft1);
591 ok( ret, "GetFileTime error %d\n", GetLastError());
592 ft1.dwLowDateTime -= 600000000; /* 60 second */
593 ret = SetFileTime(hfile, NULL, NULL, &ft1);
594 ok( ret, "SetFileTime error %d\n", GetLastError());
595 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
596 CloseHandle(hfile);
598 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
599 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
601 SetLastError(0xdeadbeef);
602 ret = CopyFileA(source, dest, TRUE);
603 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
604 "CopyFileA: unexpected error %d\n", GetLastError());
606 ret = CopyFileA(source, dest, FALSE);
607 ok(ret, "CopyFileA: error %d\n", GetLastError());
609 /* make sure that destination has correct size */
610 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
611 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
612 ret = GetFileSize(hfile, NULL);
613 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
615 /* make sure that destination has the same filetime */
616 ret = GetFileTime(hfile, NULL, NULL, &ft2);
617 ok( ret, "GetFileTime error %d\n", GetLastError());
618 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
620 SetLastError(0xdeadbeef);
621 ret = CopyFileA(source, dest, FALSE);
622 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
623 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
625 /* make sure that destination still has correct size */
626 ret = GetFileSize(hfile, NULL);
627 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
628 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
629 ok( retok && ret == sizeof(prefix),
630 "ReadFile: error %d\n", GetLastError());
631 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
633 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
634 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
635 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
637 ret = CopyFileA(source, dest, FALSE);
638 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
639 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
641 CloseHandle(hmapfile);
642 CloseHandle(hfile);
644 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
645 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
647 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
648 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
649 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
651 ret = CopyFileA(source, dest, FALSE);
652 ok(!ret, "CopyFileA: expected failure\n");
653 ok(GetLastError() == ERROR_USER_MAPPED_FILE ||
654 broken(GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x */
655 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
657 CloseHandle(hmapfile);
658 CloseHandle(hfile);
660 ret = DeleteFileA(source);
661 ok(ret, "DeleteFileA: error %d\n", GetLastError());
662 ret = DeleteFileA(dest);
663 ok(ret, "DeleteFileA: error %d\n", GetLastError());
666 static void test_CopyFileW(void)
668 WCHAR temp_path[MAX_PATH];
669 WCHAR source[MAX_PATH], dest[MAX_PATH];
670 static const WCHAR prefix[] = {'p','f','x',0};
671 DWORD ret;
673 ret = GetTempPathW(MAX_PATH, temp_path);
674 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
676 win_skip("GetTempPathW is not available\n");
677 return;
679 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
680 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
682 ret = GetTempFileNameW(temp_path, prefix, 0, source);
683 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
685 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
686 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
688 ret = CopyFileW(source, dest, TRUE);
689 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
690 "CopyFileW: unexpected error %d\n", GetLastError());
692 ret = CopyFileW(source, dest, FALSE);
693 ok(ret, "CopyFileW: error %d\n", GetLastError());
695 ret = DeleteFileW(source);
696 ok(ret, "DeleteFileW: error %d\n", GetLastError());
697 ret = DeleteFileW(dest);
698 ok(ret, "DeleteFileW: error %d\n", GetLastError());
703 * Debugging routine to dump a buffer in a hexdump-like fashion.
705 static void dumpmem(unsigned char *mem, int len)
707 int x = 0;
708 char hex[49], *p;
709 char txt[17], *c;
711 while (x < len)
713 p = hex;
714 c = txt;
715 do {
716 p += sprintf(p, "%02hhx ", mem[x]);
717 *c++ = (mem[x] >= 32 && mem[x] <= 127) ? mem[x] : '.';
718 } while (++x % 16 && x < len);
719 *c = '\0';
720 trace("%04x: %-48s- %s\n", x, hex, txt);
724 static void test_CreateFileA(void)
726 HANDLE hFile;
727 char temp_path[MAX_PATH], dirname[MAX_PATH];
728 char filename[MAX_PATH];
729 static const char prefix[] = "pfx";
730 char windowsdir[MAX_PATH];
731 char Volume_1[MAX_PATH];
732 unsigned char buffer[512];
733 char directory[] = "removeme";
734 static const char nt_drive[] = "\\\\?\\A:";
735 DWORD i, ret, len;
736 struct test_list p[] = {
737 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
738 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
739 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
740 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
741 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
742 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
743 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
744 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
745 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
746 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
747 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
748 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
749 {NULL, 0, -1, 0, FALSE}
751 BY_HANDLE_FILE_INFORMATION Finfo;
753 ret = GetTempPathA(MAX_PATH, temp_path);
754 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
755 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
757 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
758 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
760 SetLastError(0xdeadbeef);
761 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
762 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
763 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
764 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
766 SetLastError(0xdeadbeef);
767 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
768 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
769 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
770 "hFile %p, last error %u\n", hFile, GetLastError());
772 CloseHandle(hFile);
774 SetLastError(0xdeadbeef);
775 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
776 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
777 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
778 "hFile %p, last error %u\n", hFile, GetLastError());
780 CloseHandle(hFile);
782 ret = DeleteFileA(filename);
783 ok(ret, "DeleteFileA: error %d\n", GetLastError());
785 SetLastError(0xdeadbeef);
786 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
787 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
788 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
789 "hFile %p, last error %u\n", hFile, GetLastError());
791 CloseHandle(hFile);
793 ret = DeleteFileA(filename);
794 ok(ret, "DeleteFileA: error %d\n", GetLastError());
796 /* get windows drive letter */
797 ret = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
798 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
799 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
801 /* test error return codes from CreateFile for some cases */
802 ret = GetTempPathA(MAX_PATH, temp_path);
803 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
804 strcpy(dirname, temp_path);
805 strcat(dirname, directory);
806 ret = CreateDirectory(dirname, NULL);
807 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
808 /* set current drive & directory to known location */
809 SetCurrentDirectoryA( temp_path );
810 i = 0;
811 while (p[i].file)
813 filename[0] = 0;
814 /* update the drive id in the table entry with the current one */
815 if (p[i].file[1] == ':')
817 strcpy(filename, p[i].file);
818 filename[0] = windowsdir[0];
820 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
822 strcpy(filename, p[i].file);
823 filename[4] = windowsdir[0];
825 else
827 /* prefix the table entry with the current temp directory */
828 strcpy(filename, temp_path);
829 strcat(filename, p[i].file);
831 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
832 FILE_SHARE_READ | FILE_SHARE_WRITE,
833 NULL, OPEN_EXISTING,
834 p[i].options, NULL );
835 /* if we get ACCESS_DENIED when we do not expect it, assume
836 * no access to the volume
838 if (hFile == INVALID_HANDLE_VALUE &&
839 GetLastError() == ERROR_ACCESS_DENIED &&
840 p[i].err != ERROR_ACCESS_DENIED)
842 if (p[i].todo_flag)
843 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
844 else
845 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
847 /* otherwise validate results with expectations */
848 else if (p[i].todo_flag)
849 todo_wine ok(
850 (hFile == INVALID_HANDLE_VALUE &&
851 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
852 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
853 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
854 filename, hFile, GetLastError(), p[i].err);
855 else
857 (hFile == INVALID_HANDLE_VALUE &&
858 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
859 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
860 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
861 filename, hFile, GetLastError(), p[i].err);
862 if (hFile != INVALID_HANDLE_VALUE)
863 CloseHandle( hFile );
864 i++;
866 ret = RemoveDirectoryA(dirname);
867 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
870 /* test opening directory as a directory */
871 hFile = CreateFileA( temp_path, GENERIC_READ,
872 FILE_SHARE_READ,
873 NULL,
874 OPEN_EXISTING,
875 FILE_FLAG_BACKUP_SEMANTICS, NULL );
876 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
878 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
879 "CreateFileA did not work, last error %u on volume <%s>\n",
880 GetLastError(), temp_path );
882 if (hFile != INVALID_HANDLE_VALUE)
884 ret = GetFileInformationByHandle( hFile, &Finfo );
885 if (ret)
887 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
888 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
889 temp_path, Finfo.dwFileAttributes);
891 CloseHandle( hFile );
894 else
895 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
898 /* *** Test opening volumes/devices using drive letter *** */
900 /* test using drive letter in non-rewrite format without trailing \ */
901 /* this should work */
902 strcpy(filename, nt_drive);
903 filename[4] = windowsdir[0];
904 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
905 FILE_SHARE_READ | FILE_SHARE_WRITE,
906 NULL, OPEN_EXISTING,
907 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
908 if (hFile != INVALID_HANDLE_VALUE ||
909 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
911 /* if we have adm rights to volume, then try rest of tests */
912 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
913 filename, GetLastError());
914 if (hFile != INVALID_HANDLE_VALUE)
916 /* if we opened the volume/device, try to read it. Since it */
917 /* opened, we should be able to read it. We don't care about*/
918 /* what the data is at this time. */
919 len = 512;
920 ret = ReadFile( hFile, buffer, len, &len, NULL );
921 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
922 GetLastError(), ret, filename);
923 if (ret)
925 trace("buffer is\n");
926 dumpmem(buffer, 64);
928 CloseHandle( hFile );
931 /* test using drive letter with trailing \ and in non-rewrite */
932 /* this should not work */
933 strcpy(filename, nt_drive);
934 filename[4] = windowsdir[0];
935 strcat( filename, "\\" );
936 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
937 FILE_SHARE_READ | FILE_SHARE_WRITE,
938 NULL, OPEN_EXISTING,
939 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
940 todo_wine
941 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
942 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
943 filename, GetLastError());
944 if (hFile != INVALID_HANDLE_VALUE)
945 CloseHandle( hFile );
947 /* test using temp path with trailing \ and in non-rewrite as dir */
948 /* this should work */
949 strcpy(filename, nt_drive);
950 filename[4] = 0;
951 strcat( filename, temp_path );
952 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
953 FILE_SHARE_READ | FILE_SHARE_WRITE,
954 NULL, OPEN_EXISTING,
955 FILE_FLAG_BACKUP_SEMANTICS, NULL );
956 ok(hFile != INVALID_HANDLE_VALUE,
957 "CreateFileA should have worked on %s, but got %u\n",
958 filename, GetLastError());
959 if (hFile != INVALID_HANDLE_VALUE)
960 CloseHandle( hFile );
962 /* test using drive letter without trailing \ and in device ns */
963 /* this should work */
964 strcpy(filename, nt_drive);
965 filename[4] = windowsdir[0];
966 filename[2] = '.';
967 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
968 FILE_SHARE_READ | FILE_SHARE_WRITE,
969 NULL, OPEN_EXISTING,
970 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
971 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
972 filename, GetLastError());
973 if (hFile != INVALID_HANDLE_VALUE)
974 CloseHandle( hFile );
976 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
977 else if (GetLastError() == ERROR_BAD_NETPATH)
978 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
979 else
980 skip("Do not have authority to access volumes. Tests skipped\n");
983 /* *** Test opening volumes/devices using GUID *** */
985 if (pGetVolumeNameForVolumeMountPointA)
987 strcpy(filename, "c:\\");
988 filename[0] = windowsdir[0];
989 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
990 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
991 if (ret)
993 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
995 /* test the result of opening a unique volume name (GUID)
996 * with the trailing \
997 * this should error out
999 strcpy(filename, Volume_1);
1000 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1001 FILE_SHARE_READ | FILE_SHARE_WRITE,
1002 NULL, OPEN_EXISTING,
1003 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1004 todo_wine
1005 ok(hFile == INVALID_HANDLE_VALUE,
1006 "CreateFileA should not have opened %s, hFile %p\n",
1007 filename, hFile);
1008 todo_wine
1009 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1010 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1011 filename, GetLastError());
1012 if (hFile != INVALID_HANDLE_VALUE)
1013 CloseHandle( hFile );
1015 /* test the result of opening a unique volume name (GUID)
1016 * with the temp path string as dir
1017 * this should work
1019 strcpy(filename, Volume_1);
1020 strcat(filename, temp_path+3);
1021 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1022 FILE_SHARE_READ | FILE_SHARE_WRITE,
1023 NULL, OPEN_EXISTING,
1024 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1025 todo_wine
1026 ok(hFile != INVALID_HANDLE_VALUE,
1027 "CreateFileA should have opened %s, but got %u\n",
1028 filename, GetLastError());
1029 if (hFile != INVALID_HANDLE_VALUE)
1030 CloseHandle( hFile );
1032 /* test the result of opening a unique volume name (GUID)
1033 * without the trailing \ and in device namespace
1034 * this should work
1036 strcpy(filename, Volume_1);
1037 filename[2] = '.';
1038 filename[48] = 0;
1039 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1040 FILE_SHARE_READ | FILE_SHARE_WRITE,
1041 NULL, OPEN_EXISTING,
1042 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1043 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1045 /* if we have adm rights to volume, then try rest of tests */
1046 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1047 filename, GetLastError());
1048 if (hFile != INVALID_HANDLE_VALUE)
1050 /* if we opened the volume/device, try to read it. Since it */
1051 /* opened, we should be able to read it. We don't care about*/
1052 /* what the data is at this time. */
1053 len = 512;
1054 ret = ReadFile( hFile, buffer, len, &len, NULL );
1055 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1056 GetLastError(), ret, filename);
1057 if (ret)
1059 trace("buffer is\n");
1060 dumpmem(buffer, 64);
1062 CloseHandle( hFile );
1065 else
1066 skip("Do not have authority to access volumes. Tests skipped\n");
1068 else
1069 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1071 else
1072 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1075 static void test_CreateFileW(void)
1077 HANDLE hFile;
1078 WCHAR temp_path[MAX_PATH];
1079 WCHAR filename[MAX_PATH];
1080 static const WCHAR emptyW[]={'\0'};
1081 static const WCHAR prefix[] = {'p','f','x',0};
1082 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1083 DWORD ret;
1085 ret = GetTempPathW(MAX_PATH, temp_path);
1086 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1088 win_skip("GetTempPathW is not available\n");
1089 return;
1091 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1092 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1094 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1095 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1097 SetLastError(0xdeadbeef);
1098 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1099 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1100 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1101 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1103 SetLastError(0xdeadbeef);
1104 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1105 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1106 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1107 "hFile %p, last error %u\n", hFile, GetLastError());
1109 CloseHandle(hFile);
1111 SetLastError(0xdeadbeef);
1112 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1113 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1114 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1115 "hFile %p, last error %u\n", hFile, GetLastError());
1117 CloseHandle(hFile);
1119 ret = DeleteFileW(filename);
1120 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1122 SetLastError(0xdeadbeef);
1123 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1124 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1125 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1126 "hFile %p, last error %u\n", hFile, GetLastError());
1128 CloseHandle(hFile);
1130 ret = DeleteFileW(filename);
1131 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1133 if (0)
1135 /* this crashes on NT4.0 */
1136 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1137 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1138 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1139 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1142 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1143 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1144 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1145 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1147 /* test the result of opening a nonexistent driver name */
1148 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1149 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1150 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1151 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1153 ret = CreateDirectoryW(filename, NULL);
1154 ok(ret == TRUE, "couldn't create temporary directory\n");
1155 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1156 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1157 ok(hFile != INVALID_HANDLE_VALUE,
1158 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1159 CloseHandle(hFile);
1160 ret = RemoveDirectoryW(filename);
1161 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1164 static void test_GetTempFileNameA(void)
1166 UINT result;
1167 char out[MAX_PATH];
1168 char expected[MAX_PATH + 10];
1169 char windowsdir[MAX_PATH + 10];
1170 char windowsdrive[3];
1172 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1173 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1174 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1176 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1177 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1179 strcat(windowsdir, "\\");
1182 windowsdrive[0] = windowsdir[0];
1183 windowsdrive[1] = windowsdir[1];
1184 windowsdrive[2] = '\0';
1186 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1187 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1188 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1189 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1190 windowsdrive[0], out);
1192 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1193 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1194 expected[0] = '\0';
1195 strcat(expected, windowsdir);
1196 strcat(expected, "abc2.tmp");
1197 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1198 out, expected);
1201 static void test_DeleteFileA( void )
1203 BOOL ret;
1205 ret = DeleteFileA(NULL);
1206 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1207 GetLastError() == ERROR_PATH_NOT_FOUND),
1208 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1210 ret = DeleteFileA("");
1211 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1212 GetLastError() == ERROR_BAD_PATHNAME),
1213 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1215 ret = DeleteFileA("nul");
1216 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1217 GetLastError() == ERROR_INVALID_PARAMETER ||
1218 GetLastError() == ERROR_ACCESS_DENIED ||
1219 GetLastError() == ERROR_INVALID_FUNCTION),
1220 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1223 static void test_DeleteFileW( void )
1225 BOOL ret;
1226 WCHAR pathW[MAX_PATH];
1227 WCHAR pathsubW[MAX_PATH];
1228 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1229 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1230 static const WCHAR emptyW[]={'\0'};
1232 ret = DeleteFileW(NULL);
1233 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1235 win_skip("DeleteFileW is not available\n");
1236 return;
1238 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1239 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1241 ret = DeleteFileW(emptyW);
1242 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1243 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1245 /* test DeleteFile on empty directory */
1246 ret = GetTempPathW(MAX_PATH, pathW);
1247 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1249 ok(0, "MAX_PATH exceeded in constructing paths\n");
1250 return;
1252 lstrcatW(pathW, dirW);
1253 lstrcpyW(pathsubW, pathW);
1254 lstrcatW(pathsubW, subdirW);
1255 ret = CreateDirectoryW(pathW, NULL);
1256 ok(ret == TRUE, "couldn't create directory deletefile\n");
1257 ret = DeleteFileW(pathW);
1258 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1259 ret = RemoveDirectoryW(pathW);
1260 ok(ret == TRUE, "expected to remove directory deletefile\n");
1262 /* test DeleteFile on non-empty directory */
1263 ret = CreateDirectoryW(pathW, NULL);
1264 ok(ret == TRUE, "couldn't create directory deletefile\n");
1265 ret = CreateDirectoryW(pathsubW, NULL);
1266 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1267 ret = DeleteFileW(pathW);
1268 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1269 ret = RemoveDirectoryW(pathsubW);
1270 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1271 ret = RemoveDirectoryW(pathW);
1272 ok(ret == TRUE, "expected to remove directory deletefile\n");
1275 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1277 static void test_MoveFileA(void)
1279 char tempdir[MAX_PATH];
1280 char source[MAX_PATH], dest[MAX_PATH];
1281 static const char prefix[] = "pfx";
1282 HANDLE hfile;
1283 HANDLE hmapfile;
1284 DWORD ret;
1285 BOOL retok;
1287 ret = GetTempPathA(MAX_PATH, tempdir);
1288 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1289 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1291 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1292 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1294 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1295 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1297 ret = MoveFileA(source, dest);
1298 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1299 "MoveFileA: unexpected error %d\n", GetLastError());
1301 ret = DeleteFileA(dest);
1302 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1304 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1305 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1307 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1308 ok( retok && ret == sizeof(prefix),
1309 "WriteFile error %d\n", GetLastError());
1311 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1312 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1314 ret = MoveFileA(source, dest);
1315 todo_wine {
1316 ok(!ret, "MoveFileA: expected failure\n");
1317 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1318 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1319 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1322 CloseHandle(hmapfile);
1323 CloseHandle(hfile);
1325 /* if MoveFile succeeded, move back to dest */
1326 if (ret) MoveFile(dest, source);
1328 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1329 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1331 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1332 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1334 ret = MoveFileA(source, dest);
1335 todo_wine {
1336 ok(!ret, "MoveFileA: expected failure\n");
1337 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1338 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1339 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1342 CloseHandle(hmapfile);
1343 CloseHandle(hfile);
1345 /* if MoveFile succeeded, move back to dest */
1346 if (ret) MoveFile(dest, source);
1348 ret = MoveFileA(source, dest);
1349 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1351 lstrcatA(tempdir, "Remove Me");
1352 ret = CreateDirectoryA(tempdir, NULL);
1353 ok(ret == TRUE, "CreateDirectoryA failed\n");
1355 lstrcpyA(source, dest);
1356 lstrcpyA(dest, tempdir);
1357 lstrcatA(dest, "\\wild?.*");
1358 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1359 ret = MoveFileA(source, dest);
1360 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1361 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1362 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1363 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1364 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1366 WIN32_FIND_DATAA fd;
1367 char temppath[MAX_PATH];
1368 HANDLE hFind;
1370 lstrcpyA(temppath, tempdir);
1371 lstrcatA(temppath, "\\*.*");
1372 hFind = FindFirstFileA(temppath, &fd);
1373 if (INVALID_HANDLE_VALUE != hFind)
1375 LPSTR lpName;
1378 lpName = fd.cAlternateFileName;
1379 if (!lpName[0])
1380 lpName = fd.cFileName;
1381 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1383 while (FindNextFileA(hFind, &fd));
1384 FindClose(hFind);
1387 ret = DeleteFileA(source);
1388 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1389 ret = DeleteFileA(dest);
1390 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1391 ret = RemoveDirectoryA(tempdir);
1392 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1395 static void test_MoveFileW(void)
1397 WCHAR temp_path[MAX_PATH];
1398 WCHAR source[MAX_PATH], dest[MAX_PATH];
1399 static const WCHAR prefix[] = {'p','f','x',0};
1400 DWORD ret;
1402 ret = GetTempPathW(MAX_PATH, temp_path);
1403 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1405 win_skip("GetTempPathW is not available\n");
1406 return;
1408 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1409 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1411 ret = GetTempFileNameW(temp_path, prefix, 0, source);
1412 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1414 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
1415 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1417 ret = MoveFileW(source, dest);
1418 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1419 "CopyFileW: unexpected error %d\n", GetLastError());
1421 ret = DeleteFileW(source);
1422 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1423 ret = DeleteFileW(dest);
1424 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1427 #define PATTERN_OFFSET 0x10
1429 static void test_offset_in_overlapped_structure(void)
1431 HANDLE hFile;
1432 OVERLAPPED ov;
1433 DWORD done, offset;
1434 BOOL rc;
1435 BYTE buf[256], pattern[] = "TeSt";
1436 UINT i;
1437 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
1438 BOOL ret;
1440 ret =GetTempPathA(MAX_PATH, temp_path);
1441 ok( ret, "GetTempPathA error %d\n", GetLastError());
1442 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
1443 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
1445 /*** Write File *****************************************************/
1447 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1448 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1450 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
1451 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
1452 ok( ret, "WriteFile error %d\n", GetLastError());
1453 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
1455 memset(&ov, 0, sizeof(ov));
1456 S(U(ov)).Offset = PATTERN_OFFSET;
1457 S(U(ov)).OffsetHigh = 0;
1458 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1459 /* Win 9x does not support the overlapped I/O on files */
1460 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1461 ok(rc, "WriteFile error %d\n", GetLastError());
1462 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1463 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1464 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1466 S(U(ov)).Offset = sizeof(buf) * 2;
1467 S(U(ov)).OffsetHigh = 0;
1468 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1469 ok( ret, "WriteFile error %d\n", GetLastError());
1470 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1471 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1472 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1475 CloseHandle(hFile);
1477 /*** Read File *****************************************************/
1479 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1480 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1482 memset(buf, 0, sizeof(buf));
1483 memset(&ov, 0, sizeof(ov));
1484 S(U(ov)).Offset = PATTERN_OFFSET;
1485 S(U(ov)).OffsetHigh = 0;
1486 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1487 /* Win 9x does not support the overlapped I/O on files */
1488 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1489 ok(rc, "ReadFile error %d\n", GetLastError());
1490 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1491 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1492 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1493 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1496 CloseHandle(hFile);
1498 ret = DeleteFileA(temp_fname);
1499 ok( ret, "DeleteFileA error %d\n", GetLastError());
1502 static void test_LockFile(void)
1504 HANDLE handle;
1505 DWORD written;
1506 OVERLAPPED overlapped;
1507 int limited_LockFile;
1508 int limited_UnLockFile;
1509 BOOL ret;
1511 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1512 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1513 CREATE_ALWAYS, 0, 0 );
1514 if (handle == INVALID_HANDLE_VALUE)
1516 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1517 return;
1519 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1521 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1522 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1524 limited_UnLockFile = 0;
1525 if (UnlockFile( handle, 0, 0, 0, 0 ))
1527 limited_UnLockFile = 1;
1530 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1531 /* overlapping locks must fail */
1532 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1533 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1534 /* non-overlapping locks must succeed */
1535 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1537 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1538 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1539 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1540 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1542 S(U(overlapped)).Offset = 100;
1543 S(U(overlapped)).OffsetHigh = 0;
1544 overlapped.hEvent = 0;
1546 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1547 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1549 /* LockFileEx is probably OK, test it more. */
1550 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1551 "LockFileEx 100,100 failed\n" );
1554 /* overlapping shared locks are OK */
1555 S(U(overlapped)).Offset = 150;
1556 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1558 /* but exclusive is not */
1559 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1560 0, 50, 0, &overlapped ),
1561 "LockFileEx exclusive 150,50 succeeded\n" );
1562 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1563 { /* UnLockFile is capable. */
1564 S(U(overlapped)).Offset = 100;
1565 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1566 "UnlockFileEx 150,100 again succeeded\n" );
1569 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
1570 if (ret)
1572 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1573 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1574 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1576 else /* win9x */
1577 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
1579 /* wrap-around lock should not do anything */
1580 /* (but still succeeds on NT4 so we don't check result) */
1581 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1583 limited_LockFile = 0;
1584 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1586 limited_LockFile = 1;
1589 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1591 /* zero-byte lock */
1592 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1593 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1594 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1595 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1597 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1598 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1600 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1602 CloseHandle( handle );
1603 DeleteFileA( filename );
1606 static BOOL create_fake_dll( LPCSTR filename )
1608 IMAGE_DOS_HEADER *dos;
1609 IMAGE_NT_HEADERS *nt;
1610 IMAGE_SECTION_HEADER *sec;
1611 BYTE *buffer;
1612 DWORD lfanew = sizeof(*dos);
1613 DWORD size = lfanew + sizeof(*nt) + sizeof(*sec);
1614 DWORD written;
1615 BOOL ret;
1617 HANDLE file = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1618 if (file == INVALID_HANDLE_VALUE) return FALSE;
1620 buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
1622 dos = (IMAGE_DOS_HEADER *)buffer;
1623 dos->e_magic = IMAGE_DOS_SIGNATURE;
1624 dos->e_cblp = sizeof(*dos);
1625 dos->e_cp = 1;
1626 dos->e_cparhdr = lfanew / 16;
1627 dos->e_minalloc = 0;
1628 dos->e_maxalloc = 0xffff;
1629 dos->e_ss = 0x0000;
1630 dos->e_sp = 0x00b8;
1631 dos->e_lfarlc = lfanew;
1632 dos->e_lfanew = lfanew;
1634 nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
1635 nt->Signature = IMAGE_NT_SIGNATURE;
1636 #if defined __i386__
1637 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
1638 #elif defined __x86_64__
1639 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
1640 #elif defined __powerpc__
1641 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
1642 #else
1643 # error You must specify the machine type
1644 #endif
1645 nt->FileHeader.NumberOfSections = 1;
1646 nt->FileHeader.SizeOfOptionalHeader = IMAGE_SIZEOF_NT_OPTIONAL_HEADER;
1647 nt->FileHeader.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_EXECUTABLE_IMAGE;
1648 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
1649 nt->OptionalHeader.MajorLinkerVersion = 1;
1650 nt->OptionalHeader.MinorLinkerVersion = 0;
1651 nt->OptionalHeader.ImageBase = 0x10000000;
1652 nt->OptionalHeader.SectionAlignment = 0x1000;
1653 nt->OptionalHeader.FileAlignment = 0x1000;
1654 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
1655 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
1656 nt->OptionalHeader.MajorImageVersion = 1;
1657 nt->OptionalHeader.MinorImageVersion = 0;
1658 nt->OptionalHeader.MajorSubsystemVersion = 4;
1659 nt->OptionalHeader.MinorSubsystemVersion = 0;
1660 nt->OptionalHeader.SizeOfImage = 0x2000;
1661 nt->OptionalHeader.SizeOfHeaders = size;
1662 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
1663 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1665 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
1666 memcpy( sec->Name, ".rodata", sizeof(".rodata") );
1667 sec->Misc.VirtualSize = 0x1000;
1668 sec->VirtualAddress = 0x1000;
1669 sec->SizeOfRawData = 0;
1670 sec->PointerToRawData = 0;
1671 sec->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
1673 ret = WriteFile( file, buffer, size, &written, NULL ) && written == size;
1674 HeapFree( GetProcessHeap(), 0, buffer );
1675 CloseHandle( file );
1676 return ret;
1679 static int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1681 if (!is_win9x)
1683 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1684 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1686 else
1688 access1 &= ~DELETE;
1689 if (!access1) access1 = GENERIC_READ;
1691 access2 &= ~DELETE;
1692 if (!access2) access2 = GENERIC_READ;
1695 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1696 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1697 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1698 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1699 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1700 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1701 return 1;
1704 static int is_sharing_map_compatible( DWORD map_access, DWORD access2, DWORD sharing2 )
1706 if ((map_access == PAGE_READWRITE || map_access == PAGE_EXECUTE_READWRITE) &&
1707 !(sharing2 & FILE_SHARE_WRITE)) return 0;
1708 if ((map_access & SEC_IMAGE) && (access2 & GENERIC_WRITE)) return 0;
1709 return 1;
1712 static void test_file_sharing(void)
1714 static const DWORD access_modes[] =
1715 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1716 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1717 static const DWORD sharing_modes[] =
1718 { 0, FILE_SHARE_READ,
1719 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1720 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1721 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1722 static const DWORD mapping_modes[] =
1723 { PAGE_READONLY, PAGE_WRITECOPY, PAGE_READWRITE, SEC_IMAGE | PAGE_WRITECOPY };
1724 int a1, s1, a2, s2;
1725 int ret;
1726 HANDLE h, h2;
1727 BOOL is_win9x = FALSE;
1729 /* make sure the file exists */
1730 if (!create_fake_dll( filename ))
1732 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1733 return;
1735 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1737 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1739 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1741 /* Win9x doesn't support FILE_SHARE_DELETE */
1742 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1743 continue;
1745 SetLastError(0xdeadbeef);
1746 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1747 NULL, OPEN_EXISTING, 0, 0 );
1748 if (h == INVALID_HANDLE_VALUE)
1750 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1751 return;
1753 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1755 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1757 /* Win9x doesn't support FILE_SHARE_DELETE */
1758 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1759 continue;
1761 SetLastError(0xdeadbeef);
1762 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1763 NULL, OPEN_EXISTING, 0, 0 );
1764 ret = GetLastError();
1765 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1766 access_modes[a2], sharing_modes[s2], is_win9x ))
1768 ok( h2 != INVALID_HANDLE_VALUE,
1769 "open failed for modes %x/%x/%x/%x\n",
1770 access_modes[a1], sharing_modes[s1],
1771 access_modes[a2], sharing_modes[s2] );
1772 ok( ret == 0xdeadbeef /* Win9x */ ||
1773 ret == 0, /* XP */
1774 "wrong error code %d\n", ret );
1776 else
1778 ok( h2 == INVALID_HANDLE_VALUE,
1779 "open succeeded for modes %x/%x/%x/%x\n",
1780 access_modes[a1], sharing_modes[s1],
1781 access_modes[a2], sharing_modes[s2] );
1782 ok( ret == ERROR_SHARING_VIOLATION,
1783 "wrong error code %d\n", ret );
1785 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1788 CloseHandle( h );
1792 for (a1 = 0; a1 < sizeof(mapping_modes)/sizeof(mapping_modes[0]); a1++)
1794 HANDLE m;
1796 create_fake_dll( filename );
1797 SetLastError(0xdeadbeef);
1798 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1799 if (h == INVALID_HANDLE_VALUE)
1801 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1802 return;
1804 m = CreateFileMappingA( h, NULL, mapping_modes[a1], 0, 0, NULL );
1805 ok( m != 0, "failed to create mapping %x err %u\n", mapping_modes[a1], GetLastError() );
1806 CloseHandle( h );
1807 if (!m) continue;
1809 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1811 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1813 /* Win9x doesn't support FILE_SHARE_DELETE */
1814 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1815 continue;
1817 SetLastError(0xdeadbeef);
1818 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1819 NULL, OPEN_EXISTING, 0, 0 );
1821 ret = GetLastError();
1822 if (h2 == INVALID_HANDLE_VALUE)
1824 if (is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]))
1825 ok( is_win9x, /* there's no sharing at all with a mapping on win9x */
1826 "open failed for modes map %x/%x/%x\n",
1827 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
1828 ok( ret == ERROR_SHARING_VIOLATION,
1829 "wrong error code %d\n", ret );
1831 else
1833 if (!is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]))
1834 ok( broken(1), /* no checking on nt4 */
1835 "open succeeded for modes map %x/%x/%x\n",
1836 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
1837 ok( ret == 0xdeadbeef /* Win9x */ ||
1838 ret == 0, /* XP */
1839 "wrong error code %d\n", ret );
1840 CloseHandle( h2 );
1845 /* try CREATE_ALWAYS over an existing mapping */
1846 SetLastError(0xdeadbeef);
1847 h2 = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
1848 NULL, CREATE_ALWAYS, 0, 0 );
1849 ret = GetLastError();
1850 if ((mapping_modes[a1] & SEC_IMAGE) || is_win9x)
1852 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1853 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1855 else
1857 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1858 ok( ret == ERROR_USER_MAPPED_FILE, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1860 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1862 /* try DELETE_ON_CLOSE over an existing mapping */
1863 SetLastError(0xdeadbeef);
1864 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1865 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 );
1866 ret = GetLastError();
1867 if (is_win9x)
1869 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1870 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1872 else if (mapping_modes[a1] & SEC_IMAGE)
1874 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1875 ok( ret == ERROR_ACCESS_DENIED, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1877 else
1879 ok( h2 != INVALID_HANDLE_VALUE, "open failed for map %x err %u\n", mapping_modes[a1], ret );
1881 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1883 CloseHandle( m );
1886 SetLastError(0xdeadbeef);
1887 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1888 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1890 SetLastError(0xdeadbeef);
1891 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1892 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1893 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1895 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1896 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1898 CloseHandle(h);
1899 CloseHandle(h2);
1901 DeleteFileA( filename );
1904 static char get_windows_drive(void)
1906 char windowsdir[MAX_PATH];
1907 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1908 return windowsdir[0];
1911 static void test_FindFirstFileA(void)
1913 HANDLE handle;
1914 WIN32_FIND_DATAA data;
1915 int err;
1916 char buffer[5] = "C:\\";
1917 char buffer2[100];
1918 char nonexistent[MAX_PATH];
1920 /* try FindFirstFileA on "C:\" */
1921 buffer[0] = get_windows_drive();
1923 SetLastError( 0xdeadbeaf );
1924 handle = FindFirstFileA(buffer, &data);
1925 err = GetLastError();
1926 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1927 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1929 /* try FindFirstFileA on "C:\*" */
1930 strcpy(buffer2, buffer);
1931 strcat(buffer2, "*");
1932 handle = FindFirstFileA(buffer2, &data);
1933 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1934 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1935 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1936 if (FindNextFileA( handle, &data ))
1937 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1938 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1939 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1941 /* try FindFirstFileA on windows dir */
1942 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1943 strcat(buffer2, "\\*");
1944 handle = FindFirstFileA(buffer2, &data);
1945 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1946 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1947 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1948 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1949 while (FindNextFileA( handle, &data ))
1950 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1951 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1952 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1954 /* try FindFirstFileA on "C:\foo\" */
1955 SetLastError( 0xdeadbeaf );
1956 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
1958 char tmp[MAX_PATH];
1959 GetTempPathA( sizeof(tmp), tmp );
1960 GetTempFileNameA( tmp, "foo", 0, nonexistent );
1962 DeleteFileA( nonexistent );
1963 strcpy(buffer2, nonexistent);
1964 strcat(buffer2, "\\");
1965 handle = FindFirstFileA(buffer2, &data);
1966 err = GetLastError();
1967 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1968 todo_wine {
1969 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1972 /* try FindFirstFileA on "C:\foo\bar.txt" */
1973 SetLastError( 0xdeadbeaf );
1974 strcpy(buffer2, nonexistent);
1975 strcat(buffer2, "\\bar.txt");
1976 handle = FindFirstFileA(buffer2, &data);
1977 err = GetLastError();
1978 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1979 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1981 /* try FindFirstFileA on "C:\foo\*.*" */
1982 SetLastError( 0xdeadbeaf );
1983 strcpy(buffer2, nonexistent);
1984 strcat(buffer2, "\\*.*");
1985 handle = FindFirstFileA(buffer2, &data);
1986 err = GetLastError();
1987 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1988 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1990 /* try FindFirstFileA on "foo\bar.txt" */
1991 SetLastError( 0xdeadbeaf );
1992 strcpy(buffer2, nonexistent + 3);
1993 strcat(buffer2, "\\bar.txt");
1994 handle = FindFirstFileA(buffer2, &data);
1995 err = GetLastError();
1996 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1997 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1999 /* try FindFirstFileA on "c:\nul" */
2000 SetLastError( 0xdeadbeaf );
2001 strcpy(buffer2, buffer);
2002 strcat(buffer2, "nul");
2003 handle = FindFirstFileA(buffer2, &data);
2004 err = GetLastError();
2005 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
2006 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
2007 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2008 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2009 "wrong attributes %x\n", data.dwFileAttributes );
2010 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2012 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2013 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2015 SetLastError( 0xdeadbeaf );
2016 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2017 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2018 ok( FindClose( handle ), "failed to close handle\n" );
2020 /* try FindFirstFileA on "lpt1" */
2021 SetLastError( 0xdeadbeaf );
2022 strcpy(buffer2, "lpt1");
2023 handle = FindFirstFileA(buffer2, &data);
2024 err = GetLastError();
2025 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
2026 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
2027 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2028 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2029 "wrong attributes %x\n", data.dwFileAttributes );
2030 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2032 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2033 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2035 SetLastError( 0xdeadbeaf );
2036 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2037 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2038 ok( FindClose( handle ), "failed to close handle\n" );
2040 /* try FindFirstFileA on "c:\nul\*" */
2041 SetLastError( 0xdeadbeaf );
2042 strcpy(buffer2, buffer);
2043 strcat(buffer2, "nul\\*");
2044 handle = FindFirstFileA(buffer2, &data);
2045 err = GetLastError();
2046 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2047 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2049 /* try FindFirstFileA on "c:\nul*" */
2050 SetLastError( 0xdeadbeaf );
2051 strcpy(buffer2, buffer);
2052 strcat(buffer2, "nul*");
2053 handle = FindFirstFileA(buffer2, &data);
2054 err = GetLastError();
2055 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2056 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2058 /* try FindFirstFileA on "c:\foo\bar\nul" */
2059 SetLastError( 0xdeadbeaf );
2060 strcpy(buffer2, buffer);
2061 strcat(buffer2, "foo\\bar\\nul");
2062 handle = FindFirstFileA(buffer2, &data);
2063 err = GetLastError();
2064 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2065 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2067 /* try FindFirstFileA on "c:\foo\nul\bar" */
2068 SetLastError( 0xdeadbeaf );
2069 strcpy(buffer2, buffer);
2070 strcat(buffer2, "foo\\nul\\bar");
2071 handle = FindFirstFileA(buffer2, &data);
2072 err = GetLastError();
2073 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2074 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2077 static void test_FindNextFileA(void)
2079 HANDLE handle;
2080 WIN32_FIND_DATAA search_results;
2081 int err;
2082 char buffer[5] = "C:\\*";
2084 buffer[0] = get_windows_drive();
2085 handle = FindFirstFileA(buffer,&search_results);
2086 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
2087 while (FindNextFile(handle, &search_results))
2089 /* get to the end of the files */
2091 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
2092 err = GetLastError();
2093 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
2096 static void test_FindFirstFileExA(FINDEX_SEARCH_OPS search_ops)
2098 WIN32_FIND_DATAA search_results;
2099 HANDLE handle;
2100 BOOL ret;
2102 if (!pFindFirstFileExA)
2104 win_skip("FindFirstFileExA() is missing\n");
2105 return;
2108 CreateDirectoryA("test-dir", NULL);
2109 _lclose(_lcreat("test-dir\\file1", 0));
2110 _lclose(_lcreat("test-dir\\file2", 0));
2111 CreateDirectoryA("test-dir\\dir1", NULL);
2112 SetLastError(0xdeadbeef);
2113 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, search_ops, NULL, 0);
2114 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2116 win_skip("FindFirstFileExA is not implemented\n");
2117 goto cleanup;
2119 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
2120 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
2122 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2124 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
2125 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
2127 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
2128 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
2130 SetLastError(0xdeadbeef);
2131 ret = FindNextFile(handle, &search_results);
2132 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
2134 skip("File system supports directory filtering\n");
2135 /* Results from the previous call are not cleared */
2136 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
2137 FindClose( handle );
2138 goto cleanup;
2141 ok(ret, "Fetching fourth file failed\n");
2142 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
2144 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
2145 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
2147 #undef CHECK_NAME
2149 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
2151 FindClose( handle );
2153 cleanup:
2154 DeleteFileA("test-dir\\file1");
2155 DeleteFileA("test-dir\\file2");
2156 RemoveDirectoryA("test-dir\\dir1");
2157 RemoveDirectoryA("test-dir");
2160 static int test_Mapfile_createtemp(HANDLE *handle)
2162 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
2163 DeleteFile(filename);
2164 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
2165 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2166 if (*handle != INVALID_HANDLE_VALUE) {
2168 return 1;
2171 return 0;
2174 static void test_MapFile(void)
2176 HANDLE handle;
2177 HANDLE hmap;
2179 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2181 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
2182 ok( hmap != NULL, "mapping should work, I named it!\n" );
2184 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2186 /* We have to close file before we try new stuff with mapping again.
2187 Else we would always succeed on XP or block descriptors on 95. */
2188 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2189 ok( hmap != NULL, "We should still be able to map!\n" );
2190 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2191 ok( CloseHandle( handle ), "can't close file handle\n");
2192 handle = NULL;
2194 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2196 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2197 ok( hmap == NULL, "mapped zero size file\n");
2198 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
2200 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
2201 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2202 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2203 if ( hmap )
2204 CloseHandle( hmap );
2206 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
2207 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2208 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2209 if ( hmap )
2210 CloseHandle( hmap );
2212 /* On XP you can now map again, on Win 95 you cannot. */
2214 ok( CloseHandle( handle ), "can't close file handle\n");
2215 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
2218 static void test_GetFileType(void)
2220 DWORD type;
2221 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2222 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
2223 type = GetFileType(h);
2224 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
2225 CloseHandle( h );
2226 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2227 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
2228 type = GetFileType(h);
2229 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
2230 CloseHandle( h );
2231 DeleteFileA( filename );
2234 static int completion_count;
2236 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2238 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2239 ReleaseSemaphore(ovl->hEvent, 1, NULL);
2240 completion_count++;
2243 static void test_async_file_errors(void)
2245 char szFile[MAX_PATH];
2246 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
2247 HANDLE hFile;
2248 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
2249 OVERLAPPED ovl;
2250 S(U(ovl)).Offset = 0;
2251 S(U(ovl)).OffsetHigh = 0;
2252 ovl.hEvent = hSem;
2253 completion_count = 0;
2254 szFile[0] = '\0';
2255 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
2256 strcat(szFile, "\\win.ini");
2257 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2258 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2259 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
2260 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2261 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2262 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2263 while (TRUE)
2265 BOOL res;
2266 DWORD count;
2267 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2269 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2270 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2271 if (!res)
2272 break;
2273 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2274 break;
2275 S(U(ovl)).Offset += count;
2276 /* i/o completion routine only called if ReadFileEx returned success.
2277 * we only care about violations of this rule so undo what should have
2278 * been done */
2279 completion_count--;
2281 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2282 /*printf("Error = %ld\n", GetLastError());*/
2283 HeapFree(GetProcessHeap(), 0, lpBuffer);
2286 static void test_read_write(void)
2288 DWORD bytes, ret, old_prot;
2289 HANDLE hFile;
2290 char temp_path[MAX_PATH];
2291 char filename[MAX_PATH];
2292 char *mem;
2293 static const char prefix[] = "pfx";
2295 ret = GetTempPathA(MAX_PATH, temp_path);
2296 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2297 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2299 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2300 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2302 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2303 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2304 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2306 SetLastError(12345678);
2307 bytes = 12345678;
2308 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2309 ok(ret && GetLastError() == 12345678,
2310 "ret = %d, error %d\n", ret, GetLastError());
2311 ok(!bytes, "bytes = %d\n", bytes);
2313 SetLastError(12345678);
2314 bytes = 12345678;
2315 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2316 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2317 (ret && GetLastError() == 12345678), /* Win9x */
2318 "ret = %d, error %d\n", ret, GetLastError());
2319 ok(!bytes || /* Win2k */
2320 bytes == 10, /* Win9x */
2321 "bytes = %d\n", bytes);
2323 /* make sure the file contains data */
2324 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2325 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
2327 SetLastError(12345678);
2328 bytes = 12345678;
2329 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
2330 ok(ret && GetLastError() == 12345678,
2331 "ret = %d, error %d\n", ret, GetLastError());
2332 ok(!bytes, "bytes = %d\n", bytes);
2334 SetLastError(12345678);
2335 bytes = 12345678;
2336 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
2337 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
2338 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
2339 "ret = %d, error %d\n", ret, GetLastError());
2340 ok(!bytes, "bytes = %d\n", bytes);
2342 /* test passing protected memory as buffer */
2344 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
2345 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
2347 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2348 ok( ret, "WriteFile failed error %u\n", GetLastError() );
2349 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
2351 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
2352 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2354 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2355 ok( !ret, "WriteFile succeeded\n" );
2356 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2357 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2358 "wrong error %u\n", GetLastError() );
2359 ok( bytes == 0, "wrote %x bytes\n", bytes );
2361 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
2362 ok( !ret, "WriteFile succeeded\n" );
2363 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
2364 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2365 "wrong error %u\n", GetLastError() );
2366 ok( bytes == 0, "wrote %x bytes\n", bytes );
2368 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
2369 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2371 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2372 ok( !ret, "WriteFile succeeded\n" );
2373 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2374 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2375 "wrong error %u\n", GetLastError() );
2376 ok( bytes == 0, "wrote %x bytes\n", bytes );
2378 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2380 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2381 ok( !ret, "ReadFile succeeded\n" );
2382 ok( GetLastError() == ERROR_NOACCESS ||
2383 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2384 "wrong error %u\n", GetLastError() );
2385 ok( bytes == 0, "read %x bytes\n", bytes );
2387 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
2388 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2390 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2391 ok( !ret, "ReadFile succeeded\n" );
2392 ok( GetLastError() == ERROR_NOACCESS ||
2393 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2394 "wrong error %u\n", GetLastError() );
2395 ok( bytes == 0, "read %x bytes\n", bytes );
2397 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
2398 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2400 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2401 ok( !ret, "ReadFile succeeded\n" );
2402 ok( GetLastError() == ERROR_NOACCESS ||
2403 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2404 "wrong error %u\n", GetLastError() );
2405 ok( bytes == 0, "read %x bytes\n", bytes );
2407 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
2408 SetEndOfFile( hFile );
2409 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2411 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2412 ok( !ret, "ReadFile succeeded\n" );
2413 ok( GetLastError() == ERROR_NOACCESS ||
2414 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2415 "wrong error %u\n", GetLastError() );
2416 ok( bytes == 0, "read %x bytes\n", bytes );
2418 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
2419 ok( ret, "ReadFile failed error %u\n", GetLastError() );
2420 ok( bytes == 0x1234, "read %x bytes\n", bytes );
2422 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
2423 ok( !ret, "ReadFile succeeded\n" );
2424 ok( GetLastError() == ERROR_NOACCESS ||
2425 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2426 "wrong error %u\n", GetLastError() );
2427 ok( bytes == 0, "read %x bytes\n", bytes );
2429 VirtualFree( mem, 0, MEM_FREE );
2431 ret = CloseHandle(hFile);
2432 ok( ret, "CloseHandle: error %d\n", GetLastError());
2433 ret = DeleteFileA(filename);
2434 ok( ret, "DeleteFileA: error %d\n", GetLastError());
2437 static void test_OpenFile(void)
2439 HFILE hFile;
2440 OFSTRUCT ofs;
2441 BOOL ret;
2442 DWORD retval;
2444 static const char file[] = "regedit.exe";
2445 static const char foo[] = ".\\foo-bar-foo.baz";
2446 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
2447 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2448 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2449 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2450 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2451 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2452 char buff[MAX_PATH];
2453 char buff_long[4*MAX_PATH];
2454 char filled_0xA5[OFS_MAXPATHNAME];
2455 char *p;
2456 UINT length;
2458 /* Check for existing file */
2459 if (!pGetSystemWindowsDirectoryA)
2460 length = GetWindowsDirectoryA(buff, MAX_PATH);
2461 else
2462 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
2464 if (length + sizeof(file) < MAX_PATH)
2466 p = buff + strlen(buff);
2467 if (p > buff && p[-1] != '\\') *p++ = '\\';
2468 strcpy( p, file );
2469 memset(&ofs, 0xA5, sizeof(ofs));
2470 SetLastError(0xfaceabee);
2472 hFile = OpenFile(buff, &ofs, OF_EXIST);
2473 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
2474 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2475 "GetLastError() returns %d\n", GetLastError() );
2476 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2477 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2478 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2479 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2480 ofs.szPathName, buff );
2483 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
2484 length = GetCurrentDirectoryA(MAX_PATH, buff);
2486 /* Check for nonexistent file */
2487 if (length + sizeof(foo) < MAX_PATH)
2489 p = buff + strlen(buff);
2490 if (p > buff && p[-1] != '\\') *p++ = '\\';
2491 strcpy( p, foo + 2 );
2492 memset(&ofs, 0xA5, sizeof(ofs));
2493 SetLastError(0xfaceabee);
2495 hFile = OpenFile(foo, &ofs, OF_EXIST);
2496 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2497 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
2498 todo_wine
2499 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2500 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2501 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2502 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2503 ofs.szPathName, buff );
2506 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
2507 length += lstrlenA(foo_too_long + 1);
2509 /* Check for nonexistent file with too long filename */
2510 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
2512 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
2513 memset(&ofs, 0xA5, sizeof(ofs));
2514 SetLastError(0xfaceabee);
2516 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
2517 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2518 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
2519 "GetLastError() returns %d\n", GetLastError() );
2520 todo_wine
2521 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2522 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
2523 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2524 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2525 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2526 ofs.szPathName );
2529 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
2531 if (length >= MAX_PATH)
2533 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
2534 return;
2536 p = buff + strlen(buff);
2537 if (p > buff && p[-1] != '\\') *p++ = '\\';
2538 strcpy( p, filename );
2540 memset(&ofs, 0xA5, sizeof(ofs));
2541 SetLastError(0xfaceabee);
2542 /* Create an empty file */
2543 hFile = OpenFile(filename, &ofs, OF_CREATE);
2544 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
2545 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2546 "GetLastError() returns %d\n", GetLastError() );
2547 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2548 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2549 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2550 ret = _lclose(hFile);
2551 ok( !ret, "_lclose() returns %d\n", ret );
2552 retval = GetFileAttributesA(filename);
2553 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
2555 memset(&ofs, 0xA5, sizeof(ofs));
2556 SetLastError(0xfaceabee);
2557 /* Check various opening options: */
2558 /* for reading only, */
2559 hFile = OpenFile(filename, &ofs, OF_READ);
2560 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
2561 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2562 "GetLastError() returns %d\n", GetLastError() );
2563 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2564 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2565 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2566 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2567 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2568 ret = _lclose(hFile);
2569 ok( !ret, "_lclose() returns %d\n", ret );
2571 memset(&ofs, 0xA5, sizeof(ofs));
2572 SetLastError(0xfaceabee);
2573 /* for writing only, */
2574 hFile = OpenFile(filename, &ofs, OF_WRITE);
2575 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
2576 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2577 "GetLastError() returns %d\n", GetLastError() );
2578 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2579 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2580 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2581 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2582 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2583 ret = _lclose(hFile);
2584 ok( !ret, "_lclose() returns %d\n", ret );
2586 memset(&ofs, 0xA5, sizeof(ofs));
2587 SetLastError(0xfaceabee);
2588 /* for reading and writing, */
2589 hFile = OpenFile(filename, &ofs, OF_READWRITE);
2590 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
2591 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2592 "GetLastError() returns %d\n", GetLastError() );
2593 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2594 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2595 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2596 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2597 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2598 ret = _lclose(hFile);
2599 ok( !ret, "_lclose() returns %d\n", ret );
2601 memset(&ofs, 0xA5, sizeof(ofs));
2602 SetLastError(0xfaceabee);
2603 /* for checking file presence. */
2604 hFile = OpenFile(filename, &ofs, OF_EXIST);
2605 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
2606 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2607 "GetLastError() returns %d\n", GetLastError() );
2608 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2609 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2610 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2611 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2612 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2614 memset(&ofs, 0xA5, sizeof(ofs));
2615 SetLastError(0xfaceabee);
2616 /* Delete the file and make sure it doesn't exist anymore */
2617 hFile = OpenFile(filename, &ofs, OF_DELETE);
2618 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
2619 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2620 "GetLastError() returns %d\n", GetLastError() );
2621 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2622 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2623 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2624 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2625 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2627 retval = GetFileAttributesA(filename);
2628 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
2631 static void test_overlapped(void)
2633 OVERLAPPED ov;
2634 DWORD r, result;
2636 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2637 if (0) /* tested: WinXP */
2639 GetOverlappedResult(0, NULL, &result, FALSE);
2640 GetOverlappedResult(0, &ov, NULL, FALSE);
2641 GetOverlappedResult(0, NULL, NULL, FALSE);
2644 memset( &ov, 0, sizeof ov );
2645 result = 1;
2646 r = GetOverlappedResult(0, &ov, &result, 0);
2647 if (r)
2648 ok( result == 0, "wrong result %u\n", result );
2649 else /* win9x */
2650 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2652 result = 0;
2653 ov.Internal = 0;
2654 ov.InternalHigh = 0xabcd;
2655 r = GetOverlappedResult(0, &ov, &result, 0);
2656 if (r)
2657 ok( result == 0xabcd, "wrong result %u\n", result );
2658 else /* win9x */
2659 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2661 SetLastError( 0xb00 );
2662 result = 0;
2663 ov.Internal = STATUS_INVALID_HANDLE;
2664 ov.InternalHigh = 0xabcd;
2665 r = GetOverlappedResult(0, &ov, &result, 0);
2666 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2667 ok( r == FALSE, "should return false\n");
2668 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
2670 SetLastError( 0xb00 );
2671 result = 0;
2672 ov.Internal = STATUS_PENDING;
2673 ov.InternalHigh = 0xabcd;
2674 r = GetOverlappedResult(0, &ov, &result, 0);
2675 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2676 "wrong error %u\n", GetLastError() );
2677 ok( r == FALSE, "should return false\n");
2678 ok( result == 0, "wrong result %u\n", result );
2680 SetLastError( 0xb00 );
2681 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
2682 ov.Internal = STATUS_PENDING;
2683 ov.InternalHigh = 0xabcd;
2684 r = GetOverlappedResult(0, &ov, &result, 0);
2685 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2686 "wrong error %u\n", GetLastError() );
2687 ok( r == FALSE, "should return false\n");
2689 ResetEvent( ov.hEvent );
2691 SetLastError( 0xb00 );
2692 ov.Internal = STATUS_PENDING;
2693 ov.InternalHigh = 0;
2694 r = GetOverlappedResult(0, &ov, &result, 0);
2695 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2696 "wrong error %u\n", GetLastError() );
2697 ok( r == FALSE, "should return false\n");
2699 r = CloseHandle( ov.hEvent );
2700 ok( r == TRUE, "close handle failed\n");
2703 static void test_RemoveDirectory(void)
2705 int rc;
2706 char directory[] = "removeme";
2708 rc = CreateDirectory(directory, NULL);
2709 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
2711 rc = SetCurrentDirectory(directory);
2712 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2714 rc = RemoveDirectory(".");
2715 if (!rc)
2717 rc = SetCurrentDirectory("..");
2718 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2720 rc = RemoveDirectory(directory);
2721 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2725 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
2727 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
2728 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
2729 return abs(t1 - t2) <= tolerance;
2732 static void test_ReplaceFileA(void)
2734 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2735 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
2736 static const char replacedData[] = "file-to-replace";
2737 static const char replacementData[] = "new-file";
2738 static const char backupData[] = "backup-file";
2739 FILETIME ftReplaced, ftReplacement, ftBackup;
2740 static const char prefix[] = "pfx";
2741 char temp_path[MAX_PATH];
2742 DWORD ret;
2743 BOOL retok, removeBackup = FALSE;
2745 if (!pReplaceFileA)
2747 win_skip("ReplaceFileA() is missing\n");
2748 return;
2751 ret = GetTempPathA(MAX_PATH, temp_path);
2752 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2753 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2755 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
2756 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2758 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2759 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2761 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
2762 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2764 /* place predictable data in the file to be replaced */
2765 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2766 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2767 "failed to open replaced file\n");
2768 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
2769 ok( retok && ret == sizeof(replacedData),
2770 "WriteFile error (replaced) %d\n", GetLastError());
2771 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
2772 "replaced file has wrong size\n");
2773 /* place predictable data in the file to be the replacement */
2774 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2775 ok(hReplacementFile != INVALID_HANDLE_VALUE,
2776 "failed to open replacement file\n");
2777 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
2778 ok( retok && ret == sizeof(replacementData),
2779 "WriteFile error (replacement) %d\n", GetLastError());
2780 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
2781 "replacement file has wrong size\n");
2782 /* place predictable data in the backup file (to be over-written) */
2783 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2784 ok(hBackupFile != INVALID_HANDLE_VALUE,
2785 "failed to open backup file\n");
2786 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
2787 ok( retok && ret == sizeof(backupData),
2788 "WriteFile error (replacement) %d\n", GetLastError());
2789 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
2790 "backup file has wrong size\n");
2791 /* change the filetime on the "replaced" file to ensure that it changes */
2792 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2793 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
2794 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
2795 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2796 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
2797 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
2798 CloseHandle(hReplacedFile);
2799 /* change the filetime on the backup to ensure that it changes */
2800 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2801 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
2802 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
2803 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2804 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
2805 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
2806 CloseHandle(hBackupFile);
2807 /* get the filetime on the replacement file to perform checks */
2808 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
2809 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
2810 CloseHandle(hReplacementFile);
2812 /* perform replacement w/ backup
2813 * TODO: flags are not implemented
2815 SetLastError(0xdeadbeef);
2816 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2817 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2818 /* make sure that the backup has the size of the old "replaced" file */
2819 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2820 ok(hBackupFile != INVALID_HANDLE_VALUE,
2821 "failed to open backup file\n");
2822 ret = GetFileSize(hBackupFile, NULL);
2823 ok(ret == sizeof(replacedData),
2824 "backup file has wrong size %d\n", ret);
2825 /* make sure that the "replaced" file has the size of the replacement file */
2826 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2827 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2828 "failed to open replaced file: %d\n", GetLastError());
2829 if (hReplacedFile != INVALID_HANDLE_VALUE)
2831 ret = GetFileSize(hReplacedFile, NULL);
2832 ok(ret == sizeof(replacementData),
2833 "replaced file has wrong size %d\n", ret);
2834 /* make sure that the replacement file no-longer exists */
2835 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2836 ok(hReplacementFile == INVALID_HANDLE_VALUE,
2837 "unexpected error, replacement file should not exist %d\n", GetLastError());
2838 /* make sure that the backup has the old "replaced" filetime */
2839 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2840 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2841 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
2842 CloseHandle(hBackupFile);
2843 /* make sure that the "replaced" has the old replacement filetime */
2844 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2845 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2846 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
2847 "replaced file has wrong filetime %x%08x / %x%08x\n",
2848 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
2849 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
2850 CloseHandle(hReplacedFile);
2852 else
2853 skip("couldn't open replacement file, skipping tests\n");
2855 /* re-create replacement file for pass w/o backup (blank) */
2856 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2857 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2858 /* perform replacement w/o backup
2859 * TODO: flags are not implemented
2861 SetLastError(0xdeadbeef);
2862 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2863 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2864 "ReplaceFileA: unexpected error %d\n", GetLastError());
2866 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2867 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2868 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2869 ret = DeleteFileA(backup);
2870 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2871 /* perform replacement w/ backup (no pre-existing backup)
2872 * TODO: flags are not implemented
2874 SetLastError(0xdeadbeef);
2875 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2876 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2877 "ReplaceFileA: unexpected error %d\n", GetLastError());
2878 if (ret)
2879 removeBackup = TRUE;
2881 /* re-create replacement file for pass w/ no permissions to "replaced" */
2882 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2883 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2884 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2885 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2886 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2887 /* perform replacement w/ backup (no permission to "replaced")
2888 * TODO: flags are not implemented
2890 SetLastError(0xdeadbeef);
2891 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2892 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2893 /* make sure that the replacement file still exists */
2894 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2895 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
2896 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
2897 "unexpected error, replacement file should still exist %d\n", GetLastError());
2898 CloseHandle(hReplacementFile);
2899 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2900 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2901 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2903 /* replacement file still exists, make pass w/o "replaced" */
2904 ret = DeleteFileA(replaced);
2905 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2906 "DeleteFileA: error (replaced) %d\n", GetLastError());
2907 /* perform replacement w/ backup (no pre-existing backup or "replaced")
2908 * TODO: flags are not implemented
2910 SetLastError(0xdeadbeef);
2911 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2912 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2913 GetLastError() == ERROR_ACCESS_DENIED),
2914 "ReplaceFileA: unexpected error %d\n", GetLastError());
2916 /* perform replacement w/o existing "replacement" file
2917 * TODO: flags are not implemented
2919 SetLastError(0xdeadbeef);
2920 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2921 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2922 GetLastError() == ERROR_ACCESS_DENIED),
2923 "ReplaceFileA: unexpected error %d\n", GetLastError());
2926 * if the first round (w/ backup) worked then as long as there is no
2927 * failure then there is no need to check this round (w/ backup is the
2928 * more complete case)
2931 /* delete temporary files, replacement and replaced are already deleted */
2932 if (removeBackup)
2934 ret = DeleteFileA(backup);
2935 ok(ret ||
2936 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2937 "DeleteFileA: error (backup) %d\n", GetLastError());
2942 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2943 * need to be as thorough.
2945 static void test_ReplaceFileW(void)
2947 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2948 static const WCHAR prefix[] = {'p','f','x',0};
2949 WCHAR temp_path[MAX_PATH];
2950 DWORD ret;
2951 BOOL removeBackup = FALSE;
2953 if (!pReplaceFileW)
2955 win_skip("ReplaceFileW() is missing\n");
2956 return;
2959 ret = GetTempPathW(MAX_PATH, temp_path);
2960 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2962 win_skip("GetTempPathW is not available\n");
2963 return;
2965 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2966 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2968 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
2969 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2971 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2972 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2974 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
2975 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2977 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2978 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2980 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2981 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2982 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2983 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2984 "ReplaceFileW: error %d\n", GetLastError());
2986 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2987 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2988 ret = DeleteFileW(backup);
2989 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
2990 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2991 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2992 "ReplaceFileW: error %d\n", GetLastError());
2994 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2995 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2996 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
2997 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2998 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3000 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3001 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
3002 "ReplaceFileW: unexpected error %d\n", GetLastError());
3003 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
3004 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3005 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3006 if (ret)
3007 removeBackup = TRUE;
3009 ret = DeleteFileW(replaced);
3010 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
3011 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3012 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
3014 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3015 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3016 GetLastError() == ERROR_ACCESS_DENIED),
3017 "ReplaceFileW: unexpected error %d\n", GetLastError());
3019 if (removeBackup)
3021 ret = DeleteFileW(backup);
3022 ok(ret ||
3023 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3024 "DeleteFileW: error (backup) %d\n", GetLastError());
3028 START_TEST(file)
3030 InitFunctionPointers();
3032 test__hread( );
3033 test__hwrite( );
3034 test__lclose( );
3035 test__lcreat( );
3036 test__llseek( );
3037 test__llopen( );
3038 test__lread( );
3039 test__lwrite( );
3040 test_GetTempFileNameA();
3041 test_CopyFileA();
3042 test_CopyFileW();
3043 test_CreateFileA();
3044 test_CreateFileW();
3045 test_DeleteFileA();
3046 test_DeleteFileW();
3047 test_MoveFileA();
3048 test_MoveFileW();
3049 test_FindFirstFileA();
3050 test_FindNextFileA();
3051 test_FindFirstFileExA(0);
3052 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
3053 test_FindFirstFileExA(FindExSearchLimitToDirectories);
3054 test_LockFile();
3055 test_file_sharing();
3056 test_offset_in_overlapped_structure();
3057 test_MapFile();
3058 test_GetFileType();
3059 test_async_file_errors();
3060 test_read_write();
3061 test_OpenFile();
3062 test_overlapped();
3063 test_RemoveDirectory();
3064 test_ReplaceFileA();
3065 test_ReplaceFileW();