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);
39 /* keep filename and filenameW the same */
40 static const char filename[] = "testfile.xxx";
41 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
42 static const char sillytext[] =
43 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
55 static void test__hread( void )
64 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
65 DeleteFileA( filename );
66 filehandle = _lcreat( filename, 0 );
67 if (filehandle == HFILE_ERROR)
69 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
73 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
75 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
77 filehandle = _lopen( filename, OF_READ );
79 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
81 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
83 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
85 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
87 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
88 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
89 for (i = 0; i < bytes_wanted; i++)
91 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
95 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
97 ret = DeleteFileA( filename );
98 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
102 static void test__hwrite( void )
111 HLOCAL memory_object;
115 filehandle = _lcreat( filename, 0 );
116 if (filehandle == HFILE_ERROR)
118 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
122 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
124 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
126 filehandle = _lopen( filename, OF_READ );
128 bytes_read = _hread( filehandle, buffer, 1);
130 ok( 0 == bytes_read, "file read size error\n" );
132 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
134 filehandle = _lopen( filename, OF_READWRITE );
138 srand( (unsigned)time( NULL ) );
139 for (blocks = 0; blocks < 100; blocks++)
141 for (i = 0; i < (long)sizeof( buffer ); i++)
144 checksum[0] = checksum[0] + buffer[i];
146 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
147 bytes_written = bytes_written + sizeof( buffer );
150 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
153 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
155 memory_object = LocalAlloc( LPTR, bytes_written );
157 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
159 contents = LocalLock( memory_object );
161 filehandle = _lopen( filename, OF_READ );
163 contents = LocalLock( memory_object );
165 ok( NULL != contents, "LocalLock whines\n" );
167 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
173 checksum[0] = checksum[0] + contents[i];
176 while (i < bytes_written - 1);
178 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
180 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
182 ret = DeleteFileA( filename );
183 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
187 static void test__lclose( void )
192 filehandle = _lcreat( filename, 0 );
193 if (filehandle == HFILE_ERROR)
195 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
199 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\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__lcreat( void )
212 WIN32_FIND_DATAA search_results;
213 char slashname[] = "testfi/";
218 filehandle = _lcreat( filename, 0 );
219 if (filehandle == HFILE_ERROR)
221 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
225 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
227 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
229 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
231 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
233 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
235 ret = DeleteFileA(filename);
236 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
238 filehandle = _lcreat( filename, 1 ); /* readonly */
239 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
241 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
243 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
245 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
247 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
249 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
251 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
253 filehandle = _lcreat( filename, 2 );
254 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
256 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
258 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
260 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
262 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
264 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
266 ret = DeleteFileA( filename );
267 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
269 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
270 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
272 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
274 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
276 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
278 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
280 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
282 ret = DeleteFileA( filename );
283 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
285 filehandle=_lcreat (slashname, 0); /* illegal name */
286 if (HFILE_ERROR==filehandle) {
288 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
289 "creating file \"%s\" failed with error %d\n", slashname, err);
290 } else { /* only NT succeeds */
292 find=FindFirstFileA (slashname, &search_results);
293 if (INVALID_HANDLE_VALUE!=find)
295 ret = FindClose (find);
296 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
297 slashname[strlen(slashname)-1]=0;
298 ok (!strcmp (slashname, search_results.cFileName),
299 "found unexpected name \"%s\"\n", search_results.cFileName);
300 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
301 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
302 search_results.dwFileAttributes);
304 ret = DeleteFileA( slashname );
305 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
308 filehandle=_lcreat (filename, 8); /* illegal attribute */
309 if (HFILE_ERROR==filehandle)
310 ok (0, "couldn't create volume label \"%s\"\n", filename);
313 find=FindFirstFileA (filename, &search_results);
314 if (INVALID_HANDLE_VALUE==find)
315 ok (0, "file \"%s\" not found\n", filename);
317 ret = FindClose(find);
318 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
319 ok (!strcmp (filename, 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( filename );
326 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
331 static void test__llseek( void )
339 filehandle = _lcreat( filename, 0 );
340 if (filehandle == HFILE_ERROR)
342 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
346 for (i = 0; i < 400; i++)
348 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
350 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
351 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
353 bytes_read = _hread( filehandle, buffer, 1);
354 ok( 1 == bytes_read, "file read size error\n" );
355 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
356 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
358 bytes_read = _hread( filehandle, buffer, 1);
359 ok( 1 == bytes_read, "file read size error\n" );
360 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
361 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
362 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
364 ret = DeleteFileA( filename );
365 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
369 static void test__llopen( void )
376 filehandle = _lcreat( filename, 0 );
377 if (filehandle == HFILE_ERROR)
379 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
383 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
384 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
386 filehandle = _lopen( filename, OF_READ );
387 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
388 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
389 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
390 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
392 filehandle = _lopen( filename, OF_READWRITE );
393 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
394 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
395 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
396 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
398 filehandle = _lopen( filename, OF_WRITE );
399 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
400 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
401 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
403 ret = DeleteFileA( filename );
404 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
405 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
409 static void test__lread( void )
418 filehandle = _lcreat( filename, 0 );
419 if (filehandle == HFILE_ERROR)
421 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
425 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
427 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
429 filehandle = _lopen( filename, OF_READ );
431 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
433 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
435 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
437 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
439 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
440 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
441 for (i = 0; i < bytes_wanted; i++)
443 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
447 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
449 ret = DeleteFileA( filename );
450 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
454 static void test__lwrite( void )
463 HLOCAL memory_object;
467 filehandle = _lcreat( filename, 0 );
468 if (filehandle == HFILE_ERROR)
470 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
474 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
476 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
478 filehandle = _lopen( filename, OF_READ );
480 bytes_read = _hread( filehandle, buffer, 1);
482 ok( 0 == bytes_read, "file read size error\n" );
484 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
486 filehandle = _lopen( filename, OF_READWRITE );
490 srand( (unsigned)time( NULL ) );
491 for (blocks = 0; blocks < 100; blocks++)
493 for (i = 0; i < (long)sizeof( buffer ); i++)
496 checksum[0] = checksum[0] + buffer[i];
498 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
499 bytes_written = bytes_written + sizeof( buffer );
502 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
505 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
507 memory_object = LocalAlloc( LPTR, bytes_written );
509 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
511 contents = LocalLock( memory_object );
513 filehandle = _lopen( filename, OF_READ );
515 contents = LocalLock( memory_object );
517 ok( NULL != contents, "LocalLock whines\n" );
519 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
525 checksum[0] += contents[i];
528 while (i < bytes_written - 1);
530 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
532 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
534 ret = DeleteFileA( filename );
535 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
538 static void test_CopyFileA(void)
540 char temp_path[MAX_PATH];
541 char source[MAX_PATH], dest[MAX_PATH];
542 static const char prefix[] = "pfx";
549 ret = GetTempPathA(MAX_PATH, temp_path);
550 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
551 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
553 ret = GetTempFileNameA(temp_path, prefix, 0, source);
554 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
556 /* make the source have not zero size */
557 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
558 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
559 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
560 ok( retok && ret == sizeof(prefix),
561 "WriteFile error %d\n", GetLastError());
562 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
563 /* get the file time and change it to prove the difference */
564 ret = GetFileTime(hfile, NULL, NULL, &ft1);
565 ok( ret, "GetFileTime error %d\n", GetLastError());
566 ft1.dwLowDateTime -= 600000000; /* 60 second */
567 ret = SetFileTime(hfile, NULL, NULL, &ft1);
568 ok( ret, "SetFileTime error %d\n", GetLastError());
569 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
572 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
573 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
575 SetLastError(0xdeadbeef);
576 ret = CopyFileA(source, dest, TRUE);
577 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
578 "CopyFileA: unexpected error %d\n", GetLastError());
580 ret = CopyFileA(source, dest, FALSE);
581 ok(ret, "CopyFileA: error %d\n", GetLastError());
583 /* make sure that destination has correct size */
584 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
585 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
586 ret = GetFileSize(hfile, NULL);
587 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
589 /* make sure that destination has the same filetime */
590 ret = GetFileTime(hfile, NULL, NULL, &ft2);
591 ok( ret, "GetFileTime error %d\n", GetLastError());
592 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
594 SetLastError(0xdeadbeef);
595 ret = CopyFileA(source, dest, FALSE);
596 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
597 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
599 /* make sure that destination still has correct size */
600 ret = GetFileSize(hfile, NULL);
601 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
602 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
603 ok( retok && ret == sizeof(prefix),
604 "ReadFile: error %d\n", GetLastError());
605 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
608 ret = DeleteFileA(source);
609 ok(ret, "DeleteFileA: error %d\n", GetLastError());
610 ret = DeleteFileA(dest);
611 ok(ret, "DeleteFileA: error %d\n", GetLastError());
614 static void test_CopyFileW(void)
616 WCHAR temp_path[MAX_PATH];
617 WCHAR source[MAX_PATH], dest[MAX_PATH];
618 static const WCHAR prefix[] = {'p','f','x',0};
621 ret = GetTempPathW(MAX_PATH, temp_path);
622 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
624 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
625 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
627 ret = GetTempFileNameW(temp_path, prefix, 0, source);
628 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
630 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
631 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
633 ret = CopyFileW(source, dest, TRUE);
634 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
635 "CopyFileW: unexpected error %d\n", GetLastError());
637 ret = CopyFileW(source, dest, FALSE);
638 ok(ret, "CopyFileW: error %d\n", GetLastError());
640 ret = DeleteFileW(source);
641 ok(ret, "DeleteFileW: error %d\n", GetLastError());
642 ret = DeleteFileW(dest);
643 ok(ret, "DeleteFileW: error %d\n", GetLastError());
646 static void test_CreateFileA(void)
649 char temp_path[MAX_PATH];
650 char filename[MAX_PATH];
651 static const char prefix[] = "pfx";
654 ret = GetTempPathA(MAX_PATH, temp_path);
655 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
656 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
658 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
659 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
661 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
662 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
663 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
664 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
666 ret = DeleteFileA(filename);
667 ok(ret, "DeleteFileA: error %d\n", GetLastError());
670 static void test_CreateFileW(void)
673 WCHAR temp_path[MAX_PATH];
674 WCHAR filename[MAX_PATH];
675 static const WCHAR emptyW[]={'\0'};
676 static const WCHAR prefix[] = {'p','f','x',0};
677 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
680 ret = GetTempPathW(MAX_PATH, temp_path);
681 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
683 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
684 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
686 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
687 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
689 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
690 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
691 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
692 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
694 ret = DeleteFileW(filename);
695 ok(ret, "DeleteFileW: error %d\n", GetLastError());
699 /* this crashes on NT4.0 */
700 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
701 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
702 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
703 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
706 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
707 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
708 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
709 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
711 /* test the result of opening a nonexistent driver name */
712 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
713 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
714 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
715 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
718 static void test_GetTempFileNameA(void)
722 char expected[MAX_PATH + 10];
723 char windowsdir[MAX_PATH + 10];
724 char windowsdrive[3];
726 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
727 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
728 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
730 /* If the Windows directory is the root directory, it ends in backslash, not else. */
731 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
733 strcat(windowsdir, "\\");
736 windowsdrive[0] = windowsdir[0];
737 windowsdrive[1] = windowsdir[1];
738 windowsdrive[2] = '\0';
740 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
741 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
742 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
743 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
744 windowsdrive[0], out);
746 result = GetTempFileNameA(windowsdir, "abc", 2, out);
747 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
749 strcat(expected, windowsdir);
750 strcat(expected, "abc2.tmp");
751 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
755 static void test_DeleteFileA( void )
759 ret = DeleteFileA(NULL);
760 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
761 GetLastError() == ERROR_PATH_NOT_FOUND),
762 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
764 ret = DeleteFileA("");
765 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
766 GetLastError() == ERROR_BAD_PATHNAME),
767 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
769 ret = DeleteFileA("nul");
770 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
771 GetLastError() == ERROR_INVALID_PARAMETER ||
772 GetLastError() == ERROR_ACCESS_DENIED ||
773 GetLastError() == ERROR_INVALID_FUNCTION),
774 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
777 static void test_DeleteFileW( void )
780 static const WCHAR emptyW[]={'\0'};
782 ret = DeleteFileW(NULL);
783 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
785 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
786 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
788 ret = DeleteFileW(emptyW);
789 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
790 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
793 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
795 static void test_MoveFileA(void)
797 char tempdir[MAX_PATH];
798 char source[MAX_PATH], dest[MAX_PATH];
799 static const char prefix[] = "pfx";
802 ret = GetTempPathA(MAX_PATH, tempdir);
803 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
804 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
806 ret = GetTempFileNameA(tempdir, prefix, 0, source);
807 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
809 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
810 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
812 ret = MoveFileA(source, dest);
813 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
814 "MoveFileA: unexpected error %d\n", GetLastError());
816 ret = DeleteFileA(dest);
817 ok(ret, "DeleteFileA: error %d\n", GetLastError());
819 ret = MoveFileA(source, dest);
820 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
822 lstrcatA(tempdir, "Remove Me");
823 ret = CreateDirectoryA(tempdir, NULL);
824 ok(ret == TRUE, "CreateDirectoryA failed\n");
826 lstrcpyA(source, dest);
827 lstrcpyA(dest, tempdir);
828 lstrcatA(dest, "\\wild?.*");
829 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
830 ret = MoveFileA(source, dest);
831 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
832 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
833 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
834 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
835 if (ret || (GetLastError() != ERROR_INVALID_NAME))
838 char temppath[MAX_PATH];
841 lstrcpyA(temppath, tempdir);
842 lstrcatA(temppath, "\\*.*");
843 hFind = FindFirstFileA(temppath, &fd);
844 if (INVALID_HANDLE_VALUE != hFind)
849 lpName = fd.cAlternateFileName;
851 lpName = fd.cFileName;
852 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
854 while (FindNextFileA(hFind, &fd));
858 ret = DeleteFileA(source);
859 ok(ret, "DeleteFileA: error %d\n", GetLastError());
860 ret = DeleteFileA(dest);
861 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
862 ret = RemoveDirectoryA(tempdir);
863 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
866 static void test_MoveFileW(void)
868 WCHAR temp_path[MAX_PATH];
869 WCHAR source[MAX_PATH], dest[MAX_PATH];
870 static const WCHAR prefix[] = {'p','f','x',0};
873 ret = GetTempPathW(MAX_PATH, temp_path);
874 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
876 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
877 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
879 ret = GetTempFileNameW(temp_path, prefix, 0, source);
880 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
882 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
883 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
885 ret = MoveFileW(source, dest);
886 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
887 "CopyFileW: unexpected error %d\n", GetLastError());
889 ret = DeleteFileW(source);
890 ok(ret, "DeleteFileW: error %d\n", GetLastError());
891 ret = DeleteFileW(dest);
892 ok(ret, "DeleteFileW: error %d\n", GetLastError());
895 #define PATTERN_OFFSET 0x10
897 static void test_offset_in_overlapped_structure(void)
903 BYTE buf[256], pattern[] = "TeSt";
905 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
908 ret =GetTempPathA(MAX_PATH, temp_path);
909 ok( ret, "GetTempPathA error %d\n", GetLastError());
910 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
911 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
913 /*** Write File *****************************************************/
915 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
916 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
918 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
919 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
920 ok( ret, "WriteFile error %d\n", GetLastError());
921 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
923 memset(&ov, 0, sizeof(ov));
924 S(U(ov)).Offset = PATTERN_OFFSET;
925 S(U(ov)).OffsetHigh = 0;
926 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
927 /* Win 9x does not support the overlapped I/O on files */
928 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
929 ok(rc, "WriteFile error %d\n", GetLastError());
930 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
931 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
932 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
934 S(U(ov)).Offset = sizeof(buf) * 2;
935 S(U(ov)).OffsetHigh = 0;
936 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
937 ok( ret, "WriteFile error %d\n", GetLastError());
938 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
939 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
940 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
945 /*** Read File *****************************************************/
947 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
948 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
950 memset(buf, 0, sizeof(buf));
951 memset(&ov, 0, sizeof(ov));
952 S(U(ov)).Offset = PATTERN_OFFSET;
953 S(U(ov)).OffsetHigh = 0;
954 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
955 /* Win 9x does not support the overlapped I/O on files */
956 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
957 ok(rc, "ReadFile error %d\n", GetLastError());
958 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
959 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
960 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
961 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
966 ret = DeleteFileA(temp_fname);
967 ok( ret, "DeleteFileA error %d\n", GetLastError());
970 static void test_LockFile(void)
974 OVERLAPPED overlapped;
975 int limited_LockFile;
976 int limited_UnLockFile;
977 int lockfileex_capable;
979 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
980 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
981 CREATE_ALWAYS, 0, 0 );
982 if (handle == INVALID_HANDLE_VALUE)
984 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
987 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
989 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
990 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
992 limited_UnLockFile = 0;
993 if (UnlockFile( handle, 0, 0, 0, 0 ))
995 limited_UnLockFile = 1;
998 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
999 /* overlapping locks must fail */
1000 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1001 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1002 /* non-overlapping locks must succeed */
1003 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1005 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1006 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1007 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1008 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1010 S(U(overlapped)).Offset = 100;
1011 S(U(overlapped)).OffsetHigh = 0;
1012 overlapped.hEvent = 0;
1014 lockfileex_capable = dll_capable("kernel32", "LockFileEx");
1015 if (lockfileex_capable)
1017 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1018 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1020 /* LockFileEx is probably OK, test it more. */
1021 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1022 "LockFileEx 100,100 failed\n" );
1026 /* overlapping shared locks are OK */
1027 S(U(overlapped)).Offset = 150;
1028 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1030 /* but exclusive is not */
1031 if (lockfileex_capable)
1033 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1034 0, 50, 0, &overlapped ),
1035 "LockFileEx exclusive 150,50 succeeded\n" );
1036 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1038 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1039 { /* UnLockFile is capable. */
1040 S(U(overlapped)).Offset = 100;
1041 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1042 "UnlockFileEx 150,100 again succeeded\n" );
1047 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1048 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1049 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1050 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1052 /* wrap-around lock should not do anything */
1053 /* (but still succeeds on NT4 so we don't check result) */
1054 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1056 limited_LockFile = 0;
1057 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1059 limited_LockFile = 1;
1062 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1064 /* zero-byte lock */
1065 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1066 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1067 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1068 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1070 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1071 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1073 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1075 CloseHandle( handle );
1076 DeleteFileA( filename );
1079 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1083 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1084 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1089 if (!access1) access1 = GENERIC_READ;
1092 if (!access2) access2 = GENERIC_READ;
1095 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1096 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1097 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1098 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1099 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1100 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1104 static void test_file_sharing(void)
1106 static const DWORD access_modes[] =
1107 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1108 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1109 static const DWORD sharing_modes[] =
1110 { 0, FILE_SHARE_READ,
1111 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1112 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1113 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1117 BOOL is_win9x = FALSE;
1119 /* make sure the file exists */
1120 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1121 if (h == INVALID_HANDLE_VALUE)
1123 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1126 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1129 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1131 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1133 /* Win9x doesn't support FILE_SHARE_DELETE */
1134 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1137 SetLastError(0xdeadbeef);
1138 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1139 NULL, OPEN_EXISTING, 0, 0 );
1140 if (h == INVALID_HANDLE_VALUE)
1142 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1145 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1147 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1149 /* Win9x doesn't support FILE_SHARE_DELETE */
1150 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1153 SetLastError(0xdeadbeef);
1154 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1155 NULL, OPEN_EXISTING, 0, 0 );
1157 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1158 access_modes[a2], sharing_modes[s2], is_win9x ))
1160 ret = GetLastError();
1162 ok( h2 != INVALID_HANDLE_VALUE,
1163 "open failed for modes %x/%x/%x/%x\n",
1164 access_modes[a1], sharing_modes[s1],
1165 access_modes[a2], sharing_modes[s2] );
1166 ok( ret == 0xdeadbeef /* Win9x */ ||
1168 "wrong error code %d\n", ret );
1174 ret = GetLastError();
1176 ok( h2 == INVALID_HANDLE_VALUE,
1177 "open succeeded for modes %x/%x/%x/%x\n",
1178 access_modes[a1], sharing_modes[s1],
1179 access_modes[a2], sharing_modes[s2] );
1180 ok( ret == ERROR_SHARING_VIOLATION,
1181 "wrong error code %d\n", ret );
1189 SetLastError(0xdeadbeef);
1190 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1191 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1193 SetLastError(0xdeadbeef);
1194 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1195 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1196 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1198 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1199 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1204 DeleteFileA( filename );
1207 static char get_windows_drive(void)
1209 char windowsdir[MAX_PATH];
1210 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1211 return windowsdir[0];
1214 static void test_FindFirstFileA(void)
1217 WIN32_FIND_DATAA data;
1219 char buffer[5] = "C:\\";
1222 /* try FindFirstFileA on "C:\" */
1223 buffer[0] = get_windows_drive();
1225 SetLastError( 0xdeadbeaf );
1226 handle = FindFirstFileA(buffer, &data);
1227 err = GetLastError();
1228 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1229 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1231 /* try FindFirstFileA on "C:\*" */
1232 strcpy(buffer2, buffer);
1233 strcat(buffer2, "*");
1234 handle = FindFirstFileA(buffer2, &data);
1235 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1236 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1237 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1238 if (FindNextFileA( handle, &data ))
1239 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1240 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1241 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1243 /* try FindFirstFileA on windows dir */
1244 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1245 strcat(buffer2, "\\*");
1246 handle = FindFirstFileA(buffer2, &data);
1247 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1248 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1249 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1250 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1251 while (FindNextFileA( handle, &data ))
1252 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1253 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1254 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1256 /* try FindFirstFileA on "C:\foo\" */
1257 SetLastError( 0xdeadbeaf );
1258 strcpy(buffer2, buffer);
1259 strcat(buffer2, "foo\\");
1260 handle = FindFirstFileA(buffer2, &data);
1261 err = GetLastError();
1262 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1264 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1267 /* try FindFirstFileA on "C:\foo\bar.txt" */
1268 SetLastError( 0xdeadbeaf );
1269 strcpy(buffer2, buffer);
1270 strcat(buffer2, "foo\\bar.txt");
1271 handle = FindFirstFileA(buffer2, &data);
1272 err = GetLastError();
1273 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1274 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1276 /* try FindFirstFileA on "C:\foo\*.*" */
1277 SetLastError( 0xdeadbeaf );
1278 strcpy(buffer2, buffer);
1279 strcat(buffer2, "foo\\*.*");
1280 handle = FindFirstFileA(buffer2, &data);
1281 err = GetLastError();
1282 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1283 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1285 /* try FindFirstFileA on "foo\bar.txt" */
1286 SetLastError( 0xdeadbeaf );
1287 strcpy(buffer2, "foo\\bar.txt");
1288 handle = FindFirstFileA(buffer2, &data);
1289 err = GetLastError();
1290 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1291 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1293 /* try FindFirstFileA on "c:\nul" */
1294 SetLastError( 0xdeadbeaf );
1295 strcpy(buffer2, buffer);
1296 strcat(buffer2, "nul");
1297 handle = FindFirstFileA(buffer2, &data);
1298 err = GetLastError();
1299 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1300 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1301 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1302 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1303 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1304 SetLastError( 0xdeadbeaf );
1305 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1306 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1307 ok( FindClose( handle ), "failed to close handle\n" );
1309 /* try FindFirstFileA on "lpt1" */
1310 SetLastError( 0xdeadbeaf );
1311 strcpy(buffer2, "lpt1");
1312 handle = FindFirstFileA(buffer2, &data);
1313 err = GetLastError();
1314 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1315 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1316 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1317 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1318 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1319 SetLastError( 0xdeadbeaf );
1320 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1321 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1322 ok( FindClose( handle ), "failed to close handle\n" );
1324 /* try FindFirstFileA on "c:\nul\*" */
1325 SetLastError( 0xdeadbeaf );
1326 strcpy(buffer2, buffer);
1327 strcat(buffer2, "nul\\*");
1328 handle = FindFirstFileA(buffer2, &data);
1329 err = GetLastError();
1330 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1331 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1333 /* try FindFirstFileA on "c:\nul*" */
1334 SetLastError( 0xdeadbeaf );
1335 strcpy(buffer2, buffer);
1336 strcat(buffer2, "nul*");
1337 handle = FindFirstFileA(buffer2, &data);
1338 err = GetLastError();
1339 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1340 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1342 /* try FindFirstFileA on "c:\foo\bar\nul" */
1343 SetLastError( 0xdeadbeaf );
1344 strcpy(buffer2, buffer);
1345 strcat(buffer2, "foo\\bar\\nul");
1346 handle = FindFirstFileA(buffer2, &data);
1347 err = GetLastError();
1348 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1349 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1351 /* try FindFirstFileA on "c:\foo\nul\bar" */
1352 SetLastError( 0xdeadbeaf );
1353 strcpy(buffer2, buffer);
1354 strcat(buffer2, "foo\\nul\\bar");
1355 handle = FindFirstFileA(buffer2, &data);
1356 err = GetLastError();
1357 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1358 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1361 static void test_FindNextFileA(void)
1364 WIN32_FIND_DATAA search_results;
1366 char buffer[5] = "C:\\*";
1368 buffer[0] = get_windows_drive();
1369 handle = FindFirstFileA(buffer,&search_results);
1370 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1371 while (FindNextFile(handle, &search_results))
1373 /* get to the end of the files */
1375 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1376 err = GetLastError();
1377 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1380 static int test_Mapfile_createtemp(HANDLE *handle)
1382 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1383 DeleteFile(filename);
1384 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1385 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1386 if (*handle != INVALID_HANDLE_VALUE) {
1394 static void test_MapFile(void)
1399 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1401 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1402 ok( hmap != NULL, "mapping should work, I named it!\n" );
1404 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1406 /* We have to close file before we try new stuff with mapping again.
1407 Else we would always succeed on XP or block descriptors on 95. */
1408 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1409 ok( hmap != NULL, "We should still be able to map!\n" );
1410 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1411 ok( CloseHandle( handle ), "can't close file handle\n");
1414 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1416 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1417 ok( hmap == NULL, "mapped zero size file\n");
1418 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1420 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1421 ok( hmap == NULL, "mapping should fail\n");
1422 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1424 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1425 ok( hmap == NULL, "mapping should fail\n");
1426 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1428 /* On XP you can now map again, on Win 95 you cannot. */
1430 ok( CloseHandle( handle ), "can't close file handle\n");
1431 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1434 static void test_GetFileType(void)
1437 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1438 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1439 type = GetFileType(h);
1440 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1442 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1443 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1444 type = GetFileType(h);
1445 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1447 DeleteFileA( filename );
1450 static int completion_count;
1452 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1454 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1455 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1459 static void test_async_file_errors(void)
1461 char szFile[MAX_PATH];
1462 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1464 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1466 S(U(ovl)).Offset = 0;
1467 S(U(ovl)).OffsetHigh = 0;
1469 completion_count = 0;
1471 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1472 strcat(szFile, "\\win.ini");
1473 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1474 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1478 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1480 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1481 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1484 S(U(ovl)).Offset += 4096;
1485 /* i/o completion routine only called if ReadFileEx returned success.
1486 * we only care about violations of this rule so undo what should have
1490 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1491 /*printf("Error = %ld\n", GetLastError());*/
1494 static void test_read_write(void)
1498 char temp_path[MAX_PATH];
1499 char filename[MAX_PATH];
1500 static const char prefix[] = "pfx";
1502 ret = GetTempPathA(MAX_PATH, temp_path);
1503 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1504 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1506 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1507 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1509 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1510 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1511 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1513 SetLastError(12345678);
1515 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1516 ok(ret && GetLastError() == 12345678,
1517 "ret = %d, error %d\n", ret, GetLastError());
1518 ok(!bytes, "bytes = %d\n", bytes);
1520 SetLastError(12345678);
1522 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1523 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1524 (ret && GetLastError() == 12345678), /* Win9x */
1525 "ret = %d, error %d\n", ret, GetLastError());
1526 ok(!bytes || /* Win2k */
1527 bytes == 10, /* Win9x */
1528 "bytes = %d\n", bytes);
1530 /* make sure the file contains data */
1531 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1532 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1534 SetLastError(12345678);
1536 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1537 ok(ret && GetLastError() == 12345678,
1538 "ret = %d, error %d\n", ret, GetLastError());
1539 ok(!bytes, "bytes = %d\n", bytes);
1541 SetLastError(12345678);
1543 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1544 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1545 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1546 "ret = %d, error %d\n", ret, GetLastError());
1547 ok(!bytes, "bytes = %d\n", bytes);
1549 ret = CloseHandle(hFile);
1550 ok( ret, "CloseHandle: error %d\n", GetLastError());
1551 ret = DeleteFileA(filename);
1552 ok( ret, "DeleteFileA: error %d\n", GetLastError());
1555 static void test_OpenFile(void)
1562 static const char *file = "\\regsvr32.exe";
1563 static const char *foo = ".\\foo-bar-foo.baz";
1564 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1565 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1566 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1567 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1568 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1569 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1570 static const char *backslash = "\\";
1571 char buff[MAX_PATH];
1572 char buff_long[4*MAX_PATH];
1573 char filled_0xA5[OFS_MAXPATHNAME];
1576 /* Check for existing file */
1577 length = GetSystemDirectoryA(buff, MAX_PATH);
1579 if (length + lstrlen(file) < MAX_PATH)
1581 lstrcatA(buff, file);
1582 memset(&ofs, 0xA5, sizeof(ofs));
1583 SetLastError(0xfaceabee);
1585 hFile = OpenFile(buff, &ofs, OF_EXIST);
1586 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1587 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1588 "GetLastError() returns %d\n", GetLastError() );
1589 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1590 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1591 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1592 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1593 ofs.szPathName, buff );
1596 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1597 length = GetCurrentDirectoryA(MAX_PATH, buff);
1599 /* Check for nonexistent file */
1600 if (length + lstrlenA(foo + 1) < MAX_PATH)
1602 lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1603 memset(&ofs, 0xA5, sizeof(ofs));
1604 SetLastError(0xfaceabee);
1606 hFile = OpenFile(foo, &ofs, OF_EXIST);
1607 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1608 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1610 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1611 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1612 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1613 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1614 ofs.szPathName, buff );
1617 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1618 length += lstrlenA(foo_too_long + 1);
1620 /* Check for nonexistent file with too long filename */
1621 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
1623 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1624 memset(&ofs, 0xA5, sizeof(ofs));
1625 SetLastError(0xfaceabee);
1627 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1628 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1629 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
1630 "GetLastError() returns %d\n", GetLastError() );
1632 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1633 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1634 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1635 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1636 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1640 length = GetCurrentDirectoryA(MAX_PATH, buff);
1641 length += lstrlenA(backslash);
1642 length += lstrlenA(filename);
1644 if (length >= MAX_PATH)
1646 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
1649 lstrcatA(buff, backslash);
1650 lstrcatA(buff, filename);
1652 memset(&ofs, 0xA5, sizeof(ofs));
1653 SetLastError(0xfaceabee);
1654 /* Create an empty file */
1655 hFile = OpenFile(filename, &ofs, OF_CREATE);
1656 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1657 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1658 "GetLastError() returns %d\n", GetLastError() );
1659 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1660 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1661 ret = CloseHandle((HANDLE)hFile);
1662 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1663 retval = GetFileAttributesA(filename);
1664 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1666 memset(&ofs, 0xA5, sizeof(ofs));
1667 SetLastError(0xfaceabee);
1668 /* Check various opening options: */
1669 /* for reading only, */
1670 hFile = OpenFile(filename, &ofs, OF_READ);
1671 ok( hFile != HFILE_ERROR, "OpenFile failed on read\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 writing only, */
1684 hFile = OpenFile(filename, &ofs, OF_WRITE);
1685 ok( hFile != HFILE_ERROR, "OpenFile failed on write\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 );
1692 ret = CloseHandle((HANDLE)hFile);
1693 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1695 memset(&ofs, 0xA5, sizeof(ofs));
1696 SetLastError(0xfaceabee);
1697 /* for reading and writing, */
1698 hFile = OpenFile(filename, &ofs, OF_READWRITE);
1699 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1700 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1701 "GetLastError() returns %d\n", GetLastError() );
1702 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1703 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1704 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1705 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1706 ret = CloseHandle((HANDLE)hFile);
1707 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1709 memset(&ofs, 0xA5, sizeof(ofs));
1710 SetLastError(0xfaceabee);
1711 /* for checking file presence. */
1712 hFile = OpenFile(filename, &ofs, OF_EXIST);
1713 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1714 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1715 "GetLastError() returns %d\n", GetLastError() );
1716 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1717 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1718 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1719 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1721 memset(&ofs, 0xA5, sizeof(ofs));
1722 SetLastError(0xfaceabee);
1723 /* Delete the file and make sure it doesn't exist anymore */
1724 hFile = OpenFile(filename, &ofs, OF_DELETE);
1725 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1726 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1727 "GetLastError() returns %d\n", GetLastError() );
1728 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1729 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1730 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1731 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1733 retval = GetFileAttributesA(filename);
1734 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1737 static void test_overlapped(void)
1742 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1744 memset( &ov, 0, sizeof ov );
1746 r = GetOverlappedResult(0, &ov, &result, 0);
1747 ok( r == TRUE, "should return false\n");
1748 ok( result == 0, "wrong result %u\n", result );
1752 ov.InternalHigh = 0xabcd;
1753 r = GetOverlappedResult(0, &ov, &result, 0);
1754 ok( r == TRUE, "should return false\n");
1755 ok( result == 0xabcd, "wrong result %u\n", result );
1757 SetLastError( 0xb00 );
1759 ov.Internal = STATUS_INVALID_HANDLE;
1760 ov.InternalHigh = 0xabcd;
1761 r = GetOverlappedResult(0, &ov, &result, 0);
1762 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1763 ok( r == FALSE, "should return false\n");
1764 ok( result == 0xabcd, "wrong result %u\n", result );
1766 SetLastError( 0xb00 );
1768 ov.Internal = STATUS_PENDING;
1769 ov.InternalHigh = 0xabcd;
1770 r = GetOverlappedResult(0, &ov, &result, 0);
1772 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1774 ok( r == FALSE, "should return false\n");
1775 ok( result == 0, "wrong result %u\n", result );
1777 SetLastError( 0xb00 );
1778 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
1779 ov.Internal = STATUS_PENDING;
1780 ov.InternalHigh = 0xabcd;
1781 r = GetOverlappedResult(0, &ov, &result, 0);
1782 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1783 ok( r == FALSE, "should return false\n");
1785 ResetEvent( ov.hEvent );
1787 SetLastError( 0xb00 );
1788 ov.Internal = STATUS_PENDING;
1789 ov.InternalHigh = 0;
1790 r = GetOverlappedResult(0, &ov, &result, 0);
1791 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1792 ok( r == FALSE, "should return false\n");
1794 r = CloseHandle( ov.hEvent );
1795 ok( r == TRUE, "close handle failed\n");
1798 static void test_RemoveDirectory(void)
1801 char directory[] = "removeme";
1803 rc = CreateDirectory(directory, NULL);
1804 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
1806 rc = SetCurrentDirectory(directory);
1807 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1809 rc = RemoveDirectory(".");
1811 ok( !rc, "RemoveDirectory unexpectedly worked\n" );
1814 rc = SetCurrentDirectory("..");
1815 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1817 rc = RemoveDirectory(directory);
1819 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1833 test_GetTempFileNameA();
1842 test_FindFirstFileA();
1843 test_FindNextFileA();
1845 test_file_sharing();
1846 test_offset_in_overlapped_structure();
1849 test_async_file_errors();
1853 test_RemoveDirectory();