2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/test.h"
31 static int dll_capable(const char *dll, const char *function)
33 HMODULE module = GetModuleHandleA(dll);
34 if (!module) return 0;
36 return (GetProcAddress(module, function) != NULL);
40 LPCSTR filename = "testfile.xxx";
42 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
43 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
44 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
45 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
46 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
54 static void test__hread( void )
63 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
64 DeleteFileA( filename );
65 filehandle = _lcreat( filename, 0 );
66 if (filehandle == HFILE_ERROR)
68 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
72 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
74 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
76 filehandle = _lopen( filename, OF_READ );
78 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
80 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
82 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
84 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
86 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
87 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
88 for (i = 0; i < bytes_wanted; i++)
90 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
94 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
96 ret = DeleteFileA( filename );
97 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
101 static void test__hwrite( void )
110 HLOCAL memory_object;
114 filehandle = _lcreat( filename, 0 );
115 if (filehandle == HFILE_ERROR)
117 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
121 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
123 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
125 filehandle = _lopen( filename, OF_READ );
127 bytes_read = _hread( filehandle, buffer, 1);
129 ok( 0 == bytes_read, "file read size error\n" );
131 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
133 filehandle = _lopen( filename, OF_READWRITE );
137 srand( (unsigned)time( NULL ) );
138 for (blocks = 0; blocks < 100; blocks++)
140 for (i = 0; i < (long)sizeof( buffer ); i++)
143 checksum[0] = checksum[0] + buffer[i];
145 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
146 bytes_written = bytes_written + sizeof( buffer );
149 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
152 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
154 memory_object = LocalAlloc( LPTR, bytes_written );
156 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
158 contents = LocalLock( memory_object );
160 filehandle = _lopen( filename, OF_READ );
162 contents = LocalLock( memory_object );
164 ok( NULL != contents, "LocalLock whines\n" );
166 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
172 checksum[0] = checksum[0] + contents[i];
175 while (i < bytes_written - 1);
177 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
179 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
181 ret = DeleteFileA( filename );
182 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
186 static void test__lclose( void )
191 filehandle = _lcreat( filename, 0 );
192 if (filehandle == HFILE_ERROR)
194 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
198 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
200 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
202 ret = DeleteFileA( filename );
203 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
207 static void test__lcreat( void )
211 WIN32_FIND_DATAA search_results;
212 char slashname[] = "testfi/";
217 filehandle = _lcreat( filename, 0 );
218 if (filehandle == HFILE_ERROR)
220 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
224 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
226 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
228 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
230 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
232 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
234 ret = DeleteFileA(filename);
235 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
237 filehandle = _lcreat( filename, 1 ); /* readonly */
238 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
240 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
242 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
244 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
246 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
248 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
250 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
252 filehandle = _lcreat( filename, 2 );
253 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
255 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
257 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
259 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
261 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
263 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
265 ret = DeleteFileA( filename );
266 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
268 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
269 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
271 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
273 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
275 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
277 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
279 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
281 ret = DeleteFileA( filename );
282 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
284 filehandle=_lcreat (slashname, 0); /* illegal name */
285 if (HFILE_ERROR==filehandle) {
287 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
288 "creating file \"%s\" failed with error %d\n", slashname, err);
289 } else { /* only NT succeeds */
291 find=FindFirstFileA (slashname, &search_results);
292 if (INVALID_HANDLE_VALUE!=find)
294 ret = FindClose (find);
295 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
296 slashname[strlen(slashname)-1]=0;
297 ok (!strcmp (slashname, search_results.cFileName),
298 "found unexpected name \"%s\"\n", search_results.cFileName);
299 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
300 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
301 search_results.dwFileAttributes);
303 ret = DeleteFileA( slashname );
304 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
307 filehandle=_lcreat (filename, 8); /* illegal attribute */
308 if (HFILE_ERROR==filehandle)
309 ok (0, "couldn't create volume label \"%s\"\n", filename);
312 find=FindFirstFileA (filename, &search_results);
313 if (INVALID_HANDLE_VALUE==find)
314 ok (0, "file \"%s\" not found\n", filename);
316 ret = FindClose(find);
317 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
318 ok (!strcmp (filename, search_results.cFileName),
319 "found unexpected name \"%s\"\n", search_results.cFileName);
320 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
321 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
322 search_results.dwFileAttributes);
324 ret = DeleteFileA( filename );
325 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
330 static void test__llseek( void )
338 filehandle = _lcreat( filename, 0 );
339 if (filehandle == HFILE_ERROR)
341 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
345 for (i = 0; i < 400; i++)
347 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
349 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
350 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
352 bytes_read = _hread( filehandle, buffer, 1);
353 ok( 1 == bytes_read, "file read size error\n" );
354 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
355 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
357 bytes_read = _hread( filehandle, buffer, 1);
358 ok( 1 == bytes_read, "file read size error\n" );
359 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
360 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
361 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
363 ret = DeleteFileA( filename );
364 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
368 static void test__llopen( void )
375 filehandle = _lcreat( filename, 0 );
376 if (filehandle == HFILE_ERROR)
378 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
382 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
383 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
385 filehandle = _lopen( filename, OF_READ );
386 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
387 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
388 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
389 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
391 filehandle = _lopen( filename, OF_READWRITE );
392 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
393 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
394 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
395 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
397 filehandle = _lopen( filename, OF_WRITE );
398 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
399 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
400 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
402 ret = DeleteFileA( filename );
403 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
404 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
408 static void test__lread( void )
417 filehandle = _lcreat( filename, 0 );
418 if (filehandle == HFILE_ERROR)
420 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
424 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
426 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
428 filehandle = _lopen( filename, OF_READ );
430 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
432 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
434 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
436 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
438 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
439 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
440 for (i = 0; i < bytes_wanted; i++)
442 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
446 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
448 ret = DeleteFileA( filename );
449 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
453 static void test__lwrite( void )
462 HLOCAL memory_object;
466 filehandle = _lcreat( filename, 0 );
467 if (filehandle == HFILE_ERROR)
469 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
473 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
475 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
477 filehandle = _lopen( filename, OF_READ );
479 bytes_read = _hread( filehandle, buffer, 1);
481 ok( 0 == bytes_read, "file read size error\n" );
483 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
485 filehandle = _lopen( filename, OF_READWRITE );
489 srand( (unsigned)time( NULL ) );
490 for (blocks = 0; blocks < 100; blocks++)
492 for (i = 0; i < (long)sizeof( buffer ); i++)
495 checksum[0] = checksum[0] + buffer[i];
497 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
498 bytes_written = bytes_written + sizeof( buffer );
501 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
504 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
506 memory_object = LocalAlloc( LPTR, bytes_written );
508 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
510 contents = LocalLock( memory_object );
512 filehandle = _lopen( filename, OF_READ );
514 contents = LocalLock( memory_object );
516 ok( NULL != contents, "LocalLock whines\n" );
518 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
524 checksum[0] += contents[i];
527 while (i < bytes_written - 1);
529 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
531 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
533 ret = DeleteFileA( filename );
534 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
537 static void test_CopyFileA(void)
539 char temp_path[MAX_PATH];
540 char source[MAX_PATH], dest[MAX_PATH];
541 static const char prefix[] = "pfx";
548 ret = GetTempPathA(MAX_PATH, temp_path);
549 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
550 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
552 ret = GetTempFileNameA(temp_path, prefix, 0, source);
553 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
555 /* make the source have not zero size */
556 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
557 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
558 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
559 ok( retok && ret == sizeof(prefix),
560 "WriteFile error %d\n", GetLastError());
561 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
562 /* get the file time and change it to prove the difference */
563 ret = GetFileTime(hfile, NULL, NULL, &ft1);
564 ok( ret, "GetFileTime error %d\n", GetLastError());
565 ft1.dwLowDateTime -= 600000000; /* 60 second */
566 ret = SetFileTime(hfile, NULL, NULL, &ft1);
567 ok( ret, "SetFileTime error %d\n", GetLastError());
568 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
571 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
572 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
574 SetLastError(0xdeadbeef);
575 ret = CopyFileA(source, dest, TRUE);
576 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
577 "CopyFileA: unexpected error %d\n", GetLastError());
579 ret = CopyFileA(source, dest, FALSE);
580 ok(ret, "CopyFileA: error %d\n", GetLastError());
582 /* make sure that destination has correct size */
583 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
584 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
585 ret = GetFileSize(hfile, NULL);
586 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
588 /* make sure that destination has the same filetime */
589 ret = GetFileTime(hfile, NULL, NULL, &ft2);
590 ok( ret, "GetFileTime error %d\n", GetLastError());
591 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
593 SetLastError(0xdeadbeef);
594 ret = CopyFileA(source, dest, FALSE);
595 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
596 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
598 /* make sure that destination still has correct size */
599 ret = GetFileSize(hfile, NULL);
600 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
601 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
602 ok( retok && ret == sizeof(prefix),
603 "ReadFile: error %d\n", GetLastError());
604 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
607 ret = DeleteFileA(source);
608 ok(ret, "DeleteFileA: error %d\n", GetLastError());
609 ret = DeleteFileA(dest);
610 ok(ret, "DeleteFileA: error %d\n", GetLastError());
613 static void test_CopyFileW(void)
615 WCHAR temp_path[MAX_PATH];
616 WCHAR source[MAX_PATH], dest[MAX_PATH];
617 static const WCHAR prefix[] = {'p','f','x',0};
620 ret = GetTempPathW(MAX_PATH, temp_path);
621 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
623 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
624 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
626 ret = GetTempFileNameW(temp_path, prefix, 0, source);
627 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
629 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
630 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
632 ret = CopyFileW(source, dest, TRUE);
633 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
634 "CopyFileW: unexpected error %d\n", GetLastError());
636 ret = CopyFileW(source, dest, FALSE);
637 ok(ret, "CopyFileW: error %d\n", GetLastError());
639 ret = DeleteFileW(source);
640 ok(ret, "DeleteFileW: error %d\n", GetLastError());
641 ret = DeleteFileW(dest);
642 ok(ret, "DeleteFileW: error %d\n", GetLastError());
645 static void test_CreateFileA(void)
648 char temp_path[MAX_PATH];
649 char filename[MAX_PATH];
650 static const char prefix[] = "pfx";
653 ret = GetTempPathA(MAX_PATH, temp_path);
654 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
655 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
657 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
658 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
660 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
661 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
662 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
663 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
665 ret = DeleteFileA(filename);
666 ok(ret, "DeleteFileA: error %d\n", GetLastError());
669 static void test_CreateFileW(void)
672 WCHAR temp_path[MAX_PATH];
673 WCHAR filename[MAX_PATH];
674 static const WCHAR emptyW[]={'\0'};
675 static const WCHAR prefix[] = {'p','f','x',0};
676 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
679 ret = GetTempPathW(MAX_PATH, temp_path);
680 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
682 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
683 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
685 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
686 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
688 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
689 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
690 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
691 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
693 ret = DeleteFileW(filename);
694 ok(ret, "DeleteFileW: error %d\n", GetLastError());
696 #if 0 /* this test crashes on NT4.0 */
697 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
698 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
699 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
700 "CreateFileW(NULL) returned ret=%p error=%ld\n",hFile,GetLastError());
703 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
704 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
705 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
706 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
708 /* test the result of opening a nonexistent driver name */
709 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
710 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
711 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
712 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
715 static void test_GetTempFileNameA(void)
719 char expected[MAX_PATH + 10];
720 char windowsdir[MAX_PATH + 10];
721 char windowsdrive[3];
723 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
724 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
725 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
727 /* If the Windows directory is the root directory, it ends in backslash, not else. */
728 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
730 strcat(windowsdir, "\\");
733 windowsdrive[0] = windowsdir[0];
734 windowsdrive[1] = windowsdir[1];
735 windowsdrive[2] = '\0';
737 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
738 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
739 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
740 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
741 windowsdrive[0], out);
743 result = GetTempFileNameA(windowsdir, "abc", 2, out);
744 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
746 strcat(expected, windowsdir);
747 strcat(expected, "abc2.tmp");
748 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
752 static void test_DeleteFileA( void )
756 ret = DeleteFileA(NULL);
757 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
758 GetLastError() == ERROR_PATH_NOT_FOUND),
759 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
761 ret = DeleteFileA("");
762 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
763 GetLastError() == ERROR_BAD_PATHNAME),
764 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
766 ret = DeleteFileA("nul");
767 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
768 GetLastError() == ERROR_INVALID_PARAMETER ||
769 GetLastError() == ERROR_ACCESS_DENIED ||
770 GetLastError() == ERROR_INVALID_FUNCTION),
771 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
774 static void test_DeleteFileW( void )
777 static const WCHAR emptyW[]={'\0'};
779 ret = DeleteFileW(NULL);
780 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
782 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
783 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
785 ret = DeleteFileW(emptyW);
786 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
787 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
790 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
792 static void test_MoveFileA(void)
794 char tempdir[MAX_PATH];
795 char source[MAX_PATH], dest[MAX_PATH];
796 static const char prefix[] = "pfx";
799 ret = GetTempPathA(MAX_PATH, tempdir);
800 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
801 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
803 ret = GetTempFileNameA(tempdir, prefix, 0, source);
804 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
806 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
807 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
809 ret = MoveFileA(source, dest);
810 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
811 "MoveFileA: unexpected error %d\n", GetLastError());
813 ret = DeleteFileA(dest);
814 ok(ret, "DeleteFileA: error %d\n", GetLastError());
816 ret = MoveFileA(source, dest);
817 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
819 lstrcatA(tempdir, "Remove Me");
820 ret = CreateDirectoryA(tempdir, NULL);
821 ok(ret == TRUE, "CreateDirectoryA failed\n");
823 lstrcpyA(source, dest);
824 lstrcpyA(dest, tempdir);
825 lstrcatA(dest, "\\wild?.*");
826 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
827 ret = MoveFileA(source, dest);
828 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
829 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
830 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
831 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
832 if (ret || (GetLastError() != ERROR_INVALID_NAME))
835 char temppath[MAX_PATH];
838 lstrcpyA(temppath, tempdir);
839 lstrcatA(temppath, "\\*.*");
840 hFind = FindFirstFileA(temppath, &fd);
841 if (INVALID_HANDLE_VALUE != hFind)
846 lpName = fd.cAlternateFileName;
848 lpName = fd.cFileName;
849 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
851 while (FindNextFileA(hFind, &fd));
855 ret = DeleteFileA(source);
856 ok(ret, "DeleteFileA: error %d\n", GetLastError());
857 ret = DeleteFileA(dest);
858 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
859 ret = RemoveDirectoryA(tempdir);
860 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
863 static void test_MoveFileW(void)
865 WCHAR temp_path[MAX_PATH];
866 WCHAR source[MAX_PATH], dest[MAX_PATH];
867 static const WCHAR prefix[] = {'p','f','x',0};
870 ret = GetTempPathW(MAX_PATH, temp_path);
871 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
873 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
874 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
876 ret = GetTempFileNameW(temp_path, prefix, 0, source);
877 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
879 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
880 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
882 ret = MoveFileW(source, dest);
883 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
884 "CopyFileW: unexpected error %d\n", GetLastError());
886 ret = DeleteFileW(source);
887 ok(ret, "DeleteFileW: error %d\n", GetLastError());
888 ret = DeleteFileW(dest);
889 ok(ret, "DeleteFileW: error %d\n", GetLastError());
892 #define PATTERN_OFFSET 0x10
894 static void test_offset_in_overlapped_structure(void)
900 BYTE buf[256], pattern[] = "TeSt";
902 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
905 ret =GetTempPathA(MAX_PATH, temp_path);
906 ok( ret, "GetTempPathA error %d\n", GetLastError());
907 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
908 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
910 /*** Write File *****************************************************/
912 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
913 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
915 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
916 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
917 ok( ret, "WriteFile error %d\n", GetLastError());
918 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
920 memset(&ov, 0, sizeof(ov));
921 S(U(ov)).Offset = PATTERN_OFFSET;
922 S(U(ov)).OffsetHigh = 0;
923 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
924 /* Win 9x does not support the overlapped I/O on files */
925 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
926 ok(rc, "WriteFile error %d\n", GetLastError());
927 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
928 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
929 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
931 S(U(ov)).Offset = sizeof(buf) * 2;
932 S(U(ov)).OffsetHigh = 0;
933 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
934 ok( ret, "WriteFile error %d\n", GetLastError());
935 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
936 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
937 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
942 /*** Read File *****************************************************/
944 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
945 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
947 memset(buf, 0, sizeof(buf));
948 memset(&ov, 0, sizeof(ov));
949 S(U(ov)).Offset = PATTERN_OFFSET;
950 S(U(ov)).OffsetHigh = 0;
951 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
952 /* Win 9x does not support the overlapped I/O on files */
953 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
954 ok(rc, "ReadFile error %d\n", GetLastError());
955 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
956 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
957 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
958 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
963 ret = DeleteFileA(temp_fname);
964 ok( ret, "DeleteFileA error %d\n", GetLastError());
967 static void test_LockFile(void)
971 OVERLAPPED overlapped;
972 int limited_LockFile;
973 int limited_UnLockFile;
974 int lockfileex_capable;
976 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
977 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
978 CREATE_ALWAYS, 0, 0 );
979 if (handle == INVALID_HANDLE_VALUE)
981 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
984 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
986 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
987 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
989 limited_UnLockFile = 0;
990 if (UnlockFile( handle, 0, 0, 0, 0 ))
992 limited_UnLockFile = 1;
995 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
996 /* overlapping locks must fail */
997 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
998 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
999 /* non-overlapping locks must succeed */
1000 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1002 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1003 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1004 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1005 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1007 S(U(overlapped)).Offset = 100;
1008 S(U(overlapped)).OffsetHigh = 0;
1009 overlapped.hEvent = 0;
1011 lockfileex_capable = dll_capable("kernel32", "LockFileEx");
1012 if (lockfileex_capable)
1014 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1015 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1017 /* LockFileEx is probably OK, test it more. */
1018 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1019 "LockFileEx 100,100 failed\n" );
1023 /* overlapping shared locks are OK */
1024 S(U(overlapped)).Offset = 150;
1025 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1027 /* but exclusive is not */
1028 if (lockfileex_capable)
1030 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1031 0, 50, 0, &overlapped ),
1032 "LockFileEx exclusive 150,50 succeeded\n" );
1033 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1035 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1036 { /* UnLockFile is capable. */
1037 S(U(overlapped)).Offset = 100;
1038 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1039 "UnlockFileEx 150,100 again succeeded\n" );
1044 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1045 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1046 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1047 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1049 /* wrap-around lock should not do anything */
1050 /* (but still succeeds on NT4 so we don't check result) */
1051 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1053 limited_LockFile = 0;
1054 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1056 limited_LockFile = 1;
1059 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1061 /* zero-byte lock */
1062 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1063 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1064 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1065 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1067 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1068 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1070 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1072 CloseHandle( handle );
1073 DeleteFileA( filename );
1076 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
1078 if (!access1 || !access2) return 1;
1079 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1080 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1081 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1082 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1083 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1084 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1088 static void test_file_sharing(void)
1090 static const DWORD access_modes[] =
1091 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1092 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1093 static const DWORD sharing_modes[] =
1094 { 0, FILE_SHARE_READ,
1095 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1096 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1097 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1102 /* make sure the file exists */
1103 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1104 if (h == INVALID_HANDLE_VALUE)
1106 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1111 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1113 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1115 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1116 NULL, OPEN_EXISTING, 0, 0 );
1117 if (h == INVALID_HANDLE_VALUE)
1119 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1122 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1124 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1126 SetLastError(0xdeadbeef);
1127 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1128 NULL, OPEN_EXISTING, 0, 0 );
1129 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1130 access_modes[a2], sharing_modes[s2] ))
1132 ret = GetLastError();
1133 ok( ERROR_SHARING_VIOLATION == ret || 0 == ret,
1134 "Windows 95 sets GetLastError() = ERROR_SHARING_VIOLATION and\n"
1135 " Windows XP GetLastError() = 0, but now it is %d.\n"
1136 " indexes = %d, %d, %d, %d\n"
1137 " modes =\n %x/%x/%x/%x\n",
1140 access_modes[a1], sharing_modes[s1],
1141 access_modes[a2], sharing_modes[s2]
1146 ok( h2 == INVALID_HANDLE_VALUE,
1147 "open succeeded for modes %x/%x/%x/%x\n",
1148 access_modes[a1], sharing_modes[s1],
1149 access_modes[a2], sharing_modes[s2] );
1150 if (h2 == INVALID_HANDLE_VALUE)
1151 ok( GetLastError() == ERROR_SHARING_VIOLATION,
1152 "wrong error code %d\n", GetLastError() );
1154 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1161 SetLastError(0xdeadbeef);
1162 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1163 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1165 SetLastError(0xdeadbeef);
1166 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1167 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1168 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1170 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1171 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1176 DeleteFileA( filename );
1179 static char get_windows_drive(void)
1181 char windowsdir[MAX_PATH];
1182 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1183 return windowsdir[0];
1186 static void test_FindFirstFileA(void)
1189 WIN32_FIND_DATAA data;
1191 char buffer[5] = "C:\\";
1194 /* try FindFirstFileA on "C:\" */
1195 buffer[0] = get_windows_drive();
1197 SetLastError( 0xdeadbeaf );
1198 handle = FindFirstFileA(buffer, &data);
1199 err = GetLastError();
1200 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1201 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1203 /* try FindFirstFileA on "C:\*" */
1204 strcpy(buffer2, buffer);
1205 strcat(buffer2, "*");
1206 handle = FindFirstFileA(buffer2, &data);
1207 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1208 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1209 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1210 if (FindNextFileA( handle, &data ))
1211 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1212 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1213 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1215 /* try FindFirstFileA on windows dir */
1216 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1217 strcat(buffer2, "\\*");
1218 handle = FindFirstFileA(buffer2, &data);
1219 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1220 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1221 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1222 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1223 while (FindNextFileA( handle, &data ))
1224 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1225 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1226 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1228 /* try FindFirstFileA on "C:\foo\" */
1229 SetLastError( 0xdeadbeaf );
1230 strcpy(buffer2, buffer);
1231 strcat(buffer2, "foo\\");
1232 handle = FindFirstFileA(buffer2, &data);
1233 err = GetLastError();
1234 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1236 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1239 /* try FindFirstFileA on "C:\foo\bar.txt" */
1240 SetLastError( 0xdeadbeaf );
1241 strcpy(buffer2, buffer);
1242 strcat(buffer2, "foo\\bar.txt");
1243 handle = FindFirstFileA(buffer2, &data);
1244 err = GetLastError();
1245 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1246 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1248 /* try FindFirstFileA on "C:\foo\*.*" */
1249 SetLastError( 0xdeadbeaf );
1250 strcpy(buffer2, buffer);
1251 strcat(buffer2, "foo\\*.*");
1252 handle = FindFirstFileA(buffer2, &data);
1253 err = GetLastError();
1254 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1255 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1257 /* try FindFirstFileA on "foo\bar.txt" */
1258 SetLastError( 0xdeadbeaf );
1259 strcpy(buffer2, "foo\\bar.txt");
1260 handle = FindFirstFileA(buffer2, &data);
1261 err = GetLastError();
1262 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1263 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1265 /* try FindFirstFileA on "c:\nul" */
1266 SetLastError( 0xdeadbeaf );
1267 strcpy(buffer2, buffer);
1268 strcat(buffer2, "nul");
1269 handle = FindFirstFileA(buffer2, &data);
1270 err = GetLastError();
1271 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1272 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1273 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1274 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1275 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1276 SetLastError( 0xdeadbeaf );
1277 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1278 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1279 ok( FindClose( handle ), "failed to close handle\n" );
1281 /* try FindFirstFileA on "lpt1" */
1282 SetLastError( 0xdeadbeaf );
1283 strcpy(buffer2, "lpt1");
1284 handle = FindFirstFileA(buffer2, &data);
1285 err = GetLastError();
1286 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1287 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1288 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1289 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1290 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1291 SetLastError( 0xdeadbeaf );
1292 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1293 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1294 ok( FindClose( handle ), "failed to close handle\n" );
1296 /* try FindFirstFileA on "c:\nul\*" */
1297 SetLastError( 0xdeadbeaf );
1298 strcpy(buffer2, buffer);
1299 strcat(buffer2, "nul\\*");
1300 handle = FindFirstFileA(buffer2, &data);
1301 err = GetLastError();
1302 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1303 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1305 /* try FindFirstFileA on "c:\nul*" */
1306 SetLastError( 0xdeadbeaf );
1307 strcpy(buffer2, buffer);
1308 strcat(buffer2, "nul*");
1309 handle = FindFirstFileA(buffer2, &data);
1310 err = GetLastError();
1311 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1312 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1314 /* try FindFirstFileA on "c:\foo\bar\nul" */
1315 SetLastError( 0xdeadbeaf );
1316 strcpy(buffer2, buffer);
1317 strcat(buffer2, "foo\\bar\\nul");
1318 handle = FindFirstFileA(buffer2, &data);
1319 err = GetLastError();
1320 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1321 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1323 /* try FindFirstFileA on "c:\foo\nul\bar" */
1324 SetLastError( 0xdeadbeaf );
1325 strcpy(buffer2, buffer);
1326 strcat(buffer2, "foo\\nul\\bar");
1327 handle = FindFirstFileA(buffer2, &data);
1328 err = GetLastError();
1329 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1330 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1333 static void test_FindNextFileA(void)
1336 WIN32_FIND_DATAA search_results;
1338 char buffer[5] = "C:\\*";
1340 buffer[0] = get_windows_drive();
1341 handle = FindFirstFileA(buffer,&search_results);
1342 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1343 while (FindNextFile(handle, &search_results))
1345 /* get to the end of the files */
1347 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1348 err = GetLastError();
1349 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1352 static int test_Mapfile_createtemp(HANDLE *handle)
1354 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1355 DeleteFile(filename);
1356 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1357 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1358 if (*handle != INVALID_HANDLE_VALUE) {
1366 static void test_MapFile(void)
1371 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1373 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1374 ok( hmap != NULL, "mapping should work, I named it!\n" );
1376 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1378 /* We have to close file before we try new stuff with mapping again.
1379 Else we would always succeed on XP or block descriptors on 95. */
1380 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1381 ok( hmap != NULL, "We should still be able to map!\n" );
1382 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1383 ok( CloseHandle( handle ), "can't close file handle\n");
1386 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1388 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1389 ok( hmap == NULL, "mapped zero size file\n");
1390 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1392 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1393 ok( hmap == NULL, "mapping should fail\n");
1394 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1396 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1397 ok( hmap == NULL, "mapping should fail\n");
1398 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1400 /* On XP you can now map again, on Win 95 you cannot. */
1402 ok( CloseHandle( handle ), "can't close file handle\n");
1403 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1406 static void test_GetFileType(void)
1409 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1410 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1411 type = GetFileType(h);
1412 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1414 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1415 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1416 type = GetFileType(h);
1417 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1419 DeleteFileA( filename );
1422 static int completion_count;
1424 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1426 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1427 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1431 static void test_async_file_errors(void)
1433 char szFile[MAX_PATH];
1434 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1436 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1438 S(U(ovl)).Offset = 0;
1439 S(U(ovl)).OffsetHigh = 0;
1441 completion_count = 0;
1443 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1444 strcat(szFile, "\\win.ini");
1445 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1446 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1450 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1452 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1453 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1456 S(U(ovl)).Offset += 4096;
1457 /* i/o completion routine only called if ReadFileEx returned success.
1458 * we only care about violations of this rule so undo what should have
1462 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1463 /*printf("Error = %ld\n", GetLastError());*/
1466 static void test_read_write(void)
1470 char temp_path[MAX_PATH];
1471 char filename[MAX_PATH];
1472 static const char prefix[] = "pfx";
1474 ret = GetTempPathA(MAX_PATH, temp_path);
1475 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1476 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1478 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1479 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1481 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1482 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1483 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1485 SetLastError(12345678);
1487 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1488 ok(ret && GetLastError() == 12345678,
1489 "ret = %d, error %d\n", ret, GetLastError());
1490 ok(!bytes, "bytes = %d\n", bytes);
1492 SetLastError(12345678);
1494 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1495 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1496 (ret && GetLastError() == 12345678), /* Win9x */
1497 "ret = %d, error %d\n", ret, GetLastError());
1498 ok(!bytes || /* Win2k */
1499 bytes == 10, /* Win9x */
1500 "bytes = %d\n", bytes);
1502 /* make sure the file contains data */
1503 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1504 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1506 SetLastError(12345678);
1508 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1509 ok(ret && GetLastError() == 12345678,
1510 "ret = %d, error %d\n", ret, GetLastError());
1511 ok(!bytes, "bytes = %d\n", bytes);
1513 SetLastError(12345678);
1515 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1516 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1517 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1518 "ret = %d, error %d\n", ret, GetLastError());
1519 ok(!bytes, "bytes = %d\n", bytes);
1521 ret = CloseHandle(hFile);
1522 ok( ret, "CloseHandle: error %d\n", GetLastError());
1523 ret = DeleteFileA(filename);
1524 ok( ret, "DeleteFileA: error %d\n", GetLastError());
1527 static void test_OpenFile(void)
1534 static const char *file = "\\regsvr32.exe";
1535 static const char *foo = ".\\foo-bar-foo.baz";
1536 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1537 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1538 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1539 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1540 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1541 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1542 static const char *backslash = "\\";
1543 char buff[MAX_PATH];
1544 char buff_long[4*MAX_PATH];
1545 char filled_0xA5[OFS_MAXPATHNAME];
1548 /* Check for existing file */
1549 length = GetSystemDirectoryA(buff, MAX_PATH);
1551 if (length + lstrlen(file) < MAX_PATH)
1553 lstrcatA(buff, file);
1554 memset(&ofs, 0xA5, sizeof(ofs));
1555 SetLastError(0xfaceabee);
1557 hFile = OpenFile(buff, &ofs, OF_EXIST);
1558 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1559 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1560 "GetLastError() returns %d\n", GetLastError() );
1561 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1562 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1563 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1564 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1565 ofs.szPathName, buff );
1568 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1569 length = GetCurrentDirectoryA(MAX_PATH, buff);
1571 /* Check for nonexistent file */
1572 if (length + lstrlenA(foo + 1) < MAX_PATH)
1574 lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1575 memset(&ofs, 0xA5, sizeof(ofs));
1576 SetLastError(0xfaceabee);
1578 hFile = OpenFile(foo, &ofs, OF_EXIST);
1579 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1580 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1582 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1583 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1584 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1585 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1586 ofs.szPathName, buff );
1589 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1590 length += lstrlenA(foo_too_long + 1);
1592 /* Check for nonexistent file with too long filename */
1593 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
1595 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1596 memset(&ofs, 0xA5, sizeof(ofs));
1597 SetLastError(0xfaceabee);
1599 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1600 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1601 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
1602 "GetLastError() returns %d\n", GetLastError() );
1604 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1605 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1606 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1607 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1608 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1612 length = GetCurrentDirectoryA(MAX_PATH, buff);
1613 length += lstrlenA(backslash);
1614 length += lstrlenA(filename);
1616 if (length >= MAX_PATH)
1618 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
1621 lstrcatA(buff, backslash);
1622 lstrcatA(buff, filename);
1624 memset(&ofs, 0xA5, sizeof(ofs));
1625 SetLastError(0xfaceabee);
1626 /* Create an empty file */
1627 hFile = OpenFile(filename, &ofs, OF_CREATE);
1628 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1629 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1630 "GetLastError() returns %d\n", GetLastError() );
1631 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1632 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1633 ret = CloseHandle((HANDLE)hFile);
1634 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1635 retval = GetFileAttributesA(filename);
1636 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1638 memset(&ofs, 0xA5, sizeof(ofs));
1639 SetLastError(0xfaceabee);
1640 /* Check various opening options: */
1641 /* for reading only, */
1642 hFile = OpenFile(filename, &ofs, OF_READ);
1643 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
1644 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1645 "GetLastError() returns %d\n", GetLastError() );
1646 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1647 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1648 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1649 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1650 ret = CloseHandle((HANDLE)hFile);
1651 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1653 memset(&ofs, 0xA5, sizeof(ofs));
1654 SetLastError(0xfaceabee);
1655 /* for writing only, */
1656 hFile = OpenFile(filename, &ofs, OF_WRITE);
1657 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
1658 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1659 "GetLastError() returns %d\n", GetLastError() );
1660 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1661 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1662 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1663 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1664 ret = CloseHandle((HANDLE)hFile);
1665 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1667 memset(&ofs, 0xA5, sizeof(ofs));
1668 SetLastError(0xfaceabee);
1669 /* for reading and writing, */
1670 hFile = OpenFile(filename, &ofs, OF_READWRITE);
1671 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1672 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1673 "GetLastError() returns %d\n", GetLastError() );
1674 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1675 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1676 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1677 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1678 ret = CloseHandle((HANDLE)hFile);
1679 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1681 memset(&ofs, 0xA5, sizeof(ofs));
1682 SetLastError(0xfaceabee);
1683 /* for checking file presence. */
1684 hFile = OpenFile(filename, &ofs, OF_EXIST);
1685 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1686 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1687 "GetLastError() returns %d\n", GetLastError() );
1688 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1689 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1690 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1691 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1693 memset(&ofs, 0xA5, sizeof(ofs));
1694 SetLastError(0xfaceabee);
1695 /* Delete the file and make sure it doesn't exist anymore */
1696 hFile = OpenFile(filename, &ofs, OF_DELETE);
1697 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1698 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1699 "GetLastError() returns %d\n", GetLastError() );
1700 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1701 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1702 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1703 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1705 retval = GetFileAttributesA(filename);
1706 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1709 static void test_overlapped(void)
1714 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1716 memset( &ov, 0, sizeof ov );
1718 r = GetOverlappedResult(0, &ov, &result, 0);
1719 ok( r == TRUE, "should return false\n");
1720 ok( result == 0, "result wrong\n");
1724 ov.InternalHigh = 0xabcd;
1725 r = GetOverlappedResult(0, &ov, &result, 0);
1726 ok( r == TRUE, "should return false\n");
1727 ok( result == 0xabcd, "result wrong\n");
1729 SetLastError( 0xb00 );
1731 ov.Internal = STATUS_INVALID_HANDLE;
1732 ov.InternalHigh = 0xabcd;
1733 r = GetOverlappedResult(0, &ov, &result, 0);
1734 ok (GetLastError() == ERROR_INVALID_HANDLE, "error wrong\n");
1735 ok( r == FALSE, "should return false\n");
1736 ok( result == 0xabcd, "result wrong\n");
1739 ov.Internal = STATUS_PENDING;
1740 ov.InternalHigh = 0xabcd;
1741 r = GetOverlappedResult(0, &ov, &result, 0);
1743 ok (GetLastError() == ERROR_IO_INCOMPLETE, "error wrong\n");
1745 ok( r == FALSE, "should return false\n");
1746 ok( result == 0, "result wrong\n");
1748 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
1749 ov.Internal = STATUS_PENDING;
1750 ov.InternalHigh = 0xabcd;
1751 r = GetOverlappedResult(0, &ov, &result, 0);
1752 ok (GetLastError() == ERROR_IO_INCOMPLETE, "error wrong\n");
1753 ok( r == FALSE, "should return false\n");
1755 ResetEvent( ov.hEvent );
1757 ov.Internal = STATUS_PENDING;
1758 ov.InternalHigh = 0;
1759 r = GetOverlappedResult(0, &ov, &result, 0);
1760 ok (GetLastError() == ERROR_IO_INCOMPLETE, "error wrong\n");
1761 ok( r == FALSE, "should return false\n");
1763 r = CloseHandle( ov.hEvent );
1764 ok( r == TRUE, "close handle failed\n");
1777 test_GetTempFileNameA();
1786 test_FindFirstFileA();
1787 test_FindNextFileA();
1789 test_file_sharing();
1790 test_offset_in_overlapped_structure();
1793 test_async_file_errors();