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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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=%ld)\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=%ld)\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 (%ld)\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=%ld)\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 (%ld)\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=%ld)\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 (%ld)\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=%ld)\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 (%ld)\n", GetLastError());
237 filehandle = _lcreat( filename, 1 ); /* readonly */
238 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\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=%ld)\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 (%ld)\n", GetLastError( ) );
268 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
269 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\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 (%ld)\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 (%ld)\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%04lx\n", search_results.cFileName,
301 search_results.dwFileAttributes);
303 ret = DeleteFileA( slashname );
304 ok( ret, "DeleteFile failed (%ld)\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 (%ld)\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%04lx\n", search_results.cFileName,
322 search_results.dwFileAttributes);
324 ret = DeleteFileA( filename );
325 ok( ret, "DeleteFile failed (%ld)\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=%ld)\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 (%ld)\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=%ld)\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 (%ld)\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=%ld)\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=%ld)\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 (%ld)\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=%ld)\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 (%ld)\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 %ld\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 %ld\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 %ld\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 ok(GetFileTime(hfile, NULL, NULL, &ft1), "GetFileTime error %ld\n", GetLastError());
564 ft1.dwLowDateTime -= 600000000; /* 60 second */
565 ok(SetFileTime(hfile, NULL, NULL, &ft1), "SetFileTime error %ld\n", GetLastError());
568 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
569 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
571 SetLastError(0xdeadbeef);
572 ret = CopyFileA(source, dest, TRUE);
573 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
574 "CopyFileA: unexpected error %ld\n", GetLastError());
576 ret = CopyFileA(source, dest, FALSE);
577 ok(ret, "CopyFileA: error %ld\n", GetLastError());
579 /* make sure that destination has correct size */
580 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
581 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
582 ret = GetFileSize(hfile, NULL);
583 ok(ret == sizeof(prefix), "destination file has wrong size %ld\n", ret);
585 /* make sure that destination has the same filetime */
586 ok(ret = GetFileTime(hfile, NULL, NULL, &ft1), "GetFileTime error %ld\n", GetLastError());
587 ok(ret = GetFileTime(hfile, NULL, NULL, &ft2), "GetFileTime error %ld\n", GetLastError());
588 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
590 SetLastError(0xdeadbeef);
591 ret = CopyFileA(source, dest, FALSE);
592 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
593 "CopyFileA: ret = %ld, unexpected error %ld\n", ret, GetLastError());
595 /* make sure that destination still has correct size */
596 ret = GetFileSize(hfile, NULL);
597 ok(ret == sizeof(prefix), "destination file has wrong size %ld\n", ret);
598 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
599 ok( retok && ret == sizeof(prefix),
600 "ReadFile: error %ld\n", GetLastError());
601 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
604 ret = DeleteFileA(source);
605 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
606 ret = DeleteFileA(dest);
607 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
610 static void test_CopyFileW(void)
612 WCHAR temp_path[MAX_PATH];
613 WCHAR source[MAX_PATH], dest[MAX_PATH];
614 static const WCHAR prefix[] = {'p','f','x',0};
617 ret = GetTempPathW(MAX_PATH, temp_path);
618 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
620 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
621 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
623 ret = GetTempFileNameW(temp_path, prefix, 0, source);
624 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
626 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
627 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
629 ret = CopyFileW(source, dest, TRUE);
630 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
631 "CopyFileW: unexpected error %ld\n", GetLastError());
633 ret = CopyFileW(source, dest, FALSE);
634 ok(ret, "CopyFileW: error %ld\n", GetLastError());
636 ret = DeleteFileW(source);
637 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
638 ret = DeleteFileW(dest);
639 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
642 static void test_CreateFileA(void)
645 char temp_path[MAX_PATH];
646 char filename[MAX_PATH];
647 static const char prefix[] = "pfx";
650 ret = GetTempPathA(MAX_PATH, temp_path);
651 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
652 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
654 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
655 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
657 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
658 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
659 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
660 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
662 ret = DeleteFileA(filename);
663 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
666 static void test_CreateFileW(void)
669 WCHAR temp_path[MAX_PATH];
670 WCHAR filename[MAX_PATH];
671 static const WCHAR emptyW[]={'\0'};
672 static const WCHAR prefix[] = {'p','f','x',0};
673 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
676 ret = GetTempPathW(MAX_PATH, temp_path);
677 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
679 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
680 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
682 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
683 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
685 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
686 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
687 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
688 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
690 ret = DeleteFileW(filename);
691 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
693 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
694 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
695 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
696 "CreateFileW(NULL) returned ret=%p error=%ld\n",hFile,GetLastError());
698 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
699 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
700 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
701 "CreateFileW(\"\") returned ret=%p error=%ld\n",hFile,GetLastError());
703 /* test the result of opening a nonexistent driver name */
704 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
705 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
706 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
707 "CreateFileW on invalid VxD name returned ret=%p error=%ld\n",hFile,GetLastError());
710 static void test_GetTempFileNameA()
714 char expected[MAX_PATH + 10];
715 char windowsdir[MAX_PATH + 10];
716 char windowsdrive[3];
718 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
719 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
720 ok(result != 0, "GetWindowsDirectory: error %ld\n", GetLastError());
722 /* If the Windows directory is the root directory, it ends in backslash, not else. */
723 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
725 strcat(windowsdir, "\\");
728 windowsdrive[0] = windowsdir[0];
729 windowsdrive[1] = windowsdir[1];
730 windowsdrive[2] = '\0';
732 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
733 ok(result != 0, "GetTempFileNameA: error %ld\n", GetLastError());
734 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
735 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
736 windowsdrive[0], out);
738 result = GetTempFileNameA(windowsdir, "abc", 2, out);
739 ok(result != 0, "GetTempFileNameA: error %ld\n", GetLastError());
741 strcat(expected, windowsdir);
742 strcat(expected, "abc2.tmp");
743 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
747 static void test_DeleteFileA( void )
751 ret = DeleteFileA(NULL);
752 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
753 GetLastError() == ERROR_PATH_NOT_FOUND),
754 "DeleteFileA(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
756 ret = DeleteFileA("");
757 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
758 GetLastError() == ERROR_BAD_PATHNAME),
759 "DeleteFileA(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
761 ret = DeleteFileA("nul");
762 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
763 GetLastError() == ERROR_INVALID_PARAMETER ||
764 GetLastError() == ERROR_ACCESS_DENIED),
765 "DeleteFileA(\"nul\") returned ret=%d error=%ld\n",ret,GetLastError());
768 static void test_DeleteFileW( void )
771 static const WCHAR emptyW[]={'\0'};
773 ret = DeleteFileW(NULL);
774 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
776 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
777 "DeleteFileW(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
779 ret = DeleteFileW(emptyW);
780 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
781 "DeleteFileW(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
784 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
786 static void test_MoveFileA(void)
788 char tempdir[MAX_PATH];
789 char source[MAX_PATH], dest[MAX_PATH];
790 static const char prefix[] = "pfx";
793 ret = GetTempPathA(MAX_PATH, tempdir);
794 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
795 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
797 ret = GetTempFileNameA(tempdir, prefix, 0, source);
798 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
800 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
801 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
803 ret = MoveFileA(source, dest);
804 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
805 "MoveFileA: unexpected error %ld\n", GetLastError());
807 ret = DeleteFileA(dest);
808 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
810 ret = MoveFileA(source, dest);
811 ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
813 lstrcatA(tempdir, "Remove Me");
814 ret = CreateDirectoryA(tempdir, NULL);
815 ok(ret == TRUE, "CreateDirectoryA failed\n");
817 lstrcpyA(source, dest);
818 lstrcpyA(dest, tempdir);
819 lstrcatA(dest, "\\wild?.*");
820 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
821 ret = MoveFileA(source, dest);
822 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
823 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
824 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
825 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
826 if (ret || (GetLastError() != ERROR_INVALID_NAME))
829 char temppath[MAX_PATH];
832 lstrcpyA(temppath, tempdir);
833 lstrcatA(temppath, "\\*.*");
834 hFind = FindFirstFileA(temppath, &fd);
835 if (INVALID_HANDLE_VALUE != hFind)
840 lpName = fd.cAlternateFileName;
842 lpName = fd.cFileName;
843 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
845 while (FindNextFileA(hFind, &fd));
849 ret = DeleteFileA(source);
850 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
851 ret = DeleteFileA(dest);
852 ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
853 ret = RemoveDirectoryA(tempdir);
854 ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
857 static void test_MoveFileW(void)
859 WCHAR temp_path[MAX_PATH];
860 WCHAR source[MAX_PATH], dest[MAX_PATH];
861 static const WCHAR prefix[] = {'p','f','x',0};
864 ret = GetTempPathW(MAX_PATH, temp_path);
865 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
867 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
868 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
870 ret = GetTempFileNameW(temp_path, prefix, 0, source);
871 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
873 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
874 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
876 ret = MoveFileW(source, dest);
877 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
878 "CopyFileW: unexpected error %ld\n", GetLastError());
880 ret = DeleteFileW(source);
881 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
882 ret = DeleteFileW(dest);
883 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
886 #define PATTERN_OFFSET 0x10
888 static void test_offset_in_overlapped_structure(void)
894 BYTE buf[256], pattern[] = "TeSt";
896 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
899 ret =GetTempPathA(MAX_PATH, temp_path);
900 ok( ret, "GetTempPathA error %ld\n", GetLastError());
901 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
902 ok( ret, "GetTempFileNameA error %ld\n", GetLastError());
904 /*** Write File *****************************************************/
906 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
907 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
909 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
910 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
911 ok( ret, "WriteFile error %ld\n", GetLastError());
912 ok(done == sizeof(buf), "expected number of bytes written %lu\n", done);
914 memset(&ov, 0, sizeof(ov));
915 ov.Offset = PATTERN_OFFSET;
917 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
918 /* Win 9x does not support the overlapped I/O on files */
919 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
920 ok(rc, "WriteFile error %ld\n", GetLastError());
921 ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
922 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
923 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
924 "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
926 ov.Offset = sizeof(buf) * 2;
928 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
929 ok( ret, "WriteFile error %ld\n", GetLastError());
930 ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
931 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
932 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
933 "expected file offset %d\n", sizeof(buf) * 2 + sizeof(pattern));
938 /*** Read File *****************************************************/
940 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
941 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
943 memset(buf, 0, sizeof(buf));
944 memset(&ov, 0, sizeof(ov));
945 ov.Offset = PATTERN_OFFSET;
947 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
948 /* Win 9x does not support the overlapped I/O on files */
949 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
950 ok(rc, "ReadFile error %ld\n", GetLastError());
951 ok(done == sizeof(pattern), "expected number of bytes read %lu\n", done);
952 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
953 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
954 "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
955 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
960 ret = DeleteFileA(temp_fname);
961 ok( ret, "DeleteFileA error %ld\n", GetLastError());
964 static void test_LockFile(void)
968 OVERLAPPED overlapped;
969 int limited_LockFile;
970 int limited_UnLockFile;
971 int lockfileex_capable;
973 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
974 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
975 CREATE_ALWAYS, 0, 0 );
976 if (handle == INVALID_HANDLE_VALUE)
978 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
981 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
983 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
984 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
986 limited_UnLockFile = 0;
987 if (UnlockFile( handle, 0, 0, 0, 0 ))
989 limited_UnLockFile = 1;
992 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
993 /* overlapping locks must fail */
994 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
995 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
996 /* non-overlapping locks must succeed */
997 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
999 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1000 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1001 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1002 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1004 overlapped.Offset = 100;
1005 overlapped.OffsetHigh = 0;
1006 overlapped.hEvent = 0;
1008 lockfileex_capable = dll_capable("kernel32", "LockFileEx");
1009 if (lockfileex_capable)
1011 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1012 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1014 /* LockFileEx is probably OK, test it more. */
1015 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1016 "LockFileEx 100,100 failed\n" );
1020 /* overlapping shared locks are OK */
1021 overlapped.Offset = 150;
1022 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1024 /* but exclusive is not */
1025 if (lockfileex_capable)
1027 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1028 0, 50, 0, &overlapped ),
1029 "LockFileEx exclusive 150,50 succeeded\n" );
1030 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1032 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1033 { /* UnLockFile is capable. */
1034 overlapped.Offset = 100;
1035 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1036 "UnlockFileEx 150,100 again succeeded\n" );
1041 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1042 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1043 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1044 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1046 /* wrap-around lock should not do anything */
1047 /* (but still succeeds on NT4 so we don't check result) */
1048 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1050 limited_LockFile = 0;
1051 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1053 limited_LockFile = 1;
1056 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1058 /* zero-byte lock */
1059 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1060 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1061 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1062 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1064 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1065 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1067 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1069 CloseHandle( handle );
1070 DeleteFileA( filename );
1073 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
1075 if (!access1 || !access2) return 1;
1076 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1077 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1078 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1079 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1083 static void test_file_sharing(void)
1085 static const DWORD access_modes[4] = { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE };
1086 static const DWORD sharing_modes[4] = { 0, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE };
1091 /* make sure the file exists */
1092 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1093 if (h == INVALID_HANDLE_VALUE)
1095 ok(0, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError());
1100 for (a1 = 0; a1 < 4; a1++)
1102 for (s1 = 0; s1 < 4; s1++)
1104 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1105 NULL, OPEN_EXISTING, 0, 0 );
1106 if (h == INVALID_HANDLE_VALUE)
1108 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
1111 for (a2 = 0; a2 < 4; a2++)
1113 for (s2 = 0; s2 < 4; s2++)
1115 SetLastError(0xdeadbeef);
1116 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1117 NULL, OPEN_EXISTING, 0, 0 );
1118 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1119 access_modes[a2], sharing_modes[s2] ))
1121 ret = GetLastError();
1122 ok( ERROR_SHARING_VIOLATION == ret || 0 == ret,
1123 "Windows 95 sets GetLastError() = ERROR_SHARING_VIOLATION and\n"
1124 " Windows XP GetLastError() = 0, but now it is %d.\n"
1125 " indexes = %d, %d, %d, %d\n"
1126 " modes =\n %lx/%lx/%lx/%lx\n",
1129 access_modes[a1], sharing_modes[s1],
1130 access_modes[a2], sharing_modes[s2]
1135 ok( h2 == INVALID_HANDLE_VALUE,
1136 "open succeeded for modes %lx/%lx/%lx/%lx\n",
1137 access_modes[a1], sharing_modes[s1],
1138 access_modes[a2], sharing_modes[s2] );
1139 if (h2 == INVALID_HANDLE_VALUE)
1140 ok( GetLastError() == ERROR_SHARING_VIOLATION,
1141 "wrong error code %ld\n", GetLastError() );
1143 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1150 SetLastError(0xdeadbeef);
1151 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1152 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError() );
1154 SetLastError(0xdeadbeef);
1155 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1156 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1157 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %ld\n", GetLastError() );
1159 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1160 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError() );
1165 DeleteFileA( filename );
1168 static char get_windows_drive()
1170 char windowsdir[MAX_PATH];
1171 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1172 return windowsdir[0];
1175 static void test_FindFirstFileA()
1178 WIN32_FIND_DATAA search_results;
1180 char buffer[5] = "C:\\";
1182 /* try FindFirstFileA on "C:\" */
1183 buffer[0] = get_windows_drive();
1184 handle = FindFirstFileA(buffer,&search_results);
1185 err = GetLastError();
1186 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on root directory should Fail\n");
1187 if (handle == INVALID_HANDLE_VALUE)
1188 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
1190 /* try FindFirstFileA on "C:\*" */
1191 strcat(buffer, "*");
1192 handle = FindFirstFileA(buffer,&search_results);
1193 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer );
1194 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1197 static void test_FindNextFileA()
1200 WIN32_FIND_DATAA search_results;
1202 char buffer[5] = "C:\\*";
1204 buffer[0] = get_windows_drive();
1205 handle = FindFirstFileA(buffer,&search_results);
1206 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1207 while (FindNextFile(handle, &search_results))
1209 /* get to the end of the files */
1211 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1212 err = GetLastError();
1213 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1216 static int test_Mapfile_createtemp(HANDLE *handle)
1218 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1219 DeleteFile(filename);
1220 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1221 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1222 if (*handle != INVALID_HANDLE_VALUE) {
1230 static void test_MapFile()
1235 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1237 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1238 ok( hmap != NULL, "mapping should work, I named it!\n" );
1240 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1242 /* We have to close file before we try new stuff with mapping again.
1243 Else we would always succeed on XP or block descriptors on 95. */
1244 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1245 ok( hmap != NULL, "We should still be able to map!\n" );
1246 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1247 ok( CloseHandle( handle ), "can't close file handle\n");
1250 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1252 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1253 ok( hmap == NULL, "mapped zero size file\n");
1254 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1256 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0, NULL );
1257 ok( hmap == NULL, "mapping should fail\n");
1258 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1260 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0x10000, NULL );
1261 ok( hmap == NULL, "mapping should fail\n");
1262 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1264 /* On XP you can now map again, on Win 95 you can not. */
1266 ok( CloseHandle( handle ), "can't close file handle\n");
1267 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1270 static void test_GetFileType(void)
1273 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1274 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1275 type = GetFileType(h);
1276 ok( type == FILE_TYPE_DISK, "expected type disk got %ld\n", type );
1278 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1279 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1280 type = GetFileType(h);
1281 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %ld\n", type );
1283 DeleteFileA( filename );
1286 static int completion_count;
1288 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1290 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1291 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1295 static void test_async_file_errors(void)
1297 char szFile[MAX_PATH];
1298 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1300 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1305 completion_count = 0;
1307 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1308 strcat(szFile, "\\win.ini");
1309 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1310 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1314 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1316 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1317 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1321 /* i/o completion routine only called if ReadFileEx returned success.
1322 * we only care about violations of this rule so undo what should have
1326 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1327 /*printf("Error = %ld\n", GetLastError());*/
1330 static void test_read_write(void)
1334 char temp_path[MAX_PATH];
1335 char filename[MAX_PATH];
1336 static const char prefix[] = "pfx";
1338 ret = GetTempPathA(MAX_PATH, temp_path);
1339 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
1340 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1342 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1343 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
1345 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1346 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1347 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %ld\n", GetLastError());
1349 SetLastError(12345678);
1351 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1352 ok(ret && GetLastError() == 12345678,
1353 "ret = %ld, error %ld\n", ret, GetLastError());
1354 ok(!bytes, "bytes = %ld\n", bytes);
1356 SetLastError(12345678);
1358 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1359 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1360 (ret && GetLastError() == 12345678), /* Win9x */
1361 "ret = %ld, error %ld\n", ret, GetLastError());
1362 ok(!bytes || /* Win2k */
1363 bytes == 10, /* Win9x */
1364 "bytes = %ld\n", bytes);
1366 SetLastError(12345678);
1368 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1369 ok(ret && GetLastError() == 12345678,
1370 "ret = %ld, error %ld\n", ret, GetLastError());
1371 ok(!bytes, "bytes = %ld\n", bytes);
1373 SetLastError(12345678);
1375 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1376 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1377 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1378 "ret = %ld, error %ld\n", ret, GetLastError());
1379 ok(!bytes, "bytes = %ld\n", bytes);
1381 ret = CloseHandle(hFile);
1382 ok( ret, "CloseHandle: error %ld\n", GetLastError());
1383 ret = DeleteFileA(filename);
1384 ok( ret, "DeleteFileA: error %ld\n", GetLastError());
1397 test_GetTempFileNameA();
1406 test_FindFirstFileA();
1407 test_FindNextFileA();
1409 test_file_sharing();
1410 test_offset_in_overlapped_structure();
1413 test_async_file_errors();